blob: 208c47a02cb9476436023007190e2f6e6bb78730 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Jon Hall6b687cd2015-04-23 20:04:59 -07003 *
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
20import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Jon Hall6b687cd2015-04-23 20:04:59 -070022import org.onlab.util.KryoNamespace;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.store.serializers.KryoNamespaces;
25import org.onosproject.store.service.Serializer;
26import org.onosproject.store.service.StorageService;
27
andrea669ada42015-10-21 09:03:50 -070028import java.util.Arrays;
Jon Hall6b687cd2015-04-23 20:04:59 -070029import java.util.Set;
30
31/**
32 * CLI command to get the elements in a distributed set.
33 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070034@Service
Jon Hall6b687cd2015-04-23 20:04:59 -070035@Command(scope = "onos", name = "set-test-get",
36 description = "Get the elements in a distributed set")
37public class SetTestGetCommand extends AbstractShellCommand {
38
39 @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?",
40 required = false, multiValued = false)
41 private boolean size = false;
42
43 @Argument(index = 0, name = "setName",
44 description = "set name",
45 required = true, multiValued = false)
46 String setName = null;
47
48 @Argument(index = 1, name = "values",
49 description = "Check if the set contains these value(s)",
50 required = false, multiValued = true)
51 String[] values = null;
52
53 Set<String> set;
Sho SHIMIZU49e27662015-09-11 11:23:27 -070054 String output = "";
Jon Hall6b687cd2015-04-23 20:04:59 -070055
56 Serializer serializer = Serializer.using(
57 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
58
59
60 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070061 protected void doExecute() {
Jon Hall6b687cd2015-04-23 20:04:59 -070062 StorageService storageService = get(StorageService.class);
63 set = storageService.<String>setBuilder()
64 .withName(setName)
65 .withSerializer(serializer)
Madan Jampani538be742016-02-10 14:55:38 -080066 .build()
67 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070068
69 // Print the set size
70 if (size) {
71 print("There are %d items in set %s:", set.size(), setName);
72 } else {
73 print("Items in set %s:", setName);
74 }
75 // Print the set
76 if (set.isEmpty()) {
77 print("[]");
78 } else {
Jon Hall7772af02015-04-29 17:22:25 -070079 for (String e : set.toArray(new String[set.size()])) {
Jon Hall6b687cd2015-04-23 20:04:59 -070080 if (output.isEmpty()) {
81 output += e;
82 } else {
83 output += ", " + e;
84 }
85 }
86 print("[%s]", output);
87 }
88 // Check if given values are in the set
Jon Hall7772af02015-04-29 17:22:25 -070089 if (values == null) {
90 return;
91 } else if (values.length == 1) {
Jon Hall6b687cd2015-04-23 20:04:59 -070092 // contains
93 if (set.contains(values[0])) {
94 print("Set %s contains the value %s", setName, values[0]);
95 } else {
96 print("Set %s did not contain the value %s", setName, values[0]);
97 }
98 } else if (values.length > 1) {
99 //containsAll
andrea669ada42015-10-21 09:03:50 -0700100 if (set.containsAll(Arrays.asList(values))) {
101 print("Set %s contains the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -0700102 } else {
andrea669ada42015-10-21 09:03:50 -0700103 print("Set %s did not contain the the subset %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -0700104 }
105 }
106 }
107}