Skip to content
Snippets Groups Projects
tapi_app.py 1.41 KiB
Newer Older
#!/usr/bin/python
# -*- coding: utf-8 -*-

import requests
from requests.auth import HTTPBasicAuth
import json
import matplotlib.pyplot as plt
import networkx as nx
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

from networkx.drawing.nx_pydot import write_dot
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
PORT = 8080
USER = ''
PASS = ''
TOPO_UUID = 'ols-topo'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
TOPO_LIST_URL = 'http://{:s}:{:d}/restconf/data/tapi-common:context/tapi-topology:topology-context/topology={:s}/'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
def retrieveTopology(ip, port, user, password, topo_uuid):
    print ("Reading network-topology")
    topologies = []
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topo_list_url = TOPO_LIST_URL.format(ip, port, topo_uuid)
    response = requests.get(topo_list_url, auth=HTTPBasicAuth(user, password))
    topologies.append(response.json())
    print ("Retrieved Topology: " + json.dumps(response.json(), indent=4))
    return topologies

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

    for node in topo[0]['node']:
        if node['owned-node-edge-point']:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            nwk_graph.add_node(node['uuid'])

    for link in topo[0]['link']:
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        node1 = link['node-edge-point'][0]['node-uuid']
        node2 = link['node-edge-point'][1]['node-uuid']
        nwk_graph.add_edge(node1, node2)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    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')

if __name__ == "__main__":
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    topologies = retrieveTopology(IP, PORT, USER, PASS, TOPO_UUID)
    draw_topologies(topologies)