1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #ifndef anna_core_oam_Module_hpp
38 #define anna_core_oam_Module_hpp
46 #include <anna/core/RuntimeException.hpp>
48 #include <anna/core/oam/defines.hpp>
49 #include <anna/core/oam/Handler.hpp>
60 class CounterRecorder;
72 * Class used to implement context-module-alarms/counters. Each process module (within library or process itself) must
73 * inherit from this, defining specific-alarm/counters codes (better public enum type) which will be managed within each
74 * module (usually through a management singleton class). This allow code reusability regarding alarm/counter conditions
75 * usually badly managed with API return codes instead of self-governing alarms shared and reusabled by any API client,
76 * or counters with fixed-inflexible values. It is posible to manage more than one oam module per library or proccess,
77 * simply defining more children classes, but the normal way is to use only one.
83 * class OamModule : public anna::oam::Module, public anna::Singleton <OamModule>
93 * anna_declare_enum(Counter);
101 * ErrorOnNode__s__WithSessionId__d__
104 * anna_declare_enum(Alarm);
107 * OamModule() : anna::oam::Module ("Main Proccess 'HTE_CLDAP' OAM module") {;};
109 * friend class anna::Singleton <OamModule>;
112 * Normally, we will use one scope per module (library/proccess) but we could define many oam modules per subsystem functionality.
113 * For example, libanna.diameter.b have oam modules for comm.db and codec.db. Also, macros as 'anna_declare_enum' are useful to
114 * define default descriptions for counter and alarm types.
117 * == REGISTRATION for all the linked scopes ==
119 * anna::<project>::oam::Handler *projectHandler = new anna::<project>::oam::Handler(); // handler for project alarms
121 * OamModule & oam_proc = OamModule::instantiate();
122 * <library_namespace>::OamModule & oam_lib = <library_namespace>::OamModule::instantiate();
123 * oam_proc.setHandler(projectHandler);
124 * oam_proc.initializeCounterScope (1);
125 * oam_proc.registerCounter (OamModule::Counter::ErrorLDAP, "ErrorLDAP", 0);
126 * oam_proc.registerAlarm (OamModule::Alarm::ErrorOnNode__s__WithSessionId__d__, "Error on node", 1, "nodeName,errorCode", anna::oam::MSAlarmId::NODE_ERROR);
128 * oam_lib.setHandler(projectHandler);
129 * oam_lib.initializeCounterScope (2); // different scope for different modules (normal way)
130 * oam_lib.registerCounter (<library_namespace>::OamModule::Counter::<type>, "counter description", 0);
131 * oam_lib.registerAlarm (<library_namespace>::OamModule::Alarm::<type>, "alarm description", 2, <dynamic variable names CSL>, anna::oam::MSAlarmId::<type>);
133 * -> LAUNCH for all local scopes (library launches will be done at library code):
134 * oam_proc.count (OamModule::Counter::ErrorLDAP);
135 * oam_proc.activateAlarm (OamModule::Alarm::ErrorOnNode__s__WithSessionId__d__, "my node", a_session_id);
138 * == MULTI-CONTEXT APPLICATIONS ==
140 * Suppose two contexts, one with scopes 1 and 4 for process and library respectively, and
141 * the other defined by 2 and 5:
143 * oam_proc.initializeCounterScope (1, "Main OAM Module - Context A");
144 * oam_proc.initializeCounterScope (2, "Main OAM Module - Context B");
145 * Counters registration ...
147 * oam_lib.initializeCounterScope (4, "Encoder OAM Module - Context A");
148 * oam_lib.initializeCounterScope (5, "Encoder OAM Module - Context B");
149 * Counters registration ...
151 * Application must switch between these scope ids to distinguise the contexts:
154 * oam_proc.setActiveCounterScope(1);
155 * oam_lib.setActiveCounterScope(4);
158 * oam_proc.setActiveCounterScope(2);
159 * oam_lib.setActiveCounterScope(5);
165 Handler a_defaultHandler; // default OAM handler
166 Handler *a_handler; // Handler reference
168 std::string a_className; // module description
169 bool a_counters_enabled; // Enable/Disable registered counters over this module (default is 'true')
170 bool a_alarms_enabled; // Enable/Disable registered alarms over this module (default is 'true')
172 // dynamic modifications over alarm text
173 bool a_alarms_preffix_enabled; // Show own module alarm preffix
174 bool a_alarms_suffix_enabled; // Show own module alarm suffix
175 std::string alarmComponentsToText(const std::vector<std::string> & components, const std::string & psL, const std::string & psS, const std::string & psR) const throw();
178 anna::oam::CounterScope* a_active_counter_scope; // Current scope for counters generation
179 typedef std::map <int, anna::oam::CounterScope*> scope_container;
180 scope_container a_scopes; // Module can manage N scope clones (usually, there is only one registered scope: mono-context applications)
181 typedef std::map < int /*type*/, counter_data_t > counter_container;
182 counter_container a_counters;
185 typedef std::map < int /*type*/, alarm_data_t > alarm_container;
186 alarm_container a_alarms;
188 void alarmEvent(bool activation, const int & type, va_list argList) const throw();
191 typedef scope_container::iterator scope_iterator;
192 typedef scope_container::const_iterator const_scope_iterator;
193 scope_iterator scope_find(const int &key) throw() { return a_scopes.find(key); }
194 scope_iterator scope_begin() throw() { return a_scopes.begin(); }
195 scope_iterator scope_end() throw() { return a_scopes.end(); }
196 static anna::oam::CounterScope* scope(scope_iterator ii) throw() { return ii->second; }
197 const_scope_iterator scope_begin() const throw() { return a_scopes.begin(); }
198 const_scope_iterator scope_end() const throw() { return a_scopes.end(); }
199 static const anna::oam::CounterScope* scope(const_scope_iterator ii) throw() { return ii->second; }
200 anna::oam::CounterScope *getScope(const int &id) throw();
201 typedef counter_container::iterator counter_iterator;
202 typedef counter_container::const_iterator const_counter_iterator;
203 // bool counter_remove(const int &key) throw();
204 const_counter_iterator counter_find(const int &key) const throw() { return a_counters.find(key); }
205 const_counter_iterator counter_begin() const throw() { return a_counters.begin(); }
206 const_counter_iterator counter_end() const throw() { return a_counters.end(); }
207 counter_iterator counter_find(const int &key) throw() { return a_counters.find(key); }
208 counter_iterator counter_begin() throw() { return a_counters.begin(); }
209 counter_iterator counter_end() throw() { return a_counters.end(); }
210 CounterRecorder* a_counterRecorder;
211 bool a_counterRecording;
213 class RecordingGuard {
215 RecordingGuard(Module*);
222 typedef alarm_container::iterator alarm_iterator;
223 typedef alarm_container::const_iterator const_alarm_iterator;
224 // bool alarm_remove(const int &key) throw();
225 const_alarm_iterator alarm_find(const int &key) const throw() { return a_alarms.find(key); }
226 const_alarm_iterator alarm_begin() const throw() { return a_alarms.begin(); }
227 const_alarm_iterator alarm_end() const throw() { return a_alarms.end(); }
228 alarm_iterator alarm_find(const int &key) throw() { return a_alarms.find(key); }
229 alarm_iterator alarm_begin() throw() { return a_alarms.begin(); }
230 alarm_iterator alarm_end() throw() { return a_alarms.end(); }
231 void getAlarmPreffixSuffixAndZoneSeparator(std::string & preffix, std::string & suffix, char & zS) const throw();
236 static const int MaxScope = 100; /**< Numero maximo de ambitos */
240 @param className Logical name for the class (better use fullNaming format including namespace resolution)
242 Module(const std::string &className) : a_className(className),
243 a_handler(&a_defaultHandler),
244 a_counters_enabled(true),
245 a_counterRecorder(NULL),
246 a_counterRecording(false),
247 a_alarms_enabled(true),
248 a_alarms_preffix_enabled(true),
249 a_alarms_suffix_enabled(true) {;}
258 * Enable all the counters registered in this module (enabled by default at constructor).
259 * Usually managed at PROCCESS implementation
261 void enableCounters(void) throw();
264 * Disable all the counters registered in this module (enabled by default at constructor).
265 * Usually managed at PROCCESS implementation
267 void disableCounters(void) throw();
270 * Enable all the alarms registered in this module (enabled by default at constructor).
271 * Usually managed at PROCCESS implementation
273 void enableAlarms(void) throw();
276 * Disable all the alarms registered in this module (enabled by default at constructor).
277 * Usually managed at PROCCESS implementation
279 void disableAlarms(void) throw();
282 * Show own module alarm preffix (enabled by default at constructor).
283 * Usually managed at PROCCESS implementation
285 void enableAlarmsPreffix(void) throw();
288 * Show own module alarm suffix (enabled by default at constructor).
289 * Usually managed at PROCCESS implementation
291 void enableAlarmsSuffix(void) throw();
294 * Hide own module alarm preffix (enabled by default at constructor).
295 * Usually managed at PROCCESS implementation
297 void disableAlarmsPreffix(void) throw();
300 * Hide own module alarm suffix (enabled by default at constructor).
301 * Usually managed at PROCCESS implementation
303 void disableAlarmsSuffix(void) throw();
306 * Sets the operations handler. By default, all modules will use the default anna::oam::Handler.
307 * This method will be used only when a different behaviour is needed, for example for the project
310 * Used ONLY at PROCCESS implementation (initial tasks)
312 * @param handler Handler used for OAM operations (registering and launch). NULL is ignored
314 void setHandler(Handler *handler) throw() { if(handler) a_handler = handler; }
317 * Counter scope registration. Usually, only one scope id will be registered, but multicontext applications
318 * could manage scope clones where all the counters are REPLICATED in order to manage separate sub-facilities.
319 * Multicontext applications must invoke N times to this method, and then register the common counters.
320 * Each context must activate with 'setActiveCounterScope()', the correct scope id.
321 * After invocation, provided scope id will be activated.
322 * Used ONLY at PROCCESS implementation (initial tasks)
324 * @param scopeId Counter scope id. It must be non negative (0 is usually reserved for core platform counters).
325 * @param description Counter scope id description. If missing, module description will be set, but is
326 * a good idea add different names between replicated scopes, i.e.: 'Main OAM Module - Context X', etc.
327 * better than 'Main OAM Module' for all of them. Also, you can use the same description for all scopes
328 * (that is the case of default assignment).
330 void initializeCounterScope(const int & scopeId, const std::string & description = "") throw(anna::RuntimeException);
334 * Multicontext application could decide the correct scope id in order to sure right sub-module counting.
335 * Applications that only initializes one scope (which becomes active after that), don't need to use this method.
336 * Used ONLY at PROCCESS implementation (normal tasks)
338 * @param scopeId Counter scope id which becomes active.
340 void setActiveCounterScope(const int & scopeId) throw();
344 * Gets the current activated counter scope
346 * @return Activated counter scope
348 const anna::oam::CounterScope* getActiveCounterScope() const throw() { return a_active_counter_scope; }
351 * Child oam module classes should define descriptions for each enum type. A good practice would be the use of
352 * 'anna_declare_enum' macro in order to define names for enum types. This is an oam-internal description which
353 * should be redefined at application layer during registering. Returns undefined by default.
355 * @param type Counter enum-identification within the own context/module
357 * @return Default alarm description
359 virtual std::string getDefaultInternalAlarmDescription(const int & type) const throw();
362 * Child oam module classes should define descriptions for each enum type. A good practice would be the use of
363 * 'anna_declare_enum' macro in order to define names for enum types. This is an oam-internal description which
364 * should be redefined at application layer during registering. Returns undefined by default.
366 * @param type Counter enum-identification within the own context/module
368 * @return Default counter description
370 virtual std::string getDefaultInternalCounterDescription(const int & type) const throw();
374 * Counter registration providing the specific documentacion codes.
375 * To guarantee clone operations, no scope initialization will be possible after use of this method.
376 * Used ONLY at PROCCESS implementation (initial tasks)
378 * @param type Counter enum-identification added within the module (defined enum type on @Singleton child class)
379 * @param description Counter description added within the module. If empty string is provided, default description
380 * for non-registered will be searched (#getDefaultInternalCounterDescription).
381 * @param offset Counter offset over (1000 * scope id). Offset has 0-999 range.
383 void registerCounter(const int &type, const std::string &description, const int &offset) throw(anna::RuntimeException);
387 * Alarm registration providing the specific process codes useful for manage any kind of alarm generation: external id
388 * (which could be a database unique idenfier), dynamic variables used during text parsing to allow advanced manipulation,
389 * and activation/cancellation codes (which could be used at a certain management system node).
391 * @param type Alarm enum-identification added within the module (defined enum type on @Singleton child class)
392 * @param description Alarm description added within the module. If empty string is provided, default description
393 * for non-registered will be searched (#getDefaultInternalAlarmDescription).
394 * @param externalId External text identification.
395 * @param dynamicVariablesCSL Comma-separated list of dynamic variable names (same order than launched with #activateAlarm and #cancelAlarm).
396 * @param activationId Alarm activation identifier
397 * @param cancellationId Alarm cancellation identifier. If missing, the alarm will interpreted as non-transferable
399 void registerAlarm(const int &type, const std::string &description, const int &externalId, const std::string &dynamicVariablesCSL, const int &activationId, const int &cancellationId = -1) throw(anna::RuntimeException);
403 Gets the OAM module name
405 @param OAM module name
407 const char *getClassName() const throw() { return a_className.c_str(); }
411 Gets counter data for type provided. NULL if not found.
413 @param type Alarm enum-identification within the own context/module.
415 const counter_data_t *counter(const int &type) const throw() {
416 const_counter_iterator it = counter_find(type);
417 return ((it != counter_end()) ? (&(*it).second) : NULL);
421 Gets alarm data for type provided. NULL if not found.
423 @param type Counter enum-identification within the own context/module.
425 const alarm_data_t *alarm(const int &type) const throw() {
426 const_alarm_iterator it = alarm_find(type);
427 return ((it != alarm_end()) ? (&(*it).second) : NULL);
432 * Notifies counter increase with certain amount within the ativated scope
433 * Used at MODULE implementation (library or proccess itself)
435 * @param type Counter enum-identification within the own context/module
436 * @param amount Units increased. Default is 1
438 void count(const int & type, const int & amount = 1) throw(anna::RuntimeException);
442 * Reset all counters accumulated amount (analysis purposes)
443 * Usually managed at PROCCESS implementation
445 * @param scopeId Restrict reset to provided scope id. If missing, all scopes will be affected.
447 * @return Number of affected counters which have been reset (only those which have non-zero accumulated count).
449 int resetCounters(const int & scopeId = -1) throw();
453 * Sets the instance for the class which will save the counters information.
454 * @counterRecorder Provided instance
455 * @warning It should be invoked periodically as a common solution
457 void setCounterRecorder(CounterRecorder* counterRecorder) throw() { a_counterRecorder = counterRecorder; }
460 * Dumps the modified counters from last invocation to this method.
461 * A counter recorder should have been assigned by mean setCounterRecorder(), which
462 * will have the specific behaviour. This procedure is oriented to have physical storage
463 * for counters information.
465 void recordCounters() throw(anna::RuntimeException);
469 * Activates alarm with dynamic-composed text parsed with provided data (...).
470 * Used at MODULE implementation (library or proccess itself)
472 * @param alarmType Alarm enum-identification within the own context/module
473 * @param ... Optional parsing data for dynamic-composed text.
475 void activateAlarm(const int & type, ...) const throw(anna::RuntimeException);
479 * Send transferable alarm cancellation, with dynamic-composed text parsed with provided data (...)
480 * Used at MODULE implementation (library or proccess itself)
482 * @param alarmType Alarm enum-identification within the own context/module
483 * @param ... Optional parsing data for dynamic-composed text.
485 void cancelAlarm(const int & type, ...) const throw(anna::RuntimeException);
489 * Class string representation
490 * Usually managed at PROCCESS implementation
492 * @return String with class content
494 virtual std::string asString(void) const throw();
498 * Class XML representation
499 * Usually managed at PROCCESS implementation
501 * @return XML with class content
503 virtual anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
509 * Module alarm preffix components used to add aditional information over alarm text.
510 * Oam modules push-back this additional components to global 'Configuration' preffix components.
511 * To disable, use 'disableAlarmsPreffix()'.
512 * Note that preffix components string should be multilanguage texts if alarm texts are based on
513 * language-context traslations.
514 * Used at MODULE implementation (library or proccess itself)
516 * @param components Alarm preffix components defined by oam module. Empty on default implementation.
518 virtual void readAlarmPreffixComponents(std::vector<std::string> & components) const throw() {;}
522 * Module alarm suffix components used to add aditional information over alarm text.
523 * Oam modules push-back this additional components to global 'Configuration' suffix components.
524 * To disable, use 'disableAlarmsSuffix()'.
525 * Note that suffix components string should be multilanguage texts if alarm texts are based on
526 * language-context traslations.
527 * Used at MODULE implementation (library or proccess itself)
529 * @param components Alarm suffix components defined by oam module. Empty on default implementation.
531 virtual void readAlarmSuffixComponents(std::vector<std::string> & components) const throw() {;}
534 friend class RecordingGuard;