Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
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 get the elements in a distributed set. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "set-test-get", |
| 34 | description = "Get the elements in a distributed set") |
| 35 | public class SetTestGetCommand extends AbstractShellCommand { |
| 36 | |
| 37 | @Option(name = "-s", aliases = "--size", description = "Also show the size of the set?", |
| 38 | required = false, multiValued = false) |
| 39 | private boolean size = false; |
| 40 | |
| 41 | @Argument(index = 0, name = "setName", |
| 42 | description = "set name", |
| 43 | required = true, multiValued = false) |
| 44 | String setName = null; |
| 45 | |
| 46 | @Argument(index = 1, name = "values", |
| 47 | description = "Check if the set contains these value(s)", |
| 48 | required = false, multiValued = true) |
| 49 | String[] values = null; |
| 50 | |
| 51 | Set<String> set; |
Sho SHIMIZU | 49e2766 | 2015-09-11 11:23:27 -0700 | [diff] [blame] | 52 | String output = ""; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 53 | |
| 54 | Serializer serializer = Serializer.using( |
| 55 | new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build()); |
| 56 | |
| 57 | |
| 58 | @Override |
| 59 | protected void execute() { |
| 60 | StorageService storageService = get(StorageService.class); |
| 61 | set = storageService.<String>setBuilder() |
| 62 | .withName(setName) |
| 63 | .withSerializer(serializer) |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 64 | .build() |
| 65 | .asDistributedSet(); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 66 | |
| 67 | // Print the set size |
| 68 | if (size) { |
| 69 | print("There are %d items in set %s:", set.size(), setName); |
| 70 | } else { |
| 71 | print("Items in set %s:", setName); |
| 72 | } |
| 73 | // Print the set |
| 74 | if (set.isEmpty()) { |
| 75 | print("[]"); |
| 76 | } else { |
Jon Hall | 7772af0 | 2015-04-29 17:22:25 -0700 | [diff] [blame] | 77 | for (String e : set.toArray(new String[set.size()])) { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 78 | if (output.isEmpty()) { |
| 79 | output += e; |
| 80 | } else { |
| 81 | output += ", " + e; |
| 82 | } |
| 83 | } |
| 84 | print("[%s]", output); |
| 85 | } |
| 86 | // Check if given values are in the set |
Jon Hall | 7772af0 | 2015-04-29 17:22:25 -0700 | [diff] [blame] | 87 | if (values == null) { |
| 88 | return; |
| 89 | } else if (values.length == 1) { |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 90 | // contains |
| 91 | if (set.contains(values[0])) { |
| 92 | print("Set %s contains the value %s", setName, values[0]); |
| 93 | } else { |
| 94 | print("Set %s did not contain the value %s", setName, values[0]); |
| 95 | } |
| 96 | } else if (values.length > 1) { |
| 97 | //containsAll |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 98 | if (set.containsAll(Arrays.asList(values))) { |
| 99 | print("Set %s contains the the subset %s", setName, Arrays.asList(values)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 100 | } else { |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 101 | print("Set %s did not contain the the subset %s", setName, Arrays.asList(values)); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | } |