mirror of
https://github.com/OpenNebula/one.git
synced 2025-03-30 22:50:10 +03:00
feature #266: Changes in VirtualNetwork insert and allocate methods
This commit is contained in:
parent
52e3b6f5e5
commit
fdc007cb46
@ -75,7 +75,7 @@ public:
|
||||
{
|
||||
return (public_vnet == 1);
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Publish or unpublish a virtual network
|
||||
* @param pub true to publish the image
|
||||
@ -286,20 +286,6 @@ private:
|
||||
*/
|
||||
VirtualNetworkTemplate vn_template;
|
||||
|
||||
// *************************************************************************
|
||||
// Non persistent data members from Nebula.conf
|
||||
// *************************************************************************
|
||||
|
||||
/**
|
||||
* MAC prefix for this OpenNebula site
|
||||
*/
|
||||
unsigned int mac_prefix;
|
||||
|
||||
/**
|
||||
* Default size for virtual networks
|
||||
*/
|
||||
int default_size;
|
||||
|
||||
// *************************************************************************
|
||||
// DataBase implementation (Private)
|
||||
// *************************************************************************
|
||||
@ -376,7 +362,7 @@ protected:
|
||||
// Constructor
|
||||
//**************************************************************************
|
||||
|
||||
VirtualNetwork(unsigned int _mac_prefix, int _default_size);
|
||||
VirtualNetwork();
|
||||
|
||||
~VirtualNetwork();
|
||||
|
||||
|
@ -147,16 +147,34 @@ public:
|
||||
*/
|
||||
int dump(ostringstream& oss, const string& where);
|
||||
|
||||
/**
|
||||
* Get the mac prefix
|
||||
* @return the mac prefix
|
||||
*/
|
||||
static const unsigned int& mac_prefix()
|
||||
{
|
||||
return _mac_prefix;
|
||||
};
|
||||
|
||||
/**
|
||||
* Get the default network size
|
||||
* @return the size
|
||||
*/
|
||||
static const unsigned int& default_size()
|
||||
{
|
||||
return _default_size;
|
||||
};
|
||||
|
||||
private:
|
||||
/**
|
||||
* Holds the system-wide MAC prefix
|
||||
*/
|
||||
unsigned int mac_prefix;
|
||||
static unsigned int _mac_prefix;
|
||||
|
||||
/**
|
||||
* Default size for Virtual Networks
|
||||
*/
|
||||
unsigned int default_size;
|
||||
static unsigned int _default_size;
|
||||
|
||||
/**
|
||||
* Factory method to produce VN objects
|
||||
@ -164,7 +182,7 @@ private:
|
||||
*/
|
||||
PoolObjectSQL * create()
|
||||
{
|
||||
return new VirtualNetwork(mac_prefix, default_size);
|
||||
return new VirtualNetwork();
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -197,7 +197,7 @@ int Host::update(SqlDB *db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
// Update the Template
|
||||
// Update the Template needed by the monitoring action from IM
|
||||
rc = host_template.update(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
|
@ -253,16 +253,7 @@ error_common:
|
||||
|
||||
int Image::update(SqlDB *db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = insert_replace(db, true);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
return rc;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return insert_replace(db, true);;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
@ -553,11 +553,7 @@ int VirtualMachine::parse_requirements()
|
||||
|
||||
int VirtualMachine::update(SqlDB * db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
rc = insert_replace(db, true);
|
||||
|
||||
return rc;
|
||||
return insert_replace(db, true);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------ */
|
||||
|
@ -16,6 +16,8 @@
|
||||
|
||||
|
||||
#include "VirtualNetwork.h"
|
||||
#include "VirtualNetworkPool.h"
|
||||
|
||||
#include "NebulaLog.h"
|
||||
#include "RangedLeases.h"
|
||||
#include "FixedLeases.h"
|
||||
@ -24,15 +26,13 @@
|
||||
/* Virtual Network :: Constructor/Destructor */
|
||||
/* ************************************************************************** */
|
||||
|
||||
VirtualNetwork::VirtualNetwork(unsigned int mp, int ds):
|
||||
VirtualNetwork::VirtualNetwork():
|
||||
PoolObjectSQL(-1),
|
||||
name(""),
|
||||
uid(-1),
|
||||
bridge(""),
|
||||
type(UNINITIALIZED),
|
||||
leases(0),
|
||||
mac_prefix(mp),
|
||||
default_size(ds){};
|
||||
leases(0){};
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -104,6 +104,9 @@ int VirtualNetwork::select(SqlDB * db)
|
||||
|
||||
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));
|
||||
|
||||
@ -254,25 +257,70 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
{
|
||||
ostringstream ose;
|
||||
int rc;
|
||||
|
||||
string public_attr;
|
||||
|
||||
string pub;
|
||||
string s_type;
|
||||
|
||||
unsigned int default_size = VirtualNetworkPool::default_size();
|
||||
unsigned int mac_prefix = VirtualNetworkPool::mac_prefix();
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// VirtualNetwork Attributes from the template
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
// ------------ TYPE ----------------------
|
||||
get_template_attribute("TYPE",s_type);
|
||||
|
||||
transform(s_type.begin(),s_type.end(),s_type.begin(),(int(*)(int))toupper);
|
||||
|
||||
if (s_type == "RANGED")
|
||||
{
|
||||
type = VirtualNetwork::RANGED;
|
||||
}
|
||||
else if ( s_type == "FIXED")
|
||||
{
|
||||
type = VirtualNetwork::FIXED;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto error_type;
|
||||
}
|
||||
|
||||
// ------------ NAME ----------------------
|
||||
|
||||
get_template_attribute("NAME",name);
|
||||
|
||||
if (name.empty())
|
||||
{
|
||||
goto error_name;
|
||||
}
|
||||
|
||||
// ------------ BRIDGE --------------------
|
||||
|
||||
get_template_attribute("BRIDGE",bridge);
|
||||
|
||||
if (bridge.empty())
|
||||
{
|
||||
goto error_bridge;
|
||||
}
|
||||
|
||||
// ------------ PUBLIC --------------------
|
||||
|
||||
get_template_attribute("PUBLIC", pub);
|
||||
|
||||
transform (pub.begin(), pub.end(), pub.begin(), (int(*)(int))toupper);
|
||||
|
||||
public_vnet = (pub == "YES");
|
||||
|
||||
vn_template.erase("PUBLIC");
|
||||
|
||||
// ------------ TEMPLATE --------------------
|
||||
|
||||
if ( vn_template.id == -1 )
|
||||
{
|
||||
vn_template.id = oid;
|
||||
}
|
||||
|
||||
// ------------ PUBLIC --------------------
|
||||
|
||||
get_template_attribute("PUBLIC", public_attr);
|
||||
|
||||
transform (public_attr.begin(), public_attr.end(), public_attr.begin(),
|
||||
(int(*)(int))toupper);
|
||||
|
||||
public_vnet = (public_attr == "YES");
|
||||
vn_template.erase("PUBLIC");
|
||||
|
||||
// Insert the template first
|
||||
rc = vn_template.insert(db);
|
||||
|
||||
if ( rc != 0 )
|
||||
@ -280,7 +328,10 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
goto error_template;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Insert the Virtual Network
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
rc = insert_replace(db, false);
|
||||
|
||||
if ( rc != 0 )
|
||||
@ -288,7 +339,10 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
goto error_update;
|
||||
}
|
||||
|
||||
//Get the leases
|
||||
//--------------------------------------------------------------------------
|
||||
// Get the leases
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
if (type == VirtualNetwork::RANGED)
|
||||
{
|
||||
string nclass = "";
|
||||
@ -331,7 +385,7 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
mac_prefix,
|
||||
naddr);
|
||||
}
|
||||
else if(type == VirtualNetwork::FIXED)
|
||||
else // VirtualNetwork::FIXED
|
||||
{
|
||||
vector<const Attribute *> vector_leases;
|
||||
|
||||
@ -342,10 +396,6 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
mac_prefix,
|
||||
vector_leases);
|
||||
}
|
||||
else
|
||||
{
|
||||
goto error_type;
|
||||
}
|
||||
|
||||
if (leases == 0)
|
||||
{
|
||||
@ -354,6 +404,18 @@ int VirtualNetwork::insert(SqlDB * db)
|
||||
|
||||
return 0;
|
||||
|
||||
error_type:
|
||||
ose << "Wrong type in template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_name:
|
||||
ose << "No NAME in template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_bridge:
|
||||
ose << "No BRIDGE in template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
|
||||
error_template:
|
||||
ose << "Can not insert in DB template for Virtual Network id " << oid;
|
||||
goto error_common;
|
||||
@ -363,10 +425,6 @@ error_update:
|
||||
vn_template.drop(db);
|
||||
goto error_common;
|
||||
|
||||
error_type:
|
||||
ose << "Wrong type of Virtual Network: " << type;
|
||||
goto error_leases;
|
||||
|
||||
error_addr:
|
||||
ose << "Network address is not defined nid: " << oid;
|
||||
goto error_leases;
|
||||
@ -388,17 +446,7 @@ error_common:
|
||||
|
||||
int VirtualNetwork::update(SqlDB * db)
|
||||
{
|
||||
int rc;
|
||||
|
||||
// Update the template first
|
||||
rc = vn_template.update(db);
|
||||
|
||||
if ( rc == 0 )
|
||||
{
|
||||
rc = insert_replace(db, true);
|
||||
}
|
||||
|
||||
return rc;
|
||||
return insert_replace(db, true);
|
||||
}
|
||||
|
||||
int VirtualNetwork::insert_replace(SqlDB *db, bool replace)
|
||||
|
@ -20,13 +20,18 @@
|
||||
#include <sstream>
|
||||
#include <ctype.h>
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
unsigned int VirtualNetworkPool::_mac_prefix;
|
||||
unsigned int VirtualNetworkPool::_default_size;
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
VirtualNetworkPool::VirtualNetworkPool(SqlDB * db,
|
||||
const string& prefix,
|
||||
int _default_size):
|
||||
PoolSQL(db,VirtualNetwork::table),
|
||||
mac_prefix(0),
|
||||
default_size(_default_size)
|
||||
int __default_size):
|
||||
PoolSQL(db,VirtualNetwork::table)
|
||||
{
|
||||
istringstream iss;
|
||||
size_t pos = 0;
|
||||
@ -35,6 +40,9 @@ VirtualNetworkPool::VirtualNetworkPool(SqlDB * db,
|
||||
|
||||
string mac = prefix;
|
||||
|
||||
_mac_prefix = 0;
|
||||
_default_size = __default_size;
|
||||
|
||||
while ( (pos = mac.find(':')) != string::npos )
|
||||
{
|
||||
mac.replace(pos,1," ");
|
||||
@ -45,16 +53,16 @@ VirtualNetworkPool::VirtualNetworkPool(SqlDB * db,
|
||||
{
|
||||
NebulaLog::log("VNM",Log::ERROR,
|
||||
"Wrong MAC prefix format, using default");
|
||||
mac_prefix = 1; //"00:01"
|
||||
_mac_prefix = 1; //"00:01"
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
iss.str(mac);
|
||||
|
||||
iss >> hex >> mac_prefix >> ws >> hex >> tmp >> ws;
|
||||
mac_prefix <<= 8;
|
||||
mac_prefix += tmp;
|
||||
iss >> hex >> _mac_prefix >> ws >> hex >> tmp >> ws;
|
||||
_mac_prefix <<= 8;
|
||||
_mac_prefix += tmp;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
@ -68,59 +76,27 @@ int VirtualNetworkPool::allocate (
|
||||
VirtualNetwork * vn;
|
||||
char * error_msg;
|
||||
int rc;
|
||||
ostringstream oss;
|
||||
|
||||
string name;
|
||||
string bridge;
|
||||
|
||||
string s_type;
|
||||
|
||||
// Build a new Virtual Network object
|
||||
vn = new VirtualNetwork(mac_prefix, default_size);
|
||||
vn = new VirtualNetwork();
|
||||
|
||||
vn->uid = uid;
|
||||
vn->uid = uid;
|
||||
|
||||
rc = vn->vn_template.parse(stemplate,&error_msg);
|
||||
|
||||
if ( rc != 0 )
|
||||
{
|
||||
ostringstream oss;
|
||||
|
||||
oss << error_msg;
|
||||
NebulaLog::log("VNM", Log::ERROR, oss);
|
||||
free(error_msg);
|
||||
|
||||
delete vn;
|
||||
|
||||
return -2;
|
||||
}
|
||||
|
||||
// Information about the VN needs to be extracted from the template
|
||||
vn->get_template_attribute("TYPE",s_type);
|
||||
|
||||
transform(s_type.begin(),s_type.end(),s_type.begin(),(int(*)(int))toupper);
|
||||
|
||||
if (s_type == "RANGED")
|
||||
{
|
||||
vn->type = VirtualNetwork::RANGED;
|
||||
}
|
||||
else if ( s_type == "FIXED")
|
||||
{
|
||||
vn->type = VirtualNetwork::FIXED;
|
||||
}
|
||||
else
|
||||
{
|
||||
NebulaLog::log("VNM", Log::ERROR, "Wrong type for VirtualNetwork "
|
||||
"template");
|
||||
delete vn;
|
||||
|
||||
return -3;
|
||||
return -1;
|
||||
}
|
||||
|
||||
vn->get_template_attribute("NAME",name);
|
||||
vn->name = name;
|
||||
|
||||
vn->get_template_attribute("BRIDGE",bridge);
|
||||
vn->bridge = bridge;
|
||||
|
||||
// Insert the VN in the pool so we have a valid OID
|
||||
|
||||
*oid = PoolSQL::allocate(vn);
|
||||
|
@ -193,15 +193,15 @@ public:
|
||||
|
||||
// Check template attribute
|
||||
rc = allocate(3);
|
||||
CPPUNIT_ASSERT( rc == -3 );
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
|
||||
// Parser error for Vnet template
|
||||
//TODO: Check memory leak for allocating strings in template parser
|
||||
rc = allocate(4);
|
||||
CPPUNIT_ASSERT( rc == -2 );
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
|
||||
rc = allocate(5);
|
||||
CPPUNIT_ASSERT( rc == -3 );
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
|
||||
rc = allocate(6);
|
||||
CPPUNIT_ASSERT( rc == -1 );
|
||||
|
Loading…
x
Reference in New Issue
Block a user