blob: 7417cf9567f0212cb9e5dd3fde555e423d6fa34a [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)
Madan Jampani538be742016-02-10 14:55:38 -080064 .build()
65 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070066
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 {
Jon Hall7772af02015-04-29 17:22:25 -070077 for (String e : set.toArray(new String[set.size()])) {
Jon Hall6b687cd2015-04-23 20:04:59 -070078 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
Jon Hall7772af02015-04-29 17:22:25 -070087 if (values == null) {
88 return;
89 } else if (values.length == 1) {
Jon Hall6b687cd2015-04-23 20:04:59 -070090 // 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
andrea669ada42015-10-21 09:03:50 -070098 if (set.containsAll(Arrays.asList(values))) {
99 print("Set %s contains the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -0700100 } else {
andrea669ada42015-10-21 09:03:50 -0700101 print("Set %s did not contain the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -0700102 }
103 }
104 }
105}