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