2018-05-07 01:54:47 +03:00
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2018, OpenNebula Project, OpenNebula Systems */
/* */
/* 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 SCHED_ACTION_ATTRIBUTE_H_
# define SCHED_ACTION_ATTRIBUTE_H_
# include <set>
# include "VirtualMachineAttribute.h"
/**
* The VirtualMachine SCHED_ACTION attribute
*/
class SchedAction : public VirtualMachineAttribute
{
public :
enum Repeat
{
NONE = - 1 ,
WEEKLY = 0 ,
MONTHLY = 1 ,
YEARLY = 2 ,
HOURLY = 3
} ;
enum EndOn
{
2018-05-08 17:27:39 +03:00
END_NONE = - 1 ,
2018-05-08 12:55:50 +03:00
NEVER = 0 ,
TIMES = 1 ,
DATE = 2
2018-05-07 01:54:47 +03:00
} ;
SchedAction ( VectorAttribute * va , int id ) : VirtualMachineAttribute ( va , id ) { } ;
virtual ~ SchedAction ( ) { } ;
/**
* Returns the REPEAT value of the SCHED_ACTION
* @ param r repeat WEEKLY , MONTHLY , YEARLY or HOURLY
* @ return - 1 if REPEAT not found or in wrong format 0 on success
*/
int repeat ( Repeat & r ) ;
/**
* Returns the END_TYPE value of the SCHED_ACTION
* @ param eo ends on TIMES WEEKLY , MONTHLY , YEARLY or HOURLY
* @ return - 1 if REPEAT not found or in wrong format 0 on success
*/
int endon ( EndOn & eo ) ;
/**
* Parse the DAYS attribute of the sched action
* @ param d set of days ( unique )
* @ return - 1 if not found ( d will be empty ) 0 otherwise
*/
int days ( std : : set < int > & _d ) ;
/**
* This function checks that the DAYS attributes are in range
* according to the Repeat value :
* @ param r repeat type ( hourly , weekly . . . )
* @ param error in case of error
*
* @ return true if days are in range false ( error ) if not
*/
bool days_in_range ( Repeat r , std : : string & error ) ;
/**
* This function checks that END_VALUE is properly defined for the
* associated END_TYPE
* @ param eo end type ( date , times )
* @ param error in case of error
*
* @ return true if days are in range false ( error ) if not
*/
bool ends_in_range ( EndOn eo , std : : string & error ) ;
/**
* This function parse and checks the sched action attributes : REPEAT , DAYS
* , END_TYPE , END_DATE . It also removed DONE and MESSAGE .
* @ param error
2018-05-11 17:31:00 +03:00
* @ param clean indicates if the user wants to remove DONE and MESSAGE
2018-05-07 01:54:47 +03:00
* @ return 0 if success - 1 otherwise
*/
2018-05-11 17:31:00 +03:00
int parse ( std : : string & error , bool clean ) ;
2018-05-07 01:54:47 +03:00
2018-05-07 19:31:32 +03:00
/**
* @ return true if the action needs to be executed .
*/
bool is_due ( ) ;
2018-05-07 01:54:47 +03:00
/**
* Compute the next action , updating the TIME attribute for this action
* @ return - 1 if action ended 0 otherwise
*/
int next_action ( ) ;
} ;
/**
* Set of VirtualMachine SCHED_ACTION attributes
*/
class SchedActions : public VirtualMachineAttributeSet
{
public :
/* ---------------------------------------------------------------------- */
/* Constructor and Initialization functions */
/* ---------------------------------------------------------------------- */
/**
* Creates the SchedAction set from a Template with SCHED_ACTION = [ . . . ]
* attributes , id ' s are auto - assigned for internal use
* @ param tmpl template with DISK
*/
SchedActions ( Template * tmpl ) : VirtualMachineAttributeSet ( false )
{
std : : vector < VectorAttribute * > vas ;
tmpl - > get ( " SCHED_ACTION " , vas ) ;
2018-05-07 19:31:32 +03:00
init_attribute_map ( " TIME " , vas ) ;
2018-05-07 01:54:47 +03:00
} ;
virtual ~ SchedActions ( ) { } ;
/* ---------------------------------------------------------------------- */
/**
* Parse the ScheduleActions of a template
* @ param error
2018-05-11 17:31:00 +03:00
* @ param clean indicates if the user wants to remove DONE and MESSAGE
2018-05-07 01:54:47 +03:00
* @ return - 1 in case of error 0 otherwise
*/
2018-05-11 17:31:00 +03:00
int parse ( std : : string & error , bool clean )
2018-05-07 01:54:47 +03:00
{
for ( schedaction_iterator action = begin ( ) ; action ! = end ( ) ; + + action )
{
2018-05-11 17:31:00 +03:00
if ( ( * action ) - > parse ( error , clean ) = = - 1 )
2018-05-07 01:54:47 +03:00
{
return - 1 ;
}
}
return 0 ;
}
2018-05-07 19:31:32 +03:00
bool empty ( )
{
return a_set . empty ( ) ;
}
2018-05-07 01:54:47 +03:00
/* ---------------------------------------------------------------------- */
/* Iterators */
/* ---------------------------------------------------------------------- */
/**
* Generic iterator for the SchedActions set .
*/
class SchedActionIterator : public AttributeIterator
{
public :
SchedActionIterator ( ) : AttributeIterator ( ) { } ;
SchedActionIterator ( const AttributeIterator & i ) : AttributeIterator ( i ) { } ;
virtual ~ SchedActionIterator ( ) { } ;
SchedAction * operator * ( ) const
{
return static_cast < SchedAction * > ( map_it - > second ) ;
}
} ;
SchedActionIterator begin ( )
{
SchedActionIterator it ( ExtendedAttributeSet : : begin ( ) ) ;
return it ;
}
SchedActionIterator end ( )
{
SchedActionIterator it ( ExtendedAttributeSet : : end ( ) ) ;
return it ;
}
typedef class SchedActionIterator schedaction_iterator ;
protected :
VirtualMachineAttribute * attribute_factory ( VectorAttribute * va ,
int id ) const
{
return new SchedAction ( va , id ) ;
} ;
} ;
# endif /*SCHED_ACTION_ATTRIBUTE_H_*/