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> |
| 19 | #include <phosphor-logging/log.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; |
| 25 | using namespace phosphor::logging; |
| 26 | |
Matthew Barth | 391ade0 | 2021-01-15 14:33:21 -0600 | [diff] [blame] | 27 | Group::Group(const json& jsonObj) : ConfigBase(jsonObj), _service("") |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 28 | { |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 29 | setMembers(jsonObj); |
| 30 | // Setting the group's service name is optional |
| 31 | if (jsonObj.contains("service")) |
| 32 | { |
| 33 | setService(jsonObj); |
| 34 | } |
| 35 | } |
| 36 | |
Matthew Barth | e557860 | 2021-03-30 12:53:24 -0500 | [diff] [blame^] | 37 | Group::Group(const Group& origObj) : ConfigBase(origObj) |
| 38 | { |
| 39 | // Only what was parsed by the original Group object is copied |
| 40 | _members = origObj._members; |
| 41 | _service = origObj._service; |
| 42 | } |
| 43 | |
Matthew Barth | dfd4a05 | 2020-09-02 16:31:51 -0500 | [diff] [blame] | 44 | void Group::setMembers(const json& jsonObj) |
| 45 | { |
| 46 | if (!jsonObj.contains("members")) |
| 47 | { |
| 48 | log<level::ERR>("Missing required group's members", |
| 49 | entry("JSON=%s", jsonObj.dump().c_str())); |
| 50 | throw std::runtime_error("Missing required group's members"); |
| 51 | } |
| 52 | for (const auto& member : jsonObj["members"]) |
| 53 | { |
| 54 | _members.emplace_back(member.get<std::string>()); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | void Group::setService(const json& jsonObj) |
| 59 | { |
| 60 | _service = jsonObj["service"].get<std::string>(); |
| 61 | } |
| 62 | |
| 63 | } // namespace phosphor::fan::control::json |