1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-01-03 01:17:41 +03:00

Feature #407: Host object uses new DB schema, storing the contents as XML

This commit is contained in:
Carlos Martín 2011-02-24 18:12:26 +01:00
parent 829d34c400
commit ebf66e0fda
22 changed files with 370 additions and 627 deletions

View File

@ -49,19 +49,27 @@ public:
*/
friend ostream& operator<<(ostream& os, Host& h);
/**
* Function to print the Host object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Host object into a string in plain text
* @param str the resulting string
* @return a reference to the generated string
*/
string& to_str(string& str) const;
/**
* Function to print the Host object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml(string& xml) const;
/**
* Function to print the Host object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
virtual string& to_xml(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
virtual int from_xml(const string &xml_str);
/**
* Get the Host unique identifier HID, that matches the OID of the object
@ -438,25 +446,14 @@ private:
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Host object (Host::select)
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names);
/**
* Bootstraps the database table(s) associated to the Host
*/
static void bootstrap(SqlDB * db)
{
ostringstream oss_host(Host::db_bootstrap);
ostringstream oss_share(HostShare::db_bootstrap);
db->exec(oss_host);
db->exec(oss_share);
};
protected:
@ -498,11 +495,13 @@ protected:
static const char * table;
/**
* Reads the Host (identified with its OID=HID) from the database.
* @param db pointer to the db
* @return 0 on success
* Table name
* @return the object's table name
*/
virtual int select(SqlDB *db);
virtual const char * table_name()
{
return table;
};
/**
* Writes the Host and its associated HostShares in the database.
@ -517,23 +516,6 @@ protected:
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops host from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output a Host object in to an stream in XML format
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int dump(ostringstream& oss, int num, char **values, char **names);
};
#endif /*HOST_H_*/

View File

@ -17,8 +17,7 @@
#ifndef HOST_SHARE_H_
#define HOST_SHARE_H_
#include "SqlDB.h"
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include <time.h>
using namespace std;
@ -29,12 +28,11 @@ using namespace std;
/**
* The HostShare class. It represents a logical partition of a host...
*/
class HostShare : public ObjectSQL
class HostShare : public ObjectXML
{
public:
HostShare(
int _hsid=-1,
int _max_disk=0,
int _max_mem=0,
int _max_cpu=0);
@ -108,9 +106,15 @@ public:
*/
string& to_xml(string& xml) const;
private:
/**
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
int from_xml_node(const xmlNodePtr node);
int hsid; /**< HostShare identifier */
private:
int disk_usage; /**< Disk allocated to VMs (in Mb). */
int mem_usage; /**< Memory allocated to VMs (in Mb) */
@ -137,94 +141,6 @@ private:
friend class Host;
friend class HostPool;
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
enum ColNames
{
HID = 0,
DISK_USAGE = 1,
MEM_USAGE = 2,
CPU_USAGE = 3,
MAX_DISK = 4,
MAX_MEMORY = 5,
MAX_CPU = 6,
FREE_DISK = 7,
FREE_MEMORY = 8,
FREE_CPU = 9,
USED_DISK = 10,
USED_MEMORY = 11,
USED_CPU = 12,
RUNNING_VMS = 13,
LIMIT = 14
};
static const char * table;
static const char * db_names;
static const char * db_bootstrap;
// ----------------------------------------
// Database methods
// ----------------------------------------
/**
* Reads the HostShare (identified with its HSID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select(SqlDB * db);
/**
* Writes the HostShare in the database.
* @param db pointer to the db
* @return 0 on success
*/
int insert(SqlDB * db, string& error_str);
/**
* Writes/updates the HostShare data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB * db);
/**
* Drops hostshare from the database
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db);
/**
* Execute an INSERT or REPLACE Sql query.
* @param db The SQL DB
* @param replace Execute an INSERT or a REPLACE
* @return 0 one success
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a HostShare object (HostShare::select)
* @param num the number of columns read from the DB
* @para names the column names
* @para vaues the column values
* @return 0 on success
*/
int select_cb(void * nil, int num, char **values, char **names);
/**
* Function to unmarshall a HostShare object in to an output stream in XML
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int dump(ostringstream& oss, int num, char **values, char **names);
};

View File

@ -74,7 +74,14 @@ public:
* XML resources are freed
* @param xml_doc the new xml document
*/
int update(const string &xml_doc);
int update_from_str(const string &xml_doc);
/**
* Updates the object representation with a new XML document. Previous
* XML resources are freed
* @param xml_doc the new xml document
*/
int update_from_node(const xmlNodePtr node);
// ---------------------------------------------------------
// Lex & bison parser for requirements and rank expressions

View File

@ -18,7 +18,9 @@
#define POOL_OBJECT_SQL_H_
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include <pthread.h>
#include <string.h>
using namespace std;
@ -31,11 +33,11 @@ using namespace std;
* is called.
*/
class PoolObjectSQL : public ObjectSQL
class PoolObjectSQL : public ObjectSQL, public ObjectXML
{
public:
PoolObjectSQL(int id=-1):oid(id),valid(true)
PoolObjectSQL(int id=-1):oid(id),valid(true)
{
pthread_mutex_init(&mutex,0);
};
@ -68,7 +70,7 @@ public:
void set_valid(const bool _valid)
{
valid = _valid;
}
};
/**
* Function to lock the object
@ -86,18 +88,144 @@ public:
pthread_mutex_unlock(&mutex);
};
/**
* Function to print the object into a string in XML format
* @param xml the resulting XML string
* @return a reference to the generated string
*/
// virtual string& to_xml(string& xml) const = 0;
// TODO: change to pure virtual when all child classes implement it
string& to_xml(string& xml) const
{
return xml;
};
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
// virtual int from_xml(const string &xml_str) = 0;
// TODO: change to pure virtual when all child classes implement it
virtual int from_xml(const string &xml_str)
{
return 0;
};
protected:
/**
* The object unique ID
* Callback function to unmarshall a PoolObjectSQL
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
int select_cb(void *nil, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
from_xml( values[0] );
return 0;
};
/**
* Reads the PoolObjectSQL (identified by its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table_name() << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
};
/**
* Drops object from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table_name() << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
};
/**
* Function to output a pool object into a stream in XML format
* @param oss the output stream
* @param num the number of columns read from the DB
* @param names the column names
* @param vaues the column values
* @return 0 on success
*/
static int dump(ostringstream& oss, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
oss << values[0];
return 0;
};
/**
* The object's unique ID
*/
int oid;
/**
* The contents ob this object are valid
* The contents of this object are valid
*/
bool valid;
/**
* Table name
* @return the object's table name
*/
// virtual const char * table_name() = 0;
// TODO: change to pure virtual when all child classes implement it
virtual const char * table_name()
{
return "";
};
private:
/**

View File

@ -176,6 +176,14 @@ public:
*/
int from_xml(const string &xml_str);
/**
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
int from_xml_node(const xmlNodePtr node);
protected:
/**
* The template attributes
@ -198,6 +206,12 @@ protected:
*/
Attribute* vector_xml_att(const xmlNode * node);
/**
* Builds the template attribute from the node
* @param root_element The xml element to build the template from.
*/
void rebuild_attributes(const xmlNode * root_element);
private:
bool replace_mode;

View File

@ -166,14 +166,14 @@ public:
{
// The pool is empty
// Non existing oid
obj = pool->get(13, true);
obj = pool->get(13, false);
CPPUNIT_ASSERT( obj == 0 );
// Allocate an object
allocate(0);
// Ask again for a non-existing oid
obj = pool->get(213, true);
obj = pool->get(213, false);
CPPUNIT_ASSERT( obj == 0 );
}

View File

@ -52,92 +52,11 @@ Host::~Host(){}
const char * Host::table = "host_pool";
const char * Host::db_names = "oid,host_name,state,im_mad,vm_mad,"
"tm_mad,last_mon_time, cluster, template";
const char * Host::db_names = "oid, name, body, state, last_mon_time";
const char * Host::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_pool ("
"oid INTEGER PRIMARY KEY,host_name VARCHAR(256), state INTEGER,"
"im_mad VARCHAR(128),vm_mad VARCHAR(128),tm_mad VARCHAR(128),"
"last_mon_time INTEGER, cluster VARCHAR(128), template TEXT, "
"UNIQUE(host_name))";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::select_cb(void * nil, int num, char **values, char ** names)
{
if ((!values[OID]) ||
(!values[HOST_NAME]) ||
(!values[STATE]) ||
(!values[IM_MAD]) ||
(!values[VM_MAD]) ||
(!values[TM_MAD]) ||
(!values[LAST_MON_TIME]) ||
(!values[CLUSTER]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
hostname = values[HOST_NAME];
state = static_cast<HostState>(atoi(values[STATE]));
im_mad_name = values[IM_MAD];
vmm_mad_name = values[VM_MAD];
tm_mad_name = values[TM_MAD];
last_monitored = static_cast<time_t>(atoi(values[LAST_MON_TIME]));
cluster = values[CLUSTER];
host_template.from_xml(values[TEMPLATE]);
host_share.hsid = oid;
return 0;
}
/* ------------------------------------------------------------------------ */
int Host::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&Host::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
if ( rc != 0 )
{
return -1;
}
// Select the host shares from the DB
rc = host_share.select(db);
if ( rc != 0 )
{
return rc;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, state INTEGER, "
"last_mon_time INTEGER, UNIQUE(name))";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -145,34 +64,15 @@ int Host::select(SqlDB *db)
int Host::insert(SqlDB *db, string& error_str)
{
int rc;
map<int,HostShare *>::iterator iter;
// Set up the share ID, to insert it
if ( host_share.hsid == -1 )
{
host_share.hsid = oid;
}
// Update the HostShare
rc = host_share.insert(db, error_str);
if ( rc != 0 )
{
return rc;
}
//Insert the Host
rc = insert_replace(db, false);
if ( rc != 0 )
{
error_str = "Error inserting Host in DB.";
host_share.drop(db);
return rc;
}
return 0;
return rc;
}
/* ------------------------------------------------------------------------ */
@ -182,24 +82,9 @@ int Host::update(SqlDB *db)
{
int rc;
// Update the HostShare
rc = host_share.update(db);
if ( rc != 0 )
{
return rc;
}
rc = insert_replace(db, true);
if ( rc != 0 )
{
return rc;
}
return 0;
return rc;
}
/* ------------------------------------------------------------------------ */
@ -210,14 +95,10 @@ int Host::insert_replace(SqlDB *db, bool replace)
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_hostname;
char * sql_im_mad_name;
char * sql_tm_mad_name;
char * sql_vmm_mad_name;
char * sql_cluster;
char * sql_template;
char * sql_xml;
// Update the Host
@ -228,40 +109,12 @@ int Host::insert_replace(SqlDB *db, bool replace)
goto error_hostname;
}
sql_im_mad_name = db->escape_str(im_mad_name.c_str());
to_xml(xml_body);
sql_xml = db->escape_str(xml_body.c_str());
if ( sql_im_mad_name == 0 )
if ( sql_xml == 0 )
{
goto error_im;
}
sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
}
sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
sql_cluster = db->escape_str(cluster.c_str());
if ( sql_cluster == 0 )
{
goto error_cluster;
}
host_template.to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
@ -278,34 +131,18 @@ int Host::insert_replace(SqlDB *db, bool replace)
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_hostname << "',"
<< "'" << sql_xml << "',"
<< state << ","
<< "'" << sql_im_mad_name << "',"
<< "'" << sql_vmm_mad_name << "',"
<< "'" << sql_tm_mad_name << "',"
<< last_monitored << ","
<< "'" << sql_cluster << "',"
<< "'" << sql_template << "')";
<< last_monitored << ")";
rc = db->exec(oss);
db->free_str(sql_hostname);
db->free_str(sql_im_mad_name);
db->free_str(sql_tm_mad_name);
db->free_str(sql_vmm_mad_name);
db->free_str(sql_cluster);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_cluster);
error_cluster:
db->free_str(sql_vmm_mad_name);
error_vmm:
db->free_str(sql_tm_mad_name);
error_tm:
db->free_str(sql_im_mad_name);
error_im:
error_body:
db->free_str(sql_hostname);
error_hostname:
return -1;
@ -314,66 +151,6 @@ error_hostname:
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[HOST_NAME]) ||
(!values[STATE]) ||
(!values[IM_MAD]) ||
(!values[VM_MAD]) ||
(!values[TM_MAD]) ||
(!values[LAST_MON_TIME]) ||
(!values[CLUSTER]) ||
(!values[TEMPLATE]) ||
(num != LIMIT + HostShare::LIMIT ))
{
return -1;
}
oss <<
"<HOST>" <<
"<ID>" << values[OID] <<"</ID>" <<
"<NAME>" << values[HOST_NAME] <<"</NAME>" <<
"<STATE>" << values[STATE] <<"</STATE>" <<
"<IM_MAD>" << values[IM_MAD] <<"</IM_MAD>" <<
"<VM_MAD>" << values[VM_MAD] <<"</VM_MAD>" <<
"<TM_MAD>" << values[TM_MAD] <<"</TM_MAD>" <<
"<LAST_MON_TIME>"<< values[LAST_MON_TIME]<<"</LAST_MON_TIME>"<<
"<CLUSTER>" << values[CLUSTER] <<"</CLUSTER>" <<
values[TEMPLATE];
HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT);
oss << "</HOST>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::drop(SqlDB * db)
{
ostringstream oss;
int rc;
host_share.drop(db);
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::update_info(string &parse_str)
{
char * error_msg;
@ -445,6 +222,43 @@ string& Host::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Host::from_xml(const string& xml)
{
vector<xmlNodePtr> content;
// Initialize the internal XML object
update_from_str(xml);
oid = atoi(((*this)["/HOST/ID"] )[0].c_str() );
hostname = ((*this)["/HOST/NAME"])[0];
state = static_cast<HostState>( atoi(((*this)["/HOST/STATE"])[0].c_str()) );
// TODO: create an ObjectXML method to allow this syntax:
// im_mad_name = xpath("/HOST/IM_MAD", "im_default");
im_mad_name = ((*this)["/HOST/IM_MAD"])[0];
vmm_mad_name = ((*this)["/HOST/VM_MAD"])[0];
tm_mad_name = ((*this)["/HOST/TM_MAD"])[0];
last_monitored = static_cast<time_t>( atoi(((*this)["/HOST/LAST_MON_TIME"] )[0].c_str() ) );
cluster = ((*this)["/HOST/CLUSTER"])[0];
ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
host_share.from_xml_node( content[0] );
content.clear();
ObjectXML::get_nodes("/HOST/TEMPLATE", content);
host_template.from_xml_node( content[0] );
// TODO: check for errors (missing mandatory elements)
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Host::to_str(string& str) const
{
string template_str;

View File

@ -265,9 +265,7 @@ int HostPool::dump(ostringstream& oss, const string& where)
set_callback(static_cast<Callbackable::Callback>(&HostPool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << Host::db_names << " , " << HostShare::db_names
<< " FROM " << Host::table << " JOIN " << HostShare::table
<< " ON " << Host::table << ".oid = " << HostShare::table << ".hid";
cmd << "SELECT body FROM " << Host::table;
if ( !where.empty() )
{

View File

@ -28,12 +28,9 @@
/* ************************************************************************ */
HostShare::HostShare(
int _hsid,
int _max_disk,
int _max_mem,
int _max_cpu):
ObjectSQL(),
hsid(_hsid),
disk_usage(0),
mem_usage(0),
cpu_usage(0),
@ -50,225 +47,6 @@ HostShare::HostShare(
{
}
/* ************************************************************************ */
/* HostShare :: Database Access Functions */
/* ************************************************************************ */
const char * HostShare::table = "host_shares";
const char * HostShare::db_names = "hid,"
"disk_usage, mem_usage, cpu_usage,"
"max_disk, max_mem, max_cpu,"
"free_disk, free_mem, free_cpu,"
"used_disk, used_mem, used_cpu,"
"running_vms";
const char * HostShare::db_bootstrap = "CREATE TABLE IF NOT EXISTS host_shares("
"hid INTEGER PRIMARY KEY,"
"disk_usage INTEGER, mem_usage INTEGER, cpu_usage INTEGER,"
"max_disk INTEGER, max_mem INTEGER, max_cpu INTEGER,"
"free_disk INTEGER, free_mem INTEGER, free_cpu INTEGER,"
"used_disk INTEGER, used_mem INTEGER, used_cpu INTEGER,"
"running_vms INTEGER)";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::select_cb(void * nil, int num, char **values, char **names)
{
if ((!values[HID]) ||
(!values[DISK_USAGE]) ||
(!values[MEM_USAGE]) ||
(!values[CPU_USAGE]) ||
(!values[MAX_DISK]) ||
(!values[MAX_MEMORY]) ||
(!values[MAX_CPU]) ||
(!values[FREE_DISK]) ||
(!values[FREE_MEMORY]) ||
(!values[FREE_CPU]) ||
(!values[USED_DISK]) ||
(!values[USED_MEMORY]) ||
(!values[USED_CPU]) ||
(!values[RUNNING_VMS]) ||
(num != LIMIT ))
{
return -1;
}
hsid = atoi(values[HID]);
disk_usage = atoi(values[DISK_USAGE]);
mem_usage = atoi(values[MEM_USAGE]);
cpu_usage = atoi(values[CPU_USAGE]);
max_disk = atoi(values[MAX_DISK]);
max_mem = atoi(values[MAX_MEMORY]);
max_cpu = atoi(values[MAX_CPU]);
free_disk = atoi(values[FREE_DISK]);
free_mem = atoi(values[FREE_MEMORY]);
free_cpu = atoi(values[FREE_CPU]);
used_disk = atoi(values[USED_DISK]);
used_mem = atoi(values[USED_MEMORY]);
used_cpu = atoi(values[USED_CPU]);
running_vms = atoi(values[RUNNING_VMS]);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::dump(ostringstream& oss,
int num,
char ** values,
char ** names)
{
if ((!values[HID]) ||
(!values[DISK_USAGE]) ||
(!values[MEM_USAGE]) ||
(!values[CPU_USAGE]) ||
(!values[MAX_DISK]) ||
(!values[MAX_MEMORY]) ||
(!values[MAX_CPU]) ||
(!values[FREE_DISK]) ||
(!values[FREE_MEMORY]) ||
(!values[FREE_CPU]) ||
(!values[USED_DISK]) ||
(!values[USED_MEMORY]) ||
(!values[USED_CPU]) ||
(!values[RUNNING_VMS]) ||
(num != LIMIT))
{
return -1;
}
oss <<
"<HOST_SHARE>" <<
"<HID>" << values[HID] << "</HID>" <<
"<DISK_USAGE>"<< values[DISK_USAGE] << "</DISK_USAGE>"<<
"<MEM_USAGE>" << values[MEM_USAGE] << "</MEM_USAGE>" <<
"<CPU_USAGE>" << values[CPU_USAGE] << "</CPU_USAGE>" <<
"<MAX_DISK>" << values[MAX_DISK] << "</MAX_DISK>" <<
"<MAX_MEM>" << values[MAX_MEMORY] << "</MAX_MEM>" <<
"<MAX_CPU>" << values[MAX_CPU] << "</MAX_CPU>" <<
"<FREE_DISK>" << values[FREE_DISK] << "</FREE_DISK>" <<
"<FREE_MEM>" << values[FREE_MEMORY] << "</FREE_MEM>" <<
"<FREE_CPU>" << values[FREE_CPU] << "</FREE_CPU>" <<
"<USED_DISK>" << values[USED_DISK] << "</USED_DISK>" <<
"<USED_MEM>" << values[USED_MEMORY] << "</USED_MEM>" <<
"<USED_CPU>" << values[USED_CPU] << "</USED_CPU>" <<
"<RUNNING_VMS>"<<values[RUNNING_VMS] << "</RUNNING_VMS>"<<
"</HOST_SHARE>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::select(SqlDB * db)
{
ostringstream oss;
int rc;
int bhsid;
set_callback(static_cast<Callbackable::Callback>(&HostShare::select_cb));
oss << "SELECT "<< db_names << " FROM " << table << " WHERE hid = " << hsid;
bhsid = hsid;
hsid = -1;
rc = db->exec(oss,this);
unset_callback();
if (hsid != bhsid )
{
rc = -1;
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert(SqlDB * db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
if ( rc == -1 )
{
error_str = "Error inserting Host Share in DB.";
}
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
if(replace)
{
oss << "REPLACE";
}
else
{
oss << "INSERT";
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< hsid << ","
<< disk_usage <<","<< mem_usage <<","<< cpu_usage<< ","
<< max_disk <<","<< max_mem <<","<< max_cpu << ","
<< free_disk <<","<< free_mem <<","<< free_cpu << ","
<< used_disk <<","<< used_mem <<","<< used_cpu << ","
<< running_vms<< ")";
rc = db->exec(oss);
return rc;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::drop(SqlDB * db)
{
ostringstream oss;
oss << "DELETE FROM " << table << " WHERE hid=" << hsid;
return db->exec(oss);
}
/* ************************************************************************ */
/* HostShare :: Misc */
/* ************************************************************************ */
ostream& operator<<(ostream& os, HostShare& hs)
{
string str;
@ -287,7 +65,6 @@ string& HostShare::to_xml(string& xml) const
ostringstream oss;
oss << "<HOST_SHARE>"
<< "<HID>" << hsid << "</HID>"
<< "<DISK_USAGE>" << disk_usage << "</DISK_USAGE>"
<< "<MEM_USAGE>" << mem_usage << "</MEM_USAGE>"
<< "<CPU_USAGE>" << cpu_usage << "</CPU_USAGE>"
@ -311,13 +88,41 @@ string& HostShare::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int HostShare::from_xml_node(const xmlNodePtr node)
{
// Initialize the internal XML object
ObjectXML::update_from_node(node);
disk_usage = atoi(((*this)["/HOST_SHARE/DISK_USAGE"] )[0].c_str() );
mem_usage = atoi(((*this)["/HOST_SHARE/MEM_USAGE"] )[0].c_str() );
cpu_usage = atoi(((*this)["/HOST_SHARE/CPU_USAGE"] )[0].c_str() );
max_disk = atoi(((*this)["/HOST_SHARE/MAX_DISK"] )[0].c_str() );
max_mem = atoi(((*this)["/HOST_SHARE/MAX_MEM"] )[0].c_str() );
max_cpu = atoi(((*this)["/HOST_SHARE/MAX_CPU"] )[0].c_str() );
free_disk = atoi(((*this)["/HOST_SHARE/FREE_DISK"] )[0].c_str() );
free_mem = atoi(((*this)["/HOST_SHARE/FREE_MEM"] )[0].c_str() );
free_cpu = atoi(((*this)["/HOST_SHARE/FREE_CPU"] )[0].c_str() );
used_disk = atoi(((*this)["/HOST_SHARE/USED_DISK"] )[0].c_str() );
used_mem = atoi(((*this)["/HOST_SHARE/USED_MEM"] )[0].c_str() );
used_cpu = atoi(((*this)["/HOST_SHARE/USED_CPU"] )[0].c_str() );
running_vms = atoi(((*this)["/HOST_SHARE/RUNNING_VMS"] )[0].c_str() );
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& HostShare::to_str(string& str) const
{
string template_xml;
ostringstream oss;
oss<< "\tHID = " << hsid
<< "\tCPU_USAGE = " << cpu_usage << endl
oss<< "\tCPU_USAGE = " << cpu_usage << endl
<< "\tMEMORY_USAGE = " << mem_usage << endl
<< "\tDISK_USAGE = " << disk_usage<< endl
<< "\tMAX_CPU = " << max_cpu << endl

View File

@ -21,6 +21,7 @@ Import('env')
env.Prepend(LIBS=[
'nebula_core_test',
'nebula_host',
'nebula_xml',
'nebula_vmm',
'nebula_im',
'nebula_rm',

View File

@ -30,6 +30,7 @@ env.Prepend(LIBS=[
'nebula_core',
'nebula_sql',
'nebula_log',
'nebula_xml',
'crypto'
])

View File

@ -32,6 +32,7 @@ env.Prepend(LIBS=[
'nebula_vnm',
'nebula_image',
'nebula_pool',
'nebula_xml',
'nebula_hm',
'nebula_authm',
'nebula_common',

View File

@ -52,6 +52,7 @@ env.Prepend(LIBS=[
'nebula_common',
'nebula_sql',
'nebula_log',
'nebula_xml',
'crypto'
])

View File

@ -23,6 +23,7 @@ env.Append(LIBPATH=[
env.Prepend(LIBS=[
'nebula_pool',
'nebula_xml',
'nebula_common',
'nebula_log',
'nebula_core',

View File

@ -84,7 +84,7 @@ public:
return -1;
}
update(message);
update_from_str(message);
vector<xmlNodePtr> nodes;
int num_objs;

View File

@ -445,12 +445,7 @@ Attribute * Template::vector_xml_att(const xmlNode * node)
int Template::from_xml(const string &xml_str)
{
xmlDocPtr xml_doc = 0;
xmlNode * root_element;
xmlNode * cur_node = 0;
Attribute * attr;
// Parse xml string as libxml document
xml_doc = xmlParseMemory (xml_str.c_str(),xml_str.length());
@ -460,6 +455,44 @@ int Template::from_xml(const string &xml_str)
return -1;
}
// Get the <TEMPLATE> element
root_element = xmlDocGetRootElement(xml_doc);
if( root_element == 0 )
{
return -1;
}
rebuild_attributes(root_element);
xmlFreeDoc(xml_doc);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Template::from_xml_node(const xmlNodePtr node)
{
if (node == 0)
{
return -1;
}
rebuild_attributes(node);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
void Template::rebuild_attributes(const xmlNode * root_element)
{
xmlNode * cur_node = 0;
Attribute * attr;
//Clear the template if not empty
if (!attributes.empty())
{
@ -473,9 +506,6 @@ int Template::from_xml(const string &xml_str)
attributes.clear();
}
// Get the <TEMPLATE> element
root_element = xmlDocGetRootElement(xml_doc);
// Get the root's children and try to build attributes.
for (cur_node = root_element->children;
cur_node != 0;
@ -497,10 +527,6 @@ int Template::from_xml(const string &xml_str)
}
}
}
xmlFreeDoc(xml_doc);
return 0;
}
/* -------------------------------------------------------------------------- */

View File

@ -18,6 +18,7 @@
Import('env')
env.Append(LIBS=[
'nebula_xml',
'nebula_template',
'nebula_common',
'nebula_core',

View File

@ -19,6 +19,7 @@ Import('env')
env.Prepend(LIBS=[
'nebula_um',
'nebula_pool',
'nebula_xml',
'nebula_log',
'nebula_authm',
'nebula_common',

View File

@ -24,6 +24,7 @@ env.Prepend(LIBS=[
'nebula_authm',
'nebula_template',
'nebula_pool',
'nebula_xml',
'nebula_image',
'nebula_mad',
'nebula_common',

View File

@ -23,6 +23,7 @@ env.Prepend(LIBS=[
'nebula_authm',
'nebula_template',
'nebula_pool',
'nebula_xml',
'nebula_mad',
'nebula_core',
'nebula_common',

View File

@ -169,7 +169,7 @@ int ObjectXML::get_nodes (const char * xpath_expr, vector<xmlNodePtr>& content)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::update(const string &xml_doc)
int ObjectXML::update_from_str(const string &xml_doc)
{
if (xml != 0)
{
@ -196,6 +196,50 @@ int ObjectXML::update(const string &xml_doc)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::update_from_node(const xmlNodePtr node)
{
if (xml != 0)
{
xmlFreeDoc(xml);
}
if ( ctx != 0)
{
xmlXPathFreeContext(ctx);
}
xml = xmlNewDoc(reinterpret_cast<const xmlChar *>("1.0"));
if (xml == 0)
{
return -1;
}
ctx = xmlXPathNewContext(xml);
if (ctx == 0)
{
xmlFreeDoc(xml);
return -1;
}
xmlNodePtr root_node = xmlDocCopyNode(node,xml,1);
if (root_node == 0)
{
xmlXPathFreeContext(ctx);
xmlFreeDoc(xml);
return -1;
}
xmlDocSetRootElement(xml, root_node);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ObjectXML::xml_parse(const string &xml_doc)
{
xml = xmlParseMemory (xml_doc.c_str(),xml_doc.length());

View File

@ -125,7 +125,7 @@ public:
CPPUNIT_ASSERT(hostnames[0] == "A_hostname");
CPPUNIT_ASSERT(hostnames[1] == "C_hostname");
obj.update(xml_history_dump2);
obj.update_from_str(xml_history_dump2);
hostnames = obj["/VM_POOL/VM/HISTORY/HOSTNAME"];