Naoki Shiota | 7b2ab4a | 2013-12-04 18:05:39 -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 | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 32 | # @app.route("/wm/onos/flowprogrammer/synchronizer/sync/<dpid>/json") |
Naoki Shiota | 7b2ab4a | 2013-12-04 18:05:39 -0800 | [diff] [blame] | 33 | # Sample output: |
| 34 | # "true" |
| 35 | def synchronize(dpid): |
| 36 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 37 | command = "curl -s \"http://%s:%s/wm/onos/flowprogrammer/synchronizer/sync/%s/json\"" % (ControllerIP, ControllerPort, dpid) |
Naoki Shiota | 7b2ab4a | 2013-12-04 18:05:39 -0800 | [diff] [blame] | 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 | |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 51 | # @app.route("/wm/onos/flowprogrammer/synchronizer/interrupt/<dpid>/json") |
Naoki Shiota | 7b2ab4a | 2013-12-04 18:05:39 -0800 | [diff] [blame] | 52 | # Sample output: |
| 53 | # "true" |
| 54 | def interrupt(dpid): |
| 55 | try: |
Naoki Shiota | 862cc3b | 2013-12-13 15:42:50 -0800 | [diff] [blame] | 56 | command = "curl -s \"http://%s:%s/wm/onos/flowprogrammer/synchronizer/interrupt/%s/json\"" % (ControllerIP, ControllerPort, dpid) |
Naoki Shiota | 7b2ab4a | 2013-12-04 18:05:39 -0800 | [diff] [blame] | 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 | |
| 71 | if __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 | |