blob: ef1d9bd45cc19aa8ccbef304f57d4c2c4af59f29 [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
Naoki Shiota862cc3b2013-12-13 15:42:50 -080032# @app.route("/wm/onos/flowprogrammer/synchronizer/sync/<dpid>/json")
Naoki Shiota7b2ab4a2013-12-04 18:05:39 -080033# Sample output:
34# "true"
35def synchronize(dpid):
36 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -080037 command = "curl -s \"http://%s:%s/wm/onos/flowprogrammer/synchronizer/sync/%s/json\"" % (ControllerIP, ControllerPort, dpid)
Naoki Shiota7b2ab4a2013-12-04 18:05:39 -080038 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
Naoki Shiota862cc3b2013-12-13 15:42:50 -080051# @app.route("/wm/onos/flowprogrammer/synchronizer/interrupt/<dpid>/json")
Naoki Shiota7b2ab4a2013-12-04 18:05:39 -080052# Sample output:
53# "true"
54def interrupt(dpid):
55 try:
Naoki Shiota862cc3b2013-12-13 15:42:50 -080056 command = "curl -s \"http://%s:%s/wm/onos/flowprogrammer/synchronizer/interrupt/%s/json\"" % (ControllerIP, ControllerPort, dpid)
Naoki Shiota7b2ab4a2013-12-04 18:05:39 -080057 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