#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_lpts_platform - PD show commands for show tech-support lpts
#                       spawned by show_tech_fast
#
# July 2009, Abhishek Dikshit
# October 2015, Nikki Kumar
#
# Copyright (c) 2009-2015, 2017 by cisco Systems, Inc.
# All rights reserved.
# --------------------------------------------------------------------
       
# ***********************************************************
#  Show commands to be run by the show tech-support commands
#  The PD commands specified here are included in show_tech_lpts
#  This file is sourced in show_tech_lpts.
# ***********************************************************

# This script runs all the show commands required for the lpts platform 
# show tech-support command.
#
# Options:
#
#   -I iterations           Number of times commands needs to captured
#   -T timeduration         Time duration between iterations
#   -f disk filename        Writes output to disk and filename specified
#   -l location             Gives information only for specified location 
#

# 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

# do_platform_asr9k will be called form PI show tech lpts file. 
do_platform_asr9k() {

. /pkg/bin/show_tech_main_fragment

# Set the default values for show tech-support lpts

# Number of iterations the commands needs to be captured
iterations=2

# Time duration in secs between iterations
timeduration=10

__cardtype="unspecified"

############################################################
# Commands to execute on RP nodes per iteration
command_rp_exec[1]='show drops all ongoing location all'
command_rp__ksh[1]='packet_drops.sh all ON all OFF $platform'
command_rp_exec[2]='show lpts ifib slices statistics'
command_rp__ksh[2]='show_lpts_ifib -m slices -s'
command_rp_exec[3]=''
command_rp__ksh[3]=''

############################################################
# Commands to execute on all LC per iteration
command_lc_exec[1]="show lpts pifib hardware entry type ipv4 statistics location $location"
command_lc__ksh[1]='platform_show_pifib -z 0x3 -r 0 -i $fq_nodeid'
command_lc_exec[2]="show lpts pifib hardware entry type ipv6 statistics location $location"
command_lc__ksh[2]='platform_show_pifib -z 0x3 -r 1 -i $fq_nodeid'
command_lc_exec[3]="show lpts pifib hardware police location $location"
command_lc__ksh[3]='platform_show_pifib -z 0x5 -i $fq_nodeid'
command_lc_exec[4]="show lpts pifib hardware static-police location $location"
command_lc__ksh[4]='platform_show_pifib -z 0x6 -i $fq_nodeid'
command_lc_exec[5]="show lpts pifib hardware entry brief shadow location $location"
command_lc__ksh[5]='platform_show_pifib -z 0x2 -d -i $fq_nodeid'
command_lc_exec[6]='show controllers np counters all'
command_lc__ksh[6]='prm_np_show counters'
command_lc_exec[7]=''
command_lc__ksh[7]=''

#############################################################
# Commands to execute on all LCs only once
command_lc_once_exec[1]="show lpts pifib hardware entry location $location"
command_lc_once__ksh[1]='platform_show_pifib -z 0x1 -i $fq_nodeid'
command_lc_once_exec[2]="show lpts pifib hardware usage location $location"
command_lc_once__ksh[2]='platform_show_pifib -z 0x0 -i $fq_nodeid'
command_lc_once_exec[3]="show lpts pifib hardware context location $location"
command_lc_once__ksh[3]='platform_show_pifib -z 0x8 -i $fq_nodeid'
command_lc_once_exec[4]="show lpts pifib hardware entry shadow location $location"
command_lc_once__ksh[4]='platform_show_pifib -z 0x1 -d -i $fq_nodeid'
command_lc_once_exec[5]="show uidb index location $location"
command_lc_once__ksh[5]='uidb_show index -a -i $fq_nodeid'
command_lc_once_exec[6]="show prm server tcam entries 144-LT vmr-id 1 15 all"
command_lc_once__ksh[6]='prm_np_show tcam -E -l 0x2 -v 0x1 -n 0x0F -e 0xa'
command_lc_once_exec[7]="show prm server tcam entries 576-LT vmr-id 1 15 all"
command_lc_once__ksh[7]='prm_np_show tcam -E -l 0x3 -v 0x1 -n 0x0F -e 0xa'
command_lc_once_exec[8]="show prm server tcam entries 144-LT vmr-id 1 15 shadow all"
command_lc_once__ksh[8]='prm_np_show tcam -E -l 0x2 -v 0x1 -n 0x0F -r -e 0xa'
command_lc_once_exec[9]="show prm server tcam entries 576-LT vmr-id 1 15 shadow all"
command_lc_once__ksh[9]='prm_np_show tcam -E -l 0x3 -v 0x1 -n 0x0F -r -e 0xa'
command_lc_once_exec[10]='show lpts trace platform errors location $location'
command_lc_once__ksh[10]='platform_pifib_show_trace -E -i $fq_nodeid'
command_lc_once_exec[11]="show lpts trace platform location $location"
command_lc_once__ksh[11]='platform_pifib_show_trace -i $fq_nodeid'
command_lc_once_exec[12]="show prm server trace error"
command_lc_once__ksh[12]='show_prm_server_ltrace -E | grep -E tcam'
command_lc_once_exec[13]=''
command_lc_once__ksh[13]=''



# Parse the arguments to the script.
while [ $# -gt 0 ]; do
  case "$1" in
     -I) iterations="$2"; shift 2;;
     -T) timeduration="$2"; shift 2;;
     -l) location="$3"; shift 3;;
     -t) __cardtype="$2"; shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

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

# ******************************
#  Main function  ##############
# ******************************

display() {

    # Get my node-name
    node_name=`show_platform | grep "My card name" | cut -f2 -d-`
    node_name=`node_conversion -E $node_name`

    # Print the output heading
    print_main_heading "show tech-support lpts platform: $iterations iterations every $timeduration secs. Card:$__cardtype Node:$node_name"
    
    # For some wierd reason, on a node, show-tech does not pass the -l option
    # So $location is not filled up
    # However, $location gets filled up during exec_commands for the first time
    
    if [ "$__cardtype" == "LC" ]; then
        exec_commands "command_lc_once"
    elif [ "$__cardtype" == "RP" ]; then
        exec_commands "command_rp_once"
    fi

    num_iter=1;

    while [[ $iterations -gt 0 ]];do

        print_heading "show tech-support lpts platform for location: $location iteration: $num_iter"

        if [ "$__cardtype" == "LC" ]; then
	    exec_commands "command_lc"
        elif [ "$__cardtype" == "RP" ]; then
            exec_commands "command_rp"
        fi


        (( iterations -= 1 ))
        (( num_iter += 1))

        if [ $iterations -gt 0 ]; then
            sleep $timeduration
        fi

    done

    # Print the closing heading.
    print_main_heading "show tech-support lpts platform complete"

}

. /pkg/bin/show_tech_file_fragment
}
