Updated license
[anna.git] / include / anna / diameter / stack / Format.hpp
1 // ANNA - Anna is Not Nothingness 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 #ifndef anna_diameter_stack_Format_hpp
38 #define anna_diameter_stack_Format_hpp
39
40 // Local
41 #include <anna/diameter/codec/Format.hpp>
42
43 #include <anna/config/defines.hpp>
44 #include <anna/core/RuntimeException.hpp>
45
46 // STL
47 #include <string>
48
49
50
51 namespace anna {
52 namespace xml {
53 class Node;
54 }
55 }
56
57
58
59 namespace anna {
60
61 namespace diameter {
62
63 namespace stack {
64
65 class Dictionary;
66
67 //------------------------------------------------------------------------------
68 //----------------------------------------------------------------- class Format
69 //------------------------------------------------------------------------------
70 /**
71 * Format data container
72 */
73 class Format {
74   const Dictionary *a_dictionary;
75   std::string a_name;
76   std::string a_parentName;
77
78   void _initialize(const Dictionary *d) throw() {
79     a_dictionary = d;
80     a_name = "";
81     a_parentName = "";
82   }
83
84 public:
85
86   Format(const Dictionary *d = NULL) { _initialize(d); }
87   ~Format() {};
88
89
90   // get
91
92   /**
93   * Gets the format type name
94   *
95   * @return Format type name
96   */
97   const std::string & getName(void) const throw() { return a_name; }
98
99   /**
100   * Gets the format parent type name
101   *
102   * @return Format parent type name, empty string for diameter basic format types (and reserved like 'Any, Unknown')
103   */
104   const std::string & getParentName(void) const throw() { return a_parentName; }
105
106   // helpers
107
108   /**
109   * Gets the diameter parent type reference
110   *
111   * @return Diameter parent type reference, NULL for diameter basic format types (and reserved like 'Any, Unknown')
112   */
113   const Format * getParent(void) const throw();
114
115   /**
116   * The diameter format is a derived type
117   *
118   * @return The format is derived
119   */
120   bool isDerived(void) const throw() { return (a_parentName != ""); }
121
122   /**
123   * The diameter format is a basic type:
124   *  (OctetString, Integer32, Integer64, Unsigned32, Unsigned64, Float32, Float64, Grouped)
125   *
126   * @return The format is basic
127   */
128   bool isBasic(void) const throw() { return (!isDerived() && !isReserved()); }
129
130   /**
131   * Gets the diameter basic format type from which a derived type inherit, or basic type itself for non-derived
132   *
133   * @return Diameter basic format type
134   */
135   codec::Format::_v getBasicType(void) const throw(anna::RuntimeException);
136
137   /**
138   * The diameter format belongs to RFC3588 diameter format family:
139   *  (OctetString, Integer32, Integer64, Unsigned32, Unsigned64, Float32, Float64, Grouped, Address, Time, UTF8String, DiameterIdentity, DiameterURI, Enumerated, IPFilterRule, QoSFilterRule)
140   *
141   * @return The format belongs to RFC3588
142   */
143   bool isRFC3588(void) const throw() { return (codec::Format::isRFC3588(a_name)); }
144
145   // Reserved
146   /** @return The format is 'Any' (generic AVP) */
147   bool isAny() const throw() { return (a_name == codec::Format::asText(codec::Format::Any)); }
148 //   /** @return The format is 'Unknown' */
149 //   bool isUnknown() const throw() { return (a_name == codec::Format::asText(codec::Format::Unknown)); }
150
151   // RFC 3588
152   /** @return The format is 'OctetString' */
153   bool isOctetString() const throw() { return (a_name == codec::Format::asText(codec::Format::OctetString)); }
154   /** @return The format is 'Integer32' */
155   bool isInteger32() const throw() { return (a_name == codec::Format::asText(codec::Format::Integer32)); }
156   /** @return The format is 'Integer64' */
157   bool isInteger64() const throw() { return (a_name == codec::Format::asText(codec::Format::Integer64)); }
158   /** @return The format is 'Unsigned32' */
159   bool isUnsigned32() const throw() { return (a_name == codec::Format::asText(codec::Format::Unsigned32)); }
160   /** @return The format is 'Unsigned64' */
161   bool isUnsigned64() const throw() { return (a_name == codec::Format::asText(codec::Format::Unsigned64)); }
162   /** @return The format is 'Float32' */
163   bool isFloat32() const throw() { return (a_name == codec::Format::asText(codec::Format::Float32)); }
164   /** @return The format is 'Float64' */
165   bool isFloat64() const throw() { return (a_name == codec::Format::asText(codec::Format::Float64)); }
166   /** @return The format is 'Grouped' */
167   bool isGrouped() const throw() { return (a_name == codec::Format::asText(codec::Format::Grouped)); }
168   /** @return The format is 'Address' */
169   bool isAddress() const throw() { return (a_name == codec::Format::asText(codec::Format::Address)); }
170   /** @return The format is 'Time' */
171   bool isTime() const throw() { return (a_name == codec::Format::asText(codec::Format::Time)); }
172   /** @return The format is 'UTF8String' */
173   bool isUTF8String() const throw() { return (a_name == codec::Format::asText(codec::Format::UTF8String)); }
174   /** @return The format is 'DiameterIdentity' */
175   bool isDiameterIdentity() const throw() { return (a_name == codec::Format::asText(codec::Format::DiameterIdentity)); }
176   /** @return The format is 'DiameterURI' */
177   bool isDiameterURI() const throw() { return (a_name == codec::Format::asText(codec::Format::DiameterURI)); }
178   /** @return The format is 'Enumerated' */
179   bool isEnumerated() const throw() { return (a_name == codec::Format::asText(codec::Format::Enumerated)); }
180   /** @return The format is 'IPFilterRule' */
181   bool isIPFilterRule() const throw() { return (a_name == codec::Format::asText(codec::Format::IPFilterRule)); }
182   /** @return The format is 'QoSFilterRule' */
183   bool isQoSFilterRule() const throw() { return (a_name == codec::Format::asText(codec::Format::QoSFilterRule)); }
184
185   /**
186   * The diameter format is application-specific or a format type name not registered on dictionary
187   *
188   * @return The format is application-specific
189   */
190   bool isApplicationSpecific(void) const throw() { return (!isRFC3588() && !isReserved()); }
191
192   /**
193   * The diameter format is reserved ('Any' for generic AVP, 'Unknown' for not registered avps)
194   *
195   * @return The format is reserved
196   */
197   bool isReserved(void) const throw() { return (codec::Format::isReserved(a_name)); }
198
199   /**
200   * Class string representation
201   *
202   * @return String with class content
203   */
204   std::string asString(void) const throw();
205
206   /**
207   * Class xml representation
208   *
209   * @return XML document with relevant information for this instance.
210   */
211   anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
212
213   // operators
214
215   /**
216   * @return Equality between two class instances
217   */
218   friend bool operator == (const Format & f1, const Format & f2) { return ((f1.getName() == f2.getName())); }
219
220   /**
221   * @return Difference between two class instances
222   */
223   friend bool operator != (const Format & f1, const Format & f2) { return !(f1 == f2); }
224
225   // set
226
227   /**
228   * Initializes the class content
229   */
230   void initialize(const Dictionary *d = NULL) throw() { _initialize(d); }
231
232   /**
233   * Sets Avp format type name
234   *
235   * @param type Avp format type name
236   */
237   void setName(const char * name) throw(anna::RuntimeException) {
238     if(name == NULL) throw anna::RuntimeException("Null Format-name not allowed", ANNA_FILE_LOCATION);
239
240     a_name = name;
241
242     if(a_name == "") throw anna::RuntimeException("Empty Format-name not allowed", ANNA_FILE_LOCATION);
243   }
244
245   /**
246   * Sets RFC3588 Avp format type name
247   *
248   * @param rfc3588Format RFC3588 format type
249   */
250   void setRFC3588(codec::Format::_v rfc3588Format) throw() {
251     setName(codec::Format::asText(rfc3588Format));
252   }
253
254   /**
255   * Sets Diameter-basic avp format parent
256   *
257   * @param parent Diameter-basic avp format parent
258   */
259   void setParentName(const std::string & parentName) throw(anna::RuntimeException);
260 };
261
262
263 }
264 }
265 }
266
267
268 #endif
269
270