#!/pkg/bin/ksh
#!/usr/bin/ksh
# Created by Siva Sivabalan April 2011
# Copyright (c) 2003-2017, 2019 by cisco Systems, Inc.
# All rights reserved.
# Processes: te_control, mpls_lsd
#
# This scripts expects 1 or three arguments
#
# -L <level> where level is 0x1, 0x2 or 0x3
#
# -t <tunnel-id> Tunnel number (string)
#
# -f <device> <filename> These two arguments are used to redirect
#                        output to a file system, ex: disk, 
#                        tftp, ftp etc..
#----------------------------------------------------------------------


# This script fragment contains the code for the following functions
# - print_main_heading
# - print_command_heading
# - run_single_command
# - run_commands
# - run_single_command_on_all_nodes
# - run_commands_on_all_nodes
# - default_parser_function
. /pkg/bin/show_tech_main_fragment

# List each set of show commands to be run. Each set must finish with an empty
# string. Note that it is important to use single quotes rather than double 
# quotes for the strings containing your commands; showtech infra expects the command
# exactly the way it was entered.
# Use "\" ahead of any special character (eg. |) used in the command line.

# Common commands
i=1
j=1
Common_exec[$((i++))]='show running-config'
Common__ksh[$((j++))]='nvgen -c -l 1 -t 1 -o 1'
Common_exec[$((i++))]=''
Common__ksh[$((j++))]=''


# TE general commands
i=1
j=1
TE_exec[$((i++))]='show mpls traffic-eng tp link'
TE__ksh[$((j++))]='te_tp_link_show'
TE_exec[$((i++))]='show mpls traffic-eng tp link detail'
TE__ksh[$((j++))]='te_tp_link_show -d'
TE_exec[$((i++))]='show mpls traffic-eng link-management interfaces $standby_exec'
TE__ksh[$((j++))]='mpls_lm_show $standby_te_bool__ksh 0x40'
TE_exec[$((i++))]='show mpls traffic-eng tp tunnel summary'
TE__ksh[$((j++))]='te_tp_tunnel_show 0x2 0xffffffff 0x0 0x0'
TE_exec[$((i++))]='show mpls traffic-eng tp tunnel detail'
TE__ksh[$((j++))]='te_tp_tunnel_show 0x3 0xffffffff 0x0 0x0'
TE_exec[$((i++))]='show mpls traffic-eng tp mid summary'
TE__ksh[$((j++))]='te_slsp_show 0x2 0x0 0x0 0x0 0x0'
TE_exec[$((i++))]='show mpls traffic-eng tp mid detail'
TE__ksh[$((j++))]='te_slsp_show 0x3 0x0 0x0 0x0 0x0'
TE_exec[$((i++))]=''
TE__ksh[$((j++))]=''


# RSVP commands
i=1
j=1
RSVP_exec[$((i++))]='show rsvp interface $standby_exec'
RSVP__ksh[$((j++))]='rsvp_command -o 0x4 $standby_rsvp__ksh'
RSVP_exec[$((i++))]=''
RSVP__ksh[$((j++))]=''


# BFD commands
i=1
j=1
BFD_exec[$((i++))]='show bfd label session location $location'
BFD__ksh[$((j++))]='bfd_show_command -o 0x1 -x 0x3 -e 0x0 -l $fq_nodeid'
BFD_exec[$((i++))]=''
BFD__ksh[$((j++))]=''


# LSD commands
i=1
j=1
LSD_exec[$((i++))]='show mpls lsd forwarding detail location $location'
LSD__ksh[$((j++))]='mpls_lsd_fwd_show -d -L $fq_nodeid'
LSD_exec[$((i++))]='show mpls lsd forwarding tunnels location $location'
LSD__ksh[$((j++))]='mpls_lsd_fwd_show -T -L $fq_nodeid'
LSD_exec[$((i++))]=''
LSD__ksh[$((j++))]=''


# Forwarding commands
i=1
j=1
FWD_exec[$((i++))]='show mpls forwarding location $location'
FWD__ksh[$((j++))]='fib_mpls_show_fwding -h $fq_nodeid'
FWD_exec[$((i++))]='show mpls forwarding tunnel location $location'
FWD__ksh[$((j++))]='fib_mpls_show_fwding -h $fq_nodeid -T'
FWD_exec[$((i++))]=''
FWD__ksh[$((j++))]=''


# Static Routes
i=1
j=1
ROUTE_exec[$((i++))]='show route ipv4 $standby_exec'
ROUTE__ksh[$((j++))]='show_ipv4_rib -X 0x1 -Y 0x1 -Z ________ -V ________ -s $standby_ipv4__ksh ipv4 ________'
ROUTE_exec[$((i++))]=''
ROUTE__ksh[$((j++))]=''



# Dynamically determine nodes with traces to set the
# 'location' global variable before invoking run_commands().
# Not using run_commands_on_all_nodes() as it groups by command,
# and it is desireable to keep the traces grouped by node.
# Also, want to prevent error flood by trying commandset on
# node with no traces (like LC).
run_trace_commands() {
    # We assume the path where ltraces files are stored; bad, but doubt this will change.
    # Using a 'node' wildcard is simple method to find the nodes with traces.
    # `ls -d /net/*/dev/shmem/ltrace/<X>`  outputs lines like:
    #    /net/node0_0_CPU0/dev/shmem/ltrace/<X>
    #    /net/node0_1_CPU0/dev/shmem/ltrace/<X>
    # `cut -d/ -f3`  reduces to:
    #    node0_0_CPU0
    #    node0_1_CPU0
    # `xargs -n 1` calls node_conversion for each node and final output is:
    #    0/0/CPU0
    #    0/1/CPU0
    nodes_with_trace=`ls -d /shared/*/dev/shmem/ltrace/mpls_tp | cut -d/ -f3 | xargs -n 1 node_conversion -E`

    # If above command didn't produce a node list, use 'all' as a fallback
    nodes_with_trace=${nodes_with_trace:-all}

    __saved_location=$location

    # For each node, set $location and invoke run_commands
    for location in $nodes_with_trace ; do
        run_commands Trace
    done

    location=$__saved_location
}


# Initialise any variables used. 
standby_exec=""
standby_te_bool__ksh="-STDBY false"
standby_rsvp__ksh=""
standby_ipv4__ksh=""

# Parse the arguments to the script.
# Usage: 
# 
while [ "$#" -gt "0" ]; do
  case "$1" in
     *)  default_parser_function "$@"; shift $#;;
  esac
done


# Find if the current location is active or standby RP.
source /pkg/bin/mpls_te_show_tech_helper

# A function called display() must be provided that calls the functions to 
# run the required show commands. The display() function is called in 
# /pkg/bin/show_tech_comp_file_frag

display() {
    # Print the output heading 
    print_main_heading "show tech-support mpls traffic-eng tp"
 
    if is_standby "te_control" $ext_node_name; then
        standby_exec="standby"
        standby_te_bool__ksh="-STDBY true"
        standby_rsvp__ksh="-S"
        standby_ipv4__ksh="-t"
    fi

    # Grab the traces first, before they wrap
    # run_trace_commands

    # Run commands
    exec_commands Common
    exec_commands TE
    exec_commands RSVP
    exec_commands LSD
    exec_commands BFD
    exec_commands FWD 
    exec_commands ROUTE
     
    # Print the closing heading. 
    print_main_heading "show tech-support mpls traffic-eng tp complete"
}


# This function calls the display() function and sends the output to file if
# the file option has been set. 
. /pkg/bin/show_tech_file_fragment

