1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-24 21:34:01 +03:00

Merge branch 'feature-407' into feature-523

This commit is contained in:
Ruben S. Montero 2011-03-10 12:34:46 +01:00
commit 08466818fd
90 changed files with 2474 additions and 3346 deletions

View File

@ -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')

View File

@ -25,7 +25,7 @@
using namespace std;
//TODO refactor ClusterPool
/**
* A cluster helper class. It is not a normal PoolSQL,
* but a series of static methods related to clusters.

View File

@ -150,6 +150,13 @@ private:
* @return 0 if success
*/
int unset(const string& ip);
/**
* Updates the DB entry for this lease
* @param lease Lease to be updated
* @return 0 if success
*/
int update_lease(Lease * lease);
};
#endif /*FIXED_LEASES_H_*/

View File

@ -25,7 +25,7 @@ using namespace std;
* The History class, it represents an execution record of a Virtual Machine.
*/
class History:public ObjectSQL
class History:public ObjectSQL, public ObjectXML
{
public:
enum MigrationReason
@ -40,13 +40,13 @@ public:
History(int oid, int _seq = -1);
History(
int oid,
int seq,
int hid,
string& hostname,
string& vm_dir,
string& vmm,
string& tm);
int oid,
int seq,
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm,
const string& tm);
~History(){};
@ -55,14 +55,6 @@ public:
*/
friend ostream& operator<<(ostream& os, const History& history);
/**
* Function to print the History 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 History object into a string in
* XML format
@ -78,52 +70,15 @@ private:
// ----------------------------------------
// DataBase implementation variables
// ----------------------------------------
enum ColNames
{
VID = 0,
SEQ = 1,
HOSTNAME = 2,
VM_DIR = 3,
HID = 4,
VMMMAD = 5,
TMMAD = 6,
STIME = 7,
ETIME = 8,
PROLOG_STIME = 9,
PROLOG_ETIME = 10,
RUNNING_STIME = 11,
RUNNING_ETIME = 12,
EPILOG_STIME = 13,
EPILOG_ETIME = 14,
REASON = 15,
LIMIT = 16
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
void non_persistent_data();
static string column_name(const ColNames column)
{
switch (column)
{
case HID:
return "hid";
case ETIME:
return "etime";
case RUNNING_ETIME:
return "retime";
default:
return "";
}
}
// ----------------------------------------
// History fields
// ----------------------------------------
@ -167,7 +122,12 @@ private:
* @param db pointer to the database.
* @return 0 on success.
*/
int insert(SqlDB * db, string& error_str);
int insert(SqlDB * db, string& error_str)
{
error_str.clear();
return insert_replace(db, false);
}
/**
* Reads the history record from the DB
@ -181,7 +141,10 @@ private:
* @param db pointer to the database.
* @return 0 on success.
*/
int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Removes the all history records from the DB
@ -208,15 +171,35 @@ private:
int select_cb(void *nil, int num, char **values, char **names);
/**
* Function to unmarshall a History object into an output stream with 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
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
static int dump(ostringstream& oss, int num, char **names, char **values);
int from_xml_node(const xmlNodePtr node)
{
ObjectXML::update_from_node(node);
return rebuild_attributes();
}
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str)
{
ObjectXML::update_from_str(xml_str);
return rebuild_attributes();
}
/**
* Rebuilds the internal attributes using xpath
*/
int rebuild_attributes();
};
#endif /*HISTORY_H_*/

View File

@ -49,28 +49,20 @@ 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 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
*/
string& to_xml(string& xml) const;
/**
* Get the Host unique identifier HID, that matches the OID of the object
* @return HID Host identifier
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int get_hid() const
{
return oid;
};
int from_xml(const string &xml_str);
/**
* Check if the host is enabled
@ -120,15 +112,6 @@ public:
state = INIT;
};
/**
* Returns host host_name
* @return host_name Host's hostname
*/
const string& get_hostname() const
{
return hostname;
};
/** Update host counters and update the whole host on the DB
* @param parse_str string with values to be parsed
* @return 0 on success
@ -378,9 +361,6 @@ private:
// -------------------------------------------------------------------------
// Host Description
// -------------------------------------------------------------------------
string hostname;
/**
* The state of the Host
*/
@ -426,41 +406,6 @@ private:
*/
HostShare host_share;
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
/**
* 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 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:
// *************************************************************************
// Constructor
// *************************************************************************
@ -474,23 +419,9 @@ protected:
virtual ~Host();
// *************************************************************************
// DataBase implementation
// DataBase implementation (Private)
// *************************************************************************
enum ColNames
{
OID = 0,
HOST_NAME = 1,
STATE = 2,
IM_MAD = 3,
VM_MAD = 4,
TM_MAD = 5,
LAST_MON_TIME = 6,
CLUSTER = 7,
TEMPLATE = 8,
LIMIT = 9
};
static const char * db_names;
static const char * db_bootstrap;
@ -498,42 +429,36 @@ 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
* 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);
/**
* Bootstraps the database table(s) associated to the Host
*/
virtual int select(SqlDB *db);
static void bootstrap(SqlDB * db)
{
ostringstream oss_host(Host::db_bootstrap);
db->exec(oss_host);
};
/**
* Writes the Host and its associated HostShares in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB *db, string& error_str);
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the Hosts data fields in the database.
* @param db pointer to the db
* @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);
int update(SqlDB *db);
};
#endif /*HOST_H_*/

View File

@ -68,7 +68,7 @@ public:
{
return static_cast<Host *>(PoolSQL::get(oid,lock));
};
/**
* Bootstraps the database table(s) associated to the Host pool
*/
@ -138,7 +138,10 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "HOST_POOL", Host::table, where);
}
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
@ -146,17 +149,6 @@ public:
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Returns true if the clid is an id for an existing cluster
* @param clid ID of the cluster
*
* @return true if the clid is an id for an existing cluster
*/
/* bool exists_cluster(int clid)
{
return cluster_pool.exists(clid);
};*/
/**
* Allocates a new cluster in the pool
* @param clid the id assigned to the cluster
@ -262,16 +254,6 @@ private:
* @return 0 on success
*/
int discover_cb(void * _map, int num, char **values, char **names);
/**
* Callback function to get output the host pool in XML format
* (Host::dump)
* @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 dump_cb(void * _oss, int num, char **values, char **names);
};
#endif /*HOST_POOL_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);
@ -92,14 +90,6 @@ public:
*/
friend ostream& operator<<(ostream& os, HostShare& hs);
/**
* Function to print the HostShare 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 HostShare object into a string in
* XML format
@ -110,8 +100,6 @@ public:
private:
int hsid; /**< HostShare identifier */
int disk_usage; /**< Disk allocated to VMs (in Mb). */
int mem_usage; /**< Memory allocated to VMs (in Mb) */
int cpu_usage; /**< CPU allocated to VMs (in percentage) */
@ -128,7 +116,7 @@ private:
int used_mem; /**< Used memory from the IM monitor */
int used_cpu; /**< Used cpu from the IM monitor */
int running_vms; /**< Number of running VMs in this Host */
int running_vms;/**< Number of running VMs in this Host */
// ----------------------------------------
// Friends
@ -137,95 +125,13 @@ 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
* Rebuilds the object from an xml node
* @param node The xml node pointer
*
* @return 0 on success, -1 otherwise
*/
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);
int from_xml_node(const xmlNodePtr node);
};
#endif /*HOST_SHARE_H_*/

View File

@ -59,13 +59,6 @@ public:
// Image Public Methods
// *************************************************************************
/**
* Function to print the Image 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 Image object into a string in XML format
* @param xml the resulting XML string
@ -74,31 +67,12 @@ public:
string& to_xml(string& xml) const;
/**
* Get the Image unique identifier IID, that matches the OID of the object
* @return IID Image identifier
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int get_iid() const
{
return oid;
};
/**
* Gets the uid of the owner of the Image
* @return uid
**/
int get_uid()
{
return uid;
}
/**
* Returns Image's name
* @return name Image's name
*/
const string& get_name() const
{
return name;
};
int from_xml(const string &xml_str);
/**
* Returns true if the image is public
@ -356,34 +330,29 @@ private:
// -------------------------------------------------------------------------
/**
* Owner if the image
* Image owner's name
*/
int uid;
/**
* The name of the Image
*/
string name;
string user_name;
/**
* Type of the Image
*/
ImageType type;
ImageType type;
/**
* Public scope of the Image
*/
int public_img;
int public_img;
/**
* Persistency of the Image
*/
int persistent_img;
int persistent_img;
/**
* Registration time
*/
time_t regtime;
time_t regtime;
/**
* Path to the image
@ -422,15 +391,6 @@ private:
*/
int insert_replace(SqlDB *db, bool replace);
/**
* Callback function to unmarshall a Image object (Image::select)
* @param num the number of columns read from the DB
* @param names the column names
* @param values 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 Image
*/
@ -455,7 +415,9 @@ protected:
// Constructor
// *************************************************************************
Image(int uid=-1, ImageTemplate *img_template = 0);
Image(int uid,
const string& user_name,
ImageTemplate* img_template);
virtual ~Image();
@ -463,37 +425,12 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0, /* Image identifier (IID) */
UID = 1, /* Image owner id */
NAME = 2, /* Image name */
TYPE = 3, /* 0) OS 1) CDROM 2) DATABLOCK */
PUBLIC = 4, /* Public scope (YES OR NO) */
PERSISTENT = 5, /* Peristency (YES OR NO) */
REGTIME = 6, /* Time of registration */
SOURCE = 7, /* Path to the image */
STATE = 8, /* 0) INIT 1) ALLOCATED */
RUNNING_VMS = 9, /* Number of VMs using the img */
TEMPLATE = 10, /* Image template xml data */
LIMIT = 11
};
static const char * extended_db_names;
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the Image (identified with its OID=IID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the Image in the database.
* @param db pointer to the db
@ -507,23 +444,6 @@ protected:
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops Image and associated template from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output an Image 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 /*IMAGE_H_*/

View File

@ -38,10 +38,10 @@ class ImagePool : public PoolSQL
{
public:
ImagePool(SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_dev_prefix);
ImagePool(SqlDB * db,
const string& _source_prefix,
const string& _default_type,
const string& _default_dev_prefix);
~ImagePool(){};
@ -56,20 +56,19 @@ public:
*/
int allocate (
int uid,
string user_name,
ImageTemplate * img_template,
int * oid,
string& error_str);
/**
* Function to get a Image from the pool, if the object is not in memory
** Function to get a Image from the pool, if the object is not in memory
* it is loaded from the DB
* @param oid Image unique id
* @param lock locks the Image mutex
* @return a pointer to the Image, 0 if the Image could not be loaded
*/
Image * get(
int oid,
bool lock)
Image * get(int oid, bool lock)
{
return static_cast<Image *>(PoolSQL::get(oid,lock));
};
@ -78,25 +77,15 @@ public:
* Function to get an Image from the pool using the image name
* @param name of the image
* @param lock locks the User mutex
* @return a pointer to the Image, 0 if the User could not be loaded
* @return a pointer to the Image, 0 if the image could not be loaded
*/
Image * get(
const string& name,
bool lock)
Image * get(const string& name, int uid, bool lock)
{
map<string, int>::iterator index;
index = image_names.find(name);
if ( index != image_names.end() )
{
return get((int)index->second,lock);
}
return 0;
return static_cast<Image *>(PoolSQL::get(name,uid,lock));
}
/** Update a particular Image
/**
* Update a particular Image
* @param image pointer to Image
* @return 0 on success
*/
@ -111,14 +100,7 @@ public:
*/
int drop(Image * image)
{
int rc = PoolSQL::drop(image);
if ( rc == 0)
{
image_names.erase(image->get_name());
}
return rc;
return PoolSQL::drop(image);
};
/**
@ -136,7 +118,10 @@ public:
* @param where filter for the objects, defaults to all
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "IMAGE_POOL", Image::table, where);
}
/**
* Generates a DISK attribute for VM templates using the Image metadata
@ -145,18 +130,21 @@ public:
* @param index number of datablock images used by the same VM. Will be
* automatically increased.
* @param img_type will be set to the used image's type
* @param uid of VM owner (to look for the image id within its images)
* @return 0 on success, -1 error, -2 not using the pool
*/
int disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type);
Image::ImageType * img_type,
int uid);
/**
* Generates an Authorization token for the DISK attribute
* @param disk the disk to be authorized
* @param uid of owner (to look for the image id within her images)
* @param ar the AuthRequest
*/
void authorize_disk(VectorAttribute * disk, AuthRequest * ar);
void authorize_disk(VectorAttribute * disk, int uid, AuthRequest * ar);
static const string& source_prefix()
{
@ -180,54 +168,29 @@ private:
/**
* Path to the image repository
**/
static string _source_prefix;
static string _source_prefix;
/**
* Default image type
**/
static string _default_type;
static string _default_type;
/**
* Default device prefix
**/
static string _default_dev_prefix;
static string _default_dev_prefix;
//--------------------------------------------------------------------------
// Pool Attributes
// -------------------------------------------------------------------------
/**
* This map stores the association between IIDs and Image names
*/
map<string, int> image_names;
/**
* Factory method to produce Image objects
* @return a pointer to the new Image
*/
PoolObjectSQL * create()
{
return new Image;
return new Image(-1,"",0);
};
/**
* Callback function to get output the image pool in XML format
* (Image::dump)
* @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 dump_cb(void * _oss, int num, char **values, char **names);
/**
* Callback function to build the image_names map
* @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 init_cb(void *nil, int num, char **values, char **names);
};
#endif /*IMAGE_POOL_H_*/

View File

@ -18,6 +18,7 @@
#define LEASES_H_
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include "Attribute.h"
#include <map>
@ -40,7 +41,8 @@ public:
* @param _size the max number of leases
*/
Leases(SqlDB * _db, int _oid, unsigned long _size):
oid(_oid), size(_size), db(_db){};
ObjectSQL(),
oid(_oid), size(_size), n_used(0), db(_db){};
virtual ~Leases()
{
@ -108,7 +110,7 @@ protected:
* The Lease class, it represents a pair of IP and MAC assigned to
* a Virtual Machine
*/
class Lease
class Lease : public ObjectXML
{
public:
/**
@ -129,13 +131,19 @@ protected:
* @param _used, the lease is in use
*/
Lease(unsigned int _ip, unsigned int _mac[], int _vid, bool _used=true)
:ip(_ip), vid(_vid), used(_used)
:ObjectXML(),ip(_ip), vid(_vid), used(_used)
{
// TODO check size
mac[PREFIX]=_mac[PREFIX];
mac[SUFFIX]=_mac[SUFFIX];
};
/**
* Creates a new empty lease. Method from_xml should be called right
* after this constructor.
*/
Lease():ObjectXML(){};
~Lease(){};
/**
@ -172,14 +180,6 @@ protected:
*/
friend ostream& operator<<(ostream& os, Lease& _lease);
/**
* Function to print the Lease 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 Lease object into a string in
* XML format
@ -188,6 +188,23 @@ protected:
*/
string& to_xml(string& xml) const;
/**
* Function to print the Lease object into a string in
* XML format. The output contains all the internal attributes,
* to be saved in the DB as a blob
* @param xml the resulting XML string
* @return a reference to the generated string
*/
string& to_xml_db(string& xml) const;
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
/**
* Constants to access the array storing the MAC address
*/
@ -227,6 +244,11 @@ protected:
*/
map<unsigned int, Lease *> leases;
/**
* Number of used leases
*/
int n_used;
// -------------------------------------------------------------------------
// DataBase implementation variables
// -------------------------------------------------------------------------
@ -235,17 +257,6 @@ protected:
*/
SqlDB * db;
enum ColNames
{
OID = 0,
IP = 1,
MAC_PREFIX = 2,
MAC_SUFFIX = 3,
VID = 4,
USED = 5,
LIMIT = 6
};
static const char * table;
static const char * db_names;
@ -274,14 +285,6 @@ protected:
friend ostream& operator<<(ostream& os, Lease& _lease);
/**
* Function to print the Leases 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 Leases object into a string in
* XML format

View File

@ -60,6 +60,61 @@ public:
*/
vector<string> operator[] (const char * xpath_expr);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(string& value, const char * xpath_expr, const char * def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(int& value, const char * xpath_expr, const int& def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(unsigned int& value, const char * xpath_expr,
const unsigned int& def);
/**
* Gets and sets a xpath attribute, if the attribute is not found a default
* is used
* @param value to set
* @param xpath_expr of the xml element
* @param def default value if the element is not found
*
* @return -1 if default was set
*/
int xpath(time_t& value, const char * xpath_expr, const time_t& def);
/**
* Gets the value of an element from an xml string
* @param value the value of the element
* @param xml the xml string
* @param xpath the xpath of the target element
*
* @return -1 if the element was not found
*/
static int xpath_value(string& value, const char *xml, const char *xpath);
/**
* Get xml nodes by Xpath
* @param xpath_expr the Xpath for the elements
@ -67,14 +122,21 @@ public:
* returned as pointers to the object nodes.
* @return the number of nodes found
*/
int get_nodes (const char * xpath_expr, vector<xmlNodePtr>& content);
int get_nodes(const char * xpath_expr, vector<xmlNodePtr>& content);
/**
* Updates the object representation with a new XML document. Previous
* 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,13 @@ 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, const string& _name, int _uid,const char *_table)
:ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),
valid(true),table(_table)
{
pthread_mutex_init(&mutex,0);
};
@ -47,11 +51,25 @@ public:
pthread_mutex_destroy(&mutex);
};
/* --------------------------------------------------------------------- */
int get_oid() const
{
return oid;
};
const string& get_name() const
{
return name;
};
int get_uid()
{
return uid;
};
/* --------------------------------------------------------------------- */
/**
* Check if the object is valid
* @return true if object is valid
@ -68,7 +86,7 @@ public:
void set_valid(const bool _valid)
{
valid = _valid;
}
};
/**
* Function to lock the object
@ -86,17 +104,99 @@ 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;
/**
* 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;
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 oid;
int select_cb(void *nil, int num, char **values, char **names)
{
if ( (!values[0]) || (num != 1) )
{
return -1;
}
return from_xml(values[0]);
};
/**
* The contents ob this object are valid
* Reads the PoolObjectSQL (identified by its OID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
bool valid;
virtual int select(SqlDB *db);
/**
* 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, const string& _name, int _uid);
/**
* Drops object from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* 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 object's name
*/
string name;
/**
* Object's owner, set it to -1 if owner is not used
*/
int uid;
/**
* The contents of this object are valid
*/
bool valid;
private:
@ -110,6 +210,11 @@ private:
* IS LOCKED when the class destructor is called.
*/
pthread_mutex_t mutex;
/**
* Pointer to the SQL table for the PoolObjectSQL
*/
const char * table;
};
#endif /*POOL_OBJECT_SQL_H_*/

View File

@ -45,6 +45,7 @@ public:
* @param _db a pointer to the database
* @param table the name of the table supporting the pool (to set the oid
* counter). If null the OID counter is not updated.
* @param with_uid the Pool objects have an owner id (uid)
*/
PoolSQL(SqlDB * _db, const char * table);
@ -68,9 +69,18 @@ public:
*
* @return a pointer to the object, 0 in case of failure
*/
virtual PoolObjectSQL * get(
int oid,
bool lock);
PoolObjectSQL * get(int oid, bool lock);
/**
* Gets an object from the pool (if needed the object is loaded from the
* database).
* @param name of the object
* @param uid id of owner
* @param lock locks the object if true
*
* @return a pointer to the object, 0 in case of failure
*/
PoolObjectSQL * get(const string& name, int uid, bool lock);
/**
* Finds a set objects that satisfies a given condition
@ -141,6 +151,19 @@ protected:
*/
SqlDB * db;
/**
* Dumps the pool in XML format. A filter can be also added to the
* query
* @param oss the output stream to dump the pool contents
* @param elem_name Name of the root xml pool name
* @param table Pool table name
* @param where filter for the objects, defaults to all
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& elem_name,
const char * table, const string& where);
private:
pthread_mutex_t mutex;
@ -164,6 +187,17 @@ private:
*/
map<int,PoolObjectSQL *> pool;
/**
* This is a name index for the pool map. The key is the name of the object
* , that may be combained with the owner id.
*/
map<string,PoolObjectSQL *> name_pool;
/**
* If set to true the objects of the pool will be indexed by its name also
*/
bool use_index;
/**
* Factory method, must return an ObjectSQL pointer to an allocated pool
* specific object.
@ -200,6 +234,25 @@ private:
*/
void replace();
/**
* Generate an index key for the object
* @param name of the object
* @param uid owner of the object, only used if needed
*
* @return the key, a string
*/
string key(const string& name, int uid)
{
ostringstream key;
key << name << ':' << uid;
return key.str();
};
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
/**
* Callback to set the lastOID (PoolSQL::PoolSQL)
*/
@ -209,6 +262,15 @@ private:
* Callback to store the IDs of pool objects (PoolSQL::search)
*/
int search_cb(void *_oids, int num, char **values, char **names);
/**
* Callback function to get output in XML format
* @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 dump_cb(void * _oss, int num, char **values, char **names);
};
#endif /*POOL_SQL_H_*/

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
@ -215,6 +223,12 @@ private:
* Name of the Root element for the XML document
*/
string xml_root;
/**
* 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);
};
/* -------------------------------------------------------------------------- */

View File

@ -36,13 +36,6 @@ public:
*/
friend ostream& operator<<(ostream& os, User& u);
/**
* Function to print the User 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 User object into a string in XML format
* @param xml the resulting XML string
@ -50,15 +43,6 @@ public:
*/
string& to_xml(string& xml) const;
/**
* Get the User unique identifier UID, that matches the OID of the object
* @return UID User identifier
*/
int get_uid() const
{
return oid;
};
/**
* Check if the user is enabled
* @return true if the user is enabled
@ -68,15 +52,6 @@ public:
return enabled;
}
/**
* Returns user username
* @return username User's hostname
*/
const string& get_username() const
{
return username;
};
/**
* Returns user password
* @return username User's hostname
@ -102,14 +77,6 @@ public:
enabled = false;
};
/**
* Sets user username
*/
void set_username(string _username)
{
username = _username;
};
/**
* Sets user password
*/
@ -145,11 +112,6 @@ private:
// User Attributes
// -------------------------------------------------------------------------
/**
* User's username
*/
string username;
/**
* User's password
*/
@ -191,16 +153,24 @@ private:
db->exec(oss_user);
};
/**
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int from_xml(const string &xml_str);
protected:
// *************************************************************************
// Constructor
// *************************************************************************
User(int id=-1,
string _username="",
string _password="",
bool _enabled=true);
User(int id,
string _username,
string _password,
bool _enabled);
virtual ~User();
@ -208,58 +178,28 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
USERNAME = 1,
PASSWORD = 2,
ENABLED = 3, // 0 = false, 1 = true
LIMIT = 4
};
static const char * db_names;
static const char * db_bootstrap;
static const char * table;
/**
* Reads the User (identified with its OID=UID) from the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int select(SqlDB *db);
/**
* Writes the User in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB *db, string& error_str);
int insert(SqlDB *db, string& error_str);
/**
* Writes/updates the User data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int update(SqlDB *db);
/**
* Drops USer from the database
* @param db pointer to the db
* @return 0 on success
*/
virtual int drop(SqlDB *db);
/**
* Function to output a User 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);
int update(SqlDB *db)
{
return insert_replace(db, true);
}
};
#endif /*USER_H_*/

View File

@ -61,16 +61,11 @@ public:
* @param lock locks the User mutex
* @return a pointer to the Host, 0 if the User could not be loaded
*/
User * get(
int oid,
bool lock)
User * get(int oid, bool lock)
{
User * user = static_cast<User *>(PoolSQL::get(oid,lock));
return user;
return static_cast<User *>(PoolSQL::get(oid,lock));
}
/**
* Function to get a User from the pool, if the object is not in memory
* it is loaded from the DB
@ -78,20 +73,9 @@ public:
* @param lock locks the User mutex
* @return a pointer to the User, 0 if the User could not be loaded
*/
User * get(
string username,
bool lock)
User * get(string name, bool lock)
{
map<string, int>::iterator index;
index = known_users.find(username);
if ( index != known_users.end() )
{
return get((int)index->second,lock);
}
return 0;
return static_cast<User *>(PoolSQL::get(name,-1,lock));
}
/** Update a particular User
@ -103,20 +87,12 @@ public:
return user->update(db);
};
/** Drops a user from the DB, the user mutex MUST BE locked
* @param user pointer to User
*/
int drop(User * user)
{
int rc = PoolSQL::drop(user);
if ( rc == 0)
{
known_users.erase(user->get_username());
}
return rc;
return PoolSQL::drop(user);
};
/**
@ -149,7 +125,10 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "USER_POOL", User::table, where);
}
private:
/**
@ -158,33 +137,8 @@ private:
*/
PoolObjectSQL * create()
{
return new User;
return new User(-1,"","",true);
};
/**
* This map stores the association between UIDs and Usernames
*/
map<string, int> known_users;
/**
* Callback function to get output the user pool in XML format
* (User::dump)
* @param _oss pointer to 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
*/
int dump_cb(void * _oss, int num, char **values, char **names);
/**
* Callback function to build the knwon_user map (User::User)
* @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 init_cb(void *nil, int num, char **values, char **names);
};
#endif /*USER_POOL_H_*/

View File

@ -119,14 +119,6 @@ public:
*/
friend ostream& operator<<(ostream& os, const VirtualMachine& vm);
/**
* Function to print the VirtualMachine 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 VirtualMachine object into a string in
* XML format
@ -135,6 +127,14 @@ public:
*/
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
*/
int from_xml(const string &xml_str);
// ------------------------------------------------------------------------
// Dynamic Info
// ------------------------------------------------------------------------
@ -183,16 +183,6 @@ public:
}
};
/**
* Returns the name of the VM
* @return the VM name
*/
const string& get_name() const
{
return name;
};
/**
* Returns the deployment ID
* @return the VMM driver specific ID
@ -218,11 +208,11 @@ public:
* Adds a new history record an writes it in the database.
*/
void add_history(
int hid,
string& hostname,
string& vm_dir,
string& vmm_mad,
string& tm_mad);
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm_mad,
const string& tm_mad);
/**
* Duplicates the last history record. Only the host related fields are
@ -678,16 +668,6 @@ public:
lcm_state = s;
};
/**
* Gets the user id of the owner of this VM
* @return the VM uid
*/
int get_uid() const
{
return uid;
};
// ------------------------------------------------------------------------
// Timers
// ------------------------------------------------------------------------
@ -753,7 +733,6 @@ public:
*/
int generate_context(string &files);
// ------------------------------------------------------------------------
// Image repository related functions
// ------------------------------------------------------------------------
@ -779,11 +758,10 @@ private:
// -------------------------------------------------------------------------
// Identification variables
// -------------------------------------------------------------------------
/**
* User (owner) id
* Owner's name
*/
int uid;
string user_name;
// -------------------------------------------------------------------------
// VM Scheduling & Managing Information
@ -797,11 +775,6 @@ private:
// Virtual Machine Description
// -------------------------------------------------------------------------
/**
* Name of the VM
*/
string name;
/**
* The Virtual Machine template, holds the VM attributes.
*/
@ -854,11 +827,6 @@ private:
*/
int net_rx;
/**
* Sequence number of the last history item.
*/
int last_seq;
/**
* History record, for the current host
*/
@ -981,7 +949,8 @@ protected:
// Constructor
//**************************************************************************
VirtualMachine(int id=-1, VirtualMachineTemplate * _vm_template = 0);
VirtualMachine(int id, int uid, string _user_name,
VirtualMachineTemplate * _vm_template);
virtual ~VirtualMachine();
@ -989,32 +958,10 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
UID = 1,
NAME = 2,
LAST_POLL = 3,
STATE = 4,
LCM_STATE = 5,
STIME = 6,
ETIME = 7,
DEPLOY_ID = 8,
MEMORY = 9,
CPU = 10,
NET_TX = 11,
NET_RX = 12,
LAST_SEQ = 13,
TEMPLATE = 14,
LIMIT = 15
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
/**
@ -1029,48 +976,28 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
virtual int insert(SqlDB * db, string& error_str);
int insert(SqlDB * db, string& error_str);
/**
* Writes/updates the Virtual Machine data fields in the database.
* @param db pointer to the db
* @return 0 on success
*/
virtual int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Deletes a VM from the database and all its associated information
* @param db pointer to the db
* @return -1
*/
virtual int drop(SqlDB * db)
int drop(SqlDB * db)
{
NebulaLog::log("ONE",Log::ERROR, "VM Drop not implemented!");
return -1;
}
/**
* Dumps the contect of a set of VirtualMachine objects in the given stream
* using 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);
/**
* Dumps the contect of a set of VirtualMachine objects in the given stream
* using 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_extended( ostringstream& oss,
int num, char ** values, char ** names);
};
#endif /*VIRTUAL_MACHINE_H_*/

View File

@ -48,11 +48,12 @@ public:
* the template
*/
int allocate (
int uid,
VirtualMachineTemplate *vm_template,
int * oid,
string& error_str,
bool on_hold = false);
int uid,
string user_name,
VirtualMachineTemplate * vm_template,
int * oid,
string& error_str,
bool on_hold = false);
/**
* Function to get a VM from the pool, if the object is not in memory
@ -134,7 +135,7 @@ public:
*/
int dump(ostringstream& oss, const string& where)
{
return dump(oss, true, -1, where);
return dump(oss, -1, where);
}
/**
@ -149,7 +150,7 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, bool extended, int state, const string& where);
int dump(ostringstream& oss, int state, const string& where);
private:
/**
@ -158,29 +159,8 @@ private:
*/
PoolObjectSQL * create()
{
return new VirtualMachine;
return new VirtualMachine(-1,-1,"", 0);
};
/**
* Callback function to get output the vm pool in XML format
* (VirtualMachinePool::dump)
* @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 dump_cb(void * _oss, int num, char **values, char **names);
/**
* Callback function to get output the vm pool in XML format
* (VirtualMachinePool::dump)
* @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 dump_extended_cb(void * _oss, int num, char **values, char **names);
};
#endif /*VIRTUAL_MACHINE_POOL_H_*/

View File

@ -58,24 +58,6 @@ public:
// Virtual Network Public Methods
// *************************************************************************
/**
* Get the Vnet unique identifier VNID, that matches the OID of the object
* @return VNID Image identifier
*/
int get_vnid() const
{
return oid;
};
/**
* Gets the uid of the owner of the Virtual Network
* @return uid
**/
int get_uid()
{
return uid;
}
/**
* Returns true if the Virtual Network is public
* @return true if the Virtual Network is public
@ -175,14 +157,6 @@ public:
*/
friend ostream& operator<<(ostream& os, VirtualNetwork& vn);
/**
* Function to print the VirtualNetwork 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 VirtualNetwork object into a string in
* XML format
@ -274,14 +248,9 @@ private:
// Identification variables
// -------------------------------------------------------------------------
/**
* Name of the Virtual Network
* Owner's name
*/
string name;
/**
* Owner of the Virtual Network
*/
int uid;
string user_name;
// -------------------------------------------------------------------------
// Binded physical attributes
@ -341,28 +310,29 @@ private:
};
/**
* Callback function to unmarshall a VNW object (VirtualNetwork::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
* Function to print the VirtualNetwork object into a string in
* XML format
* @param xml the resulting XML string
* @param extended If true, leases are included
* @return a reference to the generated string
*/
int select_cb(void * nil, int num, char **values, char **names);
string& to_xml_extended(string& xml, bool extended) const;
/**
* Function to drop VN entry in vn_pool
* @return 0 on success
* Rebuilds the object from an xml formatted string
* @param xml_str The xml-formatted string
*
* @return 0 on success, -1 otherwise
*/
int vn_drop(SqlDB * db);
protected:
int from_xml(const string &xml_str);
//**************************************************************************
// Constructor
//**************************************************************************
VirtualNetwork(VirtualNetworkTemplate * _vn_template = 0);
VirtualNetwork(int uid,
string _user_name,
VirtualNetworkTemplate * _vn_template = 0);
~VirtualNetwork();
@ -370,24 +340,10 @@ protected:
// DataBase implementation
// *************************************************************************
enum ColNames
{
OID = 0,
UID = 1,
NAME = 2,
TYPE = 3,
BRIDGE = 4,
PUBLIC = 5,
TEMPLATE = 6,
LIMIT = 7
};
static const char * table;
static const char * db_names;
static const char * extended_db_names;
static const char * db_bootstrap;
/**
@ -397,6 +353,23 @@ protected:
*/
int select(SqlDB * db);
/**
* Reads the Virtual Network (identified with its OID) from the database.
* @param db pointer to the db
* @param name of the network
* @param uid of the owner
*
* @return 0 on success
*/
int select(SqlDB * db, const string& name, int uid);
/**
* Reads the Virtual Network leases from the database.
* @param db pointer to the db
* @return 0 on success
*/
int select_leases(SqlDB * db);
/**
* Writes the Virtual Network and its associated template and leases in the database.
* @param db pointer to the db
@ -409,7 +382,10 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
int update(SqlDB * db);
int update(SqlDB * db)
{
return insert_replace(db, true);
}
/**
* Deletes a VNW from the database and all its associated information:
@ -418,27 +394,7 @@ protected:
* @param db pointer to the db
* @return 0 on success
*/
int drop(SqlDB * db)
{
int rc;
rc = leases->drop(db);
rc += vn_drop(db);
return rc;
}
/**
* Dumps the contect of a VirtualNetwork object in the given stream using
* 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);
int drop(SqlDB * db);
};
#endif /*VIRTUAL_NETWORK_H_*/

View File

@ -48,6 +48,7 @@ public:
*/
int allocate (
int uid,
string user_name,
VirtualNetworkTemplate * vn_template,
int * oid,
string& error_str);
@ -59,9 +60,7 @@ public:
* @param lock locks the VN mutex
* @return a pointer to the VN, 0 if the VN could not be loaded
*/
VirtualNetwork * get(
int oid,
bool lock)
VirtualNetwork * get(int oid, bool lock)
{
return static_cast<VirtualNetwork *>(PoolSQL::get(oid,lock));
};
@ -70,12 +69,14 @@ public:
* Function to get a VN from the pool using the network name
* If the object is not in memory it is loaded from the DB
* @param name VN unique name
* @param uid of the VN owner
* @param lock locks the VN mutex
* @return a pointer to the VN, 0 if the VN could not be loaded
*/
VirtualNetwork * get(
const string& name,
bool lock);
VirtualNetwork * get(const string& name, int uid, bool lock)
{
return static_cast<VirtualNetwork *>(PoolSQL::get(name,uid,lock));
};
//--------------------------------------------------------------------------
// Virtual Network DB access functions
@ -88,14 +89,14 @@ public:
* @param vid of the VM requesting the lease
* @return 0 on success, -1 error, -2 not using the pool
*/
int nic_attribute(VectorAttribute * nic, int vid);
int nic_attribute(VectorAttribute * nic, int uid, int vid);
/**
* Generates an Authorization token for a NIC attribute
* @param nic the nic to be authorized
* @param ar the AuthRequest
*/
void authorize_nic(VectorAttribute * nic, AuthRequest * ar);
void authorize_nic(VectorAttribute * nic, int uid, AuthRequest * ar);
/**
* Bootstraps the database table(s) associated to the VirtualNetwork pool
@ -113,7 +114,10 @@ public:
*
* @return 0 on success
*/
int dump(ostringstream& oss, const string& where);
int dump(ostringstream& oss, const string& where)
{
return PoolSQL::dump(oss, "VNET_POOL", VirtualNetwork::table,where);
}
/**
* Get the mac prefix
@ -150,28 +154,8 @@ private:
*/
PoolObjectSQL * create()
{
return new VirtualNetwork();
return new VirtualNetwork(0,"",0);
};
/**
* Callback function to get output the virtual network pool in XML format
* (VirtualNetworkPool::dump)
* @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 dump_cb(void * _oss, int num, char **values, char **names);
/**
* Callback function to get the ID of a given virtual network
* (VirtualNetworkPool::get)
* @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 get_cb(void * _oss, int num, char **values, char **names);
};
#endif /*VIRTUAL_NETWORK_POOL_H_*/

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

@ -6,7 +6,7 @@ TWD_DIR="../../src"
BASE_DIR=$PWD
TESTS="$TWD_DIR/vnm/test \
$TWD_DIR/scheduler/src/xml/test \
$TWD_DIR/xml/test \
$TWD_DIR/scheduler/src/pool/test \
$TWD_DIR/common/test \
$TWD_DIR/host/test \

View File

@ -33,8 +33,7 @@ Host::Host(
string _im_mad_name,
string _vmm_mad_name,
string _tm_mad_name):
PoolObjectSQL(id),
hostname(_hostname),
PoolObjectSQL(id,_hostname,-1,table),
state(INIT),
im_mad_name(_im_mad_name),
vmm_mad_name(_vmm_mad_name),
@ -52,92 +51,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 +63,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 +81,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,58 +94,25 @@ 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
sql_hostname = db->escape_str(hostname.c_str());
sql_hostname = db->escape_str(name.c_str());
if ( sql_hostname == 0 )
{
goto error_hostname;
}
sql_im_mad_name = db->escape_str(im_mad_name.c_str());
sql_xml = db->escape_str(to_xml(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)
@ -275,37 +126,21 @@ int Host::insert_replace(SqlDB *db, bool replace)
// Construct the SQL statement to Insert or Replace
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
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 +149,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;
@ -426,7 +201,7 @@ string& Host::to_xml(string& xml) const
oss <<
"<HOST>"
"<ID>" << oid << "</ID>" <<
"<NAME>" << hostname << "</NAME>" <<
"<NAME>" << name << "</NAME>" <<
"<STATE>" << state << "</STATE>" <<
"<IM_MAD>" << im_mad_name << "</IM_MAD>" <<
"<VM_MAD>" << vmm_mad_name << "</VM_MAD>" <<
@ -445,26 +220,55 @@ string& Host::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Host::to_str(string& str) const
int Host::from_xml(const string& xml)
{
string template_str;
string share_str;
vector<xmlNodePtr> content;
int int_state;
int rc = 0;
ostringstream os;
// Initialize the internal XML object
update_from_str(xml);
os <<
"ID = " << oid << endl <<
"NAME = " << hostname << endl <<
"STATE = " << state << endl <<
"IM MAD = " << im_mad_name << endl <<
"VMM MAD = " << vmm_mad_name << endl <<
"TM MAD = " << tm_mad_name << endl <<
"LAST_MON = " << last_monitored << endl <<
"CLUSTER = " << cluster << endl <<
"ATTRIBUTES" << endl << host_template.to_str(template_str) << endl <<
"HOST SHARES" << endl << host_share.to_str(share_str) <<endl;
// Get class base attributes
rc += xpath(oid, "/HOST/ID", -1);
rc += xpath(name, "/HOST/NAME", "not_found");
rc += xpath(int_state, "/HOST/STATE", 0);
str = os.str();
rc += xpath(im_mad_name, "/HOST/IM_MAD", "not_found");
rc += xpath(vmm_mad_name, "/HOST/VM_MAD", "not_found");
rc += xpath(tm_mad_name, "/HOST/TM_MAD", "not_found");
return str;
rc += xpath(last_monitored, "/HOST/LAST_MON_TIME", 0);
rc += xpath(cluster, "/HOST/CLUSTER", "not_found");
state = static_cast<HostState>( int_state );
// Get associated classes
ObjectXML::get_nodes("/HOST/HOST_SHARE", content);
if( content.size() < 1 )
{
return -1;
}
rc += host_share.from_xml_node( content[0] );
content.clear();
ObjectXML::get_nodes("/HOST/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
rc += host_template.from_xml_node( content[0] );
if (rc != 0)
{
return -1;
}
return 0;
}

View File

@ -55,7 +55,7 @@ void HostAllocateHook::do_hook(void *arg)
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
host->get_name(),
cmd,
parsed_args);
}
@ -179,7 +179,7 @@ void HostStateHook::do_hook(void *arg)
{
hmd->execute(host->get_oid(),
name,
host->get_hostname(),
host->get_name(),
cmd,
parsed_args);
}

View File

@ -202,18 +202,24 @@ int HostPool::allocate (
int HostPool::discover_cb(void * _map, int num, char **values, char **names)
{
map<int, string> * discovered_hosts;
string im_mad(values[1]);
string im_mad;
int hid;
int rc;
discovered_hosts = static_cast<map<int, string> *>(_map);
if ( (num<=0) || (values[0] == 0) )
if ( (num<2) || (values[0] == 0) || (values[1] == 0) )
{
return -1;
}
hid = atoi(values[0]);
im_mad = values[1];
hid = atoi(values[0]);
rc = ObjectXML::xpath_value(im_mad,values[1],"/HOST/IM_MAD");
if( rc != 0)
{
return -1;
}
discovered_hosts->insert(make_pair(hid,im_mad));
@ -230,7 +236,7 @@ int HostPool::discover(map<int, string> * discovered_hosts, int host_limit)
set_callback(static_cast<Callbackable::Callback>(&HostPool::discover_cb),
static_cast<void *>(discovered_hosts));
sql << "SELECT oid, im_mad FROM "
sql << "SELECT oid, body FROM "
<< Host::table << " WHERE state != "
<< Host::DISABLED << " ORDER BY last_mon_time ASC LIMIT " << host_limit;
@ -244,48 +250,6 @@ int HostPool::discover(map<int, string> * discovered_hosts, int host_limit)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostPool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return Host::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int HostPool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<HOST_POOL>";
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";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</HOST_POOL>";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int HostPool::drop_cluster(int clid)
{
int rc;

View File

@ -28,12 +28,10 @@
/* ************************************************************************ */
HostShare::HostShare(
int _hsid,
int _max_disk,
int _max_mem,
int _max_cpu):
ObjectSQL(),
hsid(_hsid),
ObjectXML(),
disk_usage(0),
mem_usage(0),
cpu_usage(0),
@ -46,228 +44,7 @@ HostShare::HostShare(
used_disk(0),
used_mem(0),
used_cpu(0),
running_vms(0)
{
}
/* ************************************************************************ */
/* 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 */
/* ************************************************************************ */
running_vms(0){};
ostream& operator<<(ostream& os, HostShare& hs)
{
@ -287,7 +64,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,30 +87,35 @@ string& HostShare::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& HostShare::to_str(string& str) const
int HostShare::from_xml_node(const xmlNodePtr node)
{
string template_xml;
ostringstream oss;
int rc = 0;
oss<< "\tHID = " << hsid
<< "\tCPU_USAGE = " << cpu_usage << endl
<< "\tMEMORY_USAGE = " << mem_usage << endl
<< "\tDISK_USAGE = " << disk_usage<< endl
<< "\tMAX_CPU = " << max_cpu << endl
<< "\tMAX_MEMORY = " << max_mem << endl
<< "\tMAX_DISK = " << max_disk<< endl
<< "\tFREE_CPU = " << free_cpu << endl
<< "\tFREE_MEMORY = " << free_mem << endl
<< "\tFREE_DISK = " << free_disk<< endl
<< "\tUSED_CPU = " << used_cpu << endl
<< "\tUSED_MEMORY = " << used_mem << endl
<< "\tUSED_DISK = " << used_disk<< endl
<< "\tRUNNING_VMS = " << running_vms<< endl;
// Initialize the internal XML object
ObjectXML::update_from_node(node);
str = oss.str();
rc += xpath(disk_usage, "/HOST_SHARE/DISK_USAGE", -1);
rc += xpath(mem_usage, "/HOST_SHARE/MEM_USAGE", -1);
rc += xpath(cpu_usage, "/HOST_SHARE/CPU_USAGE", -1);
return str;
rc += xpath(max_disk, "/HOST_SHARE/MAX_DISK", -1);
rc += xpath(max_mem , "/HOST_SHARE/MAX_MEM", -1);
rc += xpath(max_cpu , "/HOST_SHARE/MAX_CPU", -1);
rc += xpath(free_disk, "/HOST_SHARE/FREE_DISK", -1);
rc += xpath(free_mem , "/HOST_SHARE/FREE_MEM", -1);
rc += xpath(free_cpu , "/HOST_SHARE/FREE_CPU", -1);
rc += xpath(used_disk, "/HOST_SHARE/USED_DISK", -1);
rc += xpath(used_mem , "/HOST_SHARE/USED_MEM", -1);
rc += xpath(used_cpu , "/HOST_SHARE/USED_CPU", -1);
rc += xpath(running_vms,"/HOST_SHARE/RUNNING_VMS",-1);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */

View File

@ -33,7 +33,7 @@ const string xmls[] =
{
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE>"
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
@ -42,7 +42,7 @@ const string xmls[] =
"<HOST><ID>1</ID><NAME>Second host</NAME><STATE>0</STATE>"
"<IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>1</HID>"
"<LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU>"
"<FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU>"
@ -54,72 +54,72 @@ const string xmls[] =
const string xml_dump =
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM"
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST>"
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>2</ID><N"
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
"T_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
"NING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>3</ID><NAME>another "
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
"E><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
"MS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
"MS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>4</ID><NAME>host</NAME><ST"
"ATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad"
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>4</HID>"
"</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE>"
"<DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE>"
"<MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0"
"</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED"
"_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_"
"VMS></HOST_SHARE></HOST></HOST_POOL>";
"VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST></HOST_POOL>";
const string xml_dump_like_a =
"<HOST_POOL><HOST><ID>0</ID><NAME>a</NAME><STATE>0</STATE><IM_MAD>im_mad</I"
"M_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM"
"</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM"
"_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM"
">0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_"
"MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><U"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST>"
"SED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST>"
"<ID>1</ID><NAME>a name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MA"
"D>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_M"
"ON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHARE><HID>1</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"ON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</ME"
"M_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM>"
"<MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CP"
"U>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</U"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>2</ID><N"
"SED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>2</ID><N"
"AME>a_name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</V"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOS"
"T_SHARE><HID>2</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"M_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOS"
"T_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU"
"_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</"
"MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CP"
"U><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUN"
"NING_VMS>0</RUNNING_VMS></HOST_SHARE></HOST><HOST><ID>3</ID><NAME>another "
"NING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST><HOST><ID>3</ID><NAME>another "
"name</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD>"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><TEMPLATE></TEMPLATE><HOST_SHAR"
"E><HID>3</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
"<TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHAR"
"E><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE"
">0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CP"
"U><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USE"
"D_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_V"
"MS>0</RUNNING_VMS></HOST_SHARE></HOST></HOST_POOL>";
"MS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST></HOST_POOL>";
const string host0_updated =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE><ATT_A><![CDATA[VALUE_A]]></ATT_A><ATT_B><![CDATA[VALUE_B]]></ATT_B></TEMPLATE></HOST>";
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>default</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE><ATT_A><![CDATA[VALUE_A]]></ATT_A><ATT_B><![CDATA[VALUE_B]]></ATT_B></TEMPLATE></HOST>";
const string cluster_default =
"<CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER>";
@ -128,7 +128,7 @@ const string cluster_xml_dump =
"<CLUSTER_POOL><CLUSTER><ID>0</ID><NAME>default</NAME></CLUSTER><CLUSTER><ID>1</ID><NAME>cluster_a</NAME></CLUSTER><CLUSTER><ID>3</ID><NAME>cluster_c</NAME></CLUSTER><CLUSTER><ID>4</ID><NAME>cluster_d</NAME></CLUSTER></CLUSTER_POOL>";
const string host_0_cluster =
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><HID>0</HID><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
"<HOST><ID>0</ID><NAME>Host one</NAME><STATE>0</STATE><IM_MAD>im_mad</IM_MAD><VM_MAD>vmm_mad</VM_MAD><TM_MAD>tm_mad</TM_MAD><LAST_MON_TIME>0</LAST_MON_TIME><CLUSTER>cluster_a</CLUSTER><HOST_SHARE><DISK_USAGE>0</DISK_USAGE><MEM_USAGE>0</MEM_USAGE><CPU_USAGE>0</CPU_USAGE><MAX_DISK>0</MAX_DISK><MAX_MEM>0</MAX_MEM><MAX_CPU>0</MAX_CPU><FREE_DISK>0</FREE_DISK><FREE_MEM>0</FREE_MEM><FREE_CPU>0</FREE_CPU><USED_DISK>0</USED_DISK><USED_MEM>0</USED_MEM><USED_CPU>0</USED_CPU><RUNNING_VMS>0</RUNNING_VMS></HOST_SHARE><TEMPLATE></TEMPLATE></HOST>";
/* ************************************************************************* */
/* ************************************************************************* */
@ -153,6 +153,8 @@ class HostPoolTest : public PoolTest
CPPUNIT_TEST (remove_cluster);
CPPUNIT_TEST (update_info);
// CPPUNIT_TEST (scale_test);
CPPUNIT_TEST_SUITE_END ();
protected:
@ -184,12 +186,21 @@ protected:
CPPUNIT_ASSERT( obj != 0 );
string xml_str = "";
string name = host->get_hostname();
string name = host->get_name();
CPPUNIT_ASSERT( name == names[index] );
// Get the xml
host->to_xml(xml_str);
// A little help for debugging
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index];
}
//*/
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
@ -285,6 +296,15 @@ public:
string result = oss.str();
// A little help for debugging
/*
if( result != xml_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_dump;
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -304,12 +324,21 @@ public:
ostringstream oss;
rc = ((HostPool*)pool)->dump(oss, "host_name LIKE 'a%'");
rc = ((HostPool*)pool)->dump(oss, "name LIKE 'a%' ORDER BY oid");
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
// A little help for debugging
/*
if( result != xml_dump_like_a )
{
cout << endl << result << endl << "========"
<< endl << xml_dump_like_a;
}
//*/
CPPUNIT_ASSERT( result == xml_dump_like_a );
}
@ -360,6 +389,89 @@ public:
}
}
/* ********************************************************************* */
void scale_test()
{
time_t the_time, the_time2;
int oid,i,j,rc;
ostringstream oss,ossdump;
string err;
Host * host;
string monitor = "ARCH=x86_64 MODELNAME=\"Intel(R) Core(TM)2 Duo CPU P9300 @ 2.26GHz\" HYPERVISOR=kvm TOTALCPU=200 CPUSPEED=800 TOTALMEMORY=4005416 USEDMEMORY=2351928 FREEMEMORY=2826904 FREECPU=188.4 USEDCPU=11.599999999999994 NETRX=0 NETTX=0 HOSTNAME=pc-ruben";
cout << endl << "Allocate Test" << endl;
tearDown();
for (i=1000; i<30000 ; i = i + 5000)
{
setUp();
HostPool * hp = static_cast<HostPool *>(pool);
the_time = time(0);
for (j=0,oss.str(""); j<i ; j=j+1,oss.str(""))
{
oss << "host" << j;
hp->allocate(&oid, oss.str().c_str(),im_mad,vmm_mad,tm_mad,err);
}
the_time2 = time(0) - the_time;
hp->clean();
the_time = time(0);
rc = hp->dump(ossdump, "");
cout <<"\t"<<i<<"\t"<<the_time2<<"\t"<<time(0)-the_time<< endl;
tearDown();
}
cout << endl << "Read Test" << endl;
setUp();
// Allocate a HostPool
setUp();
HostPool * hp = static_cast<HostPool *>(pool);
for (i=10000,oss.str(""); i<30000 ; i++,oss.str(""))
{
oss << "host" << i;
hp->allocate(&oid,oss.str().c_str(),im_mad,vmm_mad,tm_mad,err);
host = hp->get(oid, false);
host->update_info(monitor);
hp->update(host);
}
//Load test
for (i=0; i<25000; i=i+5000)
{
hp->clean();
the_time = time(0);
for (j=0; j<i ; j++)
{
host = hp->get(j,true);
host->unlock();
}
cout << "\t" << i << "\t" << time(0) - the_time << 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

@ -201,7 +201,7 @@ void InformationManager::timer_action()
(thetime - host->get_last_monitored() >= monitor_period))
{
oss.str("");
oss << "Monitoring host " << host->get_hostname()
oss << "Monitoring host " << host->get_name()
<< " (" << it->first << ")";
NebulaLog::log("InM",Log::INFO,oss);
@ -225,7 +225,7 @@ void InformationManager::timer_action()
update_remotes = true;
}
imd->monitor(it->first,host->get_hostname(),update_remotes);
imd->monitor(it->first,host->get_name(),update_remotes);
host->set_state(Host::MONITORING);
}

View File

@ -28,14 +28,17 @@
#include "AuthManager.h"
#include "UserPool.h"
#define TO_UPPER(S) transform(S.begin(),S.end(),S.begin(),(int(*)(int))toupper)
/* ************************************************************************ */
/* Image :: Constructor/Destructor */
/* Image :: Constructor/Destructor */
/* ************************************************************************ */
Image::Image(int _uid, ImageTemplate * _image_template):
PoolObjectSQL(-1),
uid(_uid),
name(""),
Image::Image(int _uid,
const string& _user_name,
ImageTemplate * _image_template):
PoolObjectSQL(-1,"",_uid,table),
user_name(_user_name),
type(OS),
regtime(time(0)),
source(""),
@ -61,92 +64,16 @@ Image::~Image()
}
/* ************************************************************************ */
/* Image :: Database Access Functions */
/* Image :: Database Access Functions */
/* ************************************************************************ */
const char * Image::table = "image_pool";
const char * Image::db_names = "oid, uid, name, type, public, persistent, regtime, "
"source, state, running_vms, template";
const char * Image::extended_db_names = "image_pool.oid, image_pool.uid, "
"image_pool.name, image_pool.type, image_pool.public, "
"image_pool.persistent, image_pool.regtime, image_pool.source, "
"image_pool.state, image_pool.running_vms, image_pool.template";
const char * Image::db_names = "oid, name, body, uid, public";
const char * Image::db_bootstrap = "CREATE TABLE IF NOT EXISTS image_pool ("
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(128), "
"type INTEGER, public INTEGER, persistent INTEGER, regtime INTEGER, source TEXT, state INTEGER, "
"running_vms INTEGER, template TEXT, UNIQUE(name) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select_cb(void * nil, int num, char **values, char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = static_cast<ImageType>(atoi(values[TYPE]));
public_img = atoi(values[PUBLIC]);
persistent_img = atoi(values[PERSISTENT]);
regtime = static_cast<time_t>(atoi(values[REGTIME]));
source = values[SOURCE];
state = static_cast<ImageState>(atoi(values[STATE]));
running_vms = atoi(values[RUNNING_VMS]);
image_template->from_xml(values[TEMPLATE]);
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&Image::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, uid INTEGER, "
"public INTEGER, UNIQUE(name,uid) )";
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
@ -155,11 +82,11 @@ int Image::insert(SqlDB *db, string& error_str)
{
int rc;
string source_att;
string type_att;
string public_attr;
string persistent_attr;
string dev_prefix;
string source_att;
string type_att;
string public_attr;
string persistent_attr;
string dev_prefix;
// ---------------------------------------------------------------------
// Check default image attributes
@ -178,8 +105,7 @@ int Image::insert(SqlDB *db, string& error_str)
get_template_attribute("TYPE", type_att);
transform (type_att.begin(), type_att.end(), type_att.begin(),
(int(*)(int))toupper);
TO_UPPER(type_att);
if ( type_att.empty() == true )
{
@ -194,20 +120,20 @@ int Image::insert(SqlDB *db, string& error_str)
// ------------ PUBLIC --------------------
get_template_attribute("PUBLIC", public_attr);
image_template->erase("PUBLIC");
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
(int(*)(int))toupper);
TO_UPPER(public_attr);
public_img = (public_attr == "YES");
// ------------ PERSISTENT --------------------
get_template_attribute("PERSISTENT", persistent_attr);
image_template->erase("PERSISTENT");
transform (persistent_attr.begin(), persistent_attr.end(), persistent_attr.begin(),
(int(*)(int))toupper);
TO_UPPER(persistent_attr);
persistent_img = (persistent_attr == "YES");
@ -296,11 +222,10 @@ int Image::insert_replace(SqlDB *db, bool replace)
int rc;
string xml_template;
string xml_body;
char * sql_name;
char * sql_source;
char * sql_template;
char * sql_xml;
// Update the Image
@ -311,19 +236,11 @@ int Image::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
sql_source = db->escape_str(source.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_source == 0 )
if ( sql_xml == 0 )
{
goto error_source;
}
image_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)
@ -339,100 +256,24 @@ int Image::insert_replace(SqlDB *db, bool replace)
oss <<" INTO "<< table <<" ("<< db_names <<") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< type << ","
<< public_img << ","
<< persistent_img << ","
<< regtime << ","
<< "'" << sql_source << "',"
<< state << ","
<< running_vms << ","
<< "'" << sql_template << "')";
<< "'" << sql_xml << "',"
<< uid << ","
<< public_img << ")";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_source);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_source);
error_source:
error_body:
db->free_str(sql_name);
error_name:
return -1;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[PUBLIC]) ||
(!values[PERSISTENT]) ||
(!values[REGTIME]) ||
(!values[SOURCE]) ||
(!values[STATE]) ||
(!values[RUNNING_VMS]) ||
(!values[TEMPLATE]) ||
(num != LIMIT + 1))
{
return -1;
}
oss <<
"<IMAGE>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<TYPE>" << values[TYPE] << "</TYPE>" <<
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
"<PERSISTENT>" << values[PERSISTENT] << "</PERSISTENT>" <<
"<REGTIME>" << values[REGTIME] << "</REGTIME>" <<
"<SOURCE>" << values[SOURCE] << "</SOURCE>" <<
"<STATE>" << values[STATE] << "</STATE>" <<
"<RUNNING_VMS>" << values[RUNNING_VMS] << "</RUNNING_VMS>" <<
values[TEMPLATE] <<
"</IMAGE>";
return 0;
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int Image::drop(SqlDB * db)
{
ostringstream oss;
int rc;
// Only delete the VM
if (running_vms != 0)
{
return -1;
}
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ************************************************************************ */
/* Image :: Misc */
/* ************************************************************************ */
@ -458,6 +299,7 @@ string& Image::to_xml(string& xml) const
"<IMAGE>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<USERNAME>" << user_name << "</USERNAME>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<PUBLIC>" << public_img << "</PUBLIC>" <<
@ -477,30 +319,50 @@ string& Image::to_xml(string& xml) const
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
string& Image::to_str(string& str) const
int Image::from_xml(const string& xml)
{
string template_str;
vector<xmlNodePtr> content;
int int_state;
int int_type;
ostringstream os;
int rc = 0;
os <<
"ID = " << oid << endl <<
"UID = " << uid << endl <<
"NAME = " << name << endl <<
"TYPE = " << type << endl <<
"PUBLIC = " << public_img << endl <<
"PERSISTENT = " << persistent_img << endl <<
"REGTIME = " << regtime << endl <<
"SOURCE = " << source << endl <<
"STATE = " << state << endl <<
"RUNNING_VMS = " << running_vms << endl <<
"TEMPLATE" << endl
<< image_template->to_str(template_str)
<< endl;
// Initialize the internal XML object
update_from_str(xml);
str = os.str();
// Get class base attributes
rc += xpath(oid, "/IMAGE/ID", -1);
rc += xpath(uid, "/IMAGE/UID", -1);
rc += xpath(user_name, "/IMAGE/USERNAME", "not_found");
rc += xpath(name, "/IMAGE/NAME", "not_found");
return str;
rc += xpath(int_type, "/IMAGE/TYPE", 0);
rc += xpath(public_img, "/IMAGE/PUBLIC", 0);
rc += xpath(persistent_img, "/IMAGE/PERSISTENT", 0);
rc += xpath(regtime, "/IMAGE/REGTIME", 0);
rc += xpath(source, "/IMAGE/SOURCE", "not_found");
rc += xpath(int_state, "/IMAGE/STATE", 0);
rc += xpath(running_vms, "/IMAGE/RUNNING_VMS", -1);
type = static_cast<ImageType>(int_type);
state = static_cast<ImageState>(int_state);
// Get associated classes
ObjectXML::get_nodes("/IMAGE/TEMPLATE", content);
if( content.size() < 1 )
{
return -1;
}
rc += image_template->from_xml_node(content[0]);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ------------------------------------------------------------------------ */

View File

@ -27,61 +27,30 @@ string ImagePool::_source_prefix;
string ImagePool::_default_type;
string ImagePool::_default_dev_prefix;
int ImagePool::init_cb(void *nil, int num, char **values, char **names)
{
if ( num == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
image_names.insert(make_pair(values[1],atoi(values[0])));
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ImagePool::ImagePool( SqlDB * db,
const string& __source_prefix,
const string& __default_type,
const string& __default_dev_prefix):
ImagePool::ImagePool(SqlDB * db,
const string& __source_prefix,
const string& __default_type,
const string& __default_dev_prefix):
PoolSQL(db,Image::table)
{
ostringstream sql;
int rc;
ostringstream sql;
// Init static defaults
_source_prefix = __source_prefix;
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
_source_prefix = __source_prefix;
_default_type = __default_type;
_default_dev_prefix = __default_dev_prefix;
// Set default type
if (_default_type != "OS" &&
_default_type != "CDROM" &&
_default_type != "DATABLOCK" )
{
NebulaLog::log("IMG", Log::ERROR,
"Bad default for image type, setting OS");
NebulaLog::log("IMG", Log::ERROR, "Bad default for type, setting OS");
_default_type = "OS";
}
// Read from the DB the existing images, and build the ID:Name map
set_callback(static_cast<Callbackable::Callback>(&ImagePool::init_cb));
sql << "SELECT oid, name FROM " << Image::table;
rc = db->exec(sql, this);
unset_callback();
if ( rc != 0 )
{
NebulaLog::log("IMG", Log::ERROR,
"Could not load the existing images from the DB.");
}
}
/* -------------------------------------------------------------------------- */
@ -89,31 +58,43 @@ ImagePool::ImagePool( SqlDB * db,
int ImagePool::allocate (
int uid,
string user_name,
ImageTemplate* img_template,
int * oid,
string& error_str)
{
Image * img;
Image * img_aux;
string name;
// ---------------------------------------------------------------------
// Build a new Image object
// ---------------------------------------------------------------------
img = new Image(uid,img_template);
img = new Image(uid, user_name, img_template);
// ---------------------------------------------------------------------
// Check for duplicates
// ---------------------------------------------------------------------
img->get_template_attribute("NAME", name);
img_aux = get(name,uid,false);
// ---------------------------------------------------------------------
// Insert the Object in the pool
// ---------------------------------------------------------------------
*oid = PoolSQL::allocate(img, error_str);
// ---------------------------------------------------------------------
// Add the image name to the map of image_names
// ---------------------------------------------------------------------
if ( *oid != -1 )
if( img_aux != 0 )
{
image_names.insert(make_pair(name, *oid));
ostringstream oss;
oss << "NAME is already taken by IMAGE " << img_aux->get_oid() << ".";
error_str = oss.str();
*oid = -1;
delete img;
}
else
{
// ---------------------------------------------------------------------
// Insert the Object in the pool
// ---------------------------------------------------------------------
*oid = PoolSQL::allocate(img, error_str);
}
return *oid;
@ -122,54 +103,11 @@ int ImagePool::allocate (
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return Image::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<IMAGE_POOL>";
set_callback(static_cast<Callbackable::Callback>(&ImagePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT "<< Image::extended_db_names << ", user_pool.user_name FROM "
<< Image::table
<< " LEFT OUTER JOIN (SELECT oid, user_name FROM user_pool) "
<< "AS user_pool ON " << Image::table << ".uid = user_pool.oid";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</IMAGE_POOL>";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ImagePool::disk_attribute(VectorAttribute * disk,
int disk_id,
int * index,
Image::ImageType * img_type)
Image::ImageType * img_type,
int uid)
{
string source;
Image * img = 0;
@ -204,7 +142,7 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
}
else
{
img = get(source,true);
img = get(source,uid,true);
if (img == 0)
{
@ -255,7 +193,7 @@ int ImagePool::disk_attribute(VectorAttribute * disk,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
void ImagePool::authorize_disk(VectorAttribute * disk,int uid, AuthRequest * ar)
{
string source;
Image * img = 0;
@ -284,7 +222,7 @@ void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
}
else
{
img = get(source,true);
img = get(source,uid,true);
}
if (img == 0)
@ -293,7 +231,7 @@ void ImagePool::authorize_disk(VectorAttribute * disk, AuthRequest * ar)
}
ar->add_auth(AuthRequest::IMAGE,
img->get_iid(),
img->get_oid(),
AuthRequest::USE,
img->get_uid(),
img->isPublic());

View File

@ -24,6 +24,7 @@
using namespace std;
const int uids[] = {0,1,2};
const string user_names[] = {"A user","B user","C user"};
const string names[] = {"Image one", "Second Image", "The third image"};
@ -49,19 +50,19 @@ const string templates[] =
const string xmls[] =
{
"<IMAGE><ID>0</ID><UID>0</UID><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE>",
"<IMAGE><ID>0</ID><UID>2</UID><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
"<IMAGE><ID>0</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>2</ID><UID>2</UID><USERNAME>C user</USERNAME><NAME>The third image</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/e50b0c738be9d431475bf5859629e5580301a7d6</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><BUS><![CDATA[SCSI]]></BUS><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[The third image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH><PROFILE><![CDATA[STUDENT]]></PROFILE></TEMPLATE></IMAGE></IMAGE_POOL>";
const string xml_dump_where =
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
"<IMAGE_POOL><IMAGE><ID>0</ID><UID>0</UID><USERNAME>A user</USERNAME><NAME>Image one</NAME><TYPE>0</TYPE><PUBLIC>0</PUBLIC><PERSISTENT>1</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/9ab4a4e021ee2883f57e3aeecc9e2aed7c3fa198</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a very long description of an image, and to achieve the longness I will copy this over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over. This is a very long description of an image, and to achieve the longness I will copy this over. And over.This is a very long description of an image, and to achieve the longness I will copy this over.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Image one]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE><IMAGE><ID>1</ID><UID>1</UID><USERNAME>B user</USERNAME><NAME>Second Image</NAME><TYPE>0</TYPE><PUBLIC>1</PUBLIC><PERSISTENT>0</PERSISTENT><REGTIME>0000000000</REGTIME><SOURCE>source_prefix/c9d51800847467911c755e5e4c13dfe28c3a79f3</SOURCE><STATE>3</STATE><RUNNING_VMS>0</RUNNING_VMS><TEMPLATE><DESCRIPTION><![CDATA[This is a rather short description.]]></DESCRIPTION><DEV_PREFIX><![CDATA[hd]]></DEV_PREFIX><NAME><![CDATA[Second Image]]></NAME><ORIGINAL_PATH><![CDATA[/tmp/image_second_test]]></ORIGINAL_PATH></TEMPLATE></IMAGE></IMAGE_POOL>";
const string replacement = "0000000000";
@ -91,7 +92,7 @@ public:
if( rc == 0 )
{
return ImagePool::allocate(uid, img_template, oid, err);
return ImagePool::allocate(uid, user_names[uid], img_template, oid, err);
}
else
{
@ -167,33 +168,17 @@ protected:
((Image*)obj)->to_xml(xml_str);
xml_str.replace( xml_str.find("<REGTIME>")+9, 10, replacement);
//cout << endl << xml_str << endl << xmls[index] << endl;
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << xmls[index] << endl;
}
//*/
CPPUNIT_ASSERT( ((Image*)obj)->get_name() == names[index] );
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
void set_up_user_pool()
{
string err;
UserPool::bootstrap(db);
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
delete user_pool;
};
public:
ImagePoolTest(){xmlInitParser();};
@ -207,7 +192,7 @@ public:
ImagePool * imp;
Image * img;
// Allocate 2 users, so they are written to the DB.
// Allocate 2 images, so they are written to the DB.
allocate(0);
allocate(2);
@ -215,13 +200,13 @@ public:
// allocated images.
imp = new ImagePool(db, "source_prefix", "OS", "hd");
img = imp->get(names[0], false);
img = imp->get(names[0], uids[0], false);
CPPUNIT_ASSERT( img != 0 );
img = imp->get(names[1], false);
img = imp->get(names[1], uids[1], false);
CPPUNIT_ASSERT( img == 0 );
img = imp->get(names[2], false);
img = imp->get(names[2], uids[2], false);
CPPUNIT_ASSERT( img != 0 );
@ -312,7 +297,7 @@ public:
check(0, obj);
// Get using its name
obj = imp->get(names[1], true);
obj = imp->get(names[1], uids[1], true);
CPPUNIT_ASSERT( obj != 0 );
obj->unlock();
@ -324,7 +309,7 @@ public:
pool->clean();
// Get first object and check its integrity
obj = imp->get(names[0], false);
obj = imp->get(names[0], uids[0], false);
check(0, obj);
// Get using its name
@ -341,14 +326,14 @@ public:
// The pool is empty
// Non existing name
obj = imp->get("Wrong name", true);
obj = imp->get("Wrong name", 0, true);
CPPUNIT_ASSERT( obj == 0 );
// Allocate an object
allocate(0);
// Ask again for a non-existing name
obj = imp->get("Non existing name", true);
obj = imp->get("Non existing name",uids[0], true);
CPPUNIT_ASSERT( obj == 0 );
}
@ -370,9 +355,9 @@ public:
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( oid == rc );
// Try again, with different uid
// Try again, this time with different uid. Should be allowed
rc = imp->allocate(uids[1], templates[0], &oid);
CPPUNIT_ASSERT( rc == -1 );
CPPUNIT_ASSERT( rc >= 0 );
CPPUNIT_ASSERT( oid == rc );
}
@ -487,6 +472,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -508,6 +494,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -529,6 +516,7 @@ public:
CPPUNIT_ASSERT(oid >= 0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
img->enable(true);
img->disk_attribute(disk, &index, &img_type);
@ -557,6 +545,7 @@ public:
// Allocate an OS type image
oid = allocate(0);
img = imp->get(oid, false);
CPPUNIT_ASSERT( img != 0 );
// ---------------------------------------------------------------------
// A disk without a BUS attribute should not have it added.
@ -643,7 +632,7 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("IMAGE", "Image 0");
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0);
value = "";
value = disk->vector_value("TARGET");
@ -660,7 +649,7 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("IMAGE_ID", "1");
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type);
((ImagePool*)imp)->disk_attribute(disk, 0, &index, &img_type,0);
value = "";
value = disk->vector_value("TARGET");
@ -865,8 +854,6 @@ public:
int rc;
string nan;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -876,9 +863,16 @@ public:
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(1684, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
result.replace(1677, 10, replacement);
/*
if( result != xml_dump )
{
cout << endl << result << endl << xml_dump << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -894,8 +888,6 @@ public:
ostringstream oss;
ostringstream where;
set_up_user_pool();
allocate(0);
allocate(1);
allocate(2);
@ -906,8 +898,15 @@ public:
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
result.replace(164, 10, replacement);
result.replace(1154, 10, replacement);
result.replace(157, 10, replacement);
result.replace(1147, 10, replacement);
/*
if( result != xml_dump_where )
{
cout << endl << result << endl << xml_dump_where << endl;
}
//*/
CPPUNIT_ASSERT( result == xml_dump_where );
}
@ -921,7 +920,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, ImagePoolTest::suite());
}

View File

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

View File

@ -185,7 +185,7 @@ private:
if( rc == 0 )
{
return vmpool->allocate(uids[index], vm_template, &oid,
return vmpool->allocate(uids[index], "username", vm_template, &oid,
err, false);
}
else

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'
])

110
src/pool/PoolObjectSQL.cc Normal file
View File

@ -0,0 +1,110 @@
/* -------------------------------------------------------------------------- */
/* 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. */
/* -------------------------------------------------------------------------- */
#include "PoolObjectSQL.h"
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss, this);
unset_callback();
if ((rc != 0) || (oid != boid ))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::select(SqlDB *db, const string& _name, int _uid)
{
ostringstream oss;
int rc;
char * sql_name;
sql_name = db->escape_str(_name.c_str());
if ( sql_name == 0 )
{
return -1;
}
set_callback(
static_cast<Callbackable::Callback>(&PoolObjectSQL::select_cb));
oss << "SELECT body FROM " << table << " WHERE name = '" <<_name << "'";
if ( _uid != -1 )
{
oss << " AND uid = " << _uid;
}
name = "";
uid = -1;
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if ((rc != 0) || (_name != name) || (_uid != uid))
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolObjectSQL::drop(SqlDB *db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}

View File

@ -86,6 +86,7 @@ PoolSQL::~PoolSQL()
pthread_mutex_destroy(&mutex);
}
/* ************************************************************************** */
/* PoolSQL public interface */
/* ************************************************************************** */
@ -185,7 +186,82 @@ PoolObjectSQL * PoolSQL::get(
return 0;
}
string okey = key(objectsql->name,objectsql->uid);
pool.insert(make_pair(objectsql->oid,objectsql));
name_pool.insert(make_pair(okey, objectsql));
if ( olock == true )
{
objectsql->lock();
}
oid_queue.push(objectsql->oid);
if ( pool.size() > MAX_POOL_SIZE )
{
replace();
}
unlock();
return objectsql;
}
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
PoolObjectSQL * PoolSQL::get(const string& name, int ouid, bool olock)
{
map<string,PoolObjectSQL *>::iterator index;
PoolObjectSQL * objectsql;
int rc;
lock();
index = name_pool.find(key(name,ouid));
if ( index != name_pool.end() )
{
if ( index->second->isValid() == false )
{
objectsql = 0;
}
else
{
objectsql = index->second;
if ( olock == true )
{
objectsql->lock();
}
}
unlock();
return objectsql;
}
else
{
objectsql = create();
rc = objectsql->select(db,name,ouid);
if ( rc != 0 )
{
delete objectsql;
unlock();
return 0;
}
string okey = key(objectsql->name,objectsql->uid);
pool.insert(make_pair(objectsql->oid, objectsql));
name_pool.insert(make_pair(okey, objectsql));
if ( olock == true )
{
@ -236,10 +312,11 @@ void PoolSQL::replace()
}
else
{
PoolObjectSQL * tmp_ptr;
PoolObjectSQL * tmp_ptr = index->second;
string okey = key(tmp_ptr->name,tmp_ptr->uid);
tmp_ptr = index->second;
pool.erase(index);
name_pool.erase(okey);
delete tmp_ptr;
@ -266,12 +343,63 @@ void PoolSQL::clean()
}
pool.clear();
name_pool.clear();
unlock();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolSQL::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
if ( (!values[0]) || (num != 1) )
{
return -1;
}
*oss << values[0];
return 0;
}
/* -------------------------------------------------------------------------- */
int PoolSQL::dump(ostringstream& oss,
const string& elem_name,
const char * table,
const string& where)
{
int rc;
ostringstream cmd;
oss << "<" << elem_name << ">";
set_callback(static_cast<Callbackable::Callback>(&PoolSQL::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT body FROM " << table;
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
oss << "</" << elem_name << ">";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int PoolSQL:: search_cb(void * _oids, int num, char **values, char **names)
{
vector<int> * oids;
@ -309,3 +437,4 @@ int PoolSQL::search(
return rc;
}

View File

@ -22,7 +22,8 @@ lib_name='nebula_pool'
# Sources to generate the library
source_files=[
'PoolSQL.cc'
'PoolSQL.cc',
'PoolObjectSQL.cc'
]
# Build library

View File

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

View File

@ -32,10 +32,10 @@
const char * TestObjectSQL::table = "test_pool";
const char * TestObjectSQL::db_names = "(oid,number,text)";
const char * TestObjectSQL::db_names = "(oid,number,name)";
const char * TestObjectSQL::db_bootstrap = "CREATE TABLE test_pool ("
"oid INTEGER, number INTEGER, text TEXT, PRIMARY KEY(oid))";
"oid INTEGER, number INTEGER, name TEXT, PRIMARY KEY(oid))";
/* -------------------------------------------------------------------------- */

View File

@ -30,7 +30,7 @@ class TestObjectSQL : public PoolObjectSQL
{
public:
//OBJECT ATTRIBUTES
TestObjectSQL(int n=-1, string t="default"):number(n),text(t){};
TestObjectSQL(int n=-1, string t="default"):PoolObjectSQL(-1,"",0,0),number(n),text(t){};
~TestObjectSQL(){};
@ -71,6 +71,16 @@ public:
db->exec(oss,0);
};
string& to_xml(string& xml) const
{
return xml;
};
int from_xml(const string &xml_str)
{
return 0;
};
};
// THE POOL

View File

@ -141,7 +141,7 @@ public:
vector<int> results;
const char * table = "test_pool";
string where = "text = '" + stB + "'";
string where = "name = '" + stB + "'";
int ret;
ret = pool->search(results, table, where);
@ -205,4 +205,4 @@ public:
int main(int argc, char ** argv)
{
return OneUnitTest::main(argc, argv, PoolTest::suite());
}
}

View File

@ -29,6 +29,7 @@ void RequestManager::VirtualMachineAllocate::execute(
string session;
string str_template;
string error_str;
string user_name;
const string method_name = "VirtualMachineAllocate";
@ -41,6 +42,7 @@ void RequestManager::VirtualMachineAllocate::execute(
xmlrpc_c::value_array * arrayresult;
VirtualMachineTemplate * vm_template;
User * user;
char * error_msg = 0;
int num;
@ -92,7 +94,7 @@ void RequestManager::VirtualMachineAllocate::execute(
continue;
}
VirtualMachineAllocate::ipool->authorize_disk(vector,&ar);
VirtualMachineAllocate::ipool->authorize_disk(vector,uid,&ar);
}
num = vm_template->get("NIC",vectors);
@ -106,7 +108,7 @@ void RequestManager::VirtualMachineAllocate::execute(
continue;
}
VirtualMachineAllocate::vnpool->authorize_nic(vector,&ar);
VirtualMachineAllocate::vnpool->authorize_nic(vector,uid,&ar);
}
ar.add_auth(AuthRequest::VM,
@ -121,10 +123,26 @@ void RequestManager::VirtualMachineAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = VirtualMachineAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the VirtualMAchine
//--------------------------------------------------------------------------
rc = VirtualMachineAllocate::vmpool->allocate(uid,
user_name,
vm_template,
&vid,
error_str,
@ -145,6 +163,13 @@ void RequestManager::VirtualMachineAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete vm_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

View File

@ -107,7 +107,7 @@ void RequestManager::VirtualMachineDeploy::execute(
goto error_host_get;
}
hostname = host->get_hostname();
hostname = host->get_name();
vmm_mad = host->get_vmm_mad();
tm_mad = host->get_tm_mad();

View File

@ -31,8 +31,10 @@ void RequestManager::ImageAllocate::execute(
string session;
string str_template;
string error_str;
string user_name;
ImageTemplate * img_template;
ImageTemplate * img_template = 0;
User * user;
int iid;
int uid;
@ -96,10 +98,28 @@ void RequestManager::ImageAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = ImageAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the Image
//--------------------------------------------------------------------------
rc = ImageAllocate::ipool->allocate(uid,img_template,&iid, error_str);
rc = ImageAllocate::ipool->allocate(uid,user_name,
img_template,&iid, error_str);
if ( rc < 0 )
{
@ -118,6 +138,12 @@ void RequestManager::ImageAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete img_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

View File

@ -67,7 +67,7 @@ void RequestManager::VirtualMachineMigrate::execute(
goto error_host_get;
}
hostname = host->get_hostname();
hostname = host->get_name();
vmm_mad = host->get_vmm_mad();
tm_mad = host->get_tm_mad();

View File

@ -32,8 +32,6 @@ void RequestManager::VirtualMachinePoolInfo::execute(
int rc;
int state;
bool extended;
ostringstream oss;
ostringstream where_string;
@ -45,14 +43,15 @@ void RequestManager::VirtualMachinePoolInfo::execute(
NebulaLog::log("ReM",Log::DEBUG,"VirtualMachinePoolInfo method invoked");
// The extended information flag is not used, but for backwards
// compatibility, 2 or 4 arguments can be present.
switch (paramList.size())
{
case 2:
extended = true;
state = -1;
break;
case 4:
extended = xmlrpc_c::value_boolean(paramList.getBoolean(2));
// extended = xmlrpc_c::value_boolean(paramList.getBoolean(2));
state = xmlrpc_c::value_int (paramList.getInt(3));
break;
default:
@ -86,7 +85,7 @@ void RequestManager::VirtualMachinePoolInfo::execute(
where_string << "UID=" << filter_flag;
}
rc = VirtualMachinePoolInfo::vmpool->dump(oss, extended, state, where_string.str());
rc = VirtualMachinePoolInfo::vmpool->dump(oss, state, where_string.str());
if ( rc != 0 )
{

View File

@ -28,10 +28,12 @@ void RequestManager::VirtualNetworkAllocate::execute(
{
string session;
string name;
string user_name;
string str_template;
string error_str;
VirtualNetworkTemplate * vn_template;
User * user;
int nid;
int uid;
@ -96,10 +98,25 @@ void RequestManager::VirtualNetworkAllocate::execute(
}
}
//--------------------------------------------------------------------------
// Get the User Name
//--------------------------------------------------------------------------
user = VirtualNetworkAllocate::upool->get(uid,true);
if ( user == 0 )
{
goto error_user_get;
}
user_name = user->get_name();
user->unlock();
//--------------------------------------------------------------------------
// Allocate the Virtual Network
//--------------------------------------------------------------------------
rc = vnpool->allocate(uid,vn_template,&nid,error_str);
rc = vnpool->allocate(uid,user_name,vn_template,&nid,error_str);
if ( rc < 0 )
{
@ -118,6 +135,12 @@ void RequestManager::VirtualNetworkAllocate::execute(
return;
error_user_get:
oss.str(get_error(method_name, "USER", uid));
delete vn_template;
goto error_common;
error_authenticate:
oss.str(authenticate_error(method_name));
goto error_common;

View File

@ -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:

View File

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

View File

@ -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)

View File

@ -18,7 +18,7 @@ Import('sched_env')
# Libraries
sched_env.Prepend(LIBS=[
'scheduler_xml',
'nebula_xml',
'scheduler_pool',
'nebula_log',
'nebula_common',

View File

@ -31,7 +31,8 @@ sched_env.Prepend(LIBS=[
'scheduler_sched',
'scheduler_pool',
'nebula_log',
'scheduler_xml',
'scheduler_client',
'nebula_xml',
'nebula_common',
'crypto',
])

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

@ -641,7 +641,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO fwrite( template_text, template_leng, 1, template_out )
#define ECHO do { if (fwrite( template_text, template_leng, 1, template_out )) {} } while (0)
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -652,7 +652,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
int n; \
unsigned n; \
for ( n = 0; n < max_size && \
(c = getc( template_in )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \

View File

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

View File

@ -27,21 +27,13 @@
#include "User.h"
/* ************************************************************************** */
/* User :: Constructor/Destructor */
/* User :: Constructor/Destructor */
/* ************************************************************************** */
User::User(
int id,
string _username,
string _password,
bool _enabled):
PoolObjectSQL(id),
username (_username),
password (_password),
enabled (_enabled)
User::User(int id, string name, string pass, bool _enabled):
PoolObjectSQL(id,name,-1,table), password(pass), enabled(_enabled)
{};
User::~User(){};
/* ************************************************************************** */
@ -50,63 +42,10 @@ User::~User(){};
const char * User::table = "user_pool";
const char * User::db_names = "oid,user_name,password,enabled";
const char * User::db_names = "oid,name,body";
const char * User::db_bootstrap = "CREATE TABLE IF NOT EXISTS user_pool ("
"oid INTEGER PRIMARY KEY, user_name VARCHAR(256), password TEXT,"
"enabled INTEGER, UNIQUE(user_name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::select_cb(void *nil, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[USERNAME]) ||
(!values[PASSWORD]) ||
(!values[ENABLED]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
username = values[USERNAME];
password = values[PASSWORD];
enabled = (atoi(values[ENABLED])==0)?false:true;
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::select(SqlDB *db)
{
ostringstream oss;
int rc;
int boid;
set_callback(static_cast<Callbackable::Callback>(&User::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;
}
return 0;
}
"oid INTEGER PRIMARY KEY, name VARCHAR(256), body TEXT, UNIQUE(name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -129,48 +68,30 @@ int User::insert(SqlDB *db, string& error_str)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::update(SqlDB *db)
{
int rc;
rc = insert_replace(db, true);
if ( rc != 0 )
{
return rc;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_body;
char * sql_username;
char * sql_password;
int str_enabled = enabled?1:0;
char * sql_xml;
// Update the User
sql_username = db->escape_str(username.c_str());
sql_username = db->escape_str(name.c_str());
if ( sql_username == 0 )
{
goto error_username;
}
sql_password = db->escape_str(password.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_password == 0 )
if ( sql_xml == 0 )
{
goto error_password;
goto error_body;
}
// Construct the SQL statement to Insert or Replace
@ -184,69 +105,23 @@ int User::insert_replace(SqlDB *db, bool replace)
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< "'" << sql_username << "',"
<< "'" << sql_password << "',"
<< str_enabled << ")";
<< oid << ","
<< "'" << sql_username << "',"
<< "'" << sql_xml << "')";
rc = db->exec(oss);
db->free_str(sql_username);
db->free_str(sql_password);
db->free_str(sql_xml);
return rc;
error_password:
error_body:
db->free_str(sql_username);
error_username:
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[USERNAME]) ||
(!values[PASSWORD]) ||
(!values[ENABLED]) ||
(num != LIMIT))
{
return -1;
}
oss <<
"<USER>" <<
"<ID>" << values[OID] <<"</ID>" <<
"<NAME>" << values[USERNAME]<<"</NAME>" <<
"<PASSWORD>"<< values[PASSWORD]<<"</PASSWORD>"<<
"<ENABLED>" << values[ENABLED] <<"</ENABLED>" <<
"</USER>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int User::drop(SqlDB * db)
{
ostringstream oss;
int rc;
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
}
return rc;
}
/* ************************************************************************** */
/* User :: Misc */
/* ************************************************************************** */
@ -260,7 +135,6 @@ ostream& operator<<(ostream& os, User& user)
return os;
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -273,7 +147,7 @@ string& User::to_xml(string& xml) const
oss <<
"<USER>"
"<ID>" << oid <<"</ID>" <<
"<NAME>" << username <<"</NAME>" <<
"<NAME>" << name <<"</NAME>" <<
"<PASSWORD>" << password <<"</PASSWORD>" <<
"<ENABLED>" << enabled_int <<"</ENABLED>" <<
"</USER>";
@ -286,23 +160,28 @@ string& User::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& User::to_str(string& str) const
int User::from_xml(const string& xml)
{
ostringstream os;
int rc = 0;
int int_enabled;
string enabled_str = enabled?"True":"False";
// Initialize the internal XML object
update_from_str(xml);
os <<
"ID = " << oid << endl <<
"NAME = " << username << endl <<
"PASSWORD = " << password << endl <<
"ENABLED = " << enabled_str;
rc += xpath(oid, "/USER/ID", -1);
rc += xpath(name, "/USER/NAME", "not_found");
rc += xpath(password, "/USER/PASSWORD", "not_found");
rc += xpath(int_enabled, "/USER/ENABLED", 0);
str = os.str();
enabled = int_enabled;
return str;
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -31,103 +31,81 @@
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserPool::init_cb(void *nil, int num, char **values, char **names)
{
if ( num == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
known_users.insert(make_pair(values[1],atoi(values[0])));
return 0;
}
/* -------------------------------------------------------------------------- */
UserPool::UserPool(SqlDB * db):PoolSQL(db,User::table)
{
ostringstream sql;
int one_uid = -1;
ostringstream oss;
string one_token;
string one_name;
string one_pass;
string one_auth_file;
set_callback(static_cast<Callbackable::Callback>(&UserPool::init_cb));
const char * one_auth;
ifstream file;
sql << "SELECT oid,user_name FROM " << User::table;
db->exec(sql, this);
unset_callback();
if ((int) known_users.size() == 0)
if (get(0,false) != 0)
{
// User oneadmin needs to be added in the bootstrap
int one_uid = -1;
ostringstream oss;
string one_token;
string one_name;
string one_pass;
string one_auth_file;
return;
}
const char * one_auth;
ifstream file;
// User oneadmin needs to be added in the bootstrap
one_auth = getenv("ONE_AUTH");
one_auth = getenv("ONE_AUTH");
if (!one_auth)
{
struct passwd * pw_ent;
if (!one_auth)
pw_ent = getpwuid(getuid());
if ((pw_ent != NULL) && (pw_ent->pw_dir != NULL))
{
struct passwd * pw_ent;
one_auth_file = pw_ent->pw_dir;
one_auth_file += "/.one/one_auth";
pw_ent = getpwuid(getuid());
if ((pw_ent != NULL) && (pw_ent->pw_dir != NULL))
{
one_auth_file = pw_ent->pw_dir;
one_auth_file += "/.one/one_auth";
one_auth = one_auth_file.c_str();
}
else
{
oss << "Could not get one_auth file location";
}
}
file.open(one_auth);
if (file.good())
{
getline(file,one_token);
if (file.fail())
{
oss << "Error reading file: " << one_auth;
}
else
{
if (User::split_secret(one_token,one_name,one_pass) == 0)
{
string error_str;
string sha1_pass = User::sha1_digest(one_pass);
allocate(&one_uid, one_name, sha1_pass, true, error_str);
}
else
{
oss << "Wrong format must be <username>:<password>";
}
}
one_auth = one_auth_file.c_str();
}
else
{
oss << "Cloud not open file: " << one_auth;
oss << "Could not get one_auth file location";
}
}
file.close();
file.open(one_auth);
if (one_uid != 0)
if (file.good())
{
getline(file,one_token);
if (file.fail())
{
NebulaLog::log("ONE",Log::ERROR,oss);
throw;
oss << "Error reading file: " << one_auth;
}
else
{
if (User::split_secret(one_token,one_name,one_pass) == 0)
{
string error_str;
string sha1_pass = User::sha1_digest(one_pass);
allocate(&one_uid, one_name, sha1_pass, true, error_str);
}
else
{
oss << "Wrong format must be <username>:<password>";
}
}
}
else
{
oss << "Cloud not open file: " << one_auth;
}
file.close();
if (one_uid != 0)
{
NebulaLog::log("ONE",Log::ERROR,oss);
throw;
}
}
@ -143,21 +121,24 @@ int UserPool::allocate (
{
User * user;
// Build a new User object
user = get(username,false);
user = new User(-1,
username,
password,
enabled);
// Insert the Object in the pool
*oid = PoolSQL::allocate(user, error_str);
if (*oid != -1)
if ( user !=0)
{
// Add the user to the map of known_users
known_users.insert(make_pair(username,*oid));
ostringstream oss;
oss << "NAME is already taken by USER " << user->get_oid() << ".";
error_str = oss.str();
*oid = -1;
}
else
{
// Build a new User object
user = new User(-1, username, password, enabled);
// Insert the Object in the pool
*oid = PoolSQL::allocate(user, error_str);
}
return *oid;
@ -188,19 +169,12 @@ int UserPool::authenticate(string& session)
return -1;
}
index = known_users.find(username);
user = get(username,true);
if ( index != known_users.end() ) //User known to OpenNebula
if (user != 0) //User known to OpenNebula
{
user = get((int)index->second,true);
if ( user == 0 )
{
return -1;
}
u_pass = user->password;
uid = user->get_uid();
uid = user->oid;
user->unlock();
}
@ -319,42 +293,3 @@ int UserPool::authorize(AuthRequest& ar)
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int UserPool::dump_cb(void * _oss, int num, char **values, char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return User::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int UserPool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<USER_POOL>";
set_callback(static_cast<Callbackable::Callback>(&UserPool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << User::db_names << " FROM " << User::table;
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
rc = db->exec(cmd, this);
unset_callback();
oss << "</USER_POOL>";
return rc;
}

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

@ -100,16 +100,16 @@ protected:
{
CPPUNIT_ASSERT( obj != 0 );
string name = ((User*)obj)->get_username();
string name = ((User*)obj)->get_name();
CPPUNIT_ASSERT( name == usernames[index] );
CPPUNIT_ASSERT( ((User*)obj)->get_password() == passwords[index] );
};
public:
UserPoolTest(){};
UserPoolTest(){xmlInitParser();};
~UserPoolTest(){};
~UserPoolTest(){xmlCleanupParser();};
/* ********************************************************************* */
/* ********************************************************************* */
@ -149,8 +149,8 @@ public:
User* user = (User*) pool->get(0, false);
CPPUNIT_ASSERT(user != 0);
CPPUNIT_ASSERT( user->get_uid() == 0 );
CPPUNIT_ASSERT( user->get_username() == "one_user_test" );
CPPUNIT_ASSERT( user->get_oid() == 0 );
CPPUNIT_ASSERT( user->get_name() == "one_user_test" );
CPPUNIT_ASSERT( user->get_password() == User::sha1_digest("password") );
}
@ -287,6 +287,14 @@ public:
ostringstream oss;
((UserPool*)pool)->dump(oss, "");
/*
if( oss.str() != dump_result )
{
cout << endl << oss.str() << endl << "========"
<< endl << dump_result << endl << "--------";
}
//*/
CPPUNIT_ASSERT( oss.str() == dump_result );
}
@ -307,7 +315,15 @@ public:
// by" is a dirty fix (SQL injection, actually) because MySQL orders the
// results by user_name
ostringstream oss;
((UserPool*)pool)->dump(oss, "user_name LIKE 'a%' ORDER BY oid");
((UserPool*)pool)->dump(oss, "name LIKE 'a%' ORDER BY oid");
/*
if( oss.str() != dump_where_result )
{
cout << endl << oss.str() << endl << "========"
<< endl << dump_where_result << endl << "--------";
}
//*/
CPPUNIT_ASSERT( oss.str() == dump_where_result );
}

View File

@ -25,21 +25,10 @@
const char * History::table = "history";
const char * History::db_names = "vid,seq,host_name,vm_dir,hid,vm_mad,tm_mad,stime,"
"etime,pstime,petime,rstime,retime,estime,eetime,reason";
const char * History::extended_db_names =
"history.vid, history.seq, history.host_name, history.vm_dir, history.hid, "
"history.vm_mad, history.tm_mad, history.stime, history.etime, "
"history.pstime, history.petime, history.rstime, history.retime, "
"history.estime, history.eetime, history.reason";
const char * History::db_names = "vid, seq, body";
const char * History::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"history (vid INTEGER,"
"seq INTEGER,host_name TEXT,vm_dir TEXT,hid INTEGER,vm_mad TEXT,tm_mad TEXT,"
"stime INTEGER,etime INTEGER,pstime INTEGER,petime INTEGER,rstime INTEGER,"
"retime INTEGER,estime INTEGER,eetime INTEGER,reason INTEGER,"
"PRIMARY KEY(vid,seq))";
"history (vid INTEGER, seq INTEGER, body TEXT, PRIMARY KEY(vid,seq))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -47,6 +36,7 @@ const char * History::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
History::History(
int _oid,
int _seq):
ObjectXML(),
oid(_oid),
seq(_seq),
hostname(""),
@ -67,13 +57,13 @@ History::History(
/* -------------------------------------------------------------------------- */
History::History(
int _oid,
int _seq,
int _hid,
string& _hostname,
string& _vm_dir,
string& _vmm,
string& _tm):
int _oid,
int _seq,
int _hid,
const string& _hostname,
const string& _vm_dir,
const string& _vmm,
const string& _tm):
oid(_oid),
seq(_seq),
hostname(_hostname),
@ -141,72 +131,25 @@ void History::non_persistent_data()
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::insert(SqlDB * db, string& error_str)
{
int rc;
rc = insert_replace(db, false);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::update(SqlDB * db)
{
int rc;
rc = insert_replace(db, true);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_body;
char * sql_xml;
char * sql_hostname;
char * sql_vm_dir;
char * sql_vmm_mad_name;
char * sql_tm_mad_name;
int rc;
if (seq == -1)
{
return 0;
}
sql_hostname = db->escape_str(hostname.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_hostname == 0 )
if ( sql_xml == 0 )
{
goto error_hostname;
}
sql_vm_dir = db->escape_str(vm_dir.c_str());
if ( sql_vm_dir == 0 )
{
goto error_vm_dir;
}
sql_vmm_mad_name = db->escape_str(vmm_mad_name.c_str());
if ( sql_vmm_mad_name == 0 )
{
goto error_vmm;
}
sql_tm_mad_name = db->escape_str(tm_mad_name.c_str());
if ( sql_tm_mad_name == 0 )
{
goto error_tm;
goto error_body;
}
if(replace)
@ -218,40 +161,18 @@ int History::insert_replace(SqlDB *db, bool replace)
oss << "INSERT";
}
oss << " INTO " << table << " ("<< db_names <<") VALUES ("<<
oid << "," <<
seq << "," <<
"'" << sql_hostname << "',"<<
"'" << sql_vm_dir << "'," <<
hid << "," <<
"'" << sql_vmm_mad_name << "'," <<
"'" << sql_tm_mad_name << "'," <<
stime << "," <<
etime << "," <<
prolog_stime << "," <<
prolog_etime << "," <<
running_stime << "," <<
running_etime << "," <<
epilog_stime << "," <<
epilog_etime << "," <<
reason << ")";
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< seq << ","
<< "'" << sql_xml << "')";
rc = db->exec(oss);
db->free_str(sql_hostname);
db->free_str(sql_vm_dir);
db->free_str(sql_vmm_mad_name);
db->free_str(sql_tm_mad_name);
db->free_str(sql_xml);
return rc;
error_tm:
db->free_str(sql_vmm_mad_name);
error_vmm:
db->free_str(sql_vm_dir);
error_vm_dir:
db->free_str(sql_hostname);
error_hostname:
error_body:
return -1;
}
@ -260,99 +181,17 @@ error_hostname:
int History::select_cb(void *nil, int num, char **values, char **names)
{
if ((!values[VID]) ||
(!values[SEQ]) ||
(!values[HOSTNAME]) ||
(!values[VM_DIR]) ||
(!values[HID]) ||
(!values[VMMMAD]) ||
(!values[TMMAD]) ||
(!values[STIME]) ||
(!values[ETIME]) ||
(!values[PROLOG_STIME]) ||
(!values[PROLOG_ETIME]) ||
(!values[RUNNING_STIME]) ||
(!values[RUNNING_ETIME]) ||
(!values[EPILOG_STIME]) ||
(!values[EPILOG_ETIME]) ||
(!values[REASON]) ||
(num != LIMIT ))
if ( (!values[0]) || (num != 1) )
{
return -1;
}
oid = atoi(values[VID]);
seq = atoi(values[SEQ]);
hostname = values[HOSTNAME];
vm_dir = values[VM_DIR];
hid = atoi(values[HID]);
vmm_mad_name = values[VMMMAD];
tm_mad_name = values[TMMAD];
stime = static_cast<time_t>(atoi(values[STIME]));
etime = static_cast<time_t>(atoi(values[ETIME]));
prolog_stime = static_cast<time_t>(atoi(values[PROLOG_STIME]));
prolog_etime = static_cast<time_t>(atoi(values[PROLOG_ETIME]));
running_stime = static_cast<time_t>(atoi(values[RUNNING_STIME]));
running_etime = static_cast<time_t>(atoi(values[RUNNING_ETIME]));
epilog_stime = static_cast<time_t>(atoi(values[EPILOG_STIME]));
epilog_etime = static_cast<time_t>(atoi(values[EPILOG_ETIME]));
reason = static_cast<MigrationReason>(atoi(values[REASON]));
return 0;
return from_xml(values[0]);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::dump(ostringstream& oss, int num, char **values, char **names)
{
if ((!values[VID])||
(!values[SEQ])||
(!values[HOSTNAME])||
(!values[HID])||
(!values[STIME])||
(!values[ETIME])||
(!values[PROLOG_STIME])||
(!values[PROLOG_ETIME])||
(!values[RUNNING_STIME])||
(!values[RUNNING_ETIME])||
(!values[EPILOG_STIME])||
(!values[EPILOG_ETIME])||
(!values[REASON])||
(num != LIMIT))
{
return -1;
}
oss <<
"<HISTORY>" <<
"<SEQ>" << values[SEQ] << "</SEQ>" <<
"<HOSTNAME>"<< values[HOSTNAME] << "</HOSTNAME>"<<
"<HID>" << values[HID] << "</HID>" <<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<PSTIME>" << values[PROLOG_STIME] << "</PSTIME>" <<
"<PETIME>" << values[PROLOG_ETIME] << "</PETIME>" <<
"<RSTIME>" << values[RUNNING_STIME] << "</RSTIME>" <<
"<RETIME>" << values[RUNNING_ETIME] << "</RETIME>" <<
"<ESTIME>" << values[EPILOG_STIME] << "</ESTIME>" <<
"<EETIME>" << values[EPILOG_ETIME] << "</EETIME>" <<
"<REASON>" << values[REASON] << "</REASON>" <<
"</HISTORY>";
return 0;
}
/* -------------------------------------------------------------------------- */
int History::select(SqlDB * db)
{
ostringstream oss;
@ -365,12 +204,12 @@ int History::select(SqlDB * db)
if ( seq == -1)
{
oss << "SELECT " << db_names << " FROM history WHERE vid = "<< oid <<
oss << "SELECT body FROM history WHERE vid = "<< oid <<
" AND seq=(SELECT MAX(seq) FROM history WHERE vid = " << oid << ")";
}
else
{
oss << "SELECT " << db_names << " FROM history WHERE vid = " << oid
oss << "SELECT body FROM history WHERE vid = " << oid
<< " AND seq = " << seq;
}
@ -415,33 +254,6 @@ ostream& operator<<(ostream& os, const History& history)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& History::to_str(string& str) const
{
ostringstream oss;
oss<< "\tSEQ = " << seq << endl
<< "\tHOSTNAME = " << hostname << endl
<< "\tHID = " << hid << endl
<< "\tSTIME = " << stime << endl
<< "\tETIME = " << etime << endl
<< "\tPSTIME = " << prolog_stime << endl
<< "\tPETIME = " << prolog_etime << endl
<< "\tRSTIME = " << running_stime << endl
<< "\tRETIME = " << running_etime << endl
<< "\tESTIME = " << epilog_stime << endl
<< "\tEETIME = " << epilog_etime << endl
<< "\tREASON = " << reason;
str = oss.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& History::to_xml(string& xml) const
{
ostringstream oss;
@ -450,9 +262,12 @@ string& History::to_xml(string& xml) const
"<HISTORY>" <<
"<SEQ>" << seq << "</SEQ>" <<
"<HOSTNAME>"<< hostname << "</HOSTNAME>"<<
"<VM_DIR>" << vm_dir << "</VM_DIR>"<<
"<HID>" << hid << "</HID>" <<
"<STIME>" << stime << "</STIME>" <<
"<ETIME>" << etime << "</ETIME>" <<
"<VMMMAD>" << vmm_mad_name << "</VMMMAD>"<<
"<TMMAD>" << tm_mad_name << "</TMMAD>" <<
"<PSTIME>" << prolog_stime << "</PSTIME>"<<
"<PETIME>" << prolog_etime << "</PETIME>"<<
"<RSTIME>" << running_stime << "</RSTIME>"<<
@ -466,3 +281,40 @@ string& History::to_xml(string& xml) const
return xml;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int History::rebuild_attributes()
{
int int_reason;
int rc = 0;
rc += xpath(seq , "/HISTORY/SEQ", -1);
rc += xpath(hostname , "/HISTORY/HOSTNAME", "not_found");
rc += xpath(vm_dir , "/HISTORY/VM_DIR", "not_found");
rc += xpath(hid , "/HISTORY/HID", -1);
rc += xpath(stime , "/HISTORY/STIME", 0);
rc += xpath(etime , "/HISTORY/ETIME", 0);
rc += xpath(vmm_mad_name , "/HISTORY/VMMMAD", "not_found");
rc += xpath(tm_mad_name , "/HISTORY/TMMAD", "not_found");
rc += xpath(prolog_stime , "/HISTORY/PSTIME", 0);
rc += xpath(prolog_etime , "/HISTORY/PETIME", 0);
rc += xpath(running_stime, "/HISTORY/RSTIME", 0);
rc += xpath(running_etime, "/HISTORY/RETIME", 0);
rc += xpath(epilog_stime , "/HISTORY/ESTIME", 0);
rc += xpath(epilog_etime , "/HISTORY/EETIME", 0);
rc += xpath(int_reason , "/HISTORY/REASON", 0);
reason = static_cast<MigrationReason>(int_reason);
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

View File

@ -36,11 +36,13 @@
/* Virtual Machine :: Constructor/Destructor */
/* ************************************************************************** */
VirtualMachine::VirtualMachine(int id, VirtualMachineTemplate * _vm_template):
PoolObjectSQL(id),
uid(-1),
VirtualMachine::VirtualMachine(int id,
int _uid,
string _user_name,
VirtualMachineTemplate * _vm_template):
PoolObjectSQL(id,"",_uid,table),
user_name(_user_name),
last_poll(0),
name(""),
state(INIT),
lcm_state(LCM_INIT),
stime(time(0)),
@ -50,7 +52,6 @@ VirtualMachine::VirtualMachine(int id, VirtualMachineTemplate * _vm_template):
cpu(0),
net_tx(0),
net_rx(0),
last_seq(-1),
history(0),
previous_history(0),
_log(0)
@ -95,133 +96,52 @@ VirtualMachine::~VirtualMachine()
const char * VirtualMachine::table = "vm_pool";
const char * VirtualMachine::db_names =
"oid,uid,name,last_poll, state,lcm_state,stime,etime,deploy_id"
",memory,cpu,net_tx,net_rx,last_seq, template";
const char * VirtualMachine::extended_db_names =
"vm_pool.oid, vm_pool.uid, vm_pool.name, vm_pool.last_poll, vm_pool.state, "
"vm_pool.lcm_state, vm_pool.stime, vm_pool.etime, vm_pool.deploy_id, "
"vm_pool.memory, vm_pool.cpu, vm_pool.net_tx, vm_pool.net_rx, "
"vm_pool.last_seq, vm_pool.template";
"oid, name, body, uid, last_poll, state, lcm_state";
const char * VirtualMachine::db_bootstrap = "CREATE TABLE IF NOT EXISTS "
"vm_pool ("
"oid INTEGER PRIMARY KEY,uid INTEGER,name TEXT,"
"last_poll INTEGER, state INTEGER,lcm_state INTEGER,"
"stime INTEGER,etime INTEGER,deploy_id TEXT,memory INTEGER,cpu INTEGER,"
"net_tx INTEGER,net_rx INTEGER, last_seq INTEGER, template TEXT)";
"vm_pool (oid INTEGER PRIMARY KEY, name TEXT, body TEXT, uid INTEGER, "
"last_poll INTEGER, state INTEGER, lcm_state INTEGER)";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::select_cb(void *nil, int num, char **values, char **names)
{
if ((values[OID] == 0) ||
(values[UID] == 0) ||
(values[NAME] == 0) ||
(values[LAST_POLL] == 0) ||
(values[STATE] == 0) ||
(values[LCM_STATE] == 0) ||
(values[STIME] == 0) ||
(values[ETIME] == 0) ||
(values[MEMORY] == 0) ||
(values[CPU] == 0) ||
(values[NET_TX] == 0) ||
(values[NET_RX] == 0) ||
(values[LAST_SEQ] == 0) ||
(values[TEMPLATE] == 0) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
last_poll = static_cast<time_t>(atoi(values[LAST_POLL]));
state = static_cast<VmState>(atoi(values[STATE]));
lcm_state = static_cast<LcmState>(atoi(values[LCM_STATE]));
stime = static_cast<time_t>(atoi(values[STIME]));
etime = static_cast<time_t>(atoi(values[ETIME]));
if ( values[DEPLOY_ID] != 0 )
{
deploy_id = values[DEPLOY_ID];
}
memory = atoi(values[MEMORY]);
cpu = atoi(values[CPU]);
net_tx = atoi(values[NET_TX]);
net_rx = atoi(values[NET_RX]);
last_seq = atoi(values[LAST_SEQ]);
// Virtual Machine template
vm_template->from_xml(values[TEMPLATE]);
return 0;
}
/* -------------------------------------------------------------------------- */
int VirtualMachine::select(SqlDB * db)
{
ostringstream oss;
ostringstream ose;
int rc;
int boid;
int last_seq;
string filename;
Nebula& nd = Nebula::instance();
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachine::select_cb));
// Rebuld the VirtualMachine object
rc = PoolObjectSQL::select(db);
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 ))
if( rc != 0 )
{
goto error_id;
return rc;
}
//Get the History Records
if ( last_seq != -1 )
//Get History Records. Current history is record is built in from_xml() (if any).
if( hasHistory() )
{
history = new History(oid, last_seq);
last_seq = history->seq;
rc = history->select(db);
if (rc != 0)
if ( last_seq > 0 )
{
goto error_history;
}
}
previous_history = new History(oid, last_seq - 1);
if ( last_seq > 0 )
{
previous_history = new History(oid, last_seq - 1);
rc = previous_history->select(db);
rc = previous_history->select(db);
if ( rc != 0)
{
goto error_previous_history;
if ( rc != 0)
{
goto error_previous_history;
}
}
}
//Create support directory for this VM
oss.str("");
oss << nd.get_var_location() << oid;
@ -229,7 +149,6 @@ int VirtualMachine::select(SqlDB * db)
chmod(oss.str().c_str(), 0777);
//Create Log support for this VM
try
{
_log = new FileLog(nd.get_vm_log_filename(oid),Log::DEBUG);
@ -244,16 +163,6 @@ int VirtualMachine::select(SqlDB * db)
return 0;
error_id:
ose << "Error getting VM id: " << oid;
log("VMM", Log::ERROR, ose);
return -1;
error_history:
ose << "Can not get history for VM id: " << oid;
log("ONE", Log::ERROR, ose);
return -1;
error_previous_history:
ose << "Can not get previous history record (seq:" << history->seq
<< ") for VM id: " << oid;
@ -554,23 +463,15 @@ int VirtualMachine::parse_requirements()
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int VirtualMachine::update(SqlDB * db)
{
return insert_replace(db, true);
}
/* ------------------------------------------------------------------------ */
/* ------------------------------------------------------------------------ */
int VirtualMachine::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_deploy_id;
char * sql_name;
char * sql_template;
char * sql_xml;
sql_deploy_id = db->escape_str(deploy_id.c_str());
@ -586,15 +487,13 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
vm_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_template == 0 )
if ( sql_xml == 0 )
{
goto error_template;
goto error_body;
}
if(replace)
{
oss << "REPLACE";
@ -606,30 +505,23 @@ int VirtualMachine::insert_replace(SqlDB *db, bool replace)
oss << " INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< "'" << sql_xml << "',"
<< uid << ","
<< last_poll << ","
<< state << ","
<< lcm_state << ","
<< stime << ","
<< etime << ","
<< "'" << sql_deploy_id << "',"
<< memory << ","
<< cpu << ","
<< net_tx << ","
<< net_rx << ","
<< last_seq << ","
<< "'" << sql_template << "')";
<< lcm_state << ")";
db->free_str(sql_deploy_id);
db->free_str(sql_name);
db->free_str(sql_template);
db->free_str(sql_xml);
rc = db->exec(oss);
return rc;
error_template:
error_body:
db->free_str(sql_name);
error_name:
db->free_str(sql_deploy_id);
@ -640,116 +532,12 @@ error_deploy:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump( ostringstream& oss,
int num,
char **values,
char **names)
{
if ((!values[OID])||
(!values[UID])||
(!values[NAME]) ||
(!values[LAST_POLL])||
(!values[STATE])||
(!values[LCM_STATE])||
(!values[STIME])||
(!values[ETIME])||
(!values[DEPLOY_ID])||
(!values[MEMORY])||
(!values[CPU])||
(!values[NET_TX])||
(!values[NET_RX])||
(!values[LAST_SEQ])||
(!values[TEMPLATE])||
(num != LIMIT))
{
return -1;
}
oss <<
"<VM>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<<
"<STATE>" << values[STATE] << "</STATE>" <<
"<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<DEPLOY_ID>"<< values[DEPLOY_ID]<< "</DEPLOY_ID>"<<
"<MEMORY>" << values[MEMORY] << "</MEMORY>" <<
"<CPU>" << values[CPU] << "</CPU>" <<
"<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>" <<
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>" <<
values[TEMPLATE];
oss << "</VM>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachine::dump_extended( ostringstream& oss,
int num,
char **values,
char **names)
{
if ((!values[OID])||
(!values[UID])||
(!values[NAME]) ||
(!values[LAST_POLL])||
(!values[STATE])||
(!values[LCM_STATE])||
(!values[STIME])||
(!values[ETIME])||
(!values[DEPLOY_ID])||
(!values[MEMORY])||
(!values[CPU])||
(!values[NET_TX])||
(!values[NET_RX])||
(!values[LAST_SEQ])||
(!values[TEMPLATE])||
(num != (LIMIT + History::LIMIT + 1)))
{
return -1;
}
oss <<
"<VM>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT] << "</USERNAME>"<<
"<NAME>" << values[NAME] << "</NAME>" <<
"<LAST_POLL>"<< values[LAST_POLL]<< "</LAST_POLL>"<<
"<STATE>" << values[STATE] << "</STATE>" <<
"<LCM_STATE>"<< values[LCM_STATE]<< "</LCM_STATE>"<<
"<STIME>" << values[STIME] << "</STIME>" <<
"<ETIME>" << values[ETIME] << "</ETIME>" <<
"<DEPLOY_ID>"<< values[DEPLOY_ID]<< "</DEPLOY_ID>"<<
"<MEMORY>" << values[MEMORY] << "</MEMORY>" <<
"<CPU>" << values[CPU] << "</CPU>" <<
"<NET_TX>" << values[NET_TX] << "</NET_TX>" <<
"<NET_RX>" << values[NET_RX] << "</NET_RX>" <<
"<LAST_SEQ>" << values[LAST_SEQ] << "</LAST_SEQ>" <<
values[TEMPLATE];
History::dump(oss, num-LIMIT-1, values+LIMIT+1, names+LIMIT+1);
oss << "</VM>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualMachine::add_history(
int hid,
string& hostname,
string& vm_dir,
string& vmm_mad,
string& tm_mad)
int hid,
const string& hostname,
const string& vm_dir,
const string& vmm_mad,
const string& tm_mad)
{
ostringstream os;
int seq;
@ -770,8 +558,6 @@ void VirtualMachine::add_history(
previous_history = history;
}
last_seq = seq;
history = new History(oid,seq,hid,hostname,vm_dir,vmm_mad,tm_mad);
};
@ -803,8 +589,6 @@ void VirtualMachine::cp_history()
previous_history = history;
history = htmp;
last_seq = history->seq;
}
/* -------------------------------------------------------------------------- */
@ -832,8 +616,6 @@ void VirtualMachine::cp_previous_history()
previous_history = history;
history = htmp;
last_seq = history->seq;
}
/* -------------------------------------------------------------------------- */
@ -898,7 +680,7 @@ int VirtualMachine::get_disk_images(string& error_str)
continue;
}
rc = ipool->disk_attribute(disk, i, &index, &img_type);
rc = ipool->disk_attribute(disk, i, &index, &img_type, uid);
if (rc == 0 )
{
@ -1041,7 +823,7 @@ int VirtualMachine::get_network_leases()
continue;
}
rc = vnpool->nic_attribute(nic, oid);
rc = vnpool->nic_attribute(nic, uid, oid);
if (rc == -1)
{
@ -1304,6 +1086,7 @@ string& VirtualMachine::to_xml(string& xml) const
oss << "<VM>"
<< "<ID>" << oid << "</ID>"
<< "<UID>" << uid << "</UID>"
<< "<USERNAME>" << user_name << "</USERNAME>"
<< "<NAME>" << name << "</NAME>"
<< "<LAST_POLL>" << last_poll << "</LAST_POLL>"
<< "<STATE>" << state << "</STATE>"
@ -1315,7 +1098,6 @@ string& VirtualMachine::to_xml(string& xml) const
<< "<CPU>" << cpu << "</CPU>"
<< "<NET_TX>" << net_tx << "</NET_TX>"
<< "<NET_RX>" << net_rx << "</NET_RX>"
<< "<LAST_SEQ>" << last_seq << "</LAST_SEQ>"
<< vm_template->to_xml(template_xml);
if ( hasHistory() )
@ -1330,39 +1112,66 @@ string& VirtualMachine::to_xml(string& xml) const
return xml;
}
/* -------------------------------------------------------------------------- */
string& VirtualMachine::to_str(string& str) const
int VirtualMachine::from_xml(const string &xml_str)
{
string template_str;
string history_str;
vector<xmlNodePtr> content;
ostringstream oss;
int int_state;
int int_lcmstate;
int rc = 0;
oss<< "ID : " << oid << endl
<< "UID : " << uid << endl
<< "NAME : " << name << endl
<< "STATE : " << state << endl
<< "LCM STATE : " << lcm_state << endl
<< "DEPLOY ID : " << deploy_id << endl
<< "MEMORY : " << memory << endl
<< "CPU : " << cpu << endl
<< "LAST POLL : " << last_poll << endl
<< "START TIME : " << stime << endl
<< "STOP TIME : " << etime << endl
<< "NET TX : " << net_tx << endl
<< "NET RX : " << net_rx << endl
<< "LAST SEQ : " << last_seq << endl
<< "Template" << endl << vm_template->to_str(template_str) << endl;
// Initialize the internal XML object
update_from_str(xml_str);
if ( hasHistory() )
// Get class base attributes
rc += xpath(oid, "/VM/ID", -1);
rc += xpath(uid, "/VM/UID", -1);
rc += xpath(user_name, "/VM/USERNAME", "not_found");
rc += xpath(name, "/VM/NAME", "not_found");
rc += xpath(last_poll, "/VM/LAST_POLL",0);
rc += xpath(int_state, "/VM/STATE", 0);
rc += xpath(int_lcmstate,"/VM/LCM_STATE", 0);
rc += xpath(stime, "/VM/STIME", 0);
rc += xpath(etime, "/VM/ETIME", 0);
rc += xpath(deploy_id, "/VM/DEPLOY_ID","");
rc += xpath(memory, "/VM/MEMORY", 0);
rc += xpath(cpu, "/VM/CPU", 0);
rc += xpath(net_tx, "/VM/NET_TX", 0);
rc += xpath(net_rx, "/VM/NET_RX", 0);
state = static_cast<VmState>( int_state );
lcm_state = static_cast<LcmState>( int_lcmstate );
// Get associated classes
ObjectXML::get_nodes("/VM/TEMPLATE", content);
if( content.size() < 1 )
{
oss << "Last History Record" << endl << history->to_str(history_str);
return -1;
}
str = oss.str();
// Virtual Machine template
rc += vm_template->from_xml_node(content[0]);
return str;
// Last history entry
content.clear();
ObjectXML::get_nodes("/VM/HISTORY", content);
if( !content.empty() )
{
history = new History(oid);
rc += history->from_xml_node(content[0]);
}
if (rc != 0)
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */

View File

@ -167,6 +167,7 @@ VirtualMachinePool::VirtualMachinePool(SqlDB * db,
int VirtualMachinePool::allocate (
int uid,
string user_name,
VirtualMachineTemplate * vm_template,
int * oid,
string& error_str,
@ -177,7 +178,7 @@ int VirtualMachinePool::allocate (
// ------------------------------------------------------------------------
// Build a new Virtual Machine object
// ------------------------------------------------------------------------
vm = new VirtualMachine(-1,vm_template);
vm = new VirtualMachine(-1, uid, user_name, vm_template);
if (on_hold == true)
{
@ -188,8 +189,6 @@ int VirtualMachinePool::allocate (
vm->state = VirtualMachine::PENDING;
}
vm->uid = uid;
// ------------------------------------------------------------------------
// Insert the Object in the pool
// ------------------------------------------------------------------------
@ -240,84 +239,27 @@ int VirtualMachinePool::get_pending(
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump_cb(void * _oss,int num,char **values,char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualMachine::dump(*oss, num, values, names);
}
int VirtualMachinePool::dump_extended_cb(
void * _oss,int num,char **values,char **names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualMachine::dump_extended(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int VirtualMachinePool::dump( ostringstream& oss,
bool extended,
int state,
const string& where)
{
int rc;
ostringstream cmd;
oss << "<VM_POOL>";
if(extended)
{
set_callback(
static_cast<Callbackable::Callback>(
&VirtualMachinePool::dump_extended_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::extended_db_names
<< ", user_pool.user_name, "
<< History::extended_db_names << " FROM " << VirtualMachine::table
<< " LEFT OUTER JOIN " << History::table << " ON "
<< VirtualMachine::table << ".oid = " << History::table << ".vid AND "
<< History::table << ".seq = " << VirtualMachine::table
<< ".last_seq LEFT OUTER JOIN (SELECT oid,user_name FROM user_pool) "
<< "AS user_pool ON "<< VirtualMachine::table << ".uid = user_pool.oid";
}
else
{
set_callback(
static_cast<Callbackable::Callback>(&VirtualMachinePool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualMachine::db_names << " FROM "
<< VirtualMachine::table;
}
ostringstream where_oss;
if ( state != -1 )
{
cmd << " WHERE " << VirtualMachine::table << ".state = " << state;
where_oss << VirtualMachine::table << ".state = " << state;
}
else
{
cmd << " WHERE " << VirtualMachine::table << ".state <> 6";
where_oss << VirtualMachine::table << ".state <> 6";
}
if ( !where.empty() )
{
cmd << " AND " << where;
where_oss << " AND " << where;
}
rc = db->exec(cmd,this);
oss << "</VM_POOL>";
unset_callback();
return rc;
return PoolSQL::dump(oss, "VM_POOL", VirtualMachine::table,where_oss.str());
}
/* -------------------------------------------------------------------------- */

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

@ -25,6 +25,7 @@
using namespace std;
const int uids[] = {123, 261, 123};
const string user_names[] = {"A user","B user","C user"};
const string names[] = {"VM one", "Second VM", "VM one"};
@ -46,38 +47,37 @@ const string templates[] =
const string xmls[] =
{
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"<VM><ID>0</ID><UID>123</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]"
"><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]"
"]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID>"
"</TEMPLATE></VM>",
"<VM><ID>1</ID><UID>261</UID><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL>"
"<VM><ID>1</ID><UID>261</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL>"
"<STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0<"
"/ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET"
"_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY>"
"_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY>"
"<![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID>"
"<![CDATA[1]]></VMID></TEMPLATE></VM>",
"<VM><ID>0</ID><UID>123</UID><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"<VM><ID>0</ID><UID>123</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><ST"
"ATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ET"
"IME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX"
"><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU>1</CPU><MEMORY>1024</MEMORY><NAME>VM one"
"><NET_RX>0</NET_RX><TEMPLATE><CPU>1</CPU><MEMORY>1024</MEMORY><NAME>VM one"
"</NAME><VMID>0</VMID></TEMPLATE></VM>"
};
// This xml dump result has the STIMEs modified to 0000000000
const string xml_dump =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>2</UID><USERNAME>B user</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_dump_where =
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>1</UID><USERNAME>A user</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM></VM_POOL>";
const string xml_history_dump =
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>-1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>0</LAST_SEQ><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><LAST_SEQ>1</LAST_SEQ><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[1024]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[2]]></VMID></TEMPLATE><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
"<VM_POOL><VM><ID>0</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>1</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[128]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[0]]></VMID></TEMPLATE></VM><VM><ID>1</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>Second VM</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[2]]></CPU><MEMORY><![CDATA[256]]></MEMORY><NAME><![CDATA[Second VM]]></NAME><VMID><![CDATA[1]]></VMID></TEMPLATE><HISTORY><SEQ>0</SEQ><HOSTNAME>A_hostname</HOSTNAME><VM_DIR>A_vm_dir</VM_DIR><HID>0</HID><STIME>0</STIME><ETIME>0</ETIME><VMMMAD>A_vmm_mad</VMMMAD><TMMAD>A_tm_mad</TMMAD><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM><VM><ID>2</ID><UID>0</UID><USERNAME>one_user_test</USERNAME><NAME>VM one</NAME><LAST_POLL>0</LAST_POLL><STATE>2</STATE><LCM_STATE>0</LCM_STATE><STIME>0000000000</STIME><ETIME>0</ETIME><DEPLOY_ID></DEPLOY_ID><MEMORY>0</MEMORY><CPU>0</CPU><NET_TX>0</NET_TX><NET_RX>0</NET_RX><TEMPLATE><CPU><![CDATA[1]]></CPU><MEMORY><![CDATA[1024]]></MEMORY><NAME><![CDATA[VM one]]></NAME><VMID><![CDATA[2]]></VMID></TEMPLATE><HISTORY><SEQ>1</SEQ><HOSTNAME>C_hostname</HOSTNAME><VM_DIR>C_vm_dir</VM_DIR><HID>2</HID><STIME>0</STIME><ETIME>0</ETIME><VMMMAD>C_vmm_mad</VMMMAD><TMMAD>C_tm_mad</TMMAD><PSTIME>0</PSTIME><PETIME>0</PETIME><RSTIME>0</RSTIME><RETIME>0</RETIME><ESTIME>0</ESTIME><EETIME>0</EETIME><REASON>0</REASON></HISTORY></VM></VM_POOL>";
const string replacement = "0000000000";
@ -94,6 +94,7 @@ public:
int allocate (
int uid,
const string& user_name,
const string& stemplate,
int * oid,
bool on_hold = false)
@ -108,8 +109,8 @@ public:
if( rc == 0 )
{
return VirtualMachinePool::allocate(uid, vm_template, oid,
err, on_hold);
return VirtualMachinePool::allocate(uid, user_name,
vm_template, oid, err, on_hold);
}
else
{
@ -158,6 +159,7 @@ protected:
{
int oid;
return ((VirtualMachinePoolFriend*)pool)->allocate( uids[index],
user_names[index],
templates[index],
&oid, false);
};
@ -171,31 +173,19 @@ protected:
// Get the xml and replace the STIME to 0, so we can compare it
((VirtualMachine*)obj)->to_xml(xml_str);
xml_str.replace( xml_str.find("<STIME>")+7, 10, replacement);
//cout << endl << xml_str << endl;
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index] << endl << "--------";
}
//*/
CPPUNIT_ASSERT( ((VirtualMachine*)obj)->get_name() == names[index] );
CPPUNIT_ASSERT( xml_str == xmls[index]);
};
void set_up_user_pool()
{
string err;
UserPool::bootstrap(db);
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
delete user_pool;
};
public:
VirtualMachinePoolTest(){xmlInitParser();};
@ -263,20 +253,26 @@ public:
VirtualMachinePoolFriend * vmp =
static_cast<VirtualMachinePoolFriend*>(pool);
set_up_user_pool();
ostringstream oss;
int oid, rc;
vmp->allocate(1, templates[0], &oid, false);
vmp->allocate(2, templates[1], &oid, true);
vmp->allocate(1, user_names[0], templates[0], &oid, false);
vmp->allocate(2, user_names[1], templates[1], &oid, true);
rc = vmp->dump(oss, "");
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
result.replace(152, 10, replacement);
result.replace(583, 10, replacement);
result.replace(560, 10, replacement);
/*
if( result != xml_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_dump << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
}
@ -286,14 +282,12 @@ public:
VirtualMachinePoolFriend * vmp =
static_cast<VirtualMachinePoolFriend*>(pool);
set_up_user_pool();
int oid, rc;
ostringstream oss;
ostringstream where;
vmp->allocate(1, templates[0], &oid, false);
vmp->allocate(2, templates[1], &oid, true);
vmp->allocate(1, user_names[0], templates[0], &oid, false);
vmp->allocate(2, user_names[1], templates[1], &oid, true);
where << "uid < 2";
rc = vmp->dump(oss, where.str());
@ -320,16 +314,14 @@ public:
ostringstream where;
set_up_user_pool();
// Allocate a VM
rc = vmp->allocate(0, templates[0], &oid, false);
rc = vmp->allocate(0, "one_user_test", templates[0], &oid, false);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
//----------------------------------------------------------------------
// Allocate a VM with one history item
rc = vmp->allocate(0, templates[1], &oid, true);
rc = vmp->allocate(0, "one_user_test", templates[1], &oid, true);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -347,7 +339,7 @@ public:
//----------------------------------------------------------------------
// Allocate a VM with two history items
rc = vmp->allocate(0, templates[2], &oid, true);
rc = vmp->allocate(0, "one_user_test", templates[2], &oid, true);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -374,7 +366,7 @@ public:
//----------------------------------------------------------------------
// Allocate a VM, will be set to DONE
rc = vmp->allocate(1, templates[0], &oid, false);
rc = vmp->allocate(1, "one_user_test", templates[0], &oid, false);
CPPUNIT_ASSERT( rc == oid );
CPPUNIT_ASSERT( oid >= 0 );
@ -398,8 +390,16 @@ public:
string result = oss.str();
result.replace(159, 10, replacement);
result.replace(597, 10, replacement);
result.replace(1266,10, replacement);
result.replace(574, 10, replacement);
result.replace(1295,10, replacement);
/*
if( result != xml_history_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_history_dump << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_history_dump );
}
@ -472,7 +472,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, VirtualMachinePoolTest::suite());
}

View File

@ -620,7 +620,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO fwrite( vm_var_text, vm_var_leng, 1, vm_var_out )
#define ECHO do { if (fwrite( vm_var_text, vm_var_leng, 1, vm_var_out )) {} } while (0)
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -631,7 +631,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
int n; \
unsigned n; \
for ( n = 0; n < max_size && \
(c = getc( vm_var_in )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton implementation for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -45,7 +45,7 @@
#define YYBISON 1
/* Bison version. */
#define YYBISON_VERSION "2.4.2"
#define YYBISON_VERSION "2.4.3"
/* Skeleton name. */
#define YYSKELETON_NAME "yacc.c"
@ -180,7 +180,7 @@ void get_network_attribute(VirtualMachine * vm,
return;
}
vn = vnpool->get(network,true);
vn = vnpool->get(network, vm->get_uid(), true);
if ( vn == 0 )
{
@ -1374,7 +1374,7 @@ YYLTYPE yylloc;
YYLTYPE *yylsp;
/* The locations where the error started and ended. */
YYLTYPE yyerror_range[2];
YYLTYPE yyerror_range[3];
YYSIZE_T yystacksize;
@ -1754,7 +1754,7 @@ yyerrlab:
#endif
}
yyerror_range[0] = yylloc;
yyerror_range[1] = yylloc;
if (yyerrstatus == 3)
{
@ -1791,7 +1791,7 @@ yyerrorlab:
if (/*CONSTCOND*/ 0)
goto yyerrorlab;
yyerror_range[0] = yylsp[1-yylen];
yyerror_range[1] = yylsp[1-yylen];
/* Do not reclaim the symbols of the rule which action triggered
this YYERROR. */
YYPOPSTACK (yylen);
@ -1825,7 +1825,7 @@ yyerrlab1:
if (yyssp == yyss)
YYABORT;
yyerror_range[0] = *yylsp;
yyerror_range[1] = *yylsp;
yydestruct ("Error: popping",
yystos[yystate], yyvsp, yylsp, mc, vm, parsed, errmsg);
YYPOPSTACK (1);
@ -1835,10 +1835,10 @@ yyerrlab1:
*++yyvsp = yylval;
yyerror_range[1] = yylloc;
yyerror_range[2] = yylloc;
/* Using YYLLOC is tempting, but would change the location of
the lookahead. YYLOC is available though. */
YYLLOC_DEFAULT (yyloc, (yyerror_range - 1), 2);
YYLLOC_DEFAULT (yyloc, yyerror_range, 2);
*++yylsp = yyloc;
/* Shift the error token. */

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@ -118,7 +118,7 @@ void get_network_attribute(VirtualMachine * vm,
return;
}
vn = vnpool->get(network,true);
vn = vnpool->get(network, vm->get_uid(), true);
if ( vn == 0 )
{

View File

@ -62,6 +62,10 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
unsigned int _ip;
unsigned int _mac [2];
Lease * lease;
string xml_body;
char * sql_xml;
int rc;
if ( Leases::Lease::ip_to_number(ip,_ip) )
@ -84,13 +88,21 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
goto error_mac;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES (" <<
oid << "," <<
_ip << "," <<
_mac[Lease::PREFIX] << "," <<
_mac[Lease::SUFFIX] << "," <<
vid << "," <<
used << ")";
lease = new Lease(_ip,_mac,vid,used);
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
goto error_body;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< _ip << ","
<< "'" << sql_xml << "')";
db->free_str(sql_xml);
rc = db->exec(oss);
@ -99,9 +111,14 @@ int FixedLeases::add(const string& ip, const string& mac, int vid,
goto error_db;
}
leases.insert(make_pair(_ip,new Lease(_ip,_mac,vid,used)));
return rc;
leases.insert( make_pair(_ip,lease) );
if(lease->used)
{
n_used++;
}
return rc;
error_ip:
oss.str("");
@ -118,9 +135,16 @@ error_duplicate:
oss << "Error inserting lease, IP " << ip << " already exists";
goto error_common;
error_body:
oss.str("");
oss << "Error inserting lease, marshall error";
delete lease;
goto error_common;
error_db:
oss.str("");
oss << "Error inserting lease in database.";
delete lease;
error_common:
NebulaLog::log("VNM", Log::ERROR, oss);
@ -167,7 +191,10 @@ int FixedLeases::remove(const string& ip, string& error_msg)
goto error_db;
}
delete it->second;
leases.erase(it);
return rc;
@ -202,7 +229,6 @@ error_common:
int FixedLeases::unset(const string& ip)
{
unsigned int _ip;
ostringstream oss;
map<unsigned int, Lease *>::iterator it_ip;
@ -219,14 +245,11 @@ int FixedLeases::unset(const string& ip)
}
// Flip used flag to false
it_ip->second->used = false;
it_ip->second->vid = -1;
oss << "UPDATE " << table << " SET used='0', vid='-1' "
<< " WHERE oid=" << oid << " AND ip='" << _ip <<"'";
return db->exec(oss);
// Update the lease
return update_lease(it_ip->second);
}
/* -------------------------------------------------------------------------- */
@ -234,7 +257,7 @@ int FixedLeases::unset(const string& ip)
int FixedLeases::get(int vid, string& ip, string& mac)
{
int rc = -1;
int rc = -1;
if (leases.empty())
{
@ -252,22 +275,14 @@ int FixedLeases::get(int vid, string& ip, string& mac)
{
ostringstream oss;
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE oid=" << oid
<< " AND ip='" << current->second->ip <<"'";
current->second->used = true;
current->second->vid = vid;
rc = db->exec(oss);
rc = update_lease(current->second);
if ( rc == 0 )
{
current->second->used = true;
current->second->vid = vid;
current->second->to_string(ip,mac);
current++;
}
current->second->to_string(ip,mac);
current++;
break;
}
}
@ -282,7 +297,6 @@ int FixedLeases::set(int vid, const string& ip, string& mac)
{
map<unsigned int,Lease *>::iterator it;
ostringstream oss;
unsigned int num_ip;
int rc;
@ -304,22 +318,45 @@ int FixedLeases::set(int vid, const string& ip, string& mac)
return -1;
}
oss << "UPDATE " << table << " SET used='1', vid='" << vid << "'"
<< " WHERE oid=" << oid << " AND ip='" << it->second->ip <<"'";
rc = db->exec(oss);
if ( rc != 0 )
{
return -1;
}
it->second->used = true;
it->second->vid = vid;
Leases::Lease::mac_to_string(it->second->mac,mac);
return 0;
return update_lease(it->second);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int FixedLeases::update_lease(Lease * lease)
{
ostringstream oss;
string xml_body;
char * sql_xml;
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
return -1;
}
if( lease->used )
{
n_used++;
}
else
{
n_used--;
}
oss << "UPDATE " << table << " SET body='" << sql_xml << "'"
<< " WHERE oid=" << oid << " AND ip='" << lease->ip <<"'";
db->free_str(sql_xml);
return db->exec(oss);
}
/* -------------------------------------------------------------------------- */

View File

@ -181,6 +181,8 @@ void Leases::Lease::mac_to_string(const unsigned int i_mac[], string& mac)
mac = oss.str();
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ostream& operator<<(ostream& os, Leases::Lease& _lease)
{
@ -191,32 +193,8 @@ ostream& operator<<(ostream& os, Leases::Lease& _lease)
return os;
}
string& Leases::Lease::to_str(string& str) const
{
string ip;
string mac;
ostringstream os;
to_string(ip,mac);
ip = "IP = " + ip;
mac = "MAC = " + mac;
os.width(20);
os << left << ip;
os.width(24);
os << left << mac;
os << left << " USED = " << used;
os << left << " VID = " << vid;
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::Lease::to_xml(string& str) const
{
@ -229,10 +207,10 @@ string& Leases::Lease::to_xml(string& str) const
os <<
"<LEASE>" <<
"<IP>"<< ip << "</IP>" <<
"<MAC>" << mac << "</MAC>" <<
"<USED>" << used << "</USED>" <<
"<VID>" << vid << "</VID>" <<
"<IP>" << ip << "</IP>" <<
"<MAC>" << mac << "</MAC>" <<
"<USED>"<< used << "</USED>"<<
"<VID>" << vid << "</VID>" <<
"</LEASE>";
str = os.str();
@ -240,6 +218,54 @@ string& Leases::Lease::to_xml(string& str) const
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::Lease::to_xml_db(string& str) const
{
ostringstream os;
os <<
"<LEASE>" <<
"<IP>" << ip << "</IP>" <<
"<MAC_PREFIX>" << mac[Lease::PREFIX] << "</MAC_PREFIX>" <<
"<MAC_SUFFIX>" << mac[Lease::SUFFIX] << "</MAC_SUFFIX>" <<
"<USED>" << used << "</USED>"<<
"<VID>" << vid << "</VID>" <<
"</LEASE>";
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::Lease::from_xml(const string &xml_str)
{
int rc = 0;
int int_used;
// Initialize the internal XML object
update_from_str(xml_str);
rc += xpath(ip , "/LEASE/IP" , 0);
rc += xpath(mac[Lease::PREFIX], "/LEASE/MAC_PREFIX" , 0);
rc += xpath(mac[Lease::SUFFIX], "/LEASE/MAC_SUFFIX" , 0);
rc += xpath(int_used , "/LEASE/USED" , 0);
rc += xpath(vid , "/LEASE/VID" , 0);
used = static_cast<bool>(int_used);
if (rc != 0)
{
return -1;
}
return 0;
}
/* ************************************************************************** */
/* ************************************************************************** */
/* Leases class */
@ -248,55 +274,38 @@ string& Leases::Lease::to_xml(string& str) const
const char * Leases::table = "leases";
const char * Leases::db_names = "oid,ip,mac_prefix,mac_suffix,vid,used";
const char * Leases::db_names = "oid, ip, body";
const char * Leases::db_bootstrap = "CREATE TABLE IF NOT EXISTS leases ("
"oid INTEGER, ip BIGINT, mac_prefix BIGINT, mac_suffix BIGINT,"
"vid INTEGER, used INTEGER, PRIMARY KEY(oid,ip))";
"oid INTEGER, ip BIGINT, body TEXT, PRIMARY KEY(oid,ip))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::select_cb(void *nil, int num, char **values, char **names)
{
if ( (values[OID] == 0) ||
(values[IP] == 0) ||
(values[MAC_PREFIX] == 0) ||
(values[MAC_SUFFIX] == 0) ||
(values[VID] == 0) ||
(values[USED]== 0) ||
(num != LIMIT ))
Lease * lease;
int rc;
if ( (!values[0]) || (num != 1) )
{
return -1;
}
unsigned int mac[2];
unsigned int ip;
int vid;
bool used;
lease = new Lease();
rc = lease->from_xml(values[0]);
istringstream iss;
if (rc != 0)
{
return -1;
}
iss.str(values[IP]);
iss >> ip;
leases.insert(make_pair(lease->ip, lease));
iss.clear();
iss.str(values[MAC_PREFIX]);
iss >> mac[Lease::PREFIX];
iss.clear();
iss.str(values[MAC_SUFFIX]);
iss >> mac[Lease::SUFFIX];
iss.clear();
iss.str(values[VID]);
iss >> vid;
iss.clear();
iss.str(values[USED]);
iss >> used;
leases.insert(make_pair(ip,new Lease(ip,mac,vid,used)));
if(lease->used)
{
n_used++;
}
return 0;
}
@ -308,9 +317,12 @@ int Leases::select(SqlDB * db)
ostringstream oss;
int rc;
// Reset the used leases counter
n_used = 0;
set_callback(static_cast<Callbackable::Callback>(&Leases::select_cb));
oss << "SELECT " << db_names << " FROM " << table << " WHERE oid = " << oid;
oss << "SELECT body FROM " << table << " WHERE oid = " << oid;
rc = db->exec(oss,this);
@ -434,25 +446,6 @@ string& Leases::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& Leases::to_str(string& str) const
{
map<unsigned int, Leases::Lease *>::const_iterator it;
ostringstream os;
string lease_str;
for(it=leases.begin();it!=leases.end();it++)
{
os << it->second->to_str(lease_str) << endl;
}
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
ostream& operator<<(ostream& os, Leases& _leases)
{
string xml;

View File

@ -132,26 +132,56 @@ int RangedLeases::add(
int vid,
bool used)
{
ostringstream oss;
int rc;
ostringstream oss;
//Insert the lease in the database
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("<<
oid << "," <<
ip << "," <<
mac[Lease::PREFIX] << "," <<
mac[Lease::SUFFIX] << "," <<
vid << "," <<
used << ")";
Lease * lease;
string xml_body;
char * sql_xml;
int rc;
lease = new Lease(ip,mac,vid,used);
sql_xml = db->escape_str(lease->to_xml_db(xml_body).c_str());
if ( sql_xml == 0 )
{
goto error_body;
}
oss << "INSERT INTO " << table << " ("<< db_names <<") VALUES ("
<< oid << ","
<< ip << ","
<< "'" << sql_xml << "')";
db->free_str(sql_xml);
rc = db->exec(oss);
if ( rc == 0 )
if ( rc != 0 )
{
leases.insert(make_pair(ip,new Lease(ip,mac,vid,used)));
goto error_db;
}
leases.insert( make_pair(ip,lease) );
n_used++;
return rc;
error_body:
oss.str("");
oss << "Error inserting lease, marshall error";
goto error_common;
error_db:
oss.str("");
oss << "Error inserting lease in database.";
error_common:
NebulaLog::log("VNM", Log::ERROR, oss);
return -1;
}
/* -------------------------------------------------------------------------- */
@ -187,6 +217,8 @@ int RangedLeases::del(const string& ip)
if ( rc == 0 )
{
n_used--;
delete it_ip->second;
leases.erase(it_ip);

View File

@ -29,10 +29,11 @@
/* Virtual Network :: Constructor/Destructor */
/* ************************************************************************** */
VirtualNetwork::VirtualNetwork(VirtualNetworkTemplate *_vn_template):
PoolObjectSQL(-1),
name(""),
uid(-1),
VirtualNetwork::VirtualNetwork(int uid,
string _user_name,
VirtualNetworkTemplate *_vn_template):
PoolObjectSQL(-1,"",uid,table),
user_name(_user_name),
bridge(""),
type(UNINITIALIZED),
leases(0)
@ -69,85 +70,57 @@ VirtualNetwork::~VirtualNetwork()
const char * VirtualNetwork::table = "network_pool";
const char * VirtualNetwork::db_names =
"oid,uid,name,type,bridge,public,template";
const char * VirtualNetwork::extended_db_names =
"network_pool.oid, network_pool.uid, network_pool.name, network_pool.type, "
"network_pool.bridge, network_pool.public, network_pool.template";
const char * VirtualNetwork::db_names = "oid, name, body, uid";
const char * VirtualNetwork::db_bootstrap = "CREATE TABLE IF NOT EXISTS"
" network_pool ("
"oid INTEGER PRIMARY KEY, uid INTEGER, name VARCHAR(256), type INTEGER, "
"bridge TEXT, public INTEGER, template TEXT, UNIQUE(name))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE]) ||
(!values[PUBLIC]) ||
(!values[TEMPLATE]) ||
(num != LIMIT ))
{
return -1;
}
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = (NetworkType)atoi(values[TYPE]);
bridge = values[BRIDGE];
public_vnet = atoi(values[PUBLIC]);
// Virtual Network template
vn_template->from_xml(values[TEMPLATE]);
return 0;
}
" network_pool (oid INTEGER PRIMARY KEY, name VARCHAR(256),"
" body TEXT, uid INTEGER, UNIQUE(name,uid))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select(SqlDB * db)
{
int rc;
rc = PoolObjectSQL::select(db);
if ( rc != 0 )
{
return rc;
}
return select_leases(db);
}
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select(SqlDB * db, const string& name, int uid)
{
int rc;
rc = PoolObjectSQL::select(db,name,uid);
if ( rc != 0 )
{
return rc;
}
return select_leases(db);
}
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select_leases(SqlDB * db)
{
ostringstream oss;
ostringstream ose;
int rc;
int boid;
string network_address;
unsigned int default_size = VirtualNetworkPool::default_size();
unsigned int mac_prefix = VirtualNetworkPool::mac_prefix();
set_callback(
static_cast<Callbackable::Callback>(&VirtualNetwork::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 ))
{
goto error_id;
}
//Get the leases
if (type == RANGED)
{
@ -207,9 +180,6 @@ int VirtualNetwork::select(SqlDB * db)
return leases->select(db);
error_id:
ose << "Error getting Virtual Network nid: " << oid;
goto error_common;
error_leases:
ose << "Error getting Virtual Network leases nid: " << oid;
@ -230,43 +200,6 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::dump(ostringstream& oss,
int num,
char ** values,
char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE]) ||
(!values[PUBLIC]) ||
(!values[TEMPLATE]) ||
(!values[LIMIT]) ||
(num != LIMIT + 2 ))
{
return -1;
}
oss <<
"<VNET>" <<
"<ID>" << values[OID] << "</ID>" <<
"<UID>" << values[UID] << "</UID>" <<
"<USERNAME>" << values[LIMIT+1] << "</USERNAME>" <<
"<NAME>" << values[NAME] << "</NAME>" <<
"<TYPE>" << values[TYPE] << "</TYPE>" <<
"<BRIDGE>" << values[BRIDGE] << "</BRIDGE>" <<
"<PUBLIC>" << values[PUBLIC] << "</PUBLIC>" <<
"<TOTAL_LEASES>"<< values[LIMIT] << "</TOTAL_LEASES>"
<< values[TEMPLATE] <<
"</VNET>";
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::insert(SqlDB * db, string& error_str)
{
ostringstream ose;
@ -444,23 +377,16 @@ error_common:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::update(SqlDB * db)
{
return insert_replace(db, true);
}
int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
{
ostringstream oss;
int rc;
string xml_template;
string xml_body;
char * sql_name;
char * sql_bridge;
char * sql_template;
char * sql_xml;
sql_name = db->escape_str(name.c_str());
@ -470,22 +396,13 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
goto error_name;
}
sql_bridge = db->escape_str(bridge.c_str());
sql_xml = db->escape_str(to_xml(xml_body).c_str());
if ( sql_bridge == 0 )
if ( sql_xml == 0 )
{
goto error_bridge;
goto error_body;
}
vn_template->to_xml(xml_template);
sql_template = db->escape_str(xml_template.c_str());
if ( sql_template == 0 )
{
goto error_template;
}
// Construct the SQL statement to Insert or Replace
if(replace)
{
@ -498,24 +415,19 @@ int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
oss << " INTO " << table << " (" << db_names << ") VALUES ("
<< oid << ","
<< uid << ","
<< "'" << sql_name << "',"
<< type << ","
<< "'" << sql_bridge << "',"
<< public_vnet << ","
<< "'" << sql_template<< "')";
<< "'" << sql_xml << "',"
<< uid << ")";
rc = db->exec(oss);
db->free_str(sql_name);
db->free_str(sql_bridge);
db->free_str(sql_template);
db->free_str(sql_xml);
return rc;
error_template:
db->free_str(sql_bridge);
error_bridge:
error_body:
db->free_str(sql_name);
error_name:
return -1;
@ -524,30 +436,20 @@ error_name:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::vn_drop(SqlDB * db)
int VirtualNetwork::drop(SqlDB * db)
{
ostringstream oss;
int rc;
int rc;
if ( leases != 0 )
rc = PoolObjectSQL::drop(db);
if ( rc == 0 && leases != 0 )
{
leases->drop(db);
}
oss << "DELETE FROM " << table << " WHERE OID=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
{
set_valid(false);
rc += leases->drop(db);
}
return rc;
}
/* ************************************************************************** */
/* Virtual Network :: Misc */
/* ************************************************************************** */
@ -556,7 +458,7 @@ ostream& operator<<(ostream& os, VirtualNetwork& vn)
{
string vnet_xml;
os << vn.to_xml(vnet_xml);
os << vn.to_xml_extended(vnet_xml,true);
return os;
};
@ -565,25 +467,45 @@ ostream& operator<<(ostream& os, VirtualNetwork& vn)
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_xml(string& xml) const
{
return to_xml_extended(xml,false);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const
{
ostringstream os;
string template_xml;
string leases_xml;
// Total leases is the number of used leases.
int total_leases = 0;
if (leases != 0)
{
total_leases = leases->n_used;
}
os <<
"<VNET>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<BRIDGE>" << bridge << "</BRIDGE>" <<
"<PUBLIC>" << public_vnet << "</PUBLIC>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
"<USERNAME>" << user_name << "</USERNAME>" <<
"<NAME>" << name << "</NAME>" <<
"<TYPE>" << type << "</TYPE>" <<
"<BRIDGE>" << bridge << "</BRIDGE>" <<
"<PUBLIC>" << public_vnet << "</PUBLIC>" <<
"<TOTAL_LEASES>"<< total_leases << "</TOTAL_LEASES>"<<
vn_template->to_xml(template_xml);
if (leases)
if (extended && leases != 0)
{
os << leases->to_xml(leases_xml);
}
os << "</VNET>";
xml = os.str();
@ -594,40 +516,44 @@ string& VirtualNetwork::to_xml(string& xml) const
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
string& VirtualNetwork::to_str(string& str) const
int VirtualNetwork::from_xml(const string &xml_str)
{
ostringstream os;
vector<xmlNodePtr> content;
string template_str;
string leases_str;
int rc = 0;
int int_type;
os << "ID : " << oid << endl;
os << "UID : " << uid << endl;
os << "NAME : " << name << endl;
os << "Type : ";
if ( type==VirtualNetwork::RANGED )
// Initialize the internal XML object
update_from_str(xml_str);
// Get class base attributes
rc += xpath(oid, "/VNET/ID", -1);
rc += xpath(uid, "/VNET/UID", -1);
rc += xpath(user_name, "/VNET/USERNAME", "not_found");
rc += xpath(name, "/VNET/NAME", "not_found");
rc += xpath(int_type, "/VNET/TYPE", -1);
rc += xpath(bridge, "/VNET/BRIDGE", "not_found");
rc += xpath(public_vnet,"/VNET/PUBLIC", 0);
type = static_cast<NetworkType>( int_type );
// Get associated classes
ObjectXML::get_nodes("/VNET/TEMPLATE", content);
if( content.size() < 1 )
{
os << "Ranged" << endl;
}
else
{
os << "Fixed" << endl;
return -1;
}
os << "Bridge : " << bridge << endl;
os << "Public : " << public_vnet << endl << endl;
// Virtual Network template
rc += vn_template->from_xml_node( content[0] );
os << "....: Template :...." << vn_template->to_str(template_str) << endl <<
endl;
if (leases)
if (rc != 0)
{
os << "....: Leases :...." << endl << leases->to_str(leases_str) << endl;
return -1;
}
str = os.str();
return str;
return 0;
}
/* -------------------------------------------------------------------------- */

View File

@ -71,17 +71,36 @@ VirtualNetworkPool::VirtualNetworkPool(SqlDB * db,
int VirtualNetworkPool::allocate (
int uid,
string user_name,
VirtualNetworkTemplate * vn_template,
int * oid,
string& error_str)
{
VirtualNetwork * vn;
VirtualNetwork * vn_aux;
string name;
vn = new VirtualNetwork(vn_template);
vn = new VirtualNetwork(uid, user_name, vn_template);
vn->uid = uid;
// Check for duplicates
vn->get_template_attribute("NAME", name);
vn_aux = get(name,uid,false);
*oid = PoolSQL::allocate(vn, error_str);
if( vn_aux != 0 )
{
ostringstream oss;
oss << "NAME is already taken by NET " << vn_aux->get_oid() << ".";
error_str = oss.str();
*oid = -1;
delete vn;
}
else
{
*oid = PoolSQL::allocate(vn, error_str);
}
return *oid;
}
@ -89,118 +108,7 @@ int VirtualNetworkPool::allocate (
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetworkPool::get_cb(void * _oid, int num, char **values,char **names)
{
int * oid;
oid = static_cast<int *>(_oid);
if ( oid == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
*oid = atoi(values[0]);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualNetwork * VirtualNetworkPool::get(const string& name, bool lock)
{
ostringstream oss;
int oid = -1;
int rc;
char * sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
return 0;
}
set_callback(
static_cast<Callbackable::Callback>(&VirtualNetworkPool::get_cb),
static_cast<void *>(&oid));
oss << "SELECT oid FROM " << VirtualNetwork::table << " WHERE name = '"
<< sql_name << "'";
rc = db->exec(oss, this);
unset_callback();
db->free_str(sql_name);
if (rc != 0 || oid == -1)
{
return 0;
}
return get(oid,lock);
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetworkPool::dump_cb(void * _oss,
int num,
char ** values,
char ** names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
return VirtualNetwork::dump(*oss, num, values, names);
}
/* -------------------------------------------------------------------------- */
int VirtualNetworkPool::dump(ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
oss << "<VNET_POOL>";
set_callback(
static_cast<Callbackable::Callback>(&VirtualNetworkPool::dump_cb),
static_cast<void *>(&oss));
cmd << "SELECT " << VirtualNetwork::extended_db_names << ",COUNT("
<< Leases::table << ".used), user_pool.user_name FROM "
<< VirtualNetwork::table
<< " LEFT OUTER JOIN " << Leases::table << " ON "
<< VirtualNetwork::table << ".oid = " << Leases::table << ".oid"
<< " AND " << Leases::table << ".used = 1"
<< " LEFT OUTER JOIN (SELECT oid,user_name FROM user_pool) "
<< " AS user_pool ON "<< VirtualNetwork::table
<< ".uid = user_pool.oid";
if ( !where.empty() )
{
cmd << " WHERE " << where;
}
cmd << " GROUP BY " << VirtualNetwork::table << ".oid";
rc = db->exec(cmd,this);
oss << "</VNET_POOL>";
unset_callback();
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int vid)
int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int uid, int vid)
{
string network;
VirtualNetwork * vnet = 0;
@ -229,7 +137,7 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int vid)
}
else
{
vnet = get(network,true);
vnet = get(network, uid, true);
}
if (vnet == 0)
@ -247,7 +155,9 @@ int VirtualNetworkPool::nic_attribute(VectorAttribute * nic, int vid)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
void VirtualNetworkPool::authorize_nic(VectorAttribute * nic, AuthRequest * ar)
void VirtualNetworkPool::authorize_nic(VectorAttribute * nic,
int uid,
AuthRequest * ar)
{
string network;
VirtualNetwork * vnet = 0;
@ -276,7 +186,7 @@ void VirtualNetworkPool::authorize_nic(VectorAttribute * nic, AuthRequest * ar)
}
else
{
vnet = get(network,true);
vnet = get(network, uid, true);
}
if (vnet == 0)
@ -285,7 +195,7 @@ void VirtualNetworkPool::authorize_nic(VectorAttribute * nic, AuthRequest * ar)
}
ar->add_auth(AuthRequest::NET,
vnet->get_vnid(),
vnet->get_oid(),
AuthRequest::USE,
vnet->get_uid(),
vnet->isPublic());

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

@ -20,13 +20,15 @@
#include "VirtualNetworkPool.h"
#include "PoolTest.h"
#include "ObjectXML.h"
using namespace std;
/* ************************************************************************* */
/* ************************************************************************* */
const int uids[] = {123, 261, 133};
const int uids[] = {123, 261, 133, 78};
const string user_names[] = {"A user","B user","C user","D user"};
const string names[] = {"Net number one", "A virtual network","Net number two"};
@ -69,11 +71,11 @@ const string templates[] =
const string xmls[] =
{
"<VNET><ID>0</ID><UID>123</UID><NAME>Net number one</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TEMPLATE><BRIDGE><![CDATA[br1]]></BRIDGE><LEASES><IP><![CDATA[130.10.0.1]]></IP><MAC><![CDATA[50:20:20:20:20:20]]></MAC></LEASES><NAME><![CDATA[Net number one]]></NAME><TYPE><![CDATA[FIXED]]></TYPE></TEMPLATE><LEASES><LEASE><IP>130.10.0.1</IP><MAC>50:20:20:20:20:20</MAC><USED>0</USED><VID>-1</VID></LEASE></LEASES></VNET>",
"<VNET><ID>0</ID><UID>123</UID><USERNAME>A user</USERNAME><NAME>Net number one</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br1]]></BRIDGE><LEASES><IP><![CDATA[130.10.0.1]]></IP><MAC><![CDATA[50:20:20:20:20:20]]></MAC></LEASES><NAME><![CDATA[Net number one]]></NAME><TYPE><![CDATA[FIXED]]></TYPE></TEMPLATE><LEASES><LEASE><IP>130.10.0.1</IP><MAC>50:20:20:20:20:20</MAC><USED>0</USED><VID>-1</VID></LEASE></LEASES></VNET>",
"<VNET><ID>1</ID><UID>261</UID><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TEMPLATE><BRIDGE><![CDATA[br0]]></BRIDGE><NAME><![CDATA[A virtual network]]></NAME><NETWORK_ADDRESS><![CDATA[192.168.0.0]]></NETWORK_ADDRESS><NETWORK_SIZE><![CDATA[C]]></NETWORK_SIZE><TYPE><![CDATA[RANGED]]></TYPE></TEMPLATE><LEASES></LEASES></VNET>",
"<VNET><ID>1</ID><UID>261</UID><USERNAME>B user</USERNAME><NAME>A virtual network</NAME><TYPE>0</TYPE><BRIDGE>br0</BRIDGE><PUBLIC>1</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br0]]></BRIDGE><NAME><![CDATA[A virtual network]]></NAME><NETWORK_ADDRESS><![CDATA[192.168.0.0]]></NETWORK_ADDRESS><NETWORK_SIZE><![CDATA[C]]></NETWORK_SIZE><TYPE><![CDATA[RANGED]]></TYPE></TEMPLATE><LEASES></LEASES></VNET>",
"<VNET><ID>0</ID><UID>133</UID><NAME>Net number two</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TEMPLATE><BRIDGE><![CDATA[br1]]></BRIDGE><LEASES><IP><![CDATA[130.10.2.1]]></IP><MAC><![CDATA[50:20:20:20:20:20]]></MAC></LEASES><NAME><![CDATA[Net number two]]></NAME><TYPE><![CDATA[fixed]]></TYPE></TEMPLATE><LEASES><LEASE><IP>130.10.2.1</IP><MAC>50:20:20:20:20:20</MAC><USED>0</USED><VID>-1</VID></LEASE></LEASES></VNET>"
"<VNET><ID>0</ID><UID>133</UID><USERNAME>C user</USERNAME><NAME>Net number two</NAME><TYPE>1</TYPE><BRIDGE>br1</BRIDGE><PUBLIC>0</PUBLIC><TOTAL_LEASES>0</TOTAL_LEASES><TEMPLATE><BRIDGE><![CDATA[br1]]></BRIDGE><LEASES><IP><![CDATA[130.10.2.1]]></IP><MAC><![CDATA[50:20:20:20:20:20]]></MAC></LEASES><NAME><![CDATA[Net number two]]></NAME><TYPE><![CDATA[fixed]]></TYPE></TEMPLATE><LEASES><LEASE><IP>130.10.2.1</IP><MAC>50:20:20:20:20:20</MAC><USED>0</USED><VID>-1</VID></LEASE></LEASES></VNET>"
};
const string xml_dump =
@ -97,7 +99,8 @@ public:
{};
int allocate(const int& uid, const std::string& stemplate, int* oid)
int allocate(const int& uid, const std::string& user_name,
const std::string& stemplate, int* oid)
{
VirtualNetworkTemplate * vn_template;
char * error_msg = 0;
@ -109,7 +112,8 @@ public:
if( rc == 0 )
{
return VirtualNetworkPool::allocate(uid, vn_template, oid, err);
return VirtualNetworkPool::allocate(uid, user_name,
vn_template, oid, err);
}
else
{
@ -182,41 +186,33 @@ protected:
int allocate(int index)
{
int oid;
return ((VirtualNetworkPoolFriend*)pool)->allocate(uids[index],
templates[index], &oid);
return ((VirtualNetworkPoolFriend*)pool)->allocate(
uids[index],user_names[index],templates[index], &oid);
};
void check(int index, PoolObjectSQL* obj)
{
CPPUNIT_ASSERT( obj != 0 );
string xml_str = "";
string xml_str;
ostringstream oss;
((VirtualNetwork*)obj)->to_xml(xml_str);
oss << * ((VirtualNetwork*)obj);
xml_str = oss.str();
/*
if( xml_str != xmls[index] )
{
cout << endl << xml_str << endl << "========"
<< endl << xmls[index] << endl << "--------";
}
//*/
CPPUNIT_ASSERT( ((VirtualNetwork*)obj)->get_uid() == uids[index] );
CPPUNIT_ASSERT( ((VirtualNetwork*)obj)->to_xml(xml_str) == xmls[index] );
CPPUNIT_ASSERT( xml_str == xmls[index] );
};
void set_up_user_pool()
{
string err;
UserPool::bootstrap(db);
UserPool * user_pool = new UserPool(db);
int uid_1, uid_2;
string username_1 = "A user";
string username_2 = "B user";
string pass_1 = "A pass";
string pass_2 = "B pass";
user_pool->allocate(&uid_1, username_1, pass_1, true, err);
user_pool->allocate(&uid_2, username_2, pass_2, true, err);
delete user_pool;
};
public:
VirtualNetworkPoolTest(){xmlInitParser();};
@ -271,7 +267,7 @@ public:
CPPUNIT_ASSERT( oid >= 0 );
// Get using its name
vn = vnpool->get(names[1], true);
vn = vnpool->get(names[1],uids[1], true);
CPPUNIT_ASSERT(vn != 0);
vn->unlock();
@ -281,7 +277,7 @@ public:
vnpool->clean();
// Get using its name
vn = vnpool->get(names[1], false);
vn = vnpool->get(names[1], uids[1], false);
check(1, vn);
};
@ -294,14 +290,14 @@ public:
VirtualNetwork * vn;
// Empty Pool
vn = vnpool->get("Wrong name", true);
vn = vnpool->get("Wrong name", 0, true);
CPPUNIT_ASSERT( vn == 0 );
// Allocate an object
allocate(0);
// Ask again for a non-existing name
vn = vnpool->get("Non existing name", true);
vn = vnpool->get("Non existing name", uids[0], true);
CPPUNIT_ASSERT( vn == 0 );
}
@ -380,7 +376,7 @@ public:
for (int i = 0 ; i < 7 ; i++)
{
rc = vnpool->allocate(uids[0], templ[i], &oid[i]);
rc = vnpool->allocate(uids[0], user_names[0], templ[i], &oid[i]);
CPPUNIT_ASSERT( rc >= 0 );
vnet = vnpool->get(oid[i], false);
@ -403,30 +399,30 @@ public:
void duplicates()
{
int rc, oid_0, oid_1;
int rc, oid_0, oid_1, oid_2, oid_3;
VirtualNetworkPoolFriend * vnpool =
static_cast<VirtualNetworkPoolFriend *>(pool);
VirtualNetwork * vnet;
// Allocate a vnet
rc = vnpool->allocate(uids[0], templates[0], &oid_0);
rc = vnpool->allocate(uids[0], user_names[0], templates[0], &oid_0);
CPPUNIT_ASSERT( rc == oid_0 );
CPPUNIT_ASSERT( rc == 0 );
// Allocate the same vnet twice, with the same user ID
rc = vnpool->allocate(uids[0], templates[0], &oid_1);
rc = vnpool->allocate(uids[0], user_names[0], templates[0], &oid_1);
CPPUNIT_ASSERT( rc == oid_1 );
CPPUNIT_ASSERT( rc == -1 );
// With different user ID
rc = vnpool->allocate(uids[1], templates[0], &oid_1);
CPPUNIT_ASSERT( rc == oid_1 );
CPPUNIT_ASSERT( rc == -1 );
// Same VNet, with different user ID
rc = vnpool->allocate(uids[1], user_names[1], templates[0], &oid_2);
CPPUNIT_ASSERT( rc == oid_2 );
CPPUNIT_ASSERT( rc == 1 );
// Insert a different template, with the same user ID
rc = vnpool->allocate(uids[1], templates[1], &oid_1);
CPPUNIT_ASSERT( rc == oid_1 );
CPPUNIT_ASSERT( rc == 1 );
rc = vnpool->allocate(uids[1], user_names[1], templates[1], &oid_3);
CPPUNIT_ASSERT( rc == oid_3 );
CPPUNIT_ASSERT( rc == 2 );
// Make sure the table contains only one vnet with name[0]
@ -438,8 +434,9 @@ public:
ret = pool->search(results, table, where);
CPPUNIT_ASSERT(ret == 0);
CPPUNIT_ASSERT(results.size() == 1);
CPPUNIT_ASSERT(results.size() == 2);
CPPUNIT_ASSERT(results.at(0) == oid_0);
CPPUNIT_ASSERT(results.at(1) == oid_2);
// Get the vnet and check it, to make sure the user id was not rewritten
vnet = vnpool->get(oid_0, false);
@ -456,16 +453,23 @@ public:
int oid, rc;
ostringstream oss;
set_up_user_pool();
vnpool->allocate(1, templates[0], &oid);
vnpool->allocate(2, templates[1], &oid);
vnpool->allocate(1, user_names[0], templates[0], &oid);
vnpool->allocate(2, user_names[1], templates[1], &oid);
rc = pool->dump(oss, "");
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
/*
if( result != xml_dump )
{
cout << endl << result << endl << "========"
<< endl << xml_dump << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_dump );
};
@ -479,17 +483,24 @@ public:
int oid, rc;
ostringstream oss;
set_up_user_pool();
vnpool->allocate(1, user_names[0], templates[0], &oid);
vnpool->allocate(2, user_names[1], templates[1], &oid);
vnpool->allocate(1, templates[0], &oid);
vnpool->allocate(2, templates[1], &oid);
string where = "uid > 1";
string where = "name LIKE '%virtual%'";
rc = pool->dump(oss, where);
CPPUNIT_ASSERT(rc == 0);
string result = oss.str();
/*
if( result != xml_dump_where )
{
cout << endl << result << endl << "========"
<< endl << xml_dump_where << endl << "--------";
}
//*/
CPPUNIT_ASSERT( result == xml_dump_where );
};
@ -594,7 +605,7 @@ public:
"NETWORK_ADDRESS = 192.168.50.0\n";
vnpool->allocate(45, tmpl, &oid);
vnpool->allocate(45, user_names[0], tmpl, &oid);
CPPUNIT_ASSERT( oid != -1 );
vn = vnpool->get(oid, false);
@ -811,15 +822,18 @@ public:
string mac = "";
string bridge = "";
ostringstream oss;
string xpath;
vector<int> results;
// First VNet template
vnpool->allocate(13, tmpl_A, &oid_0);
vnpool->allocate(13, user_names[0], tmpl_A, &oid_0);
CPPUNIT_ASSERT( oid_0 != -1 );
// Second VNet
vnpool->allocate(45, tmpl_B, &oid_new);
vnpool->allocate(45, user_names[0], tmpl_B, &oid_new);
CPPUNIT_ASSERT( oid_new != -1 );
// Get this second VNet
@ -833,15 +847,31 @@ public:
CPPUNIT_ASSERT( rc == 0 );
// ---------------------------------------------------------------------
// The IP ...1 should be tagged as used only in the second Vnet
const char * table = "leases";
string where = "used = 1 AND vid = 1234";
// Clean the cache, forcing the pool to read the objects from the DB
pool->clean();
rc = pool->search(results, table, where);
CPPUNIT_ASSERT(rc == 0);
CPPUNIT_ASSERT(results.size() == 1);
// First VNet
vn = vnpool->get(oid_0, false);
CPPUNIT_ASSERT( vn != 0 );
oss << *vn;
// 0 Used leases
ObjectXML::xpath_value(xpath, oss.str().c_str(), "/VNET/TOTAL_LEASES" );
CPPUNIT_ASSERT( xpath == "0" );
// Second VNet
vn = vnpool->get(oid_new, false);
CPPUNIT_ASSERT( vn != 0 );
oss.str("");
oss << *vn;
// 1 Used leases
ObjectXML::xpath_value(xpath, oss.str().c_str(), "/VNET/TOTAL_LEASES" );
CPPUNIT_ASSERT( xpath == "1" );
// ---------------------------------------------------------------------
// Now check that the first VNet has that IP available
@ -872,13 +902,32 @@ public:
CPPUNIT_ASSERT( rc != 0 );
// ---------------------------------------------------------------------
// Check that in the DB, only one lease is used
where = "used = 1";
// Clean the reults
results.resize(0);
rc = pool->search(results, table, where);
CPPUNIT_ASSERT(rc == 0);
CPPUNIT_ASSERT(results.size() == 1);
// Clean the cache, forcing the pool to read the objects from the DB
pool->clean();
// First VNet
vn = vnpool->get(oid_0, false);
CPPUNIT_ASSERT( vn != 0 );
oss.str("");
oss << *vn;
// 0 Used leases
ObjectXML::xpath_value(xpath, oss.str().c_str(), "/VNET/TOTAL_LEASES" );
CPPUNIT_ASSERT( xpath == "0" );
// Second VNet
vn = vnpool->get(oid_new, false);
CPPUNIT_ASSERT( vn != 0 );
oss.str("");
oss << *vn;
// 1 Used leases
ObjectXML::xpath_value(xpath, oss.str().c_str(), "/VNET/TOTAL_LEASES" );
CPPUNIT_ASSERT( xpath == "1" );
// ---------------------------------------------------------------------
}
/* -------------------------------------------------------------------------- */
@ -1000,7 +1049,7 @@ public:
while( templates[i] != "END" )
{
vnp->allocate(0, templates[i], &oid);
vnp->allocate(0, user_names[0], templates[i], &oid);
CPPUNIT_ASSERT( oid >= 0 );
@ -1038,20 +1087,17 @@ public:
"BRIDGE = br2\n"
"LEASES = [IP=130.10.0.5, MAC=50:20:20:20:20:25]";
vnp->allocate(0, template_0, &oid_0);
vnp->allocate(0, user_names[0], template_0, &oid_0);
CPPUNIT_ASSERT( oid_0 == 0 );
vnp->allocate(0, template_1, &oid_1);
vnp->allocate(0, user_names[0], template_1, &oid_1);
CPPUNIT_ASSERT( oid_1 == 1 );
// Disk using network 0
disk = new VectorAttribute("DISK");
disk->replace("NETWORK", "Net 0");
((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0);
((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0, 0);
value = "";
value = disk->vector_value("NETWORK");
@ -1080,7 +1126,7 @@ public:
disk = new VectorAttribute("DISK");
disk->replace("NETWORK_ID", "1");
((VirtualNetworkPool*)vnp)->nic_attribute(disk, 0);
((VirtualNetworkPool*)vnp)->nic_attribute(disk,0, 0);
value = "";
value = disk->vector_value("NETWORK");
@ -1530,7 +1576,5 @@ public:
int main(int argc, char ** argv)
{
OneUnitTest::set_one_auth();
return PoolTest::main(argc, argv, VirtualNetworkPoolTest::suite());
}

View File

@ -17,7 +17,8 @@
#include <ObjectXML.h>
#include <stdexcept>
#include <cstring>
#include <iostream>
#include <sstream>
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
@ -131,6 +132,142 @@ vector<string> ObjectXML::operator[] (const char * xpath_expr)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::xpath(string& value, const char * xpath_expr, const char * def)
{
vector<string> values;
int rc = 0;
values = (*this)[xpath_expr];
if ( values.empty() == true )
{
value = def;
rc = -1;
}
else
{
value = values[0];
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::xpath(int& value, const char * xpath_expr, const int& def)
{
vector<string> values;
int rc = 0;
values = (*this)[xpath_expr];
if (values.empty() == true)
{
value = def;
rc = -1;
}
else
{
istringstream iss;
iss.str(values[0]);
iss >> dec >> value;
if (iss.fail() == true)
{
value = def;
rc = -1;
}
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::xpath(unsigned int& value, const char * xpath_expr,
const unsigned int& def)
{
vector<string> values;
int rc = 0;
values = (*this)[xpath_expr];
if (values.empty() == true)
{
value = def;
rc = -1;
}
else
{
istringstream iss;
iss.str(values[0]);
iss >> dec >> value;
if (iss.fail() == true)
{
value = def;
rc = -1;
}
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::xpath(time_t& value, const char * xpath_expr, const time_t& def)
{
int int_val;
int int_def = static_cast<time_t>(def);
int rc;
rc = xpath(int_val, xpath_expr, int_def);
value = static_cast<time_t>(int_val);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::xpath_value(string& value,const char *doc,const char *the_xpath)
{
int rc = 0;
try
{
ObjectXML obj(doc);
vector<string> values;
values = obj[the_xpath];
if (values.empty() == true)
{
rc = -1;
}
else
{
value = values[0];
}
}
catch(runtime_error& re)
{
rc = -1;
}
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::get_nodes (const char * xpath_expr, vector<xmlNodePtr>& content)
{
xmlXPathObjectPtr obj;
@ -169,7 +306,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 +333,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

@ -1,4 +1,4 @@
# SConstruct for src/pool
# SConstruct for src/xml
# -------------------------------------------------------------------------- #
# Copyright 2002-2011, 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)

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton implementation for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@ -45,7 +45,7 @@
#define YYBISON 1
/* Bison version. */
#define YYBISON_VERSION "2.4.2"
#define YYBISON_VERSION "2.4.3"
/* Skeleton name. */
#define YYSKELETON_NAME "yacc.c"
@ -1232,7 +1232,7 @@ YYLTYPE yylloc;
YYLTYPE *yylsp;
/* The locations where the error started and ended. */
YYLTYPE yyerror_range[2];
YYLTYPE yyerror_range[3];
YYSIZE_T yystacksize;
@ -1643,7 +1643,7 @@ yyerrlab:
#endif
}
yyerror_range[0] = yylloc;
yyerror_range[1] = yylloc;
if (yyerrstatus == 3)
{
@ -1680,7 +1680,7 @@ yyerrorlab:
if (/*CONSTCOND*/ 0)
goto yyerrorlab;
yyerror_range[0] = yylsp[1-yylen];
yyerror_range[1] = yylsp[1-yylen];
/* Do not reclaim the symbols of the rule which action triggered
this YYERROR. */
YYPOPSTACK (yylen);
@ -1714,7 +1714,7 @@ yyerrlab1:
if (yyssp == yyss)
YYABORT;
yyerror_range[0] = *yylsp;
yyerror_range[1] = *yylsp;
yydestruct ("Error: popping",
yystos[yystate], yyvsp, yylsp, mc, oxml, result, error_msg);
YYPOPSTACK (1);
@ -1724,10 +1724,10 @@ yyerrlab1:
*++yyvsp = yylval;
yyerror_range[1] = yylloc;
yyerror_range[2] = yylloc;
/* Using YYLLOC is tempting, but would change the location of
the lookahead. YYLOC is available though. */
YYLLOC_DEFAULT (yyloc, (yyerror_range - 1), 2);
YYLLOC_DEFAULT (yyloc, yyerror_range, 2);
*++yylsp = yyloc;
/* Shift the error token. */

View File

@ -1,9 +1,9 @@
/* A Bison parser, made by GNU Bison 2.4.2. */
/* A Bison parser, made by GNU Bison 2.4.3. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2006, 2009-2010 Free Software
Foundation, Inc.
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2009, 2010 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@ -611,7 +611,7 @@ static int input (void );
/* This used to be an fputs(), but since the string might contain NUL's,
* we now use fwrite().
*/
#define ECHO fwrite( expr_text, expr_leng, 1, expr_out )
#define ECHO do { if (fwrite( expr_text, expr_leng, 1, expr_out )) {} } while (0)
#endif
/* Gets input and stuffs it into "buf". number of characters read, or YY_NULL,
@ -622,7 +622,7 @@ static int input (void );
if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
{ \
int c = '*'; \
int n; \
unsigned n; \
for ( n = 0; n < max_size && \
(c = getc( expr_in )) != EOF && c != '\n'; ++n ) \
buf[n] = (char) c; \

View File

@ -34,6 +34,8 @@ class ObjectXMLTest : public OneUnitTest
CPPUNIT_TEST( doc_update );
CPPUNIT_TEST( requirements );
CPPUNIT_TEST( rank );
CPPUNIT_TEST( xpath );
CPPUNIT_TEST( xpath_value );
CPPUNIT_TEST_SUITE_END ();
@ -76,6 +78,61 @@ public:
};
void xpath_value()
{
int rc;
string im_mad;
rc = ObjectXML::xpath_value(im_mad,host.c_str(),"/HOST/IM_MAD");
CPPUNIT_ASSERT(rc == 0);
CPPUNIT_ASSERT(im_mad == "im_kvm");
rc = ObjectXML::xpath_value(im_mad,host.c_str(),"/HOST/NO_IM_MAD");
CPPUNIT_ASSERT(rc == -1);
};
void xpath()
{
try
{
ObjectXML obj(xml_history_dump);
string str_exists;
string str_no_exists;
int int_exists;
int int_no_exists;
int int_malformed;
int rc;
rc = obj.xpath(str_exists,"/VM_POOL/VM/HISTORY/HOSTNAME","default_host");
CPPUNIT_ASSERT(str_exists == "A_hostname");
CPPUNIT_ASSERT(rc == 0);
rc = obj.xpath(str_no_exists,"/VM_POOL/NOT_AN_ELEMENT","default_host");
CPPUNIT_ASSERT(str_no_exists == "default_host");
CPPUNIT_ASSERT(rc == -1);
rc = obj.xpath(int_exists,"/VM_POOL/VM/STATE",35);
CPPUNIT_ASSERT(int_exists == 1);
CPPUNIT_ASSERT(rc == 0);
rc = obj.xpath(int_no_exists,"/VM_POOL/NOT_AN_ELEMENT",35);
CPPUNIT_ASSERT(int_no_exists == 35);
CPPUNIT_ASSERT(rc == -1);
rc = obj.xpath(int_malformed,"/VM_POOL/VM/USERNAME",33);
CPPUNIT_ASSERT(int_malformed == 33);
CPPUNIT_ASSERT(rc == -1);
}
catch(runtime_error& re)
{
cerr << re.what() << endl;
CPPUNIT_ASSERT(1 == 0);
}
};
void node_constructor()
{
try
@ -125,7 +182,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"];

View File

@ -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')