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