STC scenario for testing network related REST APIs

Tests creating, fetching, and removing Intents via REST API
Tests fetching flows via REST API
Tests fetching links via REST API
Tests fetching hosts via REST API

Change-Id: Ib6aa5138e544d869ba46183ba21591c8a73ba367
diff --git a/tools/test/scenarios/net-rest/create-intent.py b/tools/test/scenarios/net-rest/create-intent.py
new file mode 100755
index 0000000..4e5d4f6
--- /dev/null
+++ b/tools/test/scenarios/net-rest/create-intent.py
@@ -0,0 +1,49 @@
+#! /usr/bin/env python
+
+import requests
+
+from requests.auth import HTTPBasicAuth
+import sys
+
+
+
+if len(sys.argv) != 7:
+    print "usage: create-intent onos-node name ingressDevice ingressPort egressDevice egressPort"
+    sys.exit(1)
+
+node = sys.argv[1]
+name = sys.argv[2]
+ingress = sys.argv[3]
+ingressPort = sys.argv[4]
+egress = sys.argv[5]
+egressPort = sys.argv[6]
+
+intentJsonTemplate = \
+    '{{' + \
+        '"type": "PointToPointIntent",' + \
+        '"appId": "org.onosproject.cli",' + \
+        '"ingressPoint": {{' + \
+        '    "device": "{}",' + \
+        '    "port": "{}"' + \
+        '}},' + \
+        '"egressPoint": {{' + \
+        '    "device": "{}",' + \
+        '    "port": "{}"' + \
+        '}}' + \
+    '}}'
+
+intentJson = intentJsonTemplate.format(ingress, ingressPort, egress, egressPort)
+intentRequest = requests.post('http://' + node + ':8181/onos/v1/intents/',
+                              auth=HTTPBasicAuth('onos', 'rocks'),
+                              data=intentJson)
+
+if intentRequest.status_code != 201:
+    print intentRequest.text
+    sys.exit(1)
+
+location = intentRequest.headers["location"]
+print "@stc " + name + "Location=" + location
+sys.exit(0)
+
+
+
diff --git a/tools/test/scenarios/net-rest/find-device.py b/tools/test/scenarios/net-rest/find-device.py
new file mode 100755
index 0000000..430e18a
--- /dev/null
+++ b/tools/test/scenarios/net-rest/find-device.py
@@ -0,0 +1,39 @@
+#! /usr/bin/env python
+
+import requests
+import sys
+import urllib
+
+from requests.auth import HTTPBasicAuth
+
+if len(sys.argv) != 4:
+    print "usage: find-device onos-node name device-id"
+    sys.exit(1)
+
+node = sys.argv[1]
+name = sys.argv[2]
+id = sys.argv[3]
+
+deviceRequest = requests.get('http://' + node + ':8181/onos/v1/devices/' +
+                             urllib.quote_plus(id),
+                             auth=HTTPBasicAuth('onos', 'rocks'))
+
+if deviceRequest.status_code != 200:
+    print deviceRequest.text
+    sys.exit(1)
+
+deviceJson = deviceRequest.json()
+
+print "@stc " + name + "Id=" + deviceJson["id"]
+print "@stc " + name + "Type=" + deviceJson["type"]
+print "@stc " + name + "Available=" + str(deviceJson["available"])
+channelId = deviceJson["annotations"]["channelId"]
+channelIdWords = channelId.split(':')
+print "@stc " + name + "IpAddress=" + channelIdWords[0]
+
+sys.exit(0)
+
+
+
+
+
diff --git a/tools/test/scenarios/net-rest/find-flow.py b/tools/test/scenarios/net-rest/find-flow.py
new file mode 100755
index 0000000..a2f2e4d
--- /dev/null
+++ b/tools/test/scenarios/net-rest/find-flow.py
@@ -0,0 +1,40 @@
+#! /usr/bin/env python
+
+import requests
+import sys
+
+from requests.auth import HTTPBasicAuth
+
+if len(sys.argv) != 4:
+    print "usage: find-flow onos-node name device-id"
+    sys.exit(1)
+
+node = sys.argv[1]
+name = sys.argv[2]
+deviceId = sys.argv[3]
+
+flowsRequest = requests.get('http://' + node + ':8181/onos/v1/flows/' + deviceId,
+                            auth=HTTPBasicAuth('onos', 'rocks'))
+
+if flowsRequest.status_code != 200:
+    print flowsRequest.text
+    sys.exit(1)
+
+flowsJson = flowsRequest.json()
+
+for flow in flowsJson["flows"]:
+    if deviceId == flow["deviceId"]:
+        for criterion in flow["selector"]["criteria"]:
+            if criterion["type"] == 'IN_PORT' and criterion["port"] > 0:
+                for instruction in flow["treatment"]["instructions"]:
+                    if instruction["port"] > 0 and instruction["type"] == 'OUTPUT':
+                        print "@stc " + name + "FlowState=" + flow["state"]
+                        print "@stc " + name + "FlowId=" + flow["id"]
+                        print "@stc " + name + "FlowPort=" + str(instruction["port"])
+                        sys.exit(0)
+
+sys.exit(1)
+
+
+
+
diff --git a/tools/test/scenarios/net-rest/find-host.py b/tools/test/scenarios/net-rest/find-host.py
new file mode 100755
index 0000000..e87a409
--- /dev/null
+++ b/tools/test/scenarios/net-rest/find-host.py
@@ -0,0 +1,36 @@
+#! /usr/bin/env python
+
+import requests
+import sys
+import urllib
+
+from requests.auth import HTTPBasicAuth
+
+if len(sys.argv) != 4:
+    print "usage: find-host onos-node name device-id"
+    sys.exit(1)
+
+node = sys.argv[1]
+name = sys.argv[2]
+id = sys.argv[3]
+
+hostRequest = requests.get('http://' + node + ':8181/onos/v1/hosts/' +
+                           urllib.quote_plus(id),
+                           auth=HTTPBasicAuth('onos', 'rocks'))
+
+if hostRequest.status_code != 200:
+    print hostRequest.text
+    sys.exit(1)
+
+hostJson = hostRequest.json()
+
+print "@stc " + name + "Id=" + hostJson["id"]
+print "@stc " + name + "Mac=" + hostJson["mac"]
+print "@stc " + name + "IpAddress=" + hostJson["ipAddresses"][0]
+
+sys.exit(0)
+
+
+
+
+
diff --git a/tools/test/scenarios/net-rest/find-link.py b/tools/test/scenarios/net-rest/find-link.py
new file mode 100755
index 0000000..9ac6e35
--- /dev/null
+++ b/tools/test/scenarios/net-rest/find-link.py
@@ -0,0 +1,45 @@
+#! /usr/bin/env python
+
+import requests
+import sys
+
+from requests.auth import HTTPBasicAuth
+
+if len(sys.argv) != 7:
+    print "usage: find-link onos-node name src-device-id src-port dst-device-id dst-port"
+    sys.exit(1)
+
+node = sys.argv[1]
+name = sys.argv[2]
+srcDeviceId = sys.argv[3]
+srcPort = sys.argv[4]
+dstDeviceId = sys.argv[5]
+dstPort = sys.argv[6]
+
+
+linksRequest = requests.get('http://' + node + ':8181/onos/v1/links?device=' +
+                            srcDeviceId + '&port=' + srcPort,
+                            auth=HTTPBasicAuth('onos', 'rocks'))
+
+if linksRequest.status_code != 200:
+    print linksRequest.text
+    sys.exit(1)
+
+linksJson = linksRequest.json()
+
+for link in linksJson["links"]:
+    if srcDeviceId == link["src"]["device"]:
+        if dstDeviceId == link["dst"]["device"]:
+            print "@stc " + name + "SrcDevice=" + link["src"]["device"]
+            print "@stc " + name + "SrcPort=" + link["src"]["port"]
+            print "@stc " + name + "DstDevice=" + link["dst"]["device"]
+            print "@stc " + name + "DstPort=" + link["dst"]["port"]
+            print "@stc " + name + "Type=" + link["type"]
+            print "@stc " + name + "State=" + link["state"]
+            sys.exit(0)
+
+sys.exit(1)
+
+
+
+