feat: websockets mtp migrate to microservices

This commit is contained in:
leandrofars 2024-03-09 12:47:16 -03:00
parent be412cdaea
commit bda64273c6
22 changed files with 9001 additions and 63 deletions

View File

@ -48,13 +48,12 @@ func StartEventsListener(ctx context.Context, js jetstream.JetStream, h handler.
switch msgType {
case "status":
h.HandleDeviceStatus(device, msg.Subject(), data)
h.HandleDeviceStatus(device, msg.Subject(), data, event, func() { msg.Ack(); log.Println("Acked msg") })
case "info":
h.HandleDeviceInfo(device, msg.Subject(), data, event)
h.HandleDeviceInfo(device, msg.Subject(), data, event, func() { msg.Ack(); log.Println("Acked msg") })
default:
//ignoreMsg(msg.Subject(), "status", msg.Data())
}
msg.Ack()
}
}()
}

View File

@ -7,12 +7,10 @@ import (
)
const (
ONLINE = iota
OFFLINE
OFFLINE = iota
ONLINE
)
const NATS_SUBJ_PREFIX = "mqtt-adapter.usp.v1."
type Handler struct {
nc *nats.Conn
js jetstream.JetStream

View File

@ -10,8 +10,9 @@ import (
"google.golang.org/protobuf/proto"
)
func (h *Handler) HandleDeviceInfo(device, subject string, data []byte, mtp string) {
log.Printf("Device %s info", device)
func (h *Handler) HandleDeviceInfo(device, subject string, data []byte, mtp string, ack func()) {
defer ack()
log.Printf("Device %s info, mtp: %s", device, mtp)
deviceInfo := parseDeviceInfoMsg(device, subject, data, getMtp(mtp))
err := h.db.CreateDevice(deviceInfo)
if err != nil {
@ -28,7 +29,7 @@ func getMtp(mtp string) db.MTP {
case nats.STOMP_STREAM_NAME:
return db.STOMP
default:
return db.MTP(0)
return db.UNDEFINED
}
}

View File

@ -10,7 +10,8 @@ import (
"google.golang.org/protobuf/proto"
)
func (h *Handler) HandleDeviceStatus(device, subject string, data []byte) {
func (h *Handler) HandleDeviceStatus(device, subject string, data []byte, mtp string, ack func()) {
defer ack()
payload, err := strconv.Atoi(string(data))
if err != nil {
log.Printf("Status subject payload message error %q", err)
@ -18,15 +19,15 @@ func (h *Handler) HandleDeviceStatus(device, subject string, data []byte) {
switch payload {
case ONLINE:
h.deviceOnline(device)
h.deviceOnline(device, mtp)
case OFFLINE:
h.deviceOffline(device)
h.deviceOffline(device, mtp)
default:
ignoreMsg(subject, "status", data)
}
}
func (h *Handler) deviceOnline(device string) {
func (h *Handler) deviceOnline(device, mtp string) {
log.Printf("Device %s is online", device)
@ -49,16 +50,18 @@ func (h *Handler) deviceOnline(device string) {
log.Fatalln("Failed to encode tr369 record:", err)
}
err = h.nc.Publish(NATS_SUBJ_PREFIX+device+".info", tr369Message)
err = h.nc.Publish(mtp+"-adapter.usp.v1."+device+".info", tr369Message)
if err != nil {
log.Printf("Failed to publish online device message: %v", err)
}
}
func (h *Handler) deviceOffline(device string) {
func (h *Handler) deviceOffline(device, mtp string) {
log.Printf("Device %s is offline", device)
err := h.db.UpdateStatus(device, db.Offline, db.MQTT)
mtpLayer := getMtp(mtp)
err := h.db.UpdateStatus(device, db.Offline, mtpLayer)
if err != nil {
log.Fatal(err)
}

View File

@ -12,12 +12,11 @@ import (
)
const (
MQTT_ADAPTER_STREAM_NAME = "mqtt-adapter"
MQTT_STREAM_NAME = "mqtt"
WS_STREAM_NAME = "ws"
STOMP_STREAM_NAME = "stomp"
LORA_STREAM_NAME = "lora"
OPC_STREAM_NAME = "opc"
MQTT_STREAM_NAME = "mqtt"
WS_STREAM_NAME = "ws"
STOMP_STREAM_NAME = "stomp"
LORA_STREAM_NAME = "lora"
OPC_STREAM_NAME = "opc"
)
func StartNatsClient(c config.Nats) (jetstream.JetStream, *nats.Conn) {
@ -29,6 +28,8 @@ func StartNatsClient(c config.Nats) (jetstream.JetStream, *nats.Conn) {
opts := defineOptions(c)
log.Printf("Connecting to NATS server %s", c.Url)
for {
nc, err = nats.Connect(c.Url, opts...)
if err != nil {

View File

@ -15,8 +15,8 @@ import (
)
const (
ONLINE = iota
OFFLINE
OFFLINE = iota
ONLINE
)
const NATS_MQTT_SUBJECT_PREFIX = "mqtt.usp.v1."

View File

@ -27,6 +27,8 @@ func StartNatsClient(c config.Nats) (
opts := defineOptions(c)
log.Printf("Connecting to NATS server %s", c.Url)
for {
nc, err = nats.Connect(c.Url, opts...)
if err != nil {
@ -42,10 +44,6 @@ func StartNatsClient(c config.Nats) (
log.Fatalf("Failed to create JetStream client: %v", err)
}
nc.Subscribe("opa.123.dae", func(m *nats.Msg) {
log.Printf("Received message on subject %s: %s", m.Subject, string(m.Data))
})
return nc, publisher(js), subscriber(nc)
}

View File

@ -42,7 +42,7 @@ func (h *MyHook) OnDisconnect(cl *mqtt.Client, err error, expire bool) {
}
if clUser != "" {
err := server.Publish("oktopus/usp/v1/status/"+clUser, []byte("1"), false, 1)
err := server.Publish("oktopus/usp/v1/status/"+clUser, []byte("0"), false, 1)
if err != nil {
log.Println("server publish error: ", err)
}
@ -62,11 +62,11 @@ func (h *MyHook) OnSubscribed(cl *mqtt.Client, pk packets.Packet, reasonCodes []
cl.Properties.Will = mqtt.Will{
Qos: 1,
TopicName: "oktopus/usp/v1/status/" + clUser,
Payload: []byte("1"),
Payload: []byte("0"),
Retain: false,
}
log.Println("new device:", clUser)
err := server.Publish("oktopus/usp/v1/status/"+clUser, []byte("0"), false, 1)
err := server.Publish("oktopus/usp/v1/status/"+clUser, []byte("1"), false, 1)
if err != nil {
log.Println("server publish error: ", err)
}

View File

View File

@ -0,0 +1 @@
- acts as a bridge between websockets server and controller

View File

@ -0,0 +1,30 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/bridge"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/config"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/nats"
)
func main() {
done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)
c := config.NewConfig()
_, publisher, subscriber := nats.StartNatsClient(c.Nats)
bridge := bridge.NewBridge(publisher, subscriber, c.Ws.Ctx, c.Ws)
bridge.StartBridge()
<-done
log.Println("websockets adapter is shutting down...")
}

View File

@ -0,0 +1,21 @@
module github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter
go 1.22.1
require (
github.com/gorilla/websocket v1.5.1
github.com/joho/godotenv v1.5.1
github.com/nats-io/nats.go v1.33.1
)
require (
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/nats-io/nkeys v0.4.7 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)

View File

@ -0,0 +1,24 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/klauspost/compress v1.17.2 h1:RlWWUY/Dr4fL8qk9YG7DTZ7PDgME2V4csBXA8L/ixi4=
github.com/klauspost/compress v1.17.2/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/nats-io/nats.go v1.33.1 h1:8TxLZZ/seeEfR97qV0/Bl939tpDnt2Z2fK3HkPypj70=
github.com/nats-io/nats.go v1.33.1/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

View File

@ -0,0 +1,196 @@
package bridge
import (
"context"
"crypto/tls"
"encoding/json"
"log"
"reflect"
"strings"
"sync"
// "reflect"
"time"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/config"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/usp/usp_record"
"github.com/gorilla/websocket"
"github.com/nats-io/nats.go"
"google.golang.org/protobuf/proto"
)
const (
NATS_WS_SUBJECT_PREFIX = "ws.usp.v1."
NATS_WS_ADAPTER_SUBJECT_PREFIX = "ws-adapter.usp.v1.*."
WS_TOPIC_PREFIX = "oktopus/usp/"
WS_CONNECTION_RETRY = 10 * time.Second
)
const (
OFFLINE = iota
ONLINE
)
type deviceStatus struct {
Eid string
Status string
}
type (
Publisher func(string, []byte) error
Subscriber func(string, func(*nats.Msg)) error
)
type Bridge struct {
Pub Publisher
Sub Subscriber
Ws config.Ws
NewDeviceQueue map[string]string
NewDevQMutex *sync.Mutex
Ctx context.Context
}
func NewBridge(p Publisher, s Subscriber, ctx context.Context, w config.Ws) *Bridge {
return &Bridge{
Pub: p,
Sub: s,
Ws: w,
Ctx: ctx,
}
}
func (b *Bridge) StartBridge() {
url := b.urlBuild()
dialer := b.newDialer()
go func(dialer websocket.Dialer) {
for {
wc, _, err := dialer.Dial(url, nil)
if err != nil {
log.Printf("Error to connect to %s, err: %s", url, err)
time.Sleep(WS_CONNECTION_RETRY)
continue
}
log.Println("Connected to WS endpoint--> ", url)
go b.subscribe(wc)
go func(wc *websocket.Conn) {
for {
msgType, wsMsg, err := wc.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) {
log.Printf("websocket error: %v", err)
b.StartBridge()
return
}
log.Println("websocket unexpected error:", err)
return
}
if msgType == websocket.TextMessage {
b.statusMsgHandler(wsMsg)
continue
}
var record usp_record.Record
err = proto.Unmarshal(wsMsg, &record)
if err != nil {
log.Println(err)
}
device := record.FromId
noSessionRecord := &usp_record.Record_NoSessionContext{
NoSessionContext: &usp_record.NoSessionContextRecord{},
}
if reflect.TypeOf(record.RecordType) == reflect.TypeOf(noSessionRecord) {
if _, ok := b.NewDeviceQueue[device]; ok {
b.newDeviceMsgHandler(wc, device, wsMsg)
continue
}
}
// log.Println("Handle api request")
// var msg usp_msg.Msg
// err = proto.Unmarshal(record.GetNoSessionContext().Payload, &msg)
// if err != nil {
// log.Println(err)
// continue
// }
// if _, ok := w.MsgQueue[msg.Header.MsgId]; ok {
// //m.QMutex.Lock()
// w.MsgQueue[msg.Header.MsgId] <- msg
// //m.QMutex.Unlock()
// } else {
// log.Printf("Message answer to request %s arrived too late", msg.Header.MsgId)
// }
}
}(wc)
break
}
}(dialer)
}
func (b *Bridge) subscribe(wc *websocket.Conn) {
b.NewDeviceQueue = make(map[string]string)
b.NewDevQMutex = &sync.Mutex{}
b.Sub(NATS_WS_ADAPTER_SUBJECT_PREFIX+"info", func(msg *nats.Msg) {
log.Printf("Received message on info subject")
subj := strings.Split(msg.Subject, ".")
device := subj[len(subj)-2]
b.NewDevQMutex.Lock()
b.NewDeviceQueue[device] = ""
b.NewDevQMutex.Unlock()
err := wc.WriteMessage(websocket.BinaryMessage, msg.Data)
if err != nil {
log.Printf("send websocket msg error: %q", err)
return
}
})
}
func (b *Bridge) newDeviceMsgHandler(wc *websocket.Conn, device string, msg []byte) {
log.Printf("New device %s response", device)
b.Pub(NATS_WS_SUBJECT_PREFIX+device+".info", msg)
b.NewDevQMutex.Lock()
delete(b.NewDeviceQueue, device)
b.NewDevQMutex.Unlock()
}
func (b *Bridge) statusMsgHandler(wsMsg []byte) {
var deviceStatus deviceStatus
err := json.Unmarshal(wsMsg, &deviceStatus)
if err != nil {
log.Println("Websockets Text Message is not about devices status")
return
}
b.Pub(NATS_WS_SUBJECT_PREFIX+deviceStatus.Eid+".status", []byte(deviceStatus.Status))
}
func (b *Bridge) urlBuild() string {
prefix := "ws://"
if b.Ws.TlsEnable {
prefix = "wss://"
}
wsUrl := prefix + b.Ws.Addr + b.Ws.Port + b.Ws.Route
if b.Ws.AuthEnable {
wsUrl = wsUrl + "?token=" + b.Ws.Token
}
return wsUrl
}
func (b *Bridge) newDialer() websocket.Dialer {
return websocket.Dialer{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: b.Ws.SkipTlsVerify,
},
}
}

View File

@ -0,0 +1,114 @@
package config
import (
"context"
"flag"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
)
const LOCAL_ENV = ".env.local"
type Nats struct {
Url string
Name string
VerifyCertificates bool
Ctx context.Context
}
type Ws struct {
Token string
AuthEnable bool
Addr string
Port string
Route string
TlsEnable bool
SkipTlsVerify bool
Ctx context.Context
}
type Config struct {
Nats Nats
Ws Ws
}
func NewConfig() *Config {
loadEnvVariables()
log.SetFlags(log.LstdFlags | log.Lshortfile)
natsUrl := flag.String("nats_url", lookupEnvOrString("NATS_URL", "nats://localhost:4222"), "url for nats server")
natsName := flag.String("nats_name", lookupEnvOrString("NATS_NAME", "ws-adapter"), "name for nats client")
natsVerifyCertificates := flag.Bool("nats_verify_certificates", lookupEnvOrBool("NATS_VERIFY_CERTIFICATES", false), "verify validity of certificates from nats server")
wsToken := flag.String("ws_token", lookupEnvOrString("WS_TOKEN", ""), "websocket server auth token (if authentication is enabled)")
wsAuthEnable := flag.Bool("ws_auth_enable", lookupEnvOrBool("WS_AUTH_ENABLE", false), "enable authentication for websocket server")
wsAddr := flag.String("ws_addr", lookupEnvOrString("WS_ADDR", "localhost"), "websocket server address (domain or ip)")
wsPort := flag.String("ws_port", lookupEnvOrString("WS_PORT", ":8080"), "websocket server port")
wsRoute := flag.String("ws_route", lookupEnvOrString("WS_ROUTE", "/ws/controller"), "websocket server route")
wsTlsEnable := flag.Bool("ws_tls_enable", lookupEnvOrBool("WS_TLS_ENABLE", false), "access websocket via tls protocol (wss)")
wsSkipTlsVerify := flag.Bool("ws_skip_tls_verify", lookupEnvOrBool("WS_SKIP_TLS_VERIFY", false), "skip tls verification for websocket server")
flHelp := flag.Bool("help", false, "Help")
flag.Parse()
if *flHelp {
flag.Usage()
os.Exit(0)
}
ctx := context.TODO()
return &Config{
Nats: Nats{
Url: *natsUrl,
Name: *natsName,
VerifyCertificates: *natsVerifyCertificates,
Ctx: ctx,
},
Ws: Ws{
Token: *wsToken,
AuthEnable: *wsAuthEnable,
Addr: *wsAddr,
Port: *wsPort,
Route: *wsRoute,
TlsEnable: *wsTlsEnable,
SkipTlsVerify: *wsSkipTlsVerify,
Ctx: ctx,
},
}
}
func loadEnvVariables() {
err := godotenv.Load()
if _, err := os.Stat(LOCAL_ENV); err == nil {
_ = godotenv.Overload(LOCAL_ENV)
log.Printf("Loaded variables from '%s'", LOCAL_ENV)
}
if err != nil {
log.Println("Error to load environment variables:", err)
} else {
log.Println("Loaded variables from '.env'")
}
}
func lookupEnvOrString(key string, defaultVal string) string {
if val, _ := os.LookupEnv(key); val != "" {
return val
}
return defaultVal
}
func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, _ := os.LookupEnv(key); val != "" {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("LookupEnvOrBool[%s]: %v", key, err)
}
return v
}
return defaultVal
}

View File

@ -0,0 +1,89 @@
package nats
import (
"log"
"time"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/config"
"github.com/nats-io/nats.go"
"github.com/nats-io/nats.go/jetstream"
)
const (
STREAM_NAME = "ws"
)
func StartNatsClient(c config.Nats) (
*nats.Conn,
func(string, []byte) error,
func(string, func(*nats.Msg)) error,
) {
var (
nc *nats.Conn
err error
)
opts := defineOptions(c)
log.Printf("Connecting to NATS server %s", c.Url)
for {
nc, err = nats.Connect(c.Url, opts...)
if err != nil {
time.Sleep(5 * time.Second)
continue
}
break
}
log.Printf("Successfully connected to NATS server %s", c.Url)
js, err := jetstream.New(nc)
if err != nil {
log.Fatalf("Failed to create JetStream client: %v", err)
}
return nc, publisher(js), subscriber(nc)
}
func subscriber(nc *nats.Conn) func(string, func(*nats.Msg)) error {
return func(subject string, handler func(*nats.Msg)) error {
_, err := nc.Subscribe(subject, handler)
if err != nil {
log.Printf("error to subscribe to subject %s error: %q", subject, err)
}
return err
}
}
func publisher(js jetstream.JetStream) func(string, []byte) error {
return func(subject string, payload []byte) error {
_, err := js.PublishAsync(subject, payload)
if err != nil {
log.Printf("error to send jetstream message: %q", err)
}
return err
}
}
func defineOptions(c config.Nats) []nats.Option {
var opts []nats.Option
opts = append(opts, nats.Name(c.Name))
opts = append(opts, nats.MaxReconnects(-1))
opts = append(opts, nats.ReconnectWait(5*time.Second))
opts = append(opts, nats.DisconnectErrHandler(func(nc *nats.Conn, err error) {
log.Printf("Got disconnected! Reason: %q\n", err)
}))
opts = append(opts, nats.ReconnectHandler(func(nc *nats.Conn) {
log.Printf("Got reconnected to %v!\n", nc.ConnectedUrl())
}))
opts = append(opts, nats.ClosedHandler(func(nc *nats.Conn) {
log.Printf("Connection closed. Reason: %q\n", nc.LastError())
}))
if c.VerifyCertificates {
opts = append(opts, nats.RootCAs())
}
return opts
}

View File

@ -0,0 +1,149 @@
package usp
import (
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/usp/usp_msg"
"github.com/OktopUSP/oktopus/backend/services/mtp/ws-adapter/internal/usp/usp_record"
"github.com/google/uuid"
)
const VERSION = "1.0"
func NewUspRecord(p []byte, toId, fromId string) usp_record.Record {
return usp_record.Record{
Version: VERSION,
ToId: toId,
FromId: fromId,
PayloadSecurity: usp_record.Record_PLAINTEXT,
RecordType: &usp_record.Record_NoSessionContext{
NoSessionContext: &usp_record.NoSessionContextRecord{
Payload: p,
},
},
}
}
func NewCreateMsg(createStuff usp_msg.Add) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_ADD,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_Add{
Add: &createStuff,
},
},
},
},
}
}
func NewGetMsg(getStuff usp_msg.Get) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_GET,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_Get{
Get: &getStuff,
},
},
},
},
}
}
func NewDelMsg(getStuff usp_msg.Delete) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_DELETE,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_Delete{
Delete: &getStuff,
},
},
},
},
}
}
func NewSetMsg(updateStuff usp_msg.Set) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_SET,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_Set{
Set: &updateStuff,
},
},
},
},
}
}
func NewGetSupportedParametersMsg(getStuff usp_msg.GetSupportedDM) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_GET_SUPPORTED_DM,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_GetSupportedDm{
GetSupportedDm: &getStuff,
},
},
},
},
}
}
func NewGetParametersInstancesMsg(getStuff usp_msg.GetInstances) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_GET_INSTANCES,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_GetInstances{
GetInstances: &getStuff,
},
},
},
},
}
}
func NewOperateMsg(getStuff usp_msg.Operate) usp_msg.Msg {
return usp_msg.Msg{
Header: &usp_msg.Header{
MsgId: uuid.NewString(),
MsgType: usp_msg.Header_OPERATE,
},
Body: &usp_msg.Body{
MsgBody: &usp_msg.Body_Request{
Request: &usp_msg.Request{
ReqType: &usp_msg.Request_Operate{
Operate: &getStuff,
},
},
},
},
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,530 @@
syntax = "proto3";
//**************************************************************************
// TR-369 USP Message Protocol Buffer Schema
//
// Copyright (c) 2017-2018, Broadband Forum
//
// The undersigned members have elected to grant the copyright to
// their contributed material used in this software:
// Copyright (c) 2017-2018 ARRIS Enterprises, LLC.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products
// derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The above license is used as a license under copyright only.
// Please reference the Forum IPR Policy for patent licensing terms
// <https://www.broadband-forum.org/ipr-policy>.
//
// Any moral rights which are necessary to exercise under the above
// license grant are also deemed granted under this license.
//
// | Version | Name | Date |
// | TR-369 1.0.0 | User Services Platform | APR, 2018 |
// | TR-369 1.0.1 | User Services Platform | JUN, 2018 |
// | TR-369 1.0.2 | User Services Platform | OCT, 2018 |
// | TR-369 1.1 | User Services Platform | SEP, 2019 |
//
// BBF software release registry: http://www.broadband-forum.org/software
//**************************************************************************
package usp;
option go_package="./usp-msg";
message Msg {
Header header = 1; // Make required in the protocol
Body body = 2; // Make required in the protocol
}
message Header {
string msg_id = 1; // Make required in the protocol
MsgType msg_type = 2; // Make required in the protocol
enum MsgType {
ERROR = 0;
GET = 1;
GET_RESP = 2;
NOTIFY = 3;
SET = 4;
SET_RESP = 5;
OPERATE = 6;
OPERATE_RESP = 7;
ADD = 8;
ADD_RESP = 9;
DELETE = 10;
DELETE_RESP = 11;
GET_SUPPORTED_DM = 12;
GET_SUPPORTED_DM_RESP = 13;
GET_INSTANCES = 14;
GET_INSTANCES_RESP = 15;
NOTIFY_RESP = 16;
GET_SUPPORTED_PROTO = 17;
GET_SUPPORTED_PROTO_RESP = 18;
}
}
message Body {
oneof msg_body {
Request request = 1;
Response response = 2;
Error error = 3;
}
}
message Request {
oneof req_type {
Get get = 1;
GetSupportedDM get_supported_dm = 2;
GetInstances get_instances = 3;
Set set = 4;
Add add = 5;
Delete delete = 6;
Operate operate = 7;
Notify notify = 8;
GetSupportedProtocol get_supported_protocol = 9;
}
}
message Response {
oneof resp_type {
GetResp get_resp = 1;
GetSupportedDMResp get_supported_dm_resp = 2;
GetInstancesResp get_instances_resp = 3;
SetResp set_resp = 4;
AddResp add_resp = 5;
DeleteResp delete_resp = 6;
OperateResp operate_resp = 7;
NotifyResp notify_resp = 8;
GetSupportedProtocolResp get_supported_protocol_resp = 9;
}
}
message Error {
fixed32 err_code = 1;
string err_msg = 2;
repeated ParamError param_errs = 3;
message ParamError {
string param_path = 1;
fixed32 err_code = 2;
string err_msg = 3;
}
}
message Get {
repeated string param_paths = 1;
fixed32 max_depth = 2;
}
message GetResp {
repeated RequestedPathResult req_path_results = 1;
message RequestedPathResult {
string requested_path = 1;
fixed32 err_code = 2;
string err_msg = 3;
repeated ResolvedPathResult resolved_path_results = 4;
}
message ResolvedPathResult {
string resolved_path = 1;
map<string, string> result_params = 2;
}
}
message GetSupportedDM {
repeated string obj_paths = 1;
bool first_level_only = 2;
bool return_commands = 3;
bool return_events = 4;
bool return_params = 5;
}
message GetSupportedDMResp {
repeated RequestedObjectResult req_obj_results = 1;
message RequestedObjectResult {
string req_obj_path = 1;
fixed32 err_code = 2;
string err_msg = 3;
string data_model_inst_uri = 4;
repeated SupportedObjectResult supported_objs = 5;
}
message SupportedObjectResult {
string supported_obj_path = 1;
ObjAccessType access = 2;
bool is_multi_instance = 3;
repeated SupportedCommandResult supported_commands = 4;
repeated SupportedEventResult supported_events = 5;
repeated SupportedParamResult supported_params = 6;
repeated string divergent_paths = 7;
}
message SupportedParamResult {
string param_name = 1;
ParamAccessType access = 2;
ParamValueType value_type = 3;
ValueChangeType value_change = 4;
}
message SupportedCommandResult {
string command_name = 1;
repeated string input_arg_names = 2;
repeated string output_arg_names = 3;
CmdType command_type = 4;
}
message SupportedEventResult {
string event_name = 1;
repeated string arg_names = 2;
}
enum ParamAccessType {
PARAM_READ_ONLY = 0;
PARAM_READ_WRITE = 1;
PARAM_WRITE_ONLY = 2;
}
enum ObjAccessType {
OBJ_READ_ONLY = 0;
OBJ_ADD_DELETE = 1;
OBJ_ADD_ONLY = 2;
OBJ_DELETE_ONLY = 3;
}
enum ParamValueType {
PARAM_UNKNOWN = 0;
PARAM_BASE_64 = 1;
PARAM_BOOLEAN = 2;
PARAM_DATE_TIME = 3;
PARAM_DECIMAL = 4;
PARAM_HEX_BINARY = 5;
PARAM_INT = 6;
PARAM_LONG = 7;
PARAM_STRING = 8;
PARAM_UNSIGNED_INT = 9;
PARAM_UNSIGNED_LONG = 10;
}
enum ValueChangeType {
VALUE_CHANGE_UNKNOWN = 0;
VALUE_CHANGE_ALLOWED = 1;
VALUE_CHANGE_WILL_IGNORE = 2;
}
enum CmdType {
CMD_UNKNOWN = 0;
CMD_SYNC = 1;
CMD_ASYNC = 2;
}
}
message GetInstances {
repeated string obj_paths = 1;
bool first_level_only = 2;
}
message GetInstancesResp {
repeated RequestedPathResult req_path_results = 1;
message RequestedPathResult {
string requested_path = 1;
fixed32 err_code = 2;
string err_msg = 3;
repeated CurrInstance curr_insts = 4;
}
message CurrInstance {
string instantiated_obj_path = 1;
map<string, string> unique_keys = 2;
}
}
message GetSupportedProtocol {
string controller_supported_protocol_versions = 1;
}
message GetSupportedProtocolResp {
string agent_supported_protocol_versions = 1;
}
message Add {
bool allow_partial = 1;
repeated CreateObject create_objs = 2;
message CreateObject {
string obj_path = 1;
repeated CreateParamSetting param_settings = 2;
}
message CreateParamSetting {
string param = 1;
string value = 2;
bool required = 3;
}
}
message AddResp {
repeated CreatedObjectResult created_obj_results = 1;
message CreatedObjectResult {
string requested_path = 1;
OperationStatus oper_status = 2;
message OperationStatus {
oneof oper_status {
OperationFailure oper_failure = 1;
OperationSuccess oper_success = 2;
}
message OperationFailure {
fixed32 err_code = 1;
string err_msg = 2;
}
message OperationSuccess {
string instantiated_path = 1;
repeated ParameterError param_errs = 2;
map<string, string> unique_keys = 3;
}
}
}
message ParameterError {
string param = 1;
fixed32 err_code = 2;
string err_msg = 3;
}
}
message Delete {
bool allow_partial = 1;
repeated string obj_paths = 2;
}
message DeleteResp {
repeated DeletedObjectResult deleted_obj_results = 1;
message DeletedObjectResult {
string requested_path = 1;
OperationStatus oper_status = 2;
message OperationStatus {
oneof oper_status {
OperationFailure oper_failure = 1;
OperationSuccess oper_success = 2;
}
message OperationFailure {
fixed32 err_code = 1;
string err_msg = 2;
}
message OperationSuccess {
repeated string affected_paths = 1;
repeated UnaffectedPathError unaffected_path_errs = 2;
}
}
}
message UnaffectedPathError {
string unaffected_path = 1;
fixed32 err_code = 2;
string err_msg = 3;
}
}
message Set {
bool allow_partial = 1;
repeated UpdateObject update_objs = 2;
message UpdateObject {
string obj_path = 1;
repeated UpdateParamSetting param_settings = 2;
}
message UpdateParamSetting {
string param = 1;
string value = 2;
bool required = 3;
}
}
message SetResp {
repeated UpdatedObjectResult updated_obj_results = 1;
message UpdatedObjectResult {
string requested_path = 1;
OperationStatus oper_status = 2;
message OperationStatus {
oneof oper_status {
OperationFailure oper_failure = 1;
OperationSuccess oper_success = 2;
}
message OperationFailure {
fixed32 err_code = 1;
string err_msg = 2;
repeated UpdatedInstanceFailure updated_inst_failures = 3;
}
message OperationSuccess {
repeated UpdatedInstanceResult updated_inst_results = 1;
}
}
}
message UpdatedInstanceFailure {
string affected_path = 1;
repeated ParameterError param_errs = 2;
}
message UpdatedInstanceResult {
string affected_path = 1;
repeated ParameterError param_errs = 2;
map<string, string> updated_params = 3;
}
message ParameterError {
string param = 1;
fixed32 err_code = 2;
string err_msg = 3;
}
}
message Operate {
string command = 1;
string command_key = 2;
bool send_resp = 3;
map<string, string> input_args = 4;
}
message OperateResp {
repeated OperationResult operation_results = 1;
message OperationResult {
string executed_command = 1;
oneof operation_resp {
string req_obj_path = 2;
OutputArgs req_output_args = 3;
CommandFailure cmd_failure = 4;
}
message OutputArgs {
map<string, string> output_args = 1;
}
message CommandFailure {
fixed32 err_code = 1;
string err_msg = 2;
}
}
}
message Notify {
string subscription_id = 1;
bool send_resp = 2;
oneof notification {
Event event = 3;
ValueChange value_change = 4;
ObjectCreation obj_creation = 5;
ObjectDeletion obj_deletion = 6;
OperationComplete oper_complete = 7;
OnBoardRequest on_board_req = 8;
}
message Event {
string obj_path = 1;
string event_name = 2;
map<string, string> params = 3;
}
message ValueChange {
string param_path = 1;
string param_value = 2;
}
message ObjectCreation {
string obj_path = 1;
map<string, string> unique_keys = 2;
}
message ObjectDeletion {
string obj_path = 1;
}
message OperationComplete {
string obj_path = 1;
string command_name = 2;
string command_key = 3;
oneof operation_resp {
OutputArgs req_output_args = 4;
CommandFailure cmd_failure = 5;
}
message OutputArgs {
map<string, string> output_args = 1;
}
message CommandFailure {
fixed32 err_code = 1;
string err_msg = 2;
}
}
message OnBoardRequest {
string oui = 1;
string product_class = 2;
string serial_number = 3;
string agent_supported_protocol_versions = 4;
}
}
message NotifyResp {
string subscription_id = 1;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,134 @@
syntax = "proto3";
//**************************************************************************
// TR-369 USP Record Protocol Buffer Schema
//
// Copyright (c) 2017-2018, Broadband Forum
//
// The undersigned members have elected to grant the copyright to
// their contributed material used in this software:
// Copyright (c) 2017-2018 ARRIS Enterprises, LLC.
//
// Redistribution and use in source and binary forms, with or
// without modification, are permitted provided that the following
// conditions are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products
// derived from this software without specific prior written
// permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
// CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The above license is used as a license under copyright only.
// Please reference the Forum IPR Policy for patent licensing terms
// <https://www.broadband-forum.org/ipr-policy>.
//
// Any moral rights which are necessary to exercise under the above
// license grant are also deemed granted under this license.
//
//
// | Version | Name | Date |
// | TR-369 1.0.0 | User Services Platform | APR, 2018 |
// | TR-369 1.0.1 | User Services Platform | JUN, 2018 |
// | TR-369 1.0.2 | User Services Platform | OCT, 2018 |
// | TR-369 1.1 | User Services Platform | SEP, 2019 |
//
// BBF software release registry: http://www.broadband-forum.org/software
//**************************************************************************
package usp_record;
option go_package="./usp-record";
message Record {
string version = 1;
string to_id = 2;
string from_id = 3;
PayloadSecurity payload_security = 4;
bytes mac_signature = 5; //MAC or Signature
bytes sender_cert = 6;
oneof record_type {
NoSessionContextRecord no_session_context = 7;
SessionContextRecord session_context = 8;
WebSocketConnectRecord websocket_connect = 9;
MQTTConnectRecord mqtt_connect = 10;
STOMPConnectRecord stomp_connect = 11;
DisconnectRecord disconnect = 12;
}
enum PayloadSecurity {
PLAINTEXT = 0;
TLS12 = 1;
}
}
message NoSessionContextRecord {
bytes payload = 2;
}
message SessionContextRecord {
uint64 session_id = 1;
uint64 sequence_id = 2;
uint64 expected_id = 3;
uint64 retransmit_id = 4;
PayloadSARState payload_sar_state = 5;
PayloadSARState payloadrec_sar_state = 6;
repeated bytes payload = 7;
enum PayloadSARState {
NONE = 0; //No segmentation
BEGIN = 1; //Begin segmentation
INPROCESS = 2; //Segmentation in process
COMPLETE = 3; //Segmentation is complete
}
}
message WebSocketConnectRecord {
// An empty message
}
message MQTTConnectRecord {
MQTTVersion version = 1;
string subscribed_topic = 2;
enum MQTTVersion {
V3_1_1 = 0; // Represents MQTT v3.1.1, a.k.a. v4 in the MQTT Spec
V5 = 1;
}
}
message STOMPConnectRecord {
STOMPVersion version = 1;
string subscribed_destination = 2;
enum STOMPVersion {
V1_2 = 0;
}
}
message DisconnectRecord {
string reason = 1;
fixed32 reason_code = 2;
}

View File

@ -1,31 +0,0 @@
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/OktopUSP/oktopus/ws/internal/config"
"github.com/OktopUSP/oktopus/ws/internal/ws"
)
// TODO: refact from where this version is get
const VERSION = "0.0.1"
func main() {
done := make(chan os.Signal, 1)
conf := config.NewConfig()
// Locks app running until it receives a stop command as Ctrl+C.
signal.Notify(done, syscall.SIGINT)
log.Println("Starting Oktopus Websockets Version:", VERSION)
ws.StartNewServer(conf)
<-done
log.Println("(⌐■_■) Websockets server is out!")
}