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 ==================================================================================
28 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
31 type XappRnibIf struct {
35 func (x *XappRnibIf) XappRnibSubscribe(NotificationCb func(string, ...string), channel string) error {
36 return xapp.Rnib.Subscribe(NotificationCb, channel)
39 func (x *XappRnibIf) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
40 return xapp.Rnib.GetListGnbIds()
42 func (x *XappRnibIf) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
43 nodeInfo, err := xapp.Rnib.GetNodeb(inventoryName)
47 func CreateXappRnibIfInstance() XappRnibInterface {
48 return new(XappRnibIf)
51 type E2IfState struct {
54 NbIdMap map[string]string
55 NbIdStatusMap map[string]string
58 func (e *E2IfState) Init(c *Control) {
60 e.NbIdMap = make(map[string]string, 0)
61 e.NbIdStatusMap = make(map[string]string, 0)
62 e.ReadE2ConfigurationFromRnib()
63 err := e.SubscribeChannels()
65 xapp.Logger.Error("Init(): SubscribeChannels() failed: %v", err)
69 func (e *E2IfState) GetE2NodesJson() []byte {
72 defer e.mutex.Unlock()
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)
80 e2NodesJson, err := json.Marshal(ranNameList)
82 xapp.Logger.Error("GetE2Node() json.Marshal error: %v", err)
87 func (e *E2IfState) GetAllE2Nodes() map[string]string {
90 defer e.mutex.Unlock()
94 func (e *E2IfState) NotificationCb(ch string, events ...string) {
96 xapp.Logger.Debug("SDL notification received from channel=%s, event=%v", ch, events[0])
98 xapp.Logger.Error("Invalid SDL notification received: %d", len(events))
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])
106 xapp.Logger.Error("NotificationCb CONNECTED len(nbId) == 0 ")
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])
116 xapp.Logger.Error("NotificationCb DISCONNECTED len(nbId) == 0 ")
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)
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])
130 xapp.Logger.Error("NotificationCb _UNDER_RESET %v ", err)
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)
141 func (e *E2IfState) SubscribeChannels() error {
143 if err := e.control.e2IfStateDb.XappRnibSubscribe(e.NotificationCb, "RAN_CONNECTION_STATUS_CHANGE"); err != nil {
144 xapp.Logger.Error("Sdl.SubscribeChannel failed: %v", err)
147 xapp.Logger.Debug("Subscription to RAN state changes done!")
151 func (e *E2IfState) ReadE2ConfigurationFromRnib() {
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)
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)
167 // Delete all subscriptions related to InventoryName/nbId
168 e.control.registry.DeleteAllE2Subscriptions(nbIdentity.InventoryName, e.control)
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)
180 func (e *E2IfState) isNodeBActive(inventoryName string) bool {
181 nodeInfo, err := e.control.e2IfStateDb.XappRnibGetNodeb(inventoryName)
183 xapp.Logger.Error("GetNodeb() failed for inventoryName=%s: %v", inventoryName, err)
186 xapp.Logger.Debug("NodeB['%s'] connection status = %d", inventoryName, nodeInfo.ConnectionStatus)
187 return nodeInfo.ConnectionStatus == 1
190 func (e *E2IfState) IsE2ConnectionUp(nbId *string) bool {
192 if checkE2State == "false" {
196 if _, ok := e.NbIdMap[*nbId]; ok {
203 func (e *E2IfState) IsE2ConnectionUnderReset(nbId *string) bool {
205 if status := e.NbIdStatusMap[*nbId]; status == "UNDER_RESET" {
212 func ExtractNbiIdFromString(s string) (string, error) {
214 // Expected string formats are below
215 // gnb_208_092_303030_CONNECTED
216 // gnb_208_092_303030_DISCONNECTED
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]
232 return "", fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")