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

@@ -0,0 +1,234 @@
package pcf
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"
)
// SyncPcfNbiCM 从ne_info获取PCF网元并同步数据到nbi_cm表
func SyncNbiCM() error {
log.Info("Starting PCF NBI CM synchronization")
// 从ne_info表获取PCF类型的网元
var pcfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "PCF").
Find(&pcfNEs).Error
if err != nil {
log.Errorf("Failed to query PCF network elements: %v", err)
return err
}
log.Infof("Found %d PCF network elements", len(pcfNEs))
for _, ne := range pcfNEs {
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 PCF 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)
}
}
// ========== PcfFunction ==========
pcfFunction := PcfFunction{
Id: fmt.Sprintf("%s-%s-PcfFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-PcfFunction",
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: "Npcf_AMPolicyControl,Npcf_PolicyAuthorization,Npcf_SMPolicyControl,Npcf_BDTPolicyControl",
}
pcfJSON, err := json.Marshal(pcfFunction)
if err != nil {
log.Errorf("Failed to marshal PcfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
var lastPcfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "PcfFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastPcfJson).Error
newPcfValueJson := string(pcfJSON)
if err != nil || lastPcfJson == "" {
common.InsertNbiCm(ne, "PcfFunction", newPcfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "PcfFunction", newPcfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastPcfJson, newPcfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "PcfFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "PcfFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "PcfFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "PcfFunction", newPcfValueJson, 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,
MaxSubNbr: 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)
}
}
}
// ========== IPResource ==========
ipResource := IPResource{
Id: fmt.Sprintf("%s-%s-IPResource", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N5,N7,N15}",
LocIpV4AddrList: fmt.Sprintf("{%s,Default,Default,Default}", ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default}",
}
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for PCF 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("PCF NBI CM synchronization completed")
return nil
}