Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [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 | |
| 17 | package org.onosproject.t3.cli; |
| 18 | |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 19 | import org.apache.commons.lang.StringUtils; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.net.Host; |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 25 | import org.onosproject.net.flow.criteria.Criterion; |
| 26 | import org.onosproject.net.flow.criteria.IPCriterion; |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 27 | import org.onosproject.t3.api.StaticPacketTrace; |
| 28 | import org.onosproject.t3.api.TroubleshootService; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
| 32 | import 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") |
| 39 | public 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 Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 61 | print("%s", StringUtils.leftPad("", 100, '-')); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 62 | //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 Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 68 | boolean ipv4 = type.equals(EtherType.IPV4); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 69 | traces.forEach(trace -> { |
| 70 | if (trace.getInitialPacket() != null) { |
| 71 | if (verbosity1) { |
| 72 | printVerbose(trace); |
| 73 | } else { |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 74 | printResultOnly(trace, ipv4); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 75 | } |
| 76 | } else { |
Andrea Campanella | 5befe1c | 2018-02-27 14:50:45 +0100 | [diff] [blame^] | 77 | 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 Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 82 | print("Error in obtaining trace: %s", trace.resultMessage()); |
| 83 | } |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 84 | print("%s", StringUtils.leftPad("", 100, '-')); |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 85 | }); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | } |
| 90 | |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 91 | private void printResultOnly(StaticPacketTrace trace, boolean ipv4) { |
Andrea Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 92 | if (trace.getEndpointHosts().isPresent()) { |
| 93 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 94 | Host destination = trace.getEndpointHosts().get().getRight(); |
Andrea Campanella | 94dfb9e | 2018-02-27 12:36:00 +0100 | [diff] [blame] | 95 | 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 Campanella | 6a614fa | 2018-02-21 14:28:20 +0100 | [diff] [blame] | 108 | 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 | } |