Matthew Barth | 070ee3c | 2021-01-29 09:58:16 -0600 | [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 "missing_owner_target.hpp" |
| 17 | |
| 18 | #include "../manager.hpp" |
| 19 | #include "../zone.hpp" |
| 20 | #include "group.hpp" |
| 21 | |
| 22 | #include <fmt/format.h> |
| 23 | |
| 24 | #include <nlohmann/json.hpp> |
| 25 | #include <phosphor-logging/log.hpp> |
| 26 | |
| 27 | #include <algorithm> |
| 28 | |
| 29 | namespace phosphor::fan::control::json |
| 30 | { |
| 31 | |
| 32 | using json = nlohmann::json; |
| 33 | using namespace phosphor::logging; |
| 34 | |
| 35 | MissingOwnerTarget::MissingOwnerTarget(const json& jsonObj) : |
| 36 | ActionBase(MissingOwnerTarget::name) |
| 37 | { |
| 38 | setTarget(jsonObj); |
| 39 | } |
| 40 | |
| 41 | void MissingOwnerTarget::run(Zone& zone, const Group& group) |
| 42 | { |
| 43 | const auto& members = group.getMembers(); |
| 44 | auto isMissingOwner = |
| 45 | std::any_of(members.begin(), members.end(), |
| 46 | [&intf = group.getInterface()](const auto& member) { |
| 47 | return !Manager::hasOwner(member, intf); |
| 48 | }); |
| 49 | if (isMissingOwner) |
| 50 | { |
| 51 | zone.setTarget(_target); |
| 52 | } |
| 53 | // Update group's fan control active allowed based on action results |
| 54 | zone.setActiveAllow(group.getName(), !isMissingOwner); |
| 55 | } |
| 56 | |
| 57 | void MissingOwnerTarget::setTarget(const json& jsonObj) |
| 58 | { |
| 59 | if (!jsonObj.contains("speed")) |
| 60 | { |
| 61 | log<level::ERR>( |
| 62 | fmt::format("Action {}: Missing required speed value", getName()) |
| 63 | .c_str(), |
| 64 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 65 | throw std::runtime_error( |
| 66 | fmt::format("Action {}: Missing required speed value", getName()) |
| 67 | .c_str()); |
| 68 | } |
| 69 | _target = jsonObj["speed"].get<uint64_t>(); |
| 70 | } |
| 71 | |
| 72 | } // namespace phosphor::fan::control::json |