Fix xml representations
[anna.git] / source / diameter / codec / EngineImpl.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 // Local
10 #include <anna/diameter/codec/EngineImpl.hpp>
11 #include <anna/diameter/codec/Message.hpp>
12 #include <anna/diameter/codec/Avp.hpp>
13 #include <anna/diameter/stack/Engine.hpp>
14 #include <anna/diameter/stack/Dictionary.hpp>
15 #include <anna/diameter/stack/Command.hpp>
16 #include <anna/diameter/internal/sccs.hpp>
17 #include <anna/diameter/defines.hpp>
18
19 #include <anna/xml/xml.hpp>
20 #include <anna/core/tracing/Logger.hpp>
21 #include <anna/core/tracing/TraceMethod.hpp>
22 #include <anna/core/functions.hpp>
23 #include <anna/core/mt/Guard.hpp>
24
25
26 using namespace anna::diameter::codec;
27
28
29 //------------------------------------------------------------------------------
30 //----------------------------------------------------- EngineImpl::EngineImpl()
31 //------------------------------------------------------------------------------
32 EngineImpl::EngineImpl(const char* className, const stack::Dictionary * dictionary) :
33   anna::Component(className),
34   a_dictionary(dictionary),
35   a_validationDepth(ValidationDepth::FirstError),
36   a_validationMode(ValidationMode::AfterDecoding),
37   a_singleFailedAVP(true),
38   a_ignoreFlags(false),
39   a_fixMode(FixMode::BeforeEncoding) {
40   anna::diameter::sccs::activate();
41   anna::xml::functions::initialize();
42 }
43
44
45 //------------------------------------------------------------------------------
46 //------------------------------------------------------ EngineImpl::createAvp()
47 //------------------------------------------------------------------------------
48 Avp* EngineImpl::createAvp(const AvpId *id) throw(anna::RuntimeException) {
49   Avp *result(NULL);
50   anna::Guard guard(this, "diameter::codec::EngineImpl::createAvp");
51
52   if((result = allocateAvp()) == NULL)
53     throw anna::RuntimeException("diameter::codec::EngineImpl::allocateAvp returns NULL", ANNA_FILE_LOCATION);
54
55   // Sets engine
56   result->setEngine((Engine*)this);
57
58   //result->clear(); better clear this at releaseAvp(), see class-help implementation example
59   if(id) result->setId(*id);
60
61   return result;
62 }
63
64
65 //------------------------------------------------------------------------------
66 //-------------------------------------------------- EngineImpl::createMessage()
67 //------------------------------------------------------------------------------
68 Message* EngineImpl::createMessage(const CommandId *id) throw(anna::RuntimeException) {
69   Message *result(NULL);
70   anna::Guard guard(this, "diameter::codec::EngineImpl::createMessage");
71
72   if((result = allocateMessage()) == NULL)
73     throw anna::RuntimeException("diameter::codec::EngineImpl::allocateMessage returns NULL", ANNA_FILE_LOCATION);
74
75   // Sets engine
76   result->setEngine((Engine*)this);
77
78   //result->clear(); better clear this at releaseMessage(), see class-help implementation example
79   if(id) result->setId(*id);
80
81   return result;
82 }
83
84
85 //------------------------------------------------------------------------------
86 //-------------------------------------------------- EngineImpl::createMessage()
87 //------------------------------------------------------------------------------
88 Message *EngineImpl::createMessage(const std::string & xmlPathFile_or_string, bool pathfile_or_string) throw(anna::RuntimeException) {
89   Message *result = createMessage();
90   if (pathfile_or_string)
91     result->loadXMLFile(xmlPathFile_or_string);
92   else
93     result->loadXMLString(xmlPathFile_or_string);
94
95   return result;
96 }
97
98
99
100 std::string EngineImpl::asString(void) const throw() {
101   std::string result = anna::Component::asString();
102   result += "\nValidationDepth: ";
103   result += asText(a_validationDepth);
104   result += "\nValidationMode: ";
105   result += asText(a_validationMode);
106   result += "\nSingle Failed-AVP: ";
107   result += a_singleFailedAVP ? "yes" : "no";
108   result += "\nIgnore flags: ";
109   result += a_ignoreFlags ? "yes" : "no";
110   result += "\nFixMode: ";
111   result += asText(a_fixMode);
112   result += "\nActivated Dictionary: ";
113   result += a_dictionary ? (a_dictionary->getName()) : "[null]";
114   return result;
115 }
116
117 //------------------------------------------------------------------------------
118 //---------------------------------------------------------- EngineImpl::asXML()
119 //------------------------------------------------------------------------------
120 anna::xml::Node* EngineImpl::asXML(anna::xml::Node* parent) const
121 throw() {
122   parent = anna::Component::asXML(parent);
123   anna::xml::Node* result = parent->createChild("diameter.codec.EngineImpl");
124   result->createAttribute("ValidationDepth", asText(a_validationDepth));
125   result->createAttribute("ValidationMode", asText(a_validationMode));
126   result->createAttribute("SingleFailedAVP", a_singleFailedAVP ? "yes" : "no");
127   result->createAttribute("IgnoreFlags", a_ignoreFlags ? "yes" : "no");
128   result->createAttribute("FixMode", asText(a_fixMode));
129   result->createChild("EngineImpl.ActivatedDictionary");
130
131   if(a_dictionary) a_dictionary->asXML(result);
132
133   return result;
134 }
135
136 //------------------------------------------------------------------------------
137 //--------------------------------------------------------- EngineImpl::asText()
138 //------------------------------------------------------------------------------
139 const char* EngineImpl::asText(const ValidationDepth::_v vd)
140 throw() {
141   static const char* text [] = { "Complete", "FirstError" };
142   return text [vd];
143 }
144
145 //------------------------------------------------------------------------------
146 //--------------------------------------------------------- EngineImpl::asText()
147 //------------------------------------------------------------------------------
148 const char* EngineImpl::asText(const ValidationMode::_v vm)
149 throw() {
150   static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" };
151   return text [vm];
152 }
153
154 //------------------------------------------------------------------------------
155 //--------------------------------------------------------- EngineImpl::asText()
156 //------------------------------------------------------------------------------
157 const char* EngineImpl::asText(const FixMode::_v fm)
158 throw() {
159   static const char* text [] = { "BeforeEncoding", "AfterDecoding", "Always", "Never" };
160   return text [fm];
161 }
162
163 //------------------------------------------------------------------------------
164 //---------------------------------------------- EngineImpl::validationAnomaly()
165 //------------------------------------------------------------------------------
166 void EngineImpl::validationAnomaly(const std::string & description) const throw(anna::RuntimeException) {
167   std::string msg = "Validation error: ";
168
169   if(a_validationDepth == ValidationDepth::FirstError)
170     throw anna::RuntimeException(msg + description, ANNA_FILE_LOCATION);
171
172   LOGWARNING(anna::Logger::warning(msg + description, ANNA_FILE_LOCATION));
173 }
174
175
176 //------------------------------------------------------------------------------
177 //--------------------------------------------------- EngineImpl::avpIdForName()
178 //------------------------------------------------------------------------------
179 anna::diameter::AvpId EngineImpl::avpIdForName(const char * name) throw(anna::RuntimeException) {
180   if(!name)
181     throw anna::RuntimeException("Provided NULL Avp Logical Name", ANNA_FILE_LOCATION);
182
183   if(!a_dictionary) {
184     std::string msg = "Cannot find identifier '"; msg += name;
185     msg += "' for the avp: no dictionary available";
186     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
187   }
188
189   const stack::Avp * stackAvp = a_dictionary->getAvp(name);
190
191   if(!stackAvp) {
192     std::string msg = "Cannot find identifier '"; msg += name;
193     msg += "' for the avp at the available dictionary";
194     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
195   }
196
197   return (stackAvp->getId());
198 }
199
200
201 //------------------------------------------------------------------------------
202 //----------------------------------------------- EngineImpl::commandIdForName()
203 //------------------------------------------------------------------------------
204 anna::diameter::CommandId EngineImpl::commandIdForName(const char * name) throw(anna::RuntimeException) {
205   if(!name)
206     throw anna::RuntimeException("Provided NULL Command Logical Name", ANNA_FILE_LOCATION);
207
208   if(!a_dictionary) {
209     std::string msg = "Cannot find identifier '"; msg += name;
210     msg += "' for the command: no dictionary available";
211     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
212   }
213
214   const stack::Command * stackCommand = a_dictionary->getCommand(name);
215
216   if(!stackCommand) {
217     std::string msg = "Cannot find identifier '"; msg += name;
218     msg += "' for the command at the available dictionary";
219     throw anna::RuntimeException(msg, ANNA_FILE_LOCATION);
220   }
221
222   return (stackCommand->getId());
223 }
224