package nbiNRM import ( "encoding/json" "fmt" "time" "be.ems/lib/dborm" "be.ems/lib/log" "be.ems/src/modules/crontask/processor/nbiNRM/amf" "be.ems/src/modules/crontask/processor/nbiNRM/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) // 为每个网元生成ManagedElement记录 managedElement := amf.ManagedElement{ Id: "ME" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), // 生成唯一ID UserLabel: ne.NeName, VendorName: ne.VendorName, ManagedBy: ne.Dn, ManagementIpAddress: ne.Ip, // 可以从其他表获取IP地址 SwVersion: "", // 可以从其他表获取软件版本 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 } // 为每个网元生成AmfFunction记录 amfFunction := amf.AmfFunction{ Id: "AF" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), // 生成唯一ID UserLabel: ne.NeName + "-AmfFunction", AdministrativeState: adminState, OperationalState: operState, VnfInstanceId: "vnf-" + ne.NeType + "-" + ne.NeId, Fqdn: "amf-" + ne.NeId + ".5gc.3gpp.org", SbiServiceList: "Namf_Communication,Namf_EventExposure,Namf_MT,Namf_Location", AmfGuamiList: "[{\"mcc\":\"460\",\"mnc\":\"01\",\"amfId\":\"" + ne.NeId + "\"}]", SnssaiList: "[{\"sst\":1,\"sd\":\"010203\"}]", MaxUser: 1000000, 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 } // 生成唯一ID // amfID := fmt.Sprintf("nbi-cm-%s-amf-%d", ne.NeID, timestamp) // 插入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: "N8" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), UserLabel: ne.NeName + "-N8", 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: "N11" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), UserLabel: ne.NeName + "-N11", 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: "N12" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), UserLabel: ne.NeName + "-N12", 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: "IP" + fmt.Sprintf("-%s-%d", ne.NeId, timestamp), 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 }