blob: 61c8e80336bee6c4553d063f7cdc0ae595ab32a0 [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
18#include "json_config.hpp"
19#include "nonzero_speed_trust.hpp"
20#include "types.hpp"
21
22#include <nlohmann/json.hpp>
23#include <phosphor-logging/log.hpp>
24
25#include <algorithm>
26#include <map>
27#include <memory>
28#include <vector>
29
30namespace phosphor::fan::monitor
31{
32
33using json = nlohmann::json;
34using namespace phosphor::logging;
35
36namespace tClass
37{
38
39// Get a constructed trust group class for a non-zero speed group
40CreateGroupFunction
41 getNonZeroSpeed(const std::vector<trust::GroupDefinition>& group)
42{
43 return [group]() {
44 return std::make_unique<trust::NonzeroSpeed>(std::move(group));
45 };
46}
47
48} // namespace tClass
49
50const std::map<std::string, trustHandler> trusts = {
51 {"nonzerospeed", tClass::getNonZeroSpeed}};
52
53const std::vector<CreateGroupFunction> getTrustGrps(const json& obj)
54{
55 std::vector<CreateGroupFunction> grpFuncs;
56
57 if (obj.contains("sensor_trust_groups"))
58 {
59 for (auto& stg : obj["sensor_trust_groups"])
60 {
61 if (!stg.contains("class") || !stg.contains("group"))
62 {
63 // Log error on missing required parameters
64 log<level::ERR>(
65 "Missing required fan monitor trust group parameters",
66 entry("REQUIRED_PARAMETERS=%s", "{class, group}"));
67 throw std::runtime_error(
68 "Missing required fan trust group parameters");
69 }
70 auto tgClass = stg["class"].get<std::string>();
71 std::vector<trust::GroupDefinition> group;
72 for (auto& member : stg["group"])
73 {
74 // Construct list of group members
75 if (!member.contains("name"))
76 {
77 // Log error on missing required parameter
78 log<level::ERR>(
79 "Missing required fan monitor trust group member name",
80 entry("CLASS=%s", tgClass.c_str()));
81 throw std::runtime_error(
82 "Missing required fan monitor trust group member name");
83 }
84 auto in_trust = true;
85 if (member.contains("in_trust"))
86 {
87 in_trust = member["in_trust"].get<bool>();
88 }
89 group.emplace_back(trust::GroupDefinition{
90 member["name"].get<std::string>(), in_trust});
91 }
92 // The class for fan sensor trust groups
93 // (Must have a supported function within the tClass namespace)
94 std::transform(tgClass.begin(), tgClass.end(), tgClass.begin(),
95 tolower);
96 auto handler = trusts.find(tgClass);
97 if (handler != trusts.end())
98 {
99 // Call function for trust group class
100 grpFuncs.emplace_back(handler->second(group));
101 }
102 else
103 {
104 // Log error on unsupported trust group class
105 log<level::ERR>("Invalid fan monitor trust group class",
106 entry("CLASS=%s", tgClass.c_str()));
107 throw std::runtime_error(
108 "Invalid fan monitor trust group class");
109 }
110 }
111 }
112
113 return grpFuncs;
114}
115
116} // namespace phosphor::fan::monitor