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