blob: 675747f4a4493b7b1b140a7323999072356deb3c [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 remove elements from 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-remove",
36 description = "Remove from a distributed set")
37public class SetTestRemoveCommand extends AbstractShellCommand {
38
39 @Option(name = "-r", aliases = "--retain",
40 description = "Only keep the given values in the set (if they already exist in the set)",
41 required = false, multiValued = false)
42 private boolean retain = false;
43
44 @Option(name = "-c", aliases = "--clear", description = "Clear the set of all values",
45 required = false, multiValued = false)
46 private boolean clear = false;
47
48 @Argument(index = 0, name = "setName",
49 description = "set name",
50 required = true, multiValued = false)
51 String setName = null;
52
53 @Argument(index = 1, name = "values",
54 description = "Value(s) to remove from the set",
55 required = false, multiValued = true)
56 String[] values = null;
57
58 Set<String> set;
Jon Hall6b687cd2015-04-23 20:04:59 -070059 Serializer serializer = Serializer.using(
60 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
61
62
63 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070064 protected void doExecute() {
Jon Hall6b687cd2015-04-23 20:04:59 -070065 StorageService storageService = get(StorageService.class);
66 set = storageService.<String>setBuilder()
67 .withName(setName)
68 .withSerializer(serializer)
Madan Jampani538be742016-02-10 14:55:38 -080069 .build()
70 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070071
72 if (clear) {
73 set.clear();
74 print("Set %s cleared", setName);
75 return;
76 }
77
78 if (values == null) {
79 print("Error executing command: No value given");
80 return;
81 }
82
83 if (retain) { // Keep only the given values
andrea669ada42015-10-21 09:03:50 -070084 if (set.retainAll(Arrays.asList(values))) {
85 print("%s was pruned to contain only elements of set %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -070086 } else {
andrea669ada42015-10-21 09:03:50 -070087 print("%s was not changed by retaining only elements of the set %s", setName, Arrays.asList(values));
Jon Hall6b687cd2015-04-23 20:04:59 -070088 }
89 } else if (values.length == 1) {
90 // Remove a single element from the set
91 if (set.remove(values[0])) {
92 print("[%s] was removed from the set %s", values[0], setName);
93 } else {
94 print("[%s] was not in set %s", values[0], setName);
95 }
andrea669ada42015-10-21 09:03:50 -070096 } else if (values.length > 1) {
Jon Hall6b687cd2015-04-23 20:04:59 -070097 // Remove multiple elements from a set
andrea669ada42015-10-21 09:03:50 -070098 if (set.removeAll(Arrays.asList(values))) {
99 print("%s was removed from the set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -0700100 } else {
andrea669ada42015-10-21 09:03:50 -0700101 print("No element of %s was in set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -0700102 }
103 }
104 }
105}