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