blob: 66f5749cd185784b7f43fdd75dfd427a4f27b3ea [file] [log] [blame]
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +01001/*
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
Ray Milkey86ad7bb2018-09-27 12:32:28 -070019import org.apache.karaf.shell.api.action.Argument;
20import org.apache.karaf.shell.api.action.Command;
Ray Milkeyebe0ec62018-10-10 14:32:38 -070021import 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 Campanellaaf34b7c2018-02-08 17:10:11 +010024import org.onosproject.cli.AbstractShellCommand;
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080025import org.onosproject.cli.PlaceholderCompleter;
Ray Milkeyebe0ec62018-10-10 14:32:38 -070026import org.onosproject.cli.net.EthTypeCompleter;
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010027import org.onosproject.cli.net.HostIdCompleter;
28import org.onosproject.net.HostId;
29import org.onosproject.t3.api.StaticPacketTrace;
30import org.onosproject.t3.api.TroubleshootService;
31
Andrea Campanella79cb17d2018-02-27 18:03:17 +010032import java.util.Set;
33
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010034import static org.onlab.packet.EthType.EtherType;
35
36/**
37 * Starts a Static Packet Trace for a given input and prints the result.
38 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070039@Service
Andrea Campanella36769e22018-02-26 11:03:48 +010040@Command(scope = "onos", name = "t3-troubleshoot-simple",
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010041 description = "Given two hosts troubleshoots flows and groups between them, in case of segment routing")
42public class TroubleshootSimpleTraceCommand extends AbstractShellCommand {
43
44 // OSGi workaround to introduce package dependency
45 HostIdCompleter completer;
46 @Argument(index = 0, name = "one", description = "One host ID",
47 required = true, multiValued = false)
Ray Milkeyebe0ec62018-10-10 14:32:38 -070048 @Completion(HostIdCompleter.class)
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010049 String srcHost = null;
50
51 @Argument(index = 1, name = "two", description = "Another host ID",
52 required = true, multiValued = false)
Ray Milkeyebe0ec62018-10-10 14:32:38 -070053 @Completion(HostIdCompleter.class)
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010054 String dstHost = null;
55
56 @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080057 @Completion(PlaceholderCompleter.class)
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010058 private boolean verbosity1 = false;
59
60 @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device")
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080061 @Completion(PlaceholderCompleter.class)
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010062 private boolean verbosity2 = false;
63
64 @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4")
Ray Milkeyebe0ec62018-10-10 14:32:38 -070065 @Completion(EthTypeCompleter.class)
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010066 String ethType = "ipv4";
67
68 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070069 protected void doExecute() {
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010070 TroubleshootService service = get(TroubleshootService.class);
Seyeon Jeongac129562020-02-28 01:17:34 -080071 if (!service.checkNibValidity()) {
72 // if the NIB is found invalid, fill it with the current network states so that this command can proceed
73 print(T3CliUtils.NIB_AUTOFILLED);
74 TroubleshootLoadSnapshotCommand cmd = new TroubleshootLoadSnapshotCommand();
75 cmd.doExecute();
76 if (!service.checkNibValidity()) {
77 // if the NIB is still invalid even after auto-filled snapshots, stop and warn
78 print(T3CliUtils.NIB_TERMINATE);
79 return;
80 }
81 } else {
82 print(service.printNibSummary());
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080083 }
Seyeon Jeongac129562020-02-28 01:17:34 -080084
Seyeon Jeong8d3cad22020-02-28 01:17:34 -080085 if (srcHost.equals(dstHost)) {
86 print("Source and destination are same. Use different hosts");
87 return;
88 }
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010089
90 EtherType type = EtherType.valueOf(ethType.toUpperCase());
91
Andrea Campanella6be5c872018-02-21 14:28:20 +010092 //Printing the traced hosts
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +010093 print("Tracing between: %s and %s", srcHost, dstHost);
94
Andrea Campanella79cb17d2018-02-27 18:03:17 +010095 //Build the traces
96 Set<StaticPacketTrace> traces = service.trace(HostId.hostId(srcHost), HostId.hostId(dstHost), type);
97 traces.forEach(trace -> {
98 if (trace.getInitialPacket() != null) {
99 print("Tracing Packet: %s", trace.getInitialPacket());
100 print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2));
101 } else {
102 print("Cannot obtain trace between %s and %s", srcHost, dstHost);
103 print("Reason: %s", trace.resultMessage());
104 }
105 });
Andrea Campanellaaf34b7c2018-02-08 17:10:11 +0100106
107
108 }
109}