xref: /openbmc/bmcweb/features/redfish/lib/storage.hpp (revision 002d39b4a7a5ed7166e2acad84e0943c3def9492)
1a25aeccfSNikhil Potade /*
2a25aeccfSNikhil Potade // Copyright (c) 2019 Intel Corporation
3a25aeccfSNikhil Potade //
4a25aeccfSNikhil Potade // Licensed under the Apache License, Version 2.0 (the "License");
5a25aeccfSNikhil Potade // you may not use this file except in compliance with the License.
6a25aeccfSNikhil Potade // You may obtain a copy of the License at
7a25aeccfSNikhil Potade //
8a25aeccfSNikhil Potade //      http://www.apache.org/licenses/LICENSE-2.0
9a25aeccfSNikhil Potade //
10a25aeccfSNikhil Potade // Unless required by applicable law or agreed to in writing, software
11a25aeccfSNikhil Potade // distributed under the License is distributed on an "AS IS" BASIS,
12a25aeccfSNikhil Potade // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a25aeccfSNikhil Potade // See the License for the specific language governing permissions and
14a25aeccfSNikhil Potade // limitations under the License.
15a25aeccfSNikhil Potade */
16a25aeccfSNikhil Potade #pragma once
17a25aeccfSNikhil Potade 
182ad9c2f6SJames Feist #include "health.hpp"
19e284a7c1SJames Feist #include "openbmc_dbus_rest.hpp"
202ad9c2f6SJames Feist 
217e860f15SJohn Edward Broadbent #include <app.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
26a25aeccfSNikhil Potade 
27a25aeccfSNikhil Potade namespace redfish
28a25aeccfSNikhil Potade {
297e860f15SJohn Edward Broadbent inline void requestRoutesStorageCollection(App& app)
30a25aeccfSNikhil Potade {
317e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/")
32ed398213SEd Tanous         .privileges(redfish::privileges::getStorageCollection)
337e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::get)(
3445ca1b86SEd Tanous             [&app](const crow::Request& req,
357e860f15SJohn Edward Broadbent                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
3645ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
3745ca1b86SEd Tanous         {
3845ca1b86SEd Tanous             return;
3945ca1b86SEd Tanous         }
408d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
418d1b46d7Szhanghch05             "#StorageCollection.StorageCollection";
428d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
438d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage";
448d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage Collection";
451476687dSEd Tanous         nlohmann::json::array_t members;
461476687dSEd Tanous         nlohmann::json::object_t member;
471476687dSEd Tanous         member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1";
481476687dSEd Tanous         members.emplace_back(member);
491476687dSEd Tanous         asyncResp->res.jsonValue["Members"] = std::move(members);
508d1b46d7Szhanghch05         asyncResp->res.jsonValue["Members@odata.count"] = 1;
517e860f15SJohn Edward Broadbent         });
52a25aeccfSNikhil Potade }
53a25aeccfSNikhil Potade 
547e860f15SJohn Edward Broadbent inline void requestRoutesStorage(App& app)
55a25aeccfSNikhil Potade {
567e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/")
57ed398213SEd Tanous         .privileges(redfish::privileges::getStorage)
58*002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
59*002d39b4SEd Tanous             [&app](const crow::Request& req,
60*002d39b4SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
6145ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
6245ca1b86SEd Tanous         {
6345ca1b86SEd Tanous             return;
6445ca1b86SEd Tanous         }
658d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage";
668d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.id"] =
678d1b46d7Szhanghch05             "/redfish/v1/Systems/system/Storage/1";
688d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "Storage";
698d1b46d7Szhanghch05         asyncResp->res.jsonValue["Id"] = "1";
708d1b46d7Szhanghch05         asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
71a25aeccfSNikhil Potade 
72e284a7c1SJames Feist         auto health = std::make_shared<HealthPopulate>(asyncResp);
73e284a7c1SJames Feist         health->populate();
74e284a7c1SJames Feist 
75a25aeccfSNikhil Potade         crow::connections::systemBus->async_method_call(
767e860f15SJohn Edward Broadbent             [asyncResp,
777e860f15SJohn Edward Broadbent              health](const boost::system::error_code ec,
78b9d36b47SEd Tanous                      const dbus::utility::MapperGetSubTreePathsResponse&
79b9d36b47SEd Tanous                          storageList) {
80*002d39b4SEd Tanous             nlohmann::json& storageArray = asyncResp->res.jsonValue["Drives"];
81a25aeccfSNikhil Potade             storageArray = nlohmann::json::array();
82*002d39b4SEd Tanous             auto& count = 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(),
93*002d39b4SEd Tanous                                      storageList.begin(), storageList.end());
942ad9c2f6SJames Feist 
95a25aeccfSNikhil Potade             for (const std::string& objpath : storageList)
96a25aeccfSNikhil Potade             {
97f23b7296SEd Tanous                 std::size_t lastPos = objpath.rfind('/');
98a25aeccfSNikhil Potade                 if (lastPos == std::string::npos ||
99a25aeccfSNikhil Potade                     (objpath.size() <= lastPos + 1))
100a25aeccfSNikhil Potade                 {
101*002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath;
102a25aeccfSNikhil Potade                     continue;
103a25aeccfSNikhil Potade                 }
1041476687dSEd Tanous                 nlohmann::json::object_t storage;
1051476687dSEd Tanous                 storage["@odata.id"] =
106be13ceceSJames Feist                     "/redfish/v1/Systems/system/Storage/1/Drives/" +
1071476687dSEd Tanous                     objpath.substr(lastPos + 1);
1081476687dSEd Tanous                 storageArray.push_back(std::move(storage));
109a25aeccfSNikhil Potade             }
110a25aeccfSNikhil Potade 
111e284a7c1SJames Feist             count = storageArray.size();
112a25aeccfSNikhil Potade             },
113a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
114a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
115a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
116a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
117a25aeccfSNikhil Potade             std::array<const char*, 1>{
118a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
119e284a7c1SJames Feist 
120e284a7c1SJames Feist         crow::connections::systemBus->async_method_call(
121*002d39b4SEd Tanous             [asyncResp,
122*002d39b4SEd Tanous              health](const boost::system::error_code ec,
123b9d36b47SEd Tanous                      const dbus::utility::MapperGetSubTreeResponse& subtree) {
12426f6976fSEd Tanous             if (ec || subtree.empty())
125e284a7c1SJames Feist             {
126d819a420SJames Feist                 // doesn't have to be there
127e284a7c1SJames Feist                 return;
128e284a7c1SJames Feist             }
129e284a7c1SJames Feist 
130e284a7c1SJames Feist             nlohmann::json& root =
131e284a7c1SJames Feist                 asyncResp->res.jsonValue["StorageControllers"];
132e284a7c1SJames Feist             root = nlohmann::json::array();
133e284a7c1SJames Feist             for (const auto& [path, interfaceDict] : subtree)
134e284a7c1SJames Feist             {
135f23b7296SEd Tanous                 std::size_t lastPos = path.rfind('/');
136e284a7c1SJames Feist                 if (lastPos == std::string::npos ||
137e284a7c1SJames Feist                     (path.size() <= lastPos + 1))
138e284a7c1SJames Feist                 {
139*002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Failed to find '/' in " << path;
140e284a7c1SJames Feist                     return;
141e284a7c1SJames Feist                 }
142e284a7c1SJames Feist 
143e284a7c1SJames Feist                 if (interfaceDict.size() != 1)
144e284a7c1SJames Feist                 {
145e284a7c1SJames Feist                     BMCWEB_LOG_ERROR << "Connection size "
146e284a7c1SJames Feist                                      << interfaceDict.size()
147e284a7c1SJames Feist                                      << ", greater than 1";
148e284a7c1SJames Feist                     messages::internalError(asyncResp->res);
149e284a7c1SJames Feist                     return;
150e284a7c1SJames Feist                 }
151e284a7c1SJames Feist 
152*002d39b4SEd Tanous                 const std::string& connectionName = interfaceDict.front().first;
153e284a7c1SJames Feist 
154e284a7c1SJames Feist                 size_t index = root.size();
155e284a7c1SJames Feist                 nlohmann::json& storageController =
156e284a7c1SJames Feist                     root.emplace_back(nlohmann::json::object());
157e284a7c1SJames Feist 
158e284a7c1SJames Feist                 std::string id = path.substr(lastPos + 1);
159e284a7c1SJames Feist 
160e284a7c1SJames Feist                 storageController["@odata.type"] =
161e284a7c1SJames Feist                     "#Storage.v1_7_0.StorageController";
162e284a7c1SJames Feist                 storageController["@odata.id"] =
1630fda0f12SGeorge Liu                     "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" +
164e284a7c1SJames Feist                     std::to_string(index);
165e284a7c1SJames Feist                 storageController["Name"] = id;
166e284a7c1SJames Feist                 storageController["MemberId"] = id;
167e284a7c1SJames Feist                 storageController["Status"]["State"] = "Enabled";
168e284a7c1SJames Feist 
1691e1e598dSJonathan Doman                 sdbusplus::asio::getProperty<bool>(
1701e1e598dSJonathan Doman                     *crow::connections::systemBus, connectionName, path,
1711e1e598dSJonathan Doman                     "xyz.openbmc_project.Inventory.Item", "Present",
172*002d39b4SEd Tanous                     [asyncResp, index](const boost::system::error_code ec2,
1731e1e598dSJonathan Doman                                        bool enabled) {
1747e860f15SJohn Edward Broadbent                     // this interface isn't necessary, only check it
1757e860f15SJohn Edward Broadbent                     // if we get a good return
17623a21a1cSEd Tanous                     if (ec2)
177e284a7c1SJames Feist                     {
178e284a7c1SJames Feist                         return;
179e284a7c1SJames Feist                     }
1801e1e598dSJonathan Doman                     if (!enabled)
181e284a7c1SJames Feist                     {
182*002d39b4SEd Tanous                         asyncResp->res.jsonValue["StorageControllers"][index]
1837e860f15SJohn Edward Broadbent                                                 ["Status"]["State"] =
1847e860f15SJohn Edward Broadbent                             "Disabled";
185e284a7c1SJames Feist                     }
1861e1e598dSJonathan Doman                     });
187e284a7c1SJames Feist 
188e284a7c1SJames Feist                 crow::connections::systemBus->async_method_call(
189*002d39b4SEd Tanous                     [asyncResp,
190*002d39b4SEd Tanous                      index](const boost::system::error_code ec2,
191*002d39b4SEd Tanous                             const std::vector<std::pair<
192*002d39b4SEd Tanous                                 std::string, dbus::utility::DbusVariantType>>&
1937e860f15SJohn Edward Broadbent                                 propertiesList) {
1947e860f15SJohn Edward Broadbent                     if (ec2)
1957e860f15SJohn Edward Broadbent                     {
1967e860f15SJohn Edward Broadbent                         // this interface isn't necessary
1977e860f15SJohn Edward Broadbent                         return;
1987e860f15SJohn Edward Broadbent                     }
199*002d39b4SEd Tanous                     for (const std::pair<std::string,
200168e20c1SEd Tanous                                          dbus::utility::DbusVariantType>&
201168e20c1SEd Tanous                              property : propertiesList)
2027e860f15SJohn Edward Broadbent                     {
2037e860f15SJohn Edward Broadbent                         // Store DBus properties that are also
2047e860f15SJohn Edward Broadbent                         // Redfish properties with same name and a
2057e860f15SJohn Edward Broadbent                         // string value
206*002d39b4SEd Tanous                         const std::string& propertyName = property.first;
2077e860f15SJohn Edward Broadbent                         nlohmann::json& object =
2087e860f15SJohn Edward Broadbent                             asyncResp->res
209*002d39b4SEd Tanous                                 .jsonValue["StorageControllers"][index];
2107e860f15SJohn Edward Broadbent                         if ((propertyName == "PartNumber") ||
2117e860f15SJohn Edward Broadbent                             (propertyName == "SerialNumber") ||
2127e860f15SJohn Edward Broadbent                             (propertyName == "Manufacturer") ||
2137e860f15SJohn Edward Broadbent                             (propertyName == "Model"))
2147e860f15SJohn Edward Broadbent                         {
2157e860f15SJohn Edward Broadbent                             const std::string* value =
216*002d39b4SEd Tanous                                 std::get_if<std::string>(&property.second);
2177e860f15SJohn Edward Broadbent                             if (value == nullptr)
2187e860f15SJohn Edward Broadbent                             {
2197e860f15SJohn Edward Broadbent                                 // illegal property
220*002d39b4SEd Tanous                                 messages::internalError(asyncResp->res);
2217e860f15SJohn Edward Broadbent                                 return;
2227e860f15SJohn Edward Broadbent                             }
2237e860f15SJohn Edward Broadbent                             object[propertyName] = *value;
2247e860f15SJohn Edward Broadbent                         }
2257e860f15SJohn Edward Broadbent                     }
2267e860f15SJohn Edward Broadbent                     },
227*002d39b4SEd Tanous                     connectionName, path, "org.freedesktop.DBus.Properties",
228*002d39b4SEd Tanous                     "GetAll", "xyz.openbmc_project.Inventory.Decorator.Asset");
2297e860f15SJohn Edward Broadbent             }
2307e860f15SJohn Edward Broadbent 
2317e860f15SJohn Edward Broadbent             // this is done after we know the json array will no longer
2327e860f15SJohn Edward Broadbent             // be resized, as json::array uses vector underneath and we
2337e860f15SJohn Edward Broadbent             // need references to its members that won't change
2347e860f15SJohn Edward Broadbent             size_t count = 0;
235dfababfcSNan Zhou             // Pointer based on |asyncResp->res.jsonValue|
236dfababfcSNan Zhou             nlohmann::json::json_pointer rootPtr =
237dfababfcSNan Zhou                 "/StorageControllers"_json_pointer;
2387e860f15SJohn Edward Broadbent             for (const auto& [path, interfaceDict] : subtree)
2397e860f15SJohn Edward Broadbent             {
2407e860f15SJohn Edward Broadbent                 auto subHealth = std::make_shared<HealthPopulate>(
241dfababfcSNan Zhou                     asyncResp, rootPtr / count / "Status");
2427e860f15SJohn Edward Broadbent                 subHealth->inventory.emplace_back(path);
2437e860f15SJohn Edward Broadbent                 health->inventory.emplace_back(path);
2447e860f15SJohn Edward Broadbent                 health->children.emplace_back(subHealth);
2457e860f15SJohn Edward Broadbent                 count++;
2467e860f15SJohn Edward Broadbent             }
2477e860f15SJohn Edward Broadbent             },
2487e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper",
2497e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/object_mapper",
2507e860f15SJohn Edward Broadbent             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
2517e860f15SJohn Edward Broadbent             "/xyz/openbmc_project/inventory", int32_t(0),
2527e860f15SJohn Edward Broadbent             std::array<const char*, 1>{
2537e860f15SJohn Edward Broadbent                 "xyz.openbmc_project.Inventory.Item.StorageController"});
2547e860f15SJohn Edward Broadbent         });
2557e860f15SJohn Edward Broadbent }
2567e860f15SJohn Edward Broadbent 
25703913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
25803913171SWilly Tu                           const std::string& connectionName,
25903913171SWilly Tu                           const std::string& path)
26003913171SWilly Tu {
26103913171SWilly Tu     crow::connections::systemBus->async_method_call(
262168e20c1SEd Tanous         [asyncResp](const boost::system::error_code ec,
263168e20c1SEd Tanous                     const std::vector<
264168e20c1SEd Tanous                         std::pair<std::string, dbus::utility::DbusVariantType>>&
26503913171SWilly Tu                         propertiesList) {
26603913171SWilly Tu         if (ec)
26703913171SWilly Tu         {
26803913171SWilly Tu             // this interface isn't necessary
26903913171SWilly Tu             return;
27003913171SWilly Tu         }
271168e20c1SEd Tanous         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
27203913171SWilly Tu                  property : propertiesList)
27303913171SWilly Tu         {
27403913171SWilly Tu             // Store DBus properties that are also
27503913171SWilly Tu             // Redfish properties with same name and a
27603913171SWilly Tu             // string value
27703913171SWilly Tu             const std::string& propertyName = property.first;
27803913171SWilly Tu             if ((propertyName == "PartNumber") ||
27903913171SWilly Tu                 (propertyName == "SerialNumber") ||
280*002d39b4SEd Tanous                 (propertyName == "Manufacturer") || (propertyName == "Model"))
28103913171SWilly Tu             {
28203913171SWilly Tu                 const std::string* value =
28303913171SWilly Tu                     std::get_if<std::string>(&property.second);
28403913171SWilly Tu                 if (value == nullptr)
28503913171SWilly Tu                 {
28603913171SWilly Tu                     // illegal property
28703913171SWilly Tu                     messages::internalError(asyncResp->res);
28803913171SWilly Tu                     return;
28903913171SWilly Tu                 }
29003913171SWilly Tu                 asyncResp->res.jsonValue[propertyName] = *value;
29103913171SWilly Tu             }
29203913171SWilly Tu         }
29303913171SWilly Tu         },
29403913171SWilly Tu         connectionName, path, "org.freedesktop.DBus.Properties", "GetAll",
29503913171SWilly Tu         "xyz.openbmc_project.Inventory.Decorator.Asset");
29603913171SWilly Tu }
29703913171SWilly Tu 
29803913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
29903913171SWilly Tu                             const std::string& connectionName,
30003913171SWilly Tu                             const std::string& path)
30103913171SWilly Tu {
3021e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3031e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3041e1e598dSJonathan Doman         "xyz.openbmc_project.Inventory.Item", "Present",
30503913171SWilly Tu         [asyncResp, path](const boost::system::error_code ec,
3061e1e598dSJonathan Doman                           const bool enabled) {
30703913171SWilly Tu         // this interface isn't necessary, only check it if
30803913171SWilly Tu         // we get a good return
30903913171SWilly Tu         if (ec)
31003913171SWilly Tu         {
31103913171SWilly Tu             return;
31203913171SWilly Tu         }
31303913171SWilly Tu 
3141e1e598dSJonathan Doman         if (!enabled)
31503913171SWilly Tu         {
31603913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
31703913171SWilly Tu         }
3181e1e598dSJonathan Doman         });
31903913171SWilly Tu }
32003913171SWilly Tu 
32103913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
32203913171SWilly Tu                           const std::string& connectionName,
32303913171SWilly Tu                           const std::string& path)
32403913171SWilly Tu {
3251e1e598dSJonathan Doman     sdbusplus::asio::getProperty<bool>(
3261e1e598dSJonathan Doman         *crow::connections::systemBus, connectionName, path,
3271e1e598dSJonathan Doman         "xyz.openbmc_project.State.Drive", "Rebuilding",
3281e1e598dSJonathan Doman         [asyncResp](const boost::system::error_code ec, const bool updating) {
32903913171SWilly Tu         // this interface isn't necessary, only check it
33003913171SWilly Tu         // if we get a good return
33103913171SWilly Tu         if (ec)
33203913171SWilly Tu         {
33303913171SWilly Tu             return;
33403913171SWilly Tu         }
33503913171SWilly Tu 
33603913171SWilly Tu         // updating and disabled in the backend shouldn't be
33703913171SWilly Tu         // able to be set at the same time, so we don't need
33803913171SWilly Tu         // to check for the race condition of these two
33903913171SWilly Tu         // calls
3401e1e598dSJonathan Doman         if (updating)
34103913171SWilly Tu         {
34203913171SWilly Tu             asyncResp->res.jsonValue["Status"]["State"] = "Updating";
34303913171SWilly Tu         }
3441e1e598dSJonathan Doman         });
34503913171SWilly Tu }
34603913171SWilly Tu 
34719b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type)
34819b8e9a0SWilly Tu {
34919b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD")
35019b8e9a0SWilly Tu     {
35119b8e9a0SWilly Tu         return "HDD";
35219b8e9a0SWilly Tu     }
35319b8e9a0SWilly Tu     if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD")
35419b8e9a0SWilly Tu     {
35519b8e9a0SWilly Tu         return "SSD";
35619b8e9a0SWilly Tu     }
35719b8e9a0SWilly Tu 
35819b8e9a0SWilly Tu     return std::nullopt;
35919b8e9a0SWilly Tu }
36019b8e9a0SWilly Tu 
36119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto)
36219b8e9a0SWilly Tu {
36319b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS")
36419b8e9a0SWilly Tu     {
36519b8e9a0SWilly Tu         return "SAS";
36619b8e9a0SWilly Tu     }
36719b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA")
36819b8e9a0SWilly Tu     {
36919b8e9a0SWilly Tu         return "SATA";
37019b8e9a0SWilly Tu     }
37119b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe")
37219b8e9a0SWilly Tu     {
37319b8e9a0SWilly Tu         return "NVMe";
37419b8e9a0SWilly Tu     }
37519b8e9a0SWilly Tu     if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC")
37619b8e9a0SWilly Tu     {
37719b8e9a0SWilly Tu         return "FC";
37819b8e9a0SWilly Tu     }
37919b8e9a0SWilly Tu 
38019b8e9a0SWilly Tu     return std::nullopt;
38119b8e9a0SWilly Tu }
38219b8e9a0SWilly Tu 
38319b8e9a0SWilly Tu inline void
38419b8e9a0SWilly Tu     getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
38519b8e9a0SWilly Tu                            const std::string& connectionName,
38619b8e9a0SWilly Tu                            const std::string& path)
38719b8e9a0SWilly Tu {
38819b8e9a0SWilly Tu     sdbusplus::asio::getAllProperties(
38919b8e9a0SWilly Tu         *crow::connections::systemBus, connectionName, path,
39019b8e9a0SWilly Tu         "xyz.openbmc_project.Inventory.Item.Drive",
39119b8e9a0SWilly Tu         [asyncResp](const boost::system::error_code ec,
39219b8e9a0SWilly Tu                     const std::vector<
39319b8e9a0SWilly Tu                         std::pair<std::string, dbus::utility::DbusVariantType>>&
39419b8e9a0SWilly Tu                         propertiesList) {
39519b8e9a0SWilly Tu         if (ec)
39619b8e9a0SWilly Tu         {
39719b8e9a0SWilly Tu             // this interface isn't required
39819b8e9a0SWilly Tu             return;
39919b8e9a0SWilly Tu         }
40019b8e9a0SWilly Tu         for (const std::pair<std::string, dbus::utility::DbusVariantType>&
40119b8e9a0SWilly Tu                  property : propertiesList)
40219b8e9a0SWilly Tu         {
40319b8e9a0SWilly Tu             const std::string& propertyName = property.first;
40419b8e9a0SWilly Tu             if (propertyName == "Type")
40519b8e9a0SWilly Tu             {
40619b8e9a0SWilly Tu                 const std::string* value =
40719b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
40819b8e9a0SWilly Tu                 if (value == nullptr)
40919b8e9a0SWilly Tu                 {
41019b8e9a0SWilly Tu                     // illegal property
41119b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Type";
41219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
41319b8e9a0SWilly Tu                     return;
41419b8e9a0SWilly Tu                 }
41519b8e9a0SWilly Tu 
416*002d39b4SEd Tanous                 std::optional<std::string> mediaType = convertDriveType(*value);
41719b8e9a0SWilly Tu                 if (!mediaType)
41819b8e9a0SWilly Tu                 {
41919b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: "
42019b8e9a0SWilly Tu                                      << *value;
42119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
42219b8e9a0SWilly Tu                     return;
42319b8e9a0SWilly Tu                 }
42419b8e9a0SWilly Tu 
42519b8e9a0SWilly Tu                 asyncResp->res.jsonValue["MediaType"] = *mediaType;
42619b8e9a0SWilly Tu             }
42719b8e9a0SWilly Tu             else if (propertyName == "Capacity")
42819b8e9a0SWilly Tu             {
42919b8e9a0SWilly Tu                 const uint64_t* capacity =
43019b8e9a0SWilly Tu                     std::get_if<uint64_t>(&property.second);
43119b8e9a0SWilly Tu                 if (capacity == nullptr)
43219b8e9a0SWilly Tu                 {
43319b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Capacity";
43419b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
43519b8e9a0SWilly Tu                     return;
43619b8e9a0SWilly Tu                 }
43719b8e9a0SWilly Tu                 if (*capacity == 0)
43819b8e9a0SWilly Tu                 {
43919b8e9a0SWilly Tu                     // drive capacity not known
44019b8e9a0SWilly Tu                     continue;
44119b8e9a0SWilly Tu                 }
44219b8e9a0SWilly Tu 
44319b8e9a0SWilly Tu                 asyncResp->res.jsonValue["CapacityBytes"] = *capacity;
44419b8e9a0SWilly Tu             }
44519b8e9a0SWilly Tu             else if (propertyName == "Protocol")
44619b8e9a0SWilly Tu             {
44719b8e9a0SWilly Tu                 const std::string* value =
44819b8e9a0SWilly Tu                     std::get_if<std::string>(&property.second);
44919b8e9a0SWilly Tu                 if (value == nullptr)
45019b8e9a0SWilly Tu                 {
45119b8e9a0SWilly Tu                     BMCWEB_LOG_ERROR << "Illegal property: Protocol";
45219b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
45319b8e9a0SWilly Tu                     return;
45419b8e9a0SWilly Tu                 }
45519b8e9a0SWilly Tu 
456*002d39b4SEd Tanous                 std::optional<std::string> proto = convertDriveProtocol(*value);
45719b8e9a0SWilly Tu                 if (!proto)
45819b8e9a0SWilly Tu                 {
459*002d39b4SEd Tanous                     BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: "
46019b8e9a0SWilly Tu                                      << *value;
46119b8e9a0SWilly Tu                     messages::internalError(asyncResp->res);
46219b8e9a0SWilly Tu                     return;
46319b8e9a0SWilly Tu                 }
46419b8e9a0SWilly Tu                 asyncResp->res.jsonValue["Protocol"] = *proto;
46519b8e9a0SWilly Tu             }
46619b8e9a0SWilly Tu         }
46719b8e9a0SWilly Tu         });
46819b8e9a0SWilly Tu }
46919b8e9a0SWilly Tu 
4707e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app)
4717e860f15SJohn Edward Broadbent {
4727e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/")
473ed398213SEd Tanous         .privileges(redfish::privileges::getDrive)
474*002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
475*002d39b4SEd Tanous             [&app](const crow::Request& req,
47645ca1b86SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
4777e860f15SJohn Edward Broadbent                    const std::string& driveId) {
47845ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
47945ca1b86SEd Tanous         {
48045ca1b86SEd Tanous             return;
48145ca1b86SEd Tanous         }
4827e860f15SJohn Edward Broadbent         crow::connections::systemBus->async_method_call(
483*002d39b4SEd Tanous             [asyncResp,
484*002d39b4SEd Tanous              driveId](const boost::system::error_code ec,
485b9d36b47SEd Tanous                       const dbus::utility::MapperGetSubTreeResponse& subtree) {
4867e860f15SJohn Edward Broadbent             if (ec)
4877e860f15SJohn Edward Broadbent             {
4887e860f15SJohn Edward Broadbent                 BMCWEB_LOG_ERROR << "Drive mapper call error";
4897e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
4907e860f15SJohn Edward Broadbent                 return;
4917e860f15SJohn Edward Broadbent             }
4927e860f15SJohn Edward Broadbent 
49303913171SWilly Tu             auto drive = std::find_if(
4947e860f15SJohn Edward Broadbent                 subtree.begin(), subtree.end(),
495*002d39b4SEd Tanous                 [&driveId](
496*002d39b4SEd Tanous                     const std::pair<
49703913171SWilly Tu                         std::string,
49803913171SWilly Tu                         std::vector<std::pair<
499*002d39b4SEd Tanous                             std::string, std::vector<std::string>>>>& object) {
50003913171SWilly Tu                 return sdbusplus::message::object_path(object.first)
50103913171SWilly Tu                            .filename() == driveId;
5027e860f15SJohn Edward Broadbent                 });
5037e860f15SJohn Edward Broadbent 
50403913171SWilly Tu             if (drive == subtree.end())
5057e860f15SJohn Edward Broadbent             {
506*002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Drive", driveId);
5077e860f15SJohn Edward Broadbent                 return;
5087e860f15SJohn Edward Broadbent             }
5097e860f15SJohn Edward Broadbent 
51003913171SWilly Tu             const std::string& path = drive->first;
511*002d39b4SEd Tanous             const std::vector<std::pair<std::string, std::vector<std::string>>>&
51203913171SWilly Tu                 connectionNames = drive->second;
5137e860f15SJohn Edward Broadbent 
514*002d39b4SEd Tanous             asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive";
5157e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["@odata.id"] =
516*002d39b4SEd Tanous                 "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId;
5177e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Name"] = driveId;
5187e860f15SJohn Edward Broadbent             asyncResp->res.jsonValue["Id"] = driveId;
5197e860f15SJohn Edward Broadbent 
5207e860f15SJohn Edward Broadbent             if (connectionNames.size() != 1)
5217e860f15SJohn Edward Broadbent             {
522*002d39b4SEd Tanous                 BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size()
52303913171SWilly Tu                                  << ", not equal to 1";
5247e860f15SJohn Edward Broadbent                 messages::internalError(asyncResp->res);
5257e860f15SJohn Edward Broadbent                 return;
5267e860f15SJohn Edward Broadbent             }
5277e860f15SJohn Edward Broadbent 
5287e860f15SJohn Edward Broadbent             getMainChassisId(
529*002d39b4SEd Tanous                 asyncResp, [](const std::string& chassisId,
5307e860f15SJohn Edward Broadbent                               const std::shared_ptr<bmcweb::AsyncResp>& aRsp) {
531*002d39b4SEd Tanous                     aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] =
5321476687dSEd Tanous                         "/redfish/v1/Chassis/" + chassisId;
5337e860f15SJohn Edward Broadbent                 });
5347e860f15SJohn Edward Broadbent 
535a25aeccfSNikhil Potade             // default it to Enabled
536a25aeccfSNikhil Potade             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
537a25aeccfSNikhil Potade 
5382ad9c2f6SJames Feist             auto health = std::make_shared<HealthPopulate>(asyncResp);
539e284a7c1SJames Feist             health->inventory.emplace_back(path);
5402ad9c2f6SJames Feist             health->populate();
5412ad9c2f6SJames Feist 
542*002d39b4SEd Tanous             const std::string& connectionName = connectionNames[0].first;
54322984074SJames Feist 
544*002d39b4SEd Tanous             for (const std::string& interface : connectionNames[0].second)
54561b83d0cSWilly Tu             {
54661b83d0cSWilly Tu                 if (interface ==
54761b83d0cSWilly Tu                     "xyz.openbmc_project.Inventory.Decorator.Asset")
54861b83d0cSWilly Tu                 {
54903913171SWilly Tu                     getDriveAsset(asyncResp, connectionName, path);
55061b83d0cSWilly Tu                 }
551*002d39b4SEd Tanous                 else if (interface == "xyz.openbmc_project.Inventory.Item")
55261b83d0cSWilly Tu                 {
55303913171SWilly Tu                     getDrivePresent(asyncResp, connectionName, path);
55461b83d0cSWilly Tu                 }
55561b83d0cSWilly Tu                 else if (interface == "xyz.openbmc_project.State.Drive")
55661b83d0cSWilly Tu                 {
55703913171SWilly Tu                     getDriveState(asyncResp, connectionName, path);
55861b83d0cSWilly Tu                 }
55961b83d0cSWilly Tu                 else if (interface ==
56061b83d0cSWilly Tu                          "xyz.openbmc_project.Inventory.Item.Drive")
56161b83d0cSWilly Tu                 {
562*002d39b4SEd Tanous                     getDriveItemProperties(asyncResp, connectionName, path);
56361b83d0cSWilly Tu                 }
56461b83d0cSWilly Tu             }
565a25aeccfSNikhil Potade             },
566a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper",
567a25aeccfSNikhil Potade             "/xyz/openbmc_project/object_mapper",
568a25aeccfSNikhil Potade             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
569a25aeccfSNikhil Potade             "/xyz/openbmc_project/inventory", int32_t(0),
570a25aeccfSNikhil Potade             std::array<const char*, 1>{
571a25aeccfSNikhil Potade                 "xyz.openbmc_project.Inventory.Item.Drive"});
5727e860f15SJohn Edward Broadbent         });
573a25aeccfSNikhil Potade }
574a25aeccfSNikhil Potade } // namespace redfish
575