#! /pkg/bin/ksh
# ---------------------------------------------------------------------
# l2vpn_show_tech_l2fib - L2FIB showtech script
#
# February 2017, Mark Sains
#
# Copyright (c) 2017-2018 by cisco Systems, Inc.
# All rights reserved.
#----------------------------------------------------------------------

. /pkg/bin/show_tech_main_fragment

# Password flag to indicate whether log will include pw or not


# Parse the arguments to the script.
default_parser_function "$@"


my_echo() {
    #
    # Echos are no longer included in the collected output, but rather 
    # displayed directly to console. So by default this function does nothing, 
    # but is provided so that the echo can be uncommented for debugging
    # purposes.
    #
    # If these echos do need to be included in the output, then this function 
    # should be changed to add echo lines to a buffer, which is then displayed
    # as a show tech command, e.g.
    #
    #    rp_exec[2]="Echo buffer" 
    #    rp__ksh[2]="echo $__echo_buff"
    #

    # echo "$*"
    :
}


define_rack_nodes() {

    location=$ext_node_name
    node_list=`sdr_show_info -l | grep IOS | cut -c12-22`
    state_list=`sdr_show_info -l | grep IOS | cut -c40-51`
    __standby_node="FALSE"
    i=0; 
    for state in $state_list; do
        state_list[$i]=$state
        my_echo "state = $state"
        i=$(($i + 1))
        done    

    i=0; 
    for node in $node_list; do
        # check if this is a standby Node. If it is, show only command for this node
        # and ignore the other nodes that can be at a different release
        my_echo "node = $node"
        if [ $node == $location ]; then
           if [ ${state_list[$i]} == "Standby" ]; then
               __standby_node="TRUE"
           fi
        fi
        i=$(($i + 1))
        done    

    i=0; 
    j=0;
    for node in $node_list; do
        add_node="FALSE"
        # check if this is a standby Node. If it is, show only command for this node
        # and ignore the other nodes that can be at a different release
        if [ $__standby_node == "TRUE" ]; then
            # add node to the list only if this is the standby node 
            if [ $node == $location ]; then
               my_echo "Adding only standby node $node to the node list (ISSU requirement) "
               add_node="TRUE"
            else
               my_echo "Skip   normal       node $node  "
            fi
        else
            # add node to the list only if it is NOT a standby node
            if [ ${state_list[$j]} != "Standby" ]; then
               my_echo "Adding normal node $node to the node list "
               add_node="TRUE"
            else
               my_echo "Skip standby  node $node  "
            fi
        fi    

        if [ $add_node == "TRUE" ]; then
        location_list[$i]=$node
        int_node_name_list[$i]=`node_conversion -N ${location_list[$i]}`
        pq_nodeid_list[$i]=`node_conversion -I ${location_list[$i]}`
        fq_nodeid_list[$i]=`node_conversion -i ${int_node_name_list[$i]}`

        i=$(($i + 1))
        fi
        j=$(($j + 1))
        done    
    node_list_length=$i

}


# define our local macro to traverse only valid nodes
my_exec_commands_rack_nodes() {


    cmd_set="$1"
    rack_prefix=`uname -n | cut -d_ -f1`

    cmd_idx=1;
    location='$location'
    cmd_exec=$(eval "echo \${"$cmd_set"_exec[$cmd_idx]}")
    while [ -n "$cmd_exec" ]; do
        node_idx=0;
        while [ $node_idx -lt $node_list_length ]; do

            # initialize variables
 
            location=${location_list[$node_idx]}
            pq_nodeid=${pq_nodeid_list[$node_idx]}
            fq_nodeid=${fq_nodeid_list[$node_idx]}

            cmd_exec_loc=$(eval "echo $cmd_exec")
            cmd__ksh=$(eval "echo \${"$cmd_set"__ksh[$cmd_idx]}")
            cmd__ksh=$(eval "echo $cmd__ksh")

            add_commands "" "${cmd_exec_loc}" "${cmd__ksh}"
            node_idx=$(($node_idx + 1))

        done # end for

        # next command from array
        cmd_idx=$(($cmd_idx + 1))
        location='$location'
        cmd_exec=$(eval "echo \${"$cmd_set"_exec[$cmd_idx]}")
    done # end while
} 


# The following commands will execute only on RPs
rp_exec[1]='show proc l2fib_mgr txgroup trace names location $location'
rp__ksh[1]='txlist_show_trace -i $fq_nodeid -P l2fib_mgr -F 0x1'
rp_exec[2]='show proc l2fib_mgr txgroup txlist location $location'
rp__ksh[2]='txlib_show -N $fq_nodeid -E l2fib_mgr -L'
rp_exec[3]='show proc l2fib_mgr txgroup peer all detail location $location'
rp__ksh[3]='txlib_show -N $fq_nodeid -E l2fib_mgr  -P -d -a'
rp_exec[4]='show proc l2fib_mgr txgroup trace events location $location'
rp__ksh[4]='txlist_show_trace -i $fq_nodeid -P l2fib_mgr -F 0x2'
rp_exec[5]='show proc l2fib_mgr txgroup trace names location $location'
rp__ksh[5]='txlist_show_trace -i $fq_nodeid -P l2fib_mgr -F 0x1'
rp_exec[6]='show l2vpn forwarding api-statistics all detail'
rp__ksh[6]='l2fib_api_stats_cmd all -d'
rp_exec[7]=''
rp__ksh[7]=''


# Node 2 items are only run on LC locations
rplc_exec[1]='show l2vpn forwarding private no-statistics location $location'
rplc__ksh[1]='l2fib_show_client -l $fq_nodeid -p -q'
rplc_exec[2]='show l2vpn forwarding l2tpv2 private no-statistics location $location'
rplc__ksh[2]='l2fib_show_client -l $fq_nodeid -p -q -l2tpv2'
rplc_exec[3]='show l2vpn forwarding resource detail location $location'
rplc__ksh[3]='l2fib_show_client -l $fq_nodeid -Resource -d'
rplc_exec[4]='show l2vpn forwarding pwgroup private location $location'
rplc__ksh[4]='l2fib_show_client -l $fq_nodeid -p -pwgroup_all 0.0.0.0 0x0 0x0'
rplc_exec[5]='show l2vpn forwarding issu location $location'
rplc__ksh[5]='l2fib_show_client -l $fq_nodeid -issu'
rplc_exec[6]='show l2vpn forwarding bridge-domain private no-statistics location $location'
rplc__ksh[6]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -p -q'
rplc_exec[7]='show l2vpn forwarding bridge-domain mroute ipv4 summary location $location' #needed?
rplc__ksh[7]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -t -Q'
rplc_exec[8]='show l2vpn forwarding bridge-domain mroute ipv4 detail location $location' #needed?
rplc__ksh[8]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -d -t'
rplc_exec[9]='show l2vpn forwarding bridge-domain mroute ipv4 debug location $location' #needed?
rplc__ksh[9]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -D -t'
rplc_exec[10]='show l2vpn forwarding l2tp disposition hardware location $location'
rplc__ksh[10]='l2fib_show_client -l $fq_nodeid -l2tpv3Disp -I'
rplc_exec[11]='show l2vpn forwarding l2tp disposition l2tpv2 hardware location $location'
rplc__ksh[11]='l2fib_show_client -l $fq_nodeid -l2tpv2Disp -I'
rplc_exec[12]='show l2vpn forwarding pbb backbone-source-mac location $location'
rplc__ksh[12]='l2fib_show_client -l $fq_nodeid -k'
rplc_exec[13]='show l2vpn forwarding mstp private location $location'
rplc__ksh[13]='l2fib_show_client -l $fq_nodeid -p -MstpAll'
rplc_exec[14]='show l2vpn forwarding ethernet ring g8032 private location $location'
rplc__ksh[14]='l2fib_show_client -l $fq_nodeid -p -GRingAll'
rplc_exec[15]='show l2vpn forwarding message counters private location $location'
rplc__ksh[15]='l2fib_show_client -l $fq_nodeid -M -p'
rplc_exec[16]='show l2vpn forwarding protection main-interface private location $location'
rplc__ksh[16]='l2fib_show_client -l $fq_nodeid -p -MIntfAll'
rplc_exec[17]='show l2vpn forwarding gsp location $location'
rplc__ksh[17]='l2fib_show_client -l $fq_nodeid -o 0xffff'
rplc_exec[18]='show l2vpn forwarding gsp queue history location $location'
rplc__ksh[18]='l2fib_show_client -l $fq_nodeid -o 0xffff -V'
rplc_exec[19]='show l2vpn forwarding object-queues location $location'
rplc__ksh[19]='l2fib_show_client -l $fq_nodeid -r 0xffff'
rplc_exec[20]='show l2vpn forwarding object-queues history location $location'
rplc__ksh[20]='l2fib_show_client -l $fq_nodeid -r 0xffff -V'
rplc_exec[21]='show l2vpn forwarding bridge-domain mac-address summary location $location'
rplc__ksh[21]='l2fib_show_client -l $fq_nodeid -mac_table_summary'
rplc_exec[22]='show l2vpn forwarding bridge-domain p2mp ptree detail location $location'
rplc__ksh[22]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -d -p2mpPtreeFecAll 0x0 0x0 0x0 0.0.0.0'
rplc_exec[23]='show l2vpn forwarding bridge-domain mmrp detail location $location'
rplc__ksh[23]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -d -mmrp'
rplc_exec[24]='show system statistics component l2fib location $location'
rplc__ksh[24]='ship_show -c l2fib'
rplc_exec[25]='show l2vpn forwarding summary private location $location'
rplc__ksh[25]='l2fib_show_client -l $fq_nodeid -S -p'
rplc_exec[26]='show l2vpn forwarding bridge-domain evpn inclusive-multicast private location $location'
rplc__ksh[26]='l2fib_show_client -l $fq_nodeid -BridgeDomainAll -p -imcast'
rplc_exec[27]=''
rplc__ksh[27]=''

# Nodes6 and Nodes7 are to be executed only on RPs 
# It will execute a command for all LC location including itself
#  Standby node will only execute commmands for itself
rack_exec[1]='show l2vpn forwarding retry-list location $location'
rack__ksh[1]='l2fib_show_client -l $fq_nodeid -r'
rack_exec[2]='show l2vpn forwarding dhcp binding private location $location'
rack__ksh[2]='l2fib_show_client -l $fq_nodeid -p -DHCPBinding'
rack_exec[3]=''
rack__ksh[3]=''


display() {
    case "$__cardtype" in
    "SYS")
        if [ "$location" != "" ]; then
            exec_commands rack
        fi
        ;;

    "LC")
        exec_commands rplc
        ;;
   
    "RP")
        exec_commands rplc
        exec_commands rp
        if [ "$location" == "" ]; then
            define_rack_nodes
            my_exec_commands_rack_nodes rack
        fi
        ;;
    esac
}

# 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

