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 ==================================================================================
25 "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30 type XappRnibIf struct {
34 func (x *XappRnibIf) XappRnibSubscribe(NotificationCb func(string, ...string), channel string) error {
35 return xapp.Rnib.Subscribe(NotificationCb, channel)
38 func (x *XappRnibIf) XappRnibGetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) {
39 return xapp.Rnib.GetListGnbIds()
41 func (x *XappRnibIf) XappRnibGetNodeb(inventoryName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) {
42 nodeInfo, err := xapp.Rnib.GetNodeb(inventoryName)
46 func CreateXappRnibIfInstance() XappRnibInterface {
47 return new(XappRnibIf)
50 type E2IfState struct {
53 NbIdMap map[string]string
56 func (e *E2IfState) Init(c *Control) {
58 e.NbIdMap = make(map[string]string, 0)
59 e.ReadE2ConfigurationFromRnib()
63 func (e *E2IfState) GetE2NodesJson() []byte {
66 defer e.mutex.Unlock()
68 // Map contains something like this{"RAN_NAME_1":"RAN_NAME_1","RAN_NAME_11":"RAN_NAME_11","RAN_NAME_2":"RAN_NAME_2"}
69 var ranNameList []string
70 for _, ranName := range e.NbIdMap {
71 ranNameList = append(ranNameList, ranName)
74 e2NodesJson, err := json.Marshal(ranNameList)
76 xapp.Logger.Error("GetE2Node() json.Marshal error: %v", err)
81 func (e *E2IfState) GetAllE2Nodes() map[string]string {
84 defer e.mutex.Unlock()
88 func (e *E2IfState) NotificationCb(ch string, events ...string) {
90 xapp.Logger.Debug("SDL notification received from channel=%s, event=%v", ch, events[0])
92 xapp.Logger.Error("Invalid SDL notification received: %d", len(events))
96 if strings.Contains(events[0], "_CONNECTED") && !strings.Contains(events[0], "_CONNECTED_SETUP_FAILED") {
97 e.control.UpdateCounter(cE2StateChangedToUp)
98 nbId, err := ExtractNbiIdFromString(events[0])
100 xapp.Logger.Error("NotificationCb CONNECTED len(nbId) == 0 ")
103 xapp.Logger.Debug("E2 CONNECTED. NbId=%s", nbId)
104 e.NbIdMap[nbId] = nbId
105 } else if strings.Contains(events[0], "_DISCONNECTED") {
106 e.control.UpdateCounter(cE2StateChangedToDown)
107 nbId, err := ExtractNbiIdFromString(events[0])
109 xapp.Logger.Error("NotificationCb DISCONNECTED len(nbId) == 0 ")
112 xapp.Logger.Debug("E2 DISCONNECTED. NbId=%s", nbId)
113 if _, ok := e.NbIdMap[nbId]; ok {
114 delete(e.NbIdMap, nbId)
115 e.control.registry.DeleteAllE2Subscriptions(nbId, e.control)
120 func (e *E2IfState) SubscribeChannels() error {
122 if err := e.control.e2IfStateDb.XappRnibSubscribe(e.NotificationCb, "RAN_CONNECTION_STATUS_CHANGE"); err != nil {
123 xapp.Logger.Error("Sdl.SubscribeChannel failed: %v", err)
126 xapp.Logger.Debug("Subscription to RAN state changes done!")
130 func (e *E2IfState) ReadE2ConfigurationFromRnib() {
132 xapp.Logger.Debug("ReadE2ConfigurationFromRnib()")
133 nbIdentities, err := e.control.e2IfStateDb.XappRnibGetListGnbIds()
134 if err != nil || len(nbIdentities) == 0 {
135 xapp.Logger.Debug("There are no active NodeBs available: %v", err)
136 e.NbIdMap = make(map[string]string, 0)
140 for _, nbIdentity := range nbIdentities {
141 if e.isNodeBActive(nbIdentity.InventoryName) == false {
142 if _, ok := e.NbIdMap[nbIdentity.InventoryName]; ok {
143 delete(e.NbIdMap, nbIdentity.InventoryName)
144 xapp.Logger.Debug("E2 connection DISCONNETED: %v", nbIdentity.InventoryName)
146 // Delete all subscriptions related to InventoryName/nbId
147 e.control.registry.DeleteAllE2Subscriptions(nbIdentity.InventoryName, e.control)
152 if _, ok := e.NbIdMap[nbIdentity.InventoryName]; !ok {
153 e.NbIdMap[nbIdentity.InventoryName] = nbIdentity.InventoryName
154 xapp.Logger.Debug("E2 connection CONNECTED: %v", nbIdentity.InventoryName)
159 func (e *E2IfState) isNodeBActive(inventoryName string) bool {
160 nodeInfo, err := e.control.e2IfStateDb.XappRnibGetNodeb(inventoryName)
162 xapp.Logger.Error("GetNodeb() failed for inventoryName=%s: %v", inventoryName, err)
165 xapp.Logger.Debug("NodeB['%s'] connection status = %d", inventoryName, nodeInfo.ConnectionStatus)
166 return nodeInfo.ConnectionStatus == 1
169 func (e *E2IfState) IsE2ConnectionUp(nbId *string) bool {
171 if checkE2State == "false" {
175 if _, ok := e.NbIdMap[*nbId]; ok {
182 func ExtractNbiIdFromString(s string) (string, error) {
184 // Expected string formats are below
185 // gnb_208_092_303030_CONNECTED
186 // gnb_208_092_303030_DISCONNECTED
191 if strings.Contains(s, "_CONNECTED") {
192 splitStringTbl := strings.Split(s, "_CONNECTED")
193 nbId = splitStringTbl[0]
194 } else if strings.Contains(s, "_DISCONNECTED") {
195 splitStringTbl := strings.Split(s, "_DISCONNECTED")
196 nbId = splitStringTbl[0]
199 return "", fmt.Errorf("ExtractNbiIdFromString(): len(nbId) == 0 ")