1dfd4a058SMatthew Barth /**
2dfd4a058SMatthew Barth  * Copyright © 2020 IBM Corporation
3dfd4a058SMatthew Barth  *
4dfd4a058SMatthew Barth  * Licensed under the Apache License, Version 2.0 (the "License");
5dfd4a058SMatthew Barth  * you may not use this file except in compliance with the License.
6dfd4a058SMatthew Barth  * You may obtain a copy of the License at
7dfd4a058SMatthew Barth  *
8dfd4a058SMatthew Barth  *     http://www.apache.org/licenses/LICENSE-2.0
9dfd4a058SMatthew Barth  *
10dfd4a058SMatthew Barth  * Unless required by applicable law or agreed to in writing, software
11dfd4a058SMatthew Barth  * distributed under the License is distributed on an "AS IS" BASIS,
12dfd4a058SMatthew Barth  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dfd4a058SMatthew Barth  * See the License for the specific language governing permissions and
14dfd4a058SMatthew Barth  * limitations under the License.
15dfd4a058SMatthew Barth  */
16dfd4a058SMatthew Barth #include "group.hpp"
17dfd4a058SMatthew Barth 
18dfd4a058SMatthew Barth #include <nlohmann/json.hpp>
19dfd4a058SMatthew Barth #include <phosphor-logging/log.hpp>
20dfd4a058SMatthew Barth 
21dfd4a058SMatthew Barth namespace phosphor::fan::control::json
22dfd4a058SMatthew Barth {
23dfd4a058SMatthew Barth 
24dfd4a058SMatthew Barth using json = nlohmann::json;
25dfd4a058SMatthew Barth using namespace phosphor::logging;
26dfd4a058SMatthew Barth 
27*c2c2db7dSMatt Spinler std::set<std::string> Group::_allMembers{};
28*c2c2db7dSMatt Spinler 
Group(const json & jsonObj)299403a217SMatthew Barth Group::Group(const json& jsonObj) : ConfigBase(jsonObj), _service("")
30dfd4a058SMatthew Barth {
31dfd4a058SMatthew Barth     setMembers(jsonObj);
32dfd4a058SMatthew Barth     // Setting the group's service name is optional
33dfd4a058SMatthew Barth     if (jsonObj.contains("service"))
34dfd4a058SMatthew Barth     {
35dfd4a058SMatthew Barth         setService(jsonObj);
36dfd4a058SMatthew Barth     }
37dfd4a058SMatthew Barth }
38dfd4a058SMatthew Barth 
Group(const Group & origObj)39e5578602SMatthew Barth Group::Group(const Group& origObj) : ConfigBase(origObj)
40e5578602SMatthew Barth {
41b3946f83SMatthew Barth     // Copy everything from the original Group object
42e5578602SMatthew Barth     _members = origObj._members;
43e5578602SMatthew Barth     _service = origObj._service;
44b3946f83SMatthew Barth     _interface = origObj.getInterface();
45b3946f83SMatthew Barth     _property = origObj.getProperty();
46b3946f83SMatthew Barth     _type = origObj.getType();
47b3946f83SMatthew Barth     _value = origObj.getValue();
48e5578602SMatthew Barth }
49e5578602SMatthew Barth 
setMembers(const json & jsonObj)50dfd4a058SMatthew Barth void Group::setMembers(const json& jsonObj)
51dfd4a058SMatthew Barth {
52dfd4a058SMatthew Barth     if (!jsonObj.contains("members"))
53dfd4a058SMatthew Barth     {
54dfd4a058SMatthew Barth         log<level::ERR>("Missing required group's members",
55dfd4a058SMatthew Barth                         entry("JSON=%s", jsonObj.dump().c_str()));
56dfd4a058SMatthew Barth         throw std::runtime_error("Missing required group's members");
57dfd4a058SMatthew Barth     }
58dfd4a058SMatthew Barth     for (const auto& member : jsonObj["members"])
59dfd4a058SMatthew Barth     {
60dfd4a058SMatthew Barth         _members.emplace_back(member.get<std::string>());
61dfd4a058SMatthew Barth     }
62*c2c2db7dSMatt Spinler     _allMembers.insert(_members.begin(), _members.end());
63dfd4a058SMatthew Barth }
64dfd4a058SMatthew Barth 
setService(const json & jsonObj)65dfd4a058SMatthew Barth void Group::setService(const json& jsonObj)
66dfd4a058SMatthew Barth {
67dfd4a058SMatthew Barth     _service = jsonObj["service"].get<std::string>();
68dfd4a058SMatthew Barth }
69dfd4a058SMatthew Barth 
70dfd4a058SMatthew Barth } // namespace phosphor::fan::control::json
71