Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.distributedprimitives.cli; |
| 17 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 18 | import org.apache.karaf.shell.api.action.Argument; |
| 19 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 22 | import org.onosproject.core.Version; |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 23 | import org.onosproject.store.serializers.KryoNamespaces; |
| 24 | import org.onosproject.store.service.ConsistentMap; |
| 25 | import org.onosproject.store.service.Serializer; |
| 26 | import org.onosproject.store.service.StorageService; |
| 27 | import org.onosproject.store.service.Versioned; |
| 28 | |
| 29 | /** |
| 30 | * CLI command to manipulate a distributed value. |
| 31 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 32 | @Service |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 33 | @Command(scope = "onos", name = "map-test", |
| 34 | description = "Manipulate a consistent map") |
| 35 | public 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 Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 65 | protected void doExecute() { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 66 | StorageService storageService = get(StorageService.class); |
| 67 | map = storageService.<String, String>consistentMapBuilder() |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 68 | .withName(name) |
| 69 | .withSerializer(Serializer.using(KryoNamespaces.BASIC)) |
| 70 | .withVersion(Version.version("1.0.0")) |
| 71 | .withCompatibilityFunction((value, version) -> version + ":" + value) |
| 72 | .build(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 73 | if ("get".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 74 | print(map.get(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 75 | } else if ("put".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 76 | print(map.put(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 77 | } else if ("size".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 78 | print("%d", map.size()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 79 | } else if ("isEmpty".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 80 | print("%b", map.isEmpty()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 81 | } else if ("putIfAbsent".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 82 | print(map.putIfAbsent(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 83 | } else if ("putAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 84 | print(map.putAndGet(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 85 | } else if ("clear".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 86 | map.clear(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 87 | } else if ("remove".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 88 | if (arg2 == null) { |
| 89 | print(map.remove(arg1)); |
| 90 | } else { |
| 91 | print("%b", map.remove(arg1, arg2)); |
| 92 | } |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 93 | } else if ("containsKey".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 94 | print("%b", map.containsKey(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 95 | } else if ("containsValue".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 96 | print("%b", map.containsValue(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 97 | } else if ("replace".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 98 | if (arg3 == null) { |
| 99 | print(map.replace(arg1, arg2)); |
| 100 | } else { |
| 101 | print("%b", map.replace(arg1, arg2, arg3)); |
| 102 | } |
Jordan Halterman | ca7660a | 2018-04-04 23:43:23 -0700 | [diff] [blame] | 103 | } 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 Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 119 | } |
| 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 | } |