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