xref: /openbmc/bmcweb/redfish-core/lib/chassis.hpp (revision 530520ea)
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 *, 4> interfaces = {
65             "xyz.openbmc_project.Inventory.Item.Board",
66             "xyz.openbmc_project.Inventory.Item.Chassis",
67             "xyz.openbmc_project.Inventory.Item.PowerSupply",
68             "xyz.openbmc_project.Inventory.Item.System",
69         };
70         res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection";
71         res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
72         res.jsonValue["@odata.context"] =
73             "/redfish/v1/$metadata#ChassisCollection.ChassisCollection";
74         res.jsonValue["Name"] = "Chassis Collection";
75 
76         auto asyncResp = std::make_shared<AsyncResp>(res);
77         crow::connections::systemBus->async_method_call(
78             [asyncResp](const boost::system::error_code ec,
79                         const std::vector<std::string> &chassisList) {
80                 if (ec)
81                 {
82                     messages::internalError(asyncResp->res);
83                     return;
84                 }
85                 nlohmann::json &chassisArray =
86                     asyncResp->res.jsonValue["Members"];
87                 chassisArray = nlohmann::json::array();
88                 for (const std::string &objpath : chassisList)
89                 {
90                     std::size_t lastPos = objpath.rfind("/");
91                     if (lastPos == std::string::npos)
92                     {
93                         BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath;
94                         continue;
95                     }
96                     chassisArray.push_back(
97                         {{"@odata.id", "/redfish/v1/Chassis/" +
98                                            objpath.substr(lastPos + 1)}});
99                 }
100 
101                 asyncResp->res.jsonValue["Members@odata.count"] =
102                     chassisArray.size();
103             },
104             "xyz.openbmc_project.ObjectMapper",
105             "/xyz/openbmc_project/object_mapper",
106             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
107             "/xyz/openbmc_project/inventory", int32_t(3), interfaces);
108     }
109 };
110 
111 /**
112  * Chassis override class for delivering Chassis Schema
113  */
114 class Chassis : public Node
115 {
116   public:
117     Chassis(CrowApp &app) :
118         Node(app, "/redfish/v1/Chassis/<str>/", std::string())
119     {
120         entityPrivileges = {
121             {boost::beast::http::verb::get, {{"Login"}}},
122             {boost::beast::http::verb::head, {{"Login"}}},
123             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
124             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
125             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
126             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
127     }
128 
129   private:
130     /**
131      * Functions triggers appropriate requests on DBus
132      */
133     void doGet(crow::Response &res, const crow::Request &req,
134                const std::vector<std::string> &params) override
135     {
136         // Check if there is required param, truly entering this shall be
137         // impossible.
138         if (params.size() != 1)
139         {
140             messages::internalError(res);
141             res.end();
142             return;
143         }
144 
145         res.jsonValue["@odata.type"] = "#Chassis.v1_4_0.Chassis";
146         res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
147         res.jsonValue["@odata.context"] =
148             "/redfish/v1/$metadata#Chassis.Chassis";
149         res.jsonValue["Name"] = "Chassis Collection";
150         res.jsonValue["ChassisType"] = "RackMount";
151         res.jsonValue["PowerState"] = "On";
152 
153         const std::string &chassisId = params[0];
154         auto asyncResp = std::make_shared<AsyncResp>(res);
155         crow::connections::systemBus->async_method_call(
156             [asyncResp, chassisId(std::string(chassisId))](
157                 const boost::system::error_code ec,
158                 const std::vector<std::pair<
159                     std::string, std::vector<std::pair<
160                                      std::string, std::vector<std::string>>>>>
161                     &subtree) {
162                 if (ec)
163                 {
164                     messages::internalError(asyncResp->res);
165                     return;
166                 }
167                 // Iterate over all retrieved ObjectPaths.
168                 for (const std::pair<
169                          std::string,
170                          std::vector<
171                              std::pair<std::string, std::vector<std::string>>>>
172                          &object : subtree)
173                 {
174                     const std::string &path = object.first;
175                     const std::vector<
176                         std::pair<std::string, std::vector<std::string>>>
177                         &connectionNames = object.second;
178 
179                     if (!boost::ends_with(path, chassisId))
180                     {
181                         continue;
182                     }
183                     if (connectionNames.size() < 1)
184                     {
185                         BMCWEB_LOG_ERROR << "Only got "
186                                          << connectionNames.size()
187                                          << " Connection names";
188                         continue;
189                     }
190 
191                     const std::string connectionName = connectionNames[0].first;
192                     crow::connections::systemBus->async_method_call(
193                         [asyncResp, chassisId(std::string(chassisId))](
194                             const boost::system::error_code ec,
195                             const std::vector<std::pair<
196                                 std::string, VariantType>> &propertiesList) {
197                             for (const std::pair<std::string, VariantType>
198                                      &property : propertiesList)
199                             {
200                                 const std::string *value =
201                                     sdbusplus::message::variant_ns::get_if<
202                                         std::string>(&property.second);
203                                 if (value != nullptr)
204                                 {
205                                     asyncResp->res.jsonValue[property.first] =
206                                         *value;
207                                 }
208                             }
209                             asyncResp->res.jsonValue["Name"] = chassisId;
210                             asyncResp->res.jsonValue["Id"] = chassisId;
211                             asyncResp->res.jsonValue["Thermal"] = {
212                                 {"@odata.id", "/redfish/v1/Chassis/" +
213                                                   chassisId + "/Thermal"}};
214                             // Power object
215                             asyncResp->res.jsonValue["Power"] = {
216                                 {"@odata.id", "/redfish/v1/Chassis/" +
217                                                   chassisId + "/Power"}};
218 
219                             // TODO: An array of references to computer systems
220                             // contained in this chassis.
221                             // res.jsonValue["Links"]["ComputerSystems"]
222                             // =
223                             //                          {{{"@odata.id",
224                             //                          "/redfish/v1/Systems/1"}}};
225                             // An array of references to the Managers
226                             // responsible for managing this chassis.
227                             // res.jsonValue["Links"]["ManagedBy"] =
228                             //                        {{{"@odata.id",
229                             //                        "/redfish/v1/Managers/1"}}};
230                         },
231                         connectionName, path, "org.freedesktop.DBus.Properties",
232                         "GetAll",
233                         "xyz.openbmc_project.Inventory.Decorator.Asset");
234                     return;
235                 }
236 
237                 // Couldn't find an object with that name.  return an error
238                 messages::resourceNotFound(
239                     asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId);
240             },
241             "xyz.openbmc_project.ObjectMapper",
242             "/xyz/openbmc_project/object_mapper",
243             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
244             "/xyz/openbmc_project/inventory", int32_t(0),
245             std::array<const char *, 1>{
246                 "xyz.openbmc_project.Inventory.Decorator.Asset"});
247     }
248 };
249 } // namespace redfish
250