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.onlab.util.KryoNamespace; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.serializers.KryoNamespaces; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | import org.onosproject.store.service.StorageService; |
| 25 | |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 26 | import java.util.Arrays; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 27 | import java.util.Set; |
| 28 | |
| 29 | /** |
| 30 | * CLI command to add elements to a distributed set. |
| 31 | */ |
| 32 | @Command(scope = "onos", name = "set-test-add", |
| 33 | description = "Add to a distributed set") |
| 34 | public class SetTestAddCommand extends AbstractShellCommand { |
| 35 | |
| 36 | @Argument(index = 0, name = "setName", |
| 37 | description = "set name", |
| 38 | required = true, multiValued = false) |
| 39 | String setName = null; |
| 40 | |
| 41 | @Argument(index = 1, name = "values", |
| 42 | description = "Value(s) to add to the set", |
| 43 | required = true, multiValued = true) |
| 44 | String[] values = null; |
| 45 | |
| 46 | Set<String> set; |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 47 | |
| 48 | |
| 49 | Serializer serializer = Serializer.using( |
| 50 | new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build()); |
| 51 | |
| 52 | |
| 53 | @Override |
| 54 | protected void execute() { |
| 55 | StorageService storageService = get(StorageService.class); |
| 56 | set = storageService.<String>setBuilder() |
| 57 | .withName(setName) |
| 58 | .withSerializer(serializer) |
Madan Jampani | 538be74 | 2016-02-10 14:55:38 -0800 | [diff] [blame] | 59 | .build() |
| 60 | .asDistributedSet(); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 61 | |
| 62 | // Add a single element to the set |
| 63 | if (values.length == 1) { |
| 64 | if (set.add(values[0])) { |
| 65 | print("[%s] was added to the set %s", values[0], setName); |
| 66 | } else { |
| 67 | print("[%s] was already in set %s", values[0], setName); |
| 68 | } |
| 69 | } else if (values.length >= 1) { |
| 70 | // Add multiple elements to a set |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 71 | if (set.addAll(Arrays.asList(values))) { |
| 72 | print("%s was added to the set %s", Arrays.asList(values), setName); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 73 | } else { |
andrea | 669ada4 | 2015-10-21 09:03:50 -0700 | [diff] [blame] | 74 | print("%s was already in set %s", Arrays.asList(values), setName); |
Jon Hall | 6b687cd | 2015-04-23 20:04:59 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | } |
| 78 | } |