#!/pkg/bin/ksh
#
# Created by Hyojeong Kim, Oct 2008.
#
# Copyright (c) 2008-2016 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


# Initialize any variables used. 
mode=""
instance="default"
__cardtype="unspecified"
showtech_argv=""
showtech_argc="0"



# Read in the arguments to the script, setting mode
# 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
     nsr) mode="$1"; shift;;
     distributed) mode="$1"; shift;;
     instance-*) temp=$1; instance=${temp#"instance-"}; shift;;
     -t) __cardtype="$2"; shift 2;;
     *)  showtech_argv="$showtech_argv $1"; (( showtech_argc+=1 )); shift 1;;
  esac
done

#
# Call the showtech infra default parser - using set to recreate the $#, $@,
# etc. - "--" is necessary so set doesn't try to interpret any of the
# arguments we are passing through!
#
if [[ $showtech_argc -ne 0 ]]; then
    set -- $showtech_argv
    default_parser_function $@
fi

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

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

## Command set 1 - configuration & other basic info
index=1

Basic_exec[$index]='show clock'
Basic__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

# # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # #
# NOTE:
# For Panini platform, replacing "show install which comp" command
# with "show version" and "show install active" commands.
# # # # # # # # # # # # # # # # # # # # # # ## # # # # # # # # # # #

if [ "$platform" == "panini" ]; then
Basic_exec[$index]='show version'
Basic__ksh[$index]='ng_show_version'
index=$((index + 1))
Basic_exec[$index]='show install active'
Basic__ksh[$index]='sdr_instcmd show install active'
else
Basic_exec[$index]='show install which component ipv4-bgp'
Basic__ksh[$index]='instcmd show install which component ipv4-bgp detail'
fi
index=$((index + 1))

Basic_exec[$index]='show redundancy'
Basic__ksh[$index]='redcon_show'
index=$((index + 1))

Basic_exec[$index]='show process bgp location all'
Basic__ksh[$index]='sysmgr_show -o -p bgp -n all'
index=$((index + 1))

Basic_exec[$index]='show running-config router bgp'
Basic__ksh[$index]='nvgen -c -q gl/ip-bgp/'
index=$((index + 1))

Basic_exec[$index]='show running-config bmp'
Basic__ksh[$index]='nvgen -c -q gl/bmpsrvr/'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv4 unicast'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x1 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv4 multicast'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x2 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv4 mdt'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x42 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv4 tunnel'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x40 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv6 unicast'
Basic__ksh[$index]='bgp_show -V default -A 0x2 -W 0x1 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table ipv6 multicast'
Basic__ksh[$index]='bgp_show -V default -A 0x2 -W 0x2 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table vpnv4 unicast'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x80 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance table vpnv6 unicast'
Basic__ksh[$index]='bgp_show -V default -A 0x2 -W 0x80 -5 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance all all process detail'
Basic__ksh[$index]='bgp_show -V default -A 0xFFFE -W 0xFE -z -j -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance all all process performance-statistics detail'
Basic__ksh[$index]='bgp_show -V default -A 0xFFFE -W 0xFE -z -p -j -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance sessions' 
Basic__ksh[$index]='bgp_show -V default -n -4 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance ipv4 unicast nexthop'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x1 -Q -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance ipv4 unicast nexthop statistics'
Basic__ksh[$index]='bgp_show -V default -A 0x1 -W 0x1 -Q -2 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance ipv6 unicast nexthop'
Basic__ksh[$index]='bgp_show -V default -A 0x2 -W 0x1 -Q -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance ipv6 unicast nexthop statistics'
Basic__ksh[$index]='bgp_show -V default -A 0x2 -W 0x1 -Q -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance scale'
Basic__ksh[$index]='bgp_show -V novrf -A 0xFFFE -W 0xFE -scale -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp trace instance $instance location all'
Basic__ksh[$index]='bgp_show_ltrace -i all -I $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance all all nsr detail'
Basic__ksh[$index]='bgp_show -V default -A 0xFFFE -W 0xFE -6 -3 -instance $instance'
index=$((index + 1))

Basic_exec[$index]='show bgp instance $instance all all nsr detail standby'
Basic__ksh[$index]='bgp_show -V default -A 0xFFFE -W 0xFE -6 -3 -1 -instance $instance'
index=$((index + 1))


#
# IPv4
#

Basic_exec[$index]='show route summary'
Basic__ksh[$index]='show_ipv4_rib -X 0x1 -Y 0x1 -Z ________ -V ________ -S 
		ipv4 ________'
index=$((index + 1))

Basic_exec[$index]='show rib tables'
Basic__ksh[$index]='show_ipv4_rib -X 0x1 -4 0x0'
index=$((index + 1))

Basic_exec[$index]='show rib tables summary'
Basic__ksh[$index]='show_ipv4_rib -X 0x1 -0 0x0'
index=$((index + 1))

Basic_exec[$index]='show rib statistics bgp'
Basic__ksh[$index]='show_ipv4_rib -X 0x1 -Y 0x1 -Z ________ -V ________ -c bgp -a'
index=$((index + 1))


#
# IPv6
#

Basic_exec[$index]='show route ipv6 summary'
Basic__ksh[$index]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -S ipv6 ________'
index=$((index + 1))

Basic_exec[$index]='show rib ipv6 tables'
Basic__ksh[$index]='show_ipv6_rib -X 0x2 -4 0x0'
index=$((index + 1))

Basic_exec[$index]='show rib ipv6 tables summary'
Basic__ksh[$index]='show_ipv6_rib -X 0x2 -0 0x0'
index=$((index + 1))

Basic_exec[$index]='show rib ipv6 statistics bgp'
Basic__ksh[$index]='show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -c bgp -a'
index=$((index + 1))


Basic_exec[$index]='show clock'
Basic__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

Basic_exec[$index]=''
Basic__ksh[$index]=''
index=$((index + 1))

index=1

NSR_exec[$index]='show clock'
NSR__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

# 'show bgp table <afi> <safi> standby' needs to be implemented.  
# commandNSR[$i++]='show bgp table ipv4 unicast standby'
# commandNSR[$i++]='show bgp table ipv4 multicast standby'
# commandNSR[$i++]='show bgp table ipv4 mdt standby'
# commandNSR[$i++]='show bgp table ipv4 tunnel standby'
# commandNSR[$i++]='show bgp table ipv6 unicast standby'
# commandNSR[$i++]='show bgp table ipv6 multicast standby'
# commandNSR[$i++]='show bgp table vpnv4 unicast standby' 
# commandNSR[$i++]='show bgp table vpnv6 unicast standby'

# 'show bgp sessions standby' needs to be implemented. 
# commandNSR[$i++]='show bgp sessions standby'

NSR_exec[$index]='show nsr ncd client brief'
NSR__ksh[$index]='ncd_show -c brief'
index=$((index + 1))

NSR_exec[$index]='show nsr ncd client all'
NSR__ksh[$index]='ncd_show -c all'
index=$((index + 1))

NSR_exec[$index]='show nsr ncd queue all'
NSR__ksh[$index]='ncd_show -q all'
index=$((index + 1))

NSR_exec[$index]='show nsr npl trace bgp error location all'
NSR__ksh[$index]='npl_show_ltrace -i all -E -V'
index=$((index + 1))

NSR_exec[$index]='show clock'
NSR__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

NSR_exec[$index]=''
NSR__ksh[$index]=''
index=$((index + 1))


index=1
Dist_exec[$index]='show clock'
Dist__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

Dist_exec[$index]='show clock'
Dist__ksh[$index]='iosclock -d 0x0'
index=$((index + 1))

Dist_exec[$index]=''
Dist__ksh[$index]=''
index=$((index + 1))



# A function called display() must be provided that calls the functions to 
# run the required show commands. The display() function is called in 
# /pkg/bin/show_tech_comp_file_frag

# No show commands run on individual RP or LC for BGP

display() {
    # Print the output heading 
    print_main_heading "show tech-support routing bgp $mode"
     
    # Execute commands
    if [ "$__cardtype" = "SYS" ]; then
        if [ "$mode" == "" ]; then
            exec_commands Basic
            exec_commands NSR
        fi

        if [ "$mode" == "nsr" ]; then
            exec_commands Basic
            exec_commands NSR
        fi

        if [ "$mode" == "distributed" ]; then
            exec_commands Dist
        fi
    fi

    # Print the closing heading. 
    print_main_heading "show tech-support routing bgp $mode complete"
}

# 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
