7d07160e6ea526aa68648979ee10057856f39a44
[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         for _, ep := range rtmgr.Eps {
68                 if !ep.Keepalive {
69                         rtmgr.Logger.Debug("deleting %v", ep)
70                         sbi.DeleteEndpoint(ep)
71                         delete(rtmgr.Eps, ep.Uuid)
72                 } else {
73                         rtmgr.Eps[ep.Uuid].Keepalive = false
74                 }
75         }
76 }
77
78 func (s *Sbi) updateEndpoints(rcs *rtmgr.RicComponents, sbi Engine) {
79         for _, xapp := range (*rcs).XApps {
80                 for _, instance := range xapp.Instances {
81                         uuid := instance.Ip + ":" + strconv.Itoa(int(instance.Port))
82                         if _, ok := rtmgr.Eps[uuid]; ok {
83                                 rtmgr.Eps[uuid].Keepalive = true
84                         } else {
85                                 ep := &rtmgr.Endpoint{
86                                         Uuid:       uuid,
87                                         Name:       instance.Name,
88                                         XAppType:   xapp.Name,
89                                         Ip:         instance.Ip,
90                                         Port:       instance.Port,
91                                         TxMessages: instance.TxMessages,
92                                         RxMessages: instance.RxMessages,
93                                         Socket:     nil,
94                                         IsReady:    false,
95                                         Keepalive:  true,
96                                 }
97                                 if err := sbi.AddEndpoint(ep); err != nil {
98                                         rtmgr.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
99                                         continue
100                                 }
101                                 rtmgr.Eps[uuid] = ep
102                         }
103                 }
104         }
105         s.updatePlatformEndpoints(&((*rcs).Pcs), sbi)
106         s.pruneEndpointList(sbi)
107 }
108
109 func (s *Sbi) updatePlatformEndpoints(pcs *rtmgr.PlatformComponents, sbi Engine) {
110         rtmgr.Logger.Debug("updatePlatformEndpoints invoked. PCS: %v", *pcs)
111         for _, pc := range *pcs {
112                 uuid := pc.Fqdn + ":" + strconv.Itoa(int(pc.Port))
113                 if _, ok := rtmgr.Eps[uuid]; ok {
114                         rtmgr.Eps[uuid].Keepalive = true
115                 } else {
116                         ep := &rtmgr.Endpoint{
117                                 Uuid:       uuid,
118                                 Name:       pc.Name,
119                                 XAppType:   PlatformType,
120                                 Ip:         pc.Fqdn,
121                                 Port:       pc.Port,
122                                 TxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["tx"],
123                                 RxMessages: rtmgr.PLATFORMMESSAGETYPES[pc.Name]["rx"],
124                                 Socket:     nil,
125                                 IsReady:    false,
126                                 Keepalive:  true,
127                         }
128                         rtmgr.Logger.Debug("ep created: %v", ep)
129                         if err := sbi.AddEndpoint(ep); err != nil {
130                                 rtmgr.Logger.Error("can't create socket for endpoint: " + ep.Name + " due to:" + err.Error())
131                                 continue
132                         }
133                         rtmgr.Eps[uuid] = ep
134                 }
135         }
136 }