2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
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"
31 var xappRnibMock *XappRnibMock
33 type XappRnibMock struct {
35 nbIdentityMap map[string]xapp.RNIBNbIdentity
36 RNIBNodebInfoMap map[string]xapp.RNIBNodebInfo
37 RnibSubscription RnibSubscription // Submgr can have only one subscription
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
45 func CreateXappRnibIfMock() *XappRnibMock {
46 fmt.Println("XappRnibMock: CreateXappRnibIfMock()")
47 xappRnibMock = new(XappRnibMock)
52 func (x *XappRnibMock) Init() {
53 x.nbIdentityMap = make(map[string]xapp.RNIBNbIdentity, 0)
54 x.RNIBNodebInfoMap = make(map[string]xapp.RNIBNodebInfo, 0)
57 func TestMock(t *testing.T) {
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
101 func (x *XappRnibMock) CreateGnb(gnbId string, connectionStatus entities.ConnectionStatus) {
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)
113 gnb := xapp.RNIBGnb{}
115 gnb.ServedNrCells = nil
116 nb.Configuration = &xapp.RNIBNodebInfoGnb{Gnb: &gnb}
117 nbIdentity := &xapp.RNIBNbIdentity{
118 InventoryName: gnbId,
119 GlobalNbId: &xapp.RNIBGlobalNbId{
125 err := xappRnibMock.XappRnibSaveNodeb(nbIdentity, &nb)
127 xapp.Logger.Error("XappRnibMock: XappRnibSaveNodeb() failed. Error: %v", err)
131 func (x *XappRnibMock) XappRnibSaveNodeb(nbIdentity *xapp.RNIBNbIdentity, nodeb *xapp.RNIBNodebInfo) xapp.RNIBIRNibError {
133 xapp.Logger.Debug("XappRnibMock: XappRnibSaveNodeb() inventoryName=%v, ConnectionStatus=%v", nbIdentity.InventoryName, nodeb.ConnectionStatus)
135 defer x.Mutex.Unlock()
136 x.nbIdentityMap[nbIdentity.InventoryName] = *nbIdentity
137 x.RNIBNodebInfoMap[nbIdentity.InventoryName] = *nodeb
141 func (x *XappRnibMock) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
144 defer x.Mutex.Unlock()
145 xapp.Logger.Debug("XappRnibMock: XappRnibGetNodeb() inventoryName=%v", inventoryName)
146 nodebInfo, ok := x.RNIBNodebInfoMap[inventoryName]
148 return &nodebInfo, nil
150 return nil, fmt.Errorf("XappRnibMock: XappRnibGetNodeb() failed: inventoryName=%s:", inventoryName)
154 func (x *XappRnibMock) XappRnibSubscribe(cb func(string, ...string), channel string) error {
156 if x.RnibSubscription.Channel == "RAN_CONNECTION_STATUS_CHANGE" {
157 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE channel already subscribed")
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")
166 return fmt.Errorf("XappRnibMock: Invalid channel/topic to subscribe: channel = %s", channel)
170 func (x *XappRnibMock) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
172 xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds()")
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)
181 xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds(). len(nbIdentities) = %v", len(nbIdentities))
182 return nbIdentities, nil
185 func (x *XappRnibMock) XappRnibStoreAndPublish(channel string, event string, pairs ...interface{}) error {
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)
194 nbId, connectionStatus, err := ExtratNbIdAndConnectionStatus(event)
196 xapp.Logger.Error("XappRnibMock: ExtratNbIdAndConnectionStatus. Err=%s", err)
199 nbIdentity, ok := x.nbIdentityMap[nbId]
201 nbIdentity.ConnectionStatus = connectionStatus
204 if x.RnibSubscription.cb != nil {
205 x.RnibSubscription.cb(channel, event)
207 xapp.Logger.Error("XappRnibMock: x.RnibSubscription.cb == nil")
212 func ExtratNbIdAndConnectionStatus(s string) (string, entities.ConnectionStatus, error) {
214 var connectionStatus entities.ConnectionStatus
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]
249 return "", 0, fmt.Errorf("XappRnibMock: Invalid connection status. %s", s)
252 return "", 0, fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")
254 return nbId, connectionStatus, nil