feat: nbi

This commit is contained in:
simon
2025-05-23 18:24:18 +08:00
parent a5e5b3cf6e
commit 01975afe9c
31 changed files with 2338 additions and 1538 deletions

View File

@@ -1,47 +1,49 @@
package udm
import "be.ems/src/modules/crontask/processor/syncNbiNRM/common"
type ManagedElement struct {
Id string `json:"id"`
UserLabel string `json:"userLabel"`
VendorName string `json:"vendorName"`
ManagedBy string `json:"managedBy"`
ManagementIpAddress string `json:"managementIpAddress"`
SwVersion string `json:"swVersion"`
PatchInfo string `json:"patchInfo"`
AdministrativeState string `json:"administrativeState"`
OperationalState string `json:"operationalState"`
Id string `json:"id"`
UserLabel string `json:"userLabel"`
VendorName string `json:"vendorName"`
ManagedBy string `json:"managedBy"`
ManagementIpAddress string `json:"managementIpAddress"`
SwVersion string `json:"swVersion"`
PatchInfo string `json:"patchInfo"`
AdministrativeState common.AdministrativeState `json:"administrativeState"`
OperationalState common.OperationalState `json:"operationalState"`
}
type UdmFunction struct {
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState string `json:"administrativeState"`
OperationalState string `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState common.AdministrativeState `json:"administrativeState"`
OperationalState common.OperationalState `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
}
type UdrFunction struct {
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState string `json:"administrativeState"`
OperationalState string `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
MaxNumSupi int `json:"maxNumSupi"`
MaxNumMsisdn int `json:"maxNumMsisdn"`
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState common.AdministrativeState `json:"administrativeState"`
OperationalState common.OperationalState `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
MaxNumSupi int `json:"maxNumSupi"`
MaxNumMsisdn int `json:"maxNumMsisdn"`
}
type AusfFunction struct {
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState string `json:"administrativeState"`
OperationalState string `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState common.AdministrativeState `json:"administrativeState"`
OperationalState common.OperationalState `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
Fqdn string `json:"fqdn"`
SbiServiceList string `json:"sbiServiceList"`
}
type IPResource struct {

View File

@@ -0,0 +1,284 @@
package udm
import (
"encoding/json"
"fmt"
"strconv"
"strings"
"be.ems/features/nbi/redisqueue"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
)
// SyncUdmNbiCM 从ne_info获取UDM网元并同步数据到nbi_cm表
func SyncNbiCM() error {
log.Info("Starting UDM NBI CM synchronization")
// 从ne_info表获取UDM类型的网元
var udmNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "UDM").
Find(&udmNEs).Error
if err != nil {
log.Errorf("Failed to query UDM network elements: %v", err)
return err
}
log.Infof("Found %d UDM network elements", len(udmNEs))
for _, ne := range udmNEs {
adminState, operState := common.ParseStateFromStatus(ne.Status)
var version string = "-"
err := dborm.DefaultDB().Table("ne_version").
Where("ne_type = ? and ne_id = ?", ne.NeType, ne.NeId).
Pluck("version", &version).Error
if err != nil {
log.Errorf("Failed to query %s version: %v", ne.NeName, err)
return err
}
// ========== ManagedElement ==========
managedElement := ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement", ne.NeType, ne.NeId),
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: version,
PatchInfo: "-",
AdministrativeState: adminState,
OperationalState: operState,
}
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for UDM ne_id %s: %v", ne.NeId, err)
continue
}
var lastJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "ManagedElement", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastJson).Error
newValueJson := string(meJSON)
if err != nil || lastJson == "" {
common.InsertNbiCm(ne, "ManagedElement", newValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "ManagedElement", newValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastJson, newValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "ManagedElement", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "ManagedElement", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "ManagedElement", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "ManagedElement", newValueJson, common.ObjectOriginalEvent)
}
}
// ========== UdmFunction ==========
udmFunction := UdmFunction{
Id: fmt.Sprintf("%s-%s-UdmFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-UdmFunction",
AdministrativeState: adminState,
OperationalState: operState,
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
Fqdn: fmt.Sprintf("%s%s.mnc000.mcc460.3gppnetwork.org", strings.ToLower(ne.NeType), ne.NeId),
SbiServiceList: "Nudm_UEAuthentication,Nudm_SubscriberDataManagement,Nudm_UEContextManagement",
}
udmJSON, err := json.Marshal(udmFunction)
if err != nil {
log.Errorf("Failed to marshal UdmFunction for ne_id %s: %v", ne.NeId, err)
continue
}
var lastUdmJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "UdmFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastUdmJson).Error
newUdmValueJson := string(udmJSON)
if err != nil || lastUdmJson == "" {
common.InsertNbiCm(ne, "UdmFunction", newUdmValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "UdmFunction", newUdmValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastUdmJson, newUdmValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdmFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdmFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdmFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "UdmFunction", newUdmValueJson, common.ObjectOriginalEvent)
}
}
// ========== UdrFunction ==========
var capability int
err = dborm.DefaultDB().Table("ne_license").Where("ne_type = ? and ne_id = ?", ne.NeType, ne.NeId).
Pluck("capability", &capability).Error
if err != nil {
log.Errorf("Failed to query capability for ne_id %s: %v", ne.NeId, err)
capability = 0
}
udrFunction := UdrFunction{
Id: fmt.Sprintf("%s-%s-UdrFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-UdrFunction",
AdministrativeState: adminState,
OperationalState: operState,
VnfInstanceId: "vnf-UDR-" + ne.NeId,
Fqdn: fmt.Sprintf("%s%s.mnc000.mcc460.3gppnetwork.org", strings.ToLower(ne.NeType), ne.NeId),
SbiServiceList: "Nudr_DataRepository",
MaxNumSupi: capability,
MaxNumMsisdn: capability,
}
udrJSON, err := json.Marshal(udrFunction)
if err != nil {
log.Errorf("Failed to marshal UdrFunction for ne_id %s: %v", ne.NeId, err)
} else {
var lastUdrJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "UdrFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastUdrJson).Error
newUdrValueJson := string(udrJSON)
if err != nil || lastUdrJson == "" {
common.InsertNbiCm(ne, "UdrFunction", newUdrValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "UdrFunction", newUdrValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastUdrJson, newUdrValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdrFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdrFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "UdrFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "UdrFunction", newUdrValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== AusfFunction ==========
ausfFunction := AusfFunction{
Id: fmt.Sprintf("%s-%s-AusfFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-AusfFunction",
AdministrativeState: adminState,
OperationalState: operState,
VnfInstanceId: "vnf-AUSF-" + ne.NeId,
Fqdn: fmt.Sprintf("ausf%s.mnc000.mcc460.3gppnetwork.org", ne.NeId),
SbiServiceList: "Nausf_UEAuthentication,Nausf_SoRProtection",
}
ausfJSON, err := json.Marshal(ausfFunction)
if err != nil {
log.Errorf("Failed to marshal AusfFunction for ne_id %s: %v", ne.NeId, err)
} else {
var lastAusfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "AusfFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastAusfJson).Error
newAusfValueJson := string(ausfJSON)
if err != nil || lastAusfJson == "" {
common.InsertNbiCm(ne, "AusfFunction", newAusfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "AusfFunction", newAusfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastAusfJson, newAusfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "AusfFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "AusfFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "AusfFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "AusfFunction", newAusfValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== IPResource ==========
ipResource := IPResource{
Id: fmt.Sprintf("%s-%s-IPResource", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N8,N10,N12,N21}",
LocIpV4AddrList: fmt.Sprintf("{%s,%s,%s,%s,%s}", ne.Ip, ne.Ip, ne.Ip, ne.Ip, ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default,Default}",
}
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for UDM ne_id %s: %v", ne.NeId, err)
} else {
var lastIpJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "IPResource", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastIpJson).Error
newIpValueJson := string(ipJSON)
if err != nil || lastIpJson == "" {
common.InsertNbiCm(ne, "IPResource", newIpValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "IPResource", newIpValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastIpJson, newIpValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "IPResource", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "IPResource", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "IPResource", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "IPResource", newIpValueJson, common.ObjectOriginalEvent)
}
}
}
}
log.Info("UDM NBI CM synchronization completed")
return nil
}