#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2018,2019,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/events/reconfig_topology_complete.sh 1.4.7.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1996,2015 
# 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/reconfig_topology_complete.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM

#########################################################################
#                                                                       #
#       Name:           reconfig_topology_complete                      #
#                                                                       #
#       Description:    This script is called when a reconfig           #
#                       topology dare is completed.                     #
#                                                                       #
#       Called by:      cluster manager                                 #
#                                                                       #
#       Calls to:                                                       #
#                                                                       #
#       Arguments:      none                                            #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################


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

PROGNAME=${0##*/}

PATH="$(/usr/es/sbin/cluster/utilities/cl_get_path all)"
OP_SEP="$(cl_get_path -S)"

#
# this is the working directory for DARE - the location must
# match the declaration in the other scripts involved
#
export TEMPPATH="/var/hacmp/log/HACMP_RESOURCES/"

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

if [[ $VERBOSE_LOGGING == "high" ]]
then
    set -x
    version='1.4.7.4'
fi

. /usr/es/sbin/cluster/etc/ha_odm_constants

export EVENT_TYPE=$PROGNAME        # Tell other scripts who called them

CLUTIL_LOG=$(clodmget -q "name = clutils.log" -n -f value HACMPlogs)
if [[ -z $CLUTIL_LOG ]]
then
    CLUTIL_LOG="/var/hacmp/log"
fi
CLUTIL_LOG="$CLUTIL_LOG/clutils.log"

#
# This will be the exit status seen by the Cluster Manager.
# If STATUS is not 0, the Cluster Manager will enter reconfiguration
# All lower-level scripts should pass status back to the caller.
# This will allow a Resource Groups to be processed individaully,
# independent of the status of another resource group.
#
STATUS=0

set -u

if (( $# != 0 ))
then
    #
    :   No parameters should be passed to this routine
    #
    cl_echo 1035 "Usage: $PROGNAME\n" $PROGNAME
    exit 2
fi

if [[ -d $SCD ]]
then
    #
    :   Rename the SCD to the ACD temporarily because
    :   CAA will look for its information in the ACD
    #
    if ! mv $ACD $TEMPPATH/
    then
        CMD="mv $ACD $TEMPPATH/"
        cl_log 701 "NOTE: Received failed return code from command: $CMD\n" $CMD
        exit 1
    fi

    if ! mv $SCD $ACD
    then
        CMD="mv $SCD $ACD"
        cl_log 701 "NOTE: Received failed return code from command: $CMD\n" $CMD
        exit 1
    fi
fi

cname=$(clodmget -f name -n HACMPcluster)
CAA_node_name=$(cl_nn2hn $LOCALNODENAME)

#
# CAA was updated with any new and removed adapters during cldare, but
# clstrmgr would have ignored any subscription callbacks for new adatpers
# because the new config was not in use yet. And clstrmgr has no way to query
# the current state, so we send it the current state from lscluster here
#

#
: We only care about newly added interfaces, which we determine by comparing
: the incoming config in the SCD wih the current config in the ACD.
: At this point in the execution the SCD has been moved to ACD and ACD is 
: in a temporary location
#
incoming_ips=$(ODMDIR=$ACD cllsif -i $LOCALNODENAME -SJ "$OP_SEP" | grep -w boot | cut -d"$OP_SEP" -f7)

for addr in $incoming_ips
do
    #
    : Get the interface for this addr in the current config, if it exists
    #
    current_ip_ifs=$(ODMDIR=$TEMPPATH/active cllsif -i $LOCALNODENAME -n $addr -SJ "$OP_SEP" |cut -d"$OP_SEP" -f9)

    if [[ -z $current_ip_ifs ]] 
    then
        #
	:   IP address \"$addr\" is new - need to update clstrmgr with state
	:   if CAA thinks it is up
        #

        incoming_ip_ifs=$(ODMDIR=$ACD cllsif -i $LOCALNODENAME -n $addr -SJ "$OP_SEP" |cut -d"$OP_SEP" -f9)
        
        incoming_ip_state=$(lscluster -i | grep -p $CAA_node_name | 
                         egrep "Interface number|Interface state" | paste - - | 
                          awk '($4 == "'$incoming_ip_ifs'") {print $8}')

        if [[ $incoming_ip_state == "UP" ]]
        then
            #
            : Address $addr on interface $incoming_ip_ifs is new and UP
            #
	    cl_hats_adapter $incoming_ip_ifs -e $addr dare
        fi
    fi
done

if [[ -d $TEMPPATH ]]
then
    #
    :   Put all the ODM classes back where they originally were
    #
    if ! mv $ACD $SCD
    then
        CMD="mv $ACD $SCD"
        cl_log 701 "NOTE: Received failed return code from command: $CMD\n" $CMD
        exit 1
    fi

    if ! mv $TEMPPATH/active $ACD
    then
        CMD="mv $TEMPPATH/active $ACD"
        cl_log 701 "NOTE: Received failed return code from command: $CMD\n" $CMD
        exit 1
    fi
fi

return $STATUS
