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 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 :
2010-04-11 00:15:47 +04:00
VirtualNetworkPool ( SqlDB * db ,
const string & str_mac_prefix ,
int default_size ) ;
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
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 )
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 ,
2011-05-12 19:10:35 +04:00
VirtualNetworkTemplate * vn_template ,
int * oid ,
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
2008-11-13 19:21:17 +03:00
/**
* Function to get a VN from the pool using the network name
* If the object is not in memory it is loaded from the DB
* @ param name VN unique name
2011-03-05 00:37:21 +03:00
* @ param uid of the VN owner
2008-11-13 19:21:17 +03:00
* @ 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 ( const string & name , int uid , bool lock )
2011-03-05 00:37:21 +03:00
{
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
* @ param vid of the VM requesting the lease
* @ return 0 on success , - 1 error , - 2 not using the pool
*/
2011-03-05 00:37:21 +03:00
int nic_attribute ( VectorAttribute * nic , int uid , int vid ) ;
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
*/
2010-04-11 00:15:47 +04:00
static void bootstrap ( SqlDB * _db )
2008-11-13 19:21:17 +03:00
{
2009-07-09 18:34:34 +04:00
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 ( )
{
2011-06-30 13:31:00 +04:00
return new VirtualNetwork ( - 1 , - 1 , " " , " " , 0 ) ;
2009-07-09 18:34:34 +04:00
} ;
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_*/