Files
be.ems/src/modules/crontask/processor/nbiNRM/syncUdm.go
2025-04-27 20:02:36 +08:00

222 lines
6.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package nbiNRM
import (
"encoding/json"
"fmt"
"time"
"be.ems/lib/dborm"
"be.ems/lib/log"
"be.ems/src/modules/crontask/processor/nbiNRM/common"
"be.ems/src/modules/crontask/processor/nbiNRM/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)
// 为每个网元生成ManagedElement记录
managedElement := udm.ManagedElement{
Id: "ME" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp),
UserLabel: ne.NeName,
VendorName: ne.VendorName,
ManagedBy: ne.Dn,
ManagementIpAddress: ne.Ip,
SwVersion: "",
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: "UF" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp),
UserLabel: ne.NeName + "-UdmFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId,
Fqdn: "udm-" + ne.NeId + ".5gc.3gpp.org",
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
}
// 为每个网元生成UdrFunction记录
udrFunction := udm.UdrFunction{
Id: "UDR" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp),
UserLabel: ne.NeName + "-UdrFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-UDR-" + ne.NeId,
Fqdn: "udr-" + ne.NeId + ".5gc.3gpp.org",
SbiServiceList: "Nudr_DataRepository",
MaxNumSupi: 800000,
MaxNumMsisdn: 800000,
}
// 序列化为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: "AUSF" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp),
UserLabel: ne.NeName + "-AusfFunction",
AdministrativeState: string(adminState),
OperationalState: string(operState),
VnfInstanceId: "vnf-AUSF-" + ne.NeId,
Fqdn: "ausf-" + ne.NeId + ".5gc.3gpp.org",
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: "IP" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp),
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
}