blob: 82fc621c753153fa5fb2353547cb1dd35f6b8550 [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"
Jolie Ku69f2f482020-10-21 09:59:43 +080023#include "tach_sensor.hpp"
Matthew Barth9ea8bee2020-06-04 14:27:19 -050024#include "types.hpp"
25
Matt Spinlerf06ab072020-10-14 12:58:22 -050026#include <fmt/format.h>
27
Matthew Barth9ea8bee2020-06-04 14:27:19 -050028#include <nlohmann/json.hpp>
29#include <phosphor-logging/log.hpp>
30
31#include <algorithm>
32#include <map>
33#include <memory>
Matthew Barth22ab93b2020-06-08 10:47:56 -050034#include <optional>
Matthew Barth9ea8bee2020-06-04 14:27:19 -050035#include <vector>
36
37namespace phosphor::fan::monitor
38{
39
40using json = nlohmann::json;
41using namespace phosphor::logging;
42
43namespace tClass
44{
45
46// Get a constructed trust group class for a non-zero speed group
47CreateGroupFunction
48 getNonZeroSpeed(const std::vector<trust::GroupDefinition>& group)
49{
50 return [group]() {
51 return std::make_unique<trust::NonzeroSpeed>(std::move(group));
52 };
53}
54
55} // namespace tClass
56
57const std::map<std::string, trustHandler> trusts = {
58 {"nonzerospeed", tClass::getNonZeroSpeed}};
Matthew Barth3ad14342020-06-08 16:17:42 -050059const std::map<std::string, condHandler> conditions = {
60 {"propertiesmatch", condition::getPropertiesMatch}};
Jolie Ku69f2f482020-10-21 09:59:43 +080061const std::map<std::string, size_t> methods = {
62 {"timebased", MethodMode::timebased}, {"count", MethodMode::count}};
Matthew Barth9ea8bee2020-06-04 14:27:19 -050063
64const std::vector<CreateGroupFunction> getTrustGrps(const json& obj)
65{
66 std::vector<CreateGroupFunction> grpFuncs;
67
68 if (obj.contains("sensor_trust_groups"))
69 {
70 for (auto& stg : obj["sensor_trust_groups"])
71 {
72 if (!stg.contains("class") || !stg.contains("group"))
73 {
74 // Log error on missing required parameters
75 log<level::ERR>(
76 "Missing required fan monitor trust group parameters",
77 entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
78 throw std::runtime_error(
79 "Missing required fan trust group parameters");
80 }
81 auto tgClass = stg["class"].get<std::string>();
82 std::vector<trust::GroupDefinition> group;
83 for (auto& member : stg["group"])
84 {
85 // Construct list of group members
86 if (!member.contains("name"))
87 {
88 // Log error on missing required parameter
89 log<level::ERR>(
90 "Missing required fan monitor trust group member name",
91 entry("CLASS=%s", tgClass.c_str()));
92 throw std::runtime_error(
93 "Missing required fan monitor trust group member name");
94 }
95 auto in_trust = true;
96 if (member.contains("in_trust"))
97 {
98 in_trust = member["in_trust"].get<bool>();
99 }
100 group.emplace_back(trust::GroupDefinition{
101 member["name"].get<std::string>(), in_trust});
102 }
103 // The class for fan sensor trust groups
104 // (Must have a supported function within the tClass namespace)
105 std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(),
106 tolower);
107 auto handler = trusts.find(tgClass);
108 if (handler != trusts.end())
109 {
110 // Call function for trust group class
111 grpFuncs.emplace_back(handler->second(group));
112 }
113 else
114 {
115 // Log error on unsupported trust group class
116 log<level::ERR>("Invalid fan monitor trust group class",
117 entry("CLASS=%s", tgClass.c_str()));
118 throw std::runtime_error(
119 "Invalid fan monitor trust group class");
120 }
121 }
122 }
123
124 return grpFuncs;
125}
126
Matthew Barth22ab93b2020-06-08 10:47:56 -0500127const std::vector<SensorDefinition> getSensorDefs(const json& sensors)
128{
129 std::vector<SensorDefinition> sensorDefs;
130
131 for (const auto& sensor : sensors)
132 {
133 if (!sensor.contains("name") || !sensor.contains("has_target"))
134 {
135 // Log error on missing required parameters
136 log<level::ERR>(
137 "Missing required fan sensor definition parameters",
138 entry("REQUIRED_PARAMETERS=%s", "{name, has_target}"));
139 throw std::runtime_error(
140 "Missing required fan sensor definition parameters");
141 }
142 // Target interface is optional and defaults to
143 // 'xyz.openbmc_project.Control.FanSpeed'
144 std::string targetIntf = "xyz.openbmc_project.Control.FanSpeed";
145 if (sensor.contains("target_interface"))
146 {
147 targetIntf = sensor["target_interface"].get<std::string>();
148 }
149 // Factor is optional and defaults to 1
150 auto factor = 1.0;
151 if (sensor.contains("factor"))
152 {
153 factor = sensor["factor"].get<double>();
154 }
155 // Offset is optional and defaults to 0
156 auto offset = 0;
157 if (sensor.contains("offset"))
158 {
159 offset = sensor["offset"].get<int64_t>();
160 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800161 // Threshold is optional and defaults to 1
162 auto threshold = 1;
163 if (sensor.contains("threshold"))
164 {
165 threshold = sensor["threshold"].get<size_t>();
166 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500167
Jolie Ku69f2f482020-10-21 09:59:43 +0800168 sensorDefs.emplace_back(std::tuple(
169 sensor["name"].get<std::string>(), sensor["has_target"].get<bool>(),
170 targetIntf, factor, offset, threshold));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500171 }
172
173 return sensorDefs;
174}
175
176const std::vector<FanDefinition> getFanDefs(const json& obj)
177{
178 std::vector<FanDefinition> fanDefs;
179
180 for (const auto& fan : obj["fans"])
181 {
Jolie Ku69f2f482020-10-21 09:59:43 +0800182 if (!fan.contains("inventory") || !fan.contains("deviation") ||
183 !fan.contains("sensors"))
Matthew Barth22ab93b2020-06-08 10:47:56 -0500184 {
185 // Log error on missing required parameters
186 log<level::ERR>(
187 "Missing required fan monitor definition parameters",
188 entry("REQUIRED_PARAMETERS=%s",
Jolie Ku69f2f482020-10-21 09:59:43 +0800189 "{inventory, deviation, sensors}"));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500190 throw std::runtime_error(
191 "Missing required fan monitor definition parameters");
192 }
193 // Construct the sensor definitions for this fan
194 auto sensorDefs = getSensorDefs(fan["sensors"]);
195
196 // Functional delay is optional and defaults to 0
197 size_t funcDelay = 0;
198 if (fan.contains("functional_delay"))
199 {
200 funcDelay = fan["functional_delay"].get<size_t>();
201 }
202
Jolie Ku69f2f482020-10-21 09:59:43 +0800203 // Method is optional and defaults to time based functional
204 // determination
205 size_t method = MethodMode::timebased;
206 if (fan.contains("method"))
207 {
208 auto methodConf = fan["method"].get<std::string>();
209 auto methodFunc = methods.find(methodConf);
210 if (methodFunc != methods.end())
211 {
212 method = methodFunc->second;
213 }
214 else
215 {
216 // Log error on unsupported method parameter
217 log<level::ERR>("Invalid fan method");
218 throw std::runtime_error("Invalid fan method");
219 }
220 }
221
222 // Timeout defaults to 0
223 size_t timeout = 0;
224 if (method == MethodMode::timebased)
225 {
226 if (!fan.contains("allowed_out_of_range_time"))
227 {
228 // Log error on missing required parameter
229 log<level::ERR>(
230 "Missing required fan monitor definition parameters",
231 entry("REQUIRED_PARAMETER=%s",
232 "{allowed_out_of_range_time}"));
233 throw std::runtime_error(
234 "Missing required fan monitor definition parameters");
235 }
236 else
237 {
238 timeout = fan["allowed_out_of_range_time"].get<size_t>();
239 }
240 }
241
Matt Spinlerb0412d02020-10-12 16:53:52 -0500242 // Monitor start delay is optional and defaults to 0
243 size_t monitorDelay = 0;
244 if (fan.contains("monitor_start_delay"))
245 {
246 monitorDelay = fan["monitor_start_delay"].get<size_t>();
247 }
248
Matt Spinlerae1f8ef2020-10-14 16:15:51 -0500249 // num_sensors_nonfunc_for_fan_nonfunc is optional and defaults
250 // to zero if not present, meaning the code will not set the
251 // parent fan to nonfunctional based on sensors.
252 size_t nonfuncSensorsCount = 0;
253 if (fan.contains("num_sensors_nonfunc_for_fan_nonfunc"))
254 {
255 nonfuncSensorsCount =
256 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>();
257 }
258
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500259 // nonfunc_rotor_error_delay is optional, though it will
260 // default to zero if 'fault_handling' is present.
261 std::optional<size_t> nonfuncRotorErrorDelay;
262 if (fan.contains("nonfunc_rotor_error_delay"))
263 {
264 nonfuncRotorErrorDelay =
265 fan["nonfunc_rotor_error_delay"].get<size_t>();
266 }
267 else if (obj.contains("fault_handling"))
268 {
269 nonfuncRotorErrorDelay = 0;
270 }
271
Matt Spinler27f6b682020-10-27 08:43:37 -0500272 // fan_missing_error_delay is optional.
273 std::optional<size_t> fanMissingErrorDelay;
274 if (fan.contains("fan_missing_error_delay"))
275 {
276 fanMissingErrorDelay =
277 fan.at("fan_missing_error_delay").get<size_t>();
278 }
279
Matthew Barth3ad14342020-06-08 16:17:42 -0500280 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500281 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500282 if (fan.contains("condition"))
283 {
284 if (!fan["condition"].contains("name"))
285 {
286 // Log error on missing required parameter
287 log<level::ERR>(
288 "Missing required fan monitor condition parameter",
289 entry("REQUIRED_PARAMETER=%s", "{name}"));
290 throw std::runtime_error(
291 "Missing required fan monitor condition parameter");
292 }
293 auto name = fan["condition"]["name"].get<std::string>();
294 // The function for fan monitoring condition
295 // (Must have a supported function within the condition namespace)
296 std::transform(name.begin(), name.end(), name.begin(), tolower);
297 auto handler = conditions.find(name);
298 if (handler != conditions.end())
299 {
300 cond = handler->second(fan["condition"]);
301 }
302 else
303 {
304 log<level::INFO>(
305 "No handler found for configured condition",
306 entry("CONDITION_NAME=%s", name.c_str()),
307 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
308 }
309 }
Jolie Ku69f2f482020-10-21 09:59:43 +0800310
Matt Spinler27f6b682020-10-27 08:43:37 -0500311 fanDefs.emplace_back(std::tuple(
Jolie Ku69f2f482020-10-21 09:59:43 +0800312 fan["inventory"].get<std::string>(), method, funcDelay, timeout,
Matt Spinler27f6b682020-10-27 08:43:37 -0500313 fan["deviation"].get<size_t>(), nonfuncSensorsCount, monitorDelay,
314 nonfuncRotorErrorDelay, fanMissingErrorDelay, sensorDefs, cond));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500315 }
316
317 return fanDefs;
318}
319
Matt Spinlerf06ab072020-10-14 12:58:22 -0500320PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
321{
322 // The state is optional and defaults to runtime
323 PowerRuleState ruleState{PowerRuleState::runtime};
324
325 if (powerOffConfig.contains("state"))
326 {
327 auto state = powerOffConfig.at("state").get<std::string>();
328 if (state == "at_pgood")
329 {
330 ruleState = PowerRuleState::atPgood;
331 }
332 else if (state != "runtime")
333 {
334 auto msg = fmt::format("Invalid power off state entry {}", state);
335 log<level::ERR>(msg.c_str());
336 throw std::runtime_error(msg.c_str());
337 }
338 }
339
340 return ruleState;
341}
342
343std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
344{
345 std::unique_ptr<PowerOffCause> cause;
346
347 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
348 {
349 const auto msg =
350 "Missing 'count' or 'cause' entries in power off config";
351 log<level::ERR>(msg);
352 throw std::runtime_error(msg);
353 }
354
355 auto count = powerOffConfig.at("count").get<size_t>();
356 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
357
358 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
359 causes{
360 {"missing_fan_frus",
361 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
362 {"nonfunc_fan_rotors", [count]() {
363 return std::make_unique<NonfuncFanRotorCause>(count);
364 }}};
365
366 auto it = causes.find(powerOffCause);
367 if (it != causes.end())
368 {
369 cause = it->second();
370 }
371 else
372 {
373 auto msg =
374 fmt::format("Invalid power off cause {} in power off config JSON",
375 powerOffCause);
376 log<level::ERR>(msg.c_str());
377 throw std::runtime_error(msg.c_str());
378 }
379
380 return cause;
381}
382
383std::unique_ptr<PowerOffAction>
384 getPowerOffAction(const json& powerOffConfig,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500385 std::shared_ptr<PowerInterfaceBase>& powerInterface,
386 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500387{
388 std::unique_ptr<PowerOffAction> action;
389 if (!powerOffConfig.contains("type"))
390 {
391 const auto msg = "Missing 'type' entry in power off config";
392 log<level::ERR>(msg);
393 throw std::runtime_error(msg);
394 }
395
396 auto type = powerOffConfig.at("type").get<std::string>();
397
398 if (((type == "hard") || (type == "soft")) &&
399 !powerOffConfig.contains("delay"))
400 {
401 const auto msg = "Missing 'delay' entry in power off config";
402 log<level::ERR>(msg);
403 throw std::runtime_error(msg);
404 }
405 else if ((type == "epow") &&
406 (!powerOffConfig.contains("service_mode_delay") ||
407 !powerOffConfig.contains("meltdown_delay")))
408 {
409 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
410 "entry in power off config";
411 log<level::ERR>(msg);
412 throw std::runtime_error(msg);
413 }
414
415 if (type == "hard")
416 {
417 action = std::make_unique<HardPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500418 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500419 }
420 else if (type == "soft")
421 {
422 action = std::make_unique<SoftPowerOff>(
Matt Spinlerac1efc12020-10-27 10:20:11 -0500423 powerOffConfig.at("delay").get<uint32_t>(), powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500424 }
425 else if (type == "epow")
426 {
427 action = std::make_unique<EpowPowerOff>(
428 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
Matt Spinlerac1efc12020-10-27 10:20:11 -0500429 powerOffConfig.at("meltdown_delay").get<uint32_t>(), powerInterface,
430 func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500431 }
432 else
433 {
434 auto msg =
435 fmt::format("Invalid 'type' entry {} in power off config", type);
436 log<level::ERR>(msg.c_str());
437 throw std::runtime_error(msg.c_str());
438 }
439
440 return action;
441}
442
443std::vector<std::unique_ptr<PowerOffRule>>
444 getPowerOffRules(const json& obj,
Matt Spinlerac1efc12020-10-27 10:20:11 -0500445 std::shared_ptr<PowerInterfaceBase>& powerInterface,
446 PowerOffAction::PrePowerOffFunc& func)
Matt Spinlerf06ab072020-10-14 12:58:22 -0500447{
448 std::vector<std::unique_ptr<PowerOffRule>> rules;
449
450 if (!(obj.contains("fault_handling") &&
451 obj.at("fault_handling").contains("power_off_config")))
452 {
453 return rules;
454 }
455
456 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
457 {
458 auto state = getPowerOffPowerRuleState(config);
459 auto cause = getPowerOffCause(config);
Matt Spinlerac1efc12020-10-27 10:20:11 -0500460 auto action = getPowerOffAction(config, powerInterface, func);
Matt Spinlerf06ab072020-10-14 12:58:22 -0500461
462 auto rule = std::make_unique<PowerOffRule>(
463 std::move(state), std::move(cause), std::move(action));
464 rules.push_back(std::move(rule));
465 }
466
467 return rules;
468}
469
Matt Spinlerf13b42e2020-10-26 15:29:49 -0500470std::optional<size_t> getNumNonfuncRotorsBeforeError(const json& obj)
471{
472 std::optional<size_t> num;
473
474 if (obj.contains("fault_handling"))
475 {
476 // Defaults to 1 if not present inside of 'fault_handling'.
477 num = obj.at("fault_handling")
478 .value("num_nonfunc_rotors_before_error", 1);
479 }
480
481 return num;
482}
483
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500484} // namespace phosphor::fan::monitor