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 | |
| 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 | |
| 36 | namespace phosphor::fan::control::json::trigger::init |
| 37 | { |
| 38 | |
| 39 | using json = nlohmann::json; |
| 40 | using namespace phosphor::logging; |
| 41 | |
| 42 | void 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 | |
| 67 | void nameHasOwner(Manager* mgr, const Group& group) |
| 68 | { |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 69 | bool hasOwner = false; |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 70 | std::string lastName = ""; |
| 71 | for (const auto& member : group.getMembers()) |
| 72 | { |
| 73 | std::string servName = ""; |
| 74 | auto intf = group.getInterface(); |
| 75 | try |
| 76 | { |
Matthew Barth | 65f7281 | 2022-01-13 15:05:34 -0600 | [diff] [blame] | 77 | servName = group.getService(); |
| 78 | if (servName.empty()) |
| 79 | { |
| 80 | servName = mgr->getService(member, intf); |
| 81 | } |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 82 | if (!servName.empty()) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 83 | { |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 84 | if (lastName != servName) |
| 85 | { |
| 86 | // Member not provided by same service as last group member |
| 87 | lastName = servName; |
| 88 | hasOwner = util::SDBusPlus::callMethodAndRead<bool>( |
| 89 | mgr->getBus(), "org.freedesktop.DBus", |
| 90 | "/org/freedesktop/DBus", "org.freedesktop.DBus", |
| 91 | "NameHasOwner", servName); |
| 92 | } |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 93 | // Update service name owner state of group object |
| 94 | mgr->setOwner(member, servName, intf, hasOwner); |
| 95 | } |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 96 | else |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 97 | { |
| 98 | // Path and/or interface configured does not exist on dbus? |
| 99 | // TODO How to handle this? Create timer to keep checking for |
| 100 | // object/service to appear? When to stop checking? |
| 101 | log<level::ERR>( |
| 102 | fmt::format( |
| 103 | "Unable to get service name for path {}, interface {}", |
| 104 | member, intf) |
| 105 | .c_str()); |
| 106 | } |
| 107 | } |
| 108 | catch (const util::DBusMethodError& dme) |
| 109 | { |
| 110 | if (!servName.empty()) |
| 111 | { |
| 112 | // Failed to get service name owner state |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 113 | hasOwner = false; |
| 114 | mgr->setOwner(member, servName, intf, hasOwner); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
| 118 | // Path and/or interface configured does not exist on dbus? |
| 119 | // TODO How to handle this? Create timer to keep checking for |
| 120 | // object/service to appear? When to stop checking? |
Matthew Barth | 7b7df2a | 2022-02-01 16:37:28 -0600 | [diff] [blame^] | 121 | log<level::ERR>(fmt::format("Unable to get service({}) owner " |
| 122 | "state for path {}, interface {}", |
| 123 | servName, member, intf) |
| 124 | .c_str()); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 125 | throw dme; |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 131 | enableTrigger triggerInit(const json& jsonObj, const std::string& eventName, |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 132 | std::vector<std::unique_ptr<ActionBase>>& actions) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 133 | { |
| 134 | // Get the method handler if configured |
| 135 | auto handler = methods.end(); |
| 136 | if (jsonObj.contains("method")) |
| 137 | { |
| 138 | auto method = jsonObj["method"].get<std::string>(); |
| 139 | std::transform(method.begin(), method.end(), method.begin(), tolower); |
| 140 | handler = methods.find(method); |
| 141 | } |
| 142 | |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 143 | return [handler = std::move(handler)]( |
| 144 | const std::string& eventName, Manager* mgr, |
| 145 | const std::vector<Group>& groups, |
| 146 | std::vector<std::unique_ptr<ActionBase>>& actions) { |
| 147 | // Event groups are optional, so a method is only required if there |
| 148 | // are event groups i.e.) An init triggered event without any event |
| 149 | // groups results in just running the actions |
| 150 | if (!groups.empty() && handler == methods.end()) |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 151 | { |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 152 | // Construct list of available methods |
| 153 | auto availMethods = std::accumulate( |
| 154 | std::next(methods.begin()), methods.end(), |
| 155 | methods.begin()->first, [](auto list, auto method) { |
| 156 | return std::move(list) + ", " + method.first; |
| 157 | }); |
| 158 | auto msg = |
| 159 | fmt::format("Event '{}' requires a supported method given to " |
| 160 | "be init driven, available methods: {}", |
| 161 | eventName, availMethods); |
| 162 | log<level::ERR>(msg.c_str()); |
| 163 | throw std::runtime_error(msg.c_str()); |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 164 | } |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 165 | |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 166 | for (const auto& group : groups) |
| 167 | { |
| 168 | // Call method handler for each group to populate cache |
| 169 | handler->second(mgr, group); |
| 170 | } |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 171 | for (auto& action : actions) |
| 172 | { |
Matthew Barth | c7f629d | 2021-09-30 15:58:30 -0500 | [diff] [blame] | 173 | // Run each action after initializing all the groups |
Matthew Barth | 54b5a24 | 2021-05-21 11:02:52 -0500 | [diff] [blame] | 174 | action->run(); |
| 175 | } |
| 176 | }; |
Matthew Barth | e8441c6 | 2021-05-13 16:50:56 -0500 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | } // namespace phosphor::fan::control::json::trigger::init |