xref: /openbmc/bmcweb/features/redfish/lib/hypervisor_system.hpp (revision 21fe928bbf699c52b026c7ce54b58cb0e9d57545)
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"
83ccb3adbSEd Tanous #include "query.hpp"
93ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
10033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
113ccb3adbSEd Tanous #include "utils/json_utils.hpp"
12033f1e4dSEd Tanous 
13ef4c65b7SEd Tanous #include <boost/url/format.hpp>
141e1e598dSJonathan Doman #include <sdbusplus/asio/property.hpp>
15d5afb2caSAndrew Geissler 
167a1dbc48SGeorge Liu #include <array>
17d5afb2caSAndrew Geissler #include <optional>
187a1dbc48SGeorge Liu #include <string_view>
19d5afb2caSAndrew Geissler #include <utility>
20d5afb2caSAndrew Geissler 
2188a8a174SEd Tanous namespace redfish
22d5afb2caSAndrew Geissler {
23d5afb2caSAndrew Geissler 
24d5afb2caSAndrew Geissler /**
25cc0bb6f2SAndrew Geissler  * @brief Retrieves hypervisor state properties over dbus
26cc0bb6f2SAndrew Geissler  *
27cc0bb6f2SAndrew Geissler  * The hypervisor state object is optional so this function will only set the
28cc0bb6f2SAndrew Geissler  * state variables if the object is found
29cc0bb6f2SAndrew Geissler  *
30ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
31cc0bb6f2SAndrew Geissler  *
32cc0bb6f2SAndrew Geissler  * @return None.
33cc0bb6f2SAndrew Geissler  */
34ac106bf6SEd Tanous inline void
35ac106bf6SEd Tanous     getHypervisorState(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
36cc0bb6f2SAndrew Geissler {
3762598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get hypervisor state information.");
381e1e598dSJonathan Doman     sdbusplus::asio::getProperty<std::string>(
391e1e598dSJonathan Doman         *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor",
401e1e598dSJonathan Doman         "/xyz/openbmc_project/state/hypervisor0",
411e1e598dSJonathan Doman         "xyz.openbmc_project.State.Host", "CurrentHostState",
42ac106bf6SEd Tanous         [asyncResp](const boost::system::error_code& ec,
431e1e598dSJonathan Doman                     const std::string& hostState) {
44cc0bb6f2SAndrew Geissler         if (ec)
45cc0bb6f2SAndrew Geissler         {
4662598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
47cc0bb6f2SAndrew Geissler             // This is an optional D-Bus object so just return if
48cc0bb6f2SAndrew Geissler             // error occurs
49cc0bb6f2SAndrew Geissler             return;
50cc0bb6f2SAndrew Geissler         }
51cc0bb6f2SAndrew Geissler 
5262598e31SEd Tanous         BMCWEB_LOG_DEBUG("Hypervisor state: {}", hostState);
53cc0bb6f2SAndrew Geissler         // Verify Host State
541e1e598dSJonathan Doman         if (hostState == "xyz.openbmc_project.State.Host.HostState.Running")
55cc0bb6f2SAndrew Geissler         {
56ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
57ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
58cc0bb6f2SAndrew Geissler         }
591e1e598dSJonathan Doman         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
601e1e598dSJonathan Doman                               "Quiesced")
61cc0bb6f2SAndrew Geissler         {
62ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
63ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Quiesced";
64cc0bb6f2SAndrew Geissler         }
651e1e598dSJonathan Doman         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
661e1e598dSJonathan Doman                               "Standby")
67cc0bb6f2SAndrew Geissler         {
68ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "On";
69ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "StandbyOffline";
70cc0bb6f2SAndrew Geissler         }
711e1e598dSJonathan Doman         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
721e1e598dSJonathan Doman                               "TransitioningToRunning")
73cc0bb6f2SAndrew Geissler         {
74ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "PoweringOn";
75ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Starting";
76cc0bb6f2SAndrew Geissler         }
771e1e598dSJonathan Doman         else if (hostState == "xyz.openbmc_project.State.Host.HostState."
781e1e598dSJonathan Doman                               "TransitioningToOff")
79cc0bb6f2SAndrew Geissler         {
80ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "PoweringOff";
81ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Enabled";
82cc0bb6f2SAndrew Geissler         }
83002d39b4SEd Tanous         else if (hostState == "xyz.openbmc_project.State.Host.HostState.Off")
84cc0bb6f2SAndrew Geissler         {
85ac106bf6SEd Tanous             asyncResp->res.jsonValue["PowerState"] = "Off";
86ac106bf6SEd Tanous             asyncResp->res.jsonValue["Status"]["State"] = "Disabled";
87cc0bb6f2SAndrew Geissler         }
88cc0bb6f2SAndrew Geissler         else
89cc0bb6f2SAndrew Geissler         {
90ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
91cc0bb6f2SAndrew Geissler             return;
92cc0bb6f2SAndrew Geissler         }
931e1e598dSJonathan Doman     });
94cc0bb6f2SAndrew Geissler }
95cc0bb6f2SAndrew Geissler 
96cc0bb6f2SAndrew Geissler /**
974fbaf64aSAndrew Geissler  * @brief Populate Actions if any are valid for hypervisor object
984fbaf64aSAndrew Geissler  *
994fbaf64aSAndrew Geissler  * The hypervisor state object is optional so this function will only set the
1004fbaf64aSAndrew Geissler  * Action if the object is found
1014fbaf64aSAndrew Geissler  *
102ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for completing asynchronous calls.
1034fbaf64aSAndrew Geissler  *
1044fbaf64aSAndrew Geissler  * @return None.
1054fbaf64aSAndrew Geissler  */
1068d1b46d7Szhanghch05 inline void
107ac106bf6SEd Tanous     getHypervisorActions(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1084fbaf64aSAndrew Geissler {
10962598e31SEd Tanous     BMCWEB_LOG_DEBUG("Get hypervisor actions.");
1102b73119cSGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
1112b73119cSGeorge Liu         "xyz.openbmc_project.State.Host"};
1122b73119cSGeorge Liu     dbus::utility::getDbusObject(
1132b73119cSGeorge Liu         "/xyz/openbmc_project/state/hypervisor0", interfaces,
114ac106bf6SEd Tanous         [asyncResp](
1152b73119cSGeorge Liu             const boost::system::error_code& ec,
1164fbaf64aSAndrew Geissler             const std::vector<std::pair<std::string, std::vector<std::string>>>&
1174fbaf64aSAndrew Geissler                 objInfo) {
1184fbaf64aSAndrew Geissler         if (ec)
1194fbaf64aSAndrew Geissler         {
12062598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
1214fbaf64aSAndrew Geissler             // This is an optional D-Bus object so just return if
1224fbaf64aSAndrew Geissler             // error occurs
1234fbaf64aSAndrew Geissler             return;
1244fbaf64aSAndrew Geissler         }
1254fbaf64aSAndrew Geissler 
12626f6976fSEd Tanous         if (objInfo.empty())
1274fbaf64aSAndrew Geissler         {
1284fbaf64aSAndrew Geissler             // As noted above, this is an optional interface so just return
1294fbaf64aSAndrew Geissler             // if there is no instance found
1304fbaf64aSAndrew Geissler             return;
1314fbaf64aSAndrew Geissler         }
1324fbaf64aSAndrew Geissler 
1334fbaf64aSAndrew Geissler         if (objInfo.size() > 1)
1344fbaf64aSAndrew Geissler         {
1354fbaf64aSAndrew Geissler             // More then one hypervisor object is not supported and is an
1364fbaf64aSAndrew Geissler             // error
137ac106bf6SEd Tanous             messages::internalError(asyncResp->res);
1384fbaf64aSAndrew Geissler             return;
1394fbaf64aSAndrew Geissler         }
1404fbaf64aSAndrew Geissler 
1414fbaf64aSAndrew Geissler         // Object present so system support limited ComputerSystem Action
142613dabeaSEd Tanous         nlohmann::json& reset =
143ac106bf6SEd Tanous             asyncResp->res.jsonValue["Actions"]["#ComputerSystem.Reset"];
144613dabeaSEd Tanous         reset["target"] =
145613dabeaSEd Tanous             "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset";
146613dabeaSEd Tanous         reset["@Redfish.ActionInfo"] =
147613dabeaSEd Tanous             "/redfish/v1/Systems/hypervisor/ResetActionInfo";
1482b73119cSGeorge Liu     });
1494fbaf64aSAndrew Geissler }
1504fbaf64aSAndrew Geissler 
151d5afb2caSAndrew Geissler inline bool extractHypervisorInterfaceData(
152711ac7a9SEd Tanous     const std::string& ethIfaceId,
153711ac7a9SEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
15477179532SEd Tanous     EthernetInterfaceData& ethData, std::vector<IPv4AddressData>& ipv4Config)
155d5afb2caSAndrew Geissler {
156d5afb2caSAndrew Geissler     bool idFound = false;
157d5afb2caSAndrew Geissler     for (const auto& objpath : dbusData)
158d5afb2caSAndrew Geissler     {
159d5afb2caSAndrew Geissler         for (const auto& ifacePair : objpath.second)
160d5afb2caSAndrew Geissler         {
161d5afb2caSAndrew Geissler             if (objpath.first ==
162d5afb2caSAndrew Geissler                 "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId)
163d5afb2caSAndrew Geissler             {
164d5afb2caSAndrew Geissler                 idFound = true;
165d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
166d5afb2caSAndrew Geissler                 {
167d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
168d5afb2caSAndrew Geissler                     {
169d5afb2caSAndrew Geissler                         if (propertyPair.first == "MACAddress")
170d5afb2caSAndrew Geissler                         {
171d5afb2caSAndrew Geissler                             const std::string* mac =
172d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
173d5afb2caSAndrew Geissler                             if (mac != nullptr)
174d5afb2caSAndrew Geissler                             {
17582695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
176d5afb2caSAndrew Geissler                             }
177d5afb2caSAndrew Geissler                         }
178d5afb2caSAndrew Geissler                     }
179d5afb2caSAndrew Geissler                 }
180d5afb2caSAndrew Geissler                 else if (ifacePair.first ==
181d5afb2caSAndrew Geissler                          "xyz.openbmc_project.Network.EthernetInterface")
182d5afb2caSAndrew Geissler                 {
183d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
184d5afb2caSAndrew Geissler                     {
185d5afb2caSAndrew Geissler                         if (propertyPair.first == "DHCPEnabled")
186d5afb2caSAndrew Geissler                         {
187d5afb2caSAndrew Geissler                             const std::string* dhcp =
188d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
189d5afb2caSAndrew Geissler                             if (dhcp != nullptr)
190d5afb2caSAndrew Geissler                             {
19182695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcp;
192d5afb2caSAndrew Geissler                                 break; // Interested on only "DHCPEnabled".
193d5afb2caSAndrew Geissler                                        // Stop parsing since we got the
194d5afb2caSAndrew Geissler                                        // "DHCPEnabled" value.
195d5afb2caSAndrew Geissler                             }
196d5afb2caSAndrew Geissler                         }
197d5afb2caSAndrew Geissler                     }
198d5afb2caSAndrew Geissler                 }
199d5afb2caSAndrew Geissler             }
200d5afb2caSAndrew Geissler             if (objpath.first == "/xyz/openbmc_project/network/hypervisor/" +
201d5afb2caSAndrew Geissler                                      ethIfaceId + "/ipv4/addr0")
202d5afb2caSAndrew Geissler             {
20377179532SEd Tanous                 IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
204d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Object.Enable")
205d5afb2caSAndrew Geissler                 {
2069eb808c1SEd Tanous                     for (const auto& property : ifacePair.second)
207d5afb2caSAndrew Geissler                     {
208d5afb2caSAndrew Geissler                         if (property.first == "Enabled")
209d5afb2caSAndrew Geissler                         {
210d5afb2caSAndrew Geissler                             const bool* intfEnable =
211d5afb2caSAndrew Geissler                                 std::get_if<bool>(&property.second);
212d5afb2caSAndrew Geissler                             if (intfEnable != nullptr)
213d5afb2caSAndrew Geissler                             {
214d5afb2caSAndrew Geissler                                 ipv4Address.isActive = *intfEnable;
215d5afb2caSAndrew Geissler                                 break;
216d5afb2caSAndrew Geissler                             }
217d5afb2caSAndrew Geissler                         }
218d5afb2caSAndrew Geissler                     }
219d5afb2caSAndrew Geissler                 }
220d5afb2caSAndrew Geissler                 if (ifacePair.first == "xyz.openbmc_project.Network.IP")
221d5afb2caSAndrew Geissler                 {
2229eb808c1SEd Tanous                     for (const auto& property : ifacePair.second)
223d5afb2caSAndrew Geissler                     {
224d5afb2caSAndrew Geissler                         if (property.first == "Address")
225d5afb2caSAndrew Geissler                         {
226d5afb2caSAndrew Geissler                             const std::string* address =
227d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&property.second);
228d5afb2caSAndrew Geissler                             if (address != nullptr)
229d5afb2caSAndrew Geissler                             {
230d5afb2caSAndrew Geissler                                 ipv4Address.address = *address;
231d5afb2caSAndrew Geissler                             }
232d5afb2caSAndrew Geissler                         }
233d5afb2caSAndrew Geissler                         else if (property.first == "Origin")
234d5afb2caSAndrew Geissler                         {
235d5afb2caSAndrew Geissler                             const std::string* origin =
236d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&property.second);
237d5afb2caSAndrew Geissler                             if (origin != nullptr)
238d5afb2caSAndrew Geissler                             {
239d5afb2caSAndrew Geissler                                 ipv4Address.origin =
240d5afb2caSAndrew Geissler                                     translateAddressOriginDbusToRedfish(*origin,
241d5afb2caSAndrew Geissler                                                                         true);
242d5afb2caSAndrew Geissler                             }
243d5afb2caSAndrew Geissler                         }
244d5afb2caSAndrew Geissler                         else if (property.first == "PrefixLength")
245d5afb2caSAndrew Geissler                         {
246d5afb2caSAndrew Geissler                             const uint8_t* mask =
247d5afb2caSAndrew Geissler                                 std::get_if<uint8_t>(&property.second);
248d5afb2caSAndrew Geissler                             if (mask != nullptr)
249d5afb2caSAndrew Geissler                             {
250d5afb2caSAndrew Geissler                                 // convert it to the string
251d5afb2caSAndrew Geissler                                 ipv4Address.netmask = getNetmask(*mask);
252d5afb2caSAndrew Geissler                             }
253d5afb2caSAndrew Geissler                         }
254889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
255889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
256889ff694SAsmitha Karunanithi                         {
257889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
258889ff694SAsmitha Karunanithi                             continue;
259889ff694SAsmitha Karunanithi                         }
260d5afb2caSAndrew Geissler                         else
261d5afb2caSAndrew Geissler                         {
26262598e31SEd Tanous                             BMCWEB_LOG_ERROR(
26362598e31SEd Tanous                                 "Got extra property: {} on the {} object",
26462598e31SEd Tanous                                 property.first, objpath.first.str);
265d5afb2caSAndrew Geissler                         }
266d5afb2caSAndrew Geissler                     }
267d5afb2caSAndrew Geissler                 }
268d5afb2caSAndrew Geissler             }
269d5afb2caSAndrew Geissler             if (objpath.first == "/xyz/openbmc_project/network/hypervisor")
270d5afb2caSAndrew Geissler             {
271d5afb2caSAndrew Geissler                 // System configuration shows up in the global namespace, so no
272d5afb2caSAndrew Geissler                 // need to check eth number
273d5afb2caSAndrew Geissler                 if (ifacePair.first ==
274d5afb2caSAndrew Geissler                     "xyz.openbmc_project.Network.SystemConfiguration")
275d5afb2caSAndrew Geissler                 {
276d5afb2caSAndrew Geissler                     for (const auto& propertyPair : ifacePair.second)
277d5afb2caSAndrew Geissler                     {
278d5afb2caSAndrew Geissler                         if (propertyPair.first == "HostName")
279d5afb2caSAndrew Geissler                         {
280d5afb2caSAndrew Geissler                             const std::string* hostName =
281d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
282d5afb2caSAndrew Geissler                             if (hostName != nullptr)
283d5afb2caSAndrew Geissler                             {
28482695a5bSJiaqing Zhao                                 ethData.hostName = *hostName;
285d5afb2caSAndrew Geissler                             }
286d5afb2caSAndrew Geissler                         }
287d5afb2caSAndrew Geissler                         else if (propertyPair.first == "DefaultGateway")
288d5afb2caSAndrew Geissler                         {
289d5afb2caSAndrew Geissler                             const std::string* defaultGateway =
290d5afb2caSAndrew Geissler                                 std::get_if<std::string>(&propertyPair.second);
291d5afb2caSAndrew Geissler                             if (defaultGateway != nullptr)
292d5afb2caSAndrew Geissler                             {
29382695a5bSJiaqing Zhao                                 ethData.defaultGateway = *defaultGateway;
294d5afb2caSAndrew Geissler                             }
295d5afb2caSAndrew Geissler                         }
296d5afb2caSAndrew Geissler                     }
297d5afb2caSAndrew Geissler                 }
298d5afb2caSAndrew Geissler             }
299d5afb2caSAndrew Geissler         }
300d5afb2caSAndrew Geissler     }
301d5afb2caSAndrew Geissler     return idFound;
302d5afb2caSAndrew Geissler }
303d5afb2caSAndrew Geissler /**
304d5afb2caSAndrew Geissler  * Function that retrieves all properties for given Hypervisor Ethernet
305d5afb2caSAndrew Geissler  * Interface Object from Settings Manager
306d5afb2caSAndrew Geissler  * @param ethIfaceId Hypervisor ethernet interface id to query on DBus
307d5afb2caSAndrew Geissler  * @param callback a function that shall be called to convert Dbus output
308d5afb2caSAndrew Geissler  * into JSON
309d5afb2caSAndrew Geissler  */
310d5afb2caSAndrew Geissler template <typename CallbackFunc>
311d5afb2caSAndrew Geissler void getHypervisorIfaceData(const std::string& ethIfaceId,
312d5afb2caSAndrew Geissler                             CallbackFunc&& callback)
313d5afb2caSAndrew Geissler {
3145eb468daSGeorge Liu     sdbusplus::message::object_path path("/");
3155eb468daSGeorge Liu     dbus::utility::getManagedObjects(
3165eb468daSGeorge Liu         "xyz.openbmc_project.Settings", path,
317f94c4ecfSEd Tanous         [ethIfaceId{std::string{ethIfaceId}},
318f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
3198b24275dSEd Tanous             const boost::system::error_code& ec,
320*21fe928bSEd Tanous             const dbus::utility::ManagedObjectType& resp) mutable {
321d5afb2caSAndrew Geissler         EthernetInterfaceData ethData{};
32277179532SEd Tanous         std::vector<IPv4AddressData> ipv4Data;
3238b24275dSEd Tanous         if (ec)
324d5afb2caSAndrew Geissler         {
325d5afb2caSAndrew Geissler             callback(false, ethData, ipv4Data);
326d5afb2caSAndrew Geissler             return;
327d5afb2caSAndrew Geissler         }
328d5afb2caSAndrew Geissler 
32989492a15SPatrick Williams         bool found = extractHypervisorInterfaceData(ethIfaceId, resp, ethData,
33089492a15SPatrick Williams                                                     ipv4Data);
331d5afb2caSAndrew Geissler         if (!found)
332d5afb2caSAndrew Geissler         {
33362598e31SEd Tanous             BMCWEB_LOG_INFO("Hypervisor Interface not found");
334d5afb2caSAndrew Geissler         }
335d5afb2caSAndrew Geissler         callback(found, ethData, ipv4Data);
3365eb468daSGeorge Liu     });
337d5afb2caSAndrew Geissler }
338d5afb2caSAndrew Geissler 
339d5afb2caSAndrew Geissler /**
340d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface IPAddress DBUS
341d5afb2caSAndrew Geissler  *
342ac106bf6SEd Tanous  * @param[in] asyncResp          Shared pointer for generating response message.
343d5afb2caSAndrew Geissler  * @param[in] ipv4Address    Address from the incoming request
344d5afb2caSAndrew Geissler  * @param[in] ethIfaceId     Hypervisor Interface Id
345d5afb2caSAndrew Geissler  *
346d5afb2caSAndrew Geissler  * @return None.
347d5afb2caSAndrew Geissler  */
348ac106bf6SEd Tanous inline void setHypervisorIPv4Address(
349ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
350ac106bf6SEd Tanous     const std::string& ethIfaceId, const std::string& ipv4Address)
351d5afb2caSAndrew Geissler {
35262598e31SEd Tanous     BMCWEB_LOG_DEBUG("Setting the Hypervisor IPaddress : {} on Iface: {}",
35362598e31SEd Tanous                      ipv4Address, ethIfaceId);
354d82b5e1fSAsmitha Karunanithi 
355d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
356d82b5e1fSAsmitha Karunanithi                     "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId +
357d82b5e1fSAsmitha Karunanithi                         "/ipv4/addr0",
358d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.IP", "Address",
359d82b5e1fSAsmitha Karunanithi                     "IPv4StaticAddresses/1/Address", ipv4Address);
360d5afb2caSAndrew Geissler }
361d5afb2caSAndrew Geissler 
362d5afb2caSAndrew Geissler /**
363d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface SubnetMask DBUS
364d5afb2caSAndrew Geissler  *
365ac106bf6SEd Tanous  * @param[in] asyncResp     Shared pointer for generating response message.
366d5afb2caSAndrew Geissler  * @param[in] subnet    SubnetMask from the incoming request
367d5afb2caSAndrew Geissler  * @param[in] ethIfaceId Hypervisor Interface Id
368d5afb2caSAndrew Geissler  *
369d5afb2caSAndrew Geissler  * @return None.
370d5afb2caSAndrew Geissler  */
3718d1b46d7Szhanghch05 inline void
372ac106bf6SEd Tanous     setHypervisorIPv4Subnet(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3738d1b46d7Szhanghch05                             const std::string& ethIfaceId, const uint8_t subnet)
374d5afb2caSAndrew Geissler {
37562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Setting the Hypervisor subnet : {} on Iface: {}", subnet,
37662598e31SEd Tanous                      ethIfaceId);
377d5afb2caSAndrew Geissler 
378d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
379d82b5e1fSAsmitha Karunanithi                     "/xyz/openbmc_project/network/hypervisor/" + ethIfaceId +
380d82b5e1fSAsmitha Karunanithi                         "/ipv4/addr0",
381d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.IP", "PrefixLength",
382d82b5e1fSAsmitha Karunanithi                     "IPv4StaticAddresses/1/SubnetMask", subnet);
383d5afb2caSAndrew Geissler }
384d5afb2caSAndrew Geissler 
385d5afb2caSAndrew Geissler /**
386d5afb2caSAndrew Geissler  * @brief Sets the Hypervisor Interface Gateway DBUS
387d5afb2caSAndrew Geissler  *
388ac106bf6SEd Tanous  * @param[in] asyncResp          Shared pointer for generating response message.
389d5afb2caSAndrew Geissler  * @param[in] gateway        Gateway from the incoming request
390d5afb2caSAndrew Geissler  * @param[in] ethIfaceId     Hypervisor Interface Id
391d5afb2caSAndrew Geissler  *
392d5afb2caSAndrew Geissler  * @return None.
393d5afb2caSAndrew Geissler  */
394ac106bf6SEd Tanous inline void setHypervisorIPv4Gateway(
395ac106bf6SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
396d5afb2caSAndrew Geissler     const std::string& gateway)
397d5afb2caSAndrew Geissler {
39862598e31SEd Tanous     BMCWEB_LOG_DEBUG(
39962598e31SEd Tanous         "Setting the DefaultGateway to the last configured gateway");
400d5afb2caSAndrew Geissler 
401d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
402d82b5e1fSAsmitha Karunanithi                     sdbusplus::message::object_path(
403d82b5e1fSAsmitha Karunanithi                         "/xyz/openbmc_project/network/hypervisor"),
404d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.SystemConfiguration",
405d82b5e1fSAsmitha Karunanithi                     "DefaultGateway", "IPv4StaticAddresses/1/Gateway", gateway);
406d5afb2caSAndrew Geissler }
407d5afb2caSAndrew Geissler 
408d5afb2caSAndrew Geissler /**
409d5afb2caSAndrew Geissler  * @brief Creates a static IPv4 entry
410d5afb2caSAndrew Geissler  *
411d5afb2caSAndrew Geissler  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
412d5afb2caSAndrew Geissler  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
413d5afb2caSAndrew Geissler  * @param[in] gateway      IPv4 address of this interfaces gateway
414d5afb2caSAndrew Geissler  * @param[in] address      IPv4 address to assign to this interface
415d5afb2caSAndrew Geissler  * @param[io] asyncResp    Response object that will be returned to client
416d5afb2caSAndrew Geissler  *
417d5afb2caSAndrew Geissler  * @return None
418d5afb2caSAndrew Geissler  */
4198d1b46d7Szhanghch05 inline void
4208d1b46d7Szhanghch05     createHypervisorIPv4(const std::string& ifaceId, uint8_t prefixLength,
4218d1b46d7Szhanghch05                          const std::string& gateway, const std::string& address,
4228d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
423d5afb2caSAndrew Geissler {
424d5afb2caSAndrew Geissler     setHypervisorIPv4Address(asyncResp, ifaceId, address);
425d5afb2caSAndrew Geissler     setHypervisorIPv4Gateway(asyncResp, gateway);
426d5afb2caSAndrew Geissler     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
427d5afb2caSAndrew Geissler }
428d5afb2caSAndrew Geissler 
429d5afb2caSAndrew Geissler /**
430d5afb2caSAndrew Geissler  * @brief Deletes given IPv4 interface
431d5afb2caSAndrew Geissler  *
432d5afb2caSAndrew Geissler  * @param[in] ifaceId     Id of interface whose IP should be deleted
433d5afb2caSAndrew Geissler  * @param[io] asyncResp   Response object that will be returned to client
434d5afb2caSAndrew Geissler  *
435d5afb2caSAndrew Geissler  * @return None
436d5afb2caSAndrew Geissler  */
4378d1b46d7Szhanghch05 inline void
4388d1b46d7Szhanghch05     deleteHypervisorIPv4(const std::string& ifaceId,
4398d1b46d7Szhanghch05                          const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
440d5afb2caSAndrew Geissler {
441d5afb2caSAndrew Geissler     std::string address = "0.0.0.0";
442d5afb2caSAndrew Geissler     std::string gateway = "0.0.0.0";
443d5afb2caSAndrew Geissler     const uint8_t prefixLength = 0;
444d5afb2caSAndrew Geissler     setHypervisorIPv4Address(asyncResp, ifaceId, address);
445d5afb2caSAndrew Geissler     setHypervisorIPv4Gateway(asyncResp, gateway);
446d5afb2caSAndrew Geissler     setHypervisorIPv4Subnet(asyncResp, ifaceId, prefixLength);
447d5afb2caSAndrew Geissler }
448d5afb2caSAndrew Geissler 
44977179532SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
45077179532SEd Tanous                                const std::string& ifaceId,
451d5afb2caSAndrew Geissler                                const EthernetInterfaceData& ethData,
45277179532SEd Tanous                                const std::vector<IPv4AddressData>& ipv4Data)
453d5afb2caSAndrew Geissler {
454d5afb2caSAndrew Geissler     jsonResponse["Id"] = ifaceId;
455ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
456ef4c65b7SEd Tanous         "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", ifaceId);
457d5afb2caSAndrew Geissler     jsonResponse["InterfaceEnabled"] = true;
45882695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
459d5afb2caSAndrew Geissler 
46082695a5bSJiaqing Zhao     jsonResponse["HostName"] = ethData.hostName;
461d5afb2caSAndrew Geissler     jsonResponse["DHCPv4"]["DHCPEnabled"] =
46282695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
463d5afb2caSAndrew Geissler 
464d5afb2caSAndrew Geissler     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
465d5afb2caSAndrew Geissler     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
466d5afb2caSAndrew Geissler     ipv4Array = nlohmann::json::array();
467d5afb2caSAndrew Geissler     ipv4StaticArray = nlohmann::json::array();
4689eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
469d5afb2caSAndrew Geissler     {
470d5afb2caSAndrew Geissler         if (ipv4Config.isActive)
471d5afb2caSAndrew Geissler         {
4721476687dSEd Tanous             nlohmann::json::object_t ipv4;
4731476687dSEd Tanous             ipv4["AddressOrigin"] = ipv4Config.origin;
4741476687dSEd Tanous             ipv4["SubnetMask"] = ipv4Config.netmask;
4751476687dSEd Tanous             ipv4["Address"] = ipv4Config.address;
4761476687dSEd Tanous             ipv4["Gateway"] = ethData.defaultGateway;
477d5afb2caSAndrew Geissler 
478d5afb2caSAndrew Geissler             if (ipv4Config.origin == "Static")
479d5afb2caSAndrew Geissler             {
4801476687dSEd Tanous                 ipv4StaticArray.push_back(ipv4);
481d5afb2caSAndrew Geissler             }
482b2ba3072SPatrick Williams             ipv4Array.emplace_back(std::move(ipv4));
483d5afb2caSAndrew Geissler         }
484d5afb2caSAndrew Geissler     }
485d5afb2caSAndrew Geissler }
486d5afb2caSAndrew Geissler 
48777179532SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId, bool ipv4DHCPEnabled,
4888d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
489d5afb2caSAndrew Geissler {
4907e860f15SJohn Edward Broadbent     const std::string dhcp = getDhcpEnabledEnumeration(ipv4DHCPEnabled, false);
491d82b5e1fSAsmitha Karunanithi 
492d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
493d82b5e1fSAsmitha Karunanithi                     sdbusplus::message::object_path(
494d82b5e1fSAsmitha Karunanithi                         "/xyz/openbmc_project/network/hypervisor") /
495d82b5e1fSAsmitha Karunanithi                         ifaceId,
496d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.EthernetInterface",
497d82b5e1fSAsmitha Karunanithi                     "DHCPEnabled", "DHCPv4/DHCPEnabled", dhcp);
498d5afb2caSAndrew Geissler 
499d5afb2caSAndrew Geissler     // Set the IPv4 address origin to the DHCP / Static as per the new value
500d5afb2caSAndrew Geissler     // of the DHCPEnabled property
501d5afb2caSAndrew Geissler     std::string origin;
502e05aec50SEd Tanous     if (!ipv4DHCPEnabled)
503d5afb2caSAndrew Geissler     {
504d5afb2caSAndrew Geissler         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
505d5afb2caSAndrew Geissler     }
506d5afb2caSAndrew Geissler     else
507d5afb2caSAndrew Geissler     {
508d5afb2caSAndrew Geissler         // DHCPEnabled is set to true. Delete the current IPv4 settings
509d5afb2caSAndrew Geissler         // to receive the new values from DHCP server.
510d5afb2caSAndrew Geissler         deleteHypervisorIPv4(ifaceId, asyncResp);
511d5afb2caSAndrew Geissler         origin = "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
512d5afb2caSAndrew Geissler     }
513d82b5e1fSAsmitha Karunanithi 
514d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
515d82b5e1fSAsmitha Karunanithi                     "/xyz/openbmc_project/network/hypervisor/" + ifaceId +
516d82b5e1fSAsmitha Karunanithi                         "/ipv4/addr0",
517d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.IP", "Origin",
518d82b5e1fSAsmitha Karunanithi                     "IPv4StaticAddresses/1/AddressOrigin", origin);
519d5afb2caSAndrew Geissler }
520d5afb2caSAndrew Geissler 
5217e860f15SJohn Edward Broadbent inline void handleHypervisorIPv4StaticPatch(
522*21fe928bSEd Tanous     const std::string& ifaceId,
523*21fe928bSEd Tanous     std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>& input,
5247e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
525d5afb2caSAndrew Geissler {
5267e860f15SJohn Edward Broadbent     // Hypervisor considers the first IP address in the array list
5277e860f15SJohn Edward Broadbent     // as the Hypervisor's virtual management interface supports single IPv4
5287e860f15SJohn Edward Broadbent     // address
529*21fe928bSEd Tanous     std::variant<nlohmann::json::object_t, std::nullptr_t>& thisJson = input[0];
530*21fe928bSEd Tanous     nlohmann::json::object_t* obj =
531*21fe928bSEd Tanous         std::get_if<nlohmann::json::object_t>(&thisJson);
532*21fe928bSEd Tanous     if (obj == nullptr)
5337e860f15SJohn Edward Broadbent     {
534*21fe928bSEd Tanous         deleteHypervisorIPv4(ifaceId, asyncResp);
535*21fe928bSEd Tanous         return;
536*21fe928bSEd Tanous     }
537*21fe928bSEd Tanous     if (obj->empty())
538*21fe928bSEd Tanous     {
539*21fe928bSEd Tanous         return;
540*21fe928bSEd Tanous     }
541f8fe53e7SEd Tanous     // For the error string
542f8fe53e7SEd Tanous     std::string pathString = "IPv4StaticAddresses/1";
543*21fe928bSEd Tanous     std::string address;
544*21fe928bSEd Tanous     std::string subnetMask;
545*21fe928bSEd Tanous     std::string gateway;
546*21fe928bSEd Tanous     if (!json_util::readJsonObject(*obj, asyncResp->res, "Address", address,
547*21fe928bSEd Tanous                                    "SubnetMask", subnetMask, "Gateway",
5487e860f15SJohn Edward Broadbent                                    gateway))
5497e860f15SJohn Edward Broadbent     {
5507e860f15SJohn Edward Broadbent         return;
5517e860f15SJohn Edward Broadbent     }
5527e860f15SJohn Edward Broadbent 
5537e860f15SJohn Edward Broadbent     uint8_t prefixLength = 0;
554*21fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(address))
5557e860f15SJohn Edward Broadbent     {
556*21fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, address,
5577e860f15SJohn Edward Broadbent                                            pathString + "/Address");
558*21fe928bSEd Tanous         return;
5597e860f15SJohn Edward Broadbent     }
5607e860f15SJohn Edward Broadbent 
561*21fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(subnetMask, &prefixLength))
5627e860f15SJohn Edward Broadbent     {
563*21fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, subnetMask,
5647e860f15SJohn Edward Broadbent                                            pathString + "/SubnetMask");
565*21fe928bSEd Tanous         return;
5667e860f15SJohn Edward Broadbent     }
5677e860f15SJohn Edward Broadbent 
568*21fe928bSEd Tanous     if (!ip_util::ipv4VerifyIpAndGetBitcount(gateway))
5697e860f15SJohn Edward Broadbent     {
570*21fe928bSEd Tanous         messages::propertyValueFormatError(asyncResp->res, gateway,
5717e860f15SJohn Edward Broadbent                                            pathString + "/Gateway");
5727e860f15SJohn Edward Broadbent         return;
5737e860f15SJohn Edward Broadbent     }
5747e860f15SJohn Edward Broadbent 
57562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Calling createHypervisorIPv4 on : {},{}", ifaceId,
576*21fe928bSEd Tanous                      address);
577*21fe928bSEd Tanous     createHypervisorIPv4(ifaceId, prefixLength, gateway, address, asyncResp);
5787e860f15SJohn Edward Broadbent     // Set the DHCPEnabled to false since the Static IPv4 is set
5797e860f15SJohn Edward Broadbent     setDHCPEnabled(ifaceId, false, asyncResp);
5807e860f15SJohn Edward Broadbent }
5817e860f15SJohn Edward Broadbent 
58288a8a174SEd Tanous inline void handleHypervisorHostnamePatch(
58388a8a174SEd Tanous     const std::string& hostName,
5847e860f15SJohn Edward Broadbent     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
5857e860f15SJohn Edward Broadbent {
5867e860f15SJohn Edward Broadbent     if (!isHostnameValid(hostName))
5877e860f15SJohn Edward Broadbent     {
5887e860f15SJohn Edward Broadbent         messages::propertyValueFormatError(asyncResp->res, hostName,
5897e860f15SJohn Edward Broadbent                                            "HostName");
5907e860f15SJohn Edward Broadbent         return;
5917e860f15SJohn Edward Broadbent     }
5927e860f15SJohn Edward Broadbent 
5937e860f15SJohn Edward Broadbent     asyncResp->res.jsonValue["HostName"] = hostName;
594d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
595d82b5e1fSAsmitha Karunanithi                     sdbusplus::message::object_path(
596d82b5e1fSAsmitha Karunanithi                         "/xyz/openbmc_project/network/hypervisor"),
597d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Network.SystemConfiguration",
598d82b5e1fSAsmitha Karunanithi                     "HostName", "HostName", hostName);
5997e860f15SJohn Edward Broadbent }
6007e860f15SJohn Edward Broadbent 
6017e860f15SJohn Edward Broadbent inline void
60277179532SEd Tanous     setIPv4InterfaceEnabled(const std::string& ifaceId, bool isActive,
6037e860f15SJohn Edward Broadbent                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6047e860f15SJohn Edward Broadbent {
605d82b5e1fSAsmitha Karunanithi     setDbusProperty(asyncResp, "xyz.openbmc_project.Settings",
606d82b5e1fSAsmitha Karunanithi                     "/xyz/openbmc_project/network/hypervisor/" + ifaceId +
607d82b5e1fSAsmitha Karunanithi                         "/ipv4/addr0",
608d82b5e1fSAsmitha Karunanithi                     "xyz.openbmc_project.Object.Enable", "Enabled",
609d82b5e1fSAsmitha Karunanithi                     "InterfaceEnabled", isActive);
6107e860f15SJohn Edward Broadbent }
6117e860f15SJohn Edward Broadbent 
612f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceCollectionGet(
613f40448e5SGunnar Mills     App& app, const crow::Request& req,
614f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
615f40448e5SGunnar Mills {
616f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
617f40448e5SGunnar Mills     {
618f40448e5SGunnar Mills         return;
619f40448e5SGunnar Mills     }
620f40448e5SGunnar Mills     constexpr std::array<std::string_view, 1> interfaces = {
621f40448e5SGunnar Mills         "xyz.openbmc_project.Network.EthernetInterface"};
622f40448e5SGunnar Mills 
623f40448e5SGunnar Mills     dbus::utility::getSubTreePaths(
624f40448e5SGunnar Mills         "/xyz/openbmc_project/network/hypervisor", 0, interfaces,
625f40448e5SGunnar Mills         [asyncResp](
6268b24275dSEd Tanous             const boost::system::error_code& ec,
627f40448e5SGunnar Mills             const dbus::utility::MapperGetSubTreePathsResponse& ifaceList) {
6288b24275dSEd Tanous         if (ec)
629f40448e5SGunnar Mills         {
630f40448e5SGunnar Mills             messages::resourceNotFound(asyncResp->res, "System", "hypervisor");
631f40448e5SGunnar Mills             return;
632f40448e5SGunnar Mills         }
633f40448e5SGunnar Mills         asyncResp->res.jsonValue["@odata.type"] =
634f40448e5SGunnar Mills             "#EthernetInterfaceCollection."
635f40448e5SGunnar Mills             "EthernetInterfaceCollection";
636f40448e5SGunnar Mills         asyncResp->res.jsonValue["@odata.id"] =
637f40448e5SGunnar Mills             "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
638f40448e5SGunnar Mills         asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet "
639f40448e5SGunnar Mills                                            "Interface Collection";
640f40448e5SGunnar Mills         asyncResp->res.jsonValue["Description"] =
641f40448e5SGunnar Mills             "Collection of Virtual Management "
642f40448e5SGunnar Mills             "Interfaces for the hypervisor";
643f40448e5SGunnar Mills 
644f40448e5SGunnar Mills         nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
645f40448e5SGunnar Mills         ifaceArray = nlohmann::json::array();
646f40448e5SGunnar Mills         for (const std::string& iface : ifaceList)
647f40448e5SGunnar Mills         {
648f40448e5SGunnar Mills             sdbusplus::message::object_path path(iface);
649f40448e5SGunnar Mills             std::string name = path.filename();
650f40448e5SGunnar Mills             if (name.empty())
651f40448e5SGunnar Mills             {
652f40448e5SGunnar Mills                 continue;
653f40448e5SGunnar Mills             }
654f40448e5SGunnar Mills             nlohmann::json::object_t ethIface;
655ef4c65b7SEd Tanous             ethIface["@odata.id"] = boost::urls::format(
656ef4c65b7SEd Tanous                 "/redfish/v1/Systems/hypervisor/EthernetInterfaces/{}", name);
657b2ba3072SPatrick Williams             ifaceArray.emplace_back(std::move(ethIface));
658f40448e5SGunnar Mills         }
659f40448e5SGunnar Mills         asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
660f40448e5SGunnar Mills     });
661f40448e5SGunnar Mills }
662f40448e5SGunnar Mills 
663f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfaceGet(
664f40448e5SGunnar Mills     App& app, const crow::Request& req,
665f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
666f40448e5SGunnar Mills {
667f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
668f40448e5SGunnar Mills     {
669f40448e5SGunnar Mills         return;
670f40448e5SGunnar Mills     }
671f40448e5SGunnar Mills     getHypervisorIfaceData(
672f40448e5SGunnar Mills         id, [asyncResp, ifaceId{std::string(id)}](
67377179532SEd Tanous                 bool success, const EthernetInterfaceData& ethData,
67477179532SEd Tanous                 const std::vector<IPv4AddressData>& ipv4Data) {
675f40448e5SGunnar Mills         if (!success)
676f40448e5SGunnar Mills         {
677f40448e5SGunnar Mills             messages::resourceNotFound(asyncResp->res, "EthernetInterface",
678f40448e5SGunnar Mills                                        ifaceId);
679f40448e5SGunnar Mills             return;
680f40448e5SGunnar Mills         }
681f40448e5SGunnar Mills         asyncResp->res.jsonValue["@odata.type"] =
68293bbc953SJiaqing Zhao             "#EthernetInterface.v1_9_0.EthernetInterface";
683f40448e5SGunnar Mills         asyncResp->res.jsonValue["Name"] = "Hypervisor Ethernet Interface";
684f40448e5SGunnar Mills         asyncResp->res.jsonValue["Description"] =
685f40448e5SGunnar Mills             "Hypervisor's Virtual Management Ethernet Interface";
686f40448e5SGunnar Mills         parseInterfaceData(asyncResp->res.jsonValue, ifaceId, ethData,
687f40448e5SGunnar Mills                            ipv4Data);
688f40448e5SGunnar Mills     });
689f40448e5SGunnar Mills }
690f40448e5SGunnar Mills 
6918d8c30c3SGunnar Mills inline void handleHypervisorSystemGet(
6928d8c30c3SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6938d8c30c3SGunnar Mills {
6948d8c30c3SGunnar Mills     sdbusplus::asio::getProperty<std::string>(
6958d8c30c3SGunnar Mills         *crow::connections::systemBus, "xyz.openbmc_project.Settings",
6968d8c30c3SGunnar Mills         "/xyz/openbmc_project/network/hypervisor",
6978d8c30c3SGunnar Mills         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
6988d8c30c3SGunnar Mills         [asyncResp](const boost::system::error_code& ec,
6998d8c30c3SGunnar Mills                     const std::string& /*hostName*/) {
7008d8c30c3SGunnar Mills         if (ec)
7018d8c30c3SGunnar Mills         {
7028d8c30c3SGunnar Mills             messages::resourceNotFound(asyncResp->res, "System", "hypervisor");
7038d8c30c3SGunnar Mills             return;
7048d8c30c3SGunnar Mills         }
70562598e31SEd Tanous         BMCWEB_LOG_DEBUG("Hypervisor is available");
7068d8c30c3SGunnar Mills 
7078d8c30c3SGunnar Mills         asyncResp->res.jsonValue["@odata.type"] =
7088d8c30c3SGunnar Mills             "#ComputerSystem.v1_6_0.ComputerSystem";
7098d8c30c3SGunnar Mills         asyncResp->res.jsonValue["@odata.id"] =
7108d8c30c3SGunnar Mills             "/redfish/v1/Systems/hypervisor";
7118d8c30c3SGunnar Mills         asyncResp->res.jsonValue["Description"] = "Hypervisor";
7128d8c30c3SGunnar Mills         asyncResp->res.jsonValue["Name"] = "Hypervisor";
7138d8c30c3SGunnar Mills         asyncResp->res.jsonValue["Id"] = "hypervisor";
7148d8c30c3SGunnar Mills         asyncResp->res.jsonValue["SystemType"] = "OS";
7158d8c30c3SGunnar Mills         nlohmann::json::array_t managedBy;
7168d8c30c3SGunnar Mills         nlohmann::json::object_t manager;
7178d8c30c3SGunnar Mills         manager["@odata.id"] = "/redfish/v1/Managers/bmc";
718ad539545SPatrick Williams         managedBy.emplace_back(std::move(manager));
7198d8c30c3SGunnar Mills         asyncResp->res.jsonValue["Links"]["ManagedBy"] = std::move(managedBy);
7208d8c30c3SGunnar Mills         asyncResp->res.jsonValue["EthernetInterfaces"]["@odata.id"] =
7218d8c30c3SGunnar Mills             "/redfish/v1/Systems/hypervisor/EthernetInterfaces";
7228d8c30c3SGunnar Mills         getHypervisorState(asyncResp);
7238d8c30c3SGunnar Mills         getHypervisorActions(asyncResp);
7248d8c30c3SGunnar Mills         // TODO: Add "SystemType" : "hypervisor"
7258d8c30c3SGunnar Mills     });
7268d8c30c3SGunnar Mills }
7278d8c30c3SGunnar Mills 
728f40448e5SGunnar Mills inline void handleHypervisorEthernetInterfacePatch(
729f40448e5SGunnar Mills     App& app, const crow::Request& req,
730f40448e5SGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
731f40448e5SGunnar Mills     const std::string& ifaceId)
732f40448e5SGunnar Mills {
733f40448e5SGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
734f40448e5SGunnar Mills     {
735f40448e5SGunnar Mills         return;
736f40448e5SGunnar Mills     }
737f40448e5SGunnar Mills     std::optional<std::string> hostName;
738*21fe928bSEd Tanous     std::optional<
739*21fe928bSEd Tanous         std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>
740*21fe928bSEd Tanous         ipv4StaticAddresses;
741*21fe928bSEd Tanous     std::optional<std::vector<nlohmann::json::object_t>> ipv4Addresses;
742f40448e5SGunnar Mills     std::optional<bool> ipv4DHCPEnabled;
743f40448e5SGunnar Mills 
744f40448e5SGunnar Mills     if (!json_util::readJsonPatch(req, asyncResp->res, "HostName", hostName,
745f40448e5SGunnar Mills                                   "IPv4StaticAddresses", ipv4StaticAddresses,
746*21fe928bSEd Tanous                                   "IPv4Addresses", ipv4Addresses,
747*21fe928bSEd Tanous                                   "DHCPv4/DHCPEnabled", ipv4DHCPEnabled))
748f40448e5SGunnar Mills     {
749f40448e5SGunnar Mills         return;
750f40448e5SGunnar Mills     }
751f40448e5SGunnar Mills 
752f40448e5SGunnar Mills     if (ipv4Addresses)
753f40448e5SGunnar Mills     {
754f40448e5SGunnar Mills         messages::propertyNotWritable(asyncResp->res, "IPv4Addresses");
755f40448e5SGunnar Mills         return;
756f40448e5SGunnar Mills     }
757f40448e5SGunnar Mills 
758f40448e5SGunnar Mills     getHypervisorIfaceData(
759*21fe928bSEd Tanous         ifaceId,
760*21fe928bSEd Tanous         [asyncResp, ifaceId, hostName = std::move(hostName),
7615a39f77aSPatrick Williams          ipv4StaticAddresses = std::move(ipv4StaticAddresses),
762*21fe928bSEd Tanous          ipv4DHCPEnabled](bool success, const EthernetInterfaceData& ethData,
763*21fe928bSEd Tanous                           const std::vector<IPv4AddressData>&) mutable {
764f40448e5SGunnar Mills         if (!success)
765f40448e5SGunnar Mills         {
766f40448e5SGunnar Mills             messages::resourceNotFound(asyncResp->res, "EthernetInterface",
767f40448e5SGunnar Mills                                        ifaceId);
768f40448e5SGunnar Mills             return;
769f40448e5SGunnar Mills         }
770f40448e5SGunnar Mills 
771f40448e5SGunnar Mills         if (ipv4StaticAddresses)
772f40448e5SGunnar Mills         {
773*21fe928bSEd Tanous             std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>&
774*21fe928bSEd Tanous                 ipv4Static = *ipv4StaticAddresses;
775f40448e5SGunnar Mills             if (ipv4Static.begin() == ipv4Static.end())
776f40448e5SGunnar Mills             {
777*21fe928bSEd Tanous                 messages::propertyValueTypeError(asyncResp->res,
778*21fe928bSEd Tanous                                                  std::vector<std::string>(),
779f40448e5SGunnar Mills                                                  "IPv4StaticAddresses");
780f40448e5SGunnar Mills                 return;
781f40448e5SGunnar Mills             }
782f40448e5SGunnar Mills 
783f40448e5SGunnar Mills             // One and only one hypervisor instance supported
784f40448e5SGunnar Mills             if (ipv4Static.size() != 1)
785f40448e5SGunnar Mills             {
786*21fe928bSEd Tanous                 messages::propertyValueFormatError(asyncResp->res, "[]",
787f40448e5SGunnar Mills                                                    "IPv4StaticAddresses");
788f40448e5SGunnar Mills                 return;
789f40448e5SGunnar Mills             }
790f40448e5SGunnar Mills 
791*21fe928bSEd Tanous             std::variant<nlohmann::json::object_t, std::nullptr_t>& ipv4Json =
792*21fe928bSEd Tanous                 ipv4Static[0];
793f40448e5SGunnar Mills             // Check if the param is 'null'. If its null, it means
794f40448e5SGunnar Mills             // that user wants to delete the IP address. Deleting
795f40448e5SGunnar Mills             // the IP address is allowed only if its statically
796f40448e5SGunnar Mills             // configured. Deleting the address originated from DHCP
797f40448e5SGunnar Mills             // is not allowed.
798*21fe928bSEd Tanous             if (std::holds_alternative<std::nullptr_t>(ipv4Json) &&
799*21fe928bSEd Tanous                 translateDhcpEnabledToBool(ethData.dhcpEnabled, true))
800f40448e5SGunnar Mills             {
80162598e31SEd Tanous                 BMCWEB_LOG_INFO("Ignoring the delete on ipv4StaticAddresses "
80262598e31SEd Tanous                                 "as the interface is DHCP enabled");
803f40448e5SGunnar Mills             }
804f40448e5SGunnar Mills             else
805f40448e5SGunnar Mills             {
806f40448e5SGunnar Mills                 handleHypervisorIPv4StaticPatch(ifaceId, ipv4Static, asyncResp);
807f40448e5SGunnar Mills             }
808f40448e5SGunnar Mills         }
809f40448e5SGunnar Mills 
810f40448e5SGunnar Mills         if (hostName)
811f40448e5SGunnar Mills         {
812f40448e5SGunnar Mills             handleHypervisorHostnamePatch(*hostName, asyncResp);
813f40448e5SGunnar Mills         }
814f40448e5SGunnar Mills 
815*21fe928bSEd Tanous         if (ipv4DHCPEnabled)
816f40448e5SGunnar Mills         {
817f40448e5SGunnar Mills             setDHCPEnabled(ifaceId, *ipv4DHCPEnabled, asyncResp);
818f40448e5SGunnar Mills         }
819f40448e5SGunnar Mills 
820f40448e5SGunnar Mills         // Set this interface to disabled/inactive. This will be set
821f40448e5SGunnar Mills         // to enabled/active by the pldm once the hypervisor
822f40448e5SGunnar Mills         // consumes the updated settings from the user.
823f40448e5SGunnar Mills         setIPv4InterfaceEnabled(ifaceId, false, asyncResp);
824f40448e5SGunnar Mills     });
825f40448e5SGunnar Mills     asyncResp->res.result(boost::beast::http::status::accepted);
826f40448e5SGunnar Mills }
827f40448e5SGunnar Mills 
828a5d0cedbSGunnar Mills inline void handleHypervisorResetActionGet(
829a5d0cedbSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
830a5d0cedbSGunnar Mills {
831a5d0cedbSGunnar Mills     // Only return action info if hypervisor D-Bus object present
832a5d0cedbSGunnar Mills     constexpr std::array<std::string_view, 1> interfaces = {
833a5d0cedbSGunnar Mills         "xyz.openbmc_project.State.Host"};
834a5d0cedbSGunnar Mills     dbus::utility::getDbusObject(
835a5d0cedbSGunnar Mills         "/xyz/openbmc_project/state/hypervisor0", interfaces,
836a5d0cedbSGunnar Mills         [asyncResp](
837a5d0cedbSGunnar Mills             const boost::system::error_code& ec,
838a5d0cedbSGunnar Mills             const std::vector<std::pair<std::string, std::vector<std::string>>>&
839a5d0cedbSGunnar Mills                 objInfo) {
840a5d0cedbSGunnar Mills         if (ec)
841a5d0cedbSGunnar Mills         {
84262598e31SEd Tanous             BMCWEB_LOG_DEBUG("DBUS response error {}", ec);
843a5d0cedbSGunnar Mills 
844a5d0cedbSGunnar Mills             // No hypervisor objects found by mapper
845a5d0cedbSGunnar Mills             if (ec.value() == boost::system::errc::io_error)
846a5d0cedbSGunnar Mills             {
847a5d0cedbSGunnar Mills                 messages::resourceNotFound(asyncResp->res, "hypervisor",
848a5d0cedbSGunnar Mills                                            "ResetActionInfo");
849a5d0cedbSGunnar Mills                 return;
850a5d0cedbSGunnar Mills             }
851a5d0cedbSGunnar Mills 
852a5d0cedbSGunnar Mills             messages::internalError(asyncResp->res);
853a5d0cedbSGunnar Mills             return;
854a5d0cedbSGunnar Mills         }
855a5d0cedbSGunnar Mills 
856a5d0cedbSGunnar Mills         // One and only one hypervisor instance supported
857a5d0cedbSGunnar Mills         if (objInfo.size() != 1)
858a5d0cedbSGunnar Mills         {
859a5d0cedbSGunnar Mills             messages::internalError(asyncResp->res);
860a5d0cedbSGunnar Mills             return;
861a5d0cedbSGunnar Mills         }
862a5d0cedbSGunnar Mills 
863a5d0cedbSGunnar Mills         // The hypervisor object only support the ability to
864a5d0cedbSGunnar Mills         // turn On The system object Action should be utilized
865a5d0cedbSGunnar Mills         // for other operations
866a5d0cedbSGunnar Mills 
867a5d0cedbSGunnar Mills         asyncResp->res.jsonValue["@odata.type"] =
868a5d0cedbSGunnar Mills             "#ActionInfo.v1_1_2.ActionInfo";
869a5d0cedbSGunnar Mills         asyncResp->res.jsonValue["@odata.id"] =
870a5d0cedbSGunnar Mills             "/redfish/v1/Systems/hypervisor/ResetActionInfo";
871a5d0cedbSGunnar Mills         asyncResp->res.jsonValue["Name"] = "Reset Action Info";
872a5d0cedbSGunnar Mills         asyncResp->res.jsonValue["Id"] = "ResetActionInfo";
873a5d0cedbSGunnar Mills         nlohmann::json::array_t parameters;
874a5d0cedbSGunnar Mills         nlohmann::json::object_t parameter;
875a5d0cedbSGunnar Mills         parameter["Name"] = "ResetType";
876a5d0cedbSGunnar Mills         parameter["Required"] = true;
877a5d0cedbSGunnar Mills         parameter["DataType"] = "String";
878a5d0cedbSGunnar Mills         nlohmann::json::array_t allowed;
879ad539545SPatrick Williams         allowed.emplace_back("On");
880a5d0cedbSGunnar Mills         parameter["AllowableValues"] = std::move(allowed);
881ad539545SPatrick Williams         parameters.emplace_back(std::move(parameter));
882a5d0cedbSGunnar Mills         asyncResp->res.jsonValue["Parameters"] = std::move(parameters);
883a5d0cedbSGunnar Mills     });
884a5d0cedbSGunnar Mills }
885a5d0cedbSGunnar Mills 
886a5d0cedbSGunnar Mills inline void handleHypervisorSystemResetPost(
887a5d0cedbSGunnar Mills     App& app, const crow::Request& req,
888a5d0cedbSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
889a5d0cedbSGunnar Mills {
890a5d0cedbSGunnar Mills     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
891a5d0cedbSGunnar Mills     {
892a5d0cedbSGunnar Mills         return;
893a5d0cedbSGunnar Mills     }
894a5d0cedbSGunnar Mills     std::optional<std::string> resetType;
895a5d0cedbSGunnar Mills     if (!json_util::readJsonAction(req, asyncResp->res, "ResetType", resetType))
896a5d0cedbSGunnar Mills     {
897a5d0cedbSGunnar Mills         // readJson adds appropriate error to response
898a5d0cedbSGunnar Mills         return;
899a5d0cedbSGunnar Mills     }
900a5d0cedbSGunnar Mills 
901a5d0cedbSGunnar Mills     if (!resetType)
902a5d0cedbSGunnar Mills     {
903a5d0cedbSGunnar Mills         messages::actionParameterMissing(asyncResp->res, "ComputerSystem.Reset",
904a5d0cedbSGunnar Mills                                          "ResetType");
905a5d0cedbSGunnar Mills         return;
906a5d0cedbSGunnar Mills     }
907a5d0cedbSGunnar Mills 
908a5d0cedbSGunnar Mills     // Hypervisor object only support On operation
909a5d0cedbSGunnar Mills     if (resetType != "On")
910a5d0cedbSGunnar Mills     {
911a5d0cedbSGunnar Mills         messages::propertyValueNotInList(asyncResp->res, *resetType,
912a5d0cedbSGunnar Mills                                          "ResetType");
913a5d0cedbSGunnar Mills         return;
914a5d0cedbSGunnar Mills     }
915a5d0cedbSGunnar Mills 
916a5d0cedbSGunnar Mills     std::string command = "xyz.openbmc_project.State.Host.Transition.On";
917a5d0cedbSGunnar Mills 
9189ae226faSGeorge Liu     sdbusplus::asio::setProperty(
9199ae226faSGeorge Liu         *crow::connections::systemBus, "xyz.openbmc_project.State.Hypervisor",
9209ae226faSGeorge Liu         "/xyz/openbmc_project/state/hypervisor0",
9219ae226faSGeorge Liu         "xyz.openbmc_project.State.Host", "RequestedHostTransition", command,
922a5d0cedbSGunnar Mills         [asyncResp, resetType](const boost::system::error_code& ec) {
923a5d0cedbSGunnar Mills         if (ec)
924a5d0cedbSGunnar Mills         {
92562598e31SEd Tanous             BMCWEB_LOG_ERROR("D-Bus responses error: {}", ec);
926a5d0cedbSGunnar Mills             if (ec.value() == boost::asio::error::invalid_argument)
927a5d0cedbSGunnar Mills             {
928a5d0cedbSGunnar Mills                 messages::actionParameterNotSupported(asyncResp->res,
929a5d0cedbSGunnar Mills                                                       *resetType, "Reset");
930a5d0cedbSGunnar Mills                 return;
931a5d0cedbSGunnar Mills             }
932a5d0cedbSGunnar Mills 
933a5d0cedbSGunnar Mills             if (ec.value() == boost::asio::error::host_unreachable)
934a5d0cedbSGunnar Mills             {
935a5d0cedbSGunnar Mills                 messages::resourceNotFound(asyncResp->res, "Actions", "Reset");
936a5d0cedbSGunnar Mills                 return;
937a5d0cedbSGunnar Mills             }
938a5d0cedbSGunnar Mills 
939a5d0cedbSGunnar Mills             messages::internalError(asyncResp->res);
940a5d0cedbSGunnar Mills             return;
941a5d0cedbSGunnar Mills         }
942a5d0cedbSGunnar Mills         messages::success(asyncResp->res);
9439ae226faSGeorge Liu     });
944a5d0cedbSGunnar Mills }
945a5d0cedbSGunnar Mills 
9467e860f15SJohn Edward Broadbent inline void requestRoutesHypervisorSystems(App& app)
9477e860f15SJohn Edward Broadbent {
9487e860f15SJohn Edward Broadbent     /**
9497e860f15SJohn Edward Broadbent      * HypervisorInterfaceCollection class to handle the GET and PATCH on
9507e860f15SJohn Edward Broadbent      * Hypervisor Interface
9517e860f15SJohn Edward Broadbent      */
9527e860f15SJohn Edward Broadbent 
9537e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app, "/redfish/v1/Systems/hypervisor/EthernetInterfaces/")
954ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
955f40448e5SGunnar Mills         .methods(boost::beast::http::verb::get)(std::bind_front(
956f40448e5SGunnar Mills             handleHypervisorEthernetInterfaceCollectionGet, std::ref(app)));
9577e860f15SJohn Edward Broadbent 
9587e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9597e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
960ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
961f40448e5SGunnar Mills         .methods(boost::beast::http::verb::get)(std::bind_front(
962f40448e5SGunnar Mills             handleHypervisorEthernetInterfaceGet, std::ref(app)));
963d5afb2caSAndrew Geissler 
9647e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9657e860f15SJohn Edward Broadbent                  "/redfish/v1/Systems/hypervisor/EthernetInterfaces/<str>/")
966ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
967f40448e5SGunnar Mills         .methods(boost::beast::http::verb::patch)(std::bind_front(
968f40448e5SGunnar Mills             handleHypervisorEthernetInterfacePatch, std::ref(app)));
9694fbaf64aSAndrew Geissler 
9707e860f15SJohn Edward Broadbent     BMCWEB_ROUTE(app,
9714fbaf64aSAndrew Geissler                  "/redfish/v1/Systems/hypervisor/Actions/ComputerSystem.Reset/")
972ed398213SEd Tanous         .privileges(redfish::privileges::postComputerSystem)
9737e860f15SJohn Edward Broadbent         .methods(boost::beast::http::verb::post)(
974a5d0cedbSGunnar Mills             std::bind_front(handleHypervisorSystemResetPost, std::ref(app)));
9754fbaf64aSAndrew Geissler }
97688a8a174SEd Tanous } // namespace redfish
977