blob: 74c52c166b9e6438c545be7d599cab24ad41fa1d [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
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 */
16package org.onosproject.distributedprimitives.cli;
17
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.apache.karaf.shell.commands.Option;
21import org.onlab.util.KryoNamespace;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.store.serializers.KryoNamespaces;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
26
andrea669ada42015-10-21 09:03:50 -070027import java.util.Arrays;
Jon Hall6b687cd2015-04-23 20:04:59 -070028import 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")
35public 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;
Sho SHIMIZU49e27662015-09-11 11:23:27 -070052 String output = "";
Jon Hall6b687cd2015-04-23 20:04:59 -070053
54 Serializer serializer = Serializer.using(
55 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
56
57
58 @Override
59 protected void execute() {
60 StorageService storageService = get(StorageService.class);
61 set = storageService.<String>setBuilder()
62 .withName(setName)
63 .withSerializer(serializer)
64 .build();
65
66 // Print the set size
67 if (size) {
68 print("There are %d items in set %s:", set.size(), setName);
69 } else {
70 print("Items in set %s:", setName);
71 }
72 // Print the set
73 if (set.isEmpty()) {
74 print("[]");
75 } else {
Jon Hall7772af02015-04-29 17:22:25 -070076 for (String e : set.toArray(new String[set.size()])) {
Jon Hall6b687cd2015-04-23 20:04:59 -070077 if (output.isEmpty()) {
78 output += e;
79 } else {
80 output += ", " + e;
81 }
82 }
83 print("[%s]", output);
84 }
85 // Check if given values are in the set
Jon Hall7772af02015-04-29 17:22:25 -070086 if (values == null) {
87 return;
88 } else if (values.length == 1) {
Jon Hall6b687cd2015-04-23 20:04:59 -070089 // contains
90 if (set.contains(values[0])) {
91 print("Set %s contains the value %s", setName, values[0]);
92 } else {
93 print("Set %s did not contain the value %s", setName, values[0]);
94 }
95 } else if (values.length > 1) {
96 //containsAll
andrea669ada42015-10-21 09:03:50 -070097 if (set.containsAll(Arrays.asList(values))) {
98 print("Set %s contains the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -070099 } else {
andrea669ada42015-10-21 09:03:50 -0700100 print("Set %s did not contain the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -0700101 }
102 }
103 }
104}