diff --git a/src/modules/network_element/controller/ne_info.go b/src/modules/network_element/controller/ne_info.go index 116c3f14..9c89d192 100644 --- a/src/modules/network_element/controller/ne_info.go +++ b/src/modules/network_element/controller/ne_info.go @@ -204,31 +204,86 @@ func (s *NeInfoController) ConfigFileWrite(c *gin.Context) { c.JSON(200, result.Ok(nil)) } -// 网元端公共配置文件读取 +// 网元端OAM配置文件读取 // -// GET /para5GFile -func (s *NeInfoController) Para5GFileRead(c *gin.Context) { - fileType := c.Query("fileType") - data := s.neInfoService.NeConfPara5GRead(fileType) +// GET /oamFile +func (s *NeInfoController) OAMFileRead(c *gin.Context) { + language := ctx.AcceptLanguage(c) + var querys struct { + NeType string `form:"neType" binding:"required"` + NeID string `form:"neId" binding:"required"` + } + if err := c.ShouldBindQuery(&querys); err != nil { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + data, err := s.neInfoService.NeConfOAMRead(querys.NeType, querys.NeID) + if err != nil { + c.JSON(200, result.ErrMsg(err.Error())) + return + } c.JSON(200, result.OkData(data)) } -// 网元端公共配置文件写入 +// 网元端OAM配置文件写入 // -// PUT /para5GFile -func (s *NeInfoController) Para5GFileWrite(c *gin.Context) { +// PUT /oamFile +func (s *NeInfoController) OAMFileWrite(c *gin.Context) { language := ctx.AcceptLanguage(c) var body struct { - FileType string `json:"fileType" binding:"oneof='' txt json yaml yml"` // 解析内容数据到对应文件类型 - Content any `json:"content" binding:"required"` // 内容 - SyncNE []string `json:"syncNe"` // 同步到网元 + NeType string `json:"neType" binding:"required"` + NeID string `json:"neId" binding:"required"` + Content map[string]any `json:"content" binding:"required"` // 内容 + Sync bool `json:"sync"` // 同步到网元 } if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil { c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) return } - err := s.neInfoService.NeConfPara5GWirte(body.FileType, body.Content, body.SyncNE) + // 查询网元获取IP + neInfo := s.neInfoService.SelectNeInfoByNeTypeAndNeID(body.NeType, body.NeID) + if neInfo.NeId != body.NeID || neInfo.IP == "" { + c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.noNEInfo"))) + return + } + + err := s.neInfoService.NeConfOAMWirte(body.NeType, body.NeID, body.Content, body.Sync) + if err != nil { + c.JSON(200, result.ErrMsg(err.Error())) + return + } + c.JSON(200, result.Ok(nil)) +} + +// 网元端Para5G配置文件读取 +// +// GET /para5GFile +func (s *NeInfoController) Para5GFileRead(c *gin.Context) { + data, err := s.neInfoService.NeConfPara5GRead() + if err != nil { + c.JSON(200, result.ErrMsg(err.Error())) + return + } + c.JSON(200, result.OkData(data)) +} + +// 网元端Para5G配置文件写入 +// +// PUT /para5GFile +func (s *NeInfoController) Para5GFileWrite(c *gin.Context) { + language := ctx.AcceptLanguage(c) + var body struct { + Content map[string]any `json:"content" binding:"required"` // 内容 + SyncNE []string `json:"syncNe"` // 同步到网元 + } + if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil { + c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400"))) + return + } + + err := s.neInfoService.NeConfPara5GWirte(body.Content, body.SyncNE) if err != nil { c.JSON(200, result.ErrMsg(err.Error())) return diff --git a/src/modules/network_element/network_element.go b/src/modules/network_element/network_element.go index 11d8b9e9..5143a4de 100644 --- a/src/modules/network_element/network_element.go +++ b/src/modules/network_element/network_element.go @@ -67,6 +67,15 @@ func Setup(router *gin.Engine) { collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)), controller.NewNeInfo.ConfigFileWrite, ) + neInfoGroup.GET("/oamFile", + middleware.PreAuthorize(nil), + controller.NewNeInfo.OAMFileRead, + ) + neInfoGroup.PUT("/oamFile", + middleware.PreAuthorize(nil), + collectlogs.OperateLog(collectlogs.OptionNew("log.operate.title.neInfo", collectlogs.BUSINESS_TYPE_OTHER)), + controller.NewNeInfo.OAMFileWrite, + ) neInfoGroup.GET("/para5GFile", middleware.PreAuthorize(nil), controller.NewNeInfo.Para5GFileRead, diff --git a/src/modules/network_element/service/ne_info.go b/src/modules/network_element/service/ne_info.go index c8033457..206c3989 100644 --- a/src/modules/network_element/service/ne_info.go +++ b/src/modules/network_element/service/ne_info.go @@ -61,11 +61,15 @@ type INeInfo interface { // NeConfigFileWirte 网元配置文件写入 content内容 sync同步到网元端 NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType string, content any, sync bool) error + // NeConfOAMRead 网元OAM配置文件读取 + NeConfOAMRead(neType, neId string) (map[string]any, error) + + // NeConfOAMWirte 网元OAM配置文件写入 content内容 sync同步到网元端 + NeConfOAMWirte(neType, neId string, content any, sync bool) error + // NeConfPara5GRead 网元公共配置文件读取 - // - // 返回 string map[string]any - NeConfPara5GRead(fileType string) any + NeConfPara5GRead() (map[string]any, error) // NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId - NeConfPara5GWirte(fileType string, content any, syncNE []string) error + NeConfPara5GWirte(content map[string]any, syncNE []string) error } diff --git a/src/modules/network_element/service/ne_info.impl.go b/src/modules/network_element/service/ne_info.impl.go index c97e8a24..2680dd53 100644 --- a/src/modules/network_element/service/ne_info.impl.go +++ b/src/modules/network_element/service/ne_info.impl.go @@ -486,10 +486,80 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType s return nil } +// NeConfOAMRead 网元OAM配置文件读取 +func (r *NeInfoImpl) NeConfOAMRead(neType, neId string) (map[string]any, error) { + neTypeLower := strings.ToLower(neType) + // 网管本地路径 + omcPath := "/usr/local/etc/omc/ne_config" + if runtime.GOOS == "windows" { + omcPath = fmt.Sprintf("C:%s", omcPath) + } + localFilePath := fmt.Sprintf("%s/%s/%s/%s", omcPath, neTypeLower, neId, "oam_manager.yaml") + + // 读取文件内容 + bytes, err := os.ReadFile(localFilePath) + if err != nil { + logger.Warnf("NeConfOAMRead ReadFile => %s", err.Error()) + return nil, fmt.Errorf("read file error") + } + content := string(bytes) + + // 序列化Map + mapData, err := parse.ConvertConfigToMap("yaml", content) + if err != nil { + logger.Warnf("NeConfOAMRead ConvertConfigToMap => %s", err.Error()) + return nil, fmt.Errorf("content convert type error") + } + return mapData, nil +} + +// NeConfOAMWirte 网元OAM配置文件写入 content内容 sync同步到网元端 +func (r *NeInfoImpl) NeConfOAMWirte(neType, neId string, content any, sync bool) error { + neTypeLower := strings.ToLower(neType) + fileName := "oam_manager.yaml" + // 网管本地路径 + omcPath := "/usr/local/etc/omc/ne_config" + if runtime.GOOS == "windows" { + omcPath = fmt.Sprintf("C:%s", omcPath) + } + localFilePath := fmt.Sprintf("%s/%s/%s/%s", omcPath, neTypeLower, neId, fileName) + + // 写入文件 + if err := parse.ConvertConfigToFile("yaml", localFilePath, content); err != nil { + return fmt.Errorf("please check if the file exists or write permissions") + } + + // 同步到网元端 + if sync { + // 网元主机的SSH客户端 + sshClient, err := r.NeRunSSHclient(neType, neId) + if err != nil { + return err + } + defer sshClient.Close() + // 网元主机的SSH客户端进行文件传输 + sftpClient, err := sshClient.NewClientSFTP() + if err != nil { + return err + } + defer sftpClient.Close() + + // 网元端配置路径 + neFilePath := fmt.Sprintf("/usr/local/etc/%s/%s", neTypeLower, fileName) + neFileDir := filepath.Dir(neFilePath) + // 修改网元文件权限 + sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod o+w %s && sudo chmod o+w %s", neFileDir, neFileDir, neFilePath)) + // 复制到网元进行覆盖 + if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil { + return fmt.Errorf("please check if scp remote copy is allowed") + } + } + + return nil +} + // NeConfPara5GRead 网元公共配置文件读取 -// -// 返回 string map[string]any -func (r *NeInfoImpl) NeConfPara5GRead(fileType string) any { +func (r *NeInfoImpl) NeConfPara5GRead() (map[string]any, error) { // 网管本地路径 omcFilePath := "/usr/local/etc/omc/para5G.yaml" if runtime.GOOS == "windows" { @@ -499,37 +569,28 @@ func (r *NeInfoImpl) NeConfPara5GRead(fileType string) any { bytes, err := os.ReadFile(omcFilePath) if err != nil { logger.Warnf("NeConfPara5GRead ReadFile => %s", err.Error()) - return "read file error" + return nil, fmt.Errorf("read file error") } content := string(bytes) - if fileType == "" || fileType == "txt" { - return content - } + // 序列化Map - mapData, err := parse.ConvertConfigToMap(fileType, content) + mapData, err := parse.ConvertConfigToMap("yaml", content) if err != nil { logger.Warnf("NeConfPara5GRead ConvertConfigToMap => %s", err.Error()) - return "content convert type error" + return nil, fmt.Errorf("content convert type error") } - return mapData + return mapData, nil } // NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId -func (r *NeInfoImpl) NeConfPara5GWirte(fileType string, content any, syncNE []string) error { +func (r *NeInfoImpl) NeConfPara5GWirte(content map[string]any, syncNE []string) error { // 网管本地路径 omcFilePath := "/usr/local/etc/omc/para5G.yaml" if runtime.GOOS == "windows" { omcFilePath = fmt.Sprintf("C:%s", omcFilePath) } - var err error - if fileType == "" || fileType == "txt" { - err = parse.ConvertConfigToFile(fileType, omcFilePath, content) - } - if fileType == "json" || fileType == "yaml" || fileType == "yml" { - err = parse.ConvertConfigToFile(fileType, omcFilePath, content) - } - if err != nil { + if err := parse.ConvertConfigToFile("yaml", omcFilePath, content); err != nil { return fmt.Errorf("please check if the file exists or write permissions") } @@ -555,7 +616,7 @@ func (r *NeInfoImpl) NeConfPara5GWirte(fileType string, content any, syncNE []st // 网元端配置路径 neFilePath := "/usr/local/etc/conf/para5G.yaml" - neFileDir := filepath.Dir(neFilePath) + neFileDir := filepath.ToSlash(filepath.Dir(neFilePath)) // 修改网元文件权限 sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod o+w %s && sudo chmod o+w %s", neFileDir, neFileDir, neFilePath)) // 复制到网元进行覆盖 diff --git a/src/modules/network_element/service/ne_version.impl.go b/src/modules/network_element/service/ne_version.impl.go index 38f40bd0..79d6d319 100644 --- a/src/modules/network_element/service/ne_version.impl.go +++ b/src/modules/network_element/service/ne_version.impl.go @@ -146,7 +146,7 @@ func (r *NeVersionImpl) Operate(action string, neVersion model.NeVersion, preinp } // 网元端配置路径 nePara5GFilePath := "/usr/local/etc/conf/para5G.yaml" - nePara5GFileDir := filepath.Dir(nePara5GFilePath) + nePara5GFileDir := filepath.ToSlash(filepath.Dir(nePara5GFilePath)) // 修改网元文件权限 sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod o+w %s && sudo chmod o+w %s", nePara5GFileDir, nePara5GFileDir, nePara5GFilePath)) // 复制到网元进行覆盖