743 lines
21 KiB
Go
743 lines
21 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"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"
|
|
)
|
|
|
|
// 实例化服务层 NeInfoImpl 结构体
|
|
var NewNeInfoImpl = &NeInfoImpl{
|
|
neInfoRepository: repository.NewNeInfoImpl,
|
|
Para5GData: map[string]string{},
|
|
}
|
|
|
|
// 网元信息 服务层处理
|
|
type NeInfoImpl struct {
|
|
// 网元信息数据信息
|
|
neInfoRepository repository.INeInfo
|
|
Para5GData map[string]string
|
|
}
|
|
|
|
// SelectNeInfoByNeTypeAndNeID 通过ne_type和ne_id查询网元信息
|
|
func (r *NeInfoImpl) SelectNeInfoByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
|
var neInfo model.NeInfo
|
|
key := fmt.Sprintf("%s%s:%s", cachekey.NE_KEY, strings.ToUpper(neType), neID)
|
|
jsonStr, _ := redis.Get("", key)
|
|
if len(jsonStr) > 7 {
|
|
err := json.Unmarshal([]byte(jsonStr), &neInfo)
|
|
if err != nil {
|
|
neInfo = model.NeInfo{}
|
|
}
|
|
} else {
|
|
neInfo = r.neInfoRepository.SelectNeInfoByNeTypeAndNeID(neType, neID)
|
|
if neInfo.ID != "" && neInfo.NeId == neID {
|
|
redis.Del("", key)
|
|
values, _ := json.Marshal(neInfo)
|
|
redis.Set("", key, string(values))
|
|
}
|
|
}
|
|
return neInfo
|
|
}
|
|
|
|
// RefreshByNeTypeAndNeID 通过ne_type和ne_id刷新redis中的缓存
|
|
func (r *NeInfoImpl) RefreshByNeTypeAndNeID(neType, neID string) model.NeInfo {
|
|
var neInfo model.NeInfo
|
|
key := fmt.Sprintf("%s%s:%s", cachekey.NE_KEY, strings.ToUpper(neType), neID)
|
|
redis.Del("", key)
|
|
neInfo = r.neInfoRepository.SelectNeInfoByNeTypeAndNeID(neType, neID)
|
|
if neInfo.ID != "" && neInfo.NeId == neID {
|
|
values, _ := json.Marshal(neInfo)
|
|
redis.Set("", key, string(values))
|
|
}
|
|
return neInfo
|
|
}
|
|
|
|
// ClearNeCacheByNeType 清除网元类型缓存
|
|
func (r *NeInfoImpl) ClearNeCacheByNeType(neType string) bool {
|
|
key := fmt.Sprintf("%s*", cachekey.NE_KEY)
|
|
if neType != "*" {
|
|
key = fmt.Sprintf("%s%s*", cachekey.NE_KEY, neType)
|
|
}
|
|
keys, err := redis.GetKeys("", key)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
delOk, _ := redis.DelKeys("", keys)
|
|
return delOk
|
|
}
|
|
|
|
// SelectNeInfoByRmuid 通过rmUID查询网元信息
|
|
func (r *NeInfoImpl) SelectNeInfoByRmuid(rmUid string) model.NeInfo {
|
|
var neInfo model.NeInfo
|
|
cacheKeys, _ := redis.GetKeys("", cachekey.NE_KEY+"*")
|
|
if len(cacheKeys) > 0 {
|
|
for _, key := range cacheKeys {
|
|
var v model.NeInfo
|
|
jsonStr, _ := redis.Get("", key)
|
|
if len(jsonStr) > 7 {
|
|
json.Unmarshal([]byte(jsonStr), &v)
|
|
}
|
|
if v.RmUID == rmUid {
|
|
neInfo = v
|
|
break
|
|
}
|
|
}
|
|
} else {
|
|
neInfos := r.SelectList(neInfo, false, false)
|
|
for _, v := range neInfos {
|
|
key := fmt.Sprintf("%s%s:%s", cachekey.NE_KEY, strings.ToUpper(v.NeType), v.NeId)
|
|
redis.Del("", key)
|
|
values, _ := json.Marshal(v)
|
|
redis.Set("", key, string(values))
|
|
if v.RmUID == rmUid {
|
|
neInfo = v
|
|
}
|
|
}
|
|
}
|
|
return neInfo
|
|
}
|
|
|
|
// SelectPage 根据条件分页查询
|
|
//
|
|
// bandStatus 带状态信息
|
|
func (r *NeInfoImpl) SelectPage(query map[string]any, bandStatus bool) map[string]any {
|
|
data := r.neInfoRepository.SelectPage(query)
|
|
|
|
// 网元直连读取网元服务状态
|
|
if bandStatus {
|
|
rows := data["rows"].([]model.NeInfo)
|
|
r.bandNeStatus(&rows)
|
|
}
|
|
|
|
return data
|
|
}
|
|
|
|
// SelectList 查询列表
|
|
//
|
|
// bandStatus 带状态信息
|
|
// bandHost 带主机信息
|
|
func (r *NeInfoImpl) SelectList(ne model.NeInfo, bandStatus bool, bandHost bool) []model.NeInfo {
|
|
list := r.neInfoRepository.SelectList(ne)
|
|
|
|
// 网元直连读取网元服务状态
|
|
if bandStatus {
|
|
r.bandNeStatus(&list)
|
|
}
|
|
|
|
// 网元主机信息
|
|
if bandHost {
|
|
r.bandNeHosts(&list)
|
|
}
|
|
|
|
return list
|
|
}
|
|
|
|
// bandNeStatus 网元列表项数据带网元服务状态
|
|
func (r *NeInfoImpl) bandNeStatus(arr *[]model.NeInfo) {
|
|
for i := range *arr {
|
|
v := (*arr)[i]
|
|
result, err := NeState(v)
|
|
if err != nil {
|
|
(*arr)[i].ServerState = map[string]any{
|
|
"online": false,
|
|
}
|
|
// 网元状态设置为离线
|
|
if v.Status != "0" {
|
|
v.Status = "0"
|
|
(*arr)[i].Status = v.Status
|
|
r.neInfoRepository.Update(v)
|
|
}
|
|
continue
|
|
}
|
|
result["online"] = true
|
|
(*arr)[i].ServerState = result
|
|
// 网元状态设置为在线
|
|
if v.Status != "1" {
|
|
// 下发网管配置信息给网元
|
|
_, err = NeConfigOMC(v)
|
|
if err == nil {
|
|
v.Status = "1"
|
|
} else {
|
|
v.Status = "2"
|
|
}
|
|
(*arr)[i].Status = v.Status
|
|
r.neInfoRepository.Update(v)
|
|
}
|
|
}
|
|
}
|
|
|
|
// bandNeHosts 网元列表项数据带网元主机信息
|
|
func (r *NeInfoImpl) bandNeHosts(arr *[]model.NeInfo) {
|
|
for i := range *arr {
|
|
v := (*arr)[i]
|
|
if v.HostIDs != "" {
|
|
(*arr)[i].Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(v.HostIDs, ","))
|
|
}
|
|
}
|
|
}
|
|
|
|
// SelectByIds 通过ID查询
|
|
//
|
|
// bandHost 带主机信息
|
|
func (r *NeInfoImpl) SelectById(infoId string, bandHost bool) model.NeInfo {
|
|
if infoId == "" {
|
|
return model.NeInfo{}
|
|
}
|
|
neInfos := r.neInfoRepository.SelectByIds([]string{infoId})
|
|
if len(neInfos) > 0 {
|
|
neInfo := neInfos[0]
|
|
// 带主机信息
|
|
if neInfo.HostIDs != "" && bandHost {
|
|
neInfo.Hosts = NewNeHostImpl.neHostRepository.SelectByIds(strings.Split(neInfo.HostIDs, ","))
|
|
}
|
|
return neInfo
|
|
}
|
|
return model.NeInfo{}
|
|
}
|
|
|
|
// Insert 新增信息
|
|
func (r *NeInfoImpl) Insert(neInfo model.NeInfo) string {
|
|
// 主机信息新增
|
|
if neInfo.Hosts != nil {
|
|
var hostIDs []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 := NewNeHostImpl.Insert(host)
|
|
if hostId != "" {
|
|
hostIDs = append(hostIDs, hostId)
|
|
}
|
|
}
|
|
neInfo.HostIDs = strings.Join(hostIDs, ",")
|
|
}
|
|
|
|
insertId := r.neInfoRepository.Insert(neInfo)
|
|
if insertId != "" {
|
|
// 刷新缓存
|
|
r.RefreshByNeTypeAndNeID(neInfo.NeType, neInfo.NeId)
|
|
}
|
|
return insertId
|
|
}
|
|
|
|
// Update 修改信息
|
|
func (r *NeInfoImpl) Update(neInfo model.NeInfo) int64 {
|
|
// 主机信息更新
|
|
if neInfo.Hosts != nil {
|
|
for _, host := range neInfo.Hosts {
|
|
if host.HostID != "" {
|
|
host.Title = fmt.Sprintf("%s_%s_%d", strings.ToUpper(neInfo.NeType), neInfo.NeId, host.Port)
|
|
host.GroupID = "1"
|
|
NewNeHostImpl.Update(host)
|
|
}
|
|
}
|
|
}
|
|
|
|
num := r.neInfoRepository.Update(neInfo)
|
|
if num > 0 {
|
|
// 刷新缓存
|
|
r.RefreshByNeTypeAndNeID(neInfo.NeType, neInfo.NeId)
|
|
}
|
|
return num
|
|
}
|
|
|
|
// DeleteByIds 批量删除信息
|
|
func (r *NeInfoImpl) DeleteByIds(infoIds []string) (int64, error) {
|
|
// 检查是否存在
|
|
infos := r.neInfoRepository.SelectByIds(infoIds)
|
|
if len(infos) <= 0 {
|
|
return 0, fmt.Errorf("neHostCmd.noData")
|
|
}
|
|
|
|
if len(infos) == len(infoIds) {
|
|
for _, v := range infos {
|
|
// 主机信息删除
|
|
if v.HostIDs != "" {
|
|
NewNeHostImpl.DeleteByIds(strings.Split(v.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})
|
|
}
|
|
// 缓存信息删除
|
|
redis.Del("", fmt.Sprintf("%s%s:%s", cachekey.NE_KEY, v.NeType, v.NeId))
|
|
}
|
|
rows := r.neInfoRepository.DeleteByIds(infoIds)
|
|
return rows, nil
|
|
}
|
|
// 删除信息失败!
|
|
return 0, fmt.Errorf("delete fail")
|
|
}
|
|
|
|
// CheckUniqueNeTypeAndNeId 校验同类型下标识是否唯一
|
|
func (r *NeInfoImpl) CheckUniqueNeTypeAndNeId(neType, neId, id string) bool {
|
|
uniqueId := r.neInfoRepository.CheckUniqueNeTypeAndNeId(model.NeInfo{
|
|
NeType: neType,
|
|
NeId: neId,
|
|
})
|
|
if uniqueId == id {
|
|
return true
|
|
}
|
|
return uniqueId == ""
|
|
}
|
|
|
|
// 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)
|
|
var client *ssh.ConnSSH
|
|
var err error
|
|
if neHost.AuthMode == "2" {
|
|
client, err = connSSH.NewClientByLocalPrivate()
|
|
} else {
|
|
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
|
|
}
|
|
|
|
// NeRunCMD 向网元发送cmd命令
|
|
func (r *NeInfoImpl) NeRunCMD(neType, neId, cmd string) (string, error) {
|
|
sshClient, err := r.NeRunSSHclient(neType, neId)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer sshClient.Close()
|
|
|
|
// 执行命令
|
|
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
|
|
}
|
|
|
|
// neConfOAMData 网元OAM配置文件默认格式数据
|
|
func (r *NeInfoImpl) neConfOAMData() map[string]any {
|
|
return map[string]any{
|
|
"httpManageCfg": map[string]any{
|
|
"ipType": "ipv4",
|
|
// 必改
|
|
"ipv4": "172.60.5.2",
|
|
"ipv6": "",
|
|
"port": 33030,
|
|
"scheme": "http",
|
|
},
|
|
"oamConfig": map[string]any{
|
|
"enable": true,
|
|
"ipType": "ipv4",
|
|
"ipv4": "172.60.5.1", // 必改
|
|
"ipv6": "",
|
|
"port": 33030,
|
|
"scheme": "http",
|
|
"neConfig": map[string]any{ // 必改
|
|
"neId": "001",
|
|
"rmUid": "4400HX1XXX001",
|
|
"neName": "XXX_001",
|
|
"dn": "-",
|
|
"vendorName": "GD",
|
|
"province": "-",
|
|
"pvFlag": "PNF",
|
|
},
|
|
},
|
|
"snmpConfig": map[string]any{
|
|
"enable": false,
|
|
"ipType": "ipv4",
|
|
"ipv4": "172.60.5.2", // 必改
|
|
"ipv6": "",
|
|
"port": 4957,
|
|
},
|
|
"kpiConfig": map[string]any{
|
|
"enable": true,
|
|
"timer": 60, // 必改
|
|
},
|
|
// "pubConfigPath": "/usr/local/etc/conf/para5G.yaml", // 网元只会读一次后续会置空,建议不放
|
|
}
|
|
}
|
|
|
|
// 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")
|
|
// 无保留文件时返回默认文件数据
|
|
oamData := r.neConfOAMData()
|
|
r.neConfOAMWirte(neType, neId, oamData, false)
|
|
return oamData, nil
|
|
}
|
|
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.ToSlash(filepath.Dir(neFilePath))
|
|
// 修改网元文件权限
|
|
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod 775 %s && sudo touch %s && sudo chmod o+w %s", neFileDir, neFileDir, neFilePath, neFilePath))
|
|
// 复制到网元进行覆盖
|
|
if err = sftpClient.CopyFileLocalToRemote(localFilePath, neFilePath); err != nil {
|
|
return fmt.Errorf("please check if scp remote copy is allowed")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// NeConfOAMSync 网元OAM配置文件生成并同步
|
|
func (r *NeInfoImpl) NeConfOAMSync(neInfo model.NeInfo, content map[string]any, sync bool) error {
|
|
oamData, err := r.NeConfOAMRead(neInfo.NeType, neInfo.NeId)
|
|
if oamData == nil || err != nil {
|
|
return fmt.Errorf("error read OAM file info")
|
|
}
|
|
// 网元HTTP服务
|
|
if v, ok := oamData["httpManageCfg"]; ok {
|
|
item := v.(map[string]any)
|
|
item["port"] = neInfo.Port
|
|
if strings.Contains(neInfo.IP, ":") {
|
|
item["ipType"] = "ipv6"
|
|
item["ipv6"] = neInfo.IP
|
|
}
|
|
if strings.Contains(neInfo.IP, ".") {
|
|
item["ipType"] = "ipv4"
|
|
item["ipv4"] = neInfo.IP
|
|
}
|
|
|
|
oamData["httpManageCfg"] = item
|
|
}
|
|
// 对网管HTTP配置
|
|
if v, ok := oamData["oamConfig"]; ok {
|
|
item := v.(map[string]any)
|
|
item["neConfig"] = map[string]string{
|
|
"neId": neInfo.NeId,
|
|
"rmUid": neInfo.RmUID,
|
|
"neName": neInfo.NeName,
|
|
"dn": neInfo.Dn,
|
|
"vendorName": neInfo.VendorName,
|
|
"province": neInfo.Province,
|
|
"pvFlag": neInfo.PvFlag,
|
|
}
|
|
|
|
if omcIP, ok := r.Para5GData["OMC_IP"]; ok && omcIP != "" {
|
|
if strings.Contains(omcIP, ":") {
|
|
item["ipType"] = "ipv6"
|
|
item["ipv6"] = omcIP
|
|
}
|
|
if strings.Contains(omcIP, ".") {
|
|
item["ipType"] = "ipv4"
|
|
item["ipv4"] = omcIP
|
|
}
|
|
}
|
|
|
|
if oamEnable, ok := content["oamEnable"]; ok && oamEnable != nil {
|
|
item["enable"] = parse.Boolean(oamEnable)
|
|
}
|
|
if oamPort, ok := content["oamPort"]; ok && oamPort != nil {
|
|
item["port"] = parse.Number(oamPort)
|
|
}
|
|
oamData["oamConfig"] = item
|
|
}
|
|
// 对网管SNMP配置
|
|
if v, ok := oamData["snmpConfig"]; ok {
|
|
item := v.(map[string]any)
|
|
if strings.Contains(neInfo.IP, ":") {
|
|
item["ipType"] = "ipv6"
|
|
item["ipv6"] = neInfo.IP
|
|
}
|
|
if strings.Contains(neInfo.IP, ".") {
|
|
item["ipType"] = "ipv4"
|
|
item["ipv4"] = neInfo.IP
|
|
}
|
|
|
|
if snmpEnable, ok := content["snmpEnable"]; ok && snmpEnable != nil {
|
|
item["enable"] = parse.Boolean(snmpEnable)
|
|
}
|
|
if snmpPort, ok := content["snmpPort"]; ok && snmpPort != nil {
|
|
item["port"] = parse.Number(snmpPort)
|
|
}
|
|
oamData["snmpConfig"] = item
|
|
}
|
|
// 对网管KPI上报配置
|
|
if v, ok := oamData["kpiConfig"]; ok {
|
|
item := v.(map[string]any)
|
|
if neInfo.NeType == "UPF" {
|
|
item["timer"] = 5
|
|
}
|
|
|
|
if kpiEnable, ok := content["kpiEnable"]; ok && kpiEnable != nil {
|
|
item["enable"] = parse.Boolean(kpiEnable)
|
|
}
|
|
if kpiTimer, ok := content["kpiTimer"]; ok && kpiTimer != nil {
|
|
item["timer"] = parse.Number(kpiTimer)
|
|
}
|
|
oamData["kpiConfig"] = item
|
|
}
|
|
if err := NewNeInfoImpl.neConfOAMWirte(neInfo.NeType, neInfo.NeId, oamData, sync); err != nil {
|
|
return fmt.Errorf("error wirte OAM file info")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// NeConfPara5GRead 网元公共配置文件读取
|
|
func (r *NeInfoImpl) NeConfPara5GRead() (map[string]any, error) {
|
|
// 网管本地路径
|
|
omcFilePath := "/usr/local/etc/omc/para5G.yaml"
|
|
if runtime.GOOS == "windows" {
|
|
omcFilePath = fmt.Sprintf("C:%s", omcFilePath)
|
|
}
|
|
// 读取文件内容
|
|
bytes, err := os.ReadFile(omcFilePath)
|
|
if err != nil {
|
|
logger.Warnf("NeConfPara5GRead 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("NeConfPara5GRead ConvertConfigToMap => %s", err.Error())
|
|
return nil, fmt.Errorf("content convert type error")
|
|
}
|
|
return mapData, nil
|
|
}
|
|
|
|
// NeConfPara5GWirte 网元公共配置文件写入 content内容 syncNE同步到网元端NeType@NeId
|
|
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)
|
|
}
|
|
|
|
if err := parse.ConvertConfigToFile("yaml", omcFilePath, content); err != nil {
|
|
return fmt.Errorf("please check if the file exists or write permissions")
|
|
}
|
|
|
|
// 同步到网元端
|
|
if len(syncNE) > 0 {
|
|
errMsg := []string{}
|
|
for _, neTI := range syncNE {
|
|
ti := strings.SplitN(neTI, "@", 2)
|
|
// 网元主机的SSH客户端
|
|
sshClient, err := r.NeRunSSHclient(ti[0], ti[1])
|
|
if err != nil {
|
|
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
|
|
continue
|
|
}
|
|
defer sshClient.Close()
|
|
// 网元主机的SSH客户端进行文件传输
|
|
sftpClient, err := sshClient.NewClientSFTP()
|
|
if err != nil {
|
|
errMsg = append(errMsg, fmt.Sprintf("%s : %s", ti, err.Error()))
|
|
continue
|
|
}
|
|
defer sftpClient.Close()
|
|
|
|
// 网元端配置路径
|
|
neFilePath := "/usr/local/etc/conf/para5G.yaml"
|
|
neFileDir := filepath.ToSlash(filepath.Dir(neFilePath))
|
|
// 修改网元文件权限
|
|
sshClient.RunCMD(fmt.Sprintf("sudo mkdir -p %s && sudo chmod 775 %s && sudo touch %s && sudo chmod o+rw %s", neFileDir, neFileDir, neFilePath, neFilePath))
|
|
// 复制到网元进行覆盖
|
|
if err = sftpClient.CopyFileLocalToRemote(omcFilePath, neFilePath); err != nil {
|
|
errMsg = append(errMsg, fmt.Sprintf("%s : please check if scp remote copy is allowed", ti))
|
|
continue
|
|
}
|
|
}
|
|
if len(errMsg) > 0 {
|
|
return fmt.Errorf(strings.Join(errMsg, "\r\n"))
|
|
}
|
|
}
|
|
|
|
// 转换一份数据到全局
|
|
r.Para5GData = r.neConfPara5GDataConvert(content)
|
|
return nil
|
|
}
|
|
|
|
// NeConfPara5GConvert 网元公共配置数据转化 content网元公共配置文件读取内容
|
|
func (r *NeInfoImpl) neConfPara5GDataConvert(content map[string]any) map[string]string {
|
|
basic := content["basic"].(map[string]any)
|
|
external := content["external"].(map[string]any)
|
|
sbi := content["sbi"].(map[string]any)
|
|
|
|
mcc := "460"
|
|
mnc := "01"
|
|
mncDomain := "001"
|
|
if plmnId, plmnIdOk := basic["plmnId"].(map[string]any); plmnIdOk {
|
|
mcc = plmnId["mcc"].(string)
|
|
mnc = plmnId["mnc"].(string)
|
|
// If a user input two digit MNC, add a leading zero
|
|
if len(mnc) == 2 {
|
|
mncDomain = fmt.Sprintf("0%s", mnc)
|
|
} else {
|
|
mncDomain = mnc
|
|
}
|
|
}
|
|
|
|
sst := "1"
|
|
sd := "000001"
|
|
if plmnId, plmnIdOk := basic["snssai"].(map[string]any); plmnIdOk {
|
|
sst = plmnId["sst"].(string)
|
|
sd = plmnId["sd"].(string)
|
|
}
|
|
|
|
n3IPAmdMask := external["upfn3_ip"].(string)
|
|
n3Arr := strings.Split(n3IPAmdMask, "/")
|
|
n3IP := n3Arr[0]
|
|
n3Mask := parse.ConvertIPMask(parse.Number(n3Arr[1]))
|
|
|
|
n6IPAmdMask := external["upfn6_ip"].(string)
|
|
n6Arr := strings.Split(n6IPAmdMask, "/")
|
|
n6IP := n6Arr[0]
|
|
n6Mask := parse.ConvertIPMask(parse.Number(n6Arr[1]))
|
|
|
|
ueIPAmdMask := external["ue_pool"].(string)
|
|
ueArr := strings.Split(ueIPAmdMask, "/")
|
|
ueIP := ueArr[0]
|
|
ueCicr := ueArr[1]
|
|
ueMask := parse.ConvertIPMask(parse.Number(ueArr[1]))
|
|
|
|
return map[string]string{
|
|
// basic
|
|
"TAC": basic["tac"].(string),
|
|
"MCC": mcc,
|
|
"MNC": mnc,
|
|
"MNC_DOMAIN": mncDomain,
|
|
"SST": sst,
|
|
"SD": sd,
|
|
"DNN_DATA": basic["dnn_data"].(string),
|
|
"DNN_IMS": basic["dnn_ims"].(string),
|
|
|
|
// external
|
|
"N2_IP": external["amfn2_ip"].(string),
|
|
"UE_POOL": external["ue_pool"].(string),
|
|
"UE_IP": ueIP,
|
|
"UE_MASK": ueMask,
|
|
"UE_CIDR": ueCicr,
|
|
"UPF_TYPE": external["upf_type"].(string), // StandardUPF LightUPF
|
|
"N3_IP": n3IP,
|
|
"N3_MASK": n3Mask,
|
|
"N3_GW": external["upfn3_gw"].(string),
|
|
"N3_PCI": external["upfn3_pci"].(string),
|
|
"N3_MAC": external["upfn3_mac"].(string),
|
|
"N6_IP": n6IP,
|
|
"N6_MASK": n6Mask,
|
|
"N6_GW": external["upfn6_gw"].(string),
|
|
"N6_PCI": external["upfn6_pci"].(string),
|
|
"N6_MAC": external["upfn6_mac"].(string),
|
|
|
|
"SIP_IP": external["ims_sip_ip"].(string),
|
|
|
|
"S1_MMEIP": external["mmes1_ip"].(string),
|
|
"S11_MMEIP": external["mmes11_ip"].(string),
|
|
"S10_MMEIP": external["mmes10_ip"].(string),
|
|
|
|
// sbi
|
|
"OMC_IP": sbi["omc_ip"].(string),
|
|
"IMS_IP": sbi["ims_ip"].(string),
|
|
"AMF_IP": sbi["amf_ip"].(string),
|
|
"AUSF_IP": sbi["ausf_ip"].(string),
|
|
"UDM_IP": sbi["udm_ip"].(string),
|
|
"ADB_IP": sbi["adb_ip"].(string),
|
|
"SMF_IP": sbi["smf_ip"].(string),
|
|
"PCF_IP": sbi["pcf_ip"].(string),
|
|
"NSSF_IP": sbi["nssf_ip"].(string),
|
|
"NRF_IP": sbi["nrf_ip"].(string),
|
|
"UPF_IP": sbi["upf_ip"].(string),
|
|
"LMF_IP": sbi["lmf_ip"].(string),
|
|
"NEF_IP": sbi["nef_ip"].(string),
|
|
"MME_IP": sbi["mme_ip"].(string),
|
|
"N3IWF_IP": sbi["n3iwf_ip"].(string),
|
|
}
|
|
}
|