2008-11-13 19:21:17 +03:00
/* -------------------------------------------------------------------------- */
2013-01-24 19:18:30 +04:00
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */
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 VIRTUAL_NETWORK_POOL_H_
# define VIRTUAL_NETWORK_POOL_H_
# include "PoolSQL.h"
# include "VirtualNetwork.h"
# include <time.h>
2010-07-14 20:11:29 +04:00
class AuthRequest ;
2008-11-13 19:21:17 +03:00
using namespace std ;
/**
* The Virtual Network Pool class . . . .
*/
class VirtualNetworkPool : public PoolSQL
{
public :
2012-10-08 14:33:32 +04:00
VirtualNetworkPool ( SqlDB * db ,
const string & str_mac_prefix ,
int default_size ,
vector < const Attribute * > hook_mads ,
const string & remotes_location ) ;
2008-11-13 19:21:17 +03:00
~ VirtualNetworkPool ( ) { } ;
/**
2010-07-14 21:37:43 +04:00
* Function to allocate a new VNET object
2008-11-13 19:21:17 +03:00
* @ param uid user identifier
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
* @ param gid the id of the group this object is assigned to
2013-01-18 21:34:51 +04:00
* @ param uname user name
* @ param gname group name
* @ param umask permissions umask
2010-07-14 21:37:43 +04:00
* @ param vn_template a VirtualNetworkTemplate describing the VNET
2008-11-13 19:21:17 +03:00
* @ param oid the id assigned to the VM ( output )
2012-02-29 21:09:47 +04:00
* @ param cluster_id the id of the cluster this VNET will belong to
* @ param cluster_name the name of the cluster this VNET will belong to
2011-05-12 19:10:35 +04:00
* @ param error_str Returns the error reason , if any
2010-07-14 21:37:43 +04:00
* @ return oid on success , - 1 error
2008-11-13 19:21:17 +03:00
*/
int allocate (
2011-05-12 19:10:35 +04:00
int uid ,
Feature #407: Add 'GID' attribute to some pool objects; change *pool.info XML-RPC flag meaning; update onedb migrator; fix tests.
* VM, VMTEMPLATE, VNET & IMAGE objects have a GID attribute, and a table column. The group id is inherited from the user creating the object, except for VMs created from Templates, that inherit the Template's group.
* The new flag meaning has been modified in src/rm sources and CLI commands for one.(vm,template,vnet,image)pool.info . It changes from
-2 all, -1 mine & public, >=0 UID
to
-3 mine, -2 all, -1 mine & group
* USER has a group, but not secondary ones. The user_pool table doesn't have a GID column, we'll deal with it later when the group-users relations are implemented.
* onedb migrator 1.rb: deleted USERNAME, and GID added.
2011-05-16 19:00:27 +04:00
int gid ,
2011-06-30 13:31:00 +04:00
const string & uname ,
const string & gname ,
2013-01-18 21:34:51 +04:00
int umask ,
2011-05-12 19:10:35 +04:00
VirtualNetworkTemplate * vn_template ,
int * oid ,
2012-02-29 21:09:47 +04:00
int cluster_id ,
const string & cluster_name ,
2011-05-12 19:10:35 +04:00
string & error_str ) ;
2008-11-13 19:21:17 +03:00
/**
* Function to get a VN from the pool , if the object is not in memory
* it is loaded from the DB
* @ param oid VN unique id
* @ param lock locks the VN mutex
* @ return a pointer to the VN , 0 if the VN could not be loaded
*/
2011-03-05 05:24:11 +03:00
VirtualNetwork * get ( int oid , bool lock )
2008-11-13 19:21:17 +03:00
{
return static_cast < VirtualNetwork * > ( PoolSQL : : get ( oid , lock ) ) ;
} ;
2010-04-06 01:34:09 +04:00
2011-07-12 21:30:00 +04:00
/**
* Gets an object from the pool ( if needed the object is loaded from the
* database ) .
* @ param name of the object
* @ param uid id of owner
* @ param lock locks the object if true
*
* @ return a pointer to the object , 0 in case of failure
*/
VirtualNetwork * get ( const string & name , int uid , bool lock )
{
return static_cast < VirtualNetwork * > ( PoolSQL : : get ( name , uid , lock ) ) ;
} ;
2008-11-13 19:21:17 +03:00
//--------------------------------------------------------------------------
// Virtual Network DB access functions
//--------------------------------------------------------------------------
2010-04-06 01:34:09 +04:00
2010-06-25 14:08:05 +04:00
/**
* Generates a NIC attribute for VM templates using the VirtualNetwork
* metadata
* @ param nic the nic attribute to be generated
2012-12-13 16:27:36 +04:00
* @ param nic_id the id for this NIC
2012-12-12 21:31:27 +04:00
* @ param uid of the VM owner
2010-06-25 14:08:05 +04:00
* @ param vid of the VM requesting the lease
2012-02-25 00:13:56 +04:00
* @ param error_str string describing the error
2012-10-08 14:33:32 +04:00
* @ return 0 on success ,
* - 1 error ,
2011-11-13 02:39:10 +04:00
* - 2 not using the pool
2010-06-25 14:08:05 +04:00
*/
2012-12-13 16:27:36 +04:00
int nic_attribute (
VectorAttribute * nic ,
int nic_id ,
int uid ,
int vid ,
string & error_str ) ;
2010-06-25 14:08:05 +04:00
2010-07-14 20:11:29 +04:00
/**
* Generates an Authorization token for a NIC attribute
* @ param nic the nic to be authorized
* @ param ar the AuthRequest
*/
2011-03-05 00:37:21 +03:00
void authorize_nic ( VectorAttribute * nic , int uid , AuthRequest * ar ) ;
2010-07-14 20:11:29 +04:00
2008-11-13 19:21:17 +03:00
/**
* Bootstraps the database table ( s ) associated to the VirtualNetwork pool
2011-10-10 17:14:46 +04:00
* @ return 0 on success
2008-11-13 19:21:17 +03:00
*/
2011-10-10 17:14:46 +04:00
static int bootstrap ( SqlDB * _db )
2008-11-13 19:21:17 +03:00
{
2011-10-10 17:14:46 +04:00
return VirtualNetwork : : bootstrap ( _db ) ;
2008-11-13 19:21:17 +03:00
} ;
2010-04-06 01:34:09 +04:00
2008-11-13 19:21:17 +03:00
/**
2010-04-11 00:15:47 +04:00
* Dumps the Virtual Network pool in XML format . A filter can be also added
* to the query
2009-07-09 18:34:34 +04:00
* @ param oss the output stream to dump the pool contents
* @ param where filter for the objects , defaults to all
*
* @ return 0 on success
2008-11-13 19:21:17 +03:00
*/
2011-03-09 15:23:01 +03:00
int dump ( ostringstream & oss , const string & where )
{
return PoolSQL : : dump ( oss , " VNET_POOL " , VirtualNetwork : : table , where ) ;
}
2009-07-09 18:34:34 +04:00
2010-07-01 21:04:52 +04:00
/**
* Get the mac prefix
* @ return the mac prefix
*/
static const unsigned int & mac_prefix ( )
{
return _mac_prefix ;
} ;
/**
* Get the default network size
* @ return the size
*/
static const unsigned int & default_size ( )
{
return _default_size ;
} ;
2009-07-09 18:34:34 +04:00
private :
2008-11-13 19:21:17 +03:00
/**
* Holds the system - wide MAC prefix
*/
2010-07-01 21:04:52 +04:00
static unsigned int _mac_prefix ;
2010-04-06 01:34:09 +04:00
2008-11-13 19:21:17 +03:00
/**
* Default size for Virtual Networks
*/
2010-07-01 21:04:52 +04:00
static unsigned int _default_size ;
2010-04-06 01:34:09 +04:00
2009-07-09 18:34:34 +04:00
/**
* Factory method to produce VN objects
* @ return a pointer to the new VN
*/
PoolObjectSQL * create ( )
{
2013-01-18 21:34:51 +04:00
return new VirtualNetwork ( - 1 , - 1 , " " , " " , 0 , - 1 , " " , 0 ) ;
2009-07-09 18:34:34 +04:00
} ;
2011-11-13 02:39:10 +04:00
/**
2012-10-08 14:33:32 +04:00
* Function to get a VirtualNetwork by its name , as provided by a VM
2011-11-13 02:39:10 +04:00
* template
*/
2012-10-08 14:33:32 +04:00
VirtualNetwork * get_nic_by_name ( VectorAttribute * nic ,
2011-11-13 02:39:10 +04:00
const string & name ,
2012-02-25 00:13:56 +04:00
int _uidi ,
string & error ) ;
2011-11-13 02:39:10 +04:00
/**
* Function to get a VirtualNetwork by its id , as provided by a VM template
*/
2012-02-25 00:13:56 +04:00
VirtualNetwork * get_nic_by_id ( const string & id_s , string & error ) ;
2008-11-13 19:21:17 +03:00
} ;
2010-04-06 01:34:09 +04:00
2010-06-07 19:51:46 +04:00
# endif /*VIRTUAL_NETWORK_POOL_H_*/