xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision b53dcd9d8ed899dc387db272a24c05ed147e4d8f)
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;
1877e860f15SJohn Edward Broadbent                     nlohmann::json& object =
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                         }
2027e860f15SJohn Edward Broadbent                         object[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             }
47019b8e9a0SWilly Tu         }
47119b8e9a0SWilly Tu         });
47219b8e9a0SWilly Tu }
47319b8e9a0SWilly Tu 
474*b53dcd9dSNan Zhou static void addAllDriveInfo(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
475*b53dcd9dSNan Zhou                             const std::string& connectionName,
476*b53dcd9dSNan Zhou                             const std::string& path,
477e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& interfaces)
478e56ed6b9SJohn Edward Broadbent {
479e56ed6b9SJohn Edward Broadbent     for (const std::string& interface : interfaces)
480e56ed6b9SJohn Edward Broadbent     {
481e56ed6b9SJohn Edward Broadbent         if (interface == "xyz.openbmc_project.Inventory.Decorator.Asset")
482e56ed6b9SJohn Edward Broadbent         {
483e56ed6b9SJohn Edward Broadbent             getDriveAsset(asyncResp, connectionName, path);
484e56ed6b9SJohn Edward Broadbent         }
485e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item")
486e56ed6b9SJohn Edward Broadbent         {
487e56ed6b9SJohn Edward Broadbent             getDrivePresent(asyncResp, connectionName, path);
488e56ed6b9SJohn Edward Broadbent         }
489e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.State.Drive")
490e56ed6b9SJohn Edward Broadbent         {
491e56ed6b9SJohn Edward Broadbent             getDriveState(asyncResp, connectionName, path);
492e56ed6b9SJohn Edward Broadbent         }
493e56ed6b9SJohn Edward Broadbent         else if (interface == "xyz.openbmc_project.Inventory.Item.Drive")
494e56ed6b9SJohn Edward Broadbent         {
495e56ed6b9SJohn Edward Broadbent             getDriveItemProperties(asyncResp, connectionName, path);
496e56ed6b9SJohn Edward Broadbent         }
497e56ed6b9SJohn Edward Broadbent     }
498e56ed6b9SJohn Edward Broadbent }
499e56ed6b9SJohn Edward Broadbent 
5007e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
5017e860f15SJohn Edward Broadbent {
5027e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
503ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
504002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
505002d39b4SEd Tanous             [&app](const crow::Request& req,
50645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5077e860f15SJohn Edward Broadbent                    const std::string& driveId) {
5083ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
50945ca1b86SEd Tanous         {
51045ca1b86SEd Tanous             return;
51145ca1b86SEd Tanous         }
5127e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
513002d39b4SEd Tanous             [asyncResp,
514002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
515b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
5167e860f15SJohn Edward Broadbent             if (ec)
5177e860f15SJohn Edward Broadbent             {
5187e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
5197e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5207e860f15SJohn Edward Broadbent                 return;
5217e860f15SJohn Edward Broadbent             }
5227e860f15SJohn Edward Broadbent 
52303913171SWilly Tu             auto drive = std::find_if(
5247e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
525002d39b4SEd Tanous                 [&driveId](
526002d39b4SEd Tanous                     const std::pair<
52703913171SWilly Tu                         std::string,
52803913171SWilly Tu                         std::vector<std::pair<
529002d39b4SEd Tanous                             std::string, std::vector<std::string>>>>& object) {
53003913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
53103913171SWilly Tu                            .filename() == driveId;
5327e860f15SJohn Edward Broadbent                 });
5337e860f15SJohn Edward Broadbent 
53403913171SWilly Tu             if (drive == subtree.end())
5357e860f15SJohn Edward Broadbent             {
536002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5377e860f15SJohn Edward Broadbent                 return;
5387e860f15SJohn Edward Broadbent             }
5397e860f15SJohn Edward Broadbent 
54003913171SWilly Tu             const std::string& path = drive->first;
541002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
54203913171SWilly Tu                 connectionNames = drive->second;
5437e860f15SJohn Edward Broadbent 
544002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
5457e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
546002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
5477e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
5487e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
5497e860f15SJohn Edward Broadbent 
5507e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
5517e860f15SJohn Edward Broadbent             {
552002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
55303913171SWilly Tu                                  << ", not equal to 1";
5547e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5557e860f15SJohn Edward Broadbent                 return;
5567e860f15SJohn Edward Broadbent             }
5577e860f15SJohn Edward Broadbent 
5587e860f15SJohn Edward Broadbent             getMainChassisId(
559002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
5607e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
561002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
5621476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
5637e860f15SJohn Edward Broadbent                 });
5647e860f15SJohn Edward Broadbent 
565a25aeccfSNikhil Potade             // default it to Enabled
566a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
567a25aeccfSNikhil Potade 
5682ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
569e284a7c1SJames Feist             health->inventory.emplace_back(path);
5702ad9c2f6SJames Feist             health->populate();
5712ad9c2f6SJames Feist 
572e56ed6b9SJohn Edward Broadbent             addAllDriveInfo(asyncResp, connectionNames[0].first, path,
573e56ed6b9SJohn Edward Broadbent                             connectionNames[0].second);
574a25aeccfSNikhil Potade             },
575a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
576a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
577a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
578a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
579a25aeccfSNikhil Potade             std::array<const char*, 1>{
580a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
5817e860f15SJohn Edward Broadbent         });
582a25aeccfSNikhil Potade }
58392903bd4SJohn Edward Broadbent 
58492903bd4SJohn Edward Broadbent /**
58592903bd4SJohn Edward Broadbent  * Chassis drives, this URL will show all the DriveCollection
58692903bd4SJohn Edward Broadbent  * information
58792903bd4SJohn Edward Broadbent  */
588*b53dcd9dSNan Zhou inline void chassisDriveCollectionGet(
58992903bd4SJohn Edward Broadbent     crow::App& app, const crow::Request& req,
59092903bd4SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
59192903bd4SJohn Edward Broadbent     const std::string& chassisId)
59292903bd4SJohn Edward Broadbent {
5933ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
59492903bd4SJohn Edward Broadbent     {
59592903bd4SJohn Edward Broadbent         return;
59692903bd4SJohn Edward Broadbent     }
59792903bd4SJohn Edward Broadbent 
59892903bd4SJohn Edward Broadbent     // mapper call lambda
59992903bd4SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
60092903bd4SJohn Edward Broadbent         [asyncResp,
60192903bd4SJohn Edward Broadbent          chassisId](const boost::system::error_code ec,
60292903bd4SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
60392903bd4SJohn Edward Broadbent         if (ec)
60492903bd4SJohn Edward Broadbent         {
60592903bd4SJohn Edward Broadbent             if (ec == boost::system::errc::host_unreachable)
60692903bd4SJohn Edward Broadbent             {
60792903bd4SJohn Edward Broadbent                 messages::resourceNotFound(asyncResp->res, "Chassis",
60892903bd4SJohn Edward Broadbent                                            chassisId);
60992903bd4SJohn Edward Broadbent                 return;
61092903bd4SJohn Edward Broadbent             }
61192903bd4SJohn Edward Broadbent             messages::internalError(asyncResp->res);
61292903bd4SJohn Edward Broadbent             return;
61392903bd4SJohn Edward Broadbent         }
61492903bd4SJohn Edward Broadbent 
61592903bd4SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
61692903bd4SJohn Edward Broadbent         for (const std::pair<
61792903bd4SJohn Edward Broadbent                  std::string,
61892903bd4SJohn Edward Broadbent                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
61992903bd4SJohn Edward Broadbent                  object : subtree)
62092903bd4SJohn Edward Broadbent         {
62192903bd4SJohn Edward Broadbent             const std::string& path = object.first;
62292903bd4SJohn Edward Broadbent             const dbus::utility::MapperGetObject& connectionNames =
62392903bd4SJohn Edward Broadbent                 object.second;
62492903bd4SJohn Edward Broadbent 
62592903bd4SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
62692903bd4SJohn Edward Broadbent             if (objPath.filename() != chassisId)
62792903bd4SJohn Edward Broadbent             {
62892903bd4SJohn Edward Broadbent                 continue;
62992903bd4SJohn Edward Broadbent             }
63092903bd4SJohn Edward Broadbent 
63192903bd4SJohn Edward Broadbent             if (connectionNames.empty())
63292903bd4SJohn Edward Broadbent             {
63392903bd4SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
63492903bd4SJohn Edward Broadbent                 continue;
63592903bd4SJohn Edward Broadbent             }
63692903bd4SJohn Edward Broadbent 
63792903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.type"] =
63892903bd4SJohn Edward Broadbent                 "#DriveCollection.DriveCollection";
63992903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
64014bd7d9aSJohn Edward Broadbent                 crow::utility::urlFromPieces("redfish", "v1", "Chassis",
64114bd7d9aSJohn Edward Broadbent                                              chassisId, "Drives");
64292903bd4SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = "Drive Collection";
64392903bd4SJohn Edward Broadbent 
64492903bd4SJohn Edward Broadbent             // Association lambda
64592903bd4SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
64692903bd4SJohn Edward Broadbent                 *crow::connections::systemBus,
64792903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
64892903bd4SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
64992903bd4SJohn Edward Broadbent                 [asyncResp, chassisId](const boost::system::error_code ec3,
65092903bd4SJohn Edward Broadbent                                        const std::vector<std::string>& resp) {
65192903bd4SJohn Edward Broadbent                 if (ec3)
65292903bd4SJohn Edward Broadbent                 {
65392903bd4SJohn Edward Broadbent                     BMCWEB_LOG_ERROR << "Error in chassis Drive association ";
65492903bd4SJohn Edward Broadbent                 }
65592903bd4SJohn Edward Broadbent                 nlohmann::json& members = asyncResp->res.jsonValue["Members"];
65692903bd4SJohn Edward Broadbent                 // important if array is empty
65792903bd4SJohn Edward Broadbent                 members = nlohmann::json::array();
65892903bd4SJohn Edward Broadbent 
65992903bd4SJohn Edward Broadbent                 std::vector<std::string> leafNames;
66092903bd4SJohn Edward Broadbent                 for (const auto& drive : resp)
66192903bd4SJohn Edward Broadbent                 {
66292903bd4SJohn Edward Broadbent                     sdbusplus::message::object_path path(drive);
66392903bd4SJohn Edward Broadbent                     leafNames.push_back(path.filename());
66492903bd4SJohn Edward Broadbent                 }
66592903bd4SJohn Edward Broadbent 
66692903bd4SJohn Edward Broadbent                 std::sort(leafNames.begin(), leafNames.end(),
66792903bd4SJohn Edward Broadbent                           AlphanumLess<std::string>());
66892903bd4SJohn Edward Broadbent 
66992903bd4SJohn Edward Broadbent                 for (const auto& leafName : leafNames)
67092903bd4SJohn Edward Broadbent                 {
67192903bd4SJohn Edward Broadbent                     nlohmann::json::object_t member;
67292903bd4SJohn Edward Broadbent                     member["@odata.id"] = crow::utility::urlFromPieces(
67392903bd4SJohn Edward Broadbent                         "redfish", "v1", "Chassis", chassisId, "Drives",
67492903bd4SJohn Edward Broadbent                         leafName);
67592903bd4SJohn Edward Broadbent                     members.push_back(std::move(member));
67692903bd4SJohn Edward Broadbent                     // navigation links will be registered in next patch set
67792903bd4SJohn Edward Broadbent                 }
67892903bd4SJohn Edward Broadbent                 asyncResp->res.jsonValue["Members@odata.count"] = resp.size();
67992903bd4SJohn Edward Broadbent                 }); // end association lambda
68092903bd4SJohn Edward Broadbent 
68192903bd4SJohn Edward Broadbent         } // end Iterate over all retrieved ObjectPaths
68292903bd4SJohn Edward Broadbent         },
68392903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
68492903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
68592903bd4SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
68692903bd4SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0,
68792903bd4SJohn Edward Broadbent         std::array<const char*, 2>{
68892903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Board",
68992903bd4SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Chassis"});
69092903bd4SJohn Edward Broadbent }
69192903bd4SJohn Edward Broadbent 
69292903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app)
69392903bd4SJohn Edward Broadbent {
69492903bd4SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/")
69592903bd4SJohn Edward Broadbent         .privileges(redfish::privileges::getDriveCollection)
69692903bd4SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
69792903bd4SJohn Edward Broadbent             std::bind_front(chassisDriveCollectionGet, std::ref(app)));
69892903bd4SJohn Edward Broadbent }
69992903bd4SJohn Edward Broadbent 
700*b53dcd9dSNan Zhou inline void buildDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
701*b53dcd9dSNan Zhou                        const std::string& chassisId,
702*b53dcd9dSNan Zhou                        const std::string& driveName,
703e56ed6b9SJohn Edward Broadbent                        const boost::system::error_code ec,
704e56ed6b9SJohn Edward Broadbent                        const dbus::utility::MapperGetSubTreeResponse& subtree)
705e56ed6b9SJohn Edward Broadbent {
706e56ed6b9SJohn Edward Broadbent 
707e56ed6b9SJohn Edward Broadbent     if (ec)
708e56ed6b9SJohn Edward Broadbent     {
709e56ed6b9SJohn Edward Broadbent         BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
710e56ed6b9SJohn Edward Broadbent         messages::internalError(asyncResp->res);
711e56ed6b9SJohn Edward Broadbent         return;
712e56ed6b9SJohn Edward Broadbent     }
713e56ed6b9SJohn Edward Broadbent 
714e56ed6b9SJohn Edward Broadbent     // Iterate over all retrieved ObjectPaths.
715e56ed6b9SJohn Edward Broadbent     for (const std::pair<
716e56ed6b9SJohn Edward Broadbent              std::string,
717e56ed6b9SJohn Edward Broadbent              std::vector<std::pair<std::string, std::vector<std::string>>>>&
718e56ed6b9SJohn Edward Broadbent              object : subtree)
719e56ed6b9SJohn Edward Broadbent     {
720e56ed6b9SJohn Edward Broadbent         const std::string& path = object.first;
721e56ed6b9SJohn Edward Broadbent         const std::vector<std::pair<std::string, std::vector<std::string>>>&
722e56ed6b9SJohn Edward Broadbent             connectionNames = object.second;
723e56ed6b9SJohn Edward Broadbent 
724e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path objPath(path);
725e56ed6b9SJohn Edward Broadbent         if (objPath.filename() != driveName)
726e56ed6b9SJohn Edward Broadbent         {
727e56ed6b9SJohn Edward Broadbent             continue;
728e56ed6b9SJohn Edward Broadbent         }
729e56ed6b9SJohn Edward Broadbent 
730e56ed6b9SJohn Edward Broadbent         if (connectionNames.empty())
731e56ed6b9SJohn Edward Broadbent         {
732e56ed6b9SJohn Edward Broadbent             BMCWEB_LOG_ERROR << "Got 0 Connection names";
733e56ed6b9SJohn Edward Broadbent             continue;
734e56ed6b9SJohn Edward Broadbent         }
735e56ed6b9SJohn Edward Broadbent 
736e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.id"] = crow::utility::urlFromPieces(
737e56ed6b9SJohn Edward Broadbent             "redfish", "v1", "Chassis", "Drives", driveName);
738e56ed6b9SJohn Edward Broadbent 
739e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
740e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Name"] = "Name";
741e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Id"] = driveName;
742e56ed6b9SJohn Edward Broadbent         // default it to Enabled
743e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
744e56ed6b9SJohn Edward Broadbent 
745e56ed6b9SJohn Edward Broadbent         nlohmann::json::object_t linkChassisNav;
746e56ed6b9SJohn Edward Broadbent         linkChassisNav["@odata.id"] =
747e56ed6b9SJohn Edward Broadbent             crow::utility::urlFromPieces("redfish", "v1", "Chassis", chassisId);
748e56ed6b9SJohn Edward Broadbent         asyncResp->res.jsonValue["Links"]["Chassis"] = linkChassisNav;
749e56ed6b9SJohn Edward Broadbent 
750e56ed6b9SJohn Edward Broadbent         addAllDriveInfo(asyncResp, connectionNames[0].first, path,
751e56ed6b9SJohn Edward Broadbent                         connectionNames[0].second);
752e56ed6b9SJohn Edward Broadbent     }
753e56ed6b9SJohn Edward Broadbent }
754e56ed6b9SJohn Edward Broadbent 
755*b53dcd9dSNan Zhou inline void
756*b53dcd9dSNan Zhou     matchAndFillDrive(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
757e56ed6b9SJohn Edward Broadbent                       const std::string& chassisId,
758e56ed6b9SJohn Edward Broadbent                       const std::string& driveName,
759e56ed6b9SJohn Edward Broadbent                       const std::vector<std::string>& resp)
760e56ed6b9SJohn Edward Broadbent {
761e56ed6b9SJohn Edward Broadbent 
762e56ed6b9SJohn Edward Broadbent     for (const std::string& drivePath : resp)
763e56ed6b9SJohn Edward Broadbent     {
764e56ed6b9SJohn Edward Broadbent         sdbusplus::message::object_path path(drivePath);
765e56ed6b9SJohn Edward Broadbent         std::string leaf = path.filename();
766e56ed6b9SJohn Edward Broadbent         if (leaf != driveName)
767e56ed6b9SJohn Edward Broadbent         {
768e56ed6b9SJohn Edward Broadbent             continue;
769e56ed6b9SJohn Edward Broadbent         }
770e56ed6b9SJohn Edward Broadbent         //  mapper call drive
771e56ed6b9SJohn Edward Broadbent         const std::array<const char*, 1> driveInterface = {
772e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.Inventory.Item.Drive"};
773e56ed6b9SJohn Edward Broadbent 
774e56ed6b9SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
775e56ed6b9SJohn Edward Broadbent             [asyncResp, chassisId, driveName](
776e56ed6b9SJohn Edward Broadbent                 const boost::system::error_code ec,
777e56ed6b9SJohn Edward Broadbent                 const dbus::utility::MapperGetSubTreeResponse& subtree) {
778e56ed6b9SJohn Edward Broadbent             buildDrive(asyncResp, chassisId, driveName, ec, subtree);
779e56ed6b9SJohn Edward Broadbent             },
780e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
781e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
782e56ed6b9SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
783e56ed6b9SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", 0, driveInterface);
784e56ed6b9SJohn Edward Broadbent     }
785e56ed6b9SJohn Edward Broadbent }
786e56ed6b9SJohn Edward Broadbent 
787*b53dcd9dSNan Zhou inline void
788*b53dcd9dSNan Zhou     handleChassisDriveGet(crow::App& app, const crow::Request& req,
789e56ed6b9SJohn Edward Broadbent                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
790e56ed6b9SJohn Edward Broadbent                           const std::string& chassisId,
791e56ed6b9SJohn Edward Broadbent                           const std::string& driveName)
792e56ed6b9SJohn Edward Broadbent {
79303810a11SMichal Orzel     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
794e56ed6b9SJohn Edward Broadbent     {
795e56ed6b9SJohn Edward Broadbent         return;
796e56ed6b9SJohn Edward Broadbent     }
797e56ed6b9SJohn Edward Broadbent     const std::array<const char*, 2> interfaces = {
798e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Board",
799e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.Inventory.Item.Chassis"};
800e56ed6b9SJohn Edward Broadbent 
801e56ed6b9SJohn Edward Broadbent     // mapper call chassis
802e56ed6b9SJohn Edward Broadbent     crow::connections::systemBus->async_method_call(
803e56ed6b9SJohn Edward Broadbent         [asyncResp, chassisId,
804e56ed6b9SJohn Edward Broadbent          driveName](const boost::system::error_code ec,
805e56ed6b9SJohn Edward Broadbent                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
806e56ed6b9SJohn Edward Broadbent         if (ec)
807e56ed6b9SJohn Edward Broadbent         {
808e56ed6b9SJohn Edward Broadbent             messages::internalError(asyncResp->res);
809e56ed6b9SJohn Edward Broadbent             return;
810e56ed6b9SJohn Edward Broadbent         }
811e56ed6b9SJohn Edward Broadbent 
812e56ed6b9SJohn Edward Broadbent         // Iterate over all retrieved ObjectPaths.
813e56ed6b9SJohn Edward Broadbent         for (const std::pair<
814e56ed6b9SJohn Edward Broadbent                  std::string,
815e56ed6b9SJohn Edward Broadbent                  std::vector<std::pair<std::string, std::vector<std::string>>>>&
816e56ed6b9SJohn Edward Broadbent                  object : subtree)
817e56ed6b9SJohn Edward Broadbent         {
818e56ed6b9SJohn Edward Broadbent             const std::string& path = object.first;
819e56ed6b9SJohn Edward Broadbent             const std::vector<std::pair<std::string, std::vector<std::string>>>&
820e56ed6b9SJohn Edward Broadbent                 connectionNames = object.second;
821e56ed6b9SJohn Edward Broadbent 
822e56ed6b9SJohn Edward Broadbent             sdbusplus::message::object_path objPath(path);
823e56ed6b9SJohn Edward Broadbent             if (objPath.filename() != chassisId)
824e56ed6b9SJohn Edward Broadbent             {
825e56ed6b9SJohn Edward Broadbent                 continue;
826e56ed6b9SJohn Edward Broadbent             }
827e56ed6b9SJohn Edward Broadbent 
828e56ed6b9SJohn Edward Broadbent             if (connectionNames.empty())
829e56ed6b9SJohn Edward Broadbent             {
830e56ed6b9SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Got 0 Connection names";
831e56ed6b9SJohn Edward Broadbent                 continue;
832e56ed6b9SJohn Edward Broadbent             }
833e56ed6b9SJohn Edward Broadbent 
834e56ed6b9SJohn Edward Broadbent             sdbusplus::asio::getProperty<std::vector<std::string>>(
835e56ed6b9SJohn Edward Broadbent                 *crow::connections::systemBus,
836e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", path + "/drive",
837e56ed6b9SJohn Edward Broadbent                 "xyz.openbmc_project.Association", "endpoints",
838e56ed6b9SJohn Edward Broadbent                 [asyncResp, chassisId,
839e56ed6b9SJohn Edward Broadbent                  driveName](const boost::system::error_code ec3,
840e56ed6b9SJohn Edward Broadbent                             const std::vector<std::string>& resp) {
841e56ed6b9SJohn Edward Broadbent                 if (ec3)
842e56ed6b9SJohn Edward Broadbent                 {
843e56ed6b9SJohn Edward Broadbent                     return; // no drives = no failures
844e56ed6b9SJohn Edward Broadbent                 }
845e56ed6b9SJohn Edward Broadbent                 matchAndFillDrive(asyncResp, chassisId, driveName, resp);
846e56ed6b9SJohn Edward Broadbent                 });
847e56ed6b9SJohn Edward Broadbent             break;
848e56ed6b9SJohn Edward Broadbent         }
849e56ed6b9SJohn Edward Broadbent         },
850e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper",
851e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/object_mapper",
852e56ed6b9SJohn Edward Broadbent         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
853e56ed6b9SJohn Edward Broadbent         "/xyz/openbmc_project/inventory", 0, interfaces);
854e56ed6b9SJohn Edward Broadbent }
855e56ed6b9SJohn Edward Broadbent 
856e56ed6b9SJohn Edward Broadbent /**
857e56ed6b9SJohn Edward Broadbent  * This URL will show the drive interface for the specific drive in the chassis
858e56ed6b9SJohn Edward Broadbent  */
859e56ed6b9SJohn Edward Broadbent inline void requestRoutesChassisDriveName(App& app)
860e56ed6b9SJohn Edward Broadbent {
861e56ed6b9SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/<str>/")
862e56ed6b9SJohn Edward Broadbent         .privileges(redfish::privileges::getChassis)
863e56ed6b9SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
864e56ed6b9SJohn Edward Broadbent             std::bind_front(handleChassisDriveGet, std::ref(app)));
865e56ed6b9SJohn Edward Broadbent }
866e56ed6b9SJohn Edward Broadbent 
867a25aeccfSNikhil Potade } // namespace redfish
868