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,326 @@
package amf
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"
)
// SyncAmfNbiCM 从ne_info获取AMF网元并同步数据到nbi_cm表
func SyncNbiCM() error {
log.Info("Starting AMF NBI CM synchronization")
// 从ne_info表获取AMF类型的网元
var amfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "AMF").
Find(&amfNEs).Error
if err != nil {
log.Errorf("Failed to query AMF network elements: %v", err)
return err
}
log.Infof("Found %d AMF network elements", len(amfNEs))
// 遍历每个AMF网元生成对应的NBI CM记录
for _, ne := range amfNEs {
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 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)
}
}
// ========== AmfFunction ==========
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
}
amfFunction := AmfFunction{
Id: fmt.Sprintf("%s-%s-AmfFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-AmfFunction",
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: "Namf_Communication,Namf_EventExposure,Namf_MT,Namf_Location",
AmfGuamiList: "[{\"mcc\":\"460\",\"mnc\":\"000\",\"amfId\":\"" + ne.NeId + "\"}]",
SnssaiList: "[{\"sst\":1,\"sd\":\"000001\"}]",
MaxUser: capability,
RelativeCapacity: 30,
MaxGnbNum: 100,
}
amfJSON, err := json.Marshal(amfFunction)
if err != nil {
log.Errorf("Failed to marshal AmfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
var lastAmfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "AmfFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastAmfJson).Error
newAmfValueJson := string(amfJSON)
if err != nil || lastAmfJson == "" {
common.InsertNbiCm(ne, "AmfFunction", newAmfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "AmfFunction", newAmfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastAmfJson, newAmfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "AmfFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "AmfFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "AmfFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "AmfFunction", newAmfValueJson, common.ObjectOriginalEvent)
}
}
// ========== EpRpDynN8Amf ==========
epRpDynN8Amf := EpRpDynN8Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN8Amf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-EpRpDynN8Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.10.0.0/16\"]",
}
n8JSON, err := json.Marshal(epRpDynN8Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN8Amf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN8Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN8Amf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN8Json).Error
newN8ValueJson := string(n8JSON)
if err != nil || lastN8Json == "" {
common.InsertNbiCm(ne, "EpRpDynN8Amf", newN8ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN8Amf", newN8ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN8Json, newN8ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN8Amf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN8Amf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN8Amf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN8Amf", newN8ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN11Amf ==========
epRpDynN11Amf := EpRpDynN11Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN11Amf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-EpRpDynN11Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.11.0.0/16\"]",
}
n11JSON, err := json.Marshal(epRpDynN11Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN11Amf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN11Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN11Amf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN11Json).Error
newN11ValueJson := string(n11JSON)
if err != nil || lastN11Json == "" {
common.InsertNbiCm(ne, "EpRpDynN11Amf", newN11ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN11Amf", newN11ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN11Json, newN11ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN11Amf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN11Amf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN11Amf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN11Amf", newN11ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN12Amf ==========
epRpDynN12Amf := EpRpDynN12Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN12Amf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-EpRpDynN12Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.12.0.0/16\"]",
}
n12JSON, err := json.Marshal(epRpDynN12Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN12Amf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN12Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN12Amf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN12Json).Error
newN12ValueJson := string(n12JSON)
if err != nil || lastN12Json == "" {
common.InsertNbiCm(ne, "EpRpDynN12Amf", newN12ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN12Amf", newN12ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN12Json, newN12ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN12Amf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN12Amf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN12Amf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN12Amf", newN12ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== IPResource ==========
ipResource := IPResource{
Id: fmt.Sprintf("%s-%s-IPResource", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N8,N11,N12}",
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 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("AMF NBI CM synchronization completed")
return nil
}

View File

@@ -30,6 +30,7 @@ type EventType int
const (
ObjectNullEvent EventType = iota // ObjectNullEvent 空事件
ObjectOriginalEvent // ObjectOriginalEvent 原始事件
ObjectCreationEvent // ObjectCreationEvent 创建事件
ObjectDeletionEvent // ObjectDeletionEvent 删除事件
ObjectAttributeValueChangeEvent // ObjectAttributeValueChangeEvent 修改事件
@@ -38,6 +39,8 @@ const (
func (et EventType) EventTypeEnumString() string {
switch et {
case ObjectOriginalEvent:
return "ObjectOriginalEvent"
case ObjectCreationEvent:
return "ObjectCreationEvent"
case ObjectDeletionEvent:
@@ -62,6 +65,8 @@ func EventTypeInt(s string) EventType {
}
switch s {
case "ObjectOriginalEvent":
return ObjectOriginalEvent
case "ObjectCreationEvent":
return ObjectCreationEvent
case "ObjectDeletionEvent":

View File

@@ -0,0 +1,95 @@
package common
import (
"encoding/json"
"fmt"
"reflect"
"time"
"be.ems/lib/dborm"
)
// CompareJSON 比较两个 JSON返回新增、修改和删除的内容
func CompareJSON(json1, json2 string) (map[string]interface{}, map[string]interface{}, map[string]interface{}, error) {
var map1, map2 map[string]interface{}
// 解析 JSON
if err := json.Unmarshal([]byte(json1), &map1); err != nil {
return nil, nil, nil, fmt.Errorf("failed to parse json1: %v", err)
}
if err := json.Unmarshal([]byte(json2), &map2); err != nil {
return nil, nil, nil, fmt.Errorf("failed to parse json2: %v", err)
}
// 存储新增、修改和删除的内容
added := make(map[string]interface{})
modified := make(map[string]interface{})
deleted := make(map[string]interface{})
// 递归比较
compareMaps(map1, map2, added, modified, deleted, "")
return added, modified, deleted, nil
}
// compareMaps 递归比较两个 map
func compareMaps(map1, map2, added, modified, deleted map[string]interface{}, prefix string) {
// 遍历 map1检查删除或修改
for key, val1 := range map1 {
fullKey := key
if prefix != "" {
fullKey = prefix + "." + key
}
val2, exists := map2[key]
if !exists {
// 如果 key 不存在于 map2记录为删除
deleted[fullKey] = val1
} else if !reflect.DeepEqual(val1, val2) {
// 如果 key 存在但值不同,记录为修改
if reflect.TypeOf(val1) == reflect.TypeOf(val2) && reflect.TypeOf(val1).Kind() == reflect.Map {
// 如果值是嵌套对象,递归比较
compareMaps(val1.(map[string]interface{}), val2.(map[string]interface{}), added, modified, deleted, fullKey)
} else {
modified[fullKey] = map[string]interface{}{
"old": val1,
"new": val2,
}
}
}
}
// 遍历 map2检查新增
for key, val2 := range map2 {
fullKey := key
if prefix != "" {
fullKey = prefix + "." + key
}
if _, exists := map1[key]; !exists {
// 如果 key 不存在于 map1记录为新增
added[fullKey] = val2
}
}
}
func InsertNbiCm(ne NeInfo, objType, valueJson string, eventType EventType) NbiCm {
nbiCM := NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: CmVersion,
RmUid: ne.RmUid,
EventType: eventType,
ObjectType: objType,
ValueJson: valueJson,
Timestamp: time.Now().Format("2006-01-02 15:04:05"),
}
dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM)
return nbiCM
}
func ToJson(m map[string]interface{}) string {
b, _ := json.Marshal(m)
return string(b)
}

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
}

View File

@@ -1,28 +1,34 @@
package smf
import "be.ems/src/modules/crontask/processor/syncNbiNRM/common"
const (
SMF string = "SMF" // 网元类型
)
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 SmfFunction 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"`
MaxPduSessions int `json:"maxPduSessions"`
MaxQfi int `json:"maxQfi"`
UpfList string `json:"upfList"`
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"`
MaxPduSessions int `json:"maxPduSessions"`
MaxQfi int `json:"maxQfi"`
UpfList string `json:"upfList"`
}
type AddrPool struct {

View File

@@ -0,0 +1,323 @@
package smf
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"
)
// SyncSmfNbiCM 从ne_info获取SMF网元并同步数据到nbi_cm表
func SyncNbiCM() error {
log.Info("Starting SMF NBI CM synchronization")
// 从ne_info表获取SMF类型的网元
var smfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "SMF").
Find(&smfNEs).Error
if err != nil {
log.Errorf("Failed to query SMF network elements: %v", err)
return err
}
log.Infof("Found %d SMF network elements", len(smfNEs))
for _, ne := range smfNEs {
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 SMF 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)
}
}
// ========== SmfFunction ==========
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
}
smfFunction := SmfFunction{
Id: fmt.Sprintf("%s-%s-SmfFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-SmfFunction",
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: "Nsmf_PDUSession,Nsmf_EventExposure",
MaxPduSessions: capability,
MaxQfi: 64,
UpfList: "[\"UPF-001\",\"UPF-2\"]",
}
smfJSON, err := json.Marshal(smfFunction)
if err != nil {
log.Errorf("Failed to marshal SmfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
var lastSmfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "SmfFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastSmfJson).Error
newSmfValueJson := string(smfJSON)
if err != nil || lastSmfJson == "" {
common.InsertNbiCm(ne, "SmfFunction", newSmfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "SmfFunction", newSmfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastSmfJson, newSmfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "SmfFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "SmfFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "SmfFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "SmfFunction", newSmfValueJson, common.ObjectOriginalEvent)
}
}
// ========== AddrPool ==========
addrPool := AddrPool{
Id: fmt.Sprintf("%s-%s-AddrPool", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-AddrPool",
AddrType: "UE",
IpVersion: "IPv4",
AddrSegList: "[\"10.60.0.0/16\",\"10.61.0.0/16\"]",
}
addrPoolJSON, err := json.Marshal(addrPool)
if err != nil {
log.Errorf("Failed to marshal AddrPool for ne_id %s: %v", ne.NeId, err)
} else {
var lastAddrPoolJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "AddrPool", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastAddrPoolJson).Error
newAddrPoolValueJson := string(addrPoolJSON)
if err != nil || lastAddrPoolJson == "" {
common.InsertNbiCm(ne, "AddrPool", newAddrPoolValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "AddrPool", newAddrPoolValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastAddrPoolJson, newAddrPoolValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "AddrPool", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "AddrPool", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "AddrPool", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "AddrPool", newAddrPoolValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN7Smf ==========
epRpDynN7Smf := EpRpDynN7Smf{
Id: fmt.Sprintf("%s-%s-EpRpDynN7Smf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-N7",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.17.0.0/16\"]",
}
n7JSON, err := json.Marshal(epRpDynN7Smf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN7Smf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN7Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN7Smf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN7Json).Error
newN7ValueJson := string(n7JSON)
if err != nil || lastN7Json == "" {
common.InsertNbiCm(ne, "EpRpDynN7Smf", newN7ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN7Smf", newN7ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN7Json, newN7ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN7Smf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN7Smf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN7Smf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN7Smf", newN7ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN10Smf ==========
epRpDynN10Smf := EpRpDynN10Smf{
Id: fmt.Sprintf("%s-%s-EpRpDynN10Smf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-N10",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.20.0.0/16\"]",
}
n10JSON, err := json.Marshal(epRpDynN10Smf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN10Smf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN10Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN10Smf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN10Json).Error
newN10ValueJson := string(n10JSON)
if err != nil || lastN10Json == "" {
common.InsertNbiCm(ne, "EpRpDynN10Smf", newN10ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN10Smf", newN10ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN10Json, newN10ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN10Smf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN10Smf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN10Smf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN10Smf", newN10ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== IPResource ==========
ipResource := IPResource{
Id: fmt.Sprintf("%s-%s-IPResource", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N4,N7,N10,N11}",
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 SMF 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("SMF NBI CM synchronization completed")
return nil
}

View File

@@ -1,276 +0,0 @@
package syncNbiNRM
import (
"encoding/json"
"fmt"
"strings"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/amf"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
)
// SyncAmfNbiCM 从ne_info获取AMF网元并同步数据到nbi_cm表
func (s *BarProcessor) SyncAmfNbiCM() error {
log.Info("Starting AMF NBI CM synchronization")
// 从ne_info表获取AMF类型的网元
var amfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "AMF").
Find(&amfNEs).Error
if err != nil {
log.Errorf("Failed to query AMF network elements: %v", err)
return err
}
log.Infof("Found %d AMF network elements", len(amfNEs))
// 当前时间戳
now := time.Now()
timestamp := now.Unix()
// 遍历每个AMF网元生成对应的NBI CM记录
for _, ne := range amfNEs {
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 := amf.ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip, // 可以从其他表获取IP地址
SwVersion: version, // 可以从其他表获取软件版本
PatchInfo: "-",
AdministrativeState: adminState,
OperationalState: operState,
}
// 序列化为JSON
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for ne_id %s: %v", ne.NeId, err)
continue
}
// 生成唯一ID
// meID := fmt.Sprintf("nbi-cm-%s-me-%d", ne.NeID, timestamp)
// 插入ManagedElement记录
nbiCM := common.NbiCm{
// Id: meID,
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "ManagedElement",
ValueJson: string(meJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert ManagedElement record: %v", err)
continue
}
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
}
// 为每个网元生成AmfFunction记录
amfFunction := amf.AmfFunction{
Id: fmt.Sprintf("%s-%s-AmfFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-AmfFunction",
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: "Namf_Communication,Namf_EventExposure,Namf_MT,Namf_Location",
AmfGuamiList: "[{\"mcc\":\"460\",\"mnc\":\"000\",\"amfId\":\"" + ne.NeId + "\"}]",
SnssaiList: "[{\"sst\":1,\"sd\":\"000001\"}]",
MaxUser: capability,
RelativeCapacity: 30,
MaxGnbNum: 100,
}
// 序列化为JSON
amfJSON, err := json.Marshal(amfFunction)
if err != nil {
log.Errorf("Failed to marshal AmfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
// 插入AmfFunction记录
nbiCM = common.NbiCm{
// Id: amfID,
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "AmfFunction",
ValueJson: string(amfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert AmfFunction record: %v", err)
continue
}
// 在 AmfFunction 记录创建完成后添加以下代码
// 创建 EpRpDynN8Amf 记录
epRpDynN8Amf := amf.EpRpDynN8Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN8Amf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-EpRpDynN8Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.10.0.0/16\"]",
}
// 序列化为JSON
n8JSON, err := json.Marshal(epRpDynN8Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN8Amf for ne_id %s: %v", ne.NeId, err)
} else {
// 插入EpRpDynN8Amf记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN8Amf",
ValueJson: string(n8JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN8Amf record: %v", err)
}
}
// 创建 EpRpDynN11Amf 记录
epRpDynN11Amf := amf.EpRpDynN11Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN11Amf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-EpRpDynN11Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.11.0.0/16\"]",
}
// 序列化为JSON
n11JSON, err := json.Marshal(epRpDynN11Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN11Amf for ne_id %s: %v", ne.NeId, err)
} else {
// 插入EpRpDynN11Amf记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN11Amf",
ValueJson: string(n11JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN11Amf record: %v", err)
}
}
// 创建 EpRpDynN12Amf 记录
epRpDynN12Amf := amf.EpRpDynN12Amf{
Id: fmt.Sprintf("%s-%s-EpRpDynN12Amf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-EpRpDynN12Amf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.12.0.0/16\"]",
}
// 序列化为JSON
n12JSON, err := json.Marshal(epRpDynN12Amf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN12Amf for ne_id %s: %v", ne.NeId, err)
} else {
// 插入EpRpDynN12Amf记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN12Amf",
ValueJson: string(n12JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN12Amf record: %v", err)
}
}
// 创建 IPResource 记录
ipResource := amf.IPResource{
Id: fmt.Sprintf("%s-%s-IPResource-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N8,N11,N12}",
LocIpV4AddrList: fmt.Sprintf("{%s,Default,Default,Default}", ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default}",
}
// 序列化为JSON
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for ne_id %s: %v", ne.NeId, err)
} else {
// 插入IPResource记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "IPResource",
ValueJson: string(ipJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert IPResource record: %v", err)
}
}
}
log.Info("AMF NBI CM synchronization completed")
return nil
}

View File

@@ -2,6 +2,11 @@ package syncNbiNRM
import (
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/amf"
"be.ems/src/modules/crontask/processor/syncNbiNRM/pcf"
"be.ems/src/modules/crontask/processor/syncNbiNRM/smf"
"be.ems/src/modules/crontask/processor/syncNbiNRM/udm"
"be.ems/src/modules/crontask/processor/syncNbiNRM/upf"
)
var NewProcessor = &BarProcessor{
@@ -34,27 +39,27 @@ func (s *BarProcessor) Execute(data any) (any, error) {
// sysJob := options.SysJob
// var params BarParams
err := s.SyncAmfNbiCM()
err := amf.SyncNbiCM()
if err != nil {
log.Errorf("SyncAmfNbiCM error: %v", err)
return nil, err
}
err = s.SyncPcfNbiCM()
err = pcf.SyncNbiCM()
if err != nil {
log.Errorf("SyncPcfNbiCM error: %v", err)
return nil, err
}
err = s.SyncUdmNbiCM()
err = udm.SyncNbiCM()
if err != nil {
log.Errorf("SyncUdmNbiCM error: %v", err)
return nil, err
}
err = s.SyncSmfNbiCM()
err = smf.SyncNbiCM()
if err != nil {
log.Errorf("SyncSmfNbiCM error: %v", err)
return nil, err
}
err = s.SyncUpfNbiCM()
err = upf.SyncNbiCM()
if err != nil {
log.Errorf("SyncUpfNbiCM error: %v", err)
return nil, err

View File

@@ -1,200 +0,0 @@
package syncNbiNRM
import (
"encoding/json"
"fmt"
"strings"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
"be.ems/src/modules/crontask/processor/syncNbiNRM/pcf"
)
// SyncPcfNbiCM 从ne_info获取PCF网元并同步数据到nbi_cm表
func (s *BarProcessor) SyncPcfNbiCM() 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))
// 当前时间戳
now := time.Now()
timestamp := now.Unix()
// 遍历每个PCF网元生成对应的NBI CM记录
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 := pcf.ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: version,
PatchInfo: "-",
AdministrativeState: adminState,
OperationalState: operState,
}
// 序列化为JSON
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for PCF ne_id %s: %v", ne.NeId, err)
continue
}
// 插入ManagedElement记录
nbiCM := common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "ManagedElement",
ValueJson: string(meJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert PCF ManagedElement record: %v", err)
continue
}
// 为每个网元生成PcfFunction记录
pcfFunction := pcf.PcfFunction{
Id: fmt.Sprintf("%s-%s-PcfFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
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",
}
// 序列化为JSON
pcfJSON, err := json.Marshal(pcfFunction)
if err != nil {
log.Errorf("Failed to marshal PcfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
// 插入PcfFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "PcfFunction",
ValueJson: string(pcfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert PcfFunction record: %v", err)
continue
}
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 := pcf.UdrFunction{
Id: fmt.Sprintf("%s-%s-UdrFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-UdrFunction",
AdministrativeState: adminState,
OperationalState: operState,
MaxSubNbr: capability,
}
// 序列化为JSON
udrJSON, err := json.Marshal(udrFunction)
if err != nil {
log.Errorf("Failed to marshal UdrFunction for ne_id %s: %v", ne.NeId, err)
} else {
// 插入UdrFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "UdrFunction",
ValueJson: string(udrJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UdrFunction record: %v", err)
}
}
// 创建 IPResource 记录
ipResource := pcf.IPResource{
Id: fmt.Sprintf("%s-%s-IPResource-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N5,N7,N15}",
LocIpV4AddrList: fmt.Sprintf("{%s,Default,Default,Default}", ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default}",
}
// 序列化为JSON
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for PCF ne_id %s: %v", ne.NeId, err)
} else {
// 插入IPResource记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "IPResource",
ValueJson: string(ipJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert PCF IPResource record: %v", err)
}
}
}
log.Info("PCF NBI CM synchronization completed")
return nil
}

View File

@@ -1,267 +0,0 @@
package syncNbiNRM
import (
"encoding/json"
"fmt"
"strings"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
"be.ems/src/modules/crontask/processor/syncNbiNRM/smf"
)
// SyncSmfNbiCM 从ne_info获取SMF网元并同步数据到nbi_cm表
func (s *BarProcessor) SyncSmfNbiCM() error {
log.Info("Starting SMF NBI CM synchronization")
// 从ne_info表获取SMF类型的网元
var smfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "SMF").
Find(&smfNEs).Error
if err != nil {
log.Errorf("Failed to query SMF network elements: %v", err)
return err
}
log.Infof("Found %d SMF network elements", len(smfNEs))
// 当前时间戳
now := time.Now()
timestamp := now.Unix()
// 遍历每个SMF网元生成对应的NBI CM记录
for _, ne := range smfNEs {
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 := smf.ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: version,
PatchInfo: "-",
AdministrativeState: string(adminState),
OperationalState: string(operState),
}
// 序列化为JSON
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for SMF ne_id %s: %v", ne.NeId, err)
continue
}
// 插入ManagedElement记录
nbiCM := common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "ManagedElement",
ValueJson: string(meJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert SMF ManagedElement record: %v", err)
continue
}
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
}
// 为每个网元生成SmfFunction记录
smfFunction := smf.SmfFunction{
Id: fmt.Sprintf("%s-%s-SmfFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-SmfFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
Fqdn: fmt.Sprintf("%s%s.mnc000.mcc460.3gppnetwork.org", strings.ToLower(ne.NeType), ne.NeId),
SbiServiceList: "Nsmf_PDUSession,Nsmf_EventExposure",
MaxPduSessions: capability,
MaxQfi: 64,
UpfList: "[\"UPF-001\",\"UPF-2\"]",
}
// 序列化为JSON
smfJSON, err := json.Marshal(smfFunction)
if err != nil {
log.Errorf("Failed to marshal SmfFunction for ne_id %s: %v", ne.NeId, err)
continue
}
// 插入SmfFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "SmfFunction",
ValueJson: string(smfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert SmfFunction record: %v", err)
continue
}
// 创建 AddrPool 记录
addrPool := smf.AddrPool{
Id: fmt.Sprintf("%s-%s-AddrPool-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-AddrPool",
AddrType: "UE",
IpVersion: "IPv4",
AddrSegList: "[\"10.60.0.0/16\",\"10.61.0.0/16\"]",
}
// 序列化为JSON
addrPoolJSON, err := json.Marshal(addrPool)
if err != nil {
log.Errorf("Failed to marshal AddrPool for ne_id %s: %v", ne.NeId, err)
} else {
// 插入AddrPool记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "AddrPool",
ValueJson: string(addrPoolJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert AddrPool record: %v", err)
}
}
// 创建 EpRpDynN7Smf 记录
epRpDynN7Smf := smf.EpRpDynN7Smf{
Id: fmt.Sprintf("%s-%s-EpRpDynN7Smf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-N7",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.17.0.0/16\"]",
}
// 序列化为JSON
n7JSON, err := json.Marshal(epRpDynN7Smf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN7Smf for ne_id %s: %v", ne.NeId, err)
} else {
// 插入EpRpDynN7Smf记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN7Smf",
ValueJson: string(n7JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN7Smf record: %v", err)
}
}
// 创建 EpRpDynN10Smf 记录
epRpDynN10Smf := smf.EpRpDynN10Smf{
Id: fmt.Sprintf("%s-%s-EpRpDynN10Smf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-N10",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.20.0.0/16\"]",
}
// 序列化为JSON
n10JSON, err := json.Marshal(epRpDynN10Smf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN10Smf for ne_id %s: %v", ne.NeId, err)
} else {
// 插入EpRpDynN10Smf记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN10Smf",
ValueJson: string(n10JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN10Smf record: %v", err)
}
}
// 创建 IPResource 记录
ipResource := smf.IPResource{
Id: fmt.Sprintf("%s-%s-IPResource-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N4,N7,N10,N11}",
LocIpV4AddrList: fmt.Sprintf("{%s,%s,%s,%s,%s}", ne.Ip, ne.Ip, ne.Ip, ne.Ip, ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default,Default}",
}
// 序列化为JSON
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for SMF ne_id %s: %v", ne.NeId, err)
} else {
// 插入IPResource记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "IPResource",
ValueJson: string(ipJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert SMF IPResource record: %v", err)
}
}
}
log.Info("SMF NBI CM synchronization completed")
return nil
}

View File

@@ -1,239 +0,0 @@
package syncNbiNRM
import (
"encoding/json"
"fmt"
"strings"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
"be.ems/src/modules/crontask/processor/syncNbiNRM/udm"
)
// SyncUdmNbiCM 从ne_info获取UDM网元并同步数据到nbi_cm表
func (s *BarProcessor) SyncUdmNbiCM() 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))
// 当前时间戳
now := time.Now()
timestamp := now.Unix()
// 遍历每个UDM网元生成对应的NBI CM记录
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 := udm.ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: version,
PatchInfo: "-",
AdministrativeState: string(adminState),
OperationalState: string(operState),
}
// 序列化为JSON
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for UDM ne_id %s: %v", ne.NeId, err)
continue
}
// 插入ManagedElement记录
nbiCM := common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "ManagedElement",
ValueJson: string(meJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UDM ManagedElement record: %v", err)
continue
}
// 为每个网元生成UdmFunction记录
udmFunction := udm.UdmFunction{
Id: fmt.Sprintf("%s-%s-UdmFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-UdmFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
Fqdn: fmt.Sprintf("%s%s.mnc000.mcc460.3gppnetwork.org", ne.NeType, ne.NeId),
SbiServiceList: "Nudm_UEAuthentication,Nudm_SubscriberDataManagement,Nudm_UEContextManagement",
}
// 序列化为JSON
udmJSON, err := json.Marshal(udmFunction)
if err != nil {
log.Errorf("Failed to marshal UdmFunction for ne_id %s: %v", ne.NeId, err)
continue
}
// 插入UdmFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "UdmFunction",
ValueJson: string(udmJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UdmFunction record: %v", err)
continue
}
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 := udm.UdrFunction{
Id: fmt.Sprintf("%s-%s-UdrFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-UdrFunction",
AdministrativeState: string(adminState),
OperationalState: string(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,
}
// 序列化为JSON
udrJSON, err := json.Marshal(udrFunction)
if err != nil {
log.Errorf("Failed to marshal UdrFunction for ne_id %s: %v", ne.NeId, err)
} else {
// 插入UdrFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "UdrFunction",
ValueJson: string(udrJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UdrFunction record: %v", err)
}
}
// 为每个网元生成AusfFunction记录
ausfFunction := udm.AusfFunction{
Id: fmt.Sprintf("%s-%s-AusfFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-AusfFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-AUSF-" + ne.NeId,
Fqdn: fmt.Sprintf("ausf%s.mnc000.mcc460.3gppnetwork.org", ne.NeId),
SbiServiceList: "Nausf_UEAuthentication,Nausf_SoRProtection",
}
// 序列化为JSON
ausfJSON, err := json.Marshal(ausfFunction)
if err != nil {
log.Errorf("Failed to marshal AusfFunction for ne_id %s: %v", ne.NeId, err)
} else {
// 插入AusfFunction记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "AusfFunction",
ValueJson: string(ausfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert AusfFunction record: %v", err)
}
}
// 创建 IPResource 记录
ipResource := udm.IPResource{
Id: fmt.Sprintf("%s-%s-IPResource-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
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,DefaultDefault}",
}
// 序列化为JSON
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for UDM ne_id %s: %v", ne.NeId, err)
} else {
// 插入IPResource记录
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "IPResource",
ValueJson: string(ipJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UDM IPResource record: %v", err)
}
}
}
log.Info("UDM NBI CM synchronization completed")
return nil
}

View File

@@ -1,410 +0,0 @@
package syncNbiNRM
import (
"encoding/json"
"fmt"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
"be.ems/src/modules/crontask/processor/syncNbiNRM/upf"
)
// SyncUpfNbiCM 从ne_info获取UPF网元并同步数据到nbi_cm表
func (s *BarProcessor) SyncUpfNbiCM() error {
log.Info("Starting UPF NBI CM synchronization")
// 从ne_info表获取UPF类型的网元
var upfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "UPF").
Find(&upfNEs).Error
if err != nil {
log.Errorf("Failed to query UPF network elements: %v", err)
return err
}
log.Infof("Found %d UPF network elements", len(upfNEs))
// 当前时间戳
now := time.Now()
timestamp := now.Unix()
// 遍历每个UPF网元生成对应的NBI CM记录
for _, ne := range upfNEs {
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 := upf.ManagedElement{
Id: fmt.Sprintf("%s-%s-ManagedElement-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: version,
PatchInfo: "-",
AdministrativeState: string(adminState),
OperationalState: string(operState),
}
// 序列化为JSON
meJSON, err := json.Marshal(managedElement)
if err != nil {
log.Errorf("Failed to marshal ManagedElement for UPF ne_id %s: %v", ne.NeId, err)
continue
}
// 插入ManagedElement记录
nbiCM := common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "ManagedElement",
ValueJson: string(meJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
// 插入到数据库
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UPF ManagedElement record: %v", err)
continue
}
// 生成 InventoryUnitRack 记录
inventoryUnitRack := upf.InventoryUnitRack{
Id: fmt.Sprintf("%s-%s-InventoryUnitRack-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-InventoryUnitRack",
VendorUnitFamilyType: "5G-UPF",
VendorUnitTypeNumber: "UPF-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
RackPosition: "1",
}
// 序列化为JSON
rackJSON, err := json.Marshal(inventoryUnitRack)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitRack for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "InventoryUnitRack",
ValueJson: string(rackJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert InventoryUnitRack record: %v", err)
}
}
// 生成 InventoryUnitShelf 记录
inventoryUnitShelf := upf.InventoryUnitShelf{
Id: fmt.Sprintf("%s-%s-InventoryUnitShelf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-InventoryUnitShelf",
VendorUnitFamilyType: "5G-UPF-SHELF",
VendorUnitTypeNumber: "UPF-SHELF-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-SHELF-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
ShelfPosition: "1",
}
// 序列化为JSON
shelfJSON, err := json.Marshal(inventoryUnitShelf)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitShelf for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "InventoryUnitShelf",
ValueJson: string(shelfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert InventoryUnitShelf record: %v", err)
}
}
// 生成 InventoryUnitPack 记录
inventoryUnitPack := upf.InventoryUnitPack{
Id: fmt.Sprintf("%s-%s-InventoryUnitPack-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-InventoryUnitPack",
VendorUnitFamilyType: "5G-UPF-PACK",
VendorUnitTypeNumber: "UPF-PACK-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-PACK-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
SlotsOccupied: "1,2",
}
// 序列化为JSON
packJSON, err := json.Marshal(inventoryUnitPack)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitPack for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "InventoryUnitPack",
ValueJson: string(packJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert InventoryUnitPack record: %v", err)
}
}
// 生成 InventoryUnitHost 记录
inventoryUnitHost := upf.InventoryUnitHost{
Id: fmt.Sprintf("%s-%s-InventoryUnitHost-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-InventoryUnitHost",
VendorUnitFamilyType: "5G-UPF-HOST",
VendorUnitTypeNumber: "UPF-HOST-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-HOST-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
HostPosition: "1",
NumberOfCpu: "16",
MemSize: "64GB",
HardDiskSize: "1TB",
}
// 序列化为JSON
hostJSON, err := json.Marshal(inventoryUnitHost)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitHost for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "InventoryUnitHost",
ValueJson: string(hostJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert InventoryUnitHost record: %v", err)
}
}
// 生成 InventoryUnitAccessory 记录
inventoryUnitAccessory := upf.InventoryUnitAccessory{
Id: fmt.Sprintf("%s-%s-InventoryUnitAccessory-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-InventoryUnitAccessory",
VendorUnitFamilyType: "5G-UPF-ACC",
VendorUnitTypeNumber: "UPF-ACC-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-ACC-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
AccessoryPosition: "1",
AccessoryType: "FAN",
AddtionalInformation: "Cooling system",
}
// 序列化为JSON
accJSON, err := json.Marshal(inventoryUnitAccessory)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitAccessory for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "InventoryUnitAccessory",
ValueJson: string(accJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert InventoryUnitAccessory record: %v", err)
}
}
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
}
// 生成 UpfFunction 记录
upfFunction := upf.UpfFunction{
Id: fmt.Sprintf("%s-%s-UpfFunction-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-UpfFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
MaxQosFlows: fmt.Sprintf("%d", capability),
MaxThroughput: "10Gbps",
}
// 序列化为JSON
upfJSON, err := json.Marshal(upfFunction)
if err != nil {
log.Errorf("Failed to marshal UpfFunction for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "UpfFunction",
ValueJson: string(upfJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UpfFunction record: %v", err)
}
}
// 创建 EpRpDynN3Upf 记录
epRpDynN3Upf := upf.EpRpDynN3Upf{
Id: fmt.Sprintf("%s-%s-EpRpDynN3Upf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-EpRpDynN3Upf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.13.0.0/16\"]",
}
// 序列化为JSON
n3JSON, err := json.Marshal(epRpDynN3Upf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN3Upf for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN3Upf",
ValueJson: string(n3JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN3Upf record: %v", err)
}
}
// 创建 EpRpDynN9Upf 记录
epRpDynN9Upf := upf.EpRpDynN9Upf{
Id: fmt.Sprintf("%s-%s-EpRpDynN9Upf-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-EpRpDynN9Upf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.19.0.0/16\"]",
}
// 序列化为JSON
n9JSON, err := json.Marshal(epRpDynN9Upf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN9Upf for ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "EpRpDynN9Upf",
ValueJson: string(n9JSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert EpRpDynN9Upf record: %v", err)
}
}
// 创建 IPResource 记录
ipResource := upf.IPResource{
Id: fmt.Sprintf("%s-%s-IPResource-%d", ne.NeType, ne.NeId, timestamp), // 生成唯一ID
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N3,N4,N9}",
LocIpV4AddrList: fmt.Sprintf("{%s,%s,%s,%s}", ne.Ip, ne.Ip, ne.Ip, ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default}",
}
// 序列化为JSON
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for UPF ne_id %s: %v", ne.NeId, err)
} else {
nbiCM = common.NbiCm{
NeType: ne.NeType,
NeId: ne.NeId,
CmVersion: common.CmVersion,
RmUid: ne.RmUid,
EventType: common.EventTypeInt("ObjectCreationEvent"),
ObjectType: "IPResource",
ValueJson: string(ipJSON),
Timestamp: now.Format("2006-01-02 15:04:05"),
}
err = dborm.DefaultDB().Table("nbi_cm").Create(&nbiCM).Error
if err != nil {
log.Errorf("Failed to insert UPF IPResource record: %v", err)
}
}
}
log.Info("UPF NBI CM synchronization completed")
return nil
}

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
}

View File

@@ -1,15 +1,17 @@
package upf
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 InventoryUnitRack struct {
@@ -88,13 +90,13 @@ type InventoryUnitAccessory struct {
}
type UpfFunction struct {
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState string `json:"administrativeState"`
OperationalState string `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
MaxQosFlows string `json:"maxQosFlows"`
MaxThroughput string `json:"maxThroughput"`
Id string `json:"id"`
UserLabel string `json:"userLabel"`
AdministrativeState common.AdministrativeState `json:"administrativeState"`
OperationalState common.OperationalState `json:"operationalState"`
VnfInstanceId string `json:"vnfInstanceId"`
MaxQosFlows string `json:"maxQosFlows"`
MaxThroughput string `json:"maxThroughput"`
}
type EpRpDynN9Upf struct {
@@ -121,30 +123,30 @@ type AmfFunction struct {
}
type SmfFunction 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"`
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"`
}
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"`
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"`
}
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"`
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"`
}
type IPResource struct {

View File

@@ -0,0 +1,534 @@
package upf
import (
"encoding/json"
"fmt"
"strconv"
"time"
"be.ems/features/nbi/redisqueue"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/syncNbiNRM/common"
)
// SyncUpfNbiCM 从ne_info获取UPF网元并同步数据到nbi_cm表
func SyncNbiCM() error {
log.Info("Starting UPF NBI CM synchronization")
// 从ne_info表获取UPF类型的网元
var upfNEs []common.NeInfo
err := dborm.DefaultDB().Table("ne_info").Where("ne_type = ?", "UPF").
Find(&upfNEs).Error
if err != nil {
log.Errorf("Failed to query UPF network elements: %v", err)
return err
}
log.Infof("Found %d UPF network elements", len(upfNEs))
now := time.Now()
for _, ne := range upfNEs {
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 UPF 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)
}
}
// ========== InventoryUnitRack ==========
inventoryUnitRack := InventoryUnitRack{
Id: fmt.Sprintf("%s-%s-InventoryUnitRack", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-InventoryUnitRack",
VendorUnitFamilyType: "5G-UPF",
VendorUnitTypeNumber: "UPF-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
RackPosition: "1",
}
rackJSON, err := json.Marshal(inventoryUnitRack)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitRack for ne_id %s: %v", ne.NeId, err)
} else {
var lastRackJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "InventoryUnitRack", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastRackJson).Error
newRackValueJson := string(rackJSON)
if err != nil || lastRackJson == "" {
common.InsertNbiCm(ne, "InventoryUnitRack", newRackValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "InventoryUnitRack", newRackValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastRackJson, newRackValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitRack", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitRack", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitRack", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "InventoryUnitRack", newRackValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== InventoryUnitShelf ==========
inventoryUnitShelf := InventoryUnitShelf{
Id: fmt.Sprintf("%s-%s-InventoryUnitShelf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-InventoryUnitShelf",
VendorUnitFamilyType: "5G-UPF-SHELF",
VendorUnitTypeNumber: "UPF-SHELF-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-SHELF-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
ShelfPosition: "1",
}
shelfJSON, err := json.Marshal(inventoryUnitShelf)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitShelf for ne_id %s: %v", ne.NeId, err)
} else {
var lastShelfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "InventoryUnitShelf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastShelfJson).Error
newShelfValueJson := string(shelfJSON)
if err != nil || lastShelfJson == "" {
common.InsertNbiCm(ne, "InventoryUnitShelf", newShelfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "InventoryUnitShelf", newShelfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastShelfJson, newShelfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitShelf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitShelf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitShelf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "InventoryUnitShelf", newShelfValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== InventoryUnitPack ==========
inventoryUnitPack := InventoryUnitPack{
Id: fmt.Sprintf("%s-%s-InventoryUnitPack", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-InventoryUnitPack",
VendorUnitFamilyType: "5G-UPF-PACK",
VendorUnitTypeNumber: "UPF-PACK-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-PACK-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
SlotsOccupied: "1,2",
}
packJSON, err := json.Marshal(inventoryUnitPack)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitPack for ne_id %s: %v", ne.NeId, err)
} else {
var lastPackJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "InventoryUnitPack", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastPackJson).Error
newPackValueJson := string(packJSON)
if err != nil || lastPackJson == "" {
common.InsertNbiCm(ne, "InventoryUnitPack", newPackValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "InventoryUnitPack", newPackValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastPackJson, newPackValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitPack", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitPack", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitPack", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "InventoryUnitPack", newPackValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== InventoryUnitHost ==========
inventoryUnitHost := InventoryUnitHost{
Id: fmt.Sprintf("%s-%s-InventoryUnitHost", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-InventoryUnitHost",
VendorUnitFamilyType: "5G-UPF-HOST",
VendorUnitTypeNumber: "UPF-HOST-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-HOST-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
HostPosition: "1",
NumberOfCpu: "16",
MemSize: "64GB",
HardDiskSize: "1TB",
}
hostJSON, err := json.Marshal(inventoryUnitHost)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitHost for ne_id %s: %v", ne.NeId, err)
} else {
var lastHostJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "InventoryUnitHost", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastHostJson).Error
newHostValueJson := string(hostJSON)
if err != nil || lastHostJson == "" {
common.InsertNbiCm(ne, "InventoryUnitHost", newHostValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "InventoryUnitHost", newHostValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastHostJson, newHostValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitHost", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitHost", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitHost", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "InventoryUnitHost", newHostValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== InventoryUnitAccessory ==========
inventoryUnitAccessory := InventoryUnitAccessory{
Id: fmt.Sprintf("%s-%s-InventoryUnitAccessory", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-InventoryUnitAccessory",
VendorUnitFamilyType: "5G-UPF-ACC",
VendorUnitTypeNumber: "UPF-ACC-" + ne.NeId,
VendorName: ne.VendorName,
SerialNumber: "SN-UPF-ACC-" + ne.NeId,
VersionNumber: "1.0.0",
DateOfManufacture: "2023-01-01",
DateOfLastService: now.Format("2006-01-02"),
ManufacturerData: "{}",
AccessoryPosition: "1",
AccessoryType: "FAN",
AddtionalInformation: "Cooling system",
}
accJSON, err := json.Marshal(inventoryUnitAccessory)
if err != nil {
log.Errorf("Failed to marshal InventoryUnitAccessory for ne_id %s: %v", ne.NeId, err)
} else {
var lastAccJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "InventoryUnitAccessory", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastAccJson).Error
newAccValueJson := string(accJSON)
if err != nil || lastAccJson == "" {
common.InsertNbiCm(ne, "InventoryUnitAccessory", newAccValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "InventoryUnitAccessory", newAccValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastAccJson, newAccValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitAccessory", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitAccessory", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "InventoryUnitAccessory", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "InventoryUnitAccessory", newAccValueJson, common.ObjectOriginalEvent)
}
}
}
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
}
// ========== UpfFunction ==========
upfFunction := UpfFunction{
Id: fmt.Sprintf("%s-%s-UpfFunction", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-UpfFunction",
AdministrativeState: adminState,
OperationalState: operState,
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
MaxQosFlows: fmt.Sprintf("%d", capability),
MaxThroughput: "10Gbps",
}
upfJSON, err := json.Marshal(upfFunction)
if err != nil {
log.Errorf("Failed to marshal UpfFunction for ne_id %s: %v", ne.NeId, err)
} else {
var lastUpfJson string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "UpfFunction", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastUpfJson).Error
newUpfValueJson := string(upfJSON)
if err != nil || lastUpfJson == "" {
common.InsertNbiCm(ne, "UpfFunction", newUpfValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "UpfFunction", newUpfValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastUpfJson, newUpfValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "UpfFunction", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "UpfFunction", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "UpfFunction", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "UpfFunction", newUpfValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN3Upf ==========
epRpDynN3Upf := EpRpDynN3Upf{
Id: fmt.Sprintf("%s-%s-EpRpDynN3Upf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-EpRpDynN3Upf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.13.0.0/16\"]",
}
n3JSON, err := json.Marshal(epRpDynN3Upf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN3Upf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN3Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN3Upf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN3Json).Error
newN3ValueJson := string(n3JSON)
if err != nil || lastN3Json == "" {
common.InsertNbiCm(ne, "EpRpDynN3Upf", newN3ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN3Upf", newN3ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN3Json, newN3ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN3Upf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN3Upf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN3Upf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN3Upf", newN3ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== EpRpDynN9Upf ==========
epRpDynN9Upf := EpRpDynN9Upf{
Id: fmt.Sprintf("%s-%s-EpRpDynN9Upf", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-EpRpDynN9Upf",
LocIpAddrList: "[\"" + ne.Ip + "\"]",
FarIpSubnetworkList: "[\"10.19.0.0/16\"]",
}
n9JSON, err := json.Marshal(epRpDynN9Upf)
if err != nil {
log.Errorf("Failed to marshal EpRpDynN9Upf for ne_id %s: %v", ne.NeId, err)
} else {
var lastN9Json string
err = dborm.DefaultDB().Table("nbi_cm").
Where("ne_type = ? AND ne_id = ? AND object_type = ? AND event_type = ?", ne.NeType, ne.NeId, "EpRpDynN9Upf", common.ObjectOriginalEvent).
Order("timestamp ASC").Pluck("value_json", &lastN9Json).Error
newN9ValueJson := string(n9JSON)
if err != nil || lastN9Json == "" {
common.InsertNbiCm(ne, "EpRpDynN9Upf", newN9ValueJson, common.ObjectOriginalEvent)
nbiCm := common.InsertNbiCm(ne, "EpRpDynN9Upf", newN9ValueJson, common.ObjectCreationEvent)
redisqueue.AddNbiCMQueue([]string{strconv.Itoa(nbiCm.Id)})
} else {
var ids []string
added, modified, deleted, _ := common.CompareJSON(lastN9Json, newN9ValueJson)
if len(added) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN9Upf", common.ToJson(added), common.ObjectCreationEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(modified) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN9Upf", common.ToJson(modified), common.ObjectAttributeValueChangeEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(deleted) > 0 {
nbiCm := common.InsertNbiCm(ne, "EpRpDynN9Upf", common.ToJson(deleted), common.ObjectDeletionEvent)
ids = append(ids, strconv.Itoa(nbiCm.Id))
}
if len(ids) > 0 {
redisqueue.AddNbiCMQueue(ids)
common.InsertNbiCm(ne, "EpRpDynN9Upf", newN9ValueJson, common.ObjectOriginalEvent)
}
}
}
// ========== IPResource ==========
ipResource := IPResource{
Id: fmt.Sprintf("%s-%s-IPResource", ne.NeType, ne.NeId),
UserLabel: ne.NeName + "-IPResource",
InterfaceType: "{Mgt,N3,N4,N9}",
LocIpV4AddrList: fmt.Sprintf("{%s,%s,%s,%s}", ne.Ip, ne.Ip, ne.Ip, ne.Ip),
LocIpV6AddrList: "{Default,Default,Default,Default}",
}
ipJSON, err := json.Marshal(ipResource)
if err != nil {
log.Errorf("Failed to marshal IPResource for UPF 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("UPF NBI CM synchronization completed")
return nil
}