blob: b91b569c6ddc2acdfb444fbaa2a901d9b966eb51 [file] [log] [blame]
Pavlin Radoslavove784c602014-02-25 15:32:14 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
4#
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -08005# Get Network Graph Information:
6# - Switches
7# - Links
8# - Shortest Path
Pavlin Radoslavove784c602014-02-25 15:32:14 -08009#
10
11import pprint
12import os
13import sys
14import subprocess
15import json
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080016import collections
Pavlin Radoslavove784c602014-02-25 15:32:14 -080017import argparse
18import io
19import time
20
21from flask import Flask, json, Response, render_template, make_response, request
22
23## Global Var ##
24ControllerIP="127.0.0.1"
25ControllerPort=8080
26
27DEBUG=0
28pp = pprint.PrettyPrinter(indent=4)
29
30app = Flask(__name__)
31
32## Worker Functions ##
33def log_error(txt):
34 print '%s' % (txt)
35
36def debug(txt):
37 if DEBUG:
38 print '%s' % (txt)
39
Jonathan Hart5caa0442014-03-19 15:20:07 -070040# @app.route("/wm/onos/topology/links/json ")
41# @app.route("/wm/onos/topology/switches/json ")
Pavlin Radoslavov030fc442014-04-21 13:38:26 -070042# @app.route("/wm/onos/topology/shortest-path/<src-dpid>/<dst-dpid>/json ")
Pavlin Radoslavove784c602014-02-25 15:32:14 -080043# Sample output:
44
45def print_parsed_result(parsedResult):
46 print '%s' % (parsedResult),
47
48def get_network_switches():
49 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -070050 command = "curl -s \"http://%s:%s/wm/onos/topology/switches/json\"" % (ControllerIP, ControllerPort)
Pavlin Radoslavove784c602014-02-25 15:32:14 -080051 debug("get_network_switches %s" % command)
52
53 result = os.popen(command).read()
54 debug("result %s" % result)
55 if len(result) == 0:
56 print "No Switches found"
57 return;
58
59 # parsedResult = result
60 # parsedResult = json.loads(result)
61 parsedResult = json.dumps(json.loads(result), indent=4)
62 debug("parsed %s" % parsedResult)
63 except:
64 log_error("Controller IF has issue")
65 exit(1)
66
67 print_parsed_result(parsedResult)
68
69def get_network_links():
70 try:
Jonathan Hart5caa0442014-03-19 15:20:07 -070071 command = "curl -s \"http://%s:%s/wm/onos/topology/links/json\"" % (ControllerIP, ControllerPort)
Pavlin Radoslavove784c602014-02-25 15:32:14 -080072 debug("get_network_links %s" % command)
73
74 result = os.popen(command).read()
75 debug("result %s" % result)
76 if len(result) == 0:
77 print "No Links found"
78 return;
79
80 # parsedResult = result
81 # parsedResult = json.loads(result)
82 parsedResult = json.dumps(json.loads(result), indent=4)
83 debug("parsed %s" % parsedResult)
84 except:
85 log_error("Controller IF has issue")
86 exit(1)
87
88 print_parsed_result(parsedResult)
89
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080090def get_network_shortest_path(src_dpid, dst_dpid):
91 try:
Pavlin Radoslavov030fc442014-04-21 13:38:26 -070092 command = "curl -s \"http://%s:%s/wm/onos/topology/shortest-path/%s/%s/json\"" % (ControllerIP, ControllerPort, src_dpid, dst_dpid)
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080093 debug("get_network_switches %s" % command)
94
95 result = os.popen(command).read()
96 debug("result %s" % result)
97 if len(result) == 0:
98 print "No Path found"
99 return;
100
101 # parsedResult = result
102 parsedResult = json.loads(result, object_pairs_hook=collections.OrderedDict)
103 parsedResult = json.dumps(parsedResult, indent=4)
104 debug("parsed %s" % parsedResult)
105 except:
106 log_error("Controller IF has issue")
107 exit(1)
108
109 print_parsed_result(parsedResult)
110
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800111
112if __name__ == "__main__":
113 usage_msg1 = "Usage:\n"
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -0800114 usage_msg2 = "%s <arguments> : Print network information\n" % (sys.argv[0])
115 usage_msg3 = " Valid element names:\n"
116 usage_msg4 = " all : Print all network elements\n"
117 usage_msg5 = " switches : Print all switches and ports\n"
118 usage_msg6 = " links : Print all links\n"
119 usage_msg7 = " shortest-path <src-dpid> <dst-dpid> : Print shortest-path\n"
120 usage_msg8 = " (links between <src-dpid> and <dst-dpid>)\n"
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800121 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -0800122 usage_msg = usage_msg + usage_msg6 + usage_msg7 + usage_msg8
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800123
124 # Usage info
125 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
126 print(usage_msg)
127 exit(0)
128
129 # Check arguments
130 if len(sys.argv) < 2:
131 log_error(usage_msg)
132 exit(1)
133
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -0800134 if (sys.argv[1] != "all" and sys.argv[1] != "switches" and sys.argv[1] != "links" and sys.argv[1] != "shortest-path"):
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800135 log_error(usage_msg)
136 exit(1)
137
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -0800138 if (sys.argv[1] == "shortest-path"):
139 if len(sys.argv) < 4:
140 log_error(usage_msg)
141 exit(1)
142
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800143 # Do the work
144 if (sys.argv[1] == "all" or sys.argv[1] == "switches"):
145 get_network_switches()
146 if (sys.argv[1] == "all" or sys.argv[1] == "links"):
147 get_network_links()
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -0800148 if (sys.argv[1] == "shortest-path"):
149 get_network_shortest_path(sys.argv[2], sys.argv[3])