blob: d2dba49e8ca2db277820952c9fb4de47d83c9b61 [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 {
Andrea Campanella5befe1c2018-02-27 14:50:45 +010077 if (trace.getEndpointHosts().isPresent()) {
78 Host source = trace.getEndpointHosts().get().getLeft();
79 Host destination = trace.getEndpointHosts().get().getRight();
80 print("Source %s --> Destination %s", source.id(), destination.id());
81 }
Andrea Campanella6a614fa2018-02-21 14:28:20 +010082 print("Error in obtaining trace: %s", trace.resultMessage());
83 }
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010084 print("%s", StringUtils.leftPad("", 100, '-'));
Andrea Campanella6a614fa2018-02-21 14:28:20 +010085 });
86 }
87
88
89 }
90
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010091 private void printResultOnly(StaticPacketTrace trace, boolean ipv4) {
Andrea Campanella6a614fa2018-02-21 14:28:20 +010092 if (trace.getEndpointHosts().isPresent()) {
93 Host source = trace.getEndpointHosts().get().getLeft();
94 Host destination = trace.getEndpointHosts().get().getRight();
Andrea Campanella94dfb9e2018-02-27 12:36:00 +010095 IpAddress srcIP;
96 IpAddress dstIP;
97 if (ipv4 && trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC) != null) {
98 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_SRC)).ip().address();
99 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV4_DST)).ip().address();
100 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
101 } else if (trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC) != null) {
102 srcIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_SRC)).ip().address();
103 dstIP = ((IPCriterion) trace.getInitialPacket().getCriterion(Criterion.Type.IPV6_DST)).ip().address();
104 print("Source %s (%s) --> Destination %s (%s)", source.id(), srcIP, destination.id(), dstIP);
105 } else {
106 print("Source %s --> Destination %s", source.id(), destination.id());
107 }
Andrea Campanella6a614fa2018-02-21 14:28:20 +0100108 print("%s", trace.resultMessage());
109 } else {
110 print("Can't gather host information from trace");
111 print("%s", trace.resultMessage());
112 }
113 }
114
115 private void printVerbose(StaticPacketTrace trace) {
116 if (trace.getEndpointHosts().isPresent()) {
117 Host source = trace.getEndpointHosts().get().getLeft();
118 print("Source host %s", printHost(source));
119 Host destination = trace.getEndpointHosts().get().getRight();
120 print("Destination host %s", printHost(destination));
121 }
122 print("%s", trace.getInitialPacket());
123 print("%s", T3CliUtils.printTrace(trace, false, false));
124 }
125
126 private String printHost(Host host) {
127 return String.format(FMT_SHORT, host.id(), host.mac(),
128 host.locations(),
129 host.vlan(), host.ipAddresses());
130 }
131}