blob: 3d9a5351ebb545b1fa61eeaf66ad33fe3d3e360b [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
Matthew Barthe8441c62021-05-13 16:50:56 -050024#include <nlohmann/json.hpp>
25#include <phosphor-logging/log.hpp>
26
27#include <algorithm>
Patrick Williamsfbf47032023-07-17 12:27:34 -050028#include <format>
Matthew Barthe8441c62021-05-13 16:50:56 -050029#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(),
Matt Spinler9ac325c2022-04-25 14:13:49 -050054 group.getProperty(), group.getService());
Matt Spinler1f409872022-04-07 13:14:35 -050055
56 // If the service was predefined for the group, then we know
57 // all members are in the same service so the above addObjects
58 // call would have already added every present member in the
59 // group (assuming the service has an ObjectManager iface
60 // which it should). So no need to continue.
61 if (!group.getService().empty())
62 {
63 break;
64 }
Matthew Barthe8441c62021-05-13 16:50:56 -050065 }
66 }
Matt Spinlerf16f0632022-05-09 14:27:46 -050067 catch (const std::exception& e)
Matthew Barthe8441c62021-05-13 16:50:56 -050068 {
69 // Configured dbus object does not exist on dbus yet?
70 // TODO How to handle this? Create timer to keep checking for
71 // object/service to appear? When to stop checking?
72 }
73 }
74}
75
76void nameHasOwner(Manager* mgr, const Group& group)
77{
Matthew Barth7b7df2a2022-02-01 16:37:28 -060078 bool hasOwner = false;
Matthew Barthe8441c62021-05-13 16:50:56 -050079 std::string lastName = "";
80 for (const auto& member : group.getMembers())
81 {
82 std::string servName = "";
83 auto intf = group.getInterface();
84 try
85 {
Matthew Barth65f72812022-01-13 15:05:34 -060086 servName = group.getService();
87 if (servName.empty())
88 {
89 servName = mgr->getService(member, intf);
90 }
Matthew Barth7b7df2a2022-02-01 16:37:28 -060091 if (!servName.empty())
Matthew Barthe8441c62021-05-13 16:50:56 -050092 {
Matthew Barth7b7df2a2022-02-01 16:37:28 -060093 if (lastName != servName)
94 {
95 // Member not provided by same service as last group member
96 lastName = servName;
97 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
98 mgr->getBus(), "org.freedesktop.DBus",
99 "/org/freedesktop/DBus", "org.freedesktop.DBus",
100 "NameHasOwner", servName);
101 }
Matthew Barthe8441c62021-05-13 16:50:56 -0500102 // Update service name owner state of group object
103 mgr->setOwner(member, servName, intf, hasOwner);
104 }
Matthew Barth7b7df2a2022-02-01 16:37:28 -0600105 else
Matthew Barthe8441c62021-05-13 16:50:56 -0500106 {
107 // Path and/or interface configured does not exist on dbus?
108 // TODO How to handle this? Create timer to keep checking for
109 // object/service to appear? When to stop checking?
110 log<level::ERR>(
Patrick Williamsfbf47032023-07-17 12:27:34 -0500111 std::format(
Matthew Barthe8441c62021-05-13 16:50:56 -0500112 "Unable to get service name for path {}, interface {}",
113 member, intf)
114 .c_str());
115 }
116 }
117 catch (const util::DBusMethodError& dme)
118 {
119 if (!servName.empty())
120 {
121 // Failed to get service name owner state
Matthew Barth7b7df2a2022-02-01 16:37:28 -0600122 hasOwner = false;
123 mgr->setOwner(member, servName, intf, hasOwner);
Matthew Barthe8441c62021-05-13 16:50:56 -0500124 }
125 else
126 {
127 // Path and/or interface configured does not exist on dbus?
128 // TODO How to handle this? Create timer to keep checking for
129 // object/service to appear? When to stop checking?
Patrick Williamsfbf47032023-07-17 12:27:34 -0500130 log<level::ERR>(std::format("Unable to get service({}) owner "
Matthew Barth7b7df2a2022-02-01 16:37:28 -0600131 "state for path {}, interface {}",
132 servName, member, intf)
133 .c_str());
Matthew Barthe8441c62021-05-13 16:50:56 -0500134 throw dme;
135 }
136 }
137 }
138}
139
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400140enableTrigger triggerInit(const json& jsonObj, const std::string& /*eventName*/,
141 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
Matthew Barthe8441c62021-05-13 16:50:56 -0500142{
143 // Get the method handler if configured
144 auto handler = methods.end();
145 if (jsonObj.contains("method"))
146 {
147 auto method = jsonObj["method"].get<std::string>();
148 std::transform(method.begin(), method.end(), method.begin(), tolower);
149 handler = methods.find(method);
150 }
151
Matthew Barthc7f629d2021-09-30 15:58:30 -0500152 return [handler = std::move(handler)](
153 const std::string& eventName, Manager* mgr,
154 const std::vector<Group>& groups,
155 std::vector<std::unique_ptr<ActionBase>>& actions) {
156 // Event groups are optional, so a method is only required if there
157 // are event groups i.e.) An init triggered event without any event
158 // groups results in just running the actions
159 if (!groups.empty() && handler == methods.end())
Matthew Barthe8441c62021-05-13 16:50:56 -0500160 {
Matthew Barth54b5a242021-05-21 11:02:52 -0500161 // Construct list of available methods
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500162 auto availMethods = std::accumulate(
163 std::next(methods.begin()), methods.end(),
164 methods.begin()->first, [](auto list, auto method) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400165 return std::move(list) + ", " + method.first;
166 });
Matthew Barth54b5a242021-05-21 11:02:52 -0500167 auto msg =
Patrick Williamsfbf47032023-07-17 12:27:34 -0500168 std::format("Event '{}' requires a supported method given to "
Matthew Barth54b5a242021-05-21 11:02:52 -0500169 "be init driven, available methods: {}",
170 eventName, availMethods);
171 log<level::ERR>(msg.c_str());
172 throw std::runtime_error(msg.c_str());
Matthew Barthe8441c62021-05-13 16:50:56 -0500173 }
Matthew Barth54b5a242021-05-21 11:02:52 -0500174
Matthew Barthc7f629d2021-09-30 15:58:30 -0500175 for (const auto& group : groups)
176 {
177 // Call method handler for each group to populate cache
178 handler->second(mgr, group);
179 }
Matthew Barth54b5a242021-05-21 11:02:52 -0500180 for (auto& action : actions)
181 {
Matthew Barthc7f629d2021-09-30 15:58:30 -0500182 // Run each action after initializing all the groups
Matthew Barth54b5a242021-05-21 11:02:52 -0500183 action->run();
184 }
185 };
Matthew Barthe8441c62021-05-13 16:50:56 -0500186}
187
188} // namespace phosphor::fan::control::json::trigger::init