blob: 21ff78eb1d0382083d8258a74de50db1496d7fe4 [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"
rangajal20374692020-08-11 06:51:17 +000034 //"fmt"
wahidwa8596ec2019-12-05 06:30:42 +000035 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
rangajal20374692020-08-11 06:51:17 +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
wahidwbce67472020-06-15 13:52:55 +000042const DefaultRmrPipelineSocketPrefix = "tcp://"
43const DefaultRmrPipelineSocketNumber = 4561
zkoczkaaaf8d392019-10-02 17:16:06 +020044const 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 {
wahidwbce67472020-06-15 13:52:55 +000049 Name: "rmrpush",
zkoczkaeb2ff0d2019-09-26 16:59:54 +020050 Version: "v1",
wahidwbce67472020-06-15 13:52:55 +000051 Protocol: "rmrpipeline",
52 Instance: NewRmrPush(),
zkoczkaeb2ff0d2019-09-26 16:59:54 +020053 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 {
wahidw8f7ca652020-07-08 18:59:53 +053078 if rtmgr.Eps[ep.Uuid] != nil {
79 rtmgr.Eps[ep.Uuid].Keepalive = false
80 }
zkoczkaeb2ff0d2019-09-26 16:59:54 +020081 }
82 }
Peter Szilagyi16d84d62019-04-24 14:51:02 +000083}
84
zkoczkaaaf8d392019-10-02 17:16:06 +020085func (s *Sbi) updateEndpoints(rcs *rtmgr.RicComponents, sbi Engine) {
wahidwa8596ec2019-12-05 06:30:42 +000086 for _, xapps := range (*rcs).XApps {
87 for _, instance := range xapps.Instances {
zkoczkaeb2ff0d2019-09-26 16:59:54 +020088 uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
89 if _, ok := rtmgr.Eps[uuid]; ok {
90 rtmgr.Eps[uuid].Keepalive = true
91 } else {
92 ep := &rtmgr.Endpoint{
zkoczkaaaf8d392019-10-02 17:16:06 +020093 Uuid: uuid,
94 Name: instance.Name,
wahidwa8596ec2019-12-05 06:30:42 +000095 XAppType: xapps.Name,
zkoczkaaaf8d392019-10-02 17:16:06 +020096 Ip: instance.Ip,
97 Port: instance.Port,
98 TxMessages: instance.TxMessages,
99 RxMessages: instance.RxMessages,
wahidwa8596ec2019-12-05 06:30:42 +0000100 Policies: instance.Policies,
zkoczkaaaf8d392019-10-02 17:16:06 +0200101 Socket: nil,
102 IsReady: false,
103 Keepalive: true,
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200104 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200105 if err := sbi.AddEndpoint(ep); err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000106 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200107 continue
108 }
109 rtmgr.Eps[uuid] = ep
110 }
111 }
112 }
zkoczkaaaf8d392019-10-02 17:16:06 +0200113 s.updatePlatformEndpoints(&((*rcs).Pcs), sbi)
wahidw6ddad902020-04-01 16:39:15 +0000114 s.updateE2TEndpoints(&((*rcs).E2Ts), sbi)
zkoczkaaaf8d392019-10-02 17:16:06 +0200115 s.pruneEndpointList(sbi)
Balint Uveges871fa392019-04-02 20:31:11 +0000116}
kalnagy92162652019-07-02 15:15:49 +0200117
zkoczkaaaf8d392019-10-02 17:16:06 +0200118func (s *Sbi) updatePlatformEndpoints(pcs *rtmgr.PlatformComponents, sbi Engine) {
wahidwa8596ec2019-12-05 06:30:42 +0000119 xapp.Logger.Debug("updatePlatformEndpoints invoked. PCS: %v", *pcs)
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200120 for _, pc := range *pcs {
121 uuid := pc.Fqdn + ":" + strconv.Itoa(int(pc.Port))
122 if _, ok := rtmgr.Eps[uuid]; ok {
123 rtmgr.Eps[uuid].Keepalive = true
124 } else {
125 ep := &rtmgr.Endpoint{
zkoczkaaaf8d392019-10-02 17:16:06 +0200126 Uuid: uuid,
127 Name: pc.Name,
128 XAppType: PlatformType,
129 Ip: pc.Fqdn,
130 Port: pc.Port,
wahidw82c72662020-07-15 20:22:50 +0530131 //TxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["tx"],
132 //RxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["rx"],
zkoczkaaaf8d392019-10-02 17:16:06 +0200133 Socket: nil,
134 IsReady: false,
135 Keepalive: true,
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200136 }
wahidwa8596ec2019-12-05 06:30:42 +0000137 xapp.Logger.Debug("ep created: %v", ep)
zkoczkaaaf8d392019-10-02 17:16:06 +0200138 if err := sbi.AddEndpoint(ep); err != nil {
wahidwa8596ec2019-12-05 06:30:42 +0000139 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
zkoczkaeb2ff0d2019-09-26 16:59:54 +0200140 continue
141 }
142 rtmgr.Eps[uuid] = ep
143 }
144 }
kalnagy92162652019-07-02 15:15:49 +0200145}
rangajal749099b2019-12-10 09:37:08 +0000146
147func (s *Sbi) updateE2TEndpoints(E2Ts *map[string]rtmgr.E2TInstance, sbi Engine) {
wahidw6ddad902020-04-01 16:39:15 +0000148 xapp.Logger.Debug("updateE2TEndpoints invoked. E2T: %v", *E2Ts)
149 for _, e2t := range *E2Ts {
150 uuid := e2t.Fqdn
151 stringSlice := strings.Split(e2t.Fqdn, ":")
152 ipaddress := stringSlice[0]
153 port, _ := strconv.Atoi(stringSlice[1])
154 if _, ok := rtmgr.Eps[uuid]; ok {
155 rtmgr.Eps[uuid].Keepalive = true
156 } else {
157 ep := &rtmgr.Endpoint{
158 Uuid: uuid,
159 Name: e2t.Name,
160 XAppType: PlatformType,
161 Ip: ipaddress,
162 Port: uint16(port),
wahidw82c72662020-07-15 20:22:50 +0530163 //TxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["tx"],
164 //RxMessages: rtmgr.PLATFORMMESSAGETYPES[e2t.Name]["rx"],
wahidw6ddad902020-04-01 16:39:15 +0000165 Socket: nil,
166 IsReady: false,
167 Keepalive: true,
168 }
169 xapp.Logger.Debug("ep created: %v", ep)
170 if err := sbi.AddEndpoint(ep); err != nil {
171 xapp.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
172 continue
173 }
174 rtmgr.Eps[uuid] = ep
175 }
176 }
rangajal749099b2019-12-10 09:37:08 +0000177}
wahidwdd6b0562020-03-31 03:09:45 +0000178
wahidw6a9ce492020-08-15 11:29:43 +0530179func (s *Sbi) createEndpoint(payload string,rmrsrc string, sbi Engine) (*string,int) {
wahidwdd6b0562020-03-31 03:09:45 +0000180 xapp.Logger.Debug("CreateEndPoint %v", payload)
rangajal20374692020-08-11 06:51:17 +0000181// stringSlice := strings.Split(payload, " ")
182// uuid := stringSlice[0]
183// xapp.Logger.Debug(">>> uuid %v", stringSlice[0])
wahidwdd6b0562020-03-31 03:09:45 +0000184
rangajal20374692020-08-11 06:51:17 +0000185/* if _, ok := rtmgr.Eps[uuid]; ok {
wahidwdd6b0562020-03-31 03:09:45 +0000186 ep := rtmgr.Eps[uuid]
187 return ep
rangajal20374692020-08-11 06:51:17 +0000188 }*/
wahidwdd6b0562020-03-31 03:09:45 +0000189
wahidw6ddad902020-04-01 16:39:15 +0000190 /* incase the stored Endpoint list is in the form of IP:port*/
rangajal20374692020-08-11 06:51:17 +0000191/* stringsubsplit := strings.Split(uuid, ":")
wahidw6ddad902020-04-01 16:39:15 +0000192 addr, err := net.LookupIP(stringsubsplit[0])
193 if err == nil {
194 convertedUuid := fmt.Sprintf("%s:%s", addr[0], stringsubsplit[1])
195 xapp.Logger.Info(" IP:Port received is %s", convertedUuid)
196 if _, ok := rtmgr.Eps[convertedUuid]; ok {
197 ep := rtmgr.Eps[convertedUuid]
198 return ep
199 }
rangajal20374692020-08-11 06:51:17 +0000200 }*/
201
202 /* Create a new mapping, this case is assumed for multiple process sending RMR request from a container */
203 srcString := strings.Split(rmrsrc," ")
204 srcStringSlice := strings.Split(srcString[0],"=")
205 Whid := int(xapp.Rmr.Openwh(srcStringSlice[1]))
206
207 xapp.Logger.Info("Wormhole Id created is %d for EndPoint %s",Whid,srcStringSlice[1])
208 if Whid > 0 {
wahidw6a9ce492020-08-15 11:29:43 +0530209// rtmgr.RmrEp[srcStringSlice[1]] = Whid
rangajal20374692020-08-11 06:51:17 +0000210 xapp.Logger.Info("received %s and mapped to Whid = %d",srcStringSlice[1],Whid)
wahidw6a9ce492020-08-15 11:29:43 +0530211 return &srcStringSlice[1],Whid
wahidw6ddad902020-04-01 16:39:15 +0000212 }
213
wahidw6a9ce492020-08-15 11:29:43 +0530214 return nil,Whid
rangajal20374692020-08-11 06:51:17 +0000215 }