2008-11-13 19:21:17 +03:00
/* -------------------------------------------------------------------------- */
2011-02-25 16:34:44 +03:00
/* Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org) */
2008-11-13 19:21:17 +03:00
/* */
/* 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 LEASES_H_
# define LEASES_H_
# include "ObjectSQL.h"
2011-03-03 20:53:41 +03:00
# include "ObjectXML.h"
2008-11-13 19:21:17 +03:00
# include "Attribute.h"
# include <map>
# include <vector>
# include <sstream>
using namespace std ;
/**
* The Leases class represents all the IP and MAC addresses ( lease ) that can
* be asigned ( or are asigned ) in a Virtual Network
*/
class Leases : public ObjectSQL
{
public :
/**
* Creates a new Lease set for a given Virtual Network ( oid )
* @ param _db pinter to the DB object used to store the leases
* @ param _oid the virtual network unique identifier
* @ param _size the max number of leases
*/
2010-04-11 00:15:47 +04:00
Leases ( SqlDB * _db , int _oid , unsigned long _size ) :
2011-03-03 20:53:41 +03:00
ObjectSQL ( ) ,
oid ( _oid ) , size ( _size ) , n_used ( 0 ) , db ( _db ) { } ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
virtual ~ Leases ( )
{
map < unsigned int , Lease * > : : iterator it ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
for ( it = leases . begin ( ) ; it ! = leases . end ( ) ; it + + )
{
delete it - > second ;
}
} ;
friend ostream & operator < < ( ostream & os , Leases & _leases ) ;
/**
* Returns an unused lease , which becomes used
* @ param vid identifier of the VM getting this lease
* @ param ip ip of the returned lease
* @ param mac mac of the returned lease
* @ return 0 if success
*/
2008-11-15 03:40:27 +03:00
virtual int get ( int vid , string & ip , string & mac ) = 0 ;
/**
* Ask for a specific lease in the network
* @ param vid identifier of the VM getting this lease
* @ param ip ip of lease requested
* @ param mac mac of the lease
* @ return 0 if success
*/
virtual int set ( int vid , const string & ip , string & mac ) = 0 ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Release an used lease , which becomes unused
* @ param ip of the lease in use
*/
virtual void release ( const string & ip ) = 0 ;
2011-02-01 20:26:26 +03:00
/**
2011-02-02 14:40:08 +03:00
* Adds New leases . ( Only implemented for FIXED networks )
* @ param vector_leases vector of VectorAttribute objects . For the
* moment , the vector can only contain one LEASE .
* @ param error_msg If the action fails , this message contains
* the reason .
* @ return 0 on success
2011-02-01 20:26:26 +03:00
*/
2011-02-02 14:40:08 +03:00
virtual int add_leases ( vector < const Attribute * > & vector_leases ,
string & error_msg ) = 0 ;
2011-02-01 20:26:26 +03:00
/**
2011-02-02 14:40:08 +03:00
* Removes leases ; if they are not used . ( Only available for FIXED networks )
* @ param vector_leases vector of VectorAttribute objects . For the
* moment , the vector can only contain one LEASE .
* @ param error_msg If the action fails , this message contains
* the reason .
* @ return 0 on success
2011-02-01 20:26:26 +03:00
*/
virtual int remove_leases ( vector < const Attribute * > & vector_leases ,
2011-02-02 14:40:08 +03:00
string & error_msg ) = 0 ;
2011-02-01 20:26:26 +03:00
2008-11-13 19:21:17 +03:00
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
protected :
/**
* The Lease class , it represents a pair of IP and MAC assigned to
* a Virtual Machine
*/
2011-03-03 20:53:41 +03:00
class Lease : public ObjectXML
2008-11-13 19:21:17 +03:00
{
public :
/**
* Creates a new lease , string form . This constructor throws a runtime
* exception if the IP or MAC format is wrong .
* @ param _ip , the Lease IP in string format
* @ param _mac , the Lease MAC in string format
* @ param _vid , the ID of the VM owning the lease
* @ param _used , the lease is in use
*/
//Lease(const string& _ip, const string& _mac,int _vid, bool _used=true);
/**
* Creates a new lease , numeric form .
* @ param _ip , the Lease IP in numeric format
* @ param _mac , the Lease MAC in numeric format
* @ param _vid , the ID of the VM owning the lease
* @ param _used , the lease is in use
*/
Lease ( unsigned int _ip , unsigned int _mac [ ] , int _vid , bool _used = true )
2011-03-03 20:53:41 +03:00
: ObjectXML ( ) , ip ( _ip ) , vid ( _vid ) , used ( _used )
2008-11-13 19:21:17 +03:00
{
// TODO check size
mac [ PREFIX ] = _mac [ PREFIX ] ;
mac [ SUFFIX ] = _mac [ SUFFIX ] ;
} ;
2011-03-03 20:53:41 +03:00
/**
* Creates a new empty lease . Method from_xml should be called right
* after this constructor .
*/
Lease ( ) : ObjectXML ( ) { } ;
2008-11-13 19:21:17 +03:00
~ Lease ( ) { } ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Converts this lease ' s IP and MAC to string
* @ param ip ip of the lease in string
* @ param mac mac of the lease in string
*/
2009-07-09 18:34:34 +04:00
void to_string ( string & _ip , string & _mac ) const ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Conversion from string IP to unsigned int IP
* @ return 0 if success
*/
static int ip_to_number ( const string & ip , unsigned int & i_ip ) ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Conversion from unsigned int IP to string IP
*/
static void ip_to_string ( const unsigned int i_ip , string & ip ) ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Conversion from string MAC to unsigned int [ ] MAC
* @ return 0 if success
*/
static int mac_to_number ( const string & mac , unsigned int i_mac [ ] ) ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Conversion from string IP to unsigned int IP
*/
static void mac_to_string ( const unsigned int i_mac [ ] , string & mac ) ;
/**
* Prints a Lease in a single line
*/
friend ostream & operator < < ( ostream & os , Lease & _lease ) ;
2009-07-09 18:34:34 +04:00
/**
* Function to print the Lease object into a string in
2011-03-03 20:53:41 +03:00
* XML format
* @ param xml the resulting XML string
2010-04-11 00:15:47 +04:00
* @ return a reference to the generated string
2009-07-09 18:34:34 +04:00
*/
2011-03-03 20:53:41 +03:00
string & to_xml ( string & xml ) const ;
2009-07-09 18:34:34 +04:00
/**
* Function to print the Lease object into a string in
2011-03-03 20:53:41 +03:00
* XML format . The output contains all the internal attributes ,
* to be saved in the DB as a blob
2009-07-09 18:34:34 +04:00
* @ param xml the resulting XML string
2010-04-11 00:15:47 +04:00
* @ return a reference to the generated string
2009-07-09 18:34:34 +04:00
*/
2011-03-03 20:53:41 +03:00
string & to_xml_db ( string & xml ) const ;
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
int from_xml ( const string & xml_str ) ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Constants to access the array storing the MAC address
*/
enum MACIndex
{
SUFFIX = 0 , /**< Lower significant 4 bytes */
PREFIX = 1 /**< Higher significant 2 bytes */
} ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
unsigned int ip ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
unsigned int mac [ 2 ] ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
int vid ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
bool used ;
} ;
friend class VirtualNetwork ;
2010-04-11 00:15:47 +04:00
friend class VirtualNetworkPool ;
2009-07-09 18:34:34 +04:00
2008-11-13 19:21:17 +03:00
// -------------------------------------------------------------------------
// Leases fields
// -------------------------------------------------------------------------
/**
* Leases indentifier . Connects it to a Virtual Network
*/
int oid ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Number of possible leases ( free + asigned )
*/
unsigned int size ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Hash of leases , indexed by lease . ip
*/
map < unsigned int , Lease * > leases ;
2011-03-03 20:53:41 +03:00
/**
* Number of used leases
*/
int n_used ;
2008-11-13 19:21:17 +03:00
// -------------------------------------------------------------------------
// DataBase implementation variables
// -------------------------------------------------------------------------
/**
* Pointer to the DataBase
*/
2010-04-11 00:15:47 +04:00
SqlDB * db ;
2008-11-13 19:21:17 +03:00
static const char * table ;
static const char * db_names ;
static const char * db_bootstrap ;
// -------------------------------------------------------------------------
// Leases methods
// -------------------------------------------------------------------------
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
* Check if the passed ip corresponds with a given lease
* @ param ip of the lease to be checked
* @ return true if the ip was already assigned
*/
2010-04-11 00:15:47 +04:00
bool check ( const string & ip ) ;
bool check ( unsigned int ip ) ;
/**
* Reads the leases from the DB , and updates the lease hash table
* @ param db pointer to the database .
* @ return 0 on success .
*/
virtual int select ( SqlDB * db ) ;
2008-11-13 19:21:17 +03:00
2009-07-09 18:34:34 +04:00
friend ostream & operator < < ( ostream & os , Lease & _lease ) ;
/**
* Function to print the Leases object into a string in
* XML format
* @ param xml the resulting XML string
2010-04-11 00:15:47 +04:00
* @ return a reference to the generated string
2009-07-09 18:34:34 +04:00
*/
string & to_xml ( string & xml ) const ;
2008-11-13 19:21:17 +03:00
private :
/**
2010-04-11 00:15:47 +04:00
* Callback function to unmarshall a Lease object ( Lease : : select )
2008-11-13 19:21:17 +03:00
* @ param num the number of columns read from the DB
* @ para names the column names
* @ para vaues the column values
* @ return 0 on success
*/
2010-04-11 00:15:47 +04:00
int select_cb ( void * nil , int num , char * * values , char * * names ) ;
2008-11-13 19:21:17 +03:00
/**
* This method should not be called , leases are added / removed / updated
* through add / del interface
* @ param db pointer to the database .
* @ return 0 on success .
*/
2010-08-05 21:28:28 +04:00
int insert ( SqlDB * db , string & error_str ) ;
2008-11-13 19:21:17 +03:00
/**
* Leases are added / removed / updated through add / del interface
* This method is for pool management .
* @ param db pointer to the database .
* @ return 0 on success .
*/
2010-04-11 00:15:47 +04:00
int drop ( SqlDB * db ) ;
2008-11-13 19:21:17 +03:00
/**
* This method should not be called , leases are added / removed / updated
* through add / del interface
* @ param db pointer to the database .
* @ return 0 on success .
*/
2010-04-11 00:15:47 +04:00
int update ( SqlDB * db ) ;
2008-11-13 19:21:17 +03:00
} ;
# endif /*LEASES_H_*/