Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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" |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 22 | #include "trigger_aliases.hpp" |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 23 | |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 24 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame^] | 25 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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; |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 38 | |
| 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(), |
Matt Spinler | 9ac325c | 2022-04-25 14:13:49 -0500 | [diff] [blame] | 52 | group.getProperty(), group.getService()); |
Matt Spinler | 1f40987 | 2022-04-07 13:14:35 -0500 | [diff] [blame] | 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 | } |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
Matt Spinler | f16f063 | 2022-05-09 14:27:46 -0500 | [diff] [blame] | 65 | catch (const std::exception& e) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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 | |
| 74 | void nameHasOwner(Manager* mgr, const Group& group) |
| 75 | { |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame] | 76 | bool hasOwner = false; |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 77 | std::string lastName = ""; |
| 78 | for (const auto& member : group.getMembers()) |
| 79 | { |
| 80 | std::string servName = ""; |
| 81 | auto intf = group.getInterface(); |
| 82 | try |
| 83 | { |
Matthew Barth | 65f7281 | 2022-01-13 15:05:34 -0600 | [diff] [blame] | 84 | servName = group.getService(); |
| 85 | if (servName.empty()) |
| 86 | { |
| 87 | servName = mgr->getService(member, intf); |
| 88 | } |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame] | 89 | if (!servName.empty()) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 90 | { |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame] | 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 | } |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 100 | // Update service name owner state of group object |
| 101 | mgr->setOwner(member, servName, intf, hasOwner); |
| 102 | } |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame] | 103 | else |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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? |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame^] | 108 | lg2::error( |
| 109 | "Unable to get service name for path {MEMBER}, interface {GROUP_INTERFACE}", |
| 110 | "MEMBER", member, "GROUP_INTERFACE", intf); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | catch (const util::DBusMethodError& dme) |
| 114 | { |
| 115 | if (!servName.empty()) |
| 116 | { |
| 117 | // Failed to get service name owner state |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame] | 118 | hasOwner = false; |
| 119 | mgr->setOwner(member, servName, intf, hasOwner); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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? |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame^] | 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); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 131 | throw dme; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
Mike Capps | b2e9a4f | 2022-06-13 10:15:42 -0400 | [diff] [blame] | 137 | enableTrigger triggerInit(const json& jsonObj, const std::string& /*eventName*/, |
| 138 | std::vector<std::unique_ptr<ActionBase>>& /*actions*/) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 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 | |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 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()) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 157 | { |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 158 | // Construct list of available methods |
Patrick Williams | 5e15c3b | 2023-10-20 11:18:11 -0500 | [diff] [blame] | 159 | auto availMethods = std::accumulate( |
| 160 | std::next(methods.begin()), methods.end(), |
| 161 | methods.begin()->first, [](auto list, auto method) { |
Patrick Williams | dfddd64 | 2024-08-16 15:21:51 -0400 | [diff] [blame] | 162 | return std::move(list) + ", " + method.first; |
| 163 | }); |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame^] | 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"); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 170 | } |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 171 | |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 172 | for (const auto& group : groups) |
| 173 | { |
| 174 | // Call method handler for each group to populate cache |
| 175 | handler->second(mgr, group); |
| 176 | } |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 177 | for (auto& action : actions) |
| 178 | { |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 179 | // Run each action after initializing all the groups |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 180 | action->run(); |
| 181 | } |
| 182 | }; |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | } // namespace phosphor::fan::control::json::trigger::init |