blob: 2a3528b6b63312e07b3f0e69e97e82750f4820bd [file] [log] [blame]
Naoki Shiota2a35b442013-11-26 19:17:38 -08001#! /usr/bin/env python
2
3
4import pprint
5import os
6import sys
7import subprocess
8import json
9import argparse
10import io
11import time
12
13from flask import Flask, json, Response, render_template, make_response, request
14
15## Global Var ##
16ControllerIP="127.0.0.1"
17ControllerPort=8080
18
19DEBUG=0
20pp = pprint.PrettyPrinter(indent=4)
21
22app = Flask(__name__)
23
24## Worker Functions ##
25def log_error(txt):
26 print '%s' % (txt)
27
28def debug(txt):
29 if DEBUG:
30 print '%s' % (txt)
31
Naoki Shiota51f76ca2013-11-27 14:37:39 -080032# @app.route("/wm/fprog/pusher/setrate/<dpid>/<rate>/json")
Naoki Shiota2a35b442013-11-26 19:17:38 -080033# Sample output:
34# "true"
Naoki Shiota2a35b442013-11-26 19:17:38 -080035def set_rate(dpid,rate):
36 try:
Naoki Shiota51f76ca2013-11-27 14:37:39 -080037 command = "curl -s \"http://%s:%s/wm/fprog/pusher/setrate/%s/%s/json\"" % (ControllerIP, ControllerPort, dpid, rate)
Naoki Shiota2a35b442013-11-26 19:17:38 -080038 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 Shiota51f76ca2013-11-27 14:37:39 -080051# @app.route("/wm/fprog/pusher/suspend/<dpid>/json")
52# Sample output:
53# "true"
Naoki Shiota2a35b442013-11-26 19:17:38 -080054def suspend(dpid):
55 try:
Naoki Shiota51f76ca2013-11-27 14:37:39 -080056 command = "curl -s \"http://%s:%s/wm/fprog/pusher/suspend/%s/json\"" % (ControllerIP, ControllerPort, dpid)
Naoki Shiota2a35b442013-11-26 19:17:38 -080057 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 Shiota51f76ca2013-11-27 14:37:39 -080070# @app.route("/wm/fprog/pusher/resume/<dpid>/json")
71# Sample output:
72# "true"
Naoki Shiota2a35b442013-11-26 19:17:38 -080073def resume(dpid):
74 try:
Naoki Shiota51f76ca2013-11-27 14:37:39 -080075 command = "curl -s \"http://%s:%s/wm/fprog/pusher/resume/%s/json\"" % (ControllerIP, ControllerPort, dpid)
Naoki Shiota2a35b442013-11-26 19:17:38 -080076 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 Shiota51f76ca2013-11-27 14:37:39 -080089# @app.route("/wm/fprog/pusher/barrier/<dpid>/json")
90# Sample output:
91# "{"version":1,"type":"BARRIER_REPLY","length":8,"xid":4,"lengthU":8}"
Naoki Shiota2a35b442013-11-26 19:17:38 -080092def barrier(dpid):
93 try:
Naoki Shiota51f76ca2013-11-27 14:37:39 -080094 command = "curl -s \"http://%s:%s/wm/fprog/pusher/barrier/%s/json\"" % (ControllerIP, ControllerPort, dpid)
Naoki Shiota2a35b442013-11-26 19:17:38 -080095 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
109if __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)