Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / service / simulator / src / common / response_model.cpp
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 #include "response_model.h"
22
23 ResponseModel::ResponseModel(int code) : m_code(code) {}
24
25 void ResponseModel::setRepSchema(SimulatorResourceModelSP &repSchema)
26 {
27     m_repSchema = repSchema;
28 }
29
30 SimulatorResult ResponseModel::verifyResponse(const OC::OCRepresentation &rep)
31 {
32     for (auto & ocAttribute : rep)
33     {
34         SimulatorResourceModel::Attribute attribute;
35         if (false == m_repSchema->getAttribute(ocAttribute.attrname(), attribute))
36         {
37             return SIMULATOR_UKNOWN_PROPERTY;
38         }
39
40         switch (attribute.getValueType())
41         {
42             case SimulatorResourceModel::Attribute::ValueType::INTEGER : // Integer
43                 {
44                     SimulatorResult result = validateAttributeInteger(attribute, ocAttribute);
45                     if (SIMULATOR_OK != result)
46                     {
47                         return result;
48                     }
49                 }
50                 break;
51
52             case SimulatorResourceModel::Attribute::ValueType::DOUBLE : // Double
53                 {
54                     SimulatorResult result = validateAttributeDouble(attribute, ocAttribute);
55                     if (SIMULATOR_OK != result)
56                     {
57                         return result;
58                     }
59                 }
60                 break;
61
62             case SimulatorResourceModel::Attribute::ValueType::STRING : // String
63                 {
64                     SimulatorResult result = validateAttributeString(attribute, ocAttribute);
65                     if (SIMULATOR_OK != result)
66                     {
67                         return result;
68                     }
69                 }
70                 break;
71         }
72     }
73
74     return SIMULATOR_OK;
75 }
76
77 SimulatorResult ResponseModel::validateAttributeInteger(
78     SimulatorResourceModel::Attribute &attrSchema,
79     const OC::OCRepresentation::AttributeItem &ocAttribute)
80 {
81     // Check the value type
82     if (OC::AttributeType::Integer != ocAttribute.type())
83     {
84         return SIMULATOR_TYPE_MISMATCH;
85     }
86
87     // Check value if it is in range
88     int min, max, value;
89     attrSchema.getRange(min, max);
90     value = ocAttribute.getValue<int>();
91     if (value < min || value > max)
92     {
93         return SIMULATOR_BAD_VALUE;
94     }
95
96     return SIMULATOR_OK;
97 }
98
99 SimulatorResult ResponseModel::validateAttributeDouble(
100     SimulatorResourceModel::Attribute &attrSchema,
101     const OC::OCRepresentation::AttributeItem &ocAttribute)
102 {
103     // Check the value type
104     if (OC::AttributeType::Double != ocAttribute.type())
105     {
106         return SIMULATOR_TYPE_MISMATCH;
107     }
108
109     return SIMULATOR_OK;
110 }
111
112 SimulatorResult ResponseModel::validateAttributeString(
113     SimulatorResourceModel::Attribute &attrSchema,
114     const OC::OCRepresentation::AttributeItem &ocAttribute)
115 {
116     // Check the value type
117     if (OC::AttributeType::String != ocAttribute.type())
118     {
119         return SIMULATOR_TYPE_MISMATCH;
120     }
121
122     // TODO: Check the allowed values
123     return SIMULATOR_OK;
124 }