blob: 61f14097bb78e2a1aaff95b7ec8f030bd9a71b2e [file] [log] [blame]
Jordan Halterman66d3e642017-07-22 12:51:26 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Jordan Halterman66d3e642017-07-22 12:51:26 -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;
Jordan Halterman66d3e642017-07-22 12:51:26 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.AtomicValue;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
26
27/**
Jordan Halterman285250a2017-07-31 14:05:05 -070028 * CLI command to manipulate a distributed value.
Jordan Halterman66d3e642017-07-22 12:51:26 -070029 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070030@Service
Jordan Halterman66d3e642017-07-22 12:51:26 -070031@Command(scope = "onos", name = "value-test",
Jordan Halterman285250a2017-07-31 14:05:05 -070032 description = "Manipulate a distributed value")
Jordan Halterman66d3e642017-07-22 12:51:26 -070033public class AtomicValueTestCommand extends AbstractShellCommand {
34
35 @Argument(index = 0, name = "name",
Jordan Halterman285250a2017-07-31 14:05:05 -070036 description = "Value name",
Jordan Halterman66d3e642017-07-22 12:51:26 -070037 required = true, multiValued = false)
38 String name = null;
39
40 @Argument(index = 1, name = "operation",
41 description = "operation name",
42 required = true, multiValued = false)
43 String operation = null;
44
Jordan Halterman285250a2017-07-31 14:05:05 -070045 @Argument(index = 2, name = "value1",
Jordan Halterman66d3e642017-07-22 12:51:26 -070046 description = "first arg",
47 required = false, multiValued = false)
Jordan Halterman285250a2017-07-31 14:05:05 -070048 String value1 = null;
Jordan Halterman66d3e642017-07-22 12:51:26 -070049
Jordan Halterman285250a2017-07-31 14:05:05 -070050 @Argument(index = 3, name = "value2",
Jordan Halterman66d3e642017-07-22 12:51:26 -070051 description = "second arg",
52 required = false, multiValued = false)
Jordan Halterman285250a2017-07-31 14:05:05 -070053 String value2 = null;
Jordan Halterman66d3e642017-07-22 12:51:26 -070054
Jordan Halterman285250a2017-07-31 14:05:05 -070055 AtomicValue<String> atomicValue;
Jordan Halterman66d3e642017-07-22 12:51:26 -070056
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Jordan Halterman66d3e642017-07-22 12:51:26 -070059 StorageService storageService = get(StorageService.class);
Jordan Halterman285250a2017-07-31 14:05:05 -070060 atomicValue = storageService.<String>atomicValueBuilder()
61 .withName(name)
62 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
63 .build()
64 .asAtomicValue();
Jordan Halterman66d3e642017-07-22 12:51:26 -070065 if ("get".equals(operation)) {
Jordan Halterman285250a2017-07-31 14:05:05 -070066 print("%s", atomicValue.get());
Jordan Halterman66d3e642017-07-22 12:51:26 -070067 } else if ("set".equals(operation)) {
Jordan Halterman285250a2017-07-31 14:05:05 -070068 atomicValue.set("null".equals(value1) ? null : value1);
Jordan Halterman66d3e642017-07-22 12:51:26 -070069 } else if ("compareAndSet".equals(operation)) {
Jordan Halterman285250a2017-07-31 14:05:05 -070070 print("%b", atomicValue.compareAndSet(
71 "null".equals(value1) ? null : value1,
72 "null".equals(value2) ? null : value2));
73 } else if ("getAndSet".equals(operation)) {
74 print("%s", atomicValue.getAndSet(value1));
75 } else if ("destroy".equals(operation)) {
76 atomicValue.destroy();
Jordan Halterman66d3e642017-07-22 12:51:26 -070077 } else {
Jordan Halterman285250a2017-07-31 14:05:05 -070078 print("Error, unknown operation %s", operation);
Jordan Halterman66d3e642017-07-22 12:51:26 -070079 }
80 }
81}