From 6b687cda890b46b38c7c6031daac59ceab4919e5 Mon Sep 17 00:00:00 2001 From: Jon Hall Date: Thu, 23 Apr 2015 20:04:59 -0700 Subject: [PATCH] Test application for counters and sets Application to expose some of the distributed primitives to the cli for system tests. Change-Id: I275c8fb6790135c6150658ca3bb5ce356ea735a7 --- apps/test/distributed-primitives/pom.xml | 70 +++++++++++ .../DistributedPrimitivesTest.java | 57 +++++++++ .../cli/CounterTestIncrementCommand.java | 79 +++++++++++++ .../cli/SetTestAddCommand.java | 81 +++++++++++++ .../cli/SetTestGetCommand.java | 106 +++++++++++++++++ .../cli/SetTestRemoveCommand.java | 109 ++++++++++++++++++ .../cli/package-info.java | 20 ++++ .../distributedprimitives/package-info.java | 20 ++++ .../OSGI-INF/blueprint/shell-config.xml | 33 ++++++ apps/test/pom.xml | 1 + 10 files changed, 576 insertions(+) create mode 100644 apps/test/distributed-primitives/pom.xml create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java create mode 100644 apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java create mode 100644 apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml diff --git a/apps/test/distributed-primitives/pom.xml b/apps/test/distributed-primitives/pom.xml new file mode 100644 index 0000000000..1da9b6a2d0 --- /dev/null +++ b/apps/test/distributed-primitives/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + + org.onosproject + onos-apps-test + 1.2.0-SNAPSHOT + ../pom.xml + + + onos-app-distributed-primitives + bundle + + ONOS app to test distributed primitives + + + org.onosproject.distributedprimitives + + + + + + org.onosproject + onos-api + ${project.version} + test + tests + + + + org.onosproject + onos-cli + ${project.version} + + + org.onosproject + onos-core-dist + 1.2.0-SNAPSHOT + + + org.osgi + org.osgi.core + + + org.apache.karaf.shell + org.apache.karaf.shell.console + + + + + 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 new file mode 100644 index 0000000000..f0892282db --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/DistributedPrimitivesTest.java @@ -0,0 +1,57 @@ +/* + * 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. + */ +package org.onosproject.distributedprimitives; + +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.ReferenceCardinality; +import org.onosproject.core.ApplicationId; +import org.onosproject.core.CoreService; +import org.slf4j.Logger; + +import static org.slf4j.LoggerFactory.getLogger; + + +/** + * Simple application to test distributed primitives. + */ +@Component(immediate = true) +public class DistributedPrimitivesTest { + + private final Logger log = getLogger(getClass()); + + private static final String APP_NAME = "org.onosproject.distributedprimitives"; + private ApplicationId appId; + + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) + protected CoreService coreService; + + + @Activate + protected void activate() { + + log.info("Distributed-Primitives-test app started"); + appId = coreService.registerApplication(APP_NAME); + } + + @Deactivate + protected void deactivate() { + + log.info("Distributed-Primitives-test app Stopped"); + } +} diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java new file mode 100644 index 0000000000..bf64f00f5d --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/CounterTestIncrementCommand.java @@ -0,0 +1,79 @@ +/* + * 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. + */ +package org.onosproject.distributedprimitives.cli; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.commands.Option; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.store.service.AsyncAtomicCounter; +import org.onosproject.store.service.StorageService; +import org.slf4j.Logger; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import static org.slf4j.LoggerFactory.getLogger; + +/** + * CLI command to increment a distributed counter. + */ +@Command(scope = "onos", name = "counter-test-increment", + description = "Increment a distributed counter") +public class CounterTestIncrementCommand extends AbstractShellCommand { + + private final Logger log = getLogger(getClass()); + + @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?", + required = false, multiValued = false) + private boolean inMemory = false; + + @Argument(index = 0, name = "counter", + description = "Counter name", + required = true, multiValued = false) + String counter = null; + + AsyncAtomicCounter atomicCounter; + + + @Override + protected void execute() { + StorageService storageService = get(StorageService.class); + if (inMemory) { + atomicCounter = storageService.atomicCounterBuilder() + .withName(counter) + .withPartitionsDisabled() + .buildAsyncCounter(); + } else { + atomicCounter = storageService.atomicCounterBuilder() + .withName(counter) + .buildAsyncCounter(); + } + + CompletableFuture result = atomicCounter.incrementAndGet(); + try { + print("%s was incremented to %d", counter, result.get(3, TimeUnit.SECONDS)); + } catch (InterruptedException e) { + return; + } catch (ExecutionException e) { + e.printStackTrace(); + } catch (TimeoutException e) { + e.printStackTrace(); + } + } +} diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java new file mode 100644 index 0000000000..0ccc2d3c42 --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestAddCommand.java @@ -0,0 +1,81 @@ +/* + * 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. + */ +package org.onosproject.distributedprimitives.cli; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.onlab.util.KryoNamespace; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.store.serializers.KryoNamespaces; +import org.onosproject.store.service.Serializer; +import org.onosproject.store.service.StorageService; + +import java.util.HashSet; +import java.util.Set; + +/** + * CLI command to add elements to a distributed set. + */ +@Command(scope = "onos", name = "set-test-add", + description = "Add to a distributed set") +public class SetTestAddCommand extends AbstractShellCommand { + + @Argument(index = 0, name = "setName", + description = "set name", + required = true, multiValued = false) + String setName = null; + + @Argument(index = 1, name = "values", + description = "Value(s) to add to the set", + required = true, multiValued = true) + String[] values = null; + + Set set; + Set toAdd = new HashSet(); + + + Serializer serializer = Serializer.using( + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build()); + + + @Override + protected void execute() { + StorageService storageService = get(StorageService.class); + set = storageService.setBuilder() + .withName(setName) + .withSerializer(serializer) + .build(); + + // Add a single element to the set + if (values.length == 1) { + if (set.add(values[0])) { + print("[%s] was added to the set %s", values[0], setName); + } else { + print("[%s] was already in set %s", values[0], setName); + } + } else if (values.length >= 1) { + // Add multiple elements to a set + for (String value : values) { + toAdd.add(value); + } + if (set.addAll(toAdd)) { + print("%s was added to the set %s", toAdd, setName); + } else { + print("%s was already in set %s", toAdd, setName); + } + } + } +} diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java new file mode 100644 index 0000000000..19d3216627 --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestGetCommand.java @@ -0,0 +1,106 @@ +/* + * 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. + */ +package org.onosproject.distributedprimitives.cli; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.commands.Option; +import org.onlab.util.KryoNamespace; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.store.serializers.KryoNamespaces; +import org.onosproject.store.service.Serializer; +import org.onosproject.store.service.StorageService; + +import java.util.HashSet; +import java.util.Set; + +/** + * CLI command to get the elements in a distributed set. + */ +@Command(scope = "onos", name = "set-test-get", + description = "Get the elements in a distributed set") +public class SetTestGetCommand extends AbstractShellCommand { + + @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?", + required = false, multiValued = false) + private boolean size = false; + + @Argument(index = 0, name = "setName", + description = "set name", + required = true, multiValued = false) + String setName = null; + + @Argument(index = 1, name = "values", + description = "Check if the set contains these value(s)", + required = false, multiValued = true) + String[] values = null; + + Set set; + Set toCheck = new HashSet(); + String output = new String(); + + Serializer serializer = Serializer.using( + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build()); + + + @Override + protected void execute() { + StorageService storageService = get(StorageService.class); + set = storageService.setBuilder() + .withName(setName) + .withSerializer(serializer) + .build(); + + // Print the set size + if (size) { + print("There are %d items in set %s:", set.size(), setName); + } else { + print("Items in set %s:", setName); + } + // Print the set + if (set.isEmpty()) { + print("[]"); + } else { + for (String e : set.toArray(new String[0])) { + if (output.isEmpty()) { + output += e; + } else { + output += ", " + e; + } + } + print("[%s]", output); + } + // Check if given values are in the set + if (values.length == 1) { + // contains + if (set.contains(values[0])) { + print("Set %s contains the value %s", setName, values[0]); + } else { + print("Set %s did not contain the value %s", setName, values[0]); + } + } else if (values.length > 1) { + //containsAll + for (String value : values) { + toCheck.add(value); + } + if (set.containsAll(toCheck)) { + print("Set %s contains the the subset %s", setName, toCheck); + } else { + print("Set %s did not contain the the subset %s", setName, toCheck); + } + } + } +} diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java new file mode 100644 index 0000000000..7e3a3e82f6 --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/SetTestRemoveCommand.java @@ -0,0 +1,109 @@ +/* + * 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. + */ +package org.onosproject.distributedprimitives.cli; + +import org.apache.karaf.shell.commands.Argument; +import org.apache.karaf.shell.commands.Command; +import org.apache.karaf.shell.commands.Option; +import org.onlab.util.KryoNamespace; +import org.onosproject.cli.AbstractShellCommand; +import org.onosproject.store.serializers.KryoNamespaces; +import org.onosproject.store.service.Serializer; +import org.onosproject.store.service.StorageService; + +import java.util.HashSet; +import java.util.Set; + +/** + * CLI command to remove elements from a distributed set. + */ +@Command(scope = "onos", name = "set-test-remove", + description = "Remove from a distributed set") +public class SetTestRemoveCommand extends AbstractShellCommand { + + @Option(name = "-r", aliases = "--retain", + description = "Only keep the given values in the set (if they already exist in the set)", + required = false, multiValued = false) + private boolean retain = false; + + @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values", + required = false, multiValued = false) + private boolean clear = false; + + @Argument(index = 0, name = "setName", + description = "set name", + required = true, multiValued = false) + String setName = null; + + @Argument(index = 1, name = "values", + description = "Value(s) to remove from the set", + required = false, multiValued = true) + String[] values = null; + + Set set; + Set givenValues = new HashSet(); + Serializer serializer = Serializer.using( + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build()); + + + @Override + protected void execute() { + StorageService storageService = get(StorageService.class); + set = storageService.setBuilder() + .withName(setName) + .withSerializer(serializer) + .build(); + + if (clear) { + set.clear(); + print("Set %s cleared", setName); + return; + } + + if (values == null) { + print("Error executing command: No value given"); + return; + } + + if (retain) { // Keep only the given values + for (String value : values) { + givenValues.add(value); + } + if (set.retainAll(givenValues)) { + print("%s was pruned to contain only elements of set %s", setName, givenValues); + } else { + print("%s was not changed by retaining only elements of the set %s", setName, givenValues); + } + } else if (values.length == 1) { + // Remove a single element from the set + if (set.remove(values[0])) { + print("[%s] was removed from the set %s", values[0], setName); + } else { + print("[%s] was not in set %s", values[0], setName); + } + } else if (values.length >= 1) { + // Remove multiple elements from a set + for (String value : values) { + givenValues.add(value); + } + if (set.removeAll(givenValues)) { + print("%s was removed from the set %s", givenValues, setName); + } else { + print("No element of %s was in set %s", givenValues, setName); + } + } + } +} diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java new file mode 100644 index 0000000000..53ed805675 --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/cli/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Distributed Primitives test command-line handlers. + */ +package org.onosproject.distributedprimitives.cli; diff --git a/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java new file mode 100644 index 0000000000..73c418188b --- /dev/null +++ b/apps/test/distributed-primitives/src/main/java/org/onosproject/distributedprimitives/package-info.java @@ -0,0 +1,20 @@ +/* + * 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. + */ + +/** + * Sample application for use in various experiments with distributed primitives. + */ +package org.onosproject.distributedprimitives; 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 new file mode 100644 index 0000000000..00ad9752db --- /dev/null +++ b/apps/test/distributed-primitives/src/main/resources/OSGI-INF/blueprint/shell-config.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + diff --git a/apps/test/pom.xml b/apps/test/pom.xml index bda05d086a..f3b717ee37 100644 --- a/apps/test/pom.xml +++ b/apps/test/pom.xml @@ -36,6 +36,7 @@ intent-perf messaging-perf demo + distributed-primitives