2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.distributedprimitives.cli;
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;
27 import java.util.HashSet;
31 * CLI command to get the elements in a distributed set.
33 @Command(scope = "onos", name = "set-test-get",
34 description = "Get the elements in a distributed set")
35 public class SetTestGetCommand extends AbstractShellCommand {
37 @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?",
38 required = false, multiValued = false)
39 private boolean size = false;
41 @Argument(index = 0, name = "setName",
42 description = "set name",
43 required = true, multiValued = false)
44 String setName = null;
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;
52 Set<String> toCheck = new HashSet<>();
55 Serializer serializer = Serializer.using(
56 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
60 protected void execute() {
61 StorageService storageService = get(StorageService.class);
62 set = storageService.<String>setBuilder()
64 .withSerializer(serializer)
69 print("There are %d items in set %s:", set.size(), setName);
71 print("Items in set %s:", setName);
77 for (String e : set.toArray(new String[set.size()])) {
78 if (output.isEmpty()) {
84 print("[%s]", output);
86 // Check if given values are in the set
89 } else if (values.length == 1) {
91 if (set.contains(values[0])) {
92 print("Set %s contains the value %s", setName, values[0]);
94 print("Set %s did not contain the value %s", setName, values[0]);
96 } else if (values.length > 1) {
98 for (String value : values) {
101 if (set.containsAll(toCheck)) {
102 print("Set %s contains the the subset %s", setName, toCheck);
104 print("Set %s did not contain the the subset %s", setName, toCheck);