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 remove elements from a distributed set.
33 @Command(scope = "onos", name = "set-test-remove",
34 description = "Remove from a distributed set")
35 public class SetTestRemoveCommand extends AbstractShellCommand {
37 @Option(name = "-r", aliases = "--retain",
38 description = "Only keep the given values in the set (if they already exist in the set)",
39 required = false, multiValued = false)
40 private boolean retain = false;
42 @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values",
43 required = false, multiValued = false)
44 private boolean clear = false;
46 @Argument(index = 0, name = "setName",
47 description = "set name",
48 required = true, multiValued = false)
49 String setName = null;
51 @Argument(index = 1, name = "values",
52 description = "Value(s) to remove from the set",
53 required = false, multiValued = true)
54 String[] values = null;
57 Set<String> givenValues = new HashSet<>();
58 Serializer serializer = Serializer.using(
59 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
63 protected void execute() {
64 StorageService storageService = get(StorageService.class);
65 set = storageService.<String>setBuilder()
67 .withSerializer(serializer)
72 print("Set %s cleared", setName);
77 print("Error executing command: No value given");
81 if (retain) { // Keep only the given values
82 for (String value : values) {
83 givenValues.add(value);
85 if (set.retainAll(givenValues)) {
86 print("%s was pruned to contain only elements of set %s", setName, givenValues);
88 print("%s was not changed by retaining only elements of the set %s", setName, givenValues);
90 } else if (values.length == 1) {
91 // Remove a single element from the set
92 if (set.remove(values[0])) {
93 print("[%s] was removed from the set %s", values[0], setName);
95 print("[%s] was not in set %s", values[0], setName);
97 } else if (values.length >= 1) {
98 // Remove multiple elements from a set
99 for (String value : values) {
100 givenValues.add(value);
102 if (set.removeAll(givenValues)) {
103 print("%s was removed from the set %s", givenValues, setName);
105 print("No element of %s was in set %s", givenValues, setName);