blob: 0c070edc28641fdeda550aaa4e89a92194ed531b [file] [log] [blame]
Ed Tanous911ac312017-08-15 09:37:42 -07001#pragma once
2
Ed Tanous911ac312017-08-15 09:37:42 -07003#include <dbus/connection.hpp>
4#include <dbus/endpoint.hpp>
5#include <dbus/filter.hpp>
6#include <dbus/match.hpp>
7#include <dbus/message.hpp>
Ed Tanousba9f9a62017-10-11 16:40:35 -07008#include <persistent_data_middleware.hpp>
9#include <token_authorization_middleware.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070010#include <fstream>
Ed Tanousba9f9a62017-10-11 16:40:35 -070011#include <streambuf>
12#include <string>
Ed Tanous2a866f82017-10-25 17:46:24 -070013#include <crow/app.h>
14#include <boost/algorithm/string.hpp>
Ed Tanous3dac7492017-08-02 13:46:20 -070015namespace crow {
16namespace redfish {
17
18template <typename... Middlewares>
Ed Tanousba9f9a62017-10-11 16:40:35 -070019void get_redfish_sub_routes(Crow<Middlewares...>& app, const std::string& url,
20 nlohmann::json& j) {
Ed Tanous1c74de82017-10-26 13:58:28 -070021 std::vector<const std::string*> routes = app.get_routes(url);
22 for (auto route : routes) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070023 auto redfish_sub_route =
Ed Tanous1c74de82017-10-26 13:58:28 -070024 route->substr(url.size(), route->size() - url.size() - 1);
Ed Tanousba9f9a62017-10-11 16:40:35 -070025 // check if the route is at this level, and we didn't find and exact match
26 // also, filter out resources that start with $ to remove $metadata
27 if (!redfish_sub_route.empty() && redfish_sub_route[0] != '$' &&
28 redfish_sub_route.find('/') == std::string::npos) {
Ed Tanous1c74de82017-10-26 13:58:28 -070029 j[redfish_sub_route] = nlohmann::json{{"@odata.id", *route}};
Ed Tanous911ac312017-08-15 09:37:42 -070030 }
Ed Tanousba9f9a62017-10-11 16:40:35 -070031 }
32}
Ed Tanous911ac312017-08-15 09:37:42 -070033
Ed Tanous2a866f82017-10-25 17:46:24 -070034std::string execute_process(const char* cmd) {
35 std::array<char, 128> buffer;
36 std::string result;
37 std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
38 if (!pipe) throw std::runtime_error("popen() failed!");
39 while (!feof(pipe.get())) {
40 if (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
41 result += buffer.data();
42 }
43 return result;
44}
45
Ed Tanousba9f9a62017-10-11 16:40:35 -070046template <typename... Middlewares>
47void request_routes(Crow<Middlewares...>& app) {
48 CROW_ROUTE(app, "/redfish/")
Ed Tanous911ac312017-08-15 09:37:42 -070049 .methods("GET"_method)([](const crow::request& req, crow::response& res) {
Ed Tanousba9f9a62017-10-11 16:40:35 -070050 res.json_value = {{"v1", "/redfish/v1/"}};
51 res.end();
52 });
53
Ed Tanousba9f9a62017-10-11 16:40:35 -070054 CROW_ROUTE(app, "/redfish/v1/Chassis/")
55 .methods("GET"_method)(
56 [&](const crow::request& req, crow::response& res) {
57 std::vector<std::string> entities;
58 /*std::ifstream f("~/system.json");
59
60 nlohmann::json input = nlohmann::json::parse(f);
61 for (auto it = input.begin(); it != input.end(); it++) {
62 auto value = it.value();
63 if (value["type"] == "Chassis") {
64 std::string str = value["name"];
65 entities.emplace_back(str);
66 }
67 }
68 */
Ed Tanous1c74de82017-10-26 13:58:28 -070069
Ed Tanousba9f9a62017-10-11 16:40:35 -070070 res.json_value = {
71 {"@odata.context",
72 "/redfish/v1/$metadata#ChassisCollection.ChassisCollection"},
73 {"@odata.id", "/redfish/v1/Chassis"},
74 {"@odata.type", "#ChassisCollection.ChassisCollection"},
75 {"Name", "Chassis Collection"},
76 {"Members@odata.count", entities.size()}};
77
78 get_redfish_sub_routes(app, "/redfish/v1/Chassis", res.json_value);
79 res.end();
80 });
81
Ed Tanousba9f9a62017-10-11 16:40:35 -070082 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/")
83 .methods(
84 "GET"_method)([&](const crow::request& req, crow::response& res) {
Ed Tanous911ac312017-08-15 09:37:42 -070085 boost::asio::io_service io;
86 auto bus = std::make_shared<dbus::connection>(io, dbus::bus::session);
87 dbus::endpoint user_list("org.openbmc.UserManager",
88 "/org/openbmc/UserManager/Users",
89 "org.openbmc.Enrol", "UserList");
90 bus->async_method_call(
91 [&](const boost::system::error_code ec,
Ed Tanousba9f9a62017-10-11 16:40:35 -070092 const std::vector<std::string>& users) {
Ed Tanous911ac312017-08-15 09:37:42 -070093 if (ec) {
94 res.code = 500;
95 } else {
Ed Tanousba9f9a62017-10-11 16:40:35 -070096 res.json_value = {
Ed Tanous911ac312017-08-15 09:37:42 -070097 {"@odata.context",
98 "/redfish/v1/"
99 "$metadata#ManagerAccountCollection."
100 "ManagerAccountCollection"},
101 {"@odata.id", "/redfish/v1/AccountService/Accounts"},
102 {"@odata.type",
103 "#ManagerAccountCollection.ManagerAccountCollection"},
104 {"Name", "Accounts Collection"},
105 {"Description", "BMC User Accounts"},
106 {"Members@odata.count", users.size()}};
Ed Tanousba9f9a62017-10-11 16:40:35 -0700107 nlohmann::json member_array = nlohmann::json::array();
Ed Tanous911ac312017-08-15 09:37:42 -0700108 int user_index = 0;
Ed Tanousc963aa42017-10-27 16:00:19 -0700109 for (int user_index = 0; user_index < users.size();
110 user_index++) {
Ed Tanous911ac312017-08-15 09:37:42 -0700111 member_array.push_back(
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100112 {{"@odata.id", "/redfish/v1/AccountService/Accounts/" +
113 std::to_string(user_index)}});
Ed Tanous911ac312017-08-15 09:37:42 -0700114 }
Ed Tanousba9f9a62017-10-11 16:40:35 -0700115 res.json_value["Members"] = member_array;
Ed Tanous911ac312017-08-15 09:37:42 -0700116 }
117 res.end();
Ed Tanousba9f9a62017-10-11 16:40:35 -0700118 },
119 user_list);
Ed Tanous3dac7492017-08-02 13:46:20 -0700120 });
121
122 CROW_ROUTE(app, "/redfish/v1/AccountService/Accounts/<int>/")
Ed Tanousba9f9a62017-10-11 16:40:35 -0700123 .methods("GET"_method)([](const crow::request& req, crow::response& res,
124 int account_index) {
125 res.json_value = {
Ed Tanous3dac7492017-08-02 13:46:20 -0700126 {"@odata.context",
127 "/redfish/v1/$metadata#ManagerAccount.ManagerAccount"},
128 {"@odata.id", "/redfish/v1/AccountService/Accounts/1"},
129 {"@odata.type", "#ManagerAccount.v1_0_3.ManagerAccount"},
130 {"Id", "1"},
131 {"Name", "User Account"},
132 {"Description", "User Account"},
133 {"Enabled", false},
134 {"Password", nullptr},
135 {"UserName", "anonymous"},
136 {"RoleId", "NoAccess"},
137 {"Links",
138 {{"Role",
139 {{"@odata.id", "/redfish/v1/AccountService/Roles/NoAccess"}}}}}};
Ed Tanousba9f9a62017-10-11 16:40:35 -0700140 res.end();
141 });
142
143 CROW_ROUTE(app, "/redfish/v1/SessionService/")
144 .methods(
145 "GET"_method)([&](const crow::request& req, crow::response& res) {
146 res.json_value = {
147 {"@odata.context",
148 "/redfish/v1/$metadata#SessionService.SessionService"},
149 {"@odata.id", "/redfish/v1/SessionService"},
150 {"@odata.type", "#SessionService.v1_1_1.SessionService"},
151 {"Id", "SessionService"},
152 {"Name", "SessionService"},
153 {"Description", "SessionService"},
154 {"Status",
155 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
156 {"ServiceEnabled", true},
157 // TODO(ed) converge with session timeouts once they exist
158 // Bogus number for now
159 {"SessionTimeout", 1800}};
160 get_redfish_sub_routes(app, "/redfish/v1/AccountService",
161 res.json_value);
162 res.end();
163 });
164
Ed Tanous2a866f82017-10-25 17:46:24 -0700165 CROW_ROUTE(app, "/redfish/v1/Managers/")
166 .methods("GET"_method)(
167 [&](const crow::request& req, crow::response& res) {
168 res.json_value = {
169 {"@odata.context",
170 "/redfish/v1/$metadata#ManagerCollection.ManagerCollection"},
171 {"@odata.id", "/redfish/v1/Managers"},
172 {"@odata.type", "#ManagerCollection.ManagerCollection"},
173 {"Name", "Manager Collection"},
174 {"Members@odata.count", 1},
175 {"Members", {{{"@odata.id", "/redfish/v1/Managers/openbmc"}}}}};
176 res.end();
177 });
178
179 CROW_ROUTE(app, "/redfish/v1/Managers/openbmc/")
180 .methods(
181 "GET"_method)([&](const crow::request& req, crow::response& res) {
182 time_t t = time(NULL);
183 tm* mytime = std::localtime(&t);
184 if (mytime == nullptr) {
185 res.code = 500;
186 res.end();
187 return;
188 }
189 std::array<char, 100> time_buffer;
190 std::size_t len = std::strftime(time_buffer.data(), time_buffer.size(),
191 "%FT%TZ", mytime);
192 if (len == 0) {
193 res.code = 500;
194 res.end();
195 return;
196 }
197 res.json_value = {
198 {"@odata.context", "/redfish/v1/$metadata#Manager.Manager"},
199 {"@odata.id", "/redfish/v1/Managers/openbmc"},
200 {"@odata.type", "#Manager.v1_3_0.Manager"},
201 {"Id", "openbmc"},
202 {"Name", "OpenBmc Manager"},
203 {"Description", "Baseboard Management Controller"},
Kowalski, Kamil2b7981f2018-01-31 13:24:59 +0100204 {"UUID", app.template get_middleware<PersistentData::Middleware>()
205 .system_uuid},
Ed Tanous2a866f82017-10-25 17:46:24 -0700206 {"Model", "OpenBmc"}, // TODO(ed), get model
207 {"DateTime", time_buffer.data()},
208 {"Status",
209 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
210 {"FirmwareVersion", "1234456789"}, // TODO(ed) get fwversion
211 {"PowerState", "On"}};
212 get_redfish_sub_routes(app, "/redfish/v1/Managers/openbmc/",
213 res.json_value);
214 res.end();
215 });
216
217 CROW_ROUTE(app, "/redfish/v1/Managers/NetworkProtocol/")
218 .methods(
219 "GET"_method)([&](const crow::request& req, crow::response& res) {
220 std::array<char, HOST_NAME_MAX> hostname;
221 if (gethostname(hostname.data(), hostname.size()) != 0) {
222 res.code = 500;
223 res.end();
224 return;
225 }
226 res.json_value = {
227 {"@odata.context",
228 "/redfish/v1/"
229 "$metadata#ManagerNetworkProtocol.ManagerNetworkProtocol"},
230 {"@odata.id", "/redfish/v1/Managers/BMC/NetworkProtocol"},
231 {"@odata.type",
232 "#ManagerNetworkProtocol.v1_1_0.ManagerNetworkProtocol"},
233 {"Id", "NetworkProtocol"},
234 {"Name", "Manager Network Protocol"},
235 {"Description", "Manager Network Service"},
236 {"Status",
237 {{"State", "Enabled"}, {"Health", "OK"}, {"HealthRollup", "OK"}}},
238 {"HostName", hostname.data()}}; // TODO(ed) get hostname
239 std::string netstat_out = execute_process("netstat -tuln");
240
241 std::map<int, const char*> service_types{{22, "SSH"},
242 {443, "HTTPS"},
243 {1900, "SSDP"},
244 {623, "IPMI"},
245 {427, "SLP"}};
246
247 std::vector<std::string> lines;
248 boost::split(lines, netstat_out, boost::is_any_of("\n"));
249 auto lines_it = lines.begin();
250 lines_it++; // skip the netstat header
251 lines_it++;
252 while (lines_it != lines.end()) {
253 std::vector<std::string> columns;
254 boost::split(columns, *lines_it, boost::is_any_of("\t "),
255 boost::token_compress_on);
256 if (columns.size() >= 5) {
257 std::size_t found = columns[3].find_last_of(":");
258 if (found != std::string::npos) {
259 std::string port_str = columns[3].substr(found + 1);
260 int port = std::stoi(port_str.c_str());
261 auto type_it = service_types.find(port);
262 if (type_it != service_types.end()) {
Ed Tanous2a866f82017-10-25 17:46:24 -0700263 res.json_value[type_it->second] = {{"ProtocolEnabled", true},
264 {"Port", port}};
265 }
266 }
267 }
268 lines_it++;
269 }
270
271 get_redfish_sub_routes(app, "/redfish/v1/", res.json_value);
272 res.end();
273 });
Ed Tanous3dac7492017-08-02 13:46:20 -0700274}
Ed Tanous911ac312017-08-15 09:37:42 -0700275} // namespace redfish
276} // namespace crow