New features
[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<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(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   * @return String with class content
108   */
109   std::string asString(void) const throw();
110
111   // set
112
113   /**
114   * Creates a diameter dictionary based on xml document file and store it over the provided stack identifier.
115   * If the stack is currently used, an exception will be launched to notify the stack collision (a dictionary
116   * could be removed with #removeStack).
117   * If missing xml path file, the dictionary will be created without loading data.
118   *
119   * <pre>
120   * Method 1 - Create and load xml data (commonly used for mono-load stacks):
121   *     engine->createDictionary(MMS_STACK_ID, "/var/tmp/mms.xml");
122   *
123   * Method 2 - Create and then, load xml data (useful for multi-load stacks):
124   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
125   *     // Loading data:
126   *     d->load("/var/tmp/mms.xml");
127   *     ...
128   * Method 3 - Create and then, load data through Dictionary API (*):
129   *     Dictionary * d = engine->createDictionary(MMS_STACK_ID);
130   *     // Loading data:
131   *     d->addFormat(...);
132   *     d->addVendor(...);
133   *     ...
134   *
135   * (*) Applications could create a dictionary with its own API (#Dictionary) but it is much more difficult,
136   *     and usually, the method version with xml document load is used instead of this.
137   *
138   * </pre>
139   *
140   * @param stackId Stack identifier for created dictionary. We recommend to use the Diameter 'Application-Id' unless
141   * the application is going to use dictionaries covering different applications (which is not very usual).
142   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
143   *
144   * @return Dictionary registered. When exception happen, dictionary can be accessed by #getDictionary
145   */
146   Dictionary * createDictionary(int stackId, const std::string & xmlPathFile = "") throw(anna::RuntimeException);
147
148   /**
149   * Register a externally created Dictionary or a derived class from Dictionary
150   *
151   * @param stackId Stack identifier for provided dictionary. We recommend to use the Diameter 'Application-Id' unless
152   * the application is going to use dictionaries covering different applications (which is not very usual).
153   * @param dictionary Externally created dictionary
154   *
155   * @return Dictionary registered. When exception happen, dictionary can be accessed by #getDictionary
156   */
157   Dictionary * registerDictionary(int stackId, Dictionary *dictionary) throw(anna::RuntimeException);
158
159   /**
160   * Loads an XML dictionary document over the diameter stack identifiers (one or more stack id's).
161   * Passing more than one stack id, synchronized loadings are performed, which could be useful when
162   * an application loads many dictionaries with common avps.
163   *
164   * @param stacks Stacks identifiers over which the dictionary will be load.
165   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
166   */
167   void loadDictionary(const std::vector<int> & stacks, const std::string & xmlPathFile) throw(anna::RuntimeException);
168
169   /**
170   * Loads an XML dictionary document over all the diameter engine registered stacks. When more than one stack id is
171   * used, synchronized loadings are performed, which could be useful when an application loads many dictionaries with
172   * common avps.
173   *
174   * @param xmlPathFile Path file to the xml document which represents the diameter dictionary.
175   */
176   void loadDictionary(const std::string & xmlPathFile) throw(anna::RuntimeException);
177
178   /**
179   * Remove all stacks registered on diameter stack engine
180   */
181   void removeStacks(void) throw() { a_stacks.clear(); }
182
183   /**
184   * Remove the stack provided.
185   *
186   * @param stackId Stack identifier for created dictionary
187   */
188   void removeStack(int stackId) throw();
189
190 private:
191
192   anna::xml::DTDMemory a_dtd;
193   stack_container a_stacks;
194
195   Engine();
196
197   friend class anna::Singleton <Engine>;
198   friend class Dictionary; // access to 'getDictionariesDTD()'
199 };
200
201
202 }
203 }
204 }
205
206
207 #endif