xref: /openbmc/bmcweb/redfish-core/lib/chassis.hpp (revision d45d2d0f)
1 /*
2 // Copyright (c) 2018 Intel 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 #pragma once
17 
18 #include "node.hpp"
19 
20 #include <boost/container/flat_map.hpp>
21 
22 namespace redfish
23 {
24 
25 /**
26  * DBus types primitives for several generic DBus interfaces
27  * TODO(Pawel) consider move this to separate file into boost::dbus
28  */
29 // Note, this is not a very useful Variant, but because it isn't used to get
30 // values, it should be as simple as possible
31 // TODO(ed) invent a nullvariant type
32 using VariantType = sdbusplus::message::variant<bool, std::string, uint64_t>;
33 using ManagedObjectsType = std::vector<std::pair<
34     sdbusplus::message::object_path,
35     std::vector<std::pair<std::string,
36                           std::vector<std::pair<std::string, VariantType>>>>>>;
37 
38 using PropertiesType = boost::container::flat_map<std::string, VariantType>;
39 
40 /**
41  * ChassisCollection derived class for delivering Chassis Collection Schema
42  */
43 class ChassisCollection : public Node
44 {
45   public:
46     ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/")
47     {
48         entityPrivileges = {
49             {boost::beast::http::verb::get, {{"Login"}}},
50             {boost::beast::http::verb::head, {{"Login"}}},
51             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
52             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
53             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
54             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
55     }
56 
57   private:
58     /**
59      * Functions triggers appropriate requests on DBus
60      */
61     void doGet(crow::Response &res, const crow::Request &req,
62                const std::vector<std::string> &params) override
63     {
64         const std::array<const char *, 3> interfaces = {
65             "xyz.openbmc_project.Inventory.Item.Board",
66             "xyz.openbmc_project.Inventory.Item.Chassis",
67             "xyz.openbmc_project.Inventory.Item.PowerSupply"};
68         res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection";
69         res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
70         res.jsonValue["@odata.context"] =
71             "/redfish/v1/$metadata#ChassisCollection.ChassisCollection";
72         res.jsonValue["Name"] = "Chassis Collection";
73 
74         auto asyncResp = std::make_shared<AsyncResp>(res);
75         crow::connections::systemBus->async_method_call(
76             [asyncResp](const boost::system::error_code ec,
77                         const std::vector<std::string> &chassisList) {
78                 if (ec)
79                 {
80                     messages::internalError(asyncResp->res);
81                     return;
82                 }
83                 nlohmann::json &chassisArray =
84                     asyncResp->res.jsonValue["Members"];
85                 chassisArray = nlohmann::json::array();
86                 for (const std::string &objpath : chassisList)
87                 {
88                     std::size_t lastPos = objpath.rfind("/");
89                     if (lastPos == std::string::npos)
90                     {
91                         BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath;
92                         continue;
93                     }
94                     chassisArray.push_back(
95                         {{"@odata.id", "/redfish/v1/Chassis/" +
96                                            objpath.substr(lastPos + 1)}});
97                 }
98 
99                 asyncResp->res.jsonValue["Members@odata.count"] =
100                     chassisArray.size();
101             },
102             "xyz.openbmc_project.ObjectMapper",
103             "/xyz/openbmc_project/object_mapper",
104             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
105             "/xyz/openbmc_project/inventory", int32_t(3), interfaces);
106     }
107 };
108 
109 /**
110  * Chassis override class for delivering Chassis Schema
111  */
112 class Chassis : public Node
113 {
114   public:
115     Chassis(CrowApp &app) :
116         Node(app, "/redfish/v1/Chassis/<str>/", std::string())
117     {
118         entityPrivileges = {
119             {boost::beast::http::verb::get, {{"Login"}}},
120             {boost::beast::http::verb::head, {{"Login"}}},
121             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
122             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
123             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
124             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
125     }
126 
127   private:
128     /**
129      * Functions triggers appropriate requests on DBus
130      */
131     void doGet(crow::Response &res, const crow::Request &req,
132                const std::vector<std::string> &params) override
133     {
134         // Check if there is required param, truly entering this shall be
135         // impossible.
136         if (params.size() != 1)
137         {
138             messages::internalError(res);
139             res.end();
140             return;
141         }
142 
143         res.jsonValue["@odata.type"] = "#Chassis.v1_4_0.Chassis";
144         res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
145         res.jsonValue["@odata.context"] =
146             "/redfish/v1/$metadata#Chassis.Chassis";
147         res.jsonValue["Name"] = "Chassis Collection";
148         res.jsonValue["ChassisType"] = "RackMount";
149         res.jsonValue["PowerState"] = "On";
150 
151         const std::string &chassisId = params[0];
152         auto asyncResp = std::make_shared<AsyncResp>(res);
153         crow::connections::systemBus->async_method_call(
154             [asyncResp, chassisId(std::string(chassisId))](
155                 const boost::system::error_code ec,
156                 const std::vector<std::pair<
157                     std::string, std::vector<std::pair<
158                                      std::string, std::vector<std::string>>>>>
159                     &subtree) {
160                 if (ec)
161                 {
162                     messages::internalError(asyncResp->res);
163                     return;
164                 }
165                 // Iterate over all retrieved ObjectPaths.
166                 for (const std::pair<
167                          std::string,
168                          std::vector<
169                              std::pair<std::string, std::vector<std::string>>>>
170                          &object : subtree)
171                 {
172                     const std::string &path = object.first;
173                     const std::vector<
174                         std::pair<std::string, std::vector<std::string>>>
175                         &connectionNames = object.second;
176 
177                     if (!boost::ends_with(path, chassisId))
178                     {
179                         continue;
180                     }
181                     if (connectionNames.size() < 1)
182                     {
183                         BMCWEB_LOG_ERROR << "Only got "
184                                          << connectionNames.size()
185                                          << " Connection names";
186                         continue;
187                     }
188 
189                     const std::string connectionName = connectionNames[0].first;
190                     crow::connections::systemBus->async_method_call(
191                         [asyncResp, chassisId(std::string(chassisId))](
192                             const boost::system::error_code ec,
193                             const std::vector<std::pair<
194                                 std::string, VariantType>> &propertiesList) {
195                             for (const std::pair<std::string, VariantType>
196                                      &property : propertiesList)
197                             {
198                                 const std::string *value =
199                                     sdbusplus::message::variant_ns::get_if<
200                                         std::string>(&property.second);
201                                 if (value != nullptr)
202                                 {
203                                     asyncResp->res.jsonValue[property.first] =
204                                         *value;
205                                 }
206                             }
207                             asyncResp->res.jsonValue["Name"] = chassisId;
208                             asyncResp->res.jsonValue["Id"] = chassisId;
209                             asyncResp->res.jsonValue["Thermal"] = {
210                                 {"@odata.id", "/redfish/v1/Chassis/" +
211                                                   chassisId + "/Thermal"}};
212                             // Power object
213                             asyncResp->res.jsonValue["Power"] = {
214                                 {"@odata.id", "/redfish/v1/Chassis/" +
215                                                   chassisId + "/Power"}};
216 
217                             // TODO: An array of references to computer systems
218                             // contained in this chassis.
219                             // res.jsonValue["Links"]["ComputerSystems"]
220                             // =
221                             //                          {{{"@odata.id",
222                             //                          "/redfish/v1/Systems/1"}}};
223                             // An array of references to the Managers
224                             // responsible for managing this chassis.
225                             // res.jsonValue["Links"]["ManagedBy"] =
226                             //                        {{{"@odata.id",
227                             //                        "/redfish/v1/Managers/1"}}};
228                         },
229                         connectionName, path, "org.freedesktop.DBus.Properties",
230                         "GetAll",
231                         "xyz.openbmc_project.Inventory.Decorator.Asset");
232                     return;
233                 }
234 
235                 // Couldn't find an object with that name.  return an error
236                 messages::resourceNotFound(
237                     asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId);
238             },
239             "xyz.openbmc_project.ObjectMapper",
240             "/xyz/openbmc_project/object_mapper",
241             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
242             "/xyz/openbmc_project/inventory", int32_t(0),
243             std::array<const char *, 1>{
244                 "xyz.openbmc_project.Inventory.Decorator.Asset"});
245     }
246 };
247 } // namespace redfish
248