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/bin/find-intent.py b/tools/test/bin/find-intent.py
deleted file mode 100755
index e785a69..0000000
--- a/tools/test/bin/find-intent.py
+++ /dev/null
@@ -1,19 +0,0 @@
-#! /usr/bin/env python
-
-import requests
-
-from requests.auth import HTTPBasicAuth
-
-r = requests.get('http://192.168.56.101:8181/onos/v1/flows', auth=HTTPBasicAuth('onos', 'rocks'))
-deviceId = "of:0000000000000001"
-port = 4
-flowsJson = r.json()
-
-for flow in flowsJson["flows"]:
-    if deviceId == flow["deviceId"]:
-        if flow["treatment"]["instructions"][0]["port"] == port:
-            print flow
-
-
-
-
diff --git a/tools/test/scenarios/net-rest.xml b/tools/test/scenarios/net-rest.xml
new file mode 100644
index 0000000..a066c86
--- /dev/null
+++ b/tools/test/scenarios/net-rest.xml
@@ -0,0 +1,169 @@
+<!--
+  ~ Copyright 2015 Open Networking Laboratory
+  ~
+  ~ Licensed under the Apache License, Version 2.0 (the "License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the License at
+  ~
+  ~     http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distributed under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  -->
+<scenario name="net-rest"
+          description="Network REST API test">
+    <!-- TODO: parametrize this via recipes -->
+    <group name="Net-REST">
+
+        <!-- Make sure that reactive forwarding is off -->
+        <step name="Net-REST.Uninstall-Reactive-Forwarding"
+              exec="onos ${OC1} app deactivate org.onosproject.fwd org.onosproject.ifwd"/>
+        <step name="Net-REST.Check-Apps" requires="^"
+              exec="onos-check-apps ${OC1} fwd,ifwd excludes"/>
+
+        <!-- Force discovery of hosts -->
+        <step name="Net-REST.Find-Host-1" requires="^"
+              exec="onos-mininet sendAndExpect h1 ping -c1 -w1 h4 --expect ."/>
+        <step name="Net-REST.Find-Host-2" requires="^"
+              exec="onos-mininet sendAndExpect h4 ping -c1 -w1 h1 --expect ."/>
+
+        <!-- Verify some links using the REST API -->
+        <step name="Net-REST.Query-Link-1" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-link.py
+                    ${OC1} link1 of:0000000000000001 5 of:0000000000000006 4"/>
+        <step name="Net-REST.Validate-Link-1-State" requires="^"
+              exec="test '${link1State}' == 'ACTIVE'"/>
+        <step name="Net-REST.Validate-Link-1-Type" requires="^"
+              exec="test '${link1Type}' == 'DIRECT'"/>
+        <step name="Net-REST.Validate-Link-1-Src-Device" requires="^"
+              exec="test '${link1SrcDevice}' == 'of:0000000000000001'"/>
+        <step name="Net-REST.Validate-Link-1-Dst-Device" requires="^"
+              exec="test '${link1DstDevice}' == 'of:0000000000000006'"/>
+        <step name="Net-REST.Validate-Link-1-Src-Port" requires="^"
+              exec="test '${link1SrcPort}' == '5'"/>
+        <step name="Net-REST.Validate-Link-1-Dst-Port" requires="^"
+              exec="test '${link1DstPort}' == '4'"/>
+
+        <step name="Net-REST.Query-Link-2" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-link.py
+                    ${OC1} link2 of:0000000000000019 7 of:0000000000000006 3"/>
+        <step name="Net-REST.Validate-Link-2-State" requires="^"
+              exec="test '${link2State}' == 'ACTIVE'"/>
+        <step name="Net-REST.Validate-Link-2-Type" requires="^"
+              exec="test '${link2Type}' == 'DIRECT'"/>
+        <step name="Net-REST.Validate-Link-2-Src-Device" requires="^"
+              exec="test '${link2SrcDevice}' == 'of:0000000000000019'"/>
+        <step name="Net-REST.Validate-Link-2-Dst-Device" requires="^"
+              exec="test '${link2DstDevice}' == 'of:0000000000000006'"/>
+        <step name="Net-REST.Validate-Link-2-Src-Port" requires="^"
+              exec="test '${link2SrcPort}' == '7'"/>
+        <step name="Net-REST.Validate-Link-2-Dst-Port" requires="^"
+              exec="test '${link2DstPort}' == '3'"/>
+
+        <!-- Verify the hosts using the REST API -->
+        <step name="Net-REST.Query-Host-1" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-host.py
+                    ${OC1} host1 00:00:00:00:00:01/-1"/>
+        <step name="Net-REST.Validate-Host-1-Id" requires="^"
+              exec="test '${host1Id}' == '00:00:00:00:00:01/-1'"/>
+        <step name="Net-REST.Validate-Host-1-Mac" requires="^"
+              exec="test '${host1Mac}' == '00:00:00:00:00:01'"/>
+        <step name="Net-REST.Validate-Host-1-Ip" requires="^"
+              exec="test '${host1IpAddress}' == '10.0.0.1'"/>
+
+        <step name="Net-REST.Query-Host-2" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-host.py
+                    ${OC1} host2 00:00:00:00:00:04/-1"/>
+        <step name="Net-REST.Validate-Host-2-Id" requires="^"
+              exec="test '${host2Id}' == '00:00:00:00:00:04/-1'"/>
+        <step name="Net-REST.Validate-Host-2-Mac" requires="^"
+              exec="test '${host2Mac}' == '00:00:00:00:00:04'"/>
+        <step name="Net-REST.Validate-Host-2-Ip" requires="^"
+              exec="test '${host2IpAddress}' == '10.0.0.4'"/>
+
+        <!-- Verify a switch using the REST API -->
+        <step name="Net-REST.Query-Dev-1" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-device.py
+                    ${OC1} dev1 of:0000000000000014"/>
+        <step name="Net-REST.Validate-Dev1-Id" requires="^"
+              exec="test '${dev1Id}' == 'of:0000000000000014'"/>
+        <step name="Net-REST.Validate-Dev1-Type" requires="^"
+              exec="test '${dev1Type}' == 'SWITCH'"/>
+        <step name="Net-REST.Validate-Dev1-Available" requires="^"
+              exec="test '${dev1Available}' == 'True'"/>
+        <step name="Net-REST.Validate-Dev1-ChannelId" requires="^"
+              exec="test '${dev1IpAddress}' == '192.168.56.102'"/>
+
+        <!-- Use REST API to create a point to point intent in each direction -->
+        <step name="Net-REST.Create-Intent-XY" requires="Net-REST.Find-Host-2"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/create-intent.py
+                    ${OC1} xy of:0000000000000001 1 of:0000000000000004 1"/>
+        <step name="Net-REST.Create-Intent-YX" requires="^"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/create-intent.py
+                    ${OC1} yx of:0000000000000004 1 of:0000000000000001 1"/>
+
+        <!-- Check that the intents can be fetched via the REST API -->
+        <step name="Net-REST.Validate-Intent-XY-Installed"
+              requires="Net-REST.Create-Intent-YX"
+              exec="curl -f -uonos:rocks ${xyLocation}"/>
+        <step name="Net-REST.Validate-Intent-YX-Installed"
+              requires="Net-REST.Create-Intent-YX"
+              exec="curl -f -uonos:rocks ${yxLocation}"/>
+
+        <!-- Use REST API to query flows created by the intents -->
+        <step name="Net-REST.Validate-Intent-XY-Flow-Installed"
+              requires="Net-REST.Create-Intent-YX"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-flow.py
+                    ${OC1} dev1out of:0000000000000001"/>
+        <step name="Net-REST.Validate-XY-Flow-State" requires="^"
+              exec="test '${dev1outFlowState}' == 'ADDED' -o '${dev1outFlowState}' == 'PENDING_ADD'"/>
+        <step name="Net-REST.Validate-XY-Flow-Port" requires="^"
+              exec="test ${dev1outFlowPort} -ge 1 -a ${dev1outFlowPort} -le 5"/>
+
+        <step name="Net-REST.Validate-Intent-YX-Flow-Installed" requires="Net-REST.Create-Intent-YX"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-flow.py
+                    ${OC1} dev4out of:0000000000000004"/>
+        <step name="Net-REST.Validate-YX-Flow-State" requires="^"
+              exec="test '${dev4outFlowState}' == 'ADDED' -o '${dev4outFlowState}' == 'PENDING_ADD'"/>
+        <step name="Net-REST.Validate-YX-Flow-Port" requires="^"
+              exec="test '${dev4outFlowPort}' == '3'"/>
+
+        <!-- Check that connectivity was established -->
+        <step name="Net-REST.Ping-XY" requires="Net-REST.Create-Intent-XY,Net-REST.Create-Intent-YX"
+              exec="onos-mininet sendAndExpect h1 ping -c1 h4 --expect \ 0% packet loss"/>
+        <step name="Net-REST.Ping-YX" requires="^"
+              exec="onos-mininet sendAndExpect h4 ping -c1 h1 --expect \ 0% packet loss"/>
+
+        <!-- Remove the intents via the REST API -->
+        <step name="Net-REST.Delete-Intent-XY"
+              requires="Net-REST.Validate-Intent-XY-Installed,
+                        Net-REST.Validate-Intent-YX-Installed,
+                        Net-REST.Validate-Intent-XY-Flow-Installed,
+                        Net-REST.Validate-Intent-YX-Flow-Installed,
+                        Net-REST.Ping-XY,
+                        Net-REST.Ping-YX"
+              exec="curl -f -X DELETE -uonos:rocks ${xyLocation}"/>
+        <step name="Net-REST.Delete-Intent-YX" requires="^"
+              exec="curl -f -X DELETE -uonos:rocks ${yxLocation}"/>
+
+        <!-- Use REST API to be sure that flows are no longer installed -->
+        <step name="Net-REST.Validate-XY-Flows-Removed" requires="Net-REST.Delete-Intent-XY"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-flow.py
+                    ${OC1} dev1out of:0000000000000001"
+              env="!"/>
+        <step name="Net-REST.Validate-YX-Flows-Removed" requires="Net-REST.Delete-Intent-YX"
+              exec="${ONOS_ROOT}/tools/test/scenarios/net-rest/find-flow.py
+                    ${OC1} dev4out of:0000000000000004"
+              env="!"/>
+
+        <!-- Check that the deleted intents no longer appear in the REST API -->
+        <step name="Net-REST.Validate-Intent-XY-Removed" requires="Net-REST.Delete-Intent-XY"
+              exec="curl -f -uonos:rocks ${xyLocation}" env="!"/>
+        <step name="Net-REST.Validate-Intent-YX-Removed" requires="Net-REST.Delete-Intent-YX"
+              exec="curl -f -uonos:rocks ${yxLocation}" env="!"/>
+
+    </group>
+</scenario>
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)
+
+
+
+
diff --git a/tools/test/scenarios/net-smoke.xml b/tools/test/scenarios/net-smoke.xml
index 025dd26..b411765 100644
--- a/tools/test/scenarios/net-smoke.xml
+++ b/tools/test/scenarios/net-smoke.xml
@@ -29,7 +29,10 @@
         <import file="${ONOS_SCENARIOS}/net-point-intent.xml"/>
         <dependency name="P2P-Intent-Connectivity" requires="Net-Setup,~Reactive-Forwarding.Net-Link-Down-Up,Host-Intent-Connectivity"/>
 
+        <import file="${ONOS_SCENARIOS}/net-rest.xml"/>
+        <dependency name="Net-REST" requires="Net-Setup,P2P-Intent-Connectivity"/>
+
         <import file="${ONOS_SCENARIOS}/net-teardown.xml"/>
-        <dependency name="Net-Teardown" requires="~Host-Intent-Connectivity,~P2P-Intent-Connectivity"/>
+        <dependency name="Net-Teardown" requires="~Host-Intent-Connectivity,~P2P-Intent-Connectivity,~Net-REST"/>
     </group>
 </scenario>