Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | # -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- |
| 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 | # |
| 16 | # TODO: remove this! We don't use JSON argument here! |
| 17 | # curl http://127.0.0.1:8080/wm/flow/delete/{"value":"0xf"}/json' |
| 18 | # |
| 19 | |
| 20 | ## Global Var ## |
| 21 | ControllerIP="127.0.0.1" |
| 22 | ControllerPort=8080 |
| 23 | |
| 24 | DEBUG=0 |
| 25 | pp = pprint.PrettyPrinter(indent=4) |
| 26 | |
| 27 | app = Flask(__name__) |
| 28 | |
| 29 | ## Worker Functions ## |
| 30 | def log_error(txt): |
| 31 | print '%s' % (txt) |
| 32 | |
| 33 | def debug(txt): |
| 34 | if DEBUG: |
| 35 | print '%s' % (txt) |
| 36 | |
| 37 | # @app.route("/wm/flow/delete/<flow-id>/json") |
| 38 | def delete_flow_path(flow_id): |
| 39 | command = "curl -s \"http://%s:%s/wm/flow/delete/%s/json\"" % (ControllerIP, ControllerPort, flow_id) |
| 40 | debug("delete_flow_path %s" % command) |
| 41 | result = os.popen(command).read() |
| 42 | debug("result %s" % result) |
| 43 | # parsedResult = json.loads(result) |
| 44 | # debug("parsed %s" % parsedResult) |
| 45 | |
| 46 | if __name__ == "__main__": |
Pavlin Radoslavov | 02223ec | 2013-03-14 17:47:26 -0700 | [diff] [blame] | 47 | usage_msg = "Delete flow state from the ONOS Network Map and the switches\n" |
Pavlin Radoslavov | 7be3bac | 2013-03-27 09:59:34 -0700 | [diff] [blame^] | 48 | usage_msg = usage_msg + "Usage: %s <begin-flow-id> <end-flow-id>\n" % (sys.argv[0]) |
| 49 | usage_msg = usage_msg + " %s <flow-id>\n" % (sys.argv[0]) |
| 50 | usage_msg = usage_msg + "\n" |
| 51 | usage_msg = usage_msg + " Arguments:\n" |
| 52 | usage_msg = usage_msg + " <begin-flow-id> <end-flow-id> Delete all flows in the flow ID range\n" |
| 53 | usage_msg = usage_msg + " <flow-id> Delete a single flow with the flow ID\n" |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 54 | |
| 55 | # app.debug = False; |
| 56 | |
| 57 | # Usage info |
| 58 | if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"): |
| 59 | print(usage_msg) |
| 60 | exit(0) |
| 61 | |
| 62 | # Check arguments |
| 63 | if len(sys.argv) < 2: |
| 64 | log_error(usage_msg) |
| 65 | exit(1) |
Pavlin Radoslavov | 2615fc3 | 2013-03-19 22:58:44 -0700 | [diff] [blame] | 66 | begin_flow_id = int(sys.argv[1], 0) |
| 67 | if len(sys.argv) >= 3: |
| 68 | end_flow_id = int(sys.argv[2], 0) |
| 69 | else: |
| 70 | end_flow_id = begin_flow_id |
Pavlin Radoslavov | f4ad989 | 2013-03-04 14:15:19 -0800 | [diff] [blame] | 71 | |
| 72 | # Do the work |
Pavlin Radoslavov | 2615fc3 | 2013-03-19 22:58:44 -0700 | [diff] [blame] | 73 | flow_id = begin_flow_id |
| 74 | while flow_id <= end_flow_id: |
| 75 | delete_flow_path(flow_id) |
| 76 | flow_id = flow_id + 1 |