Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.distributedprimitives.cli; |
| 17 | |
| 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; |
| 26 | |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 27 | import java.util.Arrays; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 28 | import 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") |
| 35 | public 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 Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 57 | 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 Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 67 | .build() |
| 68 | .asDistributedSet(); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 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 |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 82 | if (set.retainAll(Arrays.asList(values))) { |
| 83 | print("%s was pruned to contain only elements of set %s", setName, Arrays.asList(values)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 84 | } else { |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 85 | print("%s was not changed by retaining only elements of the set %s", setName, Arrays.asList(values)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 86 | } |
| 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 | } |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 94 | } else if (values.length > 1) { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 95 | // Remove multiple elements from a set |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 96 | if (set.removeAll(Arrays.asList(values))) { |
| 97 | print("%s was removed from the set %s", Arrays.asList(values), setName); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 98 | } else { |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 99 | print("No element of %s was in set %s", Arrays.asList(values), setName); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | } |
| 103 | } |