Remove dynamic exceptions
[anna.git] / include / anna / diameter / stack / Avp.hpp
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 #ifndef anna_diameter_stack_Avp_hpp
10 #define anna_diameter_stack_Avp_hpp
11
12
13 // Local
14 #include <anna/diameter/defines.hpp>
15 #include <anna/diameter/stack/AvpRule.hpp>
16 #include <anna/core/util/MultiRangeExpression.hpp>
17
18 #include <anna/core/RuntimeException.hpp>
19 #include <anna/config/defines.hpp>
20
21 // STL
22 #include <string>
23 #include <map>
24
25
26
27 namespace anna {
28 namespace xml {
29 class Node;
30 }
31 }
32
33
34
35
36 namespace anna {
37
38 namespace diameter {
39
40 namespace stack {
41
42
43 //typedef std::map<AvpId, AvpRule> avprule_container;
44 typedef std::map < int /*position*/, AvpRule > avprule_container;
45 typedef avprule_container::iterator avprule_iterator;
46 typedef avprule_container::const_iterator const_avprule_iterator;
47
48
49 class Format;
50 class Dictionary;
51
52 //------------------------------------------------------------------------------
53 //-------------------------------------------------------------------- class Avp
54 //------------------------------------------------------------------------------
55 /**
56 * Avp Reference information
57 */
58 class Avp {
59
60 public:
61
62   struct lessLabel {
63     bool operator()(const std::string &d1, const std::string &d2) const {
64       return (atoi(d1.c_str()) < atoi(d2.c_str()));
65     }
66   };
67   typedef std::map < std::string /* data */, std::string /* alias */, lessLabel > label_container;
68   typedef label_container::iterator label_iterator;
69   typedef label_container::const_iterator const_label_iterator;
70
71
72
73   struct FlagRule {
74     enum _v {
75       None = -1, // Initialized
76       must,
77       may,
78       shouldnot,
79       mustnot
80     };
81
82     anna_declare_enum(FlagRule);
83
84     /**
85     * FlagRule description
86     * @param v FlagRule type
87     * @return FlagRule description
88     */
89     static const char* asText(const FlagRule::_v v) noexcept(false) {
90       return asCString(v);
91     }
92   };
93
94
95 private:
96
97   const Dictionary *a_dictionary;
98   AvpId a_id;
99   std::string a_name;
100   std::string a_formatName;
101   std::string a_vendorName;
102
103   // Flag Rules:
104   FlagRule::_v a_vBit, a_mBit, a_pBit;
105   bool a_mayEncrypt;
106
107   MultiRangeExpression a_enums;
108   label_container a_labels;
109
110   // Grouped:
111   avprule_container a_avprules;
112   bool a_allowFixedRule;
113   int a_avprulePosition;
114
115   void _initializeEnumsLabelsAndRules() {
116     a_enums.setLiteral("");
117     a_labels.clear();
118     a_avprules.clear();
119     a_allowFixedRule = true;
120     a_avprulePosition = 0;
121   }
122
123   void _initialize(const Dictionary *d) {
124     a_dictionary = d;
125     a_name = "";
126     a_formatName = "";
127     a_vendorName = "";
128     a_vBit = FlagRule::mustnot;
129     a_mBit = FlagRule::must;
130     a_pBit = FlagRule::may;
131     a_mayEncrypt = true;
132     _initializeEnumsLabelsAndRules();
133   }
134
135 public:
136
137   Avp(const Dictionary *d = NULL) { _initialize(d); }
138   ~Avp();
139
140
141   // get
142   const AvpId & getId(void) const { return a_id; }
143   const std::string & getName(void) const { return a_name; }
144   const std::string & getFormatName(void) const { return a_formatName; }
145
146   const FlagRule::_v & getVbit(void) const { return a_vBit; }
147   const FlagRule::_v & getMbit(void) const { return a_mBit; }
148   const FlagRule::_v & getPbit(void) const { return a_pBit; }
149   bool mayEncrypt(void) const { return a_mayEncrypt; }
150
151   const char * getEnums(void) const { return a_enums.getLiteral(); }
152   const char * getAlias(const std::string data) const {
153     std::map<std::string, std::string>::const_iterator it = a_labels.find(data);
154     return ((it != a_labels.end()) ? ((*it).second.c_str()) : NULL);
155   }
156
157   // containers
158   const_avprule_iterator avprule_begin() const { return a_avprules.begin(); }
159   const_avprule_iterator avprule_end() const { return a_avprules.end(); }
160   int avprule_size() const { return a_avprules.size(); }
161
162   const_label_iterator label_begin() const { return a_labels.begin(); }
163   const_label_iterator label_end() const { return a_labels.end(); }
164   int label_size() const { return a_labels.size(); }
165
166
167   // helpers
168   bool allowEnum(int value) const { return a_enums.contain(value); }
169   bool hasAliases(void) const { return (a_labels.size() != 0); }
170   bool isChild(const AvpId & avp) const ;
171   std::string getFlagsDescription(void) const ;
172   std::string getFlagRulesDescription(void) const ;
173   const Format * getFormat() const ;
174
175   std::string asString(void) const ;
176   anna::xml::Node* asXML(anna::xml::Node* parent) const ;
177
178   // operators
179
180   // set
181   void setCode(const S32 & c) noexcept(false) {
182     if(c < 0) throw anna::RuntimeException("Negative avp-code not allowed", ANNA_FILE_LOCATION);
183
184     a_id.first = c;
185   }
186
187   void setVendorId(const S32 & v) noexcept(false) {
188     if(v < 0) throw anna::RuntimeException("Negative vendor-id not allowed", ANNA_FILE_LOCATION);
189
190     a_id.second = v;
191   }
192
193   void setName(const std::string & n) noexcept(false) {
194     if(n == "") throw anna::RuntimeException("Empty avp-name string not allowed", ANNA_FILE_LOCATION);
195
196     a_name = n;
197   }
198
199   void initialize(const Dictionary *d = NULL) { _initialize(d); }
200   void setVendorName(const std::string & vn) { a_vendorName = vn; }
201   void setFormatName(const std::string & fn) { a_formatName = fn; }
202   void setVbit(const FlagRule::_v &v) { a_vBit = v; }
203   void setMbit(const FlagRule::_v &m) { a_mBit = m; }
204   void setPbit(const FlagRule::_v &p) { a_pBit = p; } // deprecated flag ...
205   void setMayEncrypt(bool me) { a_mayEncrypt = me; }
206
207   void setEnums(const char * e) { a_enums.setLiteral(e); }
208   void addEnums(const char * e) { a_enums.addLiteral(e); a_enums.simplifyLiteral(); }
209
210   // After format configuration:
211   void addLabel(const std::string & data,  const std::string & alias) noexcept(false);
212   void addAvpRule(const AvpRule & avpRule) noexcept(false);
213 };
214
215
216 }
217 }
218 }
219
220
221 #endif