xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision d78572018fc2022091ff8b8eb5a7fef2172ba3d6)
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"
9*d7857201SEd Tanous #include "dbus_singleton.hpp"
102b73119cSGeorge Liu #include "dbus_utility.hpp"
11*d7857201SEd Tanous #include "error_messages.hpp"
12739b87efSEd Tanous #include "generated/enums/virtual_media.hpp"
13*d7857201SEd Tanous #include "http_request.hpp"
14*d7857201SEd 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 
19*d7857201SEd Tanous #include <boost/beast/http/status.hpp>
20*d7857201SEd Tanous #include <boost/beast/http/verb.hpp>
21*d7857201SEd Tanous #include <boost/system/result.hpp>
22ef4c65b7SEd Tanous #include <boost/url/format.hpp>
23*d7857201SEd Tanous #include <boost/url/parse.hpp>
249e319cf0SAnna Platash #include <boost/url/url_view.hpp>
254a7fbefdSEd Tanous #include <boost/url/url_view_base.hpp>
26*d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp>
27107077deSPrzemyslaw Czarnowski 
28*d7857201SEd Tanous #include <cstddef>
29*d7857201SEd Tanous #include <filesystem>
30*d7857201SEd Tanous #include <functional>
31*d7857201SEd Tanous #include <memory>
32*d7857201SEd Tanous #include <optional>
333544d2a7SEd Tanous #include <ranges>
34*d7857201SEd Tanous #include <string>
352b73119cSGeorge Liu #include <string_view>
36*d7857201SEd Tanous #include <utility>
37*d7857201SEd Tanous #include <variant>
382b73119cSGeorge Liu 
39107077deSPrzemyslaw Czarnowski namespace redfish
40107077deSPrzemyslaw Czarnowski {
41365a73f4SEd Tanous 
42365a73f4SEd Tanous enum class VmMode
43365a73f4SEd Tanous {
44365a73f4SEd Tanous     Invalid,
45365a73f4SEd Tanous     Legacy,
46365a73f4SEd Tanous     Proxy
47365a73f4SEd Tanous };
48365a73f4SEd Tanous 
49bd79bce8SPatrick Williams inline VmMode parseObjectPathAndGetMode(
50bd79bce8SPatrick Williams     const sdbusplus::message::object_path& itemPath, const std::string& resName)
51365a73f4SEd Tanous {
52365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
5362598e31SEd Tanous     BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath);
54365a73f4SEd Tanous 
55365a73f4SEd Tanous     if (thisPath.empty())
56365a73f4SEd Tanous     {
57365a73f4SEd Tanous         return VmMode::Invalid;
58365a73f4SEd Tanous     }
59365a73f4SEd Tanous 
60365a73f4SEd Tanous     if (thisPath != resName)
61365a73f4SEd Tanous     {
62365a73f4SEd Tanous         return VmMode::Invalid;
63365a73f4SEd Tanous     }
64365a73f4SEd Tanous 
65365a73f4SEd Tanous     auto mode = itemPath.parent_path();
66365a73f4SEd Tanous     auto type = mode.parent_path();
67365a73f4SEd Tanous 
68365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
69365a73f4SEd Tanous     {
70365a73f4SEd Tanous         return VmMode::Invalid;
71365a73f4SEd Tanous     }
72365a73f4SEd Tanous 
73365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
74365a73f4SEd Tanous     {
75365a73f4SEd Tanous         return VmMode::Invalid;
76365a73f4SEd Tanous     }
77365a73f4SEd Tanous     std::string modeStr = mode.filename();
78365a73f4SEd Tanous     if (modeStr == "Legacy")
79365a73f4SEd Tanous     {
80365a73f4SEd Tanous         return VmMode::Legacy;
81365a73f4SEd Tanous     }
82365a73f4SEd Tanous     if (modeStr == "Proxy")
83365a73f4SEd Tanous     {
84365a73f4SEd Tanous         return VmMode::Proxy;
85365a73f4SEd Tanous     }
86365a73f4SEd Tanous     return VmMode::Invalid;
87365a73f4SEd Tanous }
88365a73f4SEd Tanous 
8979fdf63eSPrzemyslaw Czarnowski using CheckItemHandler =
9079fdf63eSPrzemyslaw Czarnowski     std::function<void(const std::string& service, const std::string& resName,
9179fdf63eSPrzemyslaw Czarnowski                        const std::shared_ptr<bmcweb::AsyncResp>&,
9270cbdf53SGeorge Liu                        const std::pair<sdbusplus::message::object_path,
9380f79a40SMichael Shen                                        dbus::utility::DBusInterfacesMap>&)>;
9479fdf63eSPrzemyslaw Czarnowski 
95ac106bf6SEd Tanous inline void
96ac106bf6SEd Tanous     findAndParseObject(const std::string& service, const std::string& resName,
97ac106bf6SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9879fdf63eSPrzemyslaw Czarnowski                        CheckItemHandler&& handler)
9979fdf63eSPrzemyslaw Czarnowski {
1005eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia");
1015eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1025eb468daSGeorge Liu         service, path,
1038cb2c024SEd Tanous         [service, resName, asyncResp, handler = std::move(handler)](
1048cb2c024SEd Tanous             const boost::system::error_code& ec,
10570cbdf53SGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
10679fdf63eSPrzemyslaw Czarnowski             if (ec)
10779fdf63eSPrzemyslaw Czarnowski             {
10862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error");
10979fdf63eSPrzemyslaw Czarnowski 
11079fdf63eSPrzemyslaw Czarnowski                 return;
11179fdf63eSPrzemyslaw Czarnowski             }
11279fdf63eSPrzemyslaw Czarnowski 
11370cbdf53SGeorge Liu             for (const auto& item : subtree)
11479fdf63eSPrzemyslaw Czarnowski             {
11579fdf63eSPrzemyslaw Czarnowski                 VmMode mode = parseObjectPathAndGetMode(item.first, resName);
11679fdf63eSPrzemyslaw Czarnowski                 if (mode != VmMode::Invalid)
11779fdf63eSPrzemyslaw Czarnowski                 {
118ac106bf6SEd Tanous                     handler(service, resName, asyncResp, item);
11979fdf63eSPrzemyslaw Czarnowski                     return;
12079fdf63eSPrzemyslaw Czarnowski                 }
12179fdf63eSPrzemyslaw Czarnowski             }
12279fdf63eSPrzemyslaw Czarnowski 
12362598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
124ac106bf6SEd Tanous             asyncResp->res.result(boost::beast::http::status::not_found);
1255eb468daSGeorge Liu         });
12679fdf63eSPrzemyslaw Czarnowski }
12779fdf63eSPrzemyslaw Czarnowski 
1289e319cf0SAnna Platash /**
1299e319cf0SAnna Platash  * @brief Function extracts transfer protocol name from URI.
1309e319cf0SAnna Platash  */
13167df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
13267df073bSEd Tanous {
1336fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
134079360aeSEd Tanous         boost::urls::parse_uri(imageUri);
13567df073bSEd Tanous     if (!url)
13667df073bSEd Tanous     {
13767df073bSEd Tanous         return "None";
13867df073bSEd Tanous     }
139079360aeSEd Tanous     std::string_view scheme = url->scheme();
14067df073bSEd Tanous     if (scheme == "smb")
14167df073bSEd Tanous     {
14267df073bSEd Tanous         return "CIFS";
14367df073bSEd Tanous     }
14467df073bSEd Tanous     if (scheme == "https")
14567df073bSEd Tanous     {
14667df073bSEd Tanous         return "HTTPS";
14767df073bSEd Tanous     }
14867df073bSEd Tanous 
14967df073bSEd Tanous     return "None";
15067df073bSEd Tanous }
151107077deSPrzemyslaw Czarnowski 
152107077deSPrzemyslaw Czarnowski /**
153107077deSPrzemyslaw Czarnowski  * @brief Read all known properties from VM object interfaces
154107077deSPrzemyslaw Czarnowski  */
15522db1728SEd Tanous inline void
15680f79a40SMichael Shen     vmParseInterfaceObject(const dbus::utility::DBusInterfacesMap& interfaces,
157ac106bf6SEd Tanous                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
158107077deSPrzemyslaw Czarnowski {
1598a592810SEd Tanous     for (const auto& [interface, values] : interfaces)
160107077deSPrzemyslaw Czarnowski     {
161711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
162107077deSPrzemyslaw Czarnowski         {
163711ac7a9SEd Tanous             for (const auto& [property, value] : values)
164107077deSPrzemyslaw Czarnowski             {
165711ac7a9SEd Tanous                 if (property == "EndpointId")
166107077deSPrzemyslaw Czarnowski                 {
167107077deSPrzemyslaw Czarnowski                     const std::string* endpointIdValue =
168711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
169711ac7a9SEd Tanous                     if (endpointIdValue == nullptr)
170107077deSPrzemyslaw Czarnowski                     {
171711ac7a9SEd Tanous                         continue;
172711ac7a9SEd Tanous                     }
173107077deSPrzemyslaw Czarnowski                     if (!endpointIdValue->empty())
174107077deSPrzemyslaw Czarnowski                     {
175107077deSPrzemyslaw Czarnowski                         // Proxy mode
176ac106bf6SEd Tanous                         asyncResp->res
177711ac7a9SEd Tanous                             .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
178d04ba325SPrzemyslaw Czarnowski                             *endpointIdValue;
179ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
180ac106bf6SEd Tanous                             "OEM";
181107077deSPrzemyslaw Czarnowski                     }
182107077deSPrzemyslaw Czarnowski                 }
183711ac7a9SEd Tanous                 if (property == "ImageURL")
184107077deSPrzemyslaw Czarnowski                 {
185107077deSPrzemyslaw Czarnowski                     const std::string* imageUrlValue =
186711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
18726f6976fSEd Tanous                     if (imageUrlValue != nullptr && !imageUrlValue->empty())
188107077deSPrzemyslaw Czarnowski                     {
189da4784d8SPrzemyslaw Czarnowski                         std::filesystem::path filePath = *imageUrlValue;
190da4784d8SPrzemyslaw Czarnowski                         if (!filePath.has_filename())
191da4784d8SPrzemyslaw Czarnowski                         {
1929e319cf0SAnna Platash                             // this will handle https share, which not
1939e319cf0SAnna Platash                             // necessarily has to have filename given.
194ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] = "";
195da4784d8SPrzemyslaw Czarnowski                         }
196da4784d8SPrzemyslaw Czarnowski                         else
197da4784d8SPrzemyslaw Czarnowski                         {
198ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] =
1999e319cf0SAnna Platash                                 filePath.filename();
200da4784d8SPrzemyslaw Czarnowski                         }
201da4784d8SPrzemyslaw Czarnowski 
202ac106bf6SEd Tanous                         asyncResp->res.jsonValue["Image"] = *imageUrlValue;
203ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
2049e319cf0SAnna Platash                             getTransferProtocolTypeFromUri(*imageUrlValue);
2059e319cf0SAnna Platash 
206ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
207739b87efSEd Tanous                             virtual_media::ConnectedVia::URI;
208107077deSPrzemyslaw Czarnowski                     }
209107077deSPrzemyslaw Czarnowski                 }
210711ac7a9SEd Tanous                 if (property == "WriteProtected")
2119e319cf0SAnna Platash                 {
212711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
213e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
2149e319cf0SAnna Platash                     {
215ac106bf6SEd Tanous                         asyncResp->res.jsonValue["WriteProtected"] =
2169e319cf0SAnna Platash                             *writeProtectedValue;
2179e319cf0SAnna Platash                     }
2189e319cf0SAnna Platash                 }
2199e319cf0SAnna Platash             }
220107077deSPrzemyslaw Czarnowski         }
221711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
222711ac7a9SEd Tanous         {
223711ac7a9SEd Tanous             for (const auto& [property, value] : values)
224711ac7a9SEd Tanous             {
225711ac7a9SEd Tanous                 if (property == "Active")
226711ac7a9SEd Tanous                 {
227711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
228e662eae8SEd Tanous                     if (activeValue == nullptr)
229711ac7a9SEd Tanous                     {
23062598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Value Active not found");
231711ac7a9SEd Tanous                         return;
232711ac7a9SEd Tanous                     }
233ac106bf6SEd Tanous                     asyncResp->res.jsonValue["Inserted"] = *activeValue;
234711ac7a9SEd Tanous 
235e05aec50SEd Tanous                     if (*activeValue)
236711ac7a9SEd Tanous                     {
237ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
238739b87efSEd Tanous                             virtual_media::ConnectedVia::Applet;
239711ac7a9SEd Tanous                     }
240711ac7a9SEd Tanous                 }
241711ac7a9SEd Tanous             }
242711ac7a9SEd Tanous         }
243107077deSPrzemyslaw Czarnowski     }
244107077deSPrzemyslaw Czarnowski }
245107077deSPrzemyslaw Czarnowski 
246107077deSPrzemyslaw Czarnowski /**
247107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
248107077deSPrzemyslaw Czarnowski  */
24922db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
250107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
251107077deSPrzemyslaw Czarnowski {
252107077deSPrzemyslaw Czarnowski     nlohmann::json item;
253ef4c65b7SEd Tanous     item["@odata.id"] = boost::urls::format(
254ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName);
25522db1728SEd Tanous 
256d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
257107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
258107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
259107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
260739b87efSEd Tanous     item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
261613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
262539d8c6bSEd Tanous     item["TransferMethod"] = virtual_media::TransferMethod::Stream;
263d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
264f958ed9cSEd Tanous         "#OpenBMCVirtualMedia.v1_0_0.VirtualMedia";
26515b89725SV-Sanjana     item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format(
26615b89725SV-Sanjana         "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName);
267107077deSPrzemyslaw Czarnowski 
268107077deSPrzemyslaw Czarnowski     return item;
269107077deSPrzemyslaw Czarnowski }
270107077deSPrzemyslaw Czarnowski 
271107077deSPrzemyslaw Czarnowski /**
272107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
273107077deSPrzemyslaw Czarnowski  */
274ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
275107077deSPrzemyslaw Czarnowski                               const std::string& service,
276107077deSPrzemyslaw Czarnowski                               const std::string& name)
277107077deSPrzemyslaw Czarnowski {
27862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get available Virtual Media resources.");
2795eb468daSGeorge Liu     sdbusplus::message::object_path objPath(
2805eb468daSGeorge Liu         "/xyz/openbmc_project/VirtualMedia");
2815eb468daSGeorge Liu     dbus::utility::getManagedObjects(
2825eb468daSGeorge Liu         service, objPath,
283ac106bf6SEd Tanous         [name, asyncResp{std::move(asyncResp)}](
2845e7e2dc5SEd Tanous             const boost::system::error_code& ec,
28502cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
286107077deSPrzemyslaw Czarnowski             if (ec)
287107077deSPrzemyslaw Czarnowski             {
28862598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error");
289107077deSPrzemyslaw Czarnowski                 return;
290107077deSPrzemyslaw Czarnowski             }
291ac106bf6SEd Tanous             nlohmann::json& members = asyncResp->res.jsonValue["Members"];
292107077deSPrzemyslaw Czarnowski             members = nlohmann::json::array();
293107077deSPrzemyslaw Czarnowski 
294107077deSPrzemyslaw Czarnowski             for (const auto& object : subtree)
295107077deSPrzemyslaw Czarnowski             {
296107077deSPrzemyslaw Czarnowski                 nlohmann::json item;
2972dfd18efSEd Tanous                 std::string path = object.first.filename();
2982dfd18efSEd Tanous                 if (path.empty())
299107077deSPrzemyslaw Czarnowski                 {
300107077deSPrzemyslaw Czarnowski                     continue;
301107077deSPrzemyslaw Czarnowski                 }
302107077deSPrzemyslaw Czarnowski 
303ef4c65b7SEd Tanous                 item["@odata.id"] = boost::urls::format(
304ef4c65b7SEd Tanous                     "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path);
305107077deSPrzemyslaw Czarnowski                 members.emplace_back(std::move(item));
306107077deSPrzemyslaw Czarnowski             }
307ac106bf6SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = members.size();
3085eb468daSGeorge Liu         });
309107077deSPrzemyslaw Czarnowski }
310107077deSPrzemyslaw Czarnowski 
31170cbdf53SGeorge Liu inline void
31270cbdf53SGeorge Liu     afterGetVmData(const std::string& name, const std::string& /*service*/,
31379fdf63eSPrzemyslaw Czarnowski                    const std::string& resName,
31479fdf63eSPrzemyslaw Czarnowski                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31570cbdf53SGeorge Liu                    const std::pair<sdbusplus::message::object_path,
31680f79a40SMichael Shen                                    dbus::utility::DBusInterfacesMap>& item)
31779fdf63eSPrzemyslaw Czarnowski {
31879fdf63eSPrzemyslaw Czarnowski     VmMode mode = parseObjectPathAndGetMode(item.first, resName);
31979fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Invalid)
32079fdf63eSPrzemyslaw Czarnowski     {
32179fdf63eSPrzemyslaw Czarnowski         return;
32279fdf63eSPrzemyslaw Czarnowski     }
32379fdf63eSPrzemyslaw Czarnowski 
32479fdf63eSPrzemyslaw Czarnowski     asyncResp->res.jsonValue = vmItemTemplate(name, resName);
32579fdf63eSPrzemyslaw Czarnowski 
32679fdf63eSPrzemyslaw Czarnowski     // Check if dbus path is Legacy type
32779fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Legacy)
32879fdf63eSPrzemyslaw Czarnowski     {
329ef4c65b7SEd Tanous         asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
330ef4c65b7SEd Tanous                                 ["target"] = boost::urls::format(
331ef4c65b7SEd Tanous             "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia",
332ef4c65b7SEd Tanous             name, resName);
33379fdf63eSPrzemyslaw Czarnowski     }
33479fdf63eSPrzemyslaw Czarnowski 
33579fdf63eSPrzemyslaw Czarnowski     vmParseInterfaceObject(item.second, asyncResp);
33679fdf63eSPrzemyslaw Czarnowski 
337ef4c65b7SEd Tanous     asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"]
338ef4c65b7SEd Tanous                             ["target"] = boost::urls::format(
339ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia",
340ef4c65b7SEd Tanous         name, resName);
34179fdf63eSPrzemyslaw Czarnowski }
34279fdf63eSPrzemyslaw Czarnowski 
343107077deSPrzemyslaw Czarnowski /**
344107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
345107077deSPrzemyslaw Czarnowski  */
346ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
347107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
348107077deSPrzemyslaw Czarnowski                       const std::string& resName)
349107077deSPrzemyslaw Czarnowski {
35062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Virtual Media resource data.");
351107077deSPrzemyslaw Czarnowski 
352ac106bf6SEd Tanous     findAndParseObject(service, resName, asyncResp,
35370cbdf53SGeorge Liu                        std::bind_front(afterGetVmData, name));
354107077deSPrzemyslaw Czarnowski }
355107077deSPrzemyslaw Czarnowski 
356e13c2760SPrzemyslaw Czarnowski /**
357c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
358c6f4e017SAgata Olender  *
359c6f4e017SAgata Olender  */
360c6f4e017SAgata Olender enum class TransferProtocol
361c6f4e017SAgata Olender {
362c6f4e017SAgata Olender     https,
363c6f4e017SAgata Olender     smb,
364c6f4e017SAgata Olender     invalid
365c6f4e017SAgata Olender };
366c6f4e017SAgata Olender 
367c6f4e017SAgata Olender /**
368c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
369c6f4e017SAgata Olender  *
370c6f4e017SAgata Olender  */
37167df073bSEd Tanous inline std::optional<TransferProtocol>
3724a7fbefdSEd Tanous     getTransferProtocolFromUri(const boost::urls::url_view_base& imageUri)
37367df073bSEd Tanous {
374079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
37567df073bSEd Tanous     if (scheme == "smb")
37667df073bSEd Tanous     {
37767df073bSEd Tanous         return TransferProtocol::smb;
37867df073bSEd Tanous     }
37967df073bSEd Tanous     if (scheme == "https")
38067df073bSEd Tanous     {
38167df073bSEd Tanous         return TransferProtocol::https;
38267df073bSEd Tanous     }
38367df073bSEd Tanous     if (!scheme.empty())
38467df073bSEd Tanous     {
38567df073bSEd Tanous         return TransferProtocol::invalid;
38667df073bSEd Tanous     }
38767df073bSEd Tanous 
38867df073bSEd Tanous     return {};
38967df073bSEd Tanous }
390c6f4e017SAgata Olender 
391c6f4e017SAgata Olender /**
392c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
393c6f4e017SAgata Olender  *
394c6f4e017SAgata Olender  */
39522db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
396c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
397c6f4e017SAgata Olender {
398e01d0c36SEd Tanous     if (!transferProtocolType)
399c6f4e017SAgata Olender     {
400c6f4e017SAgata Olender         return {};
401c6f4e017SAgata Olender     }
402c6f4e017SAgata Olender 
403c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
404c6f4e017SAgata Olender     {
405c6f4e017SAgata Olender         return TransferProtocol::smb;
406c6f4e017SAgata Olender     }
407c6f4e017SAgata Olender 
408c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
409c6f4e017SAgata Olender     {
410c6f4e017SAgata Olender         return TransferProtocol::https;
411c6f4e017SAgata Olender     }
412c6f4e017SAgata Olender 
413c6f4e017SAgata Olender     return TransferProtocol::invalid;
414c6f4e017SAgata Olender }
415c6f4e017SAgata Olender 
416c6f4e017SAgata Olender /**
417c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
418c6f4e017SAgata Olender  *
419c6f4e017SAgata Olender  */
420bd79bce8SPatrick Williams inline std::string getUriWithTransferProtocol(
421bd79bce8SPatrick Williams     const std::string& imageUri, const TransferProtocol& transferProtocol)
422c6f4e017SAgata Olender {
423c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
424c6f4e017SAgata Olender     {
425c6f4e017SAgata Olender         return "smb://" + imageUri;
426c6f4e017SAgata Olender     }
427c6f4e017SAgata Olender 
428c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
429c6f4e017SAgata Olender     {
430c6f4e017SAgata Olender         return "https://" + imageUri;
431c6f4e017SAgata Olender     }
432c6f4e017SAgata Olender 
433c6f4e017SAgata Olender     return imageUri;
434c6f4e017SAgata Olender }
435c6f4e017SAgata Olender 
4361f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
4371f2a40ceSPrzemyslaw Czarnowski {
438120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
4391f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4401f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4411f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4421f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4431f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4441f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4451f2a40ceSPrzemyslaw Czarnowski };
4461f2a40ceSPrzemyslaw Czarnowski 
447e13c2760SPrzemyslaw Czarnowski /**
448e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
449e13c2760SPrzemyslaw Czarnowski  *
450e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
451e13c2760SPrzemyslaw Czarnowski  */
45222db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
453e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
45411e8f60dSEd Tanous                             const std::string& imageUrl, bool rw,
455988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
456e13c2760SPrzemyslaw Czarnowski {
45711e8f60dSEd Tanous     int fd = -1;
45811e8f60dSEd Tanous     std::shared_ptr<CredentialsPipe> secretPipe;
459988fb7b2SAdrian Ambrożewicz     if (!userName.empty() || !password.empty())
460988fb7b2SAdrian Ambrożewicz     {
461988fb7b2SAdrian Ambrożewicz         // Payload must contain data + NULL delimiters
46211e8f60dSEd Tanous         constexpr const size_t secretLimit = 1024;
46311e8f60dSEd Tanous         if (userName.size() + password.size() + 2 > secretLimit)
464988fb7b2SAdrian Ambrożewicz         {
46562598e31SEd Tanous             BMCWEB_LOG_ERROR("Credentials too long to handle");
466988fb7b2SAdrian Ambrożewicz             messages::unrecognizedRequestBody(asyncResp->res);
467988fb7b2SAdrian Ambrożewicz             return;
468988fb7b2SAdrian Ambrożewicz         }
469988fb7b2SAdrian Ambrożewicz 
470988fb7b2SAdrian Ambrożewicz         // Open pipe
47111e8f60dSEd Tanous         secretPipe = std::make_shared<CredentialsPipe>(
47211e8f60dSEd Tanous             crow::connections::systemBus->get_io_context());
4733bfa3b29SEd Tanous         fd = secretPipe->releaseFd();
474988fb7b2SAdrian Ambrożewicz 
475988fb7b2SAdrian Ambrożewicz         // Pass secret over pipe
47681ce609eSEd Tanous         secretPipe->asyncWrite(
47711e8f60dSEd Tanous             std::move(userName), std::move(password),
478bd79bce8SPatrick Williams             [asyncResp,
479bd79bce8SPatrick Williams              secretPipe](const boost::system::error_code& ec, std::size_t) {
480988fb7b2SAdrian Ambrożewicz                 if (ec)
481988fb7b2SAdrian Ambrożewicz                 {
48262598e31SEd Tanous                     BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec);
483988fb7b2SAdrian Ambrożewicz                     messages::internalError(asyncResp->res);
484988fb7b2SAdrian Ambrożewicz                 }
485988fb7b2SAdrian Ambrożewicz             });
486988fb7b2SAdrian Ambrożewicz     }
487988fb7b2SAdrian Ambrożewicz 
488e3648032SEd Tanous     std::variant<sdbusplus::message::unix_fd> unixFd(
48911e8f60dSEd Tanous         std::in_place_type<sdbusplus::message::unix_fd>, fd);
49011e8f60dSEd Tanous 
49111e8f60dSEd Tanous     sdbusplus::message::object_path path(
49211e8f60dSEd Tanous         "/xyz/openbmc_project/VirtualMedia/Legacy");
49311e8f60dSEd Tanous     path /= name;
494e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
495bd79bce8SPatrick Williams         [asyncResp,
496bd79bce8SPatrick Williams          secretPipe](const boost::system::error_code& ec, bool success) {
497e13c2760SPrzemyslaw Czarnowski             if (ec)
498e13c2760SPrzemyslaw Czarnowski             {
49962598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
500e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
50111e8f60dSEd Tanous                 return;
502d6da5bebSAdrian Ambrożewicz             }
50311e8f60dSEd Tanous             if (!success)
504d6da5bebSAdrian Ambrożewicz             {
50562598e31SEd Tanous                 BMCWEB_LOG_ERROR("Service responded with error");
50611e8f60dSEd Tanous                 messages::internalError(asyncResp->res);
507e13c2760SPrzemyslaw Czarnowski             }
508e13c2760SPrzemyslaw Czarnowski         },
50911e8f60dSEd Tanous         service, path.str, "xyz.openbmc_project.VirtualMedia.Legacy", "Mount",
51011e8f60dSEd Tanous         imageUrl, rw, unixFd);
511e13c2760SPrzemyslaw Czarnowski }
512e13c2760SPrzemyslaw Czarnowski 
513e13c2760SPrzemyslaw Czarnowski /**
514120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
515120fa86aSPrzemyslaw Czarnowski  *
516120fa86aSPrzemyslaw Czarnowski  */
517120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
518120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
519120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
520120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
521120fa86aSPrzemyslaw Czarnowski {
52262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Validation started");
523120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
524120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
525120fa86aSPrzemyslaw Czarnowski     {
52662598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter Image is empty.");
527120fa86aSPrzemyslaw Czarnowski 
528120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
529120fa86aSPrzemyslaw Czarnowski 
530120fa86aSPrzemyslaw Czarnowski         return;
531120fa86aSPrzemyslaw Czarnowski     }
532120fa86aSPrzemyslaw Czarnowski 
533120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
534e01d0c36SEd Tanous     if (actionParams.inserted && !*actionParams.inserted)
535120fa86aSPrzemyslaw Czarnowski     {
53662598e31SEd Tanous         BMCWEB_LOG_ERROR(
53762598e31SEd Tanous             "Request action optional parameter Inserted must be true.");
538120fa86aSPrzemyslaw Czarnowski 
539120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
540120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
541120fa86aSPrzemyslaw Czarnowski 
542120fa86aSPrzemyslaw Czarnowski         return;
543120fa86aSPrzemyslaw Czarnowski     }
544120fa86aSPrzemyslaw Czarnowski 
545120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
546e01d0c36SEd Tanous     if (actionParams.transferMethod &&
547120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
548120fa86aSPrzemyslaw Czarnowski     {
54962598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action optional parameter "
55062598e31SEd Tanous                          "TransferMethod must be Stream.");
551120fa86aSPrzemyslaw Czarnowski 
552120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
553120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
554120fa86aSPrzemyslaw Czarnowski 
555120fa86aSPrzemyslaw Czarnowski         return;
556120fa86aSPrzemyslaw Czarnowski     }
5576fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
558120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
559120fa86aSPrzemyslaw Czarnowski     if (!url)
560120fa86aSPrzemyslaw Czarnowski     {
561120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
562120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
563120fa86aSPrzemyslaw Czarnowski         return;
564120fa86aSPrzemyslaw Czarnowski     }
565120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
566120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
567120fa86aSPrzemyslaw Czarnowski 
568120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
569120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
570120fa86aSPrzemyslaw Czarnowski 
571120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
572e01d0c36SEd Tanous     if (uriTransferProtocolType &&
573e01d0c36SEd Tanous         *uriTransferProtocolType == TransferProtocol::invalid)
574120fa86aSPrzemyslaw Czarnowski     {
57562598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
576120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type from list: "
57762598e31SEd Tanous                          "(smb, https).");
578120fa86aSPrzemyslaw Czarnowski 
579120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
580120fa86aSPrzemyslaw Czarnowski 
581120fa86aSPrzemyslaw Czarnowski         return;
582120fa86aSPrzemyslaw Czarnowski     }
583120fa86aSPrzemyslaw Czarnowski 
584120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
585e01d0c36SEd Tanous     if (paramTransferProtocolType &&
586e01d0c36SEd Tanous         *paramTransferProtocolType == TransferProtocol::invalid)
587120fa86aSPrzemyslaw Czarnowski     {
58862598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType "
589120fa86aSPrzemyslaw Czarnowski                          "must be provided with value from list: "
59062598e31SEd Tanous                          "(CIFS, HTTPS).");
591120fa86aSPrzemyslaw Czarnowski 
592e01d0c36SEd Tanous         messages::propertyValueNotInList(
593e01d0c36SEd Tanous             asyncResp->res, actionParams.transferProtocolType.value_or(""),
594120fa86aSPrzemyslaw Czarnowski             "TransferProtocolType");
595120fa86aSPrzemyslaw Czarnowski         return;
596120fa86aSPrzemyslaw Czarnowski     }
597120fa86aSPrzemyslaw Czarnowski 
598120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
599e01d0c36SEd Tanous     if (!uriTransferProtocolType && !paramTransferProtocolType)
600120fa86aSPrzemyslaw Czarnowski     {
60162598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
602120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type or param "
60362598e31SEd Tanous                          "TransferProtocolType must be provided.");
604120fa86aSPrzemyslaw Czarnowski 
605120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
606120fa86aSPrzemyslaw Czarnowski 
607120fa86aSPrzemyslaw Czarnowski         return;
608120fa86aSPrzemyslaw Czarnowski     }
609120fa86aSPrzemyslaw Czarnowski 
610120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
611e01d0c36SEd Tanous     if (paramTransferProtocolType && uriTransferProtocolType)
612120fa86aSPrzemyslaw Czarnowski     {
613120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
614120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
615120fa86aSPrzemyslaw Czarnowski         {
61662598e31SEd Tanous             BMCWEB_LOG_ERROR("Request action parameter "
617120fa86aSPrzemyslaw Czarnowski                              "TransferProtocolType must  contain the "
618120fa86aSPrzemyslaw Czarnowski                              "same protocol type as protocol type "
61962598e31SEd Tanous                              "provided with param imageUrl.");
620120fa86aSPrzemyslaw Czarnowski 
621120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
622e01d0c36SEd Tanous                 asyncResp->res, actionParams.transferProtocolType.value_or(""),
623120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
624120fa86aSPrzemyslaw Czarnowski 
625120fa86aSPrzemyslaw Czarnowski             return;
626120fa86aSPrzemyslaw Czarnowski         }
627120fa86aSPrzemyslaw Czarnowski     }
628120fa86aSPrzemyslaw Czarnowski 
629120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
6307ead48e6SBoleslaw Ogonczyk Makowski     if (!uriTransferProtocolType && paramTransferProtocolType)
631120fa86aSPrzemyslaw Czarnowski     {
632120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
633120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
634120fa86aSPrzemyslaw Czarnowski     }
635120fa86aSPrzemyslaw Czarnowski 
636452bd8d8SJayaprakash Mutyala     if (!actionParams.userName)
637452bd8d8SJayaprakash Mutyala     {
638452bd8d8SJayaprakash Mutyala         actionParams.userName = "";
639452bd8d8SJayaprakash Mutyala     }
640452bd8d8SJayaprakash Mutyala 
641452bd8d8SJayaprakash Mutyala     if (!actionParams.password)
642452bd8d8SJayaprakash Mutyala     {
643452bd8d8SJayaprakash Mutyala         actionParams.password = "";
644452bd8d8SJayaprakash Mutyala     }
645452bd8d8SJayaprakash Mutyala 
646120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
647e01d0c36SEd Tanous                     !(actionParams.writeProtected.value_or(false)),
648120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
649120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
650120fa86aSPrzemyslaw Czarnowski }
651120fa86aSPrzemyslaw Czarnowski 
652120fa86aSPrzemyslaw Czarnowski /**
653e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
654e13c2760SPrzemyslaw Czarnowski  *
655e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
656e13c2760SPrzemyslaw Czarnowski  */
65724e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
658e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
659e13c2760SPrzemyslaw Czarnowski                           bool legacy)
660e13c2760SPrzemyslaw Czarnowski {
661e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
662e13c2760SPrzemyslaw Czarnowski     if (legacy)
663e13c2760SPrzemyslaw Czarnowski     {
664e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6655e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
666e13c2760SPrzemyslaw Czarnowski                 if (ec)
667e13c2760SPrzemyslaw Czarnowski                 {
66862598e31SEd Tanous                     BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
669e13c2760SPrzemyslaw Czarnowski 
670e13c2760SPrzemyslaw Czarnowski                     messages::internalError(asyncResp->res);
671e13c2760SPrzemyslaw Czarnowski                     return;
672e13c2760SPrzemyslaw Czarnowski                 }
673e13c2760SPrzemyslaw Czarnowski             },
674e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
675e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
676e13c2760SPrzemyslaw Czarnowski     }
677e13c2760SPrzemyslaw Czarnowski     else // proxy
678e13c2760SPrzemyslaw Czarnowski     {
679e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6805e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
681e13c2760SPrzemyslaw Czarnowski                 if (ec)
682e13c2760SPrzemyslaw Czarnowski                 {
68362598e31SEd Tanous                     BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
684e13c2760SPrzemyslaw Czarnowski 
685e13c2760SPrzemyslaw Czarnowski                     messages::internalError(asyncResp->res);
686e13c2760SPrzemyslaw Czarnowski                     return;
687e13c2760SPrzemyslaw Czarnowski                 }
688e13c2760SPrzemyslaw Czarnowski             },
689e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
690e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
691e13c2760SPrzemyslaw Czarnowski     }
692e13c2760SPrzemyslaw Czarnowski }
693e13c2760SPrzemyslaw Czarnowski 
69496825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
69596825bebSEd Tanous     crow::App& app, const crow::Request& req,
69622db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69796825bebSEd Tanous     const std::string& name, const std::string& resName)
69896825bebSEd Tanous {
6993ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
70045ca1b86SEd Tanous     {
70145ca1b86SEd Tanous         return;
70245ca1b86SEd Tanous     }
70379fdf63eSPrzemyslaw Czarnowski 
70479fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.InsertMedia";
70522db1728SEd Tanous     if (name != "bmc")
706107077deSPrzemyslaw Czarnowski     {
70779fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
708107077deSPrzemyslaw Czarnowski 
709107077deSPrzemyslaw Czarnowski         return;
710107077deSPrzemyslaw Czarnowski     }
71179fdf63eSPrzemyslaw Czarnowski     InsertMediaActionParams actionParams;
71298be3e39SEd Tanous 
713120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
714afc474aeSMyung Bae     if (!json_util::readJsonAction( //
715afc474aeSMyung Bae             req, asyncResp->res, //
716afc474aeSMyung Bae             "Image", actionParams.imageUrl, //
717afc474aeSMyung Bae             "Inserted", actionParams.inserted, //
718afc474aeSMyung Bae             "Password", actionParams.password, //
719afc474aeSMyung Bae             "TransferMethod", actionParams.transferMethod, //
720afc474aeSMyung Bae             "TransferProtocolType", actionParams.transferProtocolType, //
721afc474aeSMyung Bae             "UserName", actionParams.userName, //
722afc474aeSMyung Bae             "WriteProtected", actionParams.writeProtected //
723afc474aeSMyung Bae             ))
72498be3e39SEd Tanous     {
72598be3e39SEd Tanous         return;
72698be3e39SEd Tanous     }
727107077deSPrzemyslaw Czarnowski 
7282b73119cSGeorge Liu     dbus::utility::getDbusObject(
7292b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
73079fdf63eSPrzemyslaw Czarnowski         [asyncResp, action, actionParams,
7312b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
732002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
73322db1728SEd Tanous             if (ec)
73422db1728SEd Tanous             {
73562598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
73679fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
737107077deSPrzemyslaw Czarnowski 
73822db1728SEd Tanous                 return;
73922db1728SEd Tanous             }
74079fdf63eSPrzemyslaw Czarnowski 
74122db1728SEd Tanous             std::string service = getObjectType.begin()->first;
74262598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
74322db1728SEd Tanous 
7445eb468daSGeorge Liu             sdbusplus::message::object_path path(
7455eb468daSGeorge Liu                 "/xyz/openbmc_project/VirtualMedia");
7465eb468daSGeorge Liu             dbus::utility::getManagedObjects(
7475eb468daSGeorge Liu                 service, path,
7485eb468daSGeorge Liu                 [service, resName, action, actionParams, asyncResp](
7495eb468daSGeorge Liu                     const boost::system::error_code& ec2,
7505eb468daSGeorge Liu                     const dbus::utility::ManagedObjectType& subtree) mutable {
7518a592810SEd Tanous                     if (ec2)
75222db1728SEd Tanous                     {
75379fdf63eSPrzemyslaw Czarnowski                         // Not possible in proxy mode
75462598e31SEd Tanous                         BMCWEB_LOG_DEBUG("InsertMedia not "
75562598e31SEd Tanous                                          "allowed in proxy mode");
756bd79bce8SPatrick Williams                         messages::resourceNotFound(asyncResp->res, action,
757bd79bce8SPatrick Williams                                                    resName);
75822db1728SEd Tanous 
75922db1728SEd Tanous                         return;
76022db1728SEd Tanous                     }
76122db1728SEd Tanous                     for (const auto& object : subtree)
76222db1728SEd Tanous                     {
763bd79bce8SPatrick Williams                         VmMode mode =
764bd79bce8SPatrick Williams                             parseObjectPathAndGetMode(object.first, resName);
7655880f0c5SBoleslaw Ogonczyk Makowski                         if (mode == VmMode::Legacy)
76622db1728SEd Tanous                         {
767bd79bce8SPatrick Williams                             validateParams(asyncResp, service, resName,
768bd79bce8SPatrick Williams                                            actionParams);
76922db1728SEd Tanous 
77022db1728SEd Tanous                             return;
77122db1728SEd Tanous                         }
77222db1728SEd Tanous                     }
77362598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Parent item not found");
774bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "VirtualMedia",
775bd79bce8SPatrick Williams                                                resName);
7765eb468daSGeorge Liu                 });
7772b73119cSGeorge Liu         });
77896825bebSEd Tanous }
77922db1728SEd Tanous 
78096825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
78196825bebSEd Tanous     crow::App& app, const crow::Request& req,
78222db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
78396825bebSEd Tanous     const std::string& managerName, const std::string& resName)
78496825bebSEd Tanous {
7853ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
78645ca1b86SEd Tanous     {
78745ca1b86SEd Tanous         return;
78845ca1b86SEd Tanous     }
78979fdf63eSPrzemyslaw Czarnowski 
79079fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.EjectMedia";
79196825bebSEd Tanous     if (managerName != "bmc")
792107077deSPrzemyslaw Czarnowski     {
79379fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
79422db1728SEd Tanous 
79522db1728SEd Tanous         return;
79622db1728SEd Tanous     }
79722db1728SEd Tanous 
7982b73119cSGeorge Liu     dbus::utility::getDbusObject(
7992b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
80079fdf63eSPrzemyslaw Czarnowski         [asyncResp, action,
8012b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
802b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
8038a592810SEd Tanous             if (ec2)
80422db1728SEd Tanous             {
805bd79bce8SPatrick Williams                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}",
806bd79bce8SPatrick Williams                                  ec2);
80722db1728SEd Tanous                 messages::internalError(asyncResp->res);
80822db1728SEd Tanous 
80922db1728SEd Tanous                 return;
81022db1728SEd Tanous             }
81122db1728SEd Tanous             std::string service = getObjectType.begin()->first;
81262598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
81322db1728SEd Tanous 
8145eb468daSGeorge Liu             sdbusplus::message::object_path path(
8155eb468daSGeorge Liu                 "/xyz/openbmc_project/VirtualMedia");
8165eb468daSGeorge Liu             dbus::utility::getManagedObjects(
8175eb468daSGeorge Liu                 service, path,
81879fdf63eSPrzemyslaw Czarnowski                 [resName, service, action,
81979fdf63eSPrzemyslaw Czarnowski                  asyncResp](const boost::system::error_code& ec,
82002cad96eSEd Tanous                             const dbus::utility::ManagedObjectType& subtree) {
82122db1728SEd Tanous                     if (ec)
82222db1728SEd Tanous                     {
82362598e31SEd Tanous                         BMCWEB_LOG_ERROR("ObjectMapper : No Service found");
824bd79bce8SPatrick Williams                         messages::resourceNotFound(asyncResp->res, action,
825bd79bce8SPatrick Williams                                                    resName);
82622db1728SEd Tanous                         return;
82722db1728SEd Tanous                     }
82822db1728SEd Tanous 
82922db1728SEd Tanous                     for (const auto& object : subtree)
83022db1728SEd Tanous                     {
831bd79bce8SPatrick Williams                         VmMode mode =
832bd79bce8SPatrick Williams                             parseObjectPathAndGetMode(object.first, resName);
833365a73f4SEd Tanous                         if (mode != VmMode::Invalid)
83422db1728SEd Tanous                         {
835365a73f4SEd Tanous                             doEjectAction(asyncResp, service, resName,
836365a73f4SEd Tanous                                           mode == VmMode::Legacy);
8375880f0c5SBoleslaw Ogonczyk Makowski                             return;
83822db1728SEd Tanous                         }
83922db1728SEd Tanous                     }
84062598e31SEd Tanous                     BMCWEB_LOG_DEBUG("Parent item not found");
841bd79bce8SPatrick Williams                     messages::resourceNotFound(asyncResp->res, "VirtualMedia",
842bd79bce8SPatrick Williams                                                resName);
8435eb468daSGeorge Liu                 });
8442b73119cSGeorge Liu         });
84596825bebSEd Tanous }
84696825bebSEd Tanous 
84796825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
84896825bebSEd Tanous     crow::App& app, const crow::Request& req,
84922db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
85096825bebSEd Tanous     const std::string& name)
85196825bebSEd Tanous {
8523ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
85345ca1b86SEd Tanous     {
85445ca1b86SEd Tanous         return;
85545ca1b86SEd Tanous     }
85622db1728SEd Tanous     if (name != "bmc")
85722db1728SEd Tanous     {
858002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
859107077deSPrzemyslaw Czarnowski 
860107077deSPrzemyslaw Czarnowski         return;
861107077deSPrzemyslaw Czarnowski     }
862107077deSPrzemyslaw Czarnowski 
8638d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
864107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
8658d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
866ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
867ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name);
868107077deSPrzemyslaw Czarnowski 
8692b73119cSGeorge Liu     dbus::utility::getDbusObject(
8702b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
8712b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
872b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
873107077deSPrzemyslaw Czarnowski             if (ec)
874107077deSPrzemyslaw Czarnowski             {
87562598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
876107077deSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
877107077deSPrzemyslaw Czarnowski 
878107077deSPrzemyslaw Czarnowski                 return;
879107077deSPrzemyslaw Czarnowski             }
880107077deSPrzemyslaw Czarnowski             std::string service = getObjectType.begin()->first;
88162598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
882107077deSPrzemyslaw Czarnowski 
883107077deSPrzemyslaw Czarnowski             getVmResourceList(asyncResp, service, name);
8842b73119cSGeorge Liu         });
88596825bebSEd Tanous }
886107077deSPrzemyslaw Czarnowski 
88796825bebSEd Tanous inline void
88896825bebSEd Tanous     handleVirtualMediaGet(crow::App& app, const crow::Request& req,
88922db1728SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
89096825bebSEd Tanous                           const std::string& name, const std::string& resName)
89196825bebSEd Tanous {
8923ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
89345ca1b86SEd Tanous     {
89445ca1b86SEd Tanous         return;
89545ca1b86SEd Tanous     }
896107077deSPrzemyslaw Czarnowski     if (name != "bmc")
897107077deSPrzemyslaw Czarnowski     {
898002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
899107077deSPrzemyslaw Czarnowski 
900107077deSPrzemyslaw Czarnowski         return;
901107077deSPrzemyslaw Czarnowski     }
902107077deSPrzemyslaw Czarnowski 
9032b73119cSGeorge Liu     dbus::utility::getDbusObject(
9042b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
905002d39b4SEd Tanous         [asyncResp, name,
9062b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
907b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
908107077deSPrzemyslaw Czarnowski             if (ec)
909107077deSPrzemyslaw Czarnowski             {
91062598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
911107077deSPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
912107077deSPrzemyslaw Czarnowski 
913107077deSPrzemyslaw Czarnowski                 return;
914107077deSPrzemyslaw Czarnowski             }
915107077deSPrzemyslaw Czarnowski             std::string service = getObjectType.begin()->first;
91662598e31SEd Tanous             BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
917107077deSPrzemyslaw Czarnowski 
918107077deSPrzemyslaw Czarnowski             getVmData(asyncResp, service, name, resName);
9192b73119cSGeorge Liu         });
92096825bebSEd Tanous }
92196825bebSEd Tanous 
92296825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
92396825bebSEd Tanous {
92496825bebSEd Tanous     BMCWEB_ROUTE(
92596825bebSEd Tanous         app,
92696825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
92796825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
92896825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
92996825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
93096825bebSEd Tanous 
93196825bebSEd Tanous     BMCWEB_ROUTE(
93296825bebSEd Tanous         app,
93396825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
93496825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
93596825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
93696825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
93796825bebSEd Tanous 
93896825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
93996825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
94096825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
94196825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
94296825bebSEd Tanous 
94396825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
94496825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
94596825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
94696825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
947107077deSPrzemyslaw Czarnowski }
948107077deSPrzemyslaw Czarnowski 
949107077deSPrzemyslaw Czarnowski } // namespace redfish
950