blob: 51399d54c348d6dff6eb17b1fa5d0389c4d1f470 [file] [log] [blame]
Naoki Shiota7b2ab4a2013-12-04 18:05:39 -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
32# @app.route("/wm/fprog/synchronizer/sync/<dpid>/json")
33# Sample output:
34# "true"
35def synchronize(dpid):
36 try:
37 command = "curl -s \"http://%s:%s/wm/fprog/synchronizer/sync/%s/json\"" % (ControllerIP, ControllerPort, dpid)
38 debug("synchronize %s" % command)
39
40 result = os.popen(command).read()
41 debug("result %s" % result)
42 if result == "false":
43 print "Failed to synchronize"
44 return;
45 except:
46 log_error("Controller IF has issue")
47 exit(1)
48
49 print "Synchronization of switch %s has successfully began" % (dpid)
50
51# @app.route("/wm/fprog/synchronizer/interrupt/<dpid>/json")
52# Sample output:
53# "true"
54def interrupt(dpid):
55 try:
56 command = "curl -s \"http://%s:%s/wm/fprog/synchronizer/interrupt/%s/json\"" % (ControllerIP, ControllerPort, dpid)
57 debug("interrupt %s" % command)
58
59 result = os.popen(command).read()
60 debug("result %s" % result)
61 if result == "false":
62 print "Failed to interrupt synchronization"
63 return;
64 except:
65 log_error("Controller IF has issue")
66 exit(1)
67
68 print "Synchronization of switch %s has successfully interrupted" % (dpid)
69
70
71if __name__ == "__main__":
72 usage_msg1 = "Usage:\n"
73 usage_msg2 = "%s sync <dpid> : Start synchronization of the switch\n" % (sys.argv[0])
74 usage_msg3 = " interrupt <dpid> : Interrupt synchronization of the switch\n"
75 usage_msg = usage_msg1 + usage_msg2 + usage_msg3;
76
77 app.debug = True;
78
79 # Usage info
80 if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
81 print(usage_msg)
82 exit(0)
83
84 # Check arguments
85 if len(sys.argv) < 2:
86 log_error(usage_msg)
87 exit(1)
88
89 # Do the work
90 if sys.argv[1] == "sync":
91 if len(sys.argv) < 3:
92 log_error(usage_msg)
93 exit(1)
94 synchronize(sys.argv[2])
95 elif sys.argv[1] == "interrupt":
96 if len(sys.argv) < 3:
97 log_error(usage_msg)
98 exit(1)
99 interrupt(sys.argv[2])
100 else:
101 log_error(usage_msg)
102 exit(1)
103