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