#!/pkg/bin/ksh
#
# DNX SPAN  "show tech-support" commands.
#
# Copyright (c) 2023 by cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------

# main infra
. /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.

_cardtype="unspecified"
arg_string=""
showtech_argv=""
showtech_argc="0"

while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *) showtech_argv="$showtech_argv $1"; (( showtech_argc+=1 )); shift 1;;
    esac
done

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

# Check if platform is J2
is_j2="No"
check_platform_is_j2() {
    pid=$(fia_driver_show -c show-unit -u 0 -n "$fq_nodeid" | grep "Unit 0" | awk '{print $4}')
    if [ "$pid" == "BCM88820_A1" ] ||
       [ "$pid" == "BCM88850_A0" ] ||
       [ "$pid" == "BCM88485_B1" ] ||
       [ "$pid" == "BCM88690_B1" ]; then
        is_j2="Yes"
    fi
}

# Get number of instances
instance=0
get_num_instances() {
    device_list=`fia_driver_show -d d -i 5 -p 16384 | grep Fabric | egrep -v Mode | grep UP | cut -c2-10 `
    for device in $device_list; do
        instance=`echo $device | cut -d"/" -f3 `
    done
}

# Common commands for lc
cmd_index=1
lc_exec[$cmd_index]='show monitor-session trace platform errors events location $location'
lc__ksh[$cmd_index]='dnx_span_show_ltrace -ER'
((cmd_index++))

lc_exec[$cmd_index]='show ofa objects spansession location $location'
lc__ksh[$cmd_index]='dpa_spansession_show_client -b'
((cmd_index++))

lc_exec[$cmd_index]='show controllers npu voq-usage interface all instance all location $location'
lc__ksh[$cmd_index]='dpa_qosea_voq_show -v x -i 0x10 -n $fq_nodeid -t n -p 0x0'
((cmd_index++))

lc_exec[$cmd_index]='show controllers npu stats counters-all instance all location $location'
lc__ksh[$cmd_index]='ofa_npu_stats_show -v a -i 0x10 -n $fq_nodeid -t e -p 0xffffffff -s 0x0 -d 0x0'
((cmd_index++))

# Shell commands for J
cmd_index=1
dshell_cmd_j[$cmd_index]='dump chg IHP_LL_MIRROR_PROFILE'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IRR_SNOOP_MIRROR_DEST_TABLE'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IHB_PINFO_PMF'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IPT_ARAD_SNP_MIR_STAMP_EN'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IHB_SNP_ACT_PROFILE'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IPT_ARAD_SNP_MIR_STAMP_VALUES'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IPT_SNP_MIR_CMD_MAP'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IHB_SNOOP_ACTION'
((cmd_index++))

dshell_cmd_j[$cmd_index]='dump chg IRR_SNOOP_MIRROR_TM_TABLE'
((cmd_index++))

# Shell commands for J2
cmd_index=1
dshell_cmd_j2[$cmd_index]='dbal table dump table=SNIF_COMMAND_TABLE'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=SNIF_EGRESS_COMMAND_TABLE'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=EGRESS_MIRROR'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=SNIF_INGRESS_MIRROR_RULE_TABLE'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=SNIF_EGRESS_MIRROR_RULE_TABLE'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=SNIF_CODE_TO_PROFILE_MAP'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=EGRESS_SPAN_FILTER'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=INGRESS_PP_PORT'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=FIELD_PMF_PP_PORT_INFO_IPMF1'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=FIELD_PMF_PP_PORT_INFO_IPMF3'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=ESEM_FORWARD_DOMAIN_MAPPING_DB'
((cmd_index++))

dshell_cmd_j2[$cmd_index]='dbal table dump table=IN_AC_INFO_DB'
((cmd_index++))

# Platform specific commands for J and J2
check_platform_is_j2
get_num_instances
cmd_index=1
if [ "$is_j2" = "Yes" ]; then
    i=0
    while [[ $i -le $instance ]]; do
        for d in "${!dshell_cmd_j2[@]}"; do
            dcmd=$(eval "echo ${dshell_cmd_j2[d]} | sed 's/ /-/g'")
            lc_j2_exec[$cmd_index]="show controllers fia diagshell $i ${dshell_cmd_j2[d]} location \$location"
            lc_j2__ksh[$cmd_index]="fia_driver_show -c $dcmd -u $i -n $fq_nodeid"
            ((cmd_index++))
        done
        ((i++))
    done
else
    i=0
    while [[ $i -le $instance ]]; do
        for d in "${!dshell_cmd_j[@]}"; do
            dcmd=$(eval "echo ${dshell_cmd_j[d]} | sed 's/ /-/g'")
            lc_j_exec[$cmd_index]="show controllers fia diagshell $i ${dshell_cmd_j[d]} location \$location"
            lc_j__ksh[$cmd_index]="fia_driver_show -c $dcmd -u $i -n $fq_nodeid"
            ((cmd_index++))
        done
        ((i++))
    done
fi

# 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() {
    # Execute commands
    print_main_heading "span_show_tech_platform"
    case "$__cardtype" in
    "RP" | "DRP")
        ;;
    "LC")
        # Common commands
        exec_commands lc

        # Platform specific commands
        if [ "$is_j2" = "Yes" ]; then
            exec_commands lc_j2
        else
            exec_commands lc_j
        fi
        ;;
    esac
}

. /pkg/bin/show_tech_file_fragment
