blob: cf84c9306a6b300cf2a5366d9a1a1dd4aea65f5e [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
18#include "async_resp.hpp"
19
John Edward Broadbent7e860f12021-04-08 15:57:16 -070020#include <app.hpp>
James Feistb49ac872019-05-21 15:12:01 -070021#include <boost/algorithm/string/predicate.hpp>
James Feistb49ac872019-05-21 15:12:01 -070022#include <dbus_singleton.hpp>
Jonathan Doman1e1e5982021-06-11 09:36:17 -070023#include <dbus_utility.hpp>
Nan Zhoudfababf2022-05-17 23:12:06 +000024#include <nlohmann/json.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050025
James Feistb49ac872019-05-21 15:12:01 -070026#include <variant>
27
28namespace redfish
29{
30
31struct HealthPopulate : std::enable_shared_from_this<HealthPopulate>
32{
Nan Zhoudfababf2022-05-17 23:12:06 +000033 // By default populate status to "/Status" of |asyncResp->res.jsonValue|.
Ed Tanous4e23a442022-06-06 09:57:26 -070034 explicit HealthPopulate(
35 const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn) :
36 asyncResp(asyncRespIn),
37 statusPtr("/Status")
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038 {}
James Feist5bc2dc82019-10-22 14:33:16 -070039
Nan Zhoudfababf2022-05-17 23:12:06 +000040 // Takes a JSON pointer rather than a reference. This is pretty useful when
41 // the address of the status JSON might change, for example, elements in an
42 // array.
zhanghch058d1b46d2021-04-01 11:18:24 +080043 HealthPopulate(const std::shared_ptr<bmcweb::AsyncResp>& asyncRespIn,
Nan Zhoudfababf2022-05-17 23:12:06 +000044 const nlohmann::json::json_pointer& ptr) :
Ed Tanous23a21a12020-07-25 04:45:05 +000045 asyncResp(asyncRespIn),
Nan Zhoudfababf2022-05-17 23:12:06 +000046 statusPtr(ptr)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050047 {}
James Feistb49ac872019-05-21 15:12:01 -070048
Ed Tanousecd6a3a2022-01-07 09:18:40 -080049 HealthPopulate(const HealthPopulate&) = delete;
50 HealthPopulate(HealthPopulate&&) = delete;
51 HealthPopulate& operator=(const HealthPopulate&) = delete;
52 HealthPopulate& operator=(const HealthPopulate&&) = delete;
53
James Feistb49ac872019-05-21 15:12:01 -070054 ~HealthPopulate()
55 {
Nan Zhoudfababf2022-05-17 23:12:06 +000056 nlohmann::json& jsonStatus = asyncResp->res.jsonValue[statusPtr];
Gunnar Mills1214b7e2020-06-04 10:11:30 -050057 nlohmann::json& health = jsonStatus["Health"];
58 nlohmann::json& rollup = jsonStatus["HealthRollup"];
James Feistb49ac872019-05-21 15:12:01 -070059
60 health = "OK";
61 rollup = "OK";
62
Ed Tanous23a21a12020-07-25 04:45:05 +000063 for (const std::shared_ptr<HealthPopulate>& healthChild : children)
James Feist5bc2dc82019-10-22 14:33:16 -070064 {
Ed Tanous23a21a12020-07-25 04:45:05 +000065 healthChild->globalInventoryPath = globalInventoryPath;
66 healthChild->statuses = statuses;
James Feist5bc2dc82019-10-22 14:33:16 -070067 }
68
Gunnar Mills1214b7e2020-06-04 10:11:30 -050069 for (const auto& [path, interfaces] : statuses)
James Feistb49ac872019-05-21 15:12:01 -070070 {
71 bool isChild = false;
Karol Wojciechowski42cbb512021-07-28 17:12:20 +020072 bool isSelf = false;
73 if (selfPath)
74 {
75 if (boost::equals(path.str, *selfPath) ||
76 boost::starts_with(path.str, *selfPath + "/"))
77 {
78 isSelf = true;
79 }
80 }
James Feistb49ac872019-05-21 15:12:01 -070081
82 // managers inventory is all the inventory, don't skip any
James Feist35e257a2020-06-05 13:30:51 -070083 if (!isManagersHealth && !isSelf)
James Feistb49ac872019-05-21 15:12:01 -070084 {
85
86 // 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
Gunnar Mills1214b7e2020-06-04 10:11:30 -050090 for (const std::string& child : inventory)
James Feistb49ac872019-05-21 15:12:01 -070091 {
92 if (boost::starts_with(path.str, child))
93 {
94 isChild = true;
95 break;
96 }
97 }
98 if (!isChild)
99 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800100 for (const auto& [interface, association] : interfaces)
James Feistb49ac872019-05-21 15:12:01 -0700101 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800102 if (interface != "xyz.openbmc_project.Association")
James Feistb49ac872019-05-21 15:12:01 -0700103 {
Ed Tanous711ac7a2021-12-20 09:34:41 -0800104 continue;
James Feistb49ac872019-05-21 15:12:01 -0700105 }
Ed Tanous711ac7a2021-12-20 09:34:41 -0800106 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 Feistb49ac872019-05-21 15:12:01 -0700137 }
138 }
139 }
140
141 if (boost::starts_with(path.str, globalInventoryPath) &&
142 boost::ends_with(path.str, "critical"))
143 {
144 health = "Critical";
145 rollup = "Critical";
146 return;
147 }
Ed Tanous3174e4d2020-10-07 11:41:22 -0700148 if (boost::starts_with(path.str, globalInventoryPath) &&
149 boost::ends_with(path.str, "warning"))
James Feistb49ac872019-05-21 15:12:01 -0700150 {
151 health = "Warning";
152 if (rollup != "Critical")
153 {
154 rollup = "Warning";
155 }
156 }
157 else if (boost::ends_with(path.str, "critical"))
158 {
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 }
166 else if (boost::ends_with(path.str, "warning"))
167 {
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 {
196 std::shared_ptr<HealthPopulate> self = shared_from_this();
197 crow::connections::systemBus->async_method_call(
198 [self](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800199 const dbus::utility::MapperGetSubTreePathsResponse& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700200 if (ec || resp.size() != 1)
201 {
202 // no global item, or too many
203 return;
204 }
205 self->globalInventoryPath = resp[0];
James Feistb49ac872019-05-21 15:12:01 -0700206 },
207 "xyz.openbmc_project.ObjectMapper",
208 "/xyz/openbmc_project/object_mapper",
Ed Tanous271584a2019-07-09 16:24:22 -0700209 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", 0,
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500210 std::array<const char*, 1>{
James Feistb49ac872019-05-21 15:12:01 -0700211 "xyz.openbmc_project.Inventory.Item.Global"});
212 }
213
214 void getAllStatusAssociations()
215 {
216 std::shared_ptr<HealthPopulate> self = shared_from_this();
217 crow::connections::systemBus->async_method_call(
218 [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 {
227 if (boost::ends_with(it->first.str, "critical") ||
228 boost::ends_with(it->first.str, "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 }
James Feistb49ac872019-05-21 15:12:01 -0700235 },
236 "xyz.openbmc_project.ObjectMapper", "/",
237 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
238 }
239
zhanghch058d1b46d2021-04-01 11:18:24 +0800240 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
Nan Zhoudfababf2022-05-17 23:12:06 +0000241
242 // Will populate the health status into |asyncResp_json[statusPtr]|
243 nlohmann::json::json_pointer statusPtr;
James Feist5bc2dc82019-10-22 14:33:16 -0700244
245 // we store pointers to other HealthPopulate items so we can update their
246 // members and reduce dbus calls. As we hold a shared_ptr to them, they get
247 // destroyed last, and they need not call populate()
248 std::vector<std::shared_ptr<HealthPopulate>> children;
249
James Feist35e257a2020-06-05 13:30:51 -0700250 // self is used if health is for an individual items status, as this is the
251 // 'lowest most' item, the rollup will equal the health
252 std::optional<std::string> selfPath;
253
James Feistb49ac872019-05-21 15:12:01 -0700254 std::vector<std::string> inventory;
255 bool isManagersHealth = false;
256 dbus::utility::ManagedObjectType statuses;
257 std::string globalInventoryPath = "-"; // default to illegal dbus path
James Feist9536a142019-11-21 09:02:32 -0800258 bool populated = false;
James Feistb49ac872019-05-21 15:12:01 -0700259};
Ed Tanous23a21a12020-07-25 04:45:05 +0000260} // namespace redfish