blob: 99e4f3a58fc810b4ff2da82309a025bb873a3a88 [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)
45
46 for f in parsedResult['dataPath']['flowEntries']:
Pavlin Radoslavovede97582013-03-08 18:57:28 -080047 inPort = f['inPort']['value']
48 outPort = f['outPort']['value']
Pavlin Radoslavov706df052013-03-06 10:49:07 -080049 dpid = f['dpid']['value']
50 userState = f['flowEntryUserState']
51 switchState = f['flowEntrySwitchState']
Pavlin Radoslavovede97582013-03-08 18:57:28 -080052 match = f['flowEntryMatch'];
53 actions = f['flowEntryActions']
Pavlin Radoslavov706df052013-03-06 10:49:07 -080054 print " FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState)
55
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -070056 #
57 # Print the match conditions
58 #
59 if match == None:
60 print " Match: %s" % (match)
61 else:
62 inPort = match['inPort']
63 matchInPort = match['matchInPort']
64 srcMac = match['srcMac']
65 matchSrcMac = match['matchSrcMac']
66 dstMac = match['dstMac']
67 matchDstMac = match['matchDstMac']
68 vlanId = match['vlanId']
69 matchVlanId = match['matchVlanId']
70 vlanPriority = match['vlanPriority']
71 matchVlanPriority = match['matchVlanPriority']
72 ethernetFrameType = match['ethernetFrameType']
73 matchEthernetFrameType = match['matchEthernetFrameType']
74 ipToS = match['ipToS']
75 matchIpToS = match['matchIpToS']
76 ipProto = match['ipProto']
77 matchIpProto = match['matchIpProto']
78 srcIPv4Net = match['srcIPv4Net']
79 matchSrcIPv4Net = match['matchSrcIPv4Net']
80 dstIPv4Net = match['dstIPv4Net']
81 matchDstIPv4Net = match['matchDstIPv4Net']
82 srcTcpUdpPort = match['srcTcpUdpPort']
83 matchSrcTcpUdpPort = match['matchSrcTcpUdpPort']
84 dstTcpUdpPort = match['dstTcpUdpPort']
85 matchDstTcpUdpPort = match['matchDstTcpUdpPort']
86 if matchInPort == True:
87 print " inPort: %s" % inPort['value']
88 if matchSrcMac == True:
89 print " srcMac: %s" % srcMac['value']
90 if matchDstMac == True:
91 print " dstMac: %s" % dstMac['value']
92 if matchVlanId == True:
93 print " vlanId: %s" % vlanId
94 if matchVlanPriority == True:
95 print " vlanPriority: %s" % vlanPriority
96 if matchEthernetFrameType == True:
97 print " ethernetFrameType: %s" % ethernetFrameType
98 if matchIpToS == True:
99 print " ipToS: %s" % ipToS
100 if matchIpProto == True:
101 print " ipProto: %s" % ipProto
102 if matchSrcIPv4Net == True:
103 print " srcIPv4Net: %s" % srcIPv4Net['value']
104 if matchDstIPv4Net == True:
105 print " dstIPv4Net: %s" % dstIPv4Net['value']
106 if matchSrcTcpUdpPort == True:
107 print " srcTcpUdpPort: %s" % srcTcpUdpPort
108 if matchDstTcpUdpPort == True:
109 print " dstTcpUdpPort: %s" % dstTcpUdpPort
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800110
Pavlin Radoslavov2d59f582013-03-11 11:36:06 -0700111 #
112 # Print the actions
113 #
114 if actions == None:
115 print " Actions: %s" % (actions)
116 else:
117 for a in actions:
118 actionType = a['actionType']
119 if actionType == "ACTION_OUTPUT":
120 port = a['actionOutput']['port']
121 maxLen = a['actionOutput']['maxLen']
122 print " actionType: %s port: %s maxLen: %s" % (actionType, port, maxLen)
123 if actionType == "ACTION_SET_VLAN_VID":
124 vlanId = a['actionSetVlanId']['vlanId']
125 print " actionType: %s vlanId: %s" % (actionType, vlanId)
126 if actionType == "ACTION_SET_VLAN_PCP":
127 vlanPriority = a['actionSetVlanPriority']['vlanPriority']
128 print " actionType: %s vlanPriority: %s" % (actionType, vlanPriority)
129 if actionType == "ACTION_STRIP_VLAN":
130 stripVlan = a['actionStripVlan']['stripVlan']
131 print " actionType: %s stripVlan: %s" % (actionType, stripVlan)
132 if actionType == "ACTION_SET_DL_SRC":
133 setEthernetSrcAddr = a['actionSetEthernetSrcAddr']['addr']['value']
134 print " actionType: %s setEthernetSrcAddr: %s" % (actionType, setEthernetSrcAddr)
135 if actionType == "ACTION_SET_DL_DST":
136 setEthernetDstAddr = a['actionSetEthernetDstAddr']['addr']['value']
137 print " actionType: %s setEthernetDstAddr: %s" % (actionType, setEthernetDstAddr)
138 if actionType == "ACTION_SET_NW_SRC":
139 setIPv4SrcAddr = a['actionSetIPv4SrcAddr']['addr']['value']
140 print " actionType: %s setIPv4SrcAddr: %s" % (actionType, setIPv4SrcAddr)
141 if actionType == "ACTION_SET_NW_DST":
142 setIPv4DstAddr = a['actionSetIPv4DstAddr']['addr']['value']
143 print " actionType: %s setIPv4DstAddr: %s" % (actionType, setIPv4DstAddr)
144 if actionType == "ACTION_SET_NW_TOS":
145 setIpToS = a['actionSetIpToS']['ipToS']
146 print " actionType: %s setIpToS: %s" % (actionType, setIpToS)
147 if actionType == "ACTION_SET_TP_SRC":
148 setTcpUdpSrcPort = a['actionSetTcpUdpSrcPort']['port']
149 print " actionType: %s setTcpUdpSrcPort: %s" % (actionType, setTcpUdpSrcPort)
150 if actionType == "ACTION_SET_TP_DST":
151 setTcpUdpDstPort = a['actionSetTcpUdpDstPort']['port']
152 print " actionType: %s setTcpUdpDstPort: %s" % (actionType, setTcpUdpDstPort)
153 if actionType == "ACTION_ENQUEUE":
154 port = a['actionEnqueue']['port']['value']
155 queueId = a['actionEnqueue']['queueId']
156 print " actionType: %s port: %s queueId: %s" % (actionType, port, queueId)
Pavlin Radoslavovede97582013-03-08 18:57:28 -0800157
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800158def get_flow_path(flow_id):
159 try:
160 command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
161 debug("get_flow_path %s" % command)
162
163 result = os.popen(command).read()
164 debug("result %s" % result)
165 if len(result) == 0:
166 print "No Flow found"
167 return;
168
169 parsedResult = json.loads(result)
170 debug("parsed %s" % parsedResult)
171 except:
172 log_error("Controller IF has issue")
173 exit(1)
174
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800175 print_flow_path(parsedResult)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800176
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800177
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800178def get_installer_flow_paths(installer_id, v1, p1, v2, p2):
179 try:
180 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)
181 debug("get_installer_flow_paths %s" % command)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800182
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800183 result = os.popen(command).read()
184 debug("result %s" % result)
185 if len(result) == 0:
186 print "No Flows found"
187 return;
188
189 parsedResult = json.loads(result)
190 debug("parsed %s" % parsedResult)
191 except:
192 log_error("Controller IF has issue")
193 exit(1)
194
195 for flowPath in parsedResult:
196 print_flow_path(flowPath)
197
198
199def get_endpoints_flow_paths(v1, p1, v2, p2):
200 try:
201 command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
202 debug("get_endpoints_flow_paths %s" % command)
203
204 result = os.popen(command).read()
205 debug("result %s" % result)
206 if len(result) == 0:
207 print "No Flows found"
208 return;
209
210 parsedResult = json.loads(result)
211 debug("parsed %s" % parsedResult)
212 except:
213 log_error("Controller IF has issue")
214 exit(1)
215
216 for flowPath in parsedResult:
217 print_flow_path(flowPath)
218
219
220def get_all_flow_paths():
221 try:
222 command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort)
223 debug("get_all_flow_paths %s" % command)
224
225 result = os.popen(command).read()
226 debug("result %s" % result)
227 if len(result) == 0:
228 print "No Flows found"
229 return;
230
231 parsedResult = json.loads(result)
232 debug("parsed %s" % parsedResult)
233 except:
234 log_error("Controller IF has issue")
235 exit(1)
236
237 for flowPath in parsedResult:
238 print_flow_path(flowPath)
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800239
240if __name__ == "__main__":
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800241 usage_msg1 = "Usage:\n"
242 usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0])
243 usage_msg3 = " all : Print all flows\n"
244 usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n"
245 usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>"
246 usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5;
Pavlin Radoslavovf4ad9892013-03-04 14:15:19 -0800247
248 # app.debug = False;
249
250 # Usage info
251 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
252 print(usage_msg)
253 exit(0)
254
255 # Check arguments
256 if len(sys.argv) < 2:
257 log_error(usage_msg)
258 exit(1)
259
260 # Do the work
Pavlin Radoslavov706df052013-03-06 10:49:07 -0800261 if sys.argv[1] == "all":
262 get_all_flow_paths()
263 elif sys.argv[1] == "installer":
264 if len(sys.argv) < 7:
265 log_error(usage_msg)
266 exit(1)
267 get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
268 sys.argv[5], sys.argv[6])
269 elif sys.argv[1] == "endpoints":
270 if len(sys.argv) < 6:
271 log_error(usage_msg)
272 exit(1)
273 get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4],
274 sys.argv[5])
275 else:
276 get_flow_path(sys.argv[1])