blob: 65bea03318b0211b96ff3d2e3d58b58b2a5884d2 [file] [log] [blame]
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001/*
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
18#include "node.hpp"
19
James Feist5b4aa862018-08-16 14:07:01 -070020#include <boost/algorithm/string/replace.hpp>
Santosh Puranikaf5d60582019-03-20 18:16:36 +053021#include <boost/date_time.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070022#include <dbus_utility.hpp>
Santosh Puranikaf5d60582019-03-20 18:16:36 +053023#include <sstream>
Bernard Wong7bffdb72019-03-20 16:17:21 +080024#include <utils/systemd_utils.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080025#include <variant>
James Feist5b4aa862018-08-16 14:07:01 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Jennifer Leeed5befb2018-08-10 11:29:45 -070029
30/**
31 * ManagerActionsReset class supports handle POST method for Reset action.
32 * The class retrieves and sends data directly to dbus.
33 */
34class ManagerActionsReset : public Node
35{
36 public:
37 ManagerActionsReset(CrowApp& app) :
38 Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/")
39 {
40 entityPrivileges = {
41 {boost::beast::http::verb::get, {{"Login"}}},
42 {boost::beast::http::verb::head, {{"Login"}}},
43 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
44 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
45 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
46 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
47 }
48
49 private:
50 /**
Jennifer Leeed5befb2018-08-10 11:29:45 -070051 * Function handles POST method request.
52 * Analyzes POST body message before sends Reset request data to dbus.
53 * OpenBMC allows for ResetType is GracefulRestart only.
54 */
55 void doPost(crow::Response& res, const crow::Request& req,
56 const std::vector<std::string>& params) override
57 {
58 std::string resetType;
59
60 if (!json_util::readJson(req, res, "ResetType", resetType))
61 {
62 return;
63 }
64
65 if (resetType != "GracefulRestart")
66 {
67 res.result(boost::beast::http::status::bad_request);
68 messages::actionParameterNotSupported(res, resetType, "ResetType");
69 BMCWEB_LOG_ERROR << "Request incorrect action parameter: "
70 << resetType;
71 res.end();
72 return;
73 }
74 doBMCGracefulRestart(res, req, params);
75 }
76
77 /**
78 * Function transceives data with dbus directly.
79 * All BMC state properties will be retrieved before sending reset request.
80 */
81 void doBMCGracefulRestart(crow::Response& res, const crow::Request& req,
82 const std::vector<std::string>& params)
83 {
84 const char* processName = "xyz.openbmc_project.State.BMC";
85 const char* objectPath = "/xyz/openbmc_project/state/bmc0";
86 const char* interfaceName = "xyz.openbmc_project.State.BMC";
87 const std::string& propertyValue =
88 "xyz.openbmc_project.State.BMC.Transition.Reboot";
89 const char* destProperty = "RequestedBMCTransition";
90
91 // Create the D-Bus variant for D-Bus call.
92 VariantType dbusPropertyValue(propertyValue);
93
94 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
95
96 crow::connections::systemBus->async_method_call(
97 [asyncResp](const boost::system::error_code ec) {
98 // Use "Set" method to set the property value.
99 if (ec)
100 {
101 BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec;
102 messages::internalError(asyncResp->res);
103 return;
104 }
105
106 messages::success(asyncResp->res);
107 },
108 processName, objectPath, "org.freedesktop.DBus.Properties", "Set",
109 interfaceName, destProperty, dbusPropertyValue);
110 }
111};
112
James Feist5b4aa862018-08-16 14:07:01 -0700113static constexpr const char* objectManagerIface =
114 "org.freedesktop.DBus.ObjectManager";
115static constexpr const char* pidConfigurationIface =
116 "xyz.openbmc_project.Configuration.Pid";
117static constexpr const char* pidZoneConfigurationIface =
118 "xyz.openbmc_project.Configuration.Pid.Zone";
James Feistb7a08d02018-12-11 14:55:37 -0800119static constexpr const char* stepwiseConfigurationIface =
120 "xyz.openbmc_project.Configuration.Stepwise";
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100121
James Feist5b4aa862018-08-16 14:07:01 -0700122static void asyncPopulatePid(const std::string& connection,
123 const std::string& path,
124 std::shared_ptr<AsyncResp> asyncResp)
125{
126
127 crow::connections::systemBus->async_method_call(
128 [asyncResp](const boost::system::error_code ec,
129 const dbus::utility::ManagedObjectType& managedObj) {
130 if (ec)
131 {
132 BMCWEB_LOG_ERROR << ec;
James Feist5b4aa862018-08-16 14:07:01 -0700133 asyncResp->res.jsonValue.clear();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700134 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700135 return;
136 }
137 nlohmann::json& configRoot =
138 asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"];
139 nlohmann::json& fans = configRoot["FanControllers"];
140 fans["@odata.type"] = "#OemManager.FanControllers";
141 fans["@odata.context"] =
142 "/redfish/v1/$metadata#OemManager.FanControllers";
143 fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/"
144 "Fan/FanControllers";
145
146 nlohmann::json& pids = configRoot["PidControllers"];
147 pids["@odata.type"] = "#OemManager.PidControllers";
148 pids["@odata.context"] =
149 "/redfish/v1/$metadata#OemManager.PidControllers";
150 pids["@odata.id"] =
151 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers";
152
James Feistb7a08d02018-12-11 14:55:37 -0800153 nlohmann::json& stepwise = configRoot["StepwiseControllers"];
154 stepwise["@odata.type"] = "#OemManager.StepwiseControllers";
155 stepwise["@odata.context"] =
156 "/redfish/v1/$metadata#OemManager.StepwiseControllers";
157 stepwise["@odata.id"] =
158 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers";
159
James Feist5b4aa862018-08-16 14:07:01 -0700160 nlohmann::json& zones = configRoot["FanZones"];
161 zones["@odata.id"] =
162 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones";
163 zones["@odata.type"] = "#OemManager.FanZones";
164 zones["@odata.context"] =
165 "/redfish/v1/$metadata#OemManager.FanZones";
166 configRoot["@odata.id"] =
167 "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan";
168 configRoot["@odata.type"] = "#OemManager.Fan";
169 configRoot["@odata.context"] =
170 "/redfish/v1/$metadata#OemManager.Fan";
171
James Feist5b4aa862018-08-16 14:07:01 -0700172 for (const auto& pathPair : managedObj)
173 {
174 for (const auto& intfPair : pathPair.second)
175 {
176 if (intfPair.first != pidConfigurationIface &&
James Feistb7a08d02018-12-11 14:55:37 -0800177 intfPair.first != pidZoneConfigurationIface &&
178 intfPair.first != stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700179 {
180 continue;
181 }
182 auto findName = intfPair.second.find("Name");
183 if (findName == intfPair.second.end())
184 {
185 BMCWEB_LOG_ERROR << "Pid Field missing Name";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800186 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700187 return;
188 }
189 const std::string* namePtr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800190 std::get_if<std::string>(&findName->second);
James Feist5b4aa862018-08-16 14:07:01 -0700191 if (namePtr == nullptr)
192 {
193 BMCWEB_LOG_ERROR << "Pid Name Field illegal";
James Feistb7a08d02018-12-11 14:55:37 -0800194 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700195 return;
196 }
197
198 std::string name = *namePtr;
199 dbus::utility::escapePathForDbus(name);
James Feistb7a08d02018-12-11 14:55:37 -0800200 nlohmann::json* config = nullptr;
James Feistc33a90e2019-03-01 10:17:44 -0800201
202 const std::string* classPtr = nullptr;
203 auto findClass = intfPair.second.find("Class");
204 if (findClass != intfPair.second.end())
205 {
206 classPtr = std::get_if<std::string>(&findClass->second);
207 }
208
James Feist5b4aa862018-08-16 14:07:01 -0700209 if (intfPair.first == pidZoneConfigurationIface)
210 {
211 std::string chassis;
212 if (!dbus::utility::getNthStringFromPath(
213 pathPair.first.str, 5, chassis))
214 {
215 chassis = "#IllegalValue";
216 }
217 nlohmann::json& zone = zones[name];
218 zone["Chassis"] = {
219 {"@odata.id", "/redfish/v1/Chassis/" + chassis}};
220 zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/"
221 "OpenBmc/Fan/FanZones/" +
222 name;
223 zone["@odata.type"] = "#OemManager.FanZone";
224 zone["@odata.context"] =
225 "/redfish/v1/$metadata#OemManager.FanZone";
James Feistb7a08d02018-12-11 14:55:37 -0800226 config = &zone;
James Feist5b4aa862018-08-16 14:07:01 -0700227 }
228
James Feistb7a08d02018-12-11 14:55:37 -0800229 else if (intfPair.first == stepwiseConfigurationIface)
230 {
James Feistc33a90e2019-03-01 10:17:44 -0800231 if (classPtr == nullptr)
232 {
233 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
234 messages::internalError(asyncResp->res);
235 return;
236 }
237
James Feistb7a08d02018-12-11 14:55:37 -0800238 nlohmann::json& controller = stepwise[name];
239 config = &controller;
240
241 controller["@odata.id"] =
242 "/redfish/v1/Managers/bmc#/Oem/"
243 "OpenBmc/Fan/StepwiseControllers/" +
244 std::string(name);
245 controller["@odata.type"] =
246 "#OemManager.StepwiseController";
247
248 controller["@odata.context"] =
249 "/redfish/v1/"
250 "$metadata#OemManager.StepwiseController";
James Feistc33a90e2019-03-01 10:17:44 -0800251 controller["Direction"] = *classPtr;
James Feistb7a08d02018-12-11 14:55:37 -0800252 }
253
254 // pid and fans are off the same configuration
255 else if (intfPair.first == pidConfigurationIface)
256 {
James Feistc33a90e2019-03-01 10:17:44 -0800257
James Feistb7a08d02018-12-11 14:55:37 -0800258 if (classPtr == nullptr)
259 {
260 BMCWEB_LOG_ERROR << "Pid Class Field illegal";
261 messages::internalError(asyncResp->res);
262 return;
263 }
264 bool isFan = *classPtr == "fan";
265 nlohmann::json& element =
266 isFan ? fans[name] : pids[name];
267 config = &element;
268 if (isFan)
269 {
270 element["@odata.id"] =
271 "/redfish/v1/Managers/bmc#/Oem/"
272 "OpenBmc/Fan/FanControllers/" +
273 std::string(name);
274 element["@odata.type"] =
275 "#OemManager.FanController";
276
277 element["@odata.context"] =
278 "/redfish/v1/"
279 "$metadata#OemManager.FanController";
280 }
281 else
282 {
283 element["@odata.id"] =
284 "/redfish/v1/Managers/bmc#/Oem/"
285 "OpenBmc/Fan/PidControllers/" +
286 std::string(name);
287 element["@odata.type"] =
288 "#OemManager.PidController";
289 element["@odata.context"] =
290 "/redfish/v1/$metadata"
291 "#OemManager.PidController";
292 }
293 }
294 else
295 {
296 BMCWEB_LOG_ERROR << "Unexpected configuration";
297 messages::internalError(asyncResp->res);
298 return;
299 }
300
301 // used for making maps out of 2 vectors
302 const std::vector<double>* keys = nullptr;
303 const std::vector<double>* values = nullptr;
304
James Feist5b4aa862018-08-16 14:07:01 -0700305 for (const auto& propertyPair : intfPair.second)
306 {
307 if (propertyPair.first == "Type" ||
308 propertyPair.first == "Class" ||
309 propertyPair.first == "Name")
310 {
311 continue;
312 }
313
314 // zones
315 if (intfPair.first == pidZoneConfigurationIface)
316 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800317 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800318 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700319 if (ptr == nullptr)
320 {
321 BMCWEB_LOG_ERROR << "Field Illegal "
322 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700323 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700324 return;
325 }
James Feistb7a08d02018-12-11 14:55:37 -0800326 (*config)[propertyPair.first] = *ptr;
327 }
328
329 if (intfPair.first == stepwiseConfigurationIface)
330 {
331 if (propertyPair.first == "Reading" ||
332 propertyPair.first == "Output")
333 {
334 const std::vector<double>* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800335 std::get_if<std::vector<double>>(
James Feistb7a08d02018-12-11 14:55:37 -0800336 &propertyPair.second);
337
338 if (ptr == nullptr)
339 {
340 BMCWEB_LOG_ERROR << "Field Illegal "
341 << propertyPair.first;
342 messages::internalError(asyncResp->res);
343 return;
344 }
345
346 if (propertyPair.first == "Reading")
347 {
348 keys = ptr;
349 }
350 else
351 {
352 values = ptr;
353 }
354 if (keys && values)
355 {
356 if (keys->size() != values->size())
357 {
358 BMCWEB_LOG_ERROR
359 << "Reading and Output size don't "
360 "match ";
361 messages::internalError(asyncResp->res);
362 return;
363 }
364 nlohmann::json& steps = (*config)["Steps"];
365 steps = nlohmann::json::array();
366 for (size_t ii = 0; ii < keys->size(); ii++)
367 {
368 steps.push_back(
369 {{"Target", (*keys)[ii]},
370 {"Output", (*values)[ii]}});
371 }
372 }
373 }
374 if (propertyPair.first == "NegativeHysteresis" ||
375 propertyPair.first == "PositiveHysteresis")
376 {
377 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800378 std::get_if<double>(&propertyPair.second);
James Feistb7a08d02018-12-11 14:55:37 -0800379 if (ptr == nullptr)
380 {
381 BMCWEB_LOG_ERROR << "Field Illegal "
382 << propertyPair.first;
383 messages::internalError(asyncResp->res);
384 return;
385 }
386 (*config)[propertyPair.first] = *ptr;
387 }
James Feist5b4aa862018-08-16 14:07:01 -0700388 }
389
390 // pid and fans are off the same configuration
James Feistb7a08d02018-12-11 14:55:37 -0800391 if (intfPair.first == pidConfigurationIface ||
392 intfPair.first == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700393 {
James Feist5b4aa862018-08-16 14:07:01 -0700394
395 if (propertyPair.first == "Zones")
396 {
397 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800398 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800399 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700400
401 if (inputs == nullptr)
402 {
403 BMCWEB_LOG_ERROR
404 << "Zones Pid Field Illegal";
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800405 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700406 return;
407 }
James Feistb7a08d02018-12-11 14:55:37 -0800408 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700409 data = nlohmann::json::array();
410 for (std::string itemCopy : *inputs)
411 {
412 dbus::utility::escapePathForDbus(itemCopy);
413 data.push_back(
414 {{"@odata.id",
415 "/redfish/v1/Managers/bmc#/Oem/"
416 "OpenBmc/Fan/FanZones/" +
417 itemCopy}});
418 }
419 }
420 // todo(james): may never happen, but this
421 // assumes configuration data referenced in the
422 // PID config is provided by the same daemon, we
423 // could add another loop to cover all cases,
424 // but I'm okay kicking this can down the road a
425 // bit
426
427 else if (propertyPair.first == "Inputs" ||
428 propertyPair.first == "Outputs")
429 {
James Feistb7a08d02018-12-11 14:55:37 -0800430 auto& data = (*config)[propertyPair.first];
James Feist5b4aa862018-08-16 14:07:01 -0700431 const std::vector<std::string>* inputs =
Ed Tanousabf2add2019-01-22 16:40:12 -0800432 std::get_if<std::vector<std::string>>(
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800433 &propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700434
435 if (inputs == nullptr)
436 {
437 BMCWEB_LOG_ERROR << "Field Illegal "
438 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700439 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700440 return;
441 }
442 data = *inputs;
443 } // doubles
444 else if (propertyPair.first ==
445 "FFGainCoefficient" ||
446 propertyPair.first == "FFOffCoefficient" ||
447 propertyPair.first == "ICoefficient" ||
448 propertyPair.first == "ILimitMax" ||
449 propertyPair.first == "ILimitMin" ||
James Feistaad1a252019-02-19 10:13:52 -0800450 propertyPair.first ==
451 "PositiveHysteresis" ||
452 propertyPair.first ==
453 "NegativeHysteresis" ||
James Feist5b4aa862018-08-16 14:07:01 -0700454 propertyPair.first == "OutLimitMax" ||
455 propertyPair.first == "OutLimitMin" ||
456 propertyPair.first == "PCoefficient" ||
James Feist7625cb82019-01-23 11:58:21 -0800457 propertyPair.first == "SetPoint" ||
James Feist5b4aa862018-08-16 14:07:01 -0700458 propertyPair.first == "SlewNeg" ||
459 propertyPair.first == "SlewPos")
460 {
461 const double* ptr =
Ed Tanousabf2add2019-01-22 16:40:12 -0800462 std::get_if<double>(&propertyPair.second);
James Feist5b4aa862018-08-16 14:07:01 -0700463 if (ptr == nullptr)
464 {
465 BMCWEB_LOG_ERROR << "Field Illegal "
466 << propertyPair.first;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700467 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700468 return;
469 }
James Feistb7a08d02018-12-11 14:55:37 -0800470 (*config)[propertyPair.first] = *ptr;
James Feist5b4aa862018-08-16 14:07:01 -0700471 }
472 }
473 }
474 }
475 }
476 },
477 connection, path, objectManagerIface, "GetManagedObjects");
478}
Jennifer Leeca537922018-08-10 10:07:30 -0700479
James Feist83ff9ab2018-08-31 10:18:24 -0700480enum class CreatePIDRet
481{
482 fail,
483 del,
484 patch
485};
486
James Feist5f2caae2018-12-12 14:08:25 -0800487static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response,
488 std::vector<nlohmann::json>& config,
489 std::vector<std::string>& zones)
490{
James Feistb6baeaa2019-02-21 10:41:40 -0800491 if (config.empty())
492 {
493 BMCWEB_LOG_ERROR << "Empty Zones";
494 messages::propertyValueFormatError(response->res,
495 nlohmann::json::array(), "Zones");
496 return false;
497 }
James Feist5f2caae2018-12-12 14:08:25 -0800498 for (auto& odata : config)
499 {
500 std::string path;
501 if (!redfish::json_util::readJson(odata, response->res, "@odata.id",
502 path))
503 {
504 return false;
505 }
506 std::string input;
James Feist61adbda2019-03-25 13:03:51 -0700507
508 // 8 below comes from
509 // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left
510 // 0 1 2 3 4 5 6 7 8
511 if (!dbus::utility::getNthStringFromPath(path, 8, input))
James Feist5f2caae2018-12-12 14:08:25 -0800512 {
513 BMCWEB_LOG_ERROR << "Got invalid path " << path;
514 BMCWEB_LOG_ERROR << "Illegal Type Zones";
515 messages::propertyValueFormatError(response->res, odata.dump(),
516 "Zones");
517 return false;
518 }
519 boost::replace_all(input, "_", " ");
520 zones.emplace_back(std::move(input));
521 }
522 return true;
523}
524
James Feistb6baeaa2019-02-21 10:41:40 -0800525static bool findChassis(const dbus::utility::ManagedObjectType& managedObj,
526 const std::string& value, std::string& chassis)
527{
528 BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n";
529
530 std::string escaped = boost::replace_all_copy(value, " ", "_");
531 escaped = "/" + escaped;
532 auto it = std::find_if(
533 managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) {
534 if (boost::algorithm::ends_with(obj.first.str, escaped))
535 {
536 BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n";
537 return true;
538 }
539 return false;
540 });
541
542 if (it == managedObj.end())
543 {
544 return false;
545 }
546 // 5 comes from <chassis-name> being the 5th element
547 // /xyz/openbmc_project/inventory/system/chassis/<chassis-name>
548 return dbus::utility::getNthStringFromPath(it->first.str, 5, chassis);
549}
550
James Feist83ff9ab2018-08-31 10:18:24 -0700551static CreatePIDRet createPidInterface(
552 const std::shared_ptr<AsyncResp>& response, const std::string& type,
James Feistb6baeaa2019-02-21 10:41:40 -0800553 nlohmann::json::iterator it, const std::string& path,
James Feist83ff9ab2018-08-31 10:18:24 -0700554 const dbus::utility::ManagedObjectType& managedObj, bool createNewObject,
555 boost::container::flat_map<std::string, dbus::utility::DbusVariantType>&
556 output,
557 std::string& chassis)
558{
559
James Feist5f2caae2018-12-12 14:08:25 -0800560 // common deleter
James Feistb6baeaa2019-02-21 10:41:40 -0800561 if (it.value() == nullptr)
James Feist5f2caae2018-12-12 14:08:25 -0800562 {
563 std::string iface;
564 if (type == "PidControllers" || type == "FanControllers")
565 {
566 iface = pidConfigurationIface;
567 }
568 else if (type == "FanZones")
569 {
570 iface = pidZoneConfigurationIface;
571 }
572 else if (type == "StepwiseControllers")
573 {
574 iface = stepwiseConfigurationIface;
575 }
576 else
577 {
578 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type "
579 << type;
580 messages::propertyUnknown(response->res, type);
581 return CreatePIDRet::fail;
582 }
583 // delete interface
584 crow::connections::systemBus->async_method_call(
585 [response, path](const boost::system::error_code ec) {
586 if (ec)
587 {
588 BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec;
589 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -0800590 return;
James Feist5f2caae2018-12-12 14:08:25 -0800591 }
James Feistb6baeaa2019-02-21 10:41:40 -0800592 messages::success(response->res);
James Feist5f2caae2018-12-12 14:08:25 -0800593 },
594 "xyz.openbmc_project.EntityManager", path, iface, "Delete");
595 return CreatePIDRet::del;
596 }
597
James Feistb6baeaa2019-02-21 10:41:40 -0800598 if (!createNewObject)
599 {
600 // if we aren't creating a new object, we should be able to find it on
601 // d-bus
602 if (!findChassis(managedObj, it.key(), chassis))
603 {
604 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
605 messages::invalidObject(response->res, it.key());
606 return CreatePIDRet::fail;
607 }
608 }
609
James Feist83ff9ab2018-08-31 10:18:24 -0700610 if (type == "PidControllers" || type == "FanControllers")
611 {
612 if (createNewObject)
613 {
614 output["Class"] = type == "PidControllers" ? std::string("temp")
615 : std::string("fan");
616 output["Type"] = std::string("Pid");
617 }
James Feist5f2caae2018-12-12 14:08:25 -0800618
619 std::optional<std::vector<nlohmann::json>> zones;
620 std::optional<std::vector<std::string>> inputs;
621 std::optional<std::vector<std::string>> outputs;
622 std::map<std::string, std::optional<double>> doubles;
623 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800624 it.value(), response->res, "Inputs", inputs, "Outputs", outputs,
James Feist5f2caae2018-12-12 14:08:25 -0800625 "Zones", zones, "FFGainCoefficient",
626 doubles["FFGainCoefficient"], "FFOffCoefficient",
627 doubles["FFOffCoefficient"], "ICoefficient",
628 doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"],
629 "ILimitMin", doubles["ILimitMin"], "OutLimitMax",
630 doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"],
631 "PCoefficient", doubles["PCoefficient"], "SetPoint",
632 doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos",
James Feistaad1a252019-02-19 10:13:52 -0800633 doubles["SlewPos"], "PositiveHysteresis",
634 doubles["PositiveHysteresis"], "NegativeHysteresis",
635 doubles["NegativeHysteresis"]))
James Feist83ff9ab2018-08-31 10:18:24 -0700636 {
James Feist5f2caae2018-12-12 14:08:25 -0800637 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
James Feistb6baeaa2019-02-21 10:41:40 -0800638 << it.value().dump();
James Feist5f2caae2018-12-12 14:08:25 -0800639 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700640 }
James Feist5f2caae2018-12-12 14:08:25 -0800641 if (zones)
James Feist83ff9ab2018-08-31 10:18:24 -0700642 {
James Feist5f2caae2018-12-12 14:08:25 -0800643 std::vector<std::string> zonesStr;
644 if (!getZonesFromJsonReq(response, *zones, zonesStr))
James Feist83ff9ab2018-08-31 10:18:24 -0700645 {
James Feist5f2caae2018-12-12 14:08:25 -0800646 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
647 return CreatePIDRet::fail;
James Feist83ff9ab2018-08-31 10:18:24 -0700648 }
James Feistb6baeaa2019-02-21 10:41:40 -0800649 if (chassis.empty() &&
650 !findChassis(managedObj, zonesStr[0], chassis))
651 {
652 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
653 messages::invalidObject(response->res, it.key());
654 return CreatePIDRet::fail;
655 }
656
James Feist5f2caae2018-12-12 14:08:25 -0800657 output["Zones"] = std::move(zonesStr);
658 }
659 if (inputs || outputs)
660 {
661 std::array<std::optional<std::vector<std::string>>*, 2> containers =
662 {&inputs, &outputs};
663 size_t index = 0;
664 for (const auto& containerPtr : containers)
James Feist83ff9ab2018-08-31 10:18:24 -0700665 {
James Feist5f2caae2018-12-12 14:08:25 -0800666 std::optional<std::vector<std::string>>& container =
667 *containerPtr;
668 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -0700669 {
James Feist5f2caae2018-12-12 14:08:25 -0800670 index++;
671 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700672 }
James Feist5f2caae2018-12-12 14:08:25 -0800673
674 for (std::string& value : *container)
James Feist83ff9ab2018-08-31 10:18:24 -0700675 {
James Feist5f2caae2018-12-12 14:08:25 -0800676 boost::replace_all(value, "_", " ");
James Feist83ff9ab2018-08-31 10:18:24 -0700677 }
James Feist5f2caae2018-12-12 14:08:25 -0800678 std::string key;
679 if (index == 0)
James Feist83ff9ab2018-08-31 10:18:24 -0700680 {
James Feist5f2caae2018-12-12 14:08:25 -0800681 key = "Inputs";
James Feist83ff9ab2018-08-31 10:18:24 -0700682 }
James Feist5f2caae2018-12-12 14:08:25 -0800683 else
684 {
685 key = "Outputs";
686 }
687 output[key] = *container;
688 index++;
James Feist83ff9ab2018-08-31 10:18:24 -0700689 }
James Feist5f2caae2018-12-12 14:08:25 -0800690 }
James Feist83ff9ab2018-08-31 10:18:24 -0700691
James Feist5f2caae2018-12-12 14:08:25 -0800692 // doubles
693 for (const auto& pairs : doubles)
694 {
695 if (!pairs.second)
James Feist83ff9ab2018-08-31 10:18:24 -0700696 {
James Feist5f2caae2018-12-12 14:08:25 -0800697 continue;
James Feist83ff9ab2018-08-31 10:18:24 -0700698 }
James Feist5f2caae2018-12-12 14:08:25 -0800699 BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second;
700 output[pairs.first] = *(pairs.second);
James Feist83ff9ab2018-08-31 10:18:24 -0700701 }
702 }
James Feist5f2caae2018-12-12 14:08:25 -0800703
James Feist83ff9ab2018-08-31 10:18:24 -0700704 else if (type == "FanZones")
705 {
James Feist83ff9ab2018-08-31 10:18:24 -0700706 output["Type"] = std::string("Pid.Zone");
707
James Feist5f2caae2018-12-12 14:08:25 -0800708 std::optional<nlohmann::json> chassisContainer;
709 std::optional<double> failSafePercent;
James Feistd3ec07f2019-02-25 14:51:15 -0800710 std::optional<double> minThermalOutput;
James Feistb6baeaa2019-02-21 10:41:40 -0800711 if (!redfish::json_util::readJson(it.value(), response->res, "Chassis",
James Feist5f2caae2018-12-12 14:08:25 -0800712 chassisContainer, "FailSafePercent",
James Feistd3ec07f2019-02-25 14:51:15 -0800713 failSafePercent, "MinThermalOutput",
714 minThermalOutput))
James Feist83ff9ab2018-08-31 10:18:24 -0700715 {
James Feist5f2caae2018-12-12 14:08:25 -0800716 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
James Feistb6baeaa2019-02-21 10:41:40 -0800717 << it.value().dump();
James Feist5f2caae2018-12-12 14:08:25 -0800718 return CreatePIDRet::fail;
719 }
James Feist83ff9ab2018-08-31 10:18:24 -0700720
James Feist5f2caae2018-12-12 14:08:25 -0800721 if (chassisContainer)
722 {
723
724 std::string chassisId;
725 if (!redfish::json_util::readJson(*chassisContainer, response->res,
726 "@odata.id", chassisId))
James Feist83ff9ab2018-08-31 10:18:24 -0700727 {
James Feist5f2caae2018-12-12 14:08:25 -0800728 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
729 << chassisContainer->dump();
James Feist83ff9ab2018-08-31 10:18:24 -0700730 return CreatePIDRet::fail;
731 }
James Feist5f2caae2018-12-12 14:08:25 -0800732
733 // /refish/v1/chassis/chassis_name/
734 if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis))
735 {
736 BMCWEB_LOG_ERROR << "Got invalid path " << chassisId;
737 messages::invalidObject(response->res, chassisId);
738 return CreatePIDRet::fail;
739 }
740 }
James Feistd3ec07f2019-02-25 14:51:15 -0800741 if (minThermalOutput)
James Feist5f2caae2018-12-12 14:08:25 -0800742 {
James Feistd3ec07f2019-02-25 14:51:15 -0800743 output["MinThermalOutput"] = *minThermalOutput;
James Feist5f2caae2018-12-12 14:08:25 -0800744 }
745 if (failSafePercent)
746 {
747 output["FailSafePercent"] = *failSafePercent;
748 }
749 }
750 else if (type == "StepwiseControllers")
751 {
752 output["Type"] = std::string("Stepwise");
753
754 std::optional<std::vector<nlohmann::json>> zones;
755 std::optional<std::vector<nlohmann::json>> steps;
756 std::optional<std::vector<std::string>> inputs;
757 std::optional<double> positiveHysteresis;
758 std::optional<double> negativeHysteresis;
James Feistc33a90e2019-03-01 10:17:44 -0800759 std::optional<std::string> direction; // upper clipping curve vs lower
James Feist5f2caae2018-12-12 14:08:25 -0800760 if (!redfish::json_util::readJson(
James Feistb6baeaa2019-02-21 10:41:40 -0800761 it.value(), response->res, "Zones", zones, "Steps", steps,
762 "Inputs", inputs, "PositiveHysteresis", positiveHysteresis,
James Feistc33a90e2019-03-01 10:17:44 -0800763 "NegativeHysteresis", negativeHysteresis, "Direction",
764 direction))
James Feist5f2caae2018-12-12 14:08:25 -0800765 {
766 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
James Feistb6baeaa2019-02-21 10:41:40 -0800767 << it.value().dump();
James Feist5f2caae2018-12-12 14:08:25 -0800768 return CreatePIDRet::fail;
769 }
770
771 if (zones)
772 {
James Feistb6baeaa2019-02-21 10:41:40 -0800773 std::vector<std::string> zonesStrs;
774 if (!getZonesFromJsonReq(response, *zones, zonesStrs))
James Feist5f2caae2018-12-12 14:08:25 -0800775 {
776 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones";
777 return CreatePIDRet::fail;
778 }
James Feistb6baeaa2019-02-21 10:41:40 -0800779 if (chassis.empty() &&
780 !findChassis(managedObj, zonesStrs[0], chassis))
781 {
782 BMCWEB_LOG_ERROR << "Failed to get chassis from config patch";
783 messages::invalidObject(response->res, it.key());
784 return CreatePIDRet::fail;
785 }
786 output["Zones"] = std::move(zonesStrs);
James Feist5f2caae2018-12-12 14:08:25 -0800787 }
788 if (steps)
789 {
790 std::vector<double> readings;
791 std::vector<double> outputs;
792 for (auto& step : *steps)
793 {
794 double target;
Ed Tanousb01bf292019-03-25 19:25:26 +0000795 double output;
James Feist5f2caae2018-12-12 14:08:25 -0800796
797 if (!redfish::json_util::readJson(step, response->res, "Target",
Ed Tanousb01bf292019-03-25 19:25:26 +0000798 target, "Output", output))
James Feist5f2caae2018-12-12 14:08:25 -0800799 {
800 BMCWEB_LOG_ERROR << "Line:" << __LINE__
James Feistb6baeaa2019-02-21 10:41:40 -0800801 << ", Illegal Property "
802 << it.value().dump();
James Feist5f2caae2018-12-12 14:08:25 -0800803 return CreatePIDRet::fail;
804 }
805 readings.emplace_back(target);
Ed Tanousb01bf292019-03-25 19:25:26 +0000806 outputs.emplace_back(output);
James Feist5f2caae2018-12-12 14:08:25 -0800807 }
808 output["Reading"] = std::move(readings);
809 output["Output"] = std::move(outputs);
810 }
811 if (inputs)
812 {
813 for (std::string& value : *inputs)
814 {
James Feist5f2caae2018-12-12 14:08:25 -0800815 boost::replace_all(value, "_", " ");
816 }
817 output["Inputs"] = std::move(*inputs);
818 }
819 if (negativeHysteresis)
820 {
821 output["NegativeHysteresis"] = *negativeHysteresis;
822 }
823 if (positiveHysteresis)
824 {
825 output["PositiveHysteresis"] = *positiveHysteresis;
James Feist83ff9ab2018-08-31 10:18:24 -0700826 }
James Feistc33a90e2019-03-01 10:17:44 -0800827 if (direction)
828 {
829 constexpr const std::array<const char*, 2> allowedDirections = {
830 "Ceiling", "Floor"};
831 if (std::find(allowedDirections.begin(), allowedDirections.end(),
832 *direction) == allowedDirections.end())
833 {
834 messages::propertyValueTypeError(response->res, "Direction",
835 *direction);
836 return CreatePIDRet::fail;
837 }
838 output["Class"] = *direction;
839 }
James Feist83ff9ab2018-08-31 10:18:24 -0700840 }
841 else
842 {
James Feist5f2caae2018-12-12 14:08:25 -0800843 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type;
Jason M. Bills35a62c72018-10-09 12:45:45 -0700844 messages::propertyUnknown(response->res, type);
James Feist83ff9ab2018-08-31 10:18:24 -0700845 return CreatePIDRet::fail;
846 }
847 return CreatePIDRet::patch;
848}
849
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850class Manager : public Node
851{
852 public:
James Feist5b4aa862018-08-16 14:07:01 -0700853 Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800855 uuid = app.template getMiddleware<crow::persistent_data::Middleware>()
856 .systemUuid;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 entityPrivileges = {
858 {boost::beast::http::verb::get, {{"Login"}}},
859 {boost::beast::http::verb::head, {{"Login"}}},
860 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
861 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
862 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
863 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
Borawski.Lukasz9c3106852018-02-09 15:24:22 +0100864 }
865
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 private:
James Feist5b4aa862018-08-16 14:07:01 -0700867 void getPidValues(std::shared_ptr<AsyncResp> asyncResp)
868 {
869 crow::connections::systemBus->async_method_call(
870 [asyncResp](const boost::system::error_code ec,
871 const crow::openbmc_mapper::GetSubTreeType& subtree) {
872 if (ec)
873 {
874 BMCWEB_LOG_ERROR << ec;
Jason M. Billsf12894f2018-10-09 12:45:45 -0700875 messages::internalError(asyncResp->res);
James Feist5b4aa862018-08-16 14:07:01 -0700876 return;
877 }
878
879 // create map of <connection, path to objMgr>>
880 boost::container::flat_map<std::string, std::string>
881 objectMgrPaths;
James Feist6bce33b2018-10-22 12:05:56 -0700882 boost::container::flat_set<std::string> calledConnections;
James Feist5b4aa862018-08-16 14:07:01 -0700883 for (const auto& pathGroup : subtree)
884 {
885 for (const auto& connectionGroup : pathGroup.second)
886 {
James Feist6bce33b2018-10-22 12:05:56 -0700887 auto findConnection =
888 calledConnections.find(connectionGroup.first);
889 if (findConnection != calledConnections.end())
890 {
891 break;
892 }
James Feist5b4aa862018-08-16 14:07:01 -0700893 for (const std::string& interface :
894 connectionGroup.second)
895 {
896 if (interface == objectManagerIface)
897 {
898 objectMgrPaths[connectionGroup.first] =
899 pathGroup.first;
900 }
901 // this list is alphabetical, so we
902 // should have found the objMgr by now
903 if (interface == pidConfigurationIface ||
James Feistb7a08d02018-12-11 14:55:37 -0800904 interface == pidZoneConfigurationIface ||
905 interface == stepwiseConfigurationIface)
James Feist5b4aa862018-08-16 14:07:01 -0700906 {
907 auto findObjMgr =
908 objectMgrPaths.find(connectionGroup.first);
909 if (findObjMgr == objectMgrPaths.end())
910 {
911 BMCWEB_LOG_DEBUG << connectionGroup.first
912 << "Has no Object Manager";
913 continue;
914 }
James Feist6bce33b2018-10-22 12:05:56 -0700915
916 calledConnections.insert(connectionGroup.first);
917
James Feist5b4aa862018-08-16 14:07:01 -0700918 asyncPopulatePid(findObjMgr->first,
919 findObjMgr->second, asyncResp);
920 break;
921 }
922 }
923 }
924 }
925 },
926 "xyz.openbmc_project.ObjectMapper",
927 "/xyz/openbmc_project/object_mapper",
928 "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
James Feistb7a08d02018-12-11 14:55:37 -0800929 std::array<const char*, 4>{
930 pidConfigurationIface, pidZoneConfigurationIface,
931 objectManagerIface, stepwiseConfigurationIface});
James Feist5b4aa862018-08-16 14:07:01 -0700932 }
933
934 void doGet(crow::Response& res, const crow::Request& req,
935 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800937 res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc";
938 res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager";
939 res.jsonValue["@odata.context"] =
940 "/redfish/v1/$metadata#Manager.Manager";
941 res.jsonValue["Id"] = "bmc";
942 res.jsonValue["Name"] = "OpenBmc Manager";
943 res.jsonValue["Description"] = "Baseboard Management Controller";
944 res.jsonValue["PowerState"] = "On";
Ed Tanous029573d2019-02-01 10:57:49 -0800945 res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}};
Ed Tanous0f74e642018-11-12 15:17:05 -0800946 res.jsonValue["ManagerType"] = "BMC";
Ed Tanous75176582018-12-14 08:14:34 -0800947 res.jsonValue["UUID"] = uuid;
Bernard Wong7bffdb72019-03-20 16:17:21 +0800948 res.jsonValue["ServiceEntryPointUUID"] = systemd_utils::getUuid();
Ed Tanous75176582018-12-14 08:14:34 -0800949 res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model
Ed Tanous0f74e642018-11-12 15:17:05 -0800950
951 res.jsonValue["LogServices"] = {
952 {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}};
953
954 res.jsonValue["NetworkProtocol"] = {
955 {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}};
956
957 res.jsonValue["EthernetInterfaces"] = {
958 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}};
959 // default oem data
960 nlohmann::json& oem = res.jsonValue["Oem"];
961 nlohmann::json& oemOpenbmc = oem["OpenBmc"];
962 oem["@odata.type"] = "#OemManager.Oem";
963 oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem";
964 oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem";
965 oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc";
966 oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc";
967 oemOpenbmc["@odata.context"] =
968 "/redfish/v1/$metadata#OemManager.OpenBmc";
969
Jennifer Leeed5befb2018-08-10 11:29:45 -0700970 // Update Actions object.
Ed Tanous0f74e642018-11-12 15:17:05 -0800971 nlohmann::json& manager_reset =
972 res.jsonValue["Actions"]["#Manager.Reset"];
Jennifer Leeed5befb2018-08-10 11:29:45 -0700973 manager_reset["target"] =
974 "/redfish/v1/Managers/bmc/Actions/Manager.Reset";
975 manager_reset["ResetType@Redfish.AllowableValues"] = {
976 "GracefulRestart"};
Jennifer Leeca537922018-08-10 10:07:30 -0700977
Andrew Geisslercb92c032018-08-17 07:56:14 -0700978 res.jsonValue["DateTime"] = crow::utility::dateTimeNow();
Santosh Puranik474bfad2019-04-02 16:00:09 +0530979
980 // Fill in GraphicalConsole and SerialConsole info
981 res.jsonValue["SerialConsole"]["ServiceEnabled"] = true;
982 res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI",
983 "SSH"};
984 // TODO (Santosh) : Uncomment when KVM support is in.
985 // res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true;
986 // res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] =
987 // {"KVMIP"};
988
Gunnar Mills603a6642019-01-21 17:03:51 -0600989 res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1;
990 res.jsonValue["Links"]["ManagerForServers"] = {
991 {{"@odata.id", "/redfish/v1/Systems/system"}}};
992#ifdef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
993 res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1;
994 res.jsonValue["Links"]["ManagerForChassis"] = {
995 {{"@odata.id", "/redfish/v1/Chassis/chassis"}}};
996#endif
Jennifer Leeed5befb2018-08-10 11:29:45 -0700997 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
James Feist5b4aa862018-08-16 14:07:01 -0700998
Jennifer Leeca537922018-08-10 10:07:30 -0700999 crow::connections::systemBus->async_method_call(
1000 [asyncResp](const boost::system::error_code ec,
James Feist5b4aa862018-08-16 14:07:01 -07001001 const dbus::utility::ManagedObjectType& resp) {
Jennifer Leeca537922018-08-10 10:07:30 -07001002 if (ec)
1003 {
1004 BMCWEB_LOG_ERROR << "Error while getting Software Version";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001005 messages::internalError(asyncResp->res);
Jennifer Leeca537922018-08-10 10:07:30 -07001006 return;
1007 }
1008
James Feist5b4aa862018-08-16 14:07:01 -07001009 for (auto& objpath : resp)
Jennifer Leeca537922018-08-10 10:07:30 -07001010 {
James Feist5b4aa862018-08-16 14:07:01 -07001011 for (auto& interface : objpath.second)
Jennifer Leeca537922018-08-10 10:07:30 -07001012 {
James Feist5f2caae2018-12-12 14:08:25 -08001013 // If interface is
1014 // xyz.openbmc_project.Software.Version, this is
1015 // what we're looking for.
Jennifer Leeca537922018-08-10 10:07:30 -07001016 if (interface.first ==
1017 "xyz.openbmc_project.Software.Version")
1018 {
1019 // Cut out everyting until last "/", ...
James Feist5b4aa862018-08-16 14:07:01 -07001020 for (auto& property : interface.second)
Jennifer Leeca537922018-08-10 10:07:30 -07001021 {
1022 if (property.first == "Version")
1023 {
James Feist5b4aa862018-08-16 14:07:01 -07001024 const std::string* value =
Ed Tanousabf2add2019-01-22 16:40:12 -08001025 std::get_if<std::string>(
1026 &property.second);
Jennifer Leeca537922018-08-10 10:07:30 -07001027 if (value == nullptr)
1028 {
1029 continue;
1030 }
1031 asyncResp->res
1032 .jsonValue["FirmwareVersion"] = *value;
1033 }
1034 }
1035 }
1036 }
1037 }
1038 },
1039 "xyz.openbmc_project.Software.BMC.Updater",
1040 "/xyz/openbmc_project/software",
1041 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
James Feist5b4aa862018-08-16 14:07:01 -07001042 getPidValues(asyncResp);
1043 }
James Feist5f2caae2018-12-12 14:08:25 -08001044 void setPidValues(std::shared_ptr<AsyncResp> response, nlohmann::json& data)
James Feist83ff9ab2018-08-31 10:18:24 -07001045 {
James Feist5f2caae2018-12-12 14:08:25 -08001046
James Feist83ff9ab2018-08-31 10:18:24 -07001047 // todo(james): might make sense to do a mapper call here if this
1048 // interface gets more traction
1049 crow::connections::systemBus->async_method_call(
1050 [response,
1051 data](const boost::system::error_code ec,
1052 const dbus::utility::ManagedObjectType& managedObj) {
1053 if (ec)
1054 {
1055 BMCWEB_LOG_ERROR << "Error communicating to Entity Manager";
Jason M. Bills35a62c72018-10-09 12:45:45 -07001056 messages::internalError(response->res);
James Feist83ff9ab2018-08-31 10:18:24 -07001057 return;
1058 }
James Feist5f2caae2018-12-12 14:08:25 -08001059
1060 // todo(james) mutable doesn't work with asio bindings
1061 nlohmann::json jsonData = data;
1062
1063 std::optional<nlohmann::json> pidControllers;
1064 std::optional<nlohmann::json> fanControllers;
1065 std::optional<nlohmann::json> fanZones;
1066 std::optional<nlohmann::json> stepwiseControllers;
1067 if (!redfish::json_util::readJson(
1068 jsonData, response->res, "PidControllers",
1069 pidControllers, "FanControllers", fanControllers,
1070 "FanZones", fanZones, "StepwiseControllers",
1071 stepwiseControllers))
James Feist83ff9ab2018-08-31 10:18:24 -07001072 {
James Feist5f2caae2018-12-12 14:08:25 -08001073 BMCWEB_LOG_ERROR << "Line:" << __LINE__
1074 << ", Illegal Property "
1075 << jsonData.dump();
1076 return;
1077 }
1078 std::array<
Ed Tanous43b761d2019-02-13 20:10:56 -08001079 std::pair<std::string, std::optional<nlohmann::json>*>, 4>
James Feist5f2caae2018-12-12 14:08:25 -08001080 sections = {
1081 std::make_pair("PidControllers", &pidControllers),
1082 std::make_pair("FanControllers", &fanControllers),
1083 std::make_pair("FanZones", &fanZones),
1084 std::make_pair("StepwiseControllers",
1085 &stepwiseControllers)};
1086
1087 for (auto& containerPair : sections)
1088 {
1089 auto& container = *(containerPair.second);
1090 if (!container)
James Feist83ff9ab2018-08-31 10:18:24 -07001091 {
James Feist5f2caae2018-12-12 14:08:25 -08001092 continue;
James Feist83ff9ab2018-08-31 10:18:24 -07001093 }
Ed Tanous43b761d2019-02-13 20:10:56 -08001094 std::string& type = containerPair.first;
James Feist5f2caae2018-12-12 14:08:25 -08001095
James Feistb6baeaa2019-02-21 10:41:40 -08001096 for (nlohmann::json::iterator it = container->begin();
1097 it != container->end(); it++)
James Feist83ff9ab2018-08-31 10:18:24 -07001098 {
James Feistb6baeaa2019-02-21 10:41:40 -08001099 const auto& name = it.key();
James Feist83ff9ab2018-08-31 10:18:24 -07001100 auto pathItr =
1101 std::find_if(managedObj.begin(), managedObj.end(),
1102 [&name](const auto& obj) {
1103 return boost::algorithm::ends_with(
James Feistb6baeaa2019-02-21 10:41:40 -08001104 obj.first.str, "/" + name);
James Feist83ff9ab2018-08-31 10:18:24 -07001105 });
1106 boost::container::flat_map<
1107 std::string, dbus::utility::DbusVariantType>
1108 output;
1109
1110 output.reserve(16); // The pid interface length
1111
1112 // determines if we're patching entity-manager or
1113 // creating a new object
1114 bool createNewObject = (pathItr == managedObj.end());
James Feist5f2caae2018-12-12 14:08:25 -08001115 std::string iface;
1116 if (type == "PidControllers" ||
1117 type == "FanControllers")
James Feist83ff9ab2018-08-31 10:18:24 -07001118 {
James Feist5f2caae2018-12-12 14:08:25 -08001119 iface = pidConfigurationIface;
James Feist83ff9ab2018-08-31 10:18:24 -07001120 if (!createNewObject &&
1121 pathItr->second.find(pidConfigurationIface) ==
1122 pathItr->second.end())
1123 {
1124 createNewObject = true;
1125 }
1126 }
James Feist5f2caae2018-12-12 14:08:25 -08001127 else if (type == "FanZones")
James Feist83ff9ab2018-08-31 10:18:24 -07001128 {
James Feist5f2caae2018-12-12 14:08:25 -08001129 iface = pidZoneConfigurationIface;
1130 if (!createNewObject &&
1131 pathItr->second.find(
1132 pidZoneConfigurationIface) ==
1133 pathItr->second.end())
1134 {
1135
1136 createNewObject = true;
1137 }
1138 }
1139 else if (type == "StepwiseControllers")
1140 {
1141 iface = stepwiseConfigurationIface;
1142 if (!createNewObject &&
1143 pathItr->second.find(
1144 stepwiseConfigurationIface) ==
1145 pathItr->second.end())
1146 {
1147 createNewObject = true;
1148 }
James Feist83ff9ab2018-08-31 10:18:24 -07001149 }
James Feistb6baeaa2019-02-21 10:41:40 -08001150 BMCWEB_LOG_DEBUG << "Create new = " << createNewObject
1151 << "\n";
James Feist83ff9ab2018-08-31 10:18:24 -07001152 output["Name"] =
1153 boost::replace_all_copy(name, "_", " ");
1154
1155 std::string chassis;
1156 CreatePIDRet ret = createPidInterface(
James Feistb6baeaa2019-02-21 10:41:40 -08001157 response, type, it, pathItr->first.str, managedObj,
1158 createNewObject, output, chassis);
James Feist83ff9ab2018-08-31 10:18:24 -07001159 if (ret == CreatePIDRet::fail)
1160 {
1161 return;
1162 }
1163 else if (ret == CreatePIDRet::del)
1164 {
1165 continue;
1166 }
1167
1168 if (!createNewObject)
1169 {
1170 for (const auto& property : output)
1171 {
James Feist83ff9ab2018-08-31 10:18:24 -07001172 crow::connections::systemBus->async_method_call(
1173 [response,
1174 propertyName{std::string(property.first)}](
1175 const boost::system::error_code ec) {
1176 if (ec)
1177 {
1178 BMCWEB_LOG_ERROR
1179 << "Error patching "
1180 << propertyName << ": " << ec;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001181 messages::internalError(
1182 response->res);
James Feistb6baeaa2019-02-21 10:41:40 -08001183 return;
James Feist83ff9ab2018-08-31 10:18:24 -07001184 }
James Feistb6baeaa2019-02-21 10:41:40 -08001185 messages::success(response->res);
James Feist83ff9ab2018-08-31 10:18:24 -07001186 },
1187 "xyz.openbmc_project.EntityManager",
1188 pathItr->first.str,
1189 "org.freedesktop.DBus.Properties", "Set",
James Feist5f2caae2018-12-12 14:08:25 -08001190 iface, property.first, property.second);
James Feist83ff9ab2018-08-31 10:18:24 -07001191 }
1192 }
1193 else
1194 {
1195 if (chassis.empty())
1196 {
1197 BMCWEB_LOG_ERROR
1198 << "Failed to get chassis from config";
Jason M. Bills35a62c72018-10-09 12:45:45 -07001199 messages::invalidObject(response->res, name);
James Feist83ff9ab2018-08-31 10:18:24 -07001200 return;
1201 }
1202
1203 bool foundChassis = false;
1204 for (const auto& obj : managedObj)
1205 {
1206 if (boost::algorithm::ends_with(obj.first.str,
1207 chassis))
1208 {
1209 chassis = obj.first.str;
1210 foundChassis = true;
1211 break;
1212 }
1213 }
1214 if (!foundChassis)
1215 {
1216 BMCWEB_LOG_ERROR
1217 << "Failed to find chassis on dbus";
Jason M. Bills35a62c72018-10-09 12:45:45 -07001218 messages::resourceMissingAtURI(
1219 response->res,
1220 "/redfish/v1/Chassis/" + chassis);
James Feist83ff9ab2018-08-31 10:18:24 -07001221 return;
1222 }
1223
1224 crow::connections::systemBus->async_method_call(
1225 [response](const boost::system::error_code ec) {
1226 if (ec)
1227 {
1228 BMCWEB_LOG_ERROR
1229 << "Error Adding Pid Object " << ec;
Jason M. Bills35a62c72018-10-09 12:45:45 -07001230 messages::internalError(response->res);
James Feistb6baeaa2019-02-21 10:41:40 -08001231 return;
James Feist83ff9ab2018-08-31 10:18:24 -07001232 }
James Feistb6baeaa2019-02-21 10:41:40 -08001233 messages::success(response->res);
James Feist83ff9ab2018-08-31 10:18:24 -07001234 },
1235 "xyz.openbmc_project.EntityManager", chassis,
1236 "xyz.openbmc_project.AddObject", "AddObject",
1237 output);
1238 }
1239 }
1240 }
1241 },
1242 "xyz.openbmc_project.EntityManager", "/", objectManagerIface,
1243 "GetManagedObjects");
1244 }
James Feist5b4aa862018-08-16 14:07:01 -07001245
1246 void doPatch(crow::Response& res, const crow::Request& req,
1247 const std::vector<std::string>& params) override
1248 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08001249 std::optional<nlohmann::json> oem;
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301250 std::optional<std::string> datetime;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001251
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301252 if (!json_util::readJson(req, res, "Oem", oem, "DateTime", datetime))
James Feist83ff9ab2018-08-31 10:18:24 -07001253 {
1254 return;
1255 }
Ed Tanous0627a2c2018-11-29 17:09:23 -08001256
James Feist83ff9ab2018-08-31 10:18:24 -07001257 std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res);
Ed Tanous0627a2c2018-11-29 17:09:23 -08001258
1259 if (oem)
James Feist83ff9ab2018-08-31 10:18:24 -07001260 {
Ed Tanous43b761d2019-02-13 20:10:56 -08001261 std::optional<nlohmann::json> openbmc;
1262 if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc))
James Feist83ff9ab2018-08-31 10:18:24 -07001263 {
Ed Tanous43b761d2019-02-13 20:10:56 -08001264 BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property "
1265 << oem->dump();
1266 return;
1267 }
1268 if (openbmc)
1269 {
1270 std::optional<nlohmann::json> fan;
1271 if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan))
James Feist83ff9ab2018-08-31 10:18:24 -07001272 {
James Feist5f2caae2018-12-12 14:08:25 -08001273 BMCWEB_LOG_ERROR << "Line:" << __LINE__
Ed Tanous43b761d2019-02-13 20:10:56 -08001274 << ", Illegal Property "
1275 << openbmc->dump();
James Feist5f2caae2018-12-12 14:08:25 -08001276 return;
1277 }
Ed Tanous43b761d2019-02-13 20:10:56 -08001278 if (fan)
James Feist5f2caae2018-12-12 14:08:25 -08001279 {
Ed Tanous43b761d2019-02-13 20:10:56 -08001280 setPidValues(response, *fan);
James Feist83ff9ab2018-08-31 10:18:24 -07001281 }
James Feist83ff9ab2018-08-31 10:18:24 -07001282 }
1283 }
Santosh Puranikaf5d60582019-03-20 18:16:36 +05301284 if (datetime)
1285 {
1286 setDateTime(response, std::move(*datetime));
1287 }
1288 }
1289
1290 void setDateTime(std::shared_ptr<AsyncResp> aResp,
1291 std::string datetime) const
1292 {
1293 BMCWEB_LOG_DEBUG << "Set date time: " << datetime;
1294
1295 std::stringstream stream(datetime);
1296 // Convert from ISO 8601 to boost local_time
1297 // (BMC only has time in UTC)
1298 boost::posix_time::ptime posixTime;
1299 boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1));
1300 // Facet gets deleted with the stringsteam
1301 auto ifc = std::make_unique<boost::local_time::local_time_input_facet>(
1302 "%Y-%m-%d %H:%M:%S%F %ZP");
1303 stream.imbue(std::locale(stream.getloc(), ifc.release()));
1304
1305 boost::local_time::local_date_time ldt(
1306 boost::local_time::not_a_date_time);
1307
1308 if (stream >> ldt)
1309 {
1310 posixTime = ldt.utc_time();
1311 boost::posix_time::time_duration dur = posixTime - epoch;
1312 uint64_t durMicroSecs =
1313 static_cast<uint64_t>(dur.total_microseconds());
1314 crow::connections::systemBus->async_method_call(
1315 [aResp{std::move(aResp)}, datetime{std::move(datetime)}](
1316 const boost::system::error_code ec) {
1317 if (ec)
1318 {
1319 BMCWEB_LOG_DEBUG << "Failed to set elapsed time. "
1320 "DBUS response error "
1321 << ec;
1322 messages::internalError(aResp->res);
1323 return;
1324 }
1325 aResp->res.jsonValue["DateTime"] = datetime;
1326 },
1327 "xyz.openbmc_project.Time.Manager",
1328 "/xyz/openbmc_project/time/bmc",
1329 "org.freedesktop.DBus.Properties", "Set",
1330 "xyz.openbmc_project.Time.EpochTime", "Elapsed",
1331 std::variant<uint64_t>(durMicroSecs));
1332 }
1333 else
1334 {
1335 messages::propertyValueFormatError(aResp->res, datetime,
1336 "DateTime");
1337 return;
1338 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001339 }
1340
Ed Tanous0f74e642018-11-12 15:17:05 -08001341 std::string uuid;
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001342};
1343
Ed Tanous1abe55e2018-09-05 08:30:59 -07001344class ManagerCollection : public Node
1345{
1346 public:
James Feist5b4aa862018-08-16 14:07:01 -07001347 ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001348 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001349 entityPrivileges = {
1350 {boost::beast::http::verb::get, {{"Login"}}},
1351 {boost::beast::http::verb::head, {{"Login"}}},
1352 {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
1353 {boost::beast::http::verb::put, {{"ConfigureManager"}}},
1354 {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
1355 {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
1356 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001357
Ed Tanous1abe55e2018-09-05 08:30:59 -07001358 private:
James Feist5b4aa862018-08-16 14:07:01 -07001359 void doGet(crow::Response& res, const crow::Request& req,
1360 const std::vector<std::string>& params) override
Ed Tanous1abe55e2018-09-05 08:30:59 -07001361 {
James Feist83ff9ab2018-08-31 10:18:24 -07001362 // Collections don't include the static data added by SubRoute
1363 // because it has a duplicate entry for members
Ed Tanous1abe55e2018-09-05 08:30:59 -07001364 res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
1365 res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
1366 res.jsonValue["@odata.context"] =
1367 "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
1368 res.jsonValue["Name"] = "Manager Collection";
1369 res.jsonValue["Members@odata.count"] = 1;
1370 res.jsonValue["Members"] = {
James Feist5b4aa862018-08-16 14:07:01 -07001371 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001372 res.end();
1373 }
Borawski.Lukasz9c3106852018-02-09 15:24:22 +01001374};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001375} // namespace redfish