Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -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; |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.distributedprimitives.DistributedPrimitivesTest; |
| 23 | import org.onosproject.store.service.EventuallyConsistentMap; |
| 24 | |
| 25 | /** |
| 26 | * CLI command to manipulate a distributed map. |
| 27 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 28 | @Service |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 29 | @Command(scope = "onos", name = "ec-map-test", |
| 30 | description = "Manipulate an eventually consistent map") |
| 31 | public class EventuallyConsistentMapTestCommand extends AbstractShellCommand { |
| 32 | |
| 33 | @Argument(index = 0, name = "name", |
| 34 | description = "map name", |
| 35 | required = true, multiValued = false) |
| 36 | String name = null; |
| 37 | |
| 38 | @Argument(index = 1, name = "operation", |
| 39 | description = "operation name", |
| 40 | required = true, multiValued = false) |
| 41 | String operation = null; |
| 42 | |
| 43 | @Argument(index = 2, name = "key", |
| 44 | description = "first arg", |
| 45 | required = false, multiValued = false) |
| 46 | String arg1 = null; |
| 47 | |
| 48 | @Argument(index = 3, name = "value1", |
| 49 | description = "second arg", |
| 50 | required = false, multiValued = false) |
| 51 | String arg2 = null; |
| 52 | |
| 53 | @Argument(index = 4, name = "value2", |
| 54 | description = "third arg", |
| 55 | required = false, multiValued = false) |
| 56 | String arg3 = null; |
| 57 | |
| 58 | EventuallyConsistentMap<String, String> map; |
| 59 | |
| 60 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 61 | protected void doExecute() { |
Jordan Halterman | 73a6f5d | 2017-05-11 10:37:43 -0700 | [diff] [blame] | 62 | DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class); |
| 63 | map = test.getEcMap(name); |
| 64 | |
| 65 | if ("get".equals(operation)) { |
| 66 | print(map.get(arg1)); |
| 67 | } else if ("put".equals(operation)) { |
| 68 | map.put(arg1, arg2); |
| 69 | } else if ("size".equals(operation)) { |
| 70 | print("%d", map.size()); |
| 71 | } else if ("isEmpty".equals(operation)) { |
| 72 | print("%b", map.isEmpty()); |
| 73 | } else if ("clear".equals(operation)) { |
| 74 | map.clear(); |
| 75 | } else if ("remove".equals(operation)) { |
| 76 | if (arg2 == null) { |
| 77 | print(map.remove(arg1)); |
| 78 | } else { |
| 79 | map.remove(arg1, arg2); |
| 80 | } |
| 81 | } else if ("containsKey".equals(operation)) { |
| 82 | print("%b", map.containsKey(arg1)); |
| 83 | } else if ("containsValue".equals(operation)) { |
| 84 | print("%b", map.containsValue(arg1)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void print(String value) { |
| 89 | if (value == null) { |
| 90 | print("null"); |
| 91 | } else { |
| 92 | print("%s", value); |
| 93 | } |
| 94 | } |
| 95 | } |