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 "node.hpp"
19 
20 namespace redfish
21 {
22 
23 class Manager : public Node
24 {
25   public:
26     Manager(CrowApp& app) : Node(app, "/redfish/v1/Managers/openbmc/")
27     {
28         Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc";
29         Node::json["@odata.type"] = "#Manager.v1_3_0.Manager";
30         Node::json["@odata.context"] = "/redfish/v1/$metadata#Manager.Manager";
31         Node::json["Id"] = "openbmc";
32         Node::json["Name"] = "OpenBmc Manager";
33         Node::json["Description"] = "Baseboard Management Controller";
34         Node::json["PowerState"] = "On";
35         Node::json["UUID"] =
36             app.template getMiddleware<crow::persistent_data::Middleware>()
37                 .systemUuid;
38         Node::json["Model"] = "OpenBmc";              // TODO(ed), get model
39         Node::json["FirmwareVersion"] = "1234456789"; // TODO(ed), get fwversion
40         Node::json["EthernetInterfaces"] = nlohmann::json(
41             {{"@odata.id", "/redfish/v1/Managers/openbmc/"
42                            "EthernetInterfaces"}}); // TODO(Pawel),
43                                                     // remove this
44                                                     // when
45                                                     // subroutes
46                                                     // will work
47                                                     // correctly
48 
49         entityPrivileges = {
50             {boost::beast::http::verb::get, {{"Login"}}},
51             {boost::beast::http::verb::head, {{"Login"}}},
52             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
53             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
54             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
55             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
56     }
57 
58   private:
59     void doGet(crow::Response& res, const crow::Request& req,
60                const std::vector<std::string>& params) override
61     {
62         Node::json["DateTime"] = getDateTime();
63         // Copy over the static data to include the entries added by SubRoute
64         res.jsonValue = Node::json;
65         res.end();
66     }
67 
68     std::string getDateTime() const
69     {
70         std::array<char, 128> dateTime;
71         std::string redfishDateTime("0000-00-00T00:00:00Z00:00");
72         std::time_t time = std::time(nullptr);
73 
74         if (std::strftime(dateTime.begin(), dateTime.size(), "%FT%T%z",
75                           std::localtime(&time)))
76         {
77             // insert the colon required by the ISO 8601 standard
78             redfishDateTime = std::string(dateTime.data());
79             redfishDateTime.insert(redfishDateTime.end() - 2, ':');
80         }
81 
82         return redfishDateTime;
83     }
84 };
85 
86 class ManagerCollection : public Node
87 {
88   public:
89     ManagerCollection(CrowApp& app) : Node(app, "/redfish/v1/Managers/")
90     {
91         Node::json["@odata.id"] = "/redfish/v1/Managers";
92         Node::json["@odata.type"] = "#ManagerCollection.ManagerCollection";
93         Node::json["@odata.context"] =
94             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
95         Node::json["Name"] = "Manager Collection";
96         Node::json["Members@odata.count"] = 1;
97         Node::json["Members"] = {
98             {{"@odata.id", "/redfish/v1/Managers/openbmc"}}};
99 
100         entityPrivileges = {
101             {boost::beast::http::verb::get, {{"Login"}}},
102             {boost::beast::http::verb::head, {{"Login"}}},
103             {boost::beast::http::verb::patch, {{"ConfigureManager"}}},
104             {boost::beast::http::verb::put, {{"ConfigureManager"}}},
105             {boost::beast::http::verb::delete_, {{"ConfigureManager"}}},
106             {boost::beast::http::verb::post, {{"ConfigureManager"}}}};
107     }
108 
109   private:
110     void doGet(crow::Response& res, const crow::Request& req,
111                const std::vector<std::string>& params) override
112     {
113         // Collections don't include the static data added by SubRoute because
114         // it has a duplicate entry for members
115         res.jsonValue["@odata.id"] = "/redfish/v1/Managers";
116         res.jsonValue["@odata.type"] = "#ManagerCollection.ManagerCollection";
117         res.jsonValue["@odata.context"] =
118             "/redfish/v1/$metadata#ManagerCollection.ManagerCollection";
119         res.jsonValue["Name"] = "Manager Collection";
120         res.jsonValue["Members@odata.count"] = 1;
121         res.jsonValue["Members"] = {
122             {{"@odata.id", "/redfish/v1/Managers/openbmc"}}};
123         res.end();
124     }
125 };
126 
127 } // namespace redfish
128