blob: 19728a470ed313516a0d700a7baaa903ae670324 [file] [log] [blame]
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +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
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080019#include "dbus_utility.hpp"
James Feist1c8fba92019-12-20 15:12:07 -080020#include "led.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080021#include "query.hpp"
Chau Ly7164bc62023-10-15 14:55:30 +000022#include "redfish_util.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080023#include "registries/privilege_registry.hpp"
24#include "utils/collection.hpp"
25#include "utils/dbus_utils.hpp"
Nan Zhoucf7eba02022-07-21 23:53:20 +000026#include "utils/json_utils.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070027
George Liue99073f2022-12-09 11:06:16 +080028#include <boost/system/error_code.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070029#include <boost/url/format.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070030#include <sdbusplus/asio/property.hpp>
Andrew Geisslerfc903b32023-05-31 14:15:42 -040031#include <sdbusplus/message.hpp>
Krzysztof Grobelny86d89ed2022-08-29 14:49:20 +020032#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033
George Liu7a1dbc42022-12-07 16:03:22 +080034#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070035#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080036#include <string_view>
37
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace redfish
39{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010040
41/**
Willy Tu5e577bc2022-07-26 00:41:55 +000042 * @brief Retrieves resources over dbus to link to the chassis
43 *
44 * @param[in] asyncResp - Shared pointer for completing asynchronous
45 * calls
46 * @param[in] path - Chassis dbus path to look for the storage.
47 *
48 * Calls the Association endpoints on the path + "/storage" and add the link of
49 * json["Links"]["Storage@odata.count"] =
50 * {"@odata.id", "/redfish/v1/Storage/" + resourceId}
51 *
52 * @return None.
53 */
54inline void getStorageLink(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55 const sdbusplus::message::object_path& path)
56{
57 sdbusplus::asio::getProperty<std::vector<std::string>>(
58 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper",
59 (path / "storage").str, "xyz.openbmc_project.Association", "endpoints",
Willy Tud4b054c2023-06-12 15:18:45 -070060 [asyncResp](const boost::system::error_code& ec,
Willy Tu5e577bc2022-07-26 00:41:55 +000061 const std::vector<std::string>& storageList) {
62 if (ec)
63 {
Ed Tanous62598e32023-07-17 17:06:25 -070064 BMCWEB_LOG_DEBUG("getStorageLink got DBUS response error");
Willy Tu5e577bc2022-07-26 00:41:55 +000065 return;
66 }
67
68 nlohmann::json::array_t storages;
69 for (const std::string& storagePath : storageList)
70 {
71 std::string id =
72 sdbusplus::message::object_path(storagePath).filename();
73 if (id.empty())
74 {
75 continue;
76 }
77
78 nlohmann::json::object_t storage;
79 storage["@odata.id"] = boost::urls::format(
80 "/redfish/v1/Systems/system/Storage/{}", id);
81 storages.emplace_back(std::move(storage));
82 }
83 asyncResp->res.jsonValue["Links"]["Storage@odata.count"] =
84 storages.size();
85 asyncResp->res.jsonValue["Links"]["Storage"] = std::move(storages);
Patrick Williams5a39f772023-10-20 11:20:21 -050086 });
Willy Tu5e577bc2022-07-26 00:41:55 +000087}
88
89/**
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060090 * @brief Retrieves chassis state properties over dbus
91 *
Ed Tanousac106bf2023-06-07 09:24:59 -070092 * @param[in] asyncResp - Shared pointer for completing asynchronous calls.
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060093 *
94 * @return None.
95 */
Ed Tanousac106bf2023-06-07 09:24:59 -070096inline void getChassisState(std::shared_ptr<bmcweb::AsyncResp> asyncResp)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -060097{
Jonathan Doman1e1e5982021-06-11 09:36:17 -070098 // crow::connections::systemBus->async_method_call(
99 sdbusplus::asio::getProperty<std::string>(
100 *crow::connections::systemBus, "xyz.openbmc_project.State.Chassis",
101 "/xyz/openbmc_project/state/chassis0",
102 "xyz.openbmc_project.State.Chassis", "CurrentPowerState",
Ed Tanousac106bf2023-06-07 09:24:59 -0700103 [asyncResp{std::move(asyncResp)}](const boost::system::error_code& ec,
104 const std::string& chassisState) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700105 if (ec)
106 {
107 if (ec == boost::system::errc::host_unreachable)
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600108 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700109 // Service not available, no error, just don't return
110 // chassis state info
Ed Tanous62598e32023-07-17 17:06:25 -0700111 BMCWEB_LOG_DEBUG("Service not available {}", ec);
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600112 return;
113 }
Ed Tanous62598e32023-07-17 17:06:25 -0700114 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
Ed Tanousac106bf2023-06-07 09:24:59 -0700115 messages::internalError(asyncResp->res);
Ed Tanous002d39b2022-05-31 08:59:27 -0700116 return;
117 }
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600118
Ed Tanous62598e32023-07-17 17:06:25 -0700119 BMCWEB_LOG_DEBUG("Chassis state: {}", chassisState);
Ed Tanous002d39b2022-05-31 08:59:27 -0700120 // Verify Chassis State
121 if (chassisState == "xyz.openbmc_project.State.Chassis.PowerState.On")
122 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700123 asyncResp->res.jsonValue["PowerState"] = "On";
124 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Ed Tanous002d39b2022-05-31 08:59:27 -0700125 }
126 else if (chassisState ==
127 "xyz.openbmc_project.State.Chassis.PowerState.Off")
128 {
Ed Tanousac106bf2023-06-07 09:24:59 -0700129 asyncResp->res.jsonValue["PowerState"] = "Off";
130 asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 }
Patrick Williams5a39f772023-10-20 11:20:21 -0500132 });
Gunnar Millsbeeca0a2019-02-14 16:30:45 -0600133}
134
Qiang XUc1819422019-02-27 13:51:32 +0800135/**
136 * Retrieves physical security properties over dbus
137 */
Chau Ly7164bc62023-10-15 14:55:30 +0000138inline void handlePhysicalSecurityGetSubTree(
139 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
140 const boost::system::error_code& ec,
141 const dbus::utility::MapperGetSubTreeResponse& subtree)
Qiang XUc1819422019-02-27 13:51:32 +0800142{
Chau Ly7164bc62023-10-15 14:55:30 +0000143 if (ec)
144 {
145 // do not add err msg in redfish response, because this is not
146 // mandatory property
147 BMCWEB_LOG_INFO("DBUS error: no matched iface {}", ec);
148 return;
149 }
150 // Iterate over all retrieved ObjectPaths.
151 for (const auto& object : subtree)
152 {
153 if (!object.second.empty())
Ed Tanous002d39b2022-05-31 08:59:27 -0700154 {
Ed Tanous89144a32024-04-08 17:27:04 -0700155 const auto& service = object.second.front();
Chau Ly7164bc62023-10-15 14:55:30 +0000156
157 BMCWEB_LOG_DEBUG("Get intrusion status by service ");
158
159 sdbusplus::asio::getProperty<std::string>(
160 *crow::connections::systemBus, service.first, object.first,
161 "xyz.openbmc_project.Chassis.Intrusion", "Status",
162 [asyncResp](const boost::system::error_code& ec1,
163 const std::string& value) {
164 if (ec1)
165 {
166 // do not add err msg in redfish response, because this is
167 // not
168 // mandatory property
169 BMCWEB_LOG_ERROR("DBUS response error {}", ec1);
170 return;
171 }
172 asyncResp->res
173 .jsonValue["PhysicalSecurity"]["IntrusionSensorNumber"] = 1;
174 asyncResp->res
175 .jsonValue["PhysicalSecurity"]["IntrusionSensor"] = value;
176 });
177
Ed Tanous002d39b2022-05-31 08:59:27 -0700178 return;
179 }
Chau Ly7164bc62023-10-15 14:55:30 +0000180 }
Qiang XUc1819422019-02-27 13:51:32 +0800181}
182
Nan Zhoucf7eba02022-07-21 23:53:20 +0000183inline void handleChassisCollectionGet(
184 App& app, const crow::Request& req,
185 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
186{
187 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188 {
189 return;
190 }
191 asyncResp->res.jsonValue["@odata.type"] =
192 "#ChassisCollection.ChassisCollection";
193 asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
194 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
195
George Liu7a1dbc42022-12-07 16:03:22 +0800196 constexpr std::array<std::string_view, 2> interfaces{
197 "xyz.openbmc_project.Inventory.Item.Board",
198 "xyz.openbmc_project.Inventory.Item.Chassis"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000199 collection_util::getCollectionMembers(
Lakshmi Yadlapati36b5f1e2023-09-26 23:53:28 -0500200 asyncResp, boost::urls::url("/redfish/v1/Chassis"), interfaces,
201 "/xyz/openbmc_project/inventory");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000202}
203
Jie Yanga5617492021-06-29 12:59:14 -0700204inline void getChassisContainedBy(
205 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206 const std::string& chassisId, const boost::system::error_code& ec,
207 const dbus::utility::MapperEndPoints& upstreamChassisPaths)
208{
209 if (ec)
210 {
211 if (ec.value() != EBADR)
212 {
Ed Tanous62598e32023-07-17 17:06:25 -0700213 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700214 messages::internalError(asyncResp->res);
215 }
216 return;
217 }
218 if (upstreamChassisPaths.empty())
219 {
220 return;
221 }
222 if (upstreamChassisPaths.size() > 1)
223 {
Ed Tanous8ece0e42024-01-02 13:16:50 -0800224 BMCWEB_LOG_ERROR("{} is contained by multiple chassis", chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700225 messages::internalError(asyncResp->res);
226 return;
227 }
228
229 sdbusplus::message::object_path upstreamChassisPath(
230 upstreamChassisPaths[0]);
231 std::string upstreamChassis = upstreamChassisPath.filename();
232 if (upstreamChassis.empty())
233 {
Ed Tanous62598e32023-07-17 17:06:25 -0700234 BMCWEB_LOG_WARNING("Malformed upstream Chassis path {} on {}",
235 upstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700236 return;
237 }
238
239 asyncResp->res.jsonValue["Links"]["ContainedBy"]["@odata.id"] =
240 boost::urls::format("/redfish/v1/Chassis/{}", upstreamChassis);
241}
242
243inline void getChassisContains(
244 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
245 const std::string& chassisId, const boost::system::error_code& ec,
246 const dbus::utility::MapperEndPoints& downstreamChassisPaths)
247{
248 if (ec)
249 {
250 if (ec.value() != EBADR)
251 {
Ed Tanous62598e32023-07-17 17:06:25 -0700252 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Jie Yanga5617492021-06-29 12:59:14 -0700253 messages::internalError(asyncResp->res);
254 }
255 return;
256 }
257 if (downstreamChassisPaths.empty())
258 {
259 return;
260 }
261 nlohmann::json& jValue = asyncResp->res.jsonValue["Links"]["Contains"];
262 if (!jValue.is_array())
263 {
264 // Create the array if it was empty
265 jValue = nlohmann::json::array();
266 }
267 for (const auto& p : downstreamChassisPaths)
268 {
269 sdbusplus::message::object_path downstreamChassisPath(p);
270 std::string downstreamChassis = downstreamChassisPath.filename();
271 if (downstreamChassis.empty())
272 {
Ed Tanous62598e32023-07-17 17:06:25 -0700273 BMCWEB_LOG_WARNING("Malformed downstream Chassis path {} on {}",
274 downstreamChassisPath.str, chassisId);
Jie Yanga5617492021-06-29 12:59:14 -0700275 continue;
276 }
277 nlohmann::json link;
278 link["@odata.id"] = boost::urls::format("/redfish/v1/Chassis/{}",
279 downstreamChassis);
280 jValue.push_back(std::move(link));
281 }
282 asyncResp->res.jsonValue["Links"]["Contains@odata.count"] = jValue.size();
283}
284
285inline void
286 getChassisConnectivity(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
287 const std::string& chassisId,
288 const std::string& chassisPath)
289{
Ed Tanous62598e32023-07-17 17:06:25 -0700290 BMCWEB_LOG_DEBUG("Get chassis connectivity");
Jie Yanga5617492021-06-29 12:59:14 -0700291
292 dbus::utility::getAssociationEndPoints(
293 chassisPath + "/contained_by",
294 std::bind_front(getChassisContainedBy, asyncResp, chassisId));
295
296 dbus::utility::getAssociationEndPoints(
297 chassisPath + "/containing",
298 std::bind_front(getChassisContains, asyncResp, chassisId));
299}
300
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100301/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100302 * ChassisCollection derived class for delivering Chassis Collection Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700303 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100304 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700305inline void requestRoutesChassisCollection(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700306{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700307 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/")
Ed Tanoused398212021-06-09 17:05:54 -0700308 .privileges(redfish::privileges::getChassisCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700309 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000310 std::bind_front(handleChassisCollectionGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700311}
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100312
Willy Tu308f70c2021-09-28 20:24:52 -0700313inline void
314 getChassisLocationCode(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
315 const std::string& connectionName,
316 const std::string& path)
317{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700318 sdbusplus::asio::getProperty<std::string>(
319 *crow::connections::systemBus, connectionName, path,
320 "xyz.openbmc_project.Inventory.Decorator.LocationCode", "LocationCode",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800321 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700322 const std::string& property) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700323 if (ec)
324 {
Ed Tanous62598e32023-07-17 17:06:25 -0700325 BMCWEB_LOG_ERROR("DBUS response error for Location");
Ed Tanous002d39b2022-05-31 08:59:27 -0700326 messages::internalError(asyncResp->res);
327 return;
328 }
Willy Tu308f70c2021-09-28 20:24:52 -0700329
Ed Tanous002d39b2022-05-31 08:59:27 -0700330 asyncResp->res.jsonValue["Location"]["PartLocation"]["ServiceLabel"] =
331 property;
Patrick Williams5a39f772023-10-20 11:20:21 -0500332 });
Willy Tu308f70c2021-09-28 20:24:52 -0700333}
334
335inline void getChassisUUID(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
336 const std::string& connectionName,
337 const std::string& path)
338{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700339 sdbusplus::asio::getProperty<std::string>(
340 *crow::connections::systemBus, connectionName, path,
341 "xyz.openbmc_project.Common.UUID", "UUID",
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800342 [asyncResp](const boost::system::error_code& ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700343 const std::string& chassisUUID) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700344 if (ec)
345 {
Ed Tanous62598e32023-07-17 17:06:25 -0700346 BMCWEB_LOG_ERROR("DBUS response error for UUID");
Ed Tanous002d39b2022-05-31 08:59:27 -0700347 messages::internalError(asyncResp->res);
348 return;
349 }
350 asyncResp->res.jsonValue["UUID"] = chassisUUID;
Patrick Williams5a39f772023-10-20 11:20:21 -0500351 });
Willy Tu308f70c2021-09-28 20:24:52 -0700352}
353
Chau Ly7164bc62023-10-15 14:55:30 +0000354inline void handleDecoratorAssetProperties(
355 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
356 const std::string& chassisId, const std::string& path,
357 const dbus::utility::DBusPropertiesMap& propertiesList)
358{
359 const std::string* partNumber = nullptr;
360 const std::string* serialNumber = nullptr;
361 const std::string* manufacturer = nullptr;
362 const std::string* model = nullptr;
363 const std::string* sparePartNumber = nullptr;
364
365 const bool success = sdbusplus::unpackPropertiesNoThrow(
366 dbus_utils::UnpackErrorPrinter(), propertiesList, "PartNumber",
367 partNumber, "SerialNumber", serialNumber, "Manufacturer", manufacturer,
368 "Model", model, "SparePartNumber", sparePartNumber);
369
370 if (!success)
371 {
372 messages::internalError(asyncResp->res);
373 return;
374 }
375
376 if (partNumber != nullptr)
377 {
378 asyncResp->res.jsonValue["PartNumber"] = *partNumber;
379 }
380
381 if (serialNumber != nullptr)
382 {
383 asyncResp->res.jsonValue["SerialNumber"] = *serialNumber;
384 }
385
386 if (manufacturer != nullptr)
387 {
388 asyncResp->res.jsonValue["Manufacturer"] = *manufacturer;
389 }
390
391 if (model != nullptr)
392 {
393 asyncResp->res.jsonValue["Model"] = *model;
394 }
395
396 // SparePartNumber is optional on D-Bus
397 // so skip if it is empty
398 if (sparePartNumber != nullptr && !sparePartNumber->empty())
399 {
400 asyncResp->res.jsonValue["SparePartNumber"] = *sparePartNumber;
401 }
402
403 asyncResp->res.jsonValue["Name"] = chassisId;
404 asyncResp->res.jsonValue["Id"] = chassisId;
Ed Tanous25b54db2024-04-17 15:40:31 -0700405
406 if constexpr (BMCWEB_REDFISH_ALLOW_DEPRECATED_POWER_THERMAL)
407 {
408 asyncResp->res.jsonValue["Thermal"]["@odata.id"] =
409 boost::urls::format("/redfish/v1/Chassis/{}/Thermal", chassisId);
410 // Power object
411 asyncResp->res.jsonValue["Power"]["@odata.id"] =
412 boost::urls::format("/redfish/v1/Chassis/{}/Power", chassisId);
413 }
414
415 if constexpr (BMCWEB_REDFISH_NEW_POWERSUBSYSTEM_THERMALSUBSYSTEM)
416 {
417 asyncResp->res.jsonValue["ThermalSubsystem"]["@odata.id"] =
418 boost::urls::format("/redfish/v1/Chassis/{}/ThermalSubsystem",
419 chassisId);
420 asyncResp->res.jsonValue["PowerSubsystem"]["@odata.id"] =
421 boost::urls::format("/redfish/v1/Chassis/{}/PowerSubsystem",
422 chassisId);
423 asyncResp->res.jsonValue["EnvironmentMetrics"]["@odata.id"] =
424 boost::urls::format("/redfish/v1/Chassis/{}/EnvironmentMetrics",
425 chassisId);
426 }
Chau Ly7164bc62023-10-15 14:55:30 +0000427 // SensorCollection
428 asyncResp->res.jsonValue["Sensors"]["@odata.id"] =
429 boost::urls::format("/redfish/v1/Chassis/{}/Sensors", chassisId);
430 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
431
432 nlohmann::json::array_t computerSystems;
433 nlohmann::json::object_t system;
434 system["@odata.id"] = "/redfish/v1/Systems/system";
435 computerSystems.emplace_back(std::move(system));
436 asyncResp->res.jsonValue["Links"]["ComputerSystems"] =
437 std::move(computerSystems);
438
439 nlohmann::json::array_t managedBy;
440 nlohmann::json::object_t manager;
441 manager["@odata.id"] = "/redfish/v1/Managers/bmc";
442 managedBy.emplace_back(std::move(manager));
443 asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
444 getChassisState(asyncResp);
445 getStorageLink(asyncResp, path);
446}
447
448inline void handleChassisGetSubTree(
449 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
450 const std::string& chassisId, const boost::system::error_code& ec,
451 const dbus::utility::MapperGetSubTreeResponse& subtree)
452{
453 if (ec)
454 {
455 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
456 messages::internalError(asyncResp->res);
457 return;
458 }
459 // Iterate over all retrieved ObjectPaths.
460 for (const std::pair<
461 std::string,
462 std::vector<std::pair<std::string, std::vector<std::string>>>>&
463 object : subtree)
464 {
465 const std::string& path = object.first;
466 const std::vector<std::pair<std::string, std::vector<std::string>>>&
467 connectionNames = object.second;
468
469 sdbusplus::message::object_path objPath(path);
470 if (objPath.filename() != chassisId)
471 {
472 continue;
473 }
474
475 getChassisConnectivity(asyncResp, chassisId, path);
476
Chau Ly7164bc62023-10-15 14:55:30 +0000477 if (connectionNames.empty())
478 {
479 BMCWEB_LOG_ERROR("Got 0 Connection names");
480 continue;
481 }
482
483 asyncResp->res.jsonValue["@odata.type"] = "#Chassis.v1_22_0.Chassis";
484 asyncResp->res.jsonValue["@odata.id"] =
485 boost::urls::format("/redfish/v1/Chassis/{}", chassisId);
486 asyncResp->res.jsonValue["Name"] = "Chassis Collection";
487 asyncResp->res.jsonValue["ChassisType"] = "RackMount";
488 asyncResp->res.jsonValue["Actions"]["#Chassis.Reset"]["target"] =
489 boost::urls::format("/redfish/v1/Chassis/{}/Actions/Chassis.Reset",
490 chassisId);
491 asyncResp->res
492 .jsonValue["Actions"]["#Chassis.Reset"]["@Redfish.ActionInfo"] =
493 boost::urls::format("/redfish/v1/Chassis/{}/ResetActionInfo",
494 chassisId);
Chau Ly7164bc62023-10-15 14:55:30 +0000495 dbus::utility::getAssociationEndPoints(
496 path + "/drive",
497 [asyncResp, chassisId](const boost::system::error_code& ec3,
498 const dbus::utility::MapperEndPoints& resp) {
499 if (ec3 || resp.empty())
500 {
501 return; // no drives = no failures
502 }
503
504 nlohmann::json reference;
505 reference["@odata.id"] =
506 boost::urls::format("/redfish/v1/Chassis/{}/Drives", chassisId);
507 asyncResp->res.jsonValue["Drives"] = std::move(reference);
508 });
509
510 const std::string& connectionName = connectionNames[0].first;
511
512 const std::vector<std::string>& interfaces2 = connectionNames[0].second;
George Liue5ae9c12021-11-16 10:31:23 +0800513 const std::array<const char*, 3> hasIndicatorLed = {
514 "xyz.openbmc_project.Inventory.Item.Chassis",
Chau Ly7164bc62023-10-15 14:55:30 +0000515 "xyz.openbmc_project.Inventory.Item.Panel",
516 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
517
518 const std::string assetTagInterface =
519 "xyz.openbmc_project.Inventory.Decorator.AssetTag";
520 const std::string replaceableInterface =
521 "xyz.openbmc_project.Inventory.Decorator.Replaceable";
Carson Labradob4d593f2024-02-16 22:34:32 +0000522 const std::string revisionInterface =
523 "xyz.openbmc_project.Inventory.Decorator.Revision";
Chau Ly7164bc62023-10-15 14:55:30 +0000524 for (const auto& interface : interfaces2)
525 {
526 if (interface == assetTagInterface)
527 {
528 sdbusplus::asio::getProperty<std::string>(
529 *crow::connections::systemBus, connectionName, path,
530 assetTagInterface, "AssetTag",
531 [asyncResp, chassisId](const boost::system::error_code& ec2,
532 const std::string& property) {
533 if (ec2)
534 {
535 BMCWEB_LOG_ERROR("DBus response error for AssetTag: {}",
536 ec2);
537 messages::internalError(asyncResp->res);
538 return;
539 }
540 asyncResp->res.jsonValue["AssetTag"] = property;
541 });
542 }
543 else if (interface == replaceableInterface)
544 {
545 sdbusplus::asio::getProperty<bool>(
546 *crow::connections::systemBus, connectionName, path,
547 replaceableInterface, "HotPluggable",
548 [asyncResp, chassisId](const boost::system::error_code& ec2,
549 const bool property) {
550 if (ec2)
551 {
552 BMCWEB_LOG_ERROR(
553 "DBus response error for HotPluggable: {}", ec2);
554 messages::internalError(asyncResp->res);
555 return;
556 }
557 asyncResp->res.jsonValue["HotPluggable"] = property;
558 });
559 }
Carson Labradob4d593f2024-02-16 22:34:32 +0000560 else if (interface == revisionInterface)
561 {
562 sdbusplus::asio::getProperty<std::string>(
563 *crow::connections::systemBus, connectionName, path,
564 revisionInterface, "Version",
565 [asyncResp, chassisId](const boost::system::error_code& ec2,
566 const std::string& property) {
567 if (ec2)
568 {
569 BMCWEB_LOG_ERROR("DBus response error for Version: {}",
570 ec2);
571 messages::internalError(asyncResp->res);
572 return;
573 }
574 asyncResp->res.jsonValue["Version"] = property;
575 });
576 }
Chau Ly7164bc62023-10-15 14:55:30 +0000577 }
578
579 for (const char* interface : hasIndicatorLed)
580 {
581 if (std::ranges::find(interfaces2, interface) != interfaces2.end())
582 {
583 getIndicatorLedState(asyncResp);
584 getSystemLocationIndicatorActive(asyncResp);
585 break;
586 }
587 }
588
589 sdbusplus::asio::getAllProperties(
590 *crow::connections::systemBus, connectionName, path,
591 "xyz.openbmc_project.Inventory.Decorator.Asset",
592 [asyncResp, chassisId,
593 path](const boost::system::error_code&,
594 const dbus::utility::DBusPropertiesMap& propertiesList) {
595 handleDecoratorAssetProperties(asyncResp, chassisId, path,
596 propertiesList);
597 });
598
599 for (const auto& interface : interfaces2)
600 {
601 if (interface == "xyz.openbmc_project.Common.UUID")
602 {
603 getChassisUUID(asyncResp, connectionName, path);
604 }
605 else if (interface ==
606 "xyz.openbmc_project.Inventory.Decorator.LocationCode")
607 {
608 getChassisLocationCode(asyncResp, connectionName, path);
609 }
610 }
611
612 return;
613 }
614
615 // Couldn't find an object with that name. return an error
616 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
617}
618
Nan Zhoucf7eba02022-07-21 23:53:20 +0000619inline void
620 handleChassisGet(App& app, const crow::Request& req,
621 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
622 const std::string& chassisId)
623{
624 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
625 {
626 return;
627 }
George Liue99073f2022-12-09 11:06:16 +0800628 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000629 "xyz.openbmc_project.Inventory.Item.Board",
630 "xyz.openbmc_project.Inventory.Item.Chassis"};
631
George Liue99073f2022-12-09 11:06:16 +0800632 dbus::utility::getSubTree(
633 "/xyz/openbmc_project/inventory", 0, interfaces,
Chau Ly7164bc62023-10-15 14:55:30 +0000634 std::bind_front(handleChassisGetSubTree, asyncResp, chassisId));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000635
Chau Ly7164bc62023-10-15 14:55:30 +0000636 constexpr std::array<std::string_view, 1> interfaces2 = {
637 "xyz.openbmc_project.Chassis.Intrusion"};
Nan Zhoucf7eba02022-07-21 23:53:20 +0000638
Chau Ly7164bc62023-10-15 14:55:30 +0000639 dbus::utility::getSubTree(
640 "/xyz/openbmc_project", 0, interfaces2,
641 std::bind_front(handlePhysicalSecurityGetSubTree, asyncResp));
Nan Zhoucf7eba02022-07-21 23:53:20 +0000642}
643
644inline void
645 handleChassisPatch(App& app, const crow::Request& req,
646 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
647 const std::string& param)
648{
649 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
650 {
651 return;
652 }
653 std::optional<bool> locationIndicatorActive;
654 std::optional<std::string> indicatorLed;
655
656 if (param.empty())
657 {
658 return;
659 }
660
661 if (!json_util::readJsonPatch(
662 req, asyncResp->res, "LocationIndicatorActive",
663 locationIndicatorActive, "IndicatorLED", indicatorLed))
664 {
665 return;
666 }
667
668 // TODO (Gunnar): Remove IndicatorLED after enough time has passed
669 if (!locationIndicatorActive && !indicatorLed)
670 {
671 return; // delete this when we support more patch properties
672 }
673 if (indicatorLed)
674 {
675 asyncResp->res.addHeader(
676 boost::beast::http::field::warning,
677 "299 - \"IndicatorLED is deprecated. Use LocationIndicatorActive instead.\"");
678 }
679
George Liue99073f2022-12-09 11:06:16 +0800680 constexpr std::array<std::string_view, 2> interfaces = {
Nan Zhoucf7eba02022-07-21 23:53:20 +0000681 "xyz.openbmc_project.Inventory.Item.Board",
682 "xyz.openbmc_project.Inventory.Item.Chassis"};
683
684 const std::string& chassisId = param;
685
George Liue99073f2022-12-09 11:06:16 +0800686 dbus::utility::getSubTree(
687 "/xyz/openbmc_project/inventory", 0, interfaces,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000688 [asyncResp, chassisId, locationIndicatorActive,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800689 indicatorLed](const boost::system::error_code& ec,
Nan Zhoucf7eba02022-07-21 23:53:20 +0000690 const dbus::utility::MapperGetSubTreeResponse& subtree) {
691 if (ec)
692 {
Ed Tanous62598e32023-07-17 17:06:25 -0700693 BMCWEB_LOG_ERROR("DBUS response error {}", ec);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000694 messages::internalError(asyncResp->res);
695 return;
696 }
697
698 // Iterate over all retrieved ObjectPaths.
699 for (const std::pair<
700 std::string,
701 std::vector<std::pair<std::string, std::vector<std::string>>>>&
702 object : subtree)
703 {
704 const std::string& path = object.first;
705 const std::vector<std::pair<std::string, std::vector<std::string>>>&
706 connectionNames = object.second;
707
708 sdbusplus::message::object_path objPath(path);
709 if (objPath.filename() != chassisId)
710 {
711 continue;
712 }
713
714 if (connectionNames.empty())
715 {
Ed Tanous62598e32023-07-17 17:06:25 -0700716 BMCWEB_LOG_ERROR("Got 0 Connection names");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000717 continue;
718 }
719
720 const std::vector<std::string>& interfaces3 =
721 connectionNames[0].second;
722
George Liue5ae9c12021-11-16 10:31:23 +0800723 const std::array<const char*, 3> hasIndicatorLed = {
724 "xyz.openbmc_project.Inventory.Item.Chassis",
Nan Zhoucf7eba02022-07-21 23:53:20 +0000725 "xyz.openbmc_project.Inventory.Item.Panel",
726 "xyz.openbmc_project.Inventory.Item.Board.Motherboard"};
727 bool indicatorChassis = false;
728 for (const char* interface : hasIndicatorLed)
729 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700730 if (std::ranges::find(interfaces3, interface) !=
731 interfaces3.end())
Nan Zhoucf7eba02022-07-21 23:53:20 +0000732 {
733 indicatorChassis = true;
734 break;
735 }
736 }
737 if (locationIndicatorActive)
738 {
739 if (indicatorChassis)
740 {
George Liu59a17e42022-10-08 09:27:47 +0800741 setSystemLocationIndicatorActive(asyncResp,
742 *locationIndicatorActive);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000743 }
744 else
745 {
746 messages::propertyUnknown(asyncResp->res,
747 "LocationIndicatorActive");
748 }
749 }
750 if (indicatorLed)
751 {
752 if (indicatorChassis)
753 {
754 setIndicatorLedState(asyncResp, *indicatorLed);
755 }
756 else
757 {
758 messages::propertyUnknown(asyncResp->res, "IndicatorLED");
759 }
760 }
761 return;
762 }
763
Jiaqing Zhaod8a5d5d2022-08-05 16:21:51 +0800764 messages::resourceNotFound(asyncResp->res, "Chassis", chassisId);
Patrick Williams5a39f772023-10-20 11:20:21 -0500765 });
Nan Zhoucf7eba02022-07-21 23:53:20 +0000766}
767
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100768/**
769 * Chassis override class for delivering Chassis Schema
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700770 * Functions triggers appropriate requests on DBus
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100771 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700772inline void requestRoutesChassis(App& app)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700773{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700774 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700775 .privileges(redfish::privileges::getChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700776 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000777 std::bind_front(handleChassisGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700778
779 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700780 .privileges(redfish::privileges::patchChassis)
Ed Tanous002d39b2022-05-31 08:59:27 -0700781 .methods(boost::beast::http::verb::patch)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000782 std::bind_front(handleChassisPatch, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700783}
P.K. Leedd99e042020-06-17 19:43:16 +0800784
zhanghch058d1b46d2021-04-01 11:18:24 +0800785inline void
786 doChassisPowerCycle(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
P.K. Leedd99e042020-06-17 19:43:16 +0800787{
George Liu7a1dbc42022-12-07 16:03:22 +0800788 constexpr std::array<std::string_view, 1> interfaces = {
Vijay Khemkac3b3c922020-09-22 23:00:12 -0700789 "xyz.openbmc_project.State.Chassis"};
790
791 // Use mapper to get subtree paths.
George Liu7a1dbc42022-12-07 16:03:22 +0800792 dbus::utility::getSubTreePaths(
793 "/", 0, interfaces,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800794 [asyncResp](
George Liu7a1dbc42022-12-07 16:03:22 +0800795 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800796 const dbus::utility::MapperGetSubTreePathsResponse& chassisList) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700797 if (ec)
798 {
Ed Tanous62598e32023-07-17 17:06:25 -0700799 BMCWEB_LOG_ERROR("[mapper] Bad D-Bus request error: {}", ec);
Ed Tanous002d39b2022-05-31 08:59:27 -0700800 messages::internalError(asyncResp->res);
801 return;
802 }
803
804 const char* processName = "xyz.openbmc_project.State.Chassis";
805 const char* interfaceName = "xyz.openbmc_project.State.Chassis";
806 const char* destProperty = "RequestedPowerTransition";
807 const std::string propertyValue =
808 "xyz.openbmc_project.State.Chassis.Transition.PowerCycle";
809 std::string objectPath = "/xyz/openbmc_project/state/chassis_system0";
810
811 /* Look for system reset chassis path */
Ed Tanous3544d2a2023-08-06 18:12:20 -0700812 if ((std::ranges::find(chassisList, objectPath)) == chassisList.end())
Ed Tanous002d39b2022-05-31 08:59:27 -0700813 {
814 /* We prefer to reset the full chassis_system, but if it doesn't
815 * exist on some platforms, fall back to a host-only power reset
816 */
817 objectPath = "/xyz/openbmc_project/state/chassis0";
818 }
819
Ed Tanousd02aad32024-02-13 14:43:34 -0800820 setDbusProperty(asyncResp, processName, objectPath, interfaceName,
821 destProperty, "ResetType", propertyValue);
Patrick Williams5a39f772023-10-20 11:20:21 -0500822 });
P.K. Leedd99e042020-06-17 19:43:16 +0800823}
824
Nan Zhoucf7eba02022-07-21 23:53:20 +0000825inline void handleChassisResetActionInfoPost(
826 App& app, const crow::Request& req,
827 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
828 const std::string& /*chassisId*/)
829{
830 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
831 {
832 return;
833 }
Ed Tanous62598e32023-07-17 17:06:25 -0700834 BMCWEB_LOG_DEBUG("Post Chassis Reset.");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000835
836 std::string resetType;
837
838 if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
839 {
840 return;
841 }
842
843 if (resetType != "PowerCycle")
844 {
Ed Tanous62598e32023-07-17 17:06:25 -0700845 BMCWEB_LOG_DEBUG("Invalid property value for ResetType: {}", resetType);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000846 messages::actionParameterNotSupported(asyncResp->res, resetType,
847 "ResetType");
848
849 return;
850 }
851 doChassisPowerCycle(asyncResp);
852}
853
P.K. Leedd99e042020-06-17 19:43:16 +0800854/**
855 * ChassisResetAction class supports the POST method for the Reset
856 * action.
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700857 * Function handles POST method request.
858 * Analyzes POST body before sending Reset request data to D-Bus.
P.K. Leedd99e042020-06-17 19:43:16 +0800859 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700860
861inline void requestRoutesChassisResetAction(App& app)
P.K. Leedd99e042020-06-17 19:43:16 +0800862{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700863 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Actions/Chassis.Reset/")
Ed Tanoused398212021-06-09 17:05:54 -0700864 .privileges(redfish::privileges::postChassis)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700865 .methods(boost::beast::http::verb::post)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000866 std::bind_front(handleChassisResetActionInfoPost, std::ref(app)));
867}
P.K. Leedd99e042020-06-17 19:43:16 +0800868
Nan Zhoucf7eba02022-07-21 23:53:20 +0000869inline void handleChassisResetActionInfoGet(
870 App& app, const crow::Request& req,
871 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
872 const std::string& chassisId)
873{
874 if (!redfish::setUpRedfishRoute(app, req, asyncResp))
875 {
876 return;
877 }
878 asyncResp->res.jsonValue["@odata.type"] = "#ActionInfo.v1_1_2.ActionInfo";
Ed Tanousef4c65b2023-04-24 15:28:50 -0700879 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
880 "/redfish/v1/Chassis/{}/ResetActionInfo", chassisId);
Nan Zhoucf7eba02022-07-21 23:53:20 +0000881 asyncResp->res.jsonValue["Name"] = "Reset Action Info";
P.K. Leedd99e042020-06-17 19:43:16 +0800882
Nan Zhoucf7eba02022-07-21 23:53:20 +0000883 asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
884 nlohmann::json::array_t parameters;
885 nlohmann::json::object_t parameter;
886 parameter["Name"] = "ResetType";
887 parameter["Required"] = true;
888 parameter["DataType"] = "String";
889 nlohmann::json::array_t allowed;
Patrick Williamsad539542023-05-12 10:10:08 -0500890 allowed.emplace_back("PowerCycle");
Nan Zhoucf7eba02022-07-21 23:53:20 +0000891 parameter["AllowableValues"] = std::move(allowed);
Patrick Williamsad539542023-05-12 10:10:08 -0500892 parameters.emplace_back(std::move(parameter));
P.K. Leedd99e042020-06-17 19:43:16 +0800893
Nan Zhoucf7eba02022-07-21 23:53:20 +0000894 asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700895}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530896
897/**
898 * ChassisResetActionInfo derived class for delivering Chassis
899 * ResetType AllowableValues using ResetInfo schema.
900 */
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700901inline void requestRoutesChassisResetActionInfo(App& app)
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530902{
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700903 BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/ResetActionInfo/")
Ed Tanoused398212021-06-09 17:05:54 -0700904 .privileges(redfish::privileges::getActionInfo)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700905 .methods(boost::beast::http::verb::get)(
Nan Zhoucf7eba02022-07-21 23:53:20 +0000906 std::bind_front(handleChassisResetActionInfoGet, std::ref(app)));
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700907}
AppaRao Puli1cb1a9e2020-07-17 23:38:57 +0530908
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909} // namespace redfish