fix: 网元信息新增更新同步操作版本和授权信息

This commit is contained in:
TsMask
2024-04-11 17:13:54 +08:00
parent ca0f047f22
commit bc599ec5b0
5 changed files with 209 additions and 90 deletions

View File

@@ -10,6 +10,7 @@ import (
"be.ems/src/framework/constants/cachekey"
"be.ems/src/framework/logger"
"be.ems/src/framework/redis"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/ssh"
"be.ems/src/modules/network_element/model"
"be.ems/src/modules/network_element/repository"
@@ -18,15 +19,12 @@ import (
// 实例化服务层 NeInfoImpl 结构体
var NewNeInfoImpl = &NeInfoImpl{
neInfoRepository: repository.NewNeInfoImpl,
neHostRepository: repository.NewNeHostImpl,
}
// 网元信息 服务层处理
type NeInfoImpl struct {
// 网元信息数据信息
neInfoRepository repository.INeInfo
// 网元主机连接表
neHostRepository repository.INeHost
}
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
@@ -126,8 +124,8 @@ func (r *NeInfoImpl) SelectPage(query map[string]any, bandStatus bool) map[strin
"online": false,
}
// 网元状态设置为离线
if v.Status != "1" {
v.Status = "1"
if v.Status != "0" {
v.Status = "0"
(*arr)[i].Status = v.Status
r.neInfoRepository.Update(v)
}
@@ -136,13 +134,13 @@ func (r *NeInfoImpl) SelectPage(query map[string]any, bandStatus bool) map[strin
result["online"] = true
(*arr)[i].ServerState = result
// 网元状态设置为在线
if v.Status != "0" {
if v.Status != "1" {
// 下发网管配置信息给网元
_, err = NeConfigOMC(v)
if err != nil {
v.Status = "3"
if err == nil {
v.Status = "1"
} else {
v.Status = "0"
v.Status = "2"
}
(*arr)[i].Status = v.Status
r.neInfoRepository.Update(v)
@@ -170,8 +168,8 @@ func (r *NeInfoImpl) SelectList(ne model.NeInfo, bandStatus bool) []model.NeInfo
"online": false,
}
// 网元状态设置为离线
if v.Status != "1" {
v.Status = "1"
if v.Status != "0" {
v.Status = "0"
(*neList)[i].Status = v.Status
r.neInfoRepository.Update(v)
}
@@ -183,10 +181,10 @@ func (r *NeInfoImpl) SelectList(ne model.NeInfo, bandStatus bool) []model.NeInfo
if v.Status != "0" {
// 下发网管配置信息给网元
_, err = NeConfigOMC(v)
if err != nil {
v.Status = "3"
if err == nil {
v.Status = "1"
} else {
v.Status = "0"
v.Status = "2"
}
(*neList)[i].Status = v.Status
r.neInfoRepository.Update(v)
@@ -209,7 +207,7 @@ func (r *NeInfoImpl) SelectById(infoId string, bandHost bool) model.NeInfo {
neInfo := neInfos[0]
// 带主机信息
if neInfo.HostIDs != "" && bandHost {
neInfo.Hosts = r.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
}
return neInfo
}
@@ -224,7 +222,7 @@ func (r *NeInfoImpl) Insert(neInfo model.NeInfo) string {
for _, host := range neInfo.Hosts {
host.Title = fmt.Sprintf("%s_%s_%d", strings.ToUpper(neInfo.NeType), neInfo.NeId, host.Port)
host.GroupID = "1"
hostId := r.neHostRepository.Insert(host)
hostId := NewNeHostImpl.Insert(host)
if hostId != "" {
hostIDs = append(hostIDs, hostId)
}
@@ -248,7 +246,7 @@ func (r *NeInfoImpl) Update(neInfo model.NeInfo) int64 {
if host.HostID != "" {
host.Title = fmt.Sprintf("%s_%s_%d", strings.ToUpper(neInfo.NeType), neInfo.NeId, host.Port)
host.GroupID = "1"
r.neHostRepository.Update(host)
NewNeHostImpl.Update(host)
}
}
}
@@ -274,7 +272,17 @@ func (r *NeInfoImpl) DeleteByIds(infoIds []string) (int64, error) {
// 主机信息删除
if v.HostIDs != "" {
hostIds := strings.Split(v.HostIDs, ",")
r.neHostRepository.DeleteByIds(hostIds)
NewNeHostImpl.DeleteByIds(hostIds)
}
// 删除License
neLicense := NewNeLicenseImpl.SelectByNeTypeAndNeID(v.NeType, v.NeId)
if neLicense.NeId == v.NeId {
NewNeLicenseImpl.DeleteByIds([]string{neLicense.ID})
}
// 删除Version
neVersion := NewNeVersionImpl.SelectByNeTypeAndNeID(v.NeType, v.NeId)
if neVersion.NeId == v.NeId {
NewNeVersionImpl.DeleteByIds([]string{neVersion.ID})
}
// 缓存信息删除
key := fmt.Sprintf("%s%s:%s", cachekey.NE_KEY, v.NeType, v.NeId)
@@ -301,47 +309,56 @@ func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neType, neId, infoId string) bool
// NeRunCMD 向网元发送cmd命令
func (r *NeInfoImpl) NeRunCMD(neType, neId, cmd string) (string, error) {
neInfo := r.SelectNeInfoByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId {
logger.Errorf("NeRunCMD NeType:%s NeID:%s not found", neType, neId)
return "", fmt.Errorf("neinfo not found")
}
// 带主机信息
if neInfo.HostIDs != "" {
neInfo.Hosts = r.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
if len(neInfo.Hosts) <= 0 {
logger.Errorf("NeRunCMD Hosts %s not found", neInfo.HostIDs)
return "", fmt.Errorf("neinfo host not found")
}
}
neHost := neInfo.Hosts[0]
if neHost.HostType != "ssh" {
logger.Errorf("NeRunCMD Hosts first HostType %s not ssh", neHost.HostType)
return "", fmt.Errorf("neinfo host type not ssh")
}
var connSSH ssh.ConnSSH
neHost.CopyTo(&connSSH)
client, err := connSSH.NewClient()
sshClient, err := r.NeRunSSHclient(neType, neId)
if err != nil {
logger.Errorf("NeRunCMD NewClient err => %s", err.Error())
return "", fmt.Errorf("neinfo ssh client new err")
return "", err
}
defer client.Close()
defer sshClient.Close()
// 执行命令
output, err := client.RunCMD(cmd)
output, err := sshClient.RunCMD(cmd)
if err != nil {
logger.Errorf("NeRunCMD RunCMD %s err => %s", output, err.Error())
return "", fmt.Errorf("neinfo ssh run cmd err")
}
return output, nil
}
// NeRunSSHclient 网元主机的SSH客户端-为创建相关连接
func (r *NeInfoImpl) NeRunSSHclient(neType, neId string) (*ssh.ConnSSH, error) {
neInfo := r.SelectNeInfoByNeTypeAndNeID(neType, neId)
if neInfo.NeId != neId {
logger.Errorf("NeRunSSHclient NeType:%s NeID:%s not found", neType, neId)
return nil, fmt.Errorf("neinfo not found")
}
// 取主机信息
if neInfo.HostIDs == "" {
logger.Errorf("NeRunSSHclient NeType:%s NeID:%s hostId not found", neType, neId)
return nil, fmt.Errorf("neinfo hostId not found")
}
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
if len(neInfo.Hosts) <= 0 {
logger.Errorf("NeRunSSHclient Hosts %s not found", neInfo.HostIDs)
return nil, fmt.Errorf("neinfo host not found")
}
neHost := neInfo.Hosts[0]
if neHost.HostType != "ssh" {
logger.Errorf("NeRunSSHclient Hosts first HostType %s not ssh", neHost.HostType)
return nil, fmt.Errorf("neinfo host type not ssh")
}
var connSSH ssh.ConnSSH
neHost.CopyTo(&connSSH)
client, err := connSSH.NewClient()
if err != nil {
logger.Errorf("NeRunSSHclient NewClient err => %s", err.Error())
return nil, fmt.Errorf("neinfo ssh client new err")
}
return client, nil
}
// NeConfigFileRead 网元配置文件读取 网元配置yaml文件复制到本地后通过filePath读取
func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath string) []string {
files := []string{}
func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath, fileType string) any {
neTypeLower := strings.ToLower(neInfo.NeType)
// 网管本地路径
@@ -356,12 +373,23 @@ func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath string) []st
bytes, err := os.ReadFile(fmt.Sprintf("%s/%s", omcPath, filePath))
if err != nil {
logger.Warnf("NeConfigFile ReadFile => %s", err.Error())
return files
return "read file error"
}
files = append(files, string(bytes))
return files
content := string(bytes)
if fileType == "" || fileType == "txt" {
return content
}
// 序列化Map
mapData, err := parse.ConvertConfigToMap(fileType, content)
if err != nil {
logger.Warnf("NeConfigFile ConvertConfigToMap => %s", err.Error())
return "content convert type error"
}
return mapData
}
// 文件列表
files := []string{}
// 删除原有配置文件
// err := os.RemoveAll(omcPath)
// if err != nil {
@@ -373,7 +401,7 @@ func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath string) []st
nePath := "/usr/local/etc"
nePath = fmt.Sprintf("%s/%s", nePath, neTypeLower)
// 各个网元与网间约定配置文件
// 各个网元与网间约定配置文件
err := ssh.FileSCPNeToLocal(neInfo.IP, nePath+"/oam_manager.yaml", omcPath+"/oam_manager.yaml")
if err == nil {
files = append(files, "oam_manager.yaml")
@@ -402,7 +430,7 @@ func (r *NeInfoImpl) NeConfigFileRead(neInfo model.NeInfo, filePath string) []st
}
// NeConfigFileWirte 网元配置文件写入 content内容 sync同步到网元端
func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, content string, sync bool) error {
func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, fileType string, content any, sync bool) error {
neTypeLower := strings.ToLower(neInfo.NeType)
// 网管本地路径
@@ -412,7 +440,13 @@ func (r *NeInfoImpl) NeConfigFileWirte(neInfo model.NeInfo, filePath, content st
}
localFilePath := fmt.Sprintf("%s/%s/%s/%s", omcPath, neTypeLower, neInfo.NeId, filePath)
err := os.WriteFile(localFilePath, []byte(content), 0644)
var err error
if fileType == "" || fileType == "txt" {
err = parse.ConvertConfigToFile(fileType, localFilePath, content)
}
if fileType == "json" || fileType == "yaml" || fileType == "yml" {
err = parse.ConvertConfigToFile(fileType, localFilePath, content)
}
if err != nil {
logger.Warnf("NeConfigFile WriteFile => %s", err.Error())
return fmt.Errorf("please check if the file exists or write permissions")