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 FIXED_LEASES_H_
# define FIXED_LEASES_H_
# include "Leases.h"
using namespace std ;
/**
2010-04-11 00:15:47 +04:00
* The FixedLeases class represents a pool of fixed IP - MAC leases . A lease can
2008-11-13 19:21:17 +03:00
* be either a IP ( the MAC is then PREFIX : IP ) or the IP and MAC addresses . The
* pool is read from a template file , each lease is in the form :
* LEASE = [ IP = " <the ip> " , MAC = " <the mac> " ]
2010-04-11 00:15:47 +04:00
*/
2008-11-13 19:21:17 +03:00
class FixedLeases : public Leases
{
public :
2010-04-11 00:15:47 +04:00
/**
* Create a FixedLeases from template
*/
FixedLeases ( SqlDB * db ,
int _oid ,
unsigned int _mac_prefix ,
vector < const Attribute * > & vector_leases ) ;
/**
* Create a plain FixedLeases , you can populate the lease pool using
* select ( )
*/
FixedLeases ( SqlDB * db ,
int _oid ,
unsigned int _mac_prefix ) :
2011-11-25 22:02:17 +04:00
Leases ( db , _oid , 0 , _mac_prefix ) ,
2010-04-11 00:15:47 +04:00
current ( leases . begin ( ) ) { } ;
2008-11-13 19:21:17 +03:00
~ FixedLeases ( ) { } ;
/**
* 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
*/
int get ( int vid , string & ip , string & mac ) ;
2010-04-11 00:15:47 +04:00
2008-11-15 03:40:27 +03:00
/**
* 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
*/
int set ( int vid , const string & ip , string & mac ) ;
2008-11-13 19:21:17 +03:00
/**
* Release an used lease , which becomes unused
* @ param ip of the lease in use
*/
void release ( const string & ip )
{
2011-02-01 20:26:26 +03:00
unset ( ip ) ;
2008-11-13 19:21:17 +03:00
}
2010-04-11 00:15:47 +04:00
2011-02-01 20:26:26 +03:00
/**
2011-02-02 14:40:08 +03:00
* Adds New leases .
* @ 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
int add_leases ( vector < const Attribute * > & leases , string & error_msg ) ;
2011-02-01 20:26:26 +03:00
/**
2011-02-02 14:40:08 +03:00
* Removes leases ; if they are not used .
* @ 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
int remove_leases ( vector < const Attribute * > & leases , string & error_msg ) ;
2011-02-01 20:26:26 +03:00
2008-11-13 19:21:17 +03:00
/**
* Loads the leases from the DB .
*/
2010-04-11 00:15:47 +04:00
int select ( SqlDB * db )
2008-11-13 19:21:17 +03:00
{
2010-04-11 00:15:47 +04:00
//Read the leases from the DB
int rc = Leases : : select ( db ) ;
//Update the size
size = leases . size ( ) ;
return rc ;
2008-11-13 19:21:17 +03:00
}
private :
2010-04-11 00:15:47 +04:00
/**
* Current lease pointer
*/
map < unsigned int , Lease * > : : iterator current ;
2008-11-13 19:21:17 +03:00
/**
* Add a lease , from the Lease interface
2011-02-02 14:40:08 +03:00
* @ param ip ip of the lease
* @ param mac mac of the lease
* @ param vid identifier of the VM getting this lease
* @ param error_msg If the action fails , this message contains the reason .
* @ param used Flag to insert the lease as used .
* @ return 0 if success
2008-11-13 19:21:17 +03:00
*/
2011-02-01 20:26:26 +03:00
int add ( const string & ip ,
const string & mac ,
int vid ,
2011-02-02 14:40:08 +03:00
string & error_msg ,
bool used = true ) ;
2011-02-01 20:26:26 +03:00
/**
2011-02-02 14:40:08 +03:00
* Remove an existing lease , if it is not in use .
* @ param ip ip of the lease
* @ param error_msg If the action fails , this message contains the reason
2011-02-01 20:26:26 +03:00
*/
2011-02-02 14:40:08 +03:00
int remove ( const string & ip , string & error_msg ) ;
2010-04-11 00:15:47 +04:00
2008-11-13 19:21:17 +03:00
/**
2011-02-01 20:26:26 +03:00
* Sets a lease as not used , from the Lease interface
2008-11-13 19:21:17 +03:00
* @ param ip ip of the lease to be deleted
* @ return 0 if success
*/
2011-02-01 20:26:26 +03:00
int unset ( const string & ip ) ;
2011-03-03 20:53:41 +03:00
/**
* Updates the DB entry for this lease
* @ param lease Lease to be updated
* @ return 0 if success
*/
int update_lease ( Lease * lease ) ;
2008-11-13 19:21:17 +03:00
} ;
# endif /*FIXED_LEASES_H_*/