blob: 359669b1e1c575e520b1d62978d9f804a48d2115 [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
18import org.apache.karaf.shell.commands.Argument;
19import org.apache.karaf.shell.commands.Command;
20import org.onosproject.cli.AbstractShellCommand;
21import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
22import org.onosproject.store.service.EventuallyConsistentMap;
23
24/**
25 * CLI command to manipulate a distributed map.
26 */
27@Command(scope = "onos", name = "ec-map-test",
28 description = "Manipulate an eventually consistent map")
29public class EventuallyConsistentMapTestCommand extends AbstractShellCommand {
30
31 @Argument(index = 0, name = "name",
32 description = "map name",
33 required = true, multiValued = false)
34 String name = null;
35
36 @Argument(index = 1, name = "operation",
37 description = "operation name",
38 required = true, multiValued = false)
39 String operation = null;
40
41 @Argument(index = 2, name = "key",
42 description = "first arg",
43 required = false, multiValued = false)
44 String arg1 = null;
45
46 @Argument(index = 3, name = "value1",
47 description = "second arg",
48 required = false, multiValued = false)
49 String arg2 = null;
50
51 @Argument(index = 4, name = "value2",
52 description = "third arg",
53 required = false, multiValued = false)
54 String arg3 = null;
55
56 EventuallyConsistentMap<String, String> map;
57
58 @Override
59 protected void execute() {
60 DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
61 map = test.getEcMap(name);
62
63 if ("get".equals(operation)) {
64 print(map.get(arg1));
65 } else if ("put".equals(operation)) {
66 map.put(arg1, arg2);
67 } else if ("size".equals(operation)) {
68 print("%d", map.size());
69 } else if ("isEmpty".equals(operation)) {
70 print("%b", map.isEmpty());
71 } else if ("clear".equals(operation)) {
72 map.clear();
73 } else if ("remove".equals(operation)) {
74 if (arg2 == null) {
75 print(map.remove(arg1));
76 } else {
77 map.remove(arg1, arg2);
78 }
79 } else if ("containsKey".equals(operation)) {
80 print("%b", map.containsKey(arg1));
81 } else if ("containsValue".equals(operation)) {
82 print("%b", map.containsValue(arg1));
83 }
84 }
85
86 void print(String value) {
87 if (value == null) {
88 print("null");
89 } else {
90 print("%s", value);
91 }
92 }
93}