Imported Upstream version 1.0.0
[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 "Utils.h"\r
52 #include "cJSON.h"\r
53 \r
54 \r
55 namespace RAML\r
56 {\r
57     /**\r
58      * @class   Raml\r
59      * @brief   This class provides data Model for RAML file.\r
60      */\r
61     class Raml\r
62     {\r
63         public:\r
64             /**\r
65                  * This method is for getting Title from Raml.\r
66                  *\r
67                  * @return Title as string.\r
68                  */\r
69             virtual std::string getTitle() const;\r
70 \r
71             /**\r
72                  * This method is for setting Title to Raml.\r
73                  *\r
74                  * @param title - Title as string\r
75                  */\r
76             virtual void setTitle(const std::string &title);\r
77 \r
78             /**\r
79                  * This method is for getting Version from Raml.\r
80                  *\r
81                  * @return Version as string.\r
82                  */\r
83             virtual std::string getVersion() const;\r
84 \r
85             /**\r
86                  * This method is for setting Version to Raml.\r
87                  *\r
88                  * @param version - Version as string\r
89                  */\r
90             virtual void setVersion(const std::string &version);\r
91 \r
92             /**\r
93                  * This method is for getting BaseUri from Raml.\r
94                  *\r
95                  * @return BaseUri as string.\r
96                  */\r
97             virtual std::string getBaseUri() const;\r
98 \r
99             /**\r
100                  * This method is for setting BaseUri to Raml.\r
101                  *\r
102                  * @param baseUri - BaseUri as string\r
103                  */\r
104             virtual void setBaseUri(const std::string &baseUri);\r
105 \r
106             /**\r
107                  * This method is for getting Protocols from Raml.\r
108                  *\r
109                  * @return list of Protocols as string.\r
110                  */\r
111             virtual std::list<std::string> const &getProtocols() const;\r
112 \r
113             /**\r
114                  * This method is for setting Protocols to Raml.\r
115                  *\r
116                  * @param protocol - Protocol as string\r
117                  */\r
118             virtual void setProtocol(const std::string &protocol);\r
119 \r
120 \r
121             /**\r
122                  * This method is for getting BaseUriParameter from Raml.\r
123                  *\r
124                  * @return map of BaseUriParameter name and Pointer to UriParameter.\r
125                  */\r
126             virtual std::map<std::string, UriParameterPtr> const &getBaseUriParameters() const;\r
127 \r
128             /**\r
129                  * This method is for setting BaseUriParameter to Raml.\r
130                  *\r
131                  * @param paramName - name of BaseUriParameter as string\r
132                  * @param uriParameter - pointer to UriParameter\r
133                  */\r
134             virtual void setBaseUriParameter(const std::string &paramName,\r
135                                              const UriParameterPtr &uriParameter);\r
136 \r
137 \r
138             /**\r
139                  * This method is for getting MediaType from Raml.\r
140                  *\r
141                  * @return Title as string.\r
142                  */\r
143             virtual std::string getMediaType() const;\r
144 \r
145             /**\r
146                  * This method is for setting MediaType to Raml.\r
147                  *\r
148                  * @param mediaType - MediaType as string\r
149                  */\r
150             virtual void setMediaType(const std::string &mediaType);\r
151 \r
152             /**\r
153                  * This method is for getting Schemas from Raml.\r
154                  *\r
155                  * @return list of Schemas name and Pointer to Schema as pair.\r
156                  */\r
157             virtual std::list<std::pair<std::string, SchemaPtr> > const &getSchemas() const;\r
158 \r
159             /**\r
160                  * This method is for setting Schemas to Raml.\r
161                  *\r
162                  * @param schemaName - name of schema as string\r
163                  * @param schema - pointer to Schema\r
164                  */\r
165             virtual void setSchema(const std::string &schemaName, const SchemaPtr &schema);\r
166 \r
167             /**\r
168                  * This method is for getting ResourceTypes from Raml.\r
169                  *\r
170                  * @return list of ResourceTypes name and Pointer to RamlResource as pair.\r
171                  */\r
172             virtual std::list<std::pair<std::string, RamlResourcePtr> > const &getResourceTypes() const;\r
173 \r
174             /**\r
175                  * This method is for setting ResourceTypes to Raml.\r
176                  *\r
177                  * @param typeName - name of ResourceType as string\r
178                  * @param resourceType - pointer to RamlResource\r
179                  */\r
180             virtual void setResourceType(const std::string &typeName,\r
181                                          const RamlResourcePtr &resourceType);\r
182 \r
183             /**\r
184                  * This method is for getting Traits from Raml.\r
185                  *\r
186                  * @return list of Traits name and Pointer to Action as pair.\r
187                  */\r
188             virtual std::list<std::pair<std::string, ActionPtr> > const &getTraits() const;\r
189 \r
190             /**\r
191                  * This method is for setting Traits to Raml.\r
192                  *\r
193                  * @param traitName - name of Trait as string\r
194                  * @param trait - pointer to Action\r
195                  */\r
196             virtual void setTrait(const std::string &traitName, const ActionPtr &trait);\r
197 \r
198             /**\r
199                  * This method is for getting Resource from Raml.\r
200                  *\r
201                  * @param resourceName - name of Resource as string\r
202                  *\r
203                  * @return Pointer to Resource\r
204                  */\r
205             virtual RamlResourcePtr getResource(const std::string &resourceName);\r
206 \r
207             /**\r
208                  * This method is for getting Resource from Raml.\r
209                  *\r
210                  * @return map of Resource name and Pointer to Resource\r
211                  */\r
212             virtual std::map<std::string, RamlResourcePtr> const &getResources() const;\r
213 \r
214             /**\r
215                  * This method is for setting Resource to Raml.\r
216                  *\r
217                  * @param resourceKey - name of Resource as string\r
218                  * @param resource - pointer to Resource\r
219                  */\r
220             virtual void setResource(const std::string &resourceKey,\r
221                                      const RamlResourcePtr &resource);\r
222 \r
223             /**\r
224                  * This method is for setting DocumentationItem to Raml.\r
225                  *\r
226                  * @param documentationItem - pointer to DocumentationItem\r
227                  */\r
228             virtual void setDocumentationItem(const std::shared_ptr<DocumentationItem> &documentationItem);\r
229 \r
230             /**\r
231                  * This method is for getting DocumentationItem from Raml.\r
232                  *\r
233                  * @return list of DocumentationItem\r
234                  */\r
235             virtual std::list<std::shared_ptr<DocumentationItem> > const &getDocumentation() const;\r
236 \r
237             /**\r
238                  * This method is for setting Raml object reading from Yaml nodes\r
239                  *\r
240                  * @param yamlNode - Reference to YamlNode for reading into Raml object\r
241                  */\r
242             void readRamlFromYaml(const YAML::Node &yamlNode);\r
243 \r
244             /**\r
245                   * Constructor of Raml.\r
246                   */\r
247             Raml() : m_includeResolver(std::make_shared<IncludeResolver>()) {}\r
248 \r
249             /**\r
250                    * Constructor of Raml.\r
251                    *\r
252                    * @param fileLocation - RAML configuration file path.\r
253                    * @param ramlName - RAML configuration file Name\r
254                    *\r
255                    *  NOTE: Constructor would throw RamlException if any error occured.\r
256                    */\r
257             Raml(const std::string &fileLocation, const std::string &ramlName)\r
258                 : m_includeResolver(std::make_shared<IncludeResolver>(fileLocation))\r
259             {\r
260                 try\r
261                 {\r
262                     YAML::Node yamlRootNode = YAML::LoadFile(fileLocation + ramlName);\r
263                     readRamlFromYaml(yamlRootNode);\r
264                 }\r
265                 catch (YAML::ParserException &e)\r
266                 {\r
267                     throw RamlParserException(e.mark, e.msg);\r
268                 }\r
269                 catch (YAML::RepresentationException &e)\r
270                 {\r
271                     throw RamlRepresentationException(e.mark, e.msg);\r
272                 }\r
273                 catch (YAML::BadFile &e)\r
274                 {\r
275                     throw RamlBadFile(e.mark, e.msg);\r
276                 }\r
277                 catch (JsonException &e)\r
278                 {\r
279                     throw;\r
280                 }\r
281             }\r
282         private:\r
283             std::string m_title;\r
284             std::string m_version;\r
285             std::string m_baseUri;\r
286             std::list<std::string> m_protocols;\r
287             std::map<std::string, UriParameterPtr> m_baseUriParameters;\r
288             std::string m_mediaType;\r
289             std::list <std::pair<std::string, SchemaPtr> > m_schemas;\r
290             std::list <std::pair<std::string, RamlResourcePtr> > m_resourceTypes;\r
291             std::list <std::pair<std::string, ActionPtr> > m_traits;\r
292             std::map<std::string, RamlResourcePtr> m_resources;\r
293             std::list<std::shared_ptr<DocumentationItem> > m_documentation;\r
294             IncludeResolverPtr m_includeResolver;\r
295     };\r
296 \r
297     /** RamlPtr - shared Ptr to Raml.*/\r
298     typedef std::shared_ptr<Raml> RamlPtr;\r
299 \r
300 }\r
301 #endif\r