blob: b20e1348bb74babc7e63f057840cc262a8a785a3 [file] [log] [blame]
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -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## Global Var ##
16ControllerIP="127.0.0.1"
17ControllerPort=8080
18
19DEBUG=0
20pp = pprint.PrettyPrinter(indent=4)
21
22app = Flask(__name__)
23
24## Worker Functions ##
25def log_error(txt):
26 print '%s' % (txt)
27
28def debug(txt):
29 if DEBUG:
30 print '%s' % (txt)
31
32# @app.route("/wm/flow/get/<flow-id>/json")
33# Sample output:
34# {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"00:00:00:00:00:00:00:02"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:02"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}
Pavlin Radoslavov706df052013-03-06 10:49:07 -080035
36def print_flow_path(parsedResult):
37 flowId = parsedResult['flowId']['value'];
38 installerId = parsedResult['installerId']['value'];
39 srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value'];
40 srcPort = parsedResult['dataPath']['srcPort']['port']['value'];
41 dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value'];
42 dstPort = parsedResult['dataPath']['dstPort']['port']['value'];
43
44 print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)
45
46 for f in parsedResult['dataPath']['flowEntries']:
47 inPort = f['inPort']['value'];
48 outPort = f['outPort']['value'];
49 dpid = f['dpid']['value']
50 userState = f['flowEntryUserState']
51 switchState = f['flowEntrySwitchState']
52 print " FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState)
53
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080054def get_flow_path(flow_id):
55 try:
56 command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
57 debug("get_flow_path %s" % command)
58
59 result = os.popen(command).read()
60 debug("result %s" % result)
61 if len(result) == 0:
62 print "No Flow found"
63 return;
64
65 parsedResult = json.loads(result)
66 debug("parsed %s" % parsedResult)
67 except:
68 log_error("Controller IF has issue")
69 exit(1)
70
Pavlin Radoslavov706df052013-03-06 10:49:07 -080071 print_flow_path(parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080072
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080073
Pavlin Radoslavov706df052013-03-06 10:49:07 -080074def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
75 try:
76 command = "curl -s \"http://%s:%s/wm/flow/getall-by-installer-id/%s/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, installer_id, v1, p1, v2, p2)
77 debug("get_installer_flow_paths %s" % command)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -080078
Pavlin Radoslavov706df052013-03-06 10:49:07 -080079 result = os.popen(command).read()
80 debug("result %s" % result)
81 if len(result) == 0:
82 print "No Flows found"
83 return;
84
85 parsedResult = json.loads(result)
86 debug("parsed %s" % parsedResult)
87 except:
88 log_error("Controller IF has issue")
89 exit(1)
90
91 for flowPath in parsedResult:
92 print_flow_path(flowPath)
93
94
95def get_endpoints_flow_paths(v1, p1, v2, p2):
96 try:
97 command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
98 debug("get_endpoints_flow_paths %s" % command)
99
100 result = os.popen(command).read()
101 debug("result %s" % result)
102 if len(result) == 0:
103 print "No Flows found"
104 return;
105
106 parsedResult = json.loads(result)
107 debug("parsed %s" % parsedResult)
108 except:
109 log_error("Controller IF has issue")
110 exit(1)
111
112 for flowPath in parsedResult:
113 print_flow_path(flowPath)
114
115
116def get_all_flow_paths():
117 try:
118 command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
119 debug("get_all_flow_paths %s" % command)
120
121 result = os.popen(command).read()
122 debug("result %s" % result)
123 if len(result) == 0:
124 print "No Flows found"
125 return;
126
127 parsedResult = json.loads(result)
128 debug("parsed %s" % parsedResult)
129 except:
130 log_error("Controller IF has issue")
131 exit(1)
132
133 for flowPath in parsedResult:
134 print_flow_path(flowPath)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800135
136if __name__ == "__main__":
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800137 usage_msg1 = "Usage:\n"
138 usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
139 usage_msg3 = " all : Print all flows\n"
140 usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
141 usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
142 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800143
144 # app.debug = False;
145
146 # Usage info
147 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
148 print(usage_msg)
149 exit(0)
150
151 # Check arguments
152 if len(sys.argv) < 2:
153 log_error(usage_msg)
154 exit(1)
155
156 # Do the work
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800157 if sys.argv[1] == "all":
158 get_all_flow_paths()
159 elif sys.argv[1] == "installer":
160 if len(sys.argv) < 7:
161 log_error(usage_msg)
162 exit(1)
163 get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
164 sys.argv[5], sys.argv[6])
165 elif sys.argv[1] == "endpoints":
166 if len(sys.argv) < 6:
167 log_error(usage_msg)
168 exit(1)
169 get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
170 sys.argv[5])
171 else:
172 get_flow_path(sys.argv[1])