feat: 网元参数配置数据支持版本区分功能

feat(database): add ne_version and schema columns to ne_info table

feat(database): update ne_info table structure in upgrade script

fix(ne_config): handle id as number in Info and Add methods

fix(ne_config): validate neId and fetch neInfo in ListByNeType and DataAdd methods

fix(ne_info): extract version from ServerState in Add and Edit methods

refactor(ne_config): update NeConfig model to include NeVersion

refactor(ne_info): add NeVersion and Schema fields to NeInfo model

test(ne_config): enhance test cases to include version parsing and saving

refactor(ne_config): improve cache handling and querying by NeType and NeVersion

fix(ne_info): update NeInfo version during status change
This commit is contained in:
TsMask
2025-10-17 10:17:07 +08:00
parent 3b053d7f47
commit 8cd86a62d6
18 changed files with 1306 additions and 254 deletions

View File

@@ -45,13 +45,13 @@ func (s NeConfigController) List(c *gin.Context) {
// GET /info/:id
func (s NeConfigController) Info(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
id := c.Query("id")
if id == "" {
id := parse.Number(c.Query("id"))
if id == 0 {
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: id is empty"))
return
}
data := s.neConfigService.SelectById(id)
data := s.neConfigService.SelectById(fmt.Sprintf("%d", id))
if data.ID != id {
// 没有可访问参数配置数据!
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neConfig.noData")))
@@ -86,7 +86,7 @@ func (s NeConfigController) Add(c *gin.Context) {
body.ParamJson = string(paramDataByte)
insertId := s.neConfigService.Insert(body)
if insertId != "" {
if insertId != 0 {
c.JSON(200, resp.Ok(nil))
return
}
@@ -106,7 +106,7 @@ func (s NeConfigController) Edit(c *gin.Context) {
}
// 检查是否存在
data := s.neConfigService.SelectById(body.ID)
data := s.neConfigService.SelectById(fmt.Sprintf("%d", body.ID))
if data.ID != body.ID {
// 没有可访问主机命令数据!
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "neConfig.noData")))
@@ -169,12 +169,19 @@ func (s NeConfigController) Remove(c *gin.Context) {
// @Description Network Element Parameter Configuration Available Attribute Values List Specify Network Element Type All Unpaged
// @Router /ne/config/list/{neType} [get]
func (s NeConfigController) ListByNeType(c *gin.Context) {
language := reqctx.AcceptLanguage(c)
neId := c.DefaultQuery("neId", "001")
neType := c.Param("neType")
if neType == "" {
c.JSON(422, resp.CodeMsg(resp.CODE_PARAM_CHEACK, "bind err: neType is empty"))
return
}
data := s.neConfigService.SelectNeConfigByNeType(neType)
neInfo := s.neInfoService.FindByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
data := s.neConfigService.FindByNeType(neInfo.NeType, neInfo.NeVersion)
c.JSON(200, resp.OkData(data))
}
@@ -332,8 +339,14 @@ func (s NeConfigController) DataAdd(c *gin.Context) {
return
}
neInfo := s.neInfoService.FindByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 检查是否array
info := s.neConfigService.SelectNeConfigByNeTypeAndParamName(body.NeType, body.ParamName)
info := s.neConfigService.FindByNeTypeAndParamName(body.NeType, neInfo.NeVersion, body.ParamName)
if info.ParamType != "array" {
c.JSON(200, resp.ErrMsg("this attribute does not support adding"))
return
@@ -345,12 +358,6 @@ func (s NeConfigController) DataAdd(c *gin.Context) {
return
}
neInfo := s.neInfoService.FindByNeTypeAndNeID(body.NeType, body.NeId)
if neInfo.NeId != body.NeId || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigInstall(neInfo, body.ParamName, body.Loc, body.ParamData)
if err != nil {
@@ -390,19 +397,19 @@ func (s NeConfigController) DataRemove(c *gin.Context) {
return
}
// 检查是否array
info := s.neConfigService.SelectNeConfigByNeTypeAndParamName(query.NeType, query.ParamName)
if info.ParamType != "array" {
c.JSON(200, resp.ErrMsg("this attribute does not support adding"))
return
}
neInfo := s.neInfoService.FindByNeTypeAndNeID(query.NeType, query.NeId)
if neInfo.NeId != query.NeId || neInfo.IP == "" {
c.JSON(200, resp.ErrMsg(i18n.TKey(language, "app.common.noNEInfo")))
return
}
// 检查是否array
info := s.neConfigService.FindByNeTypeAndParamName(query.NeType, neInfo.NeVersion, query.ParamName)
if info.ParamType != "array" {
c.JSON(200, resp.ErrMsg("this attribute does not support adding"))
return
}
// 网元直连
resData, err := neFetchlink.NeConfigDelete(neInfo, query.ParamName, query.Loc)
if err != nil {

View File

@@ -386,6 +386,7 @@ func (s *NeInfoController) Add(c *gin.Context) {
// 已有网元可获取的信息
if body.ServerState != nil {
if v, ok := body.ServerState["version"]; ok && v != nil {
body.NeVersion = fmt.Sprint(v)
neVersion.Version = v.(string)
}
if v, ok := body.ServerState["sn"]; ok && v != nil {
@@ -483,14 +484,15 @@ func (s *NeInfoController) Edit(c *gin.Context) {
// 已有网元可获取的信息
if body.ServerState != nil {
if v, ok := body.ServerState["version"]; ok && v != nil {
neVersion.Version = v.(string)
body.NeVersion = fmt.Sprint(v)
neVersion.Version = fmt.Sprint(v)
neVersion.UpdateBy = loginUserName
}
if v, ok := body.ServerState["sn"]; ok && v != nil {
neLicense.SerialNum = v.(string)
neLicense.SerialNum = fmt.Sprint(v)
}
if v, ok := body.ServerState["expire"]; ok && v != nil {
neLicense.ExpiryDate = v.(string)
neLicense.ExpiryDate = fmt.Sprint(v)
neLicense.Status = "1"
neLicense.UpdateBy = loginUserName
}

View File

@@ -2,8 +2,10 @@ package model
// NeConfig 网元_参数配置可用属性值
type NeConfig struct {
ID string `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间
NeType string `json:"neType" binding:"required" gorm:"column:ne_type"` // 网元类型
NeVersion string `json:"neVersion" gorm:"column:ne_version"` // 网元版本 前缀匹配
ParamName string `json:"paramName" binding:"required" gorm:"column:param_name"` // 参数名
ParamDisplay string `json:"paramDisplay" binding:"required" gorm:"column:param_display"` // 参数显示名
ParamType string `json:"paramType" gorm:"column:param_type"` // 参数类型 list列表单层 array数组多层
@@ -11,7 +13,6 @@ type NeConfig struct {
ParamSort int64 `json:"paramSort" gorm:"column:param_sort"` // 参数排序
ParamPerms string `json:"paramPerms" gorm:"column:param_perms"` // 操作权限 get只读 put可编辑 delete可删除 post可新增
Visible string `json:"visible" gorm:"column:visible"` // 可见性 默认public 单独网元self
UpdateTime int64 `json:"updateTime" gorm:"column:update_time"` // 更新时间
// ====== 非数据库字段属性 ======

View File

@@ -7,6 +7,8 @@ type NeInfo struct {
NeId string `json:"neId" gorm:"column:ne_id" binding:"required"`
RmUID string `json:"rmUid" gorm:"column:rm_uid"`
NeName string `json:"neName" gorm:"column:ne_name"`
NeVersion string `json:"neVersion" gorm:"column:ne_version"` // 网元版本
Schema string `json:"schema" gorm:"column:schema"` // 网元模式 http/https
IP string `json:"ip" gorm:"column:ip" binding:"required"`
Port int64 `json:"port" gorm:"column:port" binding:"required,number,max=65535,min=1"`
PvFlag string `json:"pvFlag" gorm:"column:pv_flag" binding:"oneof=PNF VNF"` // ''PNF'',''VNF''

View File

@@ -41,23 +41,39 @@ func TestConfig(t *testing.T) {
if configParamFile == "*" {
for _, v := range fileNameList {
params := parseData(filepath.Join(configParamDir, v))
filePath := filepath.Join(configParamDir, v)
version := parseVersion(filePath)
params := parseData(filePath)
if params == nil {
return
}
saveData(params)
saveData(version, params)
}
} else {
params := parseData(filepath.Join(configParamDir, configParamFile))
filePath := filepath.Join(configParamDir, configParamFile)
version := parseVersion(filePath)
params := parseData(filePath)
if params == nil {
return
}
saveData(params)
saveData(version, params)
}
}
// ========= Main =============
// 根据文件名获取版本号 mme_2_param_config.yaml -> 2
func parseVersion(filePaht string) string {
version := "2"
splits := strings.Split(filepath.Base(filePaht), "_")
if len(splits) > 0 {
if splits[2] == "param" {
version = splits[1]
}
}
return version
}
// parseData 文件转map数据
func parseData(filePaht string) []map[string]string {
data, err := parseStrToMap(filePaht)
@@ -74,7 +90,7 @@ func parseData(filePaht string) []map[string]string {
}
// saveData 保存数据
func saveData(params []map[string]string) {
func saveData(version string, params []map[string]string) {
// 定义排序函数
sort.Slice(params, func(i, j int) bool {
paramSortI := params[i]["paramSort"]
@@ -109,6 +125,7 @@ func saveData(params []map[string]string) {
}
neConfig := model.NeConfig{
NeVersion: version,
NeType: v["neType"],
ParamName: v["paramName"],
ParamDisplay: v["paramDisplay"],
@@ -153,14 +170,14 @@ func connDB() *gorm.DB {
}
// saveDB 表插入或更新
func saveDB(s model.NeConfig) string {
func saveDB(s model.NeConfig) int64 {
db := connDB()
// 检查是否存在
var id string
db.Raw("SELECT id FROM ne_config WHERE ne_type = ? AND param_name = ?", s.NeType, s.ParamName).Scan(&id)
var id int64
db.Raw("SELECT id FROM ne_config WHERE ne_type = ? AND ne_version = ? AND param_name = ?", s.NeType, s.NeVersion, s.ParamName).Scan(&id)
// 更新时间
s.UpdateTime = time.Now().UnixMilli()
if id != "" {
if id > 0 {
s.ID = id
db.Save(&s)
} else {

View File

@@ -3,6 +3,7 @@ package repository
import (
"time"
"be.ems/src/framework/database/db"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/modules/network_element/model"
@@ -45,9 +46,9 @@ func (r NeConfig) SelectByPage(query map[string]string) ([]model.NeConfig, int64
return rows, total
}
// SelectList 根据实体查询
func (r *NeConfig) SelectList(param model.NeConfig) []model.NeConfig {
tx := datasource.DB("").Model(&model.NeConfig{})
// Select 查询集合
func (r NeConfig) Select(param model.NeConfig) []model.NeConfig {
tx := db.DB("").Model(&model.NeConfig{})
// 查询条件拼接
if param.NeType != "" {
tx = tx.Where("ne_type = ?", param.NeType)
@@ -83,19 +84,19 @@ func (r *NeConfig) SelectByIds(ids []string) []model.NeConfig {
}
// Insert 新增信息
func (r *NeConfig) Insert(param model.NeConfig) string {
func (r *NeConfig) Insert(param model.NeConfig) int64 {
param.UpdateTime = time.Now().UnixMilli()
// 执行插入
if err := datasource.DB("").Create(&param).Error; err != nil {
logger.Errorf("insert err => %v", err.Error())
return ""
return 0
}
return param.ID
}
// Update 修改信息
func (r *NeConfig) Update(param model.NeConfig) int64 {
if param.ID == "" {
if param.ID == 0 {
return 0
}
param.UpdateTime = time.Now().UnixMilli()

View File

@@ -3,6 +3,7 @@ package repository
import (
"time"
"be.ems/src/framework/database/db"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/modules/network_element/model"
@@ -222,6 +223,26 @@ func (r *NeInfo) UpdateState(id, status string) int64 {
return tx.RowsAffected
}
// UpdateVersion 修改网元版本
func (r NeInfo) UpdateVersion(id string, neVersion string) int64 {
if id == "" {
return 0
}
tx := db.DB("").Model(&model.NeInfo{})
// 构建查询条件
tx = tx.Where("id = ?", id)
tx.Updates(map[string]any{
"ne_version": neVersion,
"update_time": time.Now().UnixMilli(),
})
// 执行更新
if err := tx.Error; err != nil {
logger.Errorf("update err => %v", err.Error())
return 0
}
return tx.RowsAffected
}
// DeleteByIds 批量删除网元信息
func (r NeInfo) DeleteByIds(ids []string) int64 {
if len(ids) <= 0 {

View File

@@ -5,7 +5,7 @@ import (
"fmt"
"strings"
"be.ems/src/framework/constants/cachekey"
"be.ems/src/framework/constants"
"be.ems/src/framework/database/redis"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
@@ -21,92 +21,85 @@ type NeConfig struct {
neConfigRepository *repository.NeConfig // 网元参数配置可用属性值表
}
// RefreshByNeType 通过ne_type刷新redis中的缓存
func (r *NeConfig) RefreshByNeTypeAndNeID(neType string) []model.NeConfig {
// 多个
if neType == "" || neType == "*" {
neConfigList := r.neConfigRepository.SelectList(model.NeConfig{})
if len(neConfigList) > 0 {
neConfigGroup := map[string][]model.NeConfig{}
for _, v := range neConfigList {
if item, ok := neConfigGroup[v.NeType]; ok {
neConfigGroup[v.NeType] = append(item, v)
} else {
neConfigGroup[v.NeType] = []model.NeConfig{v}
}
}
for k, v := range neConfigGroup {
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(k))
redis.Del("", key)
if len(v) > 0 {
for i, item := range v {
if err := json.Unmarshal([]byte(item.ParamJson), &item.ParamData); err != nil {
continue
}
v[i] = item
}
values, _ := json.Marshal(v)
redis.Set("", key, string(values))
}
}
}
return neConfigList
// RefreshByNeType 通过ne_type刷新redis中的缓存 并返回分组数据
func (r *NeConfig) RefreshByNeTypeAndNeID(neType string) map[string][]model.NeConfig {
neConfig := model.NeConfig{}
if neType != "*" {
neConfig.NeType = neType
}
// 单个
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
redis.Del("", key)
neConfigList := r.neConfigRepository.SelectList(model.NeConfig{
NeType: neType,
})
if len(neConfigList) > 0 {
for i, v := range neConfigList {
if err := json.Unmarshal([]byte(v.ParamJson), &v.ParamData); err != nil {
continue
}
neConfigList[i] = v
neConfigList := r.neConfigRepository.Select(neConfig)
neConfigGroup := map[string][]model.NeConfig{}
for _, v := range neConfigList {
if err := json.Unmarshal([]byte(v.ParamJson), &v.ParamData); err != nil {
continue
}
neTypeVerKey := fmt.Sprintf("%s:%s", strings.ToUpper(v.NeType), v.NeVersion)
if item, ok := neConfigGroup[neTypeVerKey]; ok {
neConfigGroup[neTypeVerKey] = append(item, v)
} else {
neConfigGroup[neTypeVerKey] = []model.NeConfig{v}
}
values, _ := json.Marshal(neConfigList)
redis.Set("", key, string(values))
}
return neConfigList
for k, v := range neConfigGroup {
key := fmt.Sprintf("%s:NeConfig:%s", constants.CACHE_NE_DATA, k)
redis.Del("", key)
if len(v) > 0 {
values, _ := json.Marshal(v)
redis.Set("", key, string(values))
}
}
return neConfigGroup
}
// ClearNeCacheByNeType 清除网元类型参数配置缓存
func (r *NeConfig) ClearNeCacheByNeType(neType string) bool {
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, neType)
key := fmt.Sprintf("%s:NeConfig:%s:*", constants.CACHE_NE_DATA, neType)
if neType == "*" {
key = fmt.Sprintf("%sNeConfig:*", cachekey.NE_DATA_KEY)
key = fmt.Sprintf("%s:NeConfig:*", constants.CACHE_NE_DATA)
}
keys, err := redis.GetKeys("", key)
if err != nil {
return false
}
err = redis.DelKeys("", keys)
return err == nil
return redis.DelKeys("", keys) == nil
}
// SelectNeConfigByNeType 查询网元类型参数配置
func (r *NeConfig) SelectNeConfigByNeType(neType string) []model.NeConfig {
var neConfigList []model.NeConfig
key := fmt.Sprintf("%sNeConfig:%s", cachekey.NE_DATA_KEY, strings.ToUpper(neType))
jsonStr, _ := redis.Get("", key)
if len(jsonStr) > 7 {
err := json.Unmarshal([]byte(jsonStr), &neConfigList)
if err != nil {
neConfigList = []model.NeConfig{}
// FindByNeType 查询网元类型参数配置
func (r *NeConfig) FindByNeType(neType, neVersion string) []model.NeConfig {
data := make([]model.NeConfig, 0)
keys, _ := redis.GetKeys("", fmt.Sprintf("%s:NeConfig:%s:*", constants.CACHE_NE_DATA, strings.ToUpper(neType)))
if len(keys) > 0 {
for _, key := range keys {
neTypeVer := strings.Split(key, ":")
if len(neTypeVer) != 4 {
continue
}
if strings.HasPrefix(neVersion, neTypeVer[3]) {
if jsonStr, _ := redis.Get("", key); len(jsonStr) > 7 {
json.Unmarshal([]byte(jsonStr), &data)
}
return data
}
}
} else {
neConfigList = r.RefreshByNeTypeAndNeID(neType)
}
return neConfigList
// 从数据库刷新缓存
neConfigGroup := r.RefreshByNeTypeAndNeID(neType)
for k, v := range neConfigGroup {
neTypeVer := strings.Split(k, ":")
if len(neTypeVer) == 2 && strings.HasPrefix(neVersion, neTypeVer[1]) {
data = v
break
}
}
return data
}
// SelectNeConfigByNeTypeAndParamName 查询网元类型参数配置By参数名
func (r *NeConfig) SelectNeConfigByNeTypeAndParamName(neType, paramName string) model.NeConfig {
neConfigList := r.SelectNeConfigByNeType(neType)
// FindByNeTypeAndParamName 查询网元类型参数配置By参数名
func (r *NeConfig) FindByNeTypeAndParamName(neType, neVersion, paramName string) model.NeConfig {
neConfigList := r.FindByNeType(neType, neVersion)
var neConfig model.NeConfig
for _, v := range neConfigList {
if v.ParamName == paramName {
if strings.HasPrefix(neVersion, v.NeVersion) && v.ParamName == paramName {
neConfig = v
break
}
@@ -121,7 +114,7 @@ func (r *NeConfig) SelectPage(query map[string]string) ([]model.NeConfig, int64)
// SelectConfigList 查询列表
func (r *NeConfig) SelectList(param model.NeConfig) []model.NeConfig {
return r.neConfigRepository.SelectList(param)
return r.neConfigRepository.Select(param)
}
// SelectByIds 通过ID查询
@@ -137,7 +130,7 @@ func (r *NeConfig) SelectById(id string) model.NeConfig {
}
// Insert 新增信息
func (r *NeConfig) Insert(param model.NeConfig) string {
func (r *NeConfig) Insert(param model.NeConfig) int64 {
return r.neConfigRepository.Insert(param)
}

View File

@@ -20,7 +20,7 @@ func (r NeConfig) GetOMCYaml(paramName string) []map[string]any {
// ModifyOMCYaml 修改OMC网元配置文件 /usr/local/etc/omc/omc.yaml
func (r NeConfig) ModifyOMCYaml(paramName, loc string, paramData any) error {
neConfig := r.SelectNeConfigByNeTypeAndParamName("OMC", paramName)
neConfig := r.FindByNeTypeAndParamName("OMC", "2", paramName)
if neConfig.ParamType == "list" {
if paramName == "trace" {
configPath := fmt.Sprint(config.Get("config")) // 获取配置文件路径

View File

@@ -303,6 +303,13 @@ func (r *NeInfo) bandNeStatus(arr *[]model.NeInfo) {
(*arr)[i].Status = status
if v.Status != status {
r.neInfoRepository.UpdateState(v.ID, status)
r.RefreshByNeTypeAndNeID(v.NeType, v.NeId)
}
// 网元版本设置为当前版本
version, ok := result["version"].(string)
if ok && version != v.NeVersion {
r.neInfoRepository.UpdateVersion(v.ID, version)
r.RefreshByNeTypeAndNeID(v.NeType, v.NeId)
}
}
}