#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_lpts_platform - PD show commands for show tech-support lpts
#                       spawned by show_tech_fast
#
# March 2018, Nikki Kumar
#
# Copyright (c) 2018-2019 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=3

# Time duration in secs between iterations
timeduration=5

__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"

############################################################
# Commands to execute on all LC per iteration
command_lc_exec[1]="show lpts pifib hardware entry statistics location $location"
command_lc__ksh[1]="platform_show_pifib -z 0x3 -i $fq_nodeid"
command_lc_exec[2]="show lpts pifib hardware police location $location"
command_lc__ksh[2]="platform_show_pifib -z 0x6 -i $fq_nodeid"
command_lc_exec[3]="show controllers np counters all"
command_lc__ksh[3]="prm_np_show counters"
command_lc_exec[4]="show controllers fabric fia stats location $location"
command_lc__ksh[4]="fsi_show -n $fq_nodeid -f -s"
command_lc_exec[5]="show lpts pifib entry brief statistics location $location"
command_lc__ksh[5]="show_lpts_pifib -z 0x0 -k 0x9 -a 0x2 -i $fq_nodeid"

#############################################################
# Commands to execute on all LCs only once
command_lc_once_exec[1]="show lpts pifib hardware context location $location"
command_lc_once__ksh[1]="platform_show_pifib -z 0x9 -i $fq_nodeid"
command_lc_once_exec[2]="show lpts pifib hardware entry brief location $location"
command_lc_once__ksh[2]="platform_show_pifib -z 0x2 -i $fq_nodeid"
command_lc_once_exec[3]="show lpts pifib hardware entry location $location"
command_lc_once__ksh[3]="platform_show_pifib -z 0x1 -i $fq_nodeid"
command_lc_once_exec[4]="show lpts trace platform error location $location"
command_lc_once__ksh[4]="platform_pifib_show_trace -E -i $fq_nodeid"
command_lc_once_exec[5]="show lpts trace platform event location $location"
command_lc_once__ksh[5]="platform_pifib_show_trace -V -i $fq_nodeid"
command_lc_once_exec[6]="show controllers rm trm table name PUNT-REASON-STR npu all summary location $location"
command_lc_once__ksh[6]="trm_show summary -f 0x40 -e 0xff -l $fq_nodeid"
command_lc_once_exec[7]="show controllers rm trm table name PUNT-REASON-STR npu all detail all-entries location $location"
command_lc_once__ksh[7]="trm_show detail -f 0x40 -e 0xff -l $fq_nodeid"
command_lc_once_exec[8]="show controllers rm trm table name LPTS-HASH-STR npu all summary location $location"
command_lc_once__ksh[8]="trm_show summary -f 0x18 -e 0xff -l $fq_nodeid"
command_lc_once_exec[9]="show controllers rm trm table name LPTS-HASH-STR npu all detail all-entries location $location"
command_lc_once__ksh[9]="trm_show detail -f 0x18 -e 0xff -l $fq_nodeid"
command_lc_once_exec[10]="show controllers rm trm table name LPTS-KEY-SEL-STR npu all summary location $location"
command_lc_once__ksh[10]="trm_show summary -f 0x17 -e 0xff -l $fq_nodeid"
command_lc_once_exec[11]="show controllers rm trm table name LPTS-KEY-SEL-STR npu all detail all-entries location $location"
command_lc_once__ksh[11]="trm_show detail -f 0x17 -e 0xff -l $fq_nodeid"

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