replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / simulator / ramlparser / raml / model / Raml.h
1 /******************************************************************\r
2  *\r
3  * Copyright 2015 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 /**\r
22  * @file   Raml.h\r
23  *\r
24  * @brief   This file provides data Model for RAML file.\r
25  */\r
26 \r
27 #ifndef RAML_H\r
28 #define RAML_H\r
29 \r
30 #include <map>\r
31 #include <list>\r
32 #include <string>\r
33 #include "AbstractParam.h"\r
34 #include "UriParameter.h"\r
35 #include "QueryParameter.h"\r
36 #include "FormParameter.h"\r
37 #include "Header.h"\r
38 \r
39 #include "RequestResponseBody.h"\r
40 #include "RamlResource.h"\r
41 #include "ActionType.h"\r
42 #include "Action.h"\r
43 #include "Response.h"\r
44 #include "Schema.h"\r
45 #include "IncludeResolver.h"\r
46 \r
47 #include "DocumentationItem.h"\r
48 #include "yaml-cpp/yaml.h"\r
49 #include "yaml-cpp/exceptions.h"\r
50 #include "RamlExceptions.h"\r
51 #include "RamlUtils.h"\r
52 #if defined (__TIZENRT__)\r
53 #include <apps/netutils/cJSON.h>\r
54 #else\r
55 #include "cJSON.h"\r
56 #endif\r
57 \r
58 \r
59 namespace RAML\r
60 {\r
61     /**\r
62      * @class   Raml\r
63      * @brief   This class provides data Model for RAML file.\r
64      */\r
65     class Raml\r
66     {\r
67         public:\r
68             /**\r
69                  * This method is for getting Title from Raml.\r
70                  *\r
71                  * @return Title as string.\r
72                  */\r
73             virtual std::string getTitle() const;\r
74 \r
75             /**\r
76                  * This method is for setting Title to Raml.\r
77                  *\r
78                  * @param title - Title as string\r
79                  */\r
80             virtual void setTitle(const std::string &title);\r
81 \r
82             /**\r
83                  * This method is for getting Version from Raml.\r
84                  *\r
85                  * @return Version as string.\r
86                  */\r
87             virtual std::string getVersion() const;\r
88 \r
89             /**\r
90                  * This method is for setting Version to Raml.\r
91                  *\r
92                  * @param version - Version as string\r
93                  */\r
94             virtual void setVersion(const std::string &version);\r
95 \r
96             /**\r
97                  * This method is for getting BaseUri from Raml.\r
98                  *\r
99                  * @return BaseUri as string.\r
100                  */\r
101             virtual std::string getBaseUri() const;\r
102 \r
103             /**\r
104                  * This method is for setting BaseUri to Raml.\r
105                  *\r
106                  * @param baseUri - BaseUri as string\r
107                  */\r
108             virtual void setBaseUri(const std::string &baseUri);\r
109 \r
110             /**\r
111                  * This method is for getting Protocols from Raml.\r
112                  *\r
113                  * @return list of Protocols as string.\r
114                  */\r
115             virtual std::list<std::string> const &getProtocols() const;\r
116 \r
117             /**\r
118                  * This method is for setting Protocols to Raml.\r
119                  *\r
120                  * @param protocol - Protocol as string\r
121                  */\r
122             virtual void setProtocol(const std::string &protocol);\r
123 \r
124 \r
125             /**\r
126                  * This method is for getting BaseUriParameter from Raml.\r
127                  *\r
128                  * @return map of BaseUriParameter name and Pointer to UriParameter.\r
129                  */\r
130             virtual std::map<std::string, UriParameterPtr> const &getBaseUriParameters() const;\r
131 \r
132             /**\r
133                  * This method is for setting BaseUriParameter to Raml.\r
134                  *\r
135                  * @param paramName - name of BaseUriParameter as string\r
136                  * @param uriParameter - pointer to UriParameter\r
137                  */\r
138             virtual void setBaseUriParameter(const std::string &paramName,\r
139                                              const UriParameterPtr &uriParameter);\r
140 \r
141 \r
142             /**\r
143                  * This method is for getting MediaType from Raml.\r
144                  *\r
145                  * @return Title as string.\r
146                  */\r
147             virtual std::string getMediaType() const;\r
148 \r
149             /**\r
150                  * This method is for setting MediaType to Raml.\r
151                  *\r
152                  * @param mediaType - MediaType as string\r
153                  */\r
154             virtual void setMediaType(const std::string &mediaType);\r
155 \r
156             /**\r
157                  * This method is for getting Schemas from Raml.\r
158                  *\r
159                  * @return list of Schemas name and Pointer to Schema as pair.\r
160                  */\r
161             virtual std::list<std::pair<std::string, SchemaPtr> > const &getSchemas() const;\r
162 \r
163             /**\r
164                  * This method is for setting Schemas to Raml.\r
165                  *\r
166                  * @param schemaName - name of schema as string\r
167                  * @param schema - pointer to Schema\r
168                  */\r
169             virtual void setSchema(const std::string &schemaName, const SchemaPtr &schema);\r
170 \r
171             /**\r
172                  * This method is for getting ResourceTypes from Raml.\r
173                  *\r
174                  * @return list of ResourceTypes name and Pointer to RamlResource as pair.\r
175                  */\r
176             virtual std::list<std::pair<std::string, RamlResourcePtr> > const &getResourceTypes() const;\r
177 \r
178             /**\r
179                  * This method is for setting ResourceTypes to Raml.\r
180                  *\r
181                  * @param typeName - name of ResourceType as string\r
182                  * @param resourceType - pointer to RamlResource\r
183                  */\r
184             virtual void setResourceType(const std::string &typeName,\r
185                                          const RamlResourcePtr &resourceType);\r
186 \r
187             /**\r
188                  * This method is for getting Traits from Raml.\r
189                  *\r
190                  * @return list of Traits name and Pointer to Action as pair.\r
191                  */\r
192             virtual std::list<std::pair<std::string, ActionPtr> > const &getTraits() const;\r
193 \r
194             /**\r
195                  * This method is for setting Traits to Raml.\r
196                  *\r
197                  * @param traitName - name of Trait as string\r
198                  * @param trait - pointer to Action\r
199                  */\r
200             virtual void setTrait(const std::string &traitName, const ActionPtr &trait);\r
201 \r
202             /**\r
203                  * This method is for getting Resource from Raml.\r
204                  *\r
205                  * @param resourceName - name of Resource as string\r
206                  *\r
207                  * @return Pointer to Resource\r
208                  */\r
209             virtual RamlResourcePtr getResource(const std::string &resourceName);\r
210 \r
211             /**\r
212                  * This method is for getting Resource from Raml.\r
213                  *\r
214                  * @return map of Resource name and Pointer to Resource\r
215                  */\r
216             virtual std::map<std::string, RamlResourcePtr> const &getResources() const;\r
217 \r
218             /**\r
219                  * This method is for setting Resource to Raml.\r
220                  *\r
221                  * @param resourceKey - name of Resource as string\r
222                  * @param resource - pointer to Resource\r
223                  */\r
224             virtual void setResource(const std::string &resourceKey,\r
225                                      const RamlResourcePtr &resource);\r
226 \r
227             /**\r
228                  * This method is for setting DocumentationItem to Raml.\r
229                  *\r
230                  * @param documentationItem - pointer to DocumentationItem\r
231                  */\r
232             virtual void setDocumentationItem(const std::shared_ptr<DocumentationItem> &documentationItem);\r
233 \r
234             /**\r
235                  * This method is for getting DocumentationItem from Raml.\r
236                  *\r
237                  * @return list of DocumentationItem\r
238                  */\r
239             virtual std::list<std::shared_ptr<DocumentationItem> > const &getDocumentation() const;\r
240 \r
241             /**\r
242                  * This method is for setting Raml object reading from Yaml nodes\r
243                  *\r
244                  * @param yamlNode - Reference to YamlNode for reading into Raml object\r
245                  */\r
246             void readRamlFromYaml(const YAML::Node &yamlNode);\r
247 \r
248             /**\r
249                   * Constructor of Raml.\r
250                   */\r
251             Raml() : m_includeResolver(std::make_shared<IncludeResolver>()) {}\r
252 \r
253             /**\r
254                    * Constructor of Raml.\r
255                    *\r
256                    * @param fileLocation - RAML configuration file path.\r
257                    * @param ramlName - RAML configuration file Name\r
258                    *\r
259                    *  NOTE: Constructor would throw RamlException if any error occured.\r
260                    */\r
261             Raml(const std::string &fileLocation, const std::string &ramlName)\r
262                 : m_includeResolver(std::make_shared<IncludeResolver>(fileLocation))\r
263             {\r
264                 try\r
265                 {\r
266                     YAML::Node yamlRootNode = YAML::LoadFile(fileLocation + ramlName);\r
267                     readRamlFromYaml(yamlRootNode);\r
268                 }\r
269                 catch (YAML::ParserException &e)\r
270                 {\r
271                     throw RamlParserException(e.mark, e.msg);\r
272                 }\r
273                 catch (YAML::RepresentationException &e)\r
274                 {\r
275                     throw RamlRepresentationException(e.mark, e.msg);\r
276                 }\r
277                 catch (YAML::BadFile &e)\r
278                 {\r
279                     throw RamlBadFile(e.mark, e.msg);\r
280                 }\r
281                 catch (JsonException &e)\r
282                 {\r
283                     throw;\r
284                 }\r
285             }\r
286         private:\r
287             std::string m_title;\r
288             std::string m_version;\r
289             std::string m_baseUri;\r
290             std::list<std::string> m_protocols;\r
291             std::map<std::string, UriParameterPtr> m_baseUriParameters;\r
292             std::string m_mediaType;\r
293             std::list <std::pair<std::string, SchemaPtr> > m_schemas;\r
294             std::list <std::pair<std::string, RamlResourcePtr> > m_resourceTypes;\r
295             std::list <std::pair<std::string, ActionPtr> > m_traits;\r
296             std::map<std::string, RamlResourcePtr> m_resources;\r
297             std::list<std::shared_ptr<DocumentationItem> > m_documentation;\r
298             IncludeResolverPtr m_includeResolver;\r
299     };\r
300 \r
301     /** RamlPtr - shared Ptr to Raml.*/\r
302     typedef std::shared_ptr<Raml> RamlPtr;\r
303 \r
304 }\r
305 #endif\r