#!/bin/ksh93
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2017,2021,2022.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
###############################################################################
# @(#)  a9b2496 43haes/usr/sbin/cluster/events/utils2/cl_query_roha.sh, 61aha_r726, 2205E_aha726, Jun 06 2022 01:50 AM
###############################################################################

#########################################################################
#
#   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_query_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.
#
#########################################################################

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

#=============================================================================
#
# Name:        cl_query_roha
#
# Description: Obtain the configuration of ROHA : Query HMC, Nova, LPAR,
#              Applications.
#
# Inputs:      None
#
# Outputs:     None
#
# Globals:     roha_session
#              roha_query
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function cl_query_roha
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    : version=@(#)  a9b2496 43haes/usr/sbin/cluster/events/utils2/cl_query_roha.sh, 61aha_r726, 2205E_aha726, Jun 06 2022 01:50 AM

    typeset -i rc=$RC_SUCCESS

  # Exclude for ROHA on Cloud
  if (( $CONN_TYPE != 2 )); then 
    if [[ -z ${roha_session.preferred_hmc_list} && \
         -z ${roha_session.preferred_nova_list} ]]; then
        return $RC_QUERY_ERROR
    fi

    if [[ -n ${roha_session.preferred_hmc_list} ]]; then
        roha_query_hmc "${roha_session.preferred_hmc_list}"
        rc=$?
        roha_query_hmc_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    fi

    if [[ -n ${roha_session.preferred_nova_list} ]]; then
        roha_query_nova "${roha_session.preferred_nova_list}"
        rc=$?
        roha_query_nova_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    fi
  fi

    roha_query_partition
    rc=$?
    
    roha_query_update_lpar_cur_if_async_in_progress
    
    roha_query_partition_report
    (( $rc > 0 )) && return $RC_QUERY_ERROR

    roha_query_provisioning "roha_query.optimal" "${roha_session.optimal_apps}"
    rc=$?
    roha_query_optimal_report
    (( $rc > 0 )) && return $RC_QUERY_ERROR

    roha_query_provisioning "roha_query.running" "${roha_session.running_apps}"
    rc=$?
    roha_query_running_report
    (( $rc > 0 )) && return $RC_QUERY_ERROR

    if (( $CONN_TYPE == 2 )); then
        roha_query_managed_system
        rc=$?
        roha_query_managed_system_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    fi

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

#=============================================================================
#
# Name:        cl_query_roha_post_compute
#
# Description: Obtain the configuration of ROHA : Query Managed System,
#              Enterprise Pool resources, Enterprise Pool members,
#              Trial CoD, On/Off CoD. Query this information only when
#              roha compute operation successful.
#
# Inputs:      None
#
# Outputs:     None
#
# Globals:     roha_session
#              roha_query
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function cl_query_roha_post_compute
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    : version=@(#)  a9b2496 43haes/usr/sbin/cluster/events/utils2/cl_query_roha.sh, 61aha_r726, 2205E_aha726, Jun 06 2022 01:50 AM

    typeset -i rc=$RC_SUCCESS

    roha_query_managed_system
    rc=$?
    roha_query_managed_system_report
    (( $rc > 0 )) && return $RC_QUERY_ERROR

    if [[ -n ${roha_session.preferred_hmc_list} ]]; then
        roha_query_codpool
        rc=$?
        roha_query_codpool_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    
        roha_query_codpool_members
        rc=$?
        roha_query_codpool_members_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    
        roha_query_trial
        rc=$?
        roha_query_trial_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    
        roha_query_onoff
        rc=$?
        roha_query_onoff_report
        (( $rc > 0 )) && return $RC_QUERY_ERROR
    fi
    roha_query_other_node
    rc=$?
    roha_query_other_node_report
    (( $rc > 0 )) && return $RC_QUERY_ERROR

    #=======================================================
    : Notify user.
    #=======================================================
    if [[ ${roha_query.trial.mem.state} == "Running" ]] ; then
        logger -t "PowerHA SystemMirror for AIX" "Running Trial CoD request of ${roha_query.trial.mem.activated} GB for ${roha_query.trial.mem.days_left} days remaining."
    fi
    if [[ ${roha_query.trial.cpu.state} == "Running" ]] ; then
        logger -t "PowerHA SystemMirror for AIX" "Running Trial CoD request of ${roha_query.trial.cpu.activated} CPU(s) for ${roha_query.trial.cpu.days_left} days remaining."
    fi
    if (( ${roha_query.onoff.mem.unreturned} > 0.00 )) ; then
        logger -t "PowerHA SystemMirror for AIX" "Unreturned On/Off CoD ${roha_query.onoff.mem.unreturned} GB."
    fi
    if (( ${roha_query.onoff.cpu.unreturned} > 0.00 )) ; then
        logger -t "PowerHA SystemMirror for AIX" "Unreturned On/Off CoD ${roha_query.onoff.cpu.unreturned} CPU(s)."
    fi

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