blob: 1ef278252f21ef043244027ad55492a472e91d8d [file] [log] [blame]
Jordan Halterman95feda02018-07-02 17:40:31 -07001/*
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.proxytest;
17
18import java.util.concurrent.ExecutionException;
19import java.util.concurrent.TimeUnit;
20import java.util.concurrent.TimeoutException;
21
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Argument;
23import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070024import org.apache.karaf.shell.api.action.lifecycle.Service;
Jordan Halterman95feda02018-07-02 17:40:31 -070025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.cluster.NodeId;
27import org.onosproject.net.DeviceId;
28
29/**
30 * Proxy test command.
31 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070032@Service
Jordan Halterman95feda02018-07-02 17:40:31 -070033@Command(scope = "onos", name = "proxy-test", description = "Manipulate a distributed proxy")
34public class ProxyTestCommand extends AbstractShellCommand {
35
36 @Argument(
37 index = 0,
38 name = "operation",
39 description = "Operation name",
40 required = true,
41 multiValued = false)
42 String operation;
43
44 @Argument(
45 index = 1,
46 name = "type",
47 description = "Operation type, either \"node\" or \"master\"",
48 required = true,
49 multiValued = false)
50 String type;
51
52 @Argument(
53 index = 2,
54 name = "arg1",
55 description = "Operation argument, either a device or node identifier",
56 required = true,
57 multiValued = false)
58 String arg1;
59
60 @Argument(
61 index = 3,
62 name = "arg2",
63 description = "Operation argument",
64 required = true,
65 multiValued = false)
66 String arg2;
67
68 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070069 protected void doExecute() {
Jordan Halterman95feda02018-07-02 17:40:31 -070070 ProxyTest proxyTest = get(ProxyTest.class);
71 TestProxy proxy;
72 if ("node".equals(type)) {
73 NodeId nodeId = NodeId.nodeId(arg1);
74 proxy = proxyTest.getProxyFor(nodeId);
75 } else if ("master".equals(type)) {
76 DeviceId deviceId = DeviceId.deviceId(arg1);
77 proxy = proxyTest.getProxyFor(deviceId);
78 } else {
79 throw new IllegalArgumentException("Unknown operation type " + type);
80 }
81
82 if ("sync".equals(operation)) {
83 print("%s", proxy.testSync(arg2));
84 } else if ("async".equals(operation)) {
85 try {
86 print("%s", proxy.testAsync(arg2).get(10, TimeUnit.SECONDS));
87 } catch (InterruptedException | ExecutionException | TimeoutException e) {
88 throw new IllegalStateException(e);
89 }
90 } else {
91 throw new IllegalArgumentException("Unknown operation " + operation);
92 }
93 }
94}