2015-11-30 18:50:23 +03:00
/* -------------------------------------------------------------------------- */
2019-01-16 13:27:59 +03:00
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
2015-11-30 18:50:23 +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_ROUTER_H_
# define VIRTUAL_ROUTER_H_
# include "PoolObjectSQL.h"
# include "Template.h"
2015-12-11 13:45:34 +03:00
# include "ObjectCollection.h"
2016-01-28 18:06:18 +03:00
# include "VirtualMachineTemplate.h"
2016-02-01 19:07:04 +03:00
# include "AuthRequest.h"
2016-02-09 18:33:13 +03:00
# include "History.h"
# include "ActionSet.h"
2015-11-30 18:50:23 +03:00
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
2017-02-07 19:26:23 +03:00
struct RequestAttributes ;
2015-11-30 18:50:23 +03:00
/**
* The VirtualRouter class .
*/
class VirtualRouter : public PoolObjectSQL
{
public :
/**
* Function to print the VirtualRouter object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2019-09-03 17:31:51 +03:00
string & to_xml ( string & xml ) const override ;
2015-11-30 18:50:23 +03:00
2016-03-02 01:31:31 +03:00
int add_vmid ( int vmid )
{
return vms . add ( vmid ) ;
}
2015-12-01 18:36:50 +03:00
2016-07-08 12:20:39 +03:00
int del_vmid ( int vmid )
{
return vms . del ( vmid ) ;
}
2016-03-02 01:31:31 +03:00
bool has_vmids ( ) const
{
return vms . size ( ) > 0 ;
}
2016-01-18 17:44:42 +03:00
2016-01-28 18:06:18 +03:00
/**
* Returns a copy of the VM IDs set
*/
2016-03-02 01:31:31 +03:00
set < int > get_vms ( ) const
{
return vms . clone ( ) ;
}
2016-01-28 18:06:18 +03:00
2015-11-30 18:50:23 +03:00
// ------------------------------------------------------------------------
// Template Contents
// ------------------------------------------------------------------------
/**
* Factory method for VirtualRouter templates
*/
2019-09-03 17:31:51 +03:00
Template * get_new_template ( ) const override
2015-11-30 18:50:23 +03:00
{
return new Template ;
}
/**
* Returns a copy of the Template
* @ return A copy of the VirtualMachineTemplate
*/
Template * clone_template ( ) const
{
2016-02-07 20:19:25 +03:00
return new Template ( obj_template ) ;
2015-11-30 18:50:23 +03:00
} ;
2016-01-18 18:47:14 +03:00
Template * get_vm_template ( ) const ;
2015-12-16 14:32:19 +03:00
2016-02-04 15:17:48 +03:00
/**
* Replace template for this object . Object should be updated
* after calling this method
* @ param tmpl_str new contents
* @ param keep_restricted If true , the restricted attributes of the
* current template will override the new template
* @ param error string describing the error if any
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int replace_template ( const string & tmpl_str , bool keep_restricted , string & error ) override ;
2016-02-04 15:17:48 +03:00
/**
* Append new attributes to this object ' s template . Object should be updated
* after calling this method
* @ param tmpl_str new contents
* @ param keep_restricted If true , the restricted attributes of the
* current template will override the new template
* @ param error string describing the error if any
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int append_template ( const string & tmpl_str , bool keep_restricted , string & error ) override ;
2016-02-04 15:17:48 +03:00
2016-07-08 12:20:39 +03:00
/**
* Set the template ID to instantiate new VMs
*
* @ param tmpl_id VM Template ID
*/
void set_template_id ( int tmpl_id ) ;
/**
* Get the template ID set with set_template_id
*
* @ return VM Template ID , or - 1 if it was not found
*/
int get_template_id ( ) ;
2016-01-28 18:06:18 +03:00
// ------------------------------------------------------------------------
// Attach and detach NIC
// ------------------------------------------------------------------------
/**
* Adds a new NIC to the virtual router template .
* @ param tmpl Template , should contain only one NIC
2016-12-24 03:35:33 +03:00
* @ param error reason , if any
2016-01-28 18:06:18 +03:00
*
* @ return 0 on failure , the NIC to attach to each VM on success
*/
2016-12-24 03:35:33 +03:00
VectorAttribute * attach_nic ( VirtualMachineTemplate * tmpl , string & error ) ;
2016-01-28 18:06:18 +03:00
2016-01-28 19:20:52 +03:00
/**
* Deletes the NIC from the virtual router template .
*
* @ param nic_id of the NIC
* @ return 0 if the nic_id was found , - 1 otherwise
*/
int detach_nic ( int nic_id ) ;
2016-02-01 19:07:04 +03:00
// ------------------------------------------------------------------------
// Authorization related functions
// ------------------------------------------------------------------------
/**
* Sets an authorization request for a Virtual Router template based on
* the networks used
* @ param uid for template owner
* @ param ar the AuthRequest object
* @ param tmpl the virtual router template
2018-05-31 17:41:41 +03:00
* @ param check_lock for check if the resource is lock or not
2016-02-01 19:07:04 +03:00
*/
2018-05-31 13:50:02 +03:00
static void set_auth_request ( int uid , AuthRequest & ar , Template * tmpl ,
bool check_lock ) ;
2016-02-01 19:07:04 +03:00
2017-02-07 19:26:23 +03:00
// -------------------------------------------------------------------------
// VM Management
// -------------------------------------------------------------------------
//
/**
* Tries to shutdown , or delete , all this Virtual Router ' s VMs
* @ param ra attributes for the VirtualRouter delete operation
* @ param vms implementing the VirtualRouter
* @ param nics IP leased to the VirtualRouter
* @ return 0 on success , - 1 otherwise
*/
static int shutdown_vms ( const set < int > & vms , const RequestAttributes & ra ) ;
2015-11-30 18:50:23 +03:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class VirtualRouterPool ;
2015-12-01 18:36:50 +03:00
// *************************************************************************
// Attributes
// *************************************************************************
2015-12-11 13:45:34 +03:00
ObjectCollection vms ;
2015-12-01 18:36:50 +03:00
2015-11-30 18:50:23 +03:00
// *************************************************************************
// DataBase implementation (Private)
// *************************************************************************
/**
* Execute an INSERT or REPLACE Sql query .
* @ param db The SQL DB
* @ param replace Execute an INSERT or a REPLACE
* @ param error_str Returns the error reason , if any
* @ return 0 one success
*/
int insert_replace ( SqlDB * db , bool replace , string & error_str ) ;
/**
* Bootstraps the database table ( s ) associated to the VirtualRouter
* @ return 0 on success
*/
static int bootstrap ( SqlDB * db )
{
ostringstream oss ( VirtualRouter : : db_bootstrap ) ;
2017-04-21 20:16:45 +03:00
return db - > exec_local_wr ( oss ) ;
2015-11-30 18:50:23 +03:00
} ;
/**
* 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 ) ;
// *************************************************************************
// Constructor
// *************************************************************************
2016-12-24 03:35:33 +03:00
VirtualRouter ( int id ,
int uid ,
int gid ,
const string & uname ,
const string & gname ,
int umask ,
Template * _template_contents ) ;
2015-11-30 18:50:23 +03:00
2019-09-03 17:31:51 +03:00
virtual ~ VirtualRouter ( ) = default ;
2015-11-30 18:50:23 +03:00
// *************************************************************************
// DataBase implementation
// *************************************************************************
static const char * db_names ;
static const char * db_bootstrap ;
static const char * table ;
/**
* Writes the VirtualRouter in the database .
* @ param db pointer to the db
* @ param error_str Returns the error reason , if any
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int insert ( SqlDB * db , string & error_str ) override ;
2015-11-30 18:50:23 +03:00
2015-12-16 14:32:19 +03:00
/**
* Drops object from the database
* @ param db pointer to the db
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
virtual int drop ( SqlDB * db ) override ;
2015-12-16 14:32:19 +03:00
2015-11-30 18:50:23 +03:00
/**
* Writes / updates the VirtualRouter data fields in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2019-09-03 17:31:51 +03:00
int update ( SqlDB * db ) override
2015-11-30 18:50:23 +03:00
{
string err ;
return insert_replace ( db , true , err ) ;
} ;
2015-12-16 14:32:19 +03:00
// -------------------------------------------------------------------------
// NIC Management
// -------------------------------------------------------------------------
/**
* Get all network leases for this Virtual Router
* @ return 0 onsuccess
*/
int get_network_leases ( string & estr ) ;
/**
* Releases all network leases taken by this Virtual Router
*/
void release_network_leases ( ) ;
/**
* Releases the network lease taken by this NIC
*
* @ param nic NIC to be released
*
* @ return 0 on success , - 1 otherwise
*/
2016-02-07 20:19:25 +03:00
int release_network_leases ( const VectorAttribute * nic ) ;
2016-01-18 18:47:14 +03:00
2016-01-28 19:20:52 +03:00
/**
* Returns the nic with the giver nic_id , or 0
* @ param nic_id
* @ return nic if found , 0 if not found
*/
VectorAttribute * get_nic ( int nic_id ) const ;
2015-11-30 18:50:23 +03:00
} ;
# endif /*VIRTUAL_ROUTER_H_*/