#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ipv6_nd_fast - Show tech-support script for ipv6 nd
#
# Copyright (c) 2014-2019 by cisco Systems, Inc.
# All rights reserved.
#--------------------------------------------------------------------


#
# Load the script provided by show-tech infra, which provides worker functions
#
. /pkg/bin/show_tech_main_fragment

# Initialise any variables used.
__cardtype="unspecified"
internal_if="unspecified"  # this is ifname based support
external_if=""
trace_only="0" # future support
show_only="0"  # future support
location=""

# Parse the arguments to the script.
# -I  : Run commands for a specific Interface

while [ $# -gt 0 ]; do
  case "$1" in
     -I) internal_if="$2"; shift 2;;
     -T) trace_only="1"; shift 1;;
     -S) show_only="1"; shift 1;;

     -t) __cardtype="$2"; shift 2;;
     *)  default_parser_function "$@"; shift $#;;
  esac
done

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

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

# Convert the external interface name to internal
if [ "$internal_if" != "unspecified" ]; then
    external_if=`convert_interface_fmt '-e' $internal_if`
fi

#node_name_internal=`uname -a | cut -d " " -f2 | cut -d "-" -f2 | cut -d "" -f2,3,4`
# QNX does not provide node_list_generation but 'uname -n' provides the node name
# Linux supports node_list_generation and node_conversion to get nodename
# QNX is used by classic xr and most of other platforms is using Linux
plat=`uname`
node_list_generation=`which node_list_generation > /dev/null 2>&1`
if [ $? == 1 ]; then
    location=`uname -n`
    node_name_internal=`uname -n`
else
    location=`node_list_generation -f MY`
    node_name_internal=`node_conversion -N $location`
fi
node_number=`node_conversion -i $node_name_internal`

ipv6_nd_jid=`sysmgr_show -p ipv6_nd | grep Job |  cut -f 2 -d ":" | cut -f 2 -d " "`
ipv6_nd_pid=`sysmgr_show -p ipv6_nd | grep PID |  cut -f 2 -d ":" | cut -f 2 -d " "`

###############################################################################
# Show commands that run once per LR                                          #
###############################################################################
sys_exec[1]='show version'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[1]='ng_show_version'
else
    sys__ksh[1]='show_version'
fi

sys_exec[2]='show install active'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[2]='sdr_instcmd show install active'
else
    sys__ksh[2]='instcmd show install active'
fi

sys_exec[3]='show logging'
sys__ksh[3]='show_logging'

sys_exec[4]='show running-config'
sys__ksh[4]='nvgen -c -l 1 -t 1 -o 1'

sys_exec[5]='show redundancy location all'
sys__ksh[5]='redcon_show -n all'

sys_exec[6]='show platform'
if [[ "$platform" = "panini" ]]; then
    sys__ksh[6]='show_platform_sysdb'
else
    sys__ksh[6]='show_platform'
fi

sys_exec[7]='Get group rsi map in LR plane'
sys__ksh[7]='gsp_show -m'

sys_exec[8]='Show local rsi table'
sys__ksh[8]='gsp_show -z'

sys_exec[9]='show bundle'
sys__ksh[9]="bundlemgr_show -b all"

sys_exec[10]='show route ipv6'
sys__ksh[10]="show_ipv6_rib -X 0x2 -Y 0x1 -Z ________ -V ________ -s ipv6 ________"

sys_exec[11]='show ipv6 neighbors'
sys__ksh[11]="show_ipv6_neighbors -a"

sys_exec[12]='show ipv6 traffic'
sys__ksh[12]="show_ipv6_traffic"

sys_exec[13]='show ipv6 nd slaac location all'
sys__ksh[13]="show_ipv6_slaac_intfs -L all"

sys_exec[14]='show rib clients'
sys__ksh[14]="show_ipv4_rib -X 0x1 -r 0x0"

sys_exec[15]='show ipv6 neighbors detail'
sys__ksh[15]="show_ipv6_neighbors -f"

sys_exec[16]='show ipv6 nd subscriber statistics summary'
sys__ksh[16]="show_ipv6_nd_subscriber_statistics -S"

sys_exec[17]='show ipv6 nd subscriber statistics details'
sys__ksh[17]="show_ipv6_nd_subscriber_statistics -D"

sys_exec[18]='show ipv6 vrf all interface internal'
sys__ksh[18]="show_ipv6_interface -t -v all"

sys_exec[19]='show im database brief view local-data-plane location all'
sys__ksh[19]="im_show database -v 0x3 -l 0x1 -a"

sys_exec[20]='show im database brief view owner location all'
sys__ksh[20]="im_show database -v 0x0 -l 0x1 -a"

# NOTE:
# this should be the last one as the show tech script will stop after empty array element.
if [[ "$platform" = "panini" ]]; then
    sys_exec[21]='show issu'
    sys__ksh[21]="issu_show_status -I"

    sys_exec[22]='show issu milestone clients location all'
    sys__ksh[22]="issu_show_status -c"
else
    sys_exec[21]=''
    sys__ksh[21]=""

    sys_exec[22]=''
    sys__ksh[22]=""
fi

sys_exec[23]=''
sys__ksh[23]=""

###############################################################################
# Show commands that run on every RP                                          #
###############################################################################

# System information 

rp_exec[1]='show cli history detail'
rp__ksh[1]="show_parser_history -h 0x2"

rp_exec[2]='show replicator trace client jid $ipv6_nd_jid'
rp__ksh[2]="replicator_show_ltrace -a -j $ipv6_nd_jid"

#
# ND - MA owner channel / CLI's
#
rp_exec[3]='show  ipv6  ma owner-channel client location $location'
rp__ksh[3]="show_ipv6_oc_info -c -n $node_number"

#
# ND - IM owner chain / CLI's
#
rp_exec[4]='show  im ownerchaninfo location $location'
rp__ksh[4]="im_show ownerchaninfo -h $node_number"

rp_exec[5]=''
rp__ksh[5]=""


###############################################################################
# Show commands that run on every RP for a given interface                    #
###############################################################################

interface_rp_exec[1]='show ipv6 interface $external_if internal'
interface_rp__ksh[1]="show_ipv6_interface -I $internal_if -t -v default"

interface_rp_exec[2]='show ipv6 interface $external_if event'
interface_rp__ksh[2]="show_ipv6_interface -I $internal_if -u -v default"

interface_rp_exec[3]=''
interface_rp__ksh[3]=""

###############################################################################
# Show commands that run on all RP or LC                                      #
###############################################################################
rplc_exec[1]='show processes blocked'
if [[ "$platform" = "panini" ]]; then
   rplc__ksh[1]="sh_proc_ng_blocked"
else
   rplc__ksh[1]="show_processes -b"
fi

rplc_exec[2]='show ipv6 nd trace frequent-event location $location'
rplc__ksh[2]="show_ltrace_nd -F -i $node_number"
rplc_exec[3]='show ipv6 nd trace packet location $location'
rplc__ksh[3]="show_ltrace_nd -P -i $node_number"
rplc_exec[4]='show ipv6 nd trace error location $location'
rplc__ksh[4]="show_ltrace_nd -R -i $node_number"
rplc_exec[5]='show ipv6 nd trace timer location $location'
rplc__ksh[5]="show_ltrace_nd -T -i $node_number"
rplc_exec[6]='show ipv6 nd trace event location $location'
rplc__ksh[6]="show_ltrace_nd -E -i $node_number"
rplc_exec[7]='show ipv6 nd trace aib location $location'
rplc__ksh[7]="show_ltrace_nd -A -i $node_number"
rplc_exec[8]='show ipv6 nd trace chkpt location $location'
rplc__ksh[8]="show_ltrace_nd -H -i $node_number"
rplc_exec[9]='show ipv6 nd trace init location $location'
rplc__ksh[9]="show_ltrace_nd -C -i $node_number"
rplc_exec[10]='show ipv6 nd trace mac-acc location $location'
rplc__ksh[10]="show_ltrace_nd -G -i $node_number"
rplc_exec[11]='show ipv6 nd trace mdr location $location'
rplc__ksh[11]="show_ltrace_nd -M -i $node_number"
rplc_exec[12]='show ipv6 nd trace gsp location $location'
rplc__ksh[12]="show_ltrace_nd -I -i $node_number"

rplc_exec[13]='show ipv6 neighbors location $location'
rplc__ksh[13]="show_ipv6_neighbors -N $node_number -s 0x0"

rplc_exec[14]='show ipv6 nd idb all detail location $location'
rplc__ksh[14]="show_ipv6_idbinfo -a -N $node_number -d"
rplc_exec[15]='show ipv6 nd vrinfo location $location' # does this need to be only RP command ?
rplc__ksh[15]="show_ipv6_vrinfo -N $node_number"
rplc_exec[16]='show ipv6 neighbors summary'
rplc__ksh[16]="show_ipv6_neighbors -s 0x2"
rplc_exec[17]='show ipv6 nd statistics location $location'
rplc__ksh[17]="show_ipv6_nd_statistics -N $node_number"

rplc_exec[18]='show ipv6 nd bundle interfaces location $location'
rplc__ksh[18]="show_ipv6_nd_intfs -N $node_number"

#
# NetIO
#
rplc_exec[19]='show netio client location $location'
rplc__ksh[19]="netio_show -L -h $node_number"
#

# IM information
#
rplc_exec[20]='show im clients job $ipv6_nd_jid detail location $location'
rplc__ksh[20]="im_show clients -l 0x3 -h $node_number -j $ipv6_nd_jid"
rplc_exec[21]='show im statistics library job $ipv6_nd_jid'
rplc__ksh[21]="ship_show -c ifmgr -j $ipv6_nd_jid"
rplc_exec[22]='show im statistics client process-name ipv6_nd detail location $location'
rplc__ksh[22]="im_show stats-client -n ipv6_nd -l 0x3 -h $node_number"
rplc_exec[23]='show im registrations all job $ipv6_nd_jid detail location $location'
rplc__ksh[23]="im_show registrations -t 0x8 -l 0x3 -h $node_number -j $ipv6_nd_jid"

#
# LPTs/PIFIB: This is taking time & space...so will enable in future if required
#
# rplc_exec[22]='show lpts pifib entry brief location $location D'
# rplc__ksh[22]="show_lpts_pifib -z 0x0 -k 0x9 -a 0x1 -i $node_number"
# rplc_exec[23]='show lpts pifib hardware entry brief location $location'
# rplc__ksh[23]="platform_show_pifib -z 0x2 -i $node_number"
# rplc_exec[24]='show lpts pifib hardware entry statistics location $location'
# rplc__ksh[24]="platform_show_pifib -z 0x3 -i $node_number"



#
# Chkpt/Rpl/RDS client traces
# 
rplc_exec[24]='show checkpoint ltrace jid  $ipv6_nd_jid all'
rplc__ksh[24]="show_ltrace_chkpt -j $ipv6_nd_jid -T all"
rplc_exec[25]='show rds trace process ipv6_nd all'
rplc__ksh[25]="show_ltrace_rds -P ipv6_nd -T all"
rplc_exec[26]='chkpt_cli -p ipv6_nd -n 1 -v 2'
rplc__ksh[26]="chkpt_cli -p ipv6_nd -n 1 -v 2"
rplc_exec[27]='chkpt_cli -p ipv6_nd -n 2 -v 1'
rplc__ksh[27]="chkpt_cli -p ipv6_nd -n 2 -v 1"

#
# ipv6_nd process state & threads' callstack
#
rplc_exec[28]='show process ipv6_nd'
rplc__ksh[28]="sysmgr_show -o -p ipv6_nd"

rplc_exec[29]='follow process 237734 verbose all'
rplc__ksh[29]="attach_process -p $ipv6_nd_pid -A 3 -v"

rplc_exec[30]='show context location $location'

# Check if dumper_context exist and select appropriate executable equivalent
# to 'show context' CLI.
if [ -f "/pkg/bin/dumper_context" ]; then
    rplc__ksh[30]="dumper_context -c 0x1 -n all"
else
    rplc__ksh[30]="corehelper_context -c 0x1 -n all"
fi

#
# ND SERG Traces / CLIs
#
rplc_exec[31]='show ipv6 nd trace serg location $location'
rplc__ksh[31]="show_ltrace_nd -W -i $node_number"

#
# ND EVPN Traces / CLIs
#
rplc_exec[32]='show ipv6 nd trace evpn location $location'
rplc__ksh[32]="show_ltrace_nd -S -i $node_number"

rplc_exec[33]='show ipv6 nd packets location $location'
rplc__ksh[33]="show_ipv6_nd_packets -L $node_number"

rplc_exec[34]=''
rplc__ksh[34]=""

###############################################################################
# Interface specific commands                                                 #
###############################################################################


interface_rplc_exec[1]='show interfaces $external_if accounting'
interface_rplc__ksh[1]="show_interface -i $internal_if -o 0x4"

interface_rplc_exec[2]='show adjacency ipv6 $external_if internal detail location $location'
interface_rplc__ksh[2]="aib_show_command -E -P ipv6 -D -I $internal_if -N $node_number"

interface_rplc_exec[3]='show cef ipv6 $external_if detail internal location $location'
interface_rplc__ksh[3]="fib_show_command -t -O 0x1 -i $internal_if -d -N $node_number --internal"

interface_rplc_exec[4]='show cef ipv6 $external_if hardware egress detail location $location'
interface_rplc__ksh[4]="fib_show_command -t -O 0x1 -i $internal_if -H egress -d -N $node_number"

interface_rplc_exec[5]='show cef ipv6 $external_if hardware egress detail internal location $location'
interface_rplc__ksh[5]="fib_show_command -t -O 0x1 -i $internal_if -H egress -d -N $node_number --internal"

interface_rplc_exec[6]='show cef ipv6 $external_if hardware ingress detail location $location'
interface_rplc__ksh[6]="fib_show_command -t -O 0x1 -i $internal_if -H ingress -d -N $node_number"

interface_rplc_exec[7]='show cef ipv6 $external_if hardware ingress detail internal location $location'
interface_rplc__ksh[7]="fib_show_command -t -O 0x1 -i $internal_if -H ingress -d -N $node_number --internal"

interface_rplc_exec[8]='show netio idb $external_if location $location'
interface_rplc__ksh[8]="netio_show -I -N $internal_if -h $node_number"

interface_rplc_exec[9]='show adjacency mpls $external_if internal detail location $location'
interface_rplc__ksh[9]="aib_show_command -E -P mpls -D -I $internal_if -N $node_number"

interface_rplc_exec[10]=''
interface_rplc__ksh[10]=""

#######################################################
#  all interfaces' commands
#######################################################

all_interfaces_rplc_exec[0]='show ipv6 vrf all interface events'
all_interfaces_rplc__ksh[0]="show_ipv6_interface -u -v all"

all_interfaces_rplc_exec[1]='show adjacency ipv6 internal detail location $location'
all_interfaces_rplc__ksh[1]="aib_show_command -E -P ipv6 -D -N $node_number"

all_interfaces_rplc_exec[2]='show adjacency ipv6 detail hardware location $location'
all_interfaces_rplc__ksh[2]="aib_show_command -P ipv6 -D -W -N $node_number"

all_interfaces_rplc_exec[3]='show interfaces'
all_interfaces_rplc__ksh[3]="show_interface -a"

all_interfaces_rplc_exec[4]='show cef ipv6'
all_interfaces_rplc__ksh[4]="fib_show_command -t -O 1"

all_interfaces_rplc_exec[5]='show adjacency mpls internal detail location $location'
all_interfaces_rplc__ksh[5]="aib_show_command -E -P mpls -D -N $node_number"

all_interfaces_rplc_exec[6]='show adjacency mpls detail hardware location $location'
all_interfaces_rplc__ksh[6]="aib_show_command -P mpls -D -W -N $node_number"

all_interfaces_rplc_exec[7]=''
all_interfaces_rplc__ksh[7]=""

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

display() {
     
   # Print the output heading
   print_main_heading "show tech-support ipv6 nd"
    if [ "$__cardtype" == "SYS" ]; then
        exec_commands sys
    else
        case "$__cardtype" in
        "DRP"|"RP")
            exec_commands rplc
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands interface_rp
                exec_commands interface_rplc
            else
                exec_commands all_interfaces_rplc
            fi
            exec_commands rp
        ;;
        "LC")
            exec_commands rplc
            if [ "$internal_if" != "unspecified" ]; then
                exec_commands interface_rplc
            else
                exec_commands all_interfaces_rplc
            fi
        ;;
        esac
    fi

    # Print the closing heading.
    print_main_heading "show tech-support ipv6 nd 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

