Skip to content

Commit

Permalink
PR fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunami Dasgupta authored and Sunami Dasgupta committed Oct 22, 2024
1 parent e588a79 commit 096b20e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion autogole-api/packaging/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
57 changes: 51 additions & 6 deletions autogole-api/src/python/RTMonLibs/DiagramWorker.py
Original file line number Diff line number Diff line change
@@ -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 = {}
Expand Down Expand Up @@ -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']}"
Expand All @@ -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
Expand All @@ -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):
Expand All @@ -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]
Expand All @@ -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:
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions autogole-api/src/python/RTMonLibs/GeneralLibs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down
8 changes: 1 addition & 7 deletions autogole-api/src/python/RTMonLibs/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 096b20e

Please sign in to comment.