blob: f5e75e5269f682f6a7f496365405ef285790e643 [file] [log] [blame]
Nikhil Potadea25aecc2019-08-23 16:35:26 -07001/*
2// Copyright (c) 2019 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
James Feist2ad9c2f2019-10-29 16:26:48 -070018#include "health.hpp"
James Feiste284a7c2019-11-20 16:20:23 -080019#include "openbmc_dbus_rest.hpp"
James Feist2ad9c2f2019-10-29 16:26:48 -070020
John Edward Broadbent7e860f12021-04-08 15:57:16 -070021#include <app.hpp>
Ed Tanous168e20c2021-12-13 14:39:53 -080022#include <dbus_utility.hpp>
Ed Tanoused398212021-06-09 17:05:54 -070023#include <registries/privilege_registry.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070024#include <sdbusplus/asio/property.hpp>
Nikhil Potadea25aecc2019-08-23 16:35:26 -070025
26namespace redfish
27{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070028inline void requestRoutesStorageCollection(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070029{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070030 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
Ed Tanoused398212021-06-09 17:05:54 -070031 .privileges(redfish::privileges::getStorageCollection)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070032 .methods(boost::beast::http::verb::get)(
33 [](const crow::Request&,
34 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
35 asyncResp->res.jsonValue["@odata.type"] =
36 "#StorageCollection.StorageCollection";
37 asyncResp->res.jsonValue["@odata.id"] =
38 "/redfish/v1/Systems/system/Storage";
39 asyncResp->res.jsonValue["Name"] = "Storage Collection";
40 asyncResp->res.jsonValue["Members"] = {
41 {{"@odata.id", "/redfish/v1/Systems/system/Storage/1"}}};
42 asyncResp->res.jsonValue["Members@odata.count"] = 1;
43 });
44}
Nikhil Potadea25aecc2019-08-23 16:35:26 -070045
John Edward Broadbent7e860f12021-04-08 15:57:16 -070046inline void requestRoutesStorage(App& app)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070047{
John Edward Broadbent7e860f12021-04-08 15:57:16 -070048 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
Ed Tanoused398212021-06-09 17:05:54 -070049 .privileges(redfish::privileges::getStorage)
John Edward Broadbent7e860f12021-04-08 15:57:16 -070050 .methods(
51 boost::beast::http::verb::
52 get)([](const crow::Request&,
53 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
54 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
55 asyncResp->res.jsonValue["@odata.id"] =
56 "/redfish/v1/Systems/system/Storage/1";
57 asyncResp->res.jsonValue["Name"] = "Storage";
58 asyncResp->res.jsonValue["Id"] = "1";
59 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
Nikhil Potadea25aecc2019-08-23 16:35:26 -070060
John Edward Broadbent7e860f12021-04-08 15:57:16 -070061 auto health = std::make_shared<HealthPopulate>(asyncResp);
62 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -070063
John Edward Broadbent7e860f12021-04-08 15:57:16 -070064 crow::connections::systemBus->async_method_call(
65 [asyncResp,
66 health](const boost::system::error_code ec,
67 const std::vector<std::string>& storageList) {
68 nlohmann::json& storageArray =
69 asyncResp->res.jsonValue["Drives"];
70 storageArray = nlohmann::json::array();
71 auto& count =
72 asyncResp->res.jsonValue["Drives@odata.count"];
73 count = 0;
James Feiste284a7c2019-11-20 16:20:23 -080074
John Edward Broadbent7e860f12021-04-08 15:57:16 -070075 if (ec)
Nikhil Potadea25aecc2019-08-23 16:35:26 -070076 {
John Edward Broadbent7e860f12021-04-08 15:57:16 -070077 BMCWEB_LOG_ERROR << "Drive mapper call error";
78 messages::internalError(asyncResp->res);
James Feiste284a7c2019-11-20 16:20:23 -080079 return;
80 }
81
John Edward Broadbent7e860f12021-04-08 15:57:16 -070082 health->inventory.insert(health->inventory.end(),
83 storageList.begin(),
84 storageList.end());
85
86 for (const std::string& objpath : storageList)
87 {
88 std::size_t lastPos = objpath.rfind('/');
89 if (lastPos == std::string::npos ||
90 (objpath.size() <= lastPos + 1))
91 {
92 BMCWEB_LOG_ERROR << "Failed to find '/' in "
93 << objpath;
94 continue;
95 }
96
97 storageArray.push_back(
98 {{"@odata.id",
99 "/redfish/v1/Systems/system/Storage/1/Drives/" +
100 objpath.substr(lastPos + 1)}});
101 }
102
103 count = storageArray.size();
104 },
105 "xyz.openbmc_project.ObjectMapper",
106 "/xyz/openbmc_project/object_mapper",
107 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
108 "/xyz/openbmc_project/inventory", int32_t(0),
109 std::array<const char*, 1>{
110 "xyz.openbmc_project.Inventory.Item.Drive"});
111
112 crow::connections::systemBus->async_method_call(
113 [asyncResp,
114 health](const boost::system::error_code ec,
115 const crow::openbmc_mapper::GetSubTreeType& subtree) {
116 if (ec || !subtree.size())
117 {
118 // doesn't have to be there
119 return;
120 }
121
122 nlohmann::json& root =
123 asyncResp->res.jsonValue["StorageControllers"];
124 root = nlohmann::json::array();
125 for (const auto& [path, interfaceDict] : subtree)
126 {
127 std::size_t lastPos = path.rfind('/');
128 if (lastPos == std::string::npos ||
129 (path.size() <= lastPos + 1))
130 {
131 BMCWEB_LOG_ERROR << "Failed to find '/' in "
132 << path;
133 return;
134 }
135
136 if (interfaceDict.size() != 1)
137 {
138 BMCWEB_LOG_ERROR << "Connection size "
139 << interfaceDict.size()
140 << ", greater than 1";
141 messages::internalError(asyncResp->res);
142 return;
143 }
144
145 const std::string& connectionName =
146 interfaceDict.front().first;
147
148 size_t index = root.size();
149 nlohmann::json& storageController =
150 root.emplace_back(nlohmann::json::object());
151
152 std::string id = path.substr(lastPos + 1);
153
154 storageController["@odata.type"] =
155 "#Storage.v1_7_0.StorageController";
156 storageController["@odata.id"] =
George Liu0fda0f12021-11-16 10:06:17 +0800157 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700158 std::to_string(index);
159 storageController["Name"] = id;
160 storageController["MemberId"] = id;
161 storageController["Status"]["State"] = "Enabled";
162
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700163 sdbusplus::asio::getProperty<bool>(
164 *crow::connections::systemBus, connectionName, path,
165 "xyz.openbmc_project.Inventory.Item", "Present",
166 [asyncResp,
167 index](const boost::system::error_code ec2,
168 bool enabled) {
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700169 // this interface isn't necessary, only check it
170 // if we get a good return
171 if (ec2)
172 {
173 return;
174 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700175 if (!enabled)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700176 {
177 asyncResp->res
178 .jsonValue["StorageControllers"][index]
179 ["Status"]["State"] =
180 "Disabled";
181 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700182 });
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700183
184 crow::connections::systemBus->async_method_call(
185 [asyncResp, index](
186 const boost::system::error_code ec2,
Ed Tanous168e20c2021-12-13 14:39:53 -0800187 const std::vector<
188 std::pair<std::string,
189 dbus::utility::DbusVariantType>>&
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700190 propertiesList) {
191 if (ec2)
192 {
193 // this interface isn't necessary
194 return;
195 }
196 for (const std::pair<
197 std::string,
Ed Tanous168e20c2021-12-13 14:39:53 -0800198 dbus::utility::DbusVariantType>&
199 property : propertiesList)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700200 {
201 // Store DBus properties that are also
202 // Redfish properties with same name and a
203 // string value
204 const std::string& propertyName =
205 property.first;
206 nlohmann::json& object =
207 asyncResp->res
208 .jsonValue["StorageControllers"]
209 [index];
210 if ((propertyName == "PartNumber") ||
211 (propertyName == "SerialNumber") ||
212 (propertyName == "Manufacturer") ||
213 (propertyName == "Model"))
214 {
215 const std::string* value =
216 std::get_if<std::string>(
217 &property.second);
218 if (value == nullptr)
219 {
220 // illegal property
221 messages::internalError(
222 asyncResp->res);
223 return;
224 }
225 object[propertyName] = *value;
226 }
227 }
228 },
229 connectionName, path,
230 "org.freedesktop.DBus.Properties", "GetAll",
231 "xyz.openbmc_project.Inventory.Decorator.Asset");
232 }
233
234 // this is done after we know the json array will no longer
235 // be resized, as json::array uses vector underneath and we
236 // need references to its members that won't change
237 size_t count = 0;
238 for (const auto& [path, interfaceDict] : subtree)
239 {
240 auto subHealth = std::make_shared<HealthPopulate>(
241 asyncResp, root[count]["Status"]);
242 subHealth->inventory.emplace_back(path);
243 health->inventory.emplace_back(path);
244 health->children.emplace_back(subHealth);
245 count++;
246 }
247 },
248 "xyz.openbmc_project.ObjectMapper",
249 "/xyz/openbmc_project/object_mapper",
250 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
251 "/xyz/openbmc_project/inventory", int32_t(0),
252 std::array<const char*, 1>{
253 "xyz.openbmc_project.Inventory.Item.StorageController"});
254 });
255}
256
Willy Tu03913172021-11-08 02:03:19 -0800257inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
258 const std::string& connectionName,
259 const std::string& path)
260{
261 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -0800262 [asyncResp](const boost::system::error_code ec,
263 const std::vector<
264 std::pair<std::string, dbus::utility::DbusVariantType>>&
265 propertiesList) {
Willy Tu03913172021-11-08 02:03:19 -0800266 if (ec)
267 {
268 // this interface isn't necessary
269 return;
270 }
Ed Tanous168e20c2021-12-13 14:39:53 -0800271 for (const std::pair<std::string, dbus::utility::DbusVariantType>&
Willy Tu03913172021-11-08 02:03:19 -0800272 property : propertiesList)
273 {
274 // Store DBus properties that are also
275 // Redfish properties with same name and a
276 // string value
277 const std::string& propertyName = property.first;
278 if ((propertyName == "PartNumber") ||
279 (propertyName == "SerialNumber") ||
280 (propertyName == "Manufacturer") ||
281 (propertyName == "Model"))
282 {
283 const std::string* value =
284 std::get_if<std::string>(&property.second);
285 if (value == nullptr)
286 {
287 // illegal property
288 messages::internalError(asyncResp->res);
289 return;
290 }
291 asyncResp->res.jsonValue[propertyName] = *value;
292 }
293 }
294 },
295 connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
296 "xyz.openbmc_project.Inventory.Decorator.Asset");
297}
298
299inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
300 const std::string& connectionName,
301 const std::string& path)
302{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700303 sdbusplus::asio::getProperty<bool>(
304 *crow::connections::systemBus, connectionName, path,
305 "xyz.openbmc_project.Inventory.Item", "Present",
Willy Tu03913172021-11-08 02:03:19 -0800306 [asyncResp, path](const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700307 const bool enabled) {
Willy Tu03913172021-11-08 02:03:19 -0800308 // this interface isn't necessary, only check it if
309 // we get a good return
310 if (ec)
311 {
312 return;
313 }
314
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700315 if (!enabled)
Willy Tu03913172021-11-08 02:03:19 -0800316 {
317 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
318 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700319 });
Willy Tu03913172021-11-08 02:03:19 -0800320}
321
322inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
323 const std::string& connectionName,
324 const std::string& path)
325{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700326 sdbusplus::asio::getProperty<bool>(
327 *crow::connections::systemBus, connectionName, path,
328 "xyz.openbmc_project.State.Drive", "Rebuilding",
329 [asyncResp](const boost::system::error_code ec, const bool updating) {
Willy Tu03913172021-11-08 02:03:19 -0800330 // this interface isn't necessary, only check it
331 // if we get a good return
332 if (ec)
333 {
334 return;
335 }
336
Willy Tu03913172021-11-08 02:03:19 -0800337 // updating and disabled in the backend shouldn't be
338 // able to be set at the same time, so we don't need
339 // to check for the race condition of these two
340 // calls
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700341 if (updating)
Willy Tu03913172021-11-08 02:03:19 -0800342 {
343 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
344 }
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700345 });
Willy Tu03913172021-11-08 02:03:19 -0800346}
347
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700348inline void requestRoutesDrive(App& app)
349{
350 BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
Ed Tanoused398212021-06-09 17:05:54 -0700351 .privileges(redfish::privileges::getDrive)
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700352 .methods(
353 boost::beast::http::verb::get)([](const crow::Request&,
354 const std::shared_ptr<
355 bmcweb::AsyncResp>& asyncResp,
356 const std::string& driveId) {
357 crow::connections::systemBus->async_method_call(
358 [asyncResp,
359 driveId](const boost::system::error_code ec,
360 const crow::openbmc_mapper::GetSubTreeType& subtree) {
361 if (ec)
362 {
363 BMCWEB_LOG_ERROR << "Drive mapper call error";
364 messages::internalError(asyncResp->res);
365 return;
366 }
367
Willy Tu03913172021-11-08 02:03:19 -0800368 auto drive = std::find_if(
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700369 subtree.begin(), subtree.end(),
Willy Tu03913172021-11-08 02:03:19 -0800370 [&driveId](const std::pair<
371 std::string,
372 std::vector<std::pair<
373 std::string, std::vector<std::string>>>>&
374 object) {
375 return sdbusplus::message::object_path(object.first)
376 .filename() == driveId;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700377 });
378
Willy Tu03913172021-11-08 02:03:19 -0800379 if (drive == subtree.end())
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700380 {
381 messages::resourceNotFound(asyncResp->res, "Drive",
382 driveId);
383 return;
384 }
385
Willy Tu03913172021-11-08 02:03:19 -0800386 const std::string& path = drive->first;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700387 const std::vector<
388 std::pair<std::string, std::vector<std::string>>>&
Willy Tu03913172021-11-08 02:03:19 -0800389 connectionNames = drive->second;
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700390
391 asyncResp->res.jsonValue["@odata.type"] =
392 "#Drive.v1_7_0.Drive";
393 asyncResp->res.jsonValue["@odata.id"] =
394 "/redfish/v1/Systems/system/Storage/1/Drives/" +
395 driveId;
396 asyncResp->res.jsonValue["Name"] = driveId;
397 asyncResp->res.jsonValue["Id"] = driveId;
398
399 if (connectionNames.size() != 1)
James Feiste284a7c2019-11-20 16:20:23 -0800400 {
401 BMCWEB_LOG_ERROR << "Connection size "
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700402 << connectionNames.size()
Willy Tu03913172021-11-08 02:03:19 -0800403 << ", not equal to 1";
James Feiste284a7c2019-11-20 16:20:23 -0800404 messages::internalError(asyncResp->res);
405 return;
406 }
407
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700408 getMainChassisId(
409 asyncResp,
410 [](const std::string& chassisId,
411 const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
412 aRsp->res.jsonValue["Links"]["Chassis"] = {
413 {"@odata.id",
414 "/redfish/v1/Chassis/" + chassisId}};
415 });
416
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700417 // default it to Enabled
418 asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
419
420 auto health = std::make_shared<HealthPopulate>(asyncResp);
James Feiste284a7c2019-11-20 16:20:23 -0800421 health->inventory.emplace_back(path);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700422 health->populate();
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700423
Willy Tu03913172021-11-08 02:03:19 -0800424 const std::string& connectionName =
425 connectionNames[0].first;
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700426
Willy Tu03913172021-11-08 02:03:19 -0800427 getDriveAsset(asyncResp, connectionName, path);
428 getDrivePresent(asyncResp, connectionName, path);
429 getDriveState(asyncResp, connectionName, path);
John Edward Broadbent7e860f12021-04-08 15:57:16 -0700430 },
431 "xyz.openbmc_project.ObjectMapper",
432 "/xyz/openbmc_project/object_mapper",
433 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
434 "/xyz/openbmc_project/inventory", int32_t(0),
435 std::array<const char*, 1>{
436 "xyz.openbmc_project.Inventory.Item.Drive"});
437 });
438}
Nikhil Potadea25aecc2019-08-23 16:35:26 -0700439} // namespace redfish