blob: 1c06f01fa798130cd16bf356d5d56578af64ee3c [file] [log] [blame]
Matthew Barth9ea8bee2020-06-04 14:27:19 -05001/**
2 * Copyright © 2020 IBM 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#include "json_parser.hpp"
17
Matthew Barth22ab93b2020-06-08 10:47:56 -050018#include "conditions.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050019#include "json_config.hpp"
20#include "nonzero_speed_trust.hpp"
Matt Spinlerf06ab072020-10-14 12:58:22 -050021#include "power_interface.hpp"
22#include "power_off_rule.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050023#include "types.hpp"
24
Matt Spinlerf06ab072020-10-14 12:58:22 -050025#include <fmt/format.h>
26
Matthew Barth9ea8bee2020-06-04 14:27:19 -050027#include <nlohmann/json.hpp>
28#include <phosphor-logging/log.hpp>
29
30#include <algorithm>
31#include <map>
32#include <memory>
Matthew Barth22ab93b2020-06-08 10:47:56 -050033#include <optional>
Matthew Barth9ea8bee2020-06-04 14:27:19 -050034#include <vector>
35
36namespace phosphor::fan::monitor
37{
38
39using json = nlohmann::json;
40using namespace phosphor::logging;
41
42namespace tClass
43{
44
45// Get a constructed trust group class for a non-zero speed group
46CreateGroupFunction
47 getNonZeroSpeed(const std::vector<trust::GroupDefinition>& group)
48{
49 return [group]() {
50 return std::make_unique<trust::NonzeroSpeed>(std::move(group));
51 };
52}
53
54} // namespace tClass
55
56const std::map<std::string, trustHandler> trusts = {
57 {"nonzerospeed", tClass::getNonZeroSpeed}};
Matthew Barth3ad14342020-06-08 16:17:42 -050058const std::map<std::string, condHandler> conditions = {
59 {"propertiesmatch", condition::getPropertiesMatch}};
Matthew Barth9ea8bee2020-06-04 14:27:19 -050060
61const std::vector<CreateGroupFunction> getTrustGrps(const json& obj)
62{
63 std::vector<CreateGroupFunction> grpFuncs;
64
65 if (obj.contains("sensor_trust_groups"))
66 {
67 for (auto& stg : obj["sensor_trust_groups"])
68 {
69 if (!stg.contains("class") || !stg.contains("group"))
70 {
71 // Log error on missing required parameters
72 log<level::ERR>(
73 "Missing required fan monitor trust group parameters",
74 entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
75 throw std::runtime_error(
76 "Missing required fan trust group parameters");
77 }
78 auto tgClass = stg["class"].get<std::string>();
79 std::vector<trust::GroupDefinition> group;
80 for (auto& member : stg["group"])
81 {
82 // Construct list of group members
83 if (!member.contains("name"))
84 {
85 // Log error on missing required parameter
86 log<level::ERR>(
87 "Missing required fan monitor trust group member name",
88 entry("CLASS=%s", tgClass.c_str()));
89 throw std::runtime_error(
90 "Missing required fan monitor trust group member name");
91 }
92 auto in_trust = true;
93 if (member.contains("in_trust"))
94 {
95 in_trust = member["in_trust"].get<bool>();
96 }
97 group.emplace_back(trust::GroupDefinition{
98 member["name"].get<std::string>(), in_trust});
99 }
100 // The class for fan sensor trust groups
101 // (Must have a supported function within the tClass namespace)
102 std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(),
103 tolower);
104 auto handler = trusts.find(tgClass);
105 if (handler != trusts.end())
106 {
107 // Call function for trust group class
108 grpFuncs.emplace_back(handler->second(group));
109 }
110 else
111 {
112 // Log error on unsupported trust group class
113 log<level::ERR>("Invalid fan monitor trust group class",
114 entry("CLASS=%s", tgClass.c_str()));
115 throw std::runtime_error(
116 "Invalid fan monitor trust group class");
117 }
118 }
119 }
120
121 return grpFuncs;
122}
123
Matthew Barth22ab93b2020-06-08 10:47:56 -0500124const std::vector<SensorDefinition> getSensorDefs(const json& sensors)
125{
126 std::vector<SensorDefinition> sensorDefs;
127
128 for (const auto& sensor : sensors)
129 {
130 if (!sensor.contains("name") || !sensor.contains("has_target"))
131 {
132 // Log error on missing required parameters
133 log<level::ERR>(
134 "Missing required fan sensor definition parameters",
135 entry("REQUIRED_PARAMETERS=%s", "{name, has_target}"));
136 throw std::runtime_error(
137 "Missing required fan sensor definition parameters");
138 }
139 // Target interface is optional and defaults to
140 // 'xyz.openbmc_project.Control.FanSpeed'
141 std::string targetIntf = "xyz.openbmc_project.Control.FanSpeed";
142 if (sensor.contains("target_interface"))
143 {
144 targetIntf = sensor["target_interface"].get<std::string>();
145 }
146 // Factor is optional and defaults to 1
147 auto factor = 1.0;
148 if (sensor.contains("factor"))
149 {
150 factor = sensor["factor"].get<double>();
151 }
152 // Offset is optional and defaults to 0
153 auto offset = 0;
154 if (sensor.contains("offset"))
155 {
156 offset = sensor["offset"].get<int64_t>();
157 }
158
159 sensorDefs.emplace_back(std::tuple(sensor["name"].get<std::string>(),
160 sensor["has_target"].get<bool>(),
161 targetIntf, factor, offset));
162 }
163
164 return sensorDefs;
165}
166
167const std::vector<FanDefinition> getFanDefs(const json& obj)
168{
169 std::vector<FanDefinition> fanDefs;
170
171 for (const auto& fan : obj["fans"])
172 {
173 if (!fan.contains("inventory") ||
174 !fan.contains("allowed_out_of_range_time") ||
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500175 !fan.contains("deviation") || !fan.contains("sensors"))
Matthew Barth22ab93b2020-06-08 10:47:56 -0500176 {
177 // Log error on missing required parameters
178 log<level::ERR>(
179 "Missing required fan monitor definition parameters",
180 entry("REQUIRED_PARAMETERS=%s",
181 "{inventory, allowed_out_of_range_time, deviation, "
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500182 "sensors}"));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500183 throw std::runtime_error(
184 "Missing required fan monitor definition parameters");
185 }
186 // Construct the sensor definitions for this fan
187 auto sensorDefs = getSensorDefs(fan["sensors"]);
188
189 // Functional delay is optional and defaults to 0
190 size_t funcDelay = 0;
191 if (fan.contains("functional_delay"))
192 {
193 funcDelay = fan["functional_delay"].get<size_t>();
194 }
195
Matt Spinlerb0412d02020-10-12 16:53:52 -0500196 // Monitor start delay is optional and defaults to 0
197 size_t monitorDelay = 0;
198 if (fan.contains("monitor_start_delay"))
199 {
200 monitorDelay = fan["monitor_start_delay"].get<size_t>();
201 }
202
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500203 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults
204 // to zero if not present, meaning the code will not set the
205 // parent fan to nonfunctional based on sensors.
206 size_t nonfuncSensorsCount = 0;
207 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc"))
208 {
209 nonfuncSensorsCount =
210 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>();
211 }
212
Matthew Barth3ad14342020-06-08 16:17:42 -0500213 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500214 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500215 if (fan.contains("condition"))
216 {
217 if (!fan["condition"].contains("name"))
218 {
219 // Log error on missing required parameter
220 log<level::ERR>(
221 "Missing required fan monitor condition parameter",
222 entry("REQUIRED_PARAMETER=%s", "{name}"));
223 throw std::runtime_error(
224 "Missing required fan monitor condition parameter");
225 }
226 auto name = fan["condition"]["name"].get<std::string>();
227 // The function for fan monitoring condition
228 // (Must have a supported function within the condition namespace)
229 std::transform(name.begin(), name.end(), name.begin(), tolower);
230 auto handler = conditions.find(name);
231 if (handler != conditions.end())
232 {
233 cond = handler->second(fan["condition"]);
234 }
235 else
236 {
237 log<level::INFO>(
238 "No handler found for configured condition",
239 entry("CONDITION_NAME=%s", name.c_str()),
240 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
241 }
242 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500243 fanDefs.emplace_back(
244 std::tuple(fan["inventory"].get<std::string>(), funcDelay,
245 fan["allowed_out_of_range_time"].get<size_t>(),
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500246 fan["deviation"].get<size_t>(), nonfuncSensorsCount,
Matt Spinlerb0412d02020-10-12 16:53:52 -0500247 monitorDelay, sensorDefs, cond));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500248 }
249
250 return fanDefs;
251}
252
Matt Spinlerf06ab072020-10-14 12:58:22 -0500253PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
254{
255 // The state is optional and defaults to runtime
256 PowerRuleState ruleState{PowerRuleState::runtime};
257
258 if (powerOffConfig.contains("state"))
259 {
260 auto state = powerOffConfig.at("state").get<std::string>();
261 if (state == "at_pgood")
262 {
263 ruleState = PowerRuleState::atPgood;
264 }
265 else if (state != "runtime")
266 {
267 auto msg = fmt::format("Invalid power off state entry {}", state);
268 log<level::ERR>(msg.c_str());
269 throw std::runtime_error(msg.c_str());
270 }
271 }
272
273 return ruleState;
274}
275
276std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
277{
278 std::unique_ptr<PowerOffCause> cause;
279
280 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
281 {
282 const auto msg =
283 "Missing 'count' or 'cause' entries in power off config";
284 log<level::ERR>(msg);
285 throw std::runtime_error(msg);
286 }
287
288 auto count = powerOffConfig.at("count").get<size_t>();
289 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
290
291 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
292 causes{
293 {"missing_fan_frus",
294 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
295 {"nonfunc_fan_rotors", [count]() {
296 return std::make_unique<NonfuncFanRotorCause>(count);
297 }}};
298
299 auto it = causes.find(powerOffCause);
300 if (it != causes.end())
301 {
302 cause = it->second();
303 }
304 else
305 {
306 auto msg =
307 fmt::format("Invalid power off cause {} in power off config JSON",
308 powerOffCause);
309 log<level::ERR>(msg.c_str());
310 throw std::runtime_error(msg.c_str());
311 }
312
313 return cause;
314}
315
316std::unique_ptr<PowerOffAction>
317 getPowerOffAction(const json& powerOffConfig,
318 std::shared_ptr<PowerInterfaceBase>& powerInterface)
319{
320 std::unique_ptr<PowerOffAction> action;
321 if (!powerOffConfig.contains("type"))
322 {
323 const auto msg = "Missing 'type' entry in power off config";
324 log<level::ERR>(msg);
325 throw std::runtime_error(msg);
326 }
327
328 auto type = powerOffConfig.at("type").get<std::string>();
329
330 if (((type == "hard") || (type == "soft")) &&
331 !powerOffConfig.contains("delay"))
332 {
333 const auto msg = "Missing 'delay' entry in power off config";
334 log<level::ERR>(msg);
335 throw std::runtime_error(msg);
336 }
337 else if ((type == "epow") &&
338 (!powerOffConfig.contains("service_mode_delay") ||
339 !powerOffConfig.contains("meltdown_delay")))
340 {
341 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
342 "entry in power off config";
343 log<level::ERR>(msg);
344 throw std::runtime_error(msg);
345 }
346
347 if (type == "hard")
348 {
349 action = std::make_unique<HardPowerOff>(
350 powerOffConfig.at("delay").get<uint32_t>(), powerInterface);
351 }
352 else if (type == "soft")
353 {
354 action = std::make_unique<SoftPowerOff>(
355 powerOffConfig.at("delay").get<uint32_t>(), powerInterface);
356 }
357 else if (type == "epow")
358 {
359 action = std::make_unique<EpowPowerOff>(
360 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
361 powerOffConfig.at("meltdown_delay").get<uint32_t>(),
362 powerInterface);
363 }
364 else
365 {
366 auto msg =
367 fmt::format("Invalid 'type' entry {} in power off config", type);
368 log<level::ERR>(msg.c_str());
369 throw std::runtime_error(msg.c_str());
370 }
371
372 return action;
373}
374
375std::vector<std::unique_ptr<PowerOffRule>>
376 getPowerOffRules(const json& obj,
377 std::shared_ptr<PowerInterfaceBase>& powerInterface)
378{
379 std::vector<std::unique_ptr<PowerOffRule>> rules;
380
381 if (!(obj.contains("fault_handling") &&
382 obj.at("fault_handling").contains("power_off_config")))
383 {
384 return rules;
385 }
386
387 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
388 {
389 auto state = getPowerOffPowerRuleState(config);
390 auto cause = getPowerOffCause(config);
391 auto action = getPowerOffAction(config, powerInterface);
392
393 auto rule = std::make_unique<PowerOffRule>(
394 std::move(state), std::move(cause), std::move(action));
395 rules.push_back(std::move(rule));
396 }
397
398 return rules;
399}
400
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500401} // namespace phosphor::fan::monitor