Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -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 | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.store.serializers.KryoNamespaces; |
| 23 | import org.onosproject.store.service.AtomicValue; |
| 24 | import org.onosproject.store.service.Serializer; |
| 25 | import org.onosproject.store.service.StorageService; |
| 26 | |
| 27 | /** |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 28 | * CLI command to manipulate a distributed value. |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 29 | */ |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 30 | @Service |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 31 | @Command(scope = "onos", name = "value-test", |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 32 | description = "Manipulate a distributed value") |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 33 | public class AtomicValueTestCommand extends AbstractShellCommand { |
| 34 | |
| 35 | @Argument(index = 0, name = "name", |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 36 | description = "Value name", |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 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 | |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 45 | @Argument(index = 2, name = "value1", |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 46 | description = "first arg", |
| 47 | required = false, multiValued = false) |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 48 | String value1 = null; |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 49 | |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 50 | @Argument(index = 3, name = "value2", |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 51 | description = "second arg", |
| 52 | required = false, multiValued = false) |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 53 | String value2 = null; |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 54 | |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 55 | AtomicValue<String> atomicValue; |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 56 | |
| 57 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 58 | protected void doExecute() { |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 59 | StorageService storageService = get(StorageService.class); |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 60 | atomicValue = storageService.<String>atomicValueBuilder() |
| 61 | .withName(name) |
| 62 | .withSerializer(Serializer.using(KryoNamespaces.BASIC)) |
| 63 | .build() |
| 64 | .asAtomicValue(); |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 65 | if ("get".equals(operation)) { |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 66 | print("%s", atomicValue.get()); |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 67 | } else if ("set".equals(operation)) { |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 68 | atomicValue.set("null".equals(value1) ? null : value1); |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 69 | } else if ("compareAndSet".equals(operation)) { |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 70 | print("%b", atomicValue.compareAndSet( |
| 71 | "null".equals(value1) ? null : value1, |
| 72 | "null".equals(value2) ? null : value2)); |
| 73 | } else if ("getAndSet".equals(operation)) { |
| 74 | print("%s", atomicValue.getAndSet(value1)); |
| 75 | } else if ("destroy".equals(operation)) { |
| 76 | atomicValue.destroy(); |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 77 | } else { |
Jordan Halterman | 285250a | 2017-07-31 14:05:05 -0700 | [diff] [blame] | 78 | print("Error, unknown operation %s", operation); |
Jordan Halterman | 66d3e64 | 2017-07-22 12:51:26 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | } |