blob: 9e23cb236555770f1208a01cca8a2ecbdad100af [file] [log] [blame]
Jordan Halterman2c9b1592018-01-25 17:19:30 -08001/*
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 */
16package org.onosproject.distributedprimitives.cli;
17
18import java.time.Duration;
19
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
24import org.onosproject.store.service.DistributedLock;
25
26@Command(scope = "onos", name = "lock-test",
27 description = "DistributedLock test cli fixture")
28public class DistributedLockTestCommand extends AbstractShellCommand {
29 @Argument(index = 0, name = "name",
30 description = "lock name",
31 required = true,
32 multiValued = false)
33 String name = null;
34
35 @Argument(index = 1, name = "operation",
36 description = "operation",
37 required = true,
38 multiValued = false)
39 String operation = null;
40
41 @Argument(index = 2, name = "durationMillis",
42 description = "lock attempt duration in milliseconds",
43 required = false,
44 multiValued = false)
45 Long durationMillis = null;
46
47 DistributedLock lock;
48
49 @Override
50 protected void execute() {
51 DistributedPrimitivesTest test = get(DistributedPrimitivesTest.class);
52 lock = test.getLock(name);
53 if ("lock".equals(operation)) {
54 lock.lock();
55 } else if ("tryLock".equals(operation)) {
56 if (durationMillis == null) {
57 print("%b", lock.tryLock().isPresent());
58 } else {
59 print("%b", lock.tryLock(Duration.ofMillis(durationMillis)).isPresent());
60 }
61 } else if ("unlock".equals(operation)) {
62 lock.unlock();
63 }
64 }
65}