v2.8; see changelog
authorLucas <laimaretto@gmail.com>
Mon, 6 Jul 2020 12:19:19 +0000 (09:19 -0300)
committerLucas <laimaretto@gmail.com>
Mon, 6 Jul 2020 12:19:19 +0000 (09:19 -0300)
python-graph-topology/README.md
python-graph-topology/changelog [new file with mode: 0644]
python-graph-topology/functions.py
python-graph-topology/graph.php [changed mode: 0644->0755]
python-graph-topology/logo.png [changed mode: 0644->0755]
python-graph-topology/not.gif [changed mode: 0644->0755]
python-graph-topology/topoGen.py [moved from python-graph-topology/draw_topo_26_estable.py with 51% similarity]

index 9e50b11f8170666a68096d830dda149f776ee386..6a089ccc7adff3686118957d7aaae85127212de2 100644 (file)
@@ -8,21 +8,24 @@ Follow these steps:
 
 ```bash
 sudo apt install graphviz 
-sudo apt install python-mysqldb
-
-sudo pip install pandas
-sudo pip install graphviz
-sudo pip install networkx
-sudo pip install pydot
-sudo pip install pyyed
-sudo pip install matplotlib
-sudo pip install nuitka
+sudo apt install python3-mysqldb
+sudo apt install libgraphviz-dev 
+
+sudo pip3 install pandas
+sudo pip3 install graphviz
+sudo pip3 install networkx
+sudo pip3 install pydot
+sudo pip3 install pyyed
+sudo pip3 install pygraphviz
+sudo pip3 install matplotlib
+sudo pip3 install nuitka
+sudo pip3 install mysqldb
 ```
 
 #### Compile the python code
 
 ```python
-python -m nuitka draw_topo_26_estable.py --nofollow-import-to=MySQLdb --nofollow-import-to=graphviz --nofollow-import-to=pydot --nofollow-import-to=time --nofollow-import-to=sys --nofollow-import-to=functools --nofollow-import-to=datetime --nofollow-import-to=pandas --nofollow-import-to=networkx --nofollow-import-to=operator --nofollow-import-to=itertools --nofollow-import-to=re --nofollow-import-to=matplotlib --follow-imports
+python3 -m nuitka topoGen.py --nofollow-import-to=MySQLdb --nofollow-import-to=graphviz --nofollow-import-to=pydot --nofollow-import-to=time --nofollow-import-to=sys --nofollow-import-to=functools --nofollow-import-to=datetime --nofollow-import-to=pandas --nofollow-import-to=networkx --nofollow-import-to=operator --nofollow-import-to=itertools --nofollow-import-to=re --nofollow-import-to=matplotlib --follow-imports
 ```
 
 #### Copy Files
@@ -30,7 +33,7 @@ This includes the binary generated in the second step
 
 ```bash
 sudo cp graph.php /var/www/racktables/plugins/
-sudo cp draw_topo_26_estable.bin /var/www/racktables/plugins/
+sudo cp topoGen.bin /var/www/racktables/plugins/
 
 sudo cp logo.png /var/www/racktables/wwwroot/pix/
 sudo cp not.gif /var/www/racktables/wwwroot/pix/
@@ -55,6 +58,6 @@ grant select,lock tables,show view on racktables.* to 'viewRT'@'%';
 
 ## TODO
 
-1. Migrate the python-core to Python3.x
-2. Implement the new RackTables plugin-architecture in the file `graph.php`.
-3. Get rid of `graphviz` and try to manage all the topologies natively through `networkx`. This will make the code a lot cleaner.
\ No newline at end of file
+1. Implement the new RackTables plugin-architecture in the file `graph.php`.
+2. Get rid of `graphviz` and try to manage all the topologies natively through `networkx`. This will make the code a lot cleaner.
+3. Use OpenStreetMap
\ No newline at end of file
diff --git a/python-graph-topology/changelog b/python-graph-topology/changelog
new file mode 100644 (file)
index 0000000..edbff17
--- /dev/null
@@ -0,0 +1,57 @@
+##########################################################
+# Name: draw_topo_02.py
+# Version: 2.7
+# Author: Lucas Aimaretto
+# email: laimaretto@gmail.com
+# Date: 04-jul-2020
+
+- 2.8: migrated to python3
+- 2.7: separation of graph functions into fnc_build_graphviz(), fnc_build_graphml(), fnc_build_graphnx()
+          rename to topoGen.py
+- 2.6: fnc_graphml()
+- 2.5: rewrite of functions, using Pandas to manipiulate data.
+- 2.4: full custimoization via argv[]
+          Different color depending con Ref_Order
+- 2.3: implementing argv to pass parameters in line
+          change \n to &#92;n in port an router when mode 1,2,3
+- 2.2: Option to graph nodes with the names, only.
+- 2.1: Bug regarding getting the system IP of router: fnc_build_query_attributes(.)
+          TxType now considered when MIX TX is needed (ie: DWDM+MW)
+- 2.0: Change Name of file to match Claro's format.
+- 1.9: Ring topology now available
+- 1.8: Includes transmission with a list
+- 1.7: Different colors for MR and HR
+- 1.3: Including FO and ARSAT as possible labels for links
+- 1.2: Reducing font size of link's labels
+- 1.1: Distinction between High, Mid and Low Ran
+- 0.8: Asked whether to graph LAGs or not
+          TBD: consider when link label is something else than "LAG", "Hairpin" or "".
+- 0.7: fnc_build_query_objetos(.) modify to further filter out objects with tags (and no ipvnet)
+          fnc_build_query_interfaces(.) to bring IPs and interfaces
+          fnc_build_query_connections(.) now only brings connections and no IPs
+          fnc_cross_conn_inter(.) finds out IP for connections
+- 0.6: If no SFP, no speed can be obtained. Then "No SFP" is shown. If CES, ATM or ASAP, then "No ETH" is shown.
+- 0.5: Including system IP, sync order, color for not integrated routers.
+- 0.4: Included sync-e reference to each port. If not abailable, then N/A is shown.
+          For this, a sync_dict is created.
+- 0.3: Implementing of IP/PORT/Speed information. Change on function
+          fnc_build_query_connections SQL's query
+- 0.2: Reorder of functions; cleaning of code.
+- 0.1: first draft
+          This version will graph any topology based on the tags of the routers.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
index 7e6916162f62c9d43e3c707ea9b3c7c67b097a31..acfd43ac1a773a9cf94af25546e4539e640c558b 100644 (file)
@@ -7,8 +7,11 @@ from itertools import groupby
 import re
 import datetime
 import networkx as nx
+import graphviz as gv
 import pandas as pd 
 import pyyed
+import sys
+from pathlib import Path
 
 # This function converts M,G,T capacity into Kbps
 # If no unit, return -1
@@ -85,7 +88,6 @@ def fnc_chains_ring(vector):
 
        return tempList
 
-
 # Function that builds the filename
 def fnc_build_filename(vector):
        info = fnc_chains_ring(vector)
@@ -99,9 +101,9 @@ def fnc_build_filename(vector):
                agregador = info[0][1]
                topologia = info[0][2]
 
-               topoName1 = "Topologia "
+               topoName1 = "Topologia"
                topoName2 = tipoTopo
-               topoName3 = " - MR LR - "
+               topoName3 = "-MRLR-"
                topoName4 = topologia
                filename  = "pix/topo/" + agregador + "/" + topoName1 + topoName2 + topoName3 + topoName4
 
@@ -111,15 +113,18 @@ def fnc_build_filename(vector):
                agregador = "-".join(list(set([name[1] for name in info])))
                topologia = "-".join(list(set([name[2] for name in info])))
 
-               topoName1 = "Topologia "
+               topoName1 = "Topologia"
                topoName2 = tipoTopo
-               topoName3 = " - MR LR - "
+               topoName3 = "-MRLR-"
                topoName4 = topologia
                filename  = "pix/topo/" + agregador + "/" + topoName1 + topoName2 + topoName3 + topoName4
 
        #filename=filename + "_" + version_script + "_" +now.strftime("%Y-%m-%d")+".dot"
-       return filename + ".dot"
+       #return filename + ".dot"
+
+       Path("pix/topo/" + agregador).mkdir(parents=True, exist_ok=True)
 
+       return filename
 
 # Function that returns whether we have a input_string or router_name
 def fnc_input_string_type(vector):
@@ -135,8 +140,6 @@ def fnc_input_string_type(vector):
 
        return(listTopo,listRouter)
 
-
-
 # Funcion que organiza los puertos de cada nodo
 def fnc_port_list(routers):
        router_list=[]
@@ -175,7 +178,7 @@ def fnc_port_speed(port_string):
 # Function to compare speeds for egressRate in vPorts
 def fnc_speed_compare(strSpeed):
 
-       print strSpeed
+       #print strSpeed
 
        speed = strSpeed.split(":")[0]
        unit  = speed[-1:]
@@ -625,6 +628,148 @@ def fnc_port_string(router_label, port_string, color, router_mode, router_functi
                return temp_string
 
 
+def fnc_build_graphviz(routers,edges,global_dict,router_mode,filename,input_string,format,engine,rankdir,lines_dict):
+
+       port_dict = {}
+
+       g0 = gv.Graph(format=format, engine=engine)
+
+       topo = '\"'+fnc_build_topo_name(input_string)+'\"'
+
+       g0.body.append('label='  +topo)
+       g0.body.append('rankdir='+rankdir)
+       g0.body.append('splines='+lines_dict)
+
+       g0.node_attr['style']     = 'filled'
+       g0.node_attr['fixedsize'] = 'false'
+       g0.node_attr['fontsize']  = '9'
+
+       if router_mode == '0':
+
+               g0.node_attr['shape'] = 'box'
+               labelType="labelHtml"
+
+       elif router_mode in ['1','2','3']:
+
+               g0.body.append('overlap=false')
+               #g0.body.append('nodesep='+nodesep)
+               #g0.body.append('ranksep='+ranksep)
+
+               g0.node_attr['shape']   = 'Mrecord'
+               g0.node_attr['overlap'] = 'false'
+
+               labelType="labelText"
+
+       i = 1
+
+       for router in routers:
+
+               # Variables
+               router_name     = router[0][0]
+               router_function = get_attribute(router_name,"HWfunction",global_dict)
+               router_int      = get_attribute(router_name,"Integrado",global_dict)
+               router_ckt_id   = get_attribute(router_name,"ckt_id",global_dict)
+               router_color    = get_attribute(router_name,"color",global_dict)['graphviz']
+               router_label    = fnc_router_metadata(global_dict,router_name, labelType, router_function, router_ckt_id, router_mode)
+
+               if router_mode == '0':
+
+                       # Parametrization
+                       cluster_name = "cluster"+str(i)
+                       c = gv.Graph(cluster_name)
+                       c.body.append('label='+router_label)
+                       c.body.append('shape=box')
+                       c.body.append('style=filled')
+
+                       # Color depending on function in network
+                       c.body.append('fillcolor='+router_color)
+                       c.node_attr.update(style='filled')
+
+                       # Ports workout
+                       for port in router:
+                               node_id = port[1]
+                               if ":" in node_id: 
+                                       node_id = node_id.replace(":","#")
+                               port_id = port[2]['label']
+                               c.node(node_id,label=port_id)
+
+                       g0.subgraph(c)
+
+               elif router_mode in ['1','2','3']:
+
+                       # Parametrization
+                       struct_name = "struct"+str(i)
+
+                       # Color depending on function in network
+                       color = 'fillcolor='+router_color
+
+                       # Ports workout
+                       p=1
+                       port_string=""
+                       for port in router:
+
+                               node_id=port[1]
+                               if ":" in node_id: node_id=node_id.replace(":","#")
+                               port_id=port[2]['label']
+
+                               port_string=port_string+"|<f"+str(p)+">"+port_id
+                               dict_key = str(i)+"_"+str(p)
+                               port_dict[node_id]=dict_key
+
+                               p=p+1
+
+                       node_string = fnc_port_string(router_label, port_string, color, router_mode, router_function)
+
+                       g0.body.append(struct_name+node_string)                 
+
+               i=i+1
+
+       for e in edges:
+
+               if router_mode == '0':
+
+                       edgeA = e[0][0]
+                       if ":" in edgeA:
+                               edgeA = edgeA.replace(":","#")
+                       edgeB = e[0][1]
+                       if ":" in edgeB:
+                               edgeB = edgeB.replace(":","#")
+                       edgeLabel = e[1]['label']
+                       edgeSpeed = e[2]
+
+               elif router_mode in ['1','2','3']:
+
+                       tempA = e[0][0]
+                       if ":" in tempA:
+                               tempA=tempA.replace(":","#")
+                       tempA = port_dict[tempA].split("_")
+
+                       if router_mode =="3":
+                               edgeA = "struct"+tempA[0]
+                       else:
+                               edgeA = "struct"+tempA[0]+":f"+tempA[1]
+
+                       tempB = e[0][1]
+                       if ":" in tempB:
+                               tempB=tempB.replace(":","#")
+                       tempB = port_dict[tempB].split("_")
+
+                       if router_mode == "3":
+                               edgeB = "struct"+tempB[0]
+                       else:
+                               edgeB = "struct"+tempB[0]+":f"+tempB[1]
+
+                       edgeLabel=e[1]['label']
+                       edgeSpeed=e[2]
+
+               g0.edge_attr['fontsize']='9'
+               g0.edge(edgeA,edgeB,label=edgeLabel, color=fnc_edge_format(edgeLabel, "color", edgeSpeed), penwidth=fnc_edge_format(edgeLabel,"width",edgeSpeed))
+
+       #print filename + "." + format_dict[output_format]
+       print(filename+".dot")
+       g0.render(filename+".dot")
+       sys.exit(0)
+
 def fnc_build_graphml(routers,edges,global_dict,router_mode,filename):
 
        if router_mode in ['1','2','3','4']:
@@ -655,8 +800,9 @@ def fnc_build_graphml(routers,edges,global_dict,router_mode,filename):
                        edgeLabel = link.cableID
                        G.add_edge(link.routerA,link.routerB, arrowhead='none',arrowfoot='none',label=edgeLabel)
 
-               G.write_graph(filename+'.graphml')                      
-
+               print(filename)
+               G.write_graph(filename+'.graphml')
+               sys.exit(4)
 
        elif router_mode in ['0']:
 
@@ -691,4 +837,74 @@ def fnc_build_graphml(routers,edges,global_dict,router_mode,filename):
 
                        G.add_edge(edgeA,edgeB, arrowhead='none',arrowfoot='none',label=edgeLabel)
 
+               print(filename)
                G.write_graph(filename+'.graphml')
+               sys.exit(4)
+
+def fnc_build_graphnx(df_system,dfConnFinal,global_dict,router_mode,filename,format,engine,):
+
+       import matplotlib.pyplot as plt
+       import pygraphviz
+
+       plt.figure(figsize=(15,15))
+
+       G = nx.Graph()
+
+       for i in df_system.itertuples():
+               router_name   = i[1]
+               id            = i[2]
+               ip_system     = i[3]
+               G.add_node(router_name)
+               nx.set_node_attributes(G,{router_name:{
+                                                                               'router_name':str(router_name),
+                                                                               'ip':str(ip_system),
+                                                                               'name_ip':str(router_name) + "\n" + str(ip_system),
+                                                                               'chassis':global_dict[router_name]['HWtype'],
+                                                                               'isAbr':'-1',
+                                                                               'virtual':'-1',
+                                                                               }
+                                                                       }
+                                                               )
+
+       for i in dfConnFinal.itertuples():
+
+               router_name_A = i[1]
+               port_name_A   = i[2]
+               ip_A          = i[12]
+               ip_system_A   = global_dict[router_name_A]['system'] 
+
+               router_name_B = i[5]
+               port_name_B   = i[4]
+               ip_B          = i[13]
+               ip_system_B   = global_dict[router_name_B]['system']
+
+               cable_id      = i[3]
+
+               data = {
+                               'pA':router_name_A +':'+port_name_A, 
+                               'pB':router_name_B+':'+port_name_B, 
+                               'ipA':ip_A,
+                               'ipB':ip_B,
+                               'cable_id':cable_id,
+                               }
+
+               #print(i)
+
+               G.add_edge(router_name_A,router_name_B)
+               nx.set_edge_attributes(G, {(router_name_A,router_name_B):data})
+
+
+       pos = nx.nx_agraph.graphviz_layout(G, prog=engine)
+
+       nx.draw_networkx_nodes(G, pos, node_size=15)
+       nx.draw_networkx_edges(G, pos,)
+       nx.draw_networkx_labels(G, pos, 
+       labels={node:G.nodes[node]['name_ip'] for node in G.nodes()},
+       font_size=8,
+       )
+       
+
+       print(filename+"."+format)
+       plt.savefig(filename+"."+format, format=format, dpi=500, bbox_inches='tight')
+       #plt.savefig(filename+"."+format, format=format, dpi=500)
+       sys.exit(5)
\ No newline at end of file
old mode 100644 (file)
new mode 100755 (executable)
index 23e8ee1..e954cbe
@@ -49,7 +49,7 @@
                //variables intialization:
                $topo = $router_mode = $format = "";
                $algo = $rankdir = $lines = $aggr = $lag = "";
-               $nodesep = $ranksep = "";
+               //$nodesep = $ranksep = "";
                $svgName = "";
                $exitCode= -2;
                $scriptOutput = array();
                        $lines                  = $_POST["lines"];
                        $aggr                   = $_POST["aggr"];
                        $lag                    = $_POST["lag"];
-                       $nodesep                = $_POST["nodesep"];
-                       $ranksep                = $_POST["ranksep"];
+                       //$nodesep              = $_POST["nodesep"];
+                       //$ranksep              = $_POST["ranksep"];
                }
                ?>
-               <!-- Page help -->
-               <!--
-               <div class="help">
-                       <div id="help-button">
-                               <h2 id='help-msg'>Help</h2>
-                       </div>
-                       <div id="help-pane">
-                               <img id="help-img" src="pix/legend_topo.png">
-                               <!-- <div id="help-img"></div> -->
-                       </div>
-               </div>
-               -->
 
                <div class="settings">
                                        <!-- HTML menu formatting -->
                                                        <div class="gdiv">
                                                        <label class="ginput" for="router_mode">Router mode:</label>
                                                        <select class="ginput" name="router_mode">
-                                                               <option <?php if ($router_mode == "0") {?>selected="true" <?php }; ?>value="0">Router as cluster</option>
-                                                               <option <?php if ($router_mode == "1" || $router_mode == "") {?>selected="true" <?php }; ?>value="1">Router as node, one-line</option>
-                                                               <option <?php if ($router_mode == "2") {?>selected="true" <?php }; ?>value="2">Router as node, two-line</option>
-                                                               <option <?php if ($router_mode == "3") {?>selected="true" <?php }; ?>value="3">Router as node, only with name</option>
+                                                               <option <?php if ($router_mode == "0") {?>selected="true" <?php }; ?>value="0">gvCluster</option>
+                                                               <option <?php if ($router_mode == "1" || $router_mode == "") {?>selected="true" <?php }; ?>value="1">gvOne-line</option>
+                                                               <option <?php if ($router_mode == "2") {?>selected="true" <?php }; ?>value="2">gvTwo-line</option>
+                                                               <option <?php if ($router_mode == "3") {?>selected="true" <?php }; ?>value="3">gvOnly-name</option>
+                                                               <option <?php if ($router_mode == "4") {?>selected="true" <?php }; ?>value="4">yedOnly-name</option>
+                                                               <option <?php if ($router_mode == "5") {?>selected="true" <?php }; ?>value="5">nxOnly-name</option>
                                                        </select>
                                                        </div>
 
                                                        <div class="gdiv">
-                                                       <label class="ginput" for="format">Format:</label>
-                                                       <select class="ginput" name="format">
-                                                               <option <?php if ($format == "0") {?>selected="true" <?php }; ?>value="0">PNG</option>
-                                                               <option <?php if ($format == "1" || $format == "") {?>selected="true" <?php }; ?>value="1">SVG</option>
+                                                       <label class="ginput" for="algo">Algorithm:</label>
+                                                       <select class="ginput" name="algo">
+                                                               <option <?php if ($algo == "0") {?>selected="true" <?php }; ?>value="0">gvDOT</option>
+                                                               <option <?php if ($algo == "1") {?>selected="true" <?php }; ?>value="1">gvFDP</option>
+                                                               <option <?php if ($algo == "2") {?>selected="true" <?php }; ?>value="2">gvCIRCO</option>
+                                                               <option <?php if ($algo == "3" || $algo == "") {?>selected="true" <?php }; ?>value="3">gvTWOPI</option>
+                                                               <option <?php if ($algo == "4") {?>selected="true" <?php }; ?>value="4">gvNEATO</option>
                                                        </select>
                                                        </div>
 
                                                        <div class="gdiv">
-                                                       <label class="ginput" for="algo">Algorithm:</label>
-                                                       <select class="ginput" name="algo">
-                                                               <option <?php if ($algo == "0") {?>selected="true" <?php }; ?>value="0">DOT</option>
-                                                               <option <?php if ($algo == "1") {?>selected="true" <?php }; ?>value="1">FDP</option>
-                                                               <option <?php if ($algo == "2") {?>selected="true" <?php }; ?>value="2">CIRCO</option>
-                                                               <option <?php if ($algo == "3" || $algo == "") {?>selected="true" <?php }; ?>value="3">TWOPI</option>
-                                                               <option <?php if ($algo == "4") {?>selected="true" <?php }; ?>value="4">NEATO</option>
+                                                       <label class="ginput" for="format">Format:</label>
+                                                       <select class="ginput" name="format">
+                                                               <option <?php if ($format == "0") {?>selected="true" <?php }; ?>value="0">PNG</option>
+                                                               <option <?php if ($format == "1" || $format == "") {?>selected="true" <?php }; ?>value="1">SVG</option>
                                                        </select>
                                                        </div>
 
                                                        </select>
                                                        </div>
 
-                                                       <div class="gdiv">
-                                                       <label class="ginput" for="nodesep">NodeSep:</label>
-                                                       <input type = "number" max = "10.0" min = "0.0" value = <?php if ($nodesep != "") { echo $nodesep; } else { ?>"1.0"<?php }; ?> step = "0.2" name = "nodesep" />
-                                                       </div>
-
-                                                       <div class="gdiv">
-                                                       <label class="ginput" for="ranksep">RankSep:</label>
-                                                       <input type = "number" max = "10.0" min = "0.0" value = <?php if ($ranksep != "") { echo $ranksep; } else { ?>"1.0"<?php }; ?> step = "0.2" name = "ranksep" />
-                                                       </div>
-
                                                </fieldset>
                                                </form>
                                        </div>
                                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                                        if (empty($topo)) //Python script needs this parameter in order to work
                                        {
-                                               echo "<p> Please specify the desired topology.</p>";
+                                               {echo "<h3 class=\"warn\">Please, specify the desired topology.</h3>";}
+
                                        } else
                                        {
                                                $topo = preg_replace('/\s+/', '', $topo);
-                                               exec("plugins/draw_topo_26_estable.bin ".$topo." ".$router_mode." ".$format." ".$algo." ".$rankdir." ".$lines." ".$aggr." ".$lag." ".$nodesep." ".$ranksep , $scriptOutput, $exitCode); //PHP waits until the called program is done
+                                               //exec("plugins/topoGen.bin ".$topo." ".$router_mode." ".$format." ".$algo." ".$rankdir." ".$lines." ".$aggr." ".$lag." ".$nodesep." ".$ranksep , $scriptOutput, $exitCode); //PHP waits until the called program is done
+                                               exec("plugins/topoGen.bin ".$topo." ".$router_mode." ".$format." ".$algo." ".$rankdir." ".$lines." ".$aggr." ".$lag, $scriptOutput, $exitCode); //PHP waits until the called program is done
+
                                                switch($exitCode)
                                                {
                                                        case 255:
-                                                               echo "<h3 class=\"not\">Topology not found!</h3>";
-                                                               echo "<img class=\"not\" src=\"pix/not.gif\">";
+                                                               {echo "<h3 class=\"not\">Topology not found!</h3>";}
+                                                               {echo "<img class=\"not\" src=\"pix/not.gif\">";}
                                                                break;
-                                                       case 0:
-                                                               
+
+                                                       case 4:
                                                                {echo "<a href=\"" . $scriptOutput[count($scriptOutput)-1] . ".graphml" . "\" download>Download GRAPHML</a>";}
+                                                               break;
 
+                                                       case 5:
+
+                                                               {echo "<img class=\"topo\" src=\"" . $scriptOutput[count($scriptOutput)-1] . "\">";}
+                                                               break;
+
+                                                       case 0:
+                                                               
                                                                if ($format == "0")
                                                                {echo "<img class=\"topo\" src=\"" . $scriptOutput[count($scriptOutput)-1] . ".png\">";}
                                                                if ($format == "1")
old mode 100644 (file)
new mode 100755 (executable)
old mode 100644 (file)
new mode 100755 (executable)
similarity index 51%
rename from python-graph-topology/draw_topo_26_estable.py
rename to python-graph-topology/topoGen.py
index 9687de5af8431ebe370423d984365e71a2dc8b22..7d5d29bd05958598e56ee15c07cc17b20a181db6 100755 (executable)
@@ -1,41 +1,6 @@
 # coding=utf-8\r
-##########################################################\r
-# Name: draw_topo_02.py\r
-# Version: 0.2\r
-# Author: Lucas Aimaretto\r
-# Date: 14-jun-2015\r
-#\r
-# - 0.1:       first draft\r
-#                      This version will graph any topology based on the tags of the routers.\r
-# - 0.2:       Reorder of functions; cleaning of code.\r
-# - 0.3:       Implementing of IP/PORT/Speed information. Change on function\r
-#                      fnc_build_query_connections SQL's query\r
-# - 0.4:       Included sync-e reference to each port. If not abailable, then N/A is shown.\r
-#                      For this, a sync_dict is created.\r
-# - 0.5:       Including system IP, sync order, color for not integrated routers.\r
-# - 0.6:       If no SFP, no speed can be obtained. Then "No SFP" is shown. If CES, ATM or ASAP, then "No ETH" is shown.\r
-# - 0.7:       fnc_build_query_objetos(.) modify to further filter out objects with tags (and no ipvnet)\r
-#                      fnc_build_query_interfaces(.) to bring IPs and interfaces\r
-#                      fnc_build_query_connections(.) now only brings connections and no IPs\r
-#                      fnc_cross_conn_inter(.) finds out IP for connections\r
-# - 0.8:       Asked whether to graph LAGs or not\r
-#                      TBD: consider when link label is something else than "LAG", "Hairpin" or "".\r
-# - 1.1:       Distinction between High, Mid and Low Ran\r
-# - 1.2:       Reducing font size of link's labels\r
-# - 1.3:       Including FO and ARSAT as possible labels for links\r
-# - 1.7:       Different colors for MR and HR\r
-# - 1.8:       Includes transmission with a list\r
-# - 1.9:       Ring topology now available\r
-# - 2.0:       Change Name of file to match Claro's format.\r
-# - 2.1:       Bug regarding getting the system IP of router: fnc_build_query_attributes(.)\r
-#                      TxType now considered when MIX TX is needed (ie: DWDM+MW)\r
-# - 2.2:       Option to graph nodes with the names, only.\r
-# - 2.3:       implementing argv to pass parameters in line\r
-#                      change \n to &#92;n in port an router when mode 1,2,3\r
-# - 2.4:       full custimoization via argv[]\r
-#                      Different color depending con Ref_Order\r
-\r
 #!/usr/bin/python\r
+\r
 import MySQLdb\r
 import graphviz as gv\r
 import pydot\r
@@ -84,14 +49,10 @@ posDirect   = 5
 posLine                = 6\r
 posAggreg      = 7\r
 posLag         = 8\r
-posNodeSep  = 9\r
-posRankSep  = 10\r
-#posHairPin    = 9\r
-#posCPAM       = 10\r
 \r
 # Check for inline parameters\r
 if len(sys.argv)<9:\r
-       print "Not enough paramteres. Quitting..."\r
+       print("Not enough paramteres. Quitting...")\r
        sys.exit(-1)\r
 \r
 # Getting topo and routers name\r
@@ -197,14 +158,13 @@ global_dict = fnc_add_atributes_to_dic_global(df_attr_list)
 #==================================================================\r
 \r
 # Format Dictionaries\r
-mode_dict              = {"0":"cluster",       "1":"one-line", "2":"two-lines", "3":"only-names", "4":"networkx"}\r
+mode_dict              = {"0":"gvCluster", "1":"gvOne-line", "2":"gvTwo-lines", "3":"gvOnly-names", "4":"yed", "5":"nx"}\r
 format_dict    = {"0":"png",           "1":"svg"}\r
-algo_dict              = {"0":"dot",           "1":"fdp",              "2":"circo",    "3":"twopi",    "4":"neato"}\r
+algo_dict              = {"0":"dot",       "1":"fdp",      "2":"circo",        "3":"twopi",    "4":"neato"}\r
 rankdir_dict   = {"0":"LR",            "1":"TB"}\r
 lines_dict             = {"0":"line",          "1":"true",             "2":"ortho",    "3":"polyline"}\r
 aggr_dict              = {"0":"n",                     "1":"y"}\r
 lag_dict               = {"0":"n",                     "1":"y"}\r
-port_dict              = {}\r
 \r
 router_mode            = sys.argv[posMode]\r
 output_format          = sys.argv[posFormat]\r
@@ -213,19 +173,15 @@ output_direction  = sys.argv[posDirect]
 output_line                    = sys.argv[posLine]\r
 aggregator                     = sys.argv[posAggreg]\r
 graph_lag                      = lag_dict[sys.argv[posLag]]\r
-nodesep                                = sys.argv[posNodeSep]\r
-ranksep                                = sys.argv[posRankSep]\r
 graph_hp                       = "n"\r
 graph_cpam                     = "n"\r
 \r
-#==================================================================\r
-#==================================================================\r
-# At this instance of the run, the list object_connections[] only has\r
-# connections to routers that do have the requested tag.\r
-# The function fnc_edge_list(.) reorders that information\r
-# so it will be easier to feed graphviz.\r
-edges   = fnc_edge_list(dfConnFinal, graph_lag, graph_hp, graph_cpam, router_mode)\r
-routers = fnc_node_list(dfConnFinal, global_dict, router_mode, graph_hp, graph_lag, graph_cpam)\r
+\r
+## Obtain the filename for the topology\r
+if len(topos_names) > 0:\r
+       filename=fnc_build_filename(topos_names)\r
+elif len(routers_names) > 0:\r
+       filename=fnc_build_filename("ROUTERS")\r
 \r
 #===================================================================\r
 #===================================================================\r
@@ -233,152 +189,16 @@ routers = fnc_node_list(dfConnFinal, global_dict, router_mode, graph_hp, graph_l
 # Gegin the plot\r
 if router_mode  in ['0','1','2','3']:\r
 \r
-       g0 = gv.Graph(format=format_dict[output_format], engine=algo_dict[output_algo])\r
-\r
-       topo = '\"'+fnc_build_topo_name(input_string)+'\"'\r
-\r
-       g0.body.append('label='  +topo)\r
-       g0.body.append('rankdir='+rankdir_dict[output_direction])\r
-       g0.body.append('splines='+lines_dict[output_line])\r
-\r
-       g0.node_attr['style']     = 'filled'\r
-       g0.node_attr['fixedsize'] = 'false'\r
-       g0.node_attr['fontsize']  = '9'\r
-\r
-       if router_mode == '0':\r
-\r
-               g0.node_attr['shape'] = 'box'\r
-               labelType="labelHtml"\r
-\r
-       elif router_mode in ['1','2','3']:\r
-\r
-               g0.body.append('overlap=false')\r
-               g0.body.append('nodesep='+nodesep)\r
-               g0.body.append('ranksep='+ranksep)\r
-\r
-               g0.node_attr['shape']   = 'Mrecord'\r
-               g0.node_attr['overlap'] = 'false'\r
-\r
-               labelType="labelText"\r
-\r
-       i = 1\r
-\r
-       for router in routers:\r
-\r
-               # Variables\r
-               router_name     = router[0][0]\r
-               router_function = get_attribute(router_name,"HWfunction",global_dict)\r
-               router_int      = get_attribute(router_name,"Integrado",global_dict)\r
-               router_ckt_id   = get_attribute(router_name,"ckt_id",global_dict)\r
-               router_color    = get_attribute(router_name,"color",global_dict)['graphviz']\r
-               router_label    = fnc_router_metadata(global_dict,router_name, labelType, router_function, router_ckt_id, router_mode)\r
-\r
-               if router_mode == '0':\r
-\r
-                       # Parametrization\r
-                       cluster_name = "cluster"+str(i)\r
-                       c = gv.Graph(cluster_name)\r
-                       c.body.append('label='+router_label)\r
-                       c.body.append('shape=box')\r
-                       c.body.append('style=filled')\r
-\r
-                       # Color depending on function in network\r
-                       c.body.append('fillcolor='+router_color)\r
-                       c.node_attr.update(style='filled')\r
-\r
-                       # Ports workout\r
-                       for port in router:\r
-                               node_id = port[1]\r
-                               if ":" in node_id: \r
-                                       node_id = node_id.replace(":","#")\r
-                               port_id = port[2]['label']\r
-                               c.node(node_id,label=port_id)\r
-\r
-                       g0.subgraph(c)\r
+       edges   = fnc_edge_list(dfConnFinal, graph_lag, graph_hp, graph_cpam, router_mode)\r
+       routers = fnc_node_list(dfConnFinal, global_dict, router_mode, graph_hp, graph_lag, graph_cpam)\r
+       fnc_build_graphviz(routers,edges,global_dict,router_mode,filename,input_string,format_dict[output_format],algo_dict[output_algo],rankdir_dict[output_direction],lines_dict[output_line])\r
 \r
-               elif router_mode in ['1','2','3']:\r
+elif router_mode in ['4']:\r
 \r
-                       # Parametrization\r
-                       struct_name = "struct"+str(i)\r
-\r
-                       # Color depending on function in network\r
-                       color = 'fillcolor='+router_color\r
-\r
-                       # Ports workout\r
-                       p=1\r
-                       port_string=""\r
-                       for port in router:\r
-\r
-                               node_id=port[1]\r
-                               if ":" in node_id: node_id=node_id.replace(":","#")\r
-                               port_id=port[2]['label']\r
-\r
-                               port_string=port_string+"|<f"+str(p)+">"+port_id\r
-                               dict_key = str(i)+"_"+str(p)\r
-                               port_dict[node_id]=dict_key\r
-\r
-                               p=p+1\r
-\r
-                       node_string = fnc_port_string(router_label, port_string, color, router_mode, router_function)\r
-\r
-                       g0.body.append(struct_name+node_string)                 \r
-\r
-               i=i+1\r
-\r
-       for e in edges:\r
-\r
-               if router_mode == '0':\r
-\r
-                       edgeA = e[0][0]\r
-                       if ":" in edgeA:\r
-                               edgeA = edgeA.replace(":","#")\r
-                       edgeB = e[0][1]\r
-                       if ":" in edgeB:\r
-                               edgeB = edgeB.replace(":","#")\r
-                       edgeLabel = e[1]['label']\r
-                       edgeSpeed = e[2]\r
-\r
-               elif router_mode in ['1','2','3']:\r
-\r
-                       tempA = e[0][0]\r
-                       if ":" in tempA:\r
-                               tempA=tempA.replace(":","#")\r
-                       tempA = port_dict[tempA].split("_")\r
-\r
-                       if router_mode =="3":\r
-                               edgeA = "struct"+tempA[0]\r
-                       else:\r
-                               edgeA = "struct"+tempA[0]+":f"+tempA[1]\r
-\r
-                       tempB = e[0][1]\r
-                       if ":" in tempB:\r
-                               tempB=tempB.replace(":","#")\r
-                       tempB = port_dict[tempB].split("_")\r
-\r
-                       if router_mode == "3":\r
-                               edgeB = "struct"+tempB[0]\r
-                       else:\r
-                               edgeB = "struct"+tempB[0]+":f"+tempB[1]\r
-\r
-                       edgeLabel=e[1]['label']\r
-                       edgeSpeed=e[2]\r
-\r
-               g0.edge_attr['fontsize']='9'\r
-               g0.edge(edgeA,edgeB,label=edgeLabel, color=fnc_edge_format(edgeLabel, "color", edgeSpeed), penwidth=fnc_edge_format(edgeLabel,"width",edgeSpeed))\r
-\r
-\r
-       if len(topos_names) > 0:\r
-               filename=fnc_build_filename(topos_names)\r
-       elif len(routers_names) > 0:\r
-               filename=fnc_build_filename("ROUTERS")\r
-\r
-       #print filename + "." + format_dict[output_format]\r
-       print filename\r
-       g0.render(filename)\r
+       edges   = fnc_edge_list(dfConnFinal, graph_lag, graph_hp, graph_cpam, router_mode)\r
+       routers = fnc_node_list(dfConnFinal, global_dict, router_mode, graph_hp, graph_lag, graph_cpam)\r
+       fnc_build_graphml(routers,edges,global_dict,router_mode,filename)\r
 \r
-       # We now build the graphml\r
-       if router_mode in ['1','2','3']:\r
-               edges   = fnc_edge_list(dfConnFinal, graph_lag, graph_hp, graph_cpam, '4')\r
-               routers = fnc_node_list(dfConnFinal, global_dict, '4', graph_hp, graph_lag, graph_cpam)\r
+elif router_mode in ['5']:\r
 \r
-       fnc_build_graphml(routers,edges,global_dict,router_mode,filename)\r
+       fnc_build_graphnx(df_system,dfConnFinal,global_dict,router_mode,filename,format_dict[output_format],algo_dict[output_algo])
\ No newline at end of file