blob: d3fefec67001aa068ee8d15c4efa1bcca259b988 [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;
21import org.apache.karaf.shell.api.action.Option;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Andrea Campanellaefa05b42018-05-08 15:31:31 +020023import org.onlab.packet.EthType;
24import org.onlab.packet.IpPrefix;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020025import org.onlab.packet.VlanId;
26import org.onosproject.cli.AbstractShellCommand;
Andrea Campanellaefa05b42018-05-08 15:31:31 +020027import org.onosproject.net.ConnectPoint;
28import org.onosproject.net.flow.TrafficSelector;
29import org.onosproject.net.flow.criteria.Criterion;
30import org.onosproject.net.flow.criteria.EthTypeCriterion;
31import org.onosproject.net.flow.criteria.IPCriterion;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020032import org.onosproject.t3.api.StaticPacketTrace;
33import org.onosproject.t3.api.TroubleshootService;
34import org.onosproject.t3.impl.Generator;
35
Andrea Campanellaefa05b42018-05-08 15:31:31 +020036import java.util.ArrayList;
37import java.util.List;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020038import 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 Milkey7a2dee52018-09-28 10:58:28 -070043@Service
Andrea Campanellafa3ec192018-04-06 16:30:18 +020044@Command(scope = "onos", name = "t3-troubleshoot-mcast",
45 description = "Traces all the mcast routes present in the system")
46public class TroubleshootMcastCommand extends AbstractShellCommand {
47
48
Andrea Campanellaefa05b42018-05-08 15:31:31 +020049 @Option(name = "-v", aliases = "--verbose", description = "Outputs trace for each mcast route")
Andrea Campanellafa3ec192018-04-06 16:30:18 +020050 private boolean verbosity1 = false;
51
Andrea Campanellaefa05b42018-05-08 15:31:31 +020052 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs middle level details of every trace")
Andrea Campanellafa3ec192018-04-06 16:30:18 +020053 private boolean verbosity2 = false;
54
Andrea Campanellaefa05b42018-05-08 15:31:31 +020055 @Option(name = "-vvv", aliases = "--veryveryverbose", description = "Outputs complete details of every trace")
56 private boolean verbosity3 = false;
57
Andrea Campanellafa3ec192018-04-06 16:30:18 +020058 @Option(name = "-vid", aliases = "--vlanId", description = "Vlan of incoming packet", valueToShowInHelp = "None")
59 String vlan = "None";
60
61
62 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070063 protected void doExecute() {
Andrea Campanellafa3ec192018-04-06 16:30:18 +020064 TroubleshootService service = get(TroubleshootService.class);
65 print("Tracing all Multicast routes in the System");
Andrea Campanellafa3ec192018-04-06 16:30:18 +020066
Andrea Campanellaefa05b42018-05-08 15:31:31 +020067 //Create the generator for the list of traces.
Andrea Campanellafa3ec192018-04-06 16:30:18 +020068 VlanId vlanId = vlan == null || vlan.isEmpty() ? VlanId.NONE : VlanId.vlanId(vlan);
69 Generator<Set<StaticPacketTrace>> generator = service.traceMcast(vlanId);
Andrea Campanellaefa05b42018-05-08 15:31:31 +020070 int totalTraces = 0;
71 List<StaticPacketTrace> failedTraces = new ArrayList<>();
72 StaticPacketTrace previousTrace = null;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020073 while (generator.iterator().hasNext()) {
Andrea Campanellaefa05b42018-05-08 15:31:31 +020074 totalTraces++;
Andrea Campanellafa3ec192018-04-06 16:30:18 +020075 //Print also Route if possible or packet
76 Set<StaticPacketTrace> traces = generator.iterator().next();
Andrea Campanellaefa05b42018-05-08 15:31:31 +020077 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 Campanellafa3ec192018-04-06 16:30:18 +020094 }
95
Andrea Campanellaefa05b42018-05-08 15:31:31 +020096 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 Campanellafa3ec192018-04-06 16:30:18 +0200139 }
140}