#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# cem_driver_show_techsupport - Show tech-support script for cem_driver
#
# Sep 2021-22, Nikhil Shetty, Sreejith Sreekumaran, Sunny Keshri
#
# Copyright (c) 2017-2019, 2021-2022 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------

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

__cardtype="unspecified"

# Set the default values of the file name for which show tech-support macsec
# is to run and the file it is to write to.
# Read in the arguments to the script, setting node_required and filename
# according to these arguments.
# Note that it is important for security reasons that users can only enter
# alphanumeric filenames and nodes and that anywhere calling this script must
# enforce this.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

 # global commands on RP


###############################################################################
# Show commands that run once per LR                                          #
###############################################################################


sys_exec[1]='show install active'
sys__ksh[1]='sdr_instcmd show install active'

sys_exec[2]='show install repository'
sys__ksh[2]='sdr_instcmd show install repository'

sys_exec[3]='show version'
sys__ksh[3]='ng_show_version'

sys_exec[4]='show platform'
sys__ksh[4]='show_platform_sysdb'

sys_exec[5]='show running-config'
sys__ksh[5]='nvgen -c -i1 -l 1 -t 1 -o 1'

sys_exec[6]='show inventory all'
sys__ksh[6]='show_inventory -e -t'

sys_exec[7]='show interfaces summary'
sys__ksh[7]='show_interface -a -o 0x3'

sys_exec[8]='show interfaces brief'
sys__ksh[8]='show_interface -a -o 0x2'

sys_exec[9]='show process cem_driver location all'
sys__ksh[9]='sysmgr_show -o -p cem_driver -n all'

sys_exec[10]='show hw-mod fpd'
sys__ksh[10]='show_fpd_loc_ng fpd'

sys_exec[11]=''
sys__ksh[11]=''

lc_exec[1]=''
lc__ksh[1]=''

t1e1_exec[1]='show t1e1 ea trace all location all'
t1e1__ksh[1]='show_t1e1_ltrace -E -A -i all'

t1e1_exec[2]='show t1e1 ma trace all location all'
t1e1__ksh[2]='show_t1e1_ltrace -M -A -i all'

t1e1_exec[3]=''
t1e1__ksh[3]=''

sonet_exec[1]='show sonet-ma trace init location all '
sonet__ksh[1]='sonet_ma_show_ltrace -B -i all'

sonet_exec[1]='show sonet-ma trace init location all '
sonet__ksh[1]='sonet_ma_show_ltrace -B -i all'

sonet_exec[2]='show sonet-ma trace info location all '
sonet__ksh[2]='sonet_ma_show_ltrace -I -i all'

sonet_exec[3]='show sonet-ma trace error location all '
sonet__ksh[3]='sonet_ma_show_ltrace -E -i all'

sonet_exec[4]='show sonet-ea trace info location all '
sonet__ksh[4]='sonet_ea_show_ltrace -I -i all'

sonet_exec[5]='show sonet-ea trace error location all '
sonet__ksh[5]='sonet_ea_show_ltrace -E -i all'

sonet_exec[6]=''
sonet__ksh[6]=''

ltrace_exec[1]='Show cem-driver ltrace all location all'
ltrace__ksh[1]='cem_driver_show_ltrace -E -V -B -i all'

ltrace_exec[2]='Show ethernet v-ether trace location all'
ltrace__ksh[2]='eth_intf_show_trace -i all'

ltrace_exec[3]='show ea-server-common ltrace all location all'
ltrace__ksh[3]='ea_server_common_show_ltrace -i all -EVB'

ltrace_exec[4]=''
ltrace__ksh[4]=''
#
# Variable used to enable admin mode commands
#
admin=0;


# Each option collect follows information.
#   -L : local plane.
#   -t : card type.
#   -A : admin + shared + local plane.
#   -S : shared + local plane.
# Notice: current 'show tech multi sysdb' doesn't collect any information
#        from SP because of memory shortage on SP.
#
while [ "$#" -gt "0" ]; do
    case "$1" in
        -A) admin=1;                    shift 1;;
        -S) shared=1;                   shift 1;;
        -L) location="$2";              shift 2;;
        -t) __cardtype="$2";            shift 2;;
        *)  default_parser_function $@; shift $#;;
    esac
done

###############################################################################
# Show controller functions                                                  #
###############################################################################
declare -A pm_duration_dict
pm_duration_dict["30-sec"]=-T
pm_duration_dict["15-min"]=-m
pm_duration_dict["24-hour"]=-d
pm_duration_dict["flex-bin"]=-L

otu_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    intf_recovered -a ${rate}${r_s_i_p} >> $outfile
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} prbs-details\n" >> $outfile
    intf_recovered -r ${rate}${r_s_i_p} >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key otn \n" >> $outfile
            show_controller_otu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key fec \n" >> $outfile
            show_controller_otu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -f >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key prbs \n" >> $outfile
            show_controller_otu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -u >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}

odu_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    intf_otn_odu_recovered -a ${rate}${r_s_i_p} >> $outfile
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} prbs-details\n" >> $outfile
    intf_otn_odu_recovered -e ${rate}${r_s_i_p} >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            if [[ "$key" == '30-sec' ]]; then
                continue
            fi
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key otn pathmonitor \n" >> $outfile
            show_controller_odu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g -P >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key gfp \n" >> $outfile
            show_controller_odu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -b >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key prbs \n" >> $outfile
            show_controller_odu_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -u >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}

sonet_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    exec_sonet_ma show ${rate}${r_s_i_p} info >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key prbs \n" >> $outfile
            show_controller_oc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -P >> $outfile
            if [[ "$key" == '30-sec' || "$key" == 'flex-bin' ]]; then
                continue
            fi
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key ocn \n" >> $outfile
            show_controller_oc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}

ethernet_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    ethernet_show_controller -i ${rate}${r_s_i_p} -s oper >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key ether \n" >> $outfile
            show_controller_ethernet_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -e >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key fec \n" >> $outfile
            show_controller_ethernet_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -f >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key pcs \n" >> $outfile
            show_controller_ethernet_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key prbs \n" >> $outfile
            show_controller_ethernet_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -P >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}


sdh_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    exec_sonet_ma show  ${rate}${r_s_i_p} info >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key prbs \n" >> $outfile
            show_controller_stm_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -P >> $outfile
            if [[ "$key" == '30-sec' || "$key" == 'flex-bin' ]]; then
                continue
            fi
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key stm \n" >> $outfile
            show_controller_stm_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}

fc_controller_helper_function(){
    rate=$1
    rsip=$(echo ${r_s_i_p} | sed 's/\_/\//g')
    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} \n" >> $outfile
    fc_show_ctrlr info ${rate}${r_s_i_p} >> $outfile

    echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
    echo -e "\n show controller ${rate} ${rsip} prbs-info\n" >> $outfile
    fc_show_ctrlr prbs ${rate}${r_s_i_p} >> $outfile
    for key in "${!pm_duration_dict[@]}"
        do
            if [[ "$rate" == 'ThirtyTwoGigFibreChanCtrlr' ]]; then
                echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
                echo -e "\n show controllers ${rate} ${rsip} pm current $key fec \n" >> $outfile
                show_controller_fc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -j >> $outfile
            fi
            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key pcs \n" >> $outfile
            show_controller_fc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -g >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current $key fc \n" >> $outfile
            show_controller_fc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -f >> $outfile

            echo "++++++++++++++++++++++++++++++++++++++++" >> $outfile
            echo -e "\n show controllers ${rate} ${rsip} pm current 24-hour prbs \n" >> $outfile
            show_controller_fc_pm -n ${rate}${r_s_i_p} ${pm_duration_dict[$key]} -P >> $outfile
        done
    echo "----------------------------------------" >> $outfile
}

show_controllers_otu(){
    outfile=$1/otu_controllers
    declare -A otn_otu_rate_dict=(
    ["OTU2"]="OTU2"
    ["OTU2E"]="OTU2E"
    )
    echo "=================================" >> $outfile
    echo "--- show controller OTU : start ---" >> $outfile
    for client_rate in "${!otn_otu_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep -e "$client_rate\>" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                otu_controller_helper_function ${otn_otu_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controller OTU : End ---" >> $outfile
    echo "=================================" >> $outfile
}

show_controllers_odu(){
    outfile=$1/odu_controllers
    declare -A otn_odu_rate_dict=(
    ["OTU2"]="ODU2"
    ["OTU2E"]="ODU2E"
    )
    echo "=================================" >> $outfile
    echo "--- show controller ODU : start ---" >> $outfile
    for client_rate in "${!otn_odu_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep -e "$client_rate\>" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                odu_controller_helper_function ${otn_odu_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controller ODU : End ---" >> $outfile
    echo "=================================" >> $outfile
}

show_controllers_ethernet(){
    outfile=$1/ethernet_controllers
    declare -A ethernet_rate_dict=(
    ["1GE"]="GigabitEthCtrlr"
    ["10GE"]="TenGigECtrlr"
    )
    echo "====================================" >> $outfile
    echo "--- show controllers ETHERNET : Start ---" >> $outfile
    for client_rate in "${!ethernet_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep "$client_rate" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                ethernet_controller_helper_function ${ethernet_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controllers  ETHERNET : End ---" >> $outfile
    echo "===========================================" >> $outfile
}

show_controllers_sonet(){
    outfile=$1/sonet_controllers
    declare -A sonet_rate_dict=(
    ["OC48"]="OC48"
    ["OC192"]="OC192"
    )
    echo "=================================" >> $outfile
    echo "--- show controllers Sonet : Start ---" >> $outfile
    for client_rate in "${!sonet_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep "$client_rate" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                sonet_controller_helper_function ${sonet_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controllers  Sonet : End ---" >> $outfile
    echo "===========================================" >> $outfile
}

show_controllers_sdh(){
    outfile=$1/sdh_controllers
    declare -A sdh_rate_dict=(
    ["STM16"]="STM16"
    )
    echo "=================================" >> $outfile
    echo "--- show controllers SDH : start ---" >> $outfile
    for client_rate in "${!sdh_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep "$client_rate" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                sdh_controller_helper_function ${sdh_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controllers  SDH : End ---" >> $outfile
    echo "====================================" >> $outfile
}

show_controllers_fc(){
    outfile=$1/fc_controllers
    declare -A fc_rate_dict=(
    ["Fibre Channel 1G"]="OneGigFibreChanCtrlr"
    ["Fibre Channel 2G"]="TwoGigFibreChanCtrlr"
    ["Fibre Channel 4G"]="FourGigFibreChanCtrlr"
    ["Fibre Channel 8G"]="EightGigFibreChanCtrlr"
    ["Fibre Channel 16G"]="SixteenGigFibreChanCtrlr"
    ["Fibre Channel 32G"]="ThirtyTwoGigFibreChanCtrlr"
    )
    echo "====================================" >> $outfile
    echo "--- show controller FibreChanCtrlr : start ---" >> $outfile
    for client_rate in "${!fc_rate_dict[@]}"
    do
        /pkg/bin/portmode_show -z all | grep "$client_rate" | while read -r x;
            do
                r_s_i_p=$(echo $x |cut -c 7-13)
                fc_controller_helper_function ${fc_rate_dict[$client_rate]} ${r_s_i_p} $outfile
            done
    done
    echo "--- show controller FibreChanCtrlr : End ---" >> $outfile
    echo "====================================" >> $outfile
}

port_mode_controllers(){
    node_name=`uname -n`
    node_subdir=$__tar_file_directory_on_node/$node_name
    if [ ! -d "$node_subdir" ]; then
        /bin/mkdir $node_subdir
    fi
    show_controllers_otu $node_subdir
    show_controllers_odu $node_subdir
    show_controllers_sonet $node_subdir
    show_controllers_ethernet $node_subdir
    show_controllers_sdh $node_subdir
    show_controllers_fc $node_subdir
}

# Parse the arguments to the script.
# Usage:
#
#sys.exit()
# A function called display() must be provided that calls the functions to
# Print the output heading
do_show_command() {
    print_main_heading "show tech-support cemdriver"
    case "$__cardtype" in
    "SYS")
        exec_commands sys
        ;;
    "DRP"|"RP")
        exec_commands t1e1

        exec_commands sonet

        exec_commands ltrace

        port_mode_controllers
        ;;
    esac

    enable_techs "" /pkg/bin/show_tech_cem

    # Print the closing heading.
    print_main_heading "show tech-support cemdriver complete"
}
# The display function.
display() {
        do_show_command
}

  # 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
