22
33from django .shortcuts import render
44from django .views .generic import View
5- from dcim .models import Cable , Device , Interface , DeviceRole
5+ from dcim .models import Cable , Device , Interface , DeviceRole , PowerFeed
66from ipam .models import VLAN
77from .models import SavedTopology
88from . import forms , filters
@@ -334,22 +334,42 @@ def get_topology(nb_devices_qs):
334334 tags = filter_tags (tags )
335335 for tag in tags :
336336 all_device_tags .add ((tag , not tag_is_hidden (tag )))
337- links_from_device = Cable .objects .filter (_termination_a_device_id = nb_device .id )
338- links_to_device = Cable .objects .filter (_termination_b_device_id = nb_device .id )
339- if links_from_device :
340- # Device is considered passive if it has no linked Interfaces.
341- # Passive cabling devices use Rear and Front Ports.
342- for link in links_from_device :
343- if isinstance (link .termination_a , Interface ) and link .termination_a .device .id == nb_device .id :
344- break
345- else :
346- device_is_passive = True
347- if links_to_device :
348- for link in links_to_device :
349- if isinstance (link .termination_b , Interface ) and link .termination_b .device .id == nb_device .id :
350- break
351- else :
352- device_is_passive = True
337+ # Device is considered passive if it has no linked Interfaces.
338+ # Passive cabling devices use Rear and Front Ports.
339+ if NETBOX_CURRENT_VERSION < version .parse ("3.3" ):
340+ links_from_device = Cable .objects .filter (_termination_a_device_id = nb_device .id )
341+ links_to_device = Cable .objects .filter (_termination_b_device_id = nb_device .id )
342+ if links_from_device :
343+ for link in links_from_device :
344+ if isinstance (link .termination_a , Interface ) and link .termination_a .device .id == nb_device .id :
345+ break
346+ else :
347+ device_is_passive = True
348+ if links_to_device :
349+ for link in links_to_device :
350+ if isinstance (link .termination_b , Interface ) and link .termination_b .device .id == nb_device .id :
351+ break
352+ else :
353+ device_is_passive = True
354+ else :
355+ links_from_device = Cable .objects .filter (terminations__cable_end = 'A' , terminations___device_id = nb_device .id )
356+ links_to_device = Cable .objects .filter (terminations__cable_end = 'B' , terminations___device_id = nb_device .id )
357+ interfaces_found = False
358+ if links_from_device :
359+ for link in links_from_device :
360+ for ab_link in link .a_terminations + link .b_terminations :
361+ if isinstance (ab_link , Interface ) and ab_link .device .id == nb_device .id :
362+ interfaces_found = True
363+ break
364+ if links_to_device :
365+ for link in links_to_device :
366+ for ab_link in link .a_terminations + link .b_terminations :
367+ if isinstance (ab_link , Interface ) and ab_link .device .id == nb_device .id :
368+ interfaces_found = True
369+ break
370+ if links_to_device or links_from_device :
371+ device_is_passive = not interfaces_found
372+
353373 topology_dict ['nodes' ].append ({
354374 'id' : nb_device .id ,
355375 'name' : nb_device .name ,
@@ -372,9 +392,18 @@ def get_topology(nb_devices_qs):
372392 if not links_from_device :
373393 continue
374394 for link in links_from_device :
375- # Include links to discovered devices only
376- if link ._termination_b_device_id in device_ids :
377- links .append (link )
395+ if NETBOX_CURRENT_VERSION < version .parse ("3.3" ):
396+ # Include links to discovered devices only
397+ if link ._termination_b_device_id in device_ids :
398+ links .append (link )
399+ else :
400+ # Exclude PowerFeed-connected links
401+ if (isinstance (link .a_terminations [0 ], PowerFeed ) or (isinstance (link .b_terminations [0 ], PowerFeed ))):
402+ continue
403+ # Include links to discovered devices only
404+ if link .b_terminations [0 ].device_id in device_ids :
405+ links .append (link )
406+
378407 device_roles = list (device_roles )
379408 device_roles .sort (key = lambda i : get_node_layer_sort_preference (i [0 ]))
380409 all_device_tags = list (all_device_tags )
@@ -384,6 +413,9 @@ def get_topology(nb_devices_qs):
384413 link_ids = set ()
385414 for link in links :
386415 link_ids .add (link .id )
416+ if NETBOX_CURRENT_VERSION > version .parse ("3.3" ):
417+ link .termination_a = link .a_terminations [0 ]
418+ link .termination_b = link .b_terminations [0 ]
387419 topology_dict ['links' ].append ({
388420 'id' : link .id ,
389421 'source' : link .termination_a .device .id ,
0 commit comments