1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-03 13:47:01 +03:00

feature #2858: Adds lease usage information to XML descriptions

This commit is contained in:
Ruben S. Montero 2014-05-10 21:44:39 +02:00
parent 72b61e9ec5
commit f0e64384cf
6 changed files with 59 additions and 25 deletions

View File

@ -34,7 +34,7 @@ class AddressRange
{ {
public: public:
AddressRange(unsigned int _id):id(_id),next(0){}; AddressRange(unsigned int _id):id(_id),next(0),used_addr(0){};
virtual ~AddressRange(){}; virtual ~AddressRange(){};
@ -145,17 +145,26 @@ public:
* @param ot the object type of the owner of the address * @param ot the object type of the owner of the address
* @param obid the id of the owner of the address * @param obid the id of the owner of the address
* @param mac the MAC address in string form * @param mac the MAC address in string form
* @return 0 if the address was freed
*/ */
void free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac); int free_addr(PoolObjectSQL::ObjectType ot, int obid, const string& mac);
/** /**
* Return the id for this address range * Return the id for this address range
*/ */
unsigned int ar_id() unsigned int ar_id() const
{ {
return id; return id;
} }
/**
* Return the number of used addresses
*/
unsigned int get_used_addr() const
{
return used_addr;
}
private: private:
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/* String to binary conversion functions for different address types */ /* String to binary conversion functions for different address types */
@ -242,7 +251,7 @@ private:
/** /**
* Frees an address from the map. Updates the ALLOCATED attribute * Frees an address from the map. Updates the ALLOCATED attribute
*/ */
void free_addr(PoolObjectSQL::ObjectType ot, int obid, int free_addr(PoolObjectSQL::ObjectType ot, int obid,
unsigned int addr_index); unsigned int addr_index);
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
@ -308,6 +317,8 @@ private:
unsigned int next; unsigned int next;
unsigned int used_addr;
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/* Restricted Attributes */ /* Restricted Attributes */
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */

View File

@ -107,6 +107,14 @@ public:
void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid, void free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot, int obid,
const string& mac); const string& mac);
/**
* Return the number of used addresses
*/
unsigned int get_used_addr() const
{
return used_addr;
}
private: private:
/** /**
* Stores the Address Ranges in a template form. This template is used * Stores the Address Ranges in a template form. This template is used
@ -123,6 +131,11 @@ private:
* Map to access each range * Map to access each range
*/ */
map<unsigned int, AddressRange *> ar_pool; map<unsigned int, AddressRange *> ar_pool;
/**
* Used addresses
*/
unsigned int used_addr;
}; };
#endif #endif

View File

@ -150,26 +150,13 @@ public:
ar_pool.free_addr(arid, PoolObjectSQL::VM, vid, mac); ar_pool.free_addr(arid, PoolObjectSQL::VM, vid, mac);
} }
/**
* Gets size of the network (used + free)
* @return number of hosts that can be fitted in this network
*/
unsigned int get_size()
{
//TODO
return 0;
//return leases->size;
};
/** /**
* Gets used leases * Gets used leases
* @return number of network leases in used * @return number of network leases in used
*/ */
unsigned int get_used() unsigned int get_used()
{ {
//TODO return ar_pool.get_used_addr();
return 0;
//return leases->n_used;
}; };
/** /**

View File

@ -276,6 +276,8 @@ void AddressRange::to_xml(ostringstream &oss) const
oss << "</LEASES>"; oss << "</LEASES>";
} }
oss << "<TOTAL_LEASES>" << used_addr << "</TOTAL_LEASES>";
oss << "</AR>"; oss << "</AR>";
} }
@ -592,6 +594,8 @@ int AddressRange::attr_to_allocated(const string& allocated_s)
} }
allocated.insert(make_pair(addr_index,object_pack)); allocated.insert(make_pair(addr_index,object_pack));
used_addr++;
} }
return 0; return 0;
@ -605,12 +609,14 @@ void AddressRange::allocate_addr(PoolObjectSQL::ObjectType ot, int obid,
allocated.insert(make_pair(addr_index,ot|obid)); allocated.insert(make_pair(addr_index,ot|obid));
used_addr++;
allocated_to_attr(); allocated_to_attr();
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid,
unsigned int addr_index) unsigned int addr_index)
{ {
map<unsigned int, long long>::iterator it; map<unsigned int, long long>::iterator it;
@ -621,7 +627,13 @@ void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid,
{ {
allocated.erase(it); allocated.erase(it);
allocated_to_attr(); allocated_to_attr();
used_addr--;
return 0;
} }
return -1;
} }
/* ************************************************************************** */ /* ************************************************************************** */
@ -764,7 +776,7 @@ int AddressRange::allocate_by_ip(
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid, int AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid,
const string& mac_s) const string& mac_s)
{ {
unsigned int mac_i[2]; unsigned int mac_i[2];
@ -775,8 +787,10 @@ void AddressRange::free_addr(PoolObjectSQL::ObjectType ot, int obid,
if ((mac_i[0] >= mac[0]) && (index < size)) if ((mac_i[0] >= mac[0]) && (index < size))
{ {
free_addr(ot, obid, index); return free_addr(ot, obid, index);
} }
return -1;
} }
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -18,8 +18,8 @@
using namespace std; using namespace std;
AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"),next_ar(0) AddressRangePool::AddressRangePool():ar_template(false,'=',"AR_POOL"),
{}; next_ar(0), used_addr(0){};
AddressRangePool::~AddressRangePool() AddressRangePool::~AddressRangePool()
{ {
@ -104,6 +104,8 @@ int AddressRangePool::from_xml_node(const xmlNodePtr node)
{ {
next_ar = ar->ar_id() + 1; next_ar = ar->ar_id() + 1;
} }
used_addr += ar->get_used_addr();
} }
return 0; return 0;
@ -148,6 +150,7 @@ int AddressRangePool::allocate_addr(PoolObjectSQL::ObjectType ot, int obid,
{ {
if (it->second->allocate_addr(ot, obid, nic, inherit) == 0) if (it->second->allocate_addr(ot, obid, nic, inherit) == 0)
{ {
used_addr++;
return 0; return 0;
} }
} }
@ -168,6 +171,7 @@ int AddressRangePool::allocate_by_mac(const string &mac,
{ {
if (it->second->allocate_by_mac(mac, ot, obid, nic, inherit) == 0) if (it->second->allocate_by_mac(mac, ot, obid, nic, inherit) == 0)
{ {
used_addr++;
return 0; return 0;
} }
} }
@ -188,6 +192,7 @@ int AddressRangePool::allocate_by_ip(const string &ip,
{ {
if (it->second->allocate_by_ip(ip, ot, obid, nic, inherit) == 0) if (it->second->allocate_by_ip(ip, ot, obid, nic, inherit) == 0)
{ {
used_addr++;
return 0; return 0;
} }
} }
@ -207,6 +212,9 @@ void AddressRangePool::free_addr(unsigned int arid, PoolObjectSQL::ObjectType ot
if (it!=ar_pool.end()) if (it!=ar_pool.end())
{ {
it->second->free_addr(ot, obid, mac); if ( it->second->free_addr(ot, obid, mac) == 0 )
{
used_addr--;
}
} }
} }

View File

@ -431,6 +431,7 @@ string& VirtualNetwork::to_xml_extended(string& xml, bool extended) const
{ {
os << "<VLAN_ID/>"; os << "<VLAN_ID/>";
} }
os << "<TOTAL_LEASES>"<< ar_pool.get_used_addr() << "</TOTAL_LEASES>";
os << obj_template->to_xml(template_xml); os << obj_template->to_xml(template_xml);