fb36a06ab61eb38e044fde37f196d4ac71e77ebe
[onosfw.git] /
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<>();
53     String output = "";
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[set.size()])) {
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 == null) {
88             return;
89         } else if (values.length == 1) {
90             // contains
91             if (set.contains(values[0])) {
92                 print("Set %s contains the value %s", setName, values[0]);
93             } else {
94                 print("Set %s did not contain the value %s", setName, values[0]);
95             }
96         } else if (values.length > 1) {
97             //containsAll
98             for (String value : values) {
99                 toCheck.add(value);
100             }
101             if (set.containsAll(toCheck)) {
102                 print("Set %s contains the the subset %s", setName, toCheck);
103             } else {
104                 print("Set %s did not contain the the subset %s", setName, toCheck);
105             }
106         }
107     }
108 }