5dcb44046787c6ebc07c05e900756ee2f31c68d5
[anna.git] / include / anna / diameter / stack / Engine.hpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // http://redmine.teslayout.com/projects/anna-suite
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #ifndef anna_diameter_stack_Engine_hpp
38 #define anna_diameter_stack_Engine_hpp
39
40
41 // Local
42 #include <anna/diameter/stack/Dictionary.hpp>
43
44 #include <anna/xml/DTDMemory.hpp>
45 #include <anna/core/RuntimeException.hpp>
46 #include <anna/core/Singleton.hpp>
47
48 // STL
49 #include <string>
50 #include <vector>
51
52
53 namespace anna {
54
55 namespace diameter {
56
57 namespace stack {
58
59
60
61 //------------------------------------------------------------------------------
62 //----------------------------------------------------------------- class Engine
63 //------------------------------------------------------------------------------
64 /**
65 * Engine information
66 */
67 class Engine : public anna::Singleton <Engine> {
68
69
70   const anna::xml::DTDMemory * getDictionariesDTD() const throw() { return &a_dtd; }
71
72 public:
73
74   typedef std::map<unsigned int, Dictionary*> stack_container;
75   typedef stack_container::const_iterator const_stack_iterator;
76   typedef stack_container::iterator stack_iterator;
77
78
79   // get
80   /**
81   * Returns the dictionary registered with the provided identifier, which commonly
82   * is the 'Application-Id'.
83   *
84   * @param stackId Stack identifier.
85   * @return Dictionary reference, NULL if no stack found
86   */
87   const Dictionary * getDictionary(unsigned int stackId) const throw();
88
89   /** Beginning stack iterator */
90   const_stack_iterator stack_begin() const throw() { return a_stacks.begin(); }
91   /** Ending stack iterator */
92   const_stack_iterator stack_end() const throw() { return a_stacks.end(); }
93   /** Stack size */
94   int stack_size() const throw() { return a_stacks.size(); }
95
96   // helpers
97   /**
98   * Gets boolean about empty stack condition
99   *
100   * @return Boolean about empty stack condition
101   */
102   bool isEmpty(void) const throw() { return (!a_stacks.size()); }
103
104   /**
105   * Class string representation
106   *
107   * @param all Complete engine information versus only stacks list with its ids and dictionary names
108   *
109   * @return String with class content
110   */
111   std::string asString(bool all = true) const throw();
112
113   // set
114
115   /**
116   * Creates a diameter dictionary based on xml document file and store it over the provided stack identifier.
117   * If the stack is currently used, an exception will be launched to notify the stack collision (a dictionary
118   * could be removed with #removeStack).
119   * If missing xml path file, the dictionary will be created without loading data.
120   *
121   * <pre>
122   * Method 1 - Create and load xml data (commonly used for mono-load stacks):
123   *     engine->createDictionary(MMS_STACK_ID, "/var/tmp/mms.xml");
124   *
125   * Method 2 - Create and then, load xml data (useful for multi-load stacks):
126   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
127   *     // Loading data:
128   *     d->load("/var/tmp/mms.xml");
129   *     ...
130   * Method 3 - Create and then, load data through Dictionary API (*):
131   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
132   *     // Loading data:
133   *     d->addFormat(...);
134   *     d->addVendor(...);
135   *     ...
136   *
137   * (*) Applications could create a dictionary with its own API (#Dictionary) but it is much more difficult,
138   *     and usually, the method version with xml document load is used instead of this.
139   *
140   * </pre>
141   *
142   * @param stackId Stack identifier for created dictionary. We recommend to use the Diameter 'Application-Id' unless
143   * the application is going to use dictionaries covering different applications (which is not very usual).
144   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
145   *
146   * @return Dictionary registered. When exception happen, dictionary can be accessed by #getDictionary
147   */
148   Dictionary * createDictionary(unsigned int stackId, const std::string & xmlPathFile = "") throw(anna::RuntimeException);
149
150   /**
151   * Register a externally created Dictionary or a derived class from Dictionary
152   *
153   * @param stackId Stack identifier for provided dictionary. We recommend to use the Diameter 'Application-Id' unless
154   * the application is going to use dictionaries covering different applications (which is not very usual).
155   * @param dictionary Externally created dictionary
156   *
157   * @return Dictionary registered. When exception happen, dictionary can be accessed by #getDictionary
158   */
159   Dictionary * registerDictionary(unsigned int stackId, Dictionary *dictionary) throw(anna::RuntimeException);
160
161   /**
162   * Loads an XML dictionary document over the diameter stack identifiers (one or more stack id's).
163   * Passing more than one stack id, synchronized loadings are performed, which could be useful when
164   * an application loads many dictionaries with common avps.
165   *
166   * @param stacks Stacks identifiers over which the dictionary will be load.
167   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
168   */
169   void loadDictionary(const std::vector<unsigned int> & stacks, const std::string & xmlPathFile) throw(anna::RuntimeException);
170
171   /**
172   * Loads an XML dictionary document over all the diameter engine registered stacks. When more than one stack id is
173   * used, synchronized loadings are performed, which could be useful when an application loads many dictionaries with
174   * common avps.
175   *
176   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
177   */
178   void loadDictionary(const std::string & xmlPathFile) throw(anna::RuntimeException);
179
180   /**
181   * Remove all stacks registered on diameter stack engine
182   */
183   void removeStacks(void) throw() { a_stacks.clear(); }
184
185   /**
186   * Remove the stack provided.
187   *
188   * @param stackId Stack identifier for created dictionary
189   */
190   void removeStack(unsigned int stackId) throw();
191
192 private:
193
194   anna::xml::DTDMemory a_dtd;
195   stack_container a_stacks;
196
197   Engine();
198
199   friend class anna::Singleton <Engine>;
200   friend class Dictionary; // access to 'getDictionariesDTD()'
201 };
202
203
204 }
205 }
206 }
207
208
209 #endif