Jordan Halterman | 18dac85 | 2018-01-25 17:19:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 java.time.Duration; |
| 19 | |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Jordan Halterman | 18dac85 | 2018-01-25 17:19:30 -0800 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.distributedprimitives.DistributedPrimitivesTest; |
| 25 | import org.onosproject.store.service.DistributedLock; |
| 26 | |
Ray Milkey | 7a2dee5 | 2018-09-28 10:58:28 -0700 | [diff] [blame] | 27 | @Service |
Jordan Halterman | 18dac85 | 2018-01-25 17:19:30 -0800 | [diff] [blame] | 28 | @Command(scope = "onos", name = "lock-test", |
| 29 | description = "DistributedLock test cli fixture") |
| 30 | public class DistributedLockTestCommand extends AbstractShellCommand { |
| 31 | @Argument(index = 0, name = "name", |
| 32 | description = "lock name", |
| 33 | required = true, |
| 34 | multiValued = false) |
| 35 | String name = null; |
| 36 | |
| 37 | @Argument(index = 1, name = "operation", |
| 38 | description = "operation", |
| 39 | required = true, |
| 40 | multiValued = false) |
| 41 | String operation = null; |
| 42 | |
| 43 | @Argument(index = 2, name = "durationMillis", |
| 44 | description = "lock attempt duration in milliseconds", |
| 45 | required = false, |
| 46 | multiValued = false) |
| 47 | Long durationMillis = null; |
| 48 | |
| 49 | DistributedLock lock; |
| 50 | |
| 51 | @Override |
Ray Milkey | 86ad7bb | 2018-09-27 12:32:28 -0700 | [diff] [blame] | 52 | protected void doExecute() { |
Jordan Halterman | 18dac85 | 2018-01-25 17:19:30 -0800 | [diff] [blame] | 53 | DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class); |
| 54 | lock = test.getLock(name); |
| 55 | if ("lock".equals(operation)) { |
| 56 | lock.lock(); |
| 57 | } else if ("tryLock".equals(operation)) { |
| 58 | if (durationMillis == null) { |
| 59 | print("%b", lock.tryLock().isPresent()); |
| 60 | } else { |
| 61 | print("%b", lock.tryLock(Duration.ofMillis(durationMillis)).isPresent()); |
| 62 | } |
| 63 | } else if ("unlock".equals(operation)) { |
| 64 | lock.unlock(); |
| 65 | } |
| 66 | } |
| 67 | } |