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