blob: afb39ad5b862f602e443a2d437ea337fbb66577f [file] [log] [blame]
Madan Jampani9e1a8c12016-06-27 11:19:10 -07001/*
2 * Copyright 2016-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 * CLI command to manipulate a distributed value.
28 */
29@Command(scope = "onos", name = "value-test",
30 description = "Manipulate a distributed value")
31public class ValueTestCommand extends AbstractShellCommand {
32
33 @Argument(index = 0, name = "value",
34 description = "Value name",
35 required = true, multiValued = false)
36 String value = 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 = "value1",
44 description = "first arg",
45 required = false, multiValued = false)
46 String value1 = null;
47
48 @Argument(index = 3, name = "value2",
49 description = "second arg",
50 required = false, multiValued = false)
51 String value2 = null;
52
53 AtomicValue<String> atomicValue;
54
55 @Override
56 protected void execute() {
57 StorageService storageService = get(StorageService.class);
58 atomicValue = storageService.<String>atomicValueBuilder()
59 .withName(value)
60 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
61 .build()
62 .asAtomicValue();
Jon Hall41e6cd72017-03-28 13:57:59 -070063 if ("get".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070064 print("%s", atomicValue.get());
Jon Hall41e6cd72017-03-28 13:57:59 -070065 } else if ("set".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070066 atomicValue.set(value1);
Jon Hall41e6cd72017-03-28 13:57:59 -070067 } else if ("compareAndSet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070068 print("%b", atomicValue.compareAndSet(value1, value2));
Jon Hall41e6cd72017-03-28 13:57:59 -070069 } else if ("getAndSet".equals(operation)) {
Jon Hallb27ad7d2017-03-13 16:58:42 -070070 print("%s", atomicValue.getAndSet(value1));
Jon Hall41e6cd72017-03-28 13:57:59 -070071 } else if ("destroy".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070072 atomicValue.destroy();
Jon Hallb27ad7d2017-03-13 16:58:42 -070073 } else {
74 print("Error, unknown operation %s", operation);
Madan Jampani9e1a8c12016-06-27 11:19:10 -070075 }
76 }
77}