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