add: 提交
This commit is contained in:
165
crontask/gencmxml.go
Normal file
165
crontask/gencmxml.go
Normal file
@@ -0,0 +1,165 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
cmschema "ems.agt/crontask/cm/schema"
|
||||
"ems.agt/lib/global"
|
||||
"ems.agt/lib/log"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const (
|
||||
// Header is a generic XML header suitable for use with the output of Marshal.
|
||||
// This is not automatically added to any output of this package,
|
||||
// it is provided as a convenience.
|
||||
Header = `<?xml version="1.0" encoding="UTF-8"?>` + "\n"
|
||||
)
|
||||
|
||||
const (
|
||||
AdminStateLocked = "Locked"
|
||||
AdminStateUnlocked = "Unlocked"
|
||||
AdminStateShuttingdown = "ShuttingDown"
|
||||
OperationStateDisabled = "Disabled"
|
||||
OperationStateEnabled = "Enabled"
|
||||
)
|
||||
|
||||
func (t *TaskFunc) GenCmXmlFile(uri, params, body string) {
|
||||
log.Debug("GenCmXmlFile processing ...")
|
||||
for _, neType := range neTypes {
|
||||
t.GenNFXmlFile(neType)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *TaskFunc) ReadCmYaml(cmfile string) (map[string]interface{}, error) {
|
||||
log.Debug("cmfile:", cmfile)
|
||||
file, err := os.ReadFile(cmfile)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
log.Debug("file:", file)
|
||||
resultMap := make(map[string]interface{})
|
||||
err = yaml.Unmarshal(file, resultMap)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Debug("resultMap:", resultMap)
|
||||
return resultMap, nil
|
||||
}
|
||||
|
||||
func (t *TaskFunc) GenNFXmlFile(neType string) error {
|
||||
var nes []NeInfo
|
||||
_, err := XormGetNeInfoByType(neType, &nes)
|
||||
if err != nil {
|
||||
log.Error("Failed to get all ne info:", err)
|
||||
return err
|
||||
}
|
||||
|
||||
cmfile := fmt.Sprintf("%s/cm-%s.yaml", yamlConfig.NBI.CM.CfgFileDir, strings.ToLower(neType))
|
||||
|
||||
resultMap, _ := t.ReadCmYaml(cmfile)
|
||||
|
||||
ti := time.Now()
|
||||
timestamp := ti.Format("2006-01-02 15:04:05")
|
||||
timefile := ti.Format("20060102150405")
|
||||
date := ti.Format("20060102")
|
||||
_, offset := ti.Zone()
|
||||
var tzOffset string
|
||||
if offset >= 0 {
|
||||
tzOffset = "UTC+" + strconv.Itoa(offset/3600)
|
||||
} else {
|
||||
tzOffset = "UTC" + strconv.Itoa(offset/3600)
|
||||
}
|
||||
|
||||
nrmFile := new(cmschema.DataFile)
|
||||
|
||||
nrmFile.FileHeader = cmschema.FileHeader{
|
||||
TimeStamp: timestamp,
|
||||
TimeZone: tzOffset,
|
||||
VendorName: "Ruijie Network",
|
||||
ElementType: neType,
|
||||
CmVersion: global.Version,
|
||||
}
|
||||
|
||||
nrmFile.XsiAttr = "http://www.w3.org/2001/XMLSchema-instance"
|
||||
nrmFile.XsiLoc = "file:///usr/loal/omc/etc/schema/cm-schema.xsd"
|
||||
|
||||
for k, e := range resultMap {
|
||||
objects := cmschema.Objects{ObjectType: k}
|
||||
|
||||
sortResult := make(map[string]string)
|
||||
keys := make([]string, 0)
|
||||
for key, value := range e.(map[string]interface{}) {
|
||||
sortResult[key] = fmt.Sprintf("%v", value)
|
||||
keys = append(keys, key)
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for s, ne := range nes {
|
||||
rmUID := fmt.Sprintf("01000HX%sBJ0D0%d", neType, s+1)
|
||||
vmId := fmt.Sprintf("kylin10.0-00%d-%s", s+1, neType)
|
||||
vnfInstanceID := fmt.Sprintf("2%xd55b4-%d018-41f4-af%d5-28b%d828788", s+10, s+6, s+4, s+3)
|
||||
object := cmschema.Object{RmUIDAttr: rmUID,
|
||||
DnAttr: "DC=www.ruijie.com.cn,SubNetwork=10001,SubNetwork=114214,ManagedElement=325",
|
||||
UserLabelAttr: ne.NeName, PVFlagAttr: ne.PvFlag, VMIDAttr: vmId, VNFInstanceIDAttr: vnfInstanceID}
|
||||
|
||||
i := 1
|
||||
for _, p := range keys {
|
||||
if s == 0 {
|
||||
objects.FieldName.N = append(objects.FieldName.N, cmschema.N{IAttr: i, Value: p})
|
||||
}
|
||||
var v string
|
||||
switch p {
|
||||
case "Id":
|
||||
v = ne.NeId
|
||||
case "UserLabel":
|
||||
v = ne.NeName
|
||||
case "VendorName":
|
||||
v = "Ruijie Network"
|
||||
case "SwVersion":
|
||||
v = global.Version
|
||||
case "PatchInfo":
|
||||
v = "-"
|
||||
case "AdministrativeState":
|
||||
v = AdminStateUnlocked
|
||||
case "OperationalState":
|
||||
v = OperationStateEnabled
|
||||
case "VnfInstanceId":
|
||||
v = vnfInstanceID
|
||||
default:
|
||||
if sortResult[p] == "" {
|
||||
v = "-"
|
||||
} else {
|
||||
v = fmt.Sprintf("%s", sortResult[p])
|
||||
}
|
||||
}
|
||||
object.V = append(object.V, cmschema.V{IAttr: i, Value: v})
|
||||
i++
|
||||
}
|
||||
objects.FieldValue.Object = append(objects.FieldValue.Object, object)
|
||||
}
|
||||
nrmFile.Objects = append(nrmFile.Objects, objects)
|
||||
}
|
||||
|
||||
folderPath := global.CreateDir(date, yamlConfig.NBI.CM.XmlFileDir)
|
||||
x, _ := xml.MarshalIndent(nrmFile, "", " ")
|
||||
x = append([]byte(xml.Header), x...)
|
||||
xmlfile := fmt.Sprintf("%s/CM-HX-%s-%s-%s.xml", folderPath, neType, global.Version, timefile)
|
||||
err = os.WriteFile(xmlfile, x, 0664)
|
||||
if err != nil {
|
||||
log.Error("Failed to write xml file:", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user