2014-05-09 23:20:09 +04:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2014, OpenNebula Project (OpenNebula.org), C12G Labs */
/* */
/* 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. */
/* -------------------------------------------------------------------------- */
# ifndef ADDRESS_RANGE_POOL_H_
# define ADDRESS_RANGE_POOL_H_
# include <string>
# include <vector>
# include <map>
# include <libxml/parser.h>
# include "Template.h"
2014-05-29 14:44:41 +04:00
# include "PoolObjectSQL.h"
2014-05-09 23:20:09 +04:00
2014-05-10 21:46:27 +04:00
class VectorAttribute ;
2014-05-29 14:44:41 +04:00
class AddressRange ;
2014-05-10 21:46:27 +04:00
2014-05-09 23:20:09 +04:00
using namespace std ;
class AddressRangePool
{
2014-05-10 03:22:02 +04:00
public :
2014-05-09 23:20:09 +04:00
AddressRangePool ( ) ;
virtual ~ AddressRangePool ( ) ;
2014-05-27 19:19:36 +04:00
// *************************************************************************
// Inititalization functions
// *************************************************************************
2014-05-09 23:20:09 +04:00
/**
2014-05-29 17:36:20 +04:00
* Builds the address range from a VectorAttribute . This function is used
* to create address ranges .
2014-05-09 23:20:09 +04:00
* @ param ars the vector of address ranges
* @ param error_msg describing the error
* @ return 0 on success
*/
2014-05-29 17:36:20 +04:00
int from_vattr ( VectorAttribute * ar , string & error_msg ) ;
2014-05-09 23:20:09 +04:00
/**
* Builds the address range set from its XML representation . This function
* is used to rebuild the address ranges from the DB .
* @ param node xmlNode for the template
* @ return 0 on success
*/
int from_xml_node ( const xmlNodePtr node ) ;
2014-05-27 19:19:36 +04:00
// *************************************************************************
// Address Range management interface
// *************************************************************************
2014-05-22 22:20:33 +04:00
/**
* Removes an address range from the pool if it does not contain any used
* leases
* @ param arid of the address range to be removed
* @ return 0 on success , - 1 if not exists or has used addresses
*/
int rm_ar ( unsigned int ar_id , string & error_msg ) ;
2014-05-23 02:24:14 +04:00
/**
* Updates the given address ranges
* @ param ars vector of address ranges as VectorAttributes obtined from
* template in the form AR = [ . . . ]
*/
void update_ar ( vector < Attribute * > ars ) ;
2014-05-10 21:46:27 +04:00
/**
2014-05-29 14:44:41 +04:00
* Allocates a new * empty * address range . It is not added to the pool as it
* needs to be initialized . Only the AR_ID is set .
* @ return the new address range .
2014-05-10 21:46:27 +04:00
*/
2014-05-27 19:19:36 +04:00
AddressRange * allocate_ar ( ) ;
2014-05-29 14:44:41 +04:00
/**
* Adds a new address range to the pool . It should be allocated by the
* allocate_ar ( ) function .
* @ param ar the new address range ;
* @ return 0 on success
*/
int add_ar ( AddressRange * ar ) ;
2014-05-27 19:19:36 +04:00
// *************************************************************************
// Address allocation interface
// *************************************************************************
2014-05-10 03:22:02 +04:00
2014-05-10 21:46:27 +04:00
/**
* Allocates an address in a suitable address range from the pool
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param nic the NIC attribute to be filled with lease attributes
* @ param inherit attributes to be added to the NIC
* @ return 0 if success
*/
2014-05-10 23:06:59 +04:00
int allocate_addr ( PoolObjectSQL : : ObjectType ot , int obid ,
VectorAttribute * nic , const vector < string > & inherit ) ;
2014-05-10 21:46:27 +04:00
/**
* Allocates an address in a suitable address range from the pool by mac
* @ param mac the specific MAC address requested
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param nic the NIC attribute to be filled with lease attributes
* @ param inherit attributes to be added to the NIC
* @ return 0 if success
*/
int allocate_by_mac ( const string & mac , PoolObjectSQL : : ObjectType ot , int obid ,
VectorAttribute * nic , const vector < string > & inherit ) ;
/**
* Allocates an address in a suitable address range from the pool by mac
* @ param ip the specific IP address requested
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param nic the NIC attribute to be filled with lease attributes
* @ param inherit attributes to be added to the NIC
* @ return 0 if success
*/
int allocate_by_ip ( const string & ip , PoolObjectSQL : : ObjectType ot , int obid ,
VectorAttribute * nic , const vector < string > & inherit ) ;
2014-05-19 02:28:27 +04:00
/**
* Holds an address from the specified address range .
* @ param arid of the address range
* @ param ip the ip to hold
* @ return 0 on success
*/
int hold_by_ip ( unsigned int arid , const string & ip ) ;
/**
* Holds an address from the first address range containing the MAC
* @ param mac the mac to hold
* @ return 0 on success
*/
int hold_by_ip ( const string & ip ) ;
/**
* Holds an address from the specified address range .
* @ param arid of the address range
* @ param mac the mac to hold
* @ return 0 on success
*/
int hold_by_mac ( unsigned int arid , const string & mac ) ;
/**
* Holds an address from the first address range containing the MAC
* @ param mac the mac to hold
* @ return 0 on success
*/
int hold_by_mac ( const string & mac ) ;
2014-05-10 21:46:27 +04:00
/**
2014-05-19 19:59:06 +04:00
* Frees the given address by MAC on the given address range
2014-05-10 21:46:27 +04:00
* @ param arid the ID of the address range
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param mac the specific MAC address requested
*/
void free_addr ( unsigned int arid , PoolObjectSQL : : ObjectType ot , int obid ,
const string & mac ) ;
2014-05-19 19:59:06 +04:00
/**
* Frees the given address by IP on the given address range
* @ param arid the ID of the address range
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param ip the specific IP address requested
*/
void free_addr_by_ip ( unsigned int arid , PoolObjectSQL : : ObjectType ot ,
int obid , const string & ip ) ;
/**
* Frees the given address by MAC from all address ranges containing
* the MAC
* @ param arid the ID of the address range
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param mac the specific MAC address requested
*/
void free_addr ( PoolObjectSQL : : ObjectType ot , int obid , const string & mac ) ;
/**
* Frees the given address by IP from all address ranges containing
* the IP
* @ param arid the ID of the address range
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
* @ param ip the specific IP address requested
*/
void free_addr_by_ip ( PoolObjectSQL : : ObjectType ot , int id , const string & ip ) ;
2014-05-27 19:19:36 +04:00
/**
* Frees all the addressed owned by the given object
* @ param ot the type of the object requesting the address ( VM or NET )
* @ param obid the id of the object requesting the address
2014-05-30 14:56:35 +04:00
* @ return the number of addresses freed
2014-05-27 19:19:36 +04:00
*/
2014-05-30 14:56:35 +04:00
int free_addr_by_owner ( PoolObjectSQL : : ObjectType ot , int obid ) ;
2014-05-27 19:19:36 +04:00
// *************************************************************************
// Address reservation
// *************************************************************************
/**
* Reserve a given number of addresses from the first address range with
* enough free addresses to allocate the reservation
* @ param vid the id of the VNET making the reservation
* @ param size number of addresses to reserve
* @ param rar a new address range to place the reservation
* @ return 0 on success
*/
2014-05-30 20:59:25 +04:00
int reserve_addr ( int vid , unsigned int rsize , AddressRange * rar ) ;
2014-05-27 19:19:36 +04:00
2014-05-29 14:44:41 +04:00
/**
* Reserve a given number of addresses from the given address range
* @ param vid the id of the VNET making the reservation
* @ param rsize number of addresses to reserve
* @ param ar_id the address range to reserve the addresses from
* @ param rar a new address range to place the reservation
* @ return 0 on success
*/
2014-05-30 20:59:25 +04:00
int reserve_addr ( int vid , unsigned int rsize , unsigned int ar_id ,
2014-05-29 14:44:41 +04:00
AddressRange * rar ) ;
2014-05-30 01:55:51 +04:00
/**
* Reserve a number of addresses from an address range from a given ip
* @ param vid the id of the VNET making the reservation
* @ param rsize number of addresses to reserve
* @ param ar_id the address range to reserve the addresses from
* @ param ip the first IP in the reservation
* @ param rar a new address range to place the reservation
* @ return 0 on success
*/
2014-05-30 20:59:25 +04:00
int reserve_addr_by_ip ( int vid , unsigned int rsize , unsigned int ar_id ,
const string & ip , AddressRange * rar ) ;
2014-05-30 01:55:51 +04:00
/**
* Reserve a number of addresses from an address range from a given ip
* @ param vid the id of the VNET making the reservation
* @ param rsize number of addresses to reserve
* @ param ar_id the address range to reserve the addresses from
* @ param mac the first IP in the reservation
* @ param rar a new address range to place the reservation
* @ return 0 on success
*/
2014-05-30 20:59:25 +04:00
int reserve_addr_by_mac ( int vid , unsigned int rsize , unsigned int ar_id ,
const string & mac , AddressRange * rar ) ;
2014-05-27 19:19:36 +04:00
// *************************************************************************
// Helpers & Formatting
// *************************************************************************
2014-05-10 23:44:39 +04:00
/**
* Return the number of used addresses
*/
unsigned int get_used_addr ( ) const
{
return used_addr ;
}
2014-05-22 22:20:33 +04:00
/**
* Gets an attribute from the Address Range
* @ param name of the attribute
* @ param value of the attribute
* @ param ar_id to get the attribute from
*/
2014-05-17 03:17:58 +04:00
void get_attribute ( const char * name , string & value , int ar_id ) const ;
2014-05-27 19:19:36 +04:00
/**
* Generate a XML representation of the Address Range Pool
* @ param sstream where the ARPool is written
* @ param extended true to include lease information
* @ return the string with the XML
*/
string & to_xml ( string & sstream , bool extended ) const ;
2014-05-09 23:20:09 +04:00
private :
2014-05-10 21:46:27 +04:00
/**
* Stores the Address Ranges in a template form . This template is used
* to store the pool in the DB
*/
2014-05-09 23:20:09 +04:00
Template ar_template ;
2014-05-10 21:46:27 +04:00
/**
* ID for the next Address Range
*/
2014-05-09 23:20:09 +04:00
unsigned int next_ar ;
2014-05-10 21:46:27 +04:00
/**
* Map to access each range
*/
2014-05-09 23:20:09 +04:00
map < unsigned int , AddressRange * > ar_pool ;
2014-05-10 23:44:39 +04:00
/**
* Used addresses
*/
unsigned int used_addr ;
2014-05-09 23:20:09 +04:00
} ;
# endif