First commit
[anna.git] / source / core / oam / Handler.cpp
1 // ANNA - Anna is Not 'N' Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 // Local
38 #include <anna/core/oam/Module.hpp>
39 #include <anna/core/oam/Handler.hpp>
40 #include <anna/core/functions.hpp>
41
42 #include <anna/core/tracing/Logger.hpp>
43 #include <anna/core/tracing/TraceWriter.hpp>
44 #include <anna/core/tracing/TraceMethod.hpp>
45 #include <anna/core/functions.hpp>
46 #include <anna/core/oam/CounterManager.hpp>
47
48 // Standard
49 #include <stdarg.h>
50
51
52
53 //------------------------------------------------------------------------------
54 //-------------------------------------------------------- Handler::alarmEvent()
55 //------------------------------------------------------------------------------
56 const anna::oam::alarm_data_t *anna::oam::Handler::alarmEvent(const anna::oam::Module *module, const char *textPreffix, const char *textSuffix, char textSeparator, bool activation, const int & type, va_list argList) const throw() {
57   // OAM alarm
58   const alarm_data_t *result = module->alarm(type);
59
60   // #define LOGINFORMATION(a) if (anna::Logger::isActive (anna::Logger::Information) == true) {a;}
61   // Problemas con las comas dentro de la macro:
62   if(anna::Logger::isActive(anna::Logger::Information)) {
63     // Base text:
64     std::string base = result ? result->Description : module->getDefaultInternalAlarmDescription(type);
65     // replace possible __s__ with <%s>, and __d__ with <%d>
66     base = anna::functions::replace(base, "__s__", "<%s>");
67     base = anna::functions::replace(base, "__d__", "<%d>");
68     // Final text:
69     std::string alarm;
70
71     if(textPreffix != NULL) {
72       alarm = textPreffix;
73       alarm += textSeparator;
74       alarm += base;
75     } else
76       alarm = base;
77
78     if(textSuffix != NULL) {
79       alarm += textSeparator;
80       alarm += textSuffix;
81     }
82
83     static char cad_aux[1024];
84     vsprintf(cad_aux, alarm.c_str(), argList);
85     std::string trace = module->getClassName();
86     trace += anna::functions::asString(" | Alarm %s event: %s", (activation ? "activation" : "cancellation"), cad_aux);
87     trace += anna::functions::asString(" | Enum type on module: %d", type);
88
89     if(result) {
90       if(activation)
91         trace += anna::functions::asString(" | ActivationId: %d", result->ActivationId);
92       else
93         trace += anna::functions::asString(" | CancellationId: %d", result->CancellationId);
94
95       trace += anna::functions::asString(" | DynamicVariablesCSL: %s", result->DynamicVariablesCSL.c_str());
96       trace += anna::functions::asString(" | ExternalId: %d", result->ExternalId);
97     }
98
99     anna::Logger::information(trace, ANNA_FILE_LOCATION);
100   }
101
102   return result;
103 }
104
105
106 //------------------------------------------------------------------------------
107 //------------------------------------------------------ Handler::counterEvent()
108 //------------------------------------------------------------------------------
109
110 const anna::oam::counter_data_t *anna::oam::Handler::counterEvent(const anna::oam::Module *module, const int & type, const int & amount) const throw() {
111   // OAM counter
112   const counter_data_t *result = module->counter(type);
113   // Base text:
114   std::string base = result ? result->Description : module->getDefaultInternalCounterDescription(type);
115
116   // Count if registered
117   if(result)
118     anna::oam::CounterManager::instantiate().count(module->getActiveCounterScope()->getId(), result->Offset, amount);
119
120   LOGDEBUG
121   (
122     std::string trace = module->getClassName();
123     trace += anna::functions::asString(" | Counter event: %s", base.c_str());
124     trace += anna::functions::asString(" | Enum type on module: %d", type);
125     trace += anna::functions::asString(" | Amount increased: %d", amount);
126
127     if(result) trace += anna::functions::asString(" | Accumulated amount: %llu", module->getActiveCounterScope()->getAccValue(result->Offset));
128     anna::Logger::debug(trace, ANNA_FILE_LOCATION);
129   );
130
131   return result;
132 }
133
134