blob: 582977201dff7bc04e0f5aff5092b81673fe4fe6 [file] [log] [blame]
Daniel Parka73c2362018-09-17 17:43:25 +09001/*
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.openstacknetworking.cli;
17
Ray Milkey86ad7bb2018-09-27 12:32:28 -070018import org.apache.karaf.shell.api.action.Argument;
19import org.apache.karaf.shell.api.action.Command;
Jian Lidb521c12018-11-19 17:42:35 +090020import org.apache.karaf.shell.api.action.Completion;
Ray Milkey7a2dee52018-09-28 10:58:28 -070021import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Parka73c2362018-09-17 17:43:25 +090022import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacknetworking.api.InstancePort;
24import org.onosproject.openstacknetworking.api.InstancePortAdminService;
25import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
26import org.onosproject.openstacknode.api.OpenstackNode;
27import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
28
29import java.util.Optional;
30
31import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.sendTraceRequestToNode;
32import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.traceRequestString;
33
34/**
35 * Requests flow trace command.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Daniel Parka73c2362018-09-17 17:43:25 +090038@Command(scope = "onos", name = "openstack-flow-trace",
39 description = "Requests flow trace command")
40public class OpenstackFlowTraceCommand extends AbstractShellCommand {
41
42 @Argument(index = 0, name = "src ip address", description = "src ip address",
43 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090044 @Completion(InstanceIpAddressCompleter.class)
Daniel Parka73c2362018-09-17 17:43:25 +090045 private String srcIp = null;
46
47 @Argument(index = 1, name = "dst ip address", description = "dst ip address",
48 required = true, multiValued = false)
Jian Lidb521c12018-11-19 17:42:35 +090049 @Completion(InstanceIpAddressCompleter.class)
Daniel Parka73c2362018-09-17 17:43:25 +090050 private String dstIp = null;
51
52 private static final String NO_ELEMENT = "There's no instance port information with given ip address";
53 private static final String FLOW_TRACE_REQUEST_STRING_UPLINK = "Flow trace request string for uplink: ";
54 private static final String FLOW_TRACE_REQUEST_STRING_DOWNLINK = "Flow trace request string for downlink: ";
55
56
57 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070058 protected void doExecute() {
Daniel Parka73c2362018-09-17 17:43:25 +090059 OpenstackNodeAdminService osNodeService = AbstractShellCommand.get(OpenstackNodeAdminService.class);
60 InstancePortAdminService instancePortService = AbstractShellCommand.get(InstancePortAdminService.class);
61 OpenstackNetworkAdminService osNetService = AbstractShellCommand.get(OpenstackNetworkAdminService.class);
62
63 Optional<InstancePort> srcInstance = instancePortService.instancePorts().stream()
64 .filter(port -> port.ipAddress().toString().equals(srcIp)).findAny();
65
66 if (!srcInstance.isPresent()) {
67 print(NO_ELEMENT);
68 return;
69 }
70
71 OpenstackNode srcNode = osNodeService.node(srcInstance.get().deviceId());
72 if (srcNode == null || srcNode.sshAuthInfo() == null) {
73 log.error("Openstack node {} is null or has no SSH authentication information.\n" +
74 " Please refers to the sample network-cfg.json in OpenstackNode app to push" +
Ray Milkey4bac4972018-09-19 14:48:38 -070075 "SSH authentication information", srcNode == null ? "" : srcNode.hostname());
Daniel Parka73c2362018-09-17 17:43:25 +090076
77 return;
78 }
79
80 if (dstIp.equals(osNetService.gatewayIp(srcInstance.get().portId()))) {
81 dstIp = srcIp;
82 }
83
84 //print uplink flow trace result
85 String requestStringUplink = traceRequestString(srcIp, dstIp, srcInstance.get(),
86 osNetService, true);
87
88 print(FLOW_TRACE_REQUEST_STRING_UPLINK + requestStringUplink);
89
90 String requestStringDownlink = traceRequestString(srcIp, dstIp, srcInstance.get(),
91 osNetService, false);
92 print(FLOW_TRACE_REQUEST_STRING_DOWNLINK + requestStringDownlink);
93
94 String traceResult = sendTraceRequestToNode(requestStringUplink + '\n'
95 + requestStringDownlink, srcNode);
96 print(traceResult);
97 }
98}