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)
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)
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)
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)
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)
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)
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)
97 func (x *XappRnibMock) CreateGnb(gnbId string, connectionStatus entities.ConnectionStatus) {
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)
109 gnb := xapp.RNIBGnb{}
111 gnb.ServedNrCells = nil
112 nb.Configuration = &xapp.RNIBNodebInfoGnb{Gnb: &gnb}
113 nbIdentity := &xapp.RNIBNbIdentity{
114 InventoryName: gnbId,
115 GlobalNbId: &xapp.RNIBGlobalNbId{
121 err := xappRnibMock.XappRnibSaveNodeb(nbIdentity, &nb)
123 xapp.Logger.Error("XappRnibMock: XappRnibSaveNodeb() failed. Error: %v", err)
127 func (x *XappRnibMock) XappRnibSaveNodeb(nbIdentity *xapp.RNIBNbIdentity, nodeb *xapp.RNIBNodebInfo) xapp.RNIBIRNibError {
129 xapp.Logger.Debug("XappRnibMock: XappRnibSaveNodeb() inventoryName=%v, ConnectionStatus=%v", nbIdentity.InventoryName, nodeb.ConnectionStatus)
131 defer x.Mutex.Unlock()
132 x.nbIdentityMap[nbIdentity.InventoryName] = *nbIdentity
133 x.RNIBNodebInfoMap[nbIdentity.InventoryName] = *nodeb
137 func (x *XappRnibMock) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
140 defer x.Mutex.Unlock()
141 xapp.Logger.Debug("XappRnibMock: XappRnibGetNodeb() inventoryName=%v", inventoryName)
142 nodebInfo, ok := x.RNIBNodebInfoMap[inventoryName]
144 return &nodebInfo, nil
146 return nil, fmt.Errorf("XappRnibMock: XappRnibGetNodeb() failed: inventoryName=%s:", inventoryName)
150 func (x *XappRnibMock) XappRnibSubscribe(cb func(string, ...string), channel string) error {
152 if x.RnibSubscription.Channel == "RAN_CONNECTION_STATUS_CHANGE" {
153 xapp.Logger.Debug("XappRnibMock: RAN_CONNECTION_STATUS_CHANGE channel already subscribed")
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")
162 return fmt.Errorf("XappRnibMock: Invalid channel/topic to subscribe: channel = %s", channel)
166 func (x *XappRnibMock) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
168 xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds()")
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)
177 xapp.Logger.Debug("XappRnibMock: XappRnibGetListGnbIds(). len(nbIdentities) = %v", len(nbIdentities))
178 return nbIdentities, nil
181 func (x *XappRnibMock) XappRnibStoreAndPublish(channel string, event string, pairs ...interface{}) error {
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)
190 nbId, connectionStatus, err := ExtratNbIdAndConnectionStatus(event)
192 xapp.Logger.Error("XappRnibMock: ExtratNbIdAndConnectionStatus. Err=%s", err)
195 nbIdentity, ok := x.nbIdentityMap[nbId]
197 nbIdentity.ConnectionStatus = connectionStatus
200 if x.RnibSubscription.cb != nil {
201 x.RnibSubscription.cb(channel, event)
203 xapp.Logger.Error("XappRnibMock: x.RnibSubscription.cb == nil")
208 func ExtratNbIdAndConnectionStatus(s string) (string, entities.ConnectionStatus, error) {
210 var connectionStatus entities.ConnectionStatus
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]
241 return "", 0, fmt.Errorf("XappRnibMock: Invalid connection status. %s", s)
244 return "", 0, fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")
246 return nbId, connectionStatus, nil