blob: be415f36d04b5725e0357dbdc1a276520224f41f [file] [log] [blame]
Balint Uveges871fa392019-04-02 20:31:11 +00001/*
Peter Szilagyi16d84d62019-04-24 14:51:02 +00002w
Balint Uveges871fa392019-04-02 20:31:11 +00003==================================================================================
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.
wahidw761934a2019-11-27 06:07:26 +000018
19 This source code is part of the near-RT RIC (RAN Intelligent Controller)
20 platform project (RICP).
21
Balint Uveges871fa392019-04-02 20:31:11 +000022==================================================================================
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
30package sbi
31
32import (
33 "errors"
wahidw6ddad902020-04-01 16:39:15 +000034 "fmt"
wahidwa8596ec2019-12-05 06:30:42 +000035 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
wahidw6ddad902020-04-01 16:39:15 +000036 "net"
kalnagy92162652019-07-02 15:15:49 +020037 "routing-manager/pkg/rtmgr"
zkoczkaeb2ff0d2019-09-26 16:59:54 +020038 "strconv"
rangajal749099b2019-12-10 09:37:08 +000039 "strings"
Balint Uveges871fa392019-04-02 20:31:11 +000040)
41
zkoczkaaaf8d392019-10-02 17:16:06 +020042const DefaultNngPipelineSocketPrefix = "tcp://"
43const DefaultNngPipelineSocketNumber = 4561
44const PlatformType = "platform"
Peter Szilagyi16d84d62019-04-24 14:51:02 +000045
Balint Uveges871fa392019-04-02 20:31:11 +000046var (
zkoczkaaaf8d392019-10-02 17:16:06 +020047 SupportedSbis = []*EngineConfig{
48 {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020049 Name: "nngpush",
50 Version: "v1",
51 Protocol: "nngpipeline",
52 Instance: NewNngPush(),
53 IsAvailable: true,
54 },
Balint Uveges871fa392019-04-02 20:31:11 +000055 }
56)
57
zkoczkaaaf8d392019-10-02 17:16:06 +020058func GetSbi(sbiName string) (Engine, error) {
Balint Uveges871fa392019-04-02 20:31:11 +000059 for _, sbi := range SupportedSbis {
kalnagy92162652019-07-02 15:15:49 +020060 if sbi.Name == sbiName && sbi.IsAvailable {
61 return sbi.Instance, nil
Balint Uveges871fa392019-04-02 20:31:11 +000062 }
63 }
Peter Szilagyi16d84d62019-04-24 14:51:02 +000064 return nil, errors.New("SBI:" + sbiName + " is not supported or still not available")
65}
66
kalnagy92162652019-07-02 15:15:49 +020067type Sbi struct {
kalnagy92162652019-07-02 15:15:49 +020068}
69
zkoczkaaaf8d392019-10-02 17:16:06 +020070func (s *Sbi) pruneEndpointList(sbi Engine) {
wahidwa8596ec2019-12-05 06:30:42 +000071 xapp.Logger.Debug("pruneEndpointList invoked.")
zkoczkaeb2ff0d2019-09-26 16:59:54 +020072 for _, ep := range rtmgr.Eps {
73 if !ep.Keepalive {
wahidwa8596ec2019-12-05 06:30:42 +000074 xapp.Logger.Debug("deleting %v", ep)
zkoczkaeb2ff0d2019-09-26 16:59:54 +020075 sbi.DeleteEndpoint(ep)
76 delete(rtmgr.Eps, ep.Uuid)
77 } else {
78 rtmgr.Eps[ep.Uuid].Keepalive = false
79 }
80 }
Peter Szilagyi16d84d62019-04-24 14:51:02 +000081}
82
zkoczkaaaf8d392019-10-02 17:16:06 +020083func (s *Sbi) updateEndpoints(rcs *rtmgr.RicComponents, sbi Engine) {
wahidwa8596ec2019-12-05 06:30:42 +000084 for _, xapps := range (*rcs).XApps {
85 for _, instance := range xapps.Instances {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020086 uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
87 if _, ok := rtmgr.Eps[uuid]; ok {
88 rtmgr.Eps[uuid].Keepalive = true
89 } else {
90 ep := &rtmgr.Endpoint{
zkoczkaaaf8d392019-10-02 17:16:06 +020091 Uuid: uuid,
92 Name: instance.Name,
wahidwa8596ec2019-12-05 06:30:42 +000093 XAppType: xapps.Name,
zkoczkaaaf8d392019-10-02 17:16:06 +020094 Ip: instance.Ip,
95 Port: instance.Port,
96 TxMessages: instance.TxMessages,
97 RxMessages: instance.RxMessages,
wahidwa8596ec2019-12-05 06:30:42 +000098 Policies: instance.Policies,
zkoczkaaaf8d392019-10-02 17:16:06 +020099 Socket: nil,
100 IsReady: false,
101 Keepalive: true,
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200102 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200103 if err := sbi.AddEndpoint(ep); err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000104 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200105 continue
106 }
107 rtmgr.Eps[uuid] = ep
108 }
109 }
110 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200111 s.updatePlatformEndpoints(&((*rcs).Pcs), sbi)
wahidw6ddad902020-04-01 16:39:15 +0000112 s.updateE2TEndpoints(&((*rcs).E2Ts), sbi)
zkoczkaaaf8d392019-10-02 17:16:06 +0200113 s.pruneEndpointList(sbi)
Balint Uveges871fa392019-04-02 20:31:11 +0000114}
kalnagy92162652019-07-02 15:15:49 +0200115
zkoczkaaaf8d392019-10-02 17:16:06 +0200116func (s *Sbi) updatePlatformEndpoints(pcs *rtmgr.PlatformComponents, sbi Engine) {
wahidwa8596ec2019-12-05 06:30:42 +0000117 xapp.Logger.Debug("updatePlatformEndpoints invoked. PCS: %v", *pcs)
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200118 for _, pc := range *pcs {
119 uuid := pc.Fqdn + ":" + strconv.Itoa(int(pc.Port))
120 if _, ok := rtmgr.Eps[uuid]; ok {
121 rtmgr.Eps[uuid].Keepalive = true
122 } else {
123 ep := &rtmgr.Endpoint{
zkoczkaaaf8d392019-10-02 17:16:06 +0200124 Uuid: uuid,
125 Name: pc.Name,
126 XAppType: PlatformType,
127 Ip: pc.Fqdn,
128 Port: pc.Port,
129 TxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["tx"],
130 RxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["rx"],
131 Socket: nil,
132 IsReady: false,
133 Keepalive: true,
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200134 }
wahidwa8596ec2019-12-05 06:30:42 +0000135 xapp.Logger.Debug("ep created: %v", ep)
zkoczkaaaf8d392019-10-02 17:16:06 +0200136 if err := sbi.AddEndpoint(ep); err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000137 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200138 continue
139 }
140 rtmgr.Eps[uuid] = ep
141 }
142 }
kalnagy92162652019-07-02 15:15:49 +0200143}
rangajal749099b2019-12-10 09:37:08 +0000144
145func (s *Sbi) updateE2TEndpoints(E2Ts *map[string]rtmgr.E2TInstance, sbi Engine) {
wahidw6ddad902020-04-01 16:39:15 +0000146 xapp.Logger.Debug("updateE2TEndpoints invoked. E2T: %v", *E2Ts)
147 for _, e2t := range *E2Ts {
148 uuid := e2t.Fqdn
149 stringSlice := strings.Split(e2t.Fqdn, ":")
150 ipaddress := stringSlice[0]
151 port, _ := strconv.Atoi(stringSlice[1])
152 if _, ok := rtmgr.Eps[uuid]; ok {
153 rtmgr.Eps[uuid].Keepalive = true
154 } else {
155 ep := &rtmgr.Endpoint{
156 Uuid: uuid,
157 Name: e2t.Name,
158 XAppType: PlatformType,
159 Ip: ipaddress,
160 Port: uint16(port),
161 TxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["tx"],
162 RxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["rx"],
163 Socket: nil,
164 IsReady: false,
165 Keepalive: true,
166 }
167 xapp.Logger.Debug("ep created: %v", ep)
168 if err := sbi.AddEndpoint(ep); err != nil {
169 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
170 continue
171 }
172 rtmgr.Eps[uuid] = ep
173 }
174 }
rangajal749099b2019-12-10 09:37:08 +0000175}
wahidwdd6b0562020-03-31 03:09:45 +0000176
wahidw6ddad902020-04-01 16:39:15 +0000177func (s *Sbi) createEndpoint(payload string, sbi Engine) *rtmgr.Endpoint {
wahidwdd6b0562020-03-31 03:09:45 +0000178 xapp.Logger.Debug("CreateEndPoint %v", payload)
179 stringSlice := strings.Split(payload, " ")
180 uuid := stringSlice[0]
wahidw6ddad902020-04-01 16:39:15 +0000181 xapp.Logger.Debug(">>> uuid %v", stringSlice[0])
wahidwdd6b0562020-03-31 03:09:45 +0000182
183 if _, ok := rtmgr.Eps[uuid]; ok {
184 ep := rtmgr.Eps[uuid]
185 return ep
186 }
187
wahidw6ddad902020-04-01 16:39:15 +0000188 /* incase the stored Endpoint list is in the form of IP:port*/
189 stringsubsplit := strings.Split(uuid, ":")
190 addr, err := net.LookupIP(stringsubsplit[0])
191 if err == nil {
192 convertedUuid := fmt.Sprintf("%s:%s", addr[0], stringsubsplit[1])
193 xapp.Logger.Info(" IP:Port received is %s", convertedUuid)
194 if _, ok := rtmgr.Eps[convertedUuid]; ok {
195 ep := rtmgr.Eps[convertedUuid]
196 return ep
197 }
198 }
199
wahidwdd6b0562020-03-31 03:09:45 +0000200 return nil
201}