xref: /openbmc/bmcweb/features/redfish/lib/update_service.hpp (revision 54daabe7b7213551db91c1225a66a22088d1181e)
1729dae72SJennifer Lee /*
2729dae72SJennifer Lee // Copyright (c) 2018 Intel Corporation
3729dae72SJennifer Lee //
4729dae72SJennifer Lee // Licensed under the Apache License, Version 2.0 (the "License");
5729dae72SJennifer Lee // you may not use this file except in compliance with the License.
6729dae72SJennifer Lee // You may obtain a copy of the License at
7729dae72SJennifer Lee //
8729dae72SJennifer Lee //      http://www.apache.org/licenses/LICENSE-2.0
9729dae72SJennifer Lee //
10729dae72SJennifer Lee // Unless required by applicable law or agreed to in writing, software
11729dae72SJennifer Lee // distributed under the License is distributed on an "AS IS" BASIS,
12729dae72SJennifer Lee // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13729dae72SJennifer Lee // See the License for the specific language governing permissions and
14729dae72SJennifer Lee // limitations under the License.
15729dae72SJennifer Lee */
16729dae72SJennifer Lee #pragma once
17729dae72SJennifer Lee 
18729dae72SJennifer Lee #include "node.hpp"
191abe55efSEd Tanous 
20729dae72SJennifer Lee #include <boost/container/flat_map.hpp>
21abf2add6SEd Tanous #include <variant>
22729dae72SJennifer Lee 
231abe55efSEd Tanous namespace redfish
241abe55efSEd Tanous {
2527826b5fSEd Tanous 
26acb7cfb4SJennifer Lee static std::unique_ptr<sdbusplus::bus::match::match> fwUpdateMatcher;
27729dae72SJennifer Lee 
281abe55efSEd Tanous class UpdateService : public Node
291abe55efSEd Tanous {
30729dae72SJennifer Lee   public:
311abe55efSEd Tanous     UpdateService(CrowApp &app) : Node(app, "/redfish/v1/UpdateService/")
321abe55efSEd Tanous     {
33729dae72SJennifer Lee         entityPrivileges = {
34729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
35729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
36729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
37729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
38729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
39729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
40729dae72SJennifer Lee     }
41729dae72SJennifer Lee 
42729dae72SJennifer Lee   private:
4355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
441abe55efSEd Tanous                const std::vector<std::string> &params) override
451abe55efSEd Tanous     {
460f74e643SEd Tanous         res.jsonValue["@odata.type"] = "#UpdateService.v1_2_0.UpdateService";
470f74e643SEd Tanous         res.jsonValue["@odata.id"] = "/redfish/v1/UpdateService";
480f74e643SEd Tanous         res.jsonValue["@odata.context"] =
490f74e643SEd Tanous             "/redfish/v1/$metadata#UpdateService.UpdateService";
500f74e643SEd Tanous         res.jsonValue["Id"] = "UpdateService";
510f74e643SEd Tanous         res.jsonValue["Description"] = "Service for Software Update";
520f74e643SEd Tanous         res.jsonValue["Name"] = "Update Service";
530f74e643SEd Tanous         res.jsonValue["HttpPushUri"] = "/redfish/v1/UpdateService";
540f74e643SEd Tanous         // UpdateService cannot be disabled
550f74e643SEd Tanous         res.jsonValue["ServiceEnabled"] = true;
560f74e643SEd Tanous         res.jsonValue["FirmwareInventory"] = {
570f74e643SEd Tanous             {"@odata.id", "/redfish/v1/UpdateService/FirmwareInventory"}};
58729dae72SJennifer Lee         res.end();
59729dae72SJennifer Lee     }
601abe55efSEd Tanous     static void activateImage(const std::string &objPath)
611abe55efSEd Tanous     {
62acb7cfb4SJennifer Lee         crow::connections::systemBus->async_method_call(
63c711bf86SEd Tanous             [objPath](const boost::system::error_code error_code) {
641abe55efSEd Tanous                 if (error_code)
651abe55efSEd Tanous                 {
66acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "error_code = " << error_code;
67acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "error msg = " << error_code.message();
68acb7cfb4SJennifer Lee                 }
69acb7cfb4SJennifer Lee             },
70c711bf86SEd Tanous             "xyz.openbmc_project.Software.BMC.Updater", objPath,
71acb7cfb4SJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
72acb7cfb4SJennifer Lee             "xyz.openbmc_project.Software.Activation", "RequestedActivation",
73abf2add6SEd Tanous             std::variant<std::string>(
74acb7cfb4SJennifer Lee                 "xyz.openbmc_project.Software.Activation.RequestedActivations."
75acb7cfb4SJennifer Lee                 "Active"));
76acb7cfb4SJennifer Lee     }
77acb7cfb4SJennifer Lee     void doPost(crow::Response &res, const crow::Request &req,
781abe55efSEd Tanous                 const std::vector<std::string> &params) override
791abe55efSEd Tanous     {
80acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "doPost...";
81acb7cfb4SJennifer Lee 
82acb7cfb4SJennifer Lee         // Only allow one FW update at a time
831abe55efSEd Tanous         if (fwUpdateMatcher != nullptr)
841abe55efSEd Tanous         {
85acb7cfb4SJennifer Lee             res.addHeader("Retry-After", "30");
86f12894f8SJason M. Bills             messages::serviceTemporarilyUnavailable(res, "3");
87acb7cfb4SJennifer Lee             res.end();
88acb7cfb4SJennifer Lee             return;
89acb7cfb4SJennifer Lee         }
90acb7cfb4SJennifer Lee         // Make this const static so it survives outside this method
911abe55efSEd Tanous         static boost::asio::deadline_timer timeout(
921abe55efSEd Tanous             *req.ioService, boost::posix_time::seconds(5));
93acb7cfb4SJennifer Lee 
94acb7cfb4SJennifer Lee         timeout.expires_from_now(boost::posix_time::seconds(5));
95acb7cfb4SJennifer Lee 
96acb7cfb4SJennifer Lee         timeout.async_wait([&res](const boost::system::error_code &ec) {
97acb7cfb4SJennifer Lee             fwUpdateMatcher = nullptr;
981abe55efSEd Tanous             if (ec == boost::asio::error::operation_aborted)
991abe55efSEd Tanous             {
100acb7cfb4SJennifer Lee                 // expected, we were canceled before the timer completed.
101acb7cfb4SJennifer Lee                 return;
102acb7cfb4SJennifer Lee             }
1031abe55efSEd Tanous             BMCWEB_LOG_ERROR
1041abe55efSEd Tanous                 << "Timed out waiting for firmware object being created";
1051abe55efSEd Tanous             BMCWEB_LOG_ERROR
1061abe55efSEd Tanous                 << "FW image may has already been uploaded to server";
1071abe55efSEd Tanous             if (ec)
1081abe55efSEd Tanous             {
109acb7cfb4SJennifer Lee                 BMCWEB_LOG_ERROR << "Async_wait failed" << ec;
110acb7cfb4SJennifer Lee                 return;
111acb7cfb4SJennifer Lee             }
112acb7cfb4SJennifer Lee 
113f12894f8SJason M. Bills             redfish::messages::internalError(res);
114acb7cfb4SJennifer Lee             res.end();
115acb7cfb4SJennifer Lee         });
116acb7cfb4SJennifer Lee 
117acb7cfb4SJennifer Lee         auto callback = [&res](sdbusplus::message::message &m) {
118acb7cfb4SJennifer Lee             BMCWEB_LOG_DEBUG << "Match fired";
119acb7cfb4SJennifer Lee 
1201abe55efSEd Tanous             if (m.is_method_error())
1211abe55efSEd Tanous             {
122acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "Dbus method error!!!";
123acb7cfb4SJennifer Lee                 res.end();
124acb7cfb4SJennifer Lee                 return;
125acb7cfb4SJennifer Lee             }
126acb7cfb4SJennifer Lee             std::vector<std::pair<
127acb7cfb4SJennifer Lee                 std::string,
128abf2add6SEd Tanous                 std::vector<std::pair<std::string, std::variant<std::string>>>>>
1293ae837c9SEd Tanous                 interfacesProperties;
130acb7cfb4SJennifer Lee 
131c711bf86SEd Tanous             sdbusplus::message::object_path objPath;
132acb7cfb4SJennifer Lee 
1333ae837c9SEd Tanous             m.read(objPath, interfacesProperties); // Read in the object path
134acb7cfb4SJennifer Lee                                                    // that was just created
135c711bf86SEd Tanous             // std::string str_objpath = objPath.str;  // keep a copy for
136acb7cfb4SJennifer Lee             // constructing response message
137c711bf86SEd Tanous             BMCWEB_LOG_DEBUG << "obj path = " << objPath.str; // str_objpath;
1383ae837c9SEd Tanous             for (auto &interface : interfacesProperties)
1391abe55efSEd Tanous             {
140acb7cfb4SJennifer Lee                 BMCWEB_LOG_DEBUG << "interface = " << interface.first;
141acb7cfb4SJennifer Lee 
1421abe55efSEd Tanous                 if (interface.first ==
1431abe55efSEd Tanous                     "xyz.openbmc_project.Software.Activation")
1441abe55efSEd Tanous                 {
1451abe55efSEd Tanous                     // cancel timer only when
1461abe55efSEd Tanous                     // xyz.openbmc_project.Software.Activation interface is
1471abe55efSEd Tanous                     // added
148acb7cfb4SJennifer Lee                     boost::system::error_code ec;
149acb7cfb4SJennifer Lee                     timeout.cancel(ec);
1501abe55efSEd Tanous                     if (ec)
1511abe55efSEd Tanous                     {
152acb7cfb4SJennifer Lee                         BMCWEB_LOG_ERROR << "error canceling timer " << ec;
153acb7cfb4SJennifer Lee                     }
154c711bf86SEd Tanous                     UpdateService::activateImage(objPath.str); // str_objpath);
155f12894f8SJason M. Bills                     redfish::messages::success(res);
156acb7cfb4SJennifer Lee                     BMCWEB_LOG_DEBUG << "ending response";
157acb7cfb4SJennifer Lee                     res.end();
158acb7cfb4SJennifer Lee                     fwUpdateMatcher = nullptr;
159acb7cfb4SJennifer Lee                 }
160acb7cfb4SJennifer Lee             }
161acb7cfb4SJennifer Lee         };
162acb7cfb4SJennifer Lee 
163acb7cfb4SJennifer Lee         fwUpdateMatcher = std::make_unique<sdbusplus::bus::match::match>(
164acb7cfb4SJennifer Lee             *crow::connections::systemBus,
165acb7cfb4SJennifer Lee             "interface='org.freedesktop.DBus.ObjectManager',type='signal',"
166acb7cfb4SJennifer Lee             "member='InterfacesAdded',path='/xyz/openbmc_project/software'",
167acb7cfb4SJennifer Lee             callback);
168acb7cfb4SJennifer Lee 
169acb7cfb4SJennifer Lee         std::string filepath(
170acb7cfb4SJennifer Lee             "/tmp/images/" +
171acb7cfb4SJennifer Lee             boost::uuids::to_string(boost::uuids::random_generator()()));
172acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "Writing file to " << filepath;
173acb7cfb4SJennifer Lee         std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary |
174acb7cfb4SJennifer Lee                                         std::ofstream::trunc);
175acb7cfb4SJennifer Lee         out << req.body;
176acb7cfb4SJennifer Lee         out.close();
177acb7cfb4SJennifer Lee         BMCWEB_LOG_DEBUG << "file upload complete!!";
178acb7cfb4SJennifer Lee     }
179729dae72SJennifer Lee };
180729dae72SJennifer Lee 
1811abe55efSEd Tanous class SoftwareInventoryCollection : public Node
1821abe55efSEd Tanous {
183729dae72SJennifer Lee   public:
184729dae72SJennifer Lee     template <typename CrowApp>
1851abe55efSEd Tanous     SoftwareInventoryCollection(CrowApp &app) :
1861abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/")
1871abe55efSEd Tanous     {
188729dae72SJennifer Lee         entityPrivileges = {
189729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
190729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
191729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
192729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
193729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
194729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
195729dae72SJennifer Lee     }
196729dae72SJennifer Lee 
197729dae72SJennifer Lee   private:
19855c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
1991abe55efSEd Tanous                const std::vector<std::string> &params) override
2001abe55efSEd Tanous     {
201c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2020f74e643SEd Tanous         res.jsonValue["@odata.type"] =
2030f74e643SEd Tanous             "#SoftwareInventoryCollection.SoftwareInventoryCollection";
2040f74e643SEd Tanous         res.jsonValue["@odata.id"] =
2050f74e643SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory";
2060f74e643SEd Tanous         res.jsonValue["@odata.context"] =
2070f74e643SEd Tanous             "/redfish/v1/"
2080f74e643SEd Tanous             "$metadata#SoftwareInventoryCollection.SoftwareInventoryCollection";
2090f74e643SEd Tanous         res.jsonValue["Name"] = "Software Inventory Collection";
210c711bf86SEd Tanous 
211c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
212c711bf86SEd Tanous             [asyncResp](
213c711bf86SEd Tanous                 const boost::system::error_code ec,
2146c4eb9deSJennifer Lee                 const std::vector<std::pair<
2151abe55efSEd Tanous                     std::string, std::vector<std::pair<
2161abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
2176c4eb9deSJennifer Lee                     &subtree) {
2181abe55efSEd Tanous                 if (ec)
2191abe55efSEd Tanous                 {
220f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
2216c4eb9deSJennifer Lee                     return;
222729dae72SJennifer Lee                 }
223c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members"] = nlohmann::json::array();
224c711bf86SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] = 0;
2256c4eb9deSJennifer Lee 
2261abe55efSEd Tanous                 for (auto &obj : subtree)
2271abe55efSEd Tanous                 {
2281abe55efSEd Tanous                     const std::vector<
2291abe55efSEd Tanous                         std::pair<std::string, std::vector<std::string>>>
2306c4eb9deSJennifer Lee                         &connections = obj.second;
2316c4eb9deSJennifer Lee 
232f4b65ab1SJennifer Lee                     // if can't parse fw id then return
23327826b5fSEd Tanous                     std::size_t idPos;
23427826b5fSEd Tanous                     if ((idPos = obj.first.rfind("/")) == std::string::npos)
235f4b65ab1SJennifer Lee                     {
236f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
237f4b65ab1SJennifer Lee                         BMCWEB_LOG_DEBUG << "Can't parse firmware ID!!";
238f4b65ab1SJennifer Lee                         return;
239f4b65ab1SJennifer Lee                     }
240f4b65ab1SJennifer Lee                     std::string swId = obj.first.substr(idPos + 1);
241f4b65ab1SJennifer Lee 
2421abe55efSEd Tanous                     for (auto &conn : connections)
2431abe55efSEd Tanous                     {
244c711bf86SEd Tanous                         const std::string &connectionName = conn.first;
2451abe55efSEd Tanous                         BMCWEB_LOG_DEBUG << "connectionName = "
2461abe55efSEd Tanous                                          << connectionName;
24755c7b7a2SEd Tanous                         BMCWEB_LOG_DEBUG << "obj.first = " << obj.first;
2486c4eb9deSJennifer Lee 
24955c7b7a2SEd Tanous                         crow::connections::systemBus->async_method_call(
250f4b65ab1SJennifer Lee                             [asyncResp,
251f4b65ab1SJennifer Lee                              swId](const boost::system::error_code error_code,
252c711bf86SEd Tanous                                    const VariantType &activation) {
2531abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
2541abe55efSEd Tanous                                     << "safe returned in lambda function";
2551abe55efSEd Tanous                                 if (error_code)
2561abe55efSEd Tanous                                 {
257f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
2586c4eb9deSJennifer Lee                                     return;
2596c4eb9deSJennifer Lee                                 }
260c711bf86SEd Tanous 
261f4b65ab1SJennifer Lee                                 const std::string *swActivationStatus =
262abf2add6SEd Tanous                                     std::get_if<std::string>(&activation);
263f4b65ab1SJennifer Lee                                 if (swActivationStatus == nullptr)
2641abe55efSEd Tanous                                 {
265f12894f8SJason M. Bills                                     messages::internalError(asyncResp->res);
266acb7cfb4SJennifer Lee                                     return;
267acb7cfb4SJennifer Lee                                 }
268f4b65ab1SJennifer Lee                                 if (swActivationStatus != nullptr &&
269f4b65ab1SJennifer Lee                                     *swActivationStatus !=
270f4b65ab1SJennifer Lee                                         "xyz.openbmc_project.Software."
271f4b65ab1SJennifer Lee                                         "Activation."
272f4b65ab1SJennifer Lee                                         "Activations.Active")
2731abe55efSEd Tanous                                 {
274f4b65ab1SJennifer Lee                                     // The activation status of this software is
275f4b65ab1SJennifer Lee                                     // not currently active, so does not need to
276f4b65ab1SJennifer Lee                                     // be listed in the response
277c711bf86SEd Tanous                                     return;
278c711bf86SEd Tanous                                 }
279c711bf86SEd Tanous                                 nlohmann::json &members =
280c711bf86SEd Tanous                                     asyncResp->res.jsonValue["Members"];
281c711bf86SEd Tanous                                 members.push_back(
282f4b65ab1SJennifer Lee                                     {{"@odata.id", "/redfish/v1/UpdateService/"
2831abe55efSEd Tanous                                                    "FirmwareInventory/" +
284f4b65ab1SJennifer Lee                                                        swId}});
2851abe55efSEd Tanous                                 asyncResp->res
2861abe55efSEd Tanous                                     .jsonValue["Members@odata.count"] =
287c711bf86SEd Tanous                                     members.size();
2886c4eb9deSJennifer Lee                             },
2891abe55efSEd Tanous                             connectionName, obj.first,
2901abe55efSEd Tanous                             "org.freedesktop.DBus.Properties", "Get",
2911abe55efSEd Tanous                             "xyz.openbmc_project.Software.Activation",
292acb7cfb4SJennifer Lee                             "Activation");
2936c4eb9deSJennifer Lee                     }
2946c4eb9deSJennifer Lee                 }
295c711bf86SEd Tanous             },
296c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
297c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
298c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
299c711bf86SEd Tanous             "/xyz/openbmc_project/software", int32_t(1),
3001abe55efSEd Tanous             std::array<const char *, 1>{
3011abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
302729dae72SJennifer Lee     }
303729dae72SJennifer Lee };
304c711bf86SEd Tanous 
3051abe55efSEd Tanous class SoftwareInventory : public Node
3061abe55efSEd Tanous {
307729dae72SJennifer Lee   public:
308729dae72SJennifer Lee     template <typename CrowApp>
3091abe55efSEd Tanous     SoftwareInventory(CrowApp &app) :
3101abe55efSEd Tanous         Node(app, "/redfish/v1/UpdateService/FirmwareInventory/<str>/",
3111abe55efSEd Tanous              std::string())
3121abe55efSEd Tanous     {
313729dae72SJennifer Lee         entityPrivileges = {
314729dae72SJennifer Lee             {boost::beast::http::verb::get, {{"Login"}}},
315729dae72SJennifer Lee             {boost::beast::http::verb::head, {{"Login"}}},
316729dae72SJennifer Lee             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
317729dae72SJennifer Lee             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
318729dae72SJennifer Lee             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
319729dae72SJennifer Lee             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
320729dae72SJennifer Lee     }
321729dae72SJennifer Lee 
322729dae72SJennifer Lee   private:
32355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
3241abe55efSEd Tanous                const std::vector<std::string> &params) override
3251abe55efSEd Tanous     {
326c711bf86SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
3270f74e643SEd Tanous         res.jsonValue["@odata.type"] =
3280f74e643SEd Tanous             "#SoftwareInventory.v1_1_0.SoftwareInventory";
3290f74e643SEd Tanous         res.jsonValue["@odata.context"] =
3300f74e643SEd Tanous             "/redfish/v1/$metadata#SoftwareInventory.SoftwareInventory";
3310f74e643SEd Tanous         res.jsonValue["Name"] = "Software Inventory";
3320f74e643SEd Tanous         res.jsonValue["Updateable"] = false;
3330f74e643SEd Tanous         res.jsonValue["Status"]["Health"] = "OK";
3340f74e643SEd Tanous         res.jsonValue["Status"]["HealthRollup"] = "OK";
3350f74e643SEd Tanous         res.jsonValue["Status"]["State"] = "Enabled";
3366c4eb9deSJennifer Lee 
3371abe55efSEd Tanous         if (params.size() != 1)
3381abe55efSEd Tanous         {
339f12894f8SJason M. Bills             messages::internalError(res);
340729dae72SJennifer Lee             res.end();
341729dae72SJennifer Lee             return;
342729dae72SJennifer Lee         }
343729dae72SJennifer Lee 
3443ae837c9SEd Tanous         std::shared_ptr<std::string> swId =
345c711bf86SEd Tanous             std::make_shared<std::string>(params[0]);
346c711bf86SEd Tanous 
34755c7b7a2SEd Tanous         res.jsonValue["@odata.id"] =
3483ae837c9SEd Tanous             "/redfish/v1/UpdateService/FirmwareInventory/" + *swId;
349c711bf86SEd Tanous 
350c711bf86SEd Tanous         crow::connections::systemBus->async_method_call(
3513ae837c9SEd Tanous             [asyncResp, swId](
352c711bf86SEd Tanous                 const boost::system::error_code ec,
3536c4eb9deSJennifer Lee                 const std::vector<std::pair<
3541abe55efSEd Tanous                     std::string, std::vector<std::pair<
3551abe55efSEd Tanous                                      std::string, std::vector<std::string>>>>>
3566c4eb9deSJennifer Lee                     &subtree) {
35755c7b7a2SEd Tanous                 BMCWEB_LOG_DEBUG << "doGet callback...";
3581abe55efSEd Tanous                 if (ec)
3591abe55efSEd Tanous                 {
360f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
3616c4eb9deSJennifer Lee                     return;
3626c4eb9deSJennifer Lee                 }
3636c4eb9deSJennifer Lee 
3641abe55efSEd Tanous                 for (const std::pair<
3651abe55efSEd Tanous                          std::string,
3661abe55efSEd Tanous                          std::vector<
3671abe55efSEd Tanous                              std::pair<std::string, std::vector<std::string>>>>
3681abe55efSEd Tanous                          &obj : subtree)
3691abe55efSEd Tanous                 {
3703ae837c9SEd Tanous                     if (boost::ends_with(obj.first, *swId) != true)
3711abe55efSEd Tanous                     {
372acb7cfb4SJennifer Lee                         continue;
373acb7cfb4SJennifer Lee                     }
374acb7cfb4SJennifer Lee 
375f4b65ab1SJennifer Lee                     if (obj.second.size() < 1)
3761abe55efSEd Tanous                     {
377acb7cfb4SJennifer Lee                         continue;
378acb7cfb4SJennifer Lee                     }
3796c4eb9deSJennifer Lee 
38055c7b7a2SEd Tanous                     crow::connections::systemBus->async_method_call(
3811abe55efSEd Tanous                         [asyncResp,
3823ae837c9SEd Tanous                          swId](const boost::system::error_code error_code,
3831abe55efSEd Tanous                                const boost::container::flat_map<
3841abe55efSEd Tanous                                    std::string, VariantType> &propertiesList) {
3851abe55efSEd Tanous                             if (error_code)
3861abe55efSEd Tanous                             {
387f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
3886c4eb9deSJennifer Lee                                 return;
3896c4eb9deSJennifer Lee                             }
3901abe55efSEd Tanous                             boost::container::flat_map<
3911abe55efSEd Tanous                                 std::string, VariantType>::const_iterator it =
3926c4eb9deSJennifer Lee                                 propertiesList.find("Purpose");
3931abe55efSEd Tanous                             if (it == propertiesList.end())
3941abe55efSEd Tanous                             {
3951abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
3961abe55efSEd Tanous                                     << "Can't find property \"Purpose\"!";
397f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
398f12894f8SJason M. Bills                                                           "Purpose");
3996c4eb9deSJennifer Lee                                 return;
4006c4eb9deSJennifer Lee                             }
4013ae837c9SEd Tanous                             const std::string *swInvPurpose =
402abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
4033ae837c9SEd Tanous                             if (swInvPurpose == nullptr)
4041abe55efSEd Tanous                             {
4051abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4061abe55efSEd Tanous                                     << "wrong types for property\"Purpose\"!";
407f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
408f12894f8SJason M. Bills                                                                  "", "Purpose");
409acb7cfb4SJennifer Lee                                 return;
410acb7cfb4SJennifer Lee                             }
411c711bf86SEd Tanous 
4123ae837c9SEd Tanous                             BMCWEB_LOG_DEBUG << "swInvPurpose = "
4133ae837c9SEd Tanous                                              << *swInvPurpose;
414c711bf86SEd Tanous                             it = propertiesList.find("Version");
4151abe55efSEd Tanous                             if (it == propertiesList.end())
4161abe55efSEd Tanous                             {
4171abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4181abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
419f12894f8SJason M. Bills                                 messages::propertyMissing(asyncResp->res,
420f12894f8SJason M. Bills                                                           "Version");
421c711bf86SEd Tanous                                 return;
422acb7cfb4SJennifer Lee                             }
423acb7cfb4SJennifer Lee 
424f4b65ab1SJennifer Lee                             BMCWEB_LOG_DEBUG << "Version found!";
425c711bf86SEd Tanous 
426f4b65ab1SJennifer Lee                             const std::string *version =
427abf2add6SEd Tanous                                 std::get_if<std::string>(&it->second);
428f4b65ab1SJennifer Lee 
429f4b65ab1SJennifer Lee                             if (version == nullptr)
4301abe55efSEd Tanous                             {
4311abe55efSEd Tanous                                 BMCWEB_LOG_DEBUG
4321abe55efSEd Tanous                                     << "Can't find property \"Version\"!";
433f12894f8SJason M. Bills 
434f12894f8SJason M. Bills                                 messages::propertyValueTypeError(asyncResp->res,
435f12894f8SJason M. Bills                                                                  "", "Version");
4366c4eb9deSJennifer Lee                                 return;
4376c4eb9deSJennifer Lee                             }
438c711bf86SEd Tanous                             asyncResp->res.jsonValue["Version"] = *version;
4393ae837c9SEd Tanous                             asyncResp->res.jsonValue["Id"] = *swId;
440*54daabe7SAndrew Geissler 
441*54daabe7SAndrew Geissler                             // swInvPurpose is of format:
442*54daabe7SAndrew Geissler                             // xyz.openbmc_project.Software.Version.VersionPurpose.ABC
443*54daabe7SAndrew Geissler                             // Translate this to "ABC update"
444*54daabe7SAndrew Geissler                             size_t endDesc = swInvPurpose->rfind(".");
445*54daabe7SAndrew Geissler                             if (endDesc == std::string::npos)
446*54daabe7SAndrew Geissler                             {
447*54daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
448*54daabe7SAndrew Geissler                                 return;
449*54daabe7SAndrew Geissler                             }
450*54daabe7SAndrew Geissler                             endDesc++;
451*54daabe7SAndrew Geissler                             if (endDesc >= swInvPurpose->size())
452*54daabe7SAndrew Geissler                             {
453*54daabe7SAndrew Geissler                                 messages::internalError(asyncResp->res);
454*54daabe7SAndrew Geissler                                 return;
455*54daabe7SAndrew Geissler                             }
456*54daabe7SAndrew Geissler 
457*54daabe7SAndrew Geissler                             std::string formatDesc =
458*54daabe7SAndrew Geissler                                 swInvPurpose->substr(endDesc);
459*54daabe7SAndrew Geissler                             asyncResp->res.jsonValue["Description"] =
460*54daabe7SAndrew Geissler                                 formatDesc + " update";
4616c4eb9deSJennifer Lee                         },
462c711bf86SEd Tanous                         obj.second[0].first, obj.first,
463c711bf86SEd Tanous                         "org.freedesktop.DBus.Properties", "GetAll",
464c711bf86SEd Tanous                         "xyz.openbmc_project.Software.Version");
4656c4eb9deSJennifer Lee                 }
466c711bf86SEd Tanous             },
467c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper",
468c711bf86SEd Tanous             "/xyz/openbmc_project/object_mapper",
469c711bf86SEd Tanous             "xyz.openbmc_project.ObjectMapper", "GetSubTree",
470c711bf86SEd Tanous             "/xyz/openbmc_project/software", int32_t(1),
4711abe55efSEd Tanous             std::array<const char *, 1>{
4721abe55efSEd Tanous                 "xyz.openbmc_project.Software.Version"});
4736c4eb9deSJennifer Lee     }
474729dae72SJennifer Lee };
475729dae72SJennifer Lee 
476729dae72SJennifer Lee } // namespace redfish
477