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>
20 #include <sdbusplus/bus.hpp>
21 
22 namespace phosphor::fan::control::json
23 {
24 
25 using json = nlohmann::json;
26 using namespace phosphor::logging;
27 
28 Group::Group(sdbusplus::bus::bus& bus, const json& jsonObj) :
29     ConfigBase(jsonObj), _service("")
30 {
31     if (jsonObj.contains("profiles"))
32     {
33         for (const auto& profile : jsonObj["profiles"])
34         {
35             _profiles.emplace_back(profile.get<std::string>());
36         }
37     }
38     setMembers(jsonObj);
39     // Setting the group's service name is optional
40     if (jsonObj.contains("service"))
41     {
42         setService(jsonObj);
43     }
44 }
45 
46 void Group::setMembers(const json& jsonObj)
47 {
48     if (!jsonObj.contains("members"))
49     {
50         log<level::ERR>("Missing required group's members",
51                         entry("JSON=%s", jsonObj.dump().c_str()));
52         throw std::runtime_error("Missing required group's members");
53     }
54     for (const auto& member : jsonObj["members"])
55     {
56         _members.emplace_back(member.get<std::string>());
57     }
58 }
59 
60 void Group::setService(const json& jsonObj)
61 {
62     _service = jsonObj["service"].get<std::string>();
63 }
64 
65 } // namespace phosphor::fan::control::json
66