2017-01-03 04:06:51 +01:00
/* ------------------------------------------------------------------------ */
2023-01-09 12:23:19 +01:00
/* Copyright 2002-2023, OpenNebula Project, OpenNebula Systems */
2017-01-03 04:06:51 +01: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 VMGROUP_ROLE_H_
# define VMGROUP_ROLE_H_
2020-07-02 22:42:10 +02:00
# include "Template.h"
2017-01-03 04:06:51 +01:00
class VMGroupPool ;
2017-01-20 20:46:50 +01:00
enum class VMGroupPolicy ;
2017-01-03 04:06:51 +01:00
/**
* A VMGroupRole defines a VM type that typically implements a role in a
* multi - vm application .
*
* ROLE = [
* NAME = " Web application servers " ,
* ID = 12 ,
2017-01-13 01:56:25 +01:00
* POLICY = AFFINED ,
2017-01-03 04:06:51 +01:00
* VMS = " 1,2,45,21 "
* ]
*
*/
class VMGroupRole
{
public :
VMGroupRole ( VectorAttribute * _va ) ;
2017-01-13 01:56:25 +01:00
/**
* @ return the role id
*/
2020-07-05 22:01:32 +02:00
int id ( ) const
2017-01-13 01:56:25 +01:00
{
int rid ;
va - > vector_value ( " ID " , rid ) ;
return rid ;
}
2017-01-13 18:32:37 +01:00
/**
* @ return the role name
*/
2020-07-05 22:01:32 +02:00
std : : string name ( ) const
2017-01-13 18:32:37 +01:00
{
return va - > vector_value ( " NAME " ) ;
}
2023-09-14 15:36:26 +02:00
/**
* Set role name
*/
void name ( const std : : string & new_name )
{
va - > replace ( " NAME " , new_name ) ;
}
2017-01-25 20:08:12 +01:00
/**
* @ return the set of VMs in a string in a comma separated list
*/
2020-07-05 22:01:32 +02:00
std : : string vms_s ( ) const
2017-01-25 20:08:12 +01:00
{
return va - > vector_value ( " VMS " ) ;
}
2017-01-18 00:31:50 +01:00
/**
* @ return the policy of this role
*/
2017-01-20 20:46:50 +01:00
VMGroupPolicy policy ( ) ;
2017-01-18 00:31:50 +01:00
2023-03-08 15:52:20 +01:00
std : : string policy_s ( ) const
2017-01-13 18:32:37 +01:00
{
2017-01-18 00:31:50 +01:00
return va - > vector_value ( " POLICY " ) ;
} ;
2017-01-13 18:32:37 +01:00
2023-09-14 15:36:26 +02:00
/**
* Function to print the VMGroupRole into a string stream in XML format
* @ param xml Output string stream
*/
void to_xml ( std : : ostringstream & oss ) const ;
void update ( VectorAttribute * va_update ) ;
2017-01-03 04:06:51 +01:00
/* ---------------------------------------------------------------------- */
/* VMS set Interface */
/* ---------------------------------------------------------------------- */
2020-07-05 22:01:32 +02:00
const std : : set < int > & get_vms ( ) const
2017-01-03 04:06:51 +01:00
{
return vms ;
} ;
2020-07-05 22:01:32 +02:00
int size_vms ( ) const
2017-01-20 20:46:50 +01:00
{
return vms . size ( ) ;
}
2017-01-03 04:06:51 +01:00
void add_vm ( int vm_id ) ;
void del_vm ( int vm_id ) ;
2017-01-13 01:56:25 +01:00
/* ---------------------------------------------------------------------- */
/* Placement constraints */
/* ---------------------------------------------------------------------- */
/**
2017-01-16 19:19:05 +01:00
* Generates a string with the boolean expression to conform the role
* internal policy
2017-01-13 01:56:25 +01:00
* @ param vm_id of the VM to generate the requirements for
* @ param requirements
*/
2017-01-16 19:19:05 +01:00
void vm_role_requirements ( int vm_id , std : : string & requirements ) ;
/**
* Generates a string with the boolean expression to conform an affinity
* constraint policy
2017-01-20 20:46:50 +01:00
* @ param p policy to place VMs respect to this role VMs
2017-01-16 19:19:05 +01:00
* @ param requirements
*/
2017-01-20 20:46:50 +01:00
void role_requirements ( VMGroupPolicy p , std : : string & requirements ) ;
2017-01-13 01:56:25 +01:00
2017-01-25 20:08:12 +01:00
/**
* Gets the placement requirements for the affined HOSTS
* @ param reqs string with the requirements expression
*/
void affined_host_requirements ( std : : string & reqs ) ;
/**
* Gets the placement requirements for the antiaffined HOSTS
* @ param reqs string with the requirements expression
*/
void antiaffined_host_requirements ( std : : string & reqs ) ;
/**
* Generate generic requirements for a set of hosts
* @ param hosts the set
* @ param op1 operator for each host requirement = or ! =
* @ param op2 operator to join host requirements & or |
* @ param oss stream where the requirement expression is output
*/
static void host_requirements ( std : : set < int > & hosts , const std : : string & op1 ,
2024-06-03 11:40:24 +02:00
const std : : string & op2 , std : : ostringstream & oss ) ;
2017-01-25 20:08:12 +01:00
2017-01-03 04:06:51 +01:00
private :
/**
* The set of vms in the role
*/
std : : set < int > vms ;
/**
* The associated vector attribute
*/
2023-11-10 10:34:04 +01:00
std : : unique_ptr < VectorAttribute > va ;
2017-01-03 04:06:51 +01:00
/**
* Set the VMS attribute for the role ( list of VM IDs )
*/
void set_vms ( ) ;
} ;
/**
* The VMGroupRoles class represents a set of ROLES stored in a Template
*/
class VMGroupRoles
{
public :
2023-09-14 15:36:26 +02:00
VMGroupRoles ( ) = default ;
// Disable copy constructor
VMGroupRoles ( const VMGroupRoles & ) = delete ;
// Disable copy assignment
VMGroupRoles & operator = ( const VMGroupRoles & ) = delete ;
2017-01-03 04:06:51 +01:00
~ VMGroupRoles ( )
{
2023-09-14 15:36:26 +02:00
delete_roles ( ) ;
}
2017-01-03 04:06:51 +01:00
2017-01-16 19:19:05 +01:00
/**
* Max number of roles in a VMGroup
*/
const static int MAX_ROLES = 32 ;
2017-01-03 04:06:51 +01:00
/**
* Function to print the VMGroupRoles into a string in XML format
* @ param xml the resulting XML string
* @ return a reference to the generated string
*/
2023-09-14 15:36:26 +02:00
std : : string & to_xml ( std : : string & xml_str ) const ;
2017-01-03 04:06:51 +01:00
/**
* Builds the object from an xml node
* @ param node for the ROLES template
* @ return 0 on success , - 1 otherwise
*/
int from_xml_node ( const xmlNodePtr node ) ;
/**
* Adds a new role to the set
* @ param vrole VectorAttribute of the role
* @ param error string if any
*
* @ return 0 on success
*/
2020-07-02 22:42:10 +02:00
int add_role ( VectorAttribute * vrole , std : : string & error ) ;
2017-01-03 04:06:51 +01:00
2023-09-14 15:36:26 +02:00
/**
* Delete role from the set
* @ param id ID of the role
*/
void del_role ( int id ) ;
int rename_role ( VMGroupRole * role , const std : : string & new_name ) ;
2017-01-03 04:06:51 +01:00
/**
2017-01-13 01:56:25 +01:00
* Generates the ids corresponding to a set of role names
2017-01-16 19:19:05 +01:00
* @ param rnames string with a comma separated list of role names
2017-01-13 01:56:25 +01:00
* @ param keyi the set of ids
2017-01-16 19:19:05 +01:00
*
2017-01-13 01:56:25 +01:00
* @ return 0 if all the names were successfully translated
2017-01-03 04:06:51 +01:00
*/
2017-01-16 19:19:05 +01:00
int names_to_ids ( const std : : string & rnames , std : : set < int > & keyi ) ;
2017-01-03 04:06:51 +01:00
2017-01-03 23:08:47 +01:00
/**
* Adds a VM to a role
* @ param role_name
* @ param vmid
*
* @ return 0 if VM was successfully added , - 1 otherwise
*/
int add_vm ( const std : : string & role_name , int vmid ) ;
/**
* Deletes a VM from a role
* @ param role_name
* @ param vmid
*
* @ return 0 if VM was successfully added , - 1 otherwise
*/
int del_vm ( const std : : string & role_name , int vmid ) ;
2017-01-04 15:23:35 +01:00
/**
* @ return the total number of VMs in the group
*/
int vm_size ( ) ;
2017-01-13 18:32:37 +01:00
/**
* @ return the a VMGroupRole by its name
* @ param rname role name
*/
VMGroupRole * get ( const std : : string & rname )
{
2023-09-14 15:36:26 +02:00
auto it = by_name . find ( rname ) ;
if ( it = = by_name . end ( ) )
{
return nullptr ;
}
return it - > second ;
2017-01-13 18:32:37 +01:00
}
/**
* @ return the a VMGroupRole by its id
* @ param rname role name
*/
VMGroupRole * get ( int id )
{
2023-09-14 15:36:26 +02:00
auto it = by_id . find ( id ) ;
if ( it = = by_id . end ( ) )
{
return nullptr ;
}
return it - > second ;
2017-01-13 18:32:37 +01:00
}
2017-01-16 19:19:05 +01:00
/* ---------------------------------------------------------------------- */
/* ---------------------------------------------------------------------- */
2017-01-13 01:56:25 +01:00
/**
2017-01-16 19:19:05 +01:00
* Iterator for the roles in the group
2017-01-13 01:56:25 +01:00
*/
2017-01-16 19:19:05 +01:00
class RoleIterator
{
public :
RoleIterator & operator = ( const RoleIterator & rhs )
{
role_it = rhs . role_it ;
return * this ;
}
RoleIterator & operator + + ( )
{
+ + role_it ;
return * this ;
}
bool operator ! = ( const RoleIterator & rhs )
{
return role_it ! = rhs . role_it ;
}
VMGroupRole * operator * ( ) const
{
return role_it - > second ;
}
2024-06-03 11:40:24 +02:00
RoleIterator ( ) { } ;
RoleIterator ( const RoleIterator & rit ) : role_it ( rit . role_it ) { } ;
2017-01-16 19:19:05 +01:00
RoleIterator ( const std : : map < int , VMGroupRole * > : : iterator & _role_it )
2024-06-03 11:40:24 +02:00
: role_it ( _role_it ) { } ;
2017-01-16 19:19:05 +01:00
2024-06-03 11:40:24 +02:00
virtual ~ RoleIterator ( ) { } ;
2017-01-16 19:19:05 +01:00
private :
std : : map < int , VMGroupRole * > : : iterator role_it ;
} ;
RoleIterator begin ( )
{
RoleIterator it ( by_id . begin ( ) ) ;
return it ;
}
RoleIterator end ( )
{
RoleIterator it ( by_id . end ( ) ) ;
return it ;
}
typedef class RoleIterator role_iterator ;
2017-01-13 01:56:25 +01:00
2017-01-03 04:06:51 +01:00
private :
/**
2023-09-14 15:36:26 +02:00
* The next role id
2017-01-03 04:06:51 +01:00
*/
2023-09-14 15:36:26 +02:00
int next_role = 0 ;
2017-01-03 04:06:51 +01:00
/**
2023-09-14 15:36:26 +02:00
* Map to access the roles by their name
2017-01-03 04:06:51 +01:00
*/
2023-09-14 15:36:26 +02:00
std : : map < std : : string , VMGroupRole * > by_name ;
2017-01-03 04:06:51 +01:00
/**
2023-09-14 15:36:26 +02:00
* Map to access the roles by their id
2017-01-03 04:06:51 +01:00
*/
2023-09-14 15:36:26 +02:00
std : : map < int , VMGroupRole * > by_id ;
2017-01-03 04:06:51 +01:00
/**
2023-09-14 15:36:26 +02:00
* Frees the memory associated with the roles
2017-01-03 04:06:51 +01:00
*/
2023-09-14 15:36:26 +02:00
void delete_roles ( )
{
for ( auto it : by_id )
{
delete it . second ;
}
2017-01-03 04:06:51 +01:00
2023-09-14 15:36:26 +02:00
by_id . clear ( ) ;
by_name . clear ( ) ;
}
2017-01-03 04:06:51 +01:00
} ;
# endif /*VMGROUP_ROLE_H*/