blob: 4d24c939185cc9342bcc85cf0aabe8bd2a56c4e4 [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>
Anwaar Hadi64b5ac22025-04-04 23:54:53 +000025#include <phosphor-logging/lg2.hpp>
Matthew Barthe8441c62021-05-13 16:50:56 -050026
27#include <algorithm>
28#include <iterator>
29#include <memory>
30#include <numeric>
31#include <utility>
32#include <vector>
33
34namespace phosphor::fan::control::json::trigger::init
35{
36
37using json = nlohmann::json;
Matthew Barthe8441c62021-05-13 16:50:56 -050038
39void getProperties(Manager* mgr, const Group& group)
40{
41 for (const auto& member : group.getMembers())
42 {
43 try
44 {
45 // Check if property already cached
46 auto value = mgr->getProperty(member, group.getInterface(),
47 group.getProperty());
48 if (value == std::nullopt)
49 {
50 // Property not in cache, attempt to add it
51 mgr->addObjects(member, group.getInterface(),
Matt Spinler9ac325c2022-04-25 14:13:49 -050052 group.getProperty(), group.getService());
Matt Spinler1f409872022-04-07 13:14:35 -050053
54 // If the service was predefined for the group, then we know
55 // all members are in the same service so the above addObjects
56 // call would have already added every present member in the
57 // group (assuming the service has an ObjectManager iface
58 // which it should). So no need to continue.
59 if (!group.getService().empty())
60 {
61 break;
62 }
Matthew Barthe8441c62021-05-13 16:50:56 -050063 }
64 }
Matt Spinlerf16f0632022-05-09 14:27:46 -050065 catch (const std::exception& e)
Matthew Barthe8441c62021-05-13 16:50:56 -050066 {
67 // Configured dbus object does not exist on dbus yet?
68 // TODO How to handle this? Create timer to keep checking for
69 // object/service to appear? When to stop checking?
70 }
71 }
72}
73
74void nameHasOwner(Manager* mgr, const Group& group)
75{
Matthew Barth7b7df2a2022-02-01 16:37:28 -060076 bool hasOwner = false;
Matthew Barthe8441c62021-05-13 16:50:56 -050077 std::string lastName = "";
78 for (const auto& member : group.getMembers())
79 {
80 std::string servName = "";
81 auto intf = group.getInterface();
82 try
83 {
Matthew Barth65f72812022-01-13 15:05:34 -060084 servName = group.getService();
85 if (servName.empty())
86 {
87 servName = mgr->getService(member, intf);
88 }
Matthew Barth7b7df2a2022-02-01 16:37:28 -060089 if (!servName.empty())
Matthew Barthe8441c62021-05-13 16:50:56 -050090 {
Matthew Barth7b7df2a2022-02-01 16:37:28 -060091 if (lastName != servName)
92 {
93 // Member not provided by same service as last group member
94 lastName = servName;
95 hasOwner = util::SDBusPlus::callMethodAndRead<bool>(
96 mgr->getBus(), "org.freedesktop.DBus",
97 "/org/freedesktop/DBus", "org.freedesktop.DBus",
98 "NameHasOwner", servName);
99 }
Matthew Barthe8441c62021-05-13 16:50:56 -0500100 // Update service name owner state of group object
101 mgr->setOwner(member, servName, intf, hasOwner);
102 }
Matthew Barth7b7df2a2022-02-01 16:37:28 -0600103 else
Matthew Barthe8441c62021-05-13 16:50:56 -0500104 {
105 // Path and/or interface configured does not exist on dbus?
106 // TODO How to handle this? Create timer to keep checking for
107 // object/service to appear? When to stop checking?
Anwaar Hadi64b5ac22025-04-04 23:54:53 +0000108 lg2::error(
109 "Unable to get service name for path {MEMBER}, interface {GROUP_INTERFACE}",
110 "MEMBER", member, "GROUP_INTERFACE", intf);
Matthew Barthe8441c62021-05-13 16:50:56 -0500111 }
112 }
113 catch (const util::DBusMethodError& dme)
114 {
115 if (!servName.empty())
116 {
117 // Failed to get service name owner state
Matthew Barth7b7df2a2022-02-01 16:37:28 -0600118 hasOwner = false;
119 mgr->setOwner(member, servName, intf, hasOwner);
Matthew Barthe8441c62021-05-13 16:50:56 -0500120 }
121 else
122 {
123 // Path and/or interface configured does not exist on dbus?
124 // TODO How to handle this? Create timer to keep checking for
125 // object/service to appear? When to stop checking?
Anwaar Hadi64b5ac22025-04-04 23:54:53 +0000126 lg2::error(
127 "Unable to get service({SERVICE}) owner "
128 "state for path {MEMBER}, interface {GROUP_INTERFACE}",
129 "SERVICE", servName, "MEMBER", member, "GROUP_INTERFACE",
130 intf);
Matthew Barthe8441c62021-05-13 16:50:56 -0500131 throw dme;
132 }
133 }
134 }
135}
136
Mike Cappsb2e9a4f2022-06-13 10:15:42 -0400137enableTrigger triggerInit(const json& jsonObj, const std::string& /*eventName*/,
138 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
Matthew Barthe8441c62021-05-13 16:50:56 -0500139{
140 // Get the method handler if configured
141 auto handler = methods.end();
142 if (jsonObj.contains("method"))
143 {
144 auto method = jsonObj["method"].get<std::string>();
145 std::transform(method.begin(), method.end(), method.begin(), tolower);
146 handler = methods.find(method);
147 }
148
Matthew Barthc7f629d2021-09-30 15:58:30 -0500149 return [handler = std::move(handler)](
150 const std::string& eventName, Manager* mgr,
151 const std::vector<Group>& groups,
152 std::vector<std::unique_ptr<ActionBase>>& actions) {
153 // Event groups are optional, so a method is only required if there
154 // are event groups i.e.) An init triggered event without any event
155 // groups results in just running the actions
156 if (!groups.empty() && handler == methods.end())
Matthew Barthe8441c62021-05-13 16:50:56 -0500157 {
Matthew Barth54b5a242021-05-21 11:02:52 -0500158 // Construct list of available methods
Patrick Williams5e15c3b2023-10-20 11:18:11 -0500159 auto availMethods = std::accumulate(
160 std::next(methods.begin()), methods.end(),
161 methods.begin()->first, [](auto list, auto method) {
Patrick Williamsdfddd642024-08-16 15:21:51 -0400162 return std::move(list) + ", " + method.first;
163 });
Anwaar Hadi64b5ac22025-04-04 23:54:53 +0000164 lg2::error(
165 "Event '{EVENT_NAME}' requires a supported method given to "
166 "be init driven, available methods: {AVAILABLE_METHODS}",
167 "EVENT_NAME", eventName, "AVAILABLE_METHODS", availMethods);
168 throw std::runtime_error(
169 "Event requires a supported method given to be init driven");
Matthew Barthe8441c62021-05-13 16:50:56 -0500170 }
Matthew Barth54b5a242021-05-21 11:02:52 -0500171
Matthew Barthc7f629d2021-09-30 15:58:30 -0500172 for (const auto& group : groups)
173 {
174 // Call method handler for each group to populate cache
175 handler->second(mgr, group);
176 }
Matthew Barth54b5a242021-05-21 11:02:52 -0500177 for (auto& action : actions)
178 {
Matthew Barthc7f629d2021-09-30 15:58:30 -0500179 // Run each action after initializing all the groups
Matthew Barth54b5a242021-05-21 11:02:52 -0500180 action->run();
181 }
182 };
Matthew Barthe8441c62021-05-13 16:50:56 -0500183}
184
185} // namespace phosphor::fan::control::json::trigger::init