diff --git a/include/Host.h b/include/Host.h index ca6295305a..dc28c73fce 100644 --- a/include/Host.h +++ b/include/Host.h @@ -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_*/ diff --git a/include/HostShare.h b/include/HostShare.h index d7b6a12078..b3f277c74a 100644 --- a/include/HostShare.h +++ b/include/HostShare.h @@ -17,8 +17,7 @@ #ifndef HOST_SHARE_H_ #define HOST_SHARE_H_ -#include "SqlDB.h" -#include "ObjectSQL.h" +#include "ObjectXML.h" #include 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); - }; diff --git a/include/ObjectXML.h b/include/ObjectXML.h index be27cac09d..1f2a9bd803 100644 --- a/include/ObjectXML.h +++ b/include/ObjectXML.h @@ -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 diff --git a/include/PoolObjectSQL.h b/include/PoolObjectSQL.h index 1ac4577700..deee699821 100644 --- a/include/PoolObjectSQL.h +++ b/include/PoolObjectSQL.h @@ -18,7 +18,9 @@ #define POOL_OBJECT_SQL_H_ #include "ObjectSQL.h" +#include "ObjectXML.h" #include +#include 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(&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: /** diff --git a/include/Template.h b/include/Template.h index ad1539a3a7..6dfa90adc1 100644 --- a/include/Template.h +++ b/include/Template.h @@ -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; diff --git a/include/test/PoolTest.h b/include/test/PoolTest.h index 8545873066..50426bdf08 100644 --- a/include/test/PoolTest.h +++ b/include/test/PoolTest.h @@ -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 ); } diff --git a/src/host/Host.cc b/src/host/Host.cc index a37d8c478c..1c8aaccb7a 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -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(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(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(&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::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 << - "" << - "" << values[OID] <<"" << - "" << values[HOST_NAME] <<"" << - "" << values[STATE] <<"" << - "" << values[IM_MAD] <<"" << - "" << values[VM_MAD] <<"" << - "" << values[TM_MAD] <<"" << - ""<< values[LAST_MON_TIME]<<""<< - "" << values[CLUSTER] <<"" << - values[TEMPLATE]; - - HostShare::dump(oss,num - LIMIT, values + LIMIT, names + LIMIT); - - oss << ""; - - 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 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( 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( 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; diff --git a/src/host/HostPool.cc b/src/host/HostPool.cc index 59990790a4..c799a461cc 100644 --- a/src/host/HostPool.cc +++ b/src/host/HostPool.cc @@ -265,9 +265,7 @@ int HostPool::dump(ostringstream& oss, const string& where) set_callback(static_cast(&HostPool::dump_cb), static_cast(&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() ) { diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index 9a2c2ed25d..b4e8e3cf88 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -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 << - "" << - "" << 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] << "" << - ""<"<< - ""; - - return 0; -} - -/* ------------------------------------------------------------------------ */ -/* ------------------------------------------------------------------------ */ - -int HostShare::select(SqlDB * db) -{ - ostringstream oss; - int rc; - int bhsid; - - set_callback(static_cast(&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 << "" - << "" << hsid << "" << "" << disk_usage << "" << "" << mem_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 diff --git a/src/host/test/SConstruct b/src/host/test/SConstruct index 44b09154ab..208c1a2426 100644 --- a/src/host/test/SConstruct +++ b/src/host/test/SConstruct @@ -21,6 +21,7 @@ Import('env') env.Prepend(LIBS=[ 'nebula_core_test', 'nebula_host', + 'nebula_xml', 'nebula_vmm', 'nebula_im', 'nebula_rm', diff --git a/src/image/test/SConstruct b/src/image/test/SConstruct index 5d46ebb06e..dd27a5315f 100644 --- a/src/image/test/SConstruct +++ b/src/image/test/SConstruct @@ -30,6 +30,7 @@ env.Prepend(LIBS=[ 'nebula_core', 'nebula_sql', 'nebula_log', + 'nebula_xml', 'crypto' ]) diff --git a/src/lcm/test/SConstruct b/src/lcm/test/SConstruct index dfacc236da..d9a778de2e 100644 --- a/src/lcm/test/SConstruct +++ b/src/lcm/test/SConstruct @@ -32,6 +32,7 @@ env.Prepend(LIBS=[ 'nebula_vnm', 'nebula_image', 'nebula_pool', + 'nebula_xml', 'nebula_hm', 'nebula_authm', 'nebula_common', diff --git a/src/nebula/SConstruct b/src/nebula/SConstruct index d988750e5a..d01fc31cb4 100644 --- a/src/nebula/SConstruct +++ b/src/nebula/SConstruct @@ -52,6 +52,7 @@ env.Prepend(LIBS=[ 'nebula_common', 'nebula_sql', 'nebula_log', + 'nebula_xml', 'crypto' ]) diff --git a/src/pool/test/SConstruct b/src/pool/test/SConstruct index 6703b61b22..0d248a04a3 100644 --- a/src/pool/test/SConstruct +++ b/src/pool/test/SConstruct @@ -23,6 +23,7 @@ env.Append(LIBPATH=[ env.Prepend(LIBS=[ 'nebula_pool', + 'nebula_xml', 'nebula_common', 'nebula_log', 'nebula_core', diff --git a/src/scheduler/include/PoolXML.h b/src/scheduler/include/PoolXML.h index 7a4ed1d629..fd1af23001 100644 --- a/src/scheduler/include/PoolXML.h +++ b/src/scheduler/include/PoolXML.h @@ -84,7 +84,7 @@ public: return -1; } - update(message); + update_from_str(message); vector nodes; int num_objs; diff --git a/src/template/Template.cc b/src/template/Template.cc index 8556237ed4..afb49732ca 100644 --- a/src/template/Template.cc +++ b/src/template/Template.cc @@ -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