xref: /openbmc/bmcweb/redfish-core/lib/hypervisor_system.hpp (revision 8a7c4b475469c8811dffe265992b903060aad65f)
1 #pragma once
2 
3 #include "app.hpp"
4 #include "dbus_singleton.hpp"
5 #include "dbus_utility.hpp"
6 #include "error_messages.hpp"
7 #include "ethernet.hpp"
8 #include "query.hpp"
9 #include "registries/privilege_registry.hpp"
10 #include "utils/ip_utils.hpp"
11 #include "utils/json_utils.hpp"
12 
13 #include <boost/url/format.hpp>
14 #include <sdbusplus/asio/property.hpp>
15 
16 #include <array>
17 #include <optional>
18 #include <string_view>
19 #include <utility>
20 
21 namespace redfish
22 {
23 
24 /**
25  * @brief Retrieves hypervisor state properties over dbus
26  *
27  * The hypervisor state object is optional so this function will only set the
28  * state variables if the object is found
29  *
30  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
31  *
32  * @return None.
33  */
34 inline void
35     getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
36 {
37     BMCWEB_LOG_DEBUG << "Get hypervisor state information.";
38     sdbusplus::asio::getProperty<std::string>(
39         *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor",
40         "/xyz/openbmc_project/state/hypervisor0",
41         "xyz.openbmc_project.State.Host", "CurrentHostState",
42         [asyncResp](const boost::system::error_code& ec,
43                     const std::string& hostState) {
44         if (ec)
45         {
46             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
47             // This is an optional D-Bus object so just return if
48             // error occurs
49             return;
50         }
51 
52         BMCWEB_LOG_DEBUG << "Hypervisor state: " << hostState;
53         // Verify Host State
54         if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
55         {
56             asyncResp->res.jsonValue["PowerState"] = "On";
57             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
58         }
59         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
60                               "Quiesced")
61         {
62             asyncResp->res.jsonValue["PowerState"] = "On";
63             asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
64         }
65         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
66                               "Standby")
67         {
68             asyncResp->res.jsonValue["PowerState"] = "On";
69             asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
70         }
71         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
72                               "TransitioningToRunning")
73         {
74             asyncResp->res.jsonValue["PowerState"] = "PoweringOn";
75             asyncResp->res.jsonValue["Status"]["State"] = "Starting";
76         }
77         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
78                               "TransitioningToOff")
79         {
80             asyncResp->res.jsonValue["PowerState"] = "PoweringOff";
81             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
82         }
83         else if (hostState == "xyz.openbmc_project.State.Host.HostState.Off")
84         {
85             asyncResp->res.jsonValue["PowerState"] = "Off";
86             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
87         }
88         else
89         {
90             messages::internalError(asyncResp->res);
91             return;
92         }
93         });
94 }
95 
96 /**
97  * @brief Populate Actions if any are valid for hypervisor object
98  *
99  * The hypervisor state object is optional so this function will only set the
100  * Action if the object is found
101  *
102  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
103  *
104  * @return None.
105  */
106 inline void
107     getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
108 {
109     BMCWEB_LOG_DEBUG << "Get hypervisor actions.";
110     constexpr std::array<std::string_view, 1> interfaces = {
111         "xyz.openbmc_project.State.Host"};
112     dbus::utility::getDbusObject(
113         "/xyz/openbmc_project/state/hypervisor0", interfaces,
114         [asyncResp](
115             const boost::system::error_code& ec,
116             const std::vector<std::pair<std::string, std::vector<std::string>>>&
117                 objInfo) {
118         if (ec)
119         {
120             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
121             // This is an optional D-Bus object so just return if
122             // error occurs
123             return;
124         }
125 
126         if (objInfo.empty())
127         {
128             // As noted above, this is an optional interface so just return
129             // if there is no instance found
130             return;
131         }
132 
133         if (objInfo.size() > 1)
134         {
135             // More then one hypervisor object is not supported and is an
136             // error
137             messages::internalError(asyncResp->res);
138             return;
139         }
140 
141         // Object present so system support limited ComputerSystem Action
142         nlohmann::json& reset =
143             asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"];
144         reset["target"] =
145             "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset";
146         reset["@Redfish.ActionInfo"] =
147             "/redfish/v1/Systems/hypervisor/ResetActionInfo";
148         });
149 }
150 
151 inline bool extractHypervisorInterfaceData(
152     const std::string& ethIfaceId,
153     const dbus::utility::ManagedObjectType& dbusData,
154     EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config)
155 {
156     bool idFound = false;
157     for (const auto& objpath : dbusData)
158     {
159         for (const auto& ifacePair : objpath.second)
160         {
161             if (objpath.first ==
162                 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId)
163             {
164                 idFound = true;
165                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
166                 {
167                     for (const auto& propertyPair : ifacePair.second)
168                     {
169                         if (propertyPair.first == "MACAddress")
170                         {
171                             const std::string* mac =
172                                 std::get_if<std::string>(&propertyPair.second);
173                             if (mac != nullptr)
174                             {
175                                 ethData.macAddress = *mac;
176                             }
177                         }
178                     }
179                 }
180                 else if (ifacePair.first ==
181                          "xyz.openbmc_project.Network.EthernetInterface")
182                 {
183                     for (const auto& propertyPair : ifacePair.second)
184                     {
185                         if (propertyPair.first == "DHCPEnabled")
186                         {
187                             const std::string* dhcp =
188                                 std::get_if<std::string>(&propertyPair.second);
189                             if (dhcp != nullptr)
190                             {
191                                 ethData.dhcpEnabled = *dhcp;
192                                 break; // Interested on only "DHCPEnabled".
193                                        // Stop parsing since we got the
194                                        // "DHCPEnabled" value.
195                             }
196                         }
197                     }
198                 }
199             }
200             if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" +
201                                      ethIfaceId + "/ipv4/addr0")
202             {
203                 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
204                 if (ifacePair.first == "xyz.openbmc_project.Object.Enable")
205                 {
206                     for (const auto& property : ifacePair.second)
207                     {
208                         if (property.first == "Enabled")
209                         {
210                             const bool* intfEnable =
211                                 std::get_if<bool>(&property.second);
212                             if (intfEnable != nullptr)
213                             {
214                                 ipv4Address.isActive = *intfEnable;
215                                 break;
216                             }
217                         }
218                     }
219                 }
220                 if (ifacePair.first == "xyz.openbmc_project.Network.IP")
221                 {
222                     for (const auto& property : ifacePair.second)
223                     {
224                         if (property.first == "Address")
225                         {
226                             const std::string* address =
227                                 std::get_if<std::string>(&property.second);
228                             if (address != nullptr)
229                             {
230                                 ipv4Address.address = *address;
231                             }
232                         }
233                         else if (property.first == "Origin")
234                         {
235                             const std::string* origin =
236                                 std::get_if<std::string>(&property.second);
237                             if (origin != nullptr)
238                             {
239                                 ipv4Address.origin =
240                                     translateAddressOriginDbusToRedfish(*origin,
241                                                                         true);
242                             }
243                         }
244                         else if (property.first == "PrefixLength")
245                         {
246                             const uint8_t* mask =
247                                 std::get_if<uint8_t>(&property.second);
248                             if (mask != nullptr)
249                             {
250                                 // convert it to the string
251                                 ipv4Address.netmask = getNetmask(*mask);
252                             }
253                         }
254                         else if (property.first == "Type" ||
255                                  property.first == "Gateway")
256                         {
257                             // Type & Gateway is not used
258                             continue;
259                         }
260                         else
261                         {
262                             BMCWEB_LOG_ERROR
263                                 << "Got extra property: " << property.first
264                                 << " on the " << objpath.first.str << " object";
265                         }
266                     }
267                 }
268             }
269             if (objpath.first == "/xyz/openbmc_project/network/hypervisor")
270             {
271                 // System configuration shows up in the global namespace, so no
272                 // need to check eth number
273                 if (ifacePair.first ==
274                     "xyz.openbmc_project.Network.SystemConfiguration")
275                 {
276                     for (const auto& propertyPair : ifacePair.second)
277                     {
278                         if (propertyPair.first == "HostName")
279                         {
280                             const std::string* hostName =
281                                 std::get_if<std::string>(&propertyPair.second);
282                             if (hostName != nullptr)
283                             {
284                                 ethData.hostName = *hostName;
285                             }
286                         }
287                         else if (propertyPair.first == "DefaultGateway")
288                         {
289                             const std::string* defaultGateway =
290                                 std::get_if<std::string>(&propertyPair.second);
291                             if (defaultGateway != nullptr)
292                             {
293                                 ethData.defaultGateway = *defaultGateway;
294                             }
295                         }
296                     }
297                 }
298             }
299         }
300     }
301     return idFound;
302 }
303 /**
304  * Function that retrieves all properties for given Hypervisor Ethernet
305  * Interface Object from Settings Manager
306  * @param ethIfaceId Hypervisor ethernet interface id to query on DBus
307  * @param callback a function that shall be called to convert Dbus output
308  * into JSON
309  */
310 template <typename CallbackFunc>
311 void getHypervisorIfaceData(const std::string& ethIfaceId,
312                             CallbackFunc&& callback)
313 {
314     sdbusplus::message::object_path path("/");
315     dbus::utility::getManagedObjects(
316         "xyz.openbmc_project.Settings", path,
317         [ethIfaceId{std::string{ethIfaceId}},
318          callback{std::forward<CallbackFunc>(callback)}](
319             const boost::system::error_code& error,
320             const dbus::utility::ManagedObjectType& resp) {
321         EthernetInterfaceData ethData{};
322         std::vector<IPv4AddressData> ipv4Data;
323         if (error)
324         {
325             callback(false, ethData, ipv4Data);
326             return;
327         }
328 
329         bool found = extractHypervisorInterfaceData(ethIfaceId, resp, ethData,
330                                                     ipv4Data);
331         if (!found)
332         {
333             BMCWEB_LOG_INFO << "Hypervisor Interface not found";
334         }
335         callback(found, ethData, ipv4Data);
336         });
337 }
338 
339 /**
340  * @brief Sets the Hypervisor Interface IPAddress DBUS
341  *
342  * @param[in] asyncResp          Shared pointer for generating response message.
343  * @param[in] ipv4Address    Address from the incoming request
344  * @param[in] ethIfaceId     Hypervisor Interface Id
345  *
346  * @return None.
347  */
348 inline void setHypervisorIPv4Address(
349     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
350     const std::string& ethIfaceId, const std::string& ipv4Address)
351 {
352     BMCWEB_LOG_DEBUG << "Setting the Hypervisor IPaddress : " << ipv4Address
353                      << " on Iface: " << ethIfaceId;
354     crow::connections::systemBus->async_method_call(
355         [asyncResp](const boost::system::error_code& ec) {
356         if (ec)
357         {
358             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
359             return;
360         }
361         BMCWEB_LOG_DEBUG << "Hypervisor IPaddress is Set";
362         },
363         "xyz.openbmc_project.Settings",
364         "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0",
365         "org.freedesktop.DBus.Properties", "Set",
366         "xyz.openbmc_project.Network.IP", "Address",
367         dbus::utility::DbusVariantType(ipv4Address));
368 }
369 
370 /**
371  * @brief Sets the Hypervisor Interface SubnetMask DBUS
372  *
373  * @param[in] asyncResp     Shared pointer for generating response message.
374  * @param[in] subnet    SubnetMask from the incoming request
375  * @param[in] ethIfaceId Hypervisor Interface Id
376  *
377  * @return None.
378  */
379 inline void
380     setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
381                             const std::string& ethIfaceId, const uint8_t subnet)
382 {
383     BMCWEB_LOG_DEBUG << "Setting the Hypervisor subnet : " << subnet
384                      << " on Iface: " << ethIfaceId;
385 
386     crow::connections::systemBus->async_method_call(
387         [asyncResp](const boost::system::error_code& ec) {
388         if (ec)
389         {
390             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
391             return;
392         }
393         BMCWEB_LOG_DEBUG << "SubnetMask is Set";
394         },
395         "xyz.openbmc_project.Settings",
396         "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0",
397         "org.freedesktop.DBus.Properties", "Set",
398         "xyz.openbmc_project.Network.IP", "PrefixLength",
399         dbus::utility::DbusVariantType(subnet));
400 }
401 
402 /**
403  * @brief Sets the Hypervisor Interface Gateway DBUS
404  *
405  * @param[in] asyncResp          Shared pointer for generating response message.
406  * @param[in] gateway        Gateway from the incoming request
407  * @param[in] ethIfaceId     Hypervisor Interface Id
408  *
409  * @return None.
410  */
411 inline void setHypervisorIPv4Gateway(
412     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
413     const std::string& gateway)
414 {
415     BMCWEB_LOG_DEBUG
416         << "Setting the DefaultGateway to the last configured gateway";
417 
418     crow::connections::systemBus->async_method_call(
419         [asyncResp](const boost::system::error_code& ec) {
420         if (ec)
421         {
422             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
423             return;
424         }
425         BMCWEB_LOG_DEBUG << "Default Gateway is Set";
426         },
427         "xyz.openbmc_project.Settings",
428         "/xyz/openbmc_project/network/hypervisor",
429         "org.freedesktop.DBus.Properties", "Set",
430         "xyz.openbmc_project.Network.SystemConfiguration", "DefaultGateway",
431         dbus::utility::DbusVariantType(gateway));
432 }
433 
434 /**
435  * @brief Creates a static IPv4 entry
436  *
437  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
438  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
439  * @param[in] gateway      IPv4 address of this interfaces gateway
440  * @param[in] address      IPv4 address to assign to this interface
441  * @param[io] asyncResp    Response object that will be returned to client
442  *
443  * @return None
444  */
445 inline void
446     createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength,
447                          const std::string& gateway, const std::string& address,
448                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
449 {
450     setHypervisorIPv4Address(asyncResp, ifaceId, address);
451     setHypervisorIPv4Gateway(asyncResp, gateway);
452     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
453 }
454 
455 /**
456  * @brief Deletes given IPv4 interface
457  *
458  * @param[in] ifaceId     Id of interface whose IP should be deleted
459  * @param[io] asyncResp   Response object that will be returned to client
460  *
461  * @return None
462  */
463 inline void
464     deleteHypervisorIPv4(const std::string& ifaceId,
465                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
466 {
467     std::string address = "0.0.0.0";
468     std::string gateway = "0.0.0.0";
469     const uint8_t prefixLength = 0;
470     setHypervisorIPv4Address(asyncResp, ifaceId, address);
471     setHypervisorIPv4Gateway(asyncResp, gateway);
472     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
473 }
474 
475 inline void parseInterfaceData(nlohmann::json& jsonResponse,
476                                const std::string& ifaceId,
477                                const EthernetInterfaceData& ethData,
478                                const std::vector<IPv4AddressData>& ipv4Data)
479 {
480     jsonResponse["Id"] = ifaceId;
481     jsonResponse["@odata.id"] = boost::urls::format(
482         "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId);
483     jsonResponse["InterfaceEnabled"] = true;
484     jsonResponse["MACAddress"] = ethData.macAddress;
485 
486     jsonResponse["HostName"] = ethData.hostName;
487     jsonResponse["DHCPv4"]["DHCPEnabled"] =
488         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
489 
490     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
491     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
492     ipv4Array = nlohmann::json::array();
493     ipv4StaticArray = nlohmann::json::array();
494     for (const auto& ipv4Config : ipv4Data)
495     {
496         if (ipv4Config.isActive)
497         {
498             nlohmann::json::object_t ipv4;
499             ipv4["AddressOrigin"] = ipv4Config.origin;
500             ipv4["SubnetMask"] = ipv4Config.netmask;
501             ipv4["Address"] = ipv4Config.address;
502             ipv4["Gateway"] = ethData.defaultGateway;
503 
504             if (ipv4Config.origin == "Static")
505             {
506                 ipv4StaticArray.push_back(ipv4);
507             }
508             ipv4Array.emplace_back(std::move(ipv4));
509         }
510     }
511 }
512 
513 inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled,
514                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
515 {
516     const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false);
517     crow::connections::systemBus->async_method_call(
518         [asyncResp](const boost::system::error_code& ec) {
519         if (ec)
520         {
521             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
522             messages::internalError(asyncResp->res);
523             return;
524         }
525         },
526         "xyz.openbmc_project.Settings",
527         "/xyz/openbmc_project/network/hypervisor/" + ifaceId,
528         "org.freedesktop.DBus.Properties", "Set",
529         "xyz.openbmc_project.Network.EthernetInterface", "DHCPEnabled",
530         dbus::utility::DbusVariantType{dhcp});
531 
532     // Set the IPv4 address origin to the DHCP / Static as per the new value
533     // of the DHCPEnabled property
534     std::string origin;
535     if (!ipv4DHCPEnabled)
536     {
537         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
538     }
539     else
540     {
541         // DHCPEnabled is set to true. Delete the current IPv4 settings
542         // to receive the new values from DHCP server.
543         deleteHypervisorIPv4(ifaceId, asyncResp);
544         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
545     }
546     crow::connections::systemBus->async_method_call(
547         [asyncResp](const boost::system::error_code& ec) {
548         if (ec)
549         {
550             BMCWEB_LOG_ERROR << "DBUS response error " << ec;
551             messages::internalError(asyncResp->res);
552             return;
553         }
554         BMCWEB_LOG_DEBUG << "Hypervisor IPaddress Origin is Set";
555         },
556         "xyz.openbmc_project.Settings",
557         "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0",
558         "org.freedesktop.DBus.Properties", "Set",
559         "xyz.openbmc_project.Network.IP", "Origin",
560         dbus::utility::DbusVariantType(origin));
561 }
562 
563 inline void handleHypervisorIPv4StaticPatch(
564     const std::string& ifaceId, const nlohmann::json& input,
565     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
566 {
567     if ((!input.is_array()) || input.empty())
568     {
569         messages::propertyValueTypeError(asyncResp->res, input,
570                                          "IPv4StaticAddresses");
571         return;
572     }
573 
574     // Hypervisor considers the first IP address in the array list
575     // as the Hypervisor's virtual management interface supports single IPv4
576     // address
577     const nlohmann::json& thisJson = input[0];
578 
579     if (!thisJson.is_null() && !thisJson.empty())
580     {
581         // For the error string
582         std::string pathString = "IPv4StaticAddresses/1";
583         std::optional<std::string> address;
584         std::optional<std::string> subnetMask;
585         std::optional<std::string> gateway;
586         nlohmann::json thisJsonCopy = thisJson;
587         if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
588                                  address, "SubnetMask", subnetMask, "Gateway",
589                                  gateway))
590         {
591             messages::propertyValueFormatError(asyncResp->res, thisJson,
592                                                pathString);
593             return;
594         }
595 
596         uint8_t prefixLength = 0;
597         bool errorInEntry = false;
598         if (address)
599         {
600             if (!ip_util::ipv4VerifyIpAndGetBitcount(*address))
601             {
602                 messages::propertyValueFormatError(asyncResp->res, *address,
603                                                    pathString + "/Address");
604                 errorInEntry = true;
605             }
606         }
607         else
608         {
609             messages::propertyMissing(asyncResp->res, pathString + "/Address");
610             errorInEntry = true;
611         }
612 
613         if (subnetMask)
614         {
615             if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
616                                                      &prefixLength))
617             {
618                 messages::propertyValueFormatError(asyncResp->res, *subnetMask,
619                                                    pathString + "/SubnetMask");
620                 errorInEntry = true;
621             }
622         }
623         else
624         {
625             messages::propertyMissing(asyncResp->res,
626                                       pathString + "/SubnetMask");
627             errorInEntry = true;
628         }
629 
630         if (gateway)
631         {
632             if (!ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
633             {
634                 messages::propertyValueFormatError(asyncResp->res, *gateway,
635                                                    pathString + "/Gateway");
636                 errorInEntry = true;
637             }
638         }
639         else
640         {
641             messages::propertyMissing(asyncResp->res, pathString + "/Gateway");
642             errorInEntry = true;
643         }
644 
645         if (errorInEntry)
646         {
647             return;
648         }
649 
650         BMCWEB_LOG_DEBUG << "Calling createHypervisorIPv4 on : " << ifaceId
651                          << "," << *address;
652         createHypervisorIPv4(ifaceId, prefixLength, *gateway, *address,
653                              asyncResp);
654         // Set the DHCPEnabled to false since the Static IPv4 is set
655         setDHCPEnabled(ifaceId, false, asyncResp);
656     }
657     else
658     {
659         if (thisJson.is_null())
660         {
661             deleteHypervisorIPv4(ifaceId, asyncResp);
662         }
663     }
664 }
665 
666 inline void handleHypervisorHostnamePatch(
667     const std::string& hostName,
668     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
669 {
670     if (!isHostnameValid(hostName))
671     {
672         messages::propertyValueFormatError(asyncResp->res, hostName,
673                                            "HostName");
674         return;
675     }
676 
677     asyncResp->res.jsonValue["HostName"] = hostName;
678     crow::connections::systemBus->async_method_call(
679         [asyncResp](const boost::system::error_code& ec) {
680         if (ec)
681         {
682             messages::internalError(asyncResp->res);
683         }
684         },
685         "xyz.openbmc_project.Settings",
686         "/xyz/openbmc_project/network/hypervisor",
687         "org.freedesktop.DBus.Properties", "Set",
688         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
689         dbus::utility::DbusVariantType(hostName));
690 }
691 
692 inline void
693     setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive,
694                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
695 {
696     crow::connections::systemBus->async_method_call(
697         [asyncResp](const boost::system::error_code& ec) {
698         if (ec)
699         {
700             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
701             messages::internalError(asyncResp->res);
702             return;
703         }
704         },
705         "xyz.openbmc_project.Settings",
706         "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0",
707         "org.freedesktop.DBus.Properties", "Set",
708         "xyz.openbmc_project.Object.Enable", "Enabled",
709         dbus::utility::DbusVariantType(isActive));
710 }
711 
712 inline void handleHypervisorEthernetInterfaceCollectionGet(
713     App& app, const crow::Request& req,
714     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
715 {
716     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
717     {
718         return;
719     }
720     constexpr std::array<std::string_view, 1> interfaces = {
721         "xyz.openbmc_project.Network.EthernetInterface"};
722 
723     dbus::utility::getSubTreePaths(
724         "/xyz/openbmc_project/network/hypervisor", 0, interfaces,
725         [asyncResp](
726             const boost::system::error_code& error,
727             const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) {
728         if (error)
729         {
730             messages::resourceNotFound(asyncResp->res, "System", "hypervisor");
731             return;
732         }
733         asyncResp->res.jsonValue["@odata.type"] =
734             "#EthernetInterfaceCollection."
735             "EthernetInterfaceCollection";
736         asyncResp->res.jsonValue["@odata.id"] =
737             "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
738         asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet "
739                                            "Interface Collection";
740         asyncResp->res.jsonValue["Description"] =
741             "Collection of Virtual Management "
742             "Interfaces for the hypervisor";
743 
744         nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
745         ifaceArray = nlohmann::json::array();
746         for (const std::string& iface : ifaceList)
747         {
748             sdbusplus::message::object_path path(iface);
749             std::string name = path.filename();
750             if (name.empty())
751             {
752                 continue;
753             }
754             nlohmann::json::object_t ethIface;
755             ethIface["@odata.id"] = boost::urls::format(
756                 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", name);
757             ifaceArray.emplace_back(std::move(ethIface));
758         }
759         asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
760         });
761 }
762 
763 inline void handleHypervisorEthernetInterfaceGet(
764     App& app, const crow::Request& req,
765     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
766 {
767     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
768     {
769         return;
770     }
771     getHypervisorIfaceData(
772         id, [asyncResp, ifaceId{std::string(id)}](
773                 bool success, const EthernetInterfaceData& ethData,
774                 const std::vector<IPv4AddressData>& ipv4Data) {
775             if (!success)
776             {
777                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
778                                            ifaceId);
779                 return;
780             }
781             asyncResp->res.jsonValue["@odata.type"] =
782                 "#EthernetInterface.v1_9_0.EthernetInterface";
783             asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface";
784             asyncResp->res.jsonValue["Description"] =
785                 "Hypervisor's Virtual Management Ethernet Interface";
786             parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData,
787                                ipv4Data);
788         });
789 }
790 
791 inline void handleHypervisorSystemGet(
792     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
793 {
794     sdbusplus::asio::getProperty<std::string>(
795         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
796         "/xyz/openbmc_project/network/hypervisor",
797         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
798         [asyncResp](const boost::system::error_code& ec,
799                     const std::string& /*hostName*/) {
800         if (ec)
801         {
802             messages::resourceNotFound(asyncResp->res, "System", "hypervisor");
803             return;
804         }
805         BMCWEB_LOG_DEBUG << "Hypervisor is available";
806 
807         asyncResp->res.jsonValue["@odata.type"] =
808             "#ComputerSystem.v1_6_0.ComputerSystem";
809         asyncResp->res.jsonValue["@odata.id"] =
810             "/redfish/v1/Systems/hypervisor";
811         asyncResp->res.jsonValue["Description"] = "Hypervisor";
812         asyncResp->res.jsonValue["Name"] = "Hypervisor";
813         asyncResp->res.jsonValue["Id"] = "hypervisor";
814         asyncResp->res.jsonValue["SystemType"] = "OS";
815         nlohmann::json::array_t managedBy;
816         nlohmann::json::object_t manager;
817         manager["@odata.id"] = "/redfish/v1/Managers/bmc";
818         managedBy.emplace_back(std::move(manager));
819         asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
820         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
821             "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
822         getHypervisorState(asyncResp);
823         getHypervisorActions(asyncResp);
824         // TODO: Add "SystemType" : "hypervisor"
825         });
826 }
827 
828 inline void handleHypervisorEthernetInterfacePatch(
829     App& app, const crow::Request& req,
830     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
831     const std::string& ifaceId)
832 {
833     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
834     {
835         return;
836     }
837     std::optional<std::string> hostName;
838     std::optional<std::vector<nlohmann::json>> ipv4StaticAddresses;
839     std::optional<nlohmann::json> ipv4Addresses;
840     std::optional<nlohmann::json> dhcpv4;
841     std::optional<bool> ipv4DHCPEnabled;
842 
843     if (!json_util::readJsonPatch(req, asyncResp->res, "HostName", hostName,
844                                   "IPv4StaticAddresses", ipv4StaticAddresses,
845                                   "IPv4Addresses", ipv4Addresses, "DHCPv4",
846                                   dhcpv4))
847     {
848         return;
849     }
850 
851     if (ipv4Addresses)
852     {
853         messages::propertyNotWritable(asyncResp->res, "IPv4Addresses");
854         return;
855     }
856 
857     if (dhcpv4)
858     {
859         if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
860                                  ipv4DHCPEnabled))
861         {
862             return;
863         }
864     }
865 
866     getHypervisorIfaceData(
867         ifaceId,
868         [asyncResp, ifaceId, hostName = std::move(hostName),
869          ipv4StaticAddresses = std::move(ipv4StaticAddresses), ipv4DHCPEnabled,
870          dhcpv4 = std::move(dhcpv4)](bool success,
871                                      const EthernetInterfaceData& ethData,
872                                      const std::vector<IPv4AddressData>&) {
873         if (!success)
874         {
875             messages::resourceNotFound(asyncResp->res, "EthernetInterface",
876                                        ifaceId);
877             return;
878         }
879 
880         if (ipv4StaticAddresses)
881         {
882             const nlohmann::json& ipv4Static = *ipv4StaticAddresses;
883             if (ipv4Static.begin() == ipv4Static.end())
884             {
885                 messages::propertyValueTypeError(asyncResp->res, ipv4Static,
886                                                  "IPv4StaticAddresses");
887                 return;
888             }
889 
890             // One and only one hypervisor instance supported
891             if (ipv4Static.size() != 1)
892             {
893                 messages::propertyValueFormatError(asyncResp->res, ipv4Static,
894                                                    "IPv4StaticAddresses");
895                 return;
896             }
897 
898             const nlohmann::json& ipv4Json = ipv4Static[0];
899             // Check if the param is 'null'. If its null, it means
900             // that user wants to delete the IP address. Deleting
901             // the IP address is allowed only if its statically
902             // configured. Deleting the address originated from DHCP
903             // is not allowed.
904             if ((ipv4Json.is_null()) &&
905                 (translateDhcpEnabledToBool(ethData.dhcpEnabled, true)))
906             {
907                 BMCWEB_LOG_INFO << "Ignoring the delete on ipv4StaticAddresses "
908                                    "as the interface is DHCP enabled";
909             }
910             else
911             {
912                 handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, asyncResp);
913             }
914         }
915 
916         if (hostName)
917         {
918             handleHypervisorHostnamePatch(*hostName, asyncResp);
919         }
920 
921         if (dhcpv4)
922         {
923             setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp);
924         }
925 
926         // Set this interface to disabled/inactive. This will be set
927         // to enabled/active by the pldm once the hypervisor
928         // consumes the updated settings from the user.
929         setIPv4InterfaceEnabled(ifaceId, false, asyncResp);
930         });
931     asyncResp->res.result(boost::beast::http::status::accepted);
932 }
933 
934 inline void handleHypervisorResetActionGet(
935     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
936 {
937     // Only return action info if hypervisor D-Bus object present
938     constexpr std::array<std::string_view, 1> interfaces = {
939         "xyz.openbmc_project.State.Host"};
940     dbus::utility::getDbusObject(
941         "/xyz/openbmc_project/state/hypervisor0", interfaces,
942         [asyncResp](
943             const boost::system::error_code& ec,
944             const std::vector<std::pair<std::string, std::vector<std::string>>>&
945                 objInfo) {
946         if (ec)
947         {
948             BMCWEB_LOG_DEBUG << "DBUS response error " << ec;
949 
950             // No hypervisor objects found by mapper
951             if (ec.value() == boost::system::errc::io_error)
952             {
953                 messages::resourceNotFound(asyncResp->res, "hypervisor",
954                                            "ResetActionInfo");
955                 return;
956             }
957 
958             messages::internalError(asyncResp->res);
959             return;
960         }
961 
962         // One and only one hypervisor instance supported
963         if (objInfo.size() != 1)
964         {
965             messages::internalError(asyncResp->res);
966             return;
967         }
968 
969         // The hypervisor object only support the ability to
970         // turn On The system object Action should be utilized
971         // for other operations
972 
973         asyncResp->res.jsonValue["@odata.type"] =
974             "#ActionInfo.v1_1_2.ActionInfo";
975         asyncResp->res.jsonValue["@odata.id"] =
976             "/redfish/v1/Systems/hypervisor/ResetActionInfo";
977         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
978         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
979         nlohmann::json::array_t parameters;
980         nlohmann::json::object_t parameter;
981         parameter["Name"] = "ResetType";
982         parameter["Required"] = true;
983         parameter["DataType"] = "String";
984         nlohmann::json::array_t allowed;
985         allowed.emplace_back("On");
986         parameter["AllowableValues"] = std::move(allowed);
987         parameters.emplace_back(std::move(parameter));
988         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
989         });
990 }
991 
992 inline void handleHypervisorSystemResetPost(
993     App& app, const crow::Request& req,
994     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
995 {
996     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
997     {
998         return;
999     }
1000     std::optional<std::string> resetType;
1001     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
1002     {
1003         // readJson adds appropriate error to response
1004         return;
1005     }
1006 
1007     if (!resetType)
1008     {
1009         messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset",
1010                                          "ResetType");
1011         return;
1012     }
1013 
1014     // Hypervisor object only support On operation
1015     if (resetType != "On")
1016     {
1017         messages::propertyValueNotInList(asyncResp->res, *resetType,
1018                                          "ResetType");
1019         return;
1020     }
1021 
1022     std::string command = "xyz.openbmc_project.State.Host.Transition.On";
1023 
1024     crow::connections::systemBus->async_method_call(
1025         [asyncResp, resetType](const boost::system::error_code& ec) {
1026         if (ec)
1027         {
1028             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1029             if (ec.value() == boost::asio::error::invalid_argument)
1030             {
1031                 messages::actionParameterNotSupported(asyncResp->res,
1032                                                       *resetType, "Reset");
1033                 return;
1034             }
1035 
1036             if (ec.value() == boost::asio::error::host_unreachable)
1037             {
1038                 messages::resourceNotFound(asyncResp->res, "Actions", "Reset");
1039                 return;
1040             }
1041 
1042             messages::internalError(asyncResp->res);
1043             return;
1044         }
1045         messages::success(asyncResp->res);
1046         },
1047         "xyz.openbmc_project.State.Hypervisor",
1048         "/xyz/openbmc_project/state/hypervisor0",
1049         "org.freedesktop.DBus.Properties", "Set",
1050         "xyz.openbmc_project.State.Host", "RequestedHostTransition",
1051         dbus::utility::DbusVariantType{std::move(command)});
1052 }
1053 
1054 inline void requestRoutesHypervisorSystems(App& app)
1055 {
1056     /**
1057      * HypervisorInterfaceCollection class to handle the GET and PATCH on
1058      * Hypervisor Interface
1059      */
1060 
1061     BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/")
1062         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1063         .methods(boost::beast::http::verb::get)(std::bind_front(
1064             handleHypervisorEthernetInterfaceCollectionGet, std::ref(app)));
1065 
1066     BMCWEB_ROUTE(app,
1067                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
1068         .privileges(redfish::privileges::getEthernetInterface)
1069         .methods(boost::beast::http::verb::get)(std::bind_front(
1070             handleHypervisorEthernetInterfaceGet, std::ref(app)));
1071 
1072     BMCWEB_ROUTE(app,
1073                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
1074         .privileges(redfish::privileges::patchEthernetInterface)
1075         .methods(boost::beast::http::verb::patch)(std::bind_front(
1076             handleHypervisorEthernetInterfacePatch, std::ref(app)));
1077 
1078     BMCWEB_ROUTE(app,
1079                  "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset/")
1080         .privileges(redfish::privileges::postComputerSystem)
1081         .methods(boost::beast::http::verb::post)(
1082             std::bind_front(handleHypervisorSystemResetPost, std::ref(app)));
1083 }
1084 } // namespace redfish
1085