Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2020 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 "group.hpp" |
| 17 | |
| 18 | #include <nlohmann/json.hpp> |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 19 | #include <phosphor-logging/lg2.hpp> |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 20 | |
| 21 | namespace phosphor::fan::control::json |
| 22 | { |
| 23 | |
| 24 | using json = nlohmann::json; |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 25 | |
Matt Spinler | c2c2db7 | 2022-04-07 13:59:37 -0500 | [diff] [blame] | 26 | std::set<std::string> Group::_allMembers{}; |
| 27 | |
Matthew Barth | 9403a21 | 2021-05-17 09:31:50 -0500 | [diff] [blame] | 28 | Group::Group(const json& jsonObj) : ConfigBase(jsonObj), _service("") |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 29 | { |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 30 | setMembers(jsonObj); |
| 31 | // Setting the group's service name is optional |
| 32 | if (jsonObj.contains("service")) |
| 33 | { |
| 34 | setService(jsonObj); |
| 35 | } |
| 36 | } |
| 37 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 38 | Group::Group(const Group& origObj) : ConfigBase(origObj) |
| 39 | { |
Matthew Barth | b3946f8 | 2021-05-03 14:35:49 -0500 | [diff] [blame] | 40 | // Copy everything from the original Group object |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 41 | _members = origObj._members; |
| 42 | _service = origObj._service; |
Matthew Barth | b3946f8 | 2021-05-03 14:35:49 -0500 | [diff] [blame] | 43 | _interface = origObj.getInterface(); |
| 44 | _property = origObj.getProperty(); |
| 45 | _type = origObj.getType(); |
| 46 | _value = origObj.getValue(); |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 49 | void Group::setMembers(const json& jsonObj) |
| 50 | { |
| 51 | if (!jsonObj.contains("members")) |
| 52 | { |
Anwaar Hadi | 64b5ac2 | 2025-04-04 23:54:53 +0000 | [diff] [blame] | 53 | lg2::error("Missing required group's members", "JSON", jsonObj.dump()); |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 54 | throw std::runtime_error("Missing required group's members"); |
| 55 | } |
| 56 | for (const auto& member : jsonObj["members"]) |
| 57 | { |
| 58 | _members.emplace_back(member.get<std::string>()); |
| 59 | } |
Matt Spinler | c2c2db7 | 2022-04-07 13:59:37 -0500 | [diff] [blame] | 60 | _allMembers.insert(_members.begin(), _members.end()); |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void Group::setService(const json& jsonObj) |
| 64 | { |
| 65 | _service = jsonObj["service"].get<std::string>(); |
| 66 | } |
| 67 | |
| 68 | } // namespace phosphor::fan::control::json |