James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 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 "async_resp.hpp" |
| 19 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 20 | #include <app.hpp> |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 21 | #include <dbus_singleton.hpp> |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 22 | #include <dbus_utility.hpp> |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 23 | #include <nlohmann/json.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 25 | #include <variant> |
| 26 | |
| 27 | namespace redfish |
| 28 | { |
| 29 | |
| 30 | struct HealthPopulate : std::enable_shared_from_this<HealthPopulate> |
| 31 | { |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 32 | // By default populate status to "/Status" of |asyncResp->res.jsonValue|. |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 33 | explicit HealthPopulate( |
| 34 | const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) : |
| 35 | asyncResp(asyncRespIn), |
| 36 | statusPtr("/Status") |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 37 | {} |
James Feist | 5bc2dc8 | 2019-10-22 14:33:16 -0700 | [diff] [blame] | 38 | |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 39 | // Takes a JSON pointer rather than a reference. This is pretty useful when |
| 40 | // the address of the status JSON might change, for example, elements in an |
| 41 | // array. |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 42 | HealthPopulate(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn, |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 43 | const nlohmann::json::json_pointer& ptr) : |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 44 | asyncResp(asyncRespIn), |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 45 | statusPtr(ptr) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 46 | {} |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 47 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 48 | HealthPopulate(const HealthPopulate&) = delete; |
| 49 | HealthPopulate(HealthPopulate&&) = delete; |
| 50 | HealthPopulate& operator=(const HealthPopulate&) = delete; |
| 51 | HealthPopulate& operator=(const HealthPopulate&&) = delete; |
| 52 | |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 53 | ~HealthPopulate() |
| 54 | { |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 55 | nlohmann::json& jsonStatus = asyncResp->res.jsonValue[statusPtr]; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 56 | nlohmann::json& health = jsonStatus["Health"]; |
| 57 | nlohmann::json& rollup = jsonStatus["HealthRollup"]; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 58 | |
| 59 | health = "OK"; |
| 60 | rollup = "OK"; |
| 61 | |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 62 | for (const std::shared_ptr<HealthPopulate>& healthChild : children) |
James Feist | 5bc2dc8 | 2019-10-22 14:33:16 -0700 | [diff] [blame] | 63 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 64 | healthChild->globalInventoryPath = globalInventoryPath; |
| 65 | healthChild->statuses = statuses; |
James Feist | 5bc2dc8 | 2019-10-22 14:33:16 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 68 | for (const auto& [path, interfaces] : statuses) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 69 | { |
Karol Wojciechowski | 42cbb51 | 2021-07-28 17:12:20 +0200 | [diff] [blame] | 70 | bool isSelf = false; |
| 71 | if (selfPath) |
| 72 | { |
| 73 | if (boost::equals(path.str, *selfPath) || |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 74 | path.str.starts_with(*selfPath + "/")) |
Karol Wojciechowski | 42cbb51 | 2021-07-28 17:12:20 +0200 | [diff] [blame] | 75 | { |
| 76 | isSelf = true; |
| 77 | } |
| 78 | } |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 79 | |
| 80 | // managers inventory is all the inventory, don't skip any |
James Feist | 35e257a | 2020-06-05 13:30:51 -0700 | [diff] [blame] | 81 | if (!isManagersHealth && !isSelf) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 82 | { |
| 83 | |
| 84 | // We only want to look at this association if either the path |
| 85 | // of this association is an inventory item, or one of the |
| 86 | // endpoints in this association is a child |
| 87 | |
Ed Tanous | f8fe53e | 2022-06-30 15:55:45 -0700 | [diff] [blame] | 88 | bool isChild = false; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 89 | for (const std::string& child : inventory) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 90 | { |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 91 | if (path.str.starts_with(child)) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 92 | { |
| 93 | isChild = true; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | if (!isChild) |
| 98 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 99 | for (const auto& [interface, association] : interfaces) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 100 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 101 | if (interface != "xyz.openbmc_project.Association") |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 102 | { |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 103 | continue; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 104 | } |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 105 | for (const auto& [name, value] : association) |
| 106 | { |
| 107 | if (name != "endpoints") |
| 108 | { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | const std::vector<std::string>* endpoints = |
| 113 | std::get_if<std::vector<std::string>>(&value); |
| 114 | if (endpoints == nullptr) |
| 115 | { |
| 116 | BMCWEB_LOG_ERROR << "Illegal association at " |
| 117 | << path.str; |
| 118 | continue; |
| 119 | } |
| 120 | bool containsChild = false; |
| 121 | for (const std::string& endpoint : *endpoints) |
| 122 | { |
| 123 | if (std::find(inventory.begin(), |
| 124 | inventory.end(), |
| 125 | endpoint) != inventory.end()) |
| 126 | { |
| 127 | containsChild = true; |
| 128 | break; |
| 129 | } |
| 130 | } |
| 131 | if (!containsChild) |
| 132 | { |
| 133 | continue; |
| 134 | } |
| 135 | } |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 140 | if (path.str.starts_with(globalInventoryPath) && |
| 141 | path.str.ends_with("critical")) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 142 | { |
| 143 | health = "Critical"; |
| 144 | rollup = "Critical"; |
| 145 | return; |
| 146 | } |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 147 | if (path.str.starts_with(globalInventoryPath) && |
| 148 | path.str.ends_with("warning")) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 149 | { |
| 150 | health = "Warning"; |
| 151 | if (rollup != "Critical") |
| 152 | { |
| 153 | rollup = "Warning"; |
| 154 | } |
| 155 | } |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 156 | else if (path.str.ends_with("critical")) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 157 | { |
| 158 | rollup = "Critical"; |
James Feist | 35e257a | 2020-06-05 13:30:51 -0700 | [diff] [blame] | 159 | if (isSelf) |
| 160 | { |
| 161 | health = "Critical"; |
| 162 | return; |
| 163 | } |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 164 | } |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 165 | else if (path.str.ends_with("warning")) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 166 | { |
| 167 | if (rollup != "Critical") |
| 168 | { |
| 169 | rollup = "Warning"; |
| 170 | } |
James Feist | 35e257a | 2020-06-05 13:30:51 -0700 | [diff] [blame] | 171 | |
| 172 | if (isSelf) |
| 173 | { |
| 174 | health = "Warning"; |
| 175 | } |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
James Feist | 5bc2dc8 | 2019-10-22 14:33:16 -0700 | [diff] [blame] | 180 | // this should only be called once per url, others should get updated by |
| 181 | // being added as children to the 'main' health object for the page |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 182 | void populate() |
| 183 | { |
James Feist | 9536a14 | 2019-11-21 09:02:32 -0800 | [diff] [blame] | 184 | if (populated) |
| 185 | { |
| 186 | return; |
| 187 | } |
| 188 | populated = true; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 189 | getAllStatusAssociations(); |
| 190 | getGlobalPath(); |
| 191 | } |
| 192 | |
| 193 | void getGlobalPath() |
| 194 | { |
| 195 | std::shared_ptr<HealthPopulate> self = shared_from_this(); |
| 196 | crow::connections::systemBus->async_method_call( |
| 197 | [self](const boost::system::error_code ec, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 198 | const dbus::utility::MapperGetSubTreePathsResponse& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 199 | if (ec || resp.size() != 1) |
| 200 | { |
| 201 | // no global item, or too many |
| 202 | return; |
| 203 | } |
| 204 | self->globalInventoryPath = resp[0]; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 205 | }, |
| 206 | "xyz.openbmc_project.ObjectMapper", |
| 207 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 208 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 209 | std::array<const char*, 1>{ |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 210 | "xyz.openbmc_project.Inventory.Item.Global"}); |
| 211 | } |
| 212 | |
| 213 | void getAllStatusAssociations() |
| 214 | { |
| 215 | std::shared_ptr<HealthPopulate> self = shared_from_this(); |
| 216 | crow::connections::systemBus->async_method_call( |
| 217 | [self](const boost::system::error_code ec, |
Ed Tanous | 914e2d5 | 2022-01-07 11:38:34 -0800 | [diff] [blame] | 218 | const dbus::utility::ManagedObjectType& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 219 | if (ec) |
| 220 | { |
| 221 | return; |
| 222 | } |
| 223 | self->statuses = resp; |
| 224 | for (auto it = self->statuses.begin(); it != self->statuses.end();) |
| 225 | { |
Ed Tanous | 11ba397 | 2022-07-11 09:50:41 -0700 | [diff] [blame] | 226 | if (it->first.str.ends_with("critical") || |
| 227 | it->first.str.ends_with("warning")) |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 228 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 229 | it++; |
| 230 | continue; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 231 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 232 | it = self->statuses.erase(it); |
| 233 | } |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 234 | }, |
| 235 | "xyz.openbmc_project.ObjectMapper", "/", |
| 236 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 237 | } |
| 238 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 239 | std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
Nan Zhou | dfababf | 2022-05-17 23:12:06 +0000 | [diff] [blame] | 240 | |
| 241 | // Will populate the health status into |asyncResp_json[statusPtr]| |
| 242 | nlohmann::json::json_pointer statusPtr; |
James Feist | 5bc2dc8 | 2019-10-22 14:33:16 -0700 | [diff] [blame] | 243 | |
| 244 | // we store pointers to other HealthPopulate items so we can update their |
| 245 | // members and reduce dbus calls. As we hold a shared_ptr to them, they get |
| 246 | // destroyed last, and they need not call populate() |
| 247 | std::vector<std::shared_ptr<HealthPopulate>> children; |
| 248 | |
James Feist | 35e257a | 2020-06-05 13:30:51 -0700 | [diff] [blame] | 249 | // self is used if health is for an individual items status, as this is the |
| 250 | // 'lowest most' item, the rollup will equal the health |
| 251 | std::optional<std::string> selfPath; |
| 252 | |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 253 | std::vector<std::string> inventory; |
| 254 | bool isManagersHealth = false; |
| 255 | dbus::utility::ManagedObjectType statuses; |
| 256 | std::string globalInventoryPath = "-"; // default to illegal dbus path |
James Feist | 9536a14 | 2019-11-21 09:02:32 -0800 | [diff] [blame] | 257 | bool populated = false; |
James Feist | b49ac87 | 2019-05-21 15:12:01 -0700 | [diff] [blame] | 258 | }; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 259 | } // namespace redfish |