blob: 309d90a503ccaf51eccee56ae6315bfd30cd33b7 [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070021import org.onosproject.cli.AbstractShellCommand;
Jordan Haltermanca7660a2018-04-04 23:43:23 -070022import org.onosproject.core.Version;
Madan Jampani9e1a8c12016-06-27 11:19:10 -070023import org.onosproject.store.serializers.KryoNamespaces;
24import org.onosproject.store.service.ConsistentMap;
25import org.onosproject.store.service.Serializer;
26import org.onosproject.store.service.StorageService;
27import org.onosproject.store.service.Versioned;
28
29/**
30 * CLI command to manipulate a distributed value.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Madan Jampani9e1a8c12016-06-27 11:19:10 -070033@Command(scope = "onos", name = "map-test",
34 description = "Manipulate a consistent map")
35public class ConsistentMapTestCommand extends AbstractShellCommand {
36
37 @Argument(index = 0, name = "name",
38 description = "map name",
39 required = true, multiValued = false)
40 String name = null;
41
42 @Argument(index = 1, name = "operation",
43 description = "operation name",
44 required = true, multiValued = false)
45 String operation = null;
46
47 @Argument(index = 2, name = "key",
48 description = "first arg",
49 required = false, multiValued = false)
50 String arg1 = null;
51
52 @Argument(index = 3, name = "value1",
53 description = "second arg",
54 required = false, multiValued = false)
55 String arg2 = null;
56
57 @Argument(index = 4, name = "value2",
58 description = "third arg",
59 required = false, multiValued = false)
60 String arg3 = null;
61
62 ConsistentMap<String, String> map;
63
64 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070065 protected void doExecute() {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070066 StorageService storageService = get(StorageService.class);
67 map = storageService.<String, String>consistentMapBuilder()
Jordan Haltermanca7660a2018-04-04 23:43:23 -070068 .withName(name)
69 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
70 .withVersion(Version.version("1.0.0"))
71 .withCompatibilityFunction((value, version) -> version + ":" + value)
72 .build();
Jon Hall41e6cd72017-03-28 13:57:59 -070073 if ("get".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070074 print(map.get(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070075 } else if ("put".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070076 print(map.put(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070077 } else if ("size".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070078 print("%d", map.size());
Jon Hall41e6cd72017-03-28 13:57:59 -070079 } else if ("isEmpty".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070080 print("%b", map.isEmpty());
Jon Hall41e6cd72017-03-28 13:57:59 -070081 } else if ("putIfAbsent".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070082 print(map.putIfAbsent(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070083 } else if ("putAndGet".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070084 print(map.putAndGet(arg1, arg2));
Jon Hall41e6cd72017-03-28 13:57:59 -070085 } else if ("clear".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070086 map.clear();
Jon Hall41e6cd72017-03-28 13:57:59 -070087 } else if ("remove".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070088 if (arg2 == null) {
89 print(map.remove(arg1));
90 } else {
91 print("%b", map.remove(arg1, arg2));
92 }
Jon Hall41e6cd72017-03-28 13:57:59 -070093 } else if ("containsKey".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070094 print("%b", map.containsKey(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070095 } else if ("containsValue".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070096 print("%b", map.containsValue(arg1));
Jon Hall41e6cd72017-03-28 13:57:59 -070097 } else if ("replace".equals(operation)) {
Madan Jampani9e1a8c12016-06-27 11:19:10 -070098 if (arg3 == null) {
99 print(map.replace(arg1, arg2));
100 } else {
101 print("%b", map.replace(arg1, arg2, arg3));
102 }
Jordan Haltermanca7660a2018-04-04 23:43:23 -0700103 } else if ("compatiblePut".equals(operation)) {
104 ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder()
105 .withName(name)
106 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
107 .withCompatibilityFunction((value, version) -> version + ":" + value)
108 .withVersion(Version.version("2.0.0"))
109 .build();
110 print(map.put(arg1, arg2));
111 } else if ("compatibleGet".equals(operation)) {
112 ConsistentMap<String, String> map = storageService.<String, String>consistentMapBuilder()
113 .withName(name)
114 .withSerializer(Serializer.using(KryoNamespaces.BASIC))
115 .withCompatibilityFunction((value, version) -> version + ":" + value)
116 .withVersion(Version.version("2.0.0"))
117 .build();
118 print(map.get(arg1));
Madan Jampani9e1a8c12016-06-27 11:19:10 -0700119 }
120 }
121
122 void print(Versioned<String> value) {
123 if (value == null) {
124 print("null");
125 } else {
126 print("%s", value.value());
127 }
128 }
129}