Skip to content

Commit 1afee7c

Browse files
committed
[nxos] Include LLDP adjacencies in Interface GetStatus
Retrieve LLDP neighbor information while fetching interface status. The LLDPAdjacencyItems struct models the NX-OS DME path for LLDP adjacency data per interface.
1 parent 6e32713 commit 1afee7c

3 files changed

Lines changed: 81 additions & 23 deletions

File tree

‎internal/provider/cisco/nxos/lldp.go‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type LLDPIfItem struct {
2828
InterfaceName string `json:"id"`
2929
AdminRxSt Option[AdminSt] `json:"adminRxSt"`
3030
AdminTxSt Option[AdminSt] `json:"adminTxSt"`
31+
TlvSetVlan Option[uint16] `json:"tlvSetVlan"`
3132
}
3233

3334
func (i *LLDPIfItem) Key() string { return i.InterfaceName }
@@ -41,3 +42,27 @@ func (*LLDPOper) IsListItem() {}
4142
func (*LLDPOper) XPath() string {
4243
return "System/fm-items/lldp-items"
4344
}
45+
46+
// LLDPAdjacencyItems represents the LLDP neighbor information for a single interface.
47+
type LLDPAdjacencyItems struct {
48+
// ID is the identifier of the interface for which the LLDP neighbor information is being retrieved, e.g., "eth1/1".
49+
ID string `json:"-"`
50+
AdjItems struct {
51+
AdjEpList []struct {
52+
ChassisIdT uint8 `json:"chassisIdT"`
53+
ChassisIdV string `json:"chassisIdV"`
54+
PortIdT uint8 `json:"portIdT"`
55+
PortIdV string `json:"portIdV"`
56+
PortDesc string `json:"portDesc,omitempty"`
57+
SysName string `json:"sysName,omitempty"`
58+
SysDesc string `json:"sysDesc,omitempty"`
59+
TTL int32 `json:"ttl"`
60+
} `json:"AdjEp-list,omitzero"`
61+
} `json:"adj-items,omitzero"`
62+
}
63+
64+
func (p *LLDPAdjacencyItems) XPath() string {
65+
return "System/lldp-items/inst-items/if-items/If-list[id=" + p.ID + "]"
66+
}
67+
68+
func (*LLDPAdjacencyItems) IsListItem() {}

‎internal/provider/cisco/nxos/lldp_test.go‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,35 @@
33

44
package nxos
55

6-
func init() {
7-
lldp := &LLDP{
8-
HoldTime: NewOption(uint16(200)),
9-
InitDelay: NewOption(uint16(5)),
10-
}
11-
12-
lldp.IfItems.IfList.Set(&LLDPIfItem{
13-
InterfaceName: "eth7/1",
14-
AdminRxSt: NewOption(AdminStDisabled),
15-
AdminTxSt: NewOption(AdminStDisabled),
16-
})
6+
import (
7+
"testing"
8+
)
179

18-
lldp.IfItems.IfList.Set(&LLDPIfItem{
19-
InterfaceName: "eth8/1",
20-
AdminTxSt: NewOption(AdminStDisabled),
21-
})
10+
func TestLLDPAdjacencyItems_XPath(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
id string
14+
want string
15+
}{
16+
{
17+
name: "ethernet interface 1/1",
18+
id: "eth1/1",
19+
want: "System/lldp-items/inst-items/if-items/If-list[id=eth1/1]",
20+
},
21+
{
22+
name: "ethernet interface 1/48",
23+
id: "eth1/48",
24+
want: "System/lldp-items/inst-items/if-items/If-list[id=eth1/48]",
25+
},
26+
}
2227

23-
Register("lldp", lldp)
28+
for _, tt := range tests {
29+
t.Run(tt.name, func(t *testing.T) {
30+
adj := &LLDPAdjacencyItems{ID: tt.id}
31+
got := adj.XPath()
32+
if got != tt.want {
33+
t.Errorf("LLDPAdjacencyItems.XPath() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
2437
}

‎internal/provider/cisco/nxos/provider.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,18 +1137,35 @@ func (p *Provider) GetInterfaceStatus(ctx context.Context, req *provider.Interfa
11371137
}
11381138

11391139
var (
1140-
operSt OperSt
1141-
operMsg string
1140+
operSt OperSt
1141+
operMsg string
1142+
lldpAdjacencies []provider.LLDPAdjacency
11421143
)
11431144
switch req.Interface.Spec.Type {
11441145
case v1alpha1.InterfaceTypePhysical:
11451146
phys := new(PhysIfOperItems)
11461147
phys.ID = name
1147-
if err := p.client.GetState(ctx, phys); err != nil && !errors.Is(err, gnmiext.ErrNil) {
1148+
lldpAdj := new(LLDPAdjacencyItems)
1149+
lldpAdj.ID = name
1150+
if err := p.client.GetState(ctx, phys, lldpAdj); err != nil && !errors.Is(err, gnmiext.ErrNil) {
11481151
return provider.InterfaceStatus{}, err
11491152
}
11501153
operSt = phys.OperSt
11511154

1155+
for _, adj := range lldpAdj.AdjItems.AdjEpList {
1156+
neighbor := provider.LLDPAdjacency{
1157+
ChassisID: adj.ChassisIdV,
1158+
ChassisIDType: adj.ChassisIdT,
1159+
PortID: adj.PortIdV,
1160+
PortIDType: adj.PortIdT,
1161+
PortDescription: adj.PortDesc,
1162+
SysName: adj.SysName,
1163+
SysDescription: adj.SysDesc,
1164+
TTL: uint32(adj.TTL), // #nosec G115
1165+
}
1166+
lldpAdjacencies = append(lldpAdjacencies, neighbor)
1167+
}
1168+
11521169
case v1alpha1.InterfaceTypeLoopback:
11531170
lb := new(LoopbackOperItems)
11541171
lb.ID = name
@@ -1178,10 +1195,13 @@ func (p *Provider) GetInterfaceStatus(ctx context.Context, req *provider.Interfa
11781195
return provider.InterfaceStatus{}, fmt.Errorf("unsupported interface type: %s", req.Interface.Spec.Type)
11791196
}
11801197

1181-
return provider.InterfaceStatus{
1182-
OperStatus: operSt == OperStUp,
1183-
OperMessage: operMsg,
1184-
}, nil
1198+
status := provider.InterfaceStatus{
1199+
OperStatus: operSt == OperStUp,
1200+
OperMessage: operMsg,
1201+
LLDPAdjacencies: lldpAdjacencies,
1202+
}
1203+
1204+
return status, nil
11851205
}
11861206

11871207
var ErrInterfaceNotFound = errors.New("one or more interfaces do not exist")

0 commit comments

Comments
 (0)