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