58fe7d8e64b94046071652f78ca2ec8033948c8e
[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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22 ==================================================================================
23 */
24 /*
25   Mnemonic:     sbi.go
26   Abstract:     Contains SBI (SouthBound Interface) module definitions and generic SBI components
27   Date:         16 March 2019
28 */
29
30 package sbi
31
32 import (
33         "errors"
34         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
35         "routing-manager/pkg/rtmgr"
36         "strconv"
37         "strings"
38 )
39
40 const DefaultNngPipelineSocketPrefix = "tcp://"
41 const DefaultNngPipelineSocketNumber = 4561
42 const PlatformType = "platform"
43
44 var (
45         SupportedSbis = []*EngineConfig{
46                 {
47                         Name:        "nngpush",
48                         Version:     "v1",
49                         Protocol:    "nngpipeline",
50                         Instance:    NewNngPush(),
51                         IsAvailable: true,
52                 },
53         }
54 )
55
56 func GetSbi(sbiName string) (Engine, error) {
57         for _, sbi := range SupportedSbis {
58                 if sbi.Name == sbiName && sbi.IsAvailable {
59                         return sbi.Instance, nil
60                 }
61         }
62         return nil, errors.New("SBI:" + sbiName + " is not supported or still not available")
63 }
64
65 type Sbi struct {
66 }
67
68 func (s *Sbi) pruneEndpointList(sbi Engine) {
69         xapp.Logger.Debug("pruneEndpointList invoked.")
70         for _, ep := range rtmgr.Eps {
71                 if !ep.Keepalive {
72                         xapp.Logger.Debug("deleting %v", ep)
73                         sbi.DeleteEndpoint(ep)
74                         delete(rtmgr.Eps, ep.Uuid)
75                 } else {
76                         rtmgr.Eps[ep.Uuid].Keepalive = false
77                 }
78         }
79 }
80
81 func (s *Sbi) updateEndpoints(rcs *rtmgr.RicComponents, sbi Engine) {
82         for _, xapps := range (*rcs).XApps {
83                 for _, instance := range xapps.Instances {
84                         uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
85                         if _, ok := rtmgr.Eps[uuid]; ok {
86                                 rtmgr.Eps[uuid].Keepalive = true
87                         } else {
88                                 ep := &rtmgr.Endpoint{
89                                         Uuid:       uuid,
90                                         Name:       instance.Name,
91                                         XAppType:   xapps.Name,
92                                         Ip:         instance.Ip,
93                                         Port:       instance.Port,
94                                         TxMessages: instance.TxMessages,
95                                         RxMessages: instance.RxMessages,
96                                         Policies:   instance.Policies,
97                                         Socket:     nil,
98                                         IsReady:    false,
99                                         Keepalive:  true,
100                                 }
101                                 if err := sbi.AddEndpoint(ep); err != nil {
102                                         xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
103                                         continue
104                                 }
105                                 rtmgr.Eps[uuid] = ep
106                         }
107                 }
108         }
109         s.updatePlatformEndpoints(&((*rcs).Pcs), sbi)
110         s.updateE2TEndpoints(&((*rcs).E2Ts), sbi)
111         s.pruneEndpointList(sbi)
112 }
113
114 func (s *Sbi) updatePlatformEndpoints(pcs *rtmgr.PlatformComponents, sbi Engine) {
115         xapp.Logger.Debug("updatePlatformEndpoints invoked. PCS: %v", *pcs)
116         for _, pc := range *pcs {
117                 uuid := pc.Fqdn + ":" + strconv.Itoa(int(pc.Port))
118                 if _, ok := rtmgr.Eps[uuid]; ok {
119                         rtmgr.Eps[uuid].Keepalive = true
120                 } else {
121                         ep := &rtmgr.Endpoint{
122                                 Uuid:       uuid,
123                                 Name:       pc.Name,
124                                 XAppType:   PlatformType,
125                                 Ip:         pc.Fqdn,
126                                 Port:       pc.Port,
127                                 TxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["tx"],
128                                 RxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["rx"],
129                                 Socket:     nil,
130                                 IsReady:    false,
131                                 Keepalive:  true,
132                         }
133                         xapp.Logger.Debug("ep created: %v", ep)
134                         if err := sbi.AddEndpoint(ep); err != nil {
135                                 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
136                                 continue
137                         }
138                         rtmgr.Eps[uuid] = ep
139                 }
140         }
141 }
142
143 func (s *Sbi) updateE2TEndpoints(E2Ts *map[string]rtmgr.E2TInstance, sbi Engine) {
144         xapp.Logger.Debug("updateE2TEndpoints invoked. E2T: %v", *E2Ts)
145         for _, e2t := range *E2Ts {
146                 uuid := e2t.Fqdn
147                 stringSlice := strings.Split(e2t.Fqdn, ":")
148                 ipaddress := stringSlice[0]
149                 port, _ := strconv.Atoi(stringSlice[1])
150                 if _, ok := rtmgr.Eps[uuid]; ok {
151                         rtmgr.Eps[uuid].Keepalive = true
152                 } else {
153                         ep := &rtmgr.Endpoint{
154                                 Uuid:       uuid,
155                                 Name:       e2t.Name,
156                                 XAppType:   PlatformType,
157                                 Ip:         ipaddress,
158                                 Port:       uint16(port),
159                                 TxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["tx"],
160                                 RxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["rx"],
161                                 Socket:     nil,
162                                 IsReady:    false,
163                                 Keepalive:  true,
164                         }
165                         xapp.Logger.Debug("ep created: %v", ep)
166                         if err := sbi.AddEndpoint(ep); err != nil {
167                                 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
168                                 continue
169                         }
170                         rtmgr.Eps[uuid] = ep
171                 }
172         }
173 }
174
175 func (s *Sbi) createEndpoint(payload string, sbi Engine) (*rtmgr.Endpoint) {
176         xapp.Logger.Debug("CreateEndPoint %v", payload)
177         stringSlice := strings.Split(payload, " ")
178         uuid := stringSlice[0]
179         xapp.Logger.Debug(">>> uuid %v",  stringSlice[0])
180
181
182         if _, ok := rtmgr.Eps[uuid]; ok {
183                 ep := rtmgr.Eps[uuid]
184                 return ep
185         }
186
187         return nil
188 }