Jon Hall
Committed by Ray Milkey

Test application for counters and sets

Application to expose some of the distributed primitives to the cli for
system tests.

Change-Id: I275c8fb6790135c6150658ca3bb5ce356ea735a7
1 +<?xml version="1.0" encoding="UTF-8"?>
2 +<!--
3 + ~ Copyright 2015 Open Networking Laboratory
4 + ~
5 + ~ Licensed under the Apache License, Version 2.0 (the "License");
6 + ~ you may not use this file except in compliance with the License.
7 + ~ You may obtain a copy of the License at
8 + ~
9 + ~ http://www.apache.org/licenses/LICENSE-2.0
10 + ~
11 + ~ Unless required by applicable law or agreed to in writing, software
12 + ~ distributed under the License is distributed on an "AS IS" BASIS,
13 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 + ~ See the License for the specific language governing permissions and
15 + ~ limitations under the License.
16 + -->
17 +
18 +<project xmlns="http://maven.apache.org/POM/4.0.0"
19 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
21 + <modelVersion>4.0.0</modelVersion>
22 +
23 + <parent>
24 + <groupId>org.onosproject</groupId>
25 + <artifactId>onos-apps-test</artifactId>
26 + <version>1.2.0-SNAPSHOT</version>
27 + <relativePath>../pom.xml</relativePath>
28 + </parent>
29 +
30 + <artifactId>onos-app-distributed-primitives</artifactId>
31 + <packaging>bundle</packaging>
32 +
33 + <description>ONOS app to test distributed primitives</description>
34 +
35 + <properties>
36 + <onos.app.name>org.onosproject.distributedprimitives</onos.app.name>
37 + </properties>
38 +
39 + <dependencies>
40 +
41 + <dependency>
42 + <groupId>org.onosproject</groupId>
43 + <artifactId>onos-api</artifactId>
44 + <version>${project.version}</version>
45 + <scope>test</scope>
46 + <classifier>tests</classifier>
47 + </dependency>
48 +
49 + <dependency>
50 + <groupId>org.onosproject</groupId>
51 + <artifactId>onos-cli</artifactId>
52 + <version>${project.version}</version>
53 + </dependency>
54 + <dependency>
55 + <groupId>org.onosproject</groupId>
56 + <artifactId>onos-core-dist</artifactId>
57 + <version>1.2.0-SNAPSHOT</version>
58 + </dependency>
59 + <dependency>
60 + <groupId>org.osgi</groupId>
61 + <artifactId>org.osgi.core</artifactId>
62 + </dependency>
63 + <dependency>
64 + <groupId>org.apache.karaf.shell</groupId>
65 + <artifactId>org.apache.karaf.shell.console</artifactId>
66 + </dependency>
67 +
68 + </dependencies>
69 +
70 +</project>
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.distributedprimitives;
17 +
18 +import org.apache.felix.scr.annotations.Activate;
19 +import org.apache.felix.scr.annotations.Component;
20 +import org.apache.felix.scr.annotations.Deactivate;
21 +import org.apache.felix.scr.annotations.Reference;
22 +import org.apache.felix.scr.annotations.ReferenceCardinality;
23 +import org.onosproject.core.ApplicationId;
24 +import org.onosproject.core.CoreService;
25 +import org.slf4j.Logger;
26 +
27 +import static org.slf4j.LoggerFactory.getLogger;
28 +
29 +
30 +/**
31 + * Simple application to test distributed primitives.
32 + */
33 +@Component(immediate = true)
34 +public class DistributedPrimitivesTest {
35 +
36 + private final Logger log = getLogger(getClass());
37 +
38 + private static final String APP_NAME = "org.onosproject.distributedprimitives";
39 + private ApplicationId appId;
40 +
41 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
42 + protected CoreService coreService;
43 +
44 +
45 + @Activate
46 + protected void activate() {
47 +
48 + log.info("Distributed-Primitives-test app started");
49 + appId = coreService.registerApplication(APP_NAME);
50 + }
51 +
52 + @Deactivate
53 + protected void deactivate() {
54 +
55 + log.info("Distributed-Primitives-test app Stopped");
56 + }
57 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.store.service.AsyncAtomicCounter;
23 +import org.onosproject.store.service.StorageService;
24 +import org.slf4j.Logger;
25 +
26 +import java.util.concurrent.CompletableFuture;
27 +import java.util.concurrent.ExecutionException;
28 +import java.util.concurrent.TimeUnit;
29 +import java.util.concurrent.TimeoutException;
30 +
31 +import static org.slf4j.LoggerFactory.getLogger;
32 +
33 +/**
34 + * CLI command to increment a distributed counter.
35 + */
36 +@Command(scope = "onos", name = "counter-test-increment",
37 + description = "Increment a distributed counter")
38 +public class CounterTestIncrementCommand extends AbstractShellCommand {
39 +
40 + private final Logger log = getLogger(getClass());
41 +
42 + @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?",
43 + required = false, multiValued = false)
44 + private boolean inMemory = false;
45 +
46 + @Argument(index = 0, name = "counter",
47 + description = "Counter name",
48 + required = true, multiValued = false)
49 + String counter = null;
50 +
51 + AsyncAtomicCounter atomicCounter;
52 +
53 +
54 + @Override
55 + protected void execute() {
56 + StorageService storageService = get(StorageService.class);
57 + if (inMemory) {
58 + atomicCounter = storageService.atomicCounterBuilder()
59 + .withName(counter)
60 + .withPartitionsDisabled()
61 + .buildAsyncCounter();
62 + } else {
63 + atomicCounter = storageService.atomicCounterBuilder()
64 + .withName(counter)
65 + .buildAsyncCounter();
66 + }
67 +
68 + CompletableFuture<Long> result = atomicCounter.incrementAndGet();
69 + try {
70 + print("%s was incremented to %d", counter, result.get(3, TimeUnit.SECONDS));
71 + } catch (InterruptedException e) {
72 + return;
73 + } catch (ExecutionException e) {
74 + e.printStackTrace();
75 + } catch (TimeoutException e) {
76 + e.printStackTrace();
77 + }
78 + }
79 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.onlab.util.KryoNamespace;
21 +import org.onosproject.cli.AbstractShellCommand;
22 +import org.onosproject.store.serializers.KryoNamespaces;
23 +import org.onosproject.store.service.Serializer;
24 +import org.onosproject.store.service.StorageService;
25 +
26 +import java.util.HashSet;
27 +import java.util.Set;
28 +
29 +/**
30 + * CLI command to add elements to a distributed set.
31 + */
32 +@Command(scope = "onos", name = "set-test-add",
33 + description = "Add to a distributed set")
34 +public class SetTestAddCommand extends AbstractShellCommand {
35 +
36 + @Argument(index = 0, name = "setName",
37 + description = "set name",
38 + required = true, multiValued = false)
39 + String setName = null;
40 +
41 + @Argument(index = 1, name = "values",
42 + description = "Value(s) to add to the set",
43 + required = true, multiValued = true)
44 + String[] values = null;
45 +
46 + Set<String> set;
47 + Set<String> toAdd = new HashSet<String>();
48 +
49 +
50 + Serializer serializer = Serializer.using(
51 + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
52 +
53 +
54 + @Override
55 + protected void execute() {
56 + StorageService storageService = get(StorageService.class);
57 + set = storageService.<String>setBuilder()
58 + .withName(setName)
59 + .withSerializer(serializer)
60 + .build();
61 +
62 + // Add a single element to the set
63 + if (values.length == 1) {
64 + if (set.add(values[0])) {
65 + print("[%s] was added to the set %s", values[0], setName);
66 + } else {
67 + print("[%s] was already in set %s", values[0], setName);
68 + }
69 + } else if (values.length >= 1) {
70 + // Add multiple elements to a set
71 + for (String value : values) {
72 + toAdd.add(value);
73 + }
74 + if (set.addAll(toAdd)) {
75 + print("%s was added to the set %s", toAdd, setName);
76 + } else {
77 + print("%s was already in set %s", toAdd, setName);
78 + }
79 + }
80 + }
81 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onlab.util.KryoNamespace;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.store.serializers.KryoNamespaces;
24 +import org.onosproject.store.service.Serializer;
25 +import org.onosproject.store.service.StorageService;
26 +
27 +import java.util.HashSet;
28 +import java.util.Set;
29 +
30 +/**
31 + * CLI command to get the elements in a distributed set.
32 + */
33 +@Command(scope = "onos", name = "set-test-get",
34 + description = "Get the elements in a distributed set")
35 +public class SetTestGetCommand extends AbstractShellCommand {
36 +
37 + @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?",
38 + required = false, multiValued = false)
39 + private boolean size = false;
40 +
41 + @Argument(index = 0, name = "setName",
42 + description = "set name",
43 + required = true, multiValued = false)
44 + String setName = null;
45 +
46 + @Argument(index = 1, name = "values",
47 + description = "Check if the set contains these value(s)",
48 + required = false, multiValued = true)
49 + String[] values = null;
50 +
51 + Set<String> set;
52 + Set<String> toCheck = new HashSet<String>();
53 + String output = new String();
54 +
55 + Serializer serializer = Serializer.using(
56 + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
57 +
58 +
59 + @Override
60 + protected void execute() {
61 + StorageService storageService = get(StorageService.class);
62 + set = storageService.<String>setBuilder()
63 + .withName(setName)
64 + .withSerializer(serializer)
65 + .build();
66 +
67 + // Print the set size
68 + if (size) {
69 + print("There are %d items in set %s:", set.size(), setName);
70 + } else {
71 + print("Items in set %s:", setName);
72 + }
73 + // Print the set
74 + if (set.isEmpty()) {
75 + print("[]");
76 + } else {
77 + for (String e : set.toArray(new String[0])) {
78 + if (output.isEmpty()) {
79 + output += e;
80 + } else {
81 + output += ", " + e;
82 + }
83 + }
84 + print("[%s]", output);
85 + }
86 + // Check if given values are in the set
87 + if (values.length == 1) {
88 + // contains
89 + if (set.contains(values[0])) {
90 + print("Set %s contains the value %s", setName, values[0]);
91 + } else {
92 + print("Set %s did not contain the value %s", setName, values[0]);
93 + }
94 + } else if (values.length > 1) {
95 + //containsAll
96 + for (String value : values) {
97 + toCheck.add(value);
98 + }
99 + if (set.containsAll(toCheck)) {
100 + print("Set %s contains the the subset %s", setName, toCheck);
101 + } else {
102 + print("Set %s did not contain the the subset %s", setName, toCheck);
103 + }
104 + }
105 + }
106 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +package org.onosproject.distributedprimitives.cli;
17 +
18 +import org.apache.karaf.shell.commands.Argument;
19 +import org.apache.karaf.shell.commands.Command;
20 +import org.apache.karaf.shell.commands.Option;
21 +import org.onlab.util.KryoNamespace;
22 +import org.onosproject.cli.AbstractShellCommand;
23 +import org.onosproject.store.serializers.KryoNamespaces;
24 +import org.onosproject.store.service.Serializer;
25 +import org.onosproject.store.service.StorageService;
26 +
27 +import java.util.HashSet;
28 +import java.util.Set;
29 +
30 +/**
31 + * CLI command to remove elements from a distributed set.
32 + */
33 +@Command(scope = "onos", name = "set-test-remove",
34 + description = "Remove from a distributed set")
35 +public class SetTestRemoveCommand extends AbstractShellCommand {
36 +
37 + @Option(name = "-r", aliases = "--retain",
38 + description = "Only keep the given values in the set (if they already exist in the set)",
39 + required = false, multiValued = false)
40 + private boolean retain = false;
41 +
42 + @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values",
43 + required = false, multiValued = false)
44 + private boolean clear = false;
45 +
46 + @Argument(index = 0, name = "setName",
47 + description = "set name",
48 + required = true, multiValued = false)
49 + String setName = null;
50 +
51 + @Argument(index = 1, name = "values",
52 + description = "Value(s) to remove from the set",
53 + required = false, multiValued = true)
54 + String[] values = null;
55 +
56 + Set<String> set;
57 + Set<String> givenValues = new HashSet<String>();
58 + Serializer serializer = Serializer.using(
59 + new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
60 +
61 +
62 + @Override
63 + protected void execute() {
64 + StorageService storageService = get(StorageService.class);
65 + set = storageService.<String>setBuilder()
66 + .withName(setName)
67 + .withSerializer(serializer)
68 + .build();
69 +
70 + if (clear) {
71 + set.clear();
72 + print("Set %s cleared", setName);
73 + return;
74 + }
75 +
76 + if (values == null) {
77 + print("Error executing command: No value given");
78 + return;
79 + }
80 +
81 + if (retain) { // Keep only the given values
82 + for (String value : values) {
83 + givenValues.add(value);
84 + }
85 + if (set.retainAll(givenValues)) {
86 + print("%s was pruned to contain only elements of set %s", setName, givenValues);
87 + } else {
88 + print("%s was not changed by retaining only elements of the set %s", setName, givenValues);
89 + }
90 + } else if (values.length == 1) {
91 + // Remove a single element from the set
92 + if (set.remove(values[0])) {
93 + print("[%s] was removed from the set %s", values[0], setName);
94 + } else {
95 + print("[%s] was not in set %s", values[0], setName);
96 + }
97 + } else if (values.length >= 1) {
98 + // Remove multiple elements from a set
99 + for (String value : values) {
100 + givenValues.add(value);
101 + }
102 + if (set.removeAll(givenValues)) {
103 + print("%s was removed from the set %s", givenValues, setName);
104 + } else {
105 + print("No element of %s was in set %s", givenValues, setName);
106 + }
107 + }
108 + }
109 +}
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Distributed Primitives test command-line handlers.
19 + */
20 +package org.onosproject.distributedprimitives.cli;
1 +/*
2 + * Copyright 2015 Open Networking Laboratory
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + */
16 +
17 +/**
18 + * Sample application for use in various experiments with distributed primitives.
19 + */
20 +package org.onosproject.distributedprimitives;
1 +<!--
2 + ~ Copyright 2015 Open Networking Laboratory
3 + ~
4 + ~ Licensed under the Apache License, Version 2.0 (the "License");
5 + ~ you may not use this file except in compliance with the License.
6 + ~ You may obtain a copy of the License at
7 + ~
8 + ~ http://www.apache.org/licenses/LICENSE-2.0
9 + ~
10 + ~ Unless required by applicable law or agreed to in writing, software
11 + ~ distributed under the License is distributed on an "AS IS" BASIS,
12 + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + ~ See the License for the specific language governing permissions and
14 + ~ limitations under the License.
15 + -->
16 +<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
17 +
18 + <command-bundle xmlns="http://karaf.apache.org/xmlns/shell/v1.1.0">
19 + <command>
20 + <action class="org.onosproject.distributedprimitives.cli.CounterTestIncrementCommand"/>
21 + </command>
22 + <command>
23 + <action class="org.onosproject.distributedprimitives.cli.SetTestAddCommand"/>
24 + </command>
25 + <command>
26 + <action class="org.onosproject.distributedprimitives.cli.SetTestGetCommand"/>
27 + </command>
28 + <command>
29 + <action class="org.onosproject.distributedprimitives.cli.SetTestRemoveCommand"/>
30 + </command>
31 + </command-bundle>
32 +
33 +</blueprint>
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
36 <module>intent-perf</module> 36 <module>intent-perf</module>
37 <module>messaging-perf</module> 37 <module>messaging-perf</module>
38 <module>demo</module> 38 <module>demo</module>
39 + <module>distributed-primitives</module>
39 </modules> 40 </modules>
40 41
41 </project> 42 </project>
......