xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 168e20c1306e3e689907ba43e14ea679e2328611)
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>
22*168e20c1SEd Tanous #include <dbus_utility.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
24a25aeccfSNikhil Potade 
25a25aeccfSNikhil Potade namespace redfish
26a25aeccfSNikhil Potade {
277e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
28a25aeccfSNikhil Potade {
297e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
30ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
317e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
327e860f15SJohn Edward Broadbent             [](const crow::Request&,
337e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
348d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
358d1b46d7Szhanghch05                     "#StorageCollection.StorageCollection";
368d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
378d1b46d7Szhanghch05                     "/redfish/v1/Systems/system/Storage";
388d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Storage Collection";
398d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members"] = {
40a25aeccfSNikhil Potade                     {{"@odata.id", "/redfish/v1/Systems/system/Storage/1"}}};
418d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
427e860f15SJohn Edward Broadbent             });
43a25aeccfSNikhil Potade }
44a25aeccfSNikhil Potade 
457e860f15SJohn Edward Broadbent inline void requestRoutesStorage(App& app)
46a25aeccfSNikhil Potade {
477e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
48ed398213SEd Tanous         .privileges(redfish::privileges::getStorage)
497e860f15SJohn Edward Broadbent         .methods(
507e860f15SJohn Edward Broadbent             boost::beast::http::verb::
517e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
527e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
538d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
548d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
558d1b46d7Szhanghch05                 "/redfish/v1/Systems/system/Storage/1";
568d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Storage";
578d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "1";
588d1b46d7Szhanghch05             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
59a25aeccfSNikhil Potade 
60e284a7c1SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
61e284a7c1SJames Feist             health->populate();
62e284a7c1SJames Feist 
63a25aeccfSNikhil Potade             crow::connections::systemBus->async_method_call(
647e860f15SJohn Edward Broadbent                 [asyncResp,
657e860f15SJohn Edward Broadbent                  health](const boost::system::error_code ec,
66a25aeccfSNikhil Potade                          const std::vector<std::string>& storageList) {
67a25aeccfSNikhil Potade                     nlohmann::json& storageArray =
68a25aeccfSNikhil Potade                         asyncResp->res.jsonValue["Drives"];
69a25aeccfSNikhil Potade                     storageArray = nlohmann::json::array();
707e860f15SJohn Edward Broadbent                     auto& count =
717e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Drives@odata.count"];
72e284a7c1SJames Feist                     count = 0;
732ad9c2f6SJames Feist 
74a25aeccfSNikhil Potade                     if (ec)
75a25aeccfSNikhil Potade                     {
76a25aeccfSNikhil Potade                         BMCWEB_LOG_ERROR << "Drive mapper call error";
77a25aeccfSNikhil Potade                         messages::internalError(asyncResp->res);
78a25aeccfSNikhil Potade                         return;
79a25aeccfSNikhil Potade                     }
802ad9c2f6SJames Feist 
81e284a7c1SJames Feist                     health->inventory.insert(health->inventory.end(),
82e284a7c1SJames Feist                                              storageList.begin(),
83e284a7c1SJames Feist                                              storageList.end());
842ad9c2f6SJames Feist 
85a25aeccfSNikhil Potade                     for (const std::string& objpath : storageList)
86a25aeccfSNikhil Potade                     {
87f23b7296SEd Tanous                         std::size_t lastPos = objpath.rfind('/');
88a25aeccfSNikhil Potade                         if (lastPos == std::string::npos ||
89a25aeccfSNikhil Potade                             (objpath.size() <= lastPos + 1))
90a25aeccfSNikhil Potade                         {
917e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
927e860f15SJohn Edward Broadbent                                              << objpath;
93a25aeccfSNikhil Potade                             continue;
94a25aeccfSNikhil Potade                         }
95a25aeccfSNikhil Potade 
96a25aeccfSNikhil Potade                         storageArray.push_back(
97a25aeccfSNikhil Potade                             {{"@odata.id",
98be13ceceSJames Feist                               "/redfish/v1/Systems/system/Storage/1/Drives/" +
99a25aeccfSNikhil Potade                                   objpath.substr(lastPos + 1)}});
100a25aeccfSNikhil Potade                     }
101a25aeccfSNikhil Potade 
102e284a7c1SJames Feist                     count = storageArray.size();
103a25aeccfSNikhil Potade                 },
104a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
105a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
106a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
107a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
108a25aeccfSNikhil Potade                 std::array<const char*, 1>{
109a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
110e284a7c1SJames Feist 
111e284a7c1SJames Feist             crow::connections::systemBus->async_method_call(
112e284a7c1SJames Feist                 [asyncResp,
113e284a7c1SJames Feist                  health](const boost::system::error_code ec,
114e284a7c1SJames Feist                          const crow::openbmc_mapper::GetSubTreeType& subtree) {
115e284a7c1SJames Feist                     if (ec || !subtree.size())
116e284a7c1SJames Feist                     {
117d819a420SJames Feist                         // doesn't have to be there
118e284a7c1SJames Feist                         return;
119e284a7c1SJames Feist                     }
120e284a7c1SJames Feist 
121e284a7c1SJames Feist                     nlohmann::json& root =
122e284a7c1SJames Feist                         asyncResp->res.jsonValue["StorageControllers"];
123e284a7c1SJames Feist                     root = nlohmann::json::array();
124e284a7c1SJames Feist                     for (const auto& [path, interfaceDict] : subtree)
125e284a7c1SJames Feist                     {
126f23b7296SEd Tanous                         std::size_t lastPos = path.rfind('/');
127e284a7c1SJames Feist                         if (lastPos == std::string::npos ||
128e284a7c1SJames Feist                             (path.size() <= lastPos + 1))
129e284a7c1SJames Feist                         {
1307e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
1317e860f15SJohn Edward Broadbent                                              << path;
132e284a7c1SJames Feist                             return;
133e284a7c1SJames Feist                         }
134e284a7c1SJames Feist 
135e284a7c1SJames Feist                         if (interfaceDict.size() != 1)
136e284a7c1SJames Feist                         {
137e284a7c1SJames Feist                             BMCWEB_LOG_ERROR << "Connection size "
138e284a7c1SJames Feist                                              << interfaceDict.size()
139e284a7c1SJames Feist                                              << ", greater than 1";
140e284a7c1SJames Feist                             messages::internalError(asyncResp->res);
141e284a7c1SJames Feist                             return;
142e284a7c1SJames Feist                         }
143e284a7c1SJames Feist 
144e284a7c1SJames Feist                         const std::string& connectionName =
145e284a7c1SJames Feist                             interfaceDict.front().first;
146e284a7c1SJames Feist 
147e284a7c1SJames Feist                         size_t index = root.size();
148e284a7c1SJames Feist                         nlohmann::json& storageController =
149e284a7c1SJames Feist                             root.emplace_back(nlohmann::json::object());
150e284a7c1SJames Feist 
151e284a7c1SJames Feist                         std::string id = path.substr(lastPos + 1);
152e284a7c1SJames Feist 
153e284a7c1SJames Feist                         storageController["@odata.type"] =
154e284a7c1SJames Feist                             "#Storage.v1_7_0.StorageController";
155e284a7c1SJames Feist                         storageController["@odata.id"] =
1560fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
157e284a7c1SJames Feist                             std::to_string(index);
158e284a7c1SJames Feist                         storageController["Name"] = id;
159e284a7c1SJames Feist                         storageController["MemberId"] = id;
160e284a7c1SJames Feist                         storageController["Status"]["State"] = "Enabled";
161e284a7c1SJames Feist 
162e284a7c1SJames Feist                         crow::connections::systemBus->async_method_call(
163*168e20c1SEd Tanous                             [asyncResp, index](
164*168e20c1SEd Tanous                                 const boost::system::error_code ec2,
165*168e20c1SEd Tanous                                 const dbus::utility::DbusVariantType present) {
1667e860f15SJohn Edward Broadbent                                 // this interface isn't necessary, only check it
1677e860f15SJohn Edward Broadbent                                 // if we get a good return
16823a21a1cSEd Tanous                                 if (ec2)
169e284a7c1SJames Feist                                 {
170e284a7c1SJames Feist                                     return;
171e284a7c1SJames Feist                                 }
1727e860f15SJohn Edward Broadbent                                 const bool* enabled =
1737e860f15SJohn Edward Broadbent                                     std::get_if<bool>(&present);
174e284a7c1SJames Feist                                 if (enabled == nullptr)
175e284a7c1SJames Feist                                 {
1767e860f15SJohn Edward Broadbent                                     BMCWEB_LOG_DEBUG
1777e860f15SJohn Edward Broadbent                                         << "Illegal property present";
178e284a7c1SJames Feist                                     messages::internalError(asyncResp->res);
179e284a7c1SJames Feist                                     return;
180e284a7c1SJames Feist                                 }
181e284a7c1SJames Feist                                 if (!(*enabled))
182e284a7c1SJames Feist                                 {
183e284a7c1SJames Feist                                     asyncResp->res
184e284a7c1SJames Feist                                         .jsonValue["StorageControllers"][index]
1857e860f15SJohn Edward Broadbent                                                   ["Status"]["State"] =
1867e860f15SJohn Edward Broadbent                                         "Disabled";
187e284a7c1SJames Feist                                 }
188e284a7c1SJames Feist                             },
1897e860f15SJohn Edward Broadbent                             connectionName, path,
1907e860f15SJohn Edward Broadbent                             "org.freedesktop.DBus.Properties", "Get",
1917e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Item", "Present");
192e284a7c1SJames Feist 
193e284a7c1SJames Feist                         crow::connections::systemBus->async_method_call(
1947e860f15SJohn Edward Broadbent                             [asyncResp, index](
1957e860f15SJohn Edward Broadbent                                 const boost::system::error_code ec2,
196*168e20c1SEd Tanous                                 const std::vector<
197*168e20c1SEd Tanous                                     std::pair<std::string,
198*168e20c1SEd Tanous                                               dbus::utility::DbusVariantType>>&
1997e860f15SJohn Edward Broadbent                                     propertiesList) {
2007e860f15SJohn Edward Broadbent                                 if (ec2)
2017e860f15SJohn Edward Broadbent                                 {
2027e860f15SJohn Edward Broadbent                                     // this interface isn't necessary
2037e860f15SJohn Edward Broadbent                                     return;
2047e860f15SJohn Edward Broadbent                                 }
2057e860f15SJohn Edward Broadbent                                 for (const std::pair<
2067e860f15SJohn Edward Broadbent                                          std::string,
207*168e20c1SEd Tanous                                          dbus::utility::DbusVariantType>&
208*168e20c1SEd Tanous                                          property : propertiesList)
2097e860f15SJohn Edward Broadbent                                 {
2107e860f15SJohn Edward Broadbent                                     // Store DBus properties that are also
2117e860f15SJohn Edward Broadbent                                     // Redfish properties with same name and a
2127e860f15SJohn Edward Broadbent                                     // string value
2137e860f15SJohn Edward Broadbent                                     const std::string& propertyName =
2147e860f15SJohn Edward Broadbent                                         property.first;
2157e860f15SJohn Edward Broadbent                                     nlohmann::json& object =
2167e860f15SJohn Edward Broadbent                                         asyncResp->res
2177e860f15SJohn Edward Broadbent                                             .jsonValue["StorageControllers"]
2187e860f15SJohn Edward Broadbent                                                       [index];
2197e860f15SJohn Edward Broadbent                                     if ((propertyName == "PartNumber") ||
2207e860f15SJohn Edward Broadbent                                         (propertyName == "SerialNumber") ||
2217e860f15SJohn Edward Broadbent                                         (propertyName == "Manufacturer") ||
2227e860f15SJohn Edward Broadbent                                         (propertyName == "Model"))
2237e860f15SJohn Edward Broadbent                                     {
2247e860f15SJohn Edward Broadbent                                         const std::string* value =
2257e860f15SJohn Edward Broadbent                                             std::get_if<std::string>(
2267e860f15SJohn Edward Broadbent                                                 &property.second);
2277e860f15SJohn Edward Broadbent                                         if (value == nullptr)
2287e860f15SJohn Edward Broadbent                                         {
2297e860f15SJohn Edward Broadbent                                             // illegal property
2307e860f15SJohn Edward Broadbent                                             messages::internalError(
2317e860f15SJohn Edward Broadbent                                                 asyncResp->res);
2327e860f15SJohn Edward Broadbent                                             return;
2337e860f15SJohn Edward Broadbent                                         }
2347e860f15SJohn Edward Broadbent                                         object[propertyName] = *value;
2357e860f15SJohn Edward Broadbent                                     }
2367e860f15SJohn Edward Broadbent                                 }
2377e860f15SJohn Edward Broadbent                             },
2387e860f15SJohn Edward Broadbent                             connectionName, path,
2397e860f15SJohn Edward Broadbent                             "org.freedesktop.DBus.Properties", "GetAll",
2407e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset");
2417e860f15SJohn Edward Broadbent                     }
2427e860f15SJohn Edward Broadbent 
2437e860f15SJohn Edward Broadbent                     // this is done after we know the json array will no longer
2447e860f15SJohn Edward Broadbent                     // be resized, as json::array uses vector underneath and we
2457e860f15SJohn Edward Broadbent                     // need references to its members that won't change
2467e860f15SJohn Edward Broadbent                     size_t count = 0;
2477e860f15SJohn Edward Broadbent                     for (const auto& [path, interfaceDict] : subtree)
2487e860f15SJohn Edward Broadbent                     {
2497e860f15SJohn Edward Broadbent                         auto subHealth = std::make_shared<HealthPopulate>(
2507e860f15SJohn Edward Broadbent                             asyncResp, root[count]["Status"]);
2517e860f15SJohn Edward Broadbent                         subHealth->inventory.emplace_back(path);
2527e860f15SJohn Edward Broadbent                         health->inventory.emplace_back(path);
2537e860f15SJohn Edward Broadbent                         health->children.emplace_back(subHealth);
2547e860f15SJohn Edward Broadbent                         count++;
2557e860f15SJohn Edward Broadbent                     }
2567e860f15SJohn Edward Broadbent                 },
2577e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper",
2587e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/object_mapper",
2597e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2607e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/inventory", int32_t(0),
2617e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
2627e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Item.StorageController"});
2637e860f15SJohn Edward Broadbent         });
2647e860f15SJohn Edward Broadbent }
2657e860f15SJohn Edward Broadbent 
26603913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26703913171SWilly Tu                           const std::string& connectionName,
26803913171SWilly Tu                           const std::string& path)
26903913171SWilly Tu {
27003913171SWilly Tu     crow::connections::systemBus->async_method_call(
271*168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
272*168e20c1SEd Tanous                     const std::vector<
273*168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
27403913171SWilly Tu                         propertiesList) {
27503913171SWilly Tu             if (ec)
27603913171SWilly Tu             {
27703913171SWilly Tu                 // this interface isn't necessary
27803913171SWilly Tu                 return;
27903913171SWilly Tu             }
280*168e20c1SEd Tanous             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
28103913171SWilly Tu                      property : propertiesList)
28203913171SWilly Tu             {
28303913171SWilly Tu                 // Store DBus properties that are also
28403913171SWilly Tu                 // Redfish properties with same name and a
28503913171SWilly Tu                 // string value
28603913171SWilly Tu                 const std::string& propertyName = property.first;
28703913171SWilly Tu                 if ((propertyName == "PartNumber") ||
28803913171SWilly Tu                     (propertyName == "SerialNumber") ||
28903913171SWilly Tu                     (propertyName == "Manufacturer") ||
29003913171SWilly Tu                     (propertyName == "Model"))
29103913171SWilly Tu                 {
29203913171SWilly Tu                     const std::string* value =
29303913171SWilly Tu                         std::get_if<std::string>(&property.second);
29403913171SWilly Tu                     if (value == nullptr)
29503913171SWilly Tu                     {
29603913171SWilly Tu                         // illegal property
29703913171SWilly Tu                         messages::internalError(asyncResp->res);
29803913171SWilly Tu                         return;
29903913171SWilly Tu                     }
30003913171SWilly Tu                     asyncResp->res.jsonValue[propertyName] = *value;
30103913171SWilly Tu                 }
30203913171SWilly Tu             }
30303913171SWilly Tu         },
30403913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
30503913171SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset");
30603913171SWilly Tu }
30703913171SWilly Tu 
30803913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30903913171SWilly Tu                             const std::string& connectionName,
31003913171SWilly Tu                             const std::string& path)
31103913171SWilly Tu {
31203913171SWilly Tu     crow::connections::systemBus->async_method_call(
31303913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
314*168e20c1SEd Tanous                           const dbus::utility::DbusVariantType present) {
31503913171SWilly Tu             // this interface isn't necessary, only check it if
31603913171SWilly Tu             // we get a good return
31703913171SWilly Tu             if (ec)
31803913171SWilly Tu             {
31903913171SWilly Tu                 return;
32003913171SWilly Tu             }
32103913171SWilly Tu 
32203913171SWilly Tu             const bool* enabled = std::get_if<bool>(&present);
32303913171SWilly Tu             if (enabled == nullptr)
32403913171SWilly Tu             {
32503913171SWilly Tu                 BMCWEB_LOG_DEBUG << "Illegal property present";
32603913171SWilly Tu                 messages::internalError(asyncResp->res);
32703913171SWilly Tu                 return;
32803913171SWilly Tu             }
32903913171SWilly Tu             if (!(*enabled))
33003913171SWilly Tu             {
33103913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
33203913171SWilly Tu             }
33303913171SWilly Tu         },
33403913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "Get",
33503913171SWilly Tu         "xyz.openbmc_project.Inventory.Item", "Present");
33603913171SWilly Tu }
33703913171SWilly Tu 
33803913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33903913171SWilly Tu                           const std::string& connectionName,
34003913171SWilly Tu                           const std::string& path)
34103913171SWilly Tu {
34203913171SWilly Tu     crow::connections::systemBus->async_method_call(
34303913171SWilly Tu         [asyncResp](const boost::system::error_code ec,
344*168e20c1SEd Tanous                     const dbus::utility::DbusVariantType rebuilding) {
34503913171SWilly Tu             // this interface isn't necessary, only check it
34603913171SWilly Tu             // if we get a good return
34703913171SWilly Tu             if (ec)
34803913171SWilly Tu             {
34903913171SWilly Tu                 return;
35003913171SWilly Tu             }
35103913171SWilly Tu 
35203913171SWilly Tu             const bool* updating = std::get_if<bool>(&rebuilding);
35303913171SWilly Tu             if (updating == nullptr)
35403913171SWilly Tu             {
35503913171SWilly Tu                 BMCWEB_LOG_DEBUG << "Illegal property present";
35603913171SWilly Tu                 messages::internalError(asyncResp->res);
35703913171SWilly Tu                 return;
35803913171SWilly Tu             }
35903913171SWilly Tu 
36003913171SWilly Tu             // updating and disabled in the backend shouldn't be
36103913171SWilly Tu             // able to be set at the same time, so we don't need
36203913171SWilly Tu             // to check for the race condition of these two
36303913171SWilly Tu             // calls
36403913171SWilly Tu             if (*updating)
36503913171SWilly Tu             {
36603913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
36703913171SWilly Tu             }
36803913171SWilly Tu         },
36903913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "Get",
37003913171SWilly Tu         "xyz.openbmc_project.State.Drive", "Rebuilding");
37103913171SWilly Tu }
37203913171SWilly Tu 
3737e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
3747e860f15SJohn Edward Broadbent {
3757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
376ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
3777e860f15SJohn Edward Broadbent         .methods(
3787e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
3797e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
3807e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
3817e860f15SJohn Edward Broadbent                                               const std::string& driveId) {
3827e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
383e284a7c1SJames Feist                 [asyncResp,
3847e860f15SJohn Edward Broadbent                  driveId](const boost::system::error_code ec,
3857e860f15SJohn Edward Broadbent                           const crow::openbmc_mapper::GetSubTreeType& subtree) {
3867e860f15SJohn Edward Broadbent                     if (ec)
3877e860f15SJohn Edward Broadbent                     {
3887e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Drive mapper call error";
3897e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
3907e860f15SJohn Edward Broadbent                         return;
3917e860f15SJohn Edward Broadbent                     }
3927e860f15SJohn Edward Broadbent 
39303913171SWilly Tu                     auto drive = std::find_if(
3947e860f15SJohn Edward Broadbent                         subtree.begin(), subtree.end(),
39503913171SWilly Tu                         [&driveId](const std::pair<
39603913171SWilly Tu                                    std::string,
39703913171SWilly Tu                                    std::vector<std::pair<
39803913171SWilly Tu                                        std::string, std::vector<std::string>>>>&
39903913171SWilly Tu                                        object) {
40003913171SWilly Tu                             return sdbusplus::message::object_path(object.first)
40103913171SWilly Tu                                        .filename() == driveId;
4027e860f15SJohn Edward Broadbent                         });
4037e860f15SJohn Edward Broadbent 
40403913171SWilly Tu                     if (drive == subtree.end())
4057e860f15SJohn Edward Broadbent                     {
4067e860f15SJohn Edward Broadbent                         messages::resourceNotFound(asyncResp->res, "Drive",
4077e860f15SJohn Edward Broadbent                                                    driveId);
4087e860f15SJohn Edward Broadbent                         return;
4097e860f15SJohn Edward Broadbent                     }
4107e860f15SJohn Edward Broadbent 
41103913171SWilly Tu                     const std::string& path = drive->first;
4127e860f15SJohn Edward Broadbent                     const std::vector<
4137e860f15SJohn Edward Broadbent                         std::pair<std::string, std::vector<std::string>>>&
41403913171SWilly Tu                         connectionNames = drive->second;
4157e860f15SJohn Edward Broadbent 
4167e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.type"] =
4177e860f15SJohn Edward Broadbent                         "#Drive.v1_7_0.Drive";
4187e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.id"] =
4197e860f15SJohn Edward Broadbent                         "/redfish/v1/Systems/system/Storage/1/Drives/" +
4207e860f15SJohn Edward Broadbent                         driveId;
4217e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Name"] = driveId;
4227e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Id"] = driveId;
4237e860f15SJohn Edward Broadbent 
4247e860f15SJohn Edward Broadbent                     if (connectionNames.size() != 1)
4257e860f15SJohn Edward Broadbent                     {
4267e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Connection size "
4277e860f15SJohn Edward Broadbent                                          << connectionNames.size()
42803913171SWilly Tu                                          << ", not equal to 1";
4297e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
4307e860f15SJohn Edward Broadbent                         return;
4317e860f15SJohn Edward Broadbent                     }
4327e860f15SJohn Edward Broadbent 
4337e860f15SJohn Edward Broadbent                     getMainChassisId(
4347e860f15SJohn Edward Broadbent                         asyncResp,
4357e860f15SJohn Edward Broadbent                         [](const std::string& chassisId,
4367e860f15SJohn Edward Broadbent                            const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
4377e860f15SJohn Edward Broadbent                             aRsp->res.jsonValue["Links"]["Chassis"] = {
4387e860f15SJohn Edward Broadbent                                 {"@odata.id",
4397e860f15SJohn Edward Broadbent                                  "/redfish/v1/Chassis/" + chassisId}};
4407e860f15SJohn Edward Broadbent                         });
4417e860f15SJohn Edward Broadbent 
442a25aeccfSNikhil Potade                     // default it to Enabled
443a25aeccfSNikhil Potade                     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
444a25aeccfSNikhil Potade 
4452ad9c2f6SJames Feist                     auto health = std::make_shared<HealthPopulate>(asyncResp);
446e284a7c1SJames Feist                     health->inventory.emplace_back(path);
4472ad9c2f6SJames Feist                     health->populate();
4482ad9c2f6SJames Feist 
44903913171SWilly Tu                     const std::string& connectionName =
45003913171SWilly Tu                         connectionNames[0].first;
45122984074SJames Feist 
45203913171SWilly Tu                     getDriveAsset(asyncResp, connectionName, path);
45303913171SWilly Tu                     getDrivePresent(asyncResp, connectionName, path);
45403913171SWilly Tu                     getDriveState(asyncResp, connectionName, path);
455a25aeccfSNikhil Potade                 },
456a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
457a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
458a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
459a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
460a25aeccfSNikhil Potade                 std::array<const char*, 1>{
461a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
4627e860f15SJohn Edward Broadbent         });
463a25aeccfSNikhil Potade }
464a25aeccfSNikhil Potade } // namespace redfish
465