e8ceef2f045af2e4bddd8389f49ce9fe53672874
[ric-plt/submgr.git] / pkg / control / e2if_state.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 package control
21
22 import (
23         "encoding/json"
24         "fmt"
25         "strings"
26         "sync"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
29 )
30
31 type XappRnibIf struct {
32         XappRnibInterface
33 }
34
35 func (x *XappRnibIf) XappRnibSubscribe(NotificationCb func(string, ...string), channel string) error {
36         return xapp.Rnib.Subscribe(NotificationCb, channel)
37 }
38
39 func (x *XappRnibIf) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
40         return xapp.Rnib.GetListGnbIds()
41 }
42 func (x *XappRnibIf) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
43         nodeInfo, err := xapp.Rnib.GetNodeb(inventoryName)
44         return nodeInfo, err
45 }
46
47 func CreateXappRnibIfInstance() XappRnibInterface {
48         return new(XappRnibIf)
49 }
50
51 type E2IfState struct {
52         mutex         sync.Mutex
53         control       *Control
54         NbIdMap       map[string]string
55         NbIdStatusMap map[string]string
56 }
57
58 func (e *E2IfState) Init(c *Control) {
59         e.control = c
60         e.NbIdMap = make(map[string]string, 0)
61         e.NbIdStatusMap = make(map[string]string, 0)
62         e.ReadE2ConfigurationFromRnib()
63         err := e.SubscribeChannels()
64         if err != nil {
65                 xapp.Logger.Error("Init(): SubscribeChannels() failed: %v", err)
66         }
67 }
68
69 func (e *E2IfState) GetE2NodesJson() []byte {
70
71         e.mutex.Lock()
72         defer e.mutex.Unlock()
73
74         // Map contains something like this{"RAN_NAME_1":"RAN_NAME_1","RAN_NAME_11":"RAN_NAME_11","RAN_NAME_2":"RAN_NAME_2"}
75         var ranNameList []string
76         for _, ranName := range e.NbIdMap {
77                 ranNameList = append(ranNameList, ranName)
78         }
79
80         e2NodesJson, err := json.Marshal(ranNameList)
81         if err != nil {
82                 xapp.Logger.Error("GetE2Node() json.Marshal error: %v", err)
83         }
84         return e2NodesJson
85 }
86
87 func (e *E2IfState) GetAllE2Nodes() map[string]string {
88
89         e.mutex.Lock()
90         defer e.mutex.Unlock()
91         return e.NbIdMap
92 }
93
94 func (e *E2IfState) NotificationCb(ch string, events ...string) {
95
96         xapp.Logger.Debug("SDL notification received from channel=%s, event=%v", ch, events[0])
97         if len(events) == 0 {
98                 xapp.Logger.Error("Invalid SDL notification received: %d", len(events))
99                 return
100         }
101
102         if strings.Contains(events[0], "_CONNECTED") && !strings.Contains(events[0], "_CONNECTED_SETUP_FAILED") {
103                 e.control.UpdateCounter(cE2StateChangedToUp)
104                 nbId, err := ExtractNbiIdFromString(events[0])
105                 if err != nil {
106                         xapp.Logger.Error("NotificationCb CONNECTED len(nbId) == 0 ")
107                         return
108                 }
109                 xapp.Logger.Debug("E2 CONNECTED. NbId=%s", nbId)
110                 e.NbIdMap[nbId] = nbId
111                 e.NbIdStatusMap[nbId] = "CONNECTED"
112         } else if strings.Contains(events[0], "_DISCONNECTED") {
113                 e.control.UpdateCounter(cE2StateChangedToDown)
114                 nbId, err := ExtractNbiIdFromString(events[0])
115                 if err != nil {
116                         xapp.Logger.Error("NotificationCb DISCONNECTED len(nbId) == 0 ")
117                         return
118                 }
119                 xapp.Logger.Debug("E2 DISCONNECTED. NbId=%s", nbId)
120                 if _, ok := e.NbIdMap[nbId]; ok {
121                         e.NbIdStatusMap[nbId] = "DISCONNECTED"
122                         delete(e.NbIdMap, nbId)
123                         e.control.registry.DeleteAllE2Subscriptions(nbId, e.control)
124                 }
125         } else if strings.Contains(events[0], "_UNDER_RESET") {
126                 xapp.Logger.Debug("NotificationCb UNDER_RESET len(nbId) == 0 ")
127                 e.control.UpdateCounter(cE2StateUnderReset)
128                 nbId, err := ExtractNbiIdFromString(events[0])
129                 if err != nil {
130                         xapp.Logger.Error("NotificationCb _UNDER_RESET %v ", err)
131                         return
132                 }
133                 e.NbIdStatusMap[nbId] = "UNDER_RESET"
134                 xapp.Logger.Debug("E2 Under Reset. NbId=%s", nbId)
135                 if _, ok := e.NbIdMap[nbId]; ok {
136                         e.control.registry.DeleteAllE2Subscriptions(nbId, e.control)
137                 }
138         }
139 }
140
141 func (e *E2IfState) SubscribeChannels() error {
142
143         if err := e.control.e2IfStateDb.XappRnibSubscribe(e.NotificationCb, "RAN_CONNECTION_STATUS_CHANGE"); err != nil {
144                 xapp.Logger.Error("Sdl.SubscribeChannel failed: %v", err)
145                 return err
146         }
147         xapp.Logger.Debug("Subscription to RAN state changes done!")
148         return nil
149 }
150
151 func (e *E2IfState) ReadE2ConfigurationFromRnib() {
152
153         xapp.Logger.Debug("ReadE2ConfigurationFromRnib()")
154         nbIdentities, err := e.control.e2IfStateDb.XappRnibGetListGnbIds()
155         if err != nil || len(nbIdentities) == 0 {
156                 xapp.Logger.Debug("There are no active NodeBs available: %v", err)
157                 e.NbIdMap = make(map[string]string, 0)
158                 return
159         }
160
161         for _, nbIdentity := range nbIdentities {
162                 if e.isNodeBActive(nbIdentity.InventoryName) == false {
163                         if _, ok := e.NbIdMap[nbIdentity.InventoryName]; ok {
164                                 delete(e.NbIdMap, nbIdentity.InventoryName)
165                                 xapp.Logger.Debug("E2 connection DISCONNETED: %v", nbIdentity.InventoryName)
166
167                                 // Delete all subscriptions related to InventoryName/nbId
168                                 e.control.registry.DeleteAllE2Subscriptions(nbIdentity.InventoryName, e.control)
169                         }
170                         continue
171                 }
172
173                 if _, ok := e.NbIdMap[nbIdentity.InventoryName]; !ok {
174                         e.NbIdMap[nbIdentity.InventoryName] = nbIdentity.InventoryName
175                         xapp.Logger.Debug("E2 connection CONNECTED: %v", nbIdentity.InventoryName)
176                 }
177         }
178 }
179
180 func (e *E2IfState) isNodeBActive(inventoryName string) bool {
181         nodeInfo, err := e.control.e2IfStateDb.XappRnibGetNodeb(inventoryName)
182         if err != nil {
183                 xapp.Logger.Error("GetNodeb() failed for inventoryName=%s: %v", inventoryName, err)
184                 return false
185         }
186         xapp.Logger.Debug("NodeB['%s'] connection status = %d", inventoryName, nodeInfo.ConnectionStatus)
187         return nodeInfo.ConnectionStatus == 1
188 }
189
190 func (e *E2IfState) IsE2ConnectionUp(nbId *string) bool {
191
192         if checkE2State == "false" {
193                 return true
194         }
195
196         if _, ok := e.NbIdMap[*nbId]; ok {
197                 return true
198         } else {
199                 return false
200         }
201 }
202
203 func (e *E2IfState) IsE2ConnectionUnderReset(nbId *string) bool {
204
205         if status := e.NbIdStatusMap[*nbId]; status == "UNDER_RESET" {
206                 return true
207         } else {
208                 return false
209         }
210 }
211
212 func ExtractNbiIdFromString(s string) (string, error) {
213
214         // Expected string formats are below
215         // gnb_208_092_303030_CONNECTED
216         // gnb_208_092_303030_DISCONNECTED
217         // ...
218
219         var nbId string
220         var err error
221         if strings.Contains(s, "_CONNECTED") {
222                 splitStringTbl := strings.Split(s, "_CONNECTED")
223                 nbId = splitStringTbl[0]
224         } else if strings.Contains(s, "_DISCONNECTED") {
225                 splitStringTbl := strings.Split(s, "_DISCONNECTED")
226                 nbId = splitStringTbl[0]
227         } else if strings.Contains(s, "_UNDER_RESET") {
228                 splitStringTbl := strings.Split(s, "_UNDER_RESET")
229                 nbId = splitStringTbl[0]
230         }
231         if len(nbId) == 0 {
232                 return "", fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")
233         }
234         return nbId, err
235 }