1 /**
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 #include "trigger_aliases.hpp"
23
24 #include <nlohmann/json.hpp>
25 #include <phosphor-logging/lg2.hpp>
26
27 #include <algorithm>
28 #include <iterator>
29 #include <memory>
30 #include <numeric>
31 #include <utility>
32 #include <vector>
33
34 namespace phosphor::fan::control::json::trigger::init
35 {
36
37 using json = nlohmann::json;
38
getProperties(Manager * mgr,const Group & group)39 void 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(),
52 group.getProperty(), group.getService());
53
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 }
63 }
64 }
65 catch (const std::exception& e)
66 {
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
nameHasOwner(Manager * mgr,const Group & group)74 void nameHasOwner(Manager* mgr, const Group& group)
75 {
76 bool hasOwner = false;
77 std::string lastName = "";
78 for (const auto& member : group.getMembers())
79 {
80 std::string servName = "";
81 auto intf = group.getInterface();
82 try
83 {
84 servName = group.getService();
85 if (servName.empty())
86 {
87 servName = mgr->getService(member, intf);
88 }
89 if (!servName.empty())
90 {
91 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 }
100 // Update service name owner state of group object
101 mgr->setOwner(member, servName, intf, hasOwner);
102 }
103 else
104 {
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?
108 lg2::error(
109 "Unable to get service name for path {MEMBER}, interface {GROUP_INTERFACE}",
110 "MEMBER", member, "GROUP_INTERFACE", intf);
111 }
112 }
113 catch (const util::DBusMethodError& dme)
114 {
115 if (!servName.empty())
116 {
117 // Failed to get service name owner state
118 hasOwner = false;
119 mgr->setOwner(member, servName, intf, hasOwner);
120 }
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?
126 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);
131 throw dme;
132 }
133 }
134 }
135 }
136
triggerInit(const json & jsonObj,const std::string &,std::vector<std::unique_ptr<ActionBase>> &)137 enableTrigger triggerInit(const json& jsonObj, const std::string& /*eventName*/,
138 std::vector<std::unique_ptr<ActionBase>>& /*actions*/)
139 {
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
149 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())
157 {
158 // Construct list of available methods
159 auto availMethods = std::accumulate(
160 std::next(methods.begin()), methods.end(),
161 methods.begin()->first, [](auto list, auto method) {
162 return std::move(list) + ", " + method.first;
163 });
164 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");
170 }
171
172 for (const auto& group : groups)
173 {
174 // Call method handler for each group to populate cache
175 handler->second(mgr, group);
176 }
177 for (auto& action : actions)
178 {
179 // Run each action after initializing all the groups
180 action->run();
181 }
182 };
183 }
184
185 } // namespace phosphor::fan::control::json::trigger::init
186