blob: d6760c66475a656a3bbf61322b1c4a67f74bf502 [file] [log] [blame]
Jordan Halterman18dac852018-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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Jordan Halterman18dac852018-01-25 17:19:30 -080023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.distributedprimitives.DistributedPrimitivesTest;
25import org.onosproject.store.service.DistributedLock;
26
Ray Milkey7a2dee52018-09-28 10:58:28 -070027@Service
Jordan Halterman18dac852018-01-25 17:19:30 -080028@Command(scope = "onos", name = "lock-test",
29 description = "DistributedLock test cli fixture")
30public 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 Milkey86ad7bb2018-09-27 12:32:28 -070052 protected void doExecute() {
Jordan Halterman18dac852018-01-25 17:19:30 -080053 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}