Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 3 | |
| 4 | import pprint |
| 5 | import os |
| 6 | import sys |
| 7 | import subprocess |
| 8 | import json |
| 9 | import argparse |
| 10 | import io |
| 11 | import time |
| 12 | |
| 13 | from flask import Flask, json, Response, render_template, make_response, request |
| 14 | |
| 15 | ## Global Var ## |
| 16 | ControllerIP="127.0.0.1" |
| 17 | ControllerPort=8080 |
| 18 | |
| 19 | DEBUG=0 |
| 20 | pp = pprint.PrettyPrinter(indent=4) |
| 21 | |
| 22 | app = Flask(__name__) |
| 23 | |
| 24 | ## Worker Functions ## |
| 25 | def log_error(txt): |
| 26 | print '%s' % (txt) |
| 27 | |
| 28 | def 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 Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 35 | |
| 36 | def print_flow_path(parsedResult): |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 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'] |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 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']: |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 47 | inPort = f['inPort']['value'] |
| 48 | outPort = f['outPort']['value'] |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 49 | dpid = f['dpid']['value'] |
| 50 | userState = f['flowEntryUserState'] |
| 51 | switchState = f['flowEntrySwitchState'] |
Pavlin Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 52 | match = f['flowEntryMatch']; |
| 53 | actions = f['flowEntryActions'] |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 54 | print " FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState) |
| 55 | |
Pavlin Radoslavov | 2d59f58 | 2013-03-11 11:36:06 -0700 | [diff] [blame] | 56 | # |
| 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 Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 110 | |
Pavlin Radoslavov | 2d59f58 | 2013-03-11 11:36:06 -0700 | [diff] [blame] | 111 | # |
| 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 Radoslavov | ede9758 | 2013-03-08 18:57:28 -0800 | [diff] [blame] | 157 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 158 | def 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 Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 175 | print_flow_path(parsedResult) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 176 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 177 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 178 | def 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 Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 182 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 183 | 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 | |
| 199 | def 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 | |
| 220 | def 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 Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 239 | |
| 240 | if __name__ == "__main__": |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 241 | 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 Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 247 | |
| 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 Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame] | 261 | 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]) |