Add DistributedLock test commands and STC tests

Change-Id: If410a91418fe1749e738cdc12494859af1c74245
(cherry picked from commit 2c9b159066a564b3ee61802819d81ecadc2aef4a)
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
index a83c7db..3296046 100644
--- a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java
@@ -27,6 +27,7 @@
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
 import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.DistributedLock;
 import org.onosproject.store.service.EventuallyConsistentMap;
 import org.onosproject.store.service.LeaderElector;
 import org.onosproject.store.service.StorageService;
@@ -35,7 +36,6 @@
 
 import static org.slf4j.LoggerFactory.getLogger;
 
-
 /**
  * Simple application to test distributed primitives.
  */
@@ -56,6 +56,7 @@
 
     private final Map<String, EventuallyConsistentMap<String, String>> maps = Maps.newConcurrentMap();
     private final Map<String, LeaderElector> electors = Maps.newConcurrentMap();
+    private final Map<String, DistributedLock> locks = Maps.newConcurrentMap();
 
     @Activate
     protected void activate() {
@@ -90,8 +91,21 @@
      */
     public LeaderElector getLeaderElector(String name) {
         return electors.computeIfAbsent(name, n -> storageService.leaderElectorBuilder()
-                .withName(name)
-                .build()
-                .asLeaderElector());
+            .withName(name)
+            .build()
+            .asLeaderElector());
+    }
+
+    /**
+     * Returns a lock instance by name.
+     *
+     * @param name the lock name
+     * @return the lock
+     */
+    public DistributedLock getLock(String name) {
+        return locks.computeIfAbsent(name, n -> storageService.lockBuilder()
+            .withName(name)
+            .build()
+            .asLock());
     }
 }
diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/DistributedLockTestCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/DistributedLockTestCommand.java
new file mode 100644
index 0000000..9e23cb2
--- /dev/null
+++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/DistributedLockTestCommand.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * 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.
+ */
+package org.onosproject.distributedprimitives.cli;
+
+import java.time.Duration;
+
+import org.apache.karaf.shell.commands.Argument;
+import org.apache.karaf.shell.commands.Command;
+import org.onosproject.cli.AbstractShellCommand;
+import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
+import org.onosproject.store.service.DistributedLock;
+
+@Command(scope = "onos", name = "lock-test",
+    description = "DistributedLock test cli fixture")
+public class DistributedLockTestCommand extends AbstractShellCommand {
+    @Argument(index = 0, name = "name",
+        description = "lock name",
+        required = true,
+        multiValued = false)
+    String name = null;
+
+    @Argument(index = 1, name = "operation",
+        description = "operation",
+        required = true,
+        multiValued = false)
+    String operation = null;
+
+    @Argument(index = 2, name = "durationMillis",
+        description = "lock attempt duration in milliseconds",
+        required = false,
+        multiValued = false)
+    Long durationMillis = null;
+
+    DistributedLock lock;
+
+    @Override
+    protected void execute() {
+        DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
+        lock = test.getLock(name);
+        if ("lock".equals(operation)) {
+            lock.lock();
+        } else if ("tryLock".equals(operation)) {
+            if (durationMillis == null) {
+                print("%b", lock.tryLock().isPresent());
+            } else {
+                print("%b", lock.tryLock(Duration.ofMillis(durationMillis)).isPresent());
+            }
+        } else if ("unlock".equals(operation)) {
+            lock.unlock();
+        }
+    }
+}
diff --git a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
index ce02720..30401a0 100644
--- a/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
+++ b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml
@@ -52,6 +52,9 @@
         <command>
             <action class="org.onosproject.distributedprimitives.cli.LeaderElectorTestCommand"/>
         </command>
+        <command>
+            <action class="org.onosproject.distributedprimitives.cli.DistributedLockTestCommand"/>
+        </command>
     </command-bundle>
 
 </blueprint>
diff --git a/tools/test/bin/onos-disrupt b/tools/test/bin/onos-disrupt
index c8e343f..3829250 100755
--- a/tools/test/bin/onos-disrupt
+++ b/tools/test/bin/onos-disrupt
@@ -18,7 +18,7 @@
     echo "    ARGS             The command arguments"
     echo "    [-h | --help]   Print this help"
     echo
-    echo "COMMAND:  <crash | partition | partition-halves | partition-bridge | heal | delay | drop | reorder | duplicate | corrupt | restore>"
+    echo "COMMAND:  <crash | partition | isolate | partition-halves | partition-bridge | heal | delay | drop | reorder | duplicate | corrupt | restore>"
     echo
     echo "               crash    Crashes the given host using \"kill -9\""
     echo
@@ -46,6 +46,14 @@
     echo "                            $command_name 10.127.10.111 partition 10.127.10.111"
     echo "                            $command_name 10.127.10.111 partition 10.127.10.112 10.127.10.113"
     echo
+    echo "             isolate    Isolate the given host from all other hosts"
+    echo
+    echo "                        Syntax:"
+    echo "                            $command_name <HOST> isolate"
+    echo
+    echo "                        Examples:"
+    echo "                            $command_name 10.127.10.111 isolate"
+    echo
     echo "    partition-halves    Splits the cluster into two partitions - this is only recommended for clusters with an odd number of nodes"
     echo
     echo "                        Syntax:"
@@ -306,6 +314,25 @@
         done
         ;;
 
+    # Isolates the node from all other nodes
+    isolate)
+
+        # Default to ONOS nodes.
+        if [ -z "${1}" ]; then
+            nodes=$(onos_nodes)
+        else
+            nodes="${*}"
+        fi
+
+        # Iterate through all ONOS nodes and partition the host from them.
+        for node in ${nodes}; do
+            if [[ $node != $host ]]; then
+                partition ${host} ${node}
+                partition ${node} ${host}
+            fi
+        done
+        ;;
+
     # Splits the cluster into two halves, preserving the provided host as a bridge between the two halves.
     partition-bridge)
 
@@ -338,7 +365,9 @@
 
         # Iterate through all provided nodes and heal partitions.
         for node in $nodes; do
-            heal ${host} ${node}
+            if [[ $node != $host ]]; then
+                heal ${host} ${node}
+            fi
         done
         ;;
 
diff --git a/tools/test/scenarios/dist-lock.xml b/tools/test/scenarios/dist-lock.xml
new file mode 100644
index 0000000..cf9ec3c
--- /dev/null
+++ b/tools/test/scenarios/dist-lock.xml
@@ -0,0 +1,62 @@
+<!--
+  ~ Copyright 2018-present Open Networking Foundation
+  ~
+  ~ 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="distributed-lock-test"
+          description="ONOS DistributedLock distributed primitive Test">
+    <group name="Distributed-Primitive-Lock" if="${OC2}">
+
+        <step name="Distributed-Primitive-Lock.Test-Lock-1"
+              exec="onos ${OC1} lock-test test-lock lock"/>
+
+        <step name="Distributed-Primitive-Lock.Test-Lock-2"
+              exec="onos ${OC2} lock-test test-lock lock"
+              requires="Distributed-Primitive-Lock.Test-Lock-1"/>
+
+        <step name="Distributed-Primitive-Lock.Test-Unlock-1"
+              exec="onos ${OC1} lock-test test-lock unlock"
+              requires="Distributed-Primitive-Lock.Test-Lock-1"/>
+
+        <step name="Distributed-Primitive-Lock.Test-TryLock-3"
+              exec="onos-execute-expect ${OC1} lock-test test-lock tryLock --expect false"
+              requires="Distributed-Primitive-Lock.Test-Unlock-1"/>
+
+        <step name="Distributed-Primitive-Lock.Test-TryLock-4"
+              exec="onos-execute-expect ${OC1} lock-test test-lock tryLock 5000 --expect true"
+              requires="Distributed-Primitive-Lock.Test-TryLock-3"/>
+
+        <step name="Distributed-Primitive-Lock.Test-Unlock-2"
+              exec="onos ${OC2} lock-test test-lock unlock"
+              requires="Distributed-Primitive-Lock.Test-TryLock-3"/>
+
+        <step name="Distributed-Primitive-Lock.Test-Unlock-4"
+              exec="onos ${OC2} lock-test test-lock unlock"
+              requires="Distributed-Primitive-Lock.Test-Unlock-2"/>
+
+        <group name="Distributed-Primitive-Lock.Check-Log-Exceptions"
+               requires="Distributed-Primitive-Lock.Test-Unlock-4">
+            <parallel var="${OC#}">
+                <step name="Distributed-Primitive-Lock.Check-Log-Exceptions-${#}"
+                      exec="onos-check-logs ${OC#}"/>
+            </parallel>
+        </group>
+
+    </group>
+
+    <group name="Single-Node-NA" unless="${OC2}">
+        <step name="No-Op" exec="echo Scenario not supported for this cell size"/>
+    </group>
+</scenario>
+
diff --git a/tools/test/scenarios/dist-test.xml b/tools/test/scenarios/dist-test.xml
index 9f1e6d4..b0b9a17 100644
--- a/tools/test/scenarios/dist-test.xml
+++ b/tools/test/scenarios/dist-test.xml
@@ -38,6 +38,9 @@
             <import file="${ONOS_SCENARIOS}/dist-leader.xml"/>
             <dependency name="Distributed-Primitive-Leader"/>
 
+            <import file="${ONOS_SCENARIOS}/dist-lock.xml"/>
+            <dependency name="Distributed-Primitive-Lock"/>
+
             <import file="${ONOS_SCENARIOS}/dist-work-queue.xml"/>
             <dependency name="Distributed-Primitive-WorkQueue"/>