#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_xtc - Show tech-support script for XTC
#
# July 2017, Muhammad Abdul Rehman
#
# Copyright (c) 2017-2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------


# This script fragment contains the code for the following functions
# - print_main_ht_commaneading
# - prind_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
. /pkg/bin/xtc_show_tech_helper


XTC_SHOW_TECH_DEBUG=false
# set XTC_SHOW_TECH_DEBUG=true to enable debug lines
# XTC_SHOW_TECH_DEBUG=true

if [ $XTC_SHOW_TECH_DEBUG = true ]
then 
    debug_echo() { echo "DEBUG: " $* >&2; }
else
    debug_echo() { :; }
fi

# Initialise any variables used. 
                                                       
__cardtype="unspecified"

# Parse the arguments to the script.
# Usage: 
# 
# Run commands that display ip-infra information
while [ $# -gt 0 ]; do
  case "$1" in
     -t) __cardtype="$2"; shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

# The location and card type fetched below is used in our code
int_node_name=$showtech_node_name
ext_node_name=`node_conversion -E $int_node_name`
fq_nodeid=`node_conversion -i $int_node_name`
location=$ext_node_name

if [ "$__cardtype" == "unspecified" ]; then
     __cardtype=`node_type`
fi


# get_pid function. Given the process name, it will
# echo the pid for that process
# it will have code to take care of the host OS.
OS_STRING=`uname -s`
function get_pid {
    if [[ ${OS_STRING} = "QNX" ]]; then
        pid=`pidin | grep $1 | cut -c1-8 | head -n 1`
    else
        pid=`ps -ef | grep $1 | grep -v grep | awk '{print $2}' | head -n 1`
    fi
    echo ${pid}
}

# Function to display system commands
display_sys() {

    cmd_index=0;
    
    # Initializing commands
    debug_echo "Initializing commands"

    ((cmd_index++))
    gen_exec[cmd_index]='show clock'
    gen__ksh[cmd_index]=`get_ksh_show_clock`

    ((cmd_index++))
    gen_exec[cmd_index]='show platform'
    gen__ksh[cmd_index]=`get_ksh_show_platform`

    ((cmd_index++))
    gen_exec[cmd_index]='show version'
    gen__ksh[cmd_index]=`get_ksh_show_version`

    ((cmd_index++))
    gen_exec[cmd_index]='show redundancy'
    gen__ksh[cmd_index]=`get_ksh_show_redundancy`

    ((cmd_index++))
    gen_exec[cmd_index]='show process blocked'
    gen__ksh[cmd_index]=`get_ksh_show_process_blocked`

    ((cmd_index++))
    gen_exec[cmd_index]='show running-config'
    gen__ksh[cmd_index]='nvgen -c -l 1 -t 1 -o 1 '

    ((cmd_index++))
    gen_exec[cmd_index]='show process xtc_agent'
    gen__ksh[cmd_index]=`get_ksh_show_process_procname xtc_agent`

    ((cmd_index++))
    gen_exec[cmd_index]='show logging'
    gen__ksh[cmd_index]='show_logging'

    ((cmd_index++))
    gen_exec[cmd_index]=''
    gen__ksh[cmd_index]=''

    # Execute the commands now
    debug_echo "Executing commands"
    exec_commands gen
}

# Function to display command for active location
display_rp_active() {

    cmd_index=0;
    
    # Initializing commands
    debug_echo "Initializing commands"

    # BGP commands
    ((cmd_index++))
    bgp_exec[cmd_index]='show bgp ipv4 sr-policy'
    bgp__ksh[cmd_index]='bgp_show -V default -A 0x1 -W 0x49 -t -instance default'

    ((cmd_index++))
    bgp_exec[cmd_index]='show bgp ipv6 sr-policy'
    bgp__ksh[cmd_index]='bgp_show -V default -A 0x2 -W 0x49 -t -instance default'

    ((cmd_index++))
    bgp_exec[cmd_index]=''
    bgp__ksh[cmd_index]=''

    cmd_index=0;

    # MPLS commands
    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls forwarding no-counters'
    mpls__ksh[cmd_index]='fib_mpls_show_fwding -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls forwarding tunnels no-counters'
    mpls__ksh[cmd_index]='fib_mpls_show_fwding -T -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls forwarding tunnels sr-policy detail no-counters'
    mpls__ksh[cmd_index]='fib_mpls_show_fwding -T -o -V -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls lsd forwarding detail'
    mpls__ksh[cmd_index]='mpls_lsd_fwd_show -d -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls lsd forwarding tunnels detail'
    mpls__ksh[cmd_index]='mpls_lsd_fwd_show -T -d -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]='show mpls label table private'
    mpls__ksh[cmd_index]='mpls_lsd_show -t 0x0 -P -Z'

    ((cmd_index++))
    mpls_exec[cmd_index]=''
    mpls__ksh[cmd_index]=''

    cmd_index=0;

    # Segment routing commands
    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng policy private'
    sr__ksh[cmd_index]='xtc_show -c 0x4 -m 0x4'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng forwarding policy private'
    sr__ksh[cmd_index]='xtc_show -c 0x7 -m 0x4'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng trace'
    sr__ksh[cmd_index]='xtc_show_trace'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng id-allocator'
    sr__ksh[cmd_index]='xtc_show -c 0xA -t 0x1'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng configured-policy private'
    sr__ksh[cmd_index]='xtc_show -c 0xb -m 0x4'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng chkpt table detail'
    sr__ksh[cmd_index]='xtc_show -c 0xc -t 0x2'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng ipv4 topology'
    sr__ksh[cmd_index]='xtc_show -c 0x1 -t 0x1 -a 0x1'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng ipv4 prefix'
    sr__ksh[cmd_index]='xtc_show -c 0x2 -t 0x1 -a 0x1'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng ipv6 topology'
    sr__ksh[cmd_index]='xtc_show -c 0x1 -t 0x1 -a 0x2'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng ipv6 prefix'
    sr__ksh[cmd_index]='xtc_show -c 0x2 -t 0x1 -a 0x2'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng on-demand color private'
    sr__ksh[cmd_index]='xtc_show -c 0x6 -t 0x3'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng pcc ipv4 peer internal'
    sr__ksh[cmd_index]='pcc_show -c 0x1 -v 0x9'

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng pcc lsp internal'
    sr__ksh[cmd_index]='pcc_show -c 0x2 -v 0x9'

    ((cmd_index++))
    sr_exec[cmd_index]=''
    sr__ksh[cmd_index]=''

    # Execute the commands now
    debug_echo "Executing commands"

    exec_commands bgp
    exec_commands mpls
    exec_commands sr

}

# Function to display command for standby location
display_rp_standby() {

    cmd_index=0;
    
    # Initializing commands
    debug_echo "Initializing commands"

    ((cmd_index++))
    sr_exec[cmd_index]='show segment-routing traffic-eng trace'
    sr__ksh[cmd_index]='xtc_show_trace'

    ((cmd_index++))
    sr_exec[cmd_index]=''
    sr__ksh[cmd_index]=''

    # Execute the commands now
    debug_echo "Executing commands"

    exec_commands sr
}

# Function to display RP/DRP commands
display_rp() {
    
    # Only two locations are valid in RP/DRP. 
    # The active and the standby location
    if is_loc_active "xtc_agent" $location; then
        
        debug_echo "RP type: Active"

        display_rp_active;

    elif is_loc_standby "xtc_agent" $location; then
        
        debug_echo "RP type: Standby"

        display_rp_standby;

    else

        # Only active and standby locations are supported
        debug_echo "Location not supported ... skipping"

    fi
}

# A function called display() must be provided that calls the functions to 

# run the required show commands. The display() function will be called in 
# the /pkg/bin/show_tech_comp_file_frag

display() {

    debug_echo "__cardtype=$__cardtype"
    debug_echo "__filename=$__filename"
    debug_echo "platform=$platform"
    debug_echo "location=$location"
    
    # Print the output heading 
    print_main_heading "show tech-support segment-routing traffic-eng"
    
    # Print the specific commands as per the card type. 
    # Right now only RP|DRP are supported
    case "$__cardtype" in
    
    SYS)
        
        debug_echo "Card type: SYS"
       
        display_sys 
        
        ;;

    RP|DRP)
        
        debug_echo "Card type: RP|DRP"
        
        display_rp
       
        ;;
    
    esac
 
    # Print the closing heading. 
    print_main_heading "show tech-support segment-routign traffic-eng 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

