All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.onosproject.t3.cli.TroubleshootSimpleTraceCommand Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 * Copyright 2018-present Open Networking Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onosproject.t3.cli;

import org.apache.karaf.shell.api.action.Argument;
import org.apache.karaf.shell.api.action.Command;
import org.apache.karaf.shell.api.action.Completion;
import org.apache.karaf.shell.api.action.Option;
import org.apache.karaf.shell.api.action.lifecycle.Service;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cli.PlaceholderCompleter;
import org.onosproject.cli.net.EthTypeCompleter;
import org.onosproject.cli.net.HostIdCompleter;
import org.onosproject.net.HostId;
import org.onosproject.t3.api.StaticPacketTrace;
import org.onosproject.t3.api.TroubleshootService;

import java.util.Set;

import static org.onlab.packet.EthType.EtherType;

/**
 * Starts a Static Packet Trace for a given input and prints the result.
 */
@Service
@Command(scope = "onos", name = "t3-troubleshoot-simple",
        description = "Given two hosts troubleshoots flows and groups between them, in case of segment routing")
public class TroubleshootSimpleTraceCommand extends AbstractShellCommand {

    // OSGi workaround to introduce package dependency
    HostIdCompleter completer;
    @Argument(index = 0, name = "one", description = "One host ID",
            required = true, multiValued = false)
    @Completion(HostIdCompleter.class)
    String srcHost = null;

    @Argument(index = 1, name = "two", description = "Another host ID",
            required = true, multiValued = false)
    @Completion(HostIdCompleter.class)
    String dstHost = null;

    @Option(name = "-v", aliases = "--verbose", description = "Outputs complete path")
    @Completion(PlaceholderCompleter.class)
    private boolean verbosity1 = false;

    @Option(name = "-vv", aliases = "--veryverbose", description = "Outputs flows and groups for every device")
    @Completion(PlaceholderCompleter.class)
    private boolean verbosity2 = false;

    @Option(name = "-et", aliases = "--ethType", description = "ETH Type", valueToShowInHelp = "ipv4")
    @Completion(EthTypeCompleter.class)
    String ethType = "ipv4";

    @Override
    protected void doExecute() {
        TroubleshootService service = get(TroubleshootService.class);
        if (!service.checkNibValidity()) {
            // if the NIB is found invalid, fill it with the current network states so that this command can proceed
            print(T3CliUtils.NIB_AUTOFILLED);
            TroubleshootLoadSnapshotCommand cmd = new TroubleshootLoadSnapshotCommand();
            cmd.doExecute();
            if (!service.checkNibValidity()) {
                // if the NIB is still invalid even after auto-filled snapshots, stop and warn
                print(T3CliUtils.NIB_TERMINATE);
                return;
            }
        } else {
            print(service.printNibSummary());
        }

        if (srcHost.equals(dstHost)) {
            print("Source and destination are same. Use different hosts");
            return;
        }

        EtherType type = EtherType.valueOf(ethType.toUpperCase());

        //Printing the traced hosts
        print("Tracing between: %s and %s", srcHost, dstHost);

        //Build the traces
        Set traces = service.trace(HostId.hostId(srcHost), HostId.hostId(dstHost), type);
        traces.forEach(trace -> {
            if (trace.getInitialPacket() != null) {
                print("Tracing Packet: %s", trace.getInitialPacket());
                print("%s", T3CliUtils.printTrace(trace, verbosity1, verbosity2));
            } else {
                print("Cannot obtain trace between %s and %s", srcHost, dstHost);
                print("Reason: %s", trace.resultMessage());
            }
        });


    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy