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