blob: 751489fd8aba1c797dbb980b91a49e7d5f04ca8e [file] [log] [blame]
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -08001#! /usr/bin/env python
2# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
3
4import pprint
5import os
6import sys
7import subprocess
8import json
9import argparse
10import io
11import time
12
13from 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 ##
20ControllerIP="127.0.0.1"
21ControllerPort=8080
22
23DEBUG=0
24pp = pprint.PrettyPrinter(indent=4)
25
26app = Flask(__name__)
27
28## Worker Functions ##
29def log_error(txt):
30 print '%s' % (txt)
31
32def debug(txt):
33 if DEBUG:
34 print '%s' % (txt)
35
36# @app.route("/wm/topology/route/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -080037#
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 Radoslavov382b22a2013-01-28 09:24:04 -080041def 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 Radoslavovfd0657c2013-03-01 15:52:16 -080044 debug("shortest_path %s" % command)
45
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080046 result = os.popen(command).read()
Pavlin Radoslavovfd0657c2013-03-01 15:52:16 -080047 debug("result %s" % result)
48
49 if len(result) == 0:
50 print "No Path found"
51 return;
52
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080053 parsedResult = json.loads(result)
Pavlin Radoslavovfd0657c2013-03-01 15:52:16 -080054 debug("parsed %s" % parsedResult)
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080055 except:
56 log_error("Controller IF has issue")
57 exit(1)
58
Pavlin Radoslavovf83aa442013-02-26 14:09:01 -080059 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 Radoslavov09a9bee2013-03-14 17:12:00 -070070 print " FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort)
Pavlin Radoslavov382b22a2013-01-28 09:24:04 -080071
72
73if __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]);