Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [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.openstacknetworking.cli; |
| 17 | |
| 18 | import org.apache.karaf.shell.commands.Argument; |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.onosproject.cli.AbstractShellCommand; |
| 21 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 22 | import org.onosproject.openstacknetworking.api.InstancePortAdminService; |
| 23 | import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService; |
| 24 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 25 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 26 | |
| 27 | import java.util.Optional; |
| 28 | |
| 29 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.sendTraceRequestToNode; |
| 30 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.traceRequestString; |
| 31 | |
| 32 | /** |
| 33 | * Requests flow trace command. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "openstack-flow-trace", |
| 36 | description = "Requests flow trace command") |
| 37 | public class OpenstackFlowTraceCommand extends AbstractShellCommand { |
| 38 | |
| 39 | @Argument(index = 0, name = "src ip address", description = "src ip address", |
| 40 | required = true, multiValued = false) |
| 41 | private String srcIp = null; |
| 42 | |
| 43 | @Argument(index = 1, name = "dst ip address", description = "dst ip address", |
| 44 | required = true, multiValued = false) |
| 45 | private String dstIp = null; |
| 46 | |
| 47 | private static final String NO_ELEMENT = "There's no instance port information with given ip address"; |
| 48 | private static final String FLOW_TRACE_REQUEST_STRING_UPLINK = "Flow trace request string for uplink: "; |
| 49 | private static final String FLOW_TRACE_REQUEST_STRING_DOWNLINK = "Flow trace request string for downlink: "; |
| 50 | |
| 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
| 54 | OpenstackNodeAdminService osNodeService = AbstractShellCommand.get(OpenstackNodeAdminService.class); |
| 55 | InstancePortAdminService instancePortService = AbstractShellCommand.get(InstancePortAdminService.class); |
| 56 | OpenstackNetworkAdminService osNetService = AbstractShellCommand.get(OpenstackNetworkAdminService.class); |
| 57 | |
| 58 | Optional<InstancePort> srcInstance = instancePortService.instancePorts().stream() |
| 59 | .filter(port -> port.ipAddress().toString().equals(srcIp)).findAny(); |
| 60 | |
| 61 | if (!srcInstance.isPresent()) { |
| 62 | print(NO_ELEMENT); |
| 63 | return; |
| 64 | } |
| 65 | |
| 66 | OpenstackNode srcNode = osNodeService.node(srcInstance.get().deviceId()); |
| 67 | if (srcNode == null || srcNode.sshAuthInfo() == null) { |
| 68 | log.error("Openstack node {} is null or has no SSH authentication information.\n" + |
| 69 | " Please refers to the sample network-cfg.json in OpenstackNode app to push" + |
Ray Milkey | 4bac497 | 2018-09-19 14:48:38 -0700 | [diff] [blame^] | 70 | "SSH authentication information", srcNode == null ? "" : srcNode.hostname()); |
Daniel Park | a73c236 | 2018-09-17 17:43:25 +0900 | [diff] [blame] | 71 | |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (dstIp.equals(osNetService.gatewayIp(srcInstance.get().portId()))) { |
| 76 | dstIp = srcIp; |
| 77 | } |
| 78 | |
| 79 | //print uplink flow trace result |
| 80 | String requestStringUplink = traceRequestString(srcIp, dstIp, srcInstance.get(), |
| 81 | osNetService, true); |
| 82 | |
| 83 | print(FLOW_TRACE_REQUEST_STRING_UPLINK + requestStringUplink); |
| 84 | |
| 85 | String requestStringDownlink = traceRequestString(srcIp, dstIp, srcInstance.get(), |
| 86 | osNetService, false); |
| 87 | print(FLOW_TRACE_REQUEST_STRING_DOWNLINK + requestStringDownlink); |
| 88 | |
| 89 | String traceResult = sendTraceRequestToNode(requestStringUplink + '\n' |
| 90 | + requestStringDownlink, srcNode); |
| 91 | print(traceResult); |
| 92 | } |
| 93 | } |