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"; 45*1476687dSEd Tanous nlohmann::json::array_t members; 46*1476687dSEd Tanous nlohmann::json::object_t member; 47*1476687dSEd Tanous member["@odata.id"] = "/redfish/v1/Systems/system/Storage/1"; 48*1476687dSEd Tanous members.emplace_back(member); 49*1476687dSEd 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) 5845ca1b86SEd Tanous .methods(boost::beast::http::verb::get)([&app](const crow::Request& req, 5945ca1b86SEd Tanous const std::shared_ptr< 6045ca1b86SEd Tanous bmcweb::AsyncResp>& 6145ca1b86SEd Tanous asyncResp) { 6245ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 6345ca1b86SEd Tanous { 6445ca1b86SEd Tanous return; 6545ca1b86SEd Tanous } 668d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.type"] = "#Storage.v1_7_1.Storage"; 678d1b46d7Szhanghch05 asyncResp->res.jsonValue["@odata.id"] = 688d1b46d7Szhanghch05 "/redfish/v1/Systems/system/Storage/1"; 698d1b46d7Szhanghch05 asyncResp->res.jsonValue["Name"] = "Storage"; 708d1b46d7Szhanghch05 asyncResp->res.jsonValue["Id"] = "1"; 718d1b46d7Szhanghch05 asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 72a25aeccfSNikhil Potade 73e284a7c1SJames Feist auto health = std::make_shared<HealthPopulate>(asyncResp); 74e284a7c1SJames Feist health->populate(); 75e284a7c1SJames Feist 76a25aeccfSNikhil Potade crow::connections::systemBus->async_method_call( 777e860f15SJohn Edward Broadbent [asyncResp, 787e860f15SJohn Edward Broadbent health](const boost::system::error_code ec, 79b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreePathsResponse& 80b9d36b47SEd Tanous storageList) { 81a25aeccfSNikhil Potade nlohmann::json& storageArray = 82a25aeccfSNikhil Potade asyncResp->res.jsonValue["Drives"]; 83a25aeccfSNikhil Potade storageArray = nlohmann::json::array(); 847e860f15SJohn Edward Broadbent auto& count = 857e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Drives@odata.count"]; 86e284a7c1SJames Feist count = 0; 872ad9c2f6SJames Feist 88a25aeccfSNikhil Potade if (ec) 89a25aeccfSNikhil Potade { 90a25aeccfSNikhil Potade BMCWEB_LOG_ERROR << "Drive mapper call error"; 91a25aeccfSNikhil Potade messages::internalError(asyncResp->res); 92a25aeccfSNikhil Potade return; 93a25aeccfSNikhil Potade } 942ad9c2f6SJames Feist 95e284a7c1SJames Feist health->inventory.insert(health->inventory.end(), 96e284a7c1SJames Feist storageList.begin(), 97e284a7c1SJames Feist storageList.end()); 982ad9c2f6SJames Feist 99a25aeccfSNikhil Potade for (const std::string& objpath : storageList) 100a25aeccfSNikhil Potade { 101f23b7296SEd Tanous std::size_t lastPos = objpath.rfind('/'); 102a25aeccfSNikhil Potade if (lastPos == std::string::npos || 103a25aeccfSNikhil Potade (objpath.size() <= lastPos + 1)) 104a25aeccfSNikhil Potade { 1057e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Failed to find '/' in " 1067e860f15SJohn Edward Broadbent << objpath; 107a25aeccfSNikhil Potade continue; 108a25aeccfSNikhil Potade } 109*1476687dSEd Tanous nlohmann::json::object_t storage; 110*1476687dSEd Tanous storage["@odata.id"] = 111be13ceceSJames Feist "/redfish/v1/Systems/system/Storage/1/Drives/" + 112*1476687dSEd Tanous objpath.substr(lastPos + 1); 113*1476687dSEd Tanous storageArray.push_back(std::move(storage)); 114a25aeccfSNikhil Potade } 115a25aeccfSNikhil Potade 116e284a7c1SJames Feist count = storageArray.size(); 117a25aeccfSNikhil Potade }, 118a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", 119a25aeccfSNikhil Potade "/xyz/openbmc_project/object_mapper", 120a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", 121a25aeccfSNikhil Potade "/xyz/openbmc_project/inventory", int32_t(0), 122a25aeccfSNikhil Potade std::array<const char*, 1>{ 123a25aeccfSNikhil Potade "xyz.openbmc_project.Inventory.Item.Drive"}); 124e284a7c1SJames Feist 125e284a7c1SJames Feist crow::connections::systemBus->async_method_call( 126b9d36b47SEd Tanous [asyncResp, health]( 127b9d36b47SEd Tanous const boost::system::error_code ec, 128b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 12926f6976fSEd Tanous if (ec || subtree.empty()) 130e284a7c1SJames Feist { 131d819a420SJames Feist // doesn't have to be there 132e284a7c1SJames Feist return; 133e284a7c1SJames Feist } 134e284a7c1SJames Feist 135e284a7c1SJames Feist nlohmann::json& root = 136e284a7c1SJames Feist asyncResp->res.jsonValue["StorageControllers"]; 137e284a7c1SJames Feist root = nlohmann::json::array(); 138e284a7c1SJames Feist for (const auto& [path, interfaceDict] : subtree) 139e284a7c1SJames Feist { 140f23b7296SEd Tanous std::size_t lastPos = path.rfind('/'); 141e284a7c1SJames Feist if (lastPos == std::string::npos || 142e284a7c1SJames Feist (path.size() <= lastPos + 1)) 143e284a7c1SJames Feist { 1447e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Failed to find '/' in " 1457e860f15SJohn Edward Broadbent << path; 146e284a7c1SJames Feist return; 147e284a7c1SJames Feist } 148e284a7c1SJames Feist 149e284a7c1SJames Feist if (interfaceDict.size() != 1) 150e284a7c1SJames Feist { 151e284a7c1SJames Feist BMCWEB_LOG_ERROR << "Connection size " 152e284a7c1SJames Feist << interfaceDict.size() 153e284a7c1SJames Feist << ", greater than 1"; 154e284a7c1SJames Feist messages::internalError(asyncResp->res); 155e284a7c1SJames Feist return; 156e284a7c1SJames Feist } 157e284a7c1SJames Feist 158e284a7c1SJames Feist const std::string& connectionName = 159e284a7c1SJames Feist interfaceDict.front().first; 160e284a7c1SJames Feist 161e284a7c1SJames Feist size_t index = root.size(); 162e284a7c1SJames Feist nlohmann::json& storageController = 163e284a7c1SJames Feist root.emplace_back(nlohmann::json::object()); 164e284a7c1SJames Feist 165e284a7c1SJames Feist std::string id = path.substr(lastPos + 1); 166e284a7c1SJames Feist 167e284a7c1SJames Feist storageController["@odata.type"] = 168e284a7c1SJames Feist "#Storage.v1_7_0.StorageController"; 169e284a7c1SJames Feist storageController["@odata.id"] = 1700fda0f12SGeorge Liu "/redfish/v1/Systems/system/Storage/1#/StorageControllers/" + 171e284a7c1SJames Feist std::to_string(index); 172e284a7c1SJames Feist storageController["Name"] = id; 173e284a7c1SJames Feist storageController["MemberId"] = id; 174e284a7c1SJames Feist storageController["Status"]["State"] = "Enabled"; 175e284a7c1SJames Feist 1761e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 1771e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 1781e1e598dSJonathan Doman "xyz.openbmc_project.Inventory.Item", "Present", 1791e1e598dSJonathan Doman [asyncResp, 1801e1e598dSJonathan Doman index](const boost::system::error_code ec2, 1811e1e598dSJonathan Doman bool enabled) { 1827e860f15SJohn Edward Broadbent // this interface isn't necessary, only check it 1837e860f15SJohn Edward Broadbent // if we get a good return 18423a21a1cSEd Tanous if (ec2) 185e284a7c1SJames Feist { 186e284a7c1SJames Feist return; 187e284a7c1SJames Feist } 1881e1e598dSJonathan Doman if (!enabled) 189e284a7c1SJames Feist { 190e284a7c1SJames Feist asyncResp->res 191e284a7c1SJames Feist .jsonValue["StorageControllers"][index] 1927e860f15SJohn Edward Broadbent ["Status"]["State"] = 1937e860f15SJohn Edward Broadbent "Disabled"; 194e284a7c1SJames Feist } 1951e1e598dSJonathan Doman }); 196e284a7c1SJames Feist 197e284a7c1SJames Feist crow::connections::systemBus->async_method_call( 1987e860f15SJohn Edward Broadbent [asyncResp, index]( 1997e860f15SJohn Edward Broadbent const boost::system::error_code ec2, 200168e20c1SEd Tanous const std::vector< 201168e20c1SEd Tanous std::pair<std::string, 202168e20c1SEd Tanous dbus::utility::DbusVariantType>>& 2037e860f15SJohn Edward Broadbent propertiesList) { 2047e860f15SJohn Edward Broadbent if (ec2) 2057e860f15SJohn Edward Broadbent { 2067e860f15SJohn Edward Broadbent // this interface isn't necessary 2077e860f15SJohn Edward Broadbent return; 2087e860f15SJohn Edward Broadbent } 2097e860f15SJohn Edward Broadbent for (const std::pair< 2107e860f15SJohn Edward Broadbent std::string, 211168e20c1SEd Tanous dbus::utility::DbusVariantType>& 212168e20c1SEd Tanous property : propertiesList) 2137e860f15SJohn Edward Broadbent { 2147e860f15SJohn Edward Broadbent // Store DBus properties that are also 2157e860f15SJohn Edward Broadbent // Redfish properties with same name and a 2167e860f15SJohn Edward Broadbent // string value 2177e860f15SJohn Edward Broadbent const std::string& propertyName = 2187e860f15SJohn Edward Broadbent property.first; 2197e860f15SJohn Edward Broadbent nlohmann::json& object = 2207e860f15SJohn Edward Broadbent asyncResp->res 2217e860f15SJohn Edward Broadbent .jsonValue["StorageControllers"] 2227e860f15SJohn Edward Broadbent [index]; 2237e860f15SJohn Edward Broadbent if ((propertyName == "PartNumber") || 2247e860f15SJohn Edward Broadbent (propertyName == "SerialNumber") || 2257e860f15SJohn Edward Broadbent (propertyName == "Manufacturer") || 2267e860f15SJohn Edward Broadbent (propertyName == "Model")) 2277e860f15SJohn Edward Broadbent { 2287e860f15SJohn Edward Broadbent const std::string* value = 2297e860f15SJohn Edward Broadbent std::get_if<std::string>( 2307e860f15SJohn Edward Broadbent &property.second); 2317e860f15SJohn Edward Broadbent if (value == nullptr) 2327e860f15SJohn Edward Broadbent { 2337e860f15SJohn Edward Broadbent // illegal property 2347e860f15SJohn Edward Broadbent messages::internalError( 2357e860f15SJohn Edward Broadbent asyncResp->res); 2367e860f15SJohn Edward Broadbent return; 2377e860f15SJohn Edward Broadbent } 2387e860f15SJohn Edward Broadbent object[propertyName] = *value; 2397e860f15SJohn Edward Broadbent } 2407e860f15SJohn Edward Broadbent } 2417e860f15SJohn Edward Broadbent }, 2427e860f15SJohn Edward Broadbent connectionName, path, 2437e860f15SJohn Edward Broadbent "org.freedesktop.DBus.Properties", "GetAll", 2447e860f15SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Decorator.Asset"); 2457e860f15SJohn Edward Broadbent } 2467e860f15SJohn Edward Broadbent 2477e860f15SJohn Edward Broadbent // this is done after we know the json array will no longer 2487e860f15SJohn Edward Broadbent // be resized, as json::array uses vector underneath and we 2497e860f15SJohn Edward Broadbent // need references to its members that won't change 2507e860f15SJohn Edward Broadbent size_t count = 0; 2517e860f15SJohn Edward Broadbent for (const auto& [path, interfaceDict] : subtree) 2527e860f15SJohn Edward Broadbent { 2537e860f15SJohn Edward Broadbent auto subHealth = std::make_shared<HealthPopulate>( 2547e860f15SJohn Edward Broadbent asyncResp, root[count]["Status"]); 2557e860f15SJohn Edward Broadbent subHealth->inventory.emplace_back(path); 2567e860f15SJohn Edward Broadbent health->inventory.emplace_back(path); 2577e860f15SJohn Edward Broadbent health->children.emplace_back(subHealth); 2587e860f15SJohn Edward Broadbent count++; 2597e860f15SJohn Edward Broadbent } 2607e860f15SJohn Edward Broadbent }, 2617e860f15SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", 2627e860f15SJohn Edward Broadbent "/xyz/openbmc_project/object_mapper", 2637e860f15SJohn Edward Broadbent "xyz.openbmc_project.ObjectMapper", "GetSubTree", 2647e860f15SJohn Edward Broadbent "/xyz/openbmc_project/inventory", int32_t(0), 2657e860f15SJohn Edward Broadbent std::array<const char*, 1>{ 2667e860f15SJohn Edward Broadbent "xyz.openbmc_project.Inventory.Item.StorageController"}); 2677e860f15SJohn Edward Broadbent }); 2687e860f15SJohn Edward Broadbent } 2697e860f15SJohn Edward Broadbent 27003913171SWilly Tu inline void getDriveAsset(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 27103913171SWilly Tu const std::string& connectionName, 27203913171SWilly Tu const std::string& path) 27303913171SWilly Tu { 27403913171SWilly Tu crow::connections::systemBus->async_method_call( 275168e20c1SEd Tanous [asyncResp](const boost::system::error_code ec, 276168e20c1SEd Tanous const std::vector< 277168e20c1SEd Tanous std::pair<std::string, dbus::utility::DbusVariantType>>& 27803913171SWilly Tu propertiesList) { 27903913171SWilly Tu if (ec) 28003913171SWilly Tu { 28103913171SWilly Tu // this interface isn't necessary 28203913171SWilly Tu return; 28303913171SWilly Tu } 284168e20c1SEd Tanous for (const std::pair<std::string, dbus::utility::DbusVariantType>& 28503913171SWilly Tu property : propertiesList) 28603913171SWilly Tu { 28703913171SWilly Tu // Store DBus properties that are also 28803913171SWilly Tu // Redfish properties with same name and a 28903913171SWilly Tu // string value 29003913171SWilly Tu const std::string& propertyName = property.first; 29103913171SWilly Tu if ((propertyName == "PartNumber") || 29203913171SWilly Tu (propertyName == "SerialNumber") || 29303913171SWilly Tu (propertyName == "Manufacturer") || 29403913171SWilly Tu (propertyName == "Model")) 29503913171SWilly Tu { 29603913171SWilly Tu const std::string* value = 29703913171SWilly Tu std::get_if<std::string>(&property.second); 29803913171SWilly Tu if (value == nullptr) 29903913171SWilly Tu { 30003913171SWilly Tu // illegal property 30103913171SWilly Tu messages::internalError(asyncResp->res); 30203913171SWilly Tu return; 30303913171SWilly Tu } 30403913171SWilly Tu asyncResp->res.jsonValue[propertyName] = *value; 30503913171SWilly Tu } 30603913171SWilly Tu } 30703913171SWilly Tu }, 30803913171SWilly Tu connectionName, path, "org.freedesktop.DBus.Properties", "GetAll", 30903913171SWilly Tu "xyz.openbmc_project.Inventory.Decorator.Asset"); 31003913171SWilly Tu } 31103913171SWilly Tu 31203913171SWilly Tu inline void getDrivePresent(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 31303913171SWilly Tu const std::string& connectionName, 31403913171SWilly Tu const std::string& path) 31503913171SWilly Tu { 3161e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 3171e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 3181e1e598dSJonathan Doman "xyz.openbmc_project.Inventory.Item", "Present", 31903913171SWilly Tu [asyncResp, path](const boost::system::error_code ec, 3201e1e598dSJonathan Doman const bool enabled) { 32103913171SWilly Tu // this interface isn't necessary, only check it if 32203913171SWilly Tu // we get a good return 32303913171SWilly Tu if (ec) 32403913171SWilly Tu { 32503913171SWilly Tu return; 32603913171SWilly Tu } 32703913171SWilly Tu 3281e1e598dSJonathan Doman if (!enabled) 32903913171SWilly Tu { 33003913171SWilly Tu asyncResp->res.jsonValue["Status"]["State"] = "Disabled"; 33103913171SWilly Tu } 3321e1e598dSJonathan Doman }); 33303913171SWilly Tu } 33403913171SWilly Tu 33503913171SWilly Tu inline void getDriveState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 33603913171SWilly Tu const std::string& connectionName, 33703913171SWilly Tu const std::string& path) 33803913171SWilly Tu { 3391e1e598dSJonathan Doman sdbusplus::asio::getProperty<bool>( 3401e1e598dSJonathan Doman *crow::connections::systemBus, connectionName, path, 3411e1e598dSJonathan Doman "xyz.openbmc_project.State.Drive", "Rebuilding", 3421e1e598dSJonathan Doman [asyncResp](const boost::system::error_code ec, const bool updating) { 34303913171SWilly Tu // this interface isn't necessary, only check it 34403913171SWilly Tu // if we get a good return 34503913171SWilly Tu if (ec) 34603913171SWilly Tu { 34703913171SWilly Tu return; 34803913171SWilly Tu } 34903913171SWilly Tu 35003913171SWilly Tu // updating and disabled in the backend shouldn't be 35103913171SWilly Tu // able to be set at the same time, so we don't need 35203913171SWilly Tu // to check for the race condition of these two 35303913171SWilly Tu // calls 3541e1e598dSJonathan Doman if (updating) 35503913171SWilly Tu { 35603913171SWilly Tu asyncResp->res.jsonValue["Status"]["State"] = "Updating"; 35703913171SWilly Tu } 3581e1e598dSJonathan Doman }); 35903913171SWilly Tu } 36003913171SWilly Tu 36119b8e9a0SWilly Tu inline std::optional<std::string> convertDriveType(const std::string& type) 36219b8e9a0SWilly Tu { 36319b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.HDD") 36419b8e9a0SWilly Tu { 36519b8e9a0SWilly Tu return "HDD"; 36619b8e9a0SWilly Tu } 36719b8e9a0SWilly Tu if (type == "xyz.openbmc_project.Inventory.Item.Drive.DriveType.SSD") 36819b8e9a0SWilly Tu { 36919b8e9a0SWilly Tu return "SSD"; 37019b8e9a0SWilly Tu } 37119b8e9a0SWilly Tu 37219b8e9a0SWilly Tu return std::nullopt; 37319b8e9a0SWilly Tu } 37419b8e9a0SWilly Tu 37519b8e9a0SWilly Tu inline std::optional<std::string> convertDriveProtocol(const std::string& proto) 37619b8e9a0SWilly Tu { 37719b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SAS") 37819b8e9a0SWilly Tu { 37919b8e9a0SWilly Tu return "SAS"; 38019b8e9a0SWilly Tu } 38119b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.SATA") 38219b8e9a0SWilly Tu { 38319b8e9a0SWilly Tu return "SATA"; 38419b8e9a0SWilly Tu } 38519b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.NVMe") 38619b8e9a0SWilly Tu { 38719b8e9a0SWilly Tu return "NVMe"; 38819b8e9a0SWilly Tu } 38919b8e9a0SWilly Tu if (proto == "xyz.openbmc_project.Inventory.Item.Drive.DriveProtocol.FC") 39019b8e9a0SWilly Tu { 39119b8e9a0SWilly Tu return "FC"; 39219b8e9a0SWilly Tu } 39319b8e9a0SWilly Tu 39419b8e9a0SWilly Tu return std::nullopt; 39519b8e9a0SWilly Tu } 39619b8e9a0SWilly Tu 39719b8e9a0SWilly Tu inline void 39819b8e9a0SWilly Tu getDriveItemProperties(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 39919b8e9a0SWilly Tu const std::string& connectionName, 40019b8e9a0SWilly Tu const std::string& path) 40119b8e9a0SWilly Tu { 40219b8e9a0SWilly Tu sdbusplus::asio::getAllProperties( 40319b8e9a0SWilly Tu *crow::connections::systemBus, connectionName, path, 40419b8e9a0SWilly Tu "xyz.openbmc_project.Inventory.Item.Drive", 40519b8e9a0SWilly Tu [asyncResp](const boost::system::error_code ec, 40619b8e9a0SWilly Tu const std::vector< 40719b8e9a0SWilly Tu std::pair<std::string, dbus::utility::DbusVariantType>>& 40819b8e9a0SWilly Tu propertiesList) { 40919b8e9a0SWilly Tu if (ec) 41019b8e9a0SWilly Tu { 41119b8e9a0SWilly Tu // this interface isn't required 41219b8e9a0SWilly Tu return; 41319b8e9a0SWilly Tu } 41419b8e9a0SWilly Tu for (const std::pair<std::string, dbus::utility::DbusVariantType>& 41519b8e9a0SWilly Tu property : propertiesList) 41619b8e9a0SWilly Tu { 41719b8e9a0SWilly Tu const std::string& propertyName = property.first; 41819b8e9a0SWilly Tu if (propertyName == "Type") 41919b8e9a0SWilly Tu { 42019b8e9a0SWilly Tu const std::string* value = 42119b8e9a0SWilly Tu std::get_if<std::string>(&property.second); 42219b8e9a0SWilly Tu if (value == nullptr) 42319b8e9a0SWilly Tu { 42419b8e9a0SWilly Tu // illegal property 42519b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Type"; 42619b8e9a0SWilly Tu messages::internalError(asyncResp->res); 42719b8e9a0SWilly Tu return; 42819b8e9a0SWilly Tu } 42919b8e9a0SWilly Tu 43019b8e9a0SWilly Tu std::optional<std::string> mediaType = 43119b8e9a0SWilly Tu convertDriveType(*value); 43219b8e9a0SWilly Tu if (!mediaType) 43319b8e9a0SWilly Tu { 43419b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Unsupported DriveType Interface: " 43519b8e9a0SWilly Tu << *value; 43619b8e9a0SWilly Tu messages::internalError(asyncResp->res); 43719b8e9a0SWilly Tu return; 43819b8e9a0SWilly Tu } 43919b8e9a0SWilly Tu 44019b8e9a0SWilly Tu asyncResp->res.jsonValue["MediaType"] = *mediaType; 44119b8e9a0SWilly Tu } 44219b8e9a0SWilly Tu else if (propertyName == "Capacity") 44319b8e9a0SWilly Tu { 44419b8e9a0SWilly Tu const uint64_t* capacity = 44519b8e9a0SWilly Tu std::get_if<uint64_t>(&property.second); 44619b8e9a0SWilly Tu if (capacity == nullptr) 44719b8e9a0SWilly Tu { 44819b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Capacity"; 44919b8e9a0SWilly Tu messages::internalError(asyncResp->res); 45019b8e9a0SWilly Tu return; 45119b8e9a0SWilly Tu } 45219b8e9a0SWilly Tu if (*capacity == 0) 45319b8e9a0SWilly Tu { 45419b8e9a0SWilly Tu // drive capacity not known 45519b8e9a0SWilly Tu continue; 45619b8e9a0SWilly Tu } 45719b8e9a0SWilly Tu 45819b8e9a0SWilly Tu asyncResp->res.jsonValue["CapacityBytes"] = *capacity; 45919b8e9a0SWilly Tu } 46019b8e9a0SWilly Tu else if (propertyName == "Protocol") 46119b8e9a0SWilly Tu { 46219b8e9a0SWilly Tu const std::string* value = 46319b8e9a0SWilly Tu std::get_if<std::string>(&property.second); 46419b8e9a0SWilly Tu if (value == nullptr) 46519b8e9a0SWilly Tu { 46619b8e9a0SWilly Tu BMCWEB_LOG_ERROR << "Illegal property: Protocol"; 46719b8e9a0SWilly Tu messages::internalError(asyncResp->res); 46819b8e9a0SWilly Tu return; 46919b8e9a0SWilly Tu } 47019b8e9a0SWilly Tu 47119b8e9a0SWilly Tu std::optional<std::string> proto = 47219b8e9a0SWilly Tu convertDriveProtocol(*value); 47319b8e9a0SWilly Tu if (!proto) 47419b8e9a0SWilly Tu { 47519b8e9a0SWilly Tu BMCWEB_LOG_ERROR 47619b8e9a0SWilly Tu << "Unsupported DrivePrototype Interface: " 47719b8e9a0SWilly Tu << *value; 47819b8e9a0SWilly Tu messages::internalError(asyncResp->res); 47919b8e9a0SWilly Tu return; 48019b8e9a0SWilly Tu } 48119b8e9a0SWilly Tu asyncResp->res.jsonValue["Protocol"] = *proto; 48219b8e9a0SWilly Tu } 48319b8e9a0SWilly Tu } 48419b8e9a0SWilly Tu }); 48519b8e9a0SWilly Tu } 48619b8e9a0SWilly Tu 4877e860f15SJohn Edward Broadbent inline void requestRoutesDrive(App& app) 4887e860f15SJohn Edward Broadbent { 4897e860f15SJohn Edward Broadbent BMCWEB_ROUTE(app, "/redfish/v1/Systems/system/Storage/1/Drives/<str>/") 490ed398213SEd Tanous .privileges(redfish::privileges::getDrive) 4917e860f15SJohn Edward Broadbent .methods( 49245ca1b86SEd Tanous boost::beast::http::verb:: 49345ca1b86SEd Tanous get)([&app](const crow::Request& req, 49445ca1b86SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4957e860f15SJohn Edward Broadbent const std::string& driveId) { 49645ca1b86SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp->res)) 49745ca1b86SEd Tanous { 49845ca1b86SEd Tanous return; 49945ca1b86SEd Tanous } 5007e860f15SJohn Edward Broadbent crow::connections::systemBus->async_method_call( 501b9d36b47SEd Tanous [asyncResp, driveId]( 502b9d36b47SEd Tanous const boost::system::error_code ec, 503b9d36b47SEd Tanous const dbus::utility::MapperGetSubTreeResponse& subtree) { 5047e860f15SJohn Edward Broadbent if (ec) 5057e860f15SJohn Edward Broadbent { 5067e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Drive mapper call error"; 5077e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 5087e860f15SJohn Edward Broadbent return; 5097e860f15SJohn Edward Broadbent } 5107e860f15SJohn Edward Broadbent 51103913171SWilly Tu auto drive = std::find_if( 5127e860f15SJohn Edward Broadbent subtree.begin(), subtree.end(), 51303913171SWilly Tu [&driveId](const std::pair< 51403913171SWilly Tu std::string, 51503913171SWilly Tu std::vector<std::pair< 51603913171SWilly Tu std::string, std::vector<std::string>>>>& 51703913171SWilly Tu object) { 51803913171SWilly Tu return sdbusplus::message::object_path(object.first) 51903913171SWilly Tu .filename() == driveId; 5207e860f15SJohn Edward Broadbent }); 5217e860f15SJohn Edward Broadbent 52203913171SWilly Tu if (drive == subtree.end()) 5237e860f15SJohn Edward Broadbent { 5247e860f15SJohn Edward Broadbent messages::resourceNotFound(asyncResp->res, "Drive", 5257e860f15SJohn Edward Broadbent driveId); 5267e860f15SJohn Edward Broadbent return; 5277e860f15SJohn Edward Broadbent } 5287e860f15SJohn Edward Broadbent 52903913171SWilly Tu const std::string& path = drive->first; 5307e860f15SJohn Edward Broadbent const std::vector< 5317e860f15SJohn Edward Broadbent std::pair<std::string, std::vector<std::string>>>& 53203913171SWilly Tu connectionNames = drive->second; 5337e860f15SJohn Edward Broadbent 5347e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.type"] = 5357e860f15SJohn Edward Broadbent "#Drive.v1_7_0.Drive"; 5367e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["@odata.id"] = 5377e860f15SJohn Edward Broadbent "/redfish/v1/Systems/system/Storage/1/Drives/" + 5387e860f15SJohn Edward Broadbent driveId; 5397e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Name"] = driveId; 5407e860f15SJohn Edward Broadbent asyncResp->res.jsonValue["Id"] = driveId; 5417e860f15SJohn Edward Broadbent 5427e860f15SJohn Edward Broadbent if (connectionNames.size() != 1) 5437e860f15SJohn Edward Broadbent { 5447e860f15SJohn Edward Broadbent BMCWEB_LOG_ERROR << "Connection size " 5457e860f15SJohn Edward Broadbent << connectionNames.size() 54603913171SWilly Tu << ", not equal to 1"; 5477e860f15SJohn Edward Broadbent messages::internalError(asyncResp->res); 5487e860f15SJohn Edward Broadbent return; 5497e860f15SJohn Edward Broadbent } 5507e860f15SJohn Edward Broadbent 5517e860f15SJohn Edward Broadbent getMainChassisId( 5527e860f15SJohn Edward Broadbent asyncResp, 5537e860f15SJohn Edward Broadbent [](const std::string& chassisId, 5547e860f15SJohn Edward Broadbent const std::shared_ptr<bmcweb::AsyncResp>& aRsp) { 555*1476687dSEd Tanous aRsp->res 556*1476687dSEd Tanous .jsonValue["Links"]["Chassis"]["@odata.id"] = 557*1476687dSEd Tanous "/redfish/v1/Chassis/" + chassisId; 5587e860f15SJohn Edward Broadbent }); 5597e860f15SJohn Edward Broadbent 560a25aeccfSNikhil Potade // default it to Enabled 561a25aeccfSNikhil Potade asyncResp->res.jsonValue["Status"]["State"] = "Enabled"; 562a25aeccfSNikhil Potade 5632ad9c2f6SJames Feist auto health = std::make_shared<HealthPopulate>(asyncResp); 564e284a7c1SJames Feist health->inventory.emplace_back(path); 5652ad9c2f6SJames Feist health->populate(); 5662ad9c2f6SJames Feist 56703913171SWilly Tu const std::string& connectionName = 56803913171SWilly Tu connectionNames[0].first; 56922984074SJames Feist 57061b83d0cSWilly Tu for (const std::string& interface : 57161b83d0cSWilly Tu connectionNames[0].second) 57261b83d0cSWilly Tu { 57361b83d0cSWilly Tu if (interface == 57461b83d0cSWilly Tu "xyz.openbmc_project.Inventory.Decorator.Asset") 57561b83d0cSWilly Tu { 57603913171SWilly Tu getDriveAsset(asyncResp, connectionName, path); 57761b83d0cSWilly Tu } 57861b83d0cSWilly Tu else if (interface == 57961b83d0cSWilly Tu "xyz.openbmc_project.Inventory.Item") 58061b83d0cSWilly Tu { 58103913171SWilly Tu getDrivePresent(asyncResp, connectionName, path); 58261b83d0cSWilly Tu } 58361b83d0cSWilly Tu else if (interface == "xyz.openbmc_project.State.Drive") 58461b83d0cSWilly Tu { 58503913171SWilly Tu getDriveState(asyncResp, connectionName, path); 58661b83d0cSWilly Tu } 58761b83d0cSWilly Tu else if (interface == 58861b83d0cSWilly Tu "xyz.openbmc_project.Inventory.Item.Drive") 58961b83d0cSWilly Tu { 59061b83d0cSWilly Tu getDriveItemProperties(asyncResp, connectionName, 59161b83d0cSWilly Tu path); 59261b83d0cSWilly Tu } 59361b83d0cSWilly Tu } 594a25aeccfSNikhil Potade }, 595a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", 596a25aeccfSNikhil Potade "/xyz/openbmc_project/object_mapper", 597a25aeccfSNikhil Potade "xyz.openbmc_project.ObjectMapper", "GetSubTree", 598a25aeccfSNikhil Potade "/xyz/openbmc_project/inventory", int32_t(0), 599a25aeccfSNikhil Potade std::array<const char*, 1>{ 600a25aeccfSNikhil Potade "xyz.openbmc_project.Inventory.Item.Drive"}); 6017e860f15SJohn Edward Broadbent }); 602a25aeccfSNikhil Potade } 603a25aeccfSNikhil Potade } // namespace redfish 604