Skip to content

Commit

Permalink
Refine LsNodeNLRI and LsLinkNLRI stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
yas-nyan authored and watal committed Jan 10, 2023
1 parent a555074 commit 37f04b3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 48 deletions.
89 changes: 49 additions & 40 deletions pkg/packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5299,13 +5299,20 @@ func (l *LsLinkNLRI) String() string {
if l.LocalNodeDesc == nil || l.RemoteNodeDesc == nil {
return "LINK { EMPTY }"
}
var local string
var remote string
if l.LsNLRI.ProtocolID == LS_PROTOCOL_BGP {
local = l.LocalNodeDesc.(*LsTLVNodeDescriptor).Extract().BGPRouterID.String()
remote = l.RemoteNodeDesc.(*LsTLVNodeDescriptor).Extract().BGPRouterID.String()
} else {
local = l.LocalNodeDesc.(*LsTLVNodeDescriptor).Extract().IGPRouterID
remote = l.RemoteNodeDesc.(*LsTLVNodeDescriptor).Extract().IGPRouterID
}

local := l.LocalNodeDesc.(*LsTLVNodeDescriptor).Extract()
remote := l.RemoteNodeDesc.(*LsTLVNodeDescriptor).Extract()
link := &LsLinkDescriptor{}
link.ParseTLVs(l.LinkDesc)

return fmt.Sprintf("LINK { LOCAL_NODE: %v REMOTE_NODE: %v LINK: %v}", local.IGPRouterID, remote.IGPRouterID, link)
return fmt.Sprintf("LINK { LOCAL_NODE: %v REMOTE_NODE: %v LINK: %v}", local, remote, link)
}

func (l *LsLinkNLRI) DecodeFromBytes(data []byte) error {
Expand Down Expand Up @@ -5887,6 +5894,7 @@ func NewLsAttributeTLVs(lsAttr *LsAttribute) []LsTLVInterface {
}

if lsAttr.BgpPeerSegment.BgpPeerNodeSid != nil {
// TODO
tlvs = append(tlvs, NewLsTLVPeerNodeSID(lsAttr.BgpPeerSegment.BgpPeerNodeSid))
}
if lsAttr.BgpPeerSegment.BgpPeerAdjacencySid != nil {
Expand Down Expand Up @@ -8715,6 +8723,29 @@ func (l *LsTLVNodeDescriptor) DecodeFromBytes(data []byte) error {
return nil
}

func (l *LsTLVNodeDescriptor) Extract() *LsNodeDescriptor {
nd := &LsNodeDescriptor{}

for _, tlv := range l.SubTLVs {
switch v := tlv.(type) {
case *LsTLVAutonomousSystem:
nd.Asn = v.ASN
case *LsTLVBgpLsID:
nd.BGPLsID = v.BGPLsID
case *LsTLVOspfAreaID:
nd.OspfAreaID = v.AreaID
case *LsTLVIgpRouterID:
nd.IGPRouterID, nd.PseudoNode = parseIGPRouterID(v.RouterID)
case *LsTLVBgpRouterID:
nd.BGPRouterID = v.RouterID
case *LsTLVBgpConfederationMember:
nd.BGPConfederationMember = v.BgpConfederationMember
}
}

return nd
}

func (l *LsTLVNodeDescriptor) Serialize() ([]byte, error) {
buf := []byte{}
for _, tlv := range l.SubTLVs {
Expand All @@ -8729,20 +8760,6 @@ func (l *LsTLVNodeDescriptor) Serialize() ([]byte, error) {
return l.LsTLV.Serialize(buf)
}

func (l *LsTLVNodeDescriptor) String() string {
nd := l.Extract()

if nd.BGPRouterID == nil {
return fmt.Sprintf("{ASN: %v, BGP LS ID: %v, OSPF AREA: %v, IGP ROUTER ID: %v}", nd.Asn, nd.BGPLsID, nd.OspfAreaID, nd.IGPRouterID)
}

if l.LsTLV.Type == LS_TLV_REMOTE_NODE_DESC {
return fmt.Sprintf("{ASN: %v, BGP LS ID: %v, BGP ROUTER ID: %v, BGP CONFEDERATION MEMBER: %v}", nd.Asn, nd.BGPLsID, nd.BGPRouterID, nd.BGPConfederationMember)
}

return fmt.Sprintf("{ASN: %v, BGP LS ID: %v, BGP ROUTER ID: %v}", nd.Asn, nd.BGPLsID, nd.BGPRouterID)
}

func (l *LsTLVNodeDescriptor) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type LsTLVType `json:"type"`
Expand All @@ -8753,6 +8770,12 @@ func (l *LsTLVNodeDescriptor) MarshalJSON() ([]byte, error) {
})
}

func (l *LsTLVNodeDescriptor) String() string {
nd := l.Extract()

return nd.String()
}

type LsNodeDescriptor struct {
Asn uint32 `json:"asn"`
BGPLsID uint32 `json:"bgp_ls_id"`
Expand All @@ -8763,6 +8786,15 @@ type LsNodeDescriptor struct {
BGPConfederationMember uint32 `json:"bgp_confederation_member"`
}

func (l *LsNodeDescriptor) String() string {

if l.BGPRouterID == nil {
return fmt.Sprintf("{ASN: %v, BGP LS ID: %v, OSPF AREA: %v, IGP ROUTER ID: %v}", l.Asn, l.BGPLsID, l.OspfAreaID, l.IGPRouterID)
}

return fmt.Sprintf("{ASN: %v, BGP LS ID: %v, BGP ROUTER ID: %v}", l.Asn, l.BGPLsID, l.BGPRouterID)
}

func parseIGPRouterID(id []byte) (string, bool) {
switch len(id) {
// OSPF or OSPFv3 non-pseudonode
Expand All @@ -8786,29 +8818,6 @@ func parseIGPRouterID(id []byte) (string, bool) {
}
}

func (l *LsTLVNodeDescriptor) Extract() *LsNodeDescriptor {
nd := &LsNodeDescriptor{}

for _, tlv := range l.SubTLVs {
switch v := tlv.(type) {
case *LsTLVAutonomousSystem:
nd.Asn = v.ASN
case *LsTLVBgpLsID:
nd.BGPLsID = v.BGPLsID
case *LsTLVOspfAreaID:
nd.OspfAreaID = v.AreaID
case *LsTLVIgpRouterID:
nd.IGPRouterID, nd.PseudoNode = parseIGPRouterID(v.RouterID)
case *LsTLVBgpRouterID:
nd.BGPRouterID = v.RouterID
case *LsTLVBgpConfederationMember:
nd.BGPConfederationMember = v.BgpConfederationMember
}
}

return nd
}

// Generate LsTLVNodeDescriptor from LsNodeDescriptor
func NewLsTLVNodeDescriptor(nd *LsNodeDescriptor, tlvType LsTLVType) LsTLVNodeDescriptor {
subTLVs := []LsTLVInterface{}
Expand Down
9 changes: 1 addition & 8 deletions pkg/packet/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3049,14 +3049,7 @@ func Test_LsNodeDescriptor(t *testing.T) {
0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063
0x02, 0x04, 0x00, 0x04, 0x0a, 0xff, 0x00, 0x01, // TLV BGP ROUTER ID: "10.255.0.1"
0x02, 0x05, 0x00, 0x04, 0x07, 0x07, 0x07, 0x08, // TLV BGP CONFEDERATION MEMBER: 117901064
}, "{ASN: 117901063, BGP LS ID: 117901063, BGP ROUTER ID: 10.255.0.1, BGP CONFEDERATION MEMBER: 117901064}",
false, true},
{[]byte{
0x01, 0x01, 0x00, 0x18, // Remote Node Desc
0x02, 0x00, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV ASN: 117901063
0x02, 0x01, 0x00, 0x04, 0x07, 0x07, 0x07, 0x07, // TLV BGP LS ID: 117901063
0x02, 0x04, 0x00, 0x04, 0x0a, 0xff, 0x00, 0x01, // TLV BGP ROUTER ID: "10.255.0.1"
}, "{ASN: 117901063, BGP LS ID: 117901063, BGP ROUTER ID: 10.255.0.1, BGP CONFEDERATION MEMBER: 0}",
}, "{ASN: 117901063, BGP LS ID: 117901063, BGP ROUTER ID: 10.255.0.1}",
false, true},
}

Expand Down

0 comments on commit 37f04b3

Please sign in to comment.