blob: ad5ef2136a54ea850875358f736500766b5759ac [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;
Ray Milkey7a2dee52018-09-28 10:58:28 -070020import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Parka73c2362018-09-17 17:43:25 +090021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.openstacknetworking.api.InstancePort;
23import org.onosproject.openstacknetworking.api.InstancePortAdminService;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkAdminService;
25import org.onosproject.openstacknode.api.OpenstackNode;
26import org.onosproject.openstacknode.api.OpenstackNodeAdminService;
27
28import java.util.Optional;
29
30import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.sendTraceRequestToNode;
31import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.traceRequestString;
32
33/**
34 * Requests flow trace command.
35 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070036@Service
Daniel Parka73c2362018-09-17 17:43:25 +090037@Command(scope = "onos", name = "openstack-flow-trace",
38 description = "Requests flow trace command")
39public 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 Milkey86ad7bb2018-09-27 12:32:28 -070055 protected void doExecute() {
Daniel Parka73c2362018-09-17 17:43:25 +090056 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 Milkey4bac4972018-09-19 14:48:38 -070072 "SSH authentication information", srcNode == null ? "" : srcNode.hostname());
Daniel Parka73c2362018-09-17 17:43:25 +090073
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}