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 | |
| 19 | import org.apache.karaf.shell.commands.Command; |
| 20 | import org.apache.karaf.shell.commands.Option; |
| 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.Host; |
| 23 | import org.onosproject.t3.api.StaticPacketTrace; |
| 24 | import org.onosproject.t3.api.TroubleshootService; |
| 25 | |
| 26 | import java.util.List; |
| 27 | |
| 28 | import static org.onlab.packet.EthType.EtherType; |
| 29 | |
| 30 | /** |
| 31 | * Starts a Static Packet Trace for a given input and prints the result. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "t3-troubleshoot-pingall", |
| 34 | description = "Traces a ping between all hosts in the system of a given ETH type") |
| 35 | public class TroubleshootPingAllTraceCommand extends AbstractShellCommand { |
| 36 | |
| 37 | private static final String FMT_SHORT = |
| 38 | "id=%s, mac=%s, locations=%s, vlan=%s, ip(s)=%s"; |
| 39 | |
| 40 | @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4") |
| 41 | String ethType = "ipv4"; |
| 42 | |
| 43 | @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each host to host combination") |
| 44 | private boolean verbosity1 = false; |
| 45 | |
| 46 | @Override |
| 47 | protected void execute() { |
| 48 | TroubleshootService service = get(TroubleshootService.class); |
| 49 | |
| 50 | EtherType type = EtherType.valueOf(ethType.toUpperCase()); |
| 51 | |
| 52 | print("Tracing between all %s hosts", ethType); |
| 53 | |
| 54 | if (!type.equals(EtherType.IPV4) && !type.equals(EtherType.IPV6)) { |
| 55 | print("Command only support IPv4 or IPv6"); |
| 56 | } else { |
| 57 | print("--------------------------------------------------------------------------"); |
| 58 | //Obtain the list of traces |
| 59 | List<StaticPacketTrace> traces = service.pingAll(type); |
| 60 | |
| 61 | if (traces.size() == 0) { |
| 62 | print("No traces were obtained, please check system configuration"); |
| 63 | } |
| 64 | |
| 65 | traces.forEach(trace -> { |
| 66 | if (trace.getInitialPacket() != null) { |
| 67 | if (verbosity1) { |
| 68 | printVerbose(trace); |
| 69 | } else { |
| 70 | printResultOnly(trace); |
| 71 | } |
| 72 | } else { |
| 73 | print("Error in obtaining trace: %s", trace.resultMessage()); |
| 74 | } |
| 75 | print("--------------------------------------------------------------------------"); |
| 76 | }); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | } |
| 81 | |
| 82 | private void printResultOnly(StaticPacketTrace trace) { |
| 83 | if (trace.getEndpointHosts().isPresent()) { |
| 84 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 85 | Host destination = trace.getEndpointHosts().get().getRight(); |
| 86 | print("Source %s --> Destination %s", source.id(), destination.id()); |
| 87 | print("%s", trace.resultMessage()); |
| 88 | } else { |
| 89 | print("Can't gather host information from trace"); |
| 90 | print("%s", trace.resultMessage()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private void printVerbose(StaticPacketTrace trace) { |
| 95 | if (trace.getEndpointHosts().isPresent()) { |
| 96 | Host source = trace.getEndpointHosts().get().getLeft(); |
| 97 | print("Source host %s", printHost(source)); |
| 98 | Host destination = trace.getEndpointHosts().get().getRight(); |
| 99 | print("Destination host %s", printHost(destination)); |
| 100 | } |
| 101 | print("%s", trace.getInitialPacket()); |
| 102 | print("%s", T3CliUtils.printTrace(trace, false, false)); |
| 103 | } |
| 104 | |
| 105 | private String printHost(Host host) { |
| 106 | return String.format(FMT_SHORT, host.id(), host.mac(), |
| 107 | host.locations(), |
| 108 | host.vlan(), host.ipAddresses()); |
| 109 | } |
| 110 | } |