blob: 905a792de3bc8cb4d4cccddd5721f1a17758ee43 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onlab.util.KryoNamespace;
21import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25
andrea669ada42015-10-21 09:03:50 -070026import java.util.Arrays;
Jon Hall6b687cd2015-04-23 20:04:59 -070027import 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")
34public 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 Hall6b687cd2015-04-23 20:04:59 -070047
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 Jampani538be742016-02-10 14:55:38 -080059 .build()
60 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070061
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
andrea669ada42015-10-21 09:03:50 -070071 if (set.addAll(Arrays.asList(values))) {
72 print("%s was added to the set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -070073 } else {
andrea669ada42015-10-21 09:03:50 -070074 print("%s was already in set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -070075 }
76 }
77 }
78}