Release of Routing Manager v0.3.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         "strconv"
31         "routing-manager/pkg/rtmgr"
32 )
33
34 const DEFAULT_NNG_PUBSUB_SOCKET_PREFIX = "tcp://"
35 const DEFAULT_NNG_PUBSUB_SOCKET_NUMBER = 4560
36 const DEFAULT_NNG_PIPELINE_SOCKET_PREFIX = "tcp://"
37 const DEFAULT_NNG_PIPELINE_SOCKET_NUMBER = 4561
38 const PLATFORMTYPE = "platformcomponenttype"
39
40 var (
41         SupportedSbis = []*SbiEngineConfig{
42                 &SbiEngineConfig{
43                         Name:     "nngpush",
44                         Version:  "v1",
45                         Protocol: "nngpipeline",
46                         Instance: NewNngPush(),
47                         IsAvailable: true,
48                 },
49                 &SbiEngineConfig{
50                         Name:     "nngpub",
51                         Version:  "v1",
52                         Protocol: "nngpubsub",
53                         Instance: NewNngPub(),
54                         IsAvailable: true,
55                 },
56         }
57 )
58
59 func GetSbi(sbiName string) (SbiEngine, error) {
60         for _, sbi := range SupportedSbis {
61                 if sbi.Name == sbiName && sbi.IsAvailable {
62                         return sbi.Instance, nil
63                 }
64         }
65         return nil, errors.New("SBI:" + sbiName + " is not supported or still not available")
66 }
67
68 type Sbi struct {
69
70 }
71
72 func (s *Sbi) pruneEndpointList(sbii SbiEngine) {
73         for _, ep := range rtmgr.Eps {
74                 if !ep.Keepalive {
75                         rtmgr.Logger.Debug("deleting %v",ep)
76                         sbii.DeleteEndpoint(ep)
77                         delete(rtmgr.Eps, ep.Uuid)
78                 } else {
79                         rtmgr.Eps[ep.Uuid].Keepalive = false
80                 }
81         }
82 }
83
84 func (s *Sbi) updateEndpoints(rcs *rtmgr.RicComponents, sbii SbiEngine) {
85         for _, xapp := range (*rcs).Xapps {
86                 for _, instance := range xapp.Instances {
87                         uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
88                         if _, ok := rtmgr.Eps[uuid]; ok {
89                                 rtmgr.Eps[uuid].Keepalive = true
90                         } else {
91                                 ep := &rtmgr.Endpoint{
92                                         uuid,
93                                         instance.Name,
94                                         xapp.Name,
95                                         instance.Ip,
96                                         instance.Port,
97                                         instance.TxMessages,
98                                         instance.RxMessages,
99                                         nil,
100                                         false,
101                                         true,
102                                 }
103                                 if err := sbii.AddEndpoint(ep); err != nil {
104                                         rtmgr.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
105                                         continue
106                                 }
107                                 rtmgr.Eps[uuid] = ep
108                         }
109                 }
110         }
111         s.updatePlatformEndpoints(&((*rcs).Pcs), sbii)
112         s.pruneEndpointList(sbii)
113 }
114
115 func (s *Sbi ) updatePlatformEndpoints(pcs *rtmgr.PlatformComponents, sbii SbiEngine) {
116         rtmgr.Logger.Debug("updatePlatformEndpoints invoked. PCS: %v", *pcs)
117         for _, pc := range *pcs {
118                 uuid := pc.Fqdn + ":" + strconv.Itoa(int(pc.Port))
119                 if _, ok := rtmgr.Eps[uuid]; ok {
120                         rtmgr.Eps[uuid].Keepalive = true
121                 } else {
122                         ep := &rtmgr.Endpoint{
123                                 uuid,
124                                 pc.Name,
125                                 PLATFORMTYPE,
126                                 pc.Fqdn,
127                                 pc.Port,
128                                 rtmgr.PLATFORMMESSAGETYPES[pc.Name]["tx"],
129                                 rtmgr.PLATFORMMESSAGETYPES[pc.Name]["rx"],
130                                 nil,
131                                 false,
132                                 true,
133                         }
134                         rtmgr.Logger.Debug("ep created: %v",ep)
135                         if err := sbii.AddEndpoint(ep); err != nil {
136                                 rtmgr.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
137                                 continue
138                         }
139                         rtmgr.Eps[uuid] = ep
140                 }
141         }
142 }
143