blob: 0e510660fc7ff5fe58ebc18fed4da08971c76aa8 [file] [log] [blame]
Andrea Campanellabd15bf52018-04-06 16:30:18 +02001/*
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
17package org.onosproject.t3.cli;
18
19import org.apache.commons.lang.StringUtils;
Ray Milkey822567d2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Seyeon Jeong83e79862020-02-28 01:17:34 -080021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey822567d2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkeyac725032018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020024import org.onlab.packet.EthType;
25import org.onlab.packet.IpPrefix;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020026import org.onlab.packet.VlanId;
pierventrefe57fda2020-08-04 22:52:02 +020027import org.onlab.util.Generator;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020028import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong83e79862020-02-28 01:17:34 -080029import org.onosproject.cli.PlaceholderCompleter;
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020030import org.onosproject.net.ConnectPoint;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.criteria.Criterion;
33import org.onosproject.net.flow.criteria.EthTypeCriterion;
34import org.onosproject.net.flow.criteria.IPCriterion;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020035import org.onosproject.t3.api.StaticPacketTrace;
36import org.onosproject.t3.api.TroubleshootService;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020037
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020038import java.util.ArrayList;
39import java.util.List;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020040import 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 Milkeyac725032018-09-28 10:58:28 -070045@Service
Andrea Campanellabd15bf52018-04-06 16:30:18 +020046@Command(scope = "onos", name = "t3-troubleshoot-mcast",
47 description = "Traces all the mcast routes present in the system")
48public class TroubleshootMcastCommand extends AbstractShellCommand {
49
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020050 @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each mcast route")
Seyeon Jeong83e79862020-02-28 01:17:34 -080051 @Completion(PlaceholderCompleter.class)
Andrea Campanellabd15bf52018-04-06 16:30:18 +020052 private boolean verbosity1 = false;
53
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020054 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs middle level details of every trace")
Seyeon Jeong83e79862020-02-28 01:17:34 -080055 @Completion(PlaceholderCompleter.class)
Andrea Campanellabd15bf52018-04-06 16:30:18 +020056 private boolean verbosity2 = false;
57
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020058 @Option(name = "-vvv", aliases = "--veryveryverbose", description = "Outputs complete details of every trace")
Seyeon Jeong83e79862020-02-28 01:17:34 -080059 @Completion(PlaceholderCompleter.class)
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020060 private boolean verbosity3 = false;
61
Andrea Campanellabd15bf52018-04-06 16:30:18 +020062 @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
Seyeon Jeong83e79862020-02-28 01:17:34 -080063 @Completion(PlaceholderCompleter.class)
Andrea Campanellabd15bf52018-04-06 16:30:18 +020064 String vlan = "None";
65
66
67 @Override
Ray Milkey822567d2018-09-27 12:32:28 -070068 protected void doExecute() {
Andrea Campanellabd15bf52018-04-06 16:30:18 +020069 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeong5018bdd2020-02-28 01:17:34 -080070 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 Jeong83e79862020-02-28 01:17:34 -080082 }
83
Andrea Campanellabd15bf52018-04-06 16:30:18 +020084 print("Tracing all Multicast routes in the System");
Andrea Campanellabd15bf52018-04-06 16:30:18 +020085
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020086 //Create the generator for the list of traces.
Andrea Campanellabd15bf52018-04-06 16:30:18 +020087 VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan);
88 Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId);
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020089 int totalTraces = 0;
90 List<StaticPacketTrace> failedTraces = new ArrayList<>();
91 StaticPacketTrace previousTrace = null;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020092 while (generator.iterator().hasNext()) {
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020093 totalTraces++;
Andrea Campanellabd15bf52018-04-06 16:30:18 +020094 //Print also Route if possible or packet
95 Set<StaticPacketTrace> traces = generator.iterator().next();
Andrea Campanellaeea1fe42018-05-08 15:31:31 +020096 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 Jeongf84f8062020-03-03 17:33:40 -0800103 print(trace.resultMessage());
Andrea Campanellaeea1fe42018-05-08 15:31:31 +0200104 }
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 Campanellabd15bf52018-04-06 16:30:18 +0200113 }
114
Andrea Campanellaeea1fe42018-05-08 15:31:31 +0200115 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 Campanellabd15bf52018-04-06 16:30:18 +0200158 }
159}