# Copyright 2008-2014 Cumulus Systems Incorporated.
# All Rights Reserved.

# This file contain utility function specific to Perl based Probe configuration data.

# Name of the Package.
package commonProbeConfUtil;

# It is used for strict compilation.
use strict;

# It helps in debugging.
use warnings;

# This is required for logging information.
use commonLogModule;

# This stores an instance of commonProbeConfUtil class.
my $commProbeConfUtil = undef;

# --------------------------------------------------------------------------------------------------------------------------------------------------
# Sub-routines
# --------------------------------------------------------------------------------------------------------------------------------------------------
# Constructor of class commonProbeConfUtil class.
#
# @param :
#   $_[0] - [In] Class name.
#   $_[1] - [In] Base folder for logging.
#   $_[2] - [In] Probe Type like NetApp.
#   $_[3] - [In] Probe Id "192.168.20.100".
#   $_[4] - [In] Instance of commonLogModule.
#
# @affected global variables :
#   None
#
# @return :
#   Instance of commonProbeConfUtil class.
sub getInstance {
    # This variable stores package name.
    my $class = $_[0];

    # This variable stores base folder in which we generate logs.
    my $baseFolder = $_[1];

    # This variable stores probe type.
    my $probeType = $_[2];

    # This variable stores probe id.
    my $probeId = $_[3];

    # Get commonLogModule instance.
    my $logObject = $_[4];

    # Create class instance.
    $commProbeConfUtil = bless {
        probeType => $probeType,
        probeId => $probeId,
        logObj => $logObject,
        baseFolder => $baseFolder,
    }, shift unless $commProbeConfUtil;

    return $commProbeConfUtil;
}

# This function is used to return Configuration meta-data information for a Probe.
#
# @param :
#   $_[0] - [In] Object Reference.
#   $_[1] - [In] Site name.
#   $_[2] - [In] Data start time.
#   $_[3] - [In] Probe version.
#   $_[4] - [Out] Meta-data string.
#   $_[5] - [In] HashMap containing extra meta-data information. It is a optional parameter.
#
# @affected global variables :
#   None
#
# @return :
#   None
sub getFormattedProbeConfMetadata {
    my $metaData = "%meta\n";
    $metaData = $metaData."probe_id:$_[1]"."_"."$_[0]->{probeId}\n";
    $metaData = $metaData."probe_type:$_[0]->{probeType}\n";
    $metaData = $metaData."probe_site:$_[1]\n";
    $metaData = $metaData."probe_server:$_[0]->{probeId}\n";
    $metaData = $metaData."probe_version:$_[3]\n";

    # Check whether caller has passed optional meta-data information or not.
    my %optionalMetaDataHash = defined(%{$_[5]}) ? %{$_[5]} : ();

    # If user has passed optional meta-data information then add it in the final meta-data.
    if (%optionalMetaDataHash) {
        while ((my $key, my $value) = each %optionalMetaDataHash) {
            $metaData = $metaData.$key.":".$value."\n";
        }
    }

    $metaData = $metaData."%meta\n";
    $metaData = $metaData."t:$_[2]\n";

    $_[4] = $metaData;
}

# The file must return true ("1") as the last statement to indicate successful execution of any initialization code. So it's customary to end such a
# file with 1 unless we are sure that we will return true otherwise. But it's better just to put the 1;, in case we add more statements.
1;