Skip to content

Commit

Permalink
BGP Added
Browse files Browse the repository at this point in the history
  • Loading branch information
sunami09 committed Dec 3, 2024
1 parent a4681de commit f4898ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 38 deletions.
62 changes: 25 additions & 37 deletions autogole-api/src/python/RTMonLibs/DiagramWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
from diagrams import Diagram, Cluster, Edge
from diagrams.custom import Custom
from RTMonLibs.GeneralLibs import _processName
# # change later:
# def _processName(name):
# """Process Name for Mermaid and replace all special chars with _"""
# for repl in [[" ", "_"], [":", "_"], ["/", "_"], ["-", "_"], [".", "_"], ["?", "_"]]:
# name = name.replace(repl[0], repl[1])
# return name
# #########

class DiagramWorker:
"""
Expand All @@ -18,6 +25,9 @@ class DiagramWorker:
"""
HOST_ICON_PATH = '/srv/icons/host.png'
SWITCH_ICON_PATH = '/srv/icons/switch.png'
# # TEmp
# HOST_ICON_PATH = '/Users/sunami/Desktop/publish/sense-rtmon/autogole-api/packaging/icons/host.png'
# SWITCH_ICON_PATH = '/Users/sunami/Desktop/publish/sense-rtmon/autogole-api/packaging/icons/switch.png'

def __init__(self, indata, instance, manifest):
"""
Expand Down Expand Up @@ -136,45 +146,23 @@ def d_addSwitch(self, item):
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)
ip_label2 = ""
sitename = item["Site"]

if self.instance != None:
tempData = self.instance["intents"]
for flow in tempData:
terminals = flow["json"]["data"]["connections"][0]["terminals"]
for connection in terminals:
if connection["uri"] == sitename:
ip_label2 = connection["ipv6_prefix_list"]
break
ip_node = Custom(ip_label + "\n" + ip_label2, self.HOST_ICON_PATH)
switch1 >> Edge() << ip_node

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
7 changes: 6 additions & 1 deletion autogole-api/src/python/RTMonLibs/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os.path
from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml, escape, getUUID, _processName
from RTMonLibs.DiagramWorker import DiagramWorker

#import json
def clamp(n, minn, maxn):
"""Clamp the value between min and max"""
return max(min(maxn, n), minn)
Expand Down Expand Up @@ -639,6 +639,11 @@ def t_createTemplate(self, *args, **kwargs):
#Generate Diagrams
try:
diagram_filename = f"{self.config.get('image_dir', '/srv/images')}/diagram_{kwargs['referenceUUID']}"
# diagram_json = f"diagram_{kwargs['referenceUUID']}.json"
# with open("selfOrder" + diagram_json, 'w') as file:
# json.dump(self.orderlist, file, indent=2)
# with open("original_args" + diagram_json, 'w') as file:
# json.dump(orig_args, file, indent=2)
DiagramWorker(self.orderlist, *orig_args).createGraph(diagram_filename)
self.logger.info(f"Diagram saved at {diagram_filename}.png")
except IOError as ex:
Expand Down

0 comments on commit f4898ac

Please sign in to comment.