feature #206: Moved the Virtual Network to new DB classes

This commit is contained in:
Ruben S. Montero committed 2010-04-10 22:15:47 +02:00
1 parent 4f74172044
commit aeb27c1004
9 files changed
+556 -632

No files matched your search

+98 -98
View File
@@ -19,30 +19,30 @@
#include "Nebula.h"
FixedLeases::FixedLeases(
SqliteDB * db,
int _oid,
unsigned int _mac_prefix,
vector<const Attribute*>& vector_leases):
Leases(db,_oid,0),mac_prefix(_mac_prefix),current(leases.begin())
{
SqlDB * db,
int _oid,
unsigned int _mac_prefix,
vector<const Attribute*>& vector_leases):
Leases(db,_oid,0),mac_prefix(_mac_prefix),current(leases.begin())
{
const VectorAttribute * single_attr_lease;
string _mac;
string _ip;
size = vector_leases.size();
for (unsigned long i=0; i < size ;i++)
{
single_attr_lease = dynamic_cast<const VectorAttribute *>
(vector_leases[i]);
single_attr_lease = dynamic_cast<const VectorAttribute *>
(vector_leases[i]);
if( single_attr_lease )
{
_ip = single_attr_lease->vector_value("IP");
_mac = single_attr_lease->vector_value("MAC");
add(_ip,_mac,-1,false);
}
add(_ip,_mac,-1,false);
}
}
}
@@ -54,24 +54,24 @@ int FixedLeases::add(const string& ip, const string& mac, int vid, bool used)
ostringstream oss;
unsigned int _ip;
unsigned int _mac [2];
int rc;
if ( Leases::Lease::ip_to_number(ip,_ip) )
{
goto error_ip;
goto error_ip;
}
if (mac.empty())
{
_mac[Lease::PREFIX] = mac_prefix;
_mac[Lease::SUFFIX] = _ip;
_mac[Lease::PREFIX] = mac_prefix;
_mac[Lease::SUFFIX] = _ip;
}
else if (Leases::Lease::mac_to_number(mac,_mac))
{
goto error_mac;
goto error_mac;
}
oss << "INSERT OR REPLACE INTO " << table << " "<< db_names <<" VALUES (" <<
oid << "," <<
_ip << "," <<
@@ -81,19 +81,19 @@ int FixedLeases::add(const string& ip, const string& mac, int vid, bool used)
used << ")";
rc = db->exec(oss);
if ( rc == 0 )
{
leases.insert(make_pair(_ip,new Lease(_ip,_mac,vid,used)));
leases.insert(make_pair(_ip,new Lease(_ip,_mac,vid,used)));
}
return rc;
error_mac:
oss.str("");
oss << "Error inserting lease, MAC = " << mac;
goto error_common;
error_ip:
oss.str("");
oss << "Error inserting lease, IP = " << ip;
@@ -108,33 +108,33 @@ error_common:
int FixedLeases::del(const string& ip)
{
unsigned int _ip;
unsigned int _ip;
ostringstream oss;
map<unsigned int, Lease *>::iterator it_ip;
// Remove lease from leases map
if ( Leases::Lease::ip_to_number(ip,_ip) )
{
return 0; //Wrong format, not leased
return 0; //Wrong format, not leased
}
it_ip = leases.find(_ip);
if (it_ip == leases.end())
{
return 0; //Not in the map, not leased
return 0; //Not in the map, not leased
}
// Flip used flag to false
it_ip->second->used = false;
it_ip->second->vid = -1;
oss << "UPDATE " << table << " SET used='0', vid='-1' "
<< " WHERE ip='" << _ip <<"'";
oss << "UPDATE " << table << " SET used='0', vid='-1' "
<< " WHERE ip='" << _ip <<"'";
return db->exec(oss);
}
@@ -142,45 +142,45 @@ int FixedLeases::del(const string& ip)
/* -------------------------------------------------------------------------- */
int FixedLeases::get(int vid, string& ip, string& mac)
{
int rc = -1;
if (leases.empty())
{
return -1;
}
for(unsigned int i=0 ;i<size; i++,current++)
{
if (current == leases.end())
{
current = leases.begin();
}
{
int rc = -1;
if (current->second->used == false)
{
ostringstream oss;
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE ip='" << current->second->ip <<"'";
if (leases.empty())
{
return -1;
}
rc = db->exec(oss);
if ( rc == 0 )
{
current->second->used = true;
current->second->vid = vid;
current->second->to_string(ip,mac);
current++;
}
break;
}
}
return rc;
for(unsigned int i=0 ;i<size; i++,current++)
{
if (current == leases.end())
{
current = leases.begin();
}
if (current->second->used == false)
{
ostringstream oss;
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE ip='" << current->second->ip <<"'";
rc = db->exec(oss);
if ( rc == 0 )
{
current->second->used = true;
current->second->vid = vid;
current->second->to_string(ip,mac);
current++;
}
break;
}
}
return rc;
}
/* -------------------------------------------------------------------------- */
@@ -188,45 +188,45 @@ int FixedLeases::get(int vid, string& ip, string& mac)
int FixedLeases::set(int vid, const string& ip, string& mac)
{
map<unsigned int,Lease *>::iterator it;
ostringstream oss;
unsigned int num_ip;
int rc;
rc = Leases::Lease::ip_to_number(ip,num_ip);
if (rc != 0)
{
return -1;
}
map<unsigned int,Lease *>::iterator it;
ostringstream oss;
unsigned int num_ip;
int rc;
rc = Leases::Lease::ip_to_number(ip,num_ip);
if (rc != 0)
{
return -1;
}
it=leases.find(num_ip);
if (it == leases.end()) //it does not exists in the net
{
return -1;
return -1;
}
else if (it->second->used) //it is in use
{
return -1;
return -1;
}
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE ip='" << it->second->ip <<"'";
oss << "UPDATE " << table << " SET used='1', vid='" << vid
<< "' WHERE ip='" << it->second->ip <<"'";
rc = db->exec(oss);
if ( rc != 0 )
{
return -1;
return -1;
}
it->second->used = true;
it->second->vid = vid;
Leases::Lease::mac_to_string(it->second->mac,mac);
return 0;
}
+36 -56
View File
@@ -187,31 +187,31 @@ ostream& operator<<(ostream& os, Leases::Lease& _lease)
string xml;
os << _lease.to_xml(xml);
return os;
}
string& Leases::Lease::to_str(string& str) const
{
string ip;
string mac;
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 << " USED = " << used;
os << left << " VID = " << vid;
str = os.str();
return str;
@@ -220,14 +220,14 @@ string& Leases::Lease::to_str(string& str) const
string& Leases::Lease::to_xml(string& str) const
{
string ip;
string mac;
string ip;
string mac;
ostringstream os;
to_string(ip,mac);
os <<
os <<
"<LEASE>" <<
"<IP>"<< ip << "</IP>" <<
"<MAC>" << mac << "</MAC>" <<
@@ -251,13 +251,13 @@ const char * Leases::table = "leases";
const char * Leases::db_names = "(oid,ip,mac_prefix,mac_suffix,vid,used)";
const char * Leases::db_bootstrap = "CREATE TABLE leases ("
"oid INTEGER,ip INTEGER, mac_prefix INTEGER,mac_suffix INTEGER,"
"oid INTEGER,ip INTEGER, mac_prefix INTEGER,mac_suffix INTEGER,"
"vid INTEGER, used INTEGER, PRIMARY KEY(oid,ip))";
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::unmarshall(int num, char **names, char ** values)
int Leases::select_cb(void *nil, int num, char **values, char **names)
{
if ( (values[OID] == 0) ||
(values[IP] == 0) ||
@@ -274,65 +274,45 @@ int Leases::unmarshall(int num, char **names, char ** values)
unsigned int ip;
int vid;
bool used;
istringstream iss;
iss.str(values[IP]);
iss >> ip;
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)));
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
extern "C" int leases_select_cb (
void * _leases,
int num,
char ** values,
char ** names)
{
Leases * leases;
leases = static_cast<Leases *>(_leases);
if (leases == 0)
{
return -1;
}
return leases->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::select(SqliteDB * db)
int Leases::select(SqlDB * db)
{
ostringstream oss;
int rc;
set_callback(static_cast<Callbackable::Callback>(&Leases::select_cb));
oss << "SELECT * FROM " << table << " WHERE oid = " << oid;
rc = db->exec(oss,leases_select_cb,(void *) this);
rc = db->exec(oss,this);
if (rc != 0)
{
@@ -342,9 +322,9 @@ int Leases::select(SqliteDB * db)
return 0;
error_id:
oss.str("");
oss.str("");
oss << "Error getting leases for network nid: " << oid;
Nebula::log("VNM", Log::ERROR, oss);
return -1;
}
@@ -352,11 +332,11 @@ error_id:
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::drop(SqliteDB * db)
int Leases::drop(SqlDB * db)
{
ostringstream oss;
// Drop all the leases
// Drop all the leases
oss << "DELETE FROM " << table << " WHERE oid=" << oid;
return db->exec(oss);
@@ -365,7 +345,7 @@ int Leases::drop(SqliteDB * db)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::insert(SqliteDB * db)
int Leases::insert(SqlDB * db)
{
Nebula::log("VNM", Log::ERROR, "Should not access to Leases.insert()");
return -1;
@@ -374,7 +354,7 @@ int Leases::insert(SqliteDB * db)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int Leases::update(SqliteDB * db)
int Leases::update(SqlDB * db)
{
Nebula::log("VNM", Log::ERROR, "Should not access to Leases.update()");
return -1;
@@ -461,7 +441,7 @@ string& Leases::to_str(string& str) const
{
os << it->second->to_str(lease_str) << endl;
}
str = os.str();
return str;
+149 -218
View File
@@ -24,7 +24,7 @@
/* Virtual Network :: Constructor/Destructor */
/* ************************************************************************** */
VirtualNetwork::VirtualNetwork(unsigned int mp, int ds):
VirtualNetwork::VirtualNetwork(unsigned int mp, int ds):
PoolObjectSQL(-1),
name(""),
uid(-1),
@@ -32,19 +32,17 @@ VirtualNetwork::VirtualNetwork(unsigned int mp, int ds):
type(UNINITIALIZED),
leases(0),
mac_prefix(mp),
default_size(ds)
{
}
default_size(ds){};
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
VirtualNetwork::~VirtualNetwork()
{
{
if (leases != 0)
{
delete leases;
}
}
}
/* ************************************************************************** */
@@ -61,13 +59,13 @@ const char * VirtualNetwork::db_bootstrap = "CREATE TABLE network_pool ("
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::unmarshall(int num, char **names, char ** values)
int VirtualNetwork::select_cb(void * nil, int num, char **values, char **names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE]) ||
(!values[BRIDGE]) ||
(num != LIMIT ))
{
return -1;
@@ -75,14 +73,14 @@ int VirtualNetwork::unmarshall(int num, char **names, char ** values)
oid = atoi(values[OID]);
uid = atoi(values[UID]);
name = values[NAME];
type = (NetworkType)atoi(values[TYPE]);
bridge = values[BRIDGE];
// Virtual Network template ID is the Network ID
// Virtual Network template ID is the Network ID
vn_template.id = oid;
return 0;
@@ -91,48 +89,31 @@ int VirtualNetwork::unmarshall(int num, char **names, char ** values)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
extern "C" int vn_select_cb (
void * _vn,
int num,
char ** values,
char ** names)
{
VirtualNetwork * vn;
vn = static_cast<VirtualNetwork *>(_vn);
if (vn == 0)
{
return -1;
}
return vn->unmarshall(num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualNetwork::select(SqliteDB * db)
int VirtualNetwork::select(SqlDB * db)
{
ostringstream oss;
ostringstream ose;
int rc;
int boid;
string network_address;
set_callback(
static_cast<Callbackable::Callback>(&VirtualNetwork::select_cb));
oss << "SELECT * FROM " << table << " WHERE oid = " << oid;
boid = oid;
oid = -1;
rc = db->exec(oss,vn_select_cb,(void *) this);
rc = db->exec(oss, this);
if ((rc != 0) || (oid != boid ))
{
goto error_id;
}
//Get the template
rc = vn_template.select(db);
@@ -144,38 +125,38 @@ int VirtualNetwork::select(SqliteDB * db)
//Get the leases
if (type == RANGED)
{
string nclass = "";
int size = 0;
string nclass = "";
int size = 0;
// retrieve specific information from template
get_template_attribute("NETWORK_ADDRESS",network_address);
if (network_address.empty())
{
goto error_addr;
goto error_addr;
}
get_template_attribute("NETWORK_SIZE",nclass);
if ( nclass == "B" )
{
size = 65534;
size = 65534;
}
else if ( nclass == "C" )
{
size = 254;
size = 254;
}
else if (!nclass.empty()) //Assume its a number
{
istringstream iss(nclass);
iss >> size;
istringstream iss(nclass);
iss >> size;
}
if (size == 0)
{
size = default_size;
size = default_size;
}
leases = new RangedLeases::RangedLeases(db,
oid,
size,
@@ -184,59 +165,59 @@ int VirtualNetwork::select(SqliteDB * db)
}
else if(type == FIXED)
{
leases = new FixedLeases(db,
leases = new FixedLeases(db,
oid,
mac_prefix);
mac_prefix);
}
else
{
goto error_type;
goto error_type;
}
if (leases == 0)
{
goto error_leases;
}
return leases->select(db);
error_id:
ose << "Error getting Virtual Network nid: " << oid;
goto error_common;
goto error_common;
error_template:
ose << "Can not get template for Virtual Network nid: " << oid;
goto error_common;
goto error_common;
error_leases:
ose << "Error getting Virtual Network leases nid: " << oid;
goto error_common;
goto error_common;
error_type:
ose << "Wrong type of Virtual Network: " << type;
ose << "Wrong type of Virtual Network: " << type;
goto error_common;
error_addr:
ose << "Network address is not defined nid: " << oid;
ose << "Network address is not defined nid: " << oid;
error_common:
Nebula::log("VNM", Log::ERROR, ose);
return -1;
Nebula::log("VNM", Log::ERROR, ose);
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::unmarshall(ostringstream& oss,
int num,
char ** names,
char ** values)
int VirtualNetwork::dump(ostringstream& oss,
int num,
char ** values,
char ** names)
{
if ((!values[OID]) ||
(!values[UID]) ||
(!values[NAME]) ||
(!values[TYPE]) ||
(!values[BRIDGE])||
(!values[BRIDGE])||
(!values[LIMIT]) ||
(num != LIMIT + 2 ))
{
@@ -258,62 +239,13 @@ int VirtualNetwork::unmarshall(ostringstream& oss,
}
/* -------------------------------------------------------------------------- */
extern "C" int vn_dump_cb (
void * _oss,
int num,
char ** values,
char ** names)
{
ostringstream * oss;
oss = static_cast<ostringstream *>(_oss);
if (oss == 0)
{
return -1;
}
return VirtualNetwork::unmarshall(*oss,num,names,values);
};
/* -------------------------------------------------------------------------- */
int VirtualNetwork::dump(SqliteDB * db, ostringstream& oss, const string& where)
{
int rc;
ostringstream cmd;
cmd << "SELECT " << VirtualNetwork::table << ".*,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,vn_dump_cb,(void *) &oss);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::insert(SqliteDB * db)
int VirtualNetwork::insert(SqlDB * db)
{
ostringstream ose;
int rc;
if ( vn_template.id == -1 )
{
vn_template.id = oid;
@@ -332,90 +264,90 @@ int VirtualNetwork::insert(SqliteDB * db)
if ( rc != 0 )
{
goto error_update;
goto error_update;
}
//Get the leases
if (type == VirtualNetwork::RANGED)
{
string nclass = "";
string naddr = "";
int size = 0;
// retrieve specific information from template
get_template_attribute("NETWORK_ADDRESS",naddr);
if (naddr.empty())
{
goto error_addr;
}
get_template_attribute("NETWORK_SIZE",nclass);
if ( nclass == "B" )
{
size = 65534;
}
else if ( nclass == "C" )
{
size = 254;
}
else if (!nclass.empty())//Assume its a number
{
istringstream iss(nclass);
iss >> size;
}
if (size == 0)
{
size = default_size;
}
leases = new RangedLeases::RangedLeases(db,
oid,
size,
mac_prefix,
naddr);
}
else if(type == VirtualNetwork::FIXED)
{
vector<const Attribute *> vector_leases;
get_template_attribute("LEASES",vector_leases);
//Get the leases
if (type == VirtualNetwork::RANGED)
{
string nclass = "";
string naddr = "";
int size = 0;
leases = new FixedLeases::FixedLeases(db,
oid,
mac_prefix,
vector_leases);
}
else
{
goto error_type;
}
if (leases == 0)
{
goto error_null_leases;
}
return 0;
// retrieve specific information from template
get_template_attribute("NETWORK_ADDRESS",naddr);
if (naddr.empty())
{
goto error_addr;
}
get_template_attribute("NETWORK_SIZE",nclass);
if ( nclass == "B" )
{
size = 65534;
}
else if ( nclass == "C" )
{
size = 254;
}
else if (!nclass.empty())//Assume its a number
{
istringstream iss(nclass);
iss >> size;
}
if (size == 0)
{
size = default_size;
}
leases = new RangedLeases::RangedLeases(db,
oid,
size,
mac_prefix,
naddr);
}
else if(type == VirtualNetwork::FIXED)
{
vector<const Attribute *> vector_leases;
get_template_attribute("LEASES",vector_leases);
leases = new FixedLeases::FixedLeases(db,
oid,
mac_prefix,
vector_leases);
}
else
{
goto error_type;
}
if (leases == 0)
{
goto error_null_leases;
}
return 0;
error_template:
ose << "Can not insert in DB template for Virtual Network id " << oid;
ose << "Can not insert in DB template for Virtual Network id " << oid;
goto error_common;
error_update:
ose << "Can not update Virtual Network id " << oid;
vn_template.drop(db);
goto error_common;
ose << "Can not update Virtual Network id " << oid;
vn_template.drop(db);
goto error_common;
error_type:
ose << "Wrong type of Virtual Network: " << type;
ose << "Wrong type of Virtual Network: " << type;
goto error_leases;
error_addr:
ose << "Network address is not defined nid: " << oid;
ose << "Network address is not defined nid: " << oid;
goto error_leases;
error_null_leases:
@@ -425,30 +357,30 @@ error_leases:
vn_drop(db);
error_common:
Nebula::log("VNM", Log::ERROR, ose);
Nebula::log("VNM", Log::ERROR, ose);
return -1;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::update(SqliteDB * db)
int VirtualNetwork::update(SqlDB * db)
{
ostringstream oss;
int rc;
char * sql_name = sqlite3_mprintf("%q",name.c_str());
char * sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
return -1;
}
char * sql_bridge = sqlite3_mprintf("%q",bridge.c_str());
char * sql_bridge = db->escape_str(bridge.c_str());
if ( sql_bridge == 0 )
{
sqlite3_free(sql_name);
db->free_str(sql_name);
return -1;
}
@@ -458,29 +390,29 @@ int VirtualNetwork::update(SqliteDB * db)
"'" << sql_name << "'," <<
type << "," <<
"'" << sql_bridge << "')";
rc = db->exec(oss);
sqlite3_free(sql_name);
sqlite3_free(sql_bridge);
db->free_str(sql_name);
db->free_str(sql_bridge);
return rc;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetwork::vn_drop(SqliteDB * db)
int VirtualNetwork::vn_drop(SqlDB * db)
{
ostringstream oss;
int rc;
vn_template.drop(db);
vn_template.drop(db);
leases->drop(db);
oss << "DELETE FROM " << table << " WHERE OID=" << oid;
rc = db->exec(oss);
if ( rc == 0 )
@@ -488,7 +420,7 @@ int VirtualNetwork::vn_drop(SqliteDB * db)
set_valid(false);
}
return rc;
return rc;
}
@@ -501,8 +433,8 @@ ostream& operator<<(ostream& os, VirtualNetwork& vn)
{
string vnet_xml;
os << vn.to_xml(vnet_xml);
os << vn.to_xml(vnet_xml);
return os;
};
@@ -516,7 +448,7 @@ string& VirtualNetwork::to_xml(string& xml) const
string template_xml;
string leases_xml;
os <<
os <<
"<VNET>" <<
"<ID>" << oid << "</ID>" <<
"<UID>" << uid << "</UID>" <<
@@ -527,7 +459,7 @@ string& VirtualNetwork::to_xml(string& xml) const
if (leases)
os << leases->to_xml(leases_xml);
os << "</VNET>";
xml = os.str();
return xml;
@@ -542,7 +474,7 @@ string& VirtualNetwork::to_str(string& str) const
string template_str;
string leases_str;
os << "ID : " << oid << endl;
os << "UID : " << uid << endl;
os << "NAME : " << name << endl;
@@ -555,21 +487,20 @@ string& VirtualNetwork::to_str(string& str) const
{
os << "Fixed" << endl;
}
os << "Bridge : " << bridge << endl << endl;
os << "....: Template :...." << vn_template.to_str(template_str) << endl << endl;
if (leases)
{
os << "....: Leases :...." << endl << leases->to_str(leases_str) << endl;
}
str = os.str();
return str;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
+97 -53
View File
@@ -18,12 +18,12 @@
#include "VirtualNetworkPool.h"
#include <sstream>
VirtualNetworkPool::VirtualNetworkPool(SqliteDB * db,
const string& prefix,
int _default_size):
PoolSQL(db,VirtualNetwork::table),
mac_prefix(0),
default_size(_default_size)
VirtualNetworkPool::VirtualNetworkPool(SqlDB * db,
const string& prefix,
int _default_size):
PoolSQL(db,VirtualNetwork::table),
mac_prefix(0),
default_size(_default_size)
{
istringstream iss;
size_t pos = 0;
@@ -34,16 +34,16 @@ VirtualNetworkPool::VirtualNetworkPool(SqliteDB * db,
while ( (pos = mac.find(':')) != string::npos )
{
mac.replace(pos,1," ");
count++;
mac.replace(pos,1," ");
count++;
}
if (count != 1)
{
Nebula::log("VNM",Log::ERROR,"Wrong MAC prefix format, using default");
mac_prefix = 1; //"00:01"
return;
Nebula::log("VNM",Log::ERROR,"Wrong MAC prefix format, using default");
mac_prefix = 1; //"00:01"
return;
}
iss.str(mac);
@@ -55,41 +55,41 @@ VirtualNetworkPool::VirtualNetworkPool(SqliteDB * db,
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int VirtualNetworkPool::allocate (
int uid,
const string& stemplate,
int * oid)
{
VirtualNetwork * vn;
char * error_msg;
VirtualNetwork * vn;
char * error_msg;
int rc;
ostringstream oss;
ostringstream oss;
string name;
string bridge;
string str_type;
// Build a new Virtual Network object
vn = new VirtualNetwork(mac_prefix, default_size);
vn->uid = uid;
rc = vn->vn_template.parse(stemplate,&error_msg);
if ( rc != 0 )
{
oss << error_msg;
oss << error_msg;
Nebula::log("VNM", Log::ERROR, oss);
free(error_msg);
return -1;
}
// Information about the VN needs to be extracted from the template
vn->get_template_attribute("TYPE",str_type);
if ( str_type == "RANGED")
{
vn->type = VirtualNetwork::RANGED;
@@ -101,46 +101,39 @@ int VirtualNetworkPool::allocate (
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);
if ( *oid == -1 )
{
return -1;
}
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
extern "C"
int VirtualNetworkPool::get_cb(void * _oid, int num, char **values,char **names)
{
static int select_name_cb(
void * _value,
int num,
char ** values,
char ** names)
{
int * oid;
oid = static_cast<int *>(_value);
int * oid;
oid = static_cast<int *>(_oid);
if ( oid == 0 || values == 0 || values[0] == 0 )
{
return -1;
}
*oid = atoi(values[0]);
return 0;
}
return 0;
}
/* -------------------------------------------------------------------------- */
@@ -149,23 +142,27 @@ static int select_name_cb(
VirtualNetwork * VirtualNetworkPool::get(const string& name, bool lock)
{
ostringstream oss;
int oid;
int rc;
char * sql_name = sqlite3_mprintf("%q",name.c_str());
int oid;
int rc;
char * sql_name = db->escape_str(name.c_str());
if ( sql_name == 0 )
{
return 0;
}
oss << "SELECT oid FROM " << VirtualNetwork::table << " WHERE name = '"
<< sql_name << "'";
rc = db->exec(oss, select_name_cb, (void *) (&oid));
sqlite3_free(sql_name);
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);
db->free_str(sql_name);
if (rc != 0)
{
@@ -178,4 +175,51 @@ VirtualNetwork * VirtualNetworkPool::get(const string& name, bool 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::table << ".*,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>";
return rc;
}