#!/pkg/bin/ksh
#
# Created by Jisu Bhattacharya, September 2016
#
# DNX platform-dependent "show tech-support" commands.
#
# Copyright (c) 2016-2021, 2023 by Cisco Systems, Inc. 
# All rights reserved.
#----------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

# Password flag to indicate whether log will include pw or not
__cardtype="unspecified"
__password="FALSE"
__sanitized="FALSE"
location=""
bridge_domain=0
bridge_domain_name=""
bd_id=""
hex_bdid_byteswapped="";

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -p) __password="TRUE"; shift 1;;
        -s) __sanitized="TRUE"; shift 1;;
        -t) __cardtype="$2"; shift 2;;
        -L) location="location $2"; all_cards="location $2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
        esac
done

__parm1=`sysmgr_show -o -p exec | grep PID`
__parm2=`echo ${__parm1#*PID:}`
export EXEC_PID=$__parm2

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

# This function retrieves the bg:bd format for the bdid and also
# converts the bd_id to hex, and performs a byteswap to be used 
# for NP struct search
extract_bd_info_using_bdid () {

    # Retrieve bridge-domain brief output, the first argument of the
    # array is the bg:bd output.

    # ensure bd-id is in base-10.
    bd_id=$(echo "obase=10; $bd_id" | bc)

    VAR="$(l2fib_show_client -BridgeDomainAll | grep "  $bd_id ")"
    set -A array $VAR
    bridge_domain_name=${array[0]};   

    hex_bdid=$(echo "obase=16; $bd_id" | bc)

    a[0]="$(echo "$hex_bdid" | cut -c 1)"
    a[1]="$(echo "$hex_bdid" | cut -c 2)"
    a[2]="$(echo "$hex_bdid" | cut -c 3)"
    a[3]="$(echo "$hex_bdid" | cut -c 4)"

    if [ ! -n "${a[0]}" ]; then
        a[3]="0"
        a[2]="0"
        a[1]="0"
        a[0]="0"
    fi

    if [ ! -n "${a[1]}" ]; then
        a[3]="${a[0]}"
        a[2]="0"
        a[1]="0"
        a[0]="0"
    fi

    if [ ! -n "${a[2]}" ]; then
        a[3]="${a[1]}"
        a[2]="${a[0]}"
        a[1]="0"
        a[0]="0"
    fi

    if [ ! -n "${a[3]}" ]; then
        a[3]="${a[2]}"
        a[2]="${a[1]}"
        a[1]="${a[0]}"
        a[0]="0"
    fi

    hex_bdid_byteswapped=${a[2]}${a[3]}${a[0]}${a[1]}
}

# 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.


# Commands for all nodes

AllNodes_exec[1]='show l2vpn forwarding hardware egress detail location $location'
AllNodes__ksh[1]='l2fib_show_client -l $fq_nodeid -E -d'
AllNodes_exec[2]='show adjacency detail hardware location $location'
AllNodes__ksh[2]='aib_show_command -D -W -N $fq_nodeid'
AllNodes_exec[3]='show spp node-counters location $location'
AllNodes__ksh[3]='spp_sysdb_get -L $fq_nodeid node_counters'
AllNodes_exec[4]='show l2vpn platform trace all all location $location'
AllNodes__ksh[4]='show_dpa_l2fib_ltrace -i $fq_nodeid -D -F -G -T -U -E -R -C -V -H -J -A -L'
AllNodes_exec[5]='show l2vpn forwarding bridge-domain hardware egress detail location $location'
AllNodes__ksh[5]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -E -d'
AllNodes_exec[6]='show adjacency platform trace location $location'
AllNodes__ksh[6]='dpa_aib_show_ltrace -i $fq_nodeid'
AllNodes_exec[7]='show spp trace platform common error location $location'
AllNodes__ksh[7]='spp_fretta_show_ltrace -i $fq_nodeid -E'
AllNodes_exec[8]='show grid trace all location $location'
AllNodes__ksh[8]='rid_show_ltrace -EV -i $fq_nodeid'
AllNodes_exec[9]='show l2vpn forwarding bridge-domain detail location $location'
AllNodes__ksh[9]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -d'
AllNodes_exec[10]='show ether-ea trace all location $location'
AllNodes__ksh[10]='dpa_ether_ea_show_ltrace -i $fq_nodeid -E -V'
AllNodes_exec[11]='show l2vpn forwarding bridge-domain mroute ipv4 hardware ingress detail location $location'
AllNodes__ksh[11]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -I -d -t'
AllNodes_exec[12]='show l2vpn forwarding bridge-domain mroute ipv6 hardware ingress detail location $location'
AllNodes__ksh[12]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -I -d -t'
AllNodes_exec[13]='show l2vpn platform trace-rp forwarding vpls all location $location'
AllNodes__ksh[13]='show_fretta_l2fib_rp_ltrace -i $fq_nodeid -1 -T -U -E -R -C -V'
AllNodes_exec[14]='show igmp snooping l2fib-trace rp'
AllNodes__ksh[14]='fretta_l2fib_trace -s R -l'
AllNodes_exec[15]='show cef ext-client hardware egress detail location $location'
AllNodes__ksh[15]='fib_show_command -t -O 0x0 -e -H egress -d -N $fq_nodeid'
AllNodes_exec[16]=''
AllNodes__ksh[16]=''

# the following set are commands that only need to be executed on a LC
Support_LC_exec[1]='show ofa trace location $location'
Support_LC__ksh[1]='ofa_show_ltrace -w -i $fq_nodeid'
Support_LC_exec[2]='show controllers fia trace all location $location'
Support_LC__ksh[2]='fia_show_ltrace -A -i $fq_nodeid'
Support_LC_exec[3]='show l2vpn forwarding hardware egress private location $location'
Support_LC__ksh[3]='l2fib_show_client -E -p -l $fq_nodeid'
Support_LC_exec[19]='dnx_l2fib_mac_event_log_show -a'
Support_LC__ksh[19]='dnx_l2fib_mac_event_log_show -a'
Support_LC_exec[20]='cat /var/volatile/tmp/mac_log.txt'
Support_LC__ksh[20]='cat /var/volatile/tmp/mac_log.txt'
Support_LC_exec[21]=''
Support_LC__ksh[21]=''

# the following will execute only on the RP


Common_exec[1]='show running-config'
Common__ksh[1]='nvgen -c -l 1 -t 1 -o 1'
Common_exec[2]='show platform'
Common__ksh[2]='show_platform_sysdb'
Common_exec[3]='show platform vm'
Common__ksh[3]='show_platform_sysdb -v'
Common_exec[3]='show l2vpn bridge brief'
Common__ksh[3]='l2vpn_show -r 0x9'
Common_exec[4]=''
Common__ksh[4]=''

# 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_heading "Executing specific commands on $__cardtype cardtype and $__location location " 

echo "This node info:  intnode $int_node_name extnode $ext_node_name location $location fq_nodeid $fq_nodeid" 

    check_platform_type=""
    check_platform_type=`get_platform_type`

    if [ $bridge_domain == 1 ]; then
        extract_bd_info_using_bdid
    fi

    # Execute commands
    case "$__cardtype" in
    "LC")
        # execute the command only on a LC node
        print_heading "Executing specific commands on an LC node" 
        exec_commands AllNodes_LC
        exec_commands AllNodes
        exec_commands Support_LC
        ;;
    
    "RP")
        # Execute commands only on the RP
        print_heading "Executing specific commands on an RP node" 
       
        # Currently exec_commands_rack_nodes is not supported on eXR platforms.
        # if needed we can refer my_exec_commands_rack_nodes definition from 
        # /l2vpn/scripts/l2vpn_show_tech_infra in main lineup.
        exec_commands AllNodes
        ;;
    esac
    exec_commands Common
}

. /pkg/bin/show_tech_file_fragment
