Added three Python scripts to add/delete/get a flow that
is stored in the ONOS Network MAP.
diff --git a/web/add_flow.py b/web/add_flow.py
new file mode 100755
index 0000000..5c3b3ab
--- /dev/null
+++ b/web/add_flow.py
@@ -0,0 +1,121 @@
+#! /usr/bin/env python
+# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
+
+import pprint
+import os
+import sys
+import subprocess
+import json
+import argparse
+import io
+import time
+
+from flask import Flask, json, Response, render_template, make_response, request
+
+#
+# curl http://127.0.0.1:8080/wm/topology/route/00:00:00:00:00:00:0a:01/1/00:00:00:00:00:00:0a:04/1/json
+#
+
+## Global Var ##
+ControllerIP="127.0.0.1"
+ControllerPort=8080
+
+DEBUG=0
+pp = pprint.PrettyPrinter(indent=4)
+
+app = Flask(__name__)
+
+## Worker Functions ##
+def log_error(txt):
+ print '%s' % (txt)
+
+def debug(txt):
+ if DEBUG:
+ print '%s' % (txt)
+
+# @app.route("/wm/topology/route/<srcdpid>/<srcport>/<destdpid>/<destport>/json")
+#
+# Sample output:
+# {'dstPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:02'}}, 'srcPort': {'port': {'value': 0}, 'dpid': {'value': '00:00:00:00:00:00:00:01'}}, 'flowEntries': [{'outPort': {'value': 1}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 0}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:01'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}, {'outPort': {'value': 0}, 'flowEntryErrorState': None, 'flowEntryMatch': None, 'flowEntryActions': None, 'inPort': {'value': 9}, 'flowEntryId': None, 'flowEntryUserState': 'FE_USER_UNKNOWN', 'dpid': {'value': '00:00:00:00:00:00:00:02'}, 'flowEntrySwitchState': 'FE_SWITCH_UNKNOWN'}]}
+#
+def shortest_path(v1, p1, v2, p2):
+ try:
+ command = "curl -s http://%s:%s/wm/topology/route/%s/%s/%s/%s/json" % (ControllerIP, ControllerPort, v1, p1, v2, p2)
+ debug("shortest_path %s" % command)
+
+ result = os.popen(command).read()
+ debug("result %s" % result)
+ if len(result) == 0:
+ log_error("No Path found")
+ exit(1);
+
+ parsedResult = json.loads(result)
+ debug("parsed %s" % parsedResult)
+
+ except:
+ log_error("Controller IF has issue")
+ exit(1)
+
+ srcSwitch = parsedResult['srcPort']['dpid']['value'];
+ srcPort = parsedResult['srcPort']['port']['value'];
+ dstSwitch = parsedResult['dstPort']['dpid']['value'];
+ dstPort = parsedResult['dstPort']['port']['value'];
+
+ print "DataPath: (src = %s/%s dst = %s/%s)" % (srcSwitch, srcPort, dstSwitch, dstPort);
+
+ for f in parsedResult['flowEntries']:
+ inPort = f['inPort']['value'];
+ outPort = f['outPort']['value'];
+ dpid = f['dpid']['value']
+ print "FlowEntry: (%s, %s, %s)" % (inPort, dpid, outPort)
+
+ return parsedResult
+
+def add_flow_path(flow_path):
+ try:
+ command = "curl -s -H 'Content-Type: application/json' -d '%s' http://%s:%s/wm/flow/add/json" % (flow_path, ControllerIP, ControllerPort)
+ debug("add_flow_path %s" % command)
+ result = os.popen(command).read()
+ debug("result %s" % result)
+ # parsedResult = json.loads(result)
+ # debug("parsed %s" % parsedResult)
+ except:
+ log_error("Controller IF has issue")
+ exit(1)
+
+if __name__ == "__main__":
+ usage_msg = "Usage: %s <flow-id> <src-dpid> <src-port> <dest-dpid> <dest-port>" % (sys.argv[0])
+
+ # app.debug = False;
+
+ # Usage info
+ if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
+ print(usage_msg)
+ exit(0)
+
+ # Check arguments
+ if len(sys.argv) < 6:
+ log_error(usage_msg)
+ exit(1)
+
+ # Do the work
+ data_path = shortest_path(sys.argv[2], sys.argv[3], sys.argv[4], sys.argv[5]);
+ my_installer_id = 'ONOS-Path-Computation-Python'
+ my_flow_id = sys.argv[1];
+
+ debug("Data Path: %s" % data_path)
+
+ flow_id = {}
+ flow_id['value'] = my_flow_id
+ installer_id = {}
+ installer_id['value'] = my_installer_id
+
+ flow_path = {}
+ flow_path['flowId'] = flow_id
+ flow_path['installerId'] = installer_id
+ flow_path['dataPath'] = data_path
+
+ flow_path_json = json.dumps(flow_path)
+ debug("Flow Path: %s" % flow_path_json)
+
+ add_flow_path(flow_path_json)
diff --git a/web/delete_flow.py b/web/delete_flow.py
new file mode 100755
index 0000000..f6f3d39
--- /dev/null
+++ b/web/delete_flow.py
@@ -0,0 +1,62 @@
+#! /usr/bin/env python
+# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
+
+import pprint
+import os
+import sys
+import subprocess
+import json
+import argparse
+import io
+import time
+
+from flask import Flask, json, Response, render_template, make_response, request
+
+#
+# TODO: remove this! We don't use JSON argument here!
+# curl http://127.0.0.1:8080/wm/flow/delete/{"value":"0xf"}/json'
+#
+
+## Global Var ##
+ControllerIP="127.0.0.1"
+ControllerPort=8080
+
+DEBUG=0
+pp = pprint.PrettyPrinter(indent=4)
+
+app = Flask(__name__)
+
+## Worker Functions ##
+def log_error(txt):
+ print '%s' % (txt)
+
+def debug(txt):
+ if DEBUG:
+ print '%s' % (txt)
+
+# @app.route("/wm/flow/delete/<flow-id>/json")
+def delete_flow_path(flow_id):
+ command = "curl -s \"http://%s:%s/wm/flow/delete/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
+ debug("delete_flow_path %s" % command)
+ result = os.popen(command).read()
+ debug("result %s" % result)
+ # parsedResult = json.loads(result)
+ # debug("parsed %s" % parsedResult)
+
+if __name__ == "__main__":
+ usage_msg = "Usage: %s <flow_id>" % (sys.argv[0])
+
+ # app.debug = False;
+
+ # Usage info
+ if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
+ print(usage_msg)
+ exit(0)
+
+ # Check arguments
+ if len(sys.argv) < 2:
+ log_error(usage_msg)
+ exit(1)
+
+ # Do the work
+ delete_flow_path(sys.argv[1]);
diff --git a/web/get_flow.py b/web/get_flow.py
new file mode 100755
index 0000000..5988978
--- /dev/null
+++ b/web/get_flow.py
@@ -0,0 +1,86 @@
+#! /usr/bin/env python
+# -*- Mode: python; py-indent-offset: 4; tab-width: 8; indent-tabs-mode: t; -*-
+
+import pprint
+import os
+import sys
+import subprocess
+import json
+import argparse
+import io
+import time
+
+from flask import Flask, json, Response, render_template, make_response, request
+
+## Global Var ##
+ControllerIP="127.0.0.1"
+ControllerPort=8080
+
+DEBUG=0
+pp = pprint.PrettyPrinter(indent=4)
+
+app = Flask(__name__)
+
+## Worker Functions ##
+def log_error(txt):
+ print '%s' % (txt)
+
+def debug(txt):
+ if DEBUG:
+ print '%s' % (txt)
+
+# @app.route("/wm/flow/get/<flow-id>/json")
+# Sample output:
+# {"flowId":{"value":"0x5"},"installerId":{"value":"FOOBAR"},"dataPath":{"srcPort":{"dpid":{"value":"00:00:00:00:00:00:00:01"},"port":{"value":0}},"dstPort":{"dpid":{"value":"00:00:00:00:00:00:00:02"},"port":{"value":0}},"flowEntries":[{"flowEntryId":"0x1389","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:01"},"inPort":{"value":0},"outPort":{"value":1},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null},{"flowEntryId":"0x138a","flowEntryMatch":null,"flowEntryActions":null,"dpid":{"value":"00:00:00:00:00:00:00:02"},"inPort":{"value":9},"outPort":{"value":0},"flowEntryUserState":"FE_USER_DELETE","flowEntrySwitchState":"FE_SWITCH_NOT_UPDATED","flowEntryErrorState":null}]}}
+def get_flow_path(flow_id):
+ try:
+ command = "curl -s \"http://%s:%s/wm/flow/get/%s/json\"" % (ControllerIP, ControllerPort, flow_id)
+ debug("get_flow_path %s" % command)
+
+ result = os.popen(command).read()
+ debug("result %s" % result)
+ if len(result) == 0:
+ print "No Flow found"
+ return;
+
+ parsedResult = json.loads(result)
+ debug("parsed %s" % parsedResult)
+ except:
+ log_error("Controller IF has issue")
+ exit(1)
+
+ flowId = parsedResult['flowId']['value'];
+ installerId = parsedResult['installerId']['value'];
+ srcSwitch = parsedResult['dataPath']['srcPort']['dpid']['value'];
+ srcPort = parsedResult['dataPath']['srcPort']['port']['value'];
+ dstSwitch = parsedResult['dataPath']['dstPort']['dpid']['value'];
+ dstPort = parsedResult['dataPath']['dstPort']['port']['value'];
+
+ print "FlowPath: (flowId = %s installerId = %s src = %s/%s dst = %s/%s)" % (flowId, installerId, srcSwitch, srcPort, dstSwitch, dstPort)
+
+ for f in parsedResult['dataPath']['flowEntries']:
+ inPort = f['inPort']['value'];
+ outPort = f['outPort']['value'];
+ dpid = f['dpid']['value']
+ userState = f['flowEntryUserState']
+ switchState = f['flowEntrySwitchState']
+ print "FlowEntry: (%s, %s, %s, %s, %s)" % (inPort, dpid, outPort, userState, switchState)
+
+
+if __name__ == "__main__":
+ usage_msg = "Usage: %s <flow_id>" % (sys.argv[0])
+
+ # app.debug = False;
+
+ # Usage info
+ if len(sys.argv) > 1 and (sys.argv[1] == "-h" or sys.argv[1] == "--help"):
+ print(usage_msg)
+ exit(0)
+
+ # Check arguments
+ if len(sys.argv) < 2:
+ log_error(usage_msg)
+ exit(1)
+
+ # Do the work
+ get_flow_path(sys.argv[1]);