Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
door7302 committed Oct 18, 2023
1 parent cb3962c commit 5de0f09
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 23 deletions.
26 changes: 22 additions & 4 deletions netconf/netconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ func (r *RouterTask) Work() error {

var rawData *xml.RawData
rawData = new(xml.RawData)
rawData.IfInfo = new(xml.Ifdesc)
rawData.IfDesc = new(xml.Ifdesc)
rawData.IfList = new(xml.Iflist)
rawData.HwInfo = new(xml.Hw)
rawData.LacpInfo = new(xml.Lacp)
rawData.LacpDigest = new(xml.LacpDigest)
Expand Down Expand Up @@ -116,7 +117,7 @@ func (r *RouterTask) Work() error {

} else {
// Unmarshall the reply
rawData.IfInfo, err = xml.ParseIfdesc(reply.Data)
rawData.IfDesc, err = xml.ParseIfdesc(reply.Data)
if err != nil {
logger.Log.Warnf("[%s] Unable to parse interface description: %v", r.Name, err)
} else {
Expand All @@ -125,6 +126,23 @@ func (r *RouterTask) Work() error {

}

d = "<get-interface-information><terse/></get-interface-information>"
rpc = message.NewRPC(d)
reply, err = session.SyncRPC(rpc, int32(r.Timeout))
if err != nil || reply == nil || strings.Contains(reply.Data, "<rpc-error>") {
logger.Log.Warnf("[%s] No interfaces terse information: %v", r.Name, err)

} else {
// Unmarshall the reply
rawData.IfList, err = xml.ParseIflist(reply.Data)
if err != nil {
logger.Log.Warnf("[%s] Unable to parse interface terse: %v", r.Name, err)
} else {
hasIf = true
}

}

d = "<get-chassis-inventory></get-chassis-inventory>"
rpc = message.NewRPC(d)
reply, err = session.SyncRPC(rpc, int32(r.Timeout))
Expand Down Expand Up @@ -165,12 +183,12 @@ func (r *RouterTask) Work() error {
logger.Log.Debug("----- Interface Descriptions -----")
logger.Log.Debug("")
logger.Log.Debug(" Physicals Intf:")
for _, v := range rawData.IfInfo.Physicals {
for _, v := range rawData.IfDesc.Physicals {
logger.Log.Debugf(" ├─ %s : %s", strings.Trim(v.Name, "\n"), strings.Trim(v.Desc, "\n"))
}
logger.Log.Debug("")
logger.Log.Debug(" Logicals Intf:")
for _, v := range rawData.IfInfo.Logicals {
for _, v := range rawData.IfDesc.Logicals {
logger.Log.Debugf(" ├─ %s : %s", strings.Trim(v.Name, "\n"), strings.Trim(v.Desc, "\n"))
}
logger.Log.Debug("--------------------------------------------------------------------")
Expand Down
47 changes: 29 additions & 18 deletions output/jsonify.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,40 @@ func (m *Metadata) UpdateMeta(rd *xml.RawData) error {
m.Meta[rd.Family][rd.RtrName] = map[string]map[string]string{}
}

// ADD physical / logical interface description
for _, phy := range rd.IfInfo.Physicals {
for _, phy := range rd.IfDesc.Physicals {
phy_name := strings.Trim(phy.Name, "\n")
phy_desc := strings.Trim(phy.Desc, "\n")
_, ok := m.Meta[rd.Family][rd.RtrName][phy_name]
if !ok {
m.Meta[rd.Family][rd.RtrName][phy_name] = make(map[string]string)
}
// Keep only WAN ports
if strings.Contains(phy_name, "et-") || strings.Contains(phy_name, "xe-") || strings.Contains(phy_name, "ge-") {
_, ok := m.Meta[rd.Family][rd.RtrName][phy_name]
if !ok {
m.Meta[rd.Family][rd.RtrName][phy_name] = make(map[string]string)
}
//Default description TAG
m.Meta[rd.Family][rd.RtrName][phy_name]["DESC"] = "Unknown"
m.Meta[rd.Family][rd.RtrName][phy_name]["LINKNAME"] = phy_name + " - " + "Unknown"

m.Meta[rd.Family][rd.RtrName][phy_name]["DESC"] = strings.ToUpper(strings.Replace(strings.Replace(phy_desc, " ", "", -1), "-", "_", -1))
if phy_desc != "" {
m.Meta[rd.Family][rd.RtrName][phy_name]["LINKNAME"] = phy_name + " - " + strings.ToUpper(strings.Replace(strings.Replace(phy_desc, " ", "", -1), "-", "_", -1))
} else {
m.Meta[rd.Family][rd.RtrName][phy_name]["LINKNAME"] = phy_name
}
// Add also the parent LAG name if physical interface is a child link.
val, ok := rd.LacpDigest.LacpMap[phy_name]
if ok {
m.Meta[rd.Family][rd.RtrName][phy_name]["LAG"] = val
}

// check if PHY port has a description
// ADD physical description if present
for _, phy2 := range rd.IfDesc.Physicals {
phy_name := strings.Trim(phy2.Name, "\n")
phy_desc := strings.Trim(phy2.Desc, "\n")

// Add also the parent LAG name if physical interface is a child link.
val, ok := rd.LacpDigest.LacpMap[phy_name]
if ok {
m.Meta[rd.Family][rd.RtrName][phy_name]["LAG"] = val
if phy2 == phy && phy_desc != "" {
m.Meta[rd.Family][rd.RtrName][phy_name]["LINKNAME"] = phy_name + " - " + strings.ToUpper(strings.Replace(strings.Replace(phy_desc, " ", "", -1), "-", "_", -1))
m.Meta[rd.Family][rd.RtrName][phy_name]["DESC"] = strings.ToUpper(strings.Replace(strings.Replace(phy_desc, " ", "", -1), "-", "_", -1))
}
}
}
}
for _, lgl := range rd.IfInfo.Logicals {

// ADD logical description
for _, lgl := range rd.IfDesc.Logicals {
lgl_name := strings.Trim(lgl.Name, "\n")
lgl_desc := strings.Trim(lgl.Desc, "\n")
_, ok := m.Meta[rd.Family][rd.RtrName][lgl_name]
Expand Down
31 changes: 30 additions & 1 deletion xml/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
type RawData struct {
RtrName string
Family string
IfInfo *Ifdesc
IfDesc *Ifdesc
IfList *Iflist
HwInfo *Hw
LacpInfo *Lacp
LacpDigest *LacpDigest
Expand Down Expand Up @@ -40,6 +41,23 @@ type Log struct {
Desc string `xml:"description"`
}

// Structs for unmarshalling interfaces descriptions
type Iflist struct {
XMLName xml.Name `xml:"interface-information"`
Physicals []PhyList `xml:"physical-interface"`
}

type PhyList struct {
XMLName xml.Name `xml:"physical-interface"`
Name string `xml:"name"`
Logicals []LogList `xml:"logical-interface"`
}

type LogList struct {
XMLName xml.Name `xml:"logical-interface"`
Name string `xml:"name"`
}

// structs for umarshalling chassis hw
type Hw struct {
XMLName xml.Name `xml:"chassis-inventory"`
Expand Down Expand Up @@ -127,6 +145,17 @@ func ParseIfdesc(s string) (*Ifdesc, error) {
return &i, err
}

// Parsing function for interfaces terse
func ParseIflist(s string) (*Iflist, error) {
logger.HandlePanic()
var i Iflist
// convert in byte array
b := []byte(s)
// unmarshall xml string
err := xml.Unmarshal(b, &i)
return &i, err
}

// Parsing function for chassis hw
func ParseChassis(s string) (*Hw, error) {
logger.HandlePanic()
Expand Down

0 comments on commit 5de0f09

Please sign in to comment.