blob: 160b725f3815f1be1e66a3c9b96fd59c2837cca8 [file] [log] [blame]
Matthew Barthe8441c62021-05-13 16:50:56 -05001/**
2 * Copyright © 2021 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 "init.hpp"
17
18#include "../manager.hpp"
19#include "action.hpp"
20#include "group.hpp"
21#include "sdbusplus.hpp"
22
23#include <fmt/format.h>
24
25#include <nlohmann/json.hpp>
26#include <phosphor-logging/log.hpp>
27
28#include <algorithm>
29#include <iterator>
30#include <memory>
31#include <numeric>
32#include <utility>
33#include <vector>
34
35namespace phosphor::fan::control::json::trigger::init
36{
37
38using json = nlohmann::json;
39using namespace phosphor::logging;
40
41void getProperties(Manager* mgr, const Group& group)
42{
43 for (const auto& member : group.getMembers())
44 {
45 try
46 {
47 // Check if property already cached
48 auto value = mgr->getProperty(member, group.getInterface(),
49 group.getProperty());
50 if (value == std::nullopt)
51 {
52 // Property not in cache, attempt to add it
53 mgr->addObjects(member, group.getInterface(),
54 group.getProperty());
55 }
56 }
57 catch (const util::DBusMethodError& dme)
58 {
59 // Configured dbus object does not exist on dbus yet?
60 // TODO How to handle this? Create timer to keep checking for
61 // object/service to appear? When to stop checking?
62 }
63 }
64}
65
66void nameHasOwner(Manager* mgr, const Group& group)
67{
68 std::string lastName = "";
69 for (const auto& member : group.getMembers())
70 {
71 std::string servName = "";
72 auto intf = group.getInterface();
73 try
74 {
75 servName = mgr->getService(member, intf);
76 if (!servName.empty() && lastName != servName)
77 {
78 // Member not provided by same service as last group member
79 lastName = servName;
80 auto hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
81 mgr->getBus(), "org.freedesktop.DBus",
82 "/org/freedesktop/DBus", "org.freedesktop.DBus",
83 "NameHasOwner", servName);
84 // Update service name owner state of group object
85 mgr->setOwner(member, servName, intf, hasOwner);
86 }
87 if (servName.empty())
88 {
89 // Path and/or interface configured does not exist on dbus?
90 // TODO How to handle this? Create timer to keep checking for
91 // object/service to appear? When to stop checking?
92 log<level::ERR>(
93 fmt::format(
94 "Unable to get service name for path {}, interface {}",
95 member, intf)
96 .c_str());
97 }
98 }
99 catch (const util::DBusMethodError& dme)
100 {
101 if (!servName.empty())
102 {
103 // Failed to get service name owner state
104 mgr->setOwner(member, servName, intf, false);
105 }
106 else
107 {
108 // Path and/or interface configured does not exist on dbus?
109 // TODO How to handle this? Create timer to keep checking for
110 // object/service to appear? When to stop checking?
111 log<level::ERR>(
112 fmt::format(
113 "Unable to get service name for path {}, interface {}",
114 member, intf)
115 .c_str());
116 throw dme;
117 }
118 }
119 }
120}
121
122void triggerInit(const json& jsonObj, const std::string& eventName,
123 Manager* mgr,
124 std::vector<std::unique_ptr<ActionBase>>& actions)
125{
126 // Get the method handler if configured
127 auto handler = methods.end();
128 if (jsonObj.contains("method"))
129 {
130 auto method = jsonObj["method"].get<std::string>();
131 std::transform(method.begin(), method.end(), method.begin(), tolower);
132 handler = methods.find(method);
133 }
134
135 for (auto& action : actions)
136 {
137 // Groups are optional, so a method is only required if there are groups
138 // i.e.) An init triggered event without any groups results in just
139 // running the actions
140 for (const auto& group : action->getGroups())
141 {
142 if (handler == methods.end())
143 {
144 // Construct list of available methods
145 auto availMethods = std::accumulate(
146 std::next(methods.begin()), methods.end(),
147 methods.begin()->first, [](auto list, auto method) {
148 return std::move(list) + ", " + method.first;
149 });
150 auto msg =
151 fmt::format("Event '{}' requires a supported method given "
152 "to be init driven, available methods: {}",
153 eventName, availMethods);
154 log<level::ERR>(msg.c_str());
155 throw std::runtime_error(msg.c_str());
156 }
157 // Call method handler for each group in the actions
158 handler->second(mgr, group);
159 }
160 // Run the action
161 action->run();
162 }
163}
164
165} // namespace phosphor::fan::control::json::trigger::init