2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.distributedprimitives.cli;
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;
26 import java.util.HashSet;
30 * CLI command to add elements to a distributed set.
32 @Command(scope = "onos", name = "set-test-add",
33 description = "Add to a distributed set")
34 public class SetTestAddCommand extends AbstractShellCommand {
36 @Argument(index = 0, name = "setName",
37 description = "set name",
38 required = true, multiValued = false)
39 String setName = null;
41 @Argument(index = 1, name = "values",
42 description = "Value(s) to add to the set",
43 required = true, multiValued = true)
44 String[] values = null;
47 Set<String> toAdd = new HashSet<>();
50 Serializer serializer = Serializer.using(
51 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
55 protected void execute() {
56 StorageService storageService = get(StorageService.class);
57 set = storageService.<String>setBuilder()
59 .withSerializer(serializer)
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);
67 print("[%s] was already in set %s", values[0], setName);
69 } else if (values.length >= 1) {
70 // Add multiple elements to a set
71 for (String value : values) {
74 if (set.addAll(toAdd)) {
75 print("%s was added to the set %s", toAdd, setName);
77 print("%s was already in set %s", toAdd, setName);