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