blob: 3df110fe92c863501dee9fdb39fd97ab9e295072 [file] [log] [blame]
Andrea Campanellafa3ec192018-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 Milkey86ad7bb2018-09-27 12:32:28 -070020import org.apache.karaf.shell.api.action.Command;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080021import org.apache.karaf.shell.api.action.Completion;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070022import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanellaefa05b42018-05-08 15:31:31 +020024import org.onlab.packet.EthType;
25import org.onlab.packet.IpPrefix;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020026import org.onlab.packet.VlanId;
27import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080028import org.onosproject.cli.PlaceholderCompleter;
Andrea Campanellaefa05b42018-05-08 15:31:31 +020029import org.onosproject.net.ConnectPoint;
30import org.onosproject.net.flow.TrafficSelector;
31import org.onosproject.net.flow.criteria.Criterion;
32import org.onosproject.net.flow.criteria.EthTypeCriterion;
33import org.onosproject.net.flow.criteria.IPCriterion;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020034import org.onosproject.t3.api.StaticPacketTrace;
35import org.onosproject.t3.api.TroubleshootService;
36import org.onosproject.t3.impl.Generator;
37
Andrea Campanellaefa05b42018-05-08 15:31:31 +020038import java.util.ArrayList;
39import java.util.List;
Andrea Campanellafa3ec192018-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 Milkey7a2dee52018-09-28 10:58:28 -070045@Service
Andrea Campanellafa3ec192018-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 Campanellaefa05b42018-05-08 15:31:31 +020050 @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each mcast route")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080051 @Completion(PlaceholderCompleter.class)
Andrea Campanellafa3ec192018-04-06 16:30:18 +020052 private boolean verbosity1 = false;
53
Andrea Campanellaefa05b42018-05-08 15:31:31 +020054 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs middle level details of every trace")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080055 @Completion(PlaceholderCompleter.class)
Andrea Campanellafa3ec192018-04-06 16:30:18 +020056 private boolean verbosity2 = false;
57
Andrea Campanellaefa05b42018-05-08 15:31:31 +020058 @Option(name = "-vvv", aliases = "--veryveryverbose", description = "Outputs complete details of every trace")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080059 @Completion(PlaceholderCompleter.class)
Andrea Campanellaefa05b42018-05-08 15:31:31 +020060 private boolean verbosity3 = false;
61
Andrea Campanellafa3ec192018-04-06 16:30:18 +020062 @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080063 @Completion(PlaceholderCompleter.class)
Andrea Campanellafa3ec192018-04-06 16:30:18 +020064 String vlan = "None";
65
66
67 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070068 protected void doExecute() {
Andrea Campanellafa3ec192018-04-06 16:30:18 +020069 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080070 if (service.checkNibsUnavailable()) {
71 print(TroubleshootLoadFileCommand.ERROR_NULL);
72 return;
73 }
74
Andrea Campanellafa3ec192018-04-06 16:30:18 +020075 print("Tracing all Multicast routes in the System");
Andrea Campanellafa3ec192018-04-06 16:30:18 +020076
Andrea Campanellaefa05b42018-05-08 15:31:31 +020077 //Create the generator for the list of traces.
Andrea Campanellafa3ec192018-04-06 16:30:18 +020078 VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan);
79 Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId);
Andrea Campanellaefa05b42018-05-08 15:31:31 +020080 int totalTraces = 0;
81 List<StaticPacketTrace> failedTraces = new ArrayList<>();
82 StaticPacketTrace previousTrace = null;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020083 while (generator.iterator().hasNext()) {
Andrea Campanellaefa05b42018-05-08 15:31:31 +020084 totalTraces++;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020085 //Print also Route if possible or packet
86 Set<StaticPacketTrace> traces = generator.iterator().next();
Andrea Campanellaefa05b42018-05-08 15:31:31 +020087 if (!verbosity1 && !verbosity2 && !verbosity3) {
88 for (StaticPacketTrace trace : traces) {
89 previousTrace = printTrace(previousTrace, trace);
90 if (!trace.isSuccess()) {
91 print("Failure: %s", trace.resultMessage());
92 failedTraces.add(trace);
93 } else {
Seyeon Jeongec6baf42020-03-03 17:33:40 -080094 print(trace.resultMessage());
Andrea Campanellaefa05b42018-05-08 15:31:31 +020095 }
96 }
97 } else {
98 traces.forEach(trace -> {
99 print("Tracing packet: %s", trace.getInitialPacket());
100 print("%s", T3CliUtils.printTrace(trace, verbosity2, verbosity3));
101 print("%s", StringUtils.rightPad("", 125, '-'));
102 });
103 }
Andrea Campanellafa3ec192018-04-06 16:30:18 +0200104 }
105
Andrea Campanellaefa05b42018-05-08 15:31:31 +0200106 if (!verbosity1 && !verbosity2 && !verbosity3) {
107 if (failedTraces.size() != 0) {
108 print("%s", StringUtils.rightPad("", 125, '-'));
109 print("Failed Traces: %s", failedTraces.size());
110 }
111 previousTrace = null;
112 for (StaticPacketTrace trace : failedTraces) {
113 previousTrace = printTrace(previousTrace, trace);
114 print("Failure: %s", trace.resultMessage());
115 }
116 print("%s", StringUtils.rightPad("", 125, '-'));
117 print("Summary");
118 print("Total Traces %s, errors %s", totalTraces, failedTraces.size());
119 print("%s", StringUtils.rightPad("", 125, '-'));
120 }
121
122 }
123
124 private StaticPacketTrace printTrace(StaticPacketTrace previousTrace, StaticPacketTrace trace) {
125 if (previousTrace == null || !previousTrace.equals(trace)) {
126 print("%s", StringUtils.rightPad("", 125, '-'));
127 previousTrace = trace;
128 ConnectPoint initialConnectPoint = trace.getInitialConnectPoint();
129 TrafficSelector initialPacket = trace.getInitialPacket();
130 boolean isIPv4 = ((EthTypeCriterion) initialPacket.getCriterion(Criterion.Type.ETH_TYPE))
131 .ethType().equals(EthType.EtherType.IPV4.ethType()
132 );
133 IpPrefix group = ((IPCriterion) (isIPv4 ? trace.getInitialPacket()
134 .getCriterion(Criterion.Type.IPV4_DST) : trace.getInitialPacket()
135 .getCriterion(Criterion.Type.IPV6_DST))).ip();
136 print("Source %s, group %s", initialConnectPoint, group);
137 }
138 StringBuilder destinations = new StringBuilder();
139 if (trace.getCompletePaths().size() > 1) {
140 destinations.append("Sinks: ");
141 } else {
142 destinations.append("Sink: ");
143 }
144 trace.getCompletePaths().forEach(path -> {
145 destinations.append(path.get(path.size() - 1) + " ");
146 });
147 print("%s", destinations.toString());
148 return previousTrace;
Andrea Campanellafa3ec192018-04-06 16:30:18 +0200149 }
150}