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_diameter_stack_Avp_hpp
38 #define anna_diameter_stack_Avp_hpp
42 #include <anna/diameter/defines.hpp>
43 #include <anna/diameter/stack/AvpRule.hpp>
44 #include <anna/core/util/MultiRangeExpression.hpp>
46 #include <anna/core/RuntimeException.hpp>
47 #include <anna/config/defines.hpp>
71 //typedef std::map<AvpId, AvpRule> avprule_container;
72 typedef std::map < int /*position*/, AvpRule > avprule_container;
73 typedef avprule_container::iterator avprule_iterator;
74 typedef avprule_container::const_iterator const_avprule_iterator;
80 //------------------------------------------------------------------------------
81 //-------------------------------------------------------------------- class Avp
82 //------------------------------------------------------------------------------
84 * Avp Reference information
91 bool operator()(const std::string &d1, const std::string &d2) const {
92 return (atoi(d1.c_str()) < atoi(d2.c_str()));
95 typedef std::map < std::string /* data */, std::string /* alias */, lessLabel > label_container;
96 typedef label_container::iterator label_iterator;
97 typedef label_container::const_iterator const_label_iterator;
103 None = -1, // Initialized
110 anna_declare_enum(FlagRule);
113 * FlagRule description
114 * @param v FlagRule type
115 * @return FlagRule description
117 static const char* asText(const FlagRule::_v v) throw(anna::RuntimeException) {
125 const Dictionary *a_dictionary;
128 std::string a_formatName;
129 std::string a_vendorName;
132 FlagRule::_v a_vBit, a_mBit, a_pBit;
135 MultiRangeExpression a_enums;
136 label_container a_labels;
139 avprule_container a_avprules;
140 bool a_allowFixedRule;
141 int a_avprulePosition;
143 void _initializeEnumsLabelsAndRules() throw() {
144 a_enums.setLiteral("");
147 a_allowFixedRule = true;
148 a_avprulePosition = 0;
151 void _initialize(const Dictionary *d) throw() {
156 a_vBit = FlagRule::mustnot;
157 a_mBit = FlagRule::must;
158 a_pBit = FlagRule::may;
160 _initializeEnumsLabelsAndRules();
165 Avp(const Dictionary *d = NULL) { _initialize(d); }
170 const AvpId & getId(void) const throw() { return a_id; }
171 const std::string & getName(void) const throw() { return a_name; }
172 const std::string & getFormatName(void) const throw() { return a_formatName; }
174 const FlagRule::_v & getVbit(void) const throw() { return a_vBit; }
175 const FlagRule::_v & getMbit(void) const throw() { return a_mBit; }
176 const FlagRule::_v & getPbit(void) const throw() { return a_pBit; }
177 bool mayEncrypt(void) const throw() { return a_mayEncrypt; }
179 const char * getEnums(void) const throw() { return a_enums.getLiteral(); }
180 const char * getAlias(const std::string data) const throw() {
181 std::map<std::string, std::string>::const_iterator it = a_labels.find(data);
182 return ((it != a_labels.end()) ? ((*it).second.c_str()) : NULL);
186 const_avprule_iterator avprule_begin() const throw() { return a_avprules.begin(); }
187 const_avprule_iterator avprule_end() const throw() { return a_avprules.end(); }
188 int avprule_size() const throw() { return a_avprules.size(); }
190 const_label_iterator label_begin() const throw() { return a_labels.begin(); }
191 const_label_iterator label_end() const throw() { return a_labels.end(); }
192 int label_size() const throw() { return a_labels.size(); }
196 bool allowEnum(int value) const throw() { return a_enums.contain(value); }
197 bool hasAliases(void) const throw() { return (a_labels.size() != 0); }
198 bool isChild(const AvpId & avp) const throw();
199 std::string getFlagsDescription(void) const throw();
200 std::string getFlagRulesDescription(void) const throw();
201 const Format * getFormat() const throw();
203 std::string asString(void) const throw();
204 anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
209 void setCode(const S32 & c) throw(anna::RuntimeException) {
210 if(c < 0) throw anna::RuntimeException("Negative avp-code not allowed", ANNA_FILE_LOCATION);
215 void setVendorId(const S32 & v) throw(anna::RuntimeException) {
216 if(v < 0) throw anna::RuntimeException("Negative vendor-id not allowed", ANNA_FILE_LOCATION);
221 void setName(const std::string & n) throw(anna::RuntimeException) {
222 if(n == "") throw anna::RuntimeException("Empty avp-name string not allowed", ANNA_FILE_LOCATION);
227 void initialize(const Dictionary *d = NULL) throw() { _initialize(d); }
228 void setVendorName(const std::string & vn) throw() { a_vendorName = vn; }
229 void setFormatName(const std::string & fn) throw() { a_formatName = fn; }
230 void setVbit(const FlagRule::_v &v) throw() { a_vBit = v; }
231 void setMbit(const FlagRule::_v &m) throw() { a_mBit = m; }
232 void setPbit(const FlagRule::_v &p) throw() { a_pBit = p; }
233 void setMayEncrypt(bool me) throw() { a_mayEncrypt = me; }
235 void setEnums(const char * e) throw() { a_enums.setLiteral(e); }
237 // After format configuration:
238 void addLabel(const std::string & data, const std::string & alias) throw(anna::RuntimeException);
239 void addAvpRule(const AvpRule & avpRule) throw(anna::RuntimeException);