Fixed issue of invalid value gets update even if attribute has allowed values property.
[platform/upstream/iotivity.git] / service / simulator / src / server / simulator_resource_factory.h
1 /******************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #ifndef SIMULATOR_RESOURCE_FACTORY_H_
22 #define SIMULATOR_RESOURCE_FACTORY_H_
23
24 #include "simulator_single_resource.h"
25 #include "simulator_collection_resource.h"
26 #include "RamlParser.h"
27
28 namespace RAML
29 {
30     class RamlResource;
31     class Properties;
32     class Items;
33 }
34
35 class SimulatorResourceFactory
36 {
37     public:
38         /**
39          * API for getting singleton instance of SimulatorClient class.
40          *
41          * @return Singleton instance of SimulatorClient class.
42          *
43          */
44         static SimulatorResourceFactory *getInstance();
45
46         /**
47          * API to create resource based on the given RAML file.
48          *
49          * @param configPath - RAML file path.
50          *
51          * @return SimulatorResource shared object created using RAML file.
52          */
53         std::shared_ptr<SimulatorResource> createResource(const std::string &configPath);
54
55         /**
56          * API to create resource based on the given RAML file.
57          *
58          * @param configPath - RAML file path.
59          *
60          * @return SimulatorResource shared object created using RAML file.
61          */
62         std::vector<std::shared_ptr<SimulatorResource> > createResource(
63             const std::string &configPath, unsigned int count);
64
65         /**
66          * API to create simple resource.
67          *
68          * @param name - Name of resource.
69          * @param uri - URI of resource.
70          * @param resourceType - ResourceType of resource.
71          *
72          * @return SimulatorSimpleResource.
73          */
74         std::shared_ptr<SimulatorSingleResource> createSingleResource(
75             const std::string &name, const std::string &uri, const std::string &resourceType);
76
77         /**
78          * API to create collection resource.
79          *
80          * @param name - Name of collection resource.
81          * @param uri - URI of resource.
82          * @param resourceType - ResourceType of collection resource.
83          *
84          * @return SimulatorCollectionResource.
85          */
86         std::shared_ptr<SimulatorCollectionResource> createCollectionResource(
87             const std::string &name, const std::string &uri, const std::string &resourceType);
88
89     private:
90         SimulatorResourceModel::Attribute buildAttribute(
91             std::shared_ptr<RAML::Properties> propertyElement);
92         SimulatorResourceModel buildResourceModel(std::shared_ptr<RAML::Items> item);
93         SimulatorResourceModel buildModelFromResponseBody(
94             RAML::RequestResponseBodyPtr responseBody, std::string &resourceType,
95             std::vector<std::string> &interfaceType);
96         RAML::RequestResponseBodyPtr getRAMLResponseBody(
97             std::shared_ptr<RAML::RamlResource> ramlResource, RAML::ActionType type, std::string responseCode);
98         std::shared_ptr<SimulatorResource> buildResource(
99             std::shared_ptr<RAML::RamlResource> ramlResource);
100
101         SimulatorResourceFactory() = default;
102         SimulatorResourceFactory(const SimulatorResourceFactory &) = delete;
103         SimulatorResourceFactory &operator=(const SimulatorResourceFactory &) = delete;
104         SimulatorResourceFactory(SimulatorResourceFactory &&) = delete;
105         SimulatorResourceFactory &operator=(SimulatorResourceFactory && ) = delete;
106 };
107
108 class ResourceURIFactory
109 {
110     public:
111         /**
112          * API for getting singleton instance of SimulatorClient class.
113          *
114          * @return Singleton instance of SimulatorClient class.
115          *
116          */
117         static ResourceURIFactory *getInstance();
118
119         /**
120          * API to construct unique URI from the given base @uri.
121          *
122          * @param uri - Base uri to be used to construct unique uri.
123          *
124          * @return Unique uri.
125          */
126         std::string constructURI(const std::string &uri);
127
128     private:
129         ResourceURIFactory();
130         ResourceURIFactory(const ResourceURIFactory &) = delete;
131         ResourceURIFactory &operator=(const ResourceURIFactory &) = delete;
132         ResourceURIFactory(ResourceURIFactory &&) = delete;
133         ResourceURIFactory &operator=(ResourceURIFactory && ) = delete;
134
135         bool isUnique(const std::string &uri);
136         void updateUri(const std::string &uri);
137
138         unsigned int m_id;
139         std::mutex m_lock;
140         std::map<std::string, bool> m_uriList;
141 };
142
143 #endif