xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
25a25aeccfSNikhil Potade 
26a25aeccfSNikhil Potade namespace redfish
27a25aeccfSNikhil Potade {
287e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
29a25aeccfSNikhil Potade {
307e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
31ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
327e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
337e860f15SJohn Edward Broadbent             [](const crow::Request&,
347e860f15SJohn Edward Broadbent                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
358d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
368d1b46d7Szhanghch05                     "#StorageCollection.StorageCollection";
378d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.id"] =
388d1b46d7Szhanghch05                     "/redfish/v1/Systems/system/Storage";
398d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "Storage Collection";
408d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members"] = {
41a25aeccfSNikhil Potade                     {{"@odata.id", "/redfish/v1/Systems/system/Storage/1"}}};
428d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Members@odata.count"] = 1;
437e860f15SJohn Edward Broadbent             });
44a25aeccfSNikhil Potade }
45a25aeccfSNikhil Potade 
467e860f15SJohn Edward Broadbent inline void requestRoutesStorage(App& app)
47a25aeccfSNikhil Potade {
487e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
49ed398213SEd Tanous         .privileges(redfish::privileges::getStorage)
507e860f15SJohn Edward Broadbent         .methods(
517e860f15SJohn Edward Broadbent             boost::beast::http::verb::
527e860f15SJohn Edward Broadbent                 get)([](const crow::Request&,
537e860f15SJohn Edward Broadbent                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
548d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
558d1b46d7Szhanghch05             asyncResp->res.jsonValue["@odata.id"] =
568d1b46d7Szhanghch05                 "/redfish/v1/Systems/system/Storage/1";
578d1b46d7Szhanghch05             asyncResp->res.jsonValue["Name"] = "Storage";
588d1b46d7Szhanghch05             asyncResp->res.jsonValue["Id"] = "1";
598d1b46d7Szhanghch05             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
60a25aeccfSNikhil Potade 
61e284a7c1SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
62e284a7c1SJames Feist             health->populate();
63e284a7c1SJames Feist 
64a25aeccfSNikhil Potade             crow::connections::systemBus->async_method_call(
657e860f15SJohn Edward Broadbent                 [asyncResp,
667e860f15SJohn Edward Broadbent                  health](const boost::system::error_code ec,
67*b9d36b47SEd Tanous                          const dbus::utility::MapperGetSubTreePathsResponse&
68*b9d36b47SEd Tanous                              storageList) {
69a25aeccfSNikhil Potade                     nlohmann::json& storageArray =
70a25aeccfSNikhil Potade                         asyncResp->res.jsonValue["Drives"];
71a25aeccfSNikhil Potade                     storageArray = nlohmann::json::array();
727e860f15SJohn Edward Broadbent                     auto& count =
737e860f15SJohn Edward Broadbent                         asyncResp->res.jsonValue["Drives@odata.count"];
74e284a7c1SJames Feist                     count = 0;
752ad9c2f6SJames Feist 
76a25aeccfSNikhil Potade                     if (ec)
77a25aeccfSNikhil Potade                     {
78a25aeccfSNikhil Potade                         BMCWEB_LOG_ERROR << "Drive mapper call error";
79a25aeccfSNikhil Potade                         messages::internalError(asyncResp->res);
80a25aeccfSNikhil Potade                         return;
81a25aeccfSNikhil Potade                     }
822ad9c2f6SJames Feist 
83e284a7c1SJames Feist                     health->inventory.insert(health->inventory.end(),
84e284a7c1SJames Feist                                              storageList.begin(),
85e284a7c1SJames Feist                                              storageList.end());
862ad9c2f6SJames Feist 
87a25aeccfSNikhil Potade                     for (const std::string& objpath : storageList)
88a25aeccfSNikhil Potade                     {
89f23b7296SEd Tanous                         std::size_t lastPos = objpath.rfind('/');
90a25aeccfSNikhil Potade                         if (lastPos == std::string::npos ||
91a25aeccfSNikhil Potade                             (objpath.size() <= lastPos + 1))
92a25aeccfSNikhil Potade                         {
937e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
947e860f15SJohn Edward Broadbent                                              << objpath;
95a25aeccfSNikhil Potade                             continue;
96a25aeccfSNikhil Potade                         }
97a25aeccfSNikhil Potade 
98a25aeccfSNikhil Potade                         storageArray.push_back(
99a25aeccfSNikhil Potade                             {{"@odata.id",
100be13ceceSJames Feist                               "/redfish/v1/Systems/system/Storage/1/Drives/" +
101a25aeccfSNikhil Potade                                   objpath.substr(lastPos + 1)}});
102a25aeccfSNikhil Potade                     }
103a25aeccfSNikhil Potade 
104e284a7c1SJames Feist                     count = storageArray.size();
105a25aeccfSNikhil Potade                 },
106a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
107a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
108a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
109a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
110a25aeccfSNikhil Potade                 std::array<const char*, 1>{
111a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
112e284a7c1SJames Feist 
113e284a7c1SJames Feist             crow::connections::systemBus->async_method_call(
114*b9d36b47SEd Tanous                 [asyncResp, health](
115*b9d36b47SEd Tanous                     const boost::system::error_code ec,
116*b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
11726f6976fSEd Tanous                     if (ec || subtree.empty())
118e284a7c1SJames Feist                     {
119d819a420SJames Feist                         // doesn't have to be there
120e284a7c1SJames Feist                         return;
121e284a7c1SJames Feist                     }
122e284a7c1SJames Feist 
123e284a7c1SJames Feist                     nlohmann::json& root =
124e284a7c1SJames Feist                         asyncResp->res.jsonValue["StorageControllers"];
125e284a7c1SJames Feist                     root = nlohmann::json::array();
126e284a7c1SJames Feist                     for (const auto& [path, interfaceDict] : subtree)
127e284a7c1SJames Feist                     {
128f23b7296SEd Tanous                         std::size_t lastPos = path.rfind('/');
129e284a7c1SJames Feist                         if (lastPos == std::string::npos ||
130e284a7c1SJames Feist                             (path.size() <= lastPos + 1))
131e284a7c1SJames Feist                         {
1327e860f15SJohn Edward Broadbent                             BMCWEB_LOG_ERROR << "Failed to find '/' in "
1337e860f15SJohn Edward Broadbent                                              << path;
134e284a7c1SJames Feist                             return;
135e284a7c1SJames Feist                         }
136e284a7c1SJames Feist 
137e284a7c1SJames Feist                         if (interfaceDict.size() != 1)
138e284a7c1SJames Feist                         {
139e284a7c1SJames Feist                             BMCWEB_LOG_ERROR << "Connection size "
140e284a7c1SJames Feist                                              << interfaceDict.size()
141e284a7c1SJames Feist                                              << ", greater than 1";
142e284a7c1SJames Feist                             messages::internalError(asyncResp->res);
143e284a7c1SJames Feist                             return;
144e284a7c1SJames Feist                         }
145e284a7c1SJames Feist 
146e284a7c1SJames Feist                         const std::string& connectionName =
147e284a7c1SJames Feist                             interfaceDict.front().first;
148e284a7c1SJames Feist 
149e284a7c1SJames Feist                         size_t index = root.size();
150e284a7c1SJames Feist                         nlohmann::json& storageController =
151e284a7c1SJames Feist                             root.emplace_back(nlohmann::json::object());
152e284a7c1SJames Feist 
153e284a7c1SJames Feist                         std::string id = path.substr(lastPos + 1);
154e284a7c1SJames Feist 
155e284a7c1SJames Feist                         storageController["@odata.type"] =
156e284a7c1SJames Feist                             "#Storage.v1_7_0.StorageController";
157e284a7c1SJames Feist                         storageController["@odata.id"] =
1580fda0f12SGeorge Liu                             "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
159e284a7c1SJames Feist                             std::to_string(index);
160e284a7c1SJames Feist                         storageController["Name"] = id;
161e284a7c1SJames Feist                         storageController["MemberId"] = id;
162e284a7c1SJames Feist                         storageController["Status"]["State"] = "Enabled";
163e284a7c1SJames Feist 
1641e1e598dSJonathan Doman                         sdbusplus::asio::getProperty<bool>(
1651e1e598dSJonathan Doman                             *crow::connections::systemBus, connectionName, path,
1661e1e598dSJonathan Doman                             "xyz.openbmc_project.Inventory.Item", "Present",
1671e1e598dSJonathan Doman                             [asyncResp,
1681e1e598dSJonathan Doman                              index](const boost::system::error_code ec2,
1691e1e598dSJonathan Doman                                     bool enabled) {
1707e860f15SJohn Edward Broadbent                                 // this interface isn't necessary, only check it
1717e860f15SJohn Edward Broadbent                                 // if we get a good return
17223a21a1cSEd Tanous                                 if (ec2)
173e284a7c1SJames Feist                                 {
174e284a7c1SJames Feist                                     return;
175e284a7c1SJames Feist                                 }
1761e1e598dSJonathan Doman                                 if (!enabled)
177e284a7c1SJames Feist                                 {
178e284a7c1SJames Feist                                     asyncResp->res
179e284a7c1SJames Feist                                         .jsonValue["StorageControllers"][index]
1807e860f15SJohn Edward Broadbent                                                   ["Status"]["State"] =
1817e860f15SJohn Edward Broadbent                                         "Disabled";
182e284a7c1SJames Feist                                 }
1831e1e598dSJonathan Doman                             });
184e284a7c1SJames Feist 
185e284a7c1SJames Feist                         crow::connections::systemBus->async_method_call(
1867e860f15SJohn Edward Broadbent                             [asyncResp, index](
1877e860f15SJohn Edward Broadbent                                 const boost::system::error_code ec2,
188168e20c1SEd Tanous                                 const std::vector<
189168e20c1SEd Tanous                                     std::pair<std::string,
190168e20c1SEd Tanous                                               dbus::utility::DbusVariantType>>&
1917e860f15SJohn Edward Broadbent                                     propertiesList) {
1927e860f15SJohn Edward Broadbent                                 if (ec2)
1937e860f15SJohn Edward Broadbent                                 {
1947e860f15SJohn Edward Broadbent                                     // this interface isn't necessary
1957e860f15SJohn Edward Broadbent                                     return;
1967e860f15SJohn Edward Broadbent                                 }
1977e860f15SJohn Edward Broadbent                                 for (const std::pair<
1987e860f15SJohn Edward Broadbent                                          std::string,
199168e20c1SEd Tanous                                          dbus::utility::DbusVariantType>&
200168e20c1SEd Tanous                                          property : propertiesList)
2017e860f15SJohn Edward Broadbent                                 {
2027e860f15SJohn Edward Broadbent                                     // Store DBus properties that are also
2037e860f15SJohn Edward Broadbent                                     // Redfish properties with same name and a
2047e860f15SJohn Edward Broadbent                                     // string value
2057e860f15SJohn Edward Broadbent                                     const std::string& propertyName =
2067e860f15SJohn Edward Broadbent                                         property.first;
2077e860f15SJohn Edward Broadbent                                     nlohmann::json& object =
2087e860f15SJohn Edward Broadbent                                         asyncResp->res
2097e860f15SJohn Edward Broadbent                                             .jsonValue["StorageControllers"]
2107e860f15SJohn Edward Broadbent                                                       [index];
2117e860f15SJohn Edward Broadbent                                     if ((propertyName == "PartNumber") ||
2127e860f15SJohn Edward Broadbent                                         (propertyName == "SerialNumber") ||
2137e860f15SJohn Edward Broadbent                                         (propertyName == "Manufacturer") ||
2147e860f15SJohn Edward Broadbent                                         (propertyName == "Model"))
2157e860f15SJohn Edward Broadbent                                     {
2167e860f15SJohn Edward Broadbent                                         const std::string* value =
2177e860f15SJohn Edward Broadbent                                             std::get_if<std::string>(
2187e860f15SJohn Edward Broadbent                                                 &property.second);
2197e860f15SJohn Edward Broadbent                                         if (value == nullptr)
2207e860f15SJohn Edward Broadbent                                         {
2217e860f15SJohn Edward Broadbent                                             // illegal property
2227e860f15SJohn Edward Broadbent                                             messages::internalError(
2237e860f15SJohn Edward Broadbent                                                 asyncResp->res);
2247e860f15SJohn Edward Broadbent                                             return;
2257e860f15SJohn Edward Broadbent                                         }
2267e860f15SJohn Edward Broadbent                                         object[propertyName] = *value;
2277e860f15SJohn Edward Broadbent                                     }
2287e860f15SJohn Edward Broadbent                                 }
2297e860f15SJohn Edward Broadbent                             },
2307e860f15SJohn Edward Broadbent                             connectionName, path,
2317e860f15SJohn Edward Broadbent                             "org.freedesktop.DBus.Properties", "GetAll",
2327e860f15SJohn Edward Broadbent                             "xyz.openbmc_project.Inventory.Decorator.Asset");
2337e860f15SJohn Edward Broadbent                     }
2347e860f15SJohn Edward Broadbent 
2357e860f15SJohn Edward Broadbent                     // this is done after we know the json array will no longer
2367e860f15SJohn Edward Broadbent                     // be resized, as json::array uses vector underneath and we
2377e860f15SJohn Edward Broadbent                     // need references to its members that won't change
2387e860f15SJohn Edward Broadbent                     size_t count = 0;
2397e860f15SJohn Edward Broadbent                     for (const auto& [path, interfaceDict] : subtree)
2407e860f15SJohn Edward Broadbent                     {
2417e860f15SJohn Edward Broadbent                         auto subHealth = std::make_shared<HealthPopulate>(
2427e860f15SJohn Edward Broadbent                             asyncResp, root[count]["Status"]);
2437e860f15SJohn Edward Broadbent                         subHealth->inventory.emplace_back(path);
2447e860f15SJohn Edward Broadbent                         health->inventory.emplace_back(path);
2457e860f15SJohn Edward Broadbent                         health->children.emplace_back(subHealth);
2467e860f15SJohn Edward Broadbent                         count++;
2477e860f15SJohn Edward Broadbent                     }
2487e860f15SJohn Edward Broadbent                 },
2497e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper",
2507e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/object_mapper",
2517e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2527e860f15SJohn Edward Broadbent                 "/xyz/openbmc_project/inventory", int32_t(0),
2537e860f15SJohn Edward Broadbent                 std::array<const char*, 1>{
2547e860f15SJohn Edward Broadbent                     "xyz.openbmc_project.Inventory.Item.StorageController"});
2557e860f15SJohn Edward Broadbent         });
2567e860f15SJohn Edward Broadbent }
2577e860f15SJohn Edward Broadbent 
25803913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25903913171SWilly Tu                           const std::string& connectionName,
26003913171SWilly Tu                           const std::string& path)
26103913171SWilly Tu {
26203913171SWilly Tu     crow::connections::systemBus->async_method_call(
263168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
264168e20c1SEd Tanous                     const std::vector<
265168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
26603913171SWilly Tu                         propertiesList) {
26703913171SWilly Tu             if (ec)
26803913171SWilly Tu             {
26903913171SWilly Tu                 // this interface isn't necessary
27003913171SWilly Tu                 return;
27103913171SWilly Tu             }
272168e20c1SEd Tanous             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
27303913171SWilly Tu                      property : propertiesList)
27403913171SWilly Tu             {
27503913171SWilly Tu                 // Store DBus properties that are also
27603913171SWilly Tu                 // Redfish properties with same name and a
27703913171SWilly Tu                 // string value
27803913171SWilly Tu                 const std::string& propertyName = property.first;
27903913171SWilly Tu                 if ((propertyName == "PartNumber") ||
28003913171SWilly Tu                     (propertyName == "SerialNumber") ||
28103913171SWilly Tu                     (propertyName == "Manufacturer") ||
28203913171SWilly Tu                     (propertyName == "Model"))
28303913171SWilly Tu                 {
28403913171SWilly Tu                     const std::string* value =
28503913171SWilly Tu                         std::get_if<std::string>(&property.second);
28603913171SWilly Tu                     if (value == nullptr)
28703913171SWilly Tu                     {
28803913171SWilly Tu                         // illegal property
28903913171SWilly Tu                         messages::internalError(asyncResp->res);
29003913171SWilly Tu                         return;
29103913171SWilly Tu                     }
29203913171SWilly Tu                     asyncResp->res.jsonValue[propertyName] = *value;
29303913171SWilly Tu                 }
29403913171SWilly Tu             }
29503913171SWilly Tu         },
29603913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
29703913171SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset");
29803913171SWilly Tu }
29903913171SWilly Tu 
30003913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
30103913171SWilly Tu                             const std::string& connectionName,
30203913171SWilly Tu                             const std::string& path)
30303913171SWilly Tu {
3041e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3051e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3061e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
30703913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3081e1e598dSJonathan Doman                           const bool enabled) {
30903913171SWilly Tu             // this interface isn't necessary, only check it if
31003913171SWilly Tu             // we get a good return
31103913171SWilly Tu             if (ec)
31203913171SWilly Tu             {
31303913171SWilly Tu                 return;
31403913171SWilly Tu             }
31503913171SWilly Tu 
3161e1e598dSJonathan Doman             if (!enabled)
31703913171SWilly Tu             {
31803913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
31903913171SWilly Tu             }
3201e1e598dSJonathan Doman         });
32103913171SWilly Tu }
32203913171SWilly Tu 
32303913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32403913171SWilly Tu                           const std::string& connectionName,
32503913171SWilly Tu                           const std::string& path)
32603913171SWilly Tu {
3271e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3281e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3291e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3301e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
33103913171SWilly Tu             // this interface isn't necessary, only check it
33203913171SWilly Tu             // if we get a good return
33303913171SWilly Tu             if (ec)
33403913171SWilly Tu             {
33503913171SWilly Tu                 return;
33603913171SWilly Tu             }
33703913171SWilly Tu 
33803913171SWilly Tu             // updating and disabled in the backend shouldn't be
33903913171SWilly Tu             // able to be set at the same time, so we don't need
34003913171SWilly Tu             // to check for the race condition of these two
34103913171SWilly Tu             // calls
3421e1e598dSJonathan Doman             if (updating)
34303913171SWilly Tu             {
34403913171SWilly Tu                 asyncResp->res.jsonValue["Status"]["State"] = "Updating";
34503913171SWilly Tu             }
3461e1e598dSJonathan Doman         });
34703913171SWilly Tu }
34803913171SWilly Tu 
34919b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
35019b8e9a0SWilly Tu {
35119b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
35219b8e9a0SWilly Tu     {
35319b8e9a0SWilly Tu         return "HDD";
35419b8e9a0SWilly Tu     }
35519b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
35619b8e9a0SWilly Tu     {
35719b8e9a0SWilly Tu         return "SSD";
35819b8e9a0SWilly Tu     }
35919b8e9a0SWilly Tu 
36019b8e9a0SWilly Tu     return std::nullopt;
36119b8e9a0SWilly Tu }
36219b8e9a0SWilly Tu 
36319b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
36419b8e9a0SWilly Tu {
36519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
36619b8e9a0SWilly Tu     {
36719b8e9a0SWilly Tu         return "SAS";
36819b8e9a0SWilly Tu     }
36919b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
37019b8e9a0SWilly Tu     {
37119b8e9a0SWilly Tu         return "SATA";
37219b8e9a0SWilly Tu     }
37319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
37419b8e9a0SWilly Tu     {
37519b8e9a0SWilly Tu         return "NVMe";
37619b8e9a0SWilly Tu     }
37719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
37819b8e9a0SWilly Tu     {
37919b8e9a0SWilly Tu         return "FC";
38019b8e9a0SWilly Tu     }
38119b8e9a0SWilly Tu 
38219b8e9a0SWilly Tu     return std::nullopt;
38319b8e9a0SWilly Tu }
38419b8e9a0SWilly Tu 
38519b8e9a0SWilly Tu inline void
38619b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38719b8e9a0SWilly Tu                            const std::string& connectionName,
38819b8e9a0SWilly Tu                            const std::string& path)
38919b8e9a0SWilly Tu {
39019b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
39119b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
39219b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
39319b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
39419b8e9a0SWilly Tu                     const std::vector<
39519b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
39619b8e9a0SWilly Tu                         propertiesList) {
39719b8e9a0SWilly Tu             if (ec)
39819b8e9a0SWilly Tu             {
39919b8e9a0SWilly Tu                 // this interface isn't required
40019b8e9a0SWilly Tu                 return;
40119b8e9a0SWilly Tu             }
40219b8e9a0SWilly Tu             for (const std::pair<std::string, dbus::utility::DbusVariantType>&
40319b8e9a0SWilly Tu                      property : propertiesList)
40419b8e9a0SWilly Tu             {
40519b8e9a0SWilly Tu                 const std::string& propertyName = property.first;
40619b8e9a0SWilly Tu                 if (propertyName == "Type")
40719b8e9a0SWilly Tu                 {
40819b8e9a0SWilly Tu                     const std::string* value =
40919b8e9a0SWilly Tu                         std::get_if<std::string>(&property.second);
41019b8e9a0SWilly Tu                     if (value == nullptr)
41119b8e9a0SWilly Tu                     {
41219b8e9a0SWilly Tu                         // illegal property
41319b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Type";
41419b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
41519b8e9a0SWilly Tu                         return;
41619b8e9a0SWilly Tu                     }
41719b8e9a0SWilly Tu 
41819b8e9a0SWilly Tu                     std::optional<std::string> mediaType =
41919b8e9a0SWilly Tu                         convertDriveType(*value);
42019b8e9a0SWilly Tu                     if (!mediaType)
42119b8e9a0SWilly Tu                     {
42219b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
42319b8e9a0SWilly Tu                                          << *value;
42419b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
42519b8e9a0SWilly Tu                         return;
42619b8e9a0SWilly Tu                     }
42719b8e9a0SWilly Tu 
42819b8e9a0SWilly Tu                     asyncResp->res.jsonValue["MediaType"] = *mediaType;
42919b8e9a0SWilly Tu                 }
43019b8e9a0SWilly Tu                 else if (propertyName == "Capacity")
43119b8e9a0SWilly Tu                 {
43219b8e9a0SWilly Tu                     const uint64_t* capacity =
43319b8e9a0SWilly Tu                         std::get_if<uint64_t>(&property.second);
43419b8e9a0SWilly Tu                     if (capacity == nullptr)
43519b8e9a0SWilly Tu                     {
43619b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Capacity";
43719b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
43819b8e9a0SWilly Tu                         return;
43919b8e9a0SWilly Tu                     }
44019b8e9a0SWilly Tu                     if (*capacity == 0)
44119b8e9a0SWilly Tu                     {
44219b8e9a0SWilly Tu                         // drive capacity not known
44319b8e9a0SWilly Tu                         continue;
44419b8e9a0SWilly Tu                     }
44519b8e9a0SWilly Tu 
44619b8e9a0SWilly Tu                     asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
44719b8e9a0SWilly Tu                 }
44819b8e9a0SWilly Tu                 else if (propertyName == "Protocol")
44919b8e9a0SWilly Tu                 {
45019b8e9a0SWilly Tu                     const std::string* value =
45119b8e9a0SWilly Tu                         std::get_if<std::string>(&property.second);
45219b8e9a0SWilly Tu                     if (value == nullptr)
45319b8e9a0SWilly Tu                     {
45419b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR << "Illegal property: Protocol";
45519b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
45619b8e9a0SWilly Tu                         return;
45719b8e9a0SWilly Tu                     }
45819b8e9a0SWilly Tu 
45919b8e9a0SWilly Tu                     std::optional<std::string> proto =
46019b8e9a0SWilly Tu                         convertDriveProtocol(*value);
46119b8e9a0SWilly Tu                     if (!proto)
46219b8e9a0SWilly Tu                     {
46319b8e9a0SWilly Tu                         BMCWEB_LOG_ERROR
46419b8e9a0SWilly Tu                             << "Unsupported DrivePrototype Interface: "
46519b8e9a0SWilly Tu                             << *value;
46619b8e9a0SWilly Tu                         messages::internalError(asyncResp->res);
46719b8e9a0SWilly Tu                         return;
46819b8e9a0SWilly Tu                     }
46919b8e9a0SWilly Tu                     asyncResp->res.jsonValue["Protocol"] = *proto;
47019b8e9a0SWilly Tu                 }
47119b8e9a0SWilly Tu             }
47219b8e9a0SWilly Tu         });
47319b8e9a0SWilly Tu }
47419b8e9a0SWilly Tu 
4757e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
4767e860f15SJohn Edward Broadbent {
4777e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
478ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
4797e860f15SJohn Edward Broadbent         .methods(
4807e860f15SJohn Edward Broadbent             boost::beast::http::verb::get)([](const crow::Request&,
4817e860f15SJohn Edward Broadbent                                               const std::shared_ptr<
4827e860f15SJohn Edward Broadbent                                                   bmcweb::AsyncResp>& asyncResp,
4837e860f15SJohn Edward Broadbent                                               const std::string& driveId) {
4847e860f15SJohn Edward Broadbent             crow::connections::systemBus->async_method_call(
485*b9d36b47SEd Tanous                 [asyncResp, driveId](
486*b9d36b47SEd Tanous                     const boost::system::error_code ec,
487*b9d36b47SEd Tanous                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
4887e860f15SJohn Edward Broadbent                     if (ec)
4897e860f15SJohn Edward Broadbent                     {
4907e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Drive mapper call error";
4917e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
4927e860f15SJohn Edward Broadbent                         return;
4937e860f15SJohn Edward Broadbent                     }
4947e860f15SJohn Edward Broadbent 
49503913171SWilly Tu                     auto drive = std::find_if(
4967e860f15SJohn Edward Broadbent                         subtree.begin(), subtree.end(),
49703913171SWilly Tu                         [&driveId](const std::pair<
49803913171SWilly Tu                                    std::string,
49903913171SWilly Tu                                    std::vector<std::pair<
50003913171SWilly Tu                                        std::string, std::vector<std::string>>>>&
50103913171SWilly Tu                                        object) {
50203913171SWilly Tu                             return sdbusplus::message::object_path(object.first)
50303913171SWilly Tu                                        .filename() == driveId;
5047e860f15SJohn Edward Broadbent                         });
5057e860f15SJohn Edward Broadbent 
50603913171SWilly Tu                     if (drive == subtree.end())
5077e860f15SJohn Edward Broadbent                     {
5087e860f15SJohn Edward Broadbent                         messages::resourceNotFound(asyncResp->res, "Drive",
5097e860f15SJohn Edward Broadbent                                                    driveId);
5107e860f15SJohn Edward Broadbent                         return;
5117e860f15SJohn Edward Broadbent                     }
5127e860f15SJohn Edward Broadbent 
51303913171SWilly Tu                     const std::string& path = drive->first;
5147e860f15SJohn Edward Broadbent                     const std::vector<
5157e860f15SJohn Edward Broadbent                         std::pair<std::string, std::vector<std::string>>>&
51603913171SWilly Tu                         connectionNames = drive->second;
5177e860f15SJohn Edward Broadbent 
5187e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.type"] =
5197e860f15SJohn Edward Broadbent                         "#Drive.v1_7_0.Drive";
5207e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["@odata.id"] =
5217e860f15SJohn Edward Broadbent                         "/redfish/v1/Systems/system/Storage/1/Drives/" +
5227e860f15SJohn Edward Broadbent                         driveId;
5237e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Name"] = driveId;
5247e860f15SJohn Edward Broadbent                     asyncResp->res.jsonValue["Id"] = driveId;
5257e860f15SJohn Edward Broadbent 
5267e860f15SJohn Edward Broadbent                     if (connectionNames.size() != 1)
5277e860f15SJohn Edward Broadbent                     {
5287e860f15SJohn Edward Broadbent                         BMCWEB_LOG_ERROR << "Connection size "
5297e860f15SJohn Edward Broadbent                                          << connectionNames.size()
53003913171SWilly Tu                                          << ", not equal to 1";
5317e860f15SJohn Edward Broadbent                         messages::internalError(asyncResp->res);
5327e860f15SJohn Edward Broadbent                         return;
5337e860f15SJohn Edward Broadbent                     }
5347e860f15SJohn Edward Broadbent 
5357e860f15SJohn Edward Broadbent                     getMainChassisId(
5367e860f15SJohn Edward Broadbent                         asyncResp,
5377e860f15SJohn Edward Broadbent                         [](const std::string& chassisId,
5387e860f15SJohn Edward Broadbent                            const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
5397e860f15SJohn Edward Broadbent                             aRsp->res.jsonValue["Links"]["Chassis"] = {
5407e860f15SJohn Edward Broadbent                                 {"@odata.id",
5417e860f15SJohn Edward Broadbent                                  "/redfish/v1/Chassis/" + chassisId}};
5427e860f15SJohn Edward Broadbent                         });
5437e860f15SJohn Edward Broadbent 
544a25aeccfSNikhil Potade                     // default it to Enabled
545a25aeccfSNikhil Potade                     asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
546a25aeccfSNikhil Potade 
5472ad9c2f6SJames Feist                     auto health = std::make_shared<HealthPopulate>(asyncResp);
548e284a7c1SJames Feist                     health->inventory.emplace_back(path);
5492ad9c2f6SJames Feist                     health->populate();
5502ad9c2f6SJames Feist 
55103913171SWilly Tu                     const std::string& connectionName =
55203913171SWilly Tu                         connectionNames[0].first;
55322984074SJames Feist 
55403913171SWilly Tu                     getDriveAsset(asyncResp, connectionName, path);
55503913171SWilly Tu                     getDrivePresent(asyncResp, connectionName, path);
55603913171SWilly Tu                     getDriveState(asyncResp, connectionName, path);
55719b8e9a0SWilly Tu                     getDriveItemProperties(asyncResp, connectionName, path);
558a25aeccfSNikhil Potade                 },
559a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper",
560a25aeccfSNikhil Potade                 "/xyz/openbmc_project/object_mapper",
561a25aeccfSNikhil Potade                 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
562a25aeccfSNikhil Potade                 "/xyz/openbmc_project/inventory", int32_t(0),
563a25aeccfSNikhil Potade                 std::array<const char*, 1>{
564a25aeccfSNikhil Potade                     "xyz.openbmc_project.Inventory.Item.Drive"});
5657e860f15SJohn Edward Broadbent         });
566a25aeccfSNikhil Potade }
567a25aeccfSNikhil Potade } // namespace redfish
568