blob: 0c35cb5ff7dc7e9d12fa14aaa3a30dfcfe650213 [file] [log] [blame]
James Feistb49ac872019-05-21 15:12:01 -07001/*
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 Tanous3ccb3ad2023-01-13 17:40:03 -080018#include "app.hpp"
James Feistb49ac872019-05-21 15:12:01 -070019#include "async_resp.hpp"
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080020#include "dbus_singleton.hpp"
George Liu7a1dbc42022-12-07 16:03:22 +080021#include "dbus_utility.hpp"
James Feistb49ac872019-05-21 15:12:01 -070022
Nan Zhoudfababf2022-05-17 23:12:06 +000023#include <nlohmann/json.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024
George Liu7a1dbc42022-12-07 16:03:22 +080025#include <array>
Ed Tanous3544d2a2023-08-06 18:12:20 -070026#include <ranges>
George Liu7a1dbc42022-12-07 16:03:22 +080027#include <string_view>
James Feistb49ac872019-05-21 15:12:01 -070028#include <variant>
29
30namespace redfish
31{
32
33struct HealthPopulate : std::enable_shared_from_this<HealthPopulate>
34{
Nan Zhoudfababf2022-05-17 23:12:06 +000035 // By default populate status to "/Status" of |asyncResp->res.jsonValue|.
Ed Tanous4e23a442022-06-06 09:57:26 -070036 explicit HealthPopulate(
37 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
38 asyncResp(asyncRespIn),
39 statusPtr("/Status")
Gunnar Mills1214b7e2020-06-04 10:11:30 -050040 {}
James Feist5bc2dc82019-10-22 14:33:16 -070041
Nan Zhoudfababf2022-05-17 23:12:06 +000042 // Takes a JSON pointer rather than a reference. This is pretty useful when
43 // the address of the status JSON might change, for example, elements in an
44 // array.
zhanghch058d1b46d2021-04-01 11:18:24 +080045 HealthPopulate(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhoudfababf2022-05-17 23:12:06 +000046 const nlohmann::json::json_pointer& ptr) :
Ed Tanous23a21a12020-07-25 04:45:05 +000047 asyncResp(asyncRespIn),
Nan Zhoudfababf2022-05-17 23:12:06 +000048 statusPtr(ptr)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050049 {}
James Feistb49ac872019-05-21 15:12:01 -070050
Ed Tanousecd6a3a2022-01-07 09:18:40 -080051 HealthPopulate(const HealthPopulate&) = delete;
52 HealthPopulate(HealthPopulate&&) = delete;
53 HealthPopulate& operator=(const HealthPopulate&) = delete;
54 HealthPopulate& operator=(const HealthPopulate&&) = delete;
55
James Feistb49ac872019-05-21 15:12:01 -070056 ~HealthPopulate()
57 {
Nan Zhoudfababf2022-05-17 23:12:06 +000058 nlohmann::json& jsonStatus = asyncResp->res.jsonValue[statusPtr];
Gunnar Mills1214b7e2020-06-04 10:11:30 -050059 nlohmann::json& health = jsonStatus["Health"];
60 nlohmann::json& rollup = jsonStatus["HealthRollup"];
James Feistb49ac872019-05-21 15:12:01 -070061
62 health = "OK";
63 rollup = "OK";
64
Ed Tanous23a21a12020-07-25 04:45:05 +000065 for (const std::shared_ptr<HealthPopulate>& healthChild : children)
James Feist5bc2dc82019-10-22 14:33:16 -070066 {
Ed Tanous23a21a12020-07-25 04:45:05 +000067 healthChild->globalInventoryPath = globalInventoryPath;
68 healthChild->statuses = statuses;
James Feist5bc2dc82019-10-22 14:33:16 -070069 }
70
Gunnar Mills1214b7e2020-06-04 10:11:30 -050071 for (const auto& [path, interfaces] : statuses)
James Feistb49ac872019-05-21 15:12:01 -070072 {
Karol Wojciechowski42cbb512021-07-28 17:12:20 +020073 bool isSelf = false;
74 if (selfPath)
75 {
76 if (boost::equals(path.str, *selfPath) ||
Ed Tanous11ba3972022-07-11 09:50:41 -070077 path.str.starts_with(*selfPath + "/"))
Karol Wojciechowski42cbb512021-07-28 17:12:20 +020078 {
79 isSelf = true;
80 }
81 }
James Feistb49ac872019-05-21 15:12:01 -070082
83 // managers inventory is all the inventory, don't skip any
James Feist35e257a2020-06-05 13:30:51 -070084 if (!isManagersHealth && !isSelf)
James Feistb49ac872019-05-21 15:12:01 -070085 {
James Feistb49ac872019-05-21 15:12:01 -070086 // We only want to look at this association if either the path
87 // of this association is an inventory item, or one of the
88 // endpoints in this association is a child
89
Ed Tanousf8fe53e2022-06-30 15:55:45 -070090 bool isChild = false;
Gunnar Mills1214b7e2020-06-04 10:11:30 -050091 for (const std::string& child : inventory)
James Feistb49ac872019-05-21 15:12:01 -070092 {
Ed Tanous11ba3972022-07-11 09:50:41 -070093 if (path.str.starts_with(child))
James Feistb49ac872019-05-21 15:12:01 -070094 {
95 isChild = true;
96 break;
97 }
98 }
99 if (!isChild)
100 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800101 for (const auto& [interface, association] : interfaces)
James Feistb49ac872019-05-21 15:12:01 -0700102 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800103 if (interface != "xyz.openbmc_project.Association")
James Feistb49ac872019-05-21 15:12:01 -0700104 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800105 continue;
James Feistb49ac872019-05-21 15:12:01 -0700106 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800107 for (const auto& [name, value] : association)
108 {
109 if (name != "endpoints")
110 {
111 continue;
112 }
113
114 const std::vector<std::string>* endpoints =
115 std::get_if<std::vector<std::string>>(&value);
116 if (endpoints == nullptr)
117 {
Ed Tanous62598e32023-07-17 17:06:25 -0700118 BMCWEB_LOG_ERROR("Illegal association at {}",
119 path.str);
Ed Tanous711ac7a2021-12-20 09:34:41 -0800120 continue;
121 }
122 bool containsChild = false;
123 for (const std::string& endpoint : *endpoints)
124 {
Ed Tanous3544d2a2023-08-06 18:12:20 -0700125 if (std::ranges::find(inventory, endpoint) !=
126 inventory.end())
Ed Tanous711ac7a2021-12-20 09:34:41 -0800127 {
128 containsChild = true;
129 break;
130 }
131 }
132 if (!containsChild)
133 {
134 continue;
135 }
136 }
James Feistb49ac872019-05-21 15:12:01 -0700137 }
138 }
139 }
140
Ed Tanous11ba3972022-07-11 09:50:41 -0700141 if (path.str.starts_with(globalInventoryPath) &&
142 path.str.ends_with("critical"))
James Feistb49ac872019-05-21 15:12:01 -0700143 {
144 health = "Critical";
145 rollup = "Critical";
146 return;
147 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700148 if (path.str.starts_with(globalInventoryPath) &&
149 path.str.ends_with("warning"))
James Feistb49ac872019-05-21 15:12:01 -0700150 {
151 health = "Warning";
152 if (rollup != "Critical")
153 {
154 rollup = "Warning";
155 }
156 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700157 else if (path.str.ends_with("critical"))
James Feistb49ac872019-05-21 15:12:01 -0700158 {
159 rollup = "Critical";
James Feist35e257a2020-06-05 13:30:51 -0700160 if (isSelf)
161 {
162 health = "Critical";
163 return;
164 }
James Feistb49ac872019-05-21 15:12:01 -0700165 }
Ed Tanous11ba3972022-07-11 09:50:41 -0700166 else if (path.str.ends_with("warning"))
James Feistb49ac872019-05-21 15:12:01 -0700167 {
168 if (rollup != "Critical")
169 {
170 rollup = "Warning";
171 }
James Feist35e257a2020-06-05 13:30:51 -0700172
173 if (isSelf)
174 {
175 health = "Warning";
176 }
James Feistb49ac872019-05-21 15:12:01 -0700177 }
178 }
179 }
180
James Feist5bc2dc82019-10-22 14:33:16 -0700181 // 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 Feistb49ac872019-05-21 15:12:01 -0700183 void populate()
184 {
James Feist9536a142019-11-21 09:02:32 -0800185 if (populated)
186 {
187 return;
188 }
189 populated = true;
James Feistb49ac872019-05-21 15:12:01 -0700190 getAllStatusAssociations();
191 getGlobalPath();
192 }
193
194 void getGlobalPath()
195 {
George Liu7a1dbc42022-12-07 16:03:22 +0800196 constexpr std::array<std::string_view, 1> interfaces = {
197 "xyz.openbmc_project.Inventory.Item.Global"};
James Feistb49ac872019-05-21 15:12:01 -0700198 std::shared_ptr<HealthPopulate> self = shared_from_this();
George Liu7a1dbc42022-12-07 16:03:22 +0800199 dbus::utility::getSubTreePaths(
200 "/", 0, interfaces,
201 [self](const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800202 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700203 if (ec || resp.size() != 1)
204 {
205 // no global item, or too many
206 return;
207 }
208 self->globalInventoryPath = resp[0];
George Liu7a1dbc42022-12-07 16:03:22 +0800209 });
James Feistb49ac872019-05-21 15:12:01 -0700210 }
211
212 void getAllStatusAssociations()
213 {
214 std::shared_ptr<HealthPopulate> self = shared_from_this();
George Liu5eb468d2023-06-20 17:03:24 +0800215 sdbusplus::message::object_path path("/");
216 dbus::utility::getManagedObjects(
217 "xyz.openbmc_project.ObjectMapper", path,
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800218 [self](const boost::system::error_code& ec,
Ed Tanous914e2d52022-01-07 11:38:34 -0800219 const dbus::utility::ManagedObjectType& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700220 if (ec)
221 {
222 return;
223 }
224 self->statuses = resp;
225 for (auto it = self->statuses.begin(); it != self->statuses.end();)
226 {
Ed Tanous11ba3972022-07-11 09:50:41 -0700227 if (it->first.str.ends_with("critical") ||
228 it->first.str.ends_with("warning"))
James Feistb49ac872019-05-21 15:12:01 -0700229 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700230 it++;
231 continue;
James Feistb49ac872019-05-21 15:12:01 -0700232 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700233 it = self->statuses.erase(it);
234 }
George Liu5eb468d2023-06-20 17:03:24 +0800235 });
James Feistb49ac872019-05-21 15:12:01 -0700236 }
237
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Nan Zhoudfababf2022-05-17 23:12:06 +0000239
240 // Will populate the health status into |asyncResp_json[statusPtr]|
241 nlohmann::json::json_pointer statusPtr;
James Feist5bc2dc82019-10-22 14:33:16 -0700242
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 Feist35e257a2020-06-05 13:30:51 -0700248 // 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 Feistb49ac872019-05-21 15:12:01 -0700252 std::vector<std::string> inventory;
253 bool isManagersHealth = false;
254 dbus::utility::ManagedObjectType statuses;
255 std::string globalInventoryPath = "-"; // default to illegal dbus path
James Feist9536a142019-11-21 09:02:32 -0800256 bool populated = false;
James Feistb49ac872019-05-21 15:12:01 -0700257};
Ed Tanous23a21a12020-07-25 04:45:05 +0000258} // namespace redfish