From 9f6a7f3bb7170086270b7804fc86fd1a9d8b3998 Mon Sep 17 00:00:00 2001 From: TsMask <340112800@qq.com> Date: Tue, 15 Jul 2025 15:14:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20OMC=E9=85=8D=E7=BD=AE=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=80=9A=E7=9F=A5=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/config/expand.go | 30 ++++ .../network_element/controller/ne_config.go | 21 --- .../network_element/service/ne_config_omc.go | 146 +++++++++++++++--- 3 files changed, 153 insertions(+), 44 deletions(-) create mode 100644 src/framework/config/expand.go diff --git a/src/framework/config/expand.go b/src/framework/config/expand.go new file mode 100644 index 00000000..2941e7bf --- /dev/null +++ b/src/framework/config/expand.go @@ -0,0 +1,30 @@ +package config + +import ( + "fmt" + "os" + "regexp" +) + +// SedReplace 替换文件内容,文件来自外部文件配置config传入 +// +// sed 's/port: [0-9]\+ # trace port/port: 6964 # trace port/' /usr/local/etc/omc/omc.yaml +func SedReplace(pattern, replacement string) error { + // 外部文件配置 + externalConfig := conf.GetString("config") + if externalConfig == "" { + return fmt.Errorf("config file path not found") + } + // 读取文件内容 + data, err := os.ReadFile(externalConfig) + if err != nil { + return err + } + + // 定义正则表达式 + re := regexp.MustCompile(pattern) + // 使用正则替换,将匹配到的部分替换为新的内容 + replacedData := re.ReplaceAll(data, []byte(replacement)) + // 写回文件 + return os.WriteFile(externalConfig, replacedData, 0644) +} diff --git a/src/modules/network_element/controller/ne_config.go b/src/modules/network_element/controller/ne_config.go index 3fd08c2e..d33df1be 100644 --- a/src/modules/network_element/controller/ne_config.go +++ b/src/modules/network_element/controller/ne_config.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" - cm_omc "be.ems/features/cm/omc" "be.ems/src/framework/i18n" "be.ems/src/framework/reqctx" "be.ems/src/framework/resp" @@ -215,16 +214,6 @@ func (s NeConfigController) DataInfo(c *gin.Context) { } if query.NeType == "OMC" { - if query.ParamName == "alarmEmailForward" || query.ParamName == "alarmSMSForward" { - var o *cm_omc.ConfigOMC - resData, err := o.Query(query.ParamName) - if err != nil { - c.JSON(200, resp.ErrMsg(err.Error())) - return - } - c.JSON(200, resp.OkData(resData)) - return - } resData := s.neConfigService.GetOMCYaml(query.ParamName) c.JSON(200, resp.OkData(resData)) return @@ -274,16 +263,6 @@ func (s NeConfigController) DataEdit(c *gin.Context) { return } if body.NeType == "OMC" { - if body.ParamName == "alarmEmailForward" || body.ParamName == "alarmSMSForward" { - var o *cm_omc.ConfigOMC - resData, err := o.Modify(body.ParamName, body.ParamData) - if err != nil { - c.JSON(200, resp.ErrMsg(err.Error())) - return - } - c.JSON(200, resp.OkData(resData)) - return - } err := s.neConfigService.ModifyOMCYaml(body.ParamName, body.Loc, body.ParamData) if err != nil { c.JSON(200, resp.ErrMsg(err.Error())) diff --git a/src/modules/network_element/service/ne_config_omc.go b/src/modules/network_element/service/ne_config_omc.go index 4d288d0c..7243c3e2 100644 --- a/src/modules/network_element/service/ne_config_omc.go +++ b/src/modules/network_element/service/ne_config_omc.go @@ -2,15 +2,32 @@ package service import ( "fmt" - "runtime" "strings" - "be.ems/src/framework/cmd" "be.ems/src/framework/config" + "github.com/tsmask/go-oam/src/framework/utils/parse" ) -// GetOMCYaml 获取OMC网元配置文件 /usr/local/etc/omc/omc.yaml +// GetOMCYaml 获取OMC网元配置文件 func (r NeConfig) GetOMCYaml(paramName string) []map[string]any { + if paramName == "notificationEmail" { + notificationEmailData := config.Get("notification.email").(map[string]any) + for k, v := range notificationEmailData { + if k == "password" && v != nil { + notificationEmailData[k] = parse.SafeContent(fmt.Sprint(v)) + } + } + return []map[string]any{notificationEmailData} + } + if paramName == "notificationSMSC" { + notificationSMSCData := config.Get("notification.smsc").(map[string]any) + for k, v := range notificationSMSCData { + if k == "password" && v != nil { + notificationSMSCData[k] = parse.SafeContent(fmt.Sprint(v)) + } + } + return []map[string]any{notificationSMSCData} + } if paramName == "trace" { traceData := config.Get("trace").(map[string]any) return []map[string]any{traceData} @@ -18,30 +35,113 @@ func (r NeConfig) GetOMCYaml(paramName string) []map[string]any { return []map[string]any{} } -// ModifyOMCYaml 修改OMC网元配置文件 /usr/local/etc/omc/omc.yaml +// ModifyOMCYaml 修改OMC网元配置文件 func (r NeConfig) ModifyOMCYaml(paramName, loc string, paramData any) error { neConfig := r.FindByNeTypeAndParamName("OMC", paramName) if neConfig.ParamType == "list" { + if paramName == "notificationEmail" { + notificationEmailData := config.Get("notification.email").(map[string]any) + for k, v := range paramData.(map[string]any) { + keyLower := strings.ToLower(k) + // 改程序内 + notificationEmailData[keyLower] = v + // 改文件 + switch keyLower { + case "enabled": + pattern := `enabled: (true|false) # email enable` + replacement := fmt.Sprintf(`enabled: %v # email enable`, v) + return config.SedReplace(pattern, replacement) + case "list": + pattern := `list: ".*" # toEmail` + replacement := fmt.Sprintf(`list: "%v" # toEmail`, v) + return config.SedReplace(pattern, replacement) + case "title": + pattern := `title: ".*" # email title` + replacement := fmt.Sprintf(`title: "%v" # email title`, v) + return config.SedReplace(pattern, replacement) + case "smtp": + pattern := `smtp: ".*" # email smtp` + replacement := fmt.Sprintf(`smtp: "%v" # email smtp`, v) + return config.SedReplace(pattern, replacement) + case "port": + pattern := `port: [0-9]+ # email port` + replacement := fmt.Sprintf(`port: %v # email port`, v) + return config.SedReplace(pattern, replacement) + case "user": + pattern := `user: ".*" # email user` + replacement := fmt.Sprintf(`user: "%v" # email user`, v) + return config.SedReplace(pattern, replacement) + case "password": + pattern := `password: ".*" # email password` + replacement := fmt.Sprintf(`password: "%v" # email password`, v) + return config.SedReplace(pattern, replacement) + } + } + } + if paramName == "notificationSMSC" { + notificationSMSCData := config.Get("notification.smsc").(map[string]any) + for k, v := range paramData.(map[string]any) { + keyLower := strings.ToLower(k) + // 改程序内 + notificationSMSCData[keyLower] = v + // 改文件 + switch keyLower { + case "enabled": + pattern := `enabled: (true|false) # smsc enable` + replacement := fmt.Sprintf(`enabled: %v # smsc enable`, v) + return config.SedReplace(pattern, replacement) + case "list": + pattern := `list: ".*" # toMobile` + replacement := fmt.Sprintf(`list: "%v" # toMobile`, v) + return config.SedReplace(pattern, replacement) + case "addr": + pattern := `addr: ".*" # smsc addr` + replacement := fmt.Sprintf(`addr: "%v" # smsc addr`, v) + return config.SedReplace(pattern, replacement) + case "systemid": + pattern := `systemid: ".*" # smsc system id` + replacement := fmt.Sprintf(`systemid: "%v" # smsc system id`, v) + return config.SedReplace(pattern, replacement) + case "systemtype": + pattern := `systemtype: ".*" # smsc system type` + replacement := fmt.Sprintf(`systemtype: "%v" # smsc system type`, v) + return config.SedReplace(pattern, replacement) + case "password": + pattern := `password: ".*" # smsc password` + replacement := fmt.Sprintf(`password: "%v" # smsc password`, v) + return config.SedReplace(pattern, replacement) + case "coding": + pattern := `coding: [0-9]+ # smsc codingMap` + replacement := fmt.Sprintf(`coding: %v # smsc codingMap`, v) + return config.SedReplace(pattern, replacement) + case "servicenumber": + pattern := `servicenumber: ".*" # smsc service number` + replacement := fmt.Sprintf(`servicenumber: "%v" # smsc service number`, v) + return config.SedReplace(pattern, replacement) + + } + } + } if paramName == "trace" { - configPath := fmt.Sprint(config.Get("config")) // 获取配置文件路径 - paramDataMap := paramData.(map[string]any) - for k, v := range paramDataMap { - config.Set(fmt.Sprintf("trace.%s", strings.ToLower(k)), v) - if runtime.GOOS == "windows" { - continue // Windows系统不支持sed命令 - } - // 修改参数较少,直接命令改文件内容 - if k == "enabled" { - // sed 's/enabled: \(true\|false\) # trace enabled/enabled: true # trace enabled/' /usr/local/etc/omc/omc.yaml - cmd.Execf("sed -i 's/enabled: \\(true\\|false\\) # trace enabled/enabled: %v # trace enabled/' %s", v, configPath) - } - if k == "host" { - // sed 's/host: ".*" # trace host/host: "127.2.2.2" # trace host/' /usr/local/etc/omc/omc.yaml - cmd.Execf("sed -i 's/host: \".*\" # trace host/host: \"%v\" # trace host/' %s", v, configPath) - } - if k == "port" { - // sed 's/port: [0-9]\+ # trace port/port: 6964 # trace port/' /usr/local/etc/omc/omc.yaml - cmd.Execf("sed -i 's/port: [0-9]\\+ # trace port/port: %v # trace port/' %s", v, configPath) + traceData := config.Get("trace").(map[string]any) + for k, v := range paramData.(map[string]any) { + keyLower := strings.ToLower(k) + // 改程序内 + traceData[keyLower] = v + // 改文件 + switch keyLower { + case "enabled": + pattern := `enabled: (true|false) # trace enabled` + replacement := fmt.Sprintf(`enabled: %v # trace enabled`, v) + return config.SedReplace(pattern, replacement) + case "host": + pattern := `host: ".*" # trace host` + replacement := fmt.Sprintf(`host: "%v" # trace host`, v) + return config.SedReplace(pattern, replacement) + case "port": + pattern := `port: [0-9]+ # trace port` + replacement := fmt.Sprintf(`port: %v # trace port`, v) + return config.SedReplace(pattern, replacement) } } // 重开跟踪任务信令数据通道UDP