Remove dynamic exceptions
[anna.git] / include / anna / diameter / stack / Dictionary.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_Dictionary_hpp
10 #define anna_diameter_stack_Dictionary_hpp
11
12
13 // Local
14 #include <anna/diameter/defines.hpp>
15 #include <anna/diameter/stack/Format.hpp>
16 #include <anna/diameter/stack/Vendor.hpp>
17 #include <anna/diameter/stack/Avp.hpp>
18 #include <anna/diameter/stack/Command.hpp>
19
20 #include <anna/core/RuntimeException.hpp>
21
22 // STL
23 #include <string>
24 #include <map>
25
26 namespace anna {
27 namespace xml {
28 class Node;
29 class DTDMemory;
30 }
31 }
32
33
34
35
36 namespace anna {
37
38 namespace diameter {
39
40 namespace stack {
41
42
43
44 //------------------------------------------------------------------------------
45 //------------------------------------------------------------- class Dictionary
46 //------------------------------------------------------------------------------
47 /**
48 * Dictionary information
49 */
50 class Dictionary {
51
52 public:
53
54   typedef std::map<std::string, Format> format_container;
55   typedef format_container::const_iterator const_format_iterator;
56
57   typedef std::map<S32, Vendor> vendor_container;
58   typedef vendor_container::const_iterator const_vendor_iterator;
59
60   struct lessAvp { // order by vendor id
61     bool operator()(AvpId id1, AvpId id2) const {
62       if(id1.second == id2.second) return (id1.first < id2.first);
63
64       return (id1.second < id2.second);
65     }
66   };
67   typedef std::map<AvpId, Avp, lessAvp> avp_container;
68   typedef avp_container::const_iterator const_avp_iterator;
69
70
71   struct lessCommand {
72     bool operator()(CommandId id1, CommandId id2) const {
73       if(id1.first == id2.first) {
74         if(id1.second != id2.second) return (id1.second);  // request -> answer
75
76         return (false);
77       }
78
79       return (id1.first < id2.first);
80     }
81   };
82   typedef std::map<CommandId, Command, lessCommand> command_container;
83   typedef command_container::const_iterator const_command_iterator;
84
85 protected:
86   std::string a_name;
87
88 private:
89
90   bool a_allowUpdates;
91   format_container a_formats;
92   vendor_container a_vendors;
93   avp_container a_avps;
94   command_container a_commands;
95
96   // Auxiliary
97   const anna::xml::DTDMemory * a_dtd;
98
99   // Name identifiers:
100   typedef std::map<std::string, const Vendor *> vendorNames_container;
101   typedef vendorNames_container::const_iterator const_vendorNames_iterator;
102
103   typedef std::map<std::string, const Avp *> avpNames_container;
104   typedef avpNames_container::const_iterator const_avpNames_iterator;
105
106   typedef std::map<std::string, const Command *> commandNames_container;
107   typedef commandNames_container::const_iterator const_commandNames_iterator;
108
109   vendorNames_container a_vendorNames;
110   avpNames_container a_avpNames;
111   commandNames_container a_commandNames;
112
113
114   // init
115   void initialize() ;
116
117   // check & addings
118   //void checkUniqueIdentifiers(const anna::xml::Node *rootNode) const noexcept(false);
119   void extractFormats(const anna::xml::Node *rootNode) noexcept(false);
120   void extractVendors(const anna::xml::Node *rootNode) noexcept(false);
121   void extractAvps(const anna::xml::Node *rootNode) noexcept(false);
122   void extractCommands(const anna::xml::Node *rootNode) noexcept(false);
123
124 public:
125
126   Dictionary();
127   ~Dictionary() {};
128
129   // get
130   const std::string & getName() const { return a_name; }
131   const Format * getFormat(const std::string & formatName) const ;
132   const Vendor * getVendor(S32 vendorId) const ;
133   const Vendor * getVendor(const std::string & vendorName) const ;
134   const Avp * getAvp(const AvpId & avpId) const ;
135   const Avp * getAvp(const std::string & avpName) const ;
136   const Command * getCommand(const CommandId & commandId) const ;
137   const Command * getCommand(const std::string & commandName) const ;
138
139   // set
140   void allowUpdates(bool allow = true) { a_allowUpdates = allow; }
141   void addFormat(const Format &, bool reserved = false) noexcept(false);
142   void addVendor(const Vendor &) noexcept(false);
143   void addAvp(const Avp &) noexcept(false);
144   void addCommand(const Command &) noexcept(false);
145
146   // containers
147   const_format_iterator format_begin() const { return a_formats.begin(); }
148   const_format_iterator format_end() const { return a_formats.end(); }
149   int format_size() const { return a_formats.size(); }
150
151   const_vendor_iterator vendor_begin() const { return a_vendors.begin(); }
152   const_vendor_iterator vendor_end() const { return a_vendors.end(); }
153   int vendor_size() const { return a_vendors.size(); }
154
155   const_avp_iterator avp_begin() const { return a_avps.begin(); }
156   const_avp_iterator avp_end() const { return a_avps.end(); }
157   int avp_size() const { return a_avps.size(); }
158
159   const_command_iterator command_begin() const { return a_commands.begin(); }
160   const_command_iterator command_end() const { return a_commands.end(); }
161   int command_size() const { return a_commands.size(); }
162
163
164   // helpers
165   /**
166   * Class string representation
167   *
168   * @return String with class content
169   */
170   std::string asString(void) const ;
171
172   /**
173      Class XML representation.
174      \param parent XML node over which we will put instance information.
175      \return XML documentcon with class content.
176   */
177   anna::xml::Node* asXML(anna::xml::Node* parent) const ;
178
179   /**
180      Class XML string representation
181      \return XML string representation with class content.
182   */
183   std::string asXMLString() const ;
184
185   // operators
186
187   /**
188   * Loads an XML dictionary document over the diameter dictionary.
189   *
190   * Successive loadings will imply data accumulation, and the behaviour for redefinitions could be configured
191   * in two ways: allow updating for dictionary items, or launch exception when any collision is found. This
192   * could be set at #allowUpdates, and default value deny such redefinitions. It could be interesting, in order
193   * to keep a more compact multi-stack configuration, allow updates sharing out the whole stack dictionaries as
194   * many parts as possible to get centralized definitions. I.e. common vendors and avps used at a unique xml file,
195   * and stack-specific commands in other set of xml files. Application only would have to load common part for all
196   * the created dictionaries with this method and specific one for each final dictionary:
197   *
198   * @param pathFile Path file to the xml document which represents the diameter dictionary.
199   *
200   * <pre>
201   * Dictionary *nokiaStack = stackEngine.createDictionary("/var/tmp/vendors_and_avps.xml", NOKIA_STACK_ID);
202   * Dictionary *huaweiStack = stackEngine.createDictionary("/var/tmp/vendors_and_avps.xml", HUAWEI_STACK_ID);
203   * nokiaStack->load("/var/tmp/nokia_commands.xml");
204   * huaweiStack->load("/var/tmp/huawei_commands.xml");
205   * </pre>
206   */
207   virtual void load(const std::string & pathFile) noexcept(false);
208
209   /**
210   * Clears dictionary content
211   */
212   void clear(void) { initialize(); } // initialize and prepares the dictionary
213 };
214
215
216 }
217 }
218 }
219
220
221 #endif