blob: d1f81e422f780790bd193234d92096ed3b863c55 [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
27import java.util.HashSet;
28import java.util.Set;
29
30/**
31 * CLI command to remove elements from a distributed set.
32 */
33@Command(scope = "onos", name = "set-test-remove",
34 description = "Remove from a distributed set")
35public class SetTestRemoveCommand extends AbstractShellCommand {
36
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;
41
42 @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values",
43 required = false, multiValued = false)
44 private boolean clear = false;
45
46 @Argument(index = 0, name = "setName",
47 description = "set name",
48 required = true, multiValued = false)
49 String setName = null;
50
51 @Argument(index = 1, name = "values",
52 description = "Value(s) to remove from the set",
53 required = false, multiValued = true)
54 String[] values = null;
55
56 Set<String> set;
Sho SHIMIZU6cfc02d2015-09-11 11:19:11 -070057 Set<String> givenValues = new HashSet<>();
Jon Hall6b687cd2015-04-23 20:04:59 -070058 Serializer serializer = Serializer.using(
59 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
60
61
62 @Override
63 protected void execute() {
64 StorageService storageService = get(StorageService.class);
65 set = storageService.<String>setBuilder()
66 .withName(setName)
67 .withSerializer(serializer)
68 .build();
69
70 if (clear) {
71 set.clear();
72 print("Set %s cleared", setName);
73 return;
74 }
75
76 if (values == null) {
77 print("Error executing command: No value given");
78 return;
79 }
80
81 if (retain) { // Keep only the given values
82 for (String value : values) {
83 givenValues.add(value);
84 }
85 if (set.retainAll(givenValues)) {
86 print("%s was pruned to contain only elements of set %s", setName, givenValues);
87 } else {
88 print("%s was not changed by retaining only elements of the set %s", setName, givenValues);
89 }
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);
94 } else {
95 print("[%s] was not in set %s", values[0], setName);
96 }
97 } else if (values.length >= 1) {
98 // Remove multiple elements from a set
99 for (String value : values) {
100 givenValues.add(value);
101 }
102 if (set.removeAll(givenValues)) {
103 print("%s was removed from the set %s", givenValues, setName);
104 } else {
105 print("No element of %s was in set %s", givenValues, setName);
106 }
107 }
108 }
109}