<commit_before>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "InformationManagerDriver.h"
#include "NebulaLog.h"
#include "Util.h"
#include "VirtualMachineManagerDriver.h"
#include <sstream>


/* ************************************************************************** */
/* Driver ASCII Protocol Implementation                                       */
/* ************************************************************************** */

void InformationManagerDriver::monitor(int           oid,
                                       const string& host,
                                       bool          update) const
{
    ostringstream os;

    os << "MONITOR " << oid << " " << host << " " << update << endl;

    write(os);
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void InformationManagerDriver::protocol(
    string&     message)
{
    istringstream   is(message);
    //stores the action name
    string          action;
    //stores the action result
    string          result;
    //stores the action id of the associated HOST
    int             id;

    ostringstream   ess;

    Host *          host;

    set<int>        vm_ids;

    // Parse the driver message

    if ( is.good() )
    {
        is >> action >> ws;
    }
    else
    {
        goto error_parse;
    }

    if ( is.good() )
    {
        is >> result >> ws;
    }
    else
    {
        goto error_parse;
    }

    if ( is.good() )
    {
        is >> id >> ws;
    }
    else
    {
        goto error_parse;
    }

    // -----------------------
    // Protocol implementation
    // -----------------------

    if ( action == "MONITOR" )
    {
        host = hpool->get(id,true);

        if ( host == 0 )
        {
            goto error_host;
        }

        vm_ids = host->get_running_vms();

        if (result != "SUCCESS")
        {
            goto error_driver_info;
        }

        int     rc;
        string  hinfo64;
        string* hinfo;

        ostringstream oss;

        int     vmid;
        char *  error_msg;
        string  monitor_str;

        VectorAttribute*                vatt;
        vector<Attribute*>              vm_att;
        vector<Attribute*>::iterator    it;

        getline (is, hinfo64);

        hinfo = one_util::base64_decode(hinfo64);

        oss << "Host " << id << " successfully monitored.";
        NebulaLog::log("InM", Log::DEBUG, oss);

        rc = host->update_info(*hinfo);

        if (rc != 0)
        {
            ess << "Error parsing host information: " << *hinfo;
            delete hinfo;

            goto  error_common_info;
        }

        // TODO The hinfo string is parsed again because HostTemplate has
        // replace_mode set to true, but we expect several VM vector attributes
        Template* tmpl = new Template();
        tmpl->parse(*hinfo, &error_msg);

        tmpl->remove("VM", vm_att);

        delete hinfo;

        host->remove_template_attribute("VM");

        host->touch(true);

        hpool->update(host);
        hpool->update_monitoring(host);

        host->unlock();

        for (it=vm_att.begin(); it != vm_att.end(); it++)
        {
            vatt = dynamic_cast<VectorAttribute*>(*it);

            if (vatt == 0)
            {
                delete *it;
                continue;
            }

            rc = vatt->vector_value("ID", vmid);

            if (rc == 0)
            {
                if (vm_ids.erase(vmid) != 1)
                {
                    // TODO: This VM shoulnd't be running on this host
                }

                monitor_str = vatt->vector_value("POLL");

                VirtualMachineManagerDriver::process_poll(vmid, monitor_str);
            }
            else
            {
                // TODO: unknown VM found running on this host
            }

            delete *it;
        }

        // TODO Some drivers do not return info about the VMs, but we should
        // have a better way to check that. This will be true when no VMs are
        // reported
        if (!vm_att.empty())
        {
            for (set<int>::iterator it = vm_ids.begin(); it != vm_ids.end(); it++)
            {
                // This VM should be running on this host, but it was not reported

                VirtualMachineManagerDriver::process_poll(*it, "STATE=d");
            }
        }
    }
    else if (action == "LOG")
    {
        string info;
<commit_msg>Feature #1739: Do not update poll info for VMs that should not be in the reported host<commit_after>/* -------------------------------------------------------------------------- */
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs        */
/*                                                                            */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
/* not use this file except in compliance with the License. You may obtain    */
/* a copy of the License at                                                   */
/*                                                                            */
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
/*                                                                            */
/* Unless required by applicable law or agreed to in writing, software        */
/* distributed under the License is distributed on an "AS IS" BASIS,          */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
/* See the License for the specific language governing permissions and        */
/* limitations under the License.                                             */
/* -------------------------------------------------------------------------- */

#include "InformationManagerDriver.h"
#include "NebulaLog.h"
#include "Util.h"
#include "VirtualMachineManagerDriver.h"
#include <sstream>


/* ************************************************************************** */
/* Driver ASCII Protocol Implementation                                       */
/* ************************************************************************** */

void InformationManagerDriver::monitor(int           oid,
                                       const string& host,
                                       bool          update) const
{
    ostringstream os;

    os << "MONITOR " << oid << " " << host << " " << update << endl;

    write(os);
}

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

void InformationManagerDriver::protocol(
    string&     message)
{
    istringstream   is(message);
    //stores the action name
    string          action;
    //stores the action result
    string          result;
    //stores the action id of the associated HOST
    int             id;

    ostringstream   ess;

    Host *          host;

    set<int>        vm_ids;

    // Parse the driver message

    if ( is.good() )
    {
        is >> action >> ws;
    }
    else
    {
        goto error_parse;
    }

    if ( is.good() )
    {
        is >> result >> ws;
    }
    else
    {
        goto error_parse;
    }

    if ( is.good() )
    {
        is >> id >> ws;
    }
    else
    {
        goto error_parse;
    }

    // -----------------------
    // Protocol implementation
    // -----------------------

    if ( action == "MONITOR" )
    {
        host = hpool->get(id,true);

        if ( host == 0 )
        {
            goto error_host;
        }

        vm_ids = host->get_running_vms();

        if (result != "SUCCESS")
        {
            goto error_driver_info;
        }

        int     rc;
        string  hinfo64;
        string* hinfo;

        ostringstream oss;

        int     vmid;
        char *  error_msg;
        string  monitor_str;

        VectorAttribute*                vatt;
        vector<Attribute*>              vm_att;
        vector<Attribute*>::iterator    it;

        getline (is, hinfo64);

        hinfo = one_util::base64_decode(hinfo64);

        oss << "Host " << id << " successfully monitored.";
        NebulaLog::log("InM", Log::DEBUG, oss);

        rc = host->update_info(*hinfo);

        if (rc != 0)
        {
            ess << "Error parsing host information: " << *hinfo;
            delete hinfo;

            goto  error_common_info;
        }

        // TODO The hinfo string is parsed again because HostTemplate has
        // replace_mode set to true, but we expect several VM vector attributes
        Template* tmpl = new Template();
        tmpl->parse(*hinfo, &error_msg);

        tmpl->remove("VM", vm_att);

        delete hinfo;

        host->remove_template_attribute("VM");

        host->touch(true);

        hpool->update(host);
        hpool->update_monitoring(host);

        host->unlock();

        for (it=vm_att.begin(); it != vm_att.end(); it++)
        {
            vatt = dynamic_cast<VectorAttribute*>(*it);

            if (vatt == 0)
            {
                delete *it;
                continue;
            }

            rc = vatt->vector_value("ID", vmid);

            if (rc == 0)
            {
                if (vm_ids.erase(vmid) == 1)
                {
                    monitor_str = vatt->vector_value("POLL");

                    VirtualMachineManagerDriver::process_poll(vmid, monitor_str);
                }
                else
                {
                    // TODO: This VM shoulnd't be running on this host
                }
            }
            else
            {
                // TODO: unknown VM found running on this host
            }

            delete *it;
        }

        // TODO Some drivers do not return info about the VMs, but we should
        // have a better way to check that. This will be true when no VMs are
        // reported
        if (!vm_att.empty())
        {
            for (set<int>::iterator it = vm_ids.begin(); it != vm_ids.end(); it++)
            {
                // This VM should be running on this host, but it was not reported

                VirtualMachineManagerDriver::process_poll(*it, "STATE=d");
            }
        }
    }
    else if (action == "LOG")
    {
        string info;

{'fraction_non_alphanumeric': 0.18084277926797612, 'fraction_numerical': 0.005018603443800294, 'mean_word_length': 1.1344413665743305, 'pattern_counts': {'":': 0, '<': 63, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6182406', 'n_tokens_mistral': 2902, 'n_tokens_neox': 2707, 'n_words': 793}