#!/pkg/bin/ksh
# ---------------------------------------------------------------------
# show_tech_ip_rib - IP RIB show tech-support script
#
# Copyright (c) 2004-2017 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


# Initialise any variables used. 
afi_in="afi_all"
afi="ipv4"
os_name=""
__cardtype="Unspecified"
VERBOSE_LEVEL_MIN="0x1"
VERBOSE_LEVEL_MAX="0x2"
detail_level=$VERBOSE_LEVEL_MIN
detail_level_str=""
standby=0
# Parse the arguments to the script.
# Usage: 
# 
# -A [ipv4|ipv6]         : AFI to run commands for
# -L [0x1]               : Detail level
# -f <filename>          : save the output to filename
# 

# Parse the arguments to the script.
while [ "$#" -gt "0" ]; do
    case "$1" in
        -A) afi_in="$2"; shift 2;;
        -L) detail_level="$2"; shift 2;;
        -t) __cardtype="$2"; shift 2;;
        *)  default_parser_function "$@"; shift $#;;
    esac
done

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

# Set detail_level_str
case "$detail_level" in
    $VERBOSE_LEVEL_MIN) detail_level_str="Brief \(default\)";;
    $VERBOSE_LEVEL_MAX) detail_level_str="Detail with all VRF routes";;
    *) echo "Invalid Debug level entered: $detail_level"; exit;;
esac

if [ $detail_level == $VERBOSE_LEVEL_MAX ];then
    # Fetch the list of vrfs through show run vrf
    vrf_list=`nvgen -c -q gl/rsi/ | grep -E vrf | cut -c 5-`
fi

#Platform specific commands
# find a media to store showtech output
uname -a | grep hfr > /dev/null 2>&1;  RC=$?
uname -a | grep prp > /dev/null 2>&1;  RC1=$?
uname -a | grep enxr > /dev/null 2>&1; RC2=$?
uname -a | grep asr9k > /dev/null 2>&1; RC3=$?  
uname -a | grep xrvr > /dev/null 2>&1; RC4=$?

if [[ "$RC" -eq "0" ]]; then
    platform="hfr"
elif [[ "$RC1" -eq "0" ]]; then
    platform="prp"
elif [[ "$RC2" -eq "0" ]]; then
    platform="enxr"
elif [[ "$RC3" -eq "0" ]]; then 
    platform="viking"
elif [[ "$RC4" -eq "0" ]]; then 
    platform="xrvr"
else
    platform="panini"
fi 

# Commands that run on once per system
i=1
j=1
common_exec[$((i++))]='show clock'
common__ksh[$((j++))]='iosclock -d 0x0'
common_exec[$((i++))]='show platform'
if [[ "$platform" = "viking" ]]; then 
     common__ksh[$((j++))]='show_platform_vkg -e'
elif [[ "$platform" = "panini" ]]; then
     common__ksh[$((j++))]='show_platform_sysdb -v'
elif [[ "$platform" = "hfr" ]]; then
     common__ksh[$((j++))]='shelfmgr_show_hfr -e'
else 
     common__ksh[$((j++))]='show_platform -a'
fi
common_exec[$((i++))]='show version'
if [[ "$platform" = "panini" ]]; then 
     common__ksh[$((j++))]='ng_show_version'
else
     common__ksh[$((j++))]='show_version'
fi
if [[ "$platform" = "panini" ]]; then 
     common_exec[$((i++))]='show install active'
     common__ksh[$((j++))]='sdr_instcmd show install active'
else
     common_exec[$((i++))]='show install active summary'
     common__ksh[$((j++))]='instcmd show install active summary'
fi
common_exec[$((i++))]='show redundancy'
common__ksh[$((j++))]='redcon_show'

common_exec[$((i++))]='show proc blocked'
if [[ "$platform" = "panini" ]]; then
    common__ksh[$((j++))]='sh_proc_ng_blocked'
else
    common__ksh[$((j++))]='show_processes -b'
fi 

if [[ "$platform" -ne "panini" && "$platform" -ne "fretta" ]]; then
common_exec[$((i++))]='show install which component ip-rib'
common__ksh[$((j++))]='instcmd show install which component ip-rib detail'
fi

common_exec[$((i++))]='show running-config router rib'
common__ksh[$((j++))]='nvgen -c -q gl/rib/'

common_exec[$((i++))]='show run router static'
common__ksh[$((j++))]='nvgen -c -q gl/static/router/'

common_exec[$((i++))]=''
common__ksh[$((j++))]=''   


nodename=$showtech_node_name
nodelocation=`node_conversion -E $nodename`
standby=$(redcon_show | grep -c -E "is in STANDBY role")

#     echo "card" $__cardtype
#     echo "node" $nodelocation

# SysDB NULL path specifier
sysdb_null_path="________"
vrf_name="$sysdb_null_path"
table_filter="$sysdb_null_path"

# RIB AFI dependent commands
i=1
j=1

commonAFI_exec[$((i++))]='show processes $proc_str'
commonAFI__ksh[$((j++))]='sysmgr_show -o -p $proc_str'

commonAFI_exec[$((i++))]='show rib $afi trace location mgmt-nodes'
commonAFI__ksh[$((j++))]='$afi_ltrace_func -i mgmt-nodes'

commonAFI_exec[$((i++))]='show route $afi'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -s $afi'

commonAFI_exec[$((i++))]='show route $afi multicast'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -s $afi'

commonAFI_exec[$((i++))]='show route $afi summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -S $afi'

commonAFI_exec[$((i++))]='show route vrf all $afi summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z all -V $table_filter -S $afi'

commonAFI_exec[$((i++))]='show route $afi multicast summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -S $afi'

commonAFI_exec[$((i++))]='show route vrf all $afi multicast summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z all -V $table_filter -S $afi'

commonAFI_exec[$((i++))]='show route $afi summary detail'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -D $afi'

commonAFI_exec[$((i++))]='show route vrf all $afi summary detail'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z all -V $table_filter -D $afi'

commonAFI_exec[$((i++))]='show route $afi multicast summary detail'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -D $afi'

commonAFI_exec[$((i++))]='show route vrf all $afi multicast summary detail'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z all -V $table_filter -D $afi'

commonAFI_exec[$((i++))]='show rib $afi next-hop'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -h 0x0'

commonAFI_exec[$((i++))]='show rib $afi next-hop summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -h -8 0x0'

commonAFI_exec[$((i++))]='show rib $afi multicast next-hop'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -h 0x0'

commonAFI_exec[$((i++))]='show rib $afi next-hop damp'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -h -j 0x0'

commonAFI_exec[$((i++))]='show rib $afi attributes summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -b 0x0'

commonAFI_exec[$((i++))]='show rib $afi extcomms summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -2 0x0'

commonAFI_exec[$((i++))]='show rib $afi nhid'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -{ 0x0'

commonAFI_exec[$((i++))]='show rib $afi nhid feid'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -{ -n 0x0'

commonAFI_exec[$((i++))]='show route $afi backup'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -e $afi'

commonAFI_exec[$((i++))]='show route $afi multicast backup'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -e $afi'

commonAFI_exec[$((i++))]='show route $afi deleted'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -d $afi'

commonAFI_exec[$((i++))]='show route $afi multicast deleted'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_name -V $table_filter -d $afi'

commonAFI_exec[$((i++))]='show route $afi quarantined'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -Q $afi'

commonAFI_exec[$((i++))]='show rib $afi clients'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -r 0x0'

commonAFI_exec[$((i++))]='show rib $afi clients protocols'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -u 0x0'

commonAFI_exec[$((i++))]='show rib $afi clients redistribution'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -v 0x0'

commonAFI_exec[$((i++))]='show rib $afi clients redistribution history all'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -G 0x0'

commonAFI_exec[$((i++))]='show rib $afi tables summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -0 0x0'

commonAFI_exec[$((i++))]='show rib $afi tables'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -4 0x0'

commonAFI_exec[$((i++))]='show rib $afi opaque summary'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_name -V $table_filter -E 0x0'

commonAFI_exec[$((i++))]='show rib $afi update-groups'
commonAFI__ksh[$((j++))]='$afi_func -X $afi_code -k 0x0'

commonAFI_exec[$((i++))]='show $afi int brief'
commonAFI__ksh[$((j++))]='$afi_func_interface -b -v default'

commonAFI_exec[$((i++))]='show bcdlv2 $bcdls_group_name'
commonAFI__ksh[$((j++))]='show_bcdlv2 -C 0x1 -B $bcdls_group_name'

commonAFI_exec[$((i++))]='show bcdlv2 $bcdls_group_name consumers'
commonAFI__ksh[$((j++))]='show_bcdlv2 -C 0x6 -B $bcdls_group_name'

commonAFI_exec[$((i++))]='show bcdlv2 $bcdls_group_name producer-view statistics'
commonAFI__ksh[$((j++))]='show_bcdlv2 -C 0x12 -B bcdls_$afi_rib'

commonAFI_exec[$((i++))]='show bcdlv2 $bcdls_group_name producer-view basic'
commonAFI__ksh[$((j++))]='show_bcdlv2 -C 0xa -B $bcdls_group_name'

commonAFI_exec[$((i++))]='show bcdlv2 $bcdls_group_name update-groups'
commonAFI__ksh[$((j++))]='show_bcdlv2 -C 0x5 -B $bcdls_group_name'


# RIB commands for standby
i=1
j=1

commonSBY_exec[$((i++))]='show rib $afi clients $standby_in_exec'
commonSBY__ksh[$((j++))]='$afi_func -X $afi_code $standby_in_ksh -r 0x0'


# RIB commands at detail level 0x1
i=1
j=1

level1_exec[$((i++))]='show rib $vrf $afi label-rpf summary'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -} 0x0'

level1_exec[$((i++))]='show rib $vrf $afi next-hop summary'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -o -8 0x0'

level1_exec[$((i++))]='show rib $vrf $afi opaques summary'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -E 0x0'

level1_exec[$((i++))]='show rib $vrf $afi protocols'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -z 0x0'

level1_exec[$((i++))]='show rib $vrf $afi statistics'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -a'

level1_exec[$((i++))]='show rib $vrf $afi next-hop'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -o 0x0'

level1_exec[$((i++))]='show rib $vrf $afi next-hop damped'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -o -j 0x0'

level1_exec[$((i++))]='show route $vrf $afi'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -s $afi'

level1_exec[$((i++))]='show route $vrf $afi deleted'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -d $afi'

level1_exec[$((i++))]='show route $vrf $afi quarantined'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x1 -Z $vrf_i_ksh -V $table_filter -Q $afi'

level1_exec[$((i++))]='show route $vrf $afi multicast'
level1__ksh[$((j++))]='$afi_func -X $afi_code -Y 0x2 -Z $vrf_i_ksh -V $table_filter -s $afi'

level1_exec[$((i++))]=''
level1__ksh[$((j++))]=''   

# Logs will be printed at the end of showtech
i=1
j=1
commonLOG_exec[$((i++))]='show logging'                     
commonLOG__ksh[$((j++))]='show_logging'

commonLOG_exec[$((i++))]=''
commonLOG__ksh[$((j++))]=''   



#Get only the line containing _rib and extract the string following b, 
#which cyontains the node names (active and standby)
os_name="`uname -a | cut -f1 -d' '`"
export os_name;


####### Finding the active and standby Node ID ##########
# This is not being used now. But in Future Cases, if we need to
# seperate active and standby, the following calculations will help.

show_placement_program_command="show placement program "$afi"_rib"
singleCommand_exec[1]='$show_placement_program_command'
singleCommand__ksh[1]='placed_show -p "$afi"_rib'
singleCommand__ksh[2]=''

nodeid_part1=`exec_commands singleCommand | grep -v "show placement program" | grep _rib | cut -f2 -d'b'`
export nodeid_part1;

#Now strip out all the leading spaces in this string
if [ "$os_name" = "QNX" ] ;then
    nodeid_part2=`perl -e '$str=$ENV{"nodeid_part1"};$str =~ s/^\s+//g;print $str;'`
    export nodeid_part2;
else
    export nodeid_part2=`echo $nodeid_part1 | sed  's/^[[:space:]]*//g'`
fi

# Incase show_placement CLI changes, token count may go out of sync.
# Put a check to catch it.
token_count=`echo $nodeid_part2 | wc -w`
if [ $token_count -ne 6 ] ;then
   echo "token count $token_count mis-match";
fi

# First part of the string has active node name
nodeid_active=`echo $nodeid_part2 | cut -d' ' -f3`
#echo "nodeid_active = $nodeid_active";

#Check if standby is present, if not then look for keyword NONE.
nodeid_sby=`echo $nodeid_part2 | cut -d' ' -f5`
#echo "nodeid_sby = $nodeid_sby";
if [ "$nodeid_sby" = "NONE" ] ;then
    echo "No standby RP found.";
fi

####### Finding the active and standby Node ID - END ##########

do_show_afi_command() {
    afi=$1
    afi_code=$2
    proc_str=$afi"_rib"
    afi_func="show_"$afi"_rib"
    bcdls_group_name="bcdls_"$afi"_rib"
    if [[ "$afi" == "ipv4" ]]; then 
        afi_func_interface="show_ip_interface"
    else
        afi_func_interface="show_ipv6_interface"
    fi
    afi_ltrace_func="show_"$afi"_rib_ltrace"
    rib_running=$(sysmgr_show -o -p $afi"_rib" | grep -c -E "Process state: Run") 
    print_main_heading "show tech-Support RIB $afi"

    exec_commands commonAFI

    if [[ $rib_running == 1 ]]; then
        if [[ $standby == 1 ]]; then
            standby_in_exec='standby'
            standby_in_ksh='-t'
            exec_commands commonSBY
        fi
    fi

    if [[ $detail_level == $VERBOSE_LEVEL_MAX ]]; then
        for vrf_i in $vrf_list;do
            vrf_i_ksh=$vrf_i
            vrf="vrf $vrf_i"
            exec_commands level1
        done
    fi

    print_main_heading "show tech-Support RIB $afi completed"
}


display() {

     # Print the output heading
     print_main_heading "show tech-Support IP RIB  \(Verbosity: $detail_level_str\)"

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

         exec_commands common  

         if [[ "$afi_in" == "ipv4" || "$afi_in" == "afi_all" ]]; then
             do_show_afi_command "ipv4" "0x1"              
         fi
    
         if [[ "$afi_in" == "ipv6" || "$afi_in" == "afi_all" ]]; then
             do_show_afi_command "ipv6" "0x2"
         fi
         
         exec_commands commonLOG

     fi
     
     # Print the closing heading. 
     print_main_heading "show tech-Support IP RIB completed"
}


# This function calls the display() function and sends the output to file if
# the file option has been set. 

if [[ "$afi" = "ipv4" || "$afi" = "ipv6" ]]; then
    . /pkg/bin/show_tech_file_fragment
else
    echo "usage: show_tech_ip_rib -A [ipv4|ipv6] "
fi


