2015-11-30 18:50:23 +03:00
/* -------------------------------------------------------------------------- */
2016-05-04 13:33:23 +03:00
/* Copyright 2002-2016, 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
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
/**
* 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
*/
string & to_xml ( string & xml ) const ;
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-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
*/
Template * get_new_template ( ) const
{
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
*/
int replace_template ( const string & tmpl_str , bool keep_restricted , string & error ) ;
/**
* 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
*/
int append_template ( const string & tmpl_str , bool keep_restricted , string & error ) ;
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
* @ param error_str error reason , if any
*
* @ return 0 on failure , the NIC to attach to each VM on success
*/
2016-01-28 19:20:52 +03:00
VectorAttribute * attach_nic (
2016-01-28 18:06:18 +03:00
VirtualMachineTemplate * tmpl , string & error_str ) ;
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
*/
static void set_auth_request ( int uid ,
AuthRequest & ar ,
Template * tmpl ) ;
2016-02-09 18:33:13 +03:00
/**
* Checks if the given action is supported for Virtual Router VMs
* @ param action VM action to check
* @ return true if the action is supported for Virtual Router VMs
*/
static bool is_action_supported ( History : : VMAction action )
{
return SUPPORTED_ACTIONS . is_set ( action ) ;
}
2015-11-30 18:50:23 +03:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class VirtualRouterPool ;
2016-02-09 18:33:13 +03:00
static const ActionSet < History : : VMAction > SUPPORTED_ACTIONS ;
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 ) ;
return db - > exec ( oss ) ;
} ;
/**
* 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
// *************************************************************************
VirtualRouter ( int id ,
int uid ,
int gid ,
const string & uname ,
const string & gname ,
int umask ,
Template * _template_contents ) ;
~ VirtualRouter ( ) ;
// *************************************************************************
// 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
*/
int insert ( SqlDB * db , string & error_str ) ;
2015-12-16 14:32:19 +03:00
/**
* Drops object from the database
* @ param db pointer to the db
* @ return 0 on success
*/
virtual int drop ( SqlDB * db ) ;
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
*/
int update ( SqlDB * db )
{
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 ;
// -------------------------------------------------------------------------
// VM Management
// -------------------------------------------------------------------------
2016-01-18 18:47:14 +03:00
/**
* Tries to shutdown , or delete , all this Virtual Router ' s VMs
*
* @ return 0 on success , - 1 otherwise
*/
int shutdown_vms ( ) ;
2015-11-30 18:50:23 +03:00
} ;
# endif /*VIRTUAL_ROUTER_H_*/