blob: 86b7dc7192a133fc0dd2f650430dcbacfb4c499f [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 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;
Jon Hall6b687cd2015-04-23 20:04:59 -070057 Serializer serializer = Serializer.using(
58 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
59
60
61 @Override
62 protected void execute() {
63 StorageService storageService = get(StorageService.class);
64 set = storageService.<String>setBuilder()
65 .withName(setName)
66 .withSerializer(serializer)
Madan Jampani538be742016-02-10 14:55:38 -080067 .build()
68 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070069
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
andrea669ada42015-10-21 09:03:50 -070082 if (set.retainAll(Arrays.asList(values))) {
83 print("%s was pruned to contain only elements of set %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -070084 } else {
andrea669ada42015-10-21 09:03:50 -070085 print("%s was not changed by retaining only elements of the set %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -070086 }
87 } else if (values.length == 1) {
88 // Remove a single element from the set
89 if (set.remove(values[0])) {
90 print("[%s] was removed from the set %s", values[0], setName);
91 } else {
92 print("[%s] was not in set %s", values[0], setName);
93 }
andrea669ada42015-10-21 09:03:50 -070094 } else if (values.length > 1) {
Jon Hall6b687cd2015-04-23 20:04:59 -070095 // Remove multiple elements from a set
andrea669ada42015-10-21 09:03:50 -070096 if (set.removeAll(Arrays.asList(values))) {
97 print("%s was removed from the set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -070098 } else {
andrea669ada42015-10-21 09:03:50 -070099 print("No element of %s was in set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -0700100 }
101 }
102 }
103}