Support for NETCONF Get command to get xapp configuration in O1 client
[ric-plt/o1.git] / agent / pkg / nbi / nbi.go
index 7865f43..83c3431 100755 (executable)
@@ -25,6 +25,7 @@ import (
        "fmt"
        "github.com/spf13/viper"
        "github.com/valyala/fastjson"
+       "os"
        "strings"
        "time"
        "unsafe"
@@ -47,6 +48,7 @@ import "C"
 var sbiClient sbi.SBIClientInterface
 var nbiClient *Nbi
 var log = xapp.Logger
+var rnib iRnib = xapp.Rnib
 
 func NewNbi(s sbi.SBIClientInterface) *Nbi {
        sbiClient = s
@@ -133,6 +135,9 @@ func (n *Nbi) SubscribeStatusData() bool {
        if ok := n.SubscribeStatus("o-ran-sc-ric-alarm-v1", "/o-ran-sc-ric-alarm-v1:ric/alarms"); !ok {
                return ok
        }
+       if ok := n.SubscribeStatus("o-ran-sc-ric-xapp-desc-v1", "/o-ran-sc-ric-xapp-desc-v1:ric/configuration"); !ok {
+               return ok
+       }
 
        return true
 }
@@ -233,7 +238,11 @@ func (n *Nbi) ManageConfigmaps(module, configJson string, oper int) error {
        root := fmt.Sprintf("%s:ric", module)
        appName := string(value.GetStringBytes(root, "config", "name"))
        namespace := string(value.GetStringBytes(root, "config", "namespace"))
-       control := value.Get(root, "config", "control").String()
+       controlVal := value.Get(root, "config", "control")
+       if controlVal == nil {
+               return nil
+       }
+       control := controlVal.String()
 
        var f interface{}
        err = json.Unmarshal([]byte(strings.ReplaceAll(control, "\\", "")), &f)
@@ -269,7 +278,18 @@ func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, r
        log.Info("nbiGnbStateCB: module='%s' xpath='%s' rpath='%s' [id=%d]", mod, C.GoString(xpath), C.GoString(rpath), reqid)
 
        if mod == "o-ran-sc-ric-xapp-desc-v1" {
-               podList, _ := sbiClient.GetAllPodStatus("ricxapp")
+
+               if C.GoString(xpath) == "/o-ran-sc-ric-xapp-desc-v1:ric/configuration" {
+                       nbGetAllXappsDefCfg(session, parent)
+                       return C.SR_ERR_OK
+               }
+
+               xappnamespace := os.Getenv("XAPP_NAMESPACE")
+               if xappnamespace == "" {
+                       xappnamespace = "ricxapp"
+               }
+               podList, _ := sbiClient.GetAllPodStatus(xappnamespace)
+
                for _, pod := range podList {
                        path := fmt.Sprintf("/o-ran-sc-ric-xapp-desc-v1:ric/health/status[name='%s']", pod.Name)
                        nbiClient.CreateNewElement(session, parent, path, "name", path)
@@ -280,49 +300,80 @@ func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, r
        }
 
        if mod == "o-ran-sc-ric-alarm-v1" {
-               alerts, _ := sbiClient.GetAlerts()
-               for _, alert := range alerts.Payload {
-                       id := alert.Annotations["alarm_id"]
-                       path := fmt.Sprintf("/o-ran-sc-ric-alarm-v1:ric/alarms/alarm[alarm-id='%s']", id)
-                       nbiClient.CreateNewElement(session, parent, path, "alarm-id", id)
-                       nbiClient.CreateNewElement(session, parent, path, "fault-text", alert.Alert.Labels["alertname"])
-                       nbiClient.CreateNewElement(session, parent, path, "severity", alert.Alert.Labels["severity"])
-                       nbiClient.CreateNewElement(session, parent, path, "status", alert.Alert.Labels["status"])
-                       nbiClient.CreateNewElement(session, parent, path, "additional-info", alert.Annotations["additional_info"])
+               if alerts, _ := sbiClient.GetAlerts(); alerts != nil {
+                       for _, alert := range alerts.Payload {
+                               id := alert.Annotations["alarm_id"]
+                               path := fmt.Sprintf("/o-ran-sc-ric-alarm-v1:ric/alarms/alarm[alarm-id='%s']", id)
+                               nbiClient.CreateNewElement(session, parent, path, "alarm-id", id)
+                               nbiClient.CreateNewElement(session, parent, path, "fault-text", alert.Alert.Labels["alertname"])
+                               nbiClient.CreateNewElement(session, parent, path, "severity", alert.Alert.Labels["severity"])
+                               nbiClient.CreateNewElement(session, parent, path, "status", alert.Alert.Labels["status"])
+                               nbiClient.CreateNewElement(session, parent, path, "additional-info", alert.Annotations["additional_info"])
+                       }
                }
                return C.SR_ERR_OK
        }
 
-       gnbs, err := xapp.Rnib.GetListGnbIds()
-       if err != nil || len(gnbs) == 0 {
-               log.Info("Rnib.GetListGnbIds() returned elementCount=%d err:%v", len(gnbs), err)
-               return C.SR_ERR_OK
+       gnbs, err := rnib.GetListGnbIds()
+       log.Info("Rnib.GetListGnbIds() returned elementCount=%d err:%v", len(gnbs), err)
+       if err == nil && len(gnbs) > 0 {
+               for _, gnb := range gnbs {
+                       ranName := gnb.GetInventoryName()
+                       info, err := rnib.GetNodeb(ranName)
+                       if err != nil {
+                               log.Error("GetNodeb() failed for ranName=%s: %v", ranName, err)
+                               continue
+                       }
+
+                       prot := nbiClient.E2APProt2Str(int(info.E2ApplicationProtocol))
+                       connStat := nbiClient.ConnStatus2Str(int(info.ConnectionStatus))
+                       ntype := nbiClient.NodeType2Str(int(info.NodeType))
+
+                       log.Info("gNB info: %s -> %s %s %s -> %s %s", ranName, prot, connStat, ntype, gnb.GetGlobalNbId().GetPlmnId(), gnb.GetGlobalNbId().GetNbId())
+
+                       path := fmt.Sprintf("/o-ran-sc-ric-gnb-status-v1:ric/nodes/node[ran-name='%s']", ranName)
+                       nbiClient.CreateNewElement(session, parent, path, "ran-name", ranName)
+                       nbiClient.CreateNewElement(session, parent, path, "ip", info.Ip)
+                       nbiClient.CreateNewElement(session, parent, path, "port", fmt.Sprintf("%d", info.Port))
+                       nbiClient.CreateNewElement(session, parent, path, "plmn-id", gnb.GetGlobalNbId().GetPlmnId())
+                       nbiClient.CreateNewElement(session, parent, path, "nb-id", gnb.GetGlobalNbId().GetNbId())
+                       nbiClient.CreateNewElement(session, parent, path, "e2ap-protocol", prot)
+                       nbiClient.CreateNewElement(session, parent, path, "connection-status", connStat)
+                       nbiClient.CreateNewElement(session, parent, path, "node", ntype)
+               }
        }
 
-       for _, gnb := range gnbs {
-               ranName := gnb.GetInventoryName()
-               info, err := xapp.Rnib.GetNodeb(ranName)
-               if err != nil {
-                       log.Error("GetNodeb() failed for ranName=%s: %v", ranName, err)
-                       continue
+       //Check if any Enbs are connected to RIC
+       enbs, err2 := rnib.GetListEnbIds()
+       log.Info("Rnib.GetListEnbIds() returned elementCount=%d err:%v", len(gnbs), err)
+       if err2 == nil || len(enbs) > 0 {
+               log.Info("Getting Enb details from list of Enbs")
+               for _, enb := range enbs {
+                       ranName := enb.GetInventoryName()
+                       info, err := rnib.GetNodeb(ranName)
+                       if err != nil {
+                               log.Error("GetNodeb() failed for ranName=%s: %v", ranName, err)
+                               continue
+                       }
+
+                       prot := nbiClient.E2APProt2Str(int(info.E2ApplicationProtocol))
+                       connStat := nbiClient.ConnStatus2Str(int(info.ConnectionStatus))
+                       ntype := nbiClient.NodeType2Str(int(info.NodeType))
+
+                       log.Info("eNB info: %s -> %s %s %s -> %s %s", ranName, prot, connStat, ntype, enb.GetGlobalNbId().GetPlmnId(), enb.GetGlobalNbId().GetNbId())
+
+                       path := fmt.Sprintf("/o-ran-sc-ric-gnb-status-v1:ric/nodes/node[ran-name='%s']", ranName)
+                       nbiClient.CreateNewElement(session, parent, path, "ran-name", ranName)
+                       nbiClient.CreateNewElement(session, parent, path, "ip", info.Ip)
+                       nbiClient.CreateNewElement(session, parent, path, "port", fmt.Sprintf("%d", info.Port))
+                       nbiClient.CreateNewElement(session, parent, path, "plmn-id", enb.GetGlobalNbId().GetPlmnId())
+                       nbiClient.CreateNewElement(session, parent, path, "nb-id", enb.GetGlobalNbId().GetNbId())
+                       nbiClient.CreateNewElement(session, parent, path, "e2ap-protocol", prot)
+                       nbiClient.CreateNewElement(session, parent, path, "connection-status", connStat)
+                       nbiClient.CreateNewElement(session, parent, path, "node", ntype)
                }
-
-               prot := nbiClient.E2APProt2Str(int(info.E2ApplicationProtocol))
-               connStat := nbiClient.ConnStatus2Str(int(info.ConnectionStatus))
-               ntype := nbiClient.NodeType2Str(int(info.NodeType))
-
-               log.Info("gNB info: %s -> %s %s %s -> %s %s", ranName, prot, connStat, ntype, gnb.GetGlobalNbId().GetPlmnId(), gnb.GetGlobalNbId().GetNbId())
-
-               path := fmt.Sprintf("/o-ran-sc-ric-gnb-status-v1:ric/nodes/node[ran-name='%s']", ranName)
-               nbiClient.CreateNewElement(session, parent, path, "ran-name", ranName)
-               nbiClient.CreateNewElement(session, parent, path, "ip", info.Ip)
-               nbiClient.CreateNewElement(session, parent, path, "port", fmt.Sprintf("%d", info.Port))
-               nbiClient.CreateNewElement(session, parent, path, "plmn-id", gnb.GetGlobalNbId().GetPlmnId())
-               nbiClient.CreateNewElement(session, parent, path, "nb-id", gnb.GetGlobalNbId().GetNbId())
-               nbiClient.CreateNewElement(session, parent, path, "e2ap-protocol", prot)
-               nbiClient.CreateNewElement(session, parent, path, "connection-status", connStat)
-               nbiClient.CreateNewElement(session, parent, path, "node", ntype)
        }
+
        return C.SR_ERR_OK
 }
 
@@ -381,3 +432,65 @@ func (n *Nbi) NodeType2Str(ntype int) string {
        }
        return "not-specified"
 }
+
+func (n *Nbi) testModuleChangeCB(module string) bool {
+       var event C.sr_event_t = C.SR_EV_CHANGE
+       reqID := C.int(100)
+       modName := C.CString(module)
+       defer C.free(unsafe.Pointer(modName))
+
+       if ret := nbiModuleChangeCB(n.session, modName, nil, event, reqID); ret != C.SR_ERR_OK {
+               return false
+       }
+       return true
+}
+
+func (n *Nbi) testModuleChangeCBDone(module string) bool {
+       var event C.sr_event_t = C.SR_EV_DONE
+       reqID := C.int(100)
+       modName := C.CString(module)
+       defer C.free(unsafe.Pointer(modName))
+
+       if ret := nbiModuleChangeCB(n.session, modName, nil, event, reqID); ret != C.SR_ERR_OK {
+               return false
+       }
+       return true
+}
+
+func (n *Nbi) testGnbStateCB(module string) bool {
+       modName := C.CString(module)
+       defer C.free(unsafe.Pointer(modName))
+       reqID := C.uint32_t(100)
+       parent := make([]*C.char, 1)
+
+       if ret := nbiGnbStateCB(n.session, modName, nil, nil, reqID, &parent[0]); ret != C.SR_ERR_OK {
+               return false
+       }
+       return true
+}
+
+func nbGetAllXappsDefCfg(session *C.sr_session_ctx_t, parent **C.char) {
+       var xappNameList []string
+       var xappCfgList []string
+
+       //Get the default config of all deployed xapps from appgmr using rest api
+       xappNameList, xappCfgList = sbiClient.GetAllDeployedXappsConfig()
+       if xappCfgList == nil || len(xappCfgList) == 0 {
+               log.Error("GetAllDeployedXappsConfig() Failure")
+               return
+       }
+       log.Info("GetAllDeployedXappsConfig Success, recvd xapp config")
+
+       //Loop thru the list of recvd xapps for config
+       for i, xappCfg := range xappCfgList {
+               path := fmt.Sprintf("/o-ran-sc-ric-xapp-desc-v1:ric/configuration/xapps/xapp[name='%s']", xappNameList[i])
+               nbiClient.CreateNewElement(session, parent, path, "name", xappNameList[i])
+               nbiClient.CreateNewElement(session, parent, path, "config", xappCfg)
+       }
+}
+
+type iRnib interface {
+       GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError)
+       GetListEnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError)
+       GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError)
+}