Naoki Shiota | 2a35b44 | 2013-11-26 19:17:38 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 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/fprog/setrate/<dpid>/<rate>/json") |
| 33 | # Sample output: |
| 34 | # "true" |
| 35 | |
| 36 | |
| 37 | def set_rate(dpid,rate): |
| 38 | try: |
| 39 | command = "curl -s \"http://%s:%s/wm/fprog/setrate/%s/%s/json\"" % (ControllerIP, ControllerPort, dpid, rate) |
| 40 | debug("set_rate %s" % command) |
| 41 | |
| 42 | result = os.popen(command).read() |
| 43 | debug("result %s" % result) |
| 44 | if result == "false": |
| 45 | print "Failed to set rate" |
| 46 | return; |
| 47 | except: |
| 48 | log_error("Controller IF has issue") |
| 49 | exit(1) |
| 50 | |
| 51 | print "Sending rate to %s is successfully set to %s" % (dpid, rate) |
| 52 | |
| 53 | def suspend(dpid): |
| 54 | try: |
| 55 | command = "curl -s \"http://%s:%s/wm/fprog/suspend/%s/json\"" % (ControllerIP, ControllerPort, dpid) |
| 56 | debug("suspend %s" % command) |
| 57 | |
| 58 | result = os.popen(command).read() |
| 59 | debug("result %s" % result) |
| 60 | if result == "false": |
| 61 | print "Failed to suspend" |
| 62 | return; |
| 63 | except: |
| 64 | log_error("Controller IF has issue") |
| 65 | exit(1) |
| 66 | |
| 67 | print "DPID %s is successfully suspended" % dpid |
| 68 | |
| 69 | def resume(dpid): |
| 70 | try: |
| 71 | command = "curl -s \"http://%s:%s/wm/fprog/resume/%s/json\"" % (ControllerIP, ControllerPort, dpid) |
| 72 | debug("resume %s" % command) |
| 73 | |
| 74 | result = os.popen(command).read() |
| 75 | debug("result %s" % result) |
| 76 | if result == "false": |
| 77 | print "Failed to resume" |
| 78 | return; |
| 79 | except: |
| 80 | log_error("Controller IF has issue") |
| 81 | exit(1) |
| 82 | |
| 83 | print "DPID %s is successfully resumed" % dpid |
| 84 | |
| 85 | def barrier(dpid): |
| 86 | try: |
| 87 | command = "curl -s \"http://%s:%s/wm/fprog/barrier/%s/json\"" % (ControllerIP, ControllerPort, dpid) |
| 88 | debug("barrier %s" % command) |
| 89 | |
| 90 | result = os.popen(command).read() |
| 91 | debug("result %s" % result) |
| 92 | if result == "false": |
| 93 | print "Failed to send barrier" |
| 94 | return; |
| 95 | except: |
| 96 | log_error("Controller IF has issue") |
| 97 | exit(1) |
| 98 | |
| 99 | print "Barrier reply from %s : %s" % (dpid, result) |
| 100 | |
| 101 | |
| 102 | if __name__ == "__main__": |
| 103 | usage_msg1 = "Usage:\n" |
| 104 | usage_msg2 = "%s rate <dpid> <rate> : Set sending rate[bytes/ms] to the switch\n" % (sys.argv[0]) |
| 105 | usage_msg3 = " suspend <dpid> : Suspend sending message to the switch\n" |
| 106 | usage_msg4 = " resume <dpid> : Resume sending message to the switch\n" |
| 107 | usage_msg5 = " barrier <dpid> : Send barrier message to the switch\n" |
| 108 | usage_msg = usage_msg1 + usage_msg2 + usage_msg3 + usage_msg4 + usage_msg5; |
| 109 | |
| 110 | app.debug = True; |
| 111 | |
| 112 | # Usage info |
| 113 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 114 | print(usage_msg) |
| 115 | exit(0) |
| 116 | |
| 117 | # Check arguments |
| 118 | if len(sys.argv) < 2: |
| 119 | log_error(usage_msg) |
| 120 | exit(1) |
| 121 | |
| 122 | # Do the work |
| 123 | if sys.argv[1] == "rate": |
| 124 | if len(sys.argv) < 4: |
| 125 | log_error(usage_msg) |
| 126 | exit(1) |
| 127 | set_rate(sys.argv[2], sys.argv[3]) |
| 128 | elif sys.argv[1] == "suspend": |
| 129 | if len(sys.argv) < 3: |
| 130 | log_error(usage_msg) |
| 131 | exit(1) |
| 132 | suspend(sys.argv[2]) |
| 133 | elif sys.argv[1] == "resume": |
| 134 | if len(sys.argv) < 3: |
| 135 | log_error(usage_msg) |
| 136 | exit(1) |
| 137 | resume(sys.argv[2]) |
| 138 | elif sys.argv[1] == "barrier": |
| 139 | if len(sys.argv) < 3: |
| 140 | log_error(usage_msg) |
| 141 | exit(1) |
| 142 | barrier(sys.argv[2]) |
| 143 | else: |
| 144 | log_error(usage_msg) |
| 145 | exit(1) |