blob: 0f2b92b055080d70d12407a56faaf3a73c3c0473 [file] [log] [blame]
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +01001/*
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"
Ed Tanous1abe55e2018-09-05 08:30:59 -070019
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010020#include <boost/container/flat_map.hpp>
21
Ed Tanous1abe55e2018-09-05 08:30:59 -070022namespace redfish
23{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010024
25/**
26 * DBus types primitives for several generic DBus interfaces
27 * TODO(Pawel) consider move this to separate file into boost::dbus
28 */
Ed Tanous55c7b7a2018-05-22 15:27:24 -070029// Note, this is not a very useful Variant, but because it isn't used to get
Ed Tanousaa2e59c2018-04-12 12:17:20 -070030// values, it should be as simple as possible
31// TODO(ed) invent a nullvariant type
Ed Tanous04a258f2018-10-15 08:00:41 -070032using VariantType = sdbusplus::message::variant<bool, std::string, uint64_t>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070033using 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>>>>>>;
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010037
Ed Tanousaa2e59c2018-04-12 12:17:20 -070038using PropertiesType = boost::container::flat_map<std::string, VariantType>;
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010039
40/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010041 * ChassisCollection derived class for delivering Chassis Collection Schema
42 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070043class ChassisCollection : public Node
44{
45 public:
46 ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/")
47 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 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 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010056
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 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 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -070064 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 };
Ed Tanous0f74e642018-11-12 15:17:05 -080070 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
Ed Tanous62d5e2e2018-09-05 16:17:25 -070076 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)
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 {
Jason M. Billsf12894f2018-10-09 12:45:45 -070082 messages::internalError(asyncResp->res);
Ed Tanous62d5e2e2018-09-05 16:17:25 -070083 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -070084 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -070085 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 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100100
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700101 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 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100109};
110
111/**
112 * Chassis override class for delivering Chassis Schema
113 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114class Chassis : public Node
115{
116 public:
117 Chassis(CrowApp &app) :
118 Node(app, "/redfish/v1/Chassis/<str>/", std::string())
119 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700120 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"}}}};
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100127 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100128
Ed Tanous1abe55e2018-09-05 08:30:59 -0700129 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 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700140 messages::internalError(res);
Ed Tanousdaf36e22018-04-20 16:01:36 -0700141 res.end();
142 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700143 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700144
Ed Tanous0f74e642018-11-12 15:17:05 -0800145 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
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153 const std::string &chassisId = params[0];
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700154 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700156 [asyncResp, chassisId(std::string(chassisId))](
157 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700158 const std::vector<std::pair<
159 std::string, std::vector<std::pair<
160 std::string, std::vector<std::string>>>>>
161 &subtree) {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700162 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700163 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700164 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 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;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700178
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 if (!boost::ends_with(path, chassisId))
180 {
181 continue;
Ed Tanousdaf36e22018-04-20 16:01:36 -0700182 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700183 if (connectionNames.size() < 1)
184 {
185 BMCWEB_LOG_ERROR << "Only got "
186 << connectionNames.size()
187 << " Connection names";
188 continue;
189 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700190
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191 const std::string connectionName = connectionNames[0].first;
192 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700193 [asyncResp, chassisId(std::string(chassisId))](
194 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 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 mapbox::getPtr<const std::string>(
202 property.second);
203 if (value != nullptr)
204 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700205 asyncResp->res.jsonValue[property.first] =
206 *value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700207 }
208 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700209 asyncResp->res.jsonValue["Name"] = chassisId;
210 asyncResp->res.jsonValue["Id"] = chassisId;
211 asyncResp->res.jsonValue["Thermal"] = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700212 {"@odata.id", "/redfish/v1/Chassis/" +
213 chassisId + "/Thermal"}};
Ed Tanous2474adf2018-09-05 16:31:16 -0700214 // 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"}}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 },
231 connectionName, path, "org.freedesktop.DBus.Properties",
232 "GetAll",
233 "xyz.openbmc_project.Inventory.Decorator.Asset");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 return;
235 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700236
Ed Tanous1abe55e2018-09-05 08:30:59 -0700237 // Couldn't find an object with that name. return an error
Jason M. Billsf12894f2018-10-09 12:45:45 -0700238 messages::resourceNotFound(
239 asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700240 },
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 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700248};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249} // namespace redfish