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_Dictionary_hpp
38 #define anna_diameter_stack_Dictionary_hpp
42 #include <anna/diameter/defines.hpp>
43 #include <anna/diameter/stack/Format.hpp>
44 #include <anna/diameter/stack/Vendor.hpp>
45 #include <anna/diameter/stack/Avp.hpp>
46 #include <anna/diameter/stack/Command.hpp>
48 #include <anna/core/RuntimeException.hpp>
72 //------------------------------------------------------------------------------
73 //------------------------------------------------------------- class Dictionary
74 //------------------------------------------------------------------------------
76 * Dictionary information
82 typedef std::map<std::string, Format> format_container;
83 typedef format_container::const_iterator const_format_iterator;
85 typedef std::map<S32, Vendor> vendor_container;
86 typedef vendor_container::const_iterator const_vendor_iterator;
88 struct lessAvp { // order by vendor id
89 bool operator()(AvpId id1, AvpId id2) const {
90 if(id1.second == id2.second) return (id1.first < id2.first);
92 return (id1.second < id2.second);
95 typedef std::map<AvpId, Avp, lessAvp> avp_container;
96 typedef avp_container::const_iterator const_avp_iterator;
100 bool operator()(CommandId id1, CommandId id2) const {
101 if(id1.first == id2.first) {
102 if(id1.second != id2.second) return (id1.second); // request -> answer
107 return (id1.first < id2.first);
110 typedef std::map<CommandId, Command, lessCommand> command_container;
111 typedef command_container::const_iterator const_command_iterator;
117 format_container a_formats;
118 vendor_container a_vendors;
119 avp_container a_avps;
120 command_container a_commands;
123 const anna::xml::DTDMemory * a_dtd;
126 typedef std::map<std::string, const Vendor *> vendorNames_container;
127 typedef vendorNames_container::const_iterator const_vendorNames_iterator;
129 typedef std::map<std::string, const Avp *> avpNames_container;
130 typedef avpNames_container::const_iterator const_avpNames_iterator;
132 typedef std::map<std::string, const Command *> commandNames_container;
133 typedef commandNames_container::const_iterator const_commandNames_iterator;
135 vendorNames_container a_vendorNames;
136 avpNames_container a_avpNames;
137 commandNames_container a_commandNames;
141 void initialize() throw();
144 //void checkUniqueIdentifiers(const anna::xml::Node *rootNode) const throw(anna::RuntimeException);
145 void extractFormats(const anna::xml::Node *rootNode) throw(anna::RuntimeException);
146 void extractVendors(const anna::xml::Node *rootNode) throw(anna::RuntimeException);
147 void extractAvps(const anna::xml::Node *rootNode) throw(anna::RuntimeException);
148 void extractCommands(const anna::xml::Node *rootNode) throw(anna::RuntimeException);
156 const std::string & getName() const throw() { return a_name; }
157 const Format * getFormat(const std::string & formatName) const throw();
158 const Vendor * getVendor(S32 vendorId) const throw();
159 const Vendor * getVendor(const std::string & vendorName) const throw();
160 const Avp * getAvp(const AvpId & avpId) const throw();
161 const Avp * getAvp(const std::string & avpName) const throw();
162 const Command * getCommand(const CommandId & commandId) const throw();
163 const Command * getCommand(const std::string & commandName) const throw();
166 void allowUpdates(bool allow = true) throw() { a_allowUpdates = allow; }
167 void addFormat(const Format &, bool reserved = false) throw(anna::RuntimeException);
168 void addVendor(const Vendor &) throw(anna::RuntimeException);
169 void addAvp(const Avp &) throw(anna::RuntimeException);
170 void addCommand(const Command &) throw(anna::RuntimeException);
173 const_format_iterator format_begin() const throw() { return a_formats.begin(); }
174 const_format_iterator format_end() const throw() { return a_formats.end(); }
175 int format_size() const throw() { return a_formats.size(); }
177 const_vendor_iterator vendor_begin() const throw() { return a_vendors.begin(); }
178 const_vendor_iterator vendor_end() const throw() { return a_vendors.end(); }
179 int vendor_size() const throw() { return a_vendors.size(); }
181 const_avp_iterator avp_begin() const throw() { return a_avps.begin(); }
182 const_avp_iterator avp_end() const throw() { return a_avps.end(); }
183 int avp_size() const throw() { return a_avps.size(); }
185 const_command_iterator command_begin() const throw() { return a_commands.begin(); }
186 const_command_iterator command_end() const throw() { return a_commands.end(); }
187 int command_size() const throw() { return a_commands.size(); }
192 * Class string representation
194 * @return String with class content
196 std::string asString(void) const throw();
199 Class XML representation.
200 \param parent XML node over which we will put instance information.
201 \return XML documentcon with class content.
203 anna::xml::Node* asXML(anna::xml::Node* parent) const throw();
206 Class XML string representation
207 \return XML string representation with class content.
209 std::string asXMLString() const throw();
214 * Loads an XML dictionary document over the diameter dictionary.
216 * Successive loadings will imply data accumulation, and the behaviour for redefinitions could be configured
217 * in two ways: allow updating for dictionary items, or launch exception when any collision is found. This
218 * could be set at #allowUpdates, and default value deny such redefinitions. It could be interesting, in order
219 * to keep a more compact multi-stack configuration, allow updates sharing out the whole stack dictionaries as
220 * many parts as possible to get centralized definitions. I.e. common vendors and avps used at a unique xml file,
221 * and stack-specific commands in other set of xml files. Application only would have to load common part for all
222 * the created dictionaries with this method and specific one for each final dictionary:
224 * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
227 * Dictionary *nokiaStack = stackEngine.createDictionary("/var/tmp/vendors_and_avps.xml", NOKIA_STACK_ID);
228 * Dictionary *huaweiStack = stackEngine.createDictionary("/var/tmp/vendors_and_avps.xml", HUAWEI_STACK_ID);
229 * nokiaStack->load("/var/tmp/nokia_commands.xml");
230 * huaweiStack->load("/var/tmp/huawei_commands.xml");
233 void load(const std::string & xmlPathFile) throw(anna::RuntimeException);
236 * Clears dictionary content
238 void clear(void) throw() { initialize(); } // initialize and prepares the dictionary