Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2018 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | #pragma once |
| 17 | |
| 18 | #include "node.hpp" |
| 19 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string/replace.hpp> |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 21 | #include <boost/date_time.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 22 | #include <dbus_utility.hpp> |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 23 | #include <sstream> |
Bernard Wong | 7bffdb7 | 2019-03-20 16:17:21 +0800 | [diff] [blame] | 24 | #include <utils/systemd_utils.hpp> |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 25 | #include <variant> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace redfish |
| 28 | { |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * ManagerActionsReset class supports handle POST method for Reset action. |
| 32 | * The class retrieves and sends data directly to dbus. |
| 33 | */ |
| 34 | class ManagerActionsReset : public Node |
| 35 | { |
| 36 | public: |
| 37 | ManagerActionsReset(CrowApp& app) : |
| 38 | Node(app, "/redfish/v1/Managers/bmc/Actions/Manager.Reset/") |
| 39 | { |
| 40 | entityPrivileges = { |
| 41 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 42 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 43 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 44 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 45 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 46 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 47 | } |
| 48 | |
| 49 | private: |
| 50 | /** |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 51 | * Function handles POST method request. |
| 52 | * Analyzes POST body message before sends Reset request data to dbus. |
| 53 | * OpenBMC allows for ResetType is GracefulRestart only. |
| 54 | */ |
| 55 | void doPost(crow::Response& res, const crow::Request& req, |
| 56 | const std::vector<std::string>& params) override |
| 57 | { |
| 58 | std::string resetType; |
| 59 | |
| 60 | if (!json_util::readJson(req, res, "ResetType", resetType)) |
| 61 | { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (resetType != "GracefulRestart") |
| 66 | { |
| 67 | res.result(boost::beast::http::status::bad_request); |
| 68 | messages::actionParameterNotSupported(res, resetType, "ResetType"); |
| 69 | BMCWEB_LOG_ERROR << "Request incorrect action parameter: " |
| 70 | << resetType; |
| 71 | res.end(); |
| 72 | return; |
| 73 | } |
| 74 | doBMCGracefulRestart(res, req, params); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Function transceives data with dbus directly. |
| 79 | * All BMC state properties will be retrieved before sending reset request. |
| 80 | */ |
| 81 | void doBMCGracefulRestart(crow::Response& res, const crow::Request& req, |
| 82 | const std::vector<std::string>& params) |
| 83 | { |
| 84 | const char* processName = "xyz.openbmc_project.State.BMC"; |
| 85 | const char* objectPath = "/xyz/openbmc_project/state/bmc0"; |
| 86 | const char* interfaceName = "xyz.openbmc_project.State.BMC"; |
| 87 | const std::string& propertyValue = |
| 88 | "xyz.openbmc_project.State.BMC.Transition.Reboot"; |
| 89 | const char* destProperty = "RequestedBMCTransition"; |
| 90 | |
| 91 | // Create the D-Bus variant for D-Bus call. |
| 92 | VariantType dbusPropertyValue(propertyValue); |
| 93 | |
| 94 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
| 95 | |
| 96 | crow::connections::systemBus->async_method_call( |
| 97 | [asyncResp](const boost::system::error_code ec) { |
| 98 | // Use "Set" method to set the property value. |
| 99 | if (ec) |
| 100 | { |
| 101 | BMCWEB_LOG_ERROR << "[Set] Bad D-Bus request error: " << ec; |
| 102 | messages::internalError(asyncResp->res); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | messages::success(asyncResp->res); |
| 107 | }, |
| 108 | processName, objectPath, "org.freedesktop.DBus.Properties", "Set", |
| 109 | interfaceName, destProperty, dbusPropertyValue); |
| 110 | } |
| 111 | }; |
| 112 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 113 | static constexpr const char* objectManagerIface = |
| 114 | "org.freedesktop.DBus.ObjectManager"; |
| 115 | static constexpr const char* pidConfigurationIface = |
| 116 | "xyz.openbmc_project.Configuration.Pid"; |
| 117 | static constexpr const char* pidZoneConfigurationIface = |
| 118 | "xyz.openbmc_project.Configuration.Pid.Zone"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 119 | static constexpr const char* stepwiseConfigurationIface = |
| 120 | "xyz.openbmc_project.Configuration.Stepwise"; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 121 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 122 | static void asyncPopulatePid(const std::string& connection, |
| 123 | const std::string& path, |
| 124 | std::shared_ptr<AsyncResp> asyncResp) |
| 125 | { |
| 126 | |
| 127 | crow::connections::systemBus->async_method_call( |
| 128 | [asyncResp](const boost::system::error_code ec, |
| 129 | const dbus::utility::ManagedObjectType& managedObj) { |
| 130 | if (ec) |
| 131 | { |
| 132 | BMCWEB_LOG_ERROR << ec; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 133 | asyncResp->res.jsonValue.clear(); |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 134 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 135 | return; |
| 136 | } |
| 137 | nlohmann::json& configRoot = |
| 138 | asyncResp->res.jsonValue["Oem"]["OpenBmc"]["Fan"]; |
| 139 | nlohmann::json& fans = configRoot["FanControllers"]; |
| 140 | fans["@odata.type"] = "#OemManager.FanControllers"; |
| 141 | fans["@odata.context"] = |
| 142 | "/redfish/v1/$metadata#OemManager.FanControllers"; |
| 143 | fans["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc/" |
| 144 | "Fan/FanControllers"; |
| 145 | |
| 146 | nlohmann::json& pids = configRoot["PidControllers"]; |
| 147 | pids["@odata.type"] = "#OemManager.PidControllers"; |
| 148 | pids["@odata.context"] = |
| 149 | "/redfish/v1/$metadata#OemManager.PidControllers"; |
| 150 | pids["@odata.id"] = |
| 151 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/PidControllers"; |
| 152 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 153 | nlohmann::json& stepwise = configRoot["StepwiseControllers"]; |
| 154 | stepwise["@odata.type"] = "#OemManager.StepwiseControllers"; |
| 155 | stepwise["@odata.context"] = |
| 156 | "/redfish/v1/$metadata#OemManager.StepwiseControllers"; |
| 157 | stepwise["@odata.id"] = |
| 158 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/StepwiseControllers"; |
| 159 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 160 | nlohmann::json& zones = configRoot["FanZones"]; |
| 161 | zones["@odata.id"] = |
| 162 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones"; |
| 163 | zones["@odata.type"] = "#OemManager.FanZones"; |
| 164 | zones["@odata.context"] = |
| 165 | "/redfish/v1/$metadata#OemManager.FanZones"; |
| 166 | configRoot["@odata.id"] = |
| 167 | "/redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan"; |
| 168 | configRoot["@odata.type"] = "#OemManager.Fan"; |
| 169 | configRoot["@odata.context"] = |
| 170 | "/redfish/v1/$metadata#OemManager.Fan"; |
| 171 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 172 | for (const auto& pathPair : managedObj) |
| 173 | { |
| 174 | for (const auto& intfPair : pathPair.second) |
| 175 | { |
| 176 | if (intfPair.first != pidConfigurationIface && |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 177 | intfPair.first != pidZoneConfigurationIface && |
| 178 | intfPair.first != stepwiseConfigurationIface) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 179 | { |
| 180 | continue; |
| 181 | } |
| 182 | auto findName = intfPair.second.find("Name"); |
| 183 | if (findName == intfPair.second.end()) |
| 184 | { |
| 185 | BMCWEB_LOG_ERROR << "Pid Field missing Name"; |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 186 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 187 | return; |
| 188 | } |
| 189 | const std::string* namePtr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 190 | std::get_if<std::string>(&findName->second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 191 | if (namePtr == nullptr) |
| 192 | { |
| 193 | BMCWEB_LOG_ERROR << "Pid Name Field illegal"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 194 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 195 | return; |
| 196 | } |
| 197 | |
| 198 | std::string name = *namePtr; |
| 199 | dbus::utility::escapePathForDbus(name); |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 200 | nlohmann::json* config = nullptr; |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 201 | |
| 202 | const std::string* classPtr = nullptr; |
| 203 | auto findClass = intfPair.second.find("Class"); |
| 204 | if (findClass != intfPair.second.end()) |
| 205 | { |
| 206 | classPtr = std::get_if<std::string>(&findClass->second); |
| 207 | } |
| 208 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 209 | if (intfPair.first == pidZoneConfigurationIface) |
| 210 | { |
| 211 | std::string chassis; |
| 212 | if (!dbus::utility::getNthStringFromPath( |
| 213 | pathPair.first.str, 5, chassis)) |
| 214 | { |
| 215 | chassis = "#IllegalValue"; |
| 216 | } |
| 217 | nlohmann::json& zone = zones[name]; |
| 218 | zone["Chassis"] = { |
| 219 | {"@odata.id", "/redfish/v1/Chassis/" + chassis}}; |
| 220 | zone["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/" |
| 221 | "OpenBmc/Fan/FanZones/" + |
| 222 | name; |
| 223 | zone["@odata.type"] = "#OemManager.FanZone"; |
| 224 | zone["@odata.context"] = |
| 225 | "/redfish/v1/$metadata#OemManager.FanZone"; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 226 | config = &zone; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 227 | } |
| 228 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 229 | else if (intfPair.first == stepwiseConfigurationIface) |
| 230 | { |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 231 | if (classPtr == nullptr) |
| 232 | { |
| 233 | BMCWEB_LOG_ERROR << "Pid Class Field illegal"; |
| 234 | messages::internalError(asyncResp->res); |
| 235 | return; |
| 236 | } |
| 237 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 238 | nlohmann::json& controller = stepwise[name]; |
| 239 | config = &controller; |
| 240 | |
| 241 | controller["@odata.id"] = |
| 242 | "/redfish/v1/Managers/bmc#/Oem/" |
| 243 | "OpenBmc/Fan/StepwiseControllers/" + |
| 244 | std::string(name); |
| 245 | controller["@odata.type"] = |
| 246 | "#OemManager.StepwiseController"; |
| 247 | |
| 248 | controller["@odata.context"] = |
| 249 | "/redfish/v1/" |
| 250 | "$metadata#OemManager.StepwiseController"; |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 251 | controller["Direction"] = *classPtr; |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | // pid and fans are off the same configuration |
| 255 | else if (intfPair.first == pidConfigurationIface) |
| 256 | { |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 257 | |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 258 | if (classPtr == nullptr) |
| 259 | { |
| 260 | BMCWEB_LOG_ERROR << "Pid Class Field illegal"; |
| 261 | messages::internalError(asyncResp->res); |
| 262 | return; |
| 263 | } |
| 264 | bool isFan = *classPtr == "fan"; |
| 265 | nlohmann::json& element = |
| 266 | isFan ? fans[name] : pids[name]; |
| 267 | config = &element; |
| 268 | if (isFan) |
| 269 | { |
| 270 | element["@odata.id"] = |
| 271 | "/redfish/v1/Managers/bmc#/Oem/" |
| 272 | "OpenBmc/Fan/FanControllers/" + |
| 273 | std::string(name); |
| 274 | element["@odata.type"] = |
| 275 | "#OemManager.FanController"; |
| 276 | |
| 277 | element["@odata.context"] = |
| 278 | "/redfish/v1/" |
| 279 | "$metadata#OemManager.FanController"; |
| 280 | } |
| 281 | else |
| 282 | { |
| 283 | element["@odata.id"] = |
| 284 | "/redfish/v1/Managers/bmc#/Oem/" |
| 285 | "OpenBmc/Fan/PidControllers/" + |
| 286 | std::string(name); |
| 287 | element["@odata.type"] = |
| 288 | "#OemManager.PidController"; |
| 289 | element["@odata.context"] = |
| 290 | "/redfish/v1/$metadata" |
| 291 | "#OemManager.PidController"; |
| 292 | } |
| 293 | } |
| 294 | else |
| 295 | { |
| 296 | BMCWEB_LOG_ERROR << "Unexpected configuration"; |
| 297 | messages::internalError(asyncResp->res); |
| 298 | return; |
| 299 | } |
| 300 | |
| 301 | // used for making maps out of 2 vectors |
| 302 | const std::vector<double>* keys = nullptr; |
| 303 | const std::vector<double>* values = nullptr; |
| 304 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 305 | for (const auto& propertyPair : intfPair.second) |
| 306 | { |
| 307 | if (propertyPair.first == "Type" || |
| 308 | propertyPair.first == "Class" || |
| 309 | propertyPair.first == "Name") |
| 310 | { |
| 311 | continue; |
| 312 | } |
| 313 | |
| 314 | // zones |
| 315 | if (intfPair.first == pidZoneConfigurationIface) |
| 316 | { |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 317 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 318 | std::get_if<double>(&propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 319 | if (ptr == nullptr) |
| 320 | { |
| 321 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 322 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 323 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 324 | return; |
| 325 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 326 | (*config)[propertyPair.first] = *ptr; |
| 327 | } |
| 328 | |
| 329 | if (intfPair.first == stepwiseConfigurationIface) |
| 330 | { |
| 331 | if (propertyPair.first == "Reading" || |
| 332 | propertyPair.first == "Output") |
| 333 | { |
| 334 | const std::vector<double>* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 335 | std::get_if<std::vector<double>>( |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 336 | &propertyPair.second); |
| 337 | |
| 338 | if (ptr == nullptr) |
| 339 | { |
| 340 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 341 | << propertyPair.first; |
| 342 | messages::internalError(asyncResp->res); |
| 343 | return; |
| 344 | } |
| 345 | |
| 346 | if (propertyPair.first == "Reading") |
| 347 | { |
| 348 | keys = ptr; |
| 349 | } |
| 350 | else |
| 351 | { |
| 352 | values = ptr; |
| 353 | } |
| 354 | if (keys && values) |
| 355 | { |
| 356 | if (keys->size() != values->size()) |
| 357 | { |
| 358 | BMCWEB_LOG_ERROR |
| 359 | << "Reading and Output size don't " |
| 360 | "match "; |
| 361 | messages::internalError(asyncResp->res); |
| 362 | return; |
| 363 | } |
| 364 | nlohmann::json& steps = (*config)["Steps"]; |
| 365 | steps = nlohmann::json::array(); |
| 366 | for (size_t ii = 0; ii < keys->size(); ii++) |
| 367 | { |
| 368 | steps.push_back( |
| 369 | {{"Target", (*keys)[ii]}, |
| 370 | {"Output", (*values)[ii]}}); |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | if (propertyPair.first == "NegativeHysteresis" || |
| 375 | propertyPair.first == "PositiveHysteresis") |
| 376 | { |
| 377 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 378 | std::get_if<double>(&propertyPair.second); |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 379 | if (ptr == nullptr) |
| 380 | { |
| 381 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 382 | << propertyPair.first; |
| 383 | messages::internalError(asyncResp->res); |
| 384 | return; |
| 385 | } |
| 386 | (*config)[propertyPair.first] = *ptr; |
| 387 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | // pid and fans are off the same configuration |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 391 | if (intfPair.first == pidConfigurationIface || |
| 392 | intfPair.first == stepwiseConfigurationIface) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 393 | { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 394 | |
| 395 | if (propertyPair.first == "Zones") |
| 396 | { |
| 397 | const std::vector<std::string>* inputs = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 398 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 399 | &propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 400 | |
| 401 | if (inputs == nullptr) |
| 402 | { |
| 403 | BMCWEB_LOG_ERROR |
| 404 | << "Zones Pid Field Illegal"; |
Jason M. Bills | a08b46c | 2018-11-06 15:01:08 -0800 | [diff] [blame] | 405 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 406 | return; |
| 407 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 408 | auto& data = (*config)[propertyPair.first]; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 409 | data = nlohmann::json::array(); |
| 410 | for (std::string itemCopy : *inputs) |
| 411 | { |
| 412 | dbus::utility::escapePathForDbus(itemCopy); |
| 413 | data.push_back( |
| 414 | {{"@odata.id", |
| 415 | "/redfish/v1/Managers/bmc#/Oem/" |
| 416 | "OpenBmc/Fan/FanZones/" + |
| 417 | itemCopy}}); |
| 418 | } |
| 419 | } |
| 420 | // todo(james): may never happen, but this |
| 421 | // assumes configuration data referenced in the |
| 422 | // PID config is provided by the same daemon, we |
| 423 | // could add another loop to cover all cases, |
| 424 | // but I'm okay kicking this can down the road a |
| 425 | // bit |
| 426 | |
| 427 | else if (propertyPair.first == "Inputs" || |
| 428 | propertyPair.first == "Outputs") |
| 429 | { |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 430 | auto& data = (*config)[propertyPair.first]; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 431 | const std::vector<std::string>* inputs = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 432 | std::get_if<std::vector<std::string>>( |
Ed Tanous | 1b6b96c | 2018-11-30 11:35:41 -0800 | [diff] [blame] | 433 | &propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 434 | |
| 435 | if (inputs == nullptr) |
| 436 | { |
| 437 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 438 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 439 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 440 | return; |
| 441 | } |
| 442 | data = *inputs; |
| 443 | } // doubles |
| 444 | else if (propertyPair.first == |
| 445 | "FFGainCoefficient" || |
| 446 | propertyPair.first == "FFOffCoefficient" || |
| 447 | propertyPair.first == "ICoefficient" || |
| 448 | propertyPair.first == "ILimitMax" || |
| 449 | propertyPair.first == "ILimitMin" || |
James Feist | aad1a25 | 2019-02-19 10:13:52 -0800 | [diff] [blame] | 450 | propertyPair.first == |
| 451 | "PositiveHysteresis" || |
| 452 | propertyPair.first == |
| 453 | "NegativeHysteresis" || |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 454 | propertyPair.first == "OutLimitMax" || |
| 455 | propertyPair.first == "OutLimitMin" || |
| 456 | propertyPair.first == "PCoefficient" || |
James Feist | 7625cb8 | 2019-01-23 11:58:21 -0800 | [diff] [blame] | 457 | propertyPair.first == "SetPoint" || |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 458 | propertyPair.first == "SlewNeg" || |
| 459 | propertyPair.first == "SlewPos") |
| 460 | { |
| 461 | const double* ptr = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 462 | std::get_if<double>(&propertyPair.second); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 463 | if (ptr == nullptr) |
| 464 | { |
| 465 | BMCWEB_LOG_ERROR << "Field Illegal " |
| 466 | << propertyPair.first; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 467 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 468 | return; |
| 469 | } |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 470 | (*config)[propertyPair.first] = *ptr; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | } |
| 476 | }, |
| 477 | connection, path, objectManagerIface, "GetManagedObjects"); |
| 478 | } |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 479 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 480 | enum class CreatePIDRet |
| 481 | { |
| 482 | fail, |
| 483 | del, |
| 484 | patch |
| 485 | }; |
| 486 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 487 | static bool getZonesFromJsonReq(const std::shared_ptr<AsyncResp>& response, |
| 488 | std::vector<nlohmann::json>& config, |
| 489 | std::vector<std::string>& zones) |
| 490 | { |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 491 | if (config.empty()) |
| 492 | { |
| 493 | BMCWEB_LOG_ERROR << "Empty Zones"; |
| 494 | messages::propertyValueFormatError(response->res, |
| 495 | nlohmann::json::array(), "Zones"); |
| 496 | return false; |
| 497 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 498 | for (auto& odata : config) |
| 499 | { |
| 500 | std::string path; |
| 501 | if (!redfish::json_util::readJson(odata, response->res, "@odata.id", |
| 502 | path)) |
| 503 | { |
| 504 | return false; |
| 505 | } |
| 506 | std::string input; |
James Feist | 61adbda | 2019-03-25 13:03:51 -0700 | [diff] [blame] | 507 | |
| 508 | // 8 below comes from |
| 509 | // /redfish/v1/Managers/bmc#/Oem/OpenBmc/Fan/FanZones/Left |
| 510 | // 0 1 2 3 4 5 6 7 8 |
| 511 | if (!dbus::utility::getNthStringFromPath(path, 8, input)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 512 | { |
| 513 | BMCWEB_LOG_ERROR << "Got invalid path " << path; |
| 514 | BMCWEB_LOG_ERROR << "Illegal Type Zones"; |
| 515 | messages::propertyValueFormatError(response->res, odata.dump(), |
| 516 | "Zones"); |
| 517 | return false; |
| 518 | } |
| 519 | boost::replace_all(input, "_", " "); |
| 520 | zones.emplace_back(std::move(input)); |
| 521 | } |
| 522 | return true; |
| 523 | } |
| 524 | |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 525 | static bool findChassis(const dbus::utility::ManagedObjectType& managedObj, |
| 526 | const std::string& value, std::string& chassis) |
| 527 | { |
| 528 | BMCWEB_LOG_DEBUG << "Find Chassis: " << value << "\n"; |
| 529 | |
| 530 | std::string escaped = boost::replace_all_copy(value, " ", "_"); |
| 531 | escaped = "/" + escaped; |
| 532 | auto it = std::find_if( |
| 533 | managedObj.begin(), managedObj.end(), [&escaped](const auto& obj) { |
| 534 | if (boost::algorithm::ends_with(obj.first.str, escaped)) |
| 535 | { |
| 536 | BMCWEB_LOG_DEBUG << "Matched " << obj.first.str << "\n"; |
| 537 | return true; |
| 538 | } |
| 539 | return false; |
| 540 | }); |
| 541 | |
| 542 | if (it == managedObj.end()) |
| 543 | { |
| 544 | return false; |
| 545 | } |
| 546 | // 5 comes from <chassis-name> being the 5th element |
| 547 | // /xyz/openbmc_project/inventory/system/chassis/<chassis-name> |
| 548 | return dbus::utility::getNthStringFromPath(it->first.str, 5, chassis); |
| 549 | } |
| 550 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 551 | static CreatePIDRet createPidInterface( |
| 552 | const std::shared_ptr<AsyncResp>& response, const std::string& type, |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 553 | nlohmann::json::iterator it, const std::string& path, |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 554 | const dbus::utility::ManagedObjectType& managedObj, bool createNewObject, |
| 555 | boost::container::flat_map<std::string, dbus::utility::DbusVariantType>& |
| 556 | output, |
| 557 | std::string& chassis) |
| 558 | { |
| 559 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 560 | // common deleter |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 561 | if (it.value() == nullptr) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 562 | { |
| 563 | std::string iface; |
| 564 | if (type == "PidControllers" || type == "FanControllers") |
| 565 | { |
| 566 | iface = pidConfigurationIface; |
| 567 | } |
| 568 | else if (type == "FanZones") |
| 569 | { |
| 570 | iface = pidZoneConfigurationIface; |
| 571 | } |
| 572 | else if (type == "StepwiseControllers") |
| 573 | { |
| 574 | iface = stepwiseConfigurationIface; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " |
| 579 | << type; |
| 580 | messages::propertyUnknown(response->res, type); |
| 581 | return CreatePIDRet::fail; |
| 582 | } |
| 583 | // delete interface |
| 584 | crow::connections::systemBus->async_method_call( |
| 585 | [response, path](const boost::system::error_code ec) { |
| 586 | if (ec) |
| 587 | { |
| 588 | BMCWEB_LOG_ERROR << "Error patching " << path << ": " << ec; |
| 589 | messages::internalError(response->res); |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 590 | return; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 591 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 592 | messages::success(response->res); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 593 | }, |
| 594 | "xyz.openbmc_project.EntityManager", path, iface, "Delete"); |
| 595 | return CreatePIDRet::del; |
| 596 | } |
| 597 | |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 598 | if (!createNewObject) |
| 599 | { |
| 600 | // if we aren't creating a new object, we should be able to find it on |
| 601 | // d-bus |
| 602 | if (!findChassis(managedObj, it.key(), chassis)) |
| 603 | { |
| 604 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 605 | messages::invalidObject(response->res, it.key()); |
| 606 | return CreatePIDRet::fail; |
| 607 | } |
| 608 | } |
| 609 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 610 | if (type == "PidControllers" || type == "FanControllers") |
| 611 | { |
| 612 | if (createNewObject) |
| 613 | { |
| 614 | output["Class"] = type == "PidControllers" ? std::string("temp") |
| 615 | : std::string("fan"); |
| 616 | output["Type"] = std::string("Pid"); |
| 617 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 618 | |
| 619 | std::optional<std::vector<nlohmann::json>> zones; |
| 620 | std::optional<std::vector<std::string>> inputs; |
| 621 | std::optional<std::vector<std::string>> outputs; |
| 622 | std::map<std::string, std::optional<double>> doubles; |
| 623 | if (!redfish::json_util::readJson( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 624 | it.value(), response->res, "Inputs", inputs, "Outputs", outputs, |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 625 | "Zones", zones, "FFGainCoefficient", |
| 626 | doubles["FFGainCoefficient"], "FFOffCoefficient", |
| 627 | doubles["FFOffCoefficient"], "ICoefficient", |
| 628 | doubles["ICoefficient"], "ILimitMax", doubles["ILimitMax"], |
| 629 | "ILimitMin", doubles["ILimitMin"], "OutLimitMax", |
| 630 | doubles["OutLimitMax"], "OutLimitMin", doubles["OutLimitMin"], |
| 631 | "PCoefficient", doubles["PCoefficient"], "SetPoint", |
| 632 | doubles["SetPoint"], "SlewNeg", doubles["SlewNeg"], "SlewPos", |
James Feist | aad1a25 | 2019-02-19 10:13:52 -0800 | [diff] [blame] | 633 | doubles["SlewPos"], "PositiveHysteresis", |
| 634 | doubles["PositiveHysteresis"], "NegativeHysteresis", |
| 635 | doubles["NegativeHysteresis"])) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 636 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 637 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 638 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 639 | return CreatePIDRet::fail; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 640 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 641 | if (zones) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 642 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 643 | std::vector<std::string> zonesStr; |
| 644 | if (!getZonesFromJsonReq(response, *zones, zonesStr)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 645 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 646 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; |
| 647 | return CreatePIDRet::fail; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 648 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 649 | if (chassis.empty() && |
| 650 | !findChassis(managedObj, zonesStr[0], chassis)) |
| 651 | { |
| 652 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 653 | messages::invalidObject(response->res, it.key()); |
| 654 | return CreatePIDRet::fail; |
| 655 | } |
| 656 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 657 | output["Zones"] = std::move(zonesStr); |
| 658 | } |
| 659 | if (inputs || outputs) |
| 660 | { |
| 661 | std::array<std::optional<std::vector<std::string>>*, 2> containers = |
| 662 | {&inputs, &outputs}; |
| 663 | size_t index = 0; |
| 664 | for (const auto& containerPtr : containers) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 665 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 666 | std::optional<std::vector<std::string>>& container = |
| 667 | *containerPtr; |
| 668 | if (!container) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 669 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 670 | index++; |
| 671 | continue; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 672 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 673 | |
| 674 | for (std::string& value : *container) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 675 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 676 | boost::replace_all(value, "_", " "); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 677 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 678 | std::string key; |
| 679 | if (index == 0) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 680 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 681 | key = "Inputs"; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 682 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 683 | else |
| 684 | { |
| 685 | key = "Outputs"; |
| 686 | } |
| 687 | output[key] = *container; |
| 688 | index++; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 689 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 690 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 691 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 692 | // doubles |
| 693 | for (const auto& pairs : doubles) |
| 694 | { |
| 695 | if (!pairs.second) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 696 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 697 | continue; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 698 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 699 | BMCWEB_LOG_DEBUG << pairs.first << " = " << *pairs.second; |
| 700 | output[pairs.first] = *(pairs.second); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 701 | } |
| 702 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 703 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 704 | else if (type == "FanZones") |
| 705 | { |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 706 | output["Type"] = std::string("Pid.Zone"); |
| 707 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 708 | std::optional<nlohmann::json> chassisContainer; |
| 709 | std::optional<double> failSafePercent; |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 710 | std::optional<double> minThermalOutput; |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 711 | if (!redfish::json_util::readJson(it.value(), response->res, "Chassis", |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 712 | chassisContainer, "FailSafePercent", |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 713 | failSafePercent, "MinThermalOutput", |
| 714 | minThermalOutput)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 715 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 716 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 717 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 718 | return CreatePIDRet::fail; |
| 719 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 720 | |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 721 | if (chassisContainer) |
| 722 | { |
| 723 | |
| 724 | std::string chassisId; |
| 725 | if (!redfish::json_util::readJson(*chassisContainer, response->res, |
| 726 | "@odata.id", chassisId)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 727 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 728 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
| 729 | << chassisContainer->dump(); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 730 | return CreatePIDRet::fail; |
| 731 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 732 | |
| 733 | // /refish/v1/chassis/chassis_name/ |
| 734 | if (!dbus::utility::getNthStringFromPath(chassisId, 3, chassis)) |
| 735 | { |
| 736 | BMCWEB_LOG_ERROR << "Got invalid path " << chassisId; |
| 737 | messages::invalidObject(response->res, chassisId); |
| 738 | return CreatePIDRet::fail; |
| 739 | } |
| 740 | } |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 741 | if (minThermalOutput) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 742 | { |
James Feist | d3ec07f | 2019-02-25 14:51:15 -0800 | [diff] [blame] | 743 | output["MinThermalOutput"] = *minThermalOutput; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 744 | } |
| 745 | if (failSafePercent) |
| 746 | { |
| 747 | output["FailSafePercent"] = *failSafePercent; |
| 748 | } |
| 749 | } |
| 750 | else if (type == "StepwiseControllers") |
| 751 | { |
| 752 | output["Type"] = std::string("Stepwise"); |
| 753 | |
| 754 | std::optional<std::vector<nlohmann::json>> zones; |
| 755 | std::optional<std::vector<nlohmann::json>> steps; |
| 756 | std::optional<std::vector<std::string>> inputs; |
| 757 | std::optional<double> positiveHysteresis; |
| 758 | std::optional<double> negativeHysteresis; |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 759 | std::optional<std::string> direction; // upper clipping curve vs lower |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 760 | if (!redfish::json_util::readJson( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 761 | it.value(), response->res, "Zones", zones, "Steps", steps, |
| 762 | "Inputs", inputs, "PositiveHysteresis", positiveHysteresis, |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 763 | "NegativeHysteresis", negativeHysteresis, "Direction", |
| 764 | direction)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 765 | { |
| 766 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 767 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 768 | return CreatePIDRet::fail; |
| 769 | } |
| 770 | |
| 771 | if (zones) |
| 772 | { |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 773 | std::vector<std::string> zonesStrs; |
| 774 | if (!getZonesFromJsonReq(response, *zones, zonesStrs)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 775 | { |
| 776 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Zones"; |
| 777 | return CreatePIDRet::fail; |
| 778 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 779 | if (chassis.empty() && |
| 780 | !findChassis(managedObj, zonesStrs[0], chassis)) |
| 781 | { |
| 782 | BMCWEB_LOG_ERROR << "Failed to get chassis from config patch"; |
| 783 | messages::invalidObject(response->res, it.key()); |
| 784 | return CreatePIDRet::fail; |
| 785 | } |
| 786 | output["Zones"] = std::move(zonesStrs); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 787 | } |
| 788 | if (steps) |
| 789 | { |
| 790 | std::vector<double> readings; |
| 791 | std::vector<double> outputs; |
| 792 | for (auto& step : *steps) |
| 793 | { |
| 794 | double target; |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 795 | double output; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 796 | |
| 797 | if (!redfish::json_util::readJson(step, response->res, "Target", |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 798 | target, "Output", output)) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 799 | { |
| 800 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 801 | << ", Illegal Property " |
| 802 | << it.value().dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 803 | return CreatePIDRet::fail; |
| 804 | } |
| 805 | readings.emplace_back(target); |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 806 | outputs.emplace_back(output); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 807 | } |
| 808 | output["Reading"] = std::move(readings); |
| 809 | output["Output"] = std::move(outputs); |
| 810 | } |
| 811 | if (inputs) |
| 812 | { |
| 813 | for (std::string& value : *inputs) |
| 814 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 815 | boost::replace_all(value, "_", " "); |
| 816 | } |
| 817 | output["Inputs"] = std::move(*inputs); |
| 818 | } |
| 819 | if (negativeHysteresis) |
| 820 | { |
| 821 | output["NegativeHysteresis"] = *negativeHysteresis; |
| 822 | } |
| 823 | if (positiveHysteresis) |
| 824 | { |
| 825 | output["PositiveHysteresis"] = *positiveHysteresis; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 826 | } |
James Feist | c33a90e | 2019-03-01 10:17:44 -0800 | [diff] [blame] | 827 | if (direction) |
| 828 | { |
| 829 | constexpr const std::array<const char*, 2> allowedDirections = { |
| 830 | "Ceiling", "Floor"}; |
| 831 | if (std::find(allowedDirections.begin(), allowedDirections.end(), |
| 832 | *direction) == allowedDirections.end()) |
| 833 | { |
| 834 | messages::propertyValueTypeError(response->res, "Direction", |
| 835 | *direction); |
| 836 | return CreatePIDRet::fail; |
| 837 | } |
| 838 | output["Class"] = *direction; |
| 839 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 840 | } |
| 841 | else |
| 842 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 843 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Type " << type; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 844 | messages::propertyUnknown(response->res, type); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 845 | return CreatePIDRet::fail; |
| 846 | } |
| 847 | return CreatePIDRet::patch; |
| 848 | } |
| 849 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 850 | class Manager : public Node |
| 851 | { |
| 852 | public: |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 853 | Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/bmc/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 854 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 855 | uuid = app.template getMiddleware<crow::persistent_data::Middleware>() |
| 856 | .systemUuid; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 857 | entityPrivileges = { |
| 858 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 859 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 860 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 861 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 862 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 863 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 864 | } |
| 865 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 866 | private: |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 867 | void getPidValues(std::shared_ptr<AsyncResp> asyncResp) |
| 868 | { |
| 869 | crow::connections::systemBus->async_method_call( |
| 870 | [asyncResp](const boost::system::error_code ec, |
| 871 | const crow::openbmc_mapper::GetSubTreeType& subtree) { |
| 872 | if (ec) |
| 873 | { |
| 874 | BMCWEB_LOG_ERROR << ec; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 875 | messages::internalError(asyncResp->res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 876 | return; |
| 877 | } |
| 878 | |
| 879 | // create map of <connection, path to objMgr>> |
| 880 | boost::container::flat_map<std::string, std::string> |
| 881 | objectMgrPaths; |
James Feist | 6bce33b | 2018-10-22 12:05:56 -0700 | [diff] [blame] | 882 | boost::container::flat_set<std::string> calledConnections; |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 883 | for (const auto& pathGroup : subtree) |
| 884 | { |
| 885 | for (const auto& connectionGroup : pathGroup.second) |
| 886 | { |
James Feist | 6bce33b | 2018-10-22 12:05:56 -0700 | [diff] [blame] | 887 | auto findConnection = |
| 888 | calledConnections.find(connectionGroup.first); |
| 889 | if (findConnection != calledConnections.end()) |
| 890 | { |
| 891 | break; |
| 892 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 893 | for (const std::string& interface : |
| 894 | connectionGroup.second) |
| 895 | { |
| 896 | if (interface == objectManagerIface) |
| 897 | { |
| 898 | objectMgrPaths[connectionGroup.first] = |
| 899 | pathGroup.first; |
| 900 | } |
| 901 | // this list is alphabetical, so we |
| 902 | // should have found the objMgr by now |
| 903 | if (interface == pidConfigurationIface || |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 904 | interface == pidZoneConfigurationIface || |
| 905 | interface == stepwiseConfigurationIface) |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 906 | { |
| 907 | auto findObjMgr = |
| 908 | objectMgrPaths.find(connectionGroup.first); |
| 909 | if (findObjMgr == objectMgrPaths.end()) |
| 910 | { |
| 911 | BMCWEB_LOG_DEBUG << connectionGroup.first |
| 912 | << "Has no Object Manager"; |
| 913 | continue; |
| 914 | } |
James Feist | 6bce33b | 2018-10-22 12:05:56 -0700 | [diff] [blame] | 915 | |
| 916 | calledConnections.insert(connectionGroup.first); |
| 917 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 918 | asyncPopulatePid(findObjMgr->first, |
| 919 | findObjMgr->second, asyncResp); |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | } |
| 924 | } |
| 925 | }, |
| 926 | "xyz.openbmc_project.ObjectMapper", |
| 927 | "/xyz/openbmc_project/object_mapper", |
| 928 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0, |
James Feist | b7a08d0 | 2018-12-11 14:55:37 -0800 | [diff] [blame] | 929 | std::array<const char*, 4>{ |
| 930 | pidConfigurationIface, pidZoneConfigurationIface, |
| 931 | objectManagerIface, stepwiseConfigurationIface}); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | void doGet(crow::Response& res, const crow::Request& req, |
| 935 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 936 | { |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 937 | res.jsonValue["@odata.id"] = "/redfish/v1/Managers/bmc"; |
| 938 | res.jsonValue["@odata.type"] = "#Manager.v1_3_0.Manager"; |
| 939 | res.jsonValue["@odata.context"] = |
| 940 | "/redfish/v1/$metadata#Manager.Manager"; |
| 941 | res.jsonValue["Id"] = "bmc"; |
| 942 | res.jsonValue["Name"] = "OpenBmc Manager"; |
| 943 | res.jsonValue["Description"] = "Baseboard Management Controller"; |
| 944 | res.jsonValue["PowerState"] = "On"; |
Ed Tanous | 029573d | 2019-02-01 10:57:49 -0800 | [diff] [blame] | 945 | res.jsonValue["Status"] = {{"State", "Enabled"}, {"Health", "OK"}}; |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 946 | res.jsonValue["ManagerType"] = "BMC"; |
Ed Tanous | 7517658 | 2018-12-14 08:14:34 -0800 | [diff] [blame] | 947 | res.jsonValue["UUID"] = uuid; |
Bernard Wong | 7bffdb7 | 2019-03-20 16:17:21 +0800 | [diff] [blame] | 948 | res.jsonValue["ServiceEntryPointUUID"] = systemd_utils::getUuid(); |
Ed Tanous | 7517658 | 2018-12-14 08:14:34 -0800 | [diff] [blame] | 949 | res.jsonValue["Model"] = "OpenBmc"; // TODO(ed), get model |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 950 | |
| 951 | res.jsonValue["LogServices"] = { |
| 952 | {"@odata.id", "/redfish/v1/Managers/bmc/LogServices"}}; |
| 953 | |
| 954 | res.jsonValue["NetworkProtocol"] = { |
| 955 | {"@odata.id", "/redfish/v1/Managers/bmc/NetworkProtocol"}}; |
| 956 | |
| 957 | res.jsonValue["EthernetInterfaces"] = { |
| 958 | {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces"}}; |
| 959 | // default oem data |
| 960 | nlohmann::json& oem = res.jsonValue["Oem"]; |
| 961 | nlohmann::json& oemOpenbmc = oem["OpenBmc"]; |
| 962 | oem["@odata.type"] = "#OemManager.Oem"; |
| 963 | oem["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem"; |
| 964 | oem["@odata.context"] = "/redfish/v1/$metadata#OemManager.Oem"; |
| 965 | oemOpenbmc["@odata.type"] = "#OemManager.OpenBmc"; |
| 966 | oemOpenbmc["@odata.id"] = "/redfish/v1/Managers/bmc#/Oem/OpenBmc"; |
| 967 | oemOpenbmc["@odata.context"] = |
| 968 | "/redfish/v1/$metadata#OemManager.OpenBmc"; |
| 969 | |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 970 | // Update Actions object. |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 971 | nlohmann::json& manager_reset = |
| 972 | res.jsonValue["Actions"]["#Manager.Reset"]; |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 973 | manager_reset["target"] = |
| 974 | "/redfish/v1/Managers/bmc/Actions/Manager.Reset"; |
| 975 | manager_reset["ResetType@Redfish.AllowableValues"] = { |
| 976 | "GracefulRestart"}; |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 977 | |
Andrew Geissler | cb92c03 | 2018-08-17 07:56:14 -0700 | [diff] [blame] | 978 | res.jsonValue["DateTime"] = crow::utility::dateTimeNow(); |
Santosh Puranik | 474bfad | 2019-04-02 16:00:09 +0530 | [diff] [blame] | 979 | |
| 980 | // Fill in GraphicalConsole and SerialConsole info |
| 981 | res.jsonValue["SerialConsole"]["ServiceEnabled"] = true; |
| 982 | res.jsonValue["SerialConsole"]["ConnectTypesSupported"] = {"IPMI", |
| 983 | "SSH"}; |
| 984 | // TODO (Santosh) : Uncomment when KVM support is in. |
| 985 | // res.jsonValue["GraphicalConsole"]["ServiceEnabled"] = true; |
| 986 | // res.jsonValue["GraphicalConsole"]["ConnectTypesSupported"] = |
| 987 | // {"KVMIP"}; |
| 988 | |
Gunnar Mills | 603a664 | 2019-01-21 17:03:51 -0600 | [diff] [blame] | 989 | res.jsonValue["Links"]["ManagerForServers@odata.count"] = 1; |
| 990 | res.jsonValue["Links"]["ManagerForServers"] = { |
| 991 | {{"@odata.id", "/redfish/v1/Systems/system"}}}; |
| 992 | #ifdef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS |
| 993 | res.jsonValue["Links"]["ManagerForChassis@odata.count"] = 1; |
| 994 | res.jsonValue["Links"]["ManagerForChassis"] = { |
| 995 | {{"@odata.id", "/redfish/v1/Chassis/chassis"}}}; |
| 996 | #endif |
Jennifer Lee | ed5befb | 2018-08-10 11:29:45 -0700 | [diff] [blame] | 997 | std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 998 | |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 999 | crow::connections::systemBus->async_method_call( |
| 1000 | [asyncResp](const boost::system::error_code ec, |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1001 | const dbus::utility::ManagedObjectType& resp) { |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1002 | if (ec) |
| 1003 | { |
| 1004 | BMCWEB_LOG_ERROR << "Error while getting Software Version"; |
Jason M. Bills | f12894f | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1005 | messages::internalError(asyncResp->res); |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1006 | return; |
| 1007 | } |
| 1008 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1009 | for (auto& objpath : resp) |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1010 | { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1011 | for (auto& interface : objpath.second) |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1012 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1013 | // If interface is |
| 1014 | // xyz.openbmc_project.Software.Version, this is |
| 1015 | // what we're looking for. |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1016 | if (interface.first == |
| 1017 | "xyz.openbmc_project.Software.Version") |
| 1018 | { |
| 1019 | // Cut out everyting until last "/", ... |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1020 | for (auto& property : interface.second) |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1021 | { |
| 1022 | if (property.first == "Version") |
| 1023 | { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1024 | const std::string* value = |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 1025 | std::get_if<std::string>( |
| 1026 | &property.second); |
Jennifer Lee | ca53792 | 2018-08-10 10:07:30 -0700 | [diff] [blame] | 1027 | if (value == nullptr) |
| 1028 | { |
| 1029 | continue; |
| 1030 | } |
| 1031 | asyncResp->res |
| 1032 | .jsonValue["FirmwareVersion"] = *value; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | }, |
| 1039 | "xyz.openbmc_project.Software.BMC.Updater", |
| 1040 | "/xyz/openbmc_project/software", |
| 1041 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1042 | getPidValues(asyncResp); |
| 1043 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1044 | void setPidValues(std::shared_ptr<AsyncResp> response, nlohmann::json& data) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1045 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1046 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1047 | // todo(james): might make sense to do a mapper call here if this |
| 1048 | // interface gets more traction |
| 1049 | crow::connections::systemBus->async_method_call( |
| 1050 | [response, |
| 1051 | data](const boost::system::error_code ec, |
| 1052 | const dbus::utility::ManagedObjectType& managedObj) { |
| 1053 | if (ec) |
| 1054 | { |
| 1055 | BMCWEB_LOG_ERROR << "Error communicating to Entity Manager"; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1056 | messages::internalError(response->res); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1057 | return; |
| 1058 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1059 | |
| 1060 | // todo(james) mutable doesn't work with asio bindings |
| 1061 | nlohmann::json jsonData = data; |
| 1062 | |
| 1063 | std::optional<nlohmann::json> pidControllers; |
| 1064 | std::optional<nlohmann::json> fanControllers; |
| 1065 | std::optional<nlohmann::json> fanZones; |
| 1066 | std::optional<nlohmann::json> stepwiseControllers; |
| 1067 | if (!redfish::json_util::readJson( |
| 1068 | jsonData, response->res, "PidControllers", |
| 1069 | pidControllers, "FanControllers", fanControllers, |
| 1070 | "FanZones", fanZones, "StepwiseControllers", |
| 1071 | stepwiseControllers)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1072 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1073 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ |
| 1074 | << ", Illegal Property " |
| 1075 | << jsonData.dump(); |
| 1076 | return; |
| 1077 | } |
| 1078 | std::array< |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1079 | std::pair<std::string, std::optional<nlohmann::json>*>, 4> |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1080 | sections = { |
| 1081 | std::make_pair("PidControllers", &pidControllers), |
| 1082 | std::make_pair("FanControllers", &fanControllers), |
| 1083 | std::make_pair("FanZones", &fanZones), |
| 1084 | std::make_pair("StepwiseControllers", |
| 1085 | &stepwiseControllers)}; |
| 1086 | |
| 1087 | for (auto& containerPair : sections) |
| 1088 | { |
| 1089 | auto& container = *(containerPair.second); |
| 1090 | if (!container) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1091 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1092 | continue; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1093 | } |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1094 | std::string& type = containerPair.first; |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1095 | |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1096 | for (nlohmann::json::iterator it = container->begin(); |
| 1097 | it != container->end(); it++) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1098 | { |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1099 | const auto& name = it.key(); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1100 | auto pathItr = |
| 1101 | std::find_if(managedObj.begin(), managedObj.end(), |
| 1102 | [&name](const auto& obj) { |
| 1103 | return boost::algorithm::ends_with( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1104 | obj.first.str, "/" + name); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1105 | }); |
| 1106 | boost::container::flat_map< |
| 1107 | std::string, dbus::utility::DbusVariantType> |
| 1108 | output; |
| 1109 | |
| 1110 | output.reserve(16); // The pid interface length |
| 1111 | |
| 1112 | // determines if we're patching entity-manager or |
| 1113 | // creating a new object |
| 1114 | bool createNewObject = (pathItr == managedObj.end()); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1115 | std::string iface; |
| 1116 | if (type == "PidControllers" || |
| 1117 | type == "FanControllers") |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1118 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1119 | iface = pidConfigurationIface; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1120 | if (!createNewObject && |
| 1121 | pathItr->second.find(pidConfigurationIface) == |
| 1122 | pathItr->second.end()) |
| 1123 | { |
| 1124 | createNewObject = true; |
| 1125 | } |
| 1126 | } |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1127 | else if (type == "FanZones") |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1128 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1129 | iface = pidZoneConfigurationIface; |
| 1130 | if (!createNewObject && |
| 1131 | pathItr->second.find( |
| 1132 | pidZoneConfigurationIface) == |
| 1133 | pathItr->second.end()) |
| 1134 | { |
| 1135 | |
| 1136 | createNewObject = true; |
| 1137 | } |
| 1138 | } |
| 1139 | else if (type == "StepwiseControllers") |
| 1140 | { |
| 1141 | iface = stepwiseConfigurationIface; |
| 1142 | if (!createNewObject && |
| 1143 | pathItr->second.find( |
| 1144 | stepwiseConfigurationIface) == |
| 1145 | pathItr->second.end()) |
| 1146 | { |
| 1147 | createNewObject = true; |
| 1148 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1149 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1150 | BMCWEB_LOG_DEBUG << "Create new = " << createNewObject |
| 1151 | << "\n"; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1152 | output["Name"] = |
| 1153 | boost::replace_all_copy(name, "_", " "); |
| 1154 | |
| 1155 | std::string chassis; |
| 1156 | CreatePIDRet ret = createPidInterface( |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1157 | response, type, it, pathItr->first.str, managedObj, |
| 1158 | createNewObject, output, chassis); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1159 | if (ret == CreatePIDRet::fail) |
| 1160 | { |
| 1161 | return; |
| 1162 | } |
| 1163 | else if (ret == CreatePIDRet::del) |
| 1164 | { |
| 1165 | continue; |
| 1166 | } |
| 1167 | |
| 1168 | if (!createNewObject) |
| 1169 | { |
| 1170 | for (const auto& property : output) |
| 1171 | { |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1172 | crow::connections::systemBus->async_method_call( |
| 1173 | [response, |
| 1174 | propertyName{std::string(property.first)}]( |
| 1175 | const boost::system::error_code ec) { |
| 1176 | if (ec) |
| 1177 | { |
| 1178 | BMCWEB_LOG_ERROR |
| 1179 | << "Error patching " |
| 1180 | << propertyName << ": " << ec; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1181 | messages::internalError( |
| 1182 | response->res); |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1183 | return; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1184 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1185 | messages::success(response->res); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1186 | }, |
| 1187 | "xyz.openbmc_project.EntityManager", |
| 1188 | pathItr->first.str, |
| 1189 | "org.freedesktop.DBus.Properties", "Set", |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1190 | iface, property.first, property.second); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1191 | } |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | if (chassis.empty()) |
| 1196 | { |
| 1197 | BMCWEB_LOG_ERROR |
| 1198 | << "Failed to get chassis from config"; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1199 | messages::invalidObject(response->res, name); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1200 | return; |
| 1201 | } |
| 1202 | |
| 1203 | bool foundChassis = false; |
| 1204 | for (const auto& obj : managedObj) |
| 1205 | { |
| 1206 | if (boost::algorithm::ends_with(obj.first.str, |
| 1207 | chassis)) |
| 1208 | { |
| 1209 | chassis = obj.first.str; |
| 1210 | foundChassis = true; |
| 1211 | break; |
| 1212 | } |
| 1213 | } |
| 1214 | if (!foundChassis) |
| 1215 | { |
| 1216 | BMCWEB_LOG_ERROR |
| 1217 | << "Failed to find chassis on dbus"; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1218 | messages::resourceMissingAtURI( |
| 1219 | response->res, |
| 1220 | "/redfish/v1/Chassis/" + chassis); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1221 | return; |
| 1222 | } |
| 1223 | |
| 1224 | crow::connections::systemBus->async_method_call( |
| 1225 | [response](const boost::system::error_code ec) { |
| 1226 | if (ec) |
| 1227 | { |
| 1228 | BMCWEB_LOG_ERROR |
| 1229 | << "Error Adding Pid Object " << ec; |
Jason M. Bills | 35a62c7 | 2018-10-09 12:45:45 -0700 | [diff] [blame] | 1230 | messages::internalError(response->res); |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1231 | return; |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1232 | } |
James Feist | b6baeaa | 2019-02-21 10:41:40 -0800 | [diff] [blame] | 1233 | messages::success(response->res); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1234 | }, |
| 1235 | "xyz.openbmc_project.EntityManager", chassis, |
| 1236 | "xyz.openbmc_project.AddObject", "AddObject", |
| 1237 | output); |
| 1238 | } |
| 1239 | } |
| 1240 | } |
| 1241 | }, |
| 1242 | "xyz.openbmc_project.EntityManager", "/", objectManagerIface, |
| 1243 | "GetManagedObjects"); |
| 1244 | } |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1245 | |
| 1246 | void doPatch(crow::Response& res, const crow::Request& req, |
| 1247 | const std::vector<std::string>& params) override |
| 1248 | { |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1249 | std::optional<nlohmann::json> oem; |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 1250 | std::optional<std::string> datetime; |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1251 | |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 1252 | if (!json_util::readJson(req, res, "Oem", oem, "DateTime", datetime)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1253 | { |
| 1254 | return; |
| 1255 | } |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1256 | |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1257 | std::shared_ptr<AsyncResp> response = std::make_shared<AsyncResp>(res); |
Ed Tanous | 0627a2c | 2018-11-29 17:09:23 -0800 | [diff] [blame] | 1258 | |
| 1259 | if (oem) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1260 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1261 | std::optional<nlohmann::json> openbmc; |
| 1262 | if (!redfish::json_util::readJson(*oem, res, "OpenBmc", openbmc)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1263 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1264 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ << ", Illegal Property " |
| 1265 | << oem->dump(); |
| 1266 | return; |
| 1267 | } |
| 1268 | if (openbmc) |
| 1269 | { |
| 1270 | std::optional<nlohmann::json> fan; |
| 1271 | if (!redfish::json_util::readJson(*openbmc, res, "Fan", fan)) |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1272 | { |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1273 | BMCWEB_LOG_ERROR << "Line:" << __LINE__ |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1274 | << ", Illegal Property " |
| 1275 | << openbmc->dump(); |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1276 | return; |
| 1277 | } |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1278 | if (fan) |
James Feist | 5f2caae | 2018-12-12 14:08:25 -0800 | [diff] [blame] | 1279 | { |
Ed Tanous | 43b761d | 2019-02-13 20:10:56 -0800 | [diff] [blame] | 1280 | setPidValues(response, *fan); |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1281 | } |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1282 | } |
| 1283 | } |
Santosh Puranik | af5d6058 | 2019-03-20 18:16:36 +0530 | [diff] [blame] | 1284 | if (datetime) |
| 1285 | { |
| 1286 | setDateTime(response, std::move(*datetime)); |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | void setDateTime(std::shared_ptr<AsyncResp> aResp, |
| 1291 | std::string datetime) const |
| 1292 | { |
| 1293 | BMCWEB_LOG_DEBUG << "Set date time: " << datetime; |
| 1294 | |
| 1295 | std::stringstream stream(datetime); |
| 1296 | // Convert from ISO 8601 to boost local_time |
| 1297 | // (BMC only has time in UTC) |
| 1298 | boost::posix_time::ptime posixTime; |
| 1299 | boost::posix_time::ptime epoch(boost::gregorian::date(1970, 1, 1)); |
| 1300 | // Facet gets deleted with the stringsteam |
| 1301 | auto ifc = std::make_unique<boost::local_time::local_time_input_facet>( |
| 1302 | "%Y-%m-%d %H:%M:%S%F %ZP"); |
| 1303 | stream.imbue(std::locale(stream.getloc(), ifc.release())); |
| 1304 | |
| 1305 | boost::local_time::local_date_time ldt( |
| 1306 | boost::local_time::not_a_date_time); |
| 1307 | |
| 1308 | if (stream >> ldt) |
| 1309 | { |
| 1310 | posixTime = ldt.utc_time(); |
| 1311 | boost::posix_time::time_duration dur = posixTime - epoch; |
| 1312 | uint64_t durMicroSecs = |
| 1313 | static_cast<uint64_t>(dur.total_microseconds()); |
| 1314 | crow::connections::systemBus->async_method_call( |
| 1315 | [aResp{std::move(aResp)}, datetime{std::move(datetime)}]( |
| 1316 | const boost::system::error_code ec) { |
| 1317 | if (ec) |
| 1318 | { |
| 1319 | BMCWEB_LOG_DEBUG << "Failed to set elapsed time. " |
| 1320 | "DBUS response error " |
| 1321 | << ec; |
| 1322 | messages::internalError(aResp->res); |
| 1323 | return; |
| 1324 | } |
| 1325 | aResp->res.jsonValue["DateTime"] = datetime; |
| 1326 | }, |
| 1327 | "xyz.openbmc_project.Time.Manager", |
| 1328 | "/xyz/openbmc_project/time/bmc", |
| 1329 | "org.freedesktop.DBus.Properties", "Set", |
| 1330 | "xyz.openbmc_project.Time.EpochTime", "Elapsed", |
| 1331 | std::variant<uint64_t>(durMicroSecs)); |
| 1332 | } |
| 1333 | else |
| 1334 | { |
| 1335 | messages::propertyValueFormatError(aResp->res, datetime, |
| 1336 | "DateTime"); |
| 1337 | return; |
| 1338 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1339 | } |
| 1340 | |
Ed Tanous | 0f74e64 | 2018-11-12 15:17:05 -0800 | [diff] [blame] | 1341 | std::string uuid; |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1342 | }; |
| 1343 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1344 | class ManagerCollection : public Node |
| 1345 | { |
| 1346 | public: |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1347 | ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1348 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1349 | entityPrivileges = { |
| 1350 | {boost::beast::http::verb::get, {{"Login"}}}, |
| 1351 | {boost::beast::http::verb::head, {{"Login"}}}, |
| 1352 | {boost::beast::http::verb::patch, {{"ConfigureManager"}}}, |
| 1353 | {boost::beast::http::verb::put, {{"ConfigureManager"}}}, |
| 1354 | {boost::beast::http::verb::delete_, {{"ConfigureManager"}}}, |
| 1355 | {boost::beast::http::verb::post, {{"ConfigureManager"}}}}; |
| 1356 | } |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1357 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1358 | private: |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1359 | void doGet(crow::Response& res, const crow::Request& req, |
| 1360 | const std::vector<std::string>& params) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1361 | { |
James Feist | 83ff9ab | 2018-08-31 10:18:24 -0700 | [diff] [blame] | 1362 | // Collections don't include the static data added by SubRoute |
| 1363 | // because it has a duplicate entry for members |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1364 | res.jsonValue["@odata.id"] = "/redfish/v1/Managers"; |
| 1365 | res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection"; |
| 1366 | res.jsonValue["@odata.context"] = |
| 1367 | "/redfish/v1/$metadata#ManagerCollection.ManagerCollection"; |
| 1368 | res.jsonValue["Name"] = "Manager Collection"; |
| 1369 | res.jsonValue["Members@odata.count"] = 1; |
| 1370 | res.jsonValue["Members"] = { |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1371 | {{"@odata.id", "/redfish/v1/Managers/bmc"}}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1372 | res.end(); |
| 1373 | } |
Borawski.Lukasz | 9c310685 | 2018-02-09 15:24:22 +0100 | [diff] [blame] | 1374 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1375 | } // namespace redfish |