xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 45ca1b868e47978a4d2e8ebb680cb384e804c97e)
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>
23*45ca1b86SEd 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)(
34*45ca1b86SEd Tanous             [&app](const crow::Request& req,
357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
36*45ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
37*45ca1b86SEd Tanous                 {
38*45ca1b86SEd Tanous                     return;
39*45ca1b86SEd 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";
458d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members"] = {
46a25aeccfSNikhil Potade                     {{"@odata.id", "/redfish/v1/Systems/system/Storage/1"}}};
478d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
487e860f15SJohn Edward Broadbent             });
49a25aeccfSNikhil Potade }
50a25aeccfSNikhil Potade 
517e860f15SJohn Edward Broadbent inline void requestRoutesStorage(App& app)
52a25aeccfSNikhil Potade {
537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
54ed398213SEd Tanous         .privileges(redfish::privileges::getStorage)
55*45ca1b86SEd Tanous         .methods(boost::beast::http::verb::get)([&app](const crow::Request& req,
56*45ca1b86SEd Tanous                                                        const std::shared_ptr<
57*45ca1b86SEd Tanous                                                            bmcweb::AsyncResp>&
58*45ca1b86SEd Tanous                                                            asyncResp) {
59*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
60*45ca1b86SEd Tanous             {
61*45ca1b86SEd Tanous                 return;
62*45ca1b86SEd Tanous             }
638d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
648d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
658d1b46d7Szhanghch05                 "/redfish/v1/Systems/system/Storage/1";
668d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Storage";
678d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "1";
688d1b46d7Szhanghch05             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
69a25aeccfSNikhil Potade 
70e284a7c1SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
71e284a7c1SJames Feist             health->populate();
72e284a7c1SJames Feist 
73a25aeccfSNikhil Potade             crow::connections::systemBus->async_method_call(
747e860f15SJohn Edward Broadbent                 [asyncResp,
757e860f15SJohn Edward Broadbent                  health](const boost::system::error_code ec,
76b9d36b47SEd Tanous                          const dbus::utility::MapperGetSubTreePathsResponse&
77b9d36b47SEd Tanous                              storageList) {
78a25aeccfSNikhil Potade                     nlohmann::json& storageArray =
79a25aeccfSNikhil Potade                         asyncResp->res.jsonValue["Drives"];
80a25aeccfSNikhil Potade                     storageArray = nlohmann::json::array();
817e860f15SJohn Edward Broadbent                     auto& count =
827e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Drives@odata.count"];
83e284a7c1SJames Feist                     count = 0;
842ad9c2f6SJames Feist 
85a25aeccfSNikhil Potade                     if (ec)
86a25aeccfSNikhil Potade                     {
87a25aeccfSNikhil Potade                         BMCWEB_LOG_ERROR << "Drive mapper call error";
88a25aeccfSNikhil Potade                         messages::internalError(asyncResp->res);
89a25aeccfSNikhil Potade                         return;
90a25aeccfSNikhil Potade                     }
912ad9c2f6SJames Feist 
92e284a7c1SJames Feist                     health->inventory.insert(health->inventory.end(),
93e284a7c1SJames Feist                                              storageList.begin(),
94e284a7c1SJames Feist                                              storageList.end());
952ad9c2f6SJames Feist 
96a25aeccfSNikhil Potade                     for (const std::string& objpath : storageList)
97a25aeccfSNikhil Potade                     {
98f23b7296SEd Tanous                         std::size_t lastPos = objpath.rfind('/');
99a25aeccfSNikhil Potade                         if (lastPos == std::string::npos ||
100a25aeccfSNikhil Potade                             (objpath.size() <= lastPos + 1))
101a25aeccfSNikhil Potade                         {
1027e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
1037e860f15SJohn Edward Broadbent                                              << objpath;
104a25aeccfSNikhil Potade                             continue;
105a25aeccfSNikhil Potade                         }
106a25aeccfSNikhil Potade 
107a25aeccfSNikhil Potade                         storageArray.push_back(
108a25aeccfSNikhil Potade                             {{"@odata.id",
109be13ceceSJames Feist                               "/redfish/v1/Systems/system/Storage/1/Drives/" +
110a25aeccfSNikhil Potade                                   objpath.substr(lastPos + 1)}});
111a25aeccfSNikhil Potade                     }
112a25aeccfSNikhil Potade 
113e284a7c1SJames Feist                     count = storageArray.size();
114a25aeccfSNikhil Potade                 },
115a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
116a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
117a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
118a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
119a25aeccfSNikhil Potade                 std::array<const char*, 1>{
120a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
121e284a7c1SJames Feist 
122e284a7c1SJames Feist             crow::connections::systemBus->async_method_call(
123b9d36b47SEd Tanous                 [asyncResp, health](
124b9d36b47SEd Tanous                     const boost::system::error_code ec,
125b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
12626f6976fSEd Tanous                     if (ec || subtree.empty())
127e284a7c1SJames Feist                     {
128d819a420SJames Feist                         // doesn't have to be there
129e284a7c1SJames Feist                         return;
130e284a7c1SJames Feist                     }
131e284a7c1SJames Feist 
132e284a7c1SJames Feist                     nlohmann::json& root =
133e284a7c1SJames Feist                         asyncResp->res.jsonValue["StorageControllers"];
134e284a7c1SJames Feist                     root = nlohmann::json::array();
135e284a7c1SJames Feist                     for (const auto& [path, interfaceDict] : subtree)
136e284a7c1SJames Feist                     {
137f23b7296SEd Tanous                         std::size_t lastPos = path.rfind('/');
138e284a7c1SJames Feist                         if (lastPos == std::string::npos ||
139e284a7c1SJames Feist                             (path.size() <= lastPos + 1))
140e284a7c1SJames Feist                         {
1417e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
1427e860f15SJohn Edward Broadbent                                              << path;
143e284a7c1SJames Feist                             return;
144e284a7c1SJames Feist                         }
145e284a7c1SJames Feist 
146e284a7c1SJames Feist                         if (interfaceDict.size() != 1)
147e284a7c1SJames Feist                         {
148e284a7c1SJames Feist                             BMCWEB_LOG_ERROR << "Connection size "
149e284a7c1SJames Feist                                              << interfaceDict.size()
150e284a7c1SJames Feist                                              << ", greater than 1";
151e284a7c1SJames Feist                             messages::internalError(asyncResp->res);
152e284a7c1SJames Feist                             return;
153e284a7c1SJames Feist                         }
154e284a7c1SJames Feist 
155e284a7c1SJames Feist                         const std::string& connectionName =
156e284a7c1SJames Feist                             interfaceDict.front().first;
157e284a7c1SJames Feist 
158e284a7c1SJames Feist                         size_t index = root.size();
159e284a7c1SJames Feist                         nlohmann::json& storageController =
160e284a7c1SJames Feist                             root.emplace_back(nlohmann::json::object());
161e284a7c1SJames Feist 
162e284a7c1SJames Feist                         std::string id = path.substr(lastPos + 1);
163e284a7c1SJames Feist 
164e284a7c1SJames Feist                         storageController["@odata.type"] =
165e284a7c1SJames Feist                             "#Storage.v1_7_0.StorageController";
166e284a7c1SJames Feist                         storageController["@odata.id"] =
1670fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
168e284a7c1SJames Feist                             std::to_string(index);
169e284a7c1SJames Feist                         storageController["Name"] = id;
170e284a7c1SJames Feist                         storageController["MemberId"] = id;
171e284a7c1SJames Feist                         storageController["Status"]["State"] = "Enabled";
172e284a7c1SJames Feist 
1731e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<bool>(
1741e1e598dSJonathan Doman                             *crow::connections::systemBus, connectionName, path,
1751e1e598dSJonathan Doman                             "xyz.openbmc_project.Inventory.Item", "Present",
1761e1e598dSJonathan Doman                             [asyncResp,
1771e1e598dSJonathan Doman                              index](const boost::system::error_code ec2,
1781e1e598dSJonathan Doman                                     bool enabled) {
1797e860f15SJohn Edward Broadbent                                 // this interface isn't necessary, only check it
1807e860f15SJohn Edward Broadbent                                 // if we get a good return
18123a21a1cSEd Tanous                                 if (ec2)
182e284a7c1SJames Feist                                 {
183e284a7c1SJames Feist                                     return;
184e284a7c1SJames Feist                                 }
1851e1e598dSJonathan Doman                                 if (!enabled)
186e284a7c1SJames Feist                                 {
187e284a7c1SJames Feist                                     asyncResp->res
188e284a7c1SJames Feist                                         .jsonValue["StorageControllers"][index]
1897e860f15SJohn Edward Broadbent                                                   ["Status"]["State"] =
1907e860f15SJohn Edward Broadbent                                         "Disabled";
191e284a7c1SJames Feist                                 }
1921e1e598dSJonathan Doman                             });
193e284a7c1SJames Feist 
194e284a7c1SJames Feist                         crow::connections::systemBus->async_method_call(
1957e860f15SJohn Edward Broadbent                             [asyncResp, index](
1967e860f15SJohn Edward Broadbent                                 const boost::system::error_code ec2,
197168e20c1SEd Tanous                                 const std::vector<
198168e20c1SEd Tanous                                     std::pair<std::string,
199168e20c1SEd Tanous                                               dbus::utility::DbusVariantType>>&
2007e860f15SJohn Edward Broadbent                                     propertiesList) {
2017e860f15SJohn Edward Broadbent                                 if (ec2)
2027e860f15SJohn Edward Broadbent                                 {
2037e860f15SJohn Edward Broadbent                                     // this interface isn't necessary
2047e860f15SJohn Edward Broadbent                                     return;
2057e860f15SJohn Edward Broadbent                                 }
2067e860f15SJohn Edward Broadbent                                 for (const std::pair<
2077e860f15SJohn Edward Broadbent                                          std::string,
208168e20c1SEd Tanous                                          dbus::utility::DbusVariantType>&
209168e20c1SEd Tanous                                          property : propertiesList)
2107e860f15SJohn Edward Broadbent                                 {
2117e860f15SJohn Edward Broadbent                                     // Store DBus properties that are also
2127e860f15SJohn Edward Broadbent                                     // Redfish properties with same name and a
2137e860f15SJohn Edward Broadbent                                     // string value
2147e860f15SJohn Edward Broadbent                                     const std::string& propertyName =
2157e860f15SJohn Edward Broadbent                                         property.first;
2167e860f15SJohn Edward Broadbent                                     nlohmann::json& object =
2177e860f15SJohn Edward Broadbent                                         asyncResp->res
2187e860f15SJohn Edward Broadbent                                             .jsonValue["StorageControllers"]
2197e860f15SJohn Edward Broadbent                                                       [index];
2207e860f15SJohn Edward Broadbent                                     if ((propertyName == "PartNumber") ||
2217e860f15SJohn Edward Broadbent                                         (propertyName == "SerialNumber") ||
2227e860f15SJohn Edward Broadbent                                         (propertyName == "Manufacturer") ||
2237e860f15SJohn Edward Broadbent                                         (propertyName == "Model"))
2247e860f15SJohn Edward Broadbent                                     {
2257e860f15SJohn Edward Broadbent                                         const std::string* value =
2267e860f15SJohn Edward Broadbent                                             std::get_if<std::string>(
2277e860f15SJohn Edward Broadbent                                                 &property.second);
2287e860f15SJohn Edward Broadbent                                         if (value == nullptr)
2297e860f15SJohn Edward Broadbent                                         {
2307e860f15SJohn Edward Broadbent                                             // illegal property
2317e860f15SJohn Edward Broadbent                                             messages::internalError(
2327e860f15SJohn Edward Broadbent                                                 asyncResp->res);
2337e860f15SJohn Edward Broadbent                                             return;
2347e860f15SJohn Edward Broadbent                                         }
2357e860f15SJohn Edward Broadbent                                         object[propertyName] = *value;
2367e860f15SJohn Edward Broadbent                                     }
2377e860f15SJohn Edward Broadbent                                 }
2387e860f15SJohn Edward Broadbent                             },
2397e860f15SJohn Edward Broadbent                             connectionName, path,
2407e860f15SJohn Edward Broadbent                             "org.freedesktop.DBus.Properties", "GetAll",
2417e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset");
2427e860f15SJohn Edward Broadbent                     }
2437e860f15SJohn Edward Broadbent 
2447e860f15SJohn Edward Broadbent                     // this is done after we know the json array will no longer
2457e860f15SJohn Edward Broadbent                     // be resized, as json::array uses vector underneath and we
2467e860f15SJohn Edward Broadbent                     // need references to its members that won't change
2477e860f15SJohn Edward Broadbent                     size_t count = 0;
2487e860f15SJohn Edward Broadbent                     for (const auto& [path, interfaceDict] : subtree)
2497e860f15SJohn Edward Broadbent                     {
2507e860f15SJohn Edward Broadbent                         auto subHealth = std::make_shared<HealthPopulate>(
2517e860f15SJohn Edward Broadbent                             asyncResp, root[count]["Status"]);
2527e860f15SJohn Edward Broadbent                         subHealth->inventory.emplace_back(path);
2537e860f15SJohn Edward Broadbent                         health->inventory.emplace_back(path);
2547e860f15SJohn Edward Broadbent                         health->children.emplace_back(subHealth);
2557e860f15SJohn Edward Broadbent                         count++;
2567e860f15SJohn Edward Broadbent                     }
2577e860f15SJohn Edward Broadbent                 },
2587e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper",
2597e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/object_mapper",
2607e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2617e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/inventory", int32_t(0),
2627e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
2637e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Item.StorageController"});
2647e860f15SJohn Edward Broadbent         });
2657e860f15SJohn Edward Broadbent }
2667e860f15SJohn Edward Broadbent 
26703913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
26803913171SWilly Tu                           const std::string& connectionName,
26903913171SWilly Tu                           const std::string& path)
27003913171SWilly Tu {
27103913171SWilly Tu     crow::connections::systemBus->async_method_call(
272168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
273168e20c1SEd Tanous                     const std::vector<
274168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
27503913171SWilly Tu                         propertiesList) {
27603913171SWilly Tu             if (ec)
27703913171SWilly Tu             {
27803913171SWilly Tu                 // this interface isn't necessary
27903913171SWilly Tu                 return;
28003913171SWilly Tu             }
281168e20c1SEd Tanous             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
28203913171SWilly Tu                      property : propertiesList)
28303913171SWilly Tu             {
28403913171SWilly Tu                 // Store DBus properties that are also
28503913171SWilly Tu                 // Redfish properties with same name and a
28603913171SWilly Tu                 // string value
28703913171SWilly Tu                 const std::string& propertyName = property.first;
28803913171SWilly Tu                 if ((propertyName == "PartNumber") ||
28903913171SWilly Tu                     (propertyName == "SerialNumber") ||
29003913171SWilly Tu                     (propertyName == "Manufacturer") ||
29103913171SWilly Tu                     (propertyName == "Model"))
29203913171SWilly Tu                 {
29303913171SWilly Tu                     const std::string* value =
29403913171SWilly Tu                         std::get_if<std::string>(&property.second);
29503913171SWilly Tu                     if (value == nullptr)
29603913171SWilly Tu                     {
29703913171SWilly Tu                         // illegal property
29803913171SWilly Tu                         messages::internalError(asyncResp->res);
29903913171SWilly Tu                         return;
30003913171SWilly Tu                     }
30103913171SWilly Tu                     asyncResp->res.jsonValue[propertyName] = *value;
30203913171SWilly Tu                 }
30303913171SWilly Tu             }
30403913171SWilly Tu         },
30503913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
30603913171SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset");
30703913171SWilly Tu }
30803913171SWilly Tu 
30903913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31003913171SWilly Tu                             const std::string& connectionName,
31103913171SWilly Tu                             const std::string& path)
31203913171SWilly Tu {
3131e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3141e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3151e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
31603913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3171e1e598dSJonathan Doman                           const bool enabled) {
31803913171SWilly Tu             // this interface isn't necessary, only check it if
31903913171SWilly Tu             // we get a good return
32003913171SWilly Tu             if (ec)
32103913171SWilly Tu             {
32203913171SWilly Tu                 return;
32303913171SWilly Tu             }
32403913171SWilly Tu 
3251e1e598dSJonathan Doman             if (!enabled)
32603913171SWilly Tu             {
32703913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
32803913171SWilly Tu             }
3291e1e598dSJonathan Doman         });
33003913171SWilly Tu }
33103913171SWilly Tu 
33203913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33303913171SWilly Tu                           const std::string& connectionName,
33403913171SWilly Tu                           const std::string& path)
33503913171SWilly Tu {
3361e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3371e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3381e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3391e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
34003913171SWilly Tu             // this interface isn't necessary, only check it
34103913171SWilly Tu             // if we get a good return
34203913171SWilly Tu             if (ec)
34303913171SWilly Tu             {
34403913171SWilly Tu                 return;
34503913171SWilly Tu             }
34603913171SWilly Tu 
34703913171SWilly Tu             // updating and disabled in the backend shouldn't be
34803913171SWilly Tu             // able to be set at the same time, so we don't need
34903913171SWilly Tu             // to check for the race condition of these two
35003913171SWilly Tu             // calls
3511e1e598dSJonathan Doman             if (updating)
35203913171SWilly Tu             {
35303913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
35403913171SWilly Tu             }
3551e1e598dSJonathan Doman         });
35603913171SWilly Tu }
35703913171SWilly Tu 
35819b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
35919b8e9a0SWilly Tu {
36019b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
36119b8e9a0SWilly Tu     {
36219b8e9a0SWilly Tu         return "HDD";
36319b8e9a0SWilly Tu     }
36419b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
36519b8e9a0SWilly Tu     {
36619b8e9a0SWilly Tu         return "SSD";
36719b8e9a0SWilly Tu     }
36819b8e9a0SWilly Tu 
36919b8e9a0SWilly Tu     return std::nullopt;
37019b8e9a0SWilly Tu }
37119b8e9a0SWilly Tu 
37219b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
37319b8e9a0SWilly Tu {
37419b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
37519b8e9a0SWilly Tu     {
37619b8e9a0SWilly Tu         return "SAS";
37719b8e9a0SWilly Tu     }
37819b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
37919b8e9a0SWilly Tu     {
38019b8e9a0SWilly Tu         return "SATA";
38119b8e9a0SWilly Tu     }
38219b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
38319b8e9a0SWilly Tu     {
38419b8e9a0SWilly Tu         return "NVMe";
38519b8e9a0SWilly Tu     }
38619b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
38719b8e9a0SWilly Tu     {
38819b8e9a0SWilly Tu         return "FC";
38919b8e9a0SWilly Tu     }
39019b8e9a0SWilly Tu 
39119b8e9a0SWilly Tu     return std::nullopt;
39219b8e9a0SWilly Tu }
39319b8e9a0SWilly Tu 
39419b8e9a0SWilly Tu inline void
39519b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
39619b8e9a0SWilly Tu                            const std::string& connectionName,
39719b8e9a0SWilly Tu                            const std::string& path)
39819b8e9a0SWilly Tu {
39919b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
40019b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
40119b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
40219b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
40319b8e9a0SWilly Tu                     const std::vector<
40419b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
40519b8e9a0SWilly Tu                         propertiesList) {
40619b8e9a0SWilly Tu             if (ec)
40719b8e9a0SWilly Tu             {
40819b8e9a0SWilly Tu                 // this interface isn't required
40919b8e9a0SWilly Tu                 return;
41019b8e9a0SWilly Tu             }
41119b8e9a0SWilly Tu             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
41219b8e9a0SWilly Tu                      property : propertiesList)
41319b8e9a0SWilly Tu             {
41419b8e9a0SWilly Tu                 const std::string& propertyName = property.first;
41519b8e9a0SWilly Tu                 if (propertyName == "Type")
41619b8e9a0SWilly Tu                 {
41719b8e9a0SWilly Tu                     const std::string* value =
41819b8e9a0SWilly Tu                         std::get_if<std::string>(&property.second);
41919b8e9a0SWilly Tu                     if (value == nullptr)
42019b8e9a0SWilly Tu                     {
42119b8e9a0SWilly Tu                         // illegal property
42219b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Type";
42319b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
42419b8e9a0SWilly Tu                         return;
42519b8e9a0SWilly Tu                     }
42619b8e9a0SWilly Tu 
42719b8e9a0SWilly Tu                     std::optional<std::string> mediaType =
42819b8e9a0SWilly Tu                         convertDriveType(*value);
42919b8e9a0SWilly Tu                     if (!mediaType)
43019b8e9a0SWilly Tu                     {
43119b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
43219b8e9a0SWilly Tu                                          << *value;
43319b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
43419b8e9a0SWilly Tu                         return;
43519b8e9a0SWilly Tu                     }
43619b8e9a0SWilly Tu 
43719b8e9a0SWilly Tu                     asyncResp->res.jsonValue["MediaType"] = *mediaType;
43819b8e9a0SWilly Tu                 }
43919b8e9a0SWilly Tu                 else if (propertyName == "Capacity")
44019b8e9a0SWilly Tu                 {
44119b8e9a0SWilly Tu                     const uint64_t* capacity =
44219b8e9a0SWilly Tu                         std::get_if<uint64_t>(&property.second);
44319b8e9a0SWilly Tu                     if (capacity == nullptr)
44419b8e9a0SWilly Tu                     {
44519b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Capacity";
44619b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
44719b8e9a0SWilly Tu                         return;
44819b8e9a0SWilly Tu                     }
44919b8e9a0SWilly Tu                     if (*capacity == 0)
45019b8e9a0SWilly Tu                     {
45119b8e9a0SWilly Tu                         // drive capacity not known
45219b8e9a0SWilly Tu                         continue;
45319b8e9a0SWilly Tu                     }
45419b8e9a0SWilly Tu 
45519b8e9a0SWilly Tu                     asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
45619b8e9a0SWilly Tu                 }
45719b8e9a0SWilly Tu                 else if (propertyName == "Protocol")
45819b8e9a0SWilly Tu                 {
45919b8e9a0SWilly Tu                     const std::string* value =
46019b8e9a0SWilly Tu                         std::get_if<std::string>(&property.second);
46119b8e9a0SWilly Tu                     if (value == nullptr)
46219b8e9a0SWilly Tu                     {
46319b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Protocol";
46419b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
46519b8e9a0SWilly Tu                         return;
46619b8e9a0SWilly Tu                     }
46719b8e9a0SWilly Tu 
46819b8e9a0SWilly Tu                     std::optional<std::string> proto =
46919b8e9a0SWilly Tu                         convertDriveProtocol(*value);
47019b8e9a0SWilly Tu                     if (!proto)
47119b8e9a0SWilly Tu                     {
47219b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR
47319b8e9a0SWilly Tu                             << "Unsupported DrivePrototype Interface: "
47419b8e9a0SWilly Tu                             << *value;
47519b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
47619b8e9a0SWilly Tu                         return;
47719b8e9a0SWilly Tu                     }
47819b8e9a0SWilly Tu                     asyncResp->res.jsonValue["Protocol"] = *proto;
47919b8e9a0SWilly Tu                 }
48019b8e9a0SWilly Tu             }
48119b8e9a0SWilly Tu         });
48219b8e9a0SWilly Tu }
48319b8e9a0SWilly Tu 
4847e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
4857e860f15SJohn Edward Broadbent {
4867e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
487ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
4887e860f15SJohn Edward Broadbent         .methods(
489*45ca1b86SEd Tanous             boost::beast::http::verb::
490*45ca1b86SEd Tanous                 get)([&app](const crow::Request& req,
491*45ca1b86SEd Tanous                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4927e860f15SJohn Edward Broadbent                             const std::string& driveId) {
493*45ca1b86SEd Tanous             if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
494*45ca1b86SEd Tanous             {
495*45ca1b86SEd Tanous                 return;
496*45ca1b86SEd Tanous             }
4977e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
498b9d36b47SEd Tanous                 [asyncResp, driveId](
499b9d36b47SEd Tanous                     const boost::system::error_code ec,
500b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
5017e860f15SJohn Edward Broadbent                     if (ec)
5027e860f15SJohn Edward Broadbent                     {
5037e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Drive mapper call error";
5047e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
5057e860f15SJohn Edward Broadbent                         return;
5067e860f15SJohn Edward Broadbent                     }
5077e860f15SJohn Edward Broadbent 
50803913171SWilly Tu                     auto drive = std::find_if(
5097e860f15SJohn Edward Broadbent                         subtree.begin(), subtree.end(),
51003913171SWilly Tu                         [&driveId](const std::pair<
51103913171SWilly Tu                                    std::string,
51203913171SWilly Tu                                    std::vector<std::pair<
51303913171SWilly Tu                                        std::string, std::vector<std::string>>>>&
51403913171SWilly Tu                                        object) {
51503913171SWilly Tu                             return sdbusplus::message::object_path(object.first)
51603913171SWilly Tu                                        .filename() == driveId;
5177e860f15SJohn Edward Broadbent                         });
5187e860f15SJohn Edward Broadbent 
51903913171SWilly Tu                     if (drive == subtree.end())
5207e860f15SJohn Edward Broadbent                     {
5217e860f15SJohn Edward Broadbent                         messages::resourceNotFound(asyncResp->res, "Drive",
5227e860f15SJohn Edward Broadbent                                                    driveId);
5237e860f15SJohn Edward Broadbent                         return;
5247e860f15SJohn Edward Broadbent                     }
5257e860f15SJohn Edward Broadbent 
52603913171SWilly Tu                     const std::string& path = drive->first;
5277e860f15SJohn Edward Broadbent                     const std::vector<
5287e860f15SJohn Edward Broadbent                         std::pair<std::string, std::vector<std::string>>>&
52903913171SWilly Tu                         connectionNames = drive->second;
5307e860f15SJohn Edward Broadbent 
5317e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.type"] =
5327e860f15SJohn Edward Broadbent                         "#Drive.v1_7_0.Drive";
5337e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.id"] =
5347e860f15SJohn Edward Broadbent                         "/redfish/v1/Systems/system/Storage/1/Drives/" +
5357e860f15SJohn Edward Broadbent                         driveId;
5367e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Name"] = driveId;
5377e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Id"] = driveId;
5387e860f15SJohn Edward Broadbent 
5397e860f15SJohn Edward Broadbent                     if (connectionNames.size() != 1)
5407e860f15SJohn Edward Broadbent                     {
5417e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Connection size "
5427e860f15SJohn Edward Broadbent                                          << connectionNames.size()
54303913171SWilly Tu                                          << ", not equal to 1";
5447e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
5457e860f15SJohn Edward Broadbent                         return;
5467e860f15SJohn Edward Broadbent                     }
5477e860f15SJohn Edward Broadbent 
5487e860f15SJohn Edward Broadbent                     getMainChassisId(
5497e860f15SJohn Edward Broadbent                         asyncResp,
5507e860f15SJohn Edward Broadbent                         [](const std::string& chassisId,
5517e860f15SJohn Edward Broadbent                            const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
5527e860f15SJohn Edward Broadbent                             aRsp->res.jsonValue["Links"]["Chassis"] = {
5537e860f15SJohn Edward Broadbent                                 {"@odata.id",
5547e860f15SJohn Edward Broadbent                                  "/redfish/v1/Chassis/" + chassisId}};
5557e860f15SJohn Edward Broadbent                         });
5567e860f15SJohn Edward Broadbent 
557a25aeccfSNikhil Potade                     // default it to Enabled
558a25aeccfSNikhil Potade                     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
559a25aeccfSNikhil Potade 
5602ad9c2f6SJames Feist                     auto health = std::make_shared<HealthPopulate>(asyncResp);
561e284a7c1SJames Feist                     health->inventory.emplace_back(path);
5622ad9c2f6SJames Feist                     health->populate();
5632ad9c2f6SJames Feist 
56403913171SWilly Tu                     const std::string& connectionName =
56503913171SWilly Tu                         connectionNames[0].first;
56622984074SJames Feist 
56703913171SWilly Tu                     getDriveAsset(asyncResp, connectionName, path);
56803913171SWilly Tu                     getDrivePresent(asyncResp, connectionName, path);
56903913171SWilly Tu                     getDriveState(asyncResp, connectionName, path);
57019b8e9a0SWilly Tu                     getDriveItemProperties(asyncResp, connectionName, path);
571a25aeccfSNikhil Potade                 },
572a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
573a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
574a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
575a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
576a25aeccfSNikhil Potade                 std::array<const char*, 1>{
577a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
5787e860f15SJohn Edward Broadbent         });
579a25aeccfSNikhil Potade }
580a25aeccfSNikhil Potade } // namespace redfish
581