Skip to content

Commit ccc8336

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 2428889 commit ccc8336

3 files changed

Lines changed: 80 additions & 23 deletions

File tree

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,27 @@ func (*LLDPOper) IsListItem() {}
4141
func (*LLDPOper) XPath() string {
4242
return "System/fm-items/lldp-items"
4343
}
44+
45+
// LLDPAdjacencyItems represents the LLDP neighbor information for a single interface.
46+
type LLDPAdjacencyItems struct {
47+
// ID is the identifier of the interface for which the LLDP neighbor information is being retrieved, e.g., "eth1/1".
48+
ID string `json:"-"`
49+
AdjItems struct {
50+
AdjEpList []struct {
51+
ChassisIdT uint8 `json:"chassisIdT"`
52+
ChassisIdV string `json:"chassisIdV"`
53+
PortIdT uint8 `json:"portIdT"`
54+
PortIdV string `json:"portIdV"`
55+
PortDesc string `json:"portDesc,omitempty"`
56+
SysName string `json:"sysName,omitempty"`
57+
SysDesc string `json:"sysDesc,omitempty"`
58+
TTL int32 `json:"ttl"`
59+
} `json:"AdjEp-list,omitzero"`
60+
} `json:"adj-items,omitzero"`
61+
}
62+
63+
func (p *LLDPAdjacencyItems) XPath() string {
64+
return "System/lldp-items/inst-items/if-items/If-list[id=" + p.ID + "]"
65+
}
66+
67+
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
@@ -1138,18 +1138,35 @@ func (p *Provider) GetInterfaceStatus(ctx context.Context, req *provider.Interfa
11381138
}
11391139

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

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

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

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

0 commit comments

Comments
 (0)