Commit 2ad35901 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Hackfest

- updated tapi_app to plot topologies
parent c016f5e2
Loading
Loading
Loading
Loading
−28 KiB
Loading image diff...
+16 −0
Original line number Diff line number Diff line
strict digraph  {
"node-1";
"node-2";
"node-3";
"node-4";
"node-1" -> "node-3";
"node-1" -> "node-4";
"node-2" -> "node-3";
"node-2" -> "node-4";
"node-3" -> "node-1";
"node-3" -> "node-2";
"node-3" -> "node-4";
"node-4" -> "node-1";
"node-4" -> "node-2";
"node-4" -> "node-3";
}
+26.7 KiB
Loading image diff...
+4 −0
Original line number Diff line number Diff line
#!/bin/bash

sudo apt install graphviz
pip install -r requirements.txt
+26 −18
Original line number Diff line number Diff line
@@ -4,11 +4,8 @@
import requests
from requests.auth import HTTPBasicAuth
import json
import matplotlib.pyplot as plt
import networkx as nx

from networkx.drawing.nx_pydot import write_dot

IP = '127.0.0.1'
PORT = 8080
USER = ''
@@ -26,21 +23,32 @@ def retrieveTopology(ip, port, user, password, topo_uuid):
    print ("Retrieved Topology: " + json.dumps(response.json(), indent=4))
    return topologies

def draw_topologies (topo) :
    nwk_graph = nx.Graph()
def draw_topologies (topologies):
    nwk_graph = nx.DiGraph()

    for node in topo[0]['node']:
    for topo in topologies:
        topo_uuid = topo['uuid']
        for node in topo['node']:
            if node['owned-node-edge-point']:
                nwk_graph.add_node(node['uuid'])

    for link in topo[0]['link']:
        for link in topo['link']:
            node1 = link['node-edge-point'][0]['node-uuid']
            node2 = link['node-edge-point'][1]['node-uuid']
            nwk_graph.add_edge(node1, node2)

    nx.draw(nwk_graph, pos=nx.spring_layout(nwk_graph, scale=500))
    plt.show(block=False)
    plt.savefig('{:s}.png'.format(TOPO_UUID), format='PNG')
        # Using Matplotlib
        #import matplotlib.pyplot as plt
        #nx.draw(nwk_graph, pos=nx.spring_layout(nwk_graph, scale=500))
        #plt.show(block=False)
        #plt.savefig('{:s}.png'.format(TOPO_UUID), format='PNG')

        # Using Pydot and Graphviz
        from networkx.drawing.nx_pydot import write_dot, to_pydot
        write_dot(nwk_graph, '{:s}.dot'.format(topo_uuid))
        dot_graph = to_pydot(nwk_graph)
        with open('{:s}.png'.format(topo_uuid), 'wb') as f:
            f.write(dot_graph.create(format='png'))

if __name__ == "__main__":
    topologies = retrieveTopology(IP, PORT, USER, PASS, TOPO_UUID)