blob: 5320ea4c33eb4c5acfafc68c1e723cf8a760596e [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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Jon Hall6b687cd2015-04-23 20:04:59 -070021import org.onlab.util.KryoNamespace;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.store.serializers.KryoNamespaces;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
26
andrea669ada42015-10-21 09:03:50 -070027import java.util.Arrays;
Jon Hall6b687cd2015-04-23 20:04:59 -070028import java.util.Set;
29
30/**
31 * CLI command to add elements to a distributed set.
32 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070033@Service
Jon Hall6b687cd2015-04-23 20:04:59 -070034@Command(scope = "onos", name = "set-test-add",
35 description = "Add to a distributed set")
36public class SetTestAddCommand extends AbstractShellCommand {
37
38 @Argument(index = 0, name = "setName",
39 description = "set name",
40 required = true, multiValued = false)
41 String setName = null;
42
43 @Argument(index = 1, name = "values",
44 description = "Value(s) to add to the set",
45 required = true, multiValued = true)
46 String[] values = null;
47
48 Set<String> set;
Jon Hall6b687cd2015-04-23 20:04:59 -070049
50
51 Serializer serializer = Serializer.using(
52 new KryoNamespace.Builder().register(KryoNamespaces.BASIC).build());
53
54
55 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070056 protected void doExecute() {
Jon Hall6b687cd2015-04-23 20:04:59 -070057 StorageService storageService = get(StorageService.class);
58 set = storageService.<String>setBuilder()
59 .withName(setName)
60 .withSerializer(serializer)
Madan Jampani538be742016-02-10 14:55:38 -080061 .build()
62 .asDistributedSet();
Jon Hall6b687cd2015-04-23 20:04:59 -070063
64 // Add a single element to the set
65 if (values.length == 1) {
66 if (set.add(values[0])) {
67 print("[%s] was added to the set %s", values[0], setName);
68 } else {
69 print("[%s] was already in set %s", values[0], setName);
70 }
71 } else if (values.length >= 1) {
72 // Add multiple elements to a set
andrea669ada42015-10-21 09:03:50 -070073 if (set.addAll(Arrays.asList(values))) {
74 print("%s was added to the set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -070075 } else {
andrea669ada42015-10-21 09:03:50 -070076 print("%s was already in set %s", Arrays.asList(values), setName);
Jon Hall6b687cd2015-04-23 20:04:59 -070077 }
78 }
79 }
80}