Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [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.commons.lang.StringUtils; |
| 20 | import org.apache.karaf.shell.commands.Command; |
| 21 | import org.apache.karaf.shell.commands.Option; |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 22 | import org.onlab.packet.EthType; |
| 23 | import org.onlab.packet.IpPrefix; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 26 | import org.onosproject.net.ConnectPoint; |
| 27 | import org.onosproject.net.flow.TrafficSelector; |
| 28 | import org.onosproject.net.flow.criteria.Criterion; |
| 29 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 30 | import org.onosproject.net.flow.criteria.IPCriterion; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 31 | import org.onosproject.t3.api.StaticPacketTrace; |
| 32 | import org.onosproject.t3.api.TroubleshootService; |
| 33 | import org.onosproject.t3.impl.Generator; |
| 34 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 35 | import java.util.ArrayList; |
| 36 | import java.util.List; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 37 | import java.util.Set; |
| 38 | |
| 39 | /** |
| 40 | * Starts a Static Packet Trace for all the multicast routes in the system and prints the result. |
| 41 | */ |
| 42 | @Command(scope = "onos", name = "t3-troubleshoot-mcast", |
| 43 | description = "Traces all the mcast routes present in the system") |
| 44 | public class TroubleshootMcastCommand extends AbstractShellCommand { |
| 45 | |
| 46 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 47 | @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each mcast route") |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 48 | private boolean verbosity1 = false; |
| 49 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 50 | @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs middle level details of every trace") |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 51 | private boolean verbosity2 = false; |
| 52 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 53 | @Option(name = "-vvv", aliases = "--veryveryverbose", description = "Outputs complete details of every trace") |
| 54 | private boolean verbosity3 = false; |
| 55 | |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 56 | @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None") |
| 57 | String vlan = "None"; |
| 58 | |
| 59 | |
| 60 | @Override |
| 61 | protected void execute() { |
| 62 | TroubleshootService service = get(TroubleshootService.class); |
| 63 | print("Tracing all Multicast routes in the System"); |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 64 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 65 | //Create the generator for the list of traces. |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 66 | VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan); |
| 67 | Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId); |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 68 | int totalTraces = 0; |
| 69 | List<StaticPacketTrace> failedTraces = new ArrayList<>(); |
| 70 | StaticPacketTrace previousTrace = null; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 71 | while (generator.iterator().hasNext()) { |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 72 | totalTraces++; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 73 | //Print also Route if possible or packet |
| 74 | Set<StaticPacketTrace> traces = generator.iterator().next(); |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 75 | if (!verbosity1 && !verbosity2 && !verbosity3) { |
| 76 | for (StaticPacketTrace trace : traces) { |
| 77 | previousTrace = printTrace(previousTrace, trace); |
| 78 | if (!trace.isSuccess()) { |
| 79 | print("Failure: %s", trace.resultMessage()); |
| 80 | failedTraces.add(trace); |
| 81 | } else { |
| 82 | print("Success"); |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | traces.forEach(trace -> { |
| 87 | print("Tracing packet: %s", trace.getInitialPacket()); |
| 88 | print("%s", T3CliUtils.printTrace(trace, verbosity2, verbosity3)); |
| 89 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 90 | }); |
| 91 | } |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 92 | } |
| 93 | |
Andrea Campanella | efa05b4 | 2018-05-08 15:31:31 +0200 | [diff] [blame] | 94 | if (!verbosity1 && !verbosity2 && !verbosity3) { |
| 95 | if (failedTraces.size() != 0) { |
| 96 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 97 | print("Failed Traces: %s", failedTraces.size()); |
| 98 | } |
| 99 | previousTrace = null; |
| 100 | for (StaticPacketTrace trace : failedTraces) { |
| 101 | previousTrace = printTrace(previousTrace, trace); |
| 102 | print("Failure: %s", trace.resultMessage()); |
| 103 | } |
| 104 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 105 | print("Summary"); |
| 106 | print("Total Traces %s, errors %s", totalTraces, failedTraces.size()); |
| 107 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 108 | } |
| 109 | |
| 110 | } |
| 111 | |
| 112 | private StaticPacketTrace printTrace(StaticPacketTrace previousTrace, StaticPacketTrace trace) { |
| 113 | if (previousTrace == null || !previousTrace.equals(trace)) { |
| 114 | print("%s", StringUtils.rightPad("", 125, '-')); |
| 115 | previousTrace = trace; |
| 116 | ConnectPoint initialConnectPoint = trace.getInitialConnectPoint(); |
| 117 | TrafficSelector initialPacket = trace.getInitialPacket(); |
| 118 | boolean isIPv4 = ((EthTypeCriterion) initialPacket.getCriterion(Criterion.Type.ETH_TYPE)) |
| 119 | .ethType().equals(EthType.EtherType.IPV4.ethType() |
| 120 | ); |
| 121 | IpPrefix group = ((IPCriterion) (isIPv4 ? trace.getInitialPacket() |
| 122 | .getCriterion(Criterion.Type.IPV4_DST) : trace.getInitialPacket() |
| 123 | .getCriterion(Criterion.Type.IPV6_DST))).ip(); |
| 124 | print("Source %s, group %s", initialConnectPoint, group); |
| 125 | } |
| 126 | StringBuilder destinations = new StringBuilder(); |
| 127 | if (trace.getCompletePaths().size() > 1) { |
| 128 | destinations.append("Sinks: "); |
| 129 | } else { |
| 130 | destinations.append("Sink: "); |
| 131 | } |
| 132 | trace.getCompletePaths().forEach(path -> { |
| 133 | destinations.append(path.get(path.size() - 1) + " "); |
| 134 | }); |
| 135 | print("%s", destinations.toString()); |
| 136 | return previousTrace; |
Andrea Campanella | fa3ec19 | 2018-04-06 16:30:18 +0200 | [diff] [blame] | 137 | } |
| 138 | } |