@@ -20,36 +20,131 @@ func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interfa
20
20
return nil , err
21
21
}
22
22
23
+ interfacesWithVLANs , err := juniperAddVLANsNonELS (ctx , interfaces )
24
+ if err != nil {
25
+ log .Ctx (ctx ).Debug ().Err (err ).Msg ("getting juniper VLANs for non ELS devices failed, trying for ELS devices" )
26
+ interfacesWithVLANs , err = juniperAddVLANsELS (ctx , interfaces )
27
+ if err != nil {
28
+ log .Ctx (ctx ).Debug ().Err (err ).Msg ("getting juniper VLANs for ELS devices failed, skipping VLANs" )
29
+ interfacesWithVLANs = interfaces
30
+ }
31
+ }
32
+
33
+ return interfacesWithVLANs , nil
34
+ }
35
+
36
+ func juniperAddVLANsELS (ctx context.Context , interfaces []device.Interface ) ([]device.Interface , error ) {
23
37
con , ok := network .DeviceConnectionFromContext (ctx )
24
38
if ! ok || con .SNMP == nil {
25
39
return nil , errors .New ("snmp client is empty" )
26
40
}
27
41
28
- // dot1dBasePortIfIndex
29
- res , err := con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.2 .1.17.1.4 .1.2 " )
42
+ // jnxL2aldVlanFdbId
43
+ res , err := con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.4 .1.2636.3.48.1.3 .1.1.5 " )
30
44
if err != nil {
31
- log .Ctx (ctx ).Debug ().Err (err ).Msg ("failed to get dot1dBasePortIfIndex, skipping VLANs" )
32
- return interfaces , nil
45
+ return nil , errors .Wrap (err , "failed to get jnxL2aldVlanFdbId" )
33
46
}
34
47
35
- portIfIndex := make (map [string ]string )
48
+ vlanIndexFilterID := make (map [string ]string )
36
49
for _ , response := range res {
37
- ifIndex , err := response .GetValueString ()
50
+ filterID , err := response .GetValueString ()
38
51
if err != nil {
39
52
return nil , err
40
53
}
41
54
42
55
oid := response .GetOID ()
43
56
oidSplit := strings .Split (oid , "." )
44
57
45
- portIfIndex [oidSplit [len (oidSplit )- 1 ]] = ifIndex
58
+ vlanIndexFilterID [oidSplit [len (oidSplit )- 1 ]] = filterID
59
+ }
60
+
61
+ // jnxL2aldVlanName
62
+ res , err = con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.4.1.2636.3.48.1.3.1.1.2" )
63
+ if err != nil {
64
+ return nil , errors .Wrap (err , "failed to get jnxL2aldVlanName" )
65
+ }
66
+
67
+ filterIDVLAN := make (map [string ]device.VLAN )
68
+ for _ , response := range res {
69
+ name , err := response .GetValueString ()
70
+ if err != nil {
71
+ return nil , err
72
+ }
73
+
74
+ oid := response .GetOID ()
75
+ oidSplit := strings .Split (oid , "." )
76
+ filterID := vlanIndexFilterID [oidSplit [len (oidSplit )- 1 ]]
77
+
78
+ filterIDVLAN [filterID ] = device.VLAN {
79
+ Name : name ,
80
+ }
81
+ }
82
+
83
+ portIfIndex , err := juniperGetPortIfIndexMapping (ctx )
84
+ if err != nil {
85
+ return nil , err
86
+ }
87
+
88
+ // dot1qTpFdbPort
89
+ dot1qTpFdbPort := "1.3.6.1.2.1.17.7.1.2.2.1.2"
90
+ res , err = con .SNMP .SnmpClient .SNMPWalk (ctx , dot1qTpFdbPort )
91
+ if err != nil {
92
+ return nil , errors .Wrap (err , "failed to get dot1qTpFdbPort" )
93
+ }
94
+
95
+ ifIndexFilterIDs := make (map [string ][]string )
96
+ out:
97
+ for _ , response := range res {
98
+ port , err := response .GetValueString ()
99
+ if err != nil {
100
+ return nil , err
101
+ }
102
+
103
+ oid := strings .TrimPrefix (response .GetOID (), "." )
104
+ oidSplit := strings .Split (strings .TrimPrefix (strings .TrimPrefix (oid , dot1qTpFdbPort ), "." ), "." )
105
+ ifIndex := portIfIndex [port ]
106
+
107
+ for _ , filterID := range ifIndexFilterIDs [ifIndex ] {
108
+ if filterID == oidSplit [0 ] {
109
+ continue out
110
+ }
111
+ }
112
+ ifIndexFilterIDs [ifIndex ] = append (ifIndexFilterIDs [ifIndex ], oidSplit [0 ])
113
+ }
114
+
115
+ for i , interf := range interfaces {
116
+ if interf .IfIndex != nil {
117
+ if filterIDs , ok := ifIndexFilterIDs [fmt .Sprint (* interf .IfIndex )]; ok {
118
+ for _ , filterID := range filterIDs {
119
+ if vlan , ok := filterIDVLAN [filterID ]; ok {
120
+ if interfaces [i ].VLAN == nil {
121
+ interfaces [i ].VLAN = & device.VLANInformation {}
122
+ }
123
+ interfaces [i ].VLAN .VLANs = append (interfaces [i ].VLAN .VLANs , vlan )
124
+ }
125
+ }
126
+ }
127
+ }
128
+ }
129
+
130
+ return interfaces , nil
131
+ }
132
+
133
+ func juniperAddVLANsNonELS (ctx context.Context , interfaces []device.Interface ) ([]device.Interface , error ) {
134
+ con , ok := network .DeviceConnectionFromContext (ctx )
135
+ if ! ok || con .SNMP == nil {
136
+ return nil , errors .New ("snmp client is empty" )
46
137
}
47
138
48
139
// jnxExVlanPortStatus
49
- res , err = con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.4.1.2636.3.40.1.5.1.7.1.3" )
140
+ res , err : = con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.4.1.2636.3.40.1.5.1.7.1.3" )
50
141
if err != nil {
51
- log .Ctx (ctx ).Debug ().Err (err ).Msg ("failed to get jnxExVlanPortStatus, skipping VLANs" )
52
- return interfaces , nil
142
+ return nil , errors .Wrap (err , "failed to get jnxExVlanPortStatus" )
143
+ }
144
+
145
+ portIfIndex , err := juniperGetPortIfIndexMapping (ctx )
146
+ if err != nil {
147
+ return nil , err
53
148
}
54
149
55
150
vlanIndexVLAN := make (map [string ]device.VLAN )
@@ -73,8 +168,7 @@ func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interfa
73
168
// jnxExVlanName
74
169
res , err = con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.4.1.2636.3.40.1.5.1.5.1.2" )
75
170
if err != nil {
76
- log .Ctx (ctx ).Debug ().Err (err ).Msg ("failed to get jnxExVlanName, skipping VLANs" )
77
- return interfaces , nil
171
+ return nil , errors .Wrap (err , "failed to get jnxExVlanName" )
78
172
}
79
173
80
174
for _ , response := range res {
@@ -109,3 +203,31 @@ func (c *junosCommunicator) GetInterfaces(ctx context.Context) ([]device.Interfa
109
203
110
204
return interfaces , nil
111
205
}
206
+
207
+ func juniperGetPortIfIndexMapping (ctx context.Context ) (map [string ]string , error ) {
208
+ con , ok := network .DeviceConnectionFromContext (ctx )
209
+ if ! ok || con .SNMP == nil {
210
+ return nil , errors .New ("snmp client is empty" )
211
+ }
212
+
213
+ // dot1dBasePortIfIndex
214
+ res , err := con .SNMP .SnmpClient .SNMPWalk (ctx , "1.3.6.1.2.1.17.1.4.1.2" )
215
+ if err != nil {
216
+ return nil , errors .Wrap (err , "failed to get dot1dBasePortIfIndex" )
217
+ }
218
+
219
+ portIfIndex := make (map [string ]string )
220
+ for _ , response := range res {
221
+ ifIndex , err := response .GetValueString ()
222
+ if err != nil {
223
+ return nil , err
224
+ }
225
+
226
+ oid := response .GetOID ()
227
+ oidSplit := strings .Split (oid , "." )
228
+
229
+ portIfIndex [oidSplit [len (oidSplit )- 1 ]] = ifIndex
230
+ }
231
+
232
+ return portIfIndex , nil
233
+ }
0 commit comments