Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | ce2a03d | 2017-08-03 19:21:03 -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 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.store.serializers.KryoNamespaces; |
| 22 | import org.onosproject.store.service.ConsistentMap; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | import org.onosproject.store.service.StorageService; |
| 25 | import 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") |
| 32 | public 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(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 68 | if ("get".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 69 | print(map.get(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 70 | } else if ("put".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 71 | print(map.put(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 72 | } else if ("size".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 73 | print("%d", map.size()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 74 | } else if ("isEmpty".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 75 | print("%b", map.isEmpty()); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 76 | } else if ("putIfAbsent".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 77 | print(map.putIfAbsent(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 78 | } else if ("putAndGet".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 79 | print(map.putAndGet(arg1, arg2)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 80 | } else if ("clear".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 81 | map.clear(); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 82 | } else if ("remove".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 83 | if (arg2 == null) { |
| 84 | print(map.remove(arg1)); |
| 85 | } else { |
| 86 | print("%b", map.remove(arg1, arg2)); |
| 87 | } |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 88 | } else if ("containsKey".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 89 | print("%b", map.containsKey(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 90 | } else if ("containsValue".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 91 | print("%b", map.containsValue(arg1)); |
Jon Hall | 41e6cd7 | 2017-03-28 13:57:59 -0700 | [diff] [blame] | 92 | } else if ("replace".equals(operation)) { |
Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 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 | } |