blob: 033176dfe1cefde765b1eefded368c1dfe22b9fa [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):
Pavlin Radoslavovede97582013-03-08 18:57:28 -080037 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']
Pavlin Radoslavov706df052013-03-06 10:49:07 -080043
44 print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)
Pavlin Radoslavov67b3ef32013-04-03 02:44:48 -070045 match = parsedResult['flowEntryMatch'];
46 #
47 # Print the common conditions
48 #
49 if match == None:
50 print " Match: %s" % (match)
51 else:
52 # inPort = match['inPort']
53 # matchInPort = match['matchInPort']
54 srcMac = match['srcMac']
55 matchSrcMac = match['matchSrcMac']
56 dstMac = match['dstMac']
57 matchDstMac = match['matchDstMac']
58 vlanId = match['vlanId']
59 matchVlanId = match['matchVlanId']
60 vlanPriority = match['vlanPriority']
61 matchVlanPriority = match['matchVlanPriority']
62 ethernetFrameType = match['ethernetFrameType']
63 matchEthernetFrameType = match['matchEthernetFrameType']
64 ipToS = match['ipToS']
65 matchIpToS = match['matchIpToS']
66 ipProto = match['ipProto']
67 matchIpProto = match['matchIpProto']
68 srcIPv4Net = match['srcIPv4Net']
69 matchSrcIPv4Net = match['matchSrcIPv4Net']
70 dstIPv4Net = match['dstIPv4Net']
71 matchDstIPv4Net = match['matchDstIPv4Net']
72 srcTcpUdpPort = match['srcTcpUdpPort']
73 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
74 dstTcpUdpPort = match['dstTcpUdpPort']
75 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
76 # if matchInPort == True:
77 # print " inPort: %s" % inPort['value']
78 if matchSrcMac == True:
79 print " srcMac: %s" % srcMac['value']
80 if matchDstMac == True:
81 print " dstMac: %s" % dstMac['value']
82 if matchVlanId == True:
83 print " vlanId: %s" % vlanId
84 if matchVlanPriority == True:
85 print " vlanPriority: %s" % vlanPriority
86 if matchEthernetFrameType == True:
87 print " ethernetFrameType: %s" % hex(ethernetFrameType)
88 if matchIpToS == True:
89 print " ipToS: %s" % ipToS
90 if matchIpProto == True:
91 print " ipProto: %s" % ipProto
92 if matchSrcIPv4Net == True:
93 print " srcIPv4Net: %s" % srcIPv4Net['value']
94 if matchDstIPv4Net == True:
95 print " dstIPv4Net: %s" % dstIPv4Net['value']
96 if matchSrcTcpUdpPort == True:
97 print " srcTcpUdpPort: %s" % srcTcpUdpPort
98 if matchDstTcpUdpPort == True:
99 print " dstTcpUdpPort: %s" % dstTcpUdpPort
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800100
101 for f in parsedResult['dataPath']['flowEntries']:
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700102 flowEntryId = f['flowEntryId']
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800103 dpid = f['dpid']['value']
104 userState = f['flowEntryUserState']
105 switchState = f['flowEntrySwitchState']
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800106 match = f['flowEntryMatch'];
107 actions = f['flowEntryActions']
Pavlin Radoslavov8cdd1a22013-03-15 20:28:00 -0700108 print " FlowEntry: (%s, %s, %s, %s)" % (flowEntryId, dpid, userState, switchState)
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800109
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700110 #
111 # Print the match conditions
112 #
113 if match == None:
114 print " Match: %s" % (match)
115 else:
116 inPort = match['inPort']
117 matchInPort = match['matchInPort']
118 srcMac = match['srcMac']
119 matchSrcMac = match['matchSrcMac']
120 dstMac = match['dstMac']
121 matchDstMac = match['matchDstMac']
122 vlanId = match['vlanId']
123 matchVlanId = match['matchVlanId']
124 vlanPriority = match['vlanPriority']
125 matchVlanPriority = match['matchVlanPriority']
126 ethernetFrameType = match['ethernetFrameType']
127 matchEthernetFrameType = match['matchEthernetFrameType']
128 ipToS = match['ipToS']
129 matchIpToS = match['matchIpToS']
130 ipProto = match['ipProto']
131 matchIpProto = match['matchIpProto']
132 srcIPv4Net = match['srcIPv4Net']
133 matchSrcIPv4Net = match['matchSrcIPv4Net']
134 dstIPv4Net = match['dstIPv4Net']
135 matchDstIPv4Net = match['matchDstIPv4Net']
136 srcTcpUdpPort = match['srcTcpUdpPort']
137 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
138 dstTcpUdpPort = match['dstTcpUdpPort']
139 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
140 if matchInPort == True:
141 print " inPort: %s" % inPort['value']
142 if matchSrcMac == True:
143 print " srcMac: %s" % srcMac['value']
144 if matchDstMac == True:
145 print " dstMac: %s" % dstMac['value']
146 if matchVlanId == True:
147 print " vlanId: %s" % vlanId
148 if matchVlanPriority == True:
149 print " vlanPriority: %s" % vlanPriority
150 if matchEthernetFrameType == True:
Pavlin Radoslavov14748912013-03-12 15:44:56 -0700151 print " ethernetFrameType: %s" % hex(ethernetFrameType)
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700152 if matchIpToS == True:
153 print " ipToS: %s" % ipToS
154 if matchIpProto == True:
155 print " ipProto: %s" % ipProto
156 if matchSrcIPv4Net == True:
157 print " srcIPv4Net: %s" % srcIPv4Net['value']
158 if matchDstIPv4Net == True:
159 print " dstIPv4Net: %s" % dstIPv4Net['value']
160 if matchSrcTcpUdpPort == True:
161 print " srcTcpUdpPort: %s" % srcTcpUdpPort
162 if matchDstTcpUdpPort == True:
163 print " dstTcpUdpPort: %s" % dstTcpUdpPort
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800164
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700165 #
166 # Print the actions
167 #
168 if actions == None:
169 print " Actions: %s" % (actions)
170 else:
171 for a in actions:
172 actionType = a['actionType']
173 if actionType == "ACTION_OUTPUT":
Pavlin Radoslavov82200bc2013-03-12 13:49:20 -0700174 port = a['actionOutput']['port']['value']
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700175 maxLen = a['actionOutput']['maxLen']
176 print " actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
177 if actionType == "ACTION_SET_VLAN_VID":
178 vlanId = a['actionSetVlanId']['vlanId']
179 print " actionType: %s vlanId: %s" % (actionType, vlanId)
180 if actionType == "ACTION_SET_VLAN_PCP":
181 vlanPriority = a['actionSetVlanPriority']['vlanPriority']
182 print " actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
183 if actionType == "ACTION_STRIP_VLAN":
184 stripVlan = a['actionStripVlan']['stripVlan']
185 print " actionType: %s stripVlan: %s" % (actionType, stripVlan)
186 if actionType == "ACTION_SET_DL_SRC":
187 setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
188 print " actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
189 if actionType == "ACTION_SET_DL_DST":
190 setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
191 print " actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
192 if actionType == "ACTION_SET_NW_SRC":
193 setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
194 print " actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
195 if actionType == "ACTION_SET_NW_DST":
196 setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
197 print " actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
198 if actionType == "ACTION_SET_NW_TOS":
199 setIpToS = a['actionSetIpToS']['ipToS']
200 print " actionType: %s setIpToS: %s" % (actionType, setIpToS)
201 if actionType == "ACTION_SET_TP_SRC":
202 setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
203 print " actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
204 if actionType == "ACTION_SET_TP_DST":
205 setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
206 print " actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
207 if actionType == "ACTION_ENQUEUE":
208 port = a['actionEnqueue']['port']['value']
209 queueId = a['actionEnqueue']['queueId']
210 print " actionType: %s port: %s queueId: %s" % (actionType, port, queueId)
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800211
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800212def get_flow_path(flow_id):
213 try:
214 command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
215 debug("get_flow_path %s" % command)
216
217 result = os.popen(command).read()
218 debug("result %s" % result)
219 if len(result) == 0:
Pavlin Radoslavov8e0a00d2013-03-15 18:32:33 -0700220 print "No Flow found"
221 return;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800222
223 parsedResult = json.loads(result)
224 debug("parsed %s" % parsedResult)
225 except:
226 log_error("Controller IF has issue")
227 exit(1)
228
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800229 print_flow_path(parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800230
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800231
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800232def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
233 try:
234 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)
235 debug("get_installer_flow_paths %s" % command)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800236
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800237 result = os.popen(command).read()
238 debug("result %s" % result)
239 if len(result) == 0:
240 print "No Flows found"
241 return;
242
243 parsedResult = json.loads(result)
244 debug("parsed %s" % parsedResult)
245 except:
246 log_error("Controller IF has issue")
247 exit(1)
248
249 for flowPath in parsedResult:
250 print_flow_path(flowPath)
251
252
253def get_endpoints_flow_paths(v1, p1, v2, p2):
254 try:
255 command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
256 debug("get_endpoints_flow_paths %s" % command)
257
258 result = os.popen(command).read()
259 debug("result %s" % result)
260 if len(result) == 0:
261 print "No Flows found"
262 return;
263
264 parsedResult = json.loads(result)
265 debug("parsed %s" % parsedResult)
266 except:
267 log_error("Controller IF has issue")
268 exit(1)
269
270 for flowPath in parsedResult:
271 print_flow_path(flowPath)
272
273
274def get_all_flow_paths():
275 try:
276 command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
277 debug("get_all_flow_paths %s" % command)
278
279 result = os.popen(command).read()
280 debug("result %s" % result)
281 if len(result) == 0:
282 print "No Flows found"
283 return;
284
285 parsedResult = json.loads(result)
286 debug("parsed %s" % parsedResult)
287 except:
288 log_error("Controller IF has issue")
289 exit(1)
290
291 for flowPath in parsedResult:
292 print_flow_path(flowPath)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800293
294if __name__ == "__main__":
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800295 usage_msg1 = "Usage:\n"
296 usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
297 usage_msg3 = " all : Print all flows\n"
298 usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
299 usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
300 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800301
302 # app.debug = False;
303
304 # Usage info
305 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
306 print(usage_msg)
307 exit(0)
308
309 # Check arguments
310 if len(sys.argv) < 2:
311 log_error(usage_msg)
312 exit(1)
313
314 # Do the work
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800315 if sys.argv[1] == "all":
316 get_all_flow_paths()
317 elif sys.argv[1] == "installer":
318 if len(sys.argv) < 7:
319 log_error(usage_msg)
320 exit(1)
321 get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
322 sys.argv[5], sys.argv[6])
323 elif sys.argv[1] == "endpoints":
324 if len(sys.argv) < 6:
325 log_error(usage_msg)
326 exit(1)
327 get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
328 sys.argv[5])
329 else:
330 get_flow_path(sys.argv[1])