1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 #include <crow/http_request.h>
18 #include <crow/http_response.h>
19 
20 #include <nlohmann/json.hpp>
21 
22 namespace redfish
23 {
24 
25 namespace json_util
26 {
27 
28 /**
29  * @brief Defines JSON utils operation status
30  */
31 enum class Result
32 {
33     SUCCESS,
34     NOT_EXIST,
35     WRONG_TYPE,
36     NULL_POINTER
37 };
38 
39 /**
40  * @brief Describes JSON utils messages requirement
41  */
42 enum class MessageSetting
43 {
44     NONE = 0x0,      ///< No messages will be added
45     MISSING = 0x1,   ///< PropertyMissing message will be added
46     TYPE_ERROR = 0x2 ///< PropertyValueTypeError message will be added
47 };
48 
49 /**
50  * @brief Wrapper function for extracting string from JSON object without
51  *        throwing exceptions
52  *
53  * @param[in]  fieldName   Name of requested field
54  * @param[in]  json        JSON object from which field should be extracted
55  * @param[out] output      Variable to which extracted will be written in case
56  *                         of success
57  *
58  * @return Result informing about operation status, output will be
59  *         written only in case of Result::SUCCESS
60  */
61 Result getString(const char* fieldName, const nlohmann::json& json,
62                  const std::string*& output);
63 
64 /**
65  * @brief Wrapper function for extracting object from JSON object without
66  *        throwing exceptions
67  *
68  * @param[in]  fieldName   Name of requested field
69  * @param[in]  json        JSON object from which field should be extracted
70  * @param[out] output      Variable to which extracted will be written in case
71  *                         of success
72  *
73  * @return Result informing about operation status, output will be
74  *         written only in case of Result::SUCCESS
75  */
76 Result getObject(const char* fieldName, const nlohmann::json& json,
77                  nlohmann::json* output);
78 
79 /**
80  * @brief Wrapper function for extracting array from JSON object without
81  *        throwing exceptions
82  *
83  * @param[in]  fieldName   Name of requested field
84  * @param[in]  json        JSON object from which field should be extracted
85  * @param[out] output      Variable to which extracted will be written in case
86  *                         of success
87  *
88  * @return Result informing about operation status, output will be
89  *         written only in case of Result::SUCCESS
90  */
91 Result getArray(const char* fieldName, const nlohmann::json& json,
92                 nlohmann::json* output);
93 
94 /**
95  * @brief Wrapper function for extracting int from JSON object without
96  *        throwing exceptions
97  *
98  * @param[in]  fieldName   Name of requested field
99  * @param[in]  json        JSON object from which field should be extracted
100  * @param[out] output      Variable to which extracted will be written in case
101  *                         of success
102  *
103  * @return Result informing about operation status, output will be
104  *         written only in case of Result::SUCCESS
105  */
106 Result getInt(const char* fieldName, const nlohmann::json& json,
107               int64_t& output);
108 
109 /**
110  * @brief Wrapper function for extracting uint from JSON object without
111  *        throwing exceptions
112  *
113  * @param[in]  fieldName   Name of requested field
114  * @param[in]  json        JSON object from which field should be extracted
115  * @param[out] output      Variable to which extracted will be written in case
116  *                         of success
117  *
118  * @return Result informing about operation status, output will be
119  *         written only in case of Result::SUCCESS
120  */
121 Result getUnsigned(const char* fieldName, const nlohmann::json& json,
122                    uint64_t& output);
123 
124 /**
125  * @brief Wrapper function for extracting bool from JSON object without
126  *        throwing exceptions
127  *
128  * @param[in]  fieldName   Name of requested field
129  * @param[in]  json        JSON object from which field should be extracted
130  * @param[out] output      Variable to which extracted will be written in case
131  *                         of success
132  *
133  * @return Result informing about operation status, output will be
134  *         written only in case of Result::SUCCESS
135  */
136 Result getBool(const char* fieldName, const nlohmann::json& json, bool& output);
137 
138 /**
139  * @brief Wrapper function for extracting float from JSON object without
140  *        throwing exceptions (nlohmann stores JSON floats as C++ doubles)
141  *
142  * @param[in]  fieldName   Name of requested field
143  * @param[in]  json        JSON object from which field should be extracted
144  * @param[out] output      Variable to which extracted will be written in case
145  *                         of success
146  *
147  * @return Result informing about operation status, output will be
148  *         written only in case of Result::SUCCESS
149  */
150 Result getDouble(const char* fieldName, const nlohmann::json& json,
151                  double& output);
152 
153 /**
154  * @brief Wrapper function for extracting string from JSON object without
155  *        throwing exceptions
156  *
157  * @param[in]  fieldName   Name of requested field
158  * @param[in]  json        JSON object from which field should be extracted
159  * @param[out] output      Variable to which extracted will be written in case
160  *                         of success
161  * @param[in]  msgCfgMap   Map for message addition settings
162  * @param[out] msgJson     JSON to which error messages will be added
163  * @param[in]  fieldPath   Field path in JSON
164  *
165  * @return Result informing about operation status, output will be
166  *         written only in case of Result::SUCCESS
167  */
168 Result getString(const char* fieldName, const nlohmann::json& json,
169                  const std::string*& output, uint8_t msgCfgMap,
170                  nlohmann::json& msgJson, const std::string&& fieldPath);
171 
172 /**
173  * @brief Wrapper function for extracting object from JSON object without
174  *        throwing exceptions
175  *
176  * @param[in]  fieldName   Name of requested field
177  * @param[in]  json        JSON object from which field should be extracted
178  * @param[out] output      Variable to which extracted will be written in case
179  *                         of success
180  * @param[in]  msgCfgMap   Map for message addition settings
181  * @param[out] msgJson     JSON to which error messages will be added
182  * @param[in]  fieldPath   Field path in JSON
183  *
184  * @return Result informing about operation status, output will be
185  *         written only in case of Result::SUCCESS
186  */
187 Result getObject(const char* fieldName, const nlohmann::json& json,
188                  nlohmann::json* output, uint8_t msgCfgMap,
189                  nlohmann::json& msgJson, const std::string&& fieldPath);
190 
191 /**
192  * @brief Wrapper function for extracting array from JSON object without
193  *        throwing exceptions
194  *
195  * @param[in]  fieldName   Name of requested field
196  * @param[in]  json        JSON object from which field should be extracted
197  * @param[out] output      Variable to which extracted will be written in case
198  *                         of success
199  * @param[in]  msgCfgMap   Map for message addition settings
200  * @param[out] msgJson     JSON to which error messages will be added
201  * @param[in]  fieldPath   Field path in JSON
202  *
203  * @return Result informing about operation status, output will be
204  *         written only in case of Result::SUCCESS
205  */
206 Result getArray(const char* fieldName, const nlohmann::json& json,
207                 nlohmann::json* output, uint8_t msgCfgMap,
208                 nlohmann::json& msgJson, const std::string&& fieldPath);
209 
210 /**
211  * @brief Wrapper function for extracting int from JSON object without
212  *        throwing exceptions
213  *
214  * @param[in]  fieldName   Name of requested field
215  * @param[in]  json        JSON object from which field should be extracted
216  * @param[out] output      Variable to which extracted will be written in case
217  *                         of success
218  * @param[in]  msgCfgMap   Map for message addition settings
219  * @param[out] msgJson     JSON to which error messages will be added
220  * @param[in]  fieldPath   Field path in JSON
221  *
222  * @return Result informing about operation status, output will be
223  *         written only in case of Result::SUCCESS
224  */
225 Result getInt(const char* fieldName, const nlohmann::json& json,
226               int64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
227               const std::string&& fieldPath);
228 
229 /**
230  * @brief Wrapper function for extracting uint from JSON object without
231  *        throwing exceptions
232  *
233  * @param[in]  fieldName   Name of requested field
234  * @param[in]  json        JSON object from which field should be extracted
235  * @param[out] output      Variable to which extracted will be written in case
236  *                         of success
237  * @param[in]  msgCfgMap   Map for message addition settings
238  * @param[out] msgJson     JSON to which error messages will be added
239  * @param[in]  fieldPath   Field path in JSON
240  *
241  * @return Result informing about operation status, output will be
242  *         written only in case of Result::SUCCESS
243  */
244 Result getUnsigned(const char* fieldName, const nlohmann::json& json,
245                    uint64_t& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
246                    const std::string&& fieldPath);
247 
248 /**
249  * @brief Wrapper function for extracting bool from JSON object without
250  *        throwing exceptions
251  *
252  * @param[in]  fieldName   Name of requested field
253  * @param[in]  json        JSON object from which field should be extracted
254  * @param[out] output      Variable to which extracted will be written in case
255  *                         of success
256  * @param[in]  msgCfgMap   Map for message addition settings
257  * @param[out] msgJson     JSON to which error messages will be added
258  * @param[in]  fieldPath   Field path in JSON
259  *
260  * @return Result informing about operation status, output will be
261  *         written only in case of Result::SUCCESS
262  */
263 Result getBool(const char* fieldName, const nlohmann::json& json, bool& output,
264                uint8_t msgCfgMap, nlohmann::json& msgJson,
265                const std::string&& fieldPath);
266 
267 /**
268  * @brief Wrapper function for extracting float from JSON object without
269  *        throwing exceptions (nlohmann stores JSON floats as C++ doubles)
270  *
271  * @param[in]  fieldName   Name of requested field
272  * @param[in]  json        JSON object from which field should be extracted
273  * @param[out] output      Variable to which extracted will be written in case
274  * of success
275  * @param[in]  msgCfgMap   Map for message addition settings
276  * @param[out] msgJson     JSON to which error messages will be added
277  * @param[in]  fieldPath   Field path in JSON
278  *
279  * @return Result informing about operation status, output will be
280  *         written only in case of Result::SUCCESS
281  */
282 Result getDouble(const char* fieldName, const nlohmann::json& json,
283                  double& output, uint8_t msgCfgMap, nlohmann::json& msgJson,
284                  const std::string&& fieldPath);
285 
286 /**
287  * @brief Processes request to extract JSON from its body. If it fails, adds
288  *       MalformedJSON message to response and ends it.
289  *
290  * @param[io]  res       Response object
291  * @param[in]  req       Request object
292  * @param[out] reqJson   JSON object extracted from request's body
293  *
294  * @return true if JSON is valid, false when JSON is invalid and response has
295  *         been filled with message and ended.
296  */
297 bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
298                             nlohmann::json& reqJson);
299 
300 } // namespace json_util
301 
302 } // namespace redfish
303