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 54*a85afbe1SWilly Tu inline void getDrives(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 55*a85afbe1SWilly Tu const std::shared_ptr<HealthPopulate>& health) 56a25aeccfSNikhil Potade { 57a25aeccfSNikhil Potade crow::connections::systemBus->async_method_call( 58*a85afbe1SWilly Tu [asyncResp, health]( 59*a85afbe1SWilly Tu const boost::system::error_code ec, 60*a85afbe1SWilly Tu const dbus::utility::MapperGetSubTreePathsResponse& driveList) { 61a25aeccfSNikhil Potade if (ec) 62a25aeccfSNikhil Potade { 63a25aeccfSNikhil Potade BMCWEB_LOG_ERROR << "Drive mapper call error"; 64a25aeccfSNikhil Potade messages::internalError(asyncResp->res); 65a25aeccfSNikhil Potade return; 66a25aeccfSNikhil Potade } 672ad9c2f6SJames Feist 68*a85afbe1SWilly Tu nlohmann::json& driveArray = asyncResp->res.jsonValue["Drives"]; 69*a85afbe1SWilly Tu driveArray = nlohmann::json::array(); 70*a85afbe1SWilly Tu auto& count = asyncResp->res.jsonValue["Drives@odata.count"]; 71*a85afbe1SWilly Tu count = 0; 722ad9c2f6SJames Feist 73*a85afbe1SWilly Tu health->inventory.insert(health->inventory.end(), driveList.begin(), 74*a85afbe1SWilly Tu driveList.end()); 75*a85afbe1SWilly Tu 76*a85afbe1SWilly Tu for (const std::string& drive : driveList) 77a25aeccfSNikhil Potade { 78*a85afbe1SWilly Tu sdbusplus::message::object_path object(drive); 79*a85afbe1SWilly Tu if (object.filename().empty()) 80a25aeccfSNikhil Potade { 81*a85afbe1SWilly Tu BMCWEB_LOG_ERROR << "Failed to find filename in " << drive; 82*a85afbe1SWilly Tu return; 83a25aeccfSNikhil Potade } 84*a85afbe1SWilly Tu 85*a85afbe1SWilly Tu nlohmann::json::object_t driveJson; 86*a85afbe1SWilly Tu driveJson["@odata.id"] = 87be13ceceSJames Feist "/redfish/v1/Systems/system/Storage/1/Drives/" + 88*a85afbe1SWilly Tu object.filename(); 89*a85afbe1SWilly Tu driveArray.push_back(std::move(driveJson)); 90a25aeccfSNikhil Potade } 91a25aeccfSNikhil Potade 92*a85afbe1SWilly Tu count = driveArray.size(); 93a25aeccfSNikhil Potade }, 94a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", 95a25aeccfSNikhil Potade "/xyz/openbmc_project/object_mapper", 96a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 97a25aeccfSNikhil Potade "/xyz/openbmc_project/inventory", int32_t(0), 98*a85afbe1SWilly Tu std::array<const char*, 1>{"xyz.openbmc_project.Inventory.Item.Drive"}); 99*a85afbe1SWilly Tu } 100e284a7c1SJames Feist 101*a85afbe1SWilly Tu inline void 102*a85afbe1SWilly Tu getStorageControllers(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 103*a85afbe1SWilly Tu const std::shared_ptr<HealthPopulate>& health) 104*a85afbe1SWilly Tu { 105e284a7c1SJames Feist crow::connections::systemBus->async_method_call( 106002d39b4SEd Tanous [asyncResp, 107002d39b4SEd Tanous health](const boost::system::error_code ec, 108b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 10926f6976fSEd Tanous if (ec || subtree.empty()) 110e284a7c1SJames Feist { 111d819a420SJames Feist // doesn't have to be there 112e284a7c1SJames Feist return; 113e284a7c1SJames Feist } 114e284a7c1SJames Feist 115*a85afbe1SWilly Tu nlohmann::json& root = asyncResp->res.jsonValue["StorageControllers"]; 116e284a7c1SJames Feist root = nlohmann::json::array(); 117e284a7c1SJames Feist for (const auto& [path, interfaceDict] : subtree) 118e284a7c1SJames Feist { 119*a85afbe1SWilly Tu sdbusplus::message::object_path object(path); 120*a85afbe1SWilly Tu std::string id = object.filename(); 121*a85afbe1SWilly Tu if (id.empty()) 122e284a7c1SJames Feist { 123*a85afbe1SWilly Tu BMCWEB_LOG_ERROR << "Failed to find filename in " << path; 124e284a7c1SJames Feist return; 125e284a7c1SJames Feist } 126e284a7c1SJames Feist 127e284a7c1SJames Feist if (interfaceDict.size() != 1) 128e284a7c1SJames Feist { 129*a85afbe1SWilly Tu BMCWEB_LOG_ERROR << "Connection size " << interfaceDict.size() 130e284a7c1SJames Feist << ", greater than 1"; 131e284a7c1SJames Feist messages::internalError(asyncResp->res); 132e284a7c1SJames Feist return; 133e284a7c1SJames Feist } 134e284a7c1SJames Feist 135002d39b4SEd Tanous const std::string& connectionName = interfaceDict.front().first; 136e284a7c1SJames Feist 137e284a7c1SJames Feist size_t index = root.size(); 138e284a7c1SJames Feist nlohmann::json& storageController = 139e284a7c1SJames Feist root.emplace_back(nlohmann::json::object()); 140e284a7c1SJames Feist 141e284a7c1SJames Feist storageController["@odata.type"] = 142e284a7c1SJames Feist "#Storage.v1_7_0.StorageController"; 143e284a7c1SJames Feist storageController["@odata.id"] = 1440fda0f12SGeorge Liu "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" + 145e284a7c1SJames Feist std::to_string(index); 146e284a7c1SJames Feist storageController["Name"] = id; 147e284a7c1SJames Feist storageController["MemberId"] = id; 148e284a7c1SJames Feist storageController["Status"]["State"] = "Enabled"; 149e284a7c1SJames Feist 1501e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 1511e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 1521e1e598dSJonathan Doman "xyz.openbmc_project.Inventory.Item", "Present", 153002d39b4SEd Tanous [asyncResp, index](const boost::system::error_code ec2, 1541e1e598dSJonathan Doman bool enabled) { 1557e860f15SJohn Edward Broadbent // this interface isn't necessary, only check it 1567e860f15SJohn Edward Broadbent // if we get a good return 15723a21a1cSEd Tanous if (ec2) 158e284a7c1SJames Feist { 159e284a7c1SJames Feist return; 160e284a7c1SJames Feist } 1611e1e598dSJonathan Doman if (!enabled) 162e284a7c1SJames Feist { 163002d39b4SEd Tanous asyncResp->res.jsonValue["StorageControllers"][index] 164*a85afbe1SWilly Tu ["Status"]["State"] = "Disabled"; 165e284a7c1SJames Feist } 1661e1e598dSJonathan Doman }); 167e284a7c1SJames Feist 168e284a7c1SJames Feist crow::connections::systemBus->async_method_call( 169*a85afbe1SWilly Tu [asyncResp, index]( 170*a85afbe1SWilly Tu const boost::system::error_code ec2, 171*a85afbe1SWilly Tu const std::vector< 172*a85afbe1SWilly Tu std::pair<std::string, dbus::utility::DbusVariantType>>& 1737e860f15SJohn Edward Broadbent propertiesList) { 1747e860f15SJohn Edward Broadbent if (ec2) 1757e860f15SJohn Edward Broadbent { 1767e860f15SJohn Edward Broadbent // this interface isn't necessary 1777e860f15SJohn Edward Broadbent return; 1787e860f15SJohn Edward Broadbent } 179002d39b4SEd Tanous for (const std::pair<std::string, 180*a85afbe1SWilly Tu dbus::utility::DbusVariantType>& property : 181*a85afbe1SWilly Tu propertiesList) 1827e860f15SJohn Edward Broadbent { 1837e860f15SJohn Edward Broadbent // Store DBus properties that are also 1847e860f15SJohn Edward Broadbent // Redfish properties with same name and a 1857e860f15SJohn Edward Broadbent // string value 186002d39b4SEd Tanous const std::string& propertyName = property.first; 1877e860f15SJohn Edward Broadbent nlohmann::json& object = 188*a85afbe1SWilly Tu asyncResp->res.jsonValue["StorageControllers"][index]; 1897e860f15SJohn Edward Broadbent if ((propertyName == "PartNumber") || 1907e860f15SJohn Edward Broadbent (propertyName == "SerialNumber") || 1917e860f15SJohn Edward Broadbent (propertyName == "Manufacturer") || 1927e860f15SJohn Edward Broadbent (propertyName == "Model")) 1937e860f15SJohn Edward Broadbent { 1947e860f15SJohn Edward Broadbent const std::string* value = 195002d39b4SEd Tanous std::get_if<std::string>(&property.second); 1967e860f15SJohn Edward Broadbent if (value == nullptr) 1977e860f15SJohn Edward Broadbent { 1987e860f15SJohn Edward Broadbent // illegal property 199002d39b4SEd Tanous messages::internalError(asyncResp->res); 2007e860f15SJohn Edward Broadbent return; 2017e860f15SJohn Edward Broadbent } 2027e860f15SJohn Edward Broadbent object[propertyName] = *value; 2037e860f15SJohn Edward Broadbent } 2047e860f15SJohn Edward Broadbent } 2057e860f15SJohn Edward Broadbent }, 206002d39b4SEd Tanous connectionName, path, "org.freedesktop.DBus.Properties", 207002d39b4SEd Tanous "GetAll", "xyz.openbmc_project.Inventory.Decorator.Asset"); 2087e860f15SJohn Edward Broadbent } 2097e860f15SJohn Edward Broadbent 2107e860f15SJohn Edward Broadbent // this is done after we know the json array will no longer 2117e860f15SJohn Edward Broadbent // be resized, as json::array uses vector underneath and we 2127e860f15SJohn Edward Broadbent // need references to its members that won't change 2137e860f15SJohn Edward Broadbent size_t count = 0; 214dfababfcSNan Zhou // Pointer based on |asyncResp->res.jsonValue| 215dfababfcSNan Zhou nlohmann::json::json_pointer rootPtr = 216dfababfcSNan Zhou "/StorageControllers"_json_pointer; 2177e860f15SJohn Edward Broadbent for (const auto& [path, interfaceDict] : subtree) 2187e860f15SJohn Edward Broadbent { 2197e860f15SJohn Edward Broadbent auto subHealth = std::make_shared<HealthPopulate>( 220dfababfcSNan Zhou asyncResp, rootPtr / count / "Status"); 2217e860f15SJohn Edward Broadbent subHealth->inventory.emplace_back(path); 2227e860f15SJohn Edward Broadbent health->inventory.emplace_back(path); 2237e860f15SJohn Edward Broadbent health->children.emplace_back(subHealth); 2247e860f15SJohn Edward Broadbent count++; 2257e860f15SJohn Edward Broadbent } 2267e860f15SJohn Edward Broadbent }, 2277e860f15SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", 2287e860f15SJohn Edward Broadbent "/xyz/openbmc_project/object_mapper", 2297e860f15SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", "GetSubTree", 2307e860f15SJohn Edward Broadbent "/xyz/openbmc_project/inventory", int32_t(0), 2317e860f15SJohn Edward Broadbent std::array<const char*, 1>{ 2327e860f15SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.StorageController"}); 233*a85afbe1SWilly Tu } 234*a85afbe1SWilly Tu 235*a85afbe1SWilly Tu inline void requestRoutesStorage(App& app) 236*a85afbe1SWilly Tu { 237*a85afbe1SWilly Tu BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/") 238*a85afbe1SWilly Tu .privileges(redfish::privileges::getStorage) 239*a85afbe1SWilly Tu .methods(boost::beast::http::verb::get)( 240*a85afbe1SWilly Tu [&app](const crow::Request& req, 241*a85afbe1SWilly Tu const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { 242*a85afbe1SWilly Tu if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 243*a85afbe1SWilly Tu { 244*a85afbe1SWilly Tu return; 245*a85afbe1SWilly Tu } 246*a85afbe1SWilly Tu asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage"; 247*a85afbe1SWilly Tu asyncResp->res.jsonValue["@odata.id"] = 248*a85afbe1SWilly Tu "/redfish/v1/Systems/system/Storage/1"; 249*a85afbe1SWilly Tu asyncResp->res.jsonValue["Name"] = "Storage"; 250*a85afbe1SWilly Tu asyncResp->res.jsonValue["Id"] = "1"; 251*a85afbe1SWilly Tu asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 252*a85afbe1SWilly Tu 253*a85afbe1SWilly Tu auto health = std::make_shared<HealthPopulate>(asyncResp); 254*a85afbe1SWilly Tu health->populate(); 255*a85afbe1SWilly Tu 256*a85afbe1SWilly Tu getDrives(asyncResp, health); 257*a85afbe1SWilly Tu getStorageControllers(asyncResp, health); 2587e860f15SJohn Edward Broadbent }); 2597e860f15SJohn Edward Broadbent } 2607e860f15SJohn Edward Broadbent 26103913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 26203913171SWilly Tu const std::string& connectionName, 26303913171SWilly Tu const std::string& path) 26403913171SWilly Tu { 26503913171SWilly Tu crow::connections::systemBus->async_method_call( 266168e20c1SEd Tanous [asyncResp](const boost::system::error_code ec, 267168e20c1SEd Tanous const std::vector< 268168e20c1SEd Tanous std::pair<std::string, dbus::utility::DbusVariantType>>& 26903913171SWilly Tu propertiesList) { 27003913171SWilly Tu if (ec) 27103913171SWilly Tu { 27203913171SWilly Tu // this interface isn't necessary 27303913171SWilly Tu return; 27403913171SWilly Tu } 275168e20c1SEd Tanous for (const std::pair<std::string, dbus::utility::DbusVariantType>& 27603913171SWilly Tu property : propertiesList) 27703913171SWilly Tu { 27803913171SWilly Tu // Store DBus properties that are also 27903913171SWilly Tu // Redfish properties with same name and a 28003913171SWilly Tu // string value 28103913171SWilly Tu const std::string& propertyName = property.first; 28203913171SWilly Tu if ((propertyName == "PartNumber") || 28303913171SWilly Tu (propertyName == "SerialNumber") || 284002d39b4SEd Tanous (propertyName == "Manufacturer") || (propertyName == "Model")) 28503913171SWilly Tu { 28603913171SWilly Tu const std::string* value = 28703913171SWilly Tu std::get_if<std::string>(&property.second); 28803913171SWilly Tu if (value == nullptr) 28903913171SWilly Tu { 29003913171SWilly Tu // illegal property 29103913171SWilly Tu messages::internalError(asyncResp->res); 29203913171SWilly Tu return; 29303913171SWilly Tu } 29403913171SWilly Tu asyncResp->res.jsonValue[propertyName] = *value; 29503913171SWilly Tu } 29603913171SWilly Tu } 29703913171SWilly Tu }, 29803913171SWilly Tu connectionName, path, "org.freedesktop.DBus.Properties", "GetAll", 29903913171SWilly Tu "xyz.openbmc_project.Inventory.Decorator.Asset"); 30003913171SWilly Tu } 30103913171SWilly Tu 30203913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 30303913171SWilly Tu const std::string& connectionName, 30403913171SWilly Tu const std::string& path) 30503913171SWilly Tu { 3061e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 3071e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 3081e1e598dSJonathan Doman "xyz.openbmc_project.Inventory.Item", "Present", 30903913171SWilly Tu [asyncResp, path](const boost::system::error_code ec, 3101e1e598dSJonathan Doman const bool enabled) { 31103913171SWilly Tu // this interface isn't necessary, only check it if 31203913171SWilly Tu // we get a good return 31303913171SWilly Tu if (ec) 31403913171SWilly Tu { 31503913171SWilly Tu return; 31603913171SWilly Tu } 31703913171SWilly Tu 3181e1e598dSJonathan Doman if (!enabled) 31903913171SWilly Tu { 32003913171SWilly Tu asyncResp->res.jsonValue["Status"]["State"] = "Disabled"; 32103913171SWilly Tu } 3221e1e598dSJonathan Doman }); 32303913171SWilly Tu } 32403913171SWilly Tu 32503913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 32603913171SWilly Tu const std::string& connectionName, 32703913171SWilly Tu const std::string& path) 32803913171SWilly Tu { 3291e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 3301e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 3311e1e598dSJonathan Doman "xyz.openbmc_project.State.Drive", "Rebuilding", 3321e1e598dSJonathan Doman [asyncResp](const boost::system::error_code ec, const bool updating) { 33303913171SWilly Tu // this interface isn't necessary, only check it 33403913171SWilly Tu // if we get a good return 33503913171SWilly Tu if (ec) 33603913171SWilly Tu { 33703913171SWilly Tu return; 33803913171SWilly Tu } 33903913171SWilly Tu 34003913171SWilly Tu // updating and disabled in the backend shouldn't be 34103913171SWilly Tu // able to be set at the same time, so we don't need 34203913171SWilly Tu // to check for the race condition of these two 34303913171SWilly Tu // calls 3441e1e598dSJonathan Doman if (updating) 34503913171SWilly Tu { 34603913171SWilly Tu asyncResp->res.jsonValue["Status"]["State"] = "Updating"; 34703913171SWilly Tu } 3481e1e598dSJonathan Doman }); 34903913171SWilly Tu } 35003913171SWilly Tu 35119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type) 35219b8e9a0SWilly Tu { 35319b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD") 35419b8e9a0SWilly Tu { 35519b8e9a0SWilly Tu return "HDD"; 35619b8e9a0SWilly Tu } 35719b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD") 35819b8e9a0SWilly Tu { 35919b8e9a0SWilly Tu return "SSD"; 36019b8e9a0SWilly Tu } 36119b8e9a0SWilly Tu 36219b8e9a0SWilly Tu return std::nullopt; 36319b8e9a0SWilly Tu } 36419b8e9a0SWilly Tu 36519b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto) 36619b8e9a0SWilly Tu { 36719b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS") 36819b8e9a0SWilly Tu { 36919b8e9a0SWilly Tu return "SAS"; 37019b8e9a0SWilly Tu } 37119b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA") 37219b8e9a0SWilly Tu { 37319b8e9a0SWilly Tu return "SATA"; 37419b8e9a0SWilly Tu } 37519b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe") 37619b8e9a0SWilly Tu { 37719b8e9a0SWilly Tu return "NVMe"; 37819b8e9a0SWilly Tu } 37919b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC") 38019b8e9a0SWilly Tu { 38119b8e9a0SWilly Tu return "FC"; 38219b8e9a0SWilly Tu } 38319b8e9a0SWilly Tu 38419b8e9a0SWilly Tu return std::nullopt; 38519b8e9a0SWilly Tu } 38619b8e9a0SWilly Tu 38719b8e9a0SWilly Tu inline void 38819b8e9a0SWilly Tu getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 38919b8e9a0SWilly Tu const std::string& connectionName, 39019b8e9a0SWilly Tu const std::string& path) 39119b8e9a0SWilly Tu { 39219b8e9a0SWilly Tu sdbusplus::asio::getAllProperties( 39319b8e9a0SWilly Tu *crow::connections::systemBus, connectionName, path, 39419b8e9a0SWilly Tu "xyz.openbmc_project.Inventory.Item.Drive", 39519b8e9a0SWilly Tu [asyncResp](const boost::system::error_code ec, 39619b8e9a0SWilly Tu const std::vector< 39719b8e9a0SWilly Tu std::pair<std::string, dbus::utility::DbusVariantType>>& 39819b8e9a0SWilly Tu propertiesList) { 39919b8e9a0SWilly Tu if (ec) 40019b8e9a0SWilly Tu { 40119b8e9a0SWilly Tu // this interface isn't required 40219b8e9a0SWilly Tu return; 40319b8e9a0SWilly Tu } 40419b8e9a0SWilly Tu for (const std::pair<std::string, dbus::utility::DbusVariantType>& 40519b8e9a0SWilly Tu property : propertiesList) 40619b8e9a0SWilly Tu { 40719b8e9a0SWilly Tu const std::string& propertyName = property.first; 40819b8e9a0SWilly Tu if (propertyName == "Type") 40919b8e9a0SWilly Tu { 41019b8e9a0SWilly Tu const std::string* value = 41119b8e9a0SWilly Tu std::get_if<std::string>(&property.second); 41219b8e9a0SWilly Tu if (value == nullptr) 41319b8e9a0SWilly Tu { 41419b8e9a0SWilly Tu // illegal property 41519b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Type"; 41619b8e9a0SWilly Tu messages::internalError(asyncResp->res); 41719b8e9a0SWilly Tu return; 41819b8e9a0SWilly Tu } 41919b8e9a0SWilly Tu 420002d39b4SEd Tanous std::optional<std::string> mediaType = convertDriveType(*value); 42119b8e9a0SWilly Tu if (!mediaType) 42219b8e9a0SWilly Tu { 42319b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: " 42419b8e9a0SWilly Tu << *value; 42519b8e9a0SWilly Tu messages::internalError(asyncResp->res); 42619b8e9a0SWilly Tu return; 42719b8e9a0SWilly Tu } 42819b8e9a0SWilly Tu 42919b8e9a0SWilly Tu asyncResp->res.jsonValue["MediaType"] = *mediaType; 43019b8e9a0SWilly Tu } 43119b8e9a0SWilly Tu else if (propertyName == "Capacity") 43219b8e9a0SWilly Tu { 43319b8e9a0SWilly Tu const uint64_t* capacity = 43419b8e9a0SWilly Tu std::get_if<uint64_t>(&property.second); 43519b8e9a0SWilly Tu if (capacity == nullptr) 43619b8e9a0SWilly Tu { 43719b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Capacity"; 43819b8e9a0SWilly Tu messages::internalError(asyncResp->res); 43919b8e9a0SWilly Tu return; 44019b8e9a0SWilly Tu } 44119b8e9a0SWilly Tu if (*capacity == 0) 44219b8e9a0SWilly Tu { 44319b8e9a0SWilly Tu // drive capacity not known 44419b8e9a0SWilly Tu continue; 44519b8e9a0SWilly Tu } 44619b8e9a0SWilly Tu 44719b8e9a0SWilly Tu asyncResp->res.jsonValue["CapacityBytes"] = *capacity; 44819b8e9a0SWilly Tu } 44919b8e9a0SWilly Tu else if (propertyName == "Protocol") 45019b8e9a0SWilly Tu { 45119b8e9a0SWilly Tu const std::string* value = 45219b8e9a0SWilly Tu std::get_if<std::string>(&property.second); 45319b8e9a0SWilly Tu if (value == nullptr) 45419b8e9a0SWilly Tu { 45519b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Protocol"; 45619b8e9a0SWilly Tu messages::internalError(asyncResp->res); 45719b8e9a0SWilly Tu return; 45819b8e9a0SWilly Tu } 45919b8e9a0SWilly Tu 460002d39b4SEd Tanous std::optional<std::string> proto = convertDriveProtocol(*value); 46119b8e9a0SWilly Tu if (!proto) 46219b8e9a0SWilly Tu { 463002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Unsupported DrivePrototype Interface: " 46419b8e9a0SWilly Tu << *value; 46519b8e9a0SWilly Tu messages::internalError(asyncResp->res); 46619b8e9a0SWilly Tu return; 46719b8e9a0SWilly Tu } 46819b8e9a0SWilly Tu asyncResp->res.jsonValue["Protocol"] = *proto; 46919b8e9a0SWilly Tu } 47019b8e9a0SWilly Tu } 47119b8e9a0SWilly Tu }); 47219b8e9a0SWilly Tu } 47319b8e9a0SWilly Tu 4747e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app) 4757e860f15SJohn Edward Broadbent { 4767e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/") 477ed398213SEd Tanous .privileges(redfish::privileges::getDrive) 478002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 479002d39b4SEd Tanous [&app](const crow::Request& req, 48045ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4817e860f15SJohn Edward Broadbent const std::string& driveId) { 48245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 48345ca1b86SEd Tanous { 48445ca1b86SEd Tanous return; 48545ca1b86SEd Tanous } 4867e860f15SJohn Edward Broadbent crow::connections::systemBus->async_method_call( 487002d39b4SEd Tanous [asyncResp, 488002d39b4SEd Tanous driveId](const boost::system::error_code ec, 489b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 4907e860f15SJohn Edward Broadbent if (ec) 4917e860f15SJohn Edward Broadbent { 4927e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Drive mapper call error"; 4937e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 4947e860f15SJohn Edward Broadbent return; 4957e860f15SJohn Edward Broadbent } 4967e860f15SJohn Edward Broadbent 49703913171SWilly Tu auto drive = std::find_if( 4987e860f15SJohn Edward Broadbent subtree.begin(), subtree.end(), 499002d39b4SEd Tanous [&driveId]( 500002d39b4SEd Tanous const std::pair< 50103913171SWilly Tu std::string, 50203913171SWilly Tu std::vector<std::pair< 503002d39b4SEd Tanous std::string, std::vector<std::string>>>>& object) { 50403913171SWilly Tu return sdbusplus::message::object_path(object.first) 50503913171SWilly Tu .filename() == driveId; 5067e860f15SJohn Edward Broadbent }); 5077e860f15SJohn Edward Broadbent 50803913171SWilly Tu if (drive == subtree.end()) 5097e860f15SJohn Edward Broadbent { 510002d39b4SEd Tanous messages::resourceNotFound(asyncResp->res, "Drive", driveId); 5117e860f15SJohn Edward Broadbent return; 5127e860f15SJohn Edward Broadbent } 5137e860f15SJohn Edward Broadbent 51403913171SWilly Tu const std::string& path = drive->first; 515002d39b4SEd Tanous const std::vector<std::pair<std::string, std::vector<std::string>>>& 51603913171SWilly Tu connectionNames = drive->second; 5177e860f15SJohn Edward Broadbent 518002d39b4SEd Tanous asyncResp->res.jsonValue["@odata.type"] = "#Drive.v1_7_0.Drive"; 5197e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.id"] = 520002d39b4SEd Tanous "/redfish/v1/Systems/system/Storage/1/Drives/" + driveId; 5217e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = driveId; 5227e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Id"] = driveId; 5237e860f15SJohn Edward Broadbent 5247e860f15SJohn Edward Broadbent if (connectionNames.size() != 1) 5257e860f15SJohn Edward Broadbent { 526002d39b4SEd Tanous BMCWEB_LOG_ERROR << "Connection size " << connectionNames.size() 52703913171SWilly Tu << ", not equal to 1"; 5287e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 5297e860f15SJohn Edward Broadbent return; 5307e860f15SJohn Edward Broadbent } 5317e860f15SJohn Edward Broadbent 5327e860f15SJohn Edward Broadbent getMainChassisId( 533002d39b4SEd Tanous asyncResp, [](const std::string& chassisId, 5347e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& aRsp) { 535002d39b4SEd Tanous aRsp->res.jsonValue["Links"]["Chassis"]["@odata.id"] = 5361476687dSEd Tanous "/redfish/v1/Chassis/" + chassisId; 5377e860f15SJohn Edward Broadbent }); 5387e860f15SJohn Edward Broadbent 539a25aeccfSNikhil Potade // default it to Enabled 540a25aeccfSNikhil Potade asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 541a25aeccfSNikhil Potade 5422ad9c2f6SJames Feist auto health = std::make_shared<HealthPopulate>(asyncResp); 543e284a7c1SJames Feist health->inventory.emplace_back(path); 5442ad9c2f6SJames Feist health->populate(); 5452ad9c2f6SJames Feist 546002d39b4SEd Tanous const std::string& connectionName = connectionNames[0].first; 54722984074SJames Feist 548002d39b4SEd Tanous for (const std::string& interface : connectionNames[0].second) 54961b83d0cSWilly Tu { 55061b83d0cSWilly Tu if (interface == 55161b83d0cSWilly Tu "xyz.openbmc_project.Inventory.Decorator.Asset") 55261b83d0cSWilly Tu { 55303913171SWilly Tu getDriveAsset(asyncResp, connectionName, path); 55461b83d0cSWilly Tu } 555002d39b4SEd Tanous else if (interface == "xyz.openbmc_project.Inventory.Item") 55661b83d0cSWilly Tu { 55703913171SWilly Tu getDrivePresent(asyncResp, connectionName, path); 55861b83d0cSWilly Tu } 55961b83d0cSWilly Tu else if (interface == "xyz.openbmc_project.State.Drive") 56061b83d0cSWilly Tu { 56103913171SWilly Tu getDriveState(asyncResp, connectionName, path); 56261b83d0cSWilly Tu } 56361b83d0cSWilly Tu else if (interface == 56461b83d0cSWilly Tu "xyz.openbmc_project.Inventory.Item.Drive") 56561b83d0cSWilly Tu { 566002d39b4SEd Tanous getDriveItemProperties(asyncResp, connectionName, path); 56761b83d0cSWilly Tu } 56861b83d0cSWilly Tu } 569a25aeccfSNikhil Potade }, 570a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", 571a25aeccfSNikhil Potade "/xyz/openbmc_project/object_mapper", 572a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", "GetSubTree", 573a25aeccfSNikhil Potade "/xyz/openbmc_project/inventory", int32_t(0), 574a25aeccfSNikhil Potade std::array<const char*, 1>{ 575a25aeccfSNikhil Potade "xyz.openbmc_project.Inventory.Item.Drive"}); 5767e860f15SJohn Edward Broadbent }); 577a25aeccfSNikhil Potade } 57892903bd4SJohn Edward Broadbent 57992903bd4SJohn Edward Broadbent /** 58092903bd4SJohn Edward Broadbent * Chassis drives, this URL will show all the DriveCollection 58192903bd4SJohn Edward Broadbent * information 58292903bd4SJohn Edward Broadbent */ 58392903bd4SJohn Edward Broadbent void chassisDriveCollectionGet( 58492903bd4SJohn Edward Broadbent crow::App& app, const crow::Request& req, 58592903bd4SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 58692903bd4SJohn Edward Broadbent const std::string& chassisId) 58792903bd4SJohn Edward Broadbent { 58892903bd4SJohn Edward Broadbent if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 58992903bd4SJohn Edward Broadbent { 59092903bd4SJohn Edward Broadbent return; 59192903bd4SJohn Edward Broadbent } 59292903bd4SJohn Edward Broadbent 59392903bd4SJohn Edward Broadbent // mapper call lambda 59492903bd4SJohn Edward Broadbent crow::connections::systemBus->async_method_call( 59592903bd4SJohn Edward Broadbent [asyncResp, 59692903bd4SJohn Edward Broadbent chassisId](const boost::system::error_code ec, 59792903bd4SJohn Edward Broadbent const dbus::utility::MapperGetSubTreeResponse& subtree) { 59892903bd4SJohn Edward Broadbent if (ec) 59992903bd4SJohn Edward Broadbent { 60092903bd4SJohn Edward Broadbent if (ec == boost::system::errc::host_unreachable) 60192903bd4SJohn Edward Broadbent { 60292903bd4SJohn Edward Broadbent messages::resourceNotFound(asyncResp->res, "Chassis", 60392903bd4SJohn Edward Broadbent chassisId); 60492903bd4SJohn Edward Broadbent return; 60592903bd4SJohn Edward Broadbent } 60692903bd4SJohn Edward Broadbent messages::internalError(asyncResp->res); 60792903bd4SJohn Edward Broadbent return; 60892903bd4SJohn Edward Broadbent } 60992903bd4SJohn Edward Broadbent 61092903bd4SJohn Edward Broadbent // Iterate over all retrieved ObjectPaths. 61192903bd4SJohn Edward Broadbent for (const std::pair< 61292903bd4SJohn Edward Broadbent std::string, 61392903bd4SJohn Edward Broadbent std::vector<std::pair<std::string, std::vector<std::string>>>>& 61492903bd4SJohn Edward Broadbent object : subtree) 61592903bd4SJohn Edward Broadbent { 61692903bd4SJohn Edward Broadbent const std::string& path = object.first; 61792903bd4SJohn Edward Broadbent const dbus::utility::MapperGetObject& connectionNames = 61892903bd4SJohn Edward Broadbent object.second; 61992903bd4SJohn Edward Broadbent 62092903bd4SJohn Edward Broadbent sdbusplus::message::object_path objPath(path); 62192903bd4SJohn Edward Broadbent if (objPath.filename() != chassisId) 62292903bd4SJohn Edward Broadbent { 62392903bd4SJohn Edward Broadbent continue; 62492903bd4SJohn Edward Broadbent } 62592903bd4SJohn Edward Broadbent 62692903bd4SJohn Edward Broadbent if (connectionNames.empty()) 62792903bd4SJohn Edward Broadbent { 62892903bd4SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Got 0 Connection names"; 62992903bd4SJohn Edward Broadbent continue; 63092903bd4SJohn Edward Broadbent } 63192903bd4SJohn Edward Broadbent 63292903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.type"] = 63392903bd4SJohn Edward Broadbent "#DriveCollection.DriveCollection"; 63492903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.id"] = 63592903bd4SJohn Edward Broadbent crow::utility::urlFromPieces("redfish", "v1", 63692903bd4SJohn Edward Broadbent "Chassis" + chassisId + "Drives"); 63792903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = "Drive Collection"; 63892903bd4SJohn Edward Broadbent 63992903bd4SJohn Edward Broadbent // Association lambda 64092903bd4SJohn Edward Broadbent sdbusplus::asio::getProperty<std::vector<std::string>>( 64192903bd4SJohn Edward Broadbent *crow::connections::systemBus, 64292903bd4SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", path + "/drive", 64392903bd4SJohn Edward Broadbent "xyz.openbmc_project.Association", "endpoints", 64492903bd4SJohn Edward Broadbent [asyncResp, chassisId](const boost::system::error_code ec3, 64592903bd4SJohn Edward Broadbent const std::vector<std::string>& resp) { 64692903bd4SJohn Edward Broadbent if (ec3) 64792903bd4SJohn Edward Broadbent { 64892903bd4SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Error in chassis Drive association "; 64992903bd4SJohn Edward Broadbent } 65092903bd4SJohn Edward Broadbent nlohmann::json& members = asyncResp->res.jsonValue["Members"]; 65192903bd4SJohn Edward Broadbent // important if array is empty 65292903bd4SJohn Edward Broadbent members = nlohmann::json::array(); 65392903bd4SJohn Edward Broadbent 65492903bd4SJohn Edward Broadbent std::vector<std::string> leafNames; 65592903bd4SJohn Edward Broadbent for (const auto& drive : resp) 65692903bd4SJohn Edward Broadbent { 65792903bd4SJohn Edward Broadbent sdbusplus::message::object_path path(drive); 65892903bd4SJohn Edward Broadbent leafNames.push_back(path.filename()); 65992903bd4SJohn Edward Broadbent } 66092903bd4SJohn Edward Broadbent 66192903bd4SJohn Edward Broadbent std::sort(leafNames.begin(), leafNames.end(), 66292903bd4SJohn Edward Broadbent AlphanumLess<std::string>()); 66392903bd4SJohn Edward Broadbent 66492903bd4SJohn Edward Broadbent for (const auto& leafName : leafNames) 66592903bd4SJohn Edward Broadbent { 66692903bd4SJohn Edward Broadbent nlohmann::json::object_t member; 66792903bd4SJohn Edward Broadbent member["@odata.id"] = crow::utility::urlFromPieces( 66892903bd4SJohn Edward Broadbent "redfish", "v1", "Chassis", chassisId, "Drives", 66992903bd4SJohn Edward Broadbent leafName); 67092903bd4SJohn Edward Broadbent members.push_back(std::move(member)); 67192903bd4SJohn Edward Broadbent // navigation links will be registered in next patch set 67292903bd4SJohn Edward Broadbent } 67392903bd4SJohn Edward Broadbent asyncResp->res.jsonValue["Members@odata.count"] = resp.size(); 67492903bd4SJohn Edward Broadbent }); // end association lambda 67592903bd4SJohn Edward Broadbent 67692903bd4SJohn Edward Broadbent } // end Iterate over all retrieved ObjectPaths 67792903bd4SJohn Edward Broadbent }, 67892903bd4SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", 67992903bd4SJohn Edward Broadbent "/xyz/openbmc_project/object_mapper", 68092903bd4SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", "GetSubTree", 68192903bd4SJohn Edward Broadbent "/xyz/openbmc_project/inventory", 0, 68292903bd4SJohn Edward Broadbent std::array<const char*, 2>{ 68392903bd4SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.Board", 68492903bd4SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.Chassis"}); 68592903bd4SJohn Edward Broadbent } 68692903bd4SJohn Edward Broadbent 68792903bd4SJohn Edward Broadbent inline void requestRoutesChassisDrive(App& app) 68892903bd4SJohn Edward Broadbent { 68992903bd4SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Chassis/<str>/Drives/") 69092903bd4SJohn Edward Broadbent .privileges(redfish::privileges::getDriveCollection) 69192903bd4SJohn Edward Broadbent .methods(boost::beast::http::verb::get)( 69292903bd4SJohn Edward Broadbent std::bind_front(chassisDriveCollectionGet, std::ref(app))); 69392903bd4SJohn Edward Broadbent } 69492903bd4SJohn Edward Broadbent 695a25aeccfSNikhil Potade } // namespace redfish 696