From 829d34c400a56318f0448621912fa348dac6bd97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 23 Feb 2011 11:17:45 +0100 Subject: [PATCH 01/45] Feature #407: Code refactor, ObjectXML moved to main source tree --- SConstruct | 3 +++ .../scheduler/include => include}/ObjectXML.h | 0 src/scheduler/SConstruct | 5 ++-- src/scheduler/src/{xml => client}/Client.cc | 0 src/scheduler/src/client/SConstruct | 26 +++++++++++++++++++ src/scheduler/src/pool/test/SConstruct | 2 +- src/scheduler/src/sched/SConstruct | 3 ++- src/{scheduler/src => }/xml/ObjectXML.cc | 0 src/{scheduler/src => }/xml/SConstruct | 23 ++++++++-------- src/{scheduler/src => }/xml/expr_arith.cc | 0 src/{scheduler/src => }/xml/expr_arith.h | 0 src/{scheduler/src => }/xml/expr_arith.y | 0 src/{scheduler/src => }/xml/expr_bool.cc | 0 src/{scheduler/src => }/xml/expr_bool.h | 0 src/{scheduler/src => }/xml/expr_bool.y | 0 src/{scheduler/src => }/xml/expr_parser.c | 0 src/{scheduler/src => }/xml/expr_parser.l | 0 .../src => }/xml/test/ObjectXMLTest.cc | 0 src/{scheduler/src => }/xml/test/SConstruct | 8 +++--- 19 files changed, 49 insertions(+), 21 deletions(-) rename {src/scheduler/include => include}/ObjectXML.h (100%) rename src/scheduler/src/{xml => client}/Client.cc (100%) create mode 100644 src/scheduler/src/client/SConstruct rename src/{scheduler/src => }/xml/ObjectXML.cc (100%) rename src/{scheduler/src => }/xml/SConstruct (80%) rename src/{scheduler/src => }/xml/expr_arith.cc (100%) rename src/{scheduler/src => }/xml/expr_arith.h (100%) rename src/{scheduler/src => }/xml/expr_arith.y (100%) rename src/{scheduler/src => }/xml/expr_bool.cc (100%) rename src/{scheduler/src => }/xml/expr_bool.h (100%) rename src/{scheduler/src => }/xml/expr_bool.y (100%) rename src/{scheduler/src => }/xml/expr_parser.c (100%) rename src/{scheduler/src => }/xml/expr_parser.l (100%) rename src/{scheduler/src => }/xml/test/ObjectXMLTest.cc (100%) rename src/{scheduler/src => }/xml/test/SConstruct (91%) diff --git a/SConstruct b/SConstruct index 61eb2240fb..25e721864b 100644 --- a/SConstruct +++ b/SConstruct @@ -73,6 +73,7 @@ main_env.Append(LIBPATH=[ cwd+'/src/hm', cwd+'/src/um', cwd+'/src/authm', + cwd+'/src/xml', ]) # Compile flags @@ -187,6 +188,7 @@ build_scripts=[ 'src/hm/SConstruct', 'src/um/SConstruct', 'src/authm/SConstruct', + 'src/xml/SConstruct', ] # Testing @@ -222,6 +224,7 @@ if testing=='yes': 'src/um/test/SConstruct', 'src/vm/test/SConstruct', 'src/vnm/test/SConstruct', + 'src/xml/test/SConstruct', ]) else: main_env.Append(testing='no') diff --git a/src/scheduler/include/ObjectXML.h b/include/ObjectXML.h similarity index 100% rename from src/scheduler/include/ObjectXML.h rename to include/ObjectXML.h diff --git a/src/scheduler/SConstruct b/src/scheduler/SConstruct index 584e7986ac..01a6daf213 100644 --- a/src/scheduler/SConstruct +++ b/src/scheduler/SConstruct @@ -28,7 +28,7 @@ env.Append(CPPPATH=[ # Library dirs env.Append(LIBPATH=[ - cwd+'/src/xml', + cwd+'/src/client', cwd+'/src/pool', cwd+'/src/sched' ]) @@ -38,7 +38,7 @@ env.Append(LIBPATH=[ ################################################################################ build_scripts=[ - 'src/xml/SConstruct', + 'src/client/SConstruct', 'src/pool/SConstruct', 'src/sched/SConstruct' ] @@ -46,7 +46,6 @@ build_scripts=[ if env['testing']=='yes': build_scripts.extend([ 'src/pool/test/SConstruct', - 'src/xml/test/SConstruct', ]) for script in build_scripts: diff --git a/src/scheduler/src/xml/Client.cc b/src/scheduler/src/client/Client.cc similarity index 100% rename from src/scheduler/src/xml/Client.cc rename to src/scheduler/src/client/Client.cc diff --git a/src/scheduler/src/client/SConstruct b/src/scheduler/src/client/SConstruct new file mode 100644 index 0000000000..d9509465b1 --- /dev/null +++ b/src/scheduler/src/client/SConstruct @@ -0,0 +1,26 @@ +# SConstruct for src/pool + +# -------------------------------------------------------------------------- # +# Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # +# # +# 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. # +#--------------------------------------------------------------------------- # + +Import('sched_env') + +lib_name='scheduler_client' + +source_files=['Client.cc'] + +# Build library +sched_env.StaticLibrary(lib_name, source_files) diff --git a/src/scheduler/src/pool/test/SConstruct b/src/scheduler/src/pool/test/SConstruct index 4254d19e45..66ccbfba2f 100644 --- a/src/scheduler/src/pool/test/SConstruct +++ b/src/scheduler/src/pool/test/SConstruct @@ -18,7 +18,7 @@ Import('sched_env') # Libraries sched_env.Prepend(LIBS=[ - 'scheduler_xml', + 'nebula_xml', 'scheduler_pool', 'nebula_log', 'nebula_common', diff --git a/src/scheduler/src/sched/SConstruct b/src/scheduler/src/sched/SConstruct index 0239107e7c..52fd744fd7 100644 --- a/src/scheduler/src/sched/SConstruct +++ b/src/scheduler/src/sched/SConstruct @@ -31,7 +31,8 @@ sched_env.Prepend(LIBS=[ 'scheduler_sched', 'scheduler_pool', 'nebula_log', - 'scheduler_xml', + 'scheduler_client', + 'nebula_xml', 'nebula_common', 'crypto', ]) diff --git a/src/scheduler/src/xml/ObjectXML.cc b/src/xml/ObjectXML.cc similarity index 100% rename from src/scheduler/src/xml/ObjectXML.cc rename to src/xml/ObjectXML.cc diff --git a/src/scheduler/src/xml/SConstruct b/src/xml/SConstruct similarity index 80% rename from src/scheduler/src/xml/SConstruct rename to src/xml/SConstruct index 16765bad7b..16abefafdc 100644 --- a/src/scheduler/src/xml/SConstruct +++ b/src/xml/SConstruct @@ -1,4 +1,4 @@ -# SConstruct for src/pool +# SConstruct for src/xml # -------------------------------------------------------------------------- # # Copyright 2002-2010, OpenNebula Project Leads (OpenNebula.org) # @@ -16,33 +16,32 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -Import('sched_env') +Import('env') -lib_name='scheduler_xml' +lib_name='nebula_xml' -if sched_env['parsers']=='yes': +if env['parsers']=='yes': # LEX - parser=sched_env.Lex( + parser=env.Lex( source='expr_parser.l' ) - sched_env.NoClean(parser) + env.NoClean(parser) # BISON - parser=sched_env.Bison( + parser=env.Bison( source='expr_arith.y' ) - sched_env.NoClean(parser) + env.NoClean(parser) - parser=sched_env.Bison( + parser=env.Bison( source='expr_bool.y' ) - sched_env.NoClean(parser) + env.NoClean(parser) source_files=['ObjectXML.cc', - 'Client.cc', 'expr_parser.c', 'expr_bool.cc', 'expr_arith.cc'] # Build library -sched_env.StaticLibrary(lib_name, source_files) +env.StaticLibrary(lib_name, source_files) diff --git a/src/scheduler/src/xml/expr_arith.cc b/src/xml/expr_arith.cc similarity index 100% rename from src/scheduler/src/xml/expr_arith.cc rename to src/xml/expr_arith.cc diff --git a/src/scheduler/src/xml/expr_arith.h b/src/xml/expr_arith.h similarity index 100% rename from src/scheduler/src/xml/expr_arith.h rename to src/xml/expr_arith.h diff --git a/src/scheduler/src/xml/expr_arith.y b/src/xml/expr_arith.y similarity index 100% rename from src/scheduler/src/xml/expr_arith.y rename to src/xml/expr_arith.y diff --git a/src/scheduler/src/xml/expr_bool.cc b/src/xml/expr_bool.cc similarity index 100% rename from src/scheduler/src/xml/expr_bool.cc rename to src/xml/expr_bool.cc diff --git a/src/scheduler/src/xml/expr_bool.h b/src/xml/expr_bool.h similarity index 100% rename from src/scheduler/src/xml/expr_bool.h rename to src/xml/expr_bool.h diff --git a/src/scheduler/src/xml/expr_bool.y b/src/xml/expr_bool.y similarity index 100% rename from src/scheduler/src/xml/expr_bool.y rename to src/xml/expr_bool.y diff --git a/src/scheduler/src/xml/expr_parser.c b/src/xml/expr_parser.c similarity index 100% rename from src/scheduler/src/xml/expr_parser.c rename to src/xml/expr_parser.c diff --git a/src/scheduler/src/xml/expr_parser.l b/src/xml/expr_parser.l similarity index 100% rename from src/scheduler/src/xml/expr_parser.l rename to src/xml/expr_parser.l diff --git a/src/scheduler/src/xml/test/ObjectXMLTest.cc b/src/xml/test/ObjectXMLTest.cc similarity index 100% rename from src/scheduler/src/xml/test/ObjectXMLTest.cc rename to src/xml/test/ObjectXMLTest.cc diff --git a/src/scheduler/src/xml/test/SConstruct b/src/xml/test/SConstruct similarity index 91% rename from src/scheduler/src/xml/test/SConstruct rename to src/xml/test/SConstruct index 499fd915bd..c1f61c6ff0 100644 --- a/src/scheduler/src/xml/test/SConstruct +++ b/src/xml/test/SConstruct @@ -14,14 +14,14 @@ # limitations under the License. # #--------------------------------------------------------------------------- # -Import('sched_env') +Import('env') # Libraries -sched_env.Prepend(LIBS=[ +env.Prepend(LIBS=[ 'nebula_log', - 'scheduler_xml', + 'nebula_xml', 'nebula_common', 'nebula_test_common', ]) -sched_env.Program('test_xml','ObjectXMLTest.cc') +env.Program('test_xml','ObjectXMLTest.cc') From ebf66e0fda95c9081d4569222e2c6ab09570e2cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Thu, 24 Feb 2011 18:12:26 +0100 Subject: [PATCH 02/45] Feature #407: Host object uses new DB schema, storing the contents as XML --- include/Host.h | 70 +++----- include/HostShare.h | 104 ++---------- include/ObjectXML.h | 9 +- include/PoolObjectSQL.h | 138 ++++++++++++++- include/Template.h | 14 ++ include/test/PoolTest.h | 4 +- src/host/Host.cc | 290 ++++++-------------------------- src/host/HostPool.cc | 4 +- src/host/HostShare.cc | 255 ++++------------------------ src/host/test/SConstruct | 1 + src/image/test/SConstruct | 1 + src/lcm/test/SConstruct | 1 + src/nebula/SConstruct | 1 + src/pool/test/SConstruct | 1 + src/scheduler/include/PoolXML.h | 2 +- src/template/Template.cc | 50 ++++-- src/template/test/SConstruct | 1 + src/um/test/SConstruct | 1 + src/vm/test/SConstruct | 1 + src/vnm/test/SConstruct | 1 + src/xml/ObjectXML.cc | 46 ++++- src/xml/test/ObjectXMLTest.cc | 2 +- 22 files changed, 370 insertions(+), 627 deletions(-) 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