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

1
.gitignore vendored
View File

@@ -38,3 +38,4 @@ vendor
*.bak
*.bak*
*.exe
__debug_bin*

View File

@@ -1,48 +1,34 @@
-- MariaDB dump 10.19 Distrib 10.6.16-MariaDB, for debian-linux-gnu (x86_64)
--
-- Host: 192.168.2.219 Database: omc_db
-- ------------------------------------------------------
-- Server version 10.3.38-MariaDB
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `alarm_relation`
--
DROP TABLE IF EXISTS `alarm_relation`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `alarm_relation` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) DEFAULT NULL,
`updated_at` datetime(3) DEFAULT NULL,
`deleted_at` datetime(3) DEFAULT NULL,
`root_alarm_id` varchar(64) DEFAULT NULL,
`alarm_id` varchar(64) DEFAULT NULL,
`relation_type` varchar(64) DEFAULT NULL,
`add_info` varchar(64) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE,
KEY `idx_alarm_relation_deleted_at` (`deleted_at`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2024-03-06 17:26:55
/*
Navicat Premium Data Transfer
Source Server : root@192.168.2.165
Source Server Type : MariaDB
Source Server Version : 100335 (10.3.35-MariaDB)
Source Host : 192.168.2.165:33066
Source Schema : omc_db
Target Server Type : MariaDB
Target Server Version : 100335 (10.3.35-MariaDB)
File Encoding : 65001
Date: 16/05/2025 10:48:43
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for alarm_relation
-- ----------------------------
DROP TABLE IF EXISTS `alarm_relation`;
CREATE TABLE `alarm_relation` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`root_alarm_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`alarm_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`relation_type` enum('derived','related') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '衍生关系derived/related: 父子/兄弟',
`add_info` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`created_at` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -0,0 +1,20 @@
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
CREATE TABLE `alarm_relation` (
`id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`root_alarm_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`alarm_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`relation_type` enum('derived','related') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '衍生关系derived/related: 父子/兄弟',
`add_info` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
`created_at` datetime NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
ALTER TABLE `alarm_relation`
DROP COLUMN IF EXISTS `updated_at`,
DROP COLUMN IF EXISTS `deleted_at`,
MODIFY COLUMN IF EXISTS `relation_type` enum('derived','related') CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL COMMENT '衍生关系derived/related: 父子/兄弟' AFTER `alarm_id`,
MODIFY COLUMN IF EXISTS `created_at` datetime NULL DEFAULT NULL AFTER `add_info`;
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -10,6 +10,7 @@ import (
"strings"
"time"
"be.ems/features/nbi/redisqueue"
"be.ems/lib/config"
"be.ems/lib/dborm"
"be.ems/lib/global"
@@ -476,6 +477,24 @@ func PostAlarmFromNF(w http.ResponseWriter, r *http.Request) {
log.Error("Failed to AlarmSMSForward:", err)
}
}
if config.GetYamlConfig().Alarm.RelatedRules.Enabled {
relatedId, found := JudgeRelated(&alarmData, session, config.RelationRules.Related)
if found {
id, err := InsertAlarmRelation(session, alarmData.AlarmId, relatedId, "related")
if err != nil {
log.Error("Failed to insert alarm relation:", err)
}
redisqueue.AddAlarmRelationQueue([]string{strconv.FormatInt(id, 10)})
}
derivedId, found := JudgeDerived(&alarmData, session)
if found {
id, err := InsertAlarmRelation(session, alarmData.AlarmId, derivedId, "derived")
if err != nil {
log.Error("Failed to insert alarm relation:", err)
}
redisqueue.AddAlarmRelationQueue([]string{strconv.FormatInt(id, 10)})
}
}
}
services.ResponseStatusOK200Null(w)

78
features/fm/relation.go Normal file
View File

@@ -0,0 +1,78 @@
package fm
import (
"time"
"be.ems/lib/config"
"xorm.io/xorm"
)
func JudgeDerived(alarm *Alarm, session *xorm.Session) (parentId string, found bool) {
for _, rule := range config.RelationRules.Derived {
if alarm.AlarmCode == rule.ChildCode {
// 查询是否有 parent_code 的告警
var parent Alarm
has, _ := session.Table("alarm").
Where("alarm_code=? AND ne_id=? AND alarm_status=1", rule.ParentCode, alarm.NeId).
Get(&parent)
if has {
return parent.AlarmId, true
}
}
}
return "", false
}
func JudgeRelated(alarm *Alarm, session *xorm.Session, rules []config.RelatedRule) (relatedId string, found bool) {
for _, rule := range rules {
// 1. 检查 ne_type 是否匹配
if rule.NeType != "" && alarm.NeType != rule.NeType {
continue
}
// 2. 检查 code 是否在 codes 列表
codeMatch := false
for _, c := range rule.Codes {
if alarm.AlarmCode == c {
codeMatch = true
break
}
}
if !codeMatch {
continue
}
// 3. 查询同一ne_id、codes列表内、时间窗口内的其他告警
var brother Alarm
parsedTime, err := time.Parse(time.RFC3339, alarm.EventTime)
if err != nil {
continue
}
timeStart := parsedTime.Add(-time.Duration(rule.TimeWindow) * time.Second)
has, _ := session.Table("alarm").
Where("ne_id=? AND alarm_code IN (?) AND event_time BETWEEN ? AND ? AND alarm_id!=?",
alarm.NeId, rule.Codes, timeStart, alarm.EventTime, alarm.AlarmId).
Get(&brother)
if has {
return brother.AlarmId, true
}
}
return "", false
}
type AlarmRelation struct {
Id int64 `xorm:"pk autoincr 'id'"`
AlarmId string `xorm:"alarm_id"`
RelatedAlarmId string `xorm:"related_alarm_id"`
RelationType string `xorm:"relation_type"`
CreateAt time.Time `xorm:"create_at"`
}
func InsertAlarmRelation(session *xorm.Session, alarmId, relatedId, relationType string) (int64, error) {
relation := &AlarmRelation{
AlarmId: alarmId,
RelatedAlarmId: relatedId,
RelationType: relationType,
CreateAt: time.Now(),
}
_, err := session.Insert(relation)
return relation.Id, err // 返回插入的ID
}

View File

@@ -0,0 +1,103 @@
package redisqueue
import (
"context"
"encoding/json"
"be.ems/lib/log"
redisdb "be.ems/src/framework/database/redis"
"github.com/redis/go-redis/v9"
)
// 写入 alarm_relation 消息
func AddAlarmRelationQueue(ids []string) error {
payload := map[string][]string{"ids": ids}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
values := map[string]interface{}{
"payload": string(payloadBytes),
}
_, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "alarm_relation",
Values: values,
}).Result()
// 只保留最新2000条消息
client.XTrimMaxLen(context.Background(), "alarm_relation", 2000).Result()
return err
}
// 写入 nbi_cm 消息
func AddNbiCMQueue(ids []string) error {
payload := map[string][]string{"ids": ids}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
values := map[string]interface{}{
"payload": string(payloadBytes),
}
_, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "nbi_cm",
Values: values,
}).Result()
// 只保留最新2000条消息
client.XTrimMaxLen(context.Background(), "nbi_cm", 2000).Result()
return err
}
// 写入 nbi_pm 消息
func AddNbiPMQueue(neType, id string) error {
payload := map[string]string{"neType": neType, "id": id}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return err
}
values := map[string]interface{}{
"payload": string(payloadBytes),
}
_, err = client.XAdd(ctx, &redis.XAddArgs{
Stream: "nbi_pm",
Values: values,
}).Result()
// 只保留最新2000条消息
client.XTrimMaxLen(context.Background(), "nbi_pm", 2000).Result()
return err
}
// 读取并打印指定 stream 的最新消息
func ReadLatest(stream string) error {
res, err := client.XRevRangeN(ctx, stream, "+", "-", 1).Result()
if err != nil {
return err
}
for _, msg := range res {
log.Tracef("Stream: %s, ID: %s, Values: %v\n", stream, msg.ID, msg.Values)
}
return nil
}
var ctx = context.Background()
var client *redis.Client
func InitRedisQueue() {
// var cancel context.CancelFunc
// ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
// defer cancel()
client = redisdb.RDB("")
}
func CloseRedisQueue() {
if err := client.Close(); err != nil {
log.Errorf("redis db close: %s", err)
}
}

View File

@@ -10,6 +10,7 @@ import (
"strings"
"time"
"be.ems/features/nbi/redisqueue"
"be.ems/features/pm/kpi_c_report"
"be.ems/features/pm/kpi_c_title"
"be.ems/lib/config"
@@ -338,6 +339,12 @@ func PostKPIReportFromNF(w http.ResponseWriter, r *http.Request) {
}
}
// 推送到redis队列
err = redisqueue.AddNbiPMQueue(kpiData.NEType, strconv.Itoa(kpiData.ID))
if err != nil {
log.Warn("Failed to AddNbiPMQueue:", err)
}
services.ResponseStatusOK204NoContent(w)
}

1
go.mod
View File

@@ -37,6 +37,7 @@ require (
golang.org/x/term v0.28.0
golang.org/x/text v0.21.0
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.25.11

1
go.sum
View File

@@ -575,6 +575,7 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -0,0 +1,31 @@
package config
import (
"os"
"gopkg.in/yaml.v2"
)
type DerivedRule struct {
ParentCode int `yaml:"parentCode"`
ChildCode int `yaml:"childCode"`
}
type RelatedRule struct {
Codes []int `yaml:"codes"`
NeType string `yaml:"neType"`
TimeWindow int `yaml:"timeWindow"`
}
type AlarmRelationRules struct {
Derived []DerivedRule `yaml:"derived"`
Related []RelatedRule `yaml:"related"`
}
var RelationRules AlarmRelationRules
func LoadAlarmRelationRules(path string) error {
data, err := os.ReadFile(path)
if err != nil {
return err
}
return yaml.Unmarshal(data, &RelationRules)
}

View File

@@ -180,7 +180,11 @@ type AlarmConfig struct {
SignName string `yaml:"signName"`
TemplateCode string `yaml:"templateCode"`
} `yaml:"smsForward"`
SMProxy string `yaml:"smProxy"`
SMProxy string `yaml:"smProxy"`
RelatedRules struct {
Enabled bool `yaml:"enabled"`
File string `yaml:"file"`
} `yaml:"relatedRules"`
}
type MMLParam struct {
@@ -271,6 +275,11 @@ func ReadConfig(configFile string) {
yamlConfig = YamlConfigInfo.ConfigLines
ReadOriginalConfig(configFile)
// load alarm relation rules
if yamlConfig.Alarm.RelatedRules.Enabled {
LoadAlarmRelationRules(yamlConfig.Alarm.RelatedRules.File)
}
}
func ReadOriginalConfig(configFile string) {

View File

@@ -0,0 +1,10 @@
# alarm relation rules
derived:
- parentCode: 1001
childCode: 1002
- parentCode: 2001
childCode: 2002
related:
- codes: [3001, 3002, 3003]
neType: "AMF"
timeWindow: 60 # 秒

View File

@@ -31,8 +31,8 @@ rest:
keyFile: ./etc/certs/omc-server.key
webServer:
enabled: false
rootDir: d:/omc.git/fe.ems.vue3/dist # front-end build dist directory
enabled: true
rootDir: /home/simon/omc.git/fe.ems.vue3/dist # front-end build dist directory
listen:
- addr: :8080
schema: http
@@ -47,8 +47,8 @@ database:
type: mysql
user: root
password: "1000omc@kp!"
host: "192.168.9.58"
port: 13306
host: "192.168.2.165"
port: 33066
name: omc_db
connParam: charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&interpolateParams=True
backup: ./database
@@ -58,8 +58,8 @@ redis:
dataSource:
# OMC system db
default:
port: 16379 # Redis port
host: "192.168.9.58" # Redis host
port: 6379 # Redis port
host: "127.0.0.1" # Redis host
password: "helloearth"
db: 10 # Redis db_num
# used to specify the default data source for multiple data resourece
@@ -139,7 +139,7 @@ alarm:
enable: true
emailList:
smtp: mail.smtp.com
port: 25
port: 110
user: smtpext@smtp.com
password: "1000smtp@omc!"
tlsSkipVerify: true
@@ -159,6 +159,11 @@ alarm:
signName: xxx SMSC
templateCode: 1000
smProxy: smsc
# alarm_relation_rules.yaml
relationRules:
enabled: true
file: ./etc/alarm_relation_rules.yaml
# User authorized information
# crypt: mysql/md5/bcrypt

View File

@@ -20,6 +20,7 @@ import (
"be.ems/features/fm"
"be.ems/features/lm"
"be.ems/features/mml"
"be.ems/features/nbi/redisqueue"
"be.ems/features/pm"
featuresCfg "be.ems/lib/config"
"be.ems/lib/dborm"
@@ -209,6 +210,9 @@ func loadFeatures(app *gin.Engine) {
mml.InitMML()
// 初始化Redis Queue连接池
redisqueue.InitRedisQueue()
// 将 mux.Router 注册到 gin.Engine
// 默认路由组

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
}

101
tools/compare/cmpjson.go Normal file
View File

@@ -0,0 +1,101 @@
package main
import (
"encoding/json"
"fmt"
"reflect"
)
// 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 main() {
json1 := `{
"name": "example",
"age": 25,
"address": {
"city": "New York",
"zip": "10001"
}
}`
json2 := `{
"name": "example",
"age": 26,
"address": {
"city": "Los Angeles"
},
"phone": "123-456-7890"
}`
added, modified, deleted, err := CompareJSON(json1, json2)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Added: %v\n", added)
fmt.Printf("Modified: %v\n", modified)
fmt.Printf("Deleted: %v\n", deleted)
}