Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagram class related to #127 #135

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Added BGP
  • Loading branch information
Sunami Dasgupta authored and Sunami Dasgupta committed Nov 8, 2024
commit 0c0728511f55a678ad9f31f0c8d6ca8daa43034c
58 changes: 51 additions & 7 deletions autogole-api/src/python/RTMonLibs/DiagramWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def __init__(self, indata):
self.linksadded = set()
self.popreverse = None

def _d_findItem(self, fval, fkey):
def d_find_item(self, fval, fkey):
"""Find Item where fkey == fval"""
for key, vals in self.objects.items():
if vals.get('data', {}).get(fkey, '') == fval:
Expand Down Expand Up @@ -75,19 +75,19 @@ def d_addLinks(self):
for key, vals in self.objects.items():
data_type = vals.get('data', {}).get('Type', '')
if data_type == "Host":
fKey, fItem = self._d_findItem(key, 'PeerHost')
fKey, fItem = self.d_find_item(key, 'PeerHost')
if fKey and fItem:
self.d_addLink(self.objects[key], fItem, key, fKey)
self.d_addLink(vals, fItem, key, fKey)
elif data_type == "Switch":
if 'Peer' in vals.get('data', {}) and vals['data']['Peer'] != "?peer?":
fKey, fItem = self._d_findItem(vals['data']['Peer'], "Port")
fKey, fItem = self.d_find_item(vals['data']['Peer'], "Port")
if fKey and fItem:
self.d_addLink(self.objects[key], fItem, key, fKey)
self.d_addLink(vals, fItem, key, fKey)
elif 'PeerHost' in vals.get('data', {}):
fKey = vals['data']['PeerHost']
fItem = self.objects.get(fKey)
if fItem:
self.d_addLink(self.objects[key], fItem, key, fKey)
self.d_addLink(vals, fItem, key, fKey)

def d_addHost(self, item):
"""
Expand Down Expand Up @@ -129,7 +129,51 @@ def d_addSwitch(self, item):
uniqname = _processName(f'{item["Node"]}_{item["Name"]}')
self.added[item['Node']] = uniqname
self.objects[uniqname] = {"obj": switch1, "data": item}
# Add IPv4/IPv6 on the switch
for ipkey, ipdef in {'IPv4': '?port_ipv4?', 'IPv6': '?port_ipv6?'}.items():
if ipkey in item and item[ipkey] != ipdef:
ip_node_name = f"{item['Node']}_{ipkey}"
ip_label = item[ipkey]
ip_node = Custom(ip_label, self.HOST_ICON_PATH)
self.objects[ip_node_name] = {"obj": ip_node, "data": {}}
# Add edge between switch and IP node
self.d_addLink(self.objects[item['Port']], self.objects[ip_node_name], item['Port'], ip_node_name)
if item.get('Vlan'):
vlan_node_name = f"{item['Node']}_vlan{item['Vlan']}"
vlan_label = f"vlan.{item['Vlan']}"
vlan_node = Custom(vlan_label, self.SWITCH_ICON_PATH)
self.objects[vlan_node_name] = {"obj": vlan_node, "data": {}}
self.d_addLink(self.objects[item['Port']], self.objects[vlan_node_name], item['Port'], vlan_node_name)
self.d_addLink(self.objects[vlan_node_name], self.objects[ip_node_name], vlan_node_name, ip_node_name)
# Add BGP Peering information
bgppeer = ip_node_name
self.d_addBGP(item, ipkey, bgppeer)
return switch1

def d_addBGP(self, item, ipkey, bgppeer):
"""Add BGP into the network diagram"""
if not item.get('Site', None):
return
if not self.instance:
return
for intitem in self.instance.get('intents', []):
for connections in intitem.get('json', {}).get('data', {}).get('connections', []):
for terminal in connections.get('terminals', []):
if 'uri' not in terminal:
continue
if item['Site'] == terminal['uri'] and terminal.get(f'{ipkey.lower()}_prefix_list', None):
val = terminal[f'{ipkey.lower()}_prefix_list']
bgp_node_name = f"{bgppeer}_bgp{ipkey}"
bgp_node_label = f"BGP_{ipkey}"
bgp_node = Custom(bgp_node_label, self.SWITCH_ICON_PATH)
self.objects[bgp_node_name] = {"obj": bgp_node, "data": {}}
self.d_addLink(self.objects[bgppeer], self.objects[bgp_node_name], bgppeer, bgp_node_name)
peer_node_name = f"{bgppeer}_bgp{ipkey}_peer"
peer_node_label = val
peer_node = Custom(peer_node_label, self.SWITCH_ICON_PATH)
self.objects[peer_node_name] = {"obj": peer_node, "data": {}}
self.d_addLink(self.objects[bgp_node_name], self.objects[peer_node_name], bgp_node_name, peer_node_name)


def addItem(self, item):
"""
Expand Down Expand Up @@ -185,7 +229,7 @@ def createGraph(self, output_filename):

with Diagram("Network Topology", show=False, filename=output_filename):
while len(self.indata) > 0:
if self.popreverse == None or self.popreverse == False:
if self.popreverse in (None, False):
item = self.indata.pop(0)
elif self.popreverse == True:
item = self.indata.pop()
Expand Down