#! /pkg/bin/ksh
# ---------------------------------------------------------------------
# l2vpn_show_tech_infra - L2vpn infra 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
} 


# 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.
# Do "trace" cmds first, so events won't be lost while collecting other info

# Basic (RP) commands, in order of priority. The commands with the most volatile
# information are first. "show clock" is inserted in order to time them
# and fix speed/priority if necessary.
sys_exec[1]='show ethernet trace all'
sys__ksh[1]='ethernet_trace_show'
sys_exec[2]='show l2vpn ma bvi trace'
sys__ksh[2]='vif_ma_show_ltrace bvi_ma'
sys_exec[3]='show l2vpn resource location $location'
sys__ksh[3]='l2vpn_show -U $fq_nodeid 0x4'
sys_exec[4]='show l2vpn ma bvi detail'
sys__ksh[4]='show_bvi_ma -d 0x1'
sys_exec[5]='show proc bvi_ma txgroup peer all detail'
sys__ksh[5]='txlib_show -E bvi_ma -P -d -a '
sys_exec[6]='show l2vpn ma bvi api-statistics all detail'
sys__ksh[6]='bvi_ma_api_stats_cmd all -d'
sys_exec[7]='show l2vpn capability'
sys__ksh[7]='l2vpn_show -A 0xc'
sys_exec[8]='show proc bvi_ma'
sys__ksh[8]='sysmgr_show -o -p bvi_ma'
sys_exec[9]=''
sys__ksh[9]=''


# The following commands will execute only on RPs
rp_exec[1]='show l2vpn xconnect summary location $location'
rp__ksh[1]='l2vpn_show -U $fq_nodeid 0x6'
rp_exec[2]='show l2vpn bridge-domain summary location $location'
rp__ksh[2]='l2vpn_show -U $fq_nodeid 0xa'
rp_exec[3]='show l2vpn xconnect private location $location'
rp__ksh[3]='l2vpn_show -p -U $fq_nodeid 0x1'
rp_exec[4]='show l2vpn bridge-domain private location $location'
rp__ksh[4]='l2vpn_show -p -U $fq_nodeid 0x9'
rp_exec[5]='show l2vpn session-db private location $location'
rp__ksh[5]='l2vpn_show -p -U $fq_nodeid 0x29'
rp_exec[6]='show l2vpn collaborators location $location'
rp__ksh[6]='l2vpn_show -U $fq_nodeid 0x2'
rp_exec[7]='show l2vpn counters chunk location $location'
rp__ksh[7]='l2vpn_show -U $fq_nodeid 0x5'
rp_exec[8]='show l2vpn chkpt summary location $location'
rp__ksh[8]='l2vpn_show -U $fq_nodeid 0xf'
rp_exec[9]='show l2vpn database node location $location'
rp__ksh[9]='l2vpn_show -U $fq_nodeid 0xe'
rp_exec[10]='show l2vpn xconnect mp2mp detail location $location'
rp__ksh[10]='l2vpn_show -d -U $fq_nodeid 0x17'
rp_exec[11]='show l2vpn ipc-stats location $location'
rp__ksh[11]='l2vpn_show -L $fq_nodeid 0x1d'
rp_exec[12]='show l2vpn event location $location'
rp__ksh[12]='l2vpn_show -U $fq_nodeid 0x20'
rp_exec[13]='show generic-interface-list location $location'
rp__ksh[13]='iflist_show -U $fq_nodeid'
rp_exec[14]='show l2vpn generic-interface-list detail location $location'
rp__ksh[14]='l2vpn_show -d -U $fq_nodeid 0x26'
rp_exec[15]='show l2vpn protection main-interface private location $location'
rp__ksh[15]='l2vpn_show -p -U $fq_nodeid 0x21'
rp_exec[16]='show l2vpn ethernet ring g8032 private location $location'
rp__ksh[16]='l2vpn_show -d -U $fq_nodeid 0x22'
rp_exec[17]='show l2vpn index private location $location'
rp__ksh[17]='l2vpn_show -p -U $fq_nodeid 0x25'
rp_exec[18]='show l2vpn event counters location $location'
rp__ksh[18]='l2vpn_show -U $fq_nodeid 0xd'
rp_exec[19]='show l2vpn provision queue location $location'
rp__ksh[19]='l2vpn_show -U $fq_nodeid 0x2b'
rp_exec[20]='show proc l2vpn_mgr txgroup txlist'
rp__ksh[20]='txlib_show -E l2vpn_mgr -L'
rp_exec[21]='show proc l2vpn_mgr txgroup peer all detail'
rp__ksh[21]='txlib_show -E l2vpn_mgr  -P -d -a'
rp_exec[22]='show l2vpn red-db private location $location'
rp__ksh[22]='l2vpn_show -U $fq_nodeid 0x2a'
rp_exec[23]='show l2vpn mstp port private'
rp__ksh[23]='l2vpn_show -p 0x10'
rp_exec[24]='show l2vpn pbb backbone-source-mac location $location'
rp__ksh[24]='l2vpn_show -U $fq_nodeid 0x1e'
rp_exec[25]='show l2vpn iccp-sm private location $location'
rp__ksh[25]='l2vpn_show -U $fq_nodeid 0x35'
rp_exec[26]='show l2vpn database rg all location $location'
rp__ksh[26]='l2vpn_show -p -U $fq_nodeid 0x36'
rp_exec[27]='show l2vpn configuration history added location $location'
rp__ksh[27]='l2vpn_show -5 0x1 -U $fq_nodeid 0x38'
rp_exec[28]='show l2vpn configuration history deleted location $location'
rp__ksh[28]='l2vpn_show -5 0x2 -U $fq_nodeid 0x38'
rp_exec[29]='show l2vpn configuration history errors location $location'
rp__ksh[29]='l2vpn_show -5 0x3 -U $fq_nodeid 0x38'
rp_exec[30]='show proc l2vpn_mgr location $location'
rp__ksh[30]='sysmgr_show -o -p l2vpn_mgr -n $fq_nodeid'
rp_exec[31]='show l2vpn atom ldp det location $location'
rp__ksh[31]='l2vpn_show -d -F -U $fq_nodeid 0x3'
rp_exec[32]='show l2vpn flexible-xconnect-service summary'
rp__ksh[32]='l2vpn_show -X 0x42'
rp_exec[33]='show l2vpn flexible-xconnect-service private no-statistics'
rp__ksh[33]='l2vpn_show -p -2 0x42'
rp_exec[34]='show l2vpn discovery private'
rp__ksh[34]='l2vpn_show -p 0x13'
rp_exec[35]='show l2vpn proc fsm private loc $location'
rp__ksh[35]='l2vpn_show -p -U $fq_nodeid 0x40'
rp_exec[36]='show l2vpn proc sync counts loc $location'
rp__ksh[36]='l2vpn_show -U $fq_nodeid 0x3e'
rp_exec[37]='show l2vpn proc sync stats loc $location'
rp__ksh[37]='l2vpn_show -U $fq_nodeid 0x3d'
rp_exec[38]='show proc l2vpn_mgr waipc service detail'
rp__ksh[38]='waipc_show -e l2vpn_mgr -o 0x1 -d'
rp_exec[39]='show l2vpn api-statistics all detail'
rp__ksh[39]='l2vpn_api_stats_cmd all -d'
rp_exec[40]='show l2vpn database vrf location $location'
rp__ksh[40]='l2vpn_show -U $fq_nodeid 0x45'
rp_exec[41]=''
rp__ksh[41]=''


rplc_exec[1]='show l2vpn trace2'
rplc__ksh[1]='l2vpn_show_ltrace'
rplc_exec[2]='show im trace errors location $location'
rplc__ksh[2]='im_show_trace -E '
rplc_exec[3]='show sysdb trace verification location $location'
rplc__ksh[3]='sysdb_show_ltrace -T vrfn -i $fq_nodeid'
rplc_exec[4]='show system statistics component l2vpn-gsp location $location'
rplc__ksh[4]='ship_show -c l2vpn-gsp'
rplc_exec[5]=''
rplc__ksh[5]=''


rack_exec[1]='show proc vlan_ma txgroup txlist location $location'
rack__ksh[1]='txlib_show -N $fq_nodeid -E vlan_ma -L'
rack_exec[2]='show proc vlan_ma txgroup peer all detail location $location'
rack__ksh[2]='txlib_show -N $fq_nodeid -E vlan_ma -P -d -a'
rack_exec[3]='show proc ether_caps_partner txgroup txlist $location'
rack__ksh[3]='txlib_show -N $fq_nodeid -E ether_caps_partner -L'
rack_exec[4]='show proc ether_caps_partner txgroup peer all detail location $location'
rack__ksh[4]='txlib_show -N $fq_nodeid -E ether_caps_partner -P -d -a'
rack_exec[5]=''
rack__ksh[5]=''



display() {
    case "$__cardtype" in
    "SYS")
        exec_commands 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

