#! /usr/bin/perl
# Copyright (c) 2018-2019, AT&T Intellectual Property. All rights reserved.
#
# SPDX-License-Identifier: GPL-2.0-only
# 

use strict;
use warnings;

use lib '/opt/vyatta/share/perl5/';

use Getopt::Long;
use Vyatta::SpanningTreeBridge qw(show_spanning_tree_bridge);
use Vyatta::SpanningTreePort qw(show_spanning_tree_port);

my ( $action, $opt_switch, $opt_port, $opt_format );

$opt_format = "full";
$opt_switch = '';
$opt_port   = '';

sub usage {
    printf "Usage for vyatta-switch-stp-op\n";
    printf "  --action=show_stp --switch=<switch> [--format={brief|status}]\n";
    printf "  --action=show_stp_bridge --switch=<switch> [--format={brief|status}]\n";
    printf "  --action=show_stp_port --switch=<switch> [--port=<port>] [--format={brief|status}]\n";
    exit 1;
}

GetOptions(
    'action=s' => \$action,
    'port=s'   => \$opt_port,
    'switch=s' => \$opt_switch,
    'format=s' => \$opt_format,
) or usage();

if ( $action and $action eq 'show_stp' ) {
    action_show_stp( $opt_switch, $opt_format );
} elsif ( $action and $action eq 'show_stp_bridge' ) {
    action_show_stp_bridge( $opt_switch, $opt_format );
} elsif ( $action and $action eq 'show_stp_port' ) {
    action_show_stp_port( $opt_switch, $opt_port, $opt_format );
} else {
    usage();
}

exit 0;

sub action_show_stp {
    my ( $switch_name, $format ) = @_;

    show_spanning_tree_bridge( $switch_name, 1, 1, $format );
}

sub action_show_stp_bridge {
    my ( $switch_name, $format ) = @_;

    show_spanning_tree_bridge( $switch_name, 1, 0, $format );
}

sub action_show_stp_port {
    my ( $switch_name, $port_name, $format ) = @_;

    show_spanning_tree_port( $switch_name, $port_name, 1, $format );
}
