Madan Jampani | 9e1a8c1 | 2016-06-27 11:19:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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.AtomicValue; |
| 23 | import org.onosproject.store.service.Serializer; |
| 24 | import org.onosproject.store.service.StorageService; |
| 25 | |
| 26 | /** |
| 27 | * CLI command to manipulate a distributed value. |
| 28 | */ |
| 29 | @Command(scope = "onos", name = "value-test", |
| 30 | description = "Manipulate a distributed value") |
| 31 | public class ValueTestCommand extends AbstractShellCommand { |
| 32 | |
| 33 | @Argument(index = 0, name = "value", |
| 34 | description = "Value name", |
| 35 | required = true, multiValued = false) |
| 36 | String value = 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 = "value1", |
| 44 | description = "first arg", |
| 45 | required = false, multiValued = false) |
| 46 | String value1 = null; |
| 47 | |
| 48 | @Argument(index = 3, name = "value2", |
| 49 | description = "second arg", |
| 50 | required = false, multiValued = false) |
| 51 | String value2 = null; |
| 52 | |
| 53 | AtomicValue<String> atomicValue; |
| 54 | |
| 55 | @Override |
| 56 | protected void execute() { |
| 57 | StorageService storageService = get(StorageService.class); |
| 58 | atomicValue = storageService.<String>atomicValueBuilder() |
| 59 | .withName(value) |
| 60 | .withSerializer(Serializer.using(KryoNamespaces.BASIC)) |
| 61 | .build() |
| 62 | .asAtomicValue(); |
| 63 | if (operation.equals("get")) { |
| 64 | print("%s", atomicValue.get()); |
| 65 | } else if (operation.equals("set")) { |
| 66 | atomicValue.set(value1); |
| 67 | } else if (operation.equals("compareAndSet")) { |
| 68 | print("%b", atomicValue.compareAndSet(value1, value2)); |
| 69 | } else if (operation.equals("destroy")) { |
| 70 | atomicValue.destroy(); |
| 71 | } |
| 72 | } |
| 73 | } |