xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 3fe4d5cca464ff7696236fe42acf64637e9126d9)
1a25aeccfSNikhil Potade /*
2a25aeccfSNikhil Potade // Copyright (c) 2019 Intel Corporation
3a25aeccfSNikhil Potade //
4a25aeccfSNikhil Potade // Licensed under the Apache License, Version 2.0 (the "License");
5a25aeccfSNikhil Potade // you may not use this file except in compliance with the License.
6a25aeccfSNikhil Potade // You may obtain a copy of the License at
7a25aeccfSNikhil Potade //
8a25aeccfSNikhil Potade //      http://www.apache.org/licenses/LICENSE-2.0
9a25aeccfSNikhil Potade //
10a25aeccfSNikhil Potade // Unless required by applicable law or agreed to in writing, software
11a25aeccfSNikhil Potade // distributed under the License is distributed on an "AS IS" BASIS,
12a25aeccfSNikhil Potade // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a25aeccfSNikhil Potade // See the License for the specific language governing permissions and
14a25aeccfSNikhil Potade // limitations under the License.
15a25aeccfSNikhil Potade */
16a25aeccfSNikhil Potade #pragma once
17a25aeccfSNikhil Potade 
182ad9c2f6SJames Feist #include "health.hpp"
19e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
202ad9c2f6SJames Feist 
217e860f15SJohn Edward Broadbent #include <app.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
26a25aeccfSNikhil Potade 
27a25aeccfSNikhil Potade namespace redfish
28a25aeccfSNikhil Potade {
297e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
30a25aeccfSNikhil Potade {
317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
32ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3445ca1b86SEd Tanous             [&app](const crow::Request& req,
357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
363ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
3745ca1b86SEd Tanous         {
3845ca1b86SEd Tanous             return;
3945ca1b86SEd Tanous         }
408d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
418d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
428d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
438d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
448d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
451476687dSEd Tanous         nlohmann::json::array_t members;
461476687dSEd Tanous         nlohmann::json::object_t member;
471476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
481476687dSEd Tanous         members.emplace_back(member);
491476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
508d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
517e860f15SJohn Edward Broadbent         });
52a25aeccfSNikhil Potade }
53a25aeccfSNikhil Potade 
54a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
55a85afbe1SWilly Tu                       const std::shared_ptr<HealthPopulate>& health)
56a25aeccfSNikhil Potade {
57a25aeccfSNikhil Potade     crow::connections::systemBus->async_method_call(
58a85afbe1SWilly Tu         [asyncResp, health](
59a85afbe1SWilly Tu             const boost::system::error_code ec,
60a85afbe1SWilly Tu             const dbus::utility::MapperGetSubTreePathsResponse& driveList) {
61a25aeccfSNikhil Potade         if (ec)
62a25aeccfSNikhil Potade         {
63a25aeccfSNikhil Potade             BMCWEB_LOG_ERROR << "Drive mapper call error";
64a25aeccfSNikhil Potade             messages::internalError(asyncResp->res);
65a25aeccfSNikhil Potade             return;
66a25aeccfSNikhil Potade         }
672ad9c2f6SJames Feist 
68a85afbe1SWilly Tu         nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"];
69a85afbe1SWilly Tu         driveArray = nlohmann::json::array();
70a85afbe1SWilly Tu         auto& count = asyncResp->res.jsonValue["Drives@odata.count"];
71a85afbe1SWilly Tu         count = 0;
722ad9c2f6SJames Feist 
73a85afbe1SWilly Tu         health->inventory.insert(health->inventory.end(), driveList.begin(),
74a85afbe1SWilly Tu                                  driveList.end());
75a85afbe1SWilly Tu 
76a85afbe1SWilly Tu         for (const std::string& drive : driveList)
77a25aeccfSNikhil Potade         {
78a85afbe1SWilly Tu             sdbusplus::message::object_path object(drive);
79a85afbe1SWilly Tu             if (object.filename().empty())
80a25aeccfSNikhil Potade             {
81a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << drive;
82a85afbe1SWilly Tu                 return;
83a25aeccfSNikhil Potade             }
84a85afbe1SWilly Tu 
85a85afbe1SWilly Tu             nlohmann::json::object_t driveJson;
86a85afbe1SWilly Tu             driveJson["@odata.id"] =
87be13ceceSJames Feist                 "/redfish/v1/Systems/system/Storage/1/Drives/" +
88a85afbe1SWilly Tu                 object.filename();
89a85afbe1SWilly Tu             driveArray.push_back(std::move(driveJson));
90a25aeccfSNikhil Potade         }
91a25aeccfSNikhil Potade 
92a85afbe1SWilly Tu         count = driveArray.size();
93a25aeccfSNikhil Potade         },
94a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper",
95a25aeccfSNikhil Potade         "/xyz/openbmc_project/object_mapper",
96a25aeccfSNikhil Potade         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
97a25aeccfSNikhil Potade         "/xyz/openbmc_project/inventory", int32_t(0),
98a85afbe1SWilly Tu         std::array<const char*, 1>{"xyz.openbmc_project.Inventory.Item.Drive"});
99a85afbe1SWilly Tu }
100e284a7c1SJames Feist 
101a85afbe1SWilly Tu inline void
102a85afbe1SWilly Tu     getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
103a85afbe1SWilly Tu                           const std::shared_ptr<HealthPopulate>& health)
104a85afbe1SWilly Tu {
105e284a7c1SJames Feist     crow::connections::systemBus->async_method_call(
106002d39b4SEd Tanous         [asyncResp,
107002d39b4SEd Tanous          health](const boost::system::error_code ec,
108b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreeResponse& subtree) {
10926f6976fSEd Tanous         if (ec || subtree.empty())
110e284a7c1SJames Feist         {
111d819a420SJames Feist             // doesn't have to be there
112e284a7c1SJames Feist             return;
113e284a7c1SJames Feist         }
114e284a7c1SJames Feist 
115a85afbe1SWilly Tu         nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"];
116e284a7c1SJames Feist         root = nlohmann::json::array();
117e284a7c1SJames Feist         for (const auto& [path, interfaceDict] : subtree)
118e284a7c1SJames Feist         {
119a85afbe1SWilly Tu             sdbusplus::message::object_path object(path);
120a85afbe1SWilly Tu             std::string id = object.filename();
121a85afbe1SWilly Tu             if (id.empty())
122e284a7c1SJames Feist             {
123a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Failed to find filename in " << path;
124e284a7c1SJames Feist                 return;
125e284a7c1SJames Feist             }
126e284a7c1SJames Feist 
127e284a7c1SJames Feist             if (interfaceDict.size() != 1)
128e284a7c1SJames Feist             {
129a85afbe1SWilly Tu                 BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size()
130e284a7c1SJames Feist                                  << ", greater than 1";
131e284a7c1SJames Feist                 messages::internalError(asyncResp->res);
132e284a7c1SJames Feist                 return;
133e284a7c1SJames Feist             }
134e284a7c1SJames Feist 
135002d39b4SEd Tanous             const std::string& connectionName = interfaceDict.front().first;
136e284a7c1SJames Feist 
137e284a7c1SJames Feist             size_t index = root.size();
138e284a7c1SJames Feist             nlohmann::json& storageController =
139e284a7c1SJames Feist                 root.emplace_back(nlohmann::json::object());
140e284a7c1SJames Feist 
141e284a7c1SJames Feist             storageController["@odata.type"] =
142e284a7c1SJames Feist                 "#Storage.v1_7_0.StorageController";
143e284a7c1SJames Feist             storageController["@odata.id"] =
1440fda0f12SGeorge Liu                 "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
145e284a7c1SJames Feist                 std::to_string(index);
146e284a7c1SJames Feist             storageController["Name"] = id;
147e284a7c1SJames Feist             storageController["MemberId"] = id;
148e284a7c1SJames Feist             storageController["Status"]["State"] = "Enabled";
149e284a7c1SJames Feist 
1501e1e598dSJonathan Doman             sdbusplus::asio::getProperty<bool>(
1511e1e598dSJonathan Doman                 *crow::connections::systemBus, connectionName, path,
1521e1e598dSJonathan Doman                 "xyz.openbmc_project.Inventory.Item", "Present",
153002d39b4SEd Tanous                 [asyncResp, index](const boost::system::error_code ec2,
1541e1e598dSJonathan Doman                                    bool enabled) {
1557e860f15SJohn Edward Broadbent                 // this interface isn't necessary, only check it
1567e860f15SJohn Edward Broadbent                 // if we get a good return
15723a21a1cSEd Tanous                 if (ec2)
158e284a7c1SJames Feist                 {
159e284a7c1SJames Feist                     return;
160e284a7c1SJames Feist                 }
1611e1e598dSJonathan Doman                 if (!enabled)
162e284a7c1SJames Feist                 {
163002d39b4SEd Tanous                     asyncResp->res.jsonValue["StorageControllers"][index]
164a85afbe1SWilly Tu                                             ["Status"]["State"] = "Disabled";
165e284a7c1SJames Feist                 }
1661e1e598dSJonathan Doman                 });
167e284a7c1SJames Feist 
168e284a7c1SJames Feist             crow::connections::systemBus->async_method_call(
169a85afbe1SWilly Tu                 [asyncResp, index](
170a85afbe1SWilly Tu                     const boost::system::error_code ec2,
171a85afbe1SWilly Tu                     const std::vector<
172a85afbe1SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
1737e860f15SJohn Edward Broadbent                         propertiesList) {
1747e860f15SJohn Edward Broadbent                 if (ec2)
1757e860f15SJohn Edward Broadbent                 {
1767e860f15SJohn Edward Broadbent                     // this interface isn't necessary
1777e860f15SJohn Edward Broadbent                     return;
1787e860f15SJohn Edward Broadbent                 }
179002d39b4SEd Tanous                 for (const std::pair<std::string,
180a85afbe1SWilly Tu                                      dbus::utility::DbusVariantType>& property :
181a85afbe1SWilly Tu                      propertiesList)
1827e860f15SJohn Edward Broadbent                 {
1837e860f15SJohn Edward Broadbent                     // Store DBus properties that are also
1847e860f15SJohn Edward Broadbent                     // Redfish properties with same name and a
1857e860f15SJohn Edward Broadbent                     // string value
186002d39b4SEd Tanous                     const std::string& propertyName = property.first;
1878a592810SEd Tanous                     nlohmann::json& controller =
188a85afbe1SWilly Tu                         asyncResp->res.jsonValue["StorageControllers"][index];
1897e860f15SJohn Edward Broadbent                     if ((propertyName == "PartNumber") ||
1907e860f15SJohn Edward Broadbent                         (propertyName == "SerialNumber") ||
1917e860f15SJohn Edward Broadbent                         (propertyName == "Manufacturer") ||
1927e860f15SJohn Edward Broadbent                         (propertyName == "Model"))
1937e860f15SJohn Edward Broadbent                     {
1947e860f15SJohn Edward Broadbent                         const std::string* value =
195002d39b4SEd Tanous                             std::get_if<std::string>(&property.second);
1967e860f15SJohn Edward Broadbent                         if (value == nullptr)
1977e860f15SJohn Edward Broadbent                         {
1987e860f15SJohn Edward Broadbent                             // illegal property
199002d39b4SEd Tanous                             messages::internalError(asyncResp->res);
2007e860f15SJohn Edward Broadbent                             return;
2017e860f15SJohn Edward Broadbent                         }
2028a592810SEd Tanous                         controller[propertyName] = *value;
2037e860f15SJohn Edward Broadbent                     }
2047e860f15SJohn Edward Broadbent                 }
2057e860f15SJohn Edward Broadbent                 },
206002d39b4SEd Tanous                 connectionName, path, "org.freedesktop.DBus.Properties",
207002d39b4SEd Tanous                 "GetAll", "xyz.openbmc_project.Inventory.Decorator.Asset");
2087e860f15SJohn Edward Broadbent         }
2097e860f15SJohn Edward Broadbent 
2107e860f15SJohn Edward Broadbent         // this is done after we know the json array will no longer
2117e860f15SJohn Edward Broadbent         // be resized, as json::array uses vector underneath and we
2127e860f15SJohn Edward Broadbent         // need references to its members that won't change
2137e860f15SJohn Edward Broadbent         size_t count = 0;
214dfababfcSNan Zhou         // Pointer based on |asyncResp->res.jsonValue|
215dfababfcSNan Zhou         nlohmann::json::json_pointer rootPtr =
216dfababfcSNan Zhou             "/StorageControllers"_json_pointer;
2177e860f15SJohn Edward Broadbent         for (const auto& [path, interfaceDict] : subtree)
2187e860f15SJohn Edward Broadbent         {
2197e860f15SJohn Edward Broadbent             auto subHealth = std::make_shared<HealthPopulate>(
220dfababfcSNan Zhou                 asyncResp, rootPtr / count / "Status");
2217e860f15SJohn Edward Broadbent             subHealth->inventory.emplace_back(path);
2227e860f15SJohn Edward Broadbent             health->inventory.emplace_back(path);
2237e860f15SJohn Edward Broadbent             health->children.emplace_back(subHealth);
2247e860f15SJohn Edward Broadbent             count++;
2257e860f15SJohn Edward Broadbent         }
2267e860f15SJohn Edward Broadbent         },
2277e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
2287e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
2297e860f15SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2307e860f15SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", int32_t(0),
2317e860f15SJohn Edward Broadbent         std::array<const char*, 1>{
2327e860f15SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.StorageController"});
233a85afbe1SWilly Tu }
234a85afbe1SWilly Tu 
235a85afbe1SWilly Tu inline void requestRoutesStorage(App& app)
236a85afbe1SWilly Tu {
237a85afbe1SWilly Tu     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
238a85afbe1SWilly Tu         .privileges(redfish::privileges::getStorage)
239a85afbe1SWilly Tu         .methods(boost::beast::http::verb::get)(
240a85afbe1SWilly Tu             [&app](const crow::Request& req,
241a85afbe1SWilly Tu                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
2423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
243a85afbe1SWilly Tu         {
244a85afbe1SWilly Tu             return;
245a85afbe1SWilly Tu         }
246a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
247a85afbe1SWilly Tu         asyncResp->res.jsonValue["@odata.id"] =
248a85afbe1SWilly Tu             "/redfish/v1/Systems/system/Storage/1";
249a85afbe1SWilly Tu         asyncResp->res.jsonValue["Name"] = "Storage";
250a85afbe1SWilly Tu         asyncResp->res.jsonValue["Id"] = "1";
251a85afbe1SWilly Tu         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
252a85afbe1SWilly Tu 
253a85afbe1SWilly Tu         auto health = std::make_shared<HealthPopulate>(asyncResp);
254a85afbe1SWilly Tu         health->populate();
255a85afbe1SWilly Tu 
256a85afbe1SWilly Tu         getDrives(asyncResp, health);
257a85afbe1SWilly Tu         getStorageControllers(asyncResp, health);
2587e860f15SJohn Edward Broadbent         });
2597e860f15SJohn Edward Broadbent }
2607e860f15SJohn Edward Broadbent 
26103913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26203913171SWilly Tu                           const std::string& connectionName,
26303913171SWilly Tu                           const std::string& path)
26403913171SWilly Tu {
26503913171SWilly Tu     crow::connections::systemBus->async_method_call(
266168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
267168e20c1SEd Tanous                     const std::vector<
268168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
26903913171SWilly Tu                         propertiesList) {
27003913171SWilly Tu         if (ec)
27103913171SWilly Tu         {
27203913171SWilly Tu             // this interface isn't necessary
27303913171SWilly Tu             return;
27403913171SWilly Tu         }
275168e20c1SEd Tanous         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
27603913171SWilly Tu                  property : propertiesList)
27703913171SWilly Tu         {
27803913171SWilly Tu             // Store DBus properties that are also
27903913171SWilly Tu             // Redfish properties with same name and a
28003913171SWilly Tu             // string value
28103913171SWilly Tu             const std::string& propertyName = property.first;
28203913171SWilly Tu             if ((propertyName == "PartNumber") ||
28303913171SWilly Tu                 (propertyName == "SerialNumber") ||
284002d39b4SEd Tanous                 (propertyName == "Manufacturer") || (propertyName == "Model"))
28503913171SWilly Tu             {
28603913171SWilly Tu                 const std::string* value =
28703913171SWilly Tu                     std::get_if<std::string>(&property.second);
28803913171SWilly Tu                 if (value == nullptr)
28903913171SWilly Tu                 {
29003913171SWilly Tu                     // illegal property
29103913171SWilly Tu                     messages::internalError(asyncResp->res);
29203913171SWilly Tu                     return;
29303913171SWilly Tu                 }
29403913171SWilly Tu                 asyncResp->res.jsonValue[propertyName] = *value;
29503913171SWilly Tu             }
29603913171SWilly Tu         }
29703913171SWilly Tu         },
29803913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
29903913171SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset");
30003913171SWilly Tu }
30103913171SWilly Tu 
30203913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30303913171SWilly Tu                             const std::string& connectionName,
30403913171SWilly Tu                             const std::string& path)
30503913171SWilly Tu {
3061e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3071e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3081e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
30903913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3101e1e598dSJonathan Doman                           const bool enabled) {
31103913171SWilly Tu         // this interface isn't necessary, only check it if
31203913171SWilly Tu         // we get a good return
31303913171SWilly Tu         if (ec)
31403913171SWilly Tu         {
31503913171SWilly Tu             return;
31603913171SWilly Tu         }
31703913171SWilly Tu 
3181e1e598dSJonathan Doman         if (!enabled)
31903913171SWilly Tu         {
32003913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
32103913171SWilly Tu         }
3221e1e598dSJonathan Doman         });
32303913171SWilly Tu }
32403913171SWilly Tu 
32503913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32603913171SWilly Tu                           const std::string& connectionName,
32703913171SWilly Tu                           const std::string& path)
32803913171SWilly Tu {
3291e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3301e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3311e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3321e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
33303913171SWilly Tu         // this interface isn't necessary, only check it
33403913171SWilly Tu         // if we get a good return
33503913171SWilly Tu         if (ec)
33603913171SWilly Tu         {
33703913171SWilly Tu             return;
33803913171SWilly Tu         }
33903913171SWilly Tu 
34003913171SWilly Tu         // updating and disabled in the backend shouldn't be
34103913171SWilly Tu         // able to be set at the same time, so we don't need
34203913171SWilly Tu         // to check for the race condition of these two
34303913171SWilly Tu         // calls
3441e1e598dSJonathan Doman         if (updating)
34503913171SWilly Tu         {
34603913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
34703913171SWilly Tu         }
3481e1e598dSJonathan Doman         });
34903913171SWilly Tu }
35003913171SWilly Tu 
35119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
35219b8e9a0SWilly Tu {
35319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
35419b8e9a0SWilly Tu     {
35519b8e9a0SWilly Tu         return "HDD";
35619b8e9a0SWilly Tu     }
35719b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
35819b8e9a0SWilly Tu     {
35919b8e9a0SWilly Tu         return "SSD";
36019b8e9a0SWilly Tu     }
36119b8e9a0SWilly Tu 
36219b8e9a0SWilly Tu     return std::nullopt;
36319b8e9a0SWilly Tu }
36419b8e9a0SWilly Tu 
36519b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
36619b8e9a0SWilly Tu {
36719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
36819b8e9a0SWilly Tu     {
36919b8e9a0SWilly Tu         return "SAS";
37019b8e9a0SWilly Tu     }
37119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
37219b8e9a0SWilly Tu     {
37319b8e9a0SWilly Tu         return "SATA";
37419b8e9a0SWilly Tu     }
37519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
37619b8e9a0SWilly Tu     {
37719b8e9a0SWilly Tu         return "NVMe";
37819b8e9a0SWilly Tu     }
37919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
38019b8e9a0SWilly Tu     {
38119b8e9a0SWilly Tu         return "FC";
38219b8e9a0SWilly Tu     }
38319b8e9a0SWilly Tu 
38419b8e9a0SWilly Tu     return std::nullopt;
38519b8e9a0SWilly Tu }
38619b8e9a0SWilly Tu 
38719b8e9a0SWilly Tu inline void
38819b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38919b8e9a0SWilly Tu                            const std::string& connectionName,
39019b8e9a0SWilly Tu                            const std::string& path)
39119b8e9a0SWilly Tu {
39219b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
39319b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
39419b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
39519b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
39619b8e9a0SWilly Tu                     const std::vector<
39719b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
39819b8e9a0SWilly Tu                         propertiesList) {
39919b8e9a0SWilly Tu         if (ec)
40019b8e9a0SWilly Tu         {
40119b8e9a0SWilly Tu             // this interface isn't required
40219b8e9a0SWilly Tu             return;
40319b8e9a0SWilly Tu         }
40419b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
40519b8e9a0SWilly Tu                  property : propertiesList)
40619b8e9a0SWilly Tu         {
40719b8e9a0SWilly Tu             const std::string& propertyName = property.first;
40819b8e9a0SWilly Tu             if (propertyName == "Type")
40919b8e9a0SWilly Tu             {
41019b8e9a0SWilly Tu                 const std::string* value =
41119b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
41219b8e9a0SWilly Tu                 if (value == nullptr)
41319b8e9a0SWilly Tu                 {
41419b8e9a0SWilly Tu                     // illegal property
41519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
41619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
41719b8e9a0SWilly Tu                     return;
41819b8e9a0SWilly Tu                 }
41919b8e9a0SWilly Tu 
420002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
42119b8e9a0SWilly Tu                 if (!mediaType)
42219b8e9a0SWilly Tu                 {
42319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
42419b8e9a0SWilly Tu                                      << *value;
42519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
42619b8e9a0SWilly Tu                     return;
42719b8e9a0SWilly Tu                 }
42819b8e9a0SWilly Tu 
42919b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
43019b8e9a0SWilly Tu             }
43119b8e9a0SWilly Tu             else if (propertyName == "Capacity")
43219b8e9a0SWilly Tu             {
43319b8e9a0SWilly Tu                 const uint64_t* capacity =
43419b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
43519b8e9a0SWilly Tu                 if (capacity == nullptr)
43619b8e9a0SWilly Tu                 {
43719b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
43819b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
43919b8e9a0SWilly Tu                     return;
44019b8e9a0SWilly Tu                 }
44119b8e9a0SWilly Tu                 if (*capacity == 0)
44219b8e9a0SWilly Tu                 {
44319b8e9a0SWilly Tu                     // drive capacity not known
44419b8e9a0SWilly Tu                     continue;
44519b8e9a0SWilly Tu                 }
44619b8e9a0SWilly Tu 
44719b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
44819b8e9a0SWilly Tu             }
44919b8e9a0SWilly Tu             else if (propertyName == "Protocol")
45019b8e9a0SWilly Tu             {
45119b8e9a0SWilly Tu                 const std::string* value =
45219b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
45319b8e9a0SWilly Tu                 if (value == nullptr)
45419b8e9a0SWilly Tu                 {
45519b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
45619b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45719b8e9a0SWilly Tu                     return;
45819b8e9a0SWilly Tu                 }
45919b8e9a0SWilly Tu 
460002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
46119b8e9a0SWilly Tu                 if (!proto)
46219b8e9a0SWilly Tu                 {
463002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
46419b8e9a0SWilly Tu                                      << *value;
46519b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46619b8e9a0SWilly Tu                     return;
46719b8e9a0SWilly Tu                 }
46819b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
46919b8e9a0SWilly Tu             }
470*3fe4d5ccSJohn Edward Broadbent             else if (propertyName == "PredictedMediaLifeLeftPercent")
471*3fe4d5ccSJohn Edward Broadbent             {
472*3fe4d5ccSJohn Edward Broadbent                 const uint8_t* lifeLeft =
473*3fe4d5ccSJohn Edward Broadbent                     std::get_if<uint8_t>(&property.second);
474*3fe4d5ccSJohn Edward Broadbent                 if (lifeLeft == nullptr)
475*3fe4d5ccSJohn Edward Broadbent                 {
476*3fe4d5ccSJohn Edward Broadbent                     BMCWEB_LOG_ERROR
477*3fe4d5ccSJohn Edward Broadbent                         << "Illegal property: PredictedMediaLifeLeftPercent";
478*3fe4d5ccSJohn Edward Broadbent                     messages::internalError(asyncResp->res);
479*3fe4d5ccSJohn Edward Broadbent                     return;
480*3fe4d5ccSJohn Edward Broadbent                 }
481*3fe4d5ccSJohn Edward Broadbent                 // 255 means reading the value is not supported
482*3fe4d5ccSJohn Edward Broadbent                 if (*lifeLeft != 255)
483*3fe4d5ccSJohn Edward Broadbent                 {
484*3fe4d5ccSJohn Edward Broadbent                     asyncResp->res.jsonValue["PredictedMediaLifeLeftPercent"] =
485*3fe4d5ccSJohn Edward Broadbent                         *lifeLeft;
486*3fe4d5ccSJohn Edward Broadbent                 }
487*3fe4d5ccSJohn Edward Broadbent             }
48819b8e9a0SWilly Tu         }
48919b8e9a0SWilly Tu         });
49019b8e9a0SWilly Tu }
49119b8e9a0SWilly Tu 
492b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
493b53dcd9dSNan Zhou                             const std::string& connectionName,
494b53dcd9dSNan Zhou                             const std::string& path,
495e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
496e56ed6b9SJohn Edward Broadbent {
497e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
498e56ed6b9SJohn Edward Broadbent     {
499e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
500e56ed6b9SJohn Edward Broadbent         {
501e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
502e56ed6b9SJohn Edward Broadbent         }
503e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
504e56ed6b9SJohn Edward Broadbent         {
505e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
506e56ed6b9SJohn Edward Broadbent         }
507e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
508e56ed6b9SJohn Edward Broadbent         {
509e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
510e56ed6b9SJohn Edward Broadbent         }
511e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
512e56ed6b9SJohn Edward Broadbent         {
513e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
514e56ed6b9SJohn Edward Broadbent         }
515e56ed6b9SJohn Edward Broadbent     }
516e56ed6b9SJohn Edward Broadbent }
517e56ed6b9SJohn Edward Broadbent 
5187e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5197e860f15SJohn Edward Broadbent {
5207e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
521ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
522002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
523002d39b4SEd Tanous             [&app](const crow::Request& req,
52445ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5257e860f15SJohn Edward Broadbent                    const std::string& driveId) {
5263ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
52745ca1b86SEd Tanous         {
52845ca1b86SEd Tanous             return;
52945ca1b86SEd Tanous         }
5307e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
531002d39b4SEd Tanous             [asyncResp,
532002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
533b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5347e860f15SJohn Edward Broadbent             if (ec)
5357e860f15SJohn Edward Broadbent             {
5367e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5377e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5387e860f15SJohn Edward Broadbent                 return;
5397e860f15SJohn Edward Broadbent             }
5407e860f15SJohn Edward Broadbent 
54103913171SWilly Tu             auto drive = std::find_if(
5427e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
543002d39b4SEd Tanous                 [&driveId](
5448cb65f8aSNan Zhou                     const std::pair<std::string,
5458cb65f8aSNan Zhou                                     dbus::utility::MapperServiceMap>& object) {
54603913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
54703913171SWilly Tu                            .filename() == driveId;
5487e860f15SJohn Edward Broadbent                 });
5497e860f15SJohn Edward Broadbent 
55003913171SWilly Tu             if (drive == subtree.end())
5517e860f15SJohn Edward Broadbent             {
552002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5537e860f15SJohn Edward Broadbent                 return;
5547e860f15SJohn Edward Broadbent             }
5557e860f15SJohn Edward Broadbent 
55603913171SWilly Tu             const std::string& path = drive->first;
5578cb65f8aSNan Zhou             const dbus::utility::MapperServiceMap& connectionNames =
5588cb65f8aSNan Zhou                 drive->second;
5597e860f15SJohn Edward Broadbent 
560002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
5617e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
562002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
5637e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
5647e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
5657e860f15SJohn Edward Broadbent 
5667e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
5677e860f15SJohn Edward Broadbent             {
568002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
56903913171SWilly Tu                                  << ", not equal to 1";
5707e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5717e860f15SJohn Edward Broadbent                 return;
5727e860f15SJohn Edward Broadbent             }
5737e860f15SJohn Edward Broadbent 
5747e860f15SJohn Edward Broadbent             getMainChassisId(
575002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
5767e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
577002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
5781476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
5797e860f15SJohn Edward Broadbent                 });
5807e860f15SJohn Edward Broadbent 
581a25aeccfSNikhil Potade             // default it to Enabled
582a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
583a25aeccfSNikhil Potade 
5842ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
585e284a7c1SJames Feist             health->inventory.emplace_back(path);
5862ad9c2f6SJames Feist             health->populate();
5872ad9c2f6SJames Feist 
588e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
589e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
590a25aeccfSNikhil Potade             },
591a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
592a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
593a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
594a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
595a25aeccfSNikhil Potade             std::array<const char*, 1>{
596a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
5977e860f15SJohn Edward Broadbent         });
598a25aeccfSNikhil Potade }
59992903bd4SJohn Edward Broadbent 
60092903bd4SJohn Edward Broadbent /**
60192903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
60292903bd4SJohn Edward Broadbent  * information
60392903bd4SJohn Edward Broadbent  */
604b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
60592903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
60692903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
60792903bd4SJohn Edward Broadbent     const std::string& chassisId)
60892903bd4SJohn Edward Broadbent {
6093ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
61092903bd4SJohn Edward Broadbent     {
61192903bd4SJohn Edward Broadbent         return;
61292903bd4SJohn Edward Broadbent     }
61392903bd4SJohn Edward Broadbent 
61492903bd4SJohn Edward Broadbent     // mapper call lambda
61592903bd4SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
61692903bd4SJohn Edward Broadbent         [asyncResp,
61792903bd4SJohn Edward Broadbent          chassisId](const boost::system::error_code ec,
61892903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
61992903bd4SJohn Edward Broadbent         if (ec)
62092903bd4SJohn Edward Broadbent         {
62192903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
62292903bd4SJohn Edward Broadbent             {
62392903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
62492903bd4SJohn Edward Broadbent                                            chassisId);
62592903bd4SJohn Edward Broadbent                 return;
62692903bd4SJohn Edward Broadbent             }
62792903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
62892903bd4SJohn Edward Broadbent             return;
62992903bd4SJohn Edward Broadbent         }
63092903bd4SJohn Edward Broadbent 
63192903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
6328cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
63392903bd4SJohn Edward Broadbent         {
63492903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
63592903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
63692903bd4SJohn Edward Broadbent             {
63792903bd4SJohn Edward Broadbent                 continue;
63892903bd4SJohn Edward Broadbent             }
63992903bd4SJohn Edward Broadbent 
64092903bd4SJohn Edward Broadbent             if (connectionNames.empty())
64192903bd4SJohn Edward Broadbent             {
64292903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
64392903bd4SJohn Edward Broadbent                 continue;
64492903bd4SJohn Edward Broadbent             }
64592903bd4SJohn Edward Broadbent 
64692903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
64792903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
64892903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
64914bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
65014bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
65192903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
65292903bd4SJohn Edward Broadbent 
65392903bd4SJohn Edward Broadbent             // Association lambda
65492903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
65592903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
65692903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
65792903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
65892903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
65992903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
66092903bd4SJohn Edward Broadbent                 if (ec3)
66192903bd4SJohn Edward Broadbent                 {
66292903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
66392903bd4SJohn Edward Broadbent                 }
66492903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
66592903bd4SJohn Edward Broadbent                 // important if array is empty
66692903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
66792903bd4SJohn Edward Broadbent 
66892903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
66992903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
67092903bd4SJohn Edward Broadbent                 {
6718a592810SEd Tanous                     sdbusplus::message::object_path drivePath(drive);
6728a592810SEd Tanous                     leafNames.push_back(drivePath.filename());
67392903bd4SJohn Edward Broadbent                 }
67492903bd4SJohn Edward Broadbent 
67592903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
67692903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
67792903bd4SJohn Edward Broadbent 
67892903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
67992903bd4SJohn Edward Broadbent                 {
68092903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
68192903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
68292903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
68392903bd4SJohn Edward Broadbent                         leafName);
68492903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
68592903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
68692903bd4SJohn Edward Broadbent                 }
68792903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
68892903bd4SJohn Edward Broadbent                 }); // end association lambda
68992903bd4SJohn Edward Broadbent 
69092903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
69192903bd4SJohn Edward Broadbent         },
69292903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
69392903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
69492903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
69592903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0,
69692903bd4SJohn Edward Broadbent         std::array<const char*, 2>{
69792903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Board",
69892903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Chassis"});
69992903bd4SJohn Edward Broadbent }
70092903bd4SJohn Edward Broadbent 
70192903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
70292903bd4SJohn Edward Broadbent {
70392903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
70492903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
70592903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
70692903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
70792903bd4SJohn Edward Broadbent }
70892903bd4SJohn Edward Broadbent 
709b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
710b53dcd9dSNan Zhou                        const std::string& chassisId,
711b53dcd9dSNan Zhou                        const std::string& driveName,
712e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
713e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
714e56ed6b9SJohn Edward Broadbent {
715e56ed6b9SJohn Edward Broadbent 
716e56ed6b9SJohn Edward Broadbent     if (ec)
717e56ed6b9SJohn Edward Broadbent     {
718e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
719e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
720e56ed6b9SJohn Edward Broadbent         return;
721e56ed6b9SJohn Edward Broadbent     }
722e56ed6b9SJohn Edward Broadbent 
723e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
7248cb65f8aSNan Zhou     for (const auto& [path, connectionNames] : subtree)
725e56ed6b9SJohn Edward Broadbent     {
726e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
727e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
728e56ed6b9SJohn Edward Broadbent         {
729e56ed6b9SJohn Edward Broadbent             continue;
730e56ed6b9SJohn Edward Broadbent         }
731e56ed6b9SJohn Edward Broadbent 
732e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
733e56ed6b9SJohn Edward Broadbent         {
734e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
735e56ed6b9SJohn Edward Broadbent             continue;
736e56ed6b9SJohn Edward Broadbent         }
737e56ed6b9SJohn Edward Broadbent 
738e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
739a0cb40cbSJohn Edward Broadbent             "redfish", "v1", "Chassis", chassisId, "Drives", driveName);
740e56ed6b9SJohn Edward Broadbent 
741e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
742a0cb40cbSJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = driveName;
743e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
744e56ed6b9SJohn Edward Broadbent         // default it to Enabled
745e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
746e56ed6b9SJohn Edward Broadbent 
747e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
748e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
749e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
750e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
751e56ed6b9SJohn Edward Broadbent 
752e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
753e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
754e56ed6b9SJohn Edward Broadbent     }
755e56ed6b9SJohn Edward Broadbent }
756e56ed6b9SJohn Edward Broadbent 
757b53dcd9dSNan Zhou inline void
758b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
759e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
760e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
761e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
762e56ed6b9SJohn Edward Broadbent {
763e56ed6b9SJohn Edward Broadbent 
764e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
765e56ed6b9SJohn Edward Broadbent     {
766e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
767e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
768e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
769e56ed6b9SJohn Edward Broadbent         {
770e56ed6b9SJohn Edward Broadbent             continue;
771e56ed6b9SJohn Edward Broadbent         }
772e56ed6b9SJohn Edward Broadbent         //  mapper call drive
773e56ed6b9SJohn Edward Broadbent         const std::array<const char*, 1> driveInterface = {
774e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
775e56ed6b9SJohn Edward Broadbent 
776e56ed6b9SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
777e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
778e56ed6b9SJohn Edward Broadbent                 const boost::system::error_code ec,
779e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
780e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
781e56ed6b9SJohn Edward Broadbent             },
782e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
783e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
784e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
785e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", 0, driveInterface);
786e56ed6b9SJohn Edward Broadbent     }
787e56ed6b9SJohn Edward Broadbent }
788e56ed6b9SJohn Edward Broadbent 
789b53dcd9dSNan Zhou inline void
790b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
791e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
792e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
793e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
794e56ed6b9SJohn Edward Broadbent {
79503810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
796e56ed6b9SJohn Edward Broadbent     {
797e56ed6b9SJohn Edward Broadbent         return;
798e56ed6b9SJohn Edward Broadbent     }
799e56ed6b9SJohn Edward Broadbent     const std::array<const char*, 2> interfaces = {
800e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
801e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
802e56ed6b9SJohn Edward Broadbent 
803e56ed6b9SJohn Edward Broadbent     // mapper call chassis
804e56ed6b9SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
805e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
806e56ed6b9SJohn Edward Broadbent          driveName](const boost::system::error_code ec,
807e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
808e56ed6b9SJohn Edward Broadbent         if (ec)
809e56ed6b9SJohn Edward Broadbent         {
810e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
811e56ed6b9SJohn Edward Broadbent             return;
812e56ed6b9SJohn Edward Broadbent         }
813e56ed6b9SJohn Edward Broadbent 
814e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
8158cb65f8aSNan Zhou         for (const auto& [path, connectionNames] : subtree)
816e56ed6b9SJohn Edward Broadbent         {
817e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
818e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
819e56ed6b9SJohn Edward Broadbent             {
820e56ed6b9SJohn Edward Broadbent                 continue;
821e56ed6b9SJohn Edward Broadbent             }
822e56ed6b9SJohn Edward Broadbent 
823e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
824e56ed6b9SJohn Edward Broadbent             {
825e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
826e56ed6b9SJohn Edward Broadbent                 continue;
827e56ed6b9SJohn Edward Broadbent             }
828e56ed6b9SJohn Edward Broadbent 
829e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
830e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
831e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
832e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
833e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
834e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
835e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
836e56ed6b9SJohn Edward Broadbent                 if (ec3)
837e56ed6b9SJohn Edward Broadbent                 {
838e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
839e56ed6b9SJohn Edward Broadbent                 }
840e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
841e56ed6b9SJohn Edward Broadbent                 });
842e56ed6b9SJohn Edward Broadbent             break;
843e56ed6b9SJohn Edward Broadbent         }
844e56ed6b9SJohn Edward Broadbent         },
845e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
846e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
847e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
848e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0, interfaces);
849e56ed6b9SJohn Edward Broadbent }
850e56ed6b9SJohn Edward Broadbent 
851e56ed6b9SJohn Edward Broadbent /**
852e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
853e56ed6b9SJohn Edward Broadbent  */
854e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
855e56ed6b9SJohn Edward Broadbent {
856e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
857e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
858e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
859e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
860e56ed6b9SJohn Edward Broadbent }
861e56ed6b9SJohn Edward Broadbent 
862a25aeccfSNikhil Potade } // namespace redfish
863