:param switch_before: the switch that you come from. None if switch_start is the first one
:param detected: list of all switchs already visited. None if switch_start is the first one
:return: A list of all the links found and a list of all the switchs visited"""
detected.append(switch_start)
links_return=[]# list of dictionaries of the links to be detected
forportinswitch_start.ports.filter(related__isnull=False):# Ports that are related to another switch
ifport.related.switch!=switch_beforeandport.related.switch!=port.switch:# Not the switch that we come from, not the current switch
forportinswitch_start.ports.filter(related__isnull=False):# create links to every switchs below
ifport.related.switch!=switch_beforeandport.related.switch!=port.switchandport.related.switchnotindetected:# Not the switch that we come from, not the current switch
links={# Dictionary of a link
'depart':switch_start.id,
'arrive':port.related.switch.id
}
ifport.related.switchnotindetected:# The switch at the end of this link has not been visited
links_down,detected=recursive_switchs(port.related.switch,switch_start,detected)# explore it and get the results
forlinkinlinks_down:# Add the non empty links to the current list
iflink:
links_return.append(link)
links_return.append(links)# Add current and below levels links
detected.append(switch_start)# This switch is considered detected
forportinswitch_start.ports.filter(related__isnull=False):# go down on every related switchs
ifport.related.switchnotindetected:# The switch at the end of this link has not been visited
links_down,detected=recursive_switchs(port.related.switch,switch_start,detected)# explore it and get the results
forlinkinlinks_down:# Add the non empty links to the current list