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