2009-04-03 23:34:33 +00:00
/* -------------------------------------------------------------------------- */
2020-04-30 15:00:02 +02:00
/* Copyright 2002-2020, OpenNebula Project, OpenNebula Systems */
2009-04-03 23:34:33 +00: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_MANAGER_H_
# define HOOK_MANAGER_H_
2020-06-29 12:14:00 +02:00
# include "ProtocolMessages.h"
# include "DriverManager.h"
2009-04-05 20:34:33 +00:00
# include "ActionManager.h"
2009-04-03 23:34:33 +00:00
2019-09-09 14:43:51 +02:00
# include <vector>
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
class HMAction : public ActionRequest
{
public :
enum Actions
{
SEND_EVENT , /**< Send event to hook manager driver*/
RETRY /**< Send RETRY action to hook manager driver*/
} ;
HMAction ( Actions a , const std : : string & m ) : ActionRequest ( ActionRequest : : USER ) ,
_action ( a ) , _message ( m ) { } ;
HMAction ( const HMAction & o ) : ActionRequest ( o . _type ) , _action ( o . _action ) ,
_message ( o . _message ) { } ;
Actions action ( ) const
{
return _action ;
}
const std : : string & message ( ) const
{
return _message ;
}
ActionRequest * clone ( ) const
{
return new HMAction ( * this ) ;
}
private :
Actions _action ;
std : : string _message ;
} ;
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
2009-04-03 23:34:33 +00:00
2009-04-05 20:34:33 +00:00
extern " C " void * hm_action_loop ( void * arg ) ;
2020-06-29 12:14:00 +02:00
class HookManager :
public DriverManager < Driver < hook_msg_t > > ,
public ActionListener
2009-04-03 23:34:33 +00:00
{
public :
2020-06-29 12:14:00 +02:00
HookManager ( const std : : string & mad_location ) : DriverManager ( mad_location )
2009-04-05 20:34:33 +00:00
{
am . addListener ( this ) ;
2020-06-29 12:14:00 +02:00
}
2009-04-03 23:34:33 +00:00
2019-09-09 14:43:51 +02:00
virtual ~ HookManager ( ) = default ;
2009-04-03 23:34:33 +00:00
2009-04-05 20:34:33 +00:00
/**
2010-07-08 18:49:37 +02:00
* This functions starts the associated listener thread , and creates a
2009-04-05 20:34:33 +00:00
* new thread for the Hook Manager . This thread will wait in
* an action loop till it receives ACTION_FINALIZE .
* @ return 0 on success .
*/
int start ( ) ;
/**
* Gets the HookManager thread identification .
* @ return pthread_t for the manager thread ( that in the action loop ) .
*/
pthread_t get_thread_id ( ) const
{
return hm_thread ;
} ;
2009-04-03 23:34:33 +00:00
/**
2019-09-09 14:43:51 +02:00
* Loads Hook Manager Mads defined in configuration file
2020-06-29 12:14:00 +02:00
* @ param _mads configuration of drivers
2009-04-03 23:34:33 +00:00
*/
2020-06-29 12:14:00 +02:00
int load_drivers ( const std : : vector < const VectorAttribute * > & _mads ) ;
2009-04-03 23:34:33 +00:00
2009-04-05 20:34:33 +00:00
/**
2019-09-09 14:43:51 +02:00
* Triggers specific actions to the Hook Manager .
* @ param action the HM action
* @ param message to send to the driver
*/
void trigger ( HMAction : : Actions action , const std : : string & message )
{
HMAction hm_ar ( action , message ) ;
am . trigger ( hm_ar ) ;
}
/**
* Terminates the hook manager thread listener
2009-04-05 20:34:33 +00:00
*/
void finalize ( )
{
2017-02-03 14:19:15 +01:00
am . finalize ( ) ;
2020-06-29 12:14:00 +02:00
}
2009-04-05 20:34:33 +00:00
2009-04-03 23:34:33 +00:00
/**
2010-07-08 18:49:37 +02:00
* Returns a pointer to a Information Manager MAD . The driver is
2009-04-03 23:34:33 +00:00
* searched by its name and owned by oneadmin with uid = 0.
* @ param name of the driver
2010-07-08 18:49:37 +02:00
* @ return the Hook driver owned by uid 0 , with attribute " NAME " equal to
2009-04-03 23:34:33 +00:00
* name or 0 in not found
*/
2020-07-05 22:01:32 +02:00
const Driver < hook_msg_t > * get ( ) const
2009-04-03 23:34:33 +00:00
{
2020-06-29 12:14:00 +02:00
return DriverManager : : get_driver ( hook_driver_name ) ;
}
2010-07-08 18:49:37 +02:00
2020-07-05 22:01:32 +02:00
static std : : string format_message ( const std : : string & args ,
const std : : string & remote_host ,
int hook_id ) ;
2019-09-09 14:43:51 +02:00
2009-04-03 23:34:33 +00:00
private :
2010-07-08 18:49:37 +02:00
/**
2019-09-09 14:43:51 +02:00
* Function to execute the Manager action loop method within a new pthread
* ( requires C linkage )
2010-07-08 18:49:37 +02:00
*/
2019-09-09 14:43:51 +02:00
friend void * hm_action_loop ( void * arg ) ;
2010-07-08 18:49:37 +02:00
2009-04-03 23:34:33 +00:00
/**
2019-09-09 14:43:51 +02:00
* Generic name for the Hook driver
2009-04-03 23:34:33 +00:00
*/
2020-06-29 12:14:00 +02:00
static const char * hook_driver_name ;
2010-07-08 18:49:37 +02:00
2009-04-05 20:34:33 +00:00
/**
* Thread id for the HookManager
*/
pthread_t hm_thread ;
2010-07-08 18:49:37 +02:00
2009-04-05 20:34:33 +00:00
/**
* Action engine for the Manager
*/
ActionManager am ;
/**
2019-09-09 14:43:51 +02:00
* Send event message to the driver
* @ param message to pass to the driver
2009-04-05 20:34:33 +00:00
*/
2019-09-09 14:43:51 +02:00
void send_event_action ( const std : : string & message ) ;
/**
* Send retry message to the driver
* @ param message to pass to the driver
*/
void retry_action ( const std : : string & message ) ;
2009-04-05 20:34:33 +00:00
2020-06-29 12:14:00 +02:00
// -------------------------------------------------------------------------
// Protocol implementation, procesing messages from driver
// -------------------------------------------------------------------------
/**
*
*/
2020-07-02 22:42:10 +02:00
static void _undefined ( std : : unique_ptr < hook_msg_t > msg ) ;
2020-06-29 12:14:00 +02:00
/**
*
*/
2020-07-02 22:42:10 +02:00
void _execute ( std : : unique_ptr < hook_msg_t > msg ) ;
2020-06-29 12:14:00 +02:00
/**
*
*/
2020-07-02 22:42:10 +02:00
void _retry ( std : : unique_ptr < hook_msg_t > msg ) ;
2020-06-29 12:14:00 +02:00
/**
*
*/
2020-07-02 22:42:10 +02:00
static void _log ( std : : unique_ptr < hook_msg_t > msg ) ;
2020-06-29 12:14:00 +02:00
2017-02-03 14:19:15 +01:00
// -------------------------------------------------------------------------
// Action Listener interface
// -------------------------------------------------------------------------
2020-06-29 12:14:00 +02:00
static const int drivers_timeout = 10 ;
2017-02-03 14:19:15 +01:00
void finalize_action ( const ActionRequest & ar )
{
NebulaLog : : log ( " HKM " , Log : : INFO , " Stopping Hook Manager... " ) ;
2020-06-29 12:14:00 +02:00
DriverManager : : stop ( drivers_timeout ) ;
2017-02-03 14:19:15 +01:00
} ;
2019-09-09 14:43:51 +02:00
void user_action ( const ActionRequest & ar ) ;
2009-04-03 23:34:33 +00:00
} ;
# endif /*HOOK_MANAGER_H*/