blob: d63a46cee74dfeeb8fdc6e5cba3ac243578de076 [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.ConsistentMap;
23import org.onosproject.store.service.Serializer;
24import org.onosproject.store.service.StorageService;
25import org.onosproject.store.service.Versioned;
26
27/**
28 * CLI command to manipulate a distributed value.
29 */
30@Command(scope = "onos", name = "map-test",
31 description = "Manipulate a consistent map")
32public class ConsistentMapTestCommand extends AbstractShellCommand {
33
34 @Argument(index = 0, name = "name",
35 description = "map name",
36 required = true, multiValued = false)
37 String name = null;
38
39 @Argument(index = 1, name = "operation",
40 description = "operation name",
41 required = true, multiValued = false)
42 String operation = null;
43
44 @Argument(index = 2, name = "key",
45 description = "first arg",
46 required = false, multiValued = false)
47 String arg1 = null;
48
49 @Argument(index = 3, name = "value1",
50 description = "second arg",
51 required = false, multiValued = false)
52 String arg2 = null;
53
54 @Argument(index = 4, name = "value2",
55 description = "third arg",
56 required = false, multiValued = false)
57 String arg3 = null;
58
59 ConsistentMap<String, String> map;
60
61 @Override
62 protected void execute() {
63 StorageService storageService = get(StorageService.class);
64 map = storageService.<String, String>consistentMapBuilder()
65 .withName(name)
66 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
67 .build();
68 if (operation.equals("get")) {
69 print(map.get(arg1));
70 } else if (operation.equals("put")) {
71 print(map.put(arg1, arg2));
72 } else if (operation.equals("size")) {
73 print("%d", map.size());
74 } else if (operation.equals("isEmpty")) {
75 print("%b", map.isEmpty());
76 } else if (operation.equals("putIfAbsent")) {
77 print(map.putIfAbsent(arg1, arg2));
78 } else if (operation.equals("putAndGet")) {
79 print(map.putAndGet(arg1, arg2));
80 } else if (operation.equals("clear")) {
81 map.clear();
82 } else if (operation.equals("remove")) {
83 if (arg2 == null) {
84 print(map.remove(arg1));
85 } else {
86 print("%b", map.remove(arg1, arg2));
87 }
88 } else if (operation.equals("containsKey")) {
89 print("%b", map.containsKey(arg1));
90 } else if (operation.equals("containsValue")) {
91 print("%b", map.containsValue(arg1));
92 } else if (operation.equals("replace")) {
93 if (arg3 == null) {
94 print(map.replace(arg1, arg2));
95 } else {
96 print("%b", map.replace(arg1, arg2, arg3));
97 }
98 }
99 }
100
101 void print(Versioned<String> value) {
102 if (value == null) {
103 print("null");
104 } else {
105 print("%s", value.value());
106 }
107 }
108}