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