xref: /openbmc/bmcweb/features/redfish/lib/message_registries.hpp (revision 5f2b84ee090bb9d361de9c294aa85cecf3962c11)
1 /*
2 // Copyright (c) 2019 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 "registries.hpp"
19 #include "registries/base_message_registry.hpp"
20 #include "registries/openbmc_message_registry.hpp"
21 #include "registries/resource_event_message_registry.hpp"
22 #include "registries/task_event_message_registry.hpp"
23 
24 #include <app.hpp>
25 #include <registries/privilege_registry.hpp>
26 
27 namespace redfish
28 {
29 
30 inline void handleMessageRegistryFileCollectionGet(
31     const crow::Request& /*req*/,
32     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
33 {
34     // Collections don't include the static data added by SubRoute
35     // because it has a duplicate entry for members
36 
37     asyncResp->res.jsonValue = {
38         {"@odata.type",
39          "#MessageRegistryFileCollection.MessageRegistryFileCollection"},
40         {"@odata.id", "/redfish/v1/Registries"},
41         {"Name", "MessageRegistryFile Collection"},
42         {"Description", "Collection of MessageRegistryFiles"},
43         {"Members@odata.count", 4},
44         {"Members",
45          {{{"@odata.id", "/redfish/v1/Registries/Base"}},
46           {{"@odata.id", "/redfish/v1/Registries/TaskEvent"}},
47           {{"@odata.id", "/redfish/v1/Registries/ResourceEvent"}},
48           {{"@odata.id", "/redfish/v1/Registries/OpenBMC"}}}}};
49 }
50 
51 inline void requestRoutesMessageRegistryFileCollection(App& app)
52 {
53     /**
54      * Functions triggers appropriate requests on DBus
55      */
56     BMCWEB_ROUTE(app, "/redfish/v1/Registries/")
57         .privileges(redfish::privileges::getMessageRegistryFileCollection)
58         .methods(boost::beast::http::verb::get)(
59             handleMessageRegistryFileCollectionGet);
60 }
61 
62 inline void handleMessageRoutesMessageRegistryFileGet(
63     const crow::Request& /*req*/,
64     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
65     const std::string& registry)
66 {
67     const registries::Header* header = nullptr;
68     std::string dmtf = "DMTF ";
69     const char* url = nullptr;
70 
71     if (registry == "Base")
72     {
73         header = &registries::base::header;
74         url = registries::base::url;
75     }
76     else if (registry == "TaskEvent")
77     {
78         header = &registries::task_event::header;
79         url = registries::task_event::url;
80     }
81     else if (registry == "OpenBMC")
82     {
83         header = &registries::openbmc::header;
84         dmtf.clear();
85     }
86     else if (registry == "ResourceEvent")
87     {
88         header = &registries::resource_event::header;
89         url = registries::resource_event::url;
90     }
91     else
92     {
93         messages::resourceNotFound(
94             asyncResp->res, "#MessageRegistryFile.v1_1_0.MessageRegistryFile",
95             registry);
96         return;
97     }
98 
99     asyncResp->res.jsonValue = {
100         {"@odata.id", "/redfish/v1/Registries/" + registry},
101         {"@odata.type", "#MessageRegistryFile.v1_1_0.MessageRegistryFile"},
102         {"Name", registry + " Message Registry File"},
103         {"Description", dmtf + registry + " Message Registry File Location"},
104         {"Id", header->registryPrefix},
105         {"Registry", header->id},
106         {"Languages", {"en"}},
107         {"Languages@odata.count", 1},
108         {"Location",
109          {{{"Language", "en"},
110            {"Uri", "/redfish/v1/Registries/" + registry + "/" + registry}}}},
111         {"Location@odata.count", 1}};
112 
113     if (url != nullptr)
114     {
115         asyncResp->res.jsonValue["Location"][0]["PublicationUri"] = url;
116     }
117 }
118 
119 inline void requestRoutesMessageRegistryFile(App& app)
120 {
121     BMCWEB_ROUTE(app, "/redfish/v1/Registries/<str>/")
122         .privileges(redfish::privileges::getMessageRegistryFile)
123         .methods(boost::beast::http::verb::get)(
124             handleMessageRoutesMessageRegistryFileGet);
125 }
126 
127 inline void handleMessageRegistryGet(
128     const crow::Request& /*req*/,
129     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
130     const std::string& registry, const std::string& registryMatch)
131 
132 {
133     const registries::Header* header = nullptr;
134     std::vector<const registries::MessageEntry*> registryEntries;
135     if (registry == "Base")
136     {
137         header = &registries::base::header;
138         for (const registries::MessageEntry& entry : registries::base::registry)
139         {
140             registryEntries.emplace_back(&entry);
141         }
142     }
143     else if (registry == "TaskEvent")
144     {
145         header = &registries::task_event::header;
146         for (const registries::MessageEntry& entry :
147              registries::task_event::registry)
148         {
149             registryEntries.emplace_back(&entry);
150         }
151     }
152     else if (registry == "OpenBMC")
153     {
154         header = &registries::openbmc::header;
155         for (const registries::MessageEntry& entry :
156              registries::openbmc::registry)
157         {
158             registryEntries.emplace_back(&entry);
159         }
160     }
161     else if (registry == "ResourceEvent")
162     {
163         header = &registries::resource_event::header;
164         for (const registries::MessageEntry& entry :
165              registries::resource_event::registry)
166         {
167             registryEntries.emplace_back(&entry);
168         }
169     }
170     else
171     {
172         messages::resourceNotFound(
173             asyncResp->res, "#MessageRegistryFile.v1_1_0.MessageRegistryFile",
174             registry);
175         return;
176     }
177 
178     if (registry != registryMatch)
179     {
180         messages::resourceNotFound(asyncResp->res, header->type, registryMatch);
181         return;
182     }
183 
184     asyncResp->res.jsonValue = {{"@Redfish.Copyright", header->copyright},
185                                 {"@odata.type", header->type},
186                                 {"Id", header->id},
187                                 {"Name", header->name},
188                                 {"Language", header->language},
189                                 {"Description", header->description},
190                                 {"RegistryPrefix", header->registryPrefix},
191                                 {"RegistryVersion", header->registryVersion},
192                                 {"OwningEntity", header->owningEntity}};
193 
194     nlohmann::json& messageObj = asyncResp->res.jsonValue["Messages"];
195 
196     // Go through the Message Registry and populate each Message
197     for (const registries::MessageEntry* message : registryEntries)
198     {
199         nlohmann::json& obj = messageObj[message->first];
200         obj = {{"Description", message->second.description},
201                {"Message", message->second.message},
202                {"Severity", message->second.messageSeverity},
203                {"MessageSeverity", message->second.messageSeverity},
204                {"NumberOfArgs", message->second.numberOfArgs},
205                {"Resolution", message->second.resolution}};
206         if (message->second.numberOfArgs > 0)
207         {
208             nlohmann::json& messageParamArray = obj["ParamTypes"];
209             messageParamArray = nlohmann::json::array();
210             for (const char* str : message->second.paramTypes)
211             {
212                 if (str == nullptr)
213                 {
214                     break;
215                 }
216                 messageParamArray.push_back(str);
217             }
218         }
219     }
220 }
221 
222 inline void requestRoutesMessageRegistry(App& app)
223 {
224     BMCWEB_ROUTE(app, "/redfish/v1/Registries/<str>/<str>/")
225         .privileges(redfish::privileges::getMessageRegistryFile)
226         .methods(boost::beast::http::verb::get)(handleMessageRegistryGet);
227 }
228 } // namespace redfish
229