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