From 096b20e1ab0c497ff12de6401e1ac749b2c766a7 Mon Sep 17 00:00:00 2001 From: Sunami Dasgupta Date: Mon, 21 Oct 2024 18:49:12 -0700 Subject: [PATCH] PR fixed --- autogole-api/packaging/Dockerfile | 2 +- .../src/python/RTMonLibs/DiagramWorker.py | 57 +++++++++++++++++-- .../src/python/RTMonLibs/GeneralLibs.py | 6 ++ autogole-api/src/python/RTMonLibs/Template.py | 8 +-- 4 files changed, 59 insertions(+), 14 deletions(-) diff --git a/autogole-api/packaging/Dockerfile b/autogole-api/packaging/Dockerfile index 6e77ffd..c3d051d 100644 --- a/autogole-api/packaging/Dockerfile +++ b/autogole-api/packaging/Dockerfile @@ -21,7 +21,7 @@ RUN git clone https://github.com/sdn-sense/sense-o-py-client.git /opt/sense-o-py # Install RTMON (TODO, replace to ESnet repo once merged) RUN git clone https://github.com/esnet/sense-rtmon.git /opt/sense-rtmon && \ - cd /opt/sense-rtmon/autogole-api/ && pip3 install -r requirements.txt && pip3 install diagrams && pip3 install . && \ + cd /opt/sense-rtmon/autogole-api/ && pip3 install -r requirements.txt && pip3 install . && \ cp src/templates/* /etc/rtmon/templates/ RUN wget https://raw.githubusercontent.com/sdn-sense/rm-configs/master/CAs/SiteRM.pem -O /etc/grid-security/certificates/e52ac827.0 diff --git a/autogole-api/src/python/RTMonLibs/DiagramWorker.py b/autogole-api/src/python/RTMonLibs/DiagramWorker.py index 2ad27c2..85739d3 100644 --- a/autogole-api/src/python/RTMonLibs/DiagramWorker.py +++ b/autogole-api/src/python/RTMonLibs/DiagramWorker.py @@ -1,19 +1,30 @@ +""" +DiagramWorker Class for Network Topology Visualization +This module contains the DiagramWorker class, which generates network topology diagrams +by processing input data that includes hosts and switches. It uses the 'diagrams' library +to visualize network components and their interconnections. +""" import os from diagrams import Diagram, Cluster, Edge from diagrams.custom import Custom - -def _processName(name): - """Process Name for Diagram and replace all special chars with _""" - for repl in [[" ", "_"], [":", "_"], ["/", "_"], ["-", "_"], [".", "_"], ["?", "_"]]: - name = name.replace(repl[0], repl[1]) - return name +from RTMonLibs.GeneralLibs import _processName class DiagramWorker: + """ + DiagramWorker class is responsible for generating network topology diagrams + using the input data that contains host and switch information. The class + identifies and visualizes links between network components. + """ HOST_ICON_PATH = '/srv/icons/host.png' SWITCH_ICON_PATH = '/srv/icons/switch.png' def __init__(self, indata): + """ + Initialize the DiagramWorker with input data. + + :param indata: List of dictionaries containing host and switch details. + """ self.indata = indata self.objects = {} self.added = {} @@ -79,6 +90,12 @@ def d_addLinks(self): self.d_addLink(self.objects[key], fItem, key, fKey) def d_addHost(self, item): + """ + Add a host to the network diagram. + + :param item: Dictionary containing host details. + :return: Diagram object representing the host. + """ name = f"Host: {item['Name'].split(':')[1]}" name += f"\nInterface: {item['Interface']}" name += f"\nVlan: {item['Vlan']}" @@ -92,6 +109,12 @@ def d_addHost(self, item): return worker def d_addSwitch(self, item): + """ + Add a switch to the network diagram. + + :param item: Dictionary containing switch details. + :return: Diagram object representing the switch. + """ if item['Node'] in self.added: self.objects[item['Port']] = {"obj": self.objects[self.added[item['Node']]]["obj"], "data": item} return @@ -106,6 +129,12 @@ def d_addSwitch(self, item): return switch1 def addItem(self, item): + """ + Add an item (host or switch) to the diagram by identifying its type and location (cluster). + + :param item: Dictionary containing item details. + :return: Diagram object representing the item. + """ site = self.identifySite(item) if item['Type'] == 'Host': with Cluster(site): @@ -115,6 +144,12 @@ def addItem(self, item): return self.d_addSwitch(item) def identifySite(self, item): + """ + Identify the site or cluster to which the item (host or switch) belongs. + + :param item: Dictionary containing item details. + :return: The name of the site or cluster. + """ site = None if item['Type'] == 'Host': site = item['Name'].split(':')[0] @@ -123,6 +158,11 @@ def identifySite(self, item): return site def setreverse(self, item): + """ + Set the reverse flag for alternating between the first and last items in the input list. + + :param item: Dictionary containing item details. + """ if item['Type'] == 'Host' and self.popreverse == None: self.popreverse = False elif item['Type'] == 'Host' and self.popreverse == False: @@ -131,6 +171,11 @@ def setreverse(self, item): self.popreverse = False def createGraph(self, output_filename): + """ + Create the network topology diagram and save it to a file. + + :param output_filename: Path where the output diagram will be saved. + """ output_dir = os.path.dirname(output_filename) if not os.path.exists(output_dir): os.makedirs(output_dir) diff --git a/autogole-api/src/python/RTMonLibs/GeneralLibs.py b/autogole-api/src/python/RTMonLibs/GeneralLibs.py index b7f400c..6e9d4c9 100644 --- a/autogole-api/src/python/RTMonLibs/GeneralLibs.py +++ b/autogole-api/src/python/RTMonLibs/GeneralLibs.py @@ -9,6 +9,12 @@ from yaml import safe_load as yload from yaml import safe_dump as ydump +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 + def getUUID(inputstr): """Generate UUID from Input Str""" hashObject = hashlib.sha256(inputstr.encode('utf-8')) diff --git a/autogole-api/src/python/RTMonLibs/Template.py b/autogole-api/src/python/RTMonLibs/Template.py index e940865..7585b49 100644 --- a/autogole-api/src/python/RTMonLibs/Template.py +++ b/autogole-api/src/python/RTMonLibs/Template.py @@ -3,15 +3,9 @@ """Grafana Template Generation""" import copy import os.path -from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml, escape, getUUID +from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml, escape, getUUID, _processName from RTMonLibs.DiagramWorker import DiagramWorker -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 - def clamp(n, minn, maxn): """Clamp the value between min and max""" return max(min(maxn, n), minn)