blob: 0ccc2d3c42e05022e179718c75af34fd1e8bab85 [file] [log] [blame]
Jon Hall6b687cd2015-04-23 20:04:59 -07001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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
26import java.util.HashSet;
27import 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;
47 Set<String> toAdd = new HashSet<String>();
48
49
50 Serializer serializer = Serializer.using(
51 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
52
53
54 @Override
55 protected void execute() {
56 StorageService storageService = get(StorageService.class);
57 set = storageService.<String>setBuilder()
58 .withName(setName)
59 .withSerializer(serializer)
60 .build();
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
71 for (String value : values) {
72 toAdd.add(value);
73 }
74 if (set.addAll(toAdd)) {
75 print("%s was added to the set %s", toAdd, setName);
76 } else {
77 print("%s was already in set %s", toAdd, setName);
78 }
79 }
80 }
81}