blob: d7df0700fe17ee3c0ebb58cf8901d855bcc51cdc [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
Pavlin Radoslavove784c602014-02-25 15:32:14 -08008#
9
10import pprint
11import os
12import sys
13import subprocess
14import json
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080015import collections
Pavlin Radoslavove784c602014-02-25 15:32:14 -080016import argparse
17import io
18import time
19
20from flask import Flask, json, Response, render_template, make_response, request
21
22## Global Var ##
23ControllerIP="127.0.0.1"
24ControllerPort=8080
25
26DEBUG=0
27pp = pprint.PrettyPrinter(indent=4)
28
29app = Flask(__name__)
30
31## Worker Functions ##
32def log_error(txt):
33 print '%s' % (txt)
34
35def debug(txt):
36 if DEBUG:
37 print '%s' % (txt)
38
Yuta HIGUCHI42d7f1b2014-05-28 09:19:49 -070039# @app.route("/wm/onos/topology/links ")
40# @app.route("/wm/onos/topology/switches ")
Pavlin Radoslavove784c602014-02-25 15:32:14 -080041# Sample output:
42
43def print_parsed_result(parsedResult):
44 print '%s' % (parsedResult),
45
46def get_network_switches():
47 try:
Yuta HIGUCHI42d7f1b2014-05-28 09:19:49 -070048 command = "curl -s \"http://%s:%s/wm/onos/topology/switches\"" % (ControllerIP, ControllerPort)
Pavlin Radoslavove784c602014-02-25 15:32:14 -080049 debug("get_network_switches %s" % command)
50
51 result = os.popen(command).read()
52 debug("result %s" % result)
53 if len(result) == 0:
54 print "No Switches found"
55 return;
56
57 # parsedResult = result
58 # parsedResult = json.loads(result)
59 parsedResult = json.dumps(json.loads(result), indent=4)
60 debug("parsed %s" % parsedResult)
61 except:
62 log_error("Controller IF has issue")
63 exit(1)
64
65 print_parsed_result(parsedResult)
66
67def get_network_links():
68 try:
Yuta HIGUCHI42d7f1b2014-05-28 09:19:49 -070069 command = "curl -s \"http://%s:%s/wm/onos/topology/links\"" % (ControllerIP, ControllerPort)
Pavlin Radoslavove784c602014-02-25 15:32:14 -080070 debug("get_network_links %s" % command)
71
72 result = os.popen(command).read()
73 debug("result %s" % result)
74 if len(result) == 0:
75 print "No Links found"
76 return;
77
78 # parsedResult = result
79 # parsedResult = json.loads(result)
80 parsedResult = json.dumps(json.loads(result), indent=4)
81 debug("parsed %s" % parsedResult)
82 except:
83 log_error("Controller IF has issue")
84 exit(1)
85
86 print_parsed_result(parsedResult)
87
88
89if __name__ == "__main__":
90 usage_msg1 = "Usage:\n"
Pavlin Radoslavov97af90a2014-02-25 18:34:02 -080091 usage_msg2 = "%s <arguments> : Print network information\n" % (sys.argv[0])
92 usage_msg3 = " Valid element names:\n"
93 usage_msg4 = " all : Print all network elements\n"
94 usage_msg5 = " switches : Print all switches and ports\n"
95 usage_msg6 = " links : Print all links\n"
Pavlin Radoslavove784c602014-02-25 15:32:14 -080096 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5
Pavlin Radoslavov0de6f322014-05-23 18:25:21 -070097 usage_msg = usage_msg + usage_msg6
Pavlin Radoslavove784c602014-02-25 15:32:14 -080098
99 # Usage info
100 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
101 print(usage_msg)
102 exit(0)
103
104 # Check arguments
105 if len(sys.argv) < 2:
106 log_error(usage_msg)
107 exit(1)
108
Pavlin Radoslavov0de6f322014-05-23 18:25:21 -0700109 if (sys.argv[1] != "all" and sys.argv[1] != "switches" and sys.argv[1] != "links"):
Pavlin Radoslavove784c602014-02-25 15:32:14 -0800110 log_error(usage_msg)
111 exit(1)
112
113 # Do the work
114 if (sys.argv[1] == "all" or sys.argv[1] == "switches"):
115 get_network_switches()
116 if (sys.argv[1] == "all" or sys.argv[1] == "links"):
117 get_network_links()