Delete all subscriptions when Ran is under reset.
[ric-plt/submgr.git] / pkg / control / e2if_state_test.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         "fmt"
24         "gerrit.o-ran-sc.org/r/ric-plt/nodeb-rnib.git/entities"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "strings"
27         "sync"
28         "testing"
29 )
30
31 var xappRnibMock *XappRnibMock
32
33 type XappRnibMock struct {
34         Mutex            sync.Mutex
35         nbIdentityMap    map[string]xapp.RNIBNbIdentity
36         RNIBNodebInfoMap map[string]xapp.RNIBNodebInfo
37         RnibSubscription RnibSubscription // Submgr can have only one subscription
38 }
39
40 type RnibSubscription struct {
41         Channel string                            // Subscribed channel/topic "RAN_CONNECTION_STATUS_CHANGE"
42         cb      func(ch string, events ...string) // Submgr's call back function
43 }
44
45 func CreateXappRnibIfMock() *XappRnibMock {
46         fmt.Println("XappRnibMock: CreateXappRnibIfMock()")
47         xappRnibMock = new(XappRnibMock)
48         xappRnibMock.Init()
49         return xappRnibMock
50 }
51
52 func (x *XappRnibMock) Init() {
53         x.nbIdentityMap = make(map[string]xapp.RNIBNbIdentity, 0)
54         x.RNIBNodebInfoMap = make(map[string]xapp.RNIBNodebInfo, 0)
55 }
56
57 func TestMock(t *testing.T) {
58
59         // Current UT test cases use these ran names
60         xappRnibMock.CreateGnb("RAN_NAME_1", entities.ConnectionStatus_CONNECTED)
61         xappRnibMock.CreateGnb("RAN_NAME_11", entities.ConnectionStatus_CONNECTED)
62         xappRnibMock.CreateGnb("RAN_NAME_2", entities.ConnectionStatus_CONNECTED)
63
64         xappRnibMock.CreateGnb("gnb_208_092_303030", entities.ConnectionStatus_CONNECTED) // This same value is used in gnb simulator!
65         xappRnibMock.CreateGnb("gnb_208_092_303030", entities.ConnectionStatus_DISCONNECTED)
66
67         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_UNKNOWN_CONNECTION_STATUS)
68         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_CONNECTED_SETUP_FAILED)
69         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_CONNECTING)
70         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_CONNECTED)
71         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_SHUTTING_DOWN)
72         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_SHUT_DOWN)
73         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_DISCONNECTED)
74         xappRnibMock.CreateGnb("gnb_369_11105_aaaaa3", entities.ConnectionStatus_UNDER_RESET)
75
76         mainCtrl.c.e2IfState.ReadE2ConfigurationFromRnib()
77         mainCtrl.c.e2IfState.SubscribeChannels()
78         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_UNKNOWN_CONNECTION_STATUS", "key1", "data1"); err != nil {
79                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
80         }
81         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTED_SETUP_FAILED", "key1", "data1"); err != nil {
82                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
83         }
84         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTING", "key1", "data1"); err != nil {
85                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
86         }
87         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTED", "key1", "data1"); err != nil {
88                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
89         }
90         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_SHUTTING_DOWN", "key1", "data1"); err != nil {
91                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
92         }
93         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_DISCONNECTED", "key1", "data1"); err != nil {
94                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
95         }
96         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_UNDER_RESET", "key1", "data1"); err != nil {
97                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
98         }
99 }
100
101 func (x *XappRnibMock) CreateGnb(gnbId string, connectionStatus entities.ConnectionStatus) {
102
103         xapp.Logger.Debug("XappRnibMock: CreateGnb() gnbId=%v, ConnectionStatus=%v", gnbId, connectionStatus)
104         nb := xapp.RNIBNodebInfo{}
105         nb.NodeType = xapp.RNIBNodeGNB
106         nb.ConnectionStatus = connectionStatus
107         if nb.ConnectionStatus < 0 || nb.ConnectionStatus > 6 {
108                 xapp.Logger.Error("XappRnibMock: CreateGnb() Incorrect connectionStatus=%v", nb.ConnectionStatus)
109                 return
110         }
111         nb.Ip = "localhost"
112         nb.Port = 5656
113         gnb := xapp.RNIBGnb{}
114
115         gnb.ServedNrCells = nil
116         nb.Configuration = &xapp.RNIBNodebInfoGnb{Gnb: &gnb}
117         nbIdentity := &xapp.RNIBNbIdentity{
118                 InventoryName: gnbId,
119                 GlobalNbId: &xapp.RNIBGlobalNbId{
120                         PlmnId: "001EF5",
121                         NbId:   "0045FE50",
122                 },
123         }
124
125         err := xappRnibMock.XappRnibSaveNodeb(nbIdentity, &nb)
126         if err != nil {
127                 xapp.Logger.Error("XappRnibMock: XappRnibSaveNodeb() failed. Error: %v", err)
128         }
129 }
130
131 func (x *XappRnibMock) XappRnibSaveNodeb(nbIdentity *xapp.RNIBNbIdentity, nodeb *xapp.RNIBNodebInfo) xapp.RNIBIRNibError {
132
133         xapp.Logger.Debug("XappRnibMock: XappRnibSaveNodeb() inventoryName=%v, ConnectionStatus=%v", nbIdentity.InventoryName, nodeb.ConnectionStatus)
134         x.Mutex.Lock()
135         defer x.Mutex.Unlock()
136         x.nbIdentityMap[nbIdentity.InventoryName] = *nbIdentity
137         x.RNIBNodebInfoMap[nbIdentity.InventoryName] = *nodeb
138         return nil
139 }
140
141 func (x *XappRnibMock) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
142
143         x.Mutex.Lock()
144         defer x.Mutex.Unlock()
145         xapp.Logger.Debug("XappRnibMock: XappRnibGetNodeb() inventoryName=%v", inventoryName)
146         nodebInfo, ok := x.RNIBNodebInfoMap[inventoryName]
147         if ok {
148                 return &nodebInfo, nil
149         } else {
150                 return nil, fmt.Errorf("XappRnibMock: XappRnibGetNodeb() failed: inventoryName=%s:", inventoryName)
151         }
152 }
153
154 func (x *XappRnibMock) XappRnibSubscribe(cb func(string, ...string), channel string) error {
155
156         if x.RnibSubscription.Channel == "RAN_CONNECTION_STATUS_CHANGE" {
157                 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE channel already subscribed")
158                 return nil
159         }
160         if x.RnibSubscription.Channel == "" {
161                 x.RnibSubscription.cb = cb
162                 x.RnibSubscription.Channel = channel
163                 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE subscribed")
164                 return nil
165         } else {
166                 return fmt.Errorf("XappRnibMock: Invalid channel/topic to subscribe: channel = %s", channel)
167         }
168 }
169
170 func (x *XappRnibMock) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
171
172         xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds()")
173         x.Mutex.Lock()
174         defer x.Mutex.Unlock()
175         var nbIdentities []*xapp.RNIBNbIdentity
176         for _, nbIdentity := range x.nbIdentityMap {
177                 newNbIdentity := entities.NbIdentity{}
178                 newNbIdentity = nbIdentity
179                 nbIdentities = append(nbIdentities, &newNbIdentity)
180         }
181         xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds(). len(nbIdentities) = %v", len(nbIdentities))
182         return nbIdentities, nil
183 }
184
185 func (x *XappRnibMock) XappRnibStoreAndPublish(channel string, event string, pairs ...interface{}) error {
186
187         x.Mutex.Lock()
188         defer x.Mutex.Unlock()
189         xapp.Logger.Debug("XappRnibMock: Change published. channel=%s, event=%s", channel, event)
190         if channel != "RAN_CONNECTION_STATUS_CHANGE" || channel == "" || event == "" {
191                 xapp.Logger.Debug("XappRnibMock: Invalid change published. channel=%s, event=%s", channel, event)
192         }
193
194         nbId, connectionStatus, err := ExtratNbIdAndConnectionStatus(event)
195         if err != nil {
196                 xapp.Logger.Error("XappRnibMock: ExtratNbIdAndConnectionStatus. Err=%s", err)
197         }
198
199         nbIdentity, ok := x.nbIdentityMap[nbId]
200         if ok {
201                 nbIdentity.ConnectionStatus = connectionStatus
202         }
203
204         if x.RnibSubscription.cb != nil {
205                 x.RnibSubscription.cb(channel, event)
206         } else {
207                 xapp.Logger.Error("XappRnibMock: x.RnibSubscription.cb == nil")
208         }
209         return nil
210 }
211
212 func ExtratNbIdAndConnectionStatus(s string) (string, entities.ConnectionStatus, error) {
213
214         var connectionStatus entities.ConnectionStatus
215         var nbId string
216         if strings.Contains(s, "_UNKNOWN_CONNECTION_STATUS") {
217                 connectionStatus = entities.ConnectionStatus_UNKNOWN_CONNECTION_STATUS
218                 splitStringTbl := strings.Split(s, "_UNKNOWN_CONNECTION_STATUS")
219                 nbId = splitStringTbl[0]
220         } else if strings.Contains(s, "_CONNECTED") {
221                 connectionStatus = entities.ConnectionStatus_CONNECTED
222                 splitStringTbl := strings.Split(s, "_CONNECTED")
223                 nbId = splitStringTbl[0]
224         } else if strings.Contains(s, "_DISCONNECTED") {
225                 connectionStatus = entities.ConnectionStatus_DISCONNECTED
226                 splitStringTbl := strings.Split(s, "_DISCONNECTED")
227                 nbId = splitStringTbl[0]
228         } else if strings.Contains(s, "_CONNECTED_SETUP_FAILED") {
229                 connectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
230                 splitStringTbl := strings.Split(s, "_CONNECTED_SETUP_FAILED")
231                 nbId = splitStringTbl[0]
232         } else if strings.Contains(s, "_CONNECTING") {
233                 connectionStatus = entities.ConnectionStatus_CONNECTING
234                 splitStringTbl := strings.Split(s, "_CONNECTING")
235                 nbId = splitStringTbl[0]
236         } else if strings.Contains(s, "_SHUTTING_DOWN") {
237                 connectionStatus = entities.ConnectionStatus_SHUTTING_DOWN
238                 splitStringTbl := strings.Split(s, "_SHUTTING_DOWN")
239                 nbId = splitStringTbl[0]
240         } else if strings.Contains(s, "_SHUT_DOWN") {
241                 connectionStatus = entities.ConnectionStatus_SHUT_DOWN
242                 splitStringTbl := strings.Split(s, "_SHUT_DOWN")
243                 nbId = splitStringTbl[0]
244         } else if strings.Contains(s, "_UNDER_RESET") {
245                 connectionStatus = entities.ConnectionStatus_UNDER_RESET
246                 splitStringTbl := strings.Split(s, "_UNDER_RESET")
247                 nbId = splitStringTbl[0]
248         } else {
249                 return "", 0, fmt.Errorf("XappRnibMock: Invalid connection status. %s", s)
250         }
251         if len(nbId) == 0 {
252                 return "", 0, fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")
253         }
254         return nbId, connectionStatus, nil
255 }