#!/pkg/bin/ksh
#------------------------------------------------------------------
#
# Created by Aparna Pattekar, October 2015.
#
# Copyright (c) 2015-2018 by cisco Systems, Inc. 
# All rights reserved.
#
#------------------------------------------------------------------

#
# Include the standard show tech-support functions
#
. /pkg/bin/show_tech_main_fragment

__cardtype="unspecified"

#
# Find local node information and print all info
#
local_node_name=`uname -n`
local_nodeid=`node_conversion -i $local_node_name`

PLATFORM_TYPE=`uname -m`

#
# Generic Commands
#
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    shared_prologue_cmds_exec[1]="show version"
    shared_prologue_cmds__ksh[1]='ng_show_version'

    shared_prologue_cmds_exec[2]="show install active"
    shared_prologue_cmds__ksh[2]='sdr_instcmd show install active'

    shared_prologue_cmds_exec[3]="show platform"
    shared_prologue_cmds__ksh[3]='show_platform_sysdb'
else
    shared_prologue_cmds_exec[1]="show version"
    shared_prologue_cmds__ksh[1]='show_version'

    shared_prologue_cmds_exec[2]="show install active"
    shared_prologue_cmds__ksh[2]='instcmd show install active'

    shared_prologue_cmds_exec[3]="show platform"
    shared_prologue_cmds__ksh[3]='show_platform'
fi

shared_prologue_cmds_exec[4]="show process l2rib"
shared_prologue_cmds__ksh[4]='sysmgr_show -o -p l2rib'
shared_prologue_cmds_exec[5]="show running"
shared_prologue_cmds__ksh[5]='nvgen -c -l 1 -t 1 -o 1'


# show l2rib command output

# L2RIB_SHOW_L2RIB_CLIENTS
shared_l2rib_cmds_exec[1]="show l2rib clients"
shared_l2rib_cmds__ksh[1]='l2rib_show 0'
shared_l2rib_cmds_exec[2]="show l2rib clients detail"
shared_l2rib_cmds__ksh[2]='l2rib_show -d 0'
shared_l2rib_cmds_exec[3]="show l2rib clients private"
shared_l2rib_cmds__ksh[3]='l2rib_show -p 0'

# L2RIB_SHOW_L2RIB_PRODUCERS
shared_l2rib_cmds_exec[4]="show l2rib producers"
shared_l2rib_cmds__ksh[4]='l2rib_show 1'
shared_l2rib_cmds_exec[5]="show l2rib producers detail"
shared_l2rib_cmds__ksh[5]='l2rib_show -d 1'

# show l2route command output

# L2RIB_SHOW_L2ROUTE_SUMMARY
shared_l2route_cmds_exec[1]="show l2route summary"
shared_l2route_cmds__ksh[1]='l2rib_show 4'

# L2RIB_SHOW_L2ROUTE_EVPN_SUMMARY
shared_l2route_cmds_exec[2]="show l2route topology detail"
shared_l2route_cmds__ksh[2]='l2rib_show -d a'
shared_l2route_cmds_exec[3]="show l2route evpn mac all private"
shared_l2route_cmds__ksh[3]='l2rib_show -A -p 6'
shared_l2route_cmds_exec[4]="show l2route evpn mac-ip all private"
shared_l2route_cmds__ksh[4]='l2rib_show -A -p b'
shared_l2route_cmds_exec[5]="show l2route evpn imet all private"
shared_l2route_cmds__ksh[5]='l2rib_show -A -p c'

# L2RIB BCDLS commands
shared_bcdls_cmds_exec[1]="show bcdlv2 bcdls_l2rib producer-view basic"
shared_bcdls_cmds__ksh[1]='show_bcdlv2 -C 0xa -B bcdls_l2rib'
shared_bcdls_cmds_exec[2]="show bcdlv2 bcdls_l2rib producer-view stat"
shared_bcdls_cmds__ksh[2]='show_bcdlv2 -C 0x12 -B bcdls_l2rib'
shared_bcdls_cmds_exec[3]="show bcdlv2 bcdls_l2rib update-groups tabular"
shared_bcdls_cmds__ksh[3]='show_bcdlv2 -b -C 0x5 -B bcdls_l2rib'
shared_bcdls_cmds_exec[4]="show bcdlv2 bcdls_l2rib consumer-view consumers all"
shared_bcdls_cmds__ksh[4]='show_bcdlv2 -c all -C 0x7 -B bcdls_l2rib'

# L2RIB txlist commands
shared_txlist_cmds_exec[1]="show process l2rib txgroup txlist"
shared_txlist_cmds__ksh[1]='txlib_show -E l2rib -L'

shared_txlist_cmds_exec[2]="show process l2rib txgroup peer all det"
shared_txlist_cmds__ksh[2]='txlib_show -E l2rib -P -d -a'

# L2RIB txlist trace buffers
shared_txlist_cmds_exec[3]="show process l2rib txgroup trace names"
shared_txlist_cmds__ksh[3]='txlist_show_trace -P l2rib -F 0x1'

shared_txlist_cmds_exec[4]="show process l2rib txgroup trace events"
shared_txlist_cmds__ksh[4]='txlist_show_trace -P l2rib -F 0x2'

shared_txlist_cmds_exec[5]="show process l2rib txgroup trace errors"
shared_txlist_cmds__ksh[5]='txlist_show_trace -P l2rib -F 0x3'

shared_txlist_cmds_exec[6]="show process l2rib txgroup trace fast"
shared_txlist_cmds__ksh[6]='txlist_show_trace -P l2rib -F 0x4'

# xostrace
shared_xos_cmds_exec[1]="show l2rib trace errors"
shared_xos_cmds__ksh[1]='l2rib_show_trace -T 0x0'
shared_xos_cmds_exec[2]="show l2rib trace topology"
shared_xos_cmds__ksh[2]='l2rib_show_trace -T 0x1'
shared_xos_cmds_exec[3]="show l2rib trace tx-infra"
shared_xos_cmds__ksh[3]='l2rib_show_trace -T 0x2'
shared_xos_cmds_exec[4]="show l2rib trace client-tbl"
shared_xos_cmds__ksh[4]='l2rib_show_trace -T 0x3'
shared_xos_cmds_exec[5]="show l2rib trace mac"
shared_xos_cmds__ksh[5]='l2rib_show_trace -T 0x4'
shared_xos_cmds_exec[6]="show l2rib trace mac-ip"
shared_xos_cmds__ksh[6]='l2rib_show_trace -T 0x6'
shared_xos_cmds_exec[7]="show l2rib trace rmac"
shared_xos_cmds__ksh[7]='l2rib_show_trace -T 0x5'
shared_xos_cmds_exec[8]="show l2rib trace misc-obj"
shared_xos_cmds__ksh[8]='l2rib_show_trace -T 0x8'

# Generic commands
shared_epilogue_cmds_exec[1]="show logging"
shared_epilogue_cmds__ksh[1]='show_logging'

# Display any cores (-c 0xb will display all cores until now)
shared_epilogue_cmds_exec[2]="show context"
if [[ "${PLATFORM_TYPE}" == "x86_64" ]]; then
    shared_epilogue_cmds__ksh[2]='corehelper_context -c 0x1 -n all'
else 
    shared_epilogue_cmds__ksh[2]='dumper_context -c 0xb'
fi

#
#   -t == SYS :  shared plane.
#   -t != SYS :  local plane.
#

#echo "args: $@"

# While more positional parameters
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

if [ "$__filename" = "unspecified" ]; then
    echo " output file not specified"
    exit
fi


#
# Display function
#
display() {
    #
    # Display a header
    #
    print_main_heading "show tech-support l2rib on location $location"

    if [ "$__cardtype" == "SYS" ]; then

        # run the shared plane commands
        exec_commands shared_prologue_cmds
        exec_commands shared_l2rib_cmds

        # Show l2rib registrations client
        clients=`l2rib_show 0 | sed -n 's/^\(^[0-9]\).*/\1/p'`

        for i in $clients
        do
          # L2RIB_SHOW_L2RIB_REGISTRATIONS_GLB
          tmp_cmds_exec[1]="show l2rib registrations client $i"
          tmp_cmds__ksh[1]="'l2rib_show -C $i 2'"
          exec_commands tmp_cmds
        done

        # Show l2route commands
        exec_commands shared_l2route_cmds

        # BCDL output
        exec_commands shared_bcdls_cmds

        # txlist commands
        exec_commands shared_txlist_cmds
        
        # xos commands
        exec_commands shared_xos_cmds
 
        # epilogue commands
        exec_commands shared_epilogue_cmds
    else
        # Shared prologue commands
        exec_commands shared_prologue_cmds

    fi

    #
    # Display a footer
    #
    print_main_heading "show tech-support l2rib complete"

}

#
# Use the standard show tech-support infra to call our display function and
# send the output to console or file 
#
. /pkg/bin/show_tech_file_fragment
