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