blob: fa10d5c6ec4a5b083959d1b0d048e87468485614 [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 {
48 Node::json["@odata.type"] = "#ChassisCollection.ChassisCollection";
49 Node::json["@odata.id"] = "/redfish/v1/Chassis";
50 Node::json["@odata.context"] =
51 "/redfish/v1/$metadata#ChassisCollection.ChassisCollection";
52 Node::json["Name"] = "Chassis Collection";
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010053
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 entityPrivileges = {
55 {boost::beast::http::verb::get, {{"Login"}}},
56 {boost::beast::http::verb::head, {{"Login"}}},
57 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
58 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
59 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
60 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
61 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +010062
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 private:
64 /**
65 * Functions triggers appropriate requests on DBus
66 */
67 void doGet(crow::Response &res, const crow::Request &req,
68 const std::vector<std::string> &params) override
69 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -070070 const std::array<const char *, 4> interfaces = {
71 "xyz.openbmc_project.Inventory.Item.Board",
72 "xyz.openbmc_project.Inventory.Item.Chassis",
73 "xyz.openbmc_project.Inventory.Item.PowerSupply",
74 "xyz.openbmc_project.Inventory.Item.System",
75 };
76 res.jsonValue = Node::json;
77 auto asyncResp = std::make_shared<AsyncResp>(res);
78 crow::connections::systemBus->async_method_call(
79 [asyncResp](const boost::system::error_code ec,
80 const std::vector<std::string> &chassisList) {
81 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -070082 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -070083 messages::addMessageToErrorJson(asyncResp->res.jsonValue,
84 messages::internalError());
85 asyncResp->res.result(
Ed Tanous1abe55e2018-09-05 08:30:59 -070086 boost::beast::http::status::internal_server_error);
Ed Tanous62d5e2e2018-09-05 16:17:25 -070087 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -070088 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -070089 nlohmann::json &chassisArray =
90 asyncResp->res.jsonValue["Members"];
91 chassisArray = nlohmann::json::array();
92 for (const std::string &objpath : chassisList)
93 {
94 std::size_t lastPos = objpath.rfind("/");
95 if (lastPos == std::string::npos)
96 {
97 BMCWEB_LOG_ERROR << "Failed to find '/' in " << objpath;
98 continue;
99 }
100 chassisArray.push_back(
101 {{"@odata.id", "/redfish/v1/Chassis/" +
102 objpath.substr(lastPos + 1)}});
103 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100104
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700105 asyncResp->res.jsonValue["Members@odata.count"] =
106 chassisArray.size();
107 },
108 "xyz.openbmc_project.ObjectMapper",
109 "/xyz/openbmc_project/object_mapper",
110 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths",
111 "/xyz/openbmc_project/inventory", int32_t(3), interfaces);
112 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100113};
114
115/**
116 * Chassis override class for delivering Chassis Schema
117 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700118class Chassis : public Node
119{
120 public:
121 Chassis(CrowApp &app) :
122 Node(app, "/redfish/v1/Chassis/<str>/", std::string())
123 {
124 Node::json["@odata.type"] = "#Chassis.v1_4_0.Chassis";
125 Node::json["@odata.id"] = "/redfish/v1/Chassis";
126 Node::json["@odata.context"] = "/redfish/v1/$metadata#Chassis.Chassis";
127 Node::json["Name"] = "Chassis Collection";
128 Node::json["ChassisType"] = "RackMount";
Ed Tanousaef72902018-10-12 13:45:21 -0700129 Node::json["PowerState"] = "On";
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100130
Ed Tanous1abe55e2018-09-05 08:30:59 -0700131 entityPrivileges = {
132 {boost::beast::http::verb::get, {{"Login"}}},
133 {boost::beast::http::verb::head, {{"Login"}}},
134 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
135 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
136 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
137 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100138 }
Rapkiewicz, Pawele37f8452018-03-09 13:49:50 +0100139
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 private:
141 /**
142 * Functions triggers appropriate requests on DBus
143 */
144 void doGet(crow::Response &res, const crow::Request &req,
145 const std::vector<std::string> &params) override
146 {
147 // Check if there is required param, truly entering this shall be
148 // impossible.
149 if (params.size() != 1)
150 {
Ed Tanouse0d918b2018-03-27 17:41:04 -0700151 res.result(boost::beast::http::status::internal_server_error);
Ed Tanousdaf36e22018-04-20 16:01:36 -0700152 res.end();
153 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700154 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700155
Ed Tanous1abe55e2018-09-05 08:30:59 -0700156 res.jsonValue = Node::json;
157 const std::string &chassisId = params[0];
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700158 auto asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700159 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700160 [asyncResp, chassisId(std::string(chassisId))](
161 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 const std::vector<std::pair<
163 std::string, std::vector<std::pair<
164 std::string, std::vector<std::string>>>>>
165 &subtree) {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700166 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700168 messages::addMessageToErrorJson(asyncResp->res.jsonValue,
169 messages::internalError());
170 asyncResp->res.result(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171 boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 return;
173 }
174 // Iterate over all retrieved ObjectPaths.
175 for (const std::pair<
176 std::string,
177 std::vector<
178 std::pair<std::string, std::vector<std::string>>>>
179 &object : subtree)
180 {
181 const std::string &path = object.first;
182 const std::vector<
183 std::pair<std::string, std::vector<std::string>>>
184 &connectionNames = object.second;
Ed Tanouse0d918b2018-03-27 17:41:04 -0700185
Ed Tanous1abe55e2018-09-05 08:30:59 -0700186 if (!boost::ends_with(path, chassisId))
187 {
188 continue;
Ed Tanousdaf36e22018-04-20 16:01:36 -0700189 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 if (connectionNames.size() < 1)
191 {
192 BMCWEB_LOG_ERROR << "Only got "
193 << connectionNames.size()
194 << " Connection names";
195 continue;
196 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700197
Ed Tanous1abe55e2018-09-05 08:30:59 -0700198 const std::string connectionName = connectionNames[0].first;
199 crow::connections::systemBus->async_method_call(
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700200 [asyncResp, chassisId(std::string(chassisId))](
201 const boost::system::error_code ec,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202 const std::vector<std::pair<
203 std::string, VariantType>> &propertiesList) {
204 for (const std::pair<std::string, VariantType>
205 &property : propertiesList)
206 {
207 const std::string *value =
208 mapbox::getPtr<const std::string>(
209 property.second);
210 if (value != nullptr)
211 {
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700212 asyncResp->res.jsonValue[property.first] =
213 *value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700214 }
215 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700216 asyncResp->res.jsonValue["Name"] = chassisId;
217 asyncResp->res.jsonValue["Id"] = chassisId;
218 asyncResp->res.jsonValue["Thermal"] = {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {"@odata.id", "/redfish/v1/Chassis/" +
220 chassisId + "/Thermal"}};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 },
222 connectionName, path, "org.freedesktop.DBus.Properties",
223 "GetAll",
224 "xyz.openbmc_project.Inventory.Decorator.Asset");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 return;
226 }
Ed Tanouse0d918b2018-03-27 17:41:04 -0700227
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 // Couldn't find an object with that name. return an error
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700229 asyncResp->res.jsonValue = redfish::messages::resourceNotFound(
230 "#Chassis.v1_4_0.Chassis", chassisId);
231 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 },
233 "xyz.openbmc_project.ObjectMapper",
234 "/xyz/openbmc_project/object_mapper",
235 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
236 "/xyz/openbmc_project/inventory", int32_t(0),
237 std::array<const char *, 1>{
238 "xyz.openbmc_project.Inventory.Decorator.Asset"});
239 }
Ed Tanous62d5e2e2018-09-05 16:17:25 -0700240};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241} // namespace redfish