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