#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG
#  This is an automatically generated prolog.
#
#  Copyright (C) Altran ACT S.A.S. 2017,2018,2021,2022.  All rights reserved.
#
#  ALTRAN_PROLOG_END_TAG
#
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r721 src/43haes/usr/sbin/cluster/events/utils2/cl_apply_roha.sh 1.9 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2015,2016 
# 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 
###############################################################################
# @(#)  5881272 43haes/usr/sbin/cluster/events/utils2/cl_apply_roha.sh, 61aha_r726, 2205A_aha726, May 16 2022 12:15 PM
###############################################################################

#########################################################################
#
#   COMPONENT_NAME: hacmp.events
#
#   DESCRIPTION:
#
#       This script is one of the scripts dedicated to the management of
#       Resource Optimized High Availability (ROHA) functionality for the
#       account of PowerHA SystemMirror.
#
#   FUNCTIONS:
#
#       cl_apply_roha
#
#   NOTE:
#
#       This file should not be called directly. It should be accessed
#       through FPATH environment variable. It also needs that file
#       /usr/es/sbin/cluster/events/utils/clcommonroha is sourced before
#       calling this function. Some global variables need to be exported.
#x
#########################################################################

#================================================
# The following, commented line enforces coding
# standards when this file is edited via vim.
#================================================
# vim:tabstop=4:shiftwidth=4:expandtab:smarttab
#================================================

#=============================================================================
#
# Name:        cl_apply_roha
#
# Description: Perform acquisition/release operations for memory and processor
#              resources.
#
# Inputs:       $1 to indicate if we are in last loop of reassessment.
#              If set to 1, we are in last loop of reassessment.
#              If set to 0, we are in normal reassessment.
#
# Outputs:     None
#
# Globals:     roha_session
#
# Returns:     RC_SUCCESS
#              or else error code for apply step
#
#=============================================================================
function cl_apply_roha
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    : version=@(#)  5881272 43haes/usr/sbin/cluster/events/utils2/cl_apply_roha.sh, 61aha_r726, 2205A_aha726, May 16 2022 12:15 PM

    typeset -i rc=$RC_SUCCESS

    typeset -i LAST_LOOP_OF_FORCE_REASSESSMENT=$1

    #=================================================================
    : LAST_LOOP_OF_FORCE_REASSESSMENT=$LAST_LOOP_OF_FORCE_REASSESSMENT
    #=================================================================

    if [[ ${roha_session.operation} == "acquire" ]] ; then

      if (( CONN_TYPE != 2 )); then
        #=======================================================
        # Synchronously acquire resources.
        # First, Enterprise Pool CoD (yank resources from source
        # node if possible). Then, On/Off CoD. And finally,
        # DLPAR resources.
        # Also, rollback resources in case of error.
        #=======================================================

        roha_apply_yank_codpool_resources
        rc=$?
        (( $rc > 0 )) && { return $(( RC_APPLY_EPCOD_ERROR + rc ));}

        roha_apply_codpool_resources $LAST_LOOP_OF_FORCE_REASSESSMENT
        rc=$?
        (( $rc > 0 )) && { roha_apply_rollback_all_resources ; return $(( RC_APPLY_EPCOD_ERROR + rc ));}

        roha_apply_onoff_resources
        rc=$?
        (( $rc > 0 )) && { roha_apply_rollback_all_resources ; return $(( RC_APPLY_ONOFF_ERROR + rc )) ;}
      fi

        roha_apply_dlpar_resources $LAST_LOOP_OF_FORCE_REASSESSMENT
        rc=$?
        (( $rc > 0 )) && { roha_apply_rollback_all_resources ; return $(( RC_APPLY_DLPAR_ERROR + rc )) ;}

    else # [[ ${roha_session.operation} == "release" ]]

        if [[ ${roha_session.synchronous} == 1 ]] ; then

            #=======================================================
            # Synchronously release resources (in reverse order).
            # First, DLPAR resources. Then, On/Off CoD. And finally,
            #  Enterprise Pool CoD.
            # Also, no rollback.
            #=======================================================

            roha_apply_dlpar_resources
            rc=$?
            (( $rc > 0 )) && return $(( RC_APPLY_DLPAR_ERROR + rc ))

          if (( CONN_TYPE != 2 )); then
            roha_apply_onoff_resources
            rc=$?
            (( $rc > 0 )) && return $(( RC_APPLY_ONOFF_ERROR + rc ))

                roha_apply_codpool_resources
                rc=$?
                (( $rc > 0 )) && return $(( RC_APPLY_EPCOD_ERROR + rc ))
          fi 

        else # [[ ${roha_session.synchronous} == 0 ]]

            #=======================================================
            # Asynchronously release resources and log output to
            #  another file in same directory than hacmp.out :
            #  hacmp.out_async_release_<pid>
            # First, release Enterprise Pool CoD synchronously.
            # Resources become unreturned before being freed by
            #  DLPAR release.
            # Then, DLPAR and On/Off CoD sequentially. DLPAR
            #  resources are ensured to be released when releasing
            #  On/Off CoD.
            # In background, every command returns success.
            #=======================================================

            roha_apply_codpool_resources
            rc=$?
            (( $rc > 0 )) && return $(( RC_APPLY_EPCOD_ERROR + rc ))

            roha_session_write_odm_dynresop WAIT_APPS  "${roha_session.optimal_apps}"
            #=======================================================
            # Check if a previous instance of the Async script is running.
            #=============================================================
            ASYNC_LOGDIR=$(clodmget -q name=hacmp.out -nf defaultdir HACMPlogs)
            ps -eo 'args' | grep -vw grep |grep -qw "clasyncroha"
            if (( $? != 0 )) ; then
                ( clasyncroha ) >$ASYNC_LOGDIR/hacmp.out_async_release_$$.log 2>&1 &
                #=================================
                : background process PID is $!
                #=================================
            fi
        fi

    fi

    return $RC_SUCCESS
} # End of "cl_apply_roha()"
