feat: 网元OAM配置文件读写接口和部分接口参数变更

This commit is contained in:
TsMask
2024-04-25 17:21:48 +08:00
parent 4e33857f66
commit 795fa18ee2
5 changed files with 166 additions and 37 deletions

View File

@@ -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))
// 复制到网元进行覆盖