2014-09-08 13:50:25 +04:00
/* ------------------------------------------------------------------------ */
2022-04-07 20:49:58 +03:00
/* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */
2014-09-08 13:50:25 +04: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 SECURITYGROUP_H_
# define SECURITYGROUP_H_
# include "PoolObjectSQL.h"
2014-09-09 20:13:52 +04:00
# include "ObjectCollection.h"
2014-09-08 13:50:25 +04:00
/**
* The SecurityGroup class .
*/
class SecurityGroup : public PoolObjectSQL
{
public :
2020-09-10 10:08:29 +03:00
virtual ~ SecurityGroup ( ) = default ;
2014-09-08 13:50:25 +04:00
/**
* Function to print the SecurityGroup object into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2020-07-02 23:42:10 +03:00
std : : string & to_xml ( std : : string & xml ) const override ;
2014-09-08 13:50:25 +04:00
/**
* Rebuilds the object from an xml formatted string
* @ param xml_str The xml - formatted string
*
* @ return 0 on success , - 1 otherwise
*/
2020-07-02 23:42:10 +03:00
int from_xml ( const std : : string & xml_str ) override ;
2014-09-08 13:50:25 +04:00
/**
* Returns a copy of the Template
* @ return A copy of the Template
*/
2020-09-15 12:16:00 +03:00
std : : unique_ptr < Template > clone_template ( ) const
2014-09-08 13:50:25 +04:00
{
2020-09-15 12:16:00 +03:00
return std : : make_unique < Template > ( * obj_template ) ;
}
2014-09-08 13:50:25 +04:00
2014-09-09 20:13:52 +04:00
/* ---------------------------------------------------------------------- */
/* Access VM Counter */
/* ---------------------------------------------------------------------- */
/**
2016-03-02 01:31:31 +03:00
* Adds a VM ID to the security group ( up - to - date set )
2014-09-09 20:13:52 +04:00
* @ param vm_id The new id
*
* @ return 0 on success , - 1 if the ID was already in the set
*/
int add_vm ( int vm_id )
{
2016-03-02 01:31:31 +03:00
return updated . add ( vm_id ) ;
2014-09-09 20:13:52 +04:00
}
/**
2016-03-02 01:31:31 +03:00
* Deletes a VM ID from the security Group ( any of the sets )
2014-09-09 20:13:52 +04:00
* @ param vm_id The id
*/
2016-03-02 01:31:31 +03:00
void del_vm ( int vm_id )
2014-09-09 20:13:52 +04:00
{
2016-03-02 01:31:31 +03:00
if ( updated . del ( vm_id ) = = 0 )
{
return ;
}
if ( updating . del ( vm_id ) = = 0 )
{
return ;
}
if ( error . del ( vm_id ) = = 0 )
{
return ;
}
outdated . del ( vm_id ) ;
2014-09-09 20:13:52 +04:00
}
/**
* Returns how many VMs are using the security group .
* @ return how many IDs are there in the set .
*/
int get_vms ( ) const
{
2016-03-02 01:31:31 +03:00
return updated . size ( ) + updating . size ( ) + error . size ( ) + outdated . size ( ) ;
2014-09-09 20:13:52 +04:00
}
2014-09-10 20:59:10 +04:00
/**
* Returns a group of Vector Attributes , in the form
* SECURITY_GROUP_RULE = [ SECURITY_GROUP_ID = oid , . . . ]
*
* New objects are allocated , and must be deleted by the calling method
*
* @ return a group of vector attributes
*/
2020-07-02 23:42:10 +03:00
void get_rules ( std : : vector < VectorAttribute * > & result ) const ;
2014-09-10 20:59:10 +04:00
2016-03-02 01:31:31 +03:00
/**
* Commit SG changes to associated VMs
* @ param recover , if true It will propagate the changes to VMs in error
* and those being updated . Otherwise all VMs associated with the SG will
* be updated
*/
void commit ( bool recover )
{
if ( ! recover )
{
outdated < < updated ;
updated . clear ( ) ;
}
outdated < < updating < < error ;
updating . clear ( ) ;
error . clear ( ) ;
} ;
/**
* Functions to manipulate the vm collection id ' s
*/
int get_outdated ( int & id )
{
return outdated . pop ( id ) ;
}
2022-01-25 20:02:10 +03:00
bool is_outdated ( int id )
{
return outdated . contains ( id ) ;
}
int add_outdated ( int id )
{
return outdated . add ( id ) ;
}
2016-03-02 01:31:31 +03:00
int add_updating ( int id )
{
return updating . add ( id ) ;
}
2022-01-25 20:02:10 +03:00
bool is_updating ( int id )
{
return updating . contains ( id ) ;
}
2016-03-02 01:31:31 +03:00
int del_updating ( int id )
{
return updating . del ( id ) ;
}
int add_error ( int id )
{
return error . add ( id ) ;
}
2014-09-08 13:50:25 +04:00
private :
// -------------------------------------------------------------------------
// Friends
// -------------------------------------------------------------------------
friend class SecurityGroupPool ;
// *************************************************************************
// Constructor
// *************************************************************************
2020-07-02 23:42:10 +03:00
SecurityGroup ( int _uid ,
int _gid ,
const std : : string & _uname ,
const std : : string & _gname ,
int _umask ,
2020-09-15 12:16:00 +03:00
std : : unique_ptr < Template > sgroup_template ) ;
2014-09-08 13:50:25 +04:00
2014-11-13 18:00:03 +03:00
/**
* Check that a rule is valid
* @ param rule as a VectorAttribute
* @ param error describing the problem if any
* @ return true if the rule is valid
*/
2021-04-12 13:01:40 +03:00
bool is_valid ( const VectorAttribute * rule , std : : string & error ) const ;
/**
* Remove duplicit rules . The duplicits are removed from obj_template
* not from passed parameter
* @ param rules as vector of VectorAttributes
*/
void remove_duplicates ( std : : vector < VectorAttribute * > & rules ) ;
2014-11-13 18:00:03 +03:00
/**
* Checks the new rules
* @ param error string describing the error if any
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
int post_update_template ( std : : string & error ) override ;
2014-11-13 18:00:03 +03:00
2014-09-08 13:50:25 +04: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
*/
2020-07-02 23:42:10 +03:00
int insert_replace ( SqlDB * db , bool replace , std : : string & error_str ) ;
2014-09-08 13:50:25 +04:00
/**
* Bootstraps the database table ( s ) associated to the SecurityGroup
* @ return 0 on success
*/
2020-06-29 13:14:00 +03:00
static int bootstrap ( SqlDB * db ) ;
2014-09-08 13:50:25 +04:00
/**
* Writes the SecurityGroup in the database .
* @ param db pointer to the db
* @ return 0 on success
*/
2020-07-02 23:42:10 +03:00
int insert ( SqlDB * db , std : : string & error_str ) override ;
2014-09-08 13:50:25 +04:00
/**
* Writes / updates the SecurityGroup ' s 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
2014-09-08 13:50:25 +04:00
{
2020-07-02 23:42:10 +03:00
std : : string error_str ;
2014-09-08 13:50:25 +04:00
return insert_replace ( db , true , error_str ) ;
}
/**
* Factory method for SecurityGroup templates
*/
2020-09-15 12:16:00 +03:00
std : : unique_ptr < Template > get_new_template ( ) const override
2014-09-08 13:50:25 +04:00
{
2020-09-15 12:16:00 +03:00
return std : : make_unique < Template > ( ) ;
2014-09-08 13:50:25 +04:00
}
2014-09-09 20:13:52 +04:00
/**
2016-03-02 01:31:31 +03:00
* These collections stores the collection of VMs in the security
* group and manages the update process of a Security Group
* - updated VMs using the last version of the sg rules
* - outdated VMs with a previous version of the security group
* - updating VMs being updated , action sent to the drivers
* - error VMs that fail to update because of a wrong state or driver error
2014-09-09 20:13:52 +04:00
*/
2016-03-02 01:31:31 +03:00
ObjectCollection updated ;
ObjectCollection outdated ;
ObjectCollection updating ;
ObjectCollection error ;
2014-09-08 13:50:25 +04:00
} ;
# endif /*SECURITYGROUP_H_*/