X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=agent%2Fpkg%2Fnbi%2Fnbi.go;h=b191db51faafd0b275aacc506fe21fbe87894456;hb=a32f10007e6783f31b6b7af8b00bcc31ffae4660;hp=b1304ee47b445e495986464672b63ebece4d0f83;hpb=bad83880ceaeeff5b73e9e3aa15cdd302829b5fc;p=ric-plt%2Fo1.git diff --git a/agent/pkg/nbi/nbi.go b/agent/pkg/nbi/nbi.go index b1304ee..b191db5 100755 --- a/agent/pkg/nbi/nbi.go +++ b/agent/pkg/nbi/nbi.go @@ -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 @@ -100,6 +102,7 @@ func (n *Nbi) Setup(schemas []string) bool { func (n *Nbi) DoSubscription(schemas []string) bool { log.Info("Subscribing YANG modules ... %v", schemas) + for _, module := range schemas { modName := C.CString(module) defer C.free(unsafe.Pointer(modName)) @@ -121,8 +124,24 @@ func (n *Nbi) SubscribeModule(module *C.char) bool { } func (n *Nbi) SubscribeStatusData() bool { - mod := C.CString("o-ran-sc-ric-gnb-status-v1") - path := C.CString("/o-ran-sc-ric-gnb-status-v1:ric/nodes") + if ok := n.SubscribeStatus("o-ran-sc-ric-gnb-status-v1", "/o-ran-sc-ric-gnb-status-v1:ric/nodes"); !ok { + return ok + } + + if ok := n.SubscribeStatus("o-ran-sc-ric-xapp-desc-v1", "/o-ran-sc-ric-xapp-desc-v1:ric/health"); !ok { + return ok + } + + if ok := n.SubscribeStatus("o-ran-sc-ric-alarm-v1", "/o-ran-sc-ric-alarm-v1:ric/alarms"); !ok { + return ok + } + + return true +} + +func (n *Nbi) SubscribeStatus(module, xpath string) bool { + mod := C.CString(module) + path := C.CString(xpath) defer C.free(unsafe.Pointer(mod)) defer C.free(unsafe.Pointer(path)) @@ -139,9 +158,13 @@ func nbiModuleChangeCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.cha changedModule := C.GoString(module) changedXpath := C.GoString(xpath) - log.Info("NBI: Module change callback - event='%d' module=%s xpath=%s reqId=%d", event, changedModule, changedXpath, reqId) + log.Info("NBI: change event='%d' module=%s xpath=%s reqId=%d", event, changedModule, changedXpath, reqId) + if C.SR_EV_CHANGE != event { + log.Info("NBI: Changes finalized!") + return C.SR_ERR_OK + } - if C.SR_EV_CHANGE == event { + if changedModule == "o-ran-sc-ric-xapp-desc-v1" { configJson := C.yang_data_sr2json(session, module, event, &nbiClient.oper) err := nbiClient.ManageXapps(changedModule, C.GoString(configJson), int(nbiClient.oper)) if err != nil { @@ -149,9 +172,9 @@ func nbiModuleChangeCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.cha } } - if C.SR_EV_DONE == event { + if changedModule == "o-ran-sc-ric-ueec-config-v1" { configJson := C.get_data_json(session, module) - err := nbiClient.ManageConfigmaps(changedModule, C.GoString(configJson), int(nbiClient.oper)) + err := nbiClient.ManageConfigmaps(changedModule, C.GoString(configJson), int(C.SR_OP_MODIFIED)) if err != nil { return C.SR_ERR_OPERATION_FAILED } @@ -163,7 +186,7 @@ func nbiModuleChangeCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.cha func (n *Nbi) ManageXapps(module, configJson string, oper int) error { log.Info("ManageXapps: module=%s configJson=%s", module, configJson) - if configJson == "" || module != "o-ran-sc-ric-xapp-desc-v1" { + if configJson == "" { return nil } @@ -195,7 +218,7 @@ func (n *Nbi) ManageXapps(module, configJson string, oper int) error { func (n *Nbi) ManageConfigmaps(module, configJson string, oper int) error { log.Info("ManageConfig: module=%s configJson=%s", module, configJson) - if configJson == "" || module != "o-ran-sc-ric-ueec-config-v1" { + if configJson == "" { return nil } @@ -205,13 +228,18 @@ func (n *Nbi) ManageConfigmaps(module, configJson string, oper int) error { value, err := n.ParseJson(configJson) if err != nil { + log.Info("ParseJson failed with error: %v", oper) return err } 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) @@ -242,10 +270,42 @@ func (n *Nbi) ParseJsonArray(dsContent, model, top, elem string) ([]*fastjson.Va } //export nbiGnbStateCB -func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, req_xpath *C.char, reqid C.uint32_t, parent **C.char) C.int { - log.Info("NBI: Module state data for module='%s' path='%s' rpath='%s' requested [id=%d]", C.GoString(module), C.GoString(xpath), C.GoString(req_xpath), reqid) +func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, rpath *C.char, reqid C.uint32_t, parent **C.char) C.int { + mod := C.GoString(module) + 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" { + xappnamespace := os.Getenv("XAPP_NAMESPACE") + if xappnamespace == "" { + xappnamespace = "ricxapp" + } + podList, _ := sbiClient.GetAllPodStatus(xappnamespace) - gnbs, err := xapp.Rnib.GetListGnbIds() + 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) + nbiClient.CreateNewElement(session, parent, path, "health", pod.Health) + nbiClient.CreateNewElement(session, parent, path, "status", pod.Status) + } + return C.SR_ERR_OK + } + + if mod == "o-ran-sc-ric-alarm-v1" { + 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 := 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 @@ -253,7 +313,7 @@ func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, r for _, gnb := range gnbs { ranName := gnb.GetInventoryName() - info, err := xapp.Rnib.GetNodeb(ranName) + info, err := rnib.GetNodeb(ranName) if err != nil { log.Error("GetNodeb() failed for ranName=%s: %v", ranName, err) continue @@ -265,20 +325,21 @@ func nbiGnbStateCB(session *C.sr_session_ctx_t, module *C.char, xpath *C.char, r log.Info("gNB info: %s -> %s %s %s -> %s %s", ranName, prot, connStat, ntype, gnb.GetGlobalNbId().GetPlmnId(), gnb.GetGlobalNbId().GetNbId()) - nbiClient.CreateNewElement(session, parent, ranName, "ran-name", ranName) - nbiClient.CreateNewElement(session, parent, ranName, "ip", info.Ip) - nbiClient.CreateNewElement(session, parent, ranName, "port", fmt.Sprintf("%d", info.Port)) - nbiClient.CreateNewElement(session, parent, ranName, "plmn-id", gnb.GetGlobalNbId().GetPlmnId()) - nbiClient.CreateNewElement(session, parent, ranName, "nb-id", gnb.GetGlobalNbId().GetNbId()) - nbiClient.CreateNewElement(session, parent, ranName, "e2ap-protocol", prot) - nbiClient.CreateNewElement(session, parent, ranName, "connection-status", connStat) - nbiClient.CreateNewElement(session, parent, ranName, "node", ntype) + 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 } func (n *Nbi) CreateNewElement(session *C.sr_session_ctx_t, parent **C.char, key, name, value string) { - basePath := fmt.Sprintf("/o-ran-sc-ric-gnb-status-v1:ric/nodes/node[ran-name='%s']/%s", key, name) + basePath := fmt.Sprintf("%s/%s", key, name) log.Info("%s -> %s", basePath, value) cPath := C.CString(basePath) @@ -332,3 +393,44 @@ 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 +} + +type iRnib interface { + GetListGnbIds() ([]*xapp.RNIBNbIdentity, xapp.RNIBIRNibError) + GetNodeb(invName string) (*xapp.RNIBNodebInfo, xapp.RNIBIRNibError) +}