#!/bin/ksh
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2021.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/swap_adapter_complete.sh 1.17.1.23 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2012 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/events/swap_adapter_complete.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM
#########################################################################
#
#   COMPONENT_NAME: EVENTS
#
#   FUNCTIONS: none
#
#########################################################################

#########################################################################
#                                                                       #
#       Name:           swap_adapter_complete                           #
#                                                                       #
#	Description:	This script is called after a swap_adapter	#
#			script successfully completes.                  #
#			It pings all addresses (which have the		#
#			same network as the new address) to refresh	#
#			arp tables.					#
#									#
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      nodename network ip_address1 ip_address2        #
#                                                                       #
#									#
#			If this script was called on a node where	#
#			swap_adapter didn't actually occur, the		#
#			forth argument (ip_address2) will not be        #
#			passed by clstrmgr. 				#
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#			2	bad argument				#
#                                                                       #
#########################################################################

###############################################################################
# Name: flush_arp
#
#       Flushes entire arp cache
#
# Returns: None.
###############################################################################
flush_arp () {
    typeset PS4_FUNC="flush_arp"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    arp -an | grep "\?" | tr -d '()' | (while read host addr other ; do
	arp -d $addr
    done)
    return 0
}

###############################################################################
# Name: flush_ATM_arp
#
#       Flushes entire ATM arp cache
#
# Returns: None.
###############################################################################
flush_ATM_arp () {
    typeset PS4_FUNC="flush_ATM_arp"
    [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
    for IP_ADDR in $1 $2
    do
        IF=$(LC_ALL=C clgetif -a $IP_ADDR | sed -e "s/ //g")
        for ARP_ENTRY in $(arp -t atm -a | sed -n -e "s/(/ /p" | cut -d')' -f1 |\
                  awk '{                                                    \
                      if (match($0,"'$IF'")) {                              \
                          while ( getline line && index(line, " ") == 1 ) { \
                              split(line, field, " ");                      \
                              print field[2]                                \
                          }                                                 \
                      }                                                     \
                  }')
        do
            arp -t atm -d $ARP_ENTRY
        done
    done
    return 0
}

###############################################################################
#
# Main Starts Here
#
###############################################################################

typeset PROGNAME=${0##*/}
export PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
if [[ $VERBOSE_LOGGING == "high" ]]; then
    set -x
    version='%I%'
fi

set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

OP_SEP="$(cl_get_path -S)"

#
# turn off DNS/NIS
export NSORDER=local


if [ $# -lt 3 ]
then
    cl_echo 1041 "Usage: $PROGNAME nodename network ip_address1 ip_address2\n"  $PROGNAME
    exit 2
fi

# Lets not flush ATM unless this was on an ATM network.
NETWORK_TYPE=$(cllsif -J "$OP_SEP" -Sn $3 | cut -d"$OP_SEP" -f4 \
                | uniq)
if [ "$NETWORK_TYPE" = "atm" ]
then
        	flush_ATM_arp $3 $4
fi

# Since nameserving must be turned back on, even if no other work
# must be done, the test is altered to check for the local node name
# and if this is true, perform the steps below.  If it is not the
# local node name, turn on nameserving and exit.

# if [ $1 != $LOCALNODENAME ]
# then
#     exit 0
# fi

if [ $1 = $LOCALNODENAME ]
  then

#  set -u

#
# Get the network of the new address, then get all addresses on
# that network, finally, ping them to update the arp table.
# 
  NETWORK=$(cllsif -J "$OP_SEP" -Sn $3 | cut -d"$OP_SEP" -f3 \
            | uniq)
  if [ -n "$NETWORK" ]
  then
      ADDRS=$(cllsif -SJ "$OP_SEP" | \
                grep "$OP_SEP$NETWORK$OP_SEP" | cut -d"$OP_SEP" -f7 | sort -u)
      for addr in $ADDRS
      do
          (ping $addr 1024 1 >/dev/null 2>&1) &
      done
  fi

fi

# refresh clcomd
refresh -s clcomd
 
exit 0
