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 |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 8 | # |
| 9 | |
| 10 | import pprint |
| 11 | import os |
| 12 | import sys |
| 13 | import subprocess |
| 14 | import json |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 15 | import collections |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 16 | import argparse |
| 17 | import io |
| 18 | import time |
| 19 | |
| 20 | from flask import Flask, json, Response, render_template, make_response, request |
| 21 | |
| 22 | ## Global Var ## |
| 23 | ControllerIP="127.0.0.1" |
| 24 | ControllerPort=8080 |
| 25 | |
| 26 | DEBUG=0 |
| 27 | pp = pprint.PrettyPrinter(indent=4) |
| 28 | |
| 29 | app = Flask(__name__) |
| 30 | |
| 31 | ## Worker Functions ## |
| 32 | def log_error(txt): |
| 33 | print '%s' % (txt) |
| 34 | |
| 35 | def debug(txt): |
| 36 | if DEBUG: |
| 37 | print '%s' % (txt) |
| 38 | |
Yuta HIGUCHI | 42d7f1b | 2014-05-28 09:19:49 -0700 | [diff] [blame] | 39 | # @app.route("/wm/onos/topology/links ") |
| 40 | # @app.route("/wm/onos/topology/switches ") |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 41 | # Sample output: |
| 42 | |
| 43 | def print_parsed_result(parsedResult): |
| 44 | print '%s' % (parsedResult), |
| 45 | |
| 46 | def get_network_switches(): |
| 47 | try: |
Yuta HIGUCHI | 42d7f1b | 2014-05-28 09:19:49 -0700 | [diff] [blame] | 48 | command = "curl -s \"http://%s:%s/wm/onos/topology/switches\"" % (ControllerIP, ControllerPort) |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 49 | 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 | |
| 67 | def get_network_links(): |
| 68 | try: |
Yuta HIGUCHI | 42d7f1b | 2014-05-28 09:19:49 -0700 | [diff] [blame] | 69 | command = "curl -s \"http://%s:%s/wm/onos/topology/links\"" % (ControllerIP, ControllerPort) |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 70 | 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 | |
| 89 | if __name__ == "__main__": |
| 90 | usage_msg1 = "Usage:\n" |
Pavlin Radoslavov | 97af90a | 2014-02-25 18:34:02 -0800 | [diff] [blame] | 91 | 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 Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 96 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5 |
Pavlin Radoslavov | 0de6f32 | 2014-05-23 18:25:21 -0700 | [diff] [blame] | 97 | usage_msg = usage_msg + usage_msg6 |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 98 | |
| 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 Radoslavov | 0de6f32 | 2014-05-23 18:25:21 -0700 | [diff] [blame] | 109 | if (sys.argv[1] != "all" and sys.argv[1] != "switches" and sys.argv[1] != "links"): |
Pavlin Radoslavov | e784c60 | 2014-02-25 15:32:14 -0800 | [diff] [blame] | 110 | 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() |