ANNA Suite  2020b
Multipurpose development suite for Telco applications
Avp.hpp
Go to the documentation of this file.
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
17 
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 //------------------------------------------------------------------------------
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
77  may,
79  mustnot
80  };
81 
83 
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
const char * getEnums(void) const
Definition: Avp.hpp:151
const char * simplifyLiteral(void)
int avprule_size() const
Definition: Avp.hpp:160
const FlagRule::_v & getMbit(void) const
Definition: Avp.hpp:147
void setVbit(const FlagRule::_v &v)
Definition: Avp.hpp:202
void setVendorName(const std::string &vn)
Definition: Avp.hpp:200
bool allowEnum(int value) const
Definition: Avp.hpp:168
#define anna_declare_enum(name)
Definition: define.autoenum.hpp:48
void setFormatName(const std::string &fn)
Definition: Avp.hpp:201
Definition: Node.hpp:56
void setLiteral(const char *l)
Definition: MultiRangeExpression.hpp:76
void addLiteral(const char *l)
Definition: MultiRangeExpression.hpp:87
void setPbit(const FlagRule::_v &p)
Definition: Avp.hpp:204
std::map< int, AvpRule > avprule_container
Definition: Avp.hpp:44
std::pair< S32, S32 > AvpId
Definition: defines.hpp:31
void addEnums(const char *e)
Definition: Avp.hpp:208
void setMayEncrypt(bool me)
Definition: Avp.hpp:205
void setCode(const S32 &c) noexcept(false)
Definition: Avp.hpp:181
const char * getAlias(const std::string data) const
Definition: Avp.hpp:152
void setEnums(const char *e)
Definition: Avp.hpp:207
void setName(const std::string &n) noexcept(false)
Definition: Avp.hpp:193
const std::string & getName(void) const
Definition: Avp.hpp:143
int32_t S32
Definition: defines.hpp:78
std::map< std::string, std::string, lessLabel > label_container
Definition: Avp.hpp:67
Definition: MultiRangeExpression.hpp:23
Avp(const Dictionary *d=NULL)
Definition: Avp.hpp:137
avprule_container::iterator avprule_iterator
Definition: Avp.hpp:45
label_container::const_iterator const_label_iterator
Definition: Avp.hpp:69
const char * getLiteral(void) const
Definition: MultiRangeExpression.hpp:43
void setVendorId(const S32 &v) noexcept(false)
Definition: Avp.hpp:187
xml::Node Node
Definition: Node.hpp:21
Definition: Dictionary.hpp:50
bool mayEncrypt(void) const
Definition: Avp.hpp:149
bool contain(const unsigned int &value) const
Definition: MultiRangeExpression.hpp:67
avprule_container::const_iterator const_avprule_iterator
Definition: Avp.hpp:46
const FlagRule::_v & getPbit(void) const
Definition: Avp.hpp:148
bool operator()(const std::string &d1, const std::string &d2) const
Definition: Avp.hpp:63
const_avprule_iterator avprule_end() const
Definition: Avp.hpp:159
Definition: app.hpp:12
Definition: Avp.hpp:58
void initialize(const Dictionary *d=NULL)
Definition: Avp.hpp:199
label_container::iterator label_iterator
Definition: Avp.hpp:68
const_label_iterator label_end() const
Definition: Avp.hpp:163
const AvpId & getId(void) const
Definition: Avp.hpp:142
const FlagRule::_v & getVbit(void) const
Definition: Avp.hpp:146
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23
Definition: Format.hpp:45
const std::string & getFormatName(void) const
Definition: Avp.hpp:144
const_avprule_iterator avprule_begin() const
Definition: Avp.hpp:158
Definition: RuntimeException.hpp:23
int label_size() const
Definition: Avp.hpp:164
bool hasAliases(void) const
Definition: Avp.hpp:169
const_label_iterator label_begin() const
Definition: Avp.hpp:162
static const char * asText(const FlagRule::_v v) noexcept(false)
Definition: Avp.hpp:89
void setMbit(const FlagRule::_v &m)
Definition: Avp.hpp:203
Definition: AvpRule.hpp:51