Add version v0.1.0
[ric-plt/rtmgr.git] / pkg / sbi / sbi.go
1 /*
2 w
3 ==================================================================================
4   Copyright (c) 2019 AT&T Intellectual Property.
5   Copyright (c) 2019 Nokia
6
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at
10
11        http://www.apache.org/licenses/LICENSE-2.0
12
13    Unless required by applicable law or agreed to in writing, software
14    distributed under the License is distributed on an "AS IS" BASIS,
15    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16    See the License for the specific language governing permissions and
17    limitations under the License.
18 ==================================================================================
19 */
20 /*
21   Mnemonic:     sbi.go
22   Abstract:     Contains SBI (SouthBound Interface) module definitions and generic SBI components
23   Date:         16 March 2019
24 */
25
26 package sbi
27
28 import (
29         "errors"
30         "fmt"
31         "rtmgr"
32         "strconv"
33 )
34
35 const DEFAULT_NNG_PUBSUB_SOCKET_PREFIX = "tcp://"
36 const DEFAULT_NNG_PUBSUB_SOCKET_NUMBER = 4560
37 const DEFAULT_NNG_PIPELINE_SOCKET_PREFIX = "tcp://"
38 const DEFAULT_NNG_PIPELINE_SOCKET_NUMBER = 4561
39
40 var (
41         SupportedSbis = []*SbiEngineConfig{
42                 &SbiEngineConfig{
43                         SbiEngine{
44                                 Name:     "nngpub",
45                                 Version:  "v1",
46                                 Protocol: "nngpubsub",
47                         },
48                         openSocket(openNngPub),
49                         closeSocket(closeNngPub),
50                         createEndpointSocket(createNngPubEndpointSocket),
51                         destroyEndpointSocket(createNngPubEndpointSocket),
52                         distributeAll(publishAll),
53                         true,
54                 },
55                 &SbiEngineConfig{
56                         SbiEngine{
57                                 Name:     "nngpush",
58                                 Version:  "v1",
59                                 Protocol: "nngpipeline",
60                         },
61                         openSocket(openNngPush),
62                         closeSocket(closeNngPush),
63                         createEndpointSocket(createNngPushEndpointSocket),
64                         destroyEndpointSocket(destroyNngPushEndpointSocket),
65                         distributeAll(pushAll),
66                         true,
67                 },
68         }
69 )
70
71 func ListSbis() {
72         fmt.Printf("SBI:\n")
73         for _, sbi := range SupportedSbis {
74                 if sbi.IsAvailable {
75                         rtmgr.Logger.Info(sbi.Engine.Name + "/" + sbi.Engine.Version)
76                 }
77         }
78 }
79
80 func GetSbi(sbiName string) (*SbiEngineConfig, error) {
81         for _, sbi := range SupportedSbis {
82                 if (*sbi).Engine.Name == sbiName && (*sbi).IsAvailable {
83                         return sbi, nil
84                 }
85         }
86         return nil, errors.New("SBI:" + sbiName + " is not supported or still not available")
87 }
88
89 func pruneEndpointList(sbii *SbiEngineConfig) {
90         for _, ep := range rtmgr.Eps {
91                 if !ep.Keepalive {
92                         sbii.DestroyEndpointSocket(ep)
93                         delete(rtmgr.Eps, ep.Uuid)
94                 } else {
95                         rtmgr.Eps[ep.Uuid].Keepalive = false
96                 }
97         }
98 }
99
100 func UpdateEndpointList(xapps *[]rtmgr.XApp, sbii *SbiEngineConfig) {
101         for _, xapp := range *xapps {
102                 for _, instance := range xapp.Instances {
103                         uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
104                         if _, ok := rtmgr.Eps[uuid]; ok {
105                                 rtmgr.Eps[uuid].Keepalive = true
106                         } else {
107                                 ep := &rtmgr.Endpoint{
108                                         uuid,
109                                         instance.Name,
110                                         xapp.Name,
111                                         instance.Ip,
112                                         instance.Port,
113                                         instance.TxMessages,
114                                         instance.RxMessages,
115                                         nil,
116                                         false,
117                                         true,
118                                 }
119                                 if err := sbii.CreateEndpointSocket(ep); err != nil {
120                                         rtmgr.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
121                                         continue
122                                 }
123                                 rtmgr.Eps[uuid] = ep
124                         }
125                 }
126         }
127         pruneEndpointList(sbii)
128 }