2009-03-21 04:23:35 +03:00
/* -------------------------------------------------------------------------- */
2013-01-24 19:18:30 +04:00
/* Copyright 2002-2013, OpenNebula Project (OpenNebula.org), C12G Labs */
2009-03-21 04:23:35 +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 HOOK_H_
# define HOOK_H_
# include <vector>
# include <string>
using namespace std ;
//Forward definition of Hookable
class Hookable ;
2012-10-08 01:20:28 +04:00
class PoolObjectSQL ;
2009-03-21 04:23:35 +03:00
/**
* This class is an abstract representation of a hook , provides a method to
* check if the hook has to be executed , and a method to invoke the hook . The
* hook is a program that can be executed locally or in a remote host when a
* condition is satisfied
*/
class Hook
{
public :
2009-03-30 01:34:37 +04:00
/**
2009-04-09 04:58:43 +04:00
* Defines the hook type , so a whole hook class can be masked
2009-03-30 01:34:37 +04:00
*/
enum HookType
{
ALLOCATE = 0x1 ,
UPDATE = 0x2 ,
REMOVE = 0x4
} ;
2009-03-21 04:23:35 +03:00
//--------------------------------------------------------------------------
// Constructor and Destructor
//--------------------------------------------------------------------------
2009-04-04 03:34:33 +04:00
Hook ( const string & _name ,
2009-04-09 04:58:43 +04:00
const string & _cmd ,
2009-03-30 01:34:37 +04:00
const string & _args ,
2010-10-20 19:27:57 +04:00
int _ht ,
2009-03-30 01:34:37 +04:00
bool _remote ) :
2009-04-04 03:34:33 +04:00
name ( _name ) , cmd ( _cmd ) , args ( _args ) , hook_type ( _ht ) , remote ( _remote ) { } ;
2009-03-21 04:23:35 +03:00
virtual ~ Hook ( ) { } ;
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
//--------------------------------------------------------------------------
// Hook methods
//--------------------------------------------------------------------------
2009-03-30 01:34:37 +04:00
/**
* Returns the hook_type
*/
2010-10-20 19:27:57 +04:00
int type ( ) const
2009-03-30 01:34:37 +04:00
{
return hook_type ;
}
2009-03-21 04:23:35 +03:00
/**
* Executes the hook it self ( usually with the aid of the ExecutionManager )
2009-03-23 18:03:09 +03:00
* @ param arg additional arguments for the hook
2009-03-21 04:23:35 +03:00
*/
2009-03-23 18:03:09 +03:00
virtual void do_hook ( void * arg ) = 0 ;
2009-03-21 04:23:35 +03:00
2012-10-08 01:20:28 +04:00
/**
2012-10-08 14:33:32 +04:00
* Parses the arguments of the hook using a generic $ ID identifier , and
2012-10-08 01:20:28 +04:00
* the target object . $ TEMPLATE will be the base64 encoding of the
2012-10-08 14:33:32 +04:00
* template and $ ID the oid of the object .
2012-10-08 01:20:28 +04:00
* @ param obj pointer to the object executing the hook for
* @ param the resulting parser arguments
*/
void parse_hook_arguments ( PoolObjectSQL * obj ,
string & parsed ) ;
2009-03-21 04:23:35 +03:00
protected :
2009-04-04 03:34:33 +04:00
/**
* Name of the Hook
*/
string name ;
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
/**
* The command to be executed
*/
2009-03-30 01:34:37 +04:00
string cmd ;
2009-03-21 04:23:35 +03:00
/**
* The arguments for the command
*/
2009-03-30 01:34:37 +04:00
string args ;
2009-03-21 04:23:35 +03:00
2009-03-30 01:34:37 +04:00
/**
* The Hook Type
*/
2010-10-20 19:27:57 +04:00
int hook_type ;
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
/**
* True if the command is to be executed remotely
*/
2009-03-30 01:34:37 +04:00
bool remote ;
2009-03-21 04:23:35 +03:00
} ;
2012-10-08 01:20:28 +04:00
/**
* This class is general ObjectSQL Hook for allocation and removal .
* The object is looked when the hook is executed
*/
class AllocateRemoveHook : public Hook
{
protected :
AllocateRemoveHook ( const string & name ,
const string & cmd ,
const string & args ,
int hook_type ,
2012-10-08 14:33:32 +04:00
bool remote ) :
Hook ( name , cmd , args , hook_type , remote ) { } ;
2012-10-08 01:20:28 +04:00
virtual ~ AllocateRemoveHook ( ) { } ;
// -------------------------------------------------------------------------
// Hook methods
// -------------------------------------------------------------------------
void do_hook ( void * arg ) ;
virtual string & remote_host ( PoolObjectSQL * obj , string & hostname )
{
hostname . clear ( ) ;
return hostname ;
} ;
} ;
/**
* This class is general ObjectSQL Allocate Hook that executes a command
* when the ObjectSQL is inserted in the database . The object is looked when
* the hook is executed
*/
class AllocateHook : public AllocateRemoveHook
{
public :
// -------------------------------------------------------------------------
// Init a hook of ALLOCATE type
// -------------------------------------------------------------------------
AllocateHook ( const string & name ,
const string & cmd ,
const string & args ,
2012-10-08 14:33:32 +04:00
bool remote ) :
AllocateRemoveHook ( name , cmd , args , Hook : : ALLOCATE , remote ) { } ;
2012-10-08 01:20:28 +04:00
virtual ~ AllocateHook ( ) { } ;
} ;
/**
* This class is general ObjectSQL Remove Hook that executes a command
* when the ObjectSQL is inserted in the database .
*/
class RemoveHook : public AllocateRemoveHook
{
public :
// -------------------------------------------------------------------------
// Init a hook of ALLOCATE type
// -------------------------------------------------------------------------
RemoveHook ( const string & name ,
2012-10-08 14:33:32 +04:00
const string & cmd ,
const string & args ,
bool remote ) :
AllocateRemoveHook ( name , cmd , args , Hook : : REMOVE , remote ) { } ;
2012-10-08 01:20:28 +04:00
virtual ~ RemoveHook ( ) { } ;
} ;
2009-03-21 04:23:35 +03:00
/**
* Objects that inherits from hookable will allow others to hook into it . The
* Hookable interface handles the list of hooks and provide a method to invoke
* the hooks .
*/
class Hookable
{
public :
//--------------------------------------------------------------------------
// Constructor and Destructor Methods
//--------------------------------------------------------------------------
Hookable ( ) { } ;
virtual ~ Hookable ( )
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
delete hooks [ i ] ;
}
} ;
//--------------------------------------------------------------------------
// Hook Operations
//--------------------------------------------------------------------------
/**
* Hook in to the object
* @ param hk pointer to the hook , MUST be allocated in the HEAP
*/
void add_hook ( Hook * hk )
{
hooks . push_back ( hk ) ;
} ;
/**
* Removes all hooks from the object
*/
void clear_hooks ( )
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-04-09 04:58:43 +04:00
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
delete hooks [ i ] ;
}
hooks . clear ( ) ;
} ;
/**
* Iterates through the hooks , checking if they have to be executed and
* invokes them .
2009-03-23 18:03:09 +03:00
* @ param arg additional arguments for the hook
2009-03-21 04:23:35 +03:00
*/
2009-03-30 01:34:37 +04:00
void do_hooks ( void * arg = 0 , int hook_mask = 0xFF )
2009-03-21 04:23:35 +03:00
{
2009-03-27 03:32:02 +03:00
int sz = static_cast < int > ( hooks . size ( ) ) ;
2009-03-21 04:23:35 +03:00
for ( int i = 0 ; i < sz ; i + + )
{
2009-04-09 04:58:43 +04:00
if ( hooks [ i ] - > type ( ) & hook_mask )
2009-03-21 04:23:35 +03:00
{
2009-03-23 18:03:09 +03:00
hooks [ i ] - > do_hook ( arg ) ;
2009-03-21 04:23:35 +03:00
}
}
} ;
private :
/**
* Those that hooked in the object
*/
vector < Hook * > hooks ;
} ;
2009-03-27 03:32:02 +03:00
# endif