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

feature #523, #407, #488: Template is now part of base class PoolObjectSQL

This commit is contained in:
Ruben S. Montero 2011-04-08 01:02:55 +02:00
parent 5ba3999273
commit 1e4e4f2c32
13 changed files with 162 additions and 364 deletions

View File

@ -181,63 +181,6 @@ public:
return 0;
};
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
string& name,
vector<const Attribute*>& values) const
{
return host_template.get(name,values);
};
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
const char *name,
vector<const Attribute*>& values) const
{
string str=name;
return host_template.get(str,values);
};
/**
* Gets a string based host attribute
* @param name of the attribute
* @param value of the attribute (a string), will be "" if not defined
*/
void get_template_attribute(
const char * name,
string& value) const
{
string str=name;
host_template.get(str,value);
}
/**
* Gets a string based host attribute
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined
*/
void get_template_attribute(
const char * name,
int& value) const
{
string str=name;
host_template.get(str,value);
}
// ------------------------------------------------------------------------
// Share functions
// ------------------------------------------------------------------------
@ -394,12 +337,6 @@ private:
// -------------------------------------------------------------------------
// Host Attributes
// -------------------------------------------------------------------------
/**
* The Host template, holds the Host attributes.
*/
HostTemplate host_template;
/**
* The Share represents the logical capacity associated with the host
*/

View File

@ -250,93 +250,6 @@ public:
*/
int disk_attribute(VectorAttribute * disk, int* index, ImageType* img_type);
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
string& name,
vector<const Attribute*>& values) const
{
return image_template->get(name,values);
};
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
const char *name,
vector<const Attribute*>& values) const
{
string str=name;
return image_template->get(str,values);
};
/**
* Gets a string based Image attribute
* @param name of the attribute
* @param value of the attribute (a string), will be "" if not defined
*/
void get_template_attribute(
const char * name,
string& value) const
{
string str=name;
image_template->get(str,value);
}
/**
* Gets a string based Image attribute
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined
*/
void get_template_attribute(
const char * name,
int& value) const
{
string str=name;
image_template->get(str,value);
}
/**
* Removes an Image attribute
* @param name of the attribute
*/
int remove_template_attribute(const string& name)
{
return image_template->erase(name);
}
/**
* Adds a new attribute to the template (replacing it if
* already defined), the image's mutex SHOULD be locked
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
*/
int replace_template_attribute(
const string& name,
const string& value)
{
SingleAttribute * sattr;
image_template->erase(name);
sattr = new SingleAttribute(name,value);
image_template->set(sattr);
return 0;
}
/**
* Generates the source path for the repository.
* @param uid of the image owner
@ -397,16 +310,6 @@ private:
*/
int running_vms;
// -------------------------------------------------------------------------
// Image Attributes
// -------------------------------------------------------------------------
/**
* The Image template, holds the Image attributes.
*/
ImageTemplate * image_template;
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************

View File

@ -19,6 +19,7 @@
#include "ObjectSQL.h"
#include "ObjectXML.h"
#include "Template.h"
#include <pthread.h>
#include <string.h>
@ -32,14 +33,13 @@ using namespace std;
* implementation assumes that the mutex IS LOCKED when the class destructor
* is called.
*/
class PoolObjectSQL : public ObjectSQL, public ObjectXML
{
public:
PoolObjectSQL(int id, const string& _name, int _uid,const char *_table)
:ObjectSQL(),ObjectXML(),oid(id),name(_name),uid(_uid),
valid(true),table(_table)
valid(true),obj_template(0),table(_table)
{
pthread_mutex_init(&mutex,0);
};
@ -119,6 +119,104 @@ public:
*/
virtual int from_xml(const string &xml_str) = 0;
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
string& name,
vector<const Attribute*>& values) const
{
return obj_template->get(name,values);
};
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
const char *name,
vector<const Attribute*>& values) const
{
string str=name;
return obj_template->get(str,values);
};
/**
* Gets a string based attribute (single)
* @param name of the attribute
* @param value of the attribute (a string), will be "" if not defined or
* not a single attribute
*/
void get_template_attribute(
const char * name,
string& value) const
{
string str=name;
obj_template->get(str,value);
}
/**
* Gets an int based attribute (single)
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined or
* not a single attribute
*/
void get_template_attribute(
const char * name,
int& value) const
{
string str=name;
obj_template->get(str,value);
}
/**
* Adds a new attribute to the template (replacing it if
* already defined), the object's mutex SHOULD be locked
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
*/
int replace_template_attribute(
const string& name,
const string& value)
{
SingleAttribute * sattr;
obj_template->erase(name);
sattr = new SingleAttribute(name,value);
obj_template->set(sattr);
return 0;
}
/**
* Generates a XML string for the template of the Object
* @param xml the string to store the XML description.
*/
void template_to_xml(string &xml) const
{
obj_template->to_xml(xml);
}
/**
* Removes an Image attribute
* @param name of the attribute
*/
int remove_template_attribute(const string& name)
{
return obj_template->erase(name);
}
protected:
/**
@ -198,6 +296,11 @@ protected:
*/
bool valid;
/**
* Template for this object, will be allocated if needed
*/
Template * obj_template;
private:
/**

View File

@ -532,93 +532,6 @@ public:
// ------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
string& name,
vector<const Attribute*>& values) const
{
return vm_template->get(name,values);
};
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
const char *name,
vector<const Attribute*>& values) const
{
string str=name;
return vm_template->get(str,values);
};
/**
* Gets a string based VM attribute (single)
* @param name of the attribute
* @param value of the attribute (a string), will be "" if not defined or
* not a single attribute
*/
void get_template_attribute(
const char * name,
string& value) const
{
string str=name;
vm_template->get(str,value);
}
/**
* Gets an int based VM attribute (single)
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined or
* not a single attribute
*/
void get_template_attribute(
const char * name,
int& value) const
{
string str=name;
vm_template->get(str,value);
}
/**
* Adds a new attribute to the template (replacing it if
* already defined), the vm's mutex SHOULD be locked
* @param name of the new attribute
* @param value of the new attribute
* @return 0 on success
*/
int replace_template_attribute(
string& name,
string& value)
{
SingleAttribute * sattr;
vm_template->erase(name);
sattr = new SingleAttribute(name,value);
vm_template->set(sattr);
return 0;
}
/**
* Generates a XML string for the template of the VM
* @param xml the string to store the XML description.
*/
void template_to_xml(string &xml) const
{
vm_template->to_xml(xml);
}
/**
* Parse a string and substitute variables (e.g. $NAME) using the VM
* template values:
@ -626,8 +539,8 @@ public:
* @param parsed, the resulting parsed string
* @return 0 on success.
*/
int parse_template_attribute(const string& attribute,
string& parsed);
int parse_template_attribute(const string& attribute, string& parsed);
// ------------------------------------------------------------------------
// States
// ------------------------------------------------------------------------
@ -775,14 +688,6 @@ private:
// -------------------------------------------------------------------------
// Virtual Machine Description
// -------------------------------------------------------------------------
/**
* The Virtual Machine template, holds the VM attributes.
*/
VirtualMachineTemplate* vm_template;
// Dynamic state of the Virtual Machine
/**
* The state of the virtual machine.
*/

View File

@ -19,7 +19,6 @@
#include "PoolSQL.h"
#include "VirtualNetworkTemplate.h"
#include "Leases.h"
#include <vector>
@ -33,10 +32,11 @@ using namespace std;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class VirtualNetworkTemplate;
/**
* The Virtual Network class. It represents a Virtual Network at manages its leases.
* One lease is formed by one IP and one MAC address.
* The Virtual Network class. It represents a Virtual Network at manages its
* leases. One lease is formed by one IP and one MAC address.
* MAC address are derived from IP addresses.
*/
class VirtualNetwork : public PoolObjectSQL
@ -176,63 +176,6 @@ public:
*/
int nic_attribute(VectorAttribute * nic, int vid);
//------------------------------------------------------------------------
// Template
// ------------------------------------------------------------------------
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
string& name,
vector<const Attribute*>& values) const
{
return vn_template->get(name,values);
};
/**
* Gets the values of a template attribute
* @param name of the attribute
* @param values of the attribute
* @return the number of values
*/
int get_template_attribute(
const char *name,
vector<const Attribute*>& values) const
{
string str=name;
return vn_template->get(str,values);
};
/**
* Gets a string based VN attribute
* @param name of the attribute
* @param value of the attribute (a string), will be "" if not defined
*/
void get_template_attribute(
const char * name,
string& value) const
{
string str=name;
vn_template->get(str,value);
}
/**
* Gets a string based VN attribute
* @param name of the attribute
* @param value of the attribute (an int), will be 0 if not defined
*/
void get_template_attribute(
const char * name,
int& value) const
{
string str=name;
vn_template->get(str,value);
}
private:
// -------------------------------------------------------------------------
@ -280,11 +223,6 @@ private:
*/
Leases * leases;
/**
* The Virtual Network template, holds the VNW attributes.
*/
VirtualNetworkTemplate * vn_template;
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************

View File

@ -40,11 +40,18 @@ Host::Host(
vmm_mad_name(_vmm_mad_name),
tm_mad_name(_tm_mad_name),
last_monitored(0),
cluster(_cluster),
host_template()
{}
cluster(_cluster)
{
obj_template = new HostTemplate;
}
Host::~Host(){}
Host::~Host()
{
if ( obj_template != 0 )
{
delete obj_template;
}
}
/* ************************************************************************ */
/* Host :: Database Access Functions */
@ -167,7 +174,7 @@ int Host::update_info(string &parse_str)
char * error_msg;
int rc;
rc = host_template.parse(parse_str, &error_msg);
rc = obj_template->parse(parse_str, &error_msg);
if ( rc != 0 )
{
@ -222,7 +229,7 @@ string& Host::to_xml(string& xml) const
"<LAST_MON_TIME>" << last_monitored << "</LAST_MON_TIME>" <<
"<CLUSTER>" << cluster << "</CLUSTER>" <<
host_share.to_xml(share_xml) <<
host_template.to_xml(template_xml) <<
obj_template->to_xml(template_xml) <<
"</HOST>";
xml = oss.str();
@ -276,7 +283,7 @@ int Host::from_xml(const string& xml)
return -1;
}
rc += host_template.from_xml_node( content[0] );
rc += obj_template->from_xml_node( content[0] );
if (rc != 0)
{

View File

@ -47,19 +47,19 @@ Image::Image(int _uid,
{
if (_image_template != 0)
{
image_template = _image_template;
obj_template = _image_template;
}
else
{
image_template = new ImageTemplate;
obj_template = new ImageTemplate;
}
}
Image::~Image()
{
if (image_template != 0)
if (obj_template != 0)
{
delete image_template;
delete obj_template;
}
}
@ -132,7 +132,7 @@ int Image::insert(SqlDB *db, string& error_str)
get_template_attribute("PUBLIC", public_attr);
image_template->erase("PUBLIC");
obj_template->erase("PUBLIC");
TO_UPPER(public_attr);
@ -142,7 +142,7 @@ int Image::insert(SqlDB *db, string& error_str)
get_template_attribute("PERSISTENT", persistent_attr);
image_template->erase("PERSISTENT");
obj_template->erase("PERSISTENT");
TO_UPPER(persistent_attr);
@ -163,7 +163,7 @@ int Image::insert(SqlDB *db, string& error_str)
{
SingleAttribute * dev_att = new SingleAttribute("DEV_PREFIX",
ImagePool::default_dev_prefix());
image_template->set(dev_att);
obj_template->set(dev_att);
}
// ------------ PATH & SOURCE --------------------
@ -359,7 +359,7 @@ string& Image::to_xml(string& xml) const
"<SOURCE>" << source << "</SOURCE>" <<
"<STATE>" << state << "</STATE>" <<
"<RUNNING_VMS>" << running_vms << "</RUNNING_VMS>" <<
image_template->to_xml(template_xml) <<
obj_template->to_xml(template_xml) <<
"</IMAGE>";
xml = oss.str();
@ -406,7 +406,7 @@ int Image::from_xml(const string& xml)
return -1;
}
rc += image_template->from_xml_node(content[0]);
rc += obj_template->from_xml_node(content[0]);
if (rc != 0)
{

View File

@ -19,6 +19,7 @@
#include "NebulaLog.h"
#include "Nebula.h"
#include "VirtualNetworkTemplate.h"
#include "AuthManager.h"
/* -------------------------------------------------------------------------- */

View File

@ -16,6 +16,7 @@
#include "RequestManager.h"
#include "NebulaLog.h"
#include "VirtualNetworkTemplate.h"
#include "AuthManager.h"

View File

@ -15,6 +15,7 @@
/* -------------------------------------------------------------------------- */
#include "RequestManager.h"
#include "VirtualNetworkTemplate.h"
#include "NebulaLog.h"
#include "Nebula.h"

View File

@ -58,11 +58,11 @@ VirtualMachine::VirtualMachine(int id,
{
if (_vm_template != 0)
{
vm_template = _vm_template;
obj_template = _vm_template;
}
else
{
vm_template = new VirtualMachineTemplate;
obj_template = new VirtualMachineTemplate;
}
}
@ -83,9 +83,9 @@ VirtualMachine::~VirtualMachine()
delete _log;
}
if ( vm_template != 0 )
if ( obj_template != 0 )
{
delete vm_template;
delete obj_template;
}
}
@ -191,7 +191,7 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
attr = new SingleAttribute("VMID",value);
vm_template->set(attr);
obj_template->set(attr);
get_template_attribute("NAME",name);
@ -202,7 +202,7 @@ int VirtualMachine::insert(SqlDB * db, string& error_str)
name = oss.str();
attr = new SingleAttribute("NAME",name);
vm_template->set(attr);
obj_template->set(attr);
}
this->name = name;
@ -301,7 +301,7 @@ int VirtualMachine::parse_context(string& error_str)
string * str;
string parsed;
num = vm_template->remove("CONTEXT", array_context);
num = obj_template->remove("CONTEXT", array_context);
if ( num == 0 )
{
@ -352,7 +352,7 @@ int VirtualMachine::parse_context(string& error_str)
context_parsed->replace("TARGET", dev_prefix);
}
vm_template->set(context_parsed);
obj_template->set(context_parsed);
}
/* --- Delete old context attributes --- */
@ -378,7 +378,7 @@ void VirtualMachine::parse_graphics()
vector<Attribute *> array_graphics;
VectorAttribute * graphics;
num = vm_template->get("GRAPHICS", array_graphics);
num = obj_template->get("GRAPHICS", array_graphics);
if ( num == 0 )
{
@ -425,7 +425,7 @@ int VirtualMachine::parse_requirements(string& error_str)
string parsed;
num = vm_template->remove("REQUIREMENTS", array_reqs);
num = obj_template->remove("REQUIREMENTS", array_reqs);
if ( num == 0 )
{
@ -452,7 +452,7 @@ int VirtualMachine::parse_requirements(string& error_str)
SingleAttribute * reqs_parsed;
reqs_parsed = new SingleAttribute("REQUIREMENTS",parsed);
vm_template->set(reqs_parsed);
obj_template->set(reqs_parsed);
}
/* --- Delete old requirements attributes --- */
@ -677,7 +677,7 @@ int VirtualMachine::get_disk_images(string& error_str)
Nebula& nd = Nebula::instance();
ipool = nd.get_ipool();
num_disks = vm_template->get("DISK",disks);
num_disks = obj_template->get("DISK",disks);
for(int i=0, index=0; i<num_disks; i++)
{
@ -814,7 +814,7 @@ int VirtualMachine::get_network_leases()
Nebula& nd = Nebula::instance();
vnpool = nd.get_vnpool();
num_nics = vm_template->get("NIC",nics);
num_nics = obj_template->get("NIC",nics);
for(int i=0; i<num_nics; i++)
{
@ -964,7 +964,7 @@ int VirtualMachine::save_disk(int disk_id, int img_id)
istringstream iss;
num_disks = vm_template->get("DISK",disks);
num_disks = obj_template->get("DISK",disks);
for(int i=0; i<num_disks; i++, iss.clear())
{
@ -1100,7 +1100,7 @@ string& VirtualMachine::to_xml(string& xml) const
<< "<CPU>" << cpu << "</CPU>"
<< "<NET_TX>" << net_tx << "</NET_TX>"
<< "<NET_RX>" << net_rx << "</NET_RX>"
<< vm_template->to_xml(template_xml);
<< obj_template->to_xml(template_xml);
if ( hasHistory() )
{
@ -1156,7 +1156,7 @@ int VirtualMachine::from_xml(const string &xml_str)
}
// Virtual Machine template
rc += vm_template->from_xml_node(content[0]);
rc += obj_template->from_xml_node(content[0]);
// Last history entry
content.clear();

View File

@ -17,6 +17,7 @@
#include "VirtualNetwork.h"
#include "VirtualNetworkPool.h"
#include "VirtualNetworkTemplate.h"
#include "NebulaLog.h"
#include "RangedLeases.h"
@ -40,11 +41,11 @@ VirtualNetwork::VirtualNetwork(int uid,
{
if (_vn_template != 0)
{
vn_template = _vn_template;
obj_template = _vn_template;
}
else
{
vn_template = new VirtualNetworkTemplate;
obj_template = new VirtualNetworkTemplate;
}
};
@ -58,9 +59,9 @@ VirtualNetwork::~VirtualNetwork()
delete leases;
}
if (vn_template != 0)
if (obj_template != 0)
{
delete vn_template;
delete obj_template;
}
}
@ -263,7 +264,7 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str)
public_vnet = (pub == "YES");
vn_template->erase("PUBLIC");
obj_template->erase("PUBLIC");
//--------------------------------------------------------------------------
// Get the leases
@ -307,7 +308,7 @@ int VirtualNetwork::insert(SqlDB * db, string& error_str)
oss << default_size;
attribute = new SingleAttribute("NETWORK_SIZE",oss.str());
vn_template->set(attribute);
obj_template->set(attribute);
size = default_size;
}
@ -506,7 +507,7 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const
"<BRIDGE>" << bridge << "</BRIDGE>" <<
"<PUBLIC>" << public_vnet << "</PUBLIC>" <<
"<TOTAL_LEASES>"<< total_leases << "</TOTAL_LEASES>"<<
vn_template->to_xml(template_xml);
obj_template->to_xml(template_xml);
if (extended && leases != 0)
{
@ -553,7 +554,7 @@ int VirtualNetwork::from_xml(const string &xml_str)
}
// Virtual Network template
rc += vn_template->from_xml_node( content[0] );
rc += obj_template->from_xml_node( content[0] );
if (rc != 0)
{

View File

@ -19,6 +19,7 @@
#include <stdlib.h>
#include "VirtualNetworkPool.h"
#include "VirtualNetworkTemplate.h"
#include "PoolTest.h"
#include "ObjectXML.h"