Skip to content

Commit

Permalink
Issue
Browse files Browse the repository at this point in the history
  • Loading branch information
sunami09 committed Dec 5, 2024
1 parent f4898ac commit 84d175e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
4 changes: 2 additions & 2 deletions autogole-api/src/python/RTMon/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from RTMonLibs.SiteOverride import SiteOverride
from RTMonLibs.SiteRMApi import SiteRMApi
from RTMonLibs.ExternalAPI import ExternalAPI
from RTMonLibs.DiagramWorker import DiagramWorker


class RTMonWorker(SenseAPI, GrafanaAPI, Template, SiteOverride, SiteRMApi, ExternalAPI, Mermaid):
class RTMonWorker(SenseAPI, GrafanaAPI, Template, SiteOverride, SiteRMApi, ExternalAPI, Mermaid, DiagramWorker):
""" RTMon Worker """
def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand Down
25 changes: 15 additions & 10 deletions autogole-api/src/python/RTMonLibs/DiagramWorker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
by processing input data that includes hosts and switches. It uses the 'diagrams' library
to visualize network components and their interconnections.
"""
import os
import os, json
from diagrams import Diagram, Cluster, Edge
from diagrams.custom import Custom
from RTMonLibs.SiteOverride import SiteOverride
from RTMonLibs.GeneralLibs import _processName
# # change later:
# from SiteOverride import SiteOverride
# 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:
class DiagramWorker():
"""
DiagramWorker class is responsible for generating network topology diagrams
using the input data that contains host and switch information. The class
Expand All @@ -29,20 +31,20 @@ class DiagramWorker:
# 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):
def __init__(self, **kwargs):
"""
Initialize the DiagramWorker with input data.
:param indata: List of dictionaries containing host and switch details.
"""
self.indata = indata
super().__init__(**kwargs)
self.objects = {}
self.added = {}
self.linksadded = set()
self.popreverse = None
self.instance = instance

self.instance = kwargs.get('instance', {})

#peer port
def d_find_item(self, fval, fkey):
"""Find Item where fkey == fval"""
for key, vals in self.objects.items():
Expand Down Expand Up @@ -95,6 +97,7 @@ def d_addLinks(self):
fKey, fItem = self.d_find_item(vals['data']['Peer'], "Port")
if fKey and fItem:
self.d_addLink(vals, fItem, key, fKey)
#Here Logic Needed for peer :: How is esnet getting?
elif 'PeerHost' in vals.get('data', {}):
fKey = vals['data']['PeerHost']
fItem = self.objects.get(fKey)
Expand Down Expand Up @@ -207,7 +210,7 @@ def setreverse(self, item):
elif item['Type'] == 'Host' and self.popreverse is True:
self.popreverse = False

def createGraph(self, output_filename):
def createDiagramGraph(self, output_filename, indata):
"""
Create the network topology diagram and save it to a file.
Expand All @@ -218,11 +221,13 @@ def createGraph(self, output_filename):
os.makedirs(output_dir)

with Diagram("Network Topology", show=False, filename=output_filename):
while len(self.indata) > 0:
with open("indata" + output_filename.split('/')[-1]+".json", 'w') as file:
json.dump(indata, file, indent=2)
while len(indata) > 0:
if self.popreverse in (None, False):
item = self.indata.pop(0)
item = indata.pop(0)
elif self.popreverse == True:
item = self.indata.pop()
item = indata.pop()
self.addItem(item)
self.setreverse(item)
self.d_addLinks()
1 change: 1 addition & 0 deletions autogole-api/src/python/RTMonLibs/SiteOverride.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Class for overriding site specific settings (e.g. OpenNSA/NSI/NRM Name, ports)
"""
from RTMonLibs.GeneralLibs import loadYaml, getWebContentFromURL
# from GeneralLibs import loadYaml, getWebContentFromURL

class SiteOverride:
"""Site Override"""
Expand Down
20 changes: 13 additions & 7 deletions autogole-api/src/python/RTMonLibs/Template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import os.path
from RTMonLibs.GeneralLibs import loadJson, dumpJson, dumpYaml, escape, getUUID, _processName
from RTMonLibs.DiagramWorker import DiagramWorker
#import json

#change later
import json
import sys
def clamp(n, minn, maxn):
"""Clamp the value between min and max"""
return max(min(maxn, n), minn)
Expand Down Expand Up @@ -630,6 +633,9 @@ def t_addL2Debugging(self, *args):

def t_createTemplate(self, *args, **kwargs):
"""Create Grafana Template"""
print("This is kwargs:\n")
print(kwargs)
# sys.exit()
self._clean()
self._t_getDataSourceUid(*args)
self.generated = self.t_createDashboard(*args, **kwargs)
Expand All @@ -639,12 +645,12 @@ 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)
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)
self.createDiagramGraph(diagram_filename, self.orderlist)
self.logger.info(f"Diagram saved at {diagram_filename}.png")
except IOError as ex:
self.logger.error('Failed to create diagram: %s', ex)
Expand Down
30 changes: 30 additions & 0 deletions autogole-api/src/python/RTMonLibs/TestDiagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import time
from DiagramWorker import *

import glob


import json

# File paths
original_args_file = "/Users/sunami/Desktop/publish/sense-rtmon/autogole-api/original_argsdiagram_555da2e2-0c40-4e6d-b819-262515a05571.json"
self_order_file = "/Users/sunami/Desktop/publish/sense-rtmon/autogole-api/selfOrderdiagram_555da2e2-0c40-4e6d-b819-262515a05571.json"
kwargs = {'state': 'submitted', 'referenceUUID': '0a303274-a293-4af8-88b7-5b010d23a0f7', 'orchestrator': 'sense-o-dev.es.net', 'submission': 'AUTH_KEY', 'instance': {'intents': [{'id': '5d86903e-6b09-4c61-8cca-4d7e36c9aa1a', 'json': {'service_instance_uuid': '0a303274-a293-4af8-88b7-5b010d23a0f7', 'data': {'type': 'Multi-Path P2P VLAN', 'connections': [{'bandwidth': {'qos_class': 'bestEffort'}, 'name': 'Connection 1', 'ip_address_pool': {'netmask': '/30', 'name': 'AutoGOLE-IPv4-Test-Pool'}, 'terminals': [{'vlan_tag': 'any', 'assign_ip': True, 'uri': 'urn:ogf:network:ultralight.org:2013:sandie-7.ultralight.org'}, {'vlan_tag': 'any', 'assign_ip': True, 'uri': 'urn:ogf:network:nrp-nautilus.io:2020:k8s-gen5-01.sdsc.optiputer.net'}]}]}, 'service': 'dnc', 'options': [], 'service_profile_uuid': '49d722dc-2f8f-49f9-a7ab-92c2f26822ed', 'queries': []}, 'provisioned': True, 'serviceInstanceUUID': '0a303274-a293-4af8-88b7-5b010d23a0f7', 'serviceDeltaUUID': 'e7787217-f1e5-41e1-941d-0a63ad66ba40', 'creation_time': '2024-11-04 06:49:00'}], 'alias': 'RTMON-IT6-Caltech-SDSC', 'referenceUUID': '0a303274-a293-4af8-88b7-5b010d23a0f7', 'profileUUID': '49d722dc-2f8f-49f9-a7ab-92c2f26822ed', 'state': 'REINSTATE - READY', 'owner': '[email protected]', 'lastState': 'COMMITTED', 'timestamp': '2024/10/22 16:17:47', 'archived': False}, 'manifest': {'Ports': [{'Site': 'urn:ogf:network:nrp-nautilus.io:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:nrp-nautilus.io:2020:edgecore_s0:Ethernet32', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_SDSC:edgecore_s0', 'Peer': '?peer?', 'Host': [{'IPv6': '?ipv6?', 'IPv4': '10.251.86.138/30', 'Interface': 'enp168s0np0', 'Mac': 'a0:88:c2:86:ee:7c', 'Name': 'T2_US_SDSC:k8s-gen5-01.sdsc.optiputer.net'}], 'Vlan': '3607', 'Mac': '00:90:fb:76:e4:7b', 'Name': 'Ethernet32'}, {'Site': 'urn:ogf:network:nrp-nautilus.io:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:nrp-nautilus.io:2020:edgecore_s0:PortChannel500', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_SDSC:edgecore_s0', 'Peer': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020:oasis:Pc500', 'Vlan': '3607', 'Mac': '00:90:fb:76:e4:7b', 'Name': 'PortChannel500'}, {'Site': 'urn:ogf:network:ultralight.org:2013', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:ultralight.org:2013:dellos9_s0:Port-channel_103', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_Caltech_Test:dellos9_s0', 'Peer': 'urn:ogf:network:tier2.ultralight.org:2024:dellos10_s0:Port-channel_101', 'Vlan': '3607', 'Mac': '4c:76:25:e8:44:c2', 'Name': 'Port-channel 103'}, {'Site': 'urn:ogf:network:ultralight.org:2013', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:ultralight.org:2013:dellos9_s0:hundredGigE_1-32', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_Caltech_Test:dellos9_s0', 'Peer': '?peer?', 'Host': [{'IPv6': '?ipv6?', 'IPv4': '10.251.86.137/30', 'Interface': 'mlx5p1s1', 'Mac': 'ec:0d:9a:c1:ba:60', 'Name': 'T2_US_Caltech_Test:sandie-7.ultralight.org'}], 'Vlan': '3607', 'Mac': '4c:76:25:e8:44:c2', 'Name': 'hundredGigE 1/32'}, {'Site': 'urn:ogf:network:sc-test.cenic.net:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:sc-test.cenic.net:2020:aristaeos_s0:Port-Channel502', 'IPv4': '?port_ipv4?', 'Node': 'NRM_CENIC:aristaeos_s0', 'Peer': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020:oasis:Pc502', 'Vlan': '3607', 'Mac': '28:e7:1d:3f:53:70', 'Name': 'Port-Channel502'}, {'Site': 'urn:ogf:network:sc-test.cenic.net:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:sc-test.cenic.net:2020:aristaeos_s0:Port-Channel501', 'IPv4': '?port_ipv4?', 'Node': 'NRM_CENIC:aristaeos_s0', 'Peer': 'urn:ogf:network:tier2.ultralight.org:2024:dellos10_s0:Port-channel_102', 'Vlan': '3607', 'Mac': '28:e7:1d:3f:53:88', 'Name': 'Port-Channel501'}, {'Site': 'urn:ogf:network:tier2.ultralight.org:2024', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:tier2.ultralight.org:2024:dellos10_s0:Port-channel_101', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_Caltech:dellos10_s0', 'Peer': 'urn:ogf:network:ultralight.org:2013:dellos9_s0:Port-channel_103', 'Vlan': '3607', 'Mac': '8c:04:ba:e9:0e:a8', 'Name': 'Port-channel 101'}, {'Site': 'urn:ogf:network:tier2.ultralight.org:2024', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:tier2.ultralight.org:2024:dellos10_s0:Port-channel_102', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_Caltech:dellos10_s0', 'Peer': 'urn:ogf:network:sc-test.cenic.net:2020:aristaeos_s0:Port-Channel501', 'Vlan': '3607', 'Mac': '8c:04:ba:e9:0e:a9', 'Name': 'Port-channel 102'}, {'Site': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020:oasis:Pc500', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_UCSD_OASIS:oasis', 'Peer': 'urn:ogf:network:nrp-nautilus.io:2020:edgecore_s0:PortChannel500', 'Vlan': '3607', 'Mac': '?port_mac?', 'Name': 'Pc500'}, {'Site': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020', 'IPv6': '?port_ipv6?', 'Port': 'urn:ogf:network:sense-oasis-nrp-nautilus.io:2020:oasis:Pc502', 'IPv4': '?port_ipv4?', 'Node': 'T2_US_UCSD_OASIS:oasis', 'Peer': 'urn:ogf:network:sc-test.cenic.net:2020:aristaeos_s0:Port-Channel502', 'Vlan': '3607', 'Mac': '?port_mac?', 'Name': 'Pc502'}]}}

# Load the JSON data
try:
with open(original_args_file, 'r') as orig_file:
original_args = json.load(orig_file)
print("Loaded original_args:")
print(json.dumps(original_args, indent=2)) # Pretty print for readability

with open(self_order_file, 'r') as self_file:
self_order = json.load(self_file)
print("Loaded self_order:")
print(json.dumps(self_order, indent=2)) # Pretty print for readability

DiagramWorker(self_order, **kwargs).createGraph("/Users/sunami/Desktop/publish/sense-rtmon/autogole-api/src/python/RTMonLibs/myimage")

except Exception as e:
print(f"Error loading or processing files: {e}")

0 comments on commit 84d175e

Please sign in to comment.