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): |
| 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']; |
| 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']: |
| 47 | inPort = f['inPort']['value']; |
| 48 | outPort = f['outPort']['value']; |
| 49 | dpid = f['dpid']['value'] |
| 50 | userState = f['flowEntryUserState'] |
| 51 | switchState = f['flowEntrySwitchState'] |
| 52 | print " FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState) |
| 53 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 54 | def get_flow_path(flow_id): |
| 55 | try: |
| 56 | command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id) |
| 57 | debug("get_flow_path %s" % command) |
| 58 | |
| 59 | result = os.popen(command).read() |
| 60 | debug("result %s" % result) |
| 61 | if len(result) == 0: |
| 62 | print "No Flow found" |
| 63 | return; |
| 64 | |
| 65 | parsedResult = json.loads(result) |
| 66 | debug("parsed %s" % parsedResult) |
| 67 | except: |
| 68 | log_error("Controller IF has issue") |
| 69 | exit(1) |
| 70 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame^] | 71 | print_flow_path(parsedResult) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 72 | |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 73 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame^] | 74 | def get_installer_flow_paths(installer_id, v1, p1, v2, p2): |
| 75 | try: |
| 76 | 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) |
| 77 | debug("get_installer_flow_paths %s" % command) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 78 | |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame^] | 79 | result = os.popen(command).read() |
| 80 | debug("result %s" % result) |
| 81 | if len(result) == 0: |
| 82 | print "No Flows found" |
| 83 | return; |
| 84 | |
| 85 | parsedResult = json.loads(result) |
| 86 | debug("parsed %s" % parsedResult) |
| 87 | except: |
| 88 | log_error("Controller IF has issue") |
| 89 | exit(1) |
| 90 | |
| 91 | for flowPath in parsedResult: |
| 92 | print_flow_path(flowPath) |
| 93 | |
| 94 | |
| 95 | def get_endpoints_flow_paths(v1, p1, v2, p2): |
| 96 | try: |
| 97 | command = "curl -s \"http://%s:%s/wm/flow/getall-by-endpoints/%s/%s/%s/%s/json\"" % (ControllerIP, ControllerPort, v1, p1, v2, p2) |
| 98 | debug("get_endpoints_flow_paths %s" % command) |
| 99 | |
| 100 | result = os.popen(command).read() |
| 101 | debug("result %s" % result) |
| 102 | if len(result) == 0: |
| 103 | print "No Flows found" |
| 104 | return; |
| 105 | |
| 106 | parsedResult = json.loads(result) |
| 107 | debug("parsed %s" % parsedResult) |
| 108 | except: |
| 109 | log_error("Controller IF has issue") |
| 110 | exit(1) |
| 111 | |
| 112 | for flowPath in parsedResult: |
| 113 | print_flow_path(flowPath) |
| 114 | |
| 115 | |
| 116 | def get_all_flow_paths(): |
| 117 | try: |
| 118 | command = "curl -s \"http://%s:%s/wm/flow/getall/json\"" % (ControllerIP, ControllerPort) |
| 119 | debug("get_all_flow_paths %s" % command) |
| 120 | |
| 121 | result = os.popen(command).read() |
| 122 | debug("result %s" % result) |
| 123 | if len(result) == 0: |
| 124 | print "No Flows found" |
| 125 | return; |
| 126 | |
| 127 | parsedResult = json.loads(result) |
| 128 | debug("parsed %s" % parsedResult) |
| 129 | except: |
| 130 | log_error("Controller IF has issue") |
| 131 | exit(1) |
| 132 | |
| 133 | for flowPath in parsedResult: |
| 134 | print_flow_path(flowPath) |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 135 | |
| 136 | if __name__ == "__main__": |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame^] | 137 | usage_msg1 = "Usage:\n" |
| 138 | usage_msg2 = "%s <flow_id> : Print flow with Flow ID of <flow_id>\n" % (sys.argv[0]) |
| 139 | usage_msg3 = " all : Print all flows\n" |
| 140 | usage_msg4 = " installer <installer-id> <src-dpid> <src-port> <dest-dpid> <dest-port>\n" |
| 141 | usage_msg5 = " endpoints <src-dpid> <src-port> <dest-dpid> <dest-port>" |
| 142 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5; |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 143 | |
| 144 | # app.debug = False; |
| 145 | |
| 146 | # Usage info |
| 147 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 148 | print(usage_msg) |
| 149 | exit(0) |
| 150 | |
| 151 | # Check arguments |
| 152 | if len(sys.argv) < 2: |
| 153 | log_error(usage_msg) |
| 154 | exit(1) |
| 155 | |
| 156 | # Do the work |
Pavlin Radoslavov | 706df05 | 2013-03-06 10:49:07 -0800 | [diff] [blame^] | 157 | if sys.argv[1] == "all": |
| 158 | get_all_flow_paths() |
| 159 | elif sys.argv[1] == "installer": |
| 160 | if len(sys.argv) < 7: |
| 161 | log_error(usage_msg) |
| 162 | exit(1) |
| 163 | get_installer_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4], |
| 164 | sys.argv[5], sys.argv[6]) |
| 165 | elif sys.argv[1] == "endpoints": |
| 166 | if len(sys.argv) < 6: |
| 167 | log_error(usage_msg) |
| 168 | exit(1) |
| 169 | get_endpoints_flow_paths(sys.argv[2], sys.argv[3], sys.argv[4], |
| 170 | sys.argv[5]) |
| 171 | else: |
| 172 | get_flow_path(sys.argv[1]) |