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 | # |
| 5 | # Get Network Graph Elements |
| 6 | # |
| 7 | |
| 8 | import pprint |
| 9 | import os |
| 10 | import sys |
| 11 | import subprocess |
| 12 | import json |
| 13 | import argparse |
| 14 | import io |
| 15 | import time |
| 16 | |
| 17 | from flask import Flask, json, Response, render_template, make_response, request |
| 18 | |
| 19 | ## Global Var ## |
| 20 | ControllerIP="127.0.0.1" |
| 21 | ControllerPort=8080 |
| 22 | |
| 23 | DEBUG=0 |
| 24 | pp = pprint.PrettyPrinter(indent=4) |
| 25 | |
| 26 | app = Flask(__name__) |
| 27 | |
| 28 | ## Worker Functions ## |
| 29 | def log_error(txt): |
| 30 | print '%s' % (txt) |
| 31 | |
| 32 | def debug(txt): |
| 33 | if DEBUG: |
| 34 | print '%s' % (txt) |
| 35 | |
| 36 | # @app.route("/wm/onos/ng/links/json ") |
| 37 | # @app.route("/wm/onos/ng/switches/json ") |
| 38 | # Sample output: |
| 39 | |
| 40 | def print_parsed_result(parsedResult): |
| 41 | print '%s' % (parsedResult), |
| 42 | |
| 43 | def get_network_switches(): |
| 44 | try: |
| 45 | command = "curl -s \"http://%s:%s/wm/onos/ng/switches/json\"" % (ControllerIP, ControllerPort) |
| 46 | debug("get_network_switches %s" % command) |
| 47 | |
| 48 | result = os.popen(command).read() |
| 49 | debug("result %s" % result) |
| 50 | if len(result) == 0: |
| 51 | print "No Switches found" |
| 52 | return; |
| 53 | |
| 54 | # parsedResult = result |
| 55 | # parsedResult = json.loads(result) |
| 56 | parsedResult = json.dumps(json.loads(result), indent=4) |
| 57 | debug("parsed %s" % parsedResult) |
| 58 | except: |
| 59 | log_error("Controller IF has issue") |
| 60 | exit(1) |
| 61 | |
| 62 | print_parsed_result(parsedResult) |
| 63 | |
| 64 | def get_network_links(): |
| 65 | try: |
| 66 | command = "curl -s \"http://%s:%s/wm/onos/ng/links/json\"" % (ControllerIP, ControllerPort) |
| 67 | debug("get_network_links %s" % command) |
| 68 | |
| 69 | result = os.popen(command).read() |
| 70 | debug("result %s" % result) |
| 71 | if len(result) == 0: |
| 72 | print "No Links found" |
| 73 | return; |
| 74 | |
| 75 | # parsedResult = result |
| 76 | # parsedResult = json.loads(result) |
| 77 | parsedResult = json.dumps(json.loads(result), indent=4) |
| 78 | debug("parsed %s" % parsedResult) |
| 79 | except: |
| 80 | log_error("Controller IF has issue") |
| 81 | exit(1) |
| 82 | |
| 83 | print_parsed_result(parsedResult) |
| 84 | |
| 85 | |
| 86 | if __name__ == "__main__": |
| 87 | usage_msg1 = "Usage:\n" |
| 88 | usage_msg2 = "%s <elements_name> : Print network elements with name of <elements_name>\n" % (sys.argv[0]) |
| 89 | usage_msg3 = " Valid element names:\n" |
| 90 | usage_msg4 = " all : Print all network elements\n" |
| 91 | usage_msg5 = " switches : Print all switches and ports\n" |
| 92 | usage_msg6 = " links : Print all links\n" |
| 93 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5 |
| 94 | usage_msg = usage_msg + usage_msg6 |
| 95 | |
| 96 | # Usage info |
| 97 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 98 | print(usage_msg) |
| 99 | exit(0) |
| 100 | |
| 101 | # Check arguments |
| 102 | if len(sys.argv) < 2: |
| 103 | log_error(usage_msg) |
| 104 | exit(1) |
| 105 | |
| 106 | if (sys.argv[1] != "all" and sys.argv[1] != "switches" and sys.argv[1] != "links"): |
| 107 | log_error(usage_msg) |
| 108 | exit(1) |
| 109 | |
| 110 | # Do the work |
| 111 | if (sys.argv[1] == "all" or sys.argv[1] == "switches"): |
| 112 | get_network_switches() |
| 113 | if (sys.argv[1] == "all" or sys.argv[1] == "links"): |
| 114 | get_network_links() |