blob: bd239c6eed8eda27ce984012dfe122847f24c112 [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") ||
175 !fan.contains("deviation") ||
176 !fan.contains("num_sensors_nonfunc_for_fan_nonfunc") ||
177 !fan.contains("sensors"))
178 {
179 // Log error on missing required parameters
180 log<level::ERR>(
181 "Missing required fan monitor definition parameters",
182 entry("REQUIRED_PARAMETERS=%s",
183 "{inventory, allowed_out_of_range_time, deviation, "
184 "num_sensors_nonfunc_for_fan_nonfunc, sensors}"));
185 throw std::runtime_error(
186 "Missing required fan monitor definition parameters");
187 }
188 // Construct the sensor definitions for this fan
189 auto sensorDefs = getSensorDefs(fan["sensors"]);
190
191 // Functional delay is optional and defaults to 0
192 size_t funcDelay = 0;
193 if (fan.contains("functional_delay"))
194 {
195 funcDelay = fan["functional_delay"].get<size_t>();
196 }
197
Matt Spinlerb0412d02020-10-12 16:53:52 -0500198 // Monitor start delay is optional and defaults to 0
199 size_t monitorDelay = 0;
200 if (fan.contains("monitor_start_delay"))
201 {
202 monitorDelay = fan["monitor_start_delay"].get<size_t>();
203 }
204
Matthew Barth3ad14342020-06-08 16:17:42 -0500205 // Handle optional conditions
Matthew Barth8a0c2322020-06-17 09:53:10 -0500206 auto cond = std::optional<Condition>();
Matthew Barth3ad14342020-06-08 16:17:42 -0500207 if (fan.contains("condition"))
208 {
209 if (!fan["condition"].contains("name"))
210 {
211 // Log error on missing required parameter
212 log<level::ERR>(
213 "Missing required fan monitor condition parameter",
214 entry("REQUIRED_PARAMETER=%s", "{name}"));
215 throw std::runtime_error(
216 "Missing required fan monitor condition parameter");
217 }
218 auto name = fan["condition"]["name"].get<std::string>();
219 // The function for fan monitoring condition
220 // (Must have a supported function within the condition namespace)
221 std::transform(name.begin(), name.end(), name.begin(), tolower);
222 auto handler = conditions.find(name);
223 if (handler != conditions.end())
224 {
225 cond = handler->second(fan["condition"]);
226 }
227 else
228 {
229 log<level::INFO>(
230 "No handler found for configured condition",
231 entry("CONDITION_NAME=%s", name.c_str()),
232 entry("JSON_DUMP=%s", fan["condition"].dump().c_str()));
233 }
234 }
Matthew Barth22ab93b2020-06-08 10:47:56 -0500235 fanDefs.emplace_back(
236 std::tuple(fan["inventory"].get<std::string>(), funcDelay,
237 fan["allowed_out_of_range_time"].get<size_t>(),
238 fan["deviation"].get<size_t>(),
239 fan["num_sensors_nonfunc_for_fan_nonfunc"].get<size_t>(),
Matt Spinlerb0412d02020-10-12 16:53:52 -0500240 monitorDelay, sensorDefs, cond));
Matthew Barth22ab93b2020-06-08 10:47:56 -0500241 }
242
243 return fanDefs;
244}
245
Matt Spinlerf06ab072020-10-14 12:58:22 -0500246PowerRuleState getPowerOffPowerRuleState(const json& powerOffConfig)
247{
248 // The state is optional and defaults to runtime
249 PowerRuleState ruleState{PowerRuleState::runtime};
250
251 if (powerOffConfig.contains("state"))
252 {
253 auto state = powerOffConfig.at("state").get<std::string>();
254 if (state == "at_pgood")
255 {
256 ruleState = PowerRuleState::atPgood;
257 }
258 else if (state != "runtime")
259 {
260 auto msg = fmt::format("Invalid power off state entry {}", state);
261 log<level::ERR>(msg.c_str());
262 throw std::runtime_error(msg.c_str());
263 }
264 }
265
266 return ruleState;
267}
268
269std::unique_ptr<PowerOffCause> getPowerOffCause(const json& powerOffConfig)
270{
271 std::unique_ptr<PowerOffCause> cause;
272
273 if (!powerOffConfig.contains("count") || !powerOffConfig.contains("cause"))
274 {
275 const auto msg =
276 "Missing 'count' or 'cause' entries in power off config";
277 log<level::ERR>(msg);
278 throw std::runtime_error(msg);
279 }
280
281 auto count = powerOffConfig.at("count").get<size_t>();
282 auto powerOffCause = powerOffConfig.at("cause").get<std::string>();
283
284 const std::map<std::string, std::function<std::unique_ptr<PowerOffCause>()>>
285 causes{
286 {"missing_fan_frus",
287 [count]() { return std::make_unique<MissingFanFRUCause>(count); }},
288 {"nonfunc_fan_rotors", [count]() {
289 return std::make_unique<NonfuncFanRotorCause>(count);
290 }}};
291
292 auto it = causes.find(powerOffCause);
293 if (it != causes.end())
294 {
295 cause = it->second();
296 }
297 else
298 {
299 auto msg =
300 fmt::format("Invalid power off cause {} in power off config JSON",
301 powerOffCause);
302 log<level::ERR>(msg.c_str());
303 throw std::runtime_error(msg.c_str());
304 }
305
306 return cause;
307}
308
309std::unique_ptr<PowerOffAction>
310 getPowerOffAction(const json& powerOffConfig,
311 std::shared_ptr<PowerInterfaceBase>& powerInterface)
312{
313 std::unique_ptr<PowerOffAction> action;
314 if (!powerOffConfig.contains("type"))
315 {
316 const auto msg = "Missing 'type' entry in power off config";
317 log<level::ERR>(msg);
318 throw std::runtime_error(msg);
319 }
320
321 auto type = powerOffConfig.at("type").get<std::string>();
322
323 if (((type == "hard") || (type == "soft")) &&
324 !powerOffConfig.contains("delay"))
325 {
326 const auto msg = "Missing 'delay' entry in power off config";
327 log<level::ERR>(msg);
328 throw std::runtime_error(msg);
329 }
330 else if ((type == "epow") &&
331 (!powerOffConfig.contains("service_mode_delay") ||
332 !powerOffConfig.contains("meltdown_delay")))
333 {
334 const auto msg = "Missing 'service_mode_delay' or 'meltdown_delay' "
335 "entry in power off config";
336 log<level::ERR>(msg);
337 throw std::runtime_error(msg);
338 }
339
340 if (type == "hard")
341 {
342 action = std::make_unique<HardPowerOff>(
343 powerOffConfig.at("delay").get<uint32_t>(), powerInterface);
344 }
345 else if (type == "soft")
346 {
347 action = std::make_unique<SoftPowerOff>(
348 powerOffConfig.at("delay").get<uint32_t>(), powerInterface);
349 }
350 else if (type == "epow")
351 {
352 action = std::make_unique<EpowPowerOff>(
353 powerOffConfig.at("service_mode_delay").get<uint32_t>(),
354 powerOffConfig.at("meltdown_delay").get<uint32_t>(),
355 powerInterface);
356 }
357 else
358 {
359 auto msg =
360 fmt::format("Invalid 'type' entry {} in power off config", type);
361 log<level::ERR>(msg.c_str());
362 throw std::runtime_error(msg.c_str());
363 }
364
365 return action;
366}
367
368std::vector<std::unique_ptr<PowerOffRule>>
369 getPowerOffRules(const json& obj,
370 std::shared_ptr<PowerInterfaceBase>& powerInterface)
371{
372 std::vector<std::unique_ptr<PowerOffRule>> rules;
373
374 if (!(obj.contains("fault_handling") &&
375 obj.at("fault_handling").contains("power_off_config")))
376 {
377 return rules;
378 }
379
380 for (const auto& config : obj.at("fault_handling").at("power_off_config"))
381 {
382 auto state = getPowerOffPowerRuleState(config);
383 auto cause = getPowerOffCause(config);
384 auto action = getPowerOffAction(config, powerInterface);
385
386 auto rule = std::make_unique<PowerOffRule>(
387 std::move(state), std::move(cause), std::move(action));
388 rules.push_back(std::move(rule));
389 }
390
391 return rules;
392}
393
Matthew Barth9ea8bee2020-06-04 14:27:19 -0500394} // namespace phosphor::fan::monitor