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