#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# macsec_show_techsupport_pd_cmn - PD common Show tech-support macsec
#
# June 2019
#
# Copyright (c) 2019, 2022-2023 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------
. /pkg/bin/show_tech_main_fragment

intf_list_arr=$(im_show "database" "-v" "0x0" "-l" "0x2" | more "-f" | \
    grep -E  "^Interface (Ten|Twenty|Forty|Hundred|Gigabit|Four|Fifty|Two|TwentyFive).*([0-9]+(/[0-9]+)+)(\.[0-9]+)?"| \
    sed 's/,//g' | uniq | cut -d ' ' -f2)

intf_list_arr=$(echo $intf_list_arr |tr '\n' ' '|sed 's/ /#\n/g')

# Get the list of interfaces in which we have macsec running.
# These cannot be used directly for passing to client processes.
# Store the interface in the form R/S/I/P  only.
# They will be compared with interfaces on this node obtained from IM

macsec_interfaces_from_mka=$(show_macsec_mka -m -i all | tail -n+4 | \
    tr -s ' '  | cut -d' ' -f2 | sed '/^$/d' | sed "s/^[a-zA-Z]*//")

    i=1
    #Determining card type to run the CLI  
    cardtype=`node_type`

    #libsecy_driver_main.so will be packaged only on supported LCs on modular
    #plattforms, on RP on fixed platforms
    if [ -f /pkg/lib/libsecy_driver_main.so ]; then
	
        # Iterate mka list of interfaces and compare with IM interface 
        for msc_if in ${macsec_interfaces_from_mka[@]}; do
            msc_if_str=$(echo $intf_list_arr| grep -o "\S*$msc_if#" | sed 's/#//')
            k=1

            if [[ $msc_if_str ]]; then
                msc_if_str_internal=`convert_interface_fmt '-i' $msc_if_str`

                msc_intf_clis_exec[k]='show macsec mka statistics interface $msc_if_str'
                msc_intf_clis__ksh[k]='show_macsec_mka -m -t intf -f $msc_if_str_internal'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show macsec platform hardware flow interface $msc_if_str'
                msc_intf_clis__ksh[k]='platform_macsec_show_ea -f -i $msc_if_str_internal -h'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show macsec platform hardware sa interface $msc_if_str'
                msc_intf_clis__ksh[k]='platform_macsec_show_ea -j -i $msc_if_str_internal -h'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show macsec platform hardware stats interface $msc_if_str'
                msc_intf_clis__ksh[k]='platform_macsec_show_ea -s -i $msc_if_str_internal -h'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show macsec platform stats interface $msc_if_str'
                msc_intf_clis__ksh[k]='platform_macsec_show_ea -s -i $msc_if_str_internal'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show macsec secy stats interface $msc_if_str'
                msc_intf_clis__ksh[k]='show_macsec_secy -s -f $msc_if_str_internal'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show controllers phy stats interface $msc_if_str stats'
                msc_intf_clis__ksh[k]='optics_driver_phy_show -s -i $msc_if_str_internal'
                k=$(($k + 1))

                msc_intf_clis_exec[k]='show controllers $msc_if_str stats'
                msc_intf_clis__ksh[k]='ethernet_show_controller -i $msc_if_str_internal -s stats'
                k=$(($k + 1))
            fi
            msc_intf_clis_exec[k]=''
            msc_intf_clis__ksh[k]=''
            exec_commands msc_intf_clis
        done
        i=1

        msc_clis_exec[i]='show macsec platform idb location $ext_node_name'
        msc_clis__ksh[i]='platform_macsec_show_ea -n -l $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show controllers macsec idb location $ext_node_name'
        msc_clis__ksh[i]='secy_driver_show -o -l $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show macsec platform trace all location $ext_node_name'
        msc_clis__ksh[i]='platform_macsec_show_trace -N -h $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show controllers macsec trace all location $ext_node_name'
        msc_clis__ksh[i]='secy_driver_show_trace -N -h $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show process optics_driver location $ext_node_name'
        msc_clis__ksh[i]='sysmgr_show -o -p optics_driver -n $fq_nodeid'
        i=$(($i + 1))

        msc_clis_exec[i]='show controllers npu stats traps-all instance all location $ext_node_name'
        msc_clis__ksh[i]="ofa_npu_stats_show -v a -i 0x10 -n $fq_nodeid -t t -p 0xffffffff -s A -d 0x0 -T 0x0 -P 0xffffffff -c 0xff"
        i=$(($i + 1))

        msc_clis_exec[i]='show spp node-counters location $ext_node_name'
        msc_clis__ksh[i]='spp_sysdb_get -L $fq_nodeid node_counters'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol slow location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace -i $fq_nodeid -M -S'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol txpkt last 1000 location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace -n 0x3e8 -i $fq_nodeid -M -T'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol rxpkt last 1000 location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace -n 0x3e8 -i $fq_nodeid -M -R'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol-dot1x slow location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace -i $fq_nodeid -Y -S'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol-dot1x txpkt last 1000 location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace -n 0x3e8 -i $fq_nodeid -Y -T'
        i=$(($i + 1))

        msc_clis_exec[i]='show spio platform trace eapol-dot1x rxpkt last 1000 location $ext_node_name'
        msc_clis__ksh[i]='spio_pd_show_ltrace  -n 0x3e8 -i $fq_nodeid -Y -R'
        i=$(($i + 1))
    fi

    msc_clis_exec[i]=''
    msc_clis__ksh[i]=''
    exec_commands msc_clis

    if [ $cardtype == "RP" ]; then
        # Card type is RP
        # Iterate mka list of interfaces and compare with IM interface 
        intf_list_arr=$(im_show "database" "-l" "0x2" | more "-f" | \
            grep -E  "^Interface (Ten|Twenty|Forty|Hundred|Gigabit|Four|Fifty|Two|TwentyFive).*([0-9]+(/[0-9]+)+)(\.[0-9]+)?"| \
            sed 's/,//g' | uniq | cut -d ' ' -f2)

        intf_list_arr=$(echo $intf_list_arr |tr '\n' ' '|sed 's/ /#\n/g')

        i=1
        for msc_if in $macsec_interfaces_from_mka; do
		    #Getting port num of macsec interface
            msc_if_str=$(echo $intf_list_arr| grep -o "\S*$msc_if#" | sed 's/#//')
          	msc_if_str_internal=`convert_interface_fmt '-i' $msc_if_str`
          	if [[ $msc_if_str ]]; then
                rp_msc_clis_exec[i]='show interfaces $msc_if_str'
                rp_msc_clis__ksh[i]='show_interface -i $msc_if_str_internal'
                i=$(($i + 1))
            fi
            rp_msc_clis_exec[k]=''
            rp_msc_clis__ksh[k]=''
            exec_commands rp_msc_clis
	    done
     fi

. /pkg/bin/show_tech_file_fragment
