blob: 4b262caf2cd78b127dfdeeba521fa0c01d4e6e82 [file] [log] [blame]
Andrea Campanella6a614fa2018-02-21 14:28:20 +01001/*
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
17package org.onosproject.t3.cli;
18
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010019import org.apache.commons.lang.StringUtils;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010020import org.apache.karaf.shell.commands.Command;
21import org.apache.karaf.shell.commands.Option;
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010022import org.onlab.packet.IpAddress;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.net.Host;
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010025import org.onosproject.net.flow.criteria.Criterion;
26import org.onosproject.net.flow.criteria.IPCriterion;
Andrea Campanella6a614fa2018-02-21 14:28:20 +010027import org.onosproject.t3.api.StaticPacketTrace;
28import org.onosproject.t3.api.TroubleshootService;
29
30import java.util.List;
31
32import static org.onlab.packet.EthType.EtherType;
33
34/**
35 * Starts a Static Packet Trace for a given input and prints the result.
36 */
37@Command(scope = "onos", name = "t3-troubleshoot-pingall",
38 description = "Traces a ping between all hosts in the system of a given ETH type")
39public class TroubleshootPingAllTraceCommand extends AbstractShellCommand {
40
41 private static final String FMT_SHORT =
42 "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s";
43
44 @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4")
45 String ethType = "ipv4";
46
47 @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each host to host combination")
48 private boolean verbosity1 = false;
49
50 @Override
51 protected void execute() {
52 TroubleshootService service = get(TroubleshootService.class);
53
54 EtherType type = EtherType.valueOf(ethType.toUpperCase());
55
56 print("Tracing between all %s hosts", ethType);
57
58 if (!type.equals(EtherType.IPV4) && !type.equals(EtherType.IPV6)) {
59 print("Command only support IPv4 or IPv6");
60 } else {
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010061 print("%s", StringUtils.leftPad("", 100, '-'));
Andrea Campanella6a614fa2018-02-21 14:28:20 +010062 //Obtain the list of traces
63 List<StaticPacketTrace> traces = service.pingAll(type);
64
65 if (traces.size() == 0) {
66 print("No traces were obtained, please check system configuration");
67 }
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010068 boolean ipv4 = type.equals(EtherType.IPV4);
Andrea Campanella6a614fa2018-02-21 14:28:20 +010069 traces.forEach(trace -> {
70 if (trace.getInitialPacket() != null) {
71 if (verbosity1) {
72 printVerbose(trace);
73 } else {
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010074 printResultOnly(trace, ipv4);
Andrea Campanella6a614fa2018-02-21 14:28:20 +010075 }
76 } else {
77 print("Error in obtaining trace: %s", trace.resultMessage());
78 }
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010079 print("%s", StringUtils.leftPad("", 100, '-'));
Andrea Campanella6a614fa2018-02-21 14:28:20 +010080 });
81 }
82
83
84 }
85
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010086 private void printResultOnly(StaticPacketTrace trace, boolean ipv4) {
Andrea Campanella6a614fa2018-02-21 14:28:20 +010087 if (trace.getEndpointHosts().isPresent()) {
88 Host source = trace.getEndpointHosts().get().getLeft();
89 Host destination = trace.getEndpointHosts().get().getRight();
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010090 IpAddress srcIP;
91 IpAddress dstIP;
92 if (ipv4 && trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC) != null) {
93 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC)).ip().address();
94 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)).ip().address();
95 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
96 } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC) != null) {
97 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC)).ip().address();
98 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address();
99 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
100 } else {
101 print("Source %s --> Destination %s", source.id(), destination.id());
102 }
Andrea Campanella6a614fa2018-02-21 14:28:20 +0100103 print("%s", trace.resultMessage());
104 } else {
105 print("Can't gather host information from trace");
106 print("%s", trace.resultMessage());
107 }
108 }
109
110 private void printVerbose(StaticPacketTrace trace) {
111 if (trace.getEndpointHosts().isPresent()) {
112 Host source = trace.getEndpointHosts().get().getLeft();
113 print("Source host %s", printHost(source));
114 Host destination = trace.getEndpointHosts().get().getRight();
115 print("Destination host %s", printHost(destination));
116 }
117 print("%s", trace.getInitialPacket());
118 print("%s", T3CliUtils.printTrace(trace, false, false));
119 }
120
121 private String printHost(Host host) {
122 return String.format(FMT_SHORT, host.id(), host.mac(),
123 host.locations(),
124 host.vlan(), host.ipAddresses());
125 }
126}