E2 restart handling added
[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
75         mainCtrl.c.e2IfState.ReadE2ConfigurationFromRnib()
76         mainCtrl.c.e2IfState.SubscribeChannels()
77         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_UNKNOWN_CONNECTION_STATUS", "key1", "data1"); err != nil {
78                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
79         }
80         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTED_SETUP_FAILED", "key1", "data1"); err != nil {
81                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
82         }
83         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTING", "key1", "data1"); err != nil {
84                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
85         }
86         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_CONNECTED", "key1", "data1"); err != nil {
87                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
88         }
89         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_SHUTTING_DOWN", "key1", "data1"); err != nil {
90                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
91         }
92         if err := xappRnibMock.XappRnibStoreAndPublish("RAN_CONNECTION_STATUS_CHANGE", "gnb_369_11105_aaaaa3_DISCONNECTED", "key1", "data1"); err != nil {
93                 t.Errorf("XappRnibStoreAndPublish failed: %v", err)
94         }
95 }
96
97 func (x *XappRnibMock) CreateGnb(gnbId string, connectionStatus entities.ConnectionStatus) {
98
99         xapp.Logger.Debug("XappRnibMock: CreateGnb() gnbId=%v, ConnectionStatus=%v", gnbId, connectionStatus)
100         nb := xapp.RNIBNodebInfo{}
101         nb.NodeType = xapp.RNIBNodeGNB
102         nb.ConnectionStatus = connectionStatus
103         if nb.ConnectionStatus < 0 || nb.ConnectionStatus > 6 {
104                 xapp.Logger.Error("XappRnibMock: CreateGnb() Incorrect connectionStatus=%v", nb.ConnectionStatus)
105                 return
106         }
107         nb.Ip = "localhost"
108         nb.Port = 5656
109         gnb := xapp.RNIBGnb{}
110
111         gnb.ServedNrCells = nil
112         nb.Configuration = &xapp.RNIBNodebInfoGnb{Gnb: &gnb}
113         nbIdentity := &xapp.RNIBNbIdentity{
114                 InventoryName: gnbId,
115                 GlobalNbId: &xapp.RNIBGlobalNbId{
116                         PlmnId: "001EF5",
117                         NbId:   "0045FE50",
118                 },
119         }
120
121         err := xappRnibMock.XappRnibSaveNodeb(nbIdentity, &nb)
122         if err != nil {
123                 xapp.Logger.Error("XappRnibMock: XappRnibSaveNodeb() failed. Error: %v", err)
124         }
125 }
126
127 func (x *XappRnibMock) XappRnibSaveNodeb(nbIdentity *xapp.RNIBNbIdentity, nodeb *xapp.RNIBNodebInfo) xapp.RNIBIRNibError {
128
129         xapp.Logger.Debug("XappRnibMock: XappRnibSaveNodeb() inventoryName=%v, ConnectionStatus=%v", nbIdentity.InventoryName, nodeb.ConnectionStatus)
130         x.Mutex.Lock()
131         defer x.Mutex.Unlock()
132         x.nbIdentityMap[nbIdentity.InventoryName] = *nbIdentity
133         x.RNIBNodebInfoMap[nbIdentity.InventoryName] = *nodeb
134         return nil
135 }
136
137 func (x *XappRnibMock) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
138
139         x.Mutex.Lock()
140         defer x.Mutex.Unlock()
141         xapp.Logger.Debug("XappRnibMock: XappRnibGetNodeb() inventoryName=%v", inventoryName)
142         nodebInfo, ok := x.RNIBNodebInfoMap[inventoryName]
143         if ok {
144                 return &nodebInfo, nil
145         } else {
146                 return nil, fmt.Errorf("XappRnibMock: XappRnibGetNodeb() failed: inventoryName=%s:", inventoryName)
147         }
148 }
149
150 func (x *XappRnibMock) XappRnibSubscribe(cb func(string, ...string), channel string) error {
151
152         if x.RnibSubscription.Channel == "RAN_CONNECTION_STATUS_CHANGE" {
153                 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE channel already subscribed")
154                 return nil
155         }
156         if x.RnibSubscription.Channel == "" {
157                 x.RnibSubscription.cb = cb
158                 x.RnibSubscription.Channel = channel
159                 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE subscribed")
160                 return nil
161         } else {
162                 return fmt.Errorf("XappRnibMock: Invalid channel/topic to subscribe: channel = %s", channel)
163         }
164 }
165
166 func (x *XappRnibMock) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
167
168         xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds()")
169         x.Mutex.Lock()
170         defer x.Mutex.Unlock()
171         var nbIdentities []*xapp.RNIBNbIdentity
172         for _, nbIdentity := range x.nbIdentityMap {
173                 newNbIdentity := entities.NbIdentity{}
174                 newNbIdentity = nbIdentity
175                 nbIdentities = append(nbIdentities, &newNbIdentity)
176         }
177         xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds(). len(nbIdentities) = %v", len(nbIdentities))
178         return nbIdentities, nil
179 }
180
181 func (x *XappRnibMock) XappRnibStoreAndPublish(channel string, event string, pairs ...interface{}) error {
182
183         x.Mutex.Lock()
184         defer x.Mutex.Unlock()
185         xapp.Logger.Debug("XappRnibMock: Change published. channel=%s, event=%s", channel, event)
186         if channel != "RAN_CONNECTION_STATUS_CHANGE" || channel == "" || event == "" {
187                 xapp.Logger.Debug("XappRnibMock: Invalid change published. channel=%s, event=%s", channel, event)
188         }
189
190         nbId, connectionStatus, err := ExtratNbIdAndConnectionStatus(event)
191         if err != nil {
192                 xapp.Logger.Error("XappRnibMock: ExtratNbIdAndConnectionStatus. Err=%s", err)
193         }
194
195         nbIdentity, ok := x.nbIdentityMap[nbId]
196         if ok {
197                 nbIdentity.ConnectionStatus = connectionStatus
198         }
199
200         if x.RnibSubscription.cb != nil {
201                 x.RnibSubscription.cb(channel, event)
202         } else {
203                 xapp.Logger.Error("XappRnibMock: x.RnibSubscription.cb == nil")
204         }
205         return nil
206 }
207
208 func ExtratNbIdAndConnectionStatus(s string) (string, entities.ConnectionStatus, error) {
209
210         var connectionStatus entities.ConnectionStatus
211         var nbId string
212         if strings.Contains(s, "_UNKNOWN_CONNECTION_STATUS") {
213                 connectionStatus = entities.ConnectionStatus_UNKNOWN_CONNECTION_STATUS
214                 splitStringTbl := strings.Split(s, "_UNKNOWN_CONNECTION_STATUS")
215                 nbId = splitStringTbl[0]
216         } else if strings.Contains(s, "_CONNECTED") {
217                 connectionStatus = entities.ConnectionStatus_CONNECTED
218                 splitStringTbl := strings.Split(s, "_CONNECTED")
219                 nbId = splitStringTbl[0]
220         } else if strings.Contains(s, "_DISCONNECTED") {
221                 connectionStatus = entities.ConnectionStatus_DISCONNECTED
222                 splitStringTbl := strings.Split(s, "_DISCONNECTED")
223                 nbId = splitStringTbl[0]
224         } else if strings.Contains(s, "_CONNECTED_SETUP_FAILED") {
225                 connectionStatus = entities.ConnectionStatus_CONNECTED_SETUP_FAILED
226                 splitStringTbl := strings.Split(s, "_CONNECTED_SETUP_FAILED")
227                 nbId = splitStringTbl[0]
228         } else if strings.Contains(s, "_CONNECTING") {
229                 connectionStatus = entities.ConnectionStatus_CONNECTING
230                 splitStringTbl := strings.Split(s, "_CONNECTING")
231                 nbId = splitStringTbl[0]
232         } else if strings.Contains(s, "_SHUTTING_DOWN") {
233                 connectionStatus = entities.ConnectionStatus_SHUTTING_DOWN
234                 splitStringTbl := strings.Split(s, "_SHUTTING_DOWN")
235                 nbId = splitStringTbl[0]
236         } else if strings.Contains(s, "_SHUT_DOWN") {
237                 connectionStatus = entities.ConnectionStatus_SHUT_DOWN
238                 splitStringTbl := strings.Split(s, "_SHUT_DOWN")
239                 nbId = splitStringTbl[0]
240         } else {
241                 return "", 0, fmt.Errorf("XappRnibMock: Invalid connection status. %s", s)
242         }
243         if len(nbId) == 0 {
244                 return "", 0, fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")
245         }
246         return nbId, connectionStatus, nil
247 }