blob: a719fea37ab40e2e64ea2ca1bdfec8d7cdfeb83e [file] [log] [blame]
Madan Jampani9e1a8c12016-06-27 11:19:10 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Madan Jampani9e1a8c12016-06-27 11:19:10 -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;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070020import org.onosproject.cli.AbstractShellCommand;
Jordan Haltermanca7660a2018-04-04 23:43:23 -070021import org.onosproject.core.Version;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070022import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.ConsistentMap;
24import org.onosproject.store.service.Serializer;
25import org.onosproject.store.service.StorageService;
26import org.onosproject.store.service.Versioned;
27
28/**
29 * CLI command to manipulate a distributed value.
30 */
31@Command(scope = "onos", name = "map-test",
32 description = "Manipulate a consistent map")
33public class ConsistentMapTestCommand extends AbstractShellCommand {
34
35 @Argument(index = 0, name = "name",
36 description = "map name",
37 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
45 @Argument(index = 2, name = "key",
46 description = "first arg",
47 required = false, multiValued = false)
48 String arg1 = null;
49
50 @Argument(index = 3, name = "value1",
51 description = "second arg",
52 required = false, multiValued = false)
53 String arg2 = null;
54
55 @Argument(index = 4, name = "value2",
56 description = "third arg",
57 required = false, multiValued = false)
58 String arg3 = null;
59
60 ConsistentMap<String, String> map;
61
62 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070063 protected void doExecute() {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070064 StorageService storageService = get(StorageService.class);
65 map = storageService.<String, String>consistentMapBuilder()
Jordan Haltermanca7660a2018-04-04 23:43:23 -070066 .withName(name)
67 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
68 .withVersion(Version.version("1.0.0"))
69 .withCompatibilityFunction((value, version) -> version + ":" + value)
70 .build();
Jon Hall41e6cd72017-03-28 13:57:59 -070071 if ("get".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070072 print(map.get(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070073 } else if ("put".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070074 print(map.put(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070075 } else if ("size".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070076 print("%d", map.size());
Jon Hall41e6cd72017-03-28 13:57:59 -070077 } else if ("isEmpty".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070078 print("%b", map.isEmpty());
Jon Hall41e6cd72017-03-28 13:57:59 -070079 } else if ("putIfAbsent".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070080 print(map.putIfAbsent(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070081 } else if ("putAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070082 print(map.putAndGet(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070083 } else if ("clear".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070084 map.clear();
Jon Hall41e6cd72017-03-28 13:57:59 -070085 } else if ("remove".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070086 if (arg2 == null) {
87 print(map.remove(arg1));
88 } else {
89 print("%b", map.remove(arg1, arg2));
90 }
Jon Hall41e6cd72017-03-28 13:57:59 -070091 } else if ("containsKey".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070092 print("%b", map.containsKey(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070093 } else if ("containsValue".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070094 print("%b", map.containsValue(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070095 } else if ("replace".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070096 if (arg3 == null) {
97 print(map.replace(arg1, arg2));
98 } else {
99 print("%b", map.replace(arg1, arg2, arg3));
100 }
Jordan Haltermanca7660a2018-04-04 23:43:23 -0700101 } else if ("compatiblePut".equals(operation)) {
102 ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder()
103 .withName(name)
104 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
105 .withCompatibilityFunction((value, version) -> version + ":" + value)
106 .withVersion(Version.version("2.0.0"))
107 .build();
108 print(map.put(arg1, arg2));
109 } else if ("compatibleGet".equals(operation)) {
110 ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder()
111 .withName(name)
112 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
113 .withCompatibilityFunction((value, version) -> version + ":" + value)
114 .withVersion(Version.version("2.0.0"))
115 .build();
116 print(map.get(arg1));
Madan Jampani9e1a8c12016-06-27 11:19:10 -0700117 }
118 }
119
120 void print(Versioned<String> value) {
121 if (value == null) {
122 print("null");
123 } else {
124 print("%s", value.value());
125 }
126 }
127}