xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision 4bbf2a13c21de665aab76ae3d3a3e2633b667be2)
140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0
240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors
340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation
4107077deSPrzemyslaw Czarnowski #pragma once
5107077deSPrzemyslaw Czarnowski 
63ccb3adbSEd Tanous #include "app.hpp"
779fdf63eSPrzemyslaw Czarnowski #include "async_resp.hpp"
811e8f60dSEd Tanous #include "credential_pipe.hpp"
9d7857201SEd Tanous #include "dbus_singleton.hpp"
102b73119cSGeorge Liu #include "dbus_utility.hpp"
11d7857201SEd Tanous #include "error_messages.hpp"
12739b87efSEd Tanous #include "generated/enums/virtual_media.hpp"
13d7857201SEd Tanous #include "http_request.hpp"
14d7857201SEd Tanous #include "logging.hpp"
153ccb3adbSEd Tanous #include "query.hpp"
163ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
173ccb3adbSEd Tanous #include "utils/json_utils.hpp"
183ccb3adbSEd Tanous 
19d7857201SEd Tanous #include <boost/beast/http/status.hpp>
20d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
21d7857201SEd Tanous #include <boost/system/result.hpp>
22ef4c65b7SEd Tanous #include <boost/url/format.hpp>
23d7857201SEd Tanous #include <boost/url/parse.hpp>
249e319cf0SAnna Platash #include <boost/url/url_view.hpp>
254a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp>
26d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
27107077deSPrzemyslaw Czarnowski 
28d7857201SEd Tanous #include <cstddef>
29*4bbf2a13SEd Tanous #include <cstdint>
30d7857201SEd Tanous #include <filesystem>
31d7857201SEd Tanous #include <functional>
32d7857201SEd Tanous #include <memory>
33d7857201SEd Tanous #include <optional>
343544d2a7SEd Tanous #include <ranges>
35d7857201SEd Tanous #include <string>
362b73119cSGeorge Liu #include <string_view>
37d7857201SEd Tanous #include <utility>
38d7857201SEd Tanous #include <variant>
392b73119cSGeorge Liu 
40107077deSPrzemyslaw Czarnowski namespace redfish
41107077deSPrzemyslaw Czarnowski {
42365a73f4SEd Tanous 
43365a73f4SEd Tanous enum class VmMode
44365a73f4SEd Tanous {
45365a73f4SEd Tanous     Invalid,
46365a73f4SEd Tanous     Legacy,
47365a73f4SEd Tanous     Proxy
48365a73f4SEd Tanous };
49365a73f4SEd Tanous 
50bd79bce8SPatrick Williams inline VmMode parseObjectPathAndGetMode(
51bd79bce8SPatrick Williams     const sdbusplus::message::object_path& itemPath, const std::string& resName)
52365a73f4SEd Tanous {
53365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
5462598e31SEd Tanous     BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath);
55365a73f4SEd Tanous 
56365a73f4SEd Tanous     if (thisPath.empty())
57365a73f4SEd Tanous     {
58365a73f4SEd Tanous         return VmMode::Invalid;
59365a73f4SEd Tanous     }
60365a73f4SEd Tanous 
61365a73f4SEd Tanous     if (thisPath != resName)
62365a73f4SEd Tanous     {
63365a73f4SEd Tanous         return VmMode::Invalid;
64365a73f4SEd Tanous     }
65365a73f4SEd Tanous 
66365a73f4SEd Tanous     auto mode = itemPath.parent_path();
67365a73f4SEd Tanous     auto type = mode.parent_path();
68365a73f4SEd Tanous 
69365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
70365a73f4SEd Tanous     {
71365a73f4SEd Tanous         return VmMode::Invalid;
72365a73f4SEd Tanous     }
73365a73f4SEd Tanous 
74365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
75365a73f4SEd Tanous     {
76365a73f4SEd Tanous         return VmMode::Invalid;
77365a73f4SEd Tanous     }
78365a73f4SEd Tanous     std::string modeStr = mode.filename();
79365a73f4SEd Tanous     if (modeStr == "Legacy")
80365a73f4SEd Tanous     {
81365a73f4SEd Tanous         return VmMode::Legacy;
82365a73f4SEd Tanous     }
83365a73f4SEd Tanous     if (modeStr == "Proxy")
84365a73f4SEd Tanous     {
85365a73f4SEd Tanous         return VmMode::Proxy;
86365a73f4SEd Tanous     }
87365a73f4SEd Tanous     return VmMode::Invalid;
88365a73f4SEd Tanous }
89365a73f4SEd Tanous 
9079fdf63eSPrzemyslaw Czarnowski using CheckItemHandler =
9179fdf63eSPrzemyslaw Czarnowski     std::function<void(const std::string& service, const std::string& resName,
9279fdf63eSPrzemyslaw Czarnowski                        const std::shared_ptr<bmcweb::AsyncResp>&,
9370cbdf53SGeorge Liu                        const std::pair<sdbusplus::message::object_path,
9480f79a40SMichael Shen                                        dbus::utility::DBusInterfacesMap>&)>;
9579fdf63eSPrzemyslaw Czarnowski 
96504af5a0SPatrick Williams inline void findAndParseObject(
97504af5a0SPatrick Williams     const std::string& service, const std::string& resName,
98ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9979fdf63eSPrzemyslaw Czarnowski     CheckItemHandler&& handler)
10079fdf63eSPrzemyslaw Czarnowski {
1015eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia");
1025eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1035eb468daSGeorge Liu         service, path,
1048cb2c024SEd Tanous         [service, resName, asyncResp, handler = std::move(handler)](
1058cb2c024SEd Tanous             const boost::system::error_code& ec,
10670cbdf53SGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
10779fdf63eSPrzemyslaw Czarnowski             if (ec)
10879fdf63eSPrzemyslaw Czarnowski             {
10962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error");
11079fdf63eSPrzemyslaw Czarnowski 
11179fdf63eSPrzemyslaw Czarnowski                 return;
11279fdf63eSPrzemyslaw Czarnowski             }
11379fdf63eSPrzemyslaw Czarnowski 
11470cbdf53SGeorge Liu             for (const auto& item : subtree)
11579fdf63eSPrzemyslaw Czarnowski             {
11679fdf63eSPrzemyslaw Czarnowski                 VmMode mode = parseObjectPathAndGetMode(item.first, resName);
11779fdf63eSPrzemyslaw Czarnowski                 if (mode != VmMode::Invalid)
11879fdf63eSPrzemyslaw Czarnowski                 {
119ac106bf6SEd Tanous                     handler(service, resName, asyncResp, item);
12079fdf63eSPrzemyslaw Czarnowski                     return;
12179fdf63eSPrzemyslaw Czarnowski                 }
12279fdf63eSPrzemyslaw Czarnowski             }
12379fdf63eSPrzemyslaw Czarnowski 
12462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
125ac106bf6SEd Tanous             asyncResp->res.result(boost::beast::http::status::not_found);
1265eb468daSGeorge Liu         });
12779fdf63eSPrzemyslaw Czarnowski }
12879fdf63eSPrzemyslaw Czarnowski 
1299e319cf0SAnna Platash /**
1309e319cf0SAnna Platash  * @brief Function extracts transfer protocol name from URI.
1319e319cf0SAnna Platash  */
13267df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
13367df073bSEd Tanous {
1346fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
135079360aeSEd Tanous         boost::urls::parse_uri(imageUri);
13667df073bSEd Tanous     if (!url)
13767df073bSEd Tanous     {
13867df073bSEd Tanous         return "None";
13967df073bSEd Tanous     }
140079360aeSEd Tanous     std::string_view scheme = url->scheme();
14167df073bSEd Tanous     if (scheme == "smb")
14267df073bSEd Tanous     {
14367df073bSEd Tanous         return "CIFS";
14467df073bSEd Tanous     }
14567df073bSEd Tanous     if (scheme == "https")
14667df073bSEd Tanous     {
14767df073bSEd Tanous         return "HTTPS";
14867df073bSEd Tanous     }
14967df073bSEd Tanous 
15067df073bSEd Tanous     return "None";
15167df073bSEd Tanous }
152107077deSPrzemyslaw Czarnowski 
153107077deSPrzemyslaw Czarnowski /**
154107077deSPrzemyslaw Czarnowski  * @brief Read all known properties from VM object interfaces
155107077deSPrzemyslaw Czarnowski  */
156504af5a0SPatrick Williams inline void vmParseInterfaceObject(
157504af5a0SPatrick Williams     const dbus::utility::DBusInterfacesMap& interfaces,
158ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
159107077deSPrzemyslaw Czarnowski {
1608a592810SEd Tanous     for (const auto& [interface, values] : interfaces)
161107077deSPrzemyslaw Czarnowski     {
162711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
163107077deSPrzemyslaw Czarnowski         {
164711ac7a9SEd Tanous             for (const auto& [property, value] : values)
165107077deSPrzemyslaw Czarnowski             {
166711ac7a9SEd Tanous                 if (property == "EndpointId")
167107077deSPrzemyslaw Czarnowski                 {
168107077deSPrzemyslaw Czarnowski                     const std::string* endpointIdValue =
169711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
170711ac7a9SEd Tanous                     if (endpointIdValue == nullptr)
171107077deSPrzemyslaw Czarnowski                     {
172711ac7a9SEd Tanous                         continue;
173711ac7a9SEd Tanous                     }
174107077deSPrzemyslaw Czarnowski                     if (!endpointIdValue->empty())
175107077deSPrzemyslaw Czarnowski                     {
176107077deSPrzemyslaw Czarnowski                         // Proxy mode
177ac106bf6SEd Tanous                         asyncResp->res
178711ac7a9SEd Tanous                             .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
179d04ba325SPrzemyslaw Czarnowski                             *endpointIdValue;
180ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
181ac106bf6SEd Tanous                             "OEM";
182107077deSPrzemyslaw Czarnowski                     }
183107077deSPrzemyslaw Czarnowski                 }
184711ac7a9SEd Tanous                 if (property == "ImageURL")
185107077deSPrzemyslaw Czarnowski                 {
186107077deSPrzemyslaw Czarnowski                     const std::string* imageUrlValue =
187711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
18826f6976fSEd Tanous                     if (imageUrlValue != nullptr && !imageUrlValue->empty())
189107077deSPrzemyslaw Czarnowski                     {
190da4784d8SPrzemyslaw Czarnowski                         std::filesystem::path filePath = *imageUrlValue;
191da4784d8SPrzemyslaw Czarnowski                         if (!filePath.has_filename())
192da4784d8SPrzemyslaw Czarnowski                         {
1939e319cf0SAnna Platash                             // this will handle https share, which not
1949e319cf0SAnna Platash                             // necessarily has to have filename given.
195ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] = "";
196da4784d8SPrzemyslaw Czarnowski                         }
197da4784d8SPrzemyslaw Czarnowski                         else
198da4784d8SPrzemyslaw Czarnowski                         {
199ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] =
2009e319cf0SAnna Platash                                 filePath.filename();
201da4784d8SPrzemyslaw Czarnowski                         }
202da4784d8SPrzemyslaw Czarnowski 
203ac106bf6SEd Tanous                         asyncResp->res.jsonValue["Image"] = *imageUrlValue;
204ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
2059e319cf0SAnna Platash                             getTransferProtocolTypeFromUri(*imageUrlValue);
2069e319cf0SAnna Platash 
207ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
208739b87efSEd Tanous                             virtual_media::ConnectedVia::URI;
209107077deSPrzemyslaw Czarnowski                     }
210107077deSPrzemyslaw Czarnowski                 }
211711ac7a9SEd Tanous                 if (property == "WriteProtected")
2129e319cf0SAnna Platash                 {
213711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
214e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
2159e319cf0SAnna Platash                     {
216ac106bf6SEd Tanous                         asyncResp->res.jsonValue["WriteProtected"] =
2179e319cf0SAnna Platash                             *writeProtectedValue;
2189e319cf0SAnna Platash                     }
2199e319cf0SAnna Platash                 }
2209e319cf0SAnna Platash             }
221107077deSPrzemyslaw Czarnowski         }
222711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
223711ac7a9SEd Tanous         {
224711ac7a9SEd Tanous             for (const auto& [property, value] : values)
225711ac7a9SEd Tanous             {
226711ac7a9SEd Tanous                 if (property == "Active")
227711ac7a9SEd Tanous                 {
228711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
229e662eae8SEd Tanous                     if (activeValue == nullptr)
230711ac7a9SEd Tanous                     {
23162598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Value Active not found");
232711ac7a9SEd Tanous                         return;
233711ac7a9SEd Tanous                     }
234ac106bf6SEd Tanous                     asyncResp->res.jsonValue["Inserted"] = *activeValue;
235711ac7a9SEd Tanous 
236e05aec50SEd Tanous                     if (*activeValue)
237711ac7a9SEd Tanous                     {
238ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
239739b87efSEd Tanous                             virtual_media::ConnectedVia::Applet;
240711ac7a9SEd Tanous                     }
241711ac7a9SEd Tanous                 }
242711ac7a9SEd Tanous             }
243711ac7a9SEd Tanous         }
244107077deSPrzemyslaw Czarnowski     }
245107077deSPrzemyslaw Czarnowski }
246107077deSPrzemyslaw Czarnowski 
247107077deSPrzemyslaw Czarnowski /**
248107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
249107077deSPrzemyslaw Czarnowski  */
25022db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
251107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
252107077deSPrzemyslaw Czarnowski {
253107077deSPrzemyslaw Czarnowski     nlohmann::json item;
254ef4c65b7SEd Tanous     item["@odata.id"] = boost::urls::format(
255ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName);
25622db1728SEd Tanous 
257d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
258107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
259107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
260107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
261739b87efSEd Tanous     item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
262613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
263539d8c6bSEd Tanous     item["TransferMethod"] = virtual_media::TransferMethod::Stream;
264d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
265f958ed9cSEd Tanous         "#OpenBMCVirtualMedia.v1_0_0.VirtualMedia";
26615b89725SV-Sanjana     item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format(
26715b89725SV-Sanjana         "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName);
268107077deSPrzemyslaw Czarnowski 
269107077deSPrzemyslaw Czarnowski     return item;
270107077deSPrzemyslaw Czarnowski }
271107077deSPrzemyslaw Czarnowski 
272107077deSPrzemyslaw Czarnowski /**
273107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
274107077deSPrzemyslaw Czarnowski  */
275ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
276107077deSPrzemyslaw Czarnowski                               const std::string& service,
277107077deSPrzemyslaw Czarnowski                               const std::string& name)
278107077deSPrzemyslaw Czarnowski {
27962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get available Virtual Media resources.");
2805eb468daSGeorge Liu     sdbusplus::message::object_path objPath(
2815eb468daSGeorge Liu         "/xyz/openbmc_project/VirtualMedia");
2825eb468daSGeorge Liu     dbus::utility::getManagedObjects(
2835eb468daSGeorge Liu         service, objPath,
284ac106bf6SEd Tanous         [name, asyncResp{std::move(asyncResp)}](
2855e7e2dc5SEd Tanous             const boost::system::error_code& ec,
28602cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
287107077deSPrzemyslaw Czarnowski             if (ec)
288107077deSPrzemyslaw Czarnowski             {
28962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error");
290107077deSPrzemyslaw Czarnowski                 return;
291107077deSPrzemyslaw Czarnowski             }
292ac106bf6SEd Tanous             nlohmann::json& members = asyncResp->res.jsonValue["Members"];
293107077deSPrzemyslaw Czarnowski             members = nlohmann::json::array();
294107077deSPrzemyslaw Czarnowski 
295107077deSPrzemyslaw Czarnowski             for (const auto& object : subtree)
296107077deSPrzemyslaw Czarnowski             {
297107077deSPrzemyslaw Czarnowski                 nlohmann::json item;
2982dfd18efSEd Tanous                 std::string path = object.first.filename();
2992dfd18efSEd Tanous                 if (path.empty())
300107077deSPrzemyslaw Czarnowski                 {
301107077deSPrzemyslaw Czarnowski                     continue;
302107077deSPrzemyslaw Czarnowski                 }
303107077deSPrzemyslaw Czarnowski 
304ef4c65b7SEd Tanous                 item["@odata.id"] = boost::urls::format(
305ef4c65b7SEd Tanous                     "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path);
306107077deSPrzemyslaw Czarnowski                 members.emplace_back(std::move(item));
307107077deSPrzemyslaw Czarnowski             }
308ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = members.size();
3095eb468daSGeorge Liu         });
310107077deSPrzemyslaw Czarnowski }
311107077deSPrzemyslaw Czarnowski 
312504af5a0SPatrick Williams inline void afterGetVmData(
313504af5a0SPatrick Williams     const std::string& name, const std::string& /*service*/,
31479fdf63eSPrzemyslaw Czarnowski     const std::string& resName,
31579fdf63eSPrzemyslaw Czarnowski     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31670cbdf53SGeorge Liu     const std::pair<sdbusplus::message::object_path,
31780f79a40SMichael Shen                     dbus::utility::DBusInterfacesMap>& item)
31879fdf63eSPrzemyslaw Czarnowski {
31979fdf63eSPrzemyslaw Czarnowski     VmMode mode = parseObjectPathAndGetMode(item.first, resName);
32079fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Invalid)
32179fdf63eSPrzemyslaw Czarnowski     {
32279fdf63eSPrzemyslaw Czarnowski         return;
32379fdf63eSPrzemyslaw Czarnowski     }
32479fdf63eSPrzemyslaw Czarnowski 
32579fdf63eSPrzemyslaw Czarnowski     asyncResp->res.jsonValue = vmItemTemplate(name, resName);
32679fdf63eSPrzemyslaw Czarnowski 
32779fdf63eSPrzemyslaw Czarnowski     // Check if dbus path is Legacy type
32879fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Legacy)
32979fdf63eSPrzemyslaw Czarnowski     {
330ef4c65b7SEd Tanous         asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
331ef4c65b7SEd Tanous                                 ["target"] = boost::urls::format(
332ef4c65b7SEd Tanous             "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia",
333ef4c65b7SEd Tanous             name, resName);
33479fdf63eSPrzemyslaw Czarnowski     }
33579fdf63eSPrzemyslaw Czarnowski 
33679fdf63eSPrzemyslaw Czarnowski     vmParseInterfaceObject(item.second, asyncResp);
33779fdf63eSPrzemyslaw Czarnowski 
338ef4c65b7SEd Tanous     asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"]
339ef4c65b7SEd Tanous                             ["target"] = boost::urls::format(
340ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia",
341ef4c65b7SEd Tanous         name, resName);
34279fdf63eSPrzemyslaw Czarnowski }
34379fdf63eSPrzemyslaw Czarnowski 
344107077deSPrzemyslaw Czarnowski /**
345107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
346107077deSPrzemyslaw Czarnowski  */
347ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
348107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
349107077deSPrzemyslaw Czarnowski                       const std::string& resName)
350107077deSPrzemyslaw Czarnowski {
35162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Virtual Media resource data.");
352107077deSPrzemyslaw Czarnowski 
353ac106bf6SEd Tanous     findAndParseObject(service, resName, asyncResp,
35470cbdf53SGeorge Liu                        std::bind_front(afterGetVmData, name));
355107077deSPrzemyslaw Czarnowski }
356107077deSPrzemyslaw Czarnowski 
357e13c2760SPrzemyslaw Czarnowski /**
358c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
359c6f4e017SAgata Olender  *
360c6f4e017SAgata Olender  */
361c6f4e017SAgata Olender enum class TransferProtocol
362c6f4e017SAgata Olender {
363c6f4e017SAgata Olender     https,
364c6f4e017SAgata Olender     smb,
365c6f4e017SAgata Olender     invalid
366c6f4e017SAgata Olender };
367c6f4e017SAgata Olender 
368c6f4e017SAgata Olender /**
369c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
370c6f4e017SAgata Olender  *
371c6f4e017SAgata Olender  */
372504af5a0SPatrick Williams inline std::optional<TransferProtocol> getTransferProtocolFromUri(
373504af5a0SPatrick Williams     const boost::urls::url_view_base& imageUri)
37467df073bSEd Tanous {
375079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
37667df073bSEd Tanous     if (scheme == "smb")
37767df073bSEd Tanous     {
37867df073bSEd Tanous         return TransferProtocol::smb;
37967df073bSEd Tanous     }
38067df073bSEd Tanous     if (scheme == "https")
38167df073bSEd Tanous     {
38267df073bSEd Tanous         return TransferProtocol::https;
38367df073bSEd Tanous     }
38467df073bSEd Tanous     if (!scheme.empty())
38567df073bSEd Tanous     {
38667df073bSEd Tanous         return TransferProtocol::invalid;
38767df073bSEd Tanous     }
38867df073bSEd Tanous 
38967df073bSEd Tanous     return {};
39067df073bSEd Tanous }
391c6f4e017SAgata Olender 
392c6f4e017SAgata Olender /**
393c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
394c6f4e017SAgata Olender  *
395c6f4e017SAgata Olender  */
39622db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
397c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
398c6f4e017SAgata Olender {
399e01d0c36SEd Tanous     if (!transferProtocolType)
400c6f4e017SAgata Olender     {
401c6f4e017SAgata Olender         return {};
402c6f4e017SAgata Olender     }
403c6f4e017SAgata Olender 
404c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
405c6f4e017SAgata Olender     {
406c6f4e017SAgata Olender         return TransferProtocol::smb;
407c6f4e017SAgata Olender     }
408c6f4e017SAgata Olender 
409c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
410c6f4e017SAgata Olender     {
411c6f4e017SAgata Olender         return TransferProtocol::https;
412c6f4e017SAgata Olender     }
413c6f4e017SAgata Olender 
414c6f4e017SAgata Olender     return TransferProtocol::invalid;
415c6f4e017SAgata Olender }
416c6f4e017SAgata Olender 
417c6f4e017SAgata Olender /**
418c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
419c6f4e017SAgata Olender  *
420c6f4e017SAgata Olender  */
421bd79bce8SPatrick Williams inline std::string getUriWithTransferProtocol(
422bd79bce8SPatrick Williams     const std::string& imageUri, const TransferProtocol& transferProtocol)
423c6f4e017SAgata Olender {
424c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
425c6f4e017SAgata Olender     {
426c6f4e017SAgata Olender         return "smb://" + imageUri;
427c6f4e017SAgata Olender     }
428c6f4e017SAgata Olender 
429c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
430c6f4e017SAgata Olender     {
431c6f4e017SAgata Olender         return "https://" + imageUri;
432c6f4e017SAgata Olender     }
433c6f4e017SAgata Olender 
434c6f4e017SAgata Olender     return imageUri;
435c6f4e017SAgata Olender }
436c6f4e017SAgata Olender 
4371f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
4381f2a40ceSPrzemyslaw Czarnowski {
439120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
4401f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4411f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4421f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4431f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4441f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4451f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4461f2a40ceSPrzemyslaw Czarnowski };
4471f2a40ceSPrzemyslaw Czarnowski 
448e13c2760SPrzemyslaw Czarnowski /**
449e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
450e13c2760SPrzemyslaw Czarnowski  *
451e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
452e13c2760SPrzemyslaw Czarnowski  */
45322db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
454e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
45511e8f60dSEd Tanous                             const std::string& imageUrl, bool rw,
456988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
457e13c2760SPrzemyslaw Czarnowski {
458*4bbf2a13SEd Tanous     if (userName.contains('\0'))
459988fb7b2SAdrian Ambrożewicz     {
460*4bbf2a13SEd Tanous         messages::actionParameterValueFormatError(
461*4bbf2a13SEd Tanous             asyncResp->res, userName, "Username", "VirtualMedia.InsertMedia");
462988fb7b2SAdrian Ambrożewicz         return;
463988fb7b2SAdrian Ambrożewicz     }
464*4bbf2a13SEd Tanous     constexpr uint64_t limit = 512;
465*4bbf2a13SEd Tanous     if (userName.size() > limit)
466*4bbf2a13SEd Tanous     {
467*4bbf2a13SEd Tanous         messages::stringValueTooLong(asyncResp->res, userName, limit);
468*4bbf2a13SEd Tanous         return;
469*4bbf2a13SEd Tanous     }
470*4bbf2a13SEd Tanous     if (password.contains('\0'))
471*4bbf2a13SEd Tanous     {
472*4bbf2a13SEd Tanous         messages::actionParameterValueFormatError(
473*4bbf2a13SEd Tanous             asyncResp->res, password, "Password", "VirtualMedia.InsertMedia");
474*4bbf2a13SEd Tanous         return;
475*4bbf2a13SEd Tanous     }
476*4bbf2a13SEd Tanous     if (password.size() > limit)
477*4bbf2a13SEd Tanous     {
478*4bbf2a13SEd Tanous         messages::stringValueTooLong(asyncResp->res, password, limit);
479*4bbf2a13SEd Tanous         return;
480*4bbf2a13SEd Tanous     }
481988fb7b2SAdrian Ambrożewicz     // Open pipe
482*4bbf2a13SEd Tanous     std::shared_ptr<CredentialsPipe> secretPipe =
483*4bbf2a13SEd Tanous         std::make_shared<CredentialsPipe>(
48411e8f60dSEd Tanous             crow::connections::systemBus->get_io_context());
485*4bbf2a13SEd Tanous     int fd = secretPipe->releaseFd();
486988fb7b2SAdrian Ambrożewicz 
487988fb7b2SAdrian Ambrożewicz     // Pass secret over pipe
48881ce609eSEd Tanous     secretPipe->asyncWrite(
48911e8f60dSEd Tanous         std::move(userName), std::move(password),
490bd79bce8SPatrick Williams         [asyncResp,
491bd79bce8SPatrick Williams          secretPipe](const boost::system::error_code& ec, std::size_t) {
492988fb7b2SAdrian Ambrożewicz             if (ec)
493988fb7b2SAdrian Ambrożewicz             {
49462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec);
495988fb7b2SAdrian Ambrożewicz                 messages::internalError(asyncResp->res);
496988fb7b2SAdrian Ambrożewicz             }
497988fb7b2SAdrian Ambrożewicz         });
498988fb7b2SAdrian Ambrożewicz 
499*4bbf2a13SEd Tanous     sdbusplus::message::unix_fd unixFd(fd);
50011e8f60dSEd Tanous 
50111e8f60dSEd Tanous     sdbusplus::message::object_path path(
50211e8f60dSEd Tanous         "/xyz/openbmc_project/VirtualMedia/Legacy");
50311e8f60dSEd Tanous     path /= name;
504e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
505*4bbf2a13SEd Tanous         [asyncResp](const boost::system::error_code& ec, bool success) {
506e13c2760SPrzemyslaw Czarnowski             if (ec)
507e13c2760SPrzemyslaw Czarnowski             {
50862598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
509e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
51011e8f60dSEd Tanous                 return;
511d6da5bebSAdrian Ambrożewicz             }
51211e8f60dSEd Tanous             if (!success)
513d6da5bebSAdrian Ambrożewicz             {
51462598e31SEd Tanous                 BMCWEB_LOG_ERROR("Service responded with error");
51511e8f60dSEd Tanous                 messages::internalError(asyncResp->res);
516e13c2760SPrzemyslaw Czarnowski             }
517e13c2760SPrzemyslaw Czarnowski         },
51811e8f60dSEd Tanous         service, path.str, "xyz.openbmc_project.VirtualMedia.Legacy", "Mount",
51911e8f60dSEd Tanous         imageUrl, rw, unixFd);
520e13c2760SPrzemyslaw Czarnowski }
521e13c2760SPrzemyslaw Czarnowski 
522e13c2760SPrzemyslaw Czarnowski /**
523120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
524120fa86aSPrzemyslaw Czarnowski  *
525120fa86aSPrzemyslaw Czarnowski  */
526120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
527120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
528120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
529120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
530120fa86aSPrzemyslaw Czarnowski {
53162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Validation started");
532120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
533120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
534120fa86aSPrzemyslaw Czarnowski     {
53562598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter Image is empty.");
536120fa86aSPrzemyslaw Czarnowski 
537120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
538120fa86aSPrzemyslaw Czarnowski 
539120fa86aSPrzemyslaw Czarnowski         return;
540120fa86aSPrzemyslaw Czarnowski     }
541120fa86aSPrzemyslaw Czarnowski 
542120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
543e01d0c36SEd Tanous     if (actionParams.inserted && !*actionParams.inserted)
544120fa86aSPrzemyslaw Czarnowski     {
54562598e31SEd Tanous         BMCWEB_LOG_ERROR(
54662598e31SEd Tanous             "Request action optional parameter Inserted must be true.");
547120fa86aSPrzemyslaw Czarnowski 
548120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
549120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
550120fa86aSPrzemyslaw Czarnowski 
551120fa86aSPrzemyslaw Czarnowski         return;
552120fa86aSPrzemyslaw Czarnowski     }
553120fa86aSPrzemyslaw Czarnowski 
554120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
555e01d0c36SEd Tanous     if (actionParams.transferMethod &&
556120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
557120fa86aSPrzemyslaw Czarnowski     {
55862598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action optional parameter "
55962598e31SEd Tanous                          "TransferMethod must be Stream.");
560120fa86aSPrzemyslaw Czarnowski 
561120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
562120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
563120fa86aSPrzemyslaw Czarnowski 
564120fa86aSPrzemyslaw Czarnowski         return;
565120fa86aSPrzemyslaw Czarnowski     }
5666fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
567120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
568120fa86aSPrzemyslaw Czarnowski     if (!url)
569120fa86aSPrzemyslaw Czarnowski     {
570120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
571120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
572120fa86aSPrzemyslaw Czarnowski         return;
573120fa86aSPrzemyslaw Czarnowski     }
574120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
575120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
576120fa86aSPrzemyslaw Czarnowski 
577120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
578120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
579120fa86aSPrzemyslaw Czarnowski 
580120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
581e01d0c36SEd Tanous     if (uriTransferProtocolType &&
582e01d0c36SEd Tanous         *uriTransferProtocolType == TransferProtocol::invalid)
583120fa86aSPrzemyslaw Czarnowski     {
58462598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
585120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type from list: "
58662598e31SEd Tanous                          "(smb, https).");
587120fa86aSPrzemyslaw Czarnowski 
588120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
589120fa86aSPrzemyslaw Czarnowski 
590120fa86aSPrzemyslaw Czarnowski         return;
591120fa86aSPrzemyslaw Czarnowski     }
592120fa86aSPrzemyslaw Czarnowski 
593120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
594e01d0c36SEd Tanous     if (paramTransferProtocolType &&
595e01d0c36SEd Tanous         *paramTransferProtocolType == TransferProtocol::invalid)
596120fa86aSPrzemyslaw Czarnowski     {
59762598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType "
598120fa86aSPrzemyslaw Czarnowski                          "must be provided with value from list: "
59962598e31SEd Tanous                          "(CIFS, HTTPS).");
600120fa86aSPrzemyslaw Czarnowski 
601e01d0c36SEd Tanous         messages::propertyValueNotInList(
602e01d0c36SEd Tanous             asyncResp->res, actionParams.transferProtocolType.value_or(""),
603120fa86aSPrzemyslaw Czarnowski             "TransferProtocolType");
604120fa86aSPrzemyslaw Czarnowski         return;
605120fa86aSPrzemyslaw Czarnowski     }
606120fa86aSPrzemyslaw Czarnowski 
607120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
608e01d0c36SEd Tanous     if (!uriTransferProtocolType && !paramTransferProtocolType)
609120fa86aSPrzemyslaw Czarnowski     {
61062598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
611120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type or param "
61262598e31SEd Tanous                          "TransferProtocolType must be provided.");
613120fa86aSPrzemyslaw Czarnowski 
614120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
615120fa86aSPrzemyslaw Czarnowski 
616120fa86aSPrzemyslaw Czarnowski         return;
617120fa86aSPrzemyslaw Czarnowski     }
618120fa86aSPrzemyslaw Czarnowski 
619120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
620e01d0c36SEd Tanous     if (paramTransferProtocolType && uriTransferProtocolType)
621120fa86aSPrzemyslaw Czarnowski     {
622120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
623120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
624120fa86aSPrzemyslaw Czarnowski         {
62562598e31SEd Tanous             BMCWEB_LOG_ERROR("Request action parameter "
626120fa86aSPrzemyslaw Czarnowski                              "TransferProtocolType must  contain the "
627120fa86aSPrzemyslaw Czarnowski                              "same protocol type as protocol type "
62862598e31SEd Tanous                              "provided with param imageUrl.");
629120fa86aSPrzemyslaw Czarnowski 
630120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
631e01d0c36SEd Tanous                 asyncResp->res, actionParams.transferProtocolType.value_or(""),
632120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
633120fa86aSPrzemyslaw Czarnowski 
634120fa86aSPrzemyslaw Czarnowski             return;
635120fa86aSPrzemyslaw Czarnowski         }
636120fa86aSPrzemyslaw Czarnowski     }
637120fa86aSPrzemyslaw Czarnowski 
638120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
6397ead48e6SBoleslaw Ogonczyk Makowski     if (!uriTransferProtocolType && paramTransferProtocolType)
640120fa86aSPrzemyslaw Czarnowski     {
641120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
642120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
643120fa86aSPrzemyslaw Czarnowski     }
644120fa86aSPrzemyslaw Czarnowski 
645452bd8d8SJayaprakash Mutyala     if (!actionParams.userName)
646452bd8d8SJayaprakash Mutyala     {
647452bd8d8SJayaprakash Mutyala         actionParams.userName = "";
648452bd8d8SJayaprakash Mutyala     }
649452bd8d8SJayaprakash Mutyala 
650452bd8d8SJayaprakash Mutyala     if (!actionParams.password)
651452bd8d8SJayaprakash Mutyala     {
652452bd8d8SJayaprakash Mutyala         actionParams.password = "";
653452bd8d8SJayaprakash Mutyala     }
654452bd8d8SJayaprakash Mutyala 
655120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
656e01d0c36SEd Tanous                     !(actionParams.writeProtected.value_or(false)),
657120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
658120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
659120fa86aSPrzemyslaw Czarnowski }
660120fa86aSPrzemyslaw Czarnowski 
661120fa86aSPrzemyslaw Czarnowski /**
662e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
663e13c2760SPrzemyslaw Czarnowski  *
664e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
665e13c2760SPrzemyslaw Czarnowski  */
66624e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
667e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
668e13c2760SPrzemyslaw Czarnowski                           bool legacy)
669e13c2760SPrzemyslaw Czarnowski {
670e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
671e13c2760SPrzemyslaw Czarnowski     if (legacy)
672e13c2760SPrzemyslaw Czarnowski     {
673e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6745e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
675e13c2760SPrzemyslaw Czarnowski                 if (ec)
676e13c2760SPrzemyslaw Czarnowski                 {
67762598e31SEd Tanous                     BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
678e13c2760SPrzemyslaw Czarnowski 
679e13c2760SPrzemyslaw Czarnowski                     messages::internalError(asyncResp->res);
680e13c2760SPrzemyslaw Czarnowski                     return;
681e13c2760SPrzemyslaw Czarnowski                 }
682e13c2760SPrzemyslaw Czarnowski             },
683e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
684e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
685e13c2760SPrzemyslaw Czarnowski     }
686e13c2760SPrzemyslaw Czarnowski     else // proxy
687e13c2760SPrzemyslaw Czarnowski     {
688e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6895e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
690e13c2760SPrzemyslaw Czarnowski                 if (ec)
691e13c2760SPrzemyslaw Czarnowski                 {
69262598e31SEd Tanous                     BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
693e13c2760SPrzemyslaw Czarnowski 
694e13c2760SPrzemyslaw Czarnowski                     messages::internalError(asyncResp->res);
695e13c2760SPrzemyslaw Czarnowski                     return;
696e13c2760SPrzemyslaw Czarnowski                 }
697e13c2760SPrzemyslaw Czarnowski             },
698e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
699e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
700e13c2760SPrzemyslaw Czarnowski     }
701e13c2760SPrzemyslaw Czarnowski }
702e13c2760SPrzemyslaw Czarnowski 
70396825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
70496825bebSEd Tanous     crow::App& app, const crow::Request& req,
70522db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
70696825bebSEd Tanous     const std::string& name, const std::string& resName)
70796825bebSEd Tanous {
7083ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
70945ca1b86SEd Tanous     {
71045ca1b86SEd Tanous         return;
71145ca1b86SEd Tanous     }
71279fdf63eSPrzemyslaw Czarnowski 
71379fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.InsertMedia";
71422db1728SEd Tanous     if (name != "bmc")
715107077deSPrzemyslaw Czarnowski     {
71679fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
717107077deSPrzemyslaw Czarnowski 
718107077deSPrzemyslaw Czarnowski         return;
719107077deSPrzemyslaw Czarnowski     }
72079fdf63eSPrzemyslaw Czarnowski     InsertMediaActionParams actionParams;
72198be3e39SEd Tanous 
722120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
723afc474aeSMyung Bae     if (!json_util::readJsonAction(                                    //
724afc474aeSMyung Bae             req, asyncResp->res,                                       //
725afc474aeSMyung Bae             "Image", actionParams.imageUrl,                            //
726afc474aeSMyung Bae             "Inserted", actionParams.inserted,                         //
727afc474aeSMyung Bae             "Password", actionParams.password,                         //
728afc474aeSMyung Bae             "TransferMethod", actionParams.transferMethod,             //
729afc474aeSMyung Bae             "TransferProtocolType", actionParams.transferProtocolType, //
730afc474aeSMyung Bae             "UserName", actionParams.userName,                         //
731afc474aeSMyung Bae             "WriteProtected", actionParams.writeProtected              //
732afc474aeSMyung Bae             ))
73398be3e39SEd Tanous     {
73498be3e39SEd Tanous         return;
73598be3e39SEd Tanous     }
736107077deSPrzemyslaw Czarnowski 
7372b73119cSGeorge Liu     dbus::utility::getDbusObject(
7382b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
73979fdf63eSPrzemyslaw Czarnowski         [asyncResp, action, actionParams,
7402b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
741002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
74222db1728SEd Tanous             if (ec)
74322db1728SEd Tanous             {
74462598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
74579fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
746107077deSPrzemyslaw Czarnowski 
74722db1728SEd Tanous                 return;
74822db1728SEd Tanous             }
74979fdf63eSPrzemyslaw Czarnowski 
75022db1728SEd Tanous             std::string service = getObjectType.begin()->first;
75162598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
75222db1728SEd Tanous 
7535eb468daSGeorge Liu             sdbusplus::message::object_path path(
7545eb468daSGeorge Liu                 "/xyz/openbmc_project/VirtualMedia");
7555eb468daSGeorge Liu             dbus::utility::getManagedObjects(
7565eb468daSGeorge Liu                 service, path,
7575eb468daSGeorge Liu                 [service, resName, action, actionParams, asyncResp](
7585eb468daSGeorge Liu                     const boost::system::error_code& ec2,
7595eb468daSGeorge Liu                     const dbus::utility::ManagedObjectType& subtree) mutable {
7608a592810SEd Tanous                     if (ec2)
76122db1728SEd Tanous                     {
76279fdf63eSPrzemyslaw Czarnowski                         // Not possible in proxy mode
76362598e31SEd Tanous                         BMCWEB_LOG_DEBUG("InsertMedia not "
76462598e31SEd Tanous                                          "allowed in proxy mode");
765bd79bce8SPatrick Williams                         messages::resourceNotFound(asyncResp->res, action,
766bd79bce8SPatrick Williams                                                    resName);
76722db1728SEd Tanous 
76822db1728SEd Tanous                         return;
76922db1728SEd Tanous                     }
77022db1728SEd Tanous                     for (const auto& object : subtree)
77122db1728SEd Tanous                     {
772bd79bce8SPatrick Williams                         VmMode mode =
773bd79bce8SPatrick Williams                             parseObjectPathAndGetMode(object.first, resName);
7745880f0c5SBoleslaw Ogonczyk Makowski                         if (mode == VmMode::Legacy)
77522db1728SEd Tanous                         {
776bd79bce8SPatrick Williams                             validateParams(asyncResp, service, resName,
777bd79bce8SPatrick Williams                                            actionParams);
77822db1728SEd Tanous 
77922db1728SEd Tanous                             return;
78022db1728SEd Tanous                         }
78122db1728SEd Tanous                     }
78262598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Parent item not found");
783bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "VirtualMedia",
784bd79bce8SPatrick Williams                                                resName);
7855eb468daSGeorge Liu                 });
7862b73119cSGeorge Liu         });
78796825bebSEd Tanous }
78822db1728SEd Tanous 
78996825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
79096825bebSEd Tanous     crow::App& app, const crow::Request& req,
79122db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
79296825bebSEd Tanous     const std::string& managerName, const std::string& resName)
79396825bebSEd Tanous {
7943ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
79545ca1b86SEd Tanous     {
79645ca1b86SEd Tanous         return;
79745ca1b86SEd Tanous     }
79879fdf63eSPrzemyslaw Czarnowski 
79979fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.EjectMedia";
80096825bebSEd Tanous     if (managerName != "bmc")
801107077deSPrzemyslaw Czarnowski     {
80279fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
80322db1728SEd Tanous 
80422db1728SEd Tanous         return;
80522db1728SEd Tanous     }
80622db1728SEd Tanous 
8072b73119cSGeorge Liu     dbus::utility::getDbusObject(
8082b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
80979fdf63eSPrzemyslaw Czarnowski         [asyncResp, action,
8102b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
811b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
8128a592810SEd Tanous             if (ec2)
81322db1728SEd Tanous             {
814bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}",
815bd79bce8SPatrick Williams                                  ec2);
81622db1728SEd Tanous                 messages::internalError(asyncResp->res);
81722db1728SEd Tanous 
81822db1728SEd Tanous                 return;
81922db1728SEd Tanous             }
82022db1728SEd Tanous             std::string service = getObjectType.begin()->first;
82162598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
82222db1728SEd Tanous 
8235eb468daSGeorge Liu             sdbusplus::message::object_path path(
8245eb468daSGeorge Liu                 "/xyz/openbmc_project/VirtualMedia");
8255eb468daSGeorge Liu             dbus::utility::getManagedObjects(
8265eb468daSGeorge Liu                 service, path,
82779fdf63eSPrzemyslaw Czarnowski                 [resName, service, action,
82879fdf63eSPrzemyslaw Czarnowski                  asyncResp](const boost::system::error_code& ec,
82902cad96eSEd Tanous                             const dbus::utility::ManagedObjectType& subtree) {
83022db1728SEd Tanous                     if (ec)
83122db1728SEd Tanous                     {
83262598e31SEd Tanous                         BMCWEB_LOG_ERROR("ObjectMapper : No Service found");
833bd79bce8SPatrick Williams                         messages::resourceNotFound(asyncResp->res, action,
834bd79bce8SPatrick Williams                                                    resName);
83522db1728SEd Tanous                         return;
83622db1728SEd Tanous                     }
83722db1728SEd Tanous 
83822db1728SEd Tanous                     for (const auto& object : subtree)
83922db1728SEd Tanous                     {
840bd79bce8SPatrick Williams                         VmMode mode =
841bd79bce8SPatrick Williams                             parseObjectPathAndGetMode(object.first, resName);
842365a73f4SEd Tanous                         if (mode != VmMode::Invalid)
84322db1728SEd Tanous                         {
844365a73f4SEd Tanous                             doEjectAction(asyncResp, service, resName,
845365a73f4SEd Tanous                                           mode == VmMode::Legacy);
8465880f0c5SBoleslaw Ogonczyk Makowski                             return;
84722db1728SEd Tanous                         }
84822db1728SEd Tanous                     }
84962598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Parent item not found");
850bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "VirtualMedia",
851bd79bce8SPatrick Williams                                                resName);
8525eb468daSGeorge Liu                 });
8532b73119cSGeorge Liu         });
85496825bebSEd Tanous }
85596825bebSEd Tanous 
85696825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
85796825bebSEd Tanous     crow::App& app, const crow::Request& req,
85822db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
85996825bebSEd Tanous     const std::string& name)
86096825bebSEd Tanous {
8613ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
86245ca1b86SEd Tanous     {
86345ca1b86SEd Tanous         return;
86445ca1b86SEd Tanous     }
86522db1728SEd Tanous     if (name != "bmc")
86622db1728SEd Tanous     {
867002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
868107077deSPrzemyslaw Czarnowski 
869107077deSPrzemyslaw Czarnowski         return;
870107077deSPrzemyslaw Czarnowski     }
871107077deSPrzemyslaw Czarnowski 
8728d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
873107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
8748d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
875ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
876ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name);
877107077deSPrzemyslaw Czarnowski 
8782b73119cSGeorge Liu     dbus::utility::getDbusObject(
8792b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
8802b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
881b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
882107077deSPrzemyslaw Czarnowski             if (ec)
883107077deSPrzemyslaw Czarnowski             {
88462598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
885107077deSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
886107077deSPrzemyslaw Czarnowski 
887107077deSPrzemyslaw Czarnowski                 return;
888107077deSPrzemyslaw Czarnowski             }
889107077deSPrzemyslaw Czarnowski             std::string service = getObjectType.begin()->first;
89062598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
891107077deSPrzemyslaw Czarnowski 
892107077deSPrzemyslaw Czarnowski             getVmResourceList(asyncResp, service, name);
8932b73119cSGeorge Liu         });
89496825bebSEd Tanous }
895107077deSPrzemyslaw Czarnowski 
896504af5a0SPatrick Williams inline void handleVirtualMediaGet(
897504af5a0SPatrick Williams     crow::App& app, const crow::Request& req,
89822db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
89996825bebSEd Tanous     const std::string& name, const std::string& resName)
90096825bebSEd Tanous {
9013ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
90245ca1b86SEd Tanous     {
90345ca1b86SEd Tanous         return;
90445ca1b86SEd Tanous     }
905107077deSPrzemyslaw Czarnowski     if (name != "bmc")
906107077deSPrzemyslaw Czarnowski     {
907002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
908107077deSPrzemyslaw Czarnowski 
909107077deSPrzemyslaw Czarnowski         return;
910107077deSPrzemyslaw Czarnowski     }
911107077deSPrzemyslaw Czarnowski 
9122b73119cSGeorge Liu     dbus::utility::getDbusObject(
9132b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
914002d39b4SEd Tanous         [asyncResp, name,
9152b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
916b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
917107077deSPrzemyslaw Czarnowski             if (ec)
918107077deSPrzemyslaw Czarnowski             {
91962598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
920107077deSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
921107077deSPrzemyslaw Czarnowski 
922107077deSPrzemyslaw Czarnowski                 return;
923107077deSPrzemyslaw Czarnowski             }
924107077deSPrzemyslaw Czarnowski             std::string service = getObjectType.begin()->first;
92562598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
926107077deSPrzemyslaw Czarnowski 
927107077deSPrzemyslaw Czarnowski             getVmData(asyncResp, service, name, resName);
9282b73119cSGeorge Liu         });
92996825bebSEd Tanous }
93096825bebSEd Tanous 
93196825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
93296825bebSEd Tanous {
93396825bebSEd Tanous     BMCWEB_ROUTE(
93496825bebSEd Tanous         app,
93596825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
93696825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
93796825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
93896825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
93996825bebSEd Tanous 
94096825bebSEd Tanous     BMCWEB_ROUTE(
94196825bebSEd Tanous         app,
94296825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
94396825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
94496825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
94596825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
94696825bebSEd Tanous 
94796825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
94896825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
94996825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
95096825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
95196825bebSEd Tanous 
95296825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
95396825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
95496825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
95596825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
956107077deSPrzemyslaw Czarnowski }
957107077deSPrzemyslaw Czarnowski 
958107077deSPrzemyslaw Czarnowski } // namespace redfish
959