blob: e100d71a4bfe33924ca20320428bb456f1c02a79 [file] [log] [blame]
Matthew Barthdfd4a052020-09-02 16:31:51 -05001/**
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 Barthdfd4a052020-09-02 16:31:51 -050020
21namespace phosphor::fan::control::json
22{
23
24using json = nlohmann::json;
25using namespace phosphor::logging;
26
Matt Spinlerc2c2db72022-04-07 13:59:37 -050027std::set<std::string> Group::_allMembers{};
28
Matthew Barth9403a212021-05-17 09:31:50 -050029Group::Group(const json& jsonObj) : ConfigBase(jsonObj), _service("")
Matthew Barthdfd4a052020-09-02 16:31:51 -050030{
Matthew Barthdfd4a052020-09-02 16:31:51 -050031 setMembers(jsonObj);
32 // Setting the group's service name is optional
33 if (jsonObj.contains("service"))
34 {
35 setService(jsonObj);
36 }
37}
38
Matthew Barthe5578602021-03-30 12:53:24 -050039Group::Group(const Group& origObj) : ConfigBase(origObj)
40{
Matthew Barthb3946f82021-05-03 14:35:49 -050041 // Copy everything from the original Group object
Matthew Barthe5578602021-03-30 12:53:24 -050042 _members = origObj._members;
43 _service = origObj._service;
Matthew Barthb3946f82021-05-03 14:35:49 -050044 _interface = origObj.getInterface();
45 _property = origObj.getProperty();
46 _type = origObj.getType();
47 _value = origObj.getValue();
Matthew Barthe5578602021-03-30 12:53:24 -050048}
49
Matthew Barthdfd4a052020-09-02 16:31:51 -050050void Group::setMembers(const json& jsonObj)
51{
52 if (!jsonObj.contains("members"))
53 {
54 log<level::ERR>("Missing required group's members",
55 entry("JSON=%s", jsonObj.dump().c_str()));
56 throw std::runtime_error("Missing required group's members");
57 }
58 for (const auto& member : jsonObj["members"])
59 {
60 _members.emplace_back(member.get<std::string>());
61 }
Matt Spinlerc2c2db72022-04-07 13:59:37 -050062 _allMembers.insert(_members.begin(), _members.end());
Matthew Barthdfd4a052020-09-02 16:31:51 -050063}
64
65void Group::setService(const json& jsonObj)
66{
67 _service = jsonObj["service"].get<std::string>();
68}
69
70} // namespace phosphor::fan::control::json