blob: 7c007371438db00906c56aa661553c5f7c844f8e [file] [log] [blame]
Jordan Halterman66d3e642017-07-22 12:51:26 -07001/*
2 * Copyright 2017-present 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.onosproject.cli.AbstractShellCommand;
21import org.onosproject.store.serializers.KryoNamespaces;
22import org.onosproject.store.service.AtomicValue;
23import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25
26/**
27 * Atomic value test command.
28 */
29@Command(scope = "onos", name = "value-test",
30 description = "Manipulate an atomic value")
31public class AtomicValueTestCommand extends AbstractShellCommand {
32
33 @Argument(index = 0, name = "name",
34 description = "value name",
35 required = true, multiValued = false)
36 String name = null;
37
38 @Argument(index = 1, name = "operation",
39 description = "operation name",
40 required = true, multiValued = false)
41 String operation = null;
42
43 @Argument(index = 2, name = "key",
44 description = "first arg",
45 required = false, multiValued = false)
46 String arg1 = null;
47
48 @Argument(index = 3, name = "value1",
49 description = "second arg",
50 required = false, multiValued = false)
51 String arg2 = null;
52
53 AtomicValue<String> value;
54
55 @Override
56 protected void execute() {
57 StorageService storageService = get(StorageService.class);
58 value = storageService.<String>atomicValueBuilder()
59 .withName(name)
60 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
61 .build()
62 .asAtomicValue();
63
64 if ("get".equals(operation)) {
65 print(value.get());
66 } else if ("set".equals(operation)) {
67 value.set("null".equals(arg1) ? null : arg1);
68 } else if ("getAndSet".equals(operation)) {
69 print(value.getAndSet(arg1));
70 } else if ("compareAndSet".equals(operation)) {
71 print(value.compareAndSet("null".equals(arg1) ? null : arg1, "null".equals(arg2) ? null : arg2));
72 }
73 }
74
75 void print(Object value) {
76 if (value == null) {
77 print("null");
78 } else {
79 print("%s", value);
80 }
81 }
82}