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