Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | import pprint |
| 5 | import os |
| 6 | import sys |
| 7 | import subprocess |
| 8 | import json |
| 9 | import argparse |
| 10 | import io |
| 11 | import time |
| 12 | |
| 13 | from flask import Flask, json, Response, render_template, make_response, request |
| 14 | |
| 15 | # |
| 16 | # curl http://127.0.0.1:8080/wm/topology/route/00:00:00:00:00:00:0a:01/1/00:00:00:00:00:00:0a:04/1/json |
| 17 | # |
| 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/topology/route/<srcdpid>/<srcport>/<destdpid>/<destport>/json") |
| 37 | def shortest_path(v1, p1, v2, p2): |
| 38 | try: |
| 39 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (ControllerIP, ControllerPort, v1, p1, v2, p2) |
| 40 | result = os.popen(command).read() |
| 41 | parsedResult = json.loads(result) |
| 42 | except: |
| 43 | log_error("Controller IF has issue") |
| 44 | exit(1) |
| 45 | |
| 46 | debug("shortest_path %s" % command) |
| 47 | debug("parsed %s" % parsedResult) |
| 48 | |
| 49 | for v in parsedResult: |
| 50 | dpid = v['switch']; |
| 51 | port = v['port']; |
| 52 | print "PathEntry: (%s, %s)" % (dpid, port) |
| 53 | |
| 54 | |
| 55 | if __name__ == "__main__": |
| 56 | usage_msg = "Usage: %s <src-dpid> <src-port> <dest-dpid> <dest-port>" % (sys.argv[0]) |
| 57 | |
| 58 | # app.debug = False; |
| 59 | |
| 60 | # Usage info |
| 61 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 62 | print(usage_msg) |
| 63 | exit(0) |
| 64 | |
| 65 | # Check arguments |
| 66 | if len(sys.argv) < 5: |
| 67 | log_error(usage_msg) |
| 68 | exit(1) |
| 69 | |
| 70 | # Do the work |
| 71 | shortest_path(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]); |