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