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 "bmcweb_config.h"
19 
20 #include "app.hpp"
21 #include "async_resp.hpp"
22 #include "http_request.hpp"
23 #include "persistent_data.hpp"
24 #include "query.hpp"
25 #include "registries/privilege_registry.hpp"
26 #include "utils/systemd_utils.hpp"
27 
28 #include <nlohmann/json.hpp>
29 
30 namespace redfish
31 {
32 
33 inline void
34     handleServiceRootHead(App& app, const crow::Request& req,
35                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
36 {
37     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
38     {
39         return;
40     }
41 
42     asyncResp->res.addHeader(
43         boost::beast::http::field::link,
44         "</redfish/v1/JsonSchemas/ServiceRoot/ServiceRoot.json>; rel=describedby");
45 }
46 
47 inline void handleServiceRootGetImpl(
48     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
49 {
50     asyncResp->res.addHeader(
51         boost::beast::http::field::link,
52         "</redfish/v1/JsonSchemas/ServiceRoot/ServiceRoot.json>; rel=describedby");
53 
54     std::string uuid = persistent_data::getConfig().systemUuid;
55     asyncResp->res.jsonValue["@odata.type"] =
56         "#ServiceRoot.v1_15_0.ServiceRoot";
57     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1";
58     asyncResp->res.jsonValue["Id"] = "RootService";
59     asyncResp->res.jsonValue["Name"] = "Root Service";
60     asyncResp->res.jsonValue["RedfishVersion"] = "1.17.0";
61     asyncResp->res.jsonValue["Links"]["Sessions"]["@odata.id"] =
62         "/redfish/v1/SessionService/Sessions";
63     asyncResp->res.jsonValue["AccountService"]["@odata.id"] =
64         "/redfish/v1/AccountService";
65 #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
66     asyncResp->res.jsonValue["AggregationService"]["@odata.id"] =
67         "/redfish/v1/AggregationService";
68 #endif
69     asyncResp->res.jsonValue["Chassis"]["@odata.id"] = "/redfish/v1/Chassis";
70     asyncResp->res.jsonValue["JsonSchemas"]["@odata.id"] =
71         "/redfish/v1/JsonSchemas";
72     asyncResp->res.jsonValue["Managers"]["@odata.id"] = "/redfish/v1/Managers";
73     asyncResp->res.jsonValue["SessionService"]["@odata.id"] =
74         "/redfish/v1/SessionService";
75     asyncResp->res.jsonValue["Systems"]["@odata.id"] = "/redfish/v1/Systems";
76     asyncResp->res.jsonValue["Registries"]["@odata.id"] =
77         "/redfish/v1/Registries";
78     asyncResp->res.jsonValue["UpdateService"]["@odata.id"] =
79         "/redfish/v1/UpdateService";
80     asyncResp->res.jsonValue["UUID"] = uuid;
81     asyncResp->res.jsonValue["CertificateService"]["@odata.id"] =
82         "/redfish/v1/CertificateService";
83     asyncResp->res.jsonValue["Tasks"]["@odata.id"] = "/redfish/v1/TaskService";
84     asyncResp->res.jsonValue["EventService"]["@odata.id"] =
85         "/redfish/v1/EventService";
86     asyncResp->res.jsonValue["TelemetryService"]["@odata.id"] =
87         "/redfish/v1/TelemetryService";
88     asyncResp->res.jsonValue["Cables"]["@odata.id"] = "/redfish/v1/Cables";
89 
90     asyncResp->res.jsonValue["Links"]["ManagerProvidingService"]["@odata.id"] =
91         "/redfish/v1/Managers/bmc";
92 
93     nlohmann::json& protocolFeatures =
94         asyncResp->res.jsonValue["ProtocolFeaturesSupported"];
95     protocolFeatures["ExcerptQuery"] = false;
96 
97     protocolFeatures["ExpandQuery"]["ExpandAll"] =
98         bmcwebInsecureEnableQueryParams;
99     // This is the maximum level defined in ServiceRoot.v1_13_0.json
100     if (bmcwebInsecureEnableQueryParams)
101     {
102         protocolFeatures["ExpandQuery"]["MaxLevels"] = 6;
103     }
104     protocolFeatures["ExpandQuery"]["Levels"] = bmcwebInsecureEnableQueryParams;
105     protocolFeatures["ExpandQuery"]["Links"] = bmcwebInsecureEnableQueryParams;
106     protocolFeatures["ExpandQuery"]["NoLinks"] =
107         bmcwebInsecureEnableQueryParams;
108     protocolFeatures["FilterQuery"] = false;
109     protocolFeatures["OnlyMemberQuery"] = true;
110     protocolFeatures["SelectQuery"] = true;
111     protocolFeatures["DeepOperations"]["DeepPOST"] = false;
112     protocolFeatures["DeepOperations"]["DeepPATCH"] = false;
113 }
114 inline void
115     handleServiceRootGet(App& app, const crow::Request& req,
116                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
117 {
118     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
119     {
120         return;
121     }
122 
123     handleServiceRootGetImpl(asyncResp);
124 }
125 
126 inline void requestRoutesServiceRoot(App& app)
127 {
128     BMCWEB_ROUTE(app, "/redfish/v1/")
129         .privileges(redfish::privileges::headServiceRoot)
130         .methods(boost::beast::http::verb::head)(
131             std::bind_front(handleServiceRootHead, std::ref(app)));
132     BMCWEB_ROUTE(app, "/redfish/v1/")
133         .privileges(redfish::privileges::getServiceRoot)
134         .methods(boost::beast::http::verb::get)(
135             std::bind_front(handleServiceRootGet, std::ref(app)));
136 }
137 
138 } // namespace redfish
139