Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / service / simulator / inc / simulator_resource_model_schema.h
1 /******************************************************************
2  *
3  * Copyright 2016 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_MODEL_SCHEMA_H_
22 #define SIMULATOR_RESOURCE_MODEL_SCHEMA_H_
23
24 #include <unordered_map>
25 #include <memory>
26
27 #include "simulator_resource_model.h"
28
29 class IntegerProperty;
30 class DoubleProperty;
31 class BooleanProperty;
32 class StringProperty;
33 class ArrayProperty;
34 class ModelProperty;
35 class AttributeProperty
36 {
37     public:
38         enum class Type
39         {
40             INTEGER,
41             DOUBLE,
42             BOOLEAN,
43             STRING,
44             MODEL,
45             ARRAY
46         };
47
48         explicit AttributeProperty(Type type);
49         virtual ~AttributeProperty() {};
50
51         Type getType() const;
52         virtual bool isInteger() const { return false; }
53         virtual bool isDouble() const { return false; }
54         virtual bool isBoolean() const { return false; }
55         virtual bool isString() const { return false; }
56         virtual bool isArray() const { return false; }
57         virtual bool isModel() const { return false; }
58         virtual std::shared_ptr<IntegerProperty> asInteger() { return nullptr; }
59         virtual std::shared_ptr<DoubleProperty> asDouble() { return nullptr; }
60         virtual std::shared_ptr<BooleanProperty> asBoolean() { return nullptr; }
61         virtual std::shared_ptr<StringProperty> asString() { return nullptr; }
62         virtual std::shared_ptr<ArrayProperty> asArray() { return nullptr; }
63         virtual std::shared_ptr<ModelProperty> asModel() { return nullptr; }
64
65         virtual bool validate(const AttributeValueVariant &value) = 0;
66         virtual AttributeValueVariant buildValue() = 0;
67
68     private:
69         Type m_type;
70 };
71
72 class IntegerProperty : public AttributeProperty,
73     public std::enable_shared_from_this<IntegerProperty>
74 {
75     public:
76         static std::shared_ptr<IntegerProperty> build(int defaultValue = 0);
77
78         bool isInteger() const;
79         std::shared_ptr<IntegerProperty> asInteger();
80         void setDefaultValue(int value);
81         void setRange(int min, int max);
82         void setValues(const std::vector<int> &values);
83         bool hasRange() const;
84         bool hasValues() const;
85         int getDefaultValue() const;
86         bool getRange(int &min, int &max) const;
87         bool getValues(std::vector<int> &values) const;
88         bool validate(const AttributeValueVariant &value);
89         bool validate(const int &value);
90         AttributeValueVariant buildValue();
91
92     private:
93         explicit IntegerProperty(int defaultValue);
94
95         int m_defaultValue;
96         int m_min;
97         int m_max;
98         std::vector<int> m_values;
99         bool m_hasRange;
100 };
101
102 class DoubleProperty : public AttributeProperty,
103     public std::enable_shared_from_this<DoubleProperty>
104 {
105     public:
106         static std::shared_ptr<DoubleProperty> build(double defaultValue = 0.0);
107
108         bool isDouble() const;
109         std::shared_ptr<DoubleProperty> asDouble();
110         void setDefaultValue(double value);
111         void setRange(double min, double max);
112         void setValues(const std::vector<double> &values);
113         bool hasRange() const;
114         bool hasValues() const;
115         double getDefaultValue() const;
116         bool getRange(double &min, double &max) const;
117         bool getValues(std::vector<double> &values) const;
118         bool validate(const AttributeValueVariant &value);
119         bool validate(const double &value);
120         AttributeValueVariant buildValue();
121
122     private:
123         explicit DoubleProperty(double defaultValue);
124
125         double m_defaultValue;
126         double m_min;
127         double m_max;
128         std::vector<double> m_values;
129         bool m_hasRange;
130 };
131
132 class BooleanProperty : public AttributeProperty,
133     public std::enable_shared_from_this<BooleanProperty>
134 {
135     public:
136         static std::shared_ptr<BooleanProperty> build(bool defaultValue = true);
137
138         bool isBoolean() const;
139         std::shared_ptr<BooleanProperty> asBoolean();
140         void setDefaultValue(bool value);
141         bool getDefaultValue() const;
142         bool validate(const AttributeValueVariant &value);
143         AttributeValueVariant buildValue();
144
145     private:
146         explicit BooleanProperty(bool defaultValue);
147
148         bool m_defaultValue;
149 };
150
151 class StringProperty : public AttributeProperty,
152     public std::enable_shared_from_this<StringProperty>
153 {
154     public:
155         static std::shared_ptr<StringProperty> build(const std::string &defaultValue = "");
156
157         bool isString() const;
158         std::shared_ptr<StringProperty> asString();
159         void setDefaultValue(const std::string &value);
160         void setRange(size_t min, size_t max);
161         void setValues(const std::vector<std::string> &values);
162         bool hasRange() const;
163         bool hasValues() const;
164         std::string getDefaultValue() const;
165         bool getRange(size_t &min, size_t &max) const;
166         bool getValues(std::vector<std::string> &values) const;
167         bool validate(const AttributeValueVariant &value);
168         bool validate(const std::string &value);
169         AttributeValueVariant buildValue();
170
171     private:
172         StringProperty(const std::string &defaultValue);
173
174         std::string m_defaultValue;
175         size_t m_min;
176         size_t m_max;
177         std::vector<std::string> m_values;
178         bool m_hasRange;
179 };
180
181 class ArrayProperty : public AttributeProperty,
182     public std::enable_shared_from_this<ArrayProperty>
183 {
184     public:
185         static std::shared_ptr<ArrayProperty> build();
186
187         bool isArray() const;
188         std::shared_ptr<ArrayProperty> asArray();
189         void setRange(size_t minItems, size_t maxItems);
190         void setVariable(bool state);
191         void setUnique(bool state);
192         bool setElementProperty(const std::shared_ptr<AttributeProperty> &property);
193         bool hasRange() const;
194         bool isVariable() const;
195         bool isUnique() const;
196         size_t getMinItems() const;
197         size_t getMaxItems() const;
198         std::shared_ptr<AttributeProperty> getElementProperty();
199         bool validate(const AttributeValueVariant &value);
200         AttributeValueVariant buildValue();
201
202     private:
203         ArrayProperty();
204         int findDepth(std::shared_ptr<AttributeProperty> &elementProperty);
205
206         size_t m_min;
207         size_t m_max;
208         bool m_isVariableSize;
209         bool m_isUnique;
210         std::shared_ptr<AttributeProperty> m_elementProperty;
211         bool m_hasRange;
212 };
213
214 class ModelProperty : public AttributeProperty,
215     public std::enable_shared_from_this<ModelProperty>
216 {
217     public:
218         static std::shared_ptr<ModelProperty> build();
219
220         bool isModel() const;
221         std::shared_ptr<ModelProperty> asModel();
222         bool add(const std::string &name, const std::shared_ptr<AttributeProperty> &property,
223                  bool required = false);
224         std::shared_ptr<AttributeProperty> get(const std::string &name);
225         std::unordered_map<std::string, std::shared_ptr<AttributeProperty>> getChildProperties();
226         bool isRequired(const std::string &name);
227         void remove(const std::string &name);
228         void setRequired(const std::string &name);
229         void unsetRequired(const std::string &name);
230         SimulatorResourceModel buildResourceModel();
231         bool validate(const AttributeValueVariant &value);
232         bool validate(const SimulatorResourceModel &model);
233         AttributeValueVariant buildValue();
234
235     private:
236         ModelProperty();
237
238         std::unordered_map<std::string, bool> m_requiredAttributes;
239         std::unordered_map<std::string, std::shared_ptr<AttributeProperty>> m_childProperties;
240 };
241
242 typedef ModelProperty SimulatorResourceModelSchema;
243
244 #endif