xref: /openbmc/bmcweb/features/redfish/lib/hypervisor_system.hpp (revision bd79bce8c3f1deb1fb2773868b9ece25233cf27b)
1d5afb2caSAndrew Geissler #pragma once
2d5afb2caSAndrew Geissler 
33ccb3adbSEd Tanous #include "app.hpp"
43ccb3adbSEd Tanous #include "dbus_singleton.hpp"
57a1dbc48SGeorge Liu #include "dbus_utility.hpp"
63ccb3adbSEd Tanous #include "error_messages.hpp"
73ccb3adbSEd Tanous #include "ethernet.hpp"
8539d8c6bSEd Tanous #include "generated/enums/action_info.hpp"
9539d8c6bSEd Tanous #include "generated/enums/computer_system.hpp"
10539d8c6bSEd Tanous #include "generated/enums/resource.hpp"
113ccb3adbSEd Tanous #include "query.hpp"
123ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
13033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
143ccb3adbSEd Tanous #include "utils/json_utils.hpp"
15033f1e4dSEd Tanous 
16ef4c65b7SEd Tanous #include <boost/url/format.hpp>
171e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
18d5afb2caSAndrew Geissler 
197a1dbc48SGeorge Liu #include <array>
20d5afb2caSAndrew Geissler #include <optional>
217a1dbc48SGeorge Liu #include <string_view>
22d5afb2caSAndrew Geissler #include <utility>
23d5afb2caSAndrew Geissler 
2488a8a174SEd Tanous namespace redfish
25d5afb2caSAndrew Geissler {
26d5afb2caSAndrew Geissler 
27d5afb2caSAndrew Geissler /**
28cc0bb6f2SAndrew Geissler  * @brief Retrieves hypervisor state properties over dbus
29cc0bb6f2SAndrew Geissler  *
30cc0bb6f2SAndrew Geissler  * The hypervisor state object is optional so this function will only set the
31cc0bb6f2SAndrew Geissler  * state variables if the object is found
32cc0bb6f2SAndrew Geissler  *
33ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
34cc0bb6f2SAndrew Geissler  *
35cc0bb6f2SAndrew Geissler  * @return None.
36cc0bb6f2SAndrew Geissler  */
37ac106bf6SEd Tanous inline void
38ac106bf6SEd Tanous     getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
39cc0bb6f2SAndrew Geissler {
4062598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get hypervisor state information.");
411e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
421e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor",
431e1e598dSJonathan Doman         "/xyz/openbmc_project/state/hypervisor0",
441e1e598dSJonathan Doman         "xyz.openbmc_project.State.Host", "CurrentHostState",
45ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
461e1e598dSJonathan Doman                     const std::string& hostState) {
47cc0bb6f2SAndrew Geissler             if (ec)
48cc0bb6f2SAndrew Geissler             {
4962598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
50cc0bb6f2SAndrew Geissler                 // This is an optional D-Bus object so just return if
51cc0bb6f2SAndrew Geissler                 // error occurs
52cc0bb6f2SAndrew Geissler                 return;
53cc0bb6f2SAndrew Geissler             }
54cc0bb6f2SAndrew Geissler 
5562598e31SEd Tanous             BMCWEB_LOG_DEBUG("Hypervisor state: {}", hostState);
56cc0bb6f2SAndrew Geissler             // Verify Host State
571e1e598dSJonathan Doman             if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
58cc0bb6f2SAndrew Geissler             {
59*bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
60*bd79bce8SPatrick Williams                     resource::PowerState::On;
61539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
62539d8c6bSEd Tanous                     resource::State::Enabled;
63cc0bb6f2SAndrew Geissler             }
641e1e598dSJonathan Doman             else if (hostState == "xyz.openbmc_project.State.Host.HostState."
651e1e598dSJonathan Doman                                   "Quiesced")
66cc0bb6f2SAndrew Geissler             {
67*bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
68*bd79bce8SPatrick Williams                     resource::PowerState::On;
69539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
70539d8c6bSEd Tanous                     resource::State::Quiesced;
71cc0bb6f2SAndrew Geissler             }
721e1e598dSJonathan Doman             else if (hostState == "xyz.openbmc_project.State.Host.HostState."
731e1e598dSJonathan Doman                                   "Standby")
74cc0bb6f2SAndrew Geissler             {
75*bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
76*bd79bce8SPatrick Williams                     resource::PowerState::On;
77539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
78539d8c6bSEd Tanous                     resource::State::StandbyOffline;
79cc0bb6f2SAndrew Geissler             }
801e1e598dSJonathan Doman             else if (hostState == "xyz.openbmc_project.State.Host.HostState."
811e1e598dSJonathan Doman                                   "TransitioningToRunning")
82cc0bb6f2SAndrew Geissler             {
83539d8c6bSEd Tanous                 asyncResp->res.jsonValue["PowerState"] =
84539d8c6bSEd Tanous                     resource::PowerState::PoweringOn;
85539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
86539d8c6bSEd Tanous                     resource::State::Starting;
87cc0bb6f2SAndrew Geissler             }
881e1e598dSJonathan Doman             else if (hostState == "xyz.openbmc_project.State.Host.HostState."
891e1e598dSJonathan Doman                                   "TransitioningToOff")
90cc0bb6f2SAndrew Geissler             {
91539d8c6bSEd Tanous                 asyncResp->res.jsonValue["PowerState"] =
92539d8c6bSEd Tanous                     resource::PowerState::PoweringOff;
93539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
94539d8c6bSEd Tanous                     resource::State::Enabled;
95cc0bb6f2SAndrew Geissler             }
96*bd79bce8SPatrick Williams             else if (hostState ==
97*bd79bce8SPatrick Williams                      "xyz.openbmc_project.State.Host.HostState.Off")
98cc0bb6f2SAndrew Geissler             {
99*bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["PowerState"] =
100*bd79bce8SPatrick Williams                     resource::PowerState::Off;
101539d8c6bSEd Tanous                 asyncResp->res.jsonValue["Status"]["State"] =
102539d8c6bSEd Tanous                     resource::State::Disabled;
103cc0bb6f2SAndrew Geissler             }
104cc0bb6f2SAndrew Geissler             else
105cc0bb6f2SAndrew Geissler             {
106ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
107cc0bb6f2SAndrew Geissler                 return;
108cc0bb6f2SAndrew Geissler             }
1091e1e598dSJonathan Doman         });
110cc0bb6f2SAndrew Geissler }
111cc0bb6f2SAndrew Geissler 
112cc0bb6f2SAndrew Geissler /**
1134fbaf64aSAndrew Geissler  * @brief Populate Actions if any are valid for hypervisor object
1144fbaf64aSAndrew Geissler  *
1154fbaf64aSAndrew Geissler  * The hypervisor state object is optional so this function will only set the
1164fbaf64aSAndrew Geissler  * Action if the object is found
1174fbaf64aSAndrew Geissler  *
118ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
1194fbaf64aSAndrew Geissler  *
1204fbaf64aSAndrew Geissler  * @return None.
1214fbaf64aSAndrew Geissler  */
1228d1b46d7Szhanghch05 inline void
123ac106bf6SEd Tanous     getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1244fbaf64aSAndrew Geissler {
12562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get hypervisor actions.");
1262b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1272b73119cSGeorge Liu         "xyz.openbmc_project.State.Host"};
1282b73119cSGeorge Liu     dbus::utility::getDbusObject(
1292b73119cSGeorge Liu         "/xyz/openbmc_project/state/hypervisor0", interfaces,
130ac106bf6SEd Tanous         [asyncResp](
1312b73119cSGeorge Liu             const boost::system::error_code& ec,
1324fbaf64aSAndrew Geissler             const std::vector<std::pair<std::string, std::vector<std::string>>>&
1334fbaf64aSAndrew Geissler                 objInfo) {
1344fbaf64aSAndrew Geissler             if (ec)
1354fbaf64aSAndrew Geissler             {
13662598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1374fbaf64aSAndrew Geissler                 // This is an optional D-Bus object so just return if
1384fbaf64aSAndrew Geissler                 // error occurs
1394fbaf64aSAndrew Geissler                 return;
1404fbaf64aSAndrew Geissler             }
1414fbaf64aSAndrew Geissler 
14226f6976fSEd Tanous             if (objInfo.empty())
1434fbaf64aSAndrew Geissler             {
1444fbaf64aSAndrew Geissler                 // As noted above, this is an optional interface so just return
1454fbaf64aSAndrew Geissler                 // if there is no instance found
1464fbaf64aSAndrew Geissler                 return;
1474fbaf64aSAndrew Geissler             }
1484fbaf64aSAndrew Geissler 
1494fbaf64aSAndrew Geissler             if (objInfo.size() > 1)
1504fbaf64aSAndrew Geissler             {
1514fbaf64aSAndrew Geissler                 // More then one hypervisor object is not supported and is an
1524fbaf64aSAndrew Geissler                 // error
153ac106bf6SEd Tanous                 messages::internalError(asyncResp->res);
1544fbaf64aSAndrew Geissler                 return;
1554fbaf64aSAndrew Geissler             }
1564fbaf64aSAndrew Geissler 
1574fbaf64aSAndrew Geissler             // Object present so system support limited ComputerSystem Action
158613dabeaSEd Tanous             nlohmann::json& reset =
159ac106bf6SEd Tanous                 asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"];
160613dabeaSEd Tanous             reset["target"] =
161613dabeaSEd Tanous                 "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset";
162613dabeaSEd Tanous             reset["@Redfish.ActionInfo"] =
163613dabeaSEd Tanous                 "/redfish/v1/Systems/hypervisor/ResetActionInfo";
1642b73119cSGeorge Liu         });
1654fbaf64aSAndrew Geissler }
1664fbaf64aSAndrew Geissler 
167d5afb2caSAndrew Geissler inline bool extractHypervisorInterfaceData(
168711ac7a9SEd Tanous     const std::string& ethIfaceId,
169711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
17077179532SEd Tanous     EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config)
171d5afb2caSAndrew Geissler {
172d5afb2caSAndrew Geissler     bool idFound = false;
173d5afb2caSAndrew Geissler     for (const auto& objpath : dbusData)
174d5afb2caSAndrew Geissler     {
175d5afb2caSAndrew Geissler         for (const auto& ifacePair : objpath.second)
176d5afb2caSAndrew Geissler         {
177d5afb2caSAndrew Geissler             if (objpath.first ==
178d5afb2caSAndrew Geissler                 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId)
179d5afb2caSAndrew Geissler             {
180d5afb2caSAndrew Geissler                 idFound = true;
181d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
182d5afb2caSAndrew Geissler                 {
183d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
184d5afb2caSAndrew Geissler                     {
185d5afb2caSAndrew Geissler                         if (propertyPair.first == "MACAddress")
186d5afb2caSAndrew Geissler                         {
187d5afb2caSAndrew Geissler                             const std::string* mac =
188d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
189d5afb2caSAndrew Geissler                             if (mac != nullptr)
190d5afb2caSAndrew Geissler                             {
19182695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
192d5afb2caSAndrew Geissler                             }
193d5afb2caSAndrew Geissler                         }
194d5afb2caSAndrew Geissler                     }
195d5afb2caSAndrew Geissler                 }
196d5afb2caSAndrew Geissler                 else if (ifacePair.first ==
197d5afb2caSAndrew Geissler                          "xyz.openbmc_project.Network.EthernetInterface")
198d5afb2caSAndrew Geissler                 {
199d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
200d5afb2caSAndrew Geissler                     {
201d5afb2caSAndrew Geissler                         if (propertyPair.first == "DHCPEnabled")
202d5afb2caSAndrew Geissler                         {
203d5afb2caSAndrew Geissler                             const std::string* dhcp =
204d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
205d5afb2caSAndrew Geissler                             if (dhcp != nullptr)
206d5afb2caSAndrew Geissler                             {
20782695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcp;
208d5afb2caSAndrew Geissler                                 break; // Interested on only "DHCPEnabled".
209d5afb2caSAndrew Geissler                                        // Stop parsing since we got the
210d5afb2caSAndrew Geissler                                        // "DHCPEnabled" value.
211d5afb2caSAndrew Geissler                             }
212d5afb2caSAndrew Geissler                         }
213d5afb2caSAndrew Geissler                     }
214d5afb2caSAndrew Geissler                 }
215d5afb2caSAndrew Geissler             }
216d5afb2caSAndrew Geissler             if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" +
217d5afb2caSAndrew Geissler                                      ethIfaceId + "/ipv4/addr0")
218d5afb2caSAndrew Geissler             {
21977179532SEd Tanous                 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
220d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Object.Enable")
221d5afb2caSAndrew Geissler                 {
2229eb808c1SEd Tanous                     for (const auto& property : ifacePair.second)
223d5afb2caSAndrew Geissler                     {
224d5afb2caSAndrew Geissler                         if (property.first == "Enabled")
225d5afb2caSAndrew Geissler                         {
226d5afb2caSAndrew Geissler                             const bool* intfEnable =
227d5afb2caSAndrew Geissler                                 std::get_if<bool>(&property.second);
228d5afb2caSAndrew Geissler                             if (intfEnable != nullptr)
229d5afb2caSAndrew Geissler                             {
230d5afb2caSAndrew Geissler                                 ipv4Address.isActive = *intfEnable;
231d5afb2caSAndrew Geissler                                 break;
232d5afb2caSAndrew Geissler                             }
233d5afb2caSAndrew Geissler                         }
234d5afb2caSAndrew Geissler                     }
235d5afb2caSAndrew Geissler                 }
236d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Network.IP")
237d5afb2caSAndrew Geissler                 {
2389eb808c1SEd Tanous                     for (const auto& property : ifacePair.second)
239d5afb2caSAndrew Geissler                     {
240d5afb2caSAndrew Geissler                         if (property.first == "Address")
241d5afb2caSAndrew Geissler                         {
242d5afb2caSAndrew Geissler                             const std::string* address =
243d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&property.second);
244d5afb2caSAndrew Geissler                             if (address != nullptr)
245d5afb2caSAndrew Geissler                             {
246d5afb2caSAndrew Geissler                                 ipv4Address.address = *address;
247d5afb2caSAndrew Geissler                             }
248d5afb2caSAndrew Geissler                         }
249d5afb2caSAndrew Geissler                         else if (property.first == "Origin")
250d5afb2caSAndrew Geissler                         {
251d5afb2caSAndrew Geissler                             const std::string* origin =
252d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&property.second);
253d5afb2caSAndrew Geissler                             if (origin != nullptr)
254d5afb2caSAndrew Geissler                             {
255d5afb2caSAndrew Geissler                                 ipv4Address.origin =
256d5afb2caSAndrew Geissler                                     translateAddressOriginDbusToRedfish(*origin,
257d5afb2caSAndrew Geissler                                                                         true);
258d5afb2caSAndrew Geissler                             }
259d5afb2caSAndrew Geissler                         }
260d5afb2caSAndrew Geissler                         else if (property.first == "PrefixLength")
261d5afb2caSAndrew Geissler                         {
262d5afb2caSAndrew Geissler                             const uint8_t* mask =
263d5afb2caSAndrew Geissler                                 std::get_if<uint8_t>(&property.second);
264d5afb2caSAndrew Geissler                             if (mask != nullptr)
265d5afb2caSAndrew Geissler                             {
266d5afb2caSAndrew Geissler                                 // convert it to the string
267d5afb2caSAndrew Geissler                                 ipv4Address.netmask = getNetmask(*mask);
268d5afb2caSAndrew Geissler                             }
269d5afb2caSAndrew Geissler                         }
270889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
271889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
272889ff694SAsmitha Karunanithi                         {
273889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
274889ff694SAsmitha Karunanithi                             continue;
275889ff694SAsmitha Karunanithi                         }
276d5afb2caSAndrew Geissler                         else
277d5afb2caSAndrew Geissler                         {
27862598e31SEd Tanous                             BMCWEB_LOG_ERROR(
27962598e31SEd Tanous                                 "Got extra property: {} on the {} object",
28062598e31SEd Tanous                                 property.first, objpath.first.str);
281d5afb2caSAndrew Geissler                         }
282d5afb2caSAndrew Geissler                     }
283d5afb2caSAndrew Geissler                 }
284d5afb2caSAndrew Geissler             }
285d5afb2caSAndrew Geissler             if (objpath.first == "/xyz/openbmc_project/network/hypervisor")
286d5afb2caSAndrew Geissler             {
287d5afb2caSAndrew Geissler                 // System configuration shows up in the global namespace, so no
288d5afb2caSAndrew Geissler                 // need to check eth number
289d5afb2caSAndrew Geissler                 if (ifacePair.first ==
290d5afb2caSAndrew Geissler                     "xyz.openbmc_project.Network.SystemConfiguration")
291d5afb2caSAndrew Geissler                 {
292d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
293d5afb2caSAndrew Geissler                     {
294d5afb2caSAndrew Geissler                         if (propertyPair.first == "HostName")
295d5afb2caSAndrew Geissler                         {
296d5afb2caSAndrew Geissler                             const std::string* hostName =
297d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
298d5afb2caSAndrew Geissler                             if (hostName != nullptr)
299d5afb2caSAndrew Geissler                             {
30082695a5bSJiaqing Zhao                                 ethData.hostName = *hostName;
301d5afb2caSAndrew Geissler                             }
302d5afb2caSAndrew Geissler                         }
303d5afb2caSAndrew Geissler                         else if (propertyPair.first == "DefaultGateway")
304d5afb2caSAndrew Geissler                         {
305d5afb2caSAndrew Geissler                             const std::string* defaultGateway =
306d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
307d5afb2caSAndrew Geissler                             if (defaultGateway != nullptr)
308d5afb2caSAndrew Geissler                             {
30982695a5bSJiaqing Zhao                                 ethData.defaultGateway = *defaultGateway;
310d5afb2caSAndrew Geissler                             }
311d5afb2caSAndrew Geissler                         }
312d5afb2caSAndrew Geissler                     }
313d5afb2caSAndrew Geissler                 }
314d5afb2caSAndrew Geissler             }
315d5afb2caSAndrew Geissler         }
316d5afb2caSAndrew Geissler     }
317d5afb2caSAndrew Geissler     return idFound;
318d5afb2caSAndrew Geissler }
319d5afb2caSAndrew Geissler /**
320d5afb2caSAndrew Geissler  * Function that retrieves all properties for given Hypervisor Ethernet
321d5afb2caSAndrew Geissler  * Interface Object from Settings Manager
322d5afb2caSAndrew Geissler  * @param ethIfaceId Hypervisor ethernet interface id to query on DBus
323d5afb2caSAndrew Geissler  * @param callback a function that shall be called to convert Dbus output
324d5afb2caSAndrew Geissler  * into JSON
325d5afb2caSAndrew Geissler  */
326d5afb2caSAndrew Geissler template <typename CallbackFunc>
327d5afb2caSAndrew Geissler void getHypervisorIfaceData(const std::string& ethIfaceId,
328d5afb2caSAndrew Geissler                             CallbackFunc&& callback)
329d5afb2caSAndrew Geissler {
3305eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
3315eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3325eb468daSGeorge Liu         "xyz.openbmc_project.Settings", path,
333f94c4ecfSEd Tanous         [ethIfaceId{std::string{ethIfaceId}},
3348cb2c024SEd Tanous          callback = std::forward<CallbackFunc>(callback)](
3358b24275dSEd Tanous             const boost::system::error_code& ec,
33621fe928bSEd Tanous             const dbus::utility::ManagedObjectType& resp) mutable {
337d5afb2caSAndrew Geissler             EthernetInterfaceData ethData{};
33877179532SEd Tanous             std::vector<IPv4AddressData> ipv4Data;
3398b24275dSEd Tanous             if (ec)
340d5afb2caSAndrew Geissler             {
341d5afb2caSAndrew Geissler                 callback(false, ethData, ipv4Data);
342d5afb2caSAndrew Geissler                 return;
343d5afb2caSAndrew Geissler             }
344d5afb2caSAndrew Geissler 
345*bd79bce8SPatrick Williams             bool found = extractHypervisorInterfaceData(ethIfaceId, resp,
346*bd79bce8SPatrick Williams                                                         ethData, ipv4Data);
347d5afb2caSAndrew Geissler             if (!found)
348d5afb2caSAndrew Geissler             {
34962598e31SEd Tanous                 BMCWEB_LOG_INFO("Hypervisor Interface not found");
350d5afb2caSAndrew Geissler             }
351d5afb2caSAndrew Geissler             callback(found, ethData, ipv4Data);
3525eb468daSGeorge Liu         });
353d5afb2caSAndrew Geissler }
354d5afb2caSAndrew Geissler 
355d5afb2caSAndrew Geissler /**
356d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface IPAddress DBUS
357d5afb2caSAndrew Geissler  *
358ac106bf6SEd Tanous  * @param[in] asyncResp          Shared pointer for generating response message.
359d5afb2caSAndrew Geissler  * @param[in] ipv4Address    Address from the incoming request
360d5afb2caSAndrew Geissler  * @param[in] ethIfaceId     Hypervisor Interface Id
361d5afb2caSAndrew Geissler  *
362d5afb2caSAndrew Geissler  * @return None.
363d5afb2caSAndrew Geissler  */
364ac106bf6SEd Tanous inline void setHypervisorIPv4Address(
365ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
366ac106bf6SEd Tanous     const std::string& ethIfaceId, const std::string& ipv4Address)
367d5afb2caSAndrew Geissler {
36862598e31SEd Tanous     BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}",
36962598e31SEd Tanous                      ipv4Address, ethIfaceId);
370d82b5e1fSAsmitha Karunanithi 
371*bd79bce8SPatrick Williams     setDbusProperty(
372*bd79bce8SPatrick Williams         asyncResp, "IPv4StaticAddresses/1/Address",
373e93abac6SGinu George         "xyz.openbmc_project.Settings",
374*bd79bce8SPatrick Williams         "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0",
375e93abac6SGinu George         "xyz.openbmc_project.Network.IP", "Address", ipv4Address);
376d5afb2caSAndrew Geissler }
377d5afb2caSAndrew Geissler 
378d5afb2caSAndrew Geissler /**
379d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface SubnetMask DBUS
380d5afb2caSAndrew Geissler  *
381ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
382d5afb2caSAndrew Geissler  * @param[in] subnet    SubnetMask from the incoming request
383d5afb2caSAndrew Geissler  * @param[in] ethIfaceId Hypervisor Interface Id
384d5afb2caSAndrew Geissler  *
385d5afb2caSAndrew Geissler  * @return None.
386d5afb2caSAndrew Geissler  */
3878d1b46d7Szhanghch05 inline void
388ac106bf6SEd Tanous     setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3898d1b46d7Szhanghch05                             const std::string& ethIfaceId, const uint8_t subnet)
390d5afb2caSAndrew Geissler {
39162598e31SEd Tanous     BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet,
39262598e31SEd Tanous                      ethIfaceId);
393d5afb2caSAndrew Geissler 
394*bd79bce8SPatrick Williams     setDbusProperty(
395*bd79bce8SPatrick Williams         asyncResp, "IPv4StaticAddresses/1/SubnetMask",
396e93abac6SGinu George         "xyz.openbmc_project.Settings",
397*bd79bce8SPatrick Williams         "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId + "/ipv4/addr0",
398e93abac6SGinu George         "xyz.openbmc_project.Network.IP", "PrefixLength", subnet);
399d5afb2caSAndrew Geissler }
400d5afb2caSAndrew Geissler 
401d5afb2caSAndrew Geissler /**
402d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface Gateway DBUS
403d5afb2caSAndrew Geissler  *
404ac106bf6SEd Tanous  * @param[in] asyncResp          Shared pointer for generating response message.
405d5afb2caSAndrew Geissler  * @param[in] gateway        Gateway from the incoming request
406d5afb2caSAndrew Geissler  * @param[in] ethIfaceId     Hypervisor Interface Id
407d5afb2caSAndrew Geissler  *
408d5afb2caSAndrew Geissler  * @return None.
409d5afb2caSAndrew Geissler  */
410ac106bf6SEd Tanous inline void setHypervisorIPv4Gateway(
411ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
412d5afb2caSAndrew Geissler     const std::string& gateway)
413d5afb2caSAndrew Geissler {
41462598e31SEd Tanous     BMCWEB_LOG_DEBUG(
41562598e31SEd Tanous         "Setting the DefaultGateway to the last configured gateway");
416d5afb2caSAndrew Geissler 
417e93abac6SGinu George     setDbusProperty(asyncResp, "IPv4StaticAddresses/1/Gateway",
418e93abac6SGinu George                     "xyz.openbmc_project.Settings",
419d82b5e1fSAsmitha Karunanithi                     sdbusplus::message::object_path(
420d82b5e1fSAsmitha Karunanithi                         "/xyz/openbmc_project/network/hypervisor"),
421d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.SystemConfiguration",
422e93abac6SGinu George                     "DefaultGateway", gateway);
423d5afb2caSAndrew Geissler }
424d5afb2caSAndrew Geissler 
425d5afb2caSAndrew Geissler /**
426d5afb2caSAndrew Geissler  * @brief Creates a static IPv4 entry
427d5afb2caSAndrew Geissler  *
428d5afb2caSAndrew Geissler  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
429d5afb2caSAndrew Geissler  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
430d5afb2caSAndrew Geissler  * @param[in] gateway      IPv4 address of this interfaces gateway
431d5afb2caSAndrew Geissler  * @param[in] address      IPv4 address to assign to this interface
432d5afb2caSAndrew Geissler  * @param[io] asyncResp    Response object that will be returned to client
433d5afb2caSAndrew Geissler  *
434d5afb2caSAndrew Geissler  * @return None
435d5afb2caSAndrew Geissler  */
4368d1b46d7Szhanghch05 inline void
4378d1b46d7Szhanghch05     createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength,
4388d1b46d7Szhanghch05                          const std::string& gateway, const std::string& address,
4398d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
440d5afb2caSAndrew Geissler {
441d5afb2caSAndrew Geissler     setHypervisorIPv4Address(asyncResp, ifaceId, address);
442d5afb2caSAndrew Geissler     setHypervisorIPv4Gateway(asyncResp, gateway);
443d5afb2caSAndrew Geissler     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
444d5afb2caSAndrew Geissler }
445d5afb2caSAndrew Geissler 
446d5afb2caSAndrew Geissler /**
447d5afb2caSAndrew Geissler  * @brief Deletes given IPv4 interface
448d5afb2caSAndrew Geissler  *
449d5afb2caSAndrew Geissler  * @param[in] ifaceId     Id of interface whose IP should be deleted
450d5afb2caSAndrew Geissler  * @param[io] asyncResp   Response object that will be returned to client
451d5afb2caSAndrew Geissler  *
452d5afb2caSAndrew Geissler  * @return None
453d5afb2caSAndrew Geissler  */
4548d1b46d7Szhanghch05 inline void
4558d1b46d7Szhanghch05     deleteHypervisorIPv4(const std::string& ifaceId,
4568d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
457d5afb2caSAndrew Geissler {
458d5afb2caSAndrew Geissler     std::string address = "0.0.0.0";
459d5afb2caSAndrew Geissler     std::string gateway = "0.0.0.0";
460d5afb2caSAndrew Geissler     const uint8_t prefixLength = 0;
461d5afb2caSAndrew Geissler     setHypervisorIPv4Address(asyncResp, ifaceId, address);
462d5afb2caSAndrew Geissler     setHypervisorIPv4Gateway(asyncResp, gateway);
463d5afb2caSAndrew Geissler     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
464d5afb2caSAndrew Geissler }
465d5afb2caSAndrew Geissler 
46677179532SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
46777179532SEd Tanous                                const std::string& ifaceId,
468d5afb2caSAndrew Geissler                                const EthernetInterfaceData& ethData,
46977179532SEd Tanous                                const std::vector<IPv4AddressData>& ipv4Data)
470d5afb2caSAndrew Geissler {
471d5afb2caSAndrew Geissler     jsonResponse["Id"] = ifaceId;
472ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
473ef4c65b7SEd Tanous         "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId);
474d5afb2caSAndrew Geissler     jsonResponse["InterfaceEnabled"] = true;
47582695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
476d5afb2caSAndrew Geissler 
47782695a5bSJiaqing Zhao     jsonResponse["HostName"] = ethData.hostName;
478d5afb2caSAndrew Geissler     jsonResponse["DHCPv4"]["DHCPEnabled"] =
47982695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
480d5afb2caSAndrew Geissler 
481d5afb2caSAndrew Geissler     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
482d5afb2caSAndrew Geissler     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
483d5afb2caSAndrew Geissler     ipv4Array = nlohmann::json::array();
484d5afb2caSAndrew Geissler     ipv4StaticArray = nlohmann::json::array();
4859eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
486d5afb2caSAndrew Geissler     {
487d5afb2caSAndrew Geissler         if (ipv4Config.isActive)
488d5afb2caSAndrew Geissler         {
4891476687dSEd Tanous             nlohmann::json::object_t ipv4;
4901476687dSEd Tanous             ipv4["AddressOrigin"] = ipv4Config.origin;
4911476687dSEd Tanous             ipv4["SubnetMask"] = ipv4Config.netmask;
4921476687dSEd Tanous             ipv4["Address"] = ipv4Config.address;
4931476687dSEd Tanous             ipv4["Gateway"] = ethData.defaultGateway;
494d5afb2caSAndrew Geissler 
495d5afb2caSAndrew Geissler             if (ipv4Config.origin == "Static")
496d5afb2caSAndrew Geissler             {
4971476687dSEd Tanous                 ipv4StaticArray.push_back(ipv4);
498d5afb2caSAndrew Geissler             }
499b2ba3072SPatrick Williams             ipv4Array.emplace_back(std::move(ipv4));
500d5afb2caSAndrew Geissler         }
501d5afb2caSAndrew Geissler     }
502d5afb2caSAndrew Geissler }
503d5afb2caSAndrew Geissler 
50477179532SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled,
5058d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
506d5afb2caSAndrew Geissler {
5077e860f15SJohn Edward Broadbent     const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false);
508d82b5e1fSAsmitha Karunanithi 
509e93abac6SGinu George     setDbusProperty(
510e93abac6SGinu George         asyncResp, "DHCPv4/DHCPEnabled", "xyz.openbmc_project.Settings",
511d82b5e1fSAsmitha Karunanithi         sdbusplus::message::object_path(
512d82b5e1fSAsmitha Karunanithi             "/xyz/openbmc_project/network/hypervisor") /
513d82b5e1fSAsmitha Karunanithi             ifaceId,
514e93abac6SGinu George         "xyz.openbmc_project.Network.EthernetInterface", "DHCPEnabled", dhcp);
515d5afb2caSAndrew Geissler 
516d5afb2caSAndrew Geissler     // Set the IPv4 address origin to the DHCP / Static as per the new value
517d5afb2caSAndrew Geissler     // of the DHCPEnabled property
518d5afb2caSAndrew Geissler     std::string origin;
519e05aec50SEd Tanous     if (!ipv4DHCPEnabled)
520d5afb2caSAndrew Geissler     {
521d5afb2caSAndrew Geissler         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
522d5afb2caSAndrew Geissler     }
523d5afb2caSAndrew Geissler     else
524d5afb2caSAndrew Geissler     {
525d5afb2caSAndrew Geissler         // DHCPEnabled is set to true. Delete the current IPv4 settings
526d5afb2caSAndrew Geissler         // to receive the new values from DHCP server.
527d5afb2caSAndrew Geissler         deleteHypervisorIPv4(ifaceId, asyncResp);
528d5afb2caSAndrew Geissler         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
529d5afb2caSAndrew Geissler     }
530d82b5e1fSAsmitha Karunanithi 
531*bd79bce8SPatrick Williams     setDbusProperty(
532*bd79bce8SPatrick Williams         asyncResp, "IPv4StaticAddresses/1/AddressOrigin",
533e93abac6SGinu George         "xyz.openbmc_project.Settings",
534*bd79bce8SPatrick Williams         "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0",
535e93abac6SGinu George         "xyz.openbmc_project.Network.IP", "Origin", origin);
536d5afb2caSAndrew Geissler }
537d5afb2caSAndrew Geissler 
5387e860f15SJohn Edward Broadbent inline void handleHypervisorIPv4StaticPatch(
53921fe928bSEd Tanous     const std::string& ifaceId,
54021fe928bSEd Tanous     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
5417e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
542d5afb2caSAndrew Geissler {
5437e860f15SJohn Edward Broadbent     // Hypervisor considers the first IP address in the array list
5447e860f15SJohn Edward Broadbent     // as the Hypervisor's virtual management interface supports single IPv4
5457e860f15SJohn Edward Broadbent     // address
54621fe928bSEd Tanous     std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson = input[0];
54721fe928bSEd Tanous     nlohmann::json::object_t* obj =
54821fe928bSEd Tanous         std::get_if<nlohmann::json::object_t>(&thisJson);
54921fe928bSEd Tanous     if (obj == nullptr)
5507e860f15SJohn Edward Broadbent     {
55121fe928bSEd Tanous         deleteHypervisorIPv4(ifaceId, asyncResp);
55221fe928bSEd Tanous         return;
55321fe928bSEd Tanous     }
55421fe928bSEd Tanous     if (obj->empty())
55521fe928bSEd Tanous     {
55621fe928bSEd Tanous         return;
55721fe928bSEd Tanous     }
558f8fe53e7SEd Tanous     // For the error string
559f8fe53e7SEd Tanous     std::string pathString = "IPv4StaticAddresses/1";
56021fe928bSEd Tanous     std::string address;
56121fe928bSEd Tanous     std::string subnetMask;
56221fe928bSEd Tanous     std::string gateway;
56321fe928bSEd Tanous     if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address,
56421fe928bSEd Tanous                                    "SubnetMask", subnetMask, "Gateway",
5657e860f15SJohn Edward Broadbent                                    gateway))
5667e860f15SJohn Edward Broadbent     {
5677e860f15SJohn Edward Broadbent         return;
5687e860f15SJohn Edward Broadbent     }
5697e860f15SJohn Edward Broadbent 
5707e860f15SJohn Edward Broadbent     uint8_t prefixLength = 0;
57121fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(address))
5727e860f15SJohn Edward Broadbent     {
57321fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, address,
5747e860f15SJohn Edward Broadbent                                            pathString + "/Address");
57521fe928bSEd Tanous         return;
5767e860f15SJohn Edward Broadbent     }
5777e860f15SJohn Edward Broadbent 
57821fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(subnetMask, &prefixLength))
5797e860f15SJohn Edward Broadbent     {
58021fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, subnetMask,
5817e860f15SJohn Edward Broadbent                                            pathString + "/SubnetMask");
58221fe928bSEd Tanous         return;
5837e860f15SJohn Edward Broadbent     }
5847e860f15SJohn Edward Broadbent 
58521fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(gateway))
5867e860f15SJohn Edward Broadbent     {
58721fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, gateway,
5887e860f15SJohn Edward Broadbent                                            pathString + "/Gateway");
5897e860f15SJohn Edward Broadbent         return;
5907e860f15SJohn Edward Broadbent     }
5917e860f15SJohn Edward Broadbent 
59262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId,
59321fe928bSEd Tanous                      address);
59421fe928bSEd Tanous     createHypervisorIPv4(ifaceId, prefixLength, gateway, address, asyncResp);
5957e860f15SJohn Edward Broadbent     // Set the DHCPEnabled to false since the Static IPv4 is set
5967e860f15SJohn Edward Broadbent     setDHCPEnabled(ifaceId, false, asyncResp);
5977e860f15SJohn Edward Broadbent }
5987e860f15SJohn Edward Broadbent 
59988a8a174SEd Tanous inline void handleHypervisorHostnamePatch(
60088a8a174SEd Tanous     const std::string& hostName,
6017e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6027e860f15SJohn Edward Broadbent {
6037e860f15SJohn Edward Broadbent     if (!isHostnameValid(hostName))
6047e860f15SJohn Edward Broadbent     {
6057e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(asyncResp->res, hostName,
6067e860f15SJohn Edward Broadbent                                            "HostName");
6077e860f15SJohn Edward Broadbent         return;
6087e860f15SJohn Edward Broadbent     }
6097e860f15SJohn Edward Broadbent 
6107e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["HostName"] = hostName;
611e93abac6SGinu George     setDbusProperty(asyncResp, "HostName", "xyz.openbmc_project.Settings",
612d82b5e1fSAsmitha Karunanithi                     sdbusplus::message::object_path(
613d82b5e1fSAsmitha Karunanithi                         "/xyz/openbmc_project/network/hypervisor"),
614d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.SystemConfiguration",
615e93abac6SGinu George                     "HostName", hostName);
6167e860f15SJohn Edward Broadbent }
6177e860f15SJohn Edward Broadbent 
6187e860f15SJohn Edward Broadbent inline void
61977179532SEd Tanous     setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive,
6207e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6217e860f15SJohn Edward Broadbent {
622e93abac6SGinu George     setDbusProperty(
623e93abac6SGinu George         asyncResp, "InterfaceEnabled", "xyz.openbmc_project.Settings",
624e93abac6SGinu George         "/xyz/openbmc_project/network/hypervisor/" + ifaceId + "/ipv4/addr0",
625e93abac6SGinu George         "xyz.openbmc_project.Object.Enable", "Enabled", isActive);
6267e860f15SJohn Edward Broadbent }
6277e860f15SJohn Edward Broadbent 
628f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceCollectionGet(
629f40448e5SGunnar Mills     App& app, const crow::Request& req,
630f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
631f40448e5SGunnar Mills {
632f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
633f40448e5SGunnar Mills     {
634f40448e5SGunnar Mills         return;
635f40448e5SGunnar Mills     }
636f40448e5SGunnar Mills     constexpr std::array<std::string_view, 1> interfaces = {
637f40448e5SGunnar Mills         "xyz.openbmc_project.Network.EthernetInterface"};
638f40448e5SGunnar Mills 
639f40448e5SGunnar Mills     dbus::utility::getSubTreePaths(
640f40448e5SGunnar Mills         "/xyz/openbmc_project/network/hypervisor", 0, interfaces,
641f40448e5SGunnar Mills         [asyncResp](
6428b24275dSEd Tanous             const boost::system::error_code& ec,
643f40448e5SGunnar Mills             const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) {
6448b24275dSEd Tanous             if (ec)
645f40448e5SGunnar Mills             {
646*bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "System",
647*bd79bce8SPatrick Williams                                            "hypervisor");
648f40448e5SGunnar Mills                 return;
649f40448e5SGunnar Mills             }
650f40448e5SGunnar Mills             asyncResp->res.jsonValue["@odata.type"] =
651f40448e5SGunnar Mills                 "#EthernetInterfaceCollection."
652f40448e5SGunnar Mills                 "EthernetInterfaceCollection";
653f40448e5SGunnar Mills             asyncResp->res.jsonValue["@odata.id"] =
654f40448e5SGunnar Mills                 "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
655f40448e5SGunnar Mills             asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet "
656f40448e5SGunnar Mills                                                "Interface Collection";
657f40448e5SGunnar Mills             asyncResp->res.jsonValue["Description"] =
658f40448e5SGunnar Mills                 "Collection of Virtual Management "
659f40448e5SGunnar Mills                 "Interfaces for the hypervisor";
660f40448e5SGunnar Mills 
661f40448e5SGunnar Mills             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
662f40448e5SGunnar Mills             ifaceArray = nlohmann::json::array();
663f40448e5SGunnar Mills             for (const std::string& iface : ifaceList)
664f40448e5SGunnar Mills             {
665f40448e5SGunnar Mills                 sdbusplus::message::object_path path(iface);
666f40448e5SGunnar Mills                 std::string name = path.filename();
667f40448e5SGunnar Mills                 if (name.empty())
668f40448e5SGunnar Mills                 {
669f40448e5SGunnar Mills                     continue;
670f40448e5SGunnar Mills                 }
671f40448e5SGunnar Mills                 nlohmann::json::object_t ethIface;
672ef4c65b7SEd Tanous                 ethIface["@odata.id"] = boost::urls::format(
673*bd79bce8SPatrick Williams                     "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}",
674*bd79bce8SPatrick Williams                     name);
675b2ba3072SPatrick Williams                 ifaceArray.emplace_back(std::move(ethIface));
676f40448e5SGunnar Mills             }
677f40448e5SGunnar Mills             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
678f40448e5SGunnar Mills         });
679f40448e5SGunnar Mills }
680f40448e5SGunnar Mills 
681f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceGet(
682f40448e5SGunnar Mills     App& app, const crow::Request& req,
683f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
684f40448e5SGunnar Mills {
685f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
686f40448e5SGunnar Mills     {
687f40448e5SGunnar Mills         return;
688f40448e5SGunnar Mills     }
689f40448e5SGunnar Mills     getHypervisorIfaceData(
690f40448e5SGunnar Mills         id, [asyncResp, ifaceId{std::string(id)}](
69177179532SEd Tanous                 bool success, const EthernetInterfaceData& ethData,
69277179532SEd Tanous                 const std::vector<IPv4AddressData>& ipv4Data) {
693f40448e5SGunnar Mills             if (!success)
694f40448e5SGunnar Mills             {
695f40448e5SGunnar Mills                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
696f40448e5SGunnar Mills                                            ifaceId);
697f40448e5SGunnar Mills                 return;
698f40448e5SGunnar Mills             }
699f40448e5SGunnar Mills             asyncResp->res.jsonValue["@odata.type"] =
70093bbc953SJiaqing Zhao                 "#EthernetInterface.v1_9_0.EthernetInterface";
701f40448e5SGunnar Mills             asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface";
702f40448e5SGunnar Mills             asyncResp->res.jsonValue["Description"] =
703f40448e5SGunnar Mills                 "Hypervisor's Virtual Management Ethernet Interface";
704f40448e5SGunnar Mills             parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData,
705f40448e5SGunnar Mills                                ipv4Data);
706f40448e5SGunnar Mills         });
707f40448e5SGunnar Mills }
708f40448e5SGunnar Mills 
7098d8c30c3SGunnar Mills inline void handleHypervisorSystemGet(
7108d8c30c3SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7118d8c30c3SGunnar Mills {
7128d8c30c3SGunnar Mills     sdbusplus::asio::getProperty<std::string>(
7138d8c30c3SGunnar Mills         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
7148d8c30c3SGunnar Mills         "/xyz/openbmc_project/network/hypervisor",
7158d8c30c3SGunnar Mills         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
7168d8c30c3SGunnar Mills         [asyncResp](const boost::system::error_code& ec,
7178d8c30c3SGunnar Mills                     const std::string& /*hostName*/) {
7188d8c30c3SGunnar Mills             if (ec)
7198d8c30c3SGunnar Mills             {
720*bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "System",
721*bd79bce8SPatrick Williams                                            "hypervisor");
7228d8c30c3SGunnar Mills                 return;
7238d8c30c3SGunnar Mills             }
72462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Hypervisor is available");
7258d8c30c3SGunnar Mills 
7268d8c30c3SGunnar Mills             asyncResp->res.jsonValue["@odata.type"] =
7278d8c30c3SGunnar Mills                 "#ComputerSystem.v1_6_0.ComputerSystem";
7288d8c30c3SGunnar Mills             asyncResp->res.jsonValue["@odata.id"] =
7298d8c30c3SGunnar Mills                 "/redfish/v1/Systems/hypervisor";
7308d8c30c3SGunnar Mills             asyncResp->res.jsonValue["Description"] = "Hypervisor";
7318d8c30c3SGunnar Mills             asyncResp->res.jsonValue["Name"] = "Hypervisor";
7328d8c30c3SGunnar Mills             asyncResp->res.jsonValue["Id"] = "hypervisor";
733539d8c6bSEd Tanous             asyncResp->res.jsonValue["SystemType"] =
734539d8c6bSEd Tanous                 computer_system::SystemType::OS;
7358d8c30c3SGunnar Mills             nlohmann::json::array_t managedBy;
7368d8c30c3SGunnar Mills             nlohmann::json::object_t manager;
737253f11b8SEd Tanous             manager["@odata.id"] = boost::urls::format(
738253f11b8SEd Tanous                 "/redfish/v1/Managers/{}", BMCWEB_REDFISH_MANAGER_URI_NAME);
739ad539545SPatrick Williams             managedBy.emplace_back(std::move(manager));
740*bd79bce8SPatrick Williams             asyncResp->res.jsonValue["Links"]["ManagedBy"] =
741*bd79bce8SPatrick Williams                 std::move(managedBy);
7428d8c30c3SGunnar Mills             asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
7438d8c30c3SGunnar Mills                 "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
7448d8c30c3SGunnar Mills             getHypervisorState(asyncResp);
7458d8c30c3SGunnar Mills             getHypervisorActions(asyncResp);
7468d8c30c3SGunnar Mills             // TODO: Add "SystemType" : "hypervisor"
7478d8c30c3SGunnar Mills         });
7488d8c30c3SGunnar Mills }
7498d8c30c3SGunnar Mills 
750f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfacePatch(
751f40448e5SGunnar Mills     App& app, const crow::Request& req,
752f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
753f40448e5SGunnar Mills     const std::string& ifaceId)
754f40448e5SGunnar Mills {
755f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
756f40448e5SGunnar Mills     {
757f40448e5SGunnar Mills         return;
758f40448e5SGunnar Mills     }
759f40448e5SGunnar Mills     std::optional<std::string> hostName;
76021fe928bSEd Tanous     std::optional<
76121fe928bSEd Tanous         std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
76221fe928bSEd Tanous         ipv4StaticAddresses;
76321fe928bSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> ipv4Addresses;
764f40448e5SGunnar Mills     std::optional<bool> ipv4DHCPEnabled;
765f40448e5SGunnar Mills 
766*bd79bce8SPatrick Williams     if (!json_util::readJsonPatch(
767*bd79bce8SPatrick Williams             req, asyncResp->res, "HostName", hostName, "IPv4StaticAddresses",
768*bd79bce8SPatrick Williams             ipv4StaticAddresses, "IPv4Addresses", ipv4Addresses,
76921fe928bSEd Tanous             "DHCPv4/DHCPEnabled", ipv4DHCPEnabled))
770f40448e5SGunnar Mills     {
771f40448e5SGunnar Mills         return;
772f40448e5SGunnar Mills     }
773f40448e5SGunnar Mills 
774f40448e5SGunnar Mills     if (ipv4Addresses)
775f40448e5SGunnar Mills     {
776f40448e5SGunnar Mills         messages::propertyNotWritable(asyncResp->res, "IPv4Addresses");
777f40448e5SGunnar Mills         return;
778f40448e5SGunnar Mills     }
779f40448e5SGunnar Mills 
780f40448e5SGunnar Mills     getHypervisorIfaceData(
78121fe928bSEd Tanous         ifaceId,
78221fe928bSEd Tanous         [asyncResp, ifaceId, hostName = std::move(hostName),
7835a39f77aSPatrick Williams          ipv4StaticAddresses = std::move(ipv4StaticAddresses),
78421fe928bSEd Tanous          ipv4DHCPEnabled](bool success, const EthernetInterfaceData& ethData,
78521fe928bSEd Tanous                           const std::vector<IPv4AddressData>&) mutable {
786f40448e5SGunnar Mills             if (!success)
787f40448e5SGunnar Mills             {
788f40448e5SGunnar Mills                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
789f40448e5SGunnar Mills                                            ifaceId);
790f40448e5SGunnar Mills                 return;
791f40448e5SGunnar Mills             }
792f40448e5SGunnar Mills 
793f40448e5SGunnar Mills             if (ipv4StaticAddresses)
794f40448e5SGunnar Mills             {
795*bd79bce8SPatrick Williams                 std::vector<std::variant<nlohmann::json::object_t,
796*bd79bce8SPatrick Williams                                          std::nullptr_t>>& ipv4Static =
797*bd79bce8SPatrick Williams                     *ipv4StaticAddresses;
798f40448e5SGunnar Mills                 if (ipv4Static.begin() == ipv4Static.end())
799f40448e5SGunnar Mills                 {
80021fe928bSEd Tanous                     messages::propertyValueTypeError(asyncResp->res,
80121fe928bSEd Tanous                                                      std::vector<std::string>(),
802f40448e5SGunnar Mills                                                      "IPv4StaticAddresses");
803f40448e5SGunnar Mills                     return;
804f40448e5SGunnar Mills                 }
805f40448e5SGunnar Mills 
806f40448e5SGunnar Mills                 // One and only one hypervisor instance supported
807f40448e5SGunnar Mills                 if (ipv4Static.size() != 1)
808f40448e5SGunnar Mills                 {
80921fe928bSEd Tanous                     messages::propertyValueFormatError(asyncResp->res, "[]",
810f40448e5SGunnar Mills                                                        "IPv4StaticAddresses");
811f40448e5SGunnar Mills                     return;
812f40448e5SGunnar Mills                 }
813f40448e5SGunnar Mills 
814*bd79bce8SPatrick Williams                 std::variant<nlohmann::json::object_t, std::nullptr_t>&
815*bd79bce8SPatrick Williams                     ipv4Json = ipv4Static[0];
816f40448e5SGunnar Mills                 // Check if the param is 'null'. If its null, it means
817f40448e5SGunnar Mills                 // that user wants to delete the IP address. Deleting
818f40448e5SGunnar Mills                 // the IP address is allowed only if its statically
819f40448e5SGunnar Mills                 // configured. Deleting the address originated from DHCP
820f40448e5SGunnar Mills                 // is not allowed.
82121fe928bSEd Tanous                 if (std::holds_alternative<std::nullptr_t>(ipv4Json) &&
82221fe928bSEd Tanous                     translateDhcpEnabledToBool(ethData.dhcpEnabled, true))
823f40448e5SGunnar Mills                 {
824*bd79bce8SPatrick Williams                     BMCWEB_LOG_INFO(
825*bd79bce8SPatrick Williams                         "Ignoring the delete on ipv4StaticAddresses "
82662598e31SEd Tanous                         "as the interface is DHCP enabled");
827f40448e5SGunnar Mills                 }
828f40448e5SGunnar Mills                 else
829f40448e5SGunnar Mills                 {
830*bd79bce8SPatrick Williams                     handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static,
831*bd79bce8SPatrick Williams                                                     asyncResp);
832f40448e5SGunnar Mills                 }
833f40448e5SGunnar Mills             }
834f40448e5SGunnar Mills 
835f40448e5SGunnar Mills             if (hostName)
836f40448e5SGunnar Mills             {
837f40448e5SGunnar Mills                 handleHypervisorHostnamePatch(*hostName, asyncResp);
838f40448e5SGunnar Mills             }
839f40448e5SGunnar Mills 
84021fe928bSEd Tanous             if (ipv4DHCPEnabled)
841f40448e5SGunnar Mills             {
842f40448e5SGunnar Mills                 setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp);
843f40448e5SGunnar Mills             }
844f40448e5SGunnar Mills 
845f40448e5SGunnar Mills             // Set this interface to disabled/inactive. This will be set
846f40448e5SGunnar Mills             // to enabled/active by the pldm once the hypervisor
847f40448e5SGunnar Mills             // consumes the updated settings from the user.
848f40448e5SGunnar Mills             setIPv4InterfaceEnabled(ifaceId, false, asyncResp);
849f40448e5SGunnar Mills         });
850f40448e5SGunnar Mills     asyncResp->res.result(boost::beast::http::status::accepted);
851f40448e5SGunnar Mills }
852f40448e5SGunnar Mills 
853a5d0cedbSGunnar Mills inline void handleHypervisorResetActionGet(
854a5d0cedbSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
855a5d0cedbSGunnar Mills {
856a5d0cedbSGunnar Mills     // Only return action info if hypervisor D-Bus object present
857a5d0cedbSGunnar Mills     constexpr std::array<std::string_view, 1> interfaces = {
858a5d0cedbSGunnar Mills         "xyz.openbmc_project.State.Host"};
859a5d0cedbSGunnar Mills     dbus::utility::getDbusObject(
860a5d0cedbSGunnar Mills         "/xyz/openbmc_project/state/hypervisor0", interfaces,
861a5d0cedbSGunnar Mills         [asyncResp](
862a5d0cedbSGunnar Mills             const boost::system::error_code& ec,
863a5d0cedbSGunnar Mills             const std::vector<std::pair<std::string, std::vector<std::string>>>&
864a5d0cedbSGunnar Mills                 objInfo) {
865a5d0cedbSGunnar Mills             if (ec)
866a5d0cedbSGunnar Mills             {
86762598e31SEd Tanous                 BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
868a5d0cedbSGunnar Mills 
869a5d0cedbSGunnar Mills                 // No hypervisor objects found by mapper
870a5d0cedbSGunnar Mills                 if (ec.value() == boost::system::errc::io_error)
871a5d0cedbSGunnar Mills                 {
872a5d0cedbSGunnar Mills                     messages::resourceNotFound(asyncResp->res, "hypervisor",
873a5d0cedbSGunnar Mills                                                "ResetActionInfo");
874a5d0cedbSGunnar Mills                     return;
875a5d0cedbSGunnar Mills                 }
876a5d0cedbSGunnar Mills 
877a5d0cedbSGunnar Mills                 messages::internalError(asyncResp->res);
878a5d0cedbSGunnar Mills                 return;
879a5d0cedbSGunnar Mills             }
880a5d0cedbSGunnar Mills 
881a5d0cedbSGunnar Mills             // One and only one hypervisor instance supported
882a5d0cedbSGunnar Mills             if (objInfo.size() != 1)
883a5d0cedbSGunnar Mills             {
884a5d0cedbSGunnar Mills                 messages::internalError(asyncResp->res);
885a5d0cedbSGunnar Mills                 return;
886a5d0cedbSGunnar Mills             }
887a5d0cedbSGunnar Mills 
888a5d0cedbSGunnar Mills             // The hypervisor object only support the ability to
889a5d0cedbSGunnar Mills             // turn On The system object Action should be utilized
890a5d0cedbSGunnar Mills             // for other operations
891a5d0cedbSGunnar Mills 
892a5d0cedbSGunnar Mills             asyncResp->res.jsonValue["@odata.type"] =
893a5d0cedbSGunnar Mills                 "#ActionInfo.v1_1_2.ActionInfo";
894a5d0cedbSGunnar Mills             asyncResp->res.jsonValue["@odata.id"] =
895a5d0cedbSGunnar Mills                 "/redfish/v1/Systems/hypervisor/ResetActionInfo";
896a5d0cedbSGunnar Mills             asyncResp->res.jsonValue["Name"] = "Reset Action Info";
897a5d0cedbSGunnar Mills             asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
898a5d0cedbSGunnar Mills             nlohmann::json::array_t parameters;
899a5d0cedbSGunnar Mills             nlohmann::json::object_t parameter;
900a5d0cedbSGunnar Mills             parameter["Name"] = "ResetType";
901a5d0cedbSGunnar Mills             parameter["Required"] = true;
902539d8c6bSEd Tanous             parameter["DataType"] = action_info::ParameterTypes::String;
903a5d0cedbSGunnar Mills             nlohmann::json::array_t allowed;
904ad539545SPatrick Williams             allowed.emplace_back("On");
905a5d0cedbSGunnar Mills             parameter["AllowableValues"] = std::move(allowed);
906ad539545SPatrick Williams             parameters.emplace_back(std::move(parameter));
907a5d0cedbSGunnar Mills             asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
908a5d0cedbSGunnar Mills         });
909a5d0cedbSGunnar Mills }
910a5d0cedbSGunnar Mills 
911a5d0cedbSGunnar Mills inline void handleHypervisorSystemResetPost(
912a5d0cedbSGunnar Mills     App& app, const crow::Request& req,
913a5d0cedbSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
914a5d0cedbSGunnar Mills {
915a5d0cedbSGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
916a5d0cedbSGunnar Mills     {
917a5d0cedbSGunnar Mills         return;
918a5d0cedbSGunnar Mills     }
919a5d0cedbSGunnar Mills     std::optional<std::string> resetType;
920a5d0cedbSGunnar Mills     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
921a5d0cedbSGunnar Mills     {
922a5d0cedbSGunnar Mills         // readJson adds appropriate error to response
923a5d0cedbSGunnar Mills         return;
924a5d0cedbSGunnar Mills     }
925a5d0cedbSGunnar Mills 
926a5d0cedbSGunnar Mills     if (!resetType)
927a5d0cedbSGunnar Mills     {
928a5d0cedbSGunnar Mills         messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset",
929a5d0cedbSGunnar Mills                                          "ResetType");
930a5d0cedbSGunnar Mills         return;
931a5d0cedbSGunnar Mills     }
932a5d0cedbSGunnar Mills 
933a5d0cedbSGunnar Mills     // Hypervisor object only support On operation
934a5d0cedbSGunnar Mills     if (resetType != "On")
935a5d0cedbSGunnar Mills     {
936a5d0cedbSGunnar Mills         messages::propertyValueNotInList(asyncResp->res, *resetType,
937a5d0cedbSGunnar Mills                                          "ResetType");
938a5d0cedbSGunnar Mills         return;
939a5d0cedbSGunnar Mills     }
940a5d0cedbSGunnar Mills 
941a5d0cedbSGunnar Mills     std::string command = "xyz.openbmc_project.State.Host.Transition.On";
942a5d0cedbSGunnar Mills 
943*bd79bce8SPatrick Williams     setDbusPropertyAction(
944*bd79bce8SPatrick Williams         asyncResp, "xyz.openbmc_project.State.Hypervisor",
9451827b4f1SAsmitha Karunanithi         sdbusplus::message::object_path(
9461827b4f1SAsmitha Karunanithi             "/xyz/openbmc_project/state/hypervisor0"),
947*bd79bce8SPatrick Williams         "xyz.openbmc_project.State.Host", "RequestedHostTransition",
948*bd79bce8SPatrick Williams         "ResetType", "ComputerSystem.Reset", command);
949a5d0cedbSGunnar Mills }
950a5d0cedbSGunnar Mills 
9517e860f15SJohn Edward Broadbent inline void requestRoutesHypervisorSystems(App& app)
9527e860f15SJohn Edward Broadbent {
9537e860f15SJohn Edward Broadbent     /**
9547e860f15SJohn Edward Broadbent      * HypervisorInterfaceCollection class to handle the GET and PATCH on
9557e860f15SJohn Edward Broadbent      * Hypervisor Interface
9567e860f15SJohn Edward Broadbent      */
9577e860f15SJohn Edward Broadbent 
9587e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/")
959ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
960f40448e5SGunnar Mills         .methods(boost::beast::http::verb::get)(std::bind_front(
961f40448e5SGunnar Mills             handleHypervisorEthernetInterfaceCollectionGet, std::ref(app)));
9627e860f15SJohn Edward Broadbent 
9637e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9647e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
965ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
966f40448e5SGunnar Mills         .methods(boost::beast::http::verb::get)(std::bind_front(
967f40448e5SGunnar Mills             handleHypervisorEthernetInterfaceGet, std::ref(app)));
968d5afb2caSAndrew Geissler 
9697e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9707e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
971ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
972f40448e5SGunnar Mills         .methods(boost::beast::http::verb::patch)(std::bind_front(
973f40448e5SGunnar Mills             handleHypervisorEthernetInterfacePatch, std::ref(app)));
9744fbaf64aSAndrew Geissler 
9757e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9764fbaf64aSAndrew Geissler                  "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset/")
977ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
9787e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
979a5d0cedbSGunnar Mills             std::bind_front(handleHypervisorSystemResetPost, std::ref(app)));
9804fbaf64aSAndrew Geissler }
98188a8a174SEd Tanous } // namespace redfish
982