blob: 7f58ab5ba50d3871201a9fcf630fa2fbc7b9d569 [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>
Ed Tanousabf2add2019-01-22 16:40:12 -080021#include <variant>
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010022
Ed Tanous1abe55e2018-09-05 08:30:59 -070023namespace redfish
24{
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010025
26/**
27 * DBus types primitives for several generic DBus interfaces
28 * TODO(Pawel) consider move this to separate file into boost::dbus
29 */
Ed Tanous55c7b7a2018-05-22 15:27:24 -070030// Note, this is not a very useful Variant, but because it isn't used to get
Ed Tanousaa2e59c2018-04-12 12:17:20 -070031// values, it should be as simple as possible
32// TODO(ed) invent a nullvariant type
Ed Tanousabf2add2019-01-22 16:40:12 -080033using VariantType = std::variant<bool, std::string, uint64_t>;
Ed Tanousaa2e59c2018-04-12 12:17:20 -070034using ManagedObjectsType = std::vector<std::pair<
35 sdbusplus::message::object_path,
36 std::vector<std::pair<std::string,
37 std::vector<std::pair<std::string, VariantType>>>>>>;
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010038
Ed Tanousaa2e59c2018-04-12 12:17:20 -070039using PropertiesType = boost::container::flat_map<std::string, VariantType>;
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010040
Qiang XUc1819422019-02-27 13:51:32 +080041void getIntrusionByService(std::shared_ptr<AsyncResp> aResp,
42 const std::string &service,
43 const std::string &objPath)
44{
45 BMCWEB_LOG_DEBUG << "Get intrusion status by service \n";
46
47 crow::connections::systemBus->async_method_call(
48 [aResp{std::move(aResp)}](const boost::system::error_code ec,
49 const std::variant<std::string> &value) {
50 if (ec)
51 {
52 // do not add err msg in redfish response, becaues this is not
53 // mandatory property
54 BMCWEB_LOG_ERROR << "DBUS response error " << ec << "\n";
55 return;
56 }
57
58 const std::string *status = std::get_if<std::string>(&value);
59
60 if (status == nullptr)
61 {
62 BMCWEB_LOG_ERROR << "intrusion status read error \n";
63 return;
64 }
65
66 aResp->res.jsonValue["PhysicalSecurity"] = {
67 {"IntrusionSensorNumber", 1}, {"IntrusionSensor", *status}};
68 },
69 service, objPath, "org.freedesktop.DBus.Properties", "Get",
70 "xyz.openbmc_project.Chassis.Intrusion", "Status");
71}
72
73/**
74 * Retrieves physical security properties over dbus
75 */
76void getPhysicalSecurityData(std::shared_ptr<AsyncResp> aResp)
77{
78 crow::connections::systemBus->async_method_call(
79 [aResp{std::move(aResp)}](
80 const boost::system::error_code ec,
81 const std::vector<std::pair<
82 std::string,
83 std::vector<std::pair<std::string, std::vector<std::string>>>>>
84 &subtree) {
85 if (ec)
86 {
87 // do not add err msg in redfish response, becaues this is not
88 // mandatory property
89 BMCWEB_LOG_ERROR << "DBUS error: no matched iface " << ec
90 << "\n";
91 return;
92 }
93 // Iterate over all retrieved ObjectPaths.
94 for (const auto &object : subtree)
95 {
96 for (const auto &service : object.second)
97 {
98 getIntrusionByService(aResp, service.first, object.first);
99 return;
100 }
101 }
102 },
103 "xyz.openbmc_project.ObjectMapper",
104 "/xyz/openbmc_project/object_mapper",
105 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
106 "/xyz/openbmc_project/Intrusion", int32_t(1),
107 std::array<const char *, 1>{"xyz.openbmc_project.Chassis.Intrusion"});
108}
109
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100110/**
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100111 * ChassisCollection derived class for delivering Chassis Collection Schema
112 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700113class ChassisCollection : public Node
114{
115 public:
116 ChassisCollection(CrowApp &app) : Node(app, "/redfish/v1/Chassis/")
117 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118 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 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100126
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127 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 {
Gunnar Mills8f1ac8e2018-12-06 15:52:46 -0600134 const std::array<const char *, 3> interfaces = {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700135 "xyz.openbmc_project.Inventory.Item.Board",
136 "xyz.openbmc_project.Inventory.Item.Chassis",
Gunnar Mills8f1ac8e2018-12-06 15:52:46 -0600137 "xyz.openbmc_project.Inventory.Item.PowerSupply"};
Ed Tanous0f74e642018-11-12 15:17:05 -0800138 res.jsonValue["@odata.type"] = "#ChassisCollection.ChassisCollection";
139 res.jsonValue["@odata.id"] = "/redfish/v1/Chassis";
140 res.jsonValue["@odata.context"] =
141 "/redfish/v1/$metadata#ChassisCollection.ChassisCollection";
142 res.jsonValue["Name"] = "Chassis Collection";
143
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700144 auto asyncResp = std::make_shared<AsyncResp>(res);
145 crow::connections::systemBus->async_method_call(
146 [asyncResp](const boost::system::error_code ec,
147 const std::vector<std::string> &chassisList) {
148 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700149 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700150 messages::internalError(asyncResp->res);
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700151 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700153 nlohmann::json &chassisArray =
154 asyncResp->res.jsonValue["Members"];
155 chassisArray = nlohmann::json::array();
156 for (const std::string &objpath : chassisList)
157 {
158 std::size_t lastPos = objpath.rfind("/");
159 if (lastPos == std::string::npos)
160 {
161 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath;
162 continue;
163 }
164 chassisArray.push_back(
165 {{"@odata.id", "/redfish/v1/Chassis/" +
166 objpath.substr(lastPos + 1)}});
167 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100168
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700169 asyncResp->res.jsonValue["Members@odata.count"] =
170 chassisArray.size();
171 },
172 "xyz.openbmc_project.ObjectMapper",
173 "/xyz/openbmc_project/object_mapper",
174 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
Gunnar Mills734bfe92019-01-21 16:33:50 -0600175 "/xyz/openbmc_project/inventory", int32_t(0), interfaces);
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700176 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100177};
178
179/**
180 * Chassis override class for delivering Chassis Schema
181 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700182class Chassis : public Node
183{
184 public:
185 Chassis(CrowApp &app) :
186 Node(app, "/redfish/v1/Chassis/<str>/", std::string())
187 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 entityPrivileges = {
189 {boost::beast::http::verb::get, {{"Login"}}},
190 {boost::beast::http::verb::head, {{"Login"}}},
191 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
192 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
193 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
194 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100195 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100196
Ed Tanous1abe55e2018-09-05 08:30:59 -0700197 private:
198 /**
199 * Functions triggers appropriate requests on DBus
200 */
201 void doGet(crow::Response &res, const crow::Request &req,
202 const std::vector<std::string> &params) override
203 {
Gunnar Mills734bfe92019-01-21 16:33:50 -0600204 const std::array<const char *, 3> interfaces = {
205 "xyz.openbmc_project.Inventory.Item.Board",
206 "xyz.openbmc_project.Inventory.Item.Chassis",
207 "xyz.openbmc_project.Inventory.Item.PowerSupply"};
208
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 // Check if there is required param, truly entering this shall be
210 // impossible.
211 if (params.size() != 1)
212 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700213 messages::internalError(res);
Ed Tanousdaf36e22018-04-20 16:01:36 -0700214 res.end();
215 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 }
Shawn McCarney99cffd72019-03-01 10:46:20 -0600217 const std::string &chassisId = params[0];
Ed Tanouse0d918b2018-03-27 17:41:04 -0700218
Ed Tanous0f74e642018-11-12 15:17:05 -0800219 res.jsonValue["@odata.type"] = "#Chassis.v1_4_0.Chassis";
Shawn McCarney99cffd72019-03-01 10:46:20 -0600220 res.jsonValue["@odata.id"] = "/redfish/v1/Chassis/" + chassisId;
Ed Tanous0f74e642018-11-12 15:17:05 -0800221 res.jsonValue["@odata.context"] =
222 "/redfish/v1/$metadata#Chassis.Chassis";
223 res.jsonValue["Name"] = "Chassis Collection";
224 res.jsonValue["ChassisType"] = "RackMount";
225 res.jsonValue["PowerState"] = "On";
226
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700227 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700229 [asyncResp, chassisId(std::string(chassisId))](
230 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 const std::vector<std::pair<
232 std::string, std::vector<std::pair<
233 std::string, std::vector<std::string>>>>>
234 &subtree) {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700235 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700237 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 return;
239 }
240 // Iterate over all retrieved ObjectPaths.
241 for (const std::pair<
242 std::string,
243 std::vector<
244 std::pair<std::string, std::vector<std::string>>>>
245 &object : subtree)
246 {
247 const std::string &path = object.first;
248 const std::vector<
249 std::pair<std::string, std::vector<std::string>>>
250 &connectionNames = object.second;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700251
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252 if (!boost::ends_with(path, chassisId))
253 {
254 continue;
Ed Tanousdaf36e22018-04-20 16:01:36 -0700255 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700256 if (connectionNames.size() < 1)
257 {
258 BMCWEB_LOG_ERROR << "Only got "
259 << connectionNames.size()
260 << " Connection names";
261 continue;
262 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700263
Ed Tanous1abe55e2018-09-05 08:30:59 -0700264 const std::string connectionName = connectionNames[0].first;
265 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700266 [asyncResp, chassisId(std::string(chassisId))](
267 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 const std::vector<std::pair<
269 std::string, VariantType>> &propertiesList) {
270 for (const std::pair<std::string, VariantType>
271 &property : propertiesList)
272 {
Shawn McCarney99cffd72019-03-01 10:46:20 -0600273 // Store DBus properties that are also Redfish
274 // properties with same name and a string value
275 const std::string &propertyName =
276 property.first;
277 if ((propertyName == "PartNumber") ||
278 (propertyName == "SerialNumber") ||
279 (propertyName == "Manufacturer") ||
280 (propertyName == "Model"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700281 {
Shawn McCarney99cffd72019-03-01 10:46:20 -0600282 const std::string *value =
283 std::get_if<std::string>(
284 &property.second);
285 if (value != nullptr)
286 {
287 asyncResp->res.jsonValue[propertyName] =
288 *value;
289 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290 }
291 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700292 asyncResp->res.jsonValue["Name"] = chassisId;
293 asyncResp->res.jsonValue["Id"] = chassisId;
294 asyncResp->res.jsonValue["Thermal"] = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700295 {"@odata.id", "/redfish/v1/Chassis/" +
296 chassisId + "/Thermal"}};
Ed Tanous2474adf2018-09-05 16:31:16 -0700297 // Power object
298 asyncResp->res.jsonValue["Power"] = {
299 {"@odata.id", "/redfish/v1/Chassis/" +
300 chassisId + "/Power"}};
Ed Tanous029573d2019-02-01 10:57:49 -0800301 asyncResp->res.jsonValue["Status"] = {
302 {"Health", "OK"},
303 {"State", "Enabled"},
304 };
Ed Tanous2474adf2018-09-05 16:31:16 -0700305
Ed Tanous029573d2019-02-01 10:57:49 -0800306 asyncResp->res
307 .jsonValue["Links"]["ComputerSystems"] = {
308 {{"@odata.id", "/redfish/v1/Systems/system"}}};
309 asyncResp->res.jsonValue["Links"]["ManagedBy"] = {
310 {{"@odata.id", "/redfish/v1/Managers/bmc"}}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700311 },
312 connectionName, path, "org.freedesktop.DBus.Properties",
313 "GetAll",
314 "xyz.openbmc_project.Inventory.Decorator.Asset");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315 return;
316 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700317
Ed Tanous1abe55e2018-09-05 08:30:59 -0700318 // Couldn't find an object with that name. return an error
Jason M. Billsf12894f2018-10-09 12:45:45 -0700319 messages::resourceNotFound(
320 asyncResp->res, "#Chassis.v1_4_0.Chassis", chassisId);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 },
322 "xyz.openbmc_project.ObjectMapper",
323 "/xyz/openbmc_project/object_mapper",
324 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
Gunnar Mills734bfe92019-01-21 16:33:50 -0600325 "/xyz/openbmc_project/inventory", int32_t(0), interfaces);
Qiang XUc1819422019-02-27 13:51:32 +0800326
327 getPhysicalSecurityData(asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700328 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700329};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330} // namespace redfish