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") |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 37 | # |
| 38 | # Sample output: |
| 39 | # {'dstPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:02'}}, 'srcPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:01'}}, 'flowEntries': [{'outPort': {'value': 1}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 0}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:01'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}, {'outPort': {'value': 0}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 9}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:02'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}]} |
| 40 | # |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 41 | def shortest_path(v1, p1, v2, p2): |
| 42 | try: |
| 43 | command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (ControllerIP, ControllerPort, v1, p1, v2, p2) |
Pavlin Radoslavov | fd0657c | 2013-03-01 15:52:16 -0800 | [diff] [blame] | 44 | debug("shortest_path %s" % command) |
| 45 | |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 46 | result = os.popen(command).read() |
Pavlin Radoslavov | fd0657c | 2013-03-01 15:52:16 -0800 | [diff] [blame] | 47 | debug("result %s" % result) |
| 48 | |
| 49 | if len(result) == 0: |
| 50 | print "No Path found" |
| 51 | return; |
| 52 | |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 53 | parsedResult = json.loads(result) |
Pavlin Radoslavov | fd0657c | 2013-03-01 15:52:16 -0800 | [diff] [blame] | 54 | debug("parsed %s" % parsedResult) |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 55 | except: |
| 56 | log_error("Controller IF has issue") |
| 57 | exit(1) |
| 58 | |
Pavlin Radoslavov | f83aa44 | 2013-02-26 14:09:01 -0800 | [diff] [blame] | 59 | srcSwitch = parsedResult['srcPort']['dpid']['value']; |
| 60 | srcPort = parsedResult['srcPort']['port']['value']; |
| 61 | dstSwitch = parsedResult['dstPort']['dpid']['value']; |
| 62 | dstPort = parsedResult['dstPort']['port']['value']; |
| 63 | |
| 64 | print "DataPath: (src = %s/%s dst = %s/%s)" % (srcSwitch, srcPort, dstSwitch, dstPort); |
| 65 | |
| 66 | for f in parsedResult['flowEntries']: |
| 67 | inPort = f['inPort']['value']; |
| 68 | outPort = f['outPort']['value']; |
| 69 | dpid = f['dpid']['value'] |
Pavlin Radoslavov | 09a9bee | 2013-03-14 17:12:00 -0700 | [diff] [blame^] | 70 | print " FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort) |
Pavlin Radoslavov | 382b22a | 2013-01-28 09:24:04 -0800 | [diff] [blame] | 71 | |
| 72 | |
| 73 | if __name__ == "__main__": |
| 74 | usage_msg = "Usage: %s <src-dpid> <src-port> <dest-dpid> <dest-port>" % (sys.argv[0]) |
| 75 | |
| 76 | # app.debug = False; |
| 77 | |
| 78 | # Usage info |
| 79 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 80 | print(usage_msg) |
| 81 | exit(0) |
| 82 | |
| 83 | # Check arguments |
| 84 | if len(sys.argv) < 5: |
| 85 | log_error(usage_msg) |
| 86 | exit(1) |
| 87 | |
| 88 | # Do the work |
| 89 | shortest_path(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]); |