xref: /openbmc/bmcweb/features/redfish/lib/virtual_media.hpp (revision 8cb2c024c4625e2fe2f0b107a865faffcd4bb770)
1107077deSPrzemyslaw Czarnowski /*
2107077deSPrzemyslaw Czarnowski // Copyright (c) 2018 Intel Corporation
3107077deSPrzemyslaw Czarnowski //
4107077deSPrzemyslaw Czarnowski // Licensed under the Apache License, Version 2.0 (the "License");
5107077deSPrzemyslaw Czarnowski // you may not use this file except in compliance with the License.
6107077deSPrzemyslaw Czarnowski // You may obtain a copy of the License at
7107077deSPrzemyslaw Czarnowski //
8107077deSPrzemyslaw Czarnowski //      http://www.apache.org/licenses/LICENSE-2.0
9107077deSPrzemyslaw Czarnowski //
10107077deSPrzemyslaw Czarnowski // Unless required by applicable law or agreed to in writing, software
11107077deSPrzemyslaw Czarnowski // distributed under the License is distributed on an "AS IS" BASIS,
12107077deSPrzemyslaw Czarnowski // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13107077deSPrzemyslaw Czarnowski // See the License for the specific language governing permissions and
14107077deSPrzemyslaw Czarnowski // limitations under the License.
15107077deSPrzemyslaw Czarnowski */
16107077deSPrzemyslaw Czarnowski #pragma once
17107077deSPrzemyslaw Czarnowski 
183ccb3adbSEd Tanous #include "account_service.hpp"
193ccb3adbSEd Tanous #include "app.hpp"
2079fdf63eSPrzemyslaw Czarnowski #include "async_resp.hpp"
2111e8f60dSEd Tanous #include "credential_pipe.hpp"
222b73119cSGeorge Liu #include "dbus_utility.hpp"
23739b87efSEd Tanous #include "generated/enums/virtual_media.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
263ccb3adbSEd Tanous #include "utils/json_utils.hpp"
273ccb3adbSEd Tanous 
28988fb7b2SAdrian Ambrożewicz #include <boost/process/async_pipe.hpp>
29ef4c65b7SEd Tanous #include <boost/url/format.hpp>
309e319cf0SAnna Platash #include <boost/url/url_view.hpp>
31107077deSPrzemyslaw Czarnowski 
322b73119cSGeorge Liu #include <array>
333544d2a7SEd Tanous #include <ranges>
342b73119cSGeorge Liu #include <string_view>
352b73119cSGeorge Liu 
36107077deSPrzemyslaw Czarnowski namespace redfish
37107077deSPrzemyslaw Czarnowski {
38365a73f4SEd Tanous 
39365a73f4SEd Tanous enum class VmMode
40365a73f4SEd Tanous {
41365a73f4SEd Tanous     Invalid,
42365a73f4SEd Tanous     Legacy,
43365a73f4SEd Tanous     Proxy
44365a73f4SEd Tanous };
45365a73f4SEd Tanous 
46365a73f4SEd Tanous inline VmMode
47365a73f4SEd Tanous     parseObjectPathAndGetMode(const sdbusplus::message::object_path& itemPath,
48365a73f4SEd Tanous                               const std::string& resName)
49365a73f4SEd Tanous {
50365a73f4SEd Tanous     std::string thisPath = itemPath.filename();
5162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Filename: {}, ThisPath: {}", itemPath.str, thisPath);
52365a73f4SEd Tanous 
53365a73f4SEd Tanous     if (thisPath.empty())
54365a73f4SEd Tanous     {
55365a73f4SEd Tanous         return VmMode::Invalid;
56365a73f4SEd Tanous     }
57365a73f4SEd Tanous 
58365a73f4SEd Tanous     if (thisPath != resName)
59365a73f4SEd Tanous     {
60365a73f4SEd Tanous         return VmMode::Invalid;
61365a73f4SEd Tanous     }
62365a73f4SEd Tanous 
63365a73f4SEd Tanous     auto mode = itemPath.parent_path();
64365a73f4SEd Tanous     auto type = mode.parent_path();
65365a73f4SEd Tanous 
66365a73f4SEd Tanous     if (mode.filename().empty() || type.filename().empty())
67365a73f4SEd Tanous     {
68365a73f4SEd Tanous         return VmMode::Invalid;
69365a73f4SEd Tanous     }
70365a73f4SEd Tanous 
71365a73f4SEd Tanous     if (type.filename() != "VirtualMedia")
72365a73f4SEd Tanous     {
73365a73f4SEd Tanous         return VmMode::Invalid;
74365a73f4SEd Tanous     }
75365a73f4SEd Tanous     std::string modeStr = mode.filename();
76365a73f4SEd Tanous     if (modeStr == "Legacy")
77365a73f4SEd Tanous     {
78365a73f4SEd Tanous         return VmMode::Legacy;
79365a73f4SEd Tanous     }
80365a73f4SEd Tanous     if (modeStr == "Proxy")
81365a73f4SEd Tanous     {
82365a73f4SEd Tanous         return VmMode::Proxy;
83365a73f4SEd Tanous     }
84365a73f4SEd Tanous     return VmMode::Invalid;
85365a73f4SEd Tanous }
86365a73f4SEd Tanous 
8779fdf63eSPrzemyslaw Czarnowski using CheckItemHandler =
8879fdf63eSPrzemyslaw Czarnowski     std::function<void(const std::string& service, const std::string& resName,
8979fdf63eSPrzemyslaw Czarnowski                        const std::shared_ptr<bmcweb::AsyncResp>&,
9070cbdf53SGeorge Liu                        const std::pair<sdbusplus::message::object_path,
9180f79a40SMichael Shen                                        dbus::utility::DBusInterfacesMap>&)>;
9279fdf63eSPrzemyslaw Czarnowski 
93ac106bf6SEd Tanous inline void
94ac106bf6SEd Tanous     findAndParseObject(const std::string& service, const std::string& resName,
95ac106bf6SEd Tanous                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
9679fdf63eSPrzemyslaw Czarnowski                        CheckItemHandler&& handler)
9779fdf63eSPrzemyslaw Czarnowski {
985eb468daSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/VirtualMedia");
995eb468daSGeorge Liu     dbus::utility::getManagedObjects(
1005eb468daSGeorge Liu         service, path,
101*8cb2c024SEd Tanous         [service, resName, asyncResp, handler = std::move(handler)](
102*8cb2c024SEd Tanous             const boost::system::error_code& ec,
10370cbdf53SGeorge Liu             const dbus::utility::ManagedObjectType& subtree) {
10479fdf63eSPrzemyslaw Czarnowski         if (ec)
10579fdf63eSPrzemyslaw Czarnowski         {
10662598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error");
10779fdf63eSPrzemyslaw Czarnowski 
10879fdf63eSPrzemyslaw Czarnowski             return;
10979fdf63eSPrzemyslaw Czarnowski         }
11079fdf63eSPrzemyslaw Czarnowski 
11170cbdf53SGeorge Liu         for (const auto& item : subtree)
11279fdf63eSPrzemyslaw Czarnowski         {
11379fdf63eSPrzemyslaw Czarnowski             VmMode mode = parseObjectPathAndGetMode(item.first, resName);
11479fdf63eSPrzemyslaw Czarnowski             if (mode != VmMode::Invalid)
11579fdf63eSPrzemyslaw Czarnowski             {
116ac106bf6SEd Tanous                 handler(service, resName, asyncResp, item);
11779fdf63eSPrzemyslaw Czarnowski                 return;
11879fdf63eSPrzemyslaw Czarnowski             }
11979fdf63eSPrzemyslaw Czarnowski         }
12079fdf63eSPrzemyslaw Czarnowski 
12162598e31SEd Tanous         BMCWEB_LOG_DEBUG("Parent item not found");
122ac106bf6SEd Tanous         asyncResp->res.result(boost::beast::http::status::not_found);
1235eb468daSGeorge Liu     });
12479fdf63eSPrzemyslaw Czarnowski }
12579fdf63eSPrzemyslaw Czarnowski 
1269e319cf0SAnna Platash /**
1279e319cf0SAnna Platash  * @brief Function extracts transfer protocol name from URI.
1289e319cf0SAnna Platash  */
12967df073bSEd Tanous inline std::string getTransferProtocolTypeFromUri(const std::string& imageUri)
13067df073bSEd Tanous {
1316fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
132079360aeSEd Tanous         boost::urls::parse_uri(imageUri);
13367df073bSEd Tanous     if (!url)
13467df073bSEd Tanous     {
13567df073bSEd Tanous         return "None";
13667df073bSEd Tanous     }
137079360aeSEd Tanous     std::string_view scheme = url->scheme();
13867df073bSEd Tanous     if (scheme == "smb")
13967df073bSEd Tanous     {
14067df073bSEd Tanous         return "CIFS";
14167df073bSEd Tanous     }
14267df073bSEd Tanous     if (scheme == "https")
14367df073bSEd Tanous     {
14467df073bSEd Tanous         return "HTTPS";
14567df073bSEd Tanous     }
14667df073bSEd Tanous 
14767df073bSEd Tanous     return "None";
14867df073bSEd Tanous }
149107077deSPrzemyslaw Czarnowski 
150107077deSPrzemyslaw Czarnowski /**
151107077deSPrzemyslaw Czarnowski  * @brief Read all known properties from VM object interfaces
152107077deSPrzemyslaw Czarnowski  */
15322db1728SEd Tanous inline void
15480f79a40SMichael Shen     vmParseInterfaceObject(const dbus::utility::DBusInterfacesMap& interfaces,
155ac106bf6SEd Tanous                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
156107077deSPrzemyslaw Czarnowski {
1578a592810SEd Tanous     for (const auto& [interface, values] : interfaces)
158107077deSPrzemyslaw Czarnowski     {
159711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.MountPoint")
160107077deSPrzemyslaw Czarnowski         {
161711ac7a9SEd Tanous             for (const auto& [property, value] : values)
162107077deSPrzemyslaw Czarnowski             {
163711ac7a9SEd Tanous                 if (property == "EndpointId")
164107077deSPrzemyslaw Czarnowski                 {
165107077deSPrzemyslaw Czarnowski                     const std::string* endpointIdValue =
166711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
167711ac7a9SEd Tanous                     if (endpointIdValue == nullptr)
168107077deSPrzemyslaw Czarnowski                     {
169711ac7a9SEd Tanous                         continue;
170711ac7a9SEd Tanous                     }
171107077deSPrzemyslaw Czarnowski                     if (!endpointIdValue->empty())
172107077deSPrzemyslaw Czarnowski                     {
173107077deSPrzemyslaw Czarnowski                         // Proxy mode
174ac106bf6SEd Tanous                         asyncResp->res
175711ac7a9SEd Tanous                             .jsonValue["Oem"]["OpenBMC"]["WebSocketEndpoint"] =
176d04ba325SPrzemyslaw Czarnowski                             *endpointIdValue;
177ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
178ac106bf6SEd Tanous                             "OEM";
179107077deSPrzemyslaw Czarnowski                     }
180107077deSPrzemyslaw Czarnowski                 }
181711ac7a9SEd Tanous                 if (property == "ImageURL")
182107077deSPrzemyslaw Czarnowski                 {
183107077deSPrzemyslaw Czarnowski                     const std::string* imageUrlValue =
184711ac7a9SEd Tanous                         std::get_if<std::string>(&value);
18526f6976fSEd Tanous                     if (imageUrlValue != nullptr && !imageUrlValue->empty())
186107077deSPrzemyslaw Czarnowski                     {
187da4784d8SPrzemyslaw Czarnowski                         std::filesystem::path filePath = *imageUrlValue;
188da4784d8SPrzemyslaw Czarnowski                         if (!filePath.has_filename())
189da4784d8SPrzemyslaw Czarnowski                         {
1909e319cf0SAnna Platash                             // this will handle https share, which not
1919e319cf0SAnna Platash                             // necessarily has to have filename given.
192ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] = "";
193da4784d8SPrzemyslaw Czarnowski                         }
194da4784d8SPrzemyslaw Czarnowski                         else
195da4784d8SPrzemyslaw Czarnowski                         {
196ac106bf6SEd Tanous                             asyncResp->res.jsonValue["ImageName"] =
1979e319cf0SAnna Platash                                 filePath.filename();
198da4784d8SPrzemyslaw Czarnowski                         }
199da4784d8SPrzemyslaw Czarnowski 
200ac106bf6SEd Tanous                         asyncResp->res.jsonValue["Image"] = *imageUrlValue;
201ac106bf6SEd Tanous                         asyncResp->res.jsonValue["TransferProtocolType"] =
2029e319cf0SAnna Platash                             getTransferProtocolTypeFromUri(*imageUrlValue);
2039e319cf0SAnna Platash 
204ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
205739b87efSEd Tanous                             virtual_media::ConnectedVia::URI;
206107077deSPrzemyslaw Czarnowski                     }
207107077deSPrzemyslaw Czarnowski                 }
208711ac7a9SEd Tanous                 if (property == "WriteProtected")
2099e319cf0SAnna Platash                 {
210711ac7a9SEd Tanous                     const bool* writeProtectedValue = std::get_if<bool>(&value);
211e662eae8SEd Tanous                     if (writeProtectedValue != nullptr)
2129e319cf0SAnna Platash                     {
213ac106bf6SEd Tanous                         asyncResp->res.jsonValue["WriteProtected"] =
2149e319cf0SAnna Platash                             *writeProtectedValue;
2159e319cf0SAnna Platash                     }
2169e319cf0SAnna Platash                 }
2179e319cf0SAnna Platash             }
218107077deSPrzemyslaw Czarnowski         }
219711ac7a9SEd Tanous         if (interface == "xyz.openbmc_project.VirtualMedia.Process")
220711ac7a9SEd Tanous         {
221711ac7a9SEd Tanous             for (const auto& [property, value] : values)
222711ac7a9SEd Tanous             {
223711ac7a9SEd Tanous                 if (property == "Active")
224711ac7a9SEd Tanous                 {
225711ac7a9SEd Tanous                     const bool* activeValue = std::get_if<bool>(&value);
226e662eae8SEd Tanous                     if (activeValue == nullptr)
227711ac7a9SEd Tanous                     {
22862598e31SEd Tanous                         BMCWEB_LOG_DEBUG("Value Active not found");
229711ac7a9SEd Tanous                         return;
230711ac7a9SEd Tanous                     }
231ac106bf6SEd Tanous                     asyncResp->res.jsonValue["Inserted"] = *activeValue;
232711ac7a9SEd Tanous 
233e05aec50SEd Tanous                     if (*activeValue)
234711ac7a9SEd Tanous                     {
235ac106bf6SEd Tanous                         asyncResp->res.jsonValue["ConnectedVia"] =
236739b87efSEd Tanous                             virtual_media::ConnectedVia::Applet;
237711ac7a9SEd Tanous                     }
238711ac7a9SEd Tanous                 }
239711ac7a9SEd Tanous             }
240711ac7a9SEd Tanous         }
241107077deSPrzemyslaw Czarnowski     }
242107077deSPrzemyslaw Czarnowski }
243107077deSPrzemyslaw Czarnowski 
244107077deSPrzemyslaw Czarnowski /**
245107077deSPrzemyslaw Czarnowski  * @brief Fill template for Virtual Media Item.
246107077deSPrzemyslaw Czarnowski  */
24722db1728SEd Tanous inline nlohmann::json vmItemTemplate(const std::string& name,
248107077deSPrzemyslaw Czarnowski                                      const std::string& resName)
249107077deSPrzemyslaw Czarnowski {
250107077deSPrzemyslaw Czarnowski     nlohmann::json item;
251ef4c65b7SEd Tanous     item["@odata.id"] = boost::urls::format(
252ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}", name, resName);
25322db1728SEd Tanous 
254d04ba325SPrzemyslaw Czarnowski     item["@odata.type"] = "#VirtualMedia.v1_3_0.VirtualMedia";
255107077deSPrzemyslaw Czarnowski     item["Name"] = "Virtual Removable Media";
256107077deSPrzemyslaw Czarnowski     item["Id"] = resName;
257107077deSPrzemyslaw Czarnowski     item["WriteProtected"] = true;
258739b87efSEd Tanous     item["ConnectedVia"] = virtual_media::ConnectedVia::NotConnected;
259613dabeaSEd Tanous     item["MediaTypes"] = nlohmann::json::array_t({"CD", "USBStick"});
260107077deSPrzemyslaw Czarnowski     item["TransferMethod"] = "Stream";
261d04ba325SPrzemyslaw Czarnowski     item["Oem"]["OpenBMC"]["@odata.type"] =
262d04ba325SPrzemyslaw Czarnowski         "#OemVirtualMedia.v1_0_0.VirtualMedia";
26315b89725SV-Sanjana     item["Oem"]["OpenBMC"]["@odata.id"] = boost::urls::format(
26415b89725SV-Sanjana         "/redfish/v1/Managers/{}/VirtualMedia/{}#/Oem/OpenBMC", name, resName);
265107077deSPrzemyslaw Czarnowski 
266107077deSPrzemyslaw Czarnowski     return item;
267107077deSPrzemyslaw Czarnowski }
268107077deSPrzemyslaw Czarnowski 
269107077deSPrzemyslaw Czarnowski /**
270107077deSPrzemyslaw Czarnowski  *  @brief Fills collection data
271107077deSPrzemyslaw Czarnowski  */
272ac106bf6SEd Tanous inline void getVmResourceList(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
273107077deSPrzemyslaw Czarnowski                               const std::string& service,
274107077deSPrzemyslaw Czarnowski                               const std::string& name)
275107077deSPrzemyslaw Czarnowski {
27662598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get available Virtual Media resources.");
2775eb468daSGeorge Liu     sdbusplus::message::object_path objPath(
2785eb468daSGeorge Liu         "/xyz/openbmc_project/VirtualMedia");
2795eb468daSGeorge Liu     dbus::utility::getManagedObjects(
2805eb468daSGeorge Liu         service, objPath,
281ac106bf6SEd Tanous         [name, asyncResp{std::move(asyncResp)}](
2825e7e2dc5SEd Tanous             const boost::system::error_code& ec,
28302cad96eSEd Tanous             const dbus::utility::ManagedObjectType& subtree) {
284107077deSPrzemyslaw Czarnowski         if (ec)
285107077deSPrzemyslaw Czarnowski         {
28662598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error");
287107077deSPrzemyslaw Czarnowski             return;
288107077deSPrzemyslaw Czarnowski         }
289ac106bf6SEd Tanous         nlohmann::json& members = asyncResp->res.jsonValue["Members"];
290107077deSPrzemyslaw Czarnowski         members = nlohmann::json::array();
291107077deSPrzemyslaw Czarnowski 
292107077deSPrzemyslaw Czarnowski         for (const auto& object : subtree)
293107077deSPrzemyslaw Czarnowski         {
294107077deSPrzemyslaw Czarnowski             nlohmann::json item;
2952dfd18efSEd Tanous             std::string path = object.first.filename();
2962dfd18efSEd Tanous             if (path.empty())
297107077deSPrzemyslaw Czarnowski             {
298107077deSPrzemyslaw Czarnowski                 continue;
299107077deSPrzemyslaw Czarnowski             }
300107077deSPrzemyslaw Czarnowski 
301ef4c65b7SEd Tanous             item["@odata.id"] = boost::urls::format(
302ef4c65b7SEd Tanous                 "/redfish/v1/Managers/{}/VirtualMedia/{}", name, path);
303107077deSPrzemyslaw Czarnowski             members.emplace_back(std::move(item));
304107077deSPrzemyslaw Czarnowski         }
305ac106bf6SEd Tanous         asyncResp->res.jsonValue["Members@odata.count"] = members.size();
3065eb468daSGeorge Liu     });
307107077deSPrzemyslaw Czarnowski }
308107077deSPrzemyslaw Czarnowski 
30970cbdf53SGeorge Liu inline void
31070cbdf53SGeorge Liu     afterGetVmData(const std::string& name, const std::string& /*service*/,
31179fdf63eSPrzemyslaw Czarnowski                    const std::string& resName,
31279fdf63eSPrzemyslaw Czarnowski                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
31370cbdf53SGeorge Liu                    const std::pair<sdbusplus::message::object_path,
31480f79a40SMichael Shen                                    dbus::utility::DBusInterfacesMap>& item)
31579fdf63eSPrzemyslaw Czarnowski {
31679fdf63eSPrzemyslaw Czarnowski     VmMode mode = parseObjectPathAndGetMode(item.first, resName);
31779fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Invalid)
31879fdf63eSPrzemyslaw Czarnowski     {
31979fdf63eSPrzemyslaw Czarnowski         return;
32079fdf63eSPrzemyslaw Czarnowski     }
32179fdf63eSPrzemyslaw Czarnowski 
32279fdf63eSPrzemyslaw Czarnowski     asyncResp->res.jsonValue = vmItemTemplate(name, resName);
32379fdf63eSPrzemyslaw Czarnowski 
32479fdf63eSPrzemyslaw Czarnowski     // Check if dbus path is Legacy type
32579fdf63eSPrzemyslaw Czarnowski     if (mode == VmMode::Legacy)
32679fdf63eSPrzemyslaw Czarnowski     {
327ef4c65b7SEd Tanous         asyncResp->res.jsonValue["Actions"]["#VirtualMedia.InsertMedia"]
328ef4c65b7SEd Tanous                                 ["target"] = boost::urls::format(
329ef4c65b7SEd Tanous             "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.InsertMedia",
330ef4c65b7SEd Tanous             name, resName);
33179fdf63eSPrzemyslaw Czarnowski     }
33279fdf63eSPrzemyslaw Czarnowski 
33379fdf63eSPrzemyslaw Czarnowski     vmParseInterfaceObject(item.second, asyncResp);
33479fdf63eSPrzemyslaw Czarnowski 
335ef4c65b7SEd Tanous     asyncResp->res.jsonValue["Actions"]["#VirtualMedia.EjectMedia"]
336ef4c65b7SEd Tanous                             ["target"] = boost::urls::format(
337ef4c65b7SEd Tanous         "/redfish/v1/Managers/{}/VirtualMedia/{}/Actions/VirtualMedia.EjectMedia",
338ef4c65b7SEd Tanous         name, resName);
33979fdf63eSPrzemyslaw Czarnowski }
34079fdf63eSPrzemyslaw Czarnowski 
341107077deSPrzemyslaw Czarnowski /**
342107077deSPrzemyslaw Czarnowski  *  @brief Fills data for specific resource
343107077deSPrzemyslaw Czarnowski  */
344ac106bf6SEd Tanous inline void getVmData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
345107077deSPrzemyslaw Czarnowski                       const std::string& service, const std::string& name,
346107077deSPrzemyslaw Czarnowski                       const std::string& resName)
347107077deSPrzemyslaw Czarnowski {
34862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get Virtual Media resource data.");
349107077deSPrzemyslaw Czarnowski 
350ac106bf6SEd Tanous     findAndParseObject(service, resName, asyncResp,
35170cbdf53SGeorge Liu                        std::bind_front(afterGetVmData, name));
352107077deSPrzemyslaw Czarnowski }
353107077deSPrzemyslaw Czarnowski 
354e13c2760SPrzemyslaw Czarnowski /**
355c6f4e017SAgata Olender  * @brief Transfer protocols supported for InsertMedia action.
356c6f4e017SAgata Olender  *
357c6f4e017SAgata Olender  */
358c6f4e017SAgata Olender enum class TransferProtocol
359c6f4e017SAgata Olender {
360c6f4e017SAgata Olender     https,
361c6f4e017SAgata Olender     smb,
362c6f4e017SAgata Olender     invalid
363c6f4e017SAgata Olender };
364c6f4e017SAgata Olender 
365c6f4e017SAgata Olender /**
366c6f4e017SAgata Olender  * @brief Function extracts transfer protocol type from URI.
367c6f4e017SAgata Olender  *
368c6f4e017SAgata Olender  */
36967df073bSEd Tanous inline std::optional<TransferProtocol>
370d9f466b3SEd Tanous     getTransferProtocolFromUri(boost::urls::url_view imageUri)
37167df073bSEd Tanous {
372079360aeSEd Tanous     std::string_view scheme = imageUri.scheme();
37367df073bSEd Tanous     if (scheme == "smb")
37467df073bSEd Tanous     {
37567df073bSEd Tanous         return TransferProtocol::smb;
37667df073bSEd Tanous     }
37767df073bSEd Tanous     if (scheme == "https")
37867df073bSEd Tanous     {
37967df073bSEd Tanous         return TransferProtocol::https;
38067df073bSEd Tanous     }
38167df073bSEd Tanous     if (!scheme.empty())
38267df073bSEd Tanous     {
38367df073bSEd Tanous         return TransferProtocol::invalid;
38467df073bSEd Tanous     }
38567df073bSEd Tanous 
38667df073bSEd Tanous     return {};
38767df073bSEd Tanous }
388c6f4e017SAgata Olender 
389c6f4e017SAgata Olender /**
390c6f4e017SAgata Olender  * @brief Function convert transfer protocol from string param.
391c6f4e017SAgata Olender  *
392c6f4e017SAgata Olender  */
39322db1728SEd Tanous inline std::optional<TransferProtocol> getTransferProtocolFromParam(
394c6f4e017SAgata Olender     const std::optional<std::string>& transferProtocolType)
395c6f4e017SAgata Olender {
396e01d0c36SEd Tanous     if (!transferProtocolType)
397c6f4e017SAgata Olender     {
398c6f4e017SAgata Olender         return {};
399c6f4e017SAgata Olender     }
400c6f4e017SAgata Olender 
401c6f4e017SAgata Olender     if (*transferProtocolType == "CIFS")
402c6f4e017SAgata Olender     {
403c6f4e017SAgata Olender         return TransferProtocol::smb;
404c6f4e017SAgata Olender     }
405c6f4e017SAgata Olender 
406c6f4e017SAgata Olender     if (*transferProtocolType == "HTTPS")
407c6f4e017SAgata Olender     {
408c6f4e017SAgata Olender         return TransferProtocol::https;
409c6f4e017SAgata Olender     }
410c6f4e017SAgata Olender 
411c6f4e017SAgata Olender     return TransferProtocol::invalid;
412c6f4e017SAgata Olender }
413c6f4e017SAgata Olender 
414c6f4e017SAgata Olender /**
415c6f4e017SAgata Olender  * @brief Function extends URI with transfer protocol type.
416c6f4e017SAgata Olender  *
417c6f4e017SAgata Olender  */
41822db1728SEd Tanous inline std::string
419c6f4e017SAgata Olender     getUriWithTransferProtocol(const std::string& imageUri,
420c6f4e017SAgata Olender                                const TransferProtocol& transferProtocol)
421c6f4e017SAgata Olender {
422c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::smb)
423c6f4e017SAgata Olender     {
424c6f4e017SAgata Olender         return "smb://" + imageUri;
425c6f4e017SAgata Olender     }
426c6f4e017SAgata Olender 
427c6f4e017SAgata Olender     if (transferProtocol == TransferProtocol::https)
428c6f4e017SAgata Olender     {
429c6f4e017SAgata Olender         return "https://" + imageUri;
430c6f4e017SAgata Olender     }
431c6f4e017SAgata Olender 
432c6f4e017SAgata Olender     return imageUri;
433c6f4e017SAgata Olender }
434c6f4e017SAgata Olender 
4351f2a40ceSPrzemyslaw Czarnowski struct InsertMediaActionParams
4361f2a40ceSPrzemyslaw Czarnowski {
437120fa86aSPrzemyslaw Czarnowski     std::optional<std::string> imageUrl;
4381f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> userName;
4391f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> password;
4401f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferMethod;
4411f2a40ceSPrzemyslaw Czarnowski     std::optional<std::string> transferProtocolType;
4421f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> writeProtected = true;
4431f2a40ceSPrzemyslaw Czarnowski     std::optional<bool> inserted;
4441f2a40ceSPrzemyslaw Czarnowski };
4451f2a40ceSPrzemyslaw Czarnowski 
446e13c2760SPrzemyslaw Czarnowski /**
447e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
448e13c2760SPrzemyslaw Czarnowski  *
449e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
450e13c2760SPrzemyslaw Czarnowski  */
45122db1728SEd Tanous inline void doMountVmLegacy(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
452e13c2760SPrzemyslaw Czarnowski                             const std::string& service, const std::string& name,
45311e8f60dSEd Tanous                             const std::string& imageUrl, bool rw,
454988fb7b2SAdrian Ambrożewicz                             std::string&& userName, std::string&& password)
455e13c2760SPrzemyslaw Czarnowski {
45611e8f60dSEd Tanous     int fd = -1;
45711e8f60dSEd Tanous     std::shared_ptr<CredentialsPipe> secretPipe;
458988fb7b2SAdrian Ambrożewicz     if (!userName.empty() || !password.empty())
459988fb7b2SAdrian Ambrożewicz     {
460988fb7b2SAdrian Ambrożewicz         // Payload must contain data + NULL delimiters
46111e8f60dSEd Tanous         constexpr const size_t secretLimit = 1024;
46211e8f60dSEd Tanous         if (userName.size() + password.size() + 2 > secretLimit)
463988fb7b2SAdrian Ambrożewicz         {
46462598e31SEd Tanous             BMCWEB_LOG_ERROR("Credentials too long to handle");
465988fb7b2SAdrian Ambrożewicz             messages::unrecognizedRequestBody(asyncResp->res);
466988fb7b2SAdrian Ambrożewicz             return;
467988fb7b2SAdrian Ambrożewicz         }
468988fb7b2SAdrian Ambrożewicz 
469988fb7b2SAdrian Ambrożewicz         // Open pipe
47011e8f60dSEd Tanous         secretPipe = std::make_shared<CredentialsPipe>(
47111e8f60dSEd Tanous             crow::connections::systemBus->get_io_context());
47211e8f60dSEd Tanous         fd = secretPipe->fd();
473988fb7b2SAdrian Ambrożewicz 
474988fb7b2SAdrian Ambrożewicz         // Pass secret over pipe
47581ce609eSEd Tanous         secretPipe->asyncWrite(
47611e8f60dSEd Tanous             std::move(userName), std::move(password),
47711e8f60dSEd Tanous             [asyncResp, secretPipe](const boost::system::error_code& ec,
47811e8f60dSEd Tanous                                     std::size_t) {
479988fb7b2SAdrian Ambrożewicz             if (ec)
480988fb7b2SAdrian Ambrożewicz             {
48162598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to pass secret: {}", ec);
482988fb7b2SAdrian Ambrożewicz                 messages::internalError(asyncResp->res);
483988fb7b2SAdrian Ambrożewicz             }
484988fb7b2SAdrian Ambrożewicz         });
485988fb7b2SAdrian Ambrożewicz     }
486988fb7b2SAdrian Ambrożewicz 
48711e8f60dSEd Tanous     dbus::utility::DbusVariantType unixFd(
48811e8f60dSEd Tanous         std::in_place_type<sdbusplus::message::unix_fd>, fd);
48911e8f60dSEd Tanous 
49011e8f60dSEd Tanous     sdbusplus::message::object_path path(
49111e8f60dSEd Tanous         "/xyz/openbmc_project/VirtualMedia/Legacy");
49211e8f60dSEd Tanous     path /= name;
493e13c2760SPrzemyslaw Czarnowski     crow::connections::systemBus->async_method_call(
4945e7e2dc5SEd Tanous         [asyncResp, secretPipe](const boost::system::error_code& ec,
495988fb7b2SAdrian Ambrożewicz                                 bool success) {
496e13c2760SPrzemyslaw Czarnowski         if (ec)
497e13c2760SPrzemyslaw Czarnowski         {
49862598e31SEd Tanous             BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
499e13c2760SPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
50011e8f60dSEd Tanous             return;
501d6da5bebSAdrian Ambrożewicz         }
50211e8f60dSEd Tanous         if (!success)
503d6da5bebSAdrian Ambrożewicz         {
50462598e31SEd Tanous             BMCWEB_LOG_ERROR("Service responded with error");
50511e8f60dSEd Tanous             messages::internalError(asyncResp->res);
506e13c2760SPrzemyslaw Czarnowski         }
507e13c2760SPrzemyslaw Czarnowski     },
50811e8f60dSEd Tanous         service, path.str, "xyz.openbmc_project.VirtualMedia.Legacy", "Mount",
50911e8f60dSEd Tanous         imageUrl, rw, unixFd);
510e13c2760SPrzemyslaw Czarnowski }
511e13c2760SPrzemyslaw Czarnowski 
512e13c2760SPrzemyslaw Czarnowski /**
513120fa86aSPrzemyslaw Czarnowski  * @brief Function validate parameters of insert media request.
514120fa86aSPrzemyslaw Czarnowski  *
515120fa86aSPrzemyslaw Czarnowski  */
516120fa86aSPrzemyslaw Czarnowski inline void validateParams(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
517120fa86aSPrzemyslaw Czarnowski                            const std::string& service,
518120fa86aSPrzemyslaw Czarnowski                            const std::string& resName,
519120fa86aSPrzemyslaw Czarnowski                            InsertMediaActionParams& actionParams)
520120fa86aSPrzemyslaw Czarnowski {
52162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Validation started");
522120fa86aSPrzemyslaw Czarnowski     // required param imageUrl must not be empty
523120fa86aSPrzemyslaw Czarnowski     if (!actionParams.imageUrl)
524120fa86aSPrzemyslaw Czarnowski     {
52562598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter Image is empty.");
526120fa86aSPrzemyslaw Czarnowski 
527120fa86aSPrzemyslaw Czarnowski         messages::propertyValueFormatError(asyncResp->res, "<empty>", "Image");
528120fa86aSPrzemyslaw Czarnowski 
529120fa86aSPrzemyslaw Czarnowski         return;
530120fa86aSPrzemyslaw Czarnowski     }
531120fa86aSPrzemyslaw Czarnowski 
532120fa86aSPrzemyslaw Czarnowski     // optional param inserted must be true
533e01d0c36SEd Tanous     if (actionParams.inserted && !*actionParams.inserted)
534120fa86aSPrzemyslaw Czarnowski     {
53562598e31SEd Tanous         BMCWEB_LOG_ERROR(
53662598e31SEd Tanous             "Request action optional parameter Inserted must be true.");
537120fa86aSPrzemyslaw Czarnowski 
538120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "Inserted",
539120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
540120fa86aSPrzemyslaw Czarnowski 
541120fa86aSPrzemyslaw Czarnowski         return;
542120fa86aSPrzemyslaw Czarnowski     }
543120fa86aSPrzemyslaw Czarnowski 
544120fa86aSPrzemyslaw Czarnowski     // optional param transferMethod must be stream
545e01d0c36SEd Tanous     if (actionParams.transferMethod &&
546120fa86aSPrzemyslaw Czarnowski         (*actionParams.transferMethod != "Stream"))
547120fa86aSPrzemyslaw Czarnowski     {
54862598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action optional parameter "
54962598e31SEd Tanous                          "TransferMethod must be Stream.");
550120fa86aSPrzemyslaw Czarnowski 
551120fa86aSPrzemyslaw Czarnowski         messages::actionParameterNotSupported(asyncResp->res, "TransferMethod",
552120fa86aSPrzemyslaw Czarnowski                                               "InsertMedia");
553120fa86aSPrzemyslaw Czarnowski 
554120fa86aSPrzemyslaw Czarnowski         return;
555120fa86aSPrzemyslaw Czarnowski     }
5566fd29553SEd Tanous     boost::system::result<boost::urls::url_view> url =
557120fa86aSPrzemyslaw Czarnowski         boost::urls::parse_uri(*actionParams.imageUrl);
558120fa86aSPrzemyslaw Czarnowski     if (!url)
559120fa86aSPrzemyslaw Czarnowski     {
560120fa86aSPrzemyslaw Czarnowski         messages::actionParameterValueFormatError(
561120fa86aSPrzemyslaw Czarnowski             asyncResp->res, *actionParams.imageUrl, "Image", "InsertMedia");
562120fa86aSPrzemyslaw Czarnowski         return;
563120fa86aSPrzemyslaw Czarnowski     }
564120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> uriTransferProtocolType =
565120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromUri(*url);
566120fa86aSPrzemyslaw Czarnowski 
567120fa86aSPrzemyslaw Czarnowski     std::optional<TransferProtocol> paramTransferProtocolType =
568120fa86aSPrzemyslaw Czarnowski         getTransferProtocolFromParam(actionParams.transferProtocolType);
569120fa86aSPrzemyslaw Czarnowski 
570120fa86aSPrzemyslaw Czarnowski     // ImageUrl does not contain valid protocol type
571e01d0c36SEd Tanous     if (uriTransferProtocolType &&
572e01d0c36SEd Tanous         *uriTransferProtocolType == TransferProtocol::invalid)
573120fa86aSPrzemyslaw Czarnowski     {
57462598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
575120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type from list: "
57662598e31SEd Tanous                          "(smb, https).");
577120fa86aSPrzemyslaw Czarnowski 
578120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
579120fa86aSPrzemyslaw Czarnowski 
580120fa86aSPrzemyslaw Czarnowski         return;
581120fa86aSPrzemyslaw Czarnowski     }
582120fa86aSPrzemyslaw Czarnowski 
583120fa86aSPrzemyslaw Czarnowski     // transferProtocolType should contain value from list
584e01d0c36SEd Tanous     if (paramTransferProtocolType &&
585e01d0c36SEd Tanous         *paramTransferProtocolType == TransferProtocol::invalid)
586120fa86aSPrzemyslaw Czarnowski     {
58762598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter TransferProtocolType "
588120fa86aSPrzemyslaw Czarnowski                          "must be provided with value from list: "
58962598e31SEd Tanous                          "(CIFS, HTTPS).");
590120fa86aSPrzemyslaw Czarnowski 
591e01d0c36SEd Tanous         messages::propertyValueNotInList(
592e01d0c36SEd Tanous             asyncResp->res, actionParams.transferProtocolType.value_or(""),
593120fa86aSPrzemyslaw Czarnowski             "TransferProtocolType");
594120fa86aSPrzemyslaw Czarnowski         return;
595120fa86aSPrzemyslaw Czarnowski     }
596120fa86aSPrzemyslaw Czarnowski 
597120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol not provided either with URI nor param
598e01d0c36SEd Tanous     if (!uriTransferProtocolType && !paramTransferProtocolType)
599120fa86aSPrzemyslaw Czarnowski     {
60062598e31SEd Tanous         BMCWEB_LOG_ERROR("Request action parameter ImageUrl must "
601120fa86aSPrzemyslaw Czarnowski                          "contain specified protocol type or param "
60262598e31SEd Tanous                          "TransferProtocolType must be provided.");
603120fa86aSPrzemyslaw Czarnowski 
604120fa86aSPrzemyslaw Czarnowski         messages::resourceAtUriInUnknownFormat(asyncResp->res, *url);
605120fa86aSPrzemyslaw Czarnowski 
606120fa86aSPrzemyslaw Czarnowski         return;
607120fa86aSPrzemyslaw Czarnowski     }
608120fa86aSPrzemyslaw Czarnowski 
609120fa86aSPrzemyslaw Czarnowski     // valid transfer protocol provided both with URI and param
610e01d0c36SEd Tanous     if (paramTransferProtocolType && uriTransferProtocolType)
611120fa86aSPrzemyslaw Czarnowski     {
612120fa86aSPrzemyslaw Czarnowski         // check if protocol is the same for URI and param
613120fa86aSPrzemyslaw Czarnowski         if (*paramTransferProtocolType != *uriTransferProtocolType)
614120fa86aSPrzemyslaw Czarnowski         {
61562598e31SEd Tanous             BMCWEB_LOG_ERROR("Request action parameter "
616120fa86aSPrzemyslaw Czarnowski                              "TransferProtocolType must  contain the "
617120fa86aSPrzemyslaw Czarnowski                              "same protocol type as protocol type "
61862598e31SEd Tanous                              "provided with param imageUrl.");
619120fa86aSPrzemyslaw Czarnowski 
620120fa86aSPrzemyslaw Czarnowski             messages::actionParameterValueTypeError(
621e01d0c36SEd Tanous                 asyncResp->res, actionParams.transferProtocolType.value_or(""),
622120fa86aSPrzemyslaw Czarnowski                 "TransferProtocolType", "InsertMedia");
623120fa86aSPrzemyslaw Czarnowski 
624120fa86aSPrzemyslaw Czarnowski             return;
625120fa86aSPrzemyslaw Czarnowski         }
626120fa86aSPrzemyslaw Czarnowski     }
627120fa86aSPrzemyslaw Czarnowski 
628120fa86aSPrzemyslaw Czarnowski     // validation passed, add protocol to URI if needed
6297ead48e6SBoleslaw Ogonczyk Makowski     if (!uriTransferProtocolType && paramTransferProtocolType)
630120fa86aSPrzemyslaw Czarnowski     {
631120fa86aSPrzemyslaw Czarnowski         actionParams.imageUrl = getUriWithTransferProtocol(
632120fa86aSPrzemyslaw Czarnowski             *actionParams.imageUrl, *paramTransferProtocolType);
633120fa86aSPrzemyslaw Czarnowski     }
634120fa86aSPrzemyslaw Czarnowski 
635452bd8d8SJayaprakash Mutyala     if (!actionParams.userName)
636452bd8d8SJayaprakash Mutyala     {
637452bd8d8SJayaprakash Mutyala         actionParams.userName = "";
638452bd8d8SJayaprakash Mutyala     }
639452bd8d8SJayaprakash Mutyala 
640452bd8d8SJayaprakash Mutyala     if (!actionParams.password)
641452bd8d8SJayaprakash Mutyala     {
642452bd8d8SJayaprakash Mutyala         actionParams.password = "";
643452bd8d8SJayaprakash Mutyala     }
644452bd8d8SJayaprakash Mutyala 
645120fa86aSPrzemyslaw Czarnowski     doMountVmLegacy(asyncResp, service, resName, *actionParams.imageUrl,
646e01d0c36SEd Tanous                     !(actionParams.writeProtected.value_or(false)),
647120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.userName),
648120fa86aSPrzemyslaw Czarnowski                     std::move(*actionParams.password));
649120fa86aSPrzemyslaw Czarnowski }
650120fa86aSPrzemyslaw Czarnowski 
651120fa86aSPrzemyslaw Czarnowski /**
652e13c2760SPrzemyslaw Czarnowski  * @brief Function transceives data with dbus directly.
653e13c2760SPrzemyslaw Czarnowski  *
654e13c2760SPrzemyslaw Czarnowski  * All BMC state properties will be retrieved before sending reset request.
655e13c2760SPrzemyslaw Czarnowski  */
65624e740a7SEd Tanous inline void doEjectAction(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
657e13c2760SPrzemyslaw Czarnowski                           const std::string& service, const std::string& name,
658e13c2760SPrzemyslaw Czarnowski                           bool legacy)
659e13c2760SPrzemyslaw Czarnowski {
660e13c2760SPrzemyslaw Czarnowski     // Legacy mount requires parameter with image
661e13c2760SPrzemyslaw Czarnowski     if (legacy)
662e13c2760SPrzemyslaw Czarnowski     {
663e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6645e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
665e13c2760SPrzemyslaw Czarnowski             if (ec)
666e13c2760SPrzemyslaw Czarnowski             {
66762598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
668e13c2760SPrzemyslaw Czarnowski 
669e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
670e13c2760SPrzemyslaw Czarnowski                 return;
671e13c2760SPrzemyslaw Czarnowski             }
672e13c2760SPrzemyslaw Czarnowski         },
673e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Legacy/" + name,
674e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Legacy", "Unmount");
675e13c2760SPrzemyslaw Czarnowski     }
676e13c2760SPrzemyslaw Czarnowski     else // proxy
677e13c2760SPrzemyslaw Czarnowski     {
678e13c2760SPrzemyslaw Czarnowski         crow::connections::systemBus->async_method_call(
6795e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec) {
680e13c2760SPrzemyslaw Czarnowski             if (ec)
681e13c2760SPrzemyslaw Czarnowski             {
68262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Bad D-Bus request error: {}", ec);
683e13c2760SPrzemyslaw Czarnowski 
684e13c2760SPrzemyslaw Czarnowski                 messages::internalError(asyncResp->res);
685e13c2760SPrzemyslaw Czarnowski                 return;
686e13c2760SPrzemyslaw Czarnowski             }
687e13c2760SPrzemyslaw Czarnowski         },
688e13c2760SPrzemyslaw Czarnowski             service, "/xyz/openbmc_project/VirtualMedia/Proxy/" + name,
689e13c2760SPrzemyslaw Czarnowski             "xyz.openbmc_project.VirtualMedia.Proxy", "Unmount");
690e13c2760SPrzemyslaw Czarnowski     }
691e13c2760SPrzemyslaw Czarnowski }
692e13c2760SPrzemyslaw Czarnowski 
69396825bebSEd Tanous inline void handleManagersVirtualMediaActionInsertPost(
69496825bebSEd Tanous     crow::App& app, const crow::Request& req,
69522db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
69696825bebSEd Tanous     const std::string& name, const std::string& resName)
69796825bebSEd Tanous {
6983ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
69945ca1b86SEd Tanous     {
70045ca1b86SEd Tanous         return;
70145ca1b86SEd Tanous     }
70279fdf63eSPrzemyslaw Czarnowski 
70379fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.InsertMedia";
70422db1728SEd Tanous     if (name != "bmc")
705107077deSPrzemyslaw Czarnowski     {
70679fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
707107077deSPrzemyslaw Czarnowski 
708107077deSPrzemyslaw Czarnowski         return;
709107077deSPrzemyslaw Czarnowski     }
71079fdf63eSPrzemyslaw Czarnowski     InsertMediaActionParams actionParams;
71198be3e39SEd Tanous 
712120fa86aSPrzemyslaw Czarnowski     // Read obligatory parameters (url of image)
71315ed6780SWilly Tu     if (!json_util::readJsonAction(
71479fdf63eSPrzemyslaw Czarnowski             req, asyncResp->res, "Image", actionParams.imageUrl,
71579fdf63eSPrzemyslaw Czarnowski             "WriteProtected", actionParams.writeProtected, "UserName",
71679fdf63eSPrzemyslaw Czarnowski             actionParams.userName, "Password", actionParams.password,
71779fdf63eSPrzemyslaw Czarnowski             "Inserted", actionParams.inserted, "TransferMethod",
71879fdf63eSPrzemyslaw Czarnowski             actionParams.transferMethod, "TransferProtocolType",
71979fdf63eSPrzemyslaw Czarnowski             actionParams.transferProtocolType))
72098be3e39SEd Tanous     {
72198be3e39SEd Tanous         return;
72298be3e39SEd Tanous     }
723107077deSPrzemyslaw Czarnowski 
7242b73119cSGeorge Liu     dbus::utility::getDbusObject(
7252b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
72679fdf63eSPrzemyslaw Czarnowski         [asyncResp, action, actionParams,
7272b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
728002d39b4SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) mutable {
72922db1728SEd Tanous         if (ec)
73022db1728SEd Tanous         {
73162598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
73279fdf63eSPrzemyslaw Czarnowski             messages::resourceNotFound(asyncResp->res, action, resName);
733107077deSPrzemyslaw Czarnowski 
73422db1728SEd Tanous             return;
73522db1728SEd Tanous         }
73679fdf63eSPrzemyslaw Czarnowski 
73722db1728SEd Tanous         std::string service = getObjectType.begin()->first;
73862598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
73922db1728SEd Tanous 
7405eb468daSGeorge Liu         sdbusplus::message::object_path path(
7415eb468daSGeorge Liu             "/xyz/openbmc_project/VirtualMedia");
7425eb468daSGeorge Liu         dbus::utility::getManagedObjects(
7435eb468daSGeorge Liu             service, path,
7445eb468daSGeorge Liu             [service, resName, action, actionParams, asyncResp](
7455eb468daSGeorge Liu                 const boost::system::error_code& ec2,
7465eb468daSGeorge Liu                 const dbus::utility::ManagedObjectType& subtree) mutable {
7478a592810SEd Tanous             if (ec2)
74822db1728SEd Tanous             {
74979fdf63eSPrzemyslaw Czarnowski                 // Not possible in proxy mode
75062598e31SEd Tanous                 BMCWEB_LOG_DEBUG("InsertMedia not "
75162598e31SEd Tanous                                  "allowed in proxy mode");
75279fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
75322db1728SEd Tanous 
75422db1728SEd Tanous                 return;
75522db1728SEd Tanous             }
75622db1728SEd Tanous             for (const auto& object : subtree)
75722db1728SEd Tanous             {
758365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
7595880f0c5SBoleslaw Ogonczyk Makowski                 if (mode == VmMode::Legacy)
76022db1728SEd Tanous                 {
76179fdf63eSPrzemyslaw Czarnowski                     validateParams(asyncResp, service, resName, actionParams);
76222db1728SEd Tanous 
76322db1728SEd Tanous                     return;
76422db1728SEd Tanous                 }
76522db1728SEd Tanous             }
76662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
76796825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
7685eb468daSGeorge Liu         });
7692b73119cSGeorge Liu     });
77096825bebSEd Tanous }
77122db1728SEd Tanous 
77296825bebSEd Tanous inline void handleManagersVirtualMediaActionEject(
77396825bebSEd Tanous     crow::App& app, const crow::Request& req,
77422db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
77596825bebSEd Tanous     const std::string& managerName, const std::string& resName)
77696825bebSEd Tanous {
7773ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
77845ca1b86SEd Tanous     {
77945ca1b86SEd Tanous         return;
78045ca1b86SEd Tanous     }
78179fdf63eSPrzemyslaw Czarnowski 
78279fdf63eSPrzemyslaw Czarnowski     constexpr std::string_view action = "VirtualMedia.EjectMedia";
78396825bebSEd Tanous     if (managerName != "bmc")
784107077deSPrzemyslaw Czarnowski     {
78579fdf63eSPrzemyslaw Czarnowski         messages::resourceNotFound(asyncResp->res, action, resName);
78622db1728SEd Tanous 
78722db1728SEd Tanous         return;
78822db1728SEd Tanous     }
78922db1728SEd Tanous 
7902b73119cSGeorge Liu     dbus::utility::getDbusObject(
7912b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
79279fdf63eSPrzemyslaw Czarnowski         [asyncResp, action,
7932b73119cSGeorge Liu          resName](const boost::system::error_code& ec2,
794b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
7958a592810SEd Tanous         if (ec2)
79622db1728SEd Tanous         {
79762598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec2);
79822db1728SEd Tanous             messages::internalError(asyncResp->res);
79922db1728SEd Tanous 
80022db1728SEd Tanous             return;
80122db1728SEd Tanous         }
80222db1728SEd Tanous         std::string service = getObjectType.begin()->first;
80362598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
80422db1728SEd Tanous 
8055eb468daSGeorge Liu         sdbusplus::message::object_path path(
8065eb468daSGeorge Liu             "/xyz/openbmc_project/VirtualMedia");
8075eb468daSGeorge Liu         dbus::utility::getManagedObjects(
8085eb468daSGeorge Liu             service, path,
80979fdf63eSPrzemyslaw Czarnowski             [resName, service, action,
81079fdf63eSPrzemyslaw Czarnowski              asyncResp](const boost::system::error_code& ec,
81102cad96eSEd Tanous                         const dbus::utility::ManagedObjectType& subtree) {
81222db1728SEd Tanous             if (ec)
81322db1728SEd Tanous             {
81462598e31SEd Tanous                 BMCWEB_LOG_ERROR("ObjectMapper : No Service found");
81579fdf63eSPrzemyslaw Czarnowski                 messages::resourceNotFound(asyncResp->res, action, resName);
81622db1728SEd Tanous                 return;
81722db1728SEd Tanous             }
81822db1728SEd Tanous 
81922db1728SEd Tanous             for (const auto& object : subtree)
82022db1728SEd Tanous             {
821365a73f4SEd Tanous                 VmMode mode = parseObjectPathAndGetMode(object.first, resName);
822365a73f4SEd Tanous                 if (mode != VmMode::Invalid)
82322db1728SEd Tanous                 {
824365a73f4SEd Tanous                     doEjectAction(asyncResp, service, resName,
825365a73f4SEd Tanous                                   mode == VmMode::Legacy);
8265880f0c5SBoleslaw Ogonczyk Makowski                     return;
82722db1728SEd Tanous                 }
82822db1728SEd Tanous             }
82962598e31SEd Tanous             BMCWEB_LOG_DEBUG("Parent item not found");
83096825bebSEd Tanous             messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
8315eb468daSGeorge Liu         });
8322b73119cSGeorge Liu     });
83396825bebSEd Tanous }
83496825bebSEd Tanous 
83596825bebSEd Tanous inline void handleManagersVirtualMediaCollectionGet(
83696825bebSEd Tanous     crow::App& app, const crow::Request& req,
83722db1728SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
83896825bebSEd Tanous     const std::string& name)
83996825bebSEd Tanous {
8403ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
84145ca1b86SEd Tanous     {
84245ca1b86SEd Tanous         return;
84345ca1b86SEd Tanous     }
84422db1728SEd Tanous     if (name != "bmc")
84522db1728SEd Tanous     {
846002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", name);
847107077deSPrzemyslaw Czarnowski 
848107077deSPrzemyslaw Czarnowski         return;
849107077deSPrzemyslaw Czarnowski     }
850107077deSPrzemyslaw Czarnowski 
8518d1b46d7Szhanghch05     asyncResp->res.jsonValue["@odata.type"] =
852107077deSPrzemyslaw Czarnowski         "#VirtualMediaCollection.VirtualMediaCollection";
8538d1b46d7Szhanghch05     asyncResp->res.jsonValue["Name"] = "Virtual Media Services";
854ef4c65b7SEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
855ef4c65b7SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/VirtualMedia", name);
856107077deSPrzemyslaw Czarnowski 
8572b73119cSGeorge Liu     dbus::utility::getDbusObject(
8582b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
8592b73119cSGeorge Liu         [asyncResp, name](const boost::system::error_code& ec,
860b9d36b47SEd Tanous                           const dbus::utility::MapperGetObject& getObjectType) {
861107077deSPrzemyslaw Czarnowski         if (ec)
862107077deSPrzemyslaw Czarnowski         {
86362598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
864107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
865107077deSPrzemyslaw Czarnowski 
866107077deSPrzemyslaw Czarnowski             return;
867107077deSPrzemyslaw Czarnowski         }
868107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
86962598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
870107077deSPrzemyslaw Czarnowski 
871107077deSPrzemyslaw Czarnowski         getVmResourceList(asyncResp, service, name);
8722b73119cSGeorge Liu     });
87396825bebSEd Tanous }
874107077deSPrzemyslaw Czarnowski 
87596825bebSEd Tanous inline void
87696825bebSEd Tanous     handleVirtualMediaGet(crow::App& app, const crow::Request& req,
87722db1728SEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
87896825bebSEd Tanous                           const std::string& name, const std::string& resName)
87996825bebSEd Tanous {
8803ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
88145ca1b86SEd Tanous     {
88245ca1b86SEd Tanous         return;
88345ca1b86SEd Tanous     }
884107077deSPrzemyslaw Czarnowski     if (name != "bmc")
885107077deSPrzemyslaw Czarnowski     {
886002d39b4SEd Tanous         messages::resourceNotFound(asyncResp->res, "VirtualMedia", resName);
887107077deSPrzemyslaw Czarnowski 
888107077deSPrzemyslaw Czarnowski         return;
889107077deSPrzemyslaw Czarnowski     }
890107077deSPrzemyslaw Czarnowski 
8912b73119cSGeorge Liu     dbus::utility::getDbusObject(
8922b73119cSGeorge Liu         "/xyz/openbmc_project/VirtualMedia", {},
893002d39b4SEd Tanous         [asyncResp, name,
8942b73119cSGeorge Liu          resName](const boost::system::error_code& ec,
895b9d36b47SEd Tanous                   const dbus::utility::MapperGetObject& getObjectType) {
896107077deSPrzemyslaw Czarnowski         if (ec)
897107077deSPrzemyslaw Czarnowski         {
89862598e31SEd Tanous             BMCWEB_LOG_ERROR("ObjectMapper::GetObject call failed: {}", ec);
899107077deSPrzemyslaw Czarnowski             messages::internalError(asyncResp->res);
900107077deSPrzemyslaw Czarnowski 
901107077deSPrzemyslaw Czarnowski             return;
902107077deSPrzemyslaw Czarnowski         }
903107077deSPrzemyslaw Czarnowski         std::string service = getObjectType.begin()->first;
90462598e31SEd Tanous         BMCWEB_LOG_DEBUG("GetObjectType: {}", service);
905107077deSPrzemyslaw Czarnowski 
906107077deSPrzemyslaw Czarnowski         getVmData(asyncResp, service, name, resName);
9072b73119cSGeorge Liu     });
90896825bebSEd Tanous }
90996825bebSEd Tanous 
91096825bebSEd Tanous inline void requestNBDVirtualMediaRoutes(App& app)
91196825bebSEd Tanous {
91296825bebSEd Tanous     BMCWEB_ROUTE(
91396825bebSEd Tanous         app,
91496825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.InsertMedia")
91596825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
91696825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
91796825bebSEd Tanous             handleManagersVirtualMediaActionInsertPost, std::ref(app)));
91896825bebSEd Tanous 
91996825bebSEd Tanous     BMCWEB_ROUTE(
92096825bebSEd Tanous         app,
92196825bebSEd Tanous         "/redfish/v1/Managers/<str>/VirtualMedia/<str>/Actions/VirtualMedia.EjectMedia")
92296825bebSEd Tanous         .privileges(redfish::privileges::postVirtualMedia)
92396825bebSEd Tanous         .methods(boost::beast::http::verb::post)(std::bind_front(
92496825bebSEd Tanous             handleManagersVirtualMediaActionEject, std::ref(app)));
92596825bebSEd Tanous 
92696825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/")
92796825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMediaCollection)
92896825bebSEd Tanous         .methods(boost::beast::http::verb::get)(std::bind_front(
92996825bebSEd Tanous             handleManagersVirtualMediaCollectionGet, std::ref(app)));
93096825bebSEd Tanous 
93196825bebSEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/VirtualMedia/<str>/")
93296825bebSEd Tanous         .privileges(redfish::privileges::getVirtualMedia)
93396825bebSEd Tanous         .methods(boost::beast::http::verb::get)(
93496825bebSEd Tanous             std::bind_front(handleVirtualMediaGet, std::ref(app)));
935107077deSPrzemyslaw Czarnowski }
936107077deSPrzemyslaw Czarnowski 
937107077deSPrzemyslaw Czarnowski } // namespace redfish
938