Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | # |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 5 | # Get Network Graph Information: |
| 6 | # - Switches |
| 7 | # - Links |
| 8 | # - Shortest Path |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 9 | # |
| 10 | |
| 11 | import pprint |
| 12 | import os |
| 13 | import sys |
| 14 | import subprocess |
| 15 | import json |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 16 | import collections |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 17 | import argparse |
| 18 | import io |
| 19 | import time |
| 20 | |
| 21 | from flask import Flask, json, Response, render_template, make_response, request |
| 22 | |
| 23 | ## Global Var ## |
| 24 | ControllerIP="127.0.0.1" |
| 25 | ControllerPort=8080 |
| 26 | |
| 27 | DEBUG=0 |
| 28 | pp = pprint.PrettyPrinter(indent=4) |
| 29 | |
| 30 | app = Flask(__name__) |
| 31 | |
| 32 | ## Worker Functions ## |
| 33 | def log_error(txt): |
| 34 | print '%s' % (txt) |
| 35 | |
| 36 | def debug(txt): |
| 37 | if DEBUG: |
| 38 | print '%s' % (txt) |
| 39 | |
| 40 | # @app.route("/wm/onos/ng/links/json ") |
| 41 | # @app.route("/wm/onos/ng/switches/json ") |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 42 | # @app.route("/wm/onos/ng/shortest-path/<src-dpid>/<dst-dpid>/json ") |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 43 | # Sample output: |
| 44 | |
| 45 | def print_parsed_result(parsedResult): |
| 46 | print '%s' % (parsedResult), |
| 47 | |
| 48 | def get_network_switches(): |
| 49 | try: |
| 50 | command = "curl -s \"http://%s:%s/wm/onos/ng/switches/json\"" % (ControllerIP, ControllerPort) |
| 51 | 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 | |
| 69 | def get_network_links(): |
| 70 | try: |
| 71 | command = "curl -s \"http://%s:%s/wm/onos/ng/links/json\"" % (ControllerIP, ControllerPort) |
| 72 | 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 Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 90 | def get_network_shortest_path(src_dpid, dst_dpid): |
| 91 | try: |
| 92 | command = "curl -s \"http://%s:%s/wm/onos/ng/shortest-path/%s/%s/json\"" % (ControllerIP, ControllerPort, src_dpid, dst_dpid) |
| 93 | 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 Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 111 | |
| 112 | if __name__ == "__main__": |
| 113 | usage_msg1 = "Usage:\n" |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 114 | 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 Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 121 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5 |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 122 | usage_msg = usage_msg + usage_msg6 + usage_msg7 + usage_msg8 |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 123 | |
| 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 Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 134 | if (sys.argv[1] != "all" and sys.argv[1] != "switches" and sys.argv[1] != "links" and sys.argv[1] != "shortest-path"): |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 135 | log_error(usage_msg) |
| 136 | exit(1) |
| 137 | |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 138 | if (sys.argv[1] == "shortest-path"): |
| 139 | if len(sys.argv) < 4: |
| 140 | log_error(usage_msg) |
| 141 | exit(1) |
| 142 | |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 143 | # 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 Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 148 | if (sys.argv[1] == "shortest-path"): |
| 149 | get_network_shortest_path(sys.argv[2], sys.argv[3]) |