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

#########################################################################
#
#   COMPONENT_NAME: hacmp.events
#
#   CLASS:
#
#       roha_query
#
#   DESCRIPTION:
#
#       This file contains all atomic functions needed to perform
#        'query' step, which is the first step of the ROHA runtime
#        (query step, compute step, identify step, apply step).
#
#   FUNCTIONS:
#
#       roha_query_hmc
#       roha_query_hmc_report
#       roha_query_managed_system
#       roha_query_managed_system_report
#       roha_query_partition
#       roha_query_partition_report
#       roha_query_codpool
#       roha_query_codpool_report
#       roha_query_codpool_members
#       roha_query_codpool_members_report
#       roha_query_trial
#       roha_query_trial_report
#       roha_query_onoff
#       roha_query_onoff_report
#       roha_query_other_node
#       roha_query_other_node_report
#       roha_query_provisioning
#       roha_query_optimal_report
#       roha_query_running_report
#       roha_query_update_lpar_cur_if_async_in_progress
#
#########################################################################

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

#=======================================================
# R O H A _ Q U E R Y
#=======================================================
typeset roha_query=(

    typeset hmc=(
        typeset -A name=""
        typeset -A version=""
    )
    
    typeset nova=(
        typeset -A name=""
        typeset -A version=""
    )    

    typeset cec=(
        typeset name=""
        typeset state=""
        typeset codpool_name=""
        typeset mem=(
            typeset -F4 region_size=0.00  # memory region size (LMB)
            typeset -F4 installed=0.00    # installed memory
            typeset -F4 configurable=0.00 # configurable memory
            typeset -F4 reserved=0.00     # reserved memory (used by firmware)
            typeset -F4 available=0.00    # available DLPAR memory as returned by HMC
            typeset -F4 free=0.00         # available DLPAR memory as calculated by ROHA
            typeset -F4 realtime_inactive=0.00 # diff between installed and configurable
                                               #  updated during identify step
            typeset -F4 deconfigured=0.00 # deconfigured memory on server
        )
        typeset cpu=(
            typeset -F4 installed=0.00
            typeset -F4 configurable=0.00
            typeset -F4 available=0.00
            typeset -F4 free=0.00
            typeset -F4 ratio=0.00
            typeset -F4 realtime_inactive=0.00 # diff between installed and configurable
                                               #  updated during identify step
            typeset -F4 deconfigured=0.00 # deconfigured cpus on server
        )
    )

    typeset lpar=(
        typeset name=""
        typeset state=""
        typeset id=""
        typeset uuid=""
        typeset mem=(
            typeset -F4 min=0.00          # minimum memory for the LPAR
            typeset -F4 des=0.00          # desired memory for the LPAR
            typeset -F4 curr=0.00         # currently assigned memory for the LPAR
            typeset -F4 max=0.00          # maximum memory for the LPAR
        )
        typeset cpu=(
            typeset proc_mode=""
            typeset procs=(
                typeset -i min=0
                typeset -i des=0
                typeset -i curr=0
                typeset -i max=0
            )
            typeset proc_units=(
                typeset -F4 min=0.00
                typeset -F4 des=0.00
                typeset -F4 curr=0.00
                typeset -F4 max=0.00
                typeset spp_name=""
                typeset -i spp_max=0
                typeset -F4 spp_free=0.00
                typeset -F4 spp_reserved=0.00
            )
        )
    )

    typeset codpool=(
        typeset name=""                   # EPCoD name
        typeset state=""                  # EPCoD state
        typeset master=""                 # EPCoD master HMC
        typeset backup=""                 # EPCoD backup HMC
        typeset mem=(
            typeset -F4 available=0.00    # available EPCoD memory for all CECs
            typeset -F4 mobile=0.00       # EPCoD memory allocated to the CEC
            typeset -F4 unreturned=0.00   # unreturned EPCoD memory for the CEC
            typeset -F4 realtime_unreturned=0.00 # unreturned EPCoD memory for the pool, updated during identify
            typeset -F4 capacity=0.00     # total mobile memory for the pool
        )
        typeset cpu=(
            typeset -i available=0        # available EPCoD processor for all CECs
            typeset -i mobile=0           # EPCoD processor allocated to the CEC
            typeset -i unreturned=0       # unreturned EPCoD processor for the CEC
            typeset -F4 realtime_unreturned=0.00 # unreturned EPCoD processor for the pool, updated during identify
            typeset -F4 capacity=0.00     # total mobile processor for the pool
        )
    )

    typeset trial=(
        typeset mem=(
            typeset state=""              # state of Trial CoD memory
            typeset -F4 activated=0.00    # amount of memory currently activated for Trial CoD
            typeset -i days_left=0        # number of days left for current Trial CoD memory request
            typeset -i hours_left=0       # number of hours left for current Trial CoD memory request
        )
        typeset cpu=(
            typeset state=""              # state of Trial CoD processor
            typeset -i activated=0        # amount of processor currently activated for Trial CoD
            typeset -i days_left=0        # number of days left for current Trial CoD processor request
            typeset -i hours_left=0       # number of hours left for current Trial CoD processor request
        )
    )

    typeset onoff=(
        typeset mem=(
            typeset state=""              # state of On/Off CoD memory
            typeset -F4 activated=0.00    # amount of memory currently activated for On/Off CoD
            typeset -F4 unreturned=0.00   # amount of memory currently unreturned for On/Off CoD
            typeset -F4 qty_avail=0.00    # available amount of inactive memory for On/Off CoD memory
            typeset -i days_avail=0       # number of available On/Off GB.days in On/Off CoD Pool
            typeset -i days_left=0        # number of days left in the current On/Off CoD memory request
            typeset -i hours_left=0       # number of hours left in the current On/Off CoD memory request
        )
        typeset cpu=(
            typeset state=""              # state of On/Off CoD processor
            typeset -i activated=0        # amount of processor currently activated for On/Off CoD
            typeset -i unreturned=0       # amount of processor currently unreturned for On/Off CoD
            typeset -i qty_avail=0        # available inactive processors for On/Off CoD processor
            typeset -i days_avail=0       # number of available On/Off processor.days in On/Off CoD Pool
            typeset -i days_left=0        # number of days left in the current On/Off CoD processor request
            typeset -i hours_left=0       # number of hours left in the current On/Off CoD processor request
        )
    )

    # The target lpar in case of release, the source lpar in case of acquire
    typeset other_node=(
        typeset lpar=(
            typeset name=""
            typeset state=""
            typeset id=""
            typeset uuid=""
            typeset mem=(
                typeset -F4 min=0.00
                typeset -F4 curr=0.00
            )
            typeset cpu=(
                typeset proc_mode=""
                typeset procs=(
                    typeset -i min=0
                    typeset -i curr=0
                )
                typeset proc_units=(
                    typeset -F4 min=0.00
                    typeset -F4 curr=0.00
                )
            )
        )
        typeset cec=(
            typeset name=""
            typeset state=""
            typeset mem=(
                typeset -F4 available=0.00
            )
            typeset cpu=(
                typeset -F4 available=0.00
            )
        )
        typeset codpool=(
            typeset name=""
            typeset mem=(
                typeset -F4 mobile=0.00
            )
            typeset cpu=(
                typeset -i mobile=0
            )
        )
    )

    typeset optimal=(
        typeset table_of_apps=(
            typeset -A name             # AC's name : not necessary
            typeset -A use_desired      # if AC use profile desired
            typeset -A mem              # AC's optimal memory
            typeset -A cpu              # AC's optimal CPU
            typeset -A pu               # AC's optimal processing units
            typeset -A vp               # AC's optimal virtual processors
        )
        typeset -i  use_desired_nb=0    # number of ACs using profile desired
        typeset -F4 total_mem=0.00      # total amount of memory needed by ACs
        typeset -i  total_cpu=0         # total number of CPUs needed by ACs
        typeset -F4 total_pu=0.00       # total processing units needed by ACs
        typeset -i  total_vp=0          # total virtual processors needed by ACs
    )

    typeset running=(
        typeset table_of_apps=(
            typeset -A name             # AC's name : not necessary
            typeset -A use_desired      # if AC use profile desired
            typeset -A mem              # AC's optimal memory
            typeset -A cpu              # AC's optimal CPU
            typeset -A pu               # AC's optimal processing units
            typeset -A vp               # AC's optimal virtual processors
        )
        typeset -i  use_desired_nb=0    # number of ACs using profile desired
        typeset -F4 total_mem=0.00      # total amount of memory needed by ACs
        typeset -i  total_cpu=0         # total number of CPUs needed by ACs
        typeset -F4 total_pu=0.00       # total processing units needed by ACs
        typeset -i  total_vp=0          # total virtual processors needed by ACs
    )
)

typeset    roha_query_codpool_members_names=""  # Managed Systems which are members of the EPCoD
typeset -A roha_query_codpool_members   # Two-dimensionnal array,
                                        #  firstly indexed by the Managed System name,
                                        #  and secondly by the property stored.
typeset -i least_hmc_version=0          #  To store the least HMC version

#=============================================================================
#
# Name:        roha_query_hmc
#
# Description: Query information related to a hardware management console.
#
# Inputs:      hmc_list        Space-separated list of HMCs to contact.
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_PARAM_ERROR
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_hmc
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    
    typeset hmc_list=$1
    typeset -i version_num=0
    typeset -i flag=0
    [[ -z $hmc_list ]] && return $RC_PARAM_ERROR

    for hmc in $hmc_list ; do
        out_str=$($CLHMCCMD_CMD -H "$hmc" -o query -a hmc_version)
        if (( $? > 0 )) || [[ $out_str == "unknown" ]] ; then
            roha_query.hmc.name[$hmc]=$hmc
            roha_query.hmc.version[$hmc]="unknown"
        else
            flag=1
            roha_query.hmc.name[$hmc]=$hmc
            roha_query.hmc.version[$hmc]=$out_str
            version_num=$(print "$out_str" | cut -d "R" -f2 | awk -F"." '{print $1$2}')
            if (( $least_hmc_version==0 || $least_hmc_version > $version_num )); then
                least_hmc_version=$version_num
            fi
        fi
    done

    #
    : If None of the HMCs are reachable return failure
    #
    (( $flag ==  0 )) && return $RC_QUERY_ERROR

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

#=============================================================================
#
# Name:        roha_query_hmc_report
#
# Description: Display all information related to a hardware management console.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_hmc_report
{
    roha_session_log "$(printf "+------------------+----------------+\n")"
    roha_session_log "$(printf "| HMC              |     Version    |\n")"
    roha_session_log "$(printf "+------------------+----------------+\n")"
    for hmc in ${roha_query.hmc.name[@]} ; do
        roha_session_log "$(printf "| %         16.16s | %       14.14s |\n" "${hmc}" "${roha_query.hmc.version[$hmc]}")"
    done
    roha_session_log "$(printf "+------------------+----------------+\n\n")"
} # End of "roha_query_hmc_report()"

#=============================================================================
#
# Name:        roha_query_nova
#
# Description: Query information related to a NovaLink.
#
# Inputs:      nova_list        Space-separated list of Novas to contact.
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_PARAM_ERROR
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_nova
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    
    typeset nova_list=$1
    typeset -i flag=0

    [[ -z $nova_list ]] && return $RC_PARAM_ERROR

    for nova in $nova_list ; do
        out_str=$(clnovacmd -H "$nova" -o query -a nova_version)
        if (( $? > 0 )) || [[ $out_str == "unknown" ]] ; then
            roha_query.nova.name[$nova]=$nova
            roha_query.nova.version[$nova]="unknown"
        else
            flag=1
            roha_query.nova.name[$nova]=$nova
            roha_query.nova.version[$nova]=$out_str
        fi
    done

    #
    : If None of the Novas are reachable return failure
    #
    (( $flag ==  0 )) && return $RC_QUERY_ERROR

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

#=============================================================================
#
# Name:        roha_query_nova_report
#
# Description: Display all information related to a NovaLink.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_nova_report
{
    roha_session_log "$(printf "+------------------+----------------+\n")"
    roha_session_log "$(printf "| NovaLink         |     Version    |\n")"
    roha_session_log "$(printf "+------------------+----------------+\n")"
    for nova in ${roha_query.nova.name[@]} ; do
        roha_session_log "$(printf "| %         16.16s | %       14.14s |\n" "${nova}" "${roha_query.nova.version[$nova]}")"
    done
    roha_session_log "$(printf "+------------------+----------------+\n\n")"
} # End of "roha_query_nova_report()"

#=============================================================================
#
# Name:        roha_query_managed_system
#
# Description: Query information related to current managed system.
#
# Inputs:      None, this applies to current managed system
#
# Outputs:     All is stored in global data
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_managed_system
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    # For ROHA on Cloud
    if (( $CONN_TYPE == 2 )); then
        out_str=$($CLHMCCMD_CMD -o query -M ${roha_query.lpar.id})
        (( $? > 0 )) && return $RC_QUERY_ERROR
        print "$out_str" | grep -v "#" | IFS=: read \
                                  roha_query.cec.mem.available \
                                  roha_query.cec.cpu.available
        return $RC_SUCCESS
    fi

    typeset attrs="\
sys_name:sys_state:\
mem_region_size:installed_sys_mem:configurable_sys_mem:\
sys_firmware_mem:curr_avail_sys_mem:curr_free_sys_mem:deconfig_sys_mem:\
installed_sys_proc_units:configurable_sys_proc_units:curr_avail_sys_proc_units:\
curr_free_sys_proc_units:deconfig_sys_proc_units"
    
    if [[ -n ${roha_session.preferred_nova_list} ]]; then
        out_str=$(clnovacmd -o query -a $attrs) 
    else
        out_str=$($CLHMCCMD_CMD -o query -a $attrs)
    fi
    (( $? > 0 )) && return $RC_QUERY_ERROR
    print "$out_str" | IFS=: read roha_query.cec.name roha_query.cec.state \
                                  roha_query.cec.mem.region_size roha_query.cec.mem.installed roha_query.cec.mem.configurable \
                                  roha_query.cec.mem.reserved roha_query.cec.mem.available roha_query.cec.mem.free roha_query.cec.mem.deconfigured \
                                  roha_query.cec.cpu.installed roha_query.cec.cpu.configurable roha_query.cec.cpu.available \
                                  roha_query.cec.cpu.free roha_query.cec.cpu.deconfigured

    if [[ -n ${roha_session.preferred_hmc_list} ]]; then
        attrs="codpool_name"
        out_str=$($CLHMCCMD_CMD -o query -a $attrs)
        (( $? > 0 )) && return $RC_QUERY_ERROR
        roha_query.cec.codpool_name=$out_str
    fi

    #=======================================================
    # In case shared memory pool is enabled in CEC,
    # Free calculated memory (roha_query.cec.mem.free) may be lesser than the CEC free pool memory (roha_query.cec.mem.available)
    # So, use whichever is greater for free pool.
    #=======================================================
    if (( ${roha_query.cec.mem.available} > ${roha_query.cec.mem.free} )); then
        (( roha_query.cec.mem.free = ${roha_query.cec.mem.available} ))
    fi

    for attr in roha_query.cec.mem.region_size roha_query.cec.mem.installed roha_query.cec.mem.configurable \
                roha_query.cec.mem.reserved roha_query.cec.mem.available roha_query.cec.mem.free \
                roha_query.cec.mem.deconfigured; do
        #=======================================================
        # Convert memory to GB.
        #=======================================================
        (( ${attr} /= 1024.00 ))
    done

    #=======================================================
    : Prepare values for realtime_inactive
    : Amount of realtime_inactive is initially the difference
    :  between amount of resources installed and amount of
    :  resources configurable.
    #=======================================================
    (( roha_query.cec.mem.realtime_inactive = ${roha_query.cec.mem.installed} - ${roha_query.cec.mem.configurable} - ${roha_query.cec.mem.deconfigured} ))
    roha_query.cec.mem.realtime_inactive=$(print ${roha_query.cec.mem.realtime_inactive} | awk '{printf "%0.4f\n",int($0)}')

    (( roha_query.cec.cpu.realtime_inactive = ${roha_query.cec.cpu.installed} - ${roha_query.cec.cpu.configurable} - ${roha_query.cec.cpu.deconfigured} ))
    roha_query.cec.cpu.realtime_inactive=$(print ${roha_query.cec.cpu.realtime_inactive} | awk '{printf "%0.4f\n",int($0)}')

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

#=============================================================================
#
# Name:        roha_query_managed_system_report
#
# Description: Display all information related to current managed system.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_managed_system_report
{
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| MANAGED SYSTEM   |   Memory (GB)  |  Proc Unit(s)  |\n")"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
  if (( $CONN_TYPE == 2 )); then
    roha_session_log "$(printf "| HostID           |   %                    27.27s   |\n" ${roha_query.lpar.id})"
    roha_session_log "$(printf "| Available        |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.available} ${roha_query.cec.cpu.available})"
  else
    roha_session_log "$(printf "| Name             |   %                    27.27s   |\n" "${roha_query.cec.name}")"
    roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.cec.state}")"
    roha_session_log "$(printf "| Region Size      |   %    10.4f   |        /       |\n" ${roha_query.cec.mem.region_size})"
    roha_session_log "$(printf "| VP/PU Ratio      |        /       |    %  7.4f     |\n" ${roha_query.cec.cpu.ratio})"
    roha_session_log "$(printf "| Installed        |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.installed} ${roha_query.cec.cpu.installed})"
    roha_session_log "$(printf "| Configurable     |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.configurable} ${roha_query.cec.cpu.configurable})"
    roha_session_log "$(printf "| Deconfigured     |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.deconfigured} ${roha_query.cec.cpu.deconfigured})"
    roha_session_log "$(printf "| Reserved         |   %    10.4f   |         /      |\n" ${roha_query.cec.mem.reserved})"
    roha_session_log "$(printf "| Available        |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.available} ${roha_query.cec.cpu.available})"
    roha_session_log "$(printf "| Free (computed)  |   %    10.4f   |    %  7.4f     |\n" ${roha_query.cec.mem.free} ${roha_query.cec.cpu.free})"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"
  fi
} # End of "roha_query_managed_system_report()"

#=============================================================================
#
# Name:        roha_query_partition
#
# Description: Query information related to a partition.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_partition
{
  [[ $VERBOSE_LOGGING == high ]] && set -x

  # For ROHA on Cloud 
  if (( $CONN_TYPE == 2 )); then
    typeset attrs="minmem:processors:serverName:maxproc:health:memory:maxmem:minproc:hostID"
  else
    typeset -F4 cec_pu_vp_ratio=0.00
    typeset attrs="lpar_name:lpar_state:\
lpar_id:uuid:\
curr_min_mem:curr_mem:\
curr_max_mem:prof_desired_mem:\
curr_proc_mode:\
curr_min_procs:curr_procs:\
curr_max_procs:prof_desired_procs:\
curr_min_proc_units:curr_proc_units:\
curr_max_proc_units:prof_desired_proc_units:\
curr_shared_proc_pool_name:max_pool_proc_units:\
curr_free_pool_proc_units:curr_reserved_pool_proc_units:\
min_proc_units_per_virtual_proc"
fi

    if [[ -n ${roha_session.preferred_nova_list} ]]; then
        out_str=$(clnovacmd -o query -a $attrs)
        (( $? > 0 )) && return $RC_QUERY_ERROR
    else
        out_str=$($CLHMCCMD_CMD -o query -a $attrs)
        (( $? > 0 )) && return $RC_QUERY_ERROR
    fi

  # For ROHA on Cloud 
  if (( $CONN_TYPE == 2 )); then
    print "$out_str" | grep -v "#" | IFS=: read \
                                  roha_query.lpar.mem.min \
                                  roha_query.lpar.cpu.proc_units.curr \
                                  roha_query.lpar.name\
                                  roha_query.lpar.cpu.proc_units.max \
                                  roha_query.lpar.state \
                                  roha_query.lpar.mem.curr \
                                  roha_query.lpar.mem.max \
                                  roha_query.lpar.cpu.proc_units.min \
                                  roha_query.lpar.id
    roha_query.lpar.cpu.proc_mode="pvm"
    roha_query.lpar.mem.des=roha_query.lpar.mem.min
    roha_query.lpar.cpu.proc_units.des=roha_query.lpar.cpu.proc_units.min
  else
    print "$out_str" | IFS=: read roha_query.lpar.name roha_query.lpar.state \
                                  roha_query.lpar.id roha_query.lpar.uuid \
                                  roha_query.lpar.mem.min roha_query.lpar.mem.curr \
                                  roha_query.lpar.mem.max roha_query.lpar.mem.des \
                                  roha_query.lpar.cpu.proc_mode \
                                  roha_query.lpar.cpu.procs.min roha_query.lpar.cpu.procs.curr \
                                  roha_query.lpar.cpu.procs.max roha_query.lpar.cpu.procs.des \
                                  roha_query.lpar.cpu.proc_units.min roha_query.lpar.cpu.proc_units.curr \
                                  roha_query.lpar.cpu.proc_units.max roha_query.lpar.cpu.proc_units.des \
                                  roha_query.lpar.cpu.proc_units.spp_name roha_query.lpar.cpu.proc_units.spp_max \
                                  roha_query.lpar.cpu.proc_units.spp_free roha_query.lpar.cpu.proc_units.spp_reserved \
                                  cec_pu_vp_ratio

    for attr in roha_query.lpar.mem.min roha_query.lpar.mem.des roha_query.lpar.mem.curr roha_query.lpar.mem.max ; do
        #=======================================================
        # Convert memory to GB.
        #=======================================================
        (( ${attr} /= 1024.00 ))
    done

    #=======================================================
    # Get the CEC minimum pu/vp
    #=======================================================
    (( roha_query.cec.cpu.ratio = ${cec_pu_vp_ratio} ))

  fi

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

#=============================================================================
#
# Name:        roha_query_partition_report
#
# Description: Display all information related to a partition.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_partition_report
{

    # For ROHA on Cloud
    if [[ ${roha_query.lpar.cpu.proc_mode} == "pvm" ]] ; then
        typeset cloudType=$($CLODMGET_CMD -q "name=$LOCALNODENAME and object=POWERVS_CLOUD" -nf value HACMPnode 2>/dev/null)
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| LPAR (%.6s Cloud)|   Memory (GB)  |     CPU(s)     |\n" "${cloudType}")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Name             |   %                    27.27s   |\n" ${roha_query.lpar.name})"
        roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.lpar.state}")"
        roha_session_log "$(printf "| Id               |   %                    27.27s   |\n" ${roha_query.lpar.id})"
        roha_session_log "$(printf "| Minimum          |   %    10.4f   |  %   10.4f     |\n" ${roha_query.lpar.mem.min} ${roha_query.lpar.cpu.proc_units.min})"
        roha_session_log "$(printf "| Desired          |   %    10.4f   |  %   10.4f     |\n" ${roha_query.lpar.mem.des} ${roha_query.lpar.cpu.proc_units.des})"
        roha_session_log "$(printf "| Assigned         |   %    10.4f   |  %   10.4f     |\n" ${roha_query.lpar.mem.curr} ${roha_query.lpar.cpu.proc_units.curr})"
        roha_session_log "$(printf "| Maximum          |   %    10.4f   |  %   10.4f     |\n" ${roha_query.lpar.mem.max} ${roha_query.lpar.cpu.proc_units.max})"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"

    elif [[ ${roha_query.lpar.cpu.proc_mode} == "shared" ]] ; then
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| LPAR (shared)    |   Memory (GB)  |   PU(s)/VP(s)  |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Name             |   %                    27.27s   |\n" ${roha_query.lpar.name})"
        roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.lpar.state}")"
        roha_session_log "$(printf "| Id               |   %                    27.27s   |\n" ${roha_query.lpar.id})"
        roha_session_log "$(printf "| Uuid             |   %                    27.27s   |\n" ${roha_query.lpar.uuid})"
        roha_session_log "$(printf "| VP/PU Ratio      |   %                    26.4f    |\n" ${roha_query.cec.cpu.ratio})"
        roha_session_log "$(printf "| Minimum          |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.lpar.mem.min} ${roha_query.lpar.cpu.proc_units.min} ${roha_query.lpar.cpu.procs.min})"
        roha_session_log "$(printf "| Desired          |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.lpar.mem.des} ${roha_query.lpar.cpu.proc_units.des} ${roha_query.lpar.cpu.procs.des})"
        roha_session_log "$(printf "| Assigned         |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.lpar.mem.curr} ${roha_query.lpar.cpu.proc_units.curr} ${roha_query.lpar.cpu.procs.curr})"
        roha_session_log "$(printf "| Maximum          |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.lpar.mem.max} ${roha_query.lpar.cpu.proc_units.max} ${roha_query.lpar.cpu.procs.max})"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"

        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| SHARED PROC POOL |                |      PU(s)     |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Name             |                | %       14.14s |\n" ${roha_query.lpar.cpu.proc_units.spp_name})"
        roha_session_log "$(printf "| Maximum          |                |      %3d       |\n" ${roha_query.lpar.cpu.proc_units.spp_max})"
        roha_session_log "$(printf "| Free             |                |     % 6.4f     |\n" ${roha_query.lpar.cpu.proc_units.spp_free})"
        roha_session_log "$(printf "| Reserved         |                |     % 6.4f     |\n" ${roha_query.lpar.cpu.proc_units.spp_reserved})"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"

    else # [[ ${roha_query.lpar.cpu.proc_mode} == "ded" ]]
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| LPAR (dedicated) |   Memory (GB)  |     CPU(s)     |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Name             |   %                    27.27s   |\n" ${roha_query.lpar.name})"
        roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.lpar.state}")"
        roha_session_log "$(printf "| Id               |   %                    27.27s   |\n" ${roha_query.lpar.id})"
        roha_session_log "$(printf "| Uuid             |   %                    27.27s   |\n" ${roha_query.lpar.uuid})"
        roha_session_log "$(printf "| Minimum          |   %    10.4f   |      %3d       |\n" ${roha_query.lpar.mem.min} ${roha_query.lpar.cpu.procs.min})"
        roha_session_log "$(printf "| Desired          |   %    10.4f   |      %3d       |\n" ${roha_query.lpar.mem.des} ${roha_query.lpar.cpu.procs.des})"
        roha_session_log "$(printf "| Assigned         |   %    10.4f   |      %3d       |\n" ${roha_query.lpar.mem.curr} ${roha_query.lpar.cpu.procs.curr})"
        roha_session_log "$(printf "| Maximum          |   %    10.4f   |      %3d       |\n" ${roha_query.lpar.mem.max} ${roha_query.lpar.cpu.procs.max})"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"
    fi
} # End of "roha_query_partition_report()"

#=============================================================================
#
# Name:        roha_query_codpool
#
# Description: Query information related to an Enterprise Pool.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_codpool
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    typeset attrs="codpool_name:mobile_state:\
master_mc_name:backup_master_mc_name:\
mobile_mem:mobile_procs:\
unreturned_mobile_mem:\
unreturned_mobile_procs:\
avail_mobile_mem:\
sys_mobile_mem:sys_unreturned_mobile_mem:\
avail_mobile_procs:\
sys_mobile_procs:sys_unreturned_mobile_procs"

    out_str=$($CLHMCCMD_CMD -o query -a $attrs)
    (( $? > 0 )) && return $RC_QUERY_ERROR

    print "$out_str" | IFS=: read roha_query.codpool.name          roha_query.codpool.state \
                                  roha_query.codpool.master        roha_query.codpool.backup \
                                  roha_query.codpool.mem.capacity  roha_query.codpool.cpu.capacity \
                                  roha_query.codpool.mem.realtime_unreturned \
                                  roha_query.codpool.cpu.realtime_unreturned \
                                  roha_query.codpool.mem.available \
                                  roha_query.codpool.mem.mobile    roha_query.codpool.mem.unreturned \
                                  roha_query.codpool.cpu.available \
                                  roha_query.codpool.cpu.mobile    roha_query.codpool.cpu.unreturned

    for attr in roha_query.codpool.mem.capacity roha_query.codpool.mem.realtime_unreturned roha_query.codpool.mem.available roha_query.codpool.mem.mobile roha_query.codpool.mem.unreturned ; do
        #=======================================================
        # Convert memory to GB.
        #=======================================================
        (( ${attr} /= 1024.00 ))
    done
    
    #=================================================================
    : Unreturned resources are also counted into inactive :
    :  because unreturned resources are potential inactive resources,
    :  if they were returned by LPAR to CEC, they would become
    :  inactive resources.
    #=================================================================
    (( roha_query.cec.mem.realtime_inactive += ${roha_query.codpool.mem.unreturned} ))
    (( roha_query.cec.cpu.realtime_inactive += ${roha_query.codpool.cpu.unreturned} ))

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

#=============================================================================
#
# Name:        roha_query_codpool_report
#
# Description: Display all information related to an enterprise pool.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_codpool_report
{
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| ENTERPRISE POOL  |   Memory (GB)  |     CPU(s)     |\n")"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| Name             |   %                    27.27s   |\n" "${roha_query.codpool.name}")"
    roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.codpool.state}")"
    roha_session_log "$(printf "| Master HMC       |   %                    27.27s   |\n" "${roha_query.codpool.master}")"
    if (( $least_hmc_version < 85 ));then
        roha_session_log "$(printf "| Backup HMC       |   %                    27.27s   |\n" "${roha_query.codpool.backup}")"
    fi
    roha_session_log "$(printf "| Capacity (total) |   %    10.4f   |      %3d       |\n" ${roha_query.codpool.mem.capacity} ${roha_query.codpool.cpu.capacity})"
    roha_session_log "$(printf "| Available        |   %    10.4f   |      %3d       |\n" ${roha_query.codpool.mem.available} ${roha_query.codpool.cpu.available})"
    roha_session_log "$(printf "| Unreturned       |   %    10.4f   |      %3d       |\n" ${roha_query.codpool.mem.realtime_unreturned} ${roha_query.codpool.cpu.realtime_unreturned})"
    roha_session_log "$(printf "| Unreturned (MS)  |   %    10.4f   |      %3d       |\n" ${roha_query.codpool.mem.unreturned} ${roha_query.codpool.cpu.unreturned})"
    roha_session_log "$(printf "| Mobile (MS)      |   %    10.4f   |      %3d       |\n" ${roha_query.codpool.mem.mobile} ${roha_query.codpool.cpu.mobile})"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"
} # End of "roha_query_codpool_report()"



#=============================================================================
#
# Name:        roha_query_codpool_members
#
# Description: Query information related to members of an Enterprise Pool.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_codpool_members
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    typeset -i rc=$RC_SUCCESS
    typeset -i op_rc=$RC_SUCCESS
    typeset hmc_list=""

    typeset EPCOD_NAME=${roha_query.codpool.name}
    typeset SYS_NAMES=""
    typeset SYS_NAME=""
    typeset OUT_STR=""

    if [[ -n $EPCOD_NAME && $EPCOD_NAME != "unknown" ]]; then
        SYS_NAMES=$($CLHMCCMD_CMD -o query -a codpool_sys_names -E $EPCOD_NAME)
        op_rc=$?
        if (( $op_rc == 0 )); then
            if [[ -n $SYS_NAMES ]]; then
                for SYS_NAME in $SYS_NAMES; do
                    #
                    : define compound to store data of this Managed System
                    #
                    typeset member=(
                        typeset state=""
                        # mem
                        typeset -F4 curr_avail_sys_mem=0.00
                        typeset -F4 curr_free_sys_mem=0.00
                        typeset -F4 sys_mobile_mem=0.00
                        typeset -F4 sys_inactive_mem=0.00
                        typeset -F4 sys_unreturned_mobile_mem=0.00
                        typeset -F4 deconfig_sys_mem=0.00
                        # procs
                        typeset -F4 curr_avail_sys_proc_units=0.00
                        typeset -F4 curr_free_sys_proc_units=0.00
                        typeset -F4 sys_mobile_procs=0.00
                        typeset -F4 sys_inactive_procs=0.00
                        typeset -F4 sys_unreturned_mobile_procs=0.00
                        typeset -F4 deconfig_sys_proc_units=0.00
                    )

                    typeset epcod_attrs="sys_mobile_mem:sys_inactive_mem:sys_unreturned_mobile_mem:deconfig_sys_mem:\
sys_mobile_procs:sys_inactive_procs:sys_unreturned_mobile_procs:deconfig_sys_proc_units"
                    OUT_STR=$($CLHMCCMD_CMD -o query -a $epcod_attrs -E $EPCOD_NAME -M $SYS_NAME)
                    op_rc=$?
                    if (( $op_rc == 0 )); then
                        print "$OUT_STR" | IFS=: read \
                            member.sys_mobile_mem \
                            member.sys_inactive_mem \
                            member.sys_unreturned_mobile_mem \
                            member.deconfig_sys_mem \
                            member.sys_mobile_procs \
                            member.sys_inactive_procs \
                            member.sys_unreturned_mobile_procs \
                            member.deconfig_sys_proc_units
                        for attr in member.sys_mobile_mem member.sys_inactive_mem member.sys_unreturned_mobile_mem member.deconfig_sys_mem; do
                            #=======================================================
                            # Convert memory to GB.
                            #=======================================================
                            (( ${attr} /= 1024.00 ))
                        done
                    else
                        rc=$RC_QUERY_ERROR
                    fi

                    hmc_list=$(clodmget -f default_hmcs_list -n HACMPhmcparam | tr ' ' ':')
                    typeset sys_attrs="sys_state:curr_avail_sys_mem:curr_free_sys_mem:\
curr_avail_sys_proc_units:curr_free_sys_proc_units"
                    OUT_STR=$($CLHMCCMD_CMD -o query -H $hmc_list -a $sys_attrs -M $SYS_NAME)
                    op_rc=$?
                    if (( $op_rc == 0 )); then
                        print "$OUT_STR" | IFS=: read \
                            member.state \
                            member.curr_avail_sys_mem \
                            member.curr_free_sys_mem \
                            member.curr_avail_sys_proc_units \
                            member.curr_free_sys_proc_units
                        for attr in member.curr_avail_sys_mem member.curr_free_sys_mem ; do
                            #=======================================================
                            # Convert memory to GB.
                            #=======================================================
                            (( ${attr} /= 1024.00 ))
                        done
                    else
                        #
                        : We should not return failure for the CEC that does not
                        : contain the nodes in powerHA cluster
                        #
                        continue
                    fi

                    #
                    : Add this new member into the array members into roha_query_codpool_members,
                    :  this is a two-dimensionnal array :
                    :   indexed firstly by the Managed System name,
                    :   and secondly by the property stored.
                    #
                    roha_query_codpool_members["${SYS_NAME}" "state"]=${member.state}
                    roha_query_codpool_members["${SYS_NAME}" "curr_avail_sys_mem"]=${member.curr_avail_sys_mem}
                    roha_query_codpool_members["${SYS_NAME}" "curr_free_sys_mem"]=${member.curr_free_sys_mem}
                    roha_query_codpool_members["${SYS_NAME}" "sys_mobile_mem"]=${member.sys_mobile_mem}
                    roha_query_codpool_members["${SYS_NAME}" "sys_inactive_mem"]=${member.sys_inactive_mem}
                    roha_query_codpool_members["${SYS_NAME}" "sys_unreturned_mobile_mem"]=${member.sys_unreturned_mobile_mem}
                    roha_query_codpool_members["${SYS_NAME}" "deconfig_sys_mem"]=${member.deconfig_sys_mem}
                    roha_query_codpool_members["${SYS_NAME}" "curr_avail_sys_proc_units"]=${member.curr_avail_sys_proc_units}
                    roha_query_codpool_members["${SYS_NAME}" "curr_free_sys_proc_units"]=${member.curr_free_sys_proc_units}
                    roha_query_codpool_members["${SYS_NAME}" "sys_mobile_procs"]=${member.sys_mobile_procs}
                    roha_query_codpool_members["${SYS_NAME}" "sys_inactive_procs"]=${member.sys_inactive_procs}
                    roha_query_codpool_members["${SYS_NAME}" "sys_unreturned_mobile_procs"]=${member.sys_unreturned_mobile_procs}
                    roha_query_codpool_members["${SYS_NAME}" "deconfig_sys_proc_units"]=${member.deconfig_sys_proc_units}

                    #=======================================================
                    : Realtime values are used to know if the yanks are done
                    : compliantly or not, so that realtime unreturned can be
                    : accurately computed. So what we want to handle is the
                    : available value, rounded down
                    #=======================================================
                    roha_query_codpool_members["${SYS_NAME}" "realtime_curr_avail_sys_mem"]=$(print ${member.curr_avail_sys_mem} | awk '{printf "%0.4f\n",int($0)}')
                    roha_query_codpool_members["${SYS_NAME}" "realtime_curr_avail_sys_proc_units"]=$(print ${member.curr_avail_sys_proc_units} | awk '{printf "%0.4f\n",int($0)}')

                done

                # We store once the Managed Systems which are members of the EPCoD
                roha_query_codpool_members_names=$(print -- "${!roha_query_codpool_members[@]}" | tr ' ' '\n' | sed 'n; d' | sort -u)
            else
                #
                : Cannot get systems members of the pool.
                #
                rc=$RC_QUERY_ERROR
            fi
        fi
    fi
    (( $rc > 0 )) && return $RC_QUERY_ERROR || return $RC_SUCCESS
} # End of "roha_query_codpool_members()"

#=============================================================================
#
# Name:        roha_query_codpool_members_report
#
# Description: Display all information related to an enterprise pool.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_codpool_members_report
{
    roha_session_log "$(printf "+------------------+-----------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| ENTERPRISE POOL  | MANAGED_SYSTEM  |   Memory (GB)  |     CPU(s)     |\n")"
    roha_session_log "$(printf "+------------------+-----------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| Name             |  %                                       46.46s   |\n" "${roha_query.codpool.name}")"

    typeset SYS_NAME=""
    for SYS_NAME in ${roha_query_codpool_members_names}; do

        roha_session_log "$(printf "+------------------+-----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "|                  | Name            |   %                    27.27s   |\n" "${SYS_NAME}")"
        roha_session_log "$(printf "+------------------+-----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "|                  | State           |   %                    27.27s   |\n" "${roha_query_codpool_members["$SYS_NAME" "state"]}")"
        roha_session_log "$(printf "|                  | Mobile          |   %    10.4f   |    %   6d      |\n" \
${roha_query_codpool_members["$SYS_NAME" "sys_mobile_mem"]}            ${roha_query_codpool_members["$SYS_NAME" "sys_mobile_procs"]})"
        roha_session_log "$(printf "|                  | Inactive        |   %    10.4f   |    %   6d      |\n" \
${roha_query_codpool_members["$SYS_NAME" "sys_inactive_mem"]}          ${roha_query_codpool_members["$SYS_NAME" "sys_inactive_procs"]})"
        roha_session_log "$(printf "|                  | Deconfigured    |   %    10.4f   |    %   9.4f    |\n" \
${roha_query_codpool_members["$SYS_NAME" "deconfig_sys_mem"]}          ${roha_query_codpool_members["$SYS_NAME" "deconfig_sys_proc_units"]})"
        roha_session_log "$(printf "|                  | Unreturned      |   %    10.4f   |    %   6d      |\n" \
${roha_query_codpool_members["$SYS_NAME" "sys_unreturned_mobile_mem"]} ${roha_query_codpool_members["$SYS_NAME" "sys_unreturned_mobile_procs"]})"
        roha_session_log "$(printf "|                  | Available DLPAR |   %    10.4f   |    %    9.4f   |\n" \
${roha_query_codpool_members["$SYS_NAME" "curr_avail_sys_mem"]}        ${roha_query_codpool_members["$SYS_NAME" "curr_avail_sys_proc_units"]})"
        roha_session_log "$(printf "|                  | Free DLPAR      |   %    10.4f   |    %    9.4f   |\n" \
${roha_query_codpool_members["$SYS_NAME" "curr_free_sys_mem"]}         ${roha_query_codpool_members["$SYS_NAME" "curr_free_sys_proc_units"]})"
        roha_session_log "$(printf "+------------------+-----------------+----------------+----------------+\n")"
    done
} # End of "roha_query_codpool_members_report()"

#=============================================================================
#
# Name:        roha_query_trial
#
# Description: Query information related to Trial CoD for a managed system.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_trial
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    typeset attrs="mem_trial_state:activated_trial_mem:\
mem_trial_days_left:mem_trial_hours_left:\
proc_trial_state:activated_trial_procs:\
proc_trial_days_left:proc_trial_hours_left"

    out_str=$($CLHMCCMD_CMD -o query -a $attrs)
    (( $? > 0 )) && return $RC_QUERY_ERROR

    print "$out_str" | IFS=: read roha_query.trial.mem.state roha_query.trial.mem.activated \
                                  roha_query.trial.mem.days_left roha_query.trial.mem.hours_left \
                                  roha_query.trial.cpu.state roha_query.trial.cpu.activated \
                                  roha_query.trial.cpu.days_left roha_query.trial.cpu.hours_left

    for attr in roha_query.trial.mem.activated ; do
        #=======================================================
        # Convert memory to GB.
        #=======================================================
        (( ${attr} /= 1024.00 ))
    done

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

#=============================================================================
#
# Name:        roha_query_trial_report
#
# Description: Display all information related to Trial CoD for a managed system.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_trial_report
{
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| TRIAL COD        |   Memory (GB)  |     CPU(s)     |\n")"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| State            | %       14.14s | %       14.14s |\n" "${roha_query.trial.mem.state}" "${roha_query.trial.cpu.state}")"
    roha_session_log "$(printf "| Activated        |   %    10.4f   |      %3d       |\n" ${roha_query.trial.mem.activated} ${roha_query.trial.cpu.activated})"
    roha_session_log "$(printf "| Days left        |       %3d      |      %3d       |\n" ${roha_query.trial.mem.days_left} ${roha_query.trial.cpu.days_left})"
    roha_session_log "$(printf "| Hours left       |       %3d      |      %3d       |\n" ${roha_query.trial.mem.hours_left} ${roha_query.trial.cpu.hours_left})"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"
} # End of "roha_query_trial_report()"

#=============================================================================
#
# Name:        roha_query_onoff
#
# Description: Query information related to On/Off CoD for a managed system.
#
# Inputs:      $1 : save / nosave  (if no value passed, default to save)
#                   Save queried information into HACMPdynresop
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_onoff
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    [[ -n $1 && $1 != "save" && $1 != "nosave" ]] \
        && return $RC_FAILURE

    # avail_mem_for_onoff : inactive, or unlicensed resource
    # avail_procs_for_onoff : inactive, or unlicensed resource

    typeset attrs="mem_onoff_state:\
activated_onoff_mem:unreturned_onoff_mem:\
avail_mem_for_onoff:onoff_mem_days_avail:\
onoff_request_mem_days_left:\
onoff_mem_day_hours_left:\
proc_onoff_state:\
activated_onoff_procs:unreturned_onoff_procs:\
avail_procs_for_onoff:onoff_proc_days_avail:\
onoff_request_proc_days_left:\
onoff_proc_day_hours_left"

    out_str=$($CLHMCCMD_CMD -o query -a $attrs)
    (( $? > 0 )) && return $RC_QUERY_ERROR

    print "$out_str" | IFS=: read roha_query.onoff.mem.state \
                                  roha_query.onoff.mem.activated roha_query.onoff.mem.unreturned \
                                  roha_query.onoff.mem.qty_avail roha_query.onoff.mem.days_avail \
                                  roha_query.onoff.mem.days_left \
                                  roha_query.onoff.mem.hours_left \
                                  roha_query.onoff.cpu.state \
                                  roha_query.onoff.cpu.activated roha_query.onoff.cpu.unreturned \
                                  roha_query.onoff.cpu.qty_avail roha_query.onoff.cpu.days_avail \
                                  roha_query.onoff.cpu.days_left \
                                  roha_query.onoff.cpu.hours_left

    for attr in roha_query.onoff.mem.activated roha_query.onoff.mem.unreturned roha_query.onoff.mem.qty_avail ; do
        #=======================================================
        # Convert memory to GB.
        #=======================================================
        (( ${attr} /= 1024.00 ))
    done

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

#=============================================================================
#
# Name:        roha_query_onoff_report
#
# Description: Display all information related to On/Off CoD for a managed system.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_onoff_report
{
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| ONOFF COD        |   Memory (GB)  |     CPU(s)     |\n")"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    roha_session_log "$(printf "| State            | %       14.14s | %       14.14s |\n" "${roha_query.onoff.mem.state}" "${roha_query.onoff.cpu.state}")"
    roha_session_log "$(printf "| Activated        |   %    10.4f   |   %   6d       |\n" ${roha_query.onoff.mem.activated} ${roha_query.onoff.cpu.activated})"
    roha_session_log "$(printf "| Unreturned       |   %    10.4f   |   %   6d       |\n" ${roha_query.onoff.mem.unreturned} ${roha_query.onoff.cpu.unreturned})"
    roha_session_log "$(printf "| Available        |   %    10.4f   |   %   6d       |\n" ${roha_query.onoff.mem.qty_avail} ${roha_query.onoff.cpu.qty_avail})"
    roha_session_log "$(printf "| Days available   |    %   6d      |   %   6d       |\n" ${roha_query.onoff.mem.days_avail} ${roha_query.onoff.cpu.days_avail})"
    roha_session_log "$(printf "| Days left        |    %   6d      |   %   6d       |\n" ${roha_query.onoff.mem.days_left} ${roha_query.onoff.cpu.days_left})"
    roha_session_log "$(printf "| Hours left       |    %   6d      |   %   6d       |\n" ${roha_query.onoff.mem.hours_left} ${roha_query.onoff.cpu.hours_left})"
    roha_session_log "$(printf "+------------------+----------------+----------------+\n\n")"
} # End of "roha_query_onoff_report()"

#=============================================================================
#
# Name:        roha_query_other_node
#
# Description: Query information related to the other (source or target) node.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_other_node
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    if [[ -n ${roha_session.other_lpar} ]] ; then

        #=======================================================
        # While acquiring, query info from the source node.
        #=======================================================
        typeset attrs="lpar_name:lpar_state:\
lpar_id:uuid:\
curr_min_mem:curr_mem:\
curr_proc_mode:\
curr_min_procs:curr_procs:\
curr_min_proc_units:curr_proc_units:\
sys_name:sys_state:\
curr_avail_sys_mem:curr_avail_sys_proc_units"
        
        #================================================================
        # cl_get_nova_list and cl_get_hmc_list needs a node name as argument.
        #  nodename/lparname match are stored into HACMPdynresop ODM
        #  We use cl_dynresop to retrieve match between lparname and
        #  nodename into HACMPdynresop ODM
        #================================================================
        typeset nodename=
        nodename=$(LC_ALL=C cl_dynresop -l ${roha_session.other_lpar} -N)
        rc=$?
        if (( rc != 0 )) || [[ -z $nodename ]] ; then
            #
            : best can do, nodename is set to lpar name
            #
            nodename=${roha_session.other_lpar}
        fi

        #=======================================================
        # Check whetehr NovaLink is configured on other node.
        #=======================================================        
        typeset other_node_nova_list=        
        other_node_nova_list=$(cl_get_nova_list -n $nodename 2>/dev/null)
        if (( $? == 0 )) && [[ -n $other_node_nova_list ]]; then
            out_str=$(clnovacmd -o query -a $attrs -L ${roha_session.other_lpar} -H "$other_node_nova_list")
        else
            out_str=$($CLHMCCMD_CMD -o query -a $attrs -L ${roha_session.other_lpar})
        fi
        (( $? > 0 )) && return $RC_QUERY_ERROR

        print "$out_str" | IFS=: read roha_query.other_node.lpar.name               roha_query.other_node.lpar.state \
                                      roha_query.other_node.lpar.id                 roha_query.other_node.lpar.uuid \
                                      roha_query.other_node.lpar.mem.min            roha_query.other_node.lpar.mem.curr \
                                      roha_query.other_node.lpar.cpu.proc_mode \
                                      roha_query.other_node.lpar.cpu.procs.min      roha_query.other_node.lpar.cpu.procs.curr \
                                      roha_query.other_node.lpar.cpu.proc_units.min roha_query.other_node.lpar.cpu.proc_units.curr \
                                      roha_query.other_node.cec.name                roha_query.other_node.cec.state \
                                      roha_query.other_node.cec.mem.available       roha_query.other_node.cec.cpu.available

        typeset other_node_hmc_list=
        other_node_hmc_list=$(cl_get_hmc_list -n $nodename 2>/dev/null)
        if (( $? == 0 )) && [[ -n $other_node_hmc_list ]]; then
            attrs="codpool_name:sys_mobile_mem:sys_mobile_procs"
            out_str=$($CLHMCCMD_CMD -o query -a $attrs -L ${roha_session.other_lpar})
            (( $? > 0 )) && return $RC_QUERY_ERROR
            print "$out_str" | IFS=: read roha_query.other_node.codpool.name               roha_query.other_node.codpool.mem.mobile \
                                          roha_query.other_node.codpool.cpu.mobile        
        fi
    
        for attr in roha_query.other_node.lpar.mem.min roha_query.other_node.lpar.mem.curr roha_query.other_node.codpool.mem.mobile ; do
            #=======================================================
            # Convert memory to GB.
            #=======================================================
            (( ${attr} /= 1024.00 ))
        done
    fi

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

#=============================================================================
#
# Name:        roha_query_other_node_report
#
# Description: Display all information related to other partition.
#              By "other" partition, we mean either the source partition, or
#               the target partition
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_other_node_report
{
    if [[ -n ${roha_session.other_lpar} ]] ; then
        if [[ ${roha_query.other_node.lpar.cpu.proc_mode} == "shared" ]] ; then
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| OTHER            |   Memory (GB)  |   PU(s)/VP(s)  |\n")"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| LPAR (shared)    |   %                    27.27s   |\n" ${roha_query.other_node.lpar.name})"
            roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.other_node.lpar.state}")"
            roha_session_log "$(printf "| Id               |   %                    27.27s   |\n" ${roha_query.other_node.lpar.id})"
            roha_session_log "$(printf "| Uuid             |   %                    27.27s   |\n" ${roha_query.other_node.lpar.uuid})"
            roha_session_log "$(printf "| Minimum          |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.other_node.lpar.mem.min} ${roha_query.other_node.lpar.cpu.proc_units.min} ${roha_query.other_node.lpar.cpu.procs.min})"
            roha_session_log "$(printf "| Assigned         |   %    10.4f   |   % 6.4f/%3d   |\n" ${roha_query.other_node.lpar.mem.curr} ${roha_query.other_node.lpar.cpu.proc_units.curr} ${roha_query.other_node.lpar.cpu.procs.curr})"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| MANAGED SYSTEM   |   %                    27.27s   |\n" ${roha_query.other_node.cec.name})"
            roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.other_node.cec.state}")"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| ENTERPRISE POOL  |   %                    27.27s   |\n" "${roha_query.other_node.codpool.name}")"
            roha_session_log "$(printf "| Mobile (MS)      |   %    10.4f   |      %3d       |\n" ${roha_query.other_node.codpool.mem.mobile} ${roha_query.other_node.codpool.cpu.mobile})"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        else # [[ ${roha_query.other.lpar.cpu.proc_mode} == "ded" ]]
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| OTHER            |   Memory (GB)  |     CPU(s)     |\n")"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| LPAR (dedicated) |   %                    27.27s   |\n" ${roha_query.other_node.lpar.name})"
            roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.other_node.lpar.state}")"
            roha_session_log "$(printf "| Id               |   %                    27.27s   |\n" ${roha_query.other_node.lpar.id})"
            roha_session_log "$(printf "| Uuid             |   %                    27.27s   |\n" ${roha_query.other_node.lpar.uuid})"
            roha_session_log "$(printf "| Minimum          |   %    10.4f   |      %3d       |\n" ${roha_query.other_node.lpar.mem.min} ${roha_query.other_node.lpar.cpu.procs.min})"
            roha_session_log "$(printf "| Assigned         |   %    10.4f   |      %3d       |\n" ${roha_query.other_node.lpar.mem.curr} ${roha_query.other_node.lpar.cpu.procs.curr})"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| MANAGED SYSTEM   |   %                    27.27s   |\n" ${roha_query.other_node.cec.name})"
            roha_session_log "$(printf "| State            |   %                    27.27s   |\n" "${roha_query.other_node.cec.state}")"
            roha_session_log "$(printf "| Available        |   %    10.4f   |    %  7.4f     |\n" ${roha_query.other_node.cec.mem.available} ${roha_query.other_node.cec.cpu.available})"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
            roha_session_log "$(printf "| ENTERPRISE POOL  |   %                    27.27s   |\n" "${roha_query.other_node.codpool.name}")"
            roha_session_log "$(printf "| Mobile (MS)      |   %    10.4f   |      %3d       |\n" ${roha_query.other_node.codpool.mem.mobile} ${roha_query.other_node.codpool.cpu.mobile})"
            roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        fi
    else
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| OTHER            |   Name                          |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| LPAR             |                             /   |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+\n")"
    fi
} # End of "roha_query_other_node_report()"

#=============================================================================
#
# Name:        roha_query_provisioning
#
# Description: Compute total amount of ROHA provisioning resources for a list
#              of application controllers.
#
# Inputs:      provisioning Reference name to an object to update
#              apps         Space-separated list of application controllers.
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_FAILURE
#
#=============================================================================
function roha_query_provisioning
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    typeset -n provisioning=$1
    [[ -z $provisioning ]] && return $RC_PARAM_ERROR

    # initialize
    provisioning.use_desired_nb=0
    provisioning.total_mem=0
    provisioning.total_cpu=0
    provisioning.total_pu=0
    provisioning.total_vp=0

    for app in $2 ; do
        typeset skip="" rem=""
        typeset -i use_desired=0 cpu=0 vp=0
        typeset -F4 mem=0.0 pu=0.0

        out=$($CLLSSERV_CMD -cln $app)
        (( $? > 0 )) && return $RC_FAILURE
        print "$out" | IFS=: read skip skip skip use_desired mem cpu pu vp rem

        if (( $use_desired == 1 || $mem > 0.00 || $cpu > 0.00 || $pu > 0.00 || $vp > 0.00 )) ; then
            provisioning.table_of_apps.name[$app]=$app
            provisioning.table_of_apps.use_desired[$app]=$use_desired
            provisioning.table_of_apps.mem[$app]=$mem
            provisioning.table_of_apps.pu[$app]=$pu
          if (( CONN_TYPE != 2 )); then
            provisioning.table_of_apps.cpu[$app]=$cpu
            provisioning.table_of_apps.vp[$app]=$vp
          fi

            if (( ${use_desired:-0} == 1 )) ; then
                (( provisioning.use_desired_nb++ ))
            else
                (( provisioning.total_mem += ${mem:-0.00} ))
                (( provisioning.total_pu  += ${pu:-0.00} ))
              if (( CONN_TYPE != 2 )); then
                (( provisioning.total_cpu += ${cpu:-0.00} ))
                (( provisioning.total_vp  += ${vp:-0.00} ))
              fi
            fi
        fi
    done

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

#=============================================================================
#
# Name:        roha_query_optimal_report
#
# Description: Display all ROHA provisioning information for a list of
#              application controllers.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_optimal_report
{
    if (( ${#roha_query.optimal.table_of_apps.name[@]} > 0 )) ; then
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| OPTIMAL APPS     |  Use Desired   |   Memory (GB)  |     CPU(s)     |   PU(s)/VP(s)  |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        for app in ${roha_query.optimal.table_of_apps.name[@]} ; do
            case ${roha_query.optimal.table_of_apps.use_desired[$app]} in
            -1) roha_session_log "$(printf "| %-        16.16s |                |                |                |                |\n" ${roha_query.optimal.table_of_apps.name[$app]})"
                ;;
             0) roha_session_log "$(printf "| %-        16.16s |      %3d       |   %    10.4f   |      %3d       |   % 6.4f/%3d   |\n" ${roha_query.optimal.table_of_apps.name[$app]} ${roha_query.optimal.table_of_apps.use_desired[$app]} ${roha_query.optimal.table_of_apps.mem[$app]} ${roha_query.optimal.table_of_apps.cpu[$app]} ${roha_query.optimal.table_of_apps.pu[$app]} ${roha_query.optimal.table_of_apps.vp[$app]})"
                ;;
             1) roha_session_log "$(printf "| %-        16.16s |      %3d       |  (%    10.4f)  |     (%3d)      |  (% 6.4f/%3d)  |\n" ${roha_query.optimal.table_of_apps.name[$app]} ${roha_query.optimal.table_of_apps.use_desired[$app]} ${roha_query.optimal.table_of_apps.mem[$app]} ${roha_query.optimal.table_of_apps.cpu[$app]} ${roha_query.optimal.table_of_apps.pu[$app]} ${roha_query.optimal.table_of_apps.vp[$app]})"
                ;;
            esac
        done
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Total            |      %3d       |   %    10.4f   |      %3d       |   % 6.4f/%3d   |\n" ${roha_query.optimal.use_desired_nb} ${roha_query.optimal.total_mem} ${roha_query.optimal.total_cpu} ${roha_query.optimal.total_pu} ${roha_query.optimal.total_vp})"
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n\n")"
    fi
} # End of "roha_query_optimal_report()"

#=============================================================================
#
# Name:        roha_query_running_report
#
# Description: Display all ROHA provisioning information for a list of
#              application controllers.
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     None
#
#=============================================================================
function roha_query_running_report
{
    if (( ${#roha_query.running.table_of_apps.name[@]} > 0 )) ; then
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| RUNNING APPS     |  Use Desired   |   Memory (GB)  |     CPU(s)     |   PU(s)/VP(s)  |\n")"
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        for app in ${roha_query.running.table_of_apps.name[@]} ; do
            case ${roha_query.running.table_of_apps.use_desired[$app]} in
            -1) roha_session_log "$(printf "| %-        16.16s |                |                |                |                |\n" ${roha_query.running.table_of_apps.name[$app]})"
                ;;
             0) roha_session_log "$(printf "| %-        16.16s |      %3d       |   %    10.4f   |      %3d       |   % 6.4f/%3d   |\n" ${roha_query.running.table_of_apps.name[$app]} ${roha_query.running.table_of_apps.use_desired[$app]} ${roha_query.running.table_of_apps.mem[$app]} ${roha_query.running.table_of_apps.cpu[$app]} ${roha_query.running.table_of_apps.pu[$app]} ${roha_query.running.table_of_apps.vp[$app]})"
                ;;
             1) roha_session_log "$(printf "| %-        16.16s |      %3d       |  (%    10.4f)  |     (%3d)      |  (% 6.4f/%3d)  |\n" ${roha_query.running.table_of_apps.name[$app]} ${roha_query.running.table_of_apps.use_desired[$app]} ${roha_query.running.table_of_apps.mem[$app]} ${roha_query.running.table_of_apps.cpu[$app]} ${roha_query.running.table_of_apps.pu[$app]} ${roha_query.running.table_of_apps.vp[$app]})"
                ;;
            esac
        done
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n")"
        roha_session_log "$(printf "| Total            |      %3d       |   %    10.4f   |      %3d       |   % 6.4f/%3d   |\n" ${roha_query.running.use_desired_nb} ${roha_query.running.total_mem} ${roha_query.running.total_cpu} ${roha_query.running.total_pu} ${roha_query.running.total_vp})"
        roha_session_log "$(printf "+------------------+----------------+----------------+----------------+----------------+\n\n")"
    fi
} # End of "roha_query_running_report()"


#=============================================================================
#
# Name:        roha_query_sys_lpar_names
#
# Description: Query nb of sys_lpar_names on current CEC
#
# Inputs:      None
#
# Outputs:     None
#
# Returns:     RC_SUCCESS
#              RC_QUERY_ERROR
#
#=============================================================================
function roha_query_sys_lpar_names
{
    [[ $VERBOSE_LOGGING == high ]] && set -x

    typeset attrs="sys_lpar_names"

    if [[ -n ${roha_session.preferred_nova_list} ]]; then
        out_str=$(clnovacmd -o query -a $attrs)
    else
        out_str=$($CLHMCCMD_CMD -o query -a $attrs)
    fi
    (( $? > 0 )) && return $RC_QUERY_ERROR

    print "$out_str"
    return $RC_SUCCESS
} # End of "roha_query_sys_lpar_names()"


#=============================================================================
#
# Name:        roha_query_update_lpar_cur_if_async_in_progress
#
# Description: Update LPAR resource current values with future values from ODM.
#
# Inputs:      None
#
# Outputs:     None
#
# Globals:     roha_query
#              roha_compute
#
# Returns:     None
#
#=============================================================================
function roha_query_update_lpar_cur_if_async_in_progress
{
    [[ $VERBOSE_LOGGING == high ]] && set -x
    
    typeset -F4 expected_cur_mem=0.00
    typeset -i  expected_cur_procs=0    
    typeset -F4 expected_cur_proc_units=0.00

    #====================================================================
    # Check if Async release is in progress
    #====================================================================
    rel_apps=$(ODMDIR=/etc/es/objrepos clodmget -q "key=REL_APPS" -nf value HACMPdynresop 2>/dev/null)

    if (( $? == 0 )) && [[ -n $rel_apps ]] ; then    
        expected_cur_mem=$(ODMDIR=/etc/es/objrepos clodmget -q "key=ACTUAL_LPAR_CUR_MEM" -nf value HACMPdynresop 2>/dev/null) 
        if (( $? == 0 && $expected_cur_mem > 0.00 )) ; then
            if (( $expected_cur_mem < ${roha_query.lpar.mem.curr} )) ; then
                (( roha_query.lpar.mem.curr = $expected_cur_mem ))
            fi    
        fi

        expected_cur_procs=$(ODMDIR=/etc/es/objrepos clodmget -q "key=ACTUAL_LPAR_CUR_PROCS" -nf value HACMPdynresop 2>/dev/null) 
        if (( $? == 0 && $expected_cur_procs > 0.00 )) ; then
            if (( $expected_cur_procs < ${roha_query.lpar.cpu.procs.curr} )) ; then
                (( roha_query.lpar.cpu.procs.curr = $expected_cur_procs ))
            fi    
        fi

        if [[ ${roha_query.lpar.cpu.proc_mode} == "shared" ]] ; then
            expected_cur_proc_units=$(ODMDIR=/etc/es/objrepos clodmget -q "key=ACTUAL_LPAR_CUR_PROC_UNITS" -nf value HACMPdynresop 2>/dev/null) 
            if (( $? == 0 && $expected_cur_proc_units > 0.00 )) ; then
                if (( $expected_cur_proc_units < ${roha_query.lpar.cpu.proc_units.curr} )) ; then
                    (( roha_query.lpar.cpu.proc_units.curr = $expected_cur_proc_units ))
                fi    
            fi
        fi
    fi   
} # End of "roha_query_update_lpar_cur_if_async_in_progress()"
