blob: 778947972e7bdfe95e9040027f99e60539cd6f2f [file] [log] [blame]
Jordan Halterman73a6f5d2017-05-11 10:37:43 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jordan Halterman73a6f5d2017-05-11 10:37:43 -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 Halterman73a6f5d2017-05-11 10:37:43 -070021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
23import org.onosproject.store.service.EventuallyConsistentMap;
24
25/**
26 * CLI command to manipulate a distributed map.
27 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070028@Service
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070029@Command(scope = "onos", name = "ec-map-test",
30 description = "Manipulate an eventually consistent map")
31public class EventuallyConsistentMapTestCommand extends AbstractShellCommand {
32
33 @Argument(index = 0, name = "name",
34 description = "map 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 @Argument(index = 4, name = "value2",
54 description = "third arg",
55 required = false, multiValued = false)
56 String arg3 = null;
57
58 EventuallyConsistentMap<String, String> map;
59
60 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070061 protected void doExecute() {
Jordan Halterman73a6f5d2017-05-11 10:37:43 -070062 DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
63 map = test.getEcMap(name);
64
65 if ("get".equals(operation)) {
66 print(map.get(arg1));
67 } else if ("put".equals(operation)) {
68 map.put(arg1, arg2);
69 } else if ("size".equals(operation)) {
70 print("%d", map.size());
71 } else if ("isEmpty".equals(operation)) {
72 print("%b", map.isEmpty());
73 } else if ("clear".equals(operation)) {
74 map.clear();
75 } else if ("remove".equals(operation)) {
76 if (arg2 == null) {
77 print(map.remove(arg1));
78 } else {
79 map.remove(arg1, arg2);
80 }
81 } else if ("containsKey".equals(operation)) {
82 print("%b", map.containsKey(arg1));
83 } else if ("containsValue".equals(operation)) {
84 print("%b", map.containsValue(arg1));
85 }
86 }
87
88 void print(String value) {
89 if (value == null) {
90 print("null");
91 } else {
92 print("%s", value);
93 }
94 }
95}