First commit
[anna.git] / source / diameter / codec / tme / Avp.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/diameter/codec/tme/Avp.hpp>
39 #include <anna/diameter/codec/tme/Engine.hpp>
40
41 #include <anna/diameter/helpers/defines.hpp>
42 #include <anna/diameter/stack/Format.hpp>
43 #include <anna/core/functions.hpp>
44
45 #include <anna/xml/xml.hpp>
46
47 using namespace anna;
48 using namespace anna::diameter::codec::tme;
49
50
51
52 //------------------------------------------------------------------------------
53 //------------------------------------------------------------------- Avp::Avp()
54 //------------------------------------------------------------------------------
55 Avp::Avp() {
56   initialize();
57 }
58
59
60 //------------------------------------------------------------------------------
61 //------------------------------------------------------------------- Avp::Avp()
62 //------------------------------------------------------------------------------
63 Avp::Avp(AvpId id) {
64   initialize();
65   setId(id);
66 }
67
68
69 //------------------------------------------------------------------------------
70 //------------------------------------------------------------------ Avp::~Avp()
71 //------------------------------------------------------------------------------
72 Avp::~Avp() {
73   clear();
74 }
75
76
77 //------------------------------------------------------------------------------
78 //------------------------------------------------------------- Avp::getEngine()
79 //------------------------------------------------------------------------------
80 anna::diameter::codec::Engine * Avp::getEngine() const throw(anna::RuntimeException) {
81   return a_engine ? a_engine : (a_engine = (anna::diameter::codec::Engine *)anna::functions::component <Engine> (ANNA_FILE_LOCATION));
82 }
83
84
85 //------------------------------------------------------------------------------
86 //---------------------------------------------------- Avp::initializeByFormat()
87 //------------------------------------------------------------------------------
88 void Avp::initializeByFormat() throw() {
89   a_ISDNNumber = NULL;
90   a_ISDNAddress = NULL;
91   a_Unsigned16 = NULL;
92 }
93
94
95 //------------------------------------------------------------------------------
96 //--------------------------------------------------------- Avp::clearByFormat()
97 //------------------------------------------------------------------------------
98 void Avp::clearByFormat() throw() {
99   delete a_ISDNNumber;
100   delete a_ISDNAddress;
101   delete a_Unsigned16;
102 }
103
104
105 //------------------------------------------------------------------------------
106 //---------------------------------------------------- Avp::allocationByFormat()
107 //------------------------------------------------------------------------------
108 void Avp::allocationByFormat(const stack::Format *stackFormat) throw() {
109   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber = new ISDNNumber();
110   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress = new ISDNAddress();
111   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16 = new Unsigned16();
112 }
113
114
115 //------------------------------------------------------------------------------
116 //----------------------------------------------------- Avp::getLengthByFormat()
117 //------------------------------------------------------------------------------
118 U24 Avp::getLengthByFormat(const stack::Format *stackFormat) const throw() {
119   U24 result = 0;
120
121   if(stackFormat->getName() == "ISDNNumber") result += a_ISDNNumber->getSize();
122   else if(stackFormat->getName() == "ISDNAddress") result += a_ISDNAddress->getSize();
123   else if(stackFormat->getName() == "Unsigned16") result += a_Unsigned16->getSize();
124
125   return result;
126 }
127
128
129 //------------------------------------------------------------------------------
130 //------------------------------------------------ Avp::decodeDataPartByFormat()
131 //------------------------------------------------------------------------------
132 void Avp::decodeDataPartByFormat(const char * buffer, int size, const stack::Format *stackFormat) throw(anna::RuntimeException) {
133   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->decode(buffer, size);
134   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->decode(buffer, size);
135   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->decode(buffer, size);
136 }
137
138
139 //------------------------------------------------------------------------------
140 //---------------------------------------------------------- Avp::codeByFormat()
141 //------------------------------------------------------------------------------
142 void Avp::codeByFormat(char* dataPart, const stack::Format *stackFormat) const throw(anna::RuntimeException) {
143   int dataBytes;
144
145   if(stackFormat->getName() == "ISDNNumber") a_ISDNNumber->code(dataPart, dataBytes);
146   else if(stackFormat->getName() == "ISDNAddress") a_ISDNAddress->code(dataPart, dataBytes);
147   else if(stackFormat->getName() == "Unsigned16") a_Unsigned16->code(dataPart, dataBytes);
148 }
149
150
151 //------------------------------------------------------------------------------
152 //---------------------------------------------------- Avp::getXMLdataByFormat()
153 //------------------------------------------------------------------------------
154 std::string Avp::getXMLdataByFormat(bool & isHex, const stack::Format *stackFormat) const throw() {
155   std::string result;
156
157   if(stackFormat->getName() == "ISDNNumber") {
158     isHex = true;
159     return a_ISDNNumber->asHexString();
160   } else if(stackFormat->getName() == "ISDNAddress") {
161     isHex = true;
162     return a_ISDNAddress->asHexString();
163   } else if(stackFormat->getName() == "Unsigned16") return a_Unsigned16->asPrintableString();
164
165   return result;
166 }
167
168
169 //------------------------------------------------------------------------------
170 //------------------------------------------------------- Avp::fromXMLByFormat()
171 //------------------------------------------------------------------------------
172 void Avp::fromXMLByFormat(const anna::xml::Attribute* data, const anna::xml::Attribute* hexData, const stack::Format *stackFormat) throw(anna::RuntimeException) {
173   if(stackFormat->getName() == "ISDNNumber") {
174     if(data) a_ISDNNumber->fromPrintableString(data->getValue().c_str());
175     else if(hexData) a_ISDNNumber->fromHexString(hexData->getValue());
176   } else if(stackFormat->getName() == "ISDNAddress") {
177     if(data) a_ISDNAddress->fromPrintableString(data->getValue().c_str());
178     else if(hexData) a_ISDNAddress->fromHexString(hexData->getValue());
179   } else if(stackFormat->getName() == "Unsigned16") {
180     if(data) a_Unsigned16->fromPrintableString(data->getValue().c_str());
181     else if(hexData) a_Unsigned16->fromHexString(hexData->getValue());
182   }
183 }
184