xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 5e7e2dc585dc7a7e62d2b648b541e7f50c39ea5d)
19391bb9cSRapkiewicz, Pawel /*
29391bb9cSRapkiewicz, Pawel // Copyright (c) 2018 Intel Corporation
39391bb9cSRapkiewicz, Pawel //
49391bb9cSRapkiewicz, Pawel // Licensed under the Apache License, Version 2.0 (the "License");
59391bb9cSRapkiewicz, Pawel // you may not use this file except in compliance with the License.
69391bb9cSRapkiewicz, Pawel // You may obtain a copy of the License at
79391bb9cSRapkiewicz, Pawel //
89391bb9cSRapkiewicz, Pawel //      http://www.apache.org/licenses/LICENSE-2.0
99391bb9cSRapkiewicz, Pawel //
109391bb9cSRapkiewicz, Pawel // Unless required by applicable law or agreed to in writing, software
119391bb9cSRapkiewicz, Pawel // distributed under the License is distributed on an "AS IS" BASIS,
129391bb9cSRapkiewicz, Pawel // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
139391bb9cSRapkiewicz, Pawel // See the License for the specific language governing permissions and
149391bb9cSRapkiewicz, Pawel // limitations under the License.
159391bb9cSRapkiewicz, Pawel */
169391bb9cSRapkiewicz, Pawel #pragma once
179391bb9cSRapkiewicz, Pawel 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_singleton.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
213ccb3adbSEd Tanous #include "error_messages.hpp"
223ccb3adbSEd Tanous #include "health.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
25033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
263ccb3adbSEd Tanous #include "utils/json_utils.hpp"
27033f1e4dSEd Tanous 
2811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
2911ba3979SEd Tanous #include <boost/algorithm/string/split.hpp>
304a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
311214b7e7SGunnar Mills 
327a1dbc48SGeorge Liu #include <array>
33a24526dcSEd Tanous #include <optional>
34ab6554f1SJoshi-Mansi #include <regex>
357a1dbc48SGeorge Liu #include <string_view>
369391bb9cSRapkiewicz, Pawel 
371abe55efSEd Tanous namespace redfish
381abe55efSEd Tanous {
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous enum class LinkType
414a0cb85cSEd Tanous {
424a0cb85cSEd Tanous     Local,
434a0cb85cSEd Tanous     Global
444a0cb85cSEd Tanous };
459391bb9cSRapkiewicz, Pawel 
469391bb9cSRapkiewicz, Pawel /**
479391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
489391bb9cSRapkiewicz, Pawel  */
491abe55efSEd Tanous struct IPv4AddressData
501abe55efSEd Tanous {
51179db1d7SKowalski, Kamil     std::string id;
524a0cb85cSEd Tanous     std::string address;
534a0cb85cSEd Tanous     std::string domain;
544a0cb85cSEd Tanous     std::string gateway;
559391bb9cSRapkiewicz, Pawel     std::string netmask;
569391bb9cSRapkiewicz, Pawel     std::string origin;
574a0cb85cSEd Tanous     LinkType linktype;
5801c6e858SSunitha Harish     bool isActive;
594a0cb85cSEd Tanous 
601abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
611abe55efSEd Tanous     {
624a0cb85cSEd Tanous         return id < obj.id;
631abe55efSEd Tanous     }
649391bb9cSRapkiewicz, Pawel };
659391bb9cSRapkiewicz, Pawel 
669391bb9cSRapkiewicz, Pawel /**
67e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
68e48c0fc5SRavi Teja  */
69e48c0fc5SRavi Teja struct IPv6AddressData
70e48c0fc5SRavi Teja {
71e48c0fc5SRavi Teja     std::string id;
72e48c0fc5SRavi Teja     std::string address;
73e48c0fc5SRavi Teja     std::string origin;
74e48c0fc5SRavi Teja     uint8_t prefixLength;
75e48c0fc5SRavi Teja 
76e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
77e48c0fc5SRavi Teja     {
78e48c0fc5SRavi Teja         return id < obj.id;
79e48c0fc5SRavi Teja     }
80e48c0fc5SRavi Teja };
81e48c0fc5SRavi Teja /**
829391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
839391bb9cSRapkiewicz, Pawel  * available from DBus
849391bb9cSRapkiewicz, Pawel  */
851abe55efSEd Tanous struct EthernetInterfaceData
861abe55efSEd Tanous {
874a0cb85cSEd Tanous     uint32_t speed;
8835fb5311STejas Patil     size_t mtuSize;
8982695a5bSJiaqing Zhao     bool autoNeg;
9082695a5bSJiaqing Zhao     bool dnsEnabled;
9182695a5bSJiaqing Zhao     bool ntpEnabled;
9282695a5bSJiaqing Zhao     bool hostNameEnabled;
93aa05fb27SJohnathan Mantey     bool linkUp;
94eeedda23SJohnathan Mantey     bool nicEnabled;
9582695a5bSJiaqing Zhao     std::string dhcpEnabled;
961f8c7b5dSJohnathan Mantey     std::string operatingMode;
9782695a5bSJiaqing Zhao     std::string hostName;
9882695a5bSJiaqing Zhao     std::string defaultGateway;
9982695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
10082695a5bSJiaqing Zhao     std::string macAddress;
10117e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
1020f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1030f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
104d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1059391bb9cSRapkiewicz, Pawel };
1069391bb9cSRapkiewicz, Pawel 
1071f8c7b5dSJohnathan Mantey struct DHCPParameters
1081f8c7b5dSJohnathan Mantey {
1091f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
11082695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
11182695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
11282695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1131f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1141f8c7b5dSJohnathan Mantey };
1151f8c7b5dSJohnathan Mantey 
1169391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1179391bb9cSRapkiewicz, Pawel // into full dot notation
1181abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1191abe55efSEd Tanous {
1209391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1219391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1229391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1249391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1259391bb9cSRapkiewicz, Pawel     return netmask;
1269391bb9cSRapkiewicz, Pawel }
1279391bb9cSRapkiewicz, Pawel 
12882695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1291f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1301f8c7b5dSJohnathan Mantey {
1311f8c7b5dSJohnathan Mantey     if (isIPv4)
1321f8c7b5dSJohnathan Mantey     {
1331f8c7b5dSJohnathan Mantey         return (
1341f8c7b5dSJohnathan Mantey             (inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1361f8c7b5dSJohnathan Mantey             (inputDHCP ==
1371f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1381f8c7b5dSJohnathan Mantey     }
1391f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1401f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1411f8c7b5dSJohnathan Mantey             (inputDHCP ==
1421f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1431f8c7b5dSJohnathan Mantey }
1441f8c7b5dSJohnathan Mantey 
1452c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1461f8c7b5dSJohnathan Mantey {
1471f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1481f8c7b5dSJohnathan Mantey     {
1491f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1501f8c7b5dSJohnathan Mantey     }
1513174e4dfSEd Tanous     if (isIPv4)
1521f8c7b5dSJohnathan Mantey     {
1531f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1541f8c7b5dSJohnathan Mantey     }
1553174e4dfSEd Tanous     if (isIPv6)
1561f8c7b5dSJohnathan Mantey     {
1571f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1581f8c7b5dSJohnathan Mantey     }
1591f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1601f8c7b5dSJohnathan Mantey }
1611f8c7b5dSJohnathan Mantey 
1624a0cb85cSEd Tanous inline std::string
1634a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1644a0cb85cSEd Tanous                                         bool isIPv4)
1651abe55efSEd Tanous {
1664a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1671abe55efSEd Tanous     {
1684a0cb85cSEd Tanous         return "Static";
1699391bb9cSRapkiewicz, Pawel     }
1704a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1711abe55efSEd Tanous     {
1724a0cb85cSEd Tanous         if (isIPv4)
1731abe55efSEd Tanous         {
1744a0cb85cSEd Tanous             return "IPv4LinkLocal";
1751abe55efSEd Tanous         }
1764a0cb85cSEd Tanous         return "LinkLocal";
1779391bb9cSRapkiewicz, Pawel     }
1784a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1791abe55efSEd Tanous     {
1804a0cb85cSEd Tanous         if (isIPv4)
1814a0cb85cSEd Tanous         {
1824a0cb85cSEd Tanous             return "DHCP";
1834a0cb85cSEd Tanous         }
1844a0cb85cSEd Tanous         return "DHCPv6";
1854a0cb85cSEd Tanous     }
1864a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1874a0cb85cSEd Tanous     {
1884a0cb85cSEd Tanous         return "SLAAC";
1894a0cb85cSEd Tanous     }
1904a0cb85cSEd Tanous     return "";
1914a0cb85cSEd Tanous }
1924a0cb85cSEd Tanous 
19302cad96eSEd Tanous inline bool extractEthernetInterfaceData(
19402cad96eSEd Tanous     const std::string& ethifaceId,
19502cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1964a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1974a0cb85cSEd Tanous {
1984c9afe43SEd Tanous     bool idFound = false;
19902cad96eSEd Tanous     for (const auto& objpath : dbusData)
2004a0cb85cSEd Tanous     {
20102cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
2024a0cb85cSEd Tanous         {
20381ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
204029573d4SEd Tanous             {
2054c9afe43SEd Tanous                 idFound = true;
2064a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2074a0cb85cSEd Tanous                 {
2084a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2094a0cb85cSEd Tanous                     {
2104a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2114a0cb85cSEd Tanous                         {
2124a0cb85cSEd Tanous                             const std::string* mac =
213abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2144a0cb85cSEd Tanous                             if (mac != nullptr)
2154a0cb85cSEd Tanous                             {
21682695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2174a0cb85cSEd Tanous                             }
2184a0cb85cSEd Tanous                         }
2194a0cb85cSEd Tanous                     }
2204a0cb85cSEd Tanous                 }
2214a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2224a0cb85cSEd Tanous                 {
2234a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2244a0cb85cSEd Tanous                     {
2254a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2264a0cb85cSEd Tanous                         {
2271b6b96c5SEd Tanous                             const uint32_t* id =
228abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2294a0cb85cSEd Tanous                             if (id != nullptr)
2304a0cb85cSEd Tanous                             {
23117e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2324a0cb85cSEd Tanous                             }
2334a0cb85cSEd Tanous                         }
2344a0cb85cSEd Tanous                     }
2354a0cb85cSEd Tanous                 }
2364a0cb85cSEd Tanous                 else if (ifacePair.first ==
2374a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2384a0cb85cSEd Tanous                 {
2394a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2404a0cb85cSEd Tanous                     {
2414a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2424a0cb85cSEd Tanous                         {
2432c70f800SEd Tanous                             const bool* autoNeg =
244abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2452c70f800SEd Tanous                             if (autoNeg != nullptr)
2464a0cb85cSEd Tanous                             {
24782695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2484a0cb85cSEd Tanous                             }
2494a0cb85cSEd Tanous                         }
2504a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2514a0cb85cSEd Tanous                         {
2524a0cb85cSEd Tanous                             const uint32_t* speed =
253abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2544a0cb85cSEd Tanous                             if (speed != nullptr)
2554a0cb85cSEd Tanous                             {
2564a0cb85cSEd Tanous                                 ethData.speed = *speed;
2574a0cb85cSEd Tanous                             }
2584a0cb85cSEd Tanous                         }
25935fb5311STejas Patil                         else if (propertyPair.first == "MTU")
26035fb5311STejas Patil                         {
26135fb5311STejas Patil                             const uint32_t* mtuSize =
26235fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
26335fb5311STejas Patil                             if (mtuSize != nullptr)
26435fb5311STejas Patil                             {
26535fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26635fb5311STejas Patil                             }
26735fb5311STejas Patil                         }
268aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
269aa05fb27SJohnathan Mantey                         {
270aa05fb27SJohnathan Mantey                             const bool* linkUp =
271aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
273aa05fb27SJohnathan Mantey                             {
274aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
275aa05fb27SJohnathan Mantey                             }
276aa05fb27SJohnathan Mantey                         }
277eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
278eeedda23SJohnathan Mantey                         {
279eeedda23SJohnathan Mantey                             const bool* nicEnabled =
280eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
281eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
282eeedda23SJohnathan Mantey                             {
283eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
284eeedda23SJohnathan Mantey                             }
285eeedda23SJohnathan Mantey                         }
286f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
287029573d4SEd Tanous                         {
288029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2898d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
290029573d4SEd Tanous                                     &propertyPair.second);
291029573d4SEd Tanous                             if (nameservers != nullptr)
292029573d4SEd Tanous                             {
293f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2940f6efdc1Smanojkiran.eda@gmail.com                             }
2950f6efdc1Smanojkiran.eda@gmail.com                         }
2960f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2970f6efdc1Smanojkiran.eda@gmail.com                         {
2980f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2998d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3000f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3010f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3020f6efdc1Smanojkiran.eda@gmail.com                             {
303f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3044a0cb85cSEd Tanous                             }
3054a0cb85cSEd Tanous                         }
3062a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3072a133282Smanojkiraneda                         {
3082c70f800SEd Tanous                             const std::string* dhcpEnabled =
3091f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3102c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3112a133282Smanojkiraneda                             {
31282695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3132a133282Smanojkiraneda                             }
3142a133282Smanojkiraneda                         }
315d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
316d24bfc7aSJennifer Lee                         {
317d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3188d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
319d24bfc7aSJennifer Lee                                     &propertyPair.second);
320d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
321d24bfc7aSJennifer Lee                             {
322f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
323d24bfc7aSJennifer Lee                             }
324d24bfc7aSJennifer Lee                         }
3259010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3269010ec2eSRavi Teja                         {
3279010ec2eSRavi Teja                             const std::string* defaultGateway =
3289010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3299010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3309010ec2eSRavi Teja                             {
3319010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3329010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3339010ec2eSRavi Teja                                 {
33482695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3359010ec2eSRavi Teja                                 }
3369010ec2eSRavi Teja                                 else
3379010ec2eSRavi Teja                                 {
33882695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3399010ec2eSRavi Teja                                 }
3409010ec2eSRavi Teja                             }
3419010ec2eSRavi Teja                         }
3429010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3439010ec2eSRavi Teja                         {
3449010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3459010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3469010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3479010ec2eSRavi Teja                             {
3489010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3499010ec2eSRavi Teja                                     *defaultGateway6;
3509010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3519010ec2eSRavi Teja                                 {
35282695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3539010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3549010ec2eSRavi Teja                                 }
3559010ec2eSRavi Teja                                 else
3569010ec2eSRavi Teja                                 {
35782695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3589010ec2eSRavi Teja                                         defaultGateway6Str;
3599010ec2eSRavi Teja                                 }
3609010ec2eSRavi Teja                             }
3619010ec2eSRavi Teja                         }
362029573d4SEd Tanous                     }
363029573d4SEd Tanous                 }
364029573d4SEd Tanous             }
3651f8c7b5dSJohnathan Mantey 
3661e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3671f8c7b5dSJohnathan Mantey             {
3681f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3691f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3701f8c7b5dSJohnathan Mantey                 {
3711f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3721f8c7b5dSJohnathan Mantey                     {
3731f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3741f8c7b5dSJohnathan Mantey                         {
3752c70f800SEd Tanous                             const bool* dnsEnabled =
3761f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3772c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3781f8c7b5dSJohnathan Mantey                             {
37982695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3801f8c7b5dSJohnathan Mantey                             }
3811f8c7b5dSJohnathan Mantey                         }
3821f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3831f8c7b5dSJohnathan Mantey                         {
3842c70f800SEd Tanous                             const bool* ntpEnabled =
3851f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3862c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3871f8c7b5dSJohnathan Mantey                             {
38882695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3891f8c7b5dSJohnathan Mantey                             }
3901f8c7b5dSJohnathan Mantey                         }
3911f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3921f8c7b5dSJohnathan Mantey                         {
3932c70f800SEd Tanous                             const bool* hostNameEnabled =
3941f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3952c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3961f8c7b5dSJohnathan Mantey                             {
39782695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3981f8c7b5dSJohnathan Mantey                             }
3991f8c7b5dSJohnathan Mantey                         }
4001f8c7b5dSJohnathan Mantey                     }
4011f8c7b5dSJohnathan Mantey                 }
4021f8c7b5dSJohnathan Mantey             }
403029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
404029573d4SEd Tanous             // to check eth number
405029573d4SEd Tanous             if (ifacePair.first ==
4064a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4074a0cb85cSEd Tanous             {
4084a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4094a0cb85cSEd Tanous                 {
4104a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4114a0cb85cSEd Tanous                     {
4124a0cb85cSEd Tanous                         const std::string* hostname =
4138d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4144a0cb85cSEd Tanous                         if (hostname != nullptr)
4154a0cb85cSEd Tanous                         {
41682695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4174a0cb85cSEd Tanous                         }
4184a0cb85cSEd Tanous                     }
4194a0cb85cSEd Tanous                 }
4204a0cb85cSEd Tanous             }
4214a0cb85cSEd Tanous         }
4224a0cb85cSEd Tanous     }
4234c9afe43SEd Tanous     return idFound;
4244a0cb85cSEd Tanous }
4254a0cb85cSEd Tanous 
426e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42701784826SJohnathan Mantey inline void
42881ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
429711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43081ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
431e48c0fc5SRavi Teja {
432353163e9STony Lee     const std::string ipPathStart =
433353163e9STony Lee         "/xyz/openbmc_project/network/" + ethifaceId;
434e48c0fc5SRavi Teja 
435e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
436e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43781ce609eSEd Tanous     for (const auto& objpath : dbusData)
438e48c0fc5SRavi Teja     {
439e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
440353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
441e48c0fc5SRavi Teja         {
4429eb808c1SEd Tanous             for (const auto& interface : objpath.second)
443e48c0fc5SRavi Teja             {
444e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
445e48c0fc5SRavi Teja                 {
446353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
447353163e9STony Lee                                              interface.second.end(),
448353163e9STony Lee                                              [](const auto& property) {
449353163e9STony Lee                         return property.first == "Type";
450353163e9STony Lee                     });
451353163e9STony Lee                     if (type == interface.second.end())
452353163e9STony Lee                     {
453353163e9STony Lee                         continue;
454353163e9STony Lee                     }
455353163e9STony Lee 
456353163e9STony Lee                     const std::string* typeStr =
457353163e9STony Lee                         std::get_if<std::string>(&type->second);
458353163e9STony Lee 
459353163e9STony Lee                     if (typeStr == nullptr ||
460353163e9STony Lee                         (*typeStr !=
461353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
462353163e9STony Lee                     {
463353163e9STony Lee                         continue;
464353163e9STony Lee                     }
465353163e9STony Lee 
466e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
467e48c0fc5SRavi Teja                     // appropriate
468e48c0fc5SRavi Teja                     std::pair<
469e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
470e48c0fc5SRavi Teja                         bool>
47181ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4722c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4732c70f800SEd Tanous                     ipv6Address.id =
474353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
4759eb808c1SEd Tanous                     for (const auto& property : interface.second)
476e48c0fc5SRavi Teja                     {
477e48c0fc5SRavi Teja                         if (property.first == "Address")
478e48c0fc5SRavi Teja                         {
479e48c0fc5SRavi Teja                             const std::string* address =
480e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
481e48c0fc5SRavi Teja                             if (address != nullptr)
482e48c0fc5SRavi Teja                             {
4832c70f800SEd Tanous                                 ipv6Address.address = *address;
484e48c0fc5SRavi Teja                             }
485e48c0fc5SRavi Teja                         }
486e48c0fc5SRavi Teja                         else if (property.first == "Origin")
487e48c0fc5SRavi Teja                         {
488e48c0fc5SRavi Teja                             const std::string* origin =
489e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
490e48c0fc5SRavi Teja                             if (origin != nullptr)
491e48c0fc5SRavi Teja                             {
4922c70f800SEd Tanous                                 ipv6Address.origin =
493e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
494e48c0fc5SRavi Teja                                                                         false);
495e48c0fc5SRavi Teja                             }
496e48c0fc5SRavi Teja                         }
497e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
498e48c0fc5SRavi Teja                         {
499e48c0fc5SRavi Teja                             const uint8_t* prefix =
500e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
501e48c0fc5SRavi Teja                             if (prefix != nullptr)
502e48c0fc5SRavi Teja                             {
5032c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
504e48c0fc5SRavi Teja                             }
505e48c0fc5SRavi Teja                         }
506889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
507889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
508889ff694SAsmitha Karunanithi                         {
509889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
510889ff694SAsmitha Karunanithi                         }
511e48c0fc5SRavi Teja                         else
512e48c0fc5SRavi Teja                         {
513e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
514e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
515e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
516e48c0fc5SRavi Teja                         }
517e48c0fc5SRavi Teja                     }
518e48c0fc5SRavi Teja                 }
519e48c0fc5SRavi Teja             }
520e48c0fc5SRavi Teja         }
521e48c0fc5SRavi Teja     }
522e48c0fc5SRavi Teja }
523e48c0fc5SRavi Teja 
5244a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
52501784826SJohnathan Mantey inline void
52681ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
527711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
52881ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5294a0cb85cSEd Tanous {
530353163e9STony Lee     const std::string ipPathStart =
531353163e9STony Lee         "/xyz/openbmc_project/network/" + ethifaceId;
5324a0cb85cSEd Tanous 
5334a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5344a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
53581ce609eSEd Tanous     for (const auto& objpath : dbusData)
5364a0cb85cSEd Tanous     {
5374a0cb85cSEd Tanous         // Check if proper pattern for object path appears
538353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
5394a0cb85cSEd Tanous         {
5409eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5414a0cb85cSEd Tanous             {
5424a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5434a0cb85cSEd Tanous                 {
544353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
545353163e9STony Lee                                              interface.second.end(),
546353163e9STony Lee                                              [](const auto& property) {
547353163e9STony Lee                         return property.first == "Type";
548353163e9STony Lee                     });
549353163e9STony Lee                     if (type == interface.second.end())
550353163e9STony Lee                     {
551353163e9STony Lee                         continue;
552353163e9STony Lee                     }
553353163e9STony Lee 
554353163e9STony Lee                     const std::string* typeStr =
555353163e9STony Lee                         std::get_if<std::string>(&type->second);
556353163e9STony Lee 
557353163e9STony Lee                     if (typeStr == nullptr ||
558353163e9STony Lee                         (*typeStr !=
559353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
560353163e9STony Lee                     {
561353163e9STony Lee                         continue;
562353163e9STony Lee                     }
563353163e9STony Lee 
5644a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5654a0cb85cSEd Tanous                     // appropriate
5664a0cb85cSEd Tanous                     std::pair<
5674a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5684a0cb85cSEd Tanous                         bool>
56981ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5702c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5712c70f800SEd Tanous                     ipv4Address.id =
572353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
5739eb808c1SEd Tanous                     for (const auto& property : interface.second)
5744a0cb85cSEd Tanous                     {
5754a0cb85cSEd Tanous                         if (property.first == "Address")
5764a0cb85cSEd Tanous                         {
5774a0cb85cSEd Tanous                             const std::string* address =
578abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5794a0cb85cSEd Tanous                             if (address != nullptr)
5804a0cb85cSEd Tanous                             {
5812c70f800SEd Tanous                                 ipv4Address.address = *address;
5824a0cb85cSEd Tanous                             }
5834a0cb85cSEd Tanous                         }
5844a0cb85cSEd Tanous                         else if (property.first == "Origin")
5854a0cb85cSEd Tanous                         {
5864a0cb85cSEd Tanous                             const std::string* origin =
587abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5884a0cb85cSEd Tanous                             if (origin != nullptr)
5894a0cb85cSEd Tanous                             {
5902c70f800SEd Tanous                                 ipv4Address.origin =
5914a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5924a0cb85cSEd Tanous                                                                         true);
5934a0cb85cSEd Tanous                             }
5944a0cb85cSEd Tanous                         }
5954a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5964a0cb85cSEd Tanous                         {
5974a0cb85cSEd Tanous                             const uint8_t* mask =
598abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5994a0cb85cSEd Tanous                             if (mask != nullptr)
6004a0cb85cSEd Tanous                             {
6014a0cb85cSEd Tanous                                 // convert it to the string
6022c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
6034a0cb85cSEd Tanous                             }
6044a0cb85cSEd Tanous                         }
605889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
606889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
607889ff694SAsmitha Karunanithi                         {
608889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
609889ff694SAsmitha Karunanithi                         }
6104a0cb85cSEd Tanous                         else
6114a0cb85cSEd Tanous                         {
6124a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
6134a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
6144a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
6154a0cb85cSEd Tanous                         }
6164a0cb85cSEd Tanous                     }
6174a0cb85cSEd Tanous                     // Check if given address is local, or global
6182c70f800SEd Tanous                     ipv4Address.linktype =
61911ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
62018659d10SJohnathan Mantey                             ? LinkType::Local
62118659d10SJohnathan Mantey                             : LinkType::Global;
6224a0cb85cSEd Tanous                 }
6234a0cb85cSEd Tanous             }
6244a0cb85cSEd Tanous         }
6254a0cb85cSEd Tanous     }
6264a0cb85cSEd Tanous }
627588c3f0dSKowalski, Kamil 
628588c3f0dSKowalski, Kamil /**
62901784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
630179db1d7SKowalski, Kamil  *
631179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
632179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
633179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
634179db1d7SKowalski, Kamil  *
635179db1d7SKowalski, Kamil  * @return None
636179db1d7SKowalski, Kamil  */
6374a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
6388d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6391abe55efSEd Tanous {
64055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
641*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6421abe55efSEd Tanous         if (ec)
6431abe55efSEd Tanous         {
644a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6451abe55efSEd Tanous         }
646179db1d7SKowalski, Kamil         },
647179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
648179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
649179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
650179db1d7SKowalski, Kamil }
651179db1d7SKowalski, Kamil 
652244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
653244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
654244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6559010ec2eSRavi Teja {
6569010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
657*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6589010ec2eSRavi Teja         if (ec)
6599010ec2eSRavi Teja         {
6609010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6619010ec2eSRavi Teja             return;
6629010ec2eSRavi Teja         }
6639010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6649010ec2eSRavi Teja         },
6659010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6669010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6679010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6689010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
669168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6709010ec2eSRavi Teja }
671179db1d7SKowalski, Kamil /**
67201784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
673179db1d7SKowalski, Kamil  *
67401784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
67501784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
67601784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
67701784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
678179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
679179db1d7SKowalski, Kamil  *
680179db1d7SKowalski, Kamil  * @return None
681179db1d7SKowalski, Kamil  */
682cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
683cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6848d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6851abe55efSEd Tanous {
686002d39b4SEd Tanous     auto createIpHandler =
687*5e7e2dc5SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
6881abe55efSEd Tanous         if (ec)
6891abe55efSEd Tanous         {
690a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6919010ec2eSRavi Teja             return;
692179db1d7SKowalski, Kamil         }
6939010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6949010ec2eSRavi Teja     };
6959010ec2eSRavi Teja 
6969010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6979010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
698179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
699179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
70001784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
701179db1d7SKowalski, Kamil         gateway);
702179db1d7SKowalski, Kamil }
703e48c0fc5SRavi Teja 
704e48c0fc5SRavi Teja /**
70501784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
70601784826SJohnathan Mantey  * static IPv4 entry
70701784826SJohnathan Mantey  *
70801784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
70901784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
71001784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
71101784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
71201784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
71301784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
71401784826SJohnathan Mantey  *
71501784826SJohnathan Mantey  * @return None
71601784826SJohnathan Mantey  */
7178d1b46d7Szhanghch05 inline void
7188d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7198d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
72001784826SJohnathan Mantey                         const std::string& address,
7218d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
72201784826SJohnathan Mantey {
72301784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
72401784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
725*5e7e2dc5SEd Tanous          gateway](const boost::system::error_code& ec) {
72601784826SJohnathan Mantey         if (ec)
72701784826SJohnathan Mantey         {
72801784826SJohnathan Mantey             messages::internalError(asyncResp->res);
7299010ec2eSRavi Teja             return;
73001784826SJohnathan Mantey         }
7319010ec2eSRavi Teja 
73201784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
733*5e7e2dc5SEd Tanous             [asyncResp, ifaceId,
734*5e7e2dc5SEd Tanous              gateway](const boost::system::error_code& ec2) {
73523a21a1cSEd Tanous             if (ec2)
73601784826SJohnathan Mantey             {
73701784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
7389010ec2eSRavi Teja                 return;
73901784826SJohnathan Mantey             }
7409010ec2eSRavi Teja             updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
74101784826SJohnathan Mantey             },
74201784826SJohnathan Mantey             "xyz.openbmc_project.Network",
74301784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
74401784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
74501784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
74601784826SJohnathan Mantey             prefixLength, gateway);
74701784826SJohnathan Mantey         },
74801784826SJohnathan Mantey         "xyz.openbmc_project.Network",
74901784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
75001784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
75101784826SJohnathan Mantey }
75201784826SJohnathan Mantey 
75301784826SJohnathan Mantey /**
754e48c0fc5SRavi Teja  * @brief Deletes given IPv6
755e48c0fc5SRavi Teja  *
756e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
757e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
758e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
759e48c0fc5SRavi Teja  *
760e48c0fc5SRavi Teja  * @return None
761e48c0fc5SRavi Teja  */
762e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
7638d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
764e48c0fc5SRavi Teja {
765e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
766*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
767e48c0fc5SRavi Teja         if (ec)
768e48c0fc5SRavi Teja         {
769e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
770e48c0fc5SRavi Teja         }
771e48c0fc5SRavi Teja         },
772e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
773e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
774e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
775e48c0fc5SRavi Teja }
776e48c0fc5SRavi Teja 
777e48c0fc5SRavi Teja /**
77801784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
77901784826SJohnathan Mantey  * static IPv6 entry
78001784826SJohnathan Mantey  *
78101784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
78201784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
78301784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
78401784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
78501784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
78601784826SJohnathan Mantey  *
78701784826SJohnathan Mantey  * @return None
78801784826SJohnathan Mantey  */
7898d1b46d7Szhanghch05 inline void
7908d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
7918d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
7928d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
79301784826SJohnathan Mantey {
79401784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
79501784826SJohnathan Mantey         [asyncResp, ifaceId, address,
796*5e7e2dc5SEd Tanous          prefixLength](const boost::system::error_code& ec) {
79701784826SJohnathan Mantey         if (ec)
79801784826SJohnathan Mantey         {
79901784826SJohnathan Mantey             messages::internalError(asyncResp->res);
80001784826SJohnathan Mantey         }
80101784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
802*5e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
80323a21a1cSEd Tanous             if (ec2)
80401784826SJohnathan Mantey             {
80501784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
80601784826SJohnathan Mantey             }
80701784826SJohnathan Mantey             },
80801784826SJohnathan Mantey             "xyz.openbmc_project.Network",
80901784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
81001784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
81101784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
81201784826SJohnathan Mantey             prefixLength, "");
81301784826SJohnathan Mantey         },
81401784826SJohnathan Mantey         "xyz.openbmc_project.Network",
81501784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
81601784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
81701784826SJohnathan Mantey }
81801784826SJohnathan Mantey 
81901784826SJohnathan Mantey /**
820e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
821e48c0fc5SRavi Teja  *
822e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
823e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
824e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
825e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
826e48c0fc5SRavi Teja  *
827e48c0fc5SRavi Teja  * @return None
828e48c0fc5SRavi Teja  */
82901784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
83001784826SJohnathan Mantey                        const std::string& address,
8318d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
832e48c0fc5SRavi Teja {
833*5e7e2dc5SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
834e48c0fc5SRavi Teja         if (ec)
835e48c0fc5SRavi Teja         {
836e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
837e48c0fc5SRavi Teja         }
838e48c0fc5SRavi Teja     };
839e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8404e0453b1SGunnar Mills     // does not have associated gateway property
841e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
842e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
843e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
844e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
845e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
846e48c0fc5SRavi Teja         "");
847e48c0fc5SRavi Teja }
848e48c0fc5SRavi Teja 
849179db1d7SKowalski, Kamil /**
850179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
851179db1d7SKowalski, Kamil  * Object
852179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8534a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
854179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
855179db1d7SKowalski, Kamil  * into JSON
856179db1d7SKowalski, Kamil  */
857179db1d7SKowalski, Kamil template <typename CallbackFunc>
85881ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
8591abe55efSEd Tanous                           CallbackFunc&& callback)
8601abe55efSEd Tanous {
86155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
862f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
863f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
864*5e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
86502cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
86655c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
8674a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
868e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
869179db1d7SKowalski, Kamil 
87081ce609eSEd Tanous         if (errorCode)
8711abe55efSEd Tanous         {
87201784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
873179db1d7SKowalski, Kamil             return;
874179db1d7SKowalski, Kamil         }
875179db1d7SKowalski, Kamil 
876002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
8774c9afe43SEd Tanous         if (!found)
8784c9afe43SEd Tanous         {
87901784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8804c9afe43SEd Tanous             return;
8814c9afe43SEd Tanous         }
8824c9afe43SEd Tanous 
8832c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
884179db1d7SKowalski, Kamil         // Fix global GW
8851abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8861abe55efSEd Tanous         {
887c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
888c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8899010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8901abe55efSEd Tanous             {
89182695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
892179db1d7SKowalski, Kamil             }
893179db1d7SKowalski, Kamil         }
894179db1d7SKowalski, Kamil 
8952c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8964e0453b1SGunnar Mills         // Finally make a callback with useful data
89701784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
898179db1d7SKowalski, Kamil         },
899179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
900179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
901271584abSEd Tanous }
902179db1d7SKowalski, Kamil 
903179db1d7SKowalski, Kamil /**
9049391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9059391bb9cSRapkiewicz, Pawel  * Manager
9061abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9071abe55efSEd Tanous  * into JSON.
9089391bb9cSRapkiewicz, Pawel  */
9099391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9101abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9111abe55efSEd Tanous {
91255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
913f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
914*5e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
915711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9161abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
9171abe55efSEd Tanous         // ethernet interfaces
9182c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
9192c70f800SEd Tanous         ifaceList.reserve(resp.size());
92081ce609eSEd Tanous         if (errorCode)
9211abe55efSEd Tanous         {
9222c70f800SEd Tanous             callback(false, ifaceList);
9239391bb9cSRapkiewicz, Pawel             return;
9249391bb9cSRapkiewicz, Pawel         }
9259391bb9cSRapkiewicz, Pawel 
9269391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
9274a0cb85cSEd Tanous         for (const auto& objpath : resp)
9281abe55efSEd Tanous         {
9299391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
9304a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
9311abe55efSEd Tanous             {
9321abe55efSEd Tanous                 // If interface is
9334a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
9344a0cb85cSEd Tanous                 // what we're looking for.
9359391bb9cSRapkiewicz, Pawel                 if (interface.first ==
9361abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
9371abe55efSEd Tanous                 {
9382dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
9392dfd18efSEd Tanous                     if (ifaceId.empty())
9401abe55efSEd Tanous                     {
9412dfd18efSEd Tanous                         continue;
9429391bb9cSRapkiewicz, Pawel                     }
9432dfd18efSEd Tanous                     // and put it into output vector.
9442dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
9459391bb9cSRapkiewicz, Pawel                 }
9469391bb9cSRapkiewicz, Pawel             }
9479391bb9cSRapkiewicz, Pawel         }
948a434f2bdSEd Tanous         // Finally make a callback with useful data
9492c70f800SEd Tanous         callback(true, ifaceList);
9509391bb9cSRapkiewicz, Pawel         },
951aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
952aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
953271584abSEd Tanous }
9549391bb9cSRapkiewicz, Pawel 
9554f48d5f6SEd Tanous inline void
9564f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
9578d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9581abe55efSEd Tanous {
959ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
960ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
961ab6554f1SJoshi-Mansi     {
962ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
963ab6554f1SJoshi-Mansi                                            "HostName");
964ab6554f1SJoshi-Mansi         return;
965ab6554f1SJoshi-Mansi     }
966bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
967*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
9684a0cb85cSEd Tanous         if (ec)
9694a0cb85cSEd Tanous         {
970a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
9711abe55efSEd Tanous         }
972bc0bd6e0SEd Tanous         },
973bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
974bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
975bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
976168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
977588c3f0dSKowalski, Kamil }
978588c3f0dSKowalski, Kamil 
9794f48d5f6SEd Tanous inline void
98035fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
98135fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
98235fb5311STejas Patil {
98335fb5311STejas Patil     sdbusplus::message::object_path objPath =
98435fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
98535fb5311STejas Patil     crow::connections::systemBus->async_method_call(
986*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
98735fb5311STejas Patil         if (ec)
98835fb5311STejas Patil         {
98935fb5311STejas Patil             messages::internalError(asyncResp->res);
99035fb5311STejas Patil         }
99135fb5311STejas Patil         },
99235fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
99335fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
99435fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
99535fb5311STejas Patil         std::variant<size_t>(mtuSize));
99635fb5311STejas Patil }
99735fb5311STejas Patil 
99835fb5311STejas Patil inline void
9994f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1000bf648f77SEd Tanous                           const std::string& domainname,
10018d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1002ab6554f1SJoshi-Mansi {
1003ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1004ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1005*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1006ab6554f1SJoshi-Mansi         if (ec)
1007ab6554f1SJoshi-Mansi         {
1008ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
1009ab6554f1SJoshi-Mansi         }
1010ab6554f1SJoshi-Mansi         },
1011ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1012ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1013ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1014ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1015168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1016ab6554f1SJoshi-Mansi }
1017ab6554f1SJoshi-Mansi 
10184f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1019bf648f77SEd Tanous {
1020bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1021bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1022bf648f77SEd Tanous     {
1023bf648f77SEd Tanous         return false;
1024bf648f77SEd Tanous     }
1025bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1026bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1027bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1028bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1029bf648f77SEd Tanous     const std::regex pattern(
1030bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1031bf648f77SEd Tanous 
1032bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1033bf648f77SEd Tanous }
1034bf648f77SEd Tanous 
10354f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1036bf648f77SEd Tanous {
1037bf648f77SEd Tanous     // Can have multiple subdomains
1038bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
10390fda0f12SGeorge Liu     const std::regex pattern(
10400fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1041bf648f77SEd Tanous 
1042bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1043bf648f77SEd Tanous }
1044bf648f77SEd Tanous 
10454f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10468d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1047ab6554f1SJoshi-Mansi {
1048ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1049ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1050ab6554f1SJoshi-Mansi     {
1051ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1052ab6554f1SJoshi-Mansi         return;
1053ab6554f1SJoshi-Mansi     }
1054ab6554f1SJoshi-Mansi 
1055ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1056ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1057ab6554f1SJoshi-Mansi     {
1058ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1059ab6554f1SJoshi-Mansi         return;
1060ab6554f1SJoshi-Mansi     }
1061ab6554f1SJoshi-Mansi 
1062ab6554f1SJoshi-Mansi     std::string hostname;
1063ab6554f1SJoshi-Mansi     std::string domainname;
1064ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1065ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1066ab6554f1SJoshi-Mansi 
1067ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1068ab6554f1SJoshi-Mansi     {
1069ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1070ab6554f1SJoshi-Mansi         return;
1071ab6554f1SJoshi-Mansi     }
1072ab6554f1SJoshi-Mansi 
1073ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1074ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1075ab6554f1SJoshi-Mansi }
1076ab6554f1SJoshi-Mansi 
10774f48d5f6SEd Tanous inline void
10784f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1079bf648f77SEd Tanous                           const std::string& macAddress,
10808d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1081d577665bSRatan Gupta {
108258283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
108358283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
108458283f41SJohnathan Mantey 
1085d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1086*5e7e2dc5SEd Tanous         [asyncResp, macAddress](const boost::system::error_code& ec,
10875b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1088d577665bSRatan Gupta         if (ec)
1089d577665bSRatan Gupta         {
109058283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
109158283f41SJohnathan Mantey             if (err == nullptr)
109258283f41SJohnathan Mantey             {
109358283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
109458283f41SJohnathan Mantey                 return;
109558283f41SJohnathan Mantey             }
109658283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
109758283f41SJohnathan Mantey             {
109858283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
109958283f41SJohnathan Mantey                 return;
110058283f41SJohnathan Mantey             }
1101d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1102d577665bSRatan Gupta             return;
1103d577665bSRatan Gupta         }
1104d577665bSRatan Gupta         },
1105d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1106d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1107d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1108d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1109168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1110d577665bSRatan Gupta }
1111286b9118SJohnathan Mantey 
11124f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11134f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11144f48d5f6SEd Tanous                            const bool v6Value,
11158d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1116da131a9aSJennifer Lee {
11172c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1118da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1119*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1120da131a9aSJennifer Lee         if (ec)
1121da131a9aSJennifer Lee         {
1122da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1123da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1124da131a9aSJennifer Lee             return;
1125da131a9aSJennifer Lee         }
11268f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1127da131a9aSJennifer Lee         },
1128da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1129da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1130da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1131da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1132168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1133da131a9aSJennifer Lee }
11341f8c7b5dSJohnathan Mantey 
11354f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1136eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11378d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1138eeedda23SJohnathan Mantey {
1139eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1140*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1141eeedda23SJohnathan Mantey         if (ec)
1142eeedda23SJohnathan Mantey         {
1143eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1144eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1145eeedda23SJohnathan Mantey             return;
1146eeedda23SJohnathan Mantey         }
1147eeedda23SJohnathan Mantey         },
1148eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1149eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1150eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1151eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1152168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1153eeedda23SJohnathan Mantey }
1154eeedda23SJohnathan Mantey 
11554f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11568d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1157da131a9aSJennifer Lee {
1158da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1159da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1160*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1161da131a9aSJennifer Lee         if (ec)
1162da131a9aSJennifer Lee         {
1163da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1164da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1165da131a9aSJennifer Lee             return;
1166da131a9aSJennifer Lee         }
1167da131a9aSJennifer Lee         },
11681e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1169da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1170da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1171168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1172da131a9aSJennifer Lee }
1173d577665bSRatan Gupta 
11744f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
11751f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1176f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1177f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
11788d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1179da131a9aSJennifer Lee {
118082695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
118182695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1182da131a9aSJennifer Lee 
11831f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11841f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11851f8c7b5dSJohnathan Mantey 
11861f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11871f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1188da131a9aSJennifer Lee     {
11891f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11901f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11911f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11921f8c7b5dSJohnathan Mantey         {
1193bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1194bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11951f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1196da131a9aSJennifer Lee             return;
1197da131a9aSJennifer Lee         }
11981f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11991f8c7b5dSJohnathan Mantey     }
12001f8c7b5dSJohnathan Mantey     else
1201da131a9aSJennifer Lee     {
12021f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12031f8c7b5dSJohnathan Mantey     }
12041f8c7b5dSJohnathan Mantey 
12051f8c7b5dSJohnathan Mantey     bool nextDNS{};
120682695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
12071f8c7b5dSJohnathan Mantey     {
120882695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
12091f8c7b5dSJohnathan Mantey         {
12101f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12111f8c7b5dSJohnathan Mantey             return;
12121f8c7b5dSJohnathan Mantey         }
121382695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12141f8c7b5dSJohnathan Mantey     }
121582695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
12161f8c7b5dSJohnathan Mantey     {
121782695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12181f8c7b5dSJohnathan Mantey     }
121982695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
12201f8c7b5dSJohnathan Mantey     {
122182695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
12221f8c7b5dSJohnathan Mantey     }
12231f8c7b5dSJohnathan Mantey     else
12241f8c7b5dSJohnathan Mantey     {
122582695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
12261f8c7b5dSJohnathan Mantey     }
12271f8c7b5dSJohnathan Mantey 
12281f8c7b5dSJohnathan Mantey     bool nextNTP{};
122982695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
12301f8c7b5dSJohnathan Mantey     {
123182695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
12321f8c7b5dSJohnathan Mantey         {
12331f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12341f8c7b5dSJohnathan Mantey             return;
12351f8c7b5dSJohnathan Mantey         }
123682695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12371f8c7b5dSJohnathan Mantey     }
123882695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
12391f8c7b5dSJohnathan Mantey     {
124082695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12411f8c7b5dSJohnathan Mantey     }
124282695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12431f8c7b5dSJohnathan Mantey     {
124482695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12451f8c7b5dSJohnathan Mantey     }
12461f8c7b5dSJohnathan Mantey     else
12471f8c7b5dSJohnathan Mantey     {
124882695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12491f8c7b5dSJohnathan Mantey     }
12501f8c7b5dSJohnathan Mantey 
12511f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
125282695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12531f8c7b5dSJohnathan Mantey     {
125482695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
12551f8c7b5dSJohnathan Mantey         {
12561f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12571f8c7b5dSJohnathan Mantey             return;
12581f8c7b5dSJohnathan Mantey         }
125982695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12601f8c7b5dSJohnathan Mantey     }
126182695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12621f8c7b5dSJohnathan Mantey     {
126382695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12641f8c7b5dSJohnathan Mantey     }
126582695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12661f8c7b5dSJohnathan Mantey     {
126782695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12681f8c7b5dSJohnathan Mantey     }
12691f8c7b5dSJohnathan Mantey     else
12701f8c7b5dSJohnathan Mantey     {
127182695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
12721f8c7b5dSJohnathan Mantey     }
12731f8c7b5dSJohnathan Mantey 
1274da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
12751f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
12761f8c7b5dSJohnathan Mantey                    asyncResp);
1277da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
12781f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1279da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
12801f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
12811f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12821f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1283da131a9aSJennifer Lee }
128401784826SJohnathan Mantey 
12854f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
12862c70f800SEd Tanous     getNextStaticIpEntry(
1287bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1288bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
128901784826SJohnathan Mantey {
129017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
129117a897dfSManojkiran Eda         return value.origin == "Static";
129217a897dfSManojkiran Eda     });
129301784826SJohnathan Mantey }
129401784826SJohnathan Mantey 
12954f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
12962c70f800SEd Tanous     getNextStaticIpEntry(
1297bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1298bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
129901784826SJohnathan Mantey {
130017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
130117a897dfSManojkiran Eda         return value.origin == "Static";
130217a897dfSManojkiran Eda     });
130301784826SJohnathan Mantey }
130401784826SJohnathan Mantey 
13054f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1306f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
130701784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13088d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13091abe55efSEd Tanous {
131001784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1311f476acbfSRatan Gupta     {
131271f52d96SEd Tanous         messages::propertyValueTypeError(
131371f52d96SEd Tanous             asyncResp->res,
1314bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1315d1d50814SRavi Teja             "IPv4StaticAddresses");
1316f476acbfSRatan Gupta         return;
1317f476acbfSRatan Gupta     }
1318f476acbfSRatan Gupta 
1319271584abSEd Tanous     unsigned entryIdx = 1;
132001784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
132101784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
132201784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
132301784826SJohnathan Mantey     // into the NIC.
132485ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13252c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
132601784826SJohnathan Mantey 
1327537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13281abe55efSEd Tanous     {
13294a0cb85cSEd Tanous         std::string pathString =
1330d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1331179db1d7SKowalski, Kamil 
133201784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1333f476acbfSRatan Gupta         {
1334537174c4SEd Tanous             std::optional<std::string> address;
1335537174c4SEd Tanous             std::optional<std::string> subnetMask;
1336537174c4SEd Tanous             std::optional<std::string> gateway;
1337537174c4SEd Tanous 
1338537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13397e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13407e27d832SJohnathan Mantey                                      "Gateway", gateway))
1341537174c4SEd Tanous             {
134201784826SJohnathan Mantey                 messages::propertyValueFormatError(
134371f52d96SEd Tanous                     asyncResp->res,
134471f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
134571f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
134671f52d96SEd Tanous                     pathString);
1347537174c4SEd Tanous                 return;
1348179db1d7SKowalski, Kamil             }
1349179db1d7SKowalski, Kamil 
135001784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
135101784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
135201784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
135301784826SJohnathan Mantey             // current request.
1354271584abSEd Tanous             const std::string* addr = nullptr;
1355271584abSEd Tanous             const std::string* gw = nullptr;
135601784826SJohnathan Mantey             uint8_t prefixLength = 0;
135701784826SJohnathan Mantey             bool errorInEntry = false;
1358537174c4SEd Tanous             if (address)
13591abe55efSEd Tanous             {
1360033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
13611abe55efSEd Tanous                 {
136201784826SJohnathan Mantey                     addr = &(*address);
13634a0cb85cSEd Tanous                 }
136401784826SJohnathan Mantey                 else
136501784826SJohnathan Mantey                 {
1366bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1367bf648f77SEd Tanous                                                        pathString + "/Address");
136801784826SJohnathan Mantey                     errorInEntry = true;
136901784826SJohnathan Mantey                 }
137001784826SJohnathan Mantey             }
137185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
137201784826SJohnathan Mantey             {
137385ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
137401784826SJohnathan Mantey             }
137501784826SJohnathan Mantey             else
137601784826SJohnathan Mantey             {
137701784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
137801784826SJohnathan Mantey                                           pathString + "/Address");
137901784826SJohnathan Mantey                 errorInEntry = true;
13804a0cb85cSEd Tanous             }
13814a0cb85cSEd Tanous 
1382537174c4SEd Tanous             if (subnetMask)
13834a0cb85cSEd Tanous             {
1384033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1385033f1e4dSEd Tanous                                                          &prefixLength))
13864a0cb85cSEd Tanous                 {
1387f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1388537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13894a0cb85cSEd Tanous                         pathString + "/SubnetMask");
139001784826SJohnathan Mantey                     errorInEntry = true;
13914a0cb85cSEd Tanous                 }
13924a0cb85cSEd Tanous             }
139385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13944a0cb85cSEd Tanous             {
1395033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
139601784826SJohnathan Mantey                                                          &prefixLength))
13974a0cb85cSEd Tanous                 {
139801784826SJohnathan Mantey                     messages::propertyValueFormatError(
139985ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
140001784826SJohnathan Mantey                         pathString + "/SubnetMask");
140101784826SJohnathan Mantey                     errorInEntry = true;
14024a0cb85cSEd Tanous                 }
14034a0cb85cSEd Tanous             }
14041abe55efSEd Tanous             else
14051abe55efSEd Tanous             {
140601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
140701784826SJohnathan Mantey                                           pathString + "/SubnetMask");
140801784826SJohnathan Mantey                 errorInEntry = true;
140901784826SJohnathan Mantey             }
141001784826SJohnathan Mantey 
141101784826SJohnathan Mantey             if (gateway)
141201784826SJohnathan Mantey             {
1413033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
141401784826SJohnathan Mantey                 {
141501784826SJohnathan Mantey                     gw = &(*gateway);
141601784826SJohnathan Mantey                 }
141701784826SJohnathan Mantey                 else
141801784826SJohnathan Mantey                 {
1419bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1420bf648f77SEd Tanous                                                        pathString + "/Gateway");
142101784826SJohnathan Mantey                     errorInEntry = true;
142201784826SJohnathan Mantey                 }
142301784826SJohnathan Mantey             }
142485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
142501784826SJohnathan Mantey             {
142685ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
142701784826SJohnathan Mantey             }
142801784826SJohnathan Mantey             else
14291abe55efSEd Tanous             {
1430a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14314a0cb85cSEd Tanous                                           pathString + "/Gateway");
143201784826SJohnathan Mantey                 errorInEntry = true;
14334a0cb85cSEd Tanous             }
14344a0cb85cSEd Tanous 
143501784826SJohnathan Mantey             if (errorInEntry)
14361abe55efSEd Tanous             {
143701784826SJohnathan Mantey                 return;
14384a0cb85cSEd Tanous             }
14394a0cb85cSEd Tanous 
144085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14411abe55efSEd Tanous             {
144285ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1443bf648f77SEd Tanous                                     *addr, asyncResp);
144485ffe86aSJiaqing Zhao                 nicIpEntry =
144585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1446588c3f0dSKowalski, Kamil             }
144701784826SJohnathan Mantey             else
144801784826SJohnathan Mantey             {
1449cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1450cb13a392SEd Tanous                            asyncResp);
14514a0cb85cSEd Tanous             }
14524a0cb85cSEd Tanous             entryIdx++;
14534a0cb85cSEd Tanous         }
145401784826SJohnathan Mantey         else
145501784826SJohnathan Mantey         {
145685ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
145701784826SJohnathan Mantey             {
145801784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
145901784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
146001784826SJohnathan Mantey                 // in error, so bail out.
146101784826SJohnathan Mantey                 if (thisJson.is_null())
146201784826SJohnathan Mantey                 {
146301784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
146401784826SJohnathan Mantey                     return;
146501784826SJohnathan Mantey                 }
146601784826SJohnathan Mantey                 messages::propertyValueFormatError(
146771f52d96SEd Tanous                     asyncResp->res,
146871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
146971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
147071f52d96SEd Tanous                     pathString);
147101784826SJohnathan Mantey                 return;
147201784826SJohnathan Mantey             }
147301784826SJohnathan Mantey 
147401784826SJohnathan Mantey             if (thisJson.is_null())
147501784826SJohnathan Mantey             {
147685ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
147701784826SJohnathan Mantey             }
147885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
147901784826SJohnathan Mantey             {
148085ffe86aSJiaqing Zhao                 nicIpEntry =
148185ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
148201784826SJohnathan Mantey             }
148301784826SJohnathan Mantey             entryIdx++;
148401784826SJohnathan Mantey         }
148501784826SJohnathan Mantey     }
14864a0cb85cSEd Tanous }
14874a0cb85cSEd Tanous 
14884f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1489f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1490f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14918d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1492f85837bfSRAJESWARAN THILLAIGOVINDAN {
1493f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1494*5e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1495f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1496f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1497f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1498f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1499f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1500f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1501f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1502f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1503f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1504bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1505168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1506f85837bfSRAJESWARAN THILLAIGOVINDAN }
1507f85837bfSRAJESWARAN THILLAIGOVINDAN 
15084f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1509f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
151001784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15118d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1512e48c0fc5SRavi Teja {
151301784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1514e48c0fc5SRavi Teja     {
151571f52d96SEd Tanous         messages::propertyValueTypeError(
151671f52d96SEd Tanous             asyncResp->res,
1517bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1518e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1519e48c0fc5SRavi Teja         return;
1520e48c0fc5SRavi Teja     }
1521271584abSEd Tanous     size_t entryIdx = 1;
152285ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15232c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1524f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1525e48c0fc5SRavi Teja     {
1526e48c0fc5SRavi Teja         std::string pathString =
1527e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1528e48c0fc5SRavi Teja 
152901784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1530e48c0fc5SRavi Teja         {
1531e48c0fc5SRavi Teja             std::optional<std::string> address;
1532e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1533f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1534bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1535bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1536e48c0fc5SRavi Teja             {
153701784826SJohnathan Mantey                 messages::propertyValueFormatError(
153871f52d96SEd Tanous                     asyncResp->res,
153971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
154071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
154171f52d96SEd Tanous                     pathString);
1542e48c0fc5SRavi Teja                 return;
1543e48c0fc5SRavi Teja             }
1544e48c0fc5SRavi Teja 
1545543f4400SEd Tanous             const std::string* addr = nullptr;
1546543f4400SEd Tanous             uint8_t prefix = 0;
154701784826SJohnathan Mantey 
154801784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
154901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
155001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
155101784826SJohnathan Mantey             // current request.
1552e48c0fc5SRavi Teja             if (address)
1553e48c0fc5SRavi Teja             {
155401784826SJohnathan Mantey                 addr = &(*address);
1555e48c0fc5SRavi Teja             }
155685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
155701784826SJohnathan Mantey             {
155885ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
155901784826SJohnathan Mantey             }
156001784826SJohnathan Mantey             else
156101784826SJohnathan Mantey             {
156201784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
156301784826SJohnathan Mantey                                           pathString + "/Address");
156401784826SJohnathan Mantey                 return;
1565e48c0fc5SRavi Teja             }
1566e48c0fc5SRavi Teja 
1567e48c0fc5SRavi Teja             if (prefixLength)
1568e48c0fc5SRavi Teja             {
156901784826SJohnathan Mantey                 prefix = *prefixLength;
157001784826SJohnathan Mantey             }
157185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1572e48c0fc5SRavi Teja             {
157385ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1574e48c0fc5SRavi Teja             }
1575e48c0fc5SRavi Teja             else
1576e48c0fc5SRavi Teja             {
1577e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1578e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
157901784826SJohnathan Mantey                 return;
1580e48c0fc5SRavi Teja             }
1581e48c0fc5SRavi Teja 
158285ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1583e48c0fc5SRavi Teja             {
158485ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1585e48c0fc5SRavi Teja                                     asyncResp);
158685ffe86aSJiaqing Zhao                 nicIpEntry =
158785ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
158801784826SJohnathan Mantey             }
158901784826SJohnathan Mantey             else
159001784826SJohnathan Mantey             {
159101784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1592e48c0fc5SRavi Teja             }
1593e48c0fc5SRavi Teja             entryIdx++;
1594e48c0fc5SRavi Teja         }
159501784826SJohnathan Mantey         else
159601784826SJohnathan Mantey         {
159785ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
159801784826SJohnathan Mantey             {
159901784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
160001784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
160101784826SJohnathan Mantey                 // in error, so bail out.
160201784826SJohnathan Mantey                 if (thisJson.is_null())
160301784826SJohnathan Mantey                 {
160401784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
160501784826SJohnathan Mantey                     return;
160601784826SJohnathan Mantey                 }
160701784826SJohnathan Mantey                 messages::propertyValueFormatError(
160871f52d96SEd Tanous                     asyncResp->res,
160971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
161071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
161171f52d96SEd Tanous                     pathString);
161201784826SJohnathan Mantey                 return;
161301784826SJohnathan Mantey             }
161401784826SJohnathan Mantey 
161501784826SJohnathan Mantey             if (thisJson.is_null())
161601784826SJohnathan Mantey             {
161785ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
161801784826SJohnathan Mantey             }
161985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
162001784826SJohnathan Mantey             {
162185ffe86aSJiaqing Zhao                 nicIpEntry =
162285ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
162301784826SJohnathan Mantey             }
162401784826SJohnathan Mantey             entryIdx++;
162501784826SJohnathan Mantey         }
162601784826SJohnathan Mantey     }
1627e48c0fc5SRavi Teja }
1628e48c0fc5SRavi Teja 
16294f48d5f6SEd Tanous inline void parseInterfaceData(
16308d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16318d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1632e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
163301784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16344a0cb85cSEd Tanous {
16357a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1636eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1637eeedda23SJohnathan Mantey 
16382c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
163981ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1640eddfc437SWilly Tu     jsonResponse["@odata.id"] = crow::utility::urlFromPieces(
1641eddfc437SWilly Tu         "redfish", "v1", "Managers", "bmc", "EthernetInterfaces", ifaceId);
16422c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1643eeedda23SJohnathan Mantey 
1644eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1645eeedda23SJohnathan Mantey 
16467a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
16477a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
16487a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1649b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1650eeedda23SJohnathan Mantey         if (ec)
1651029573d4SEd Tanous         {
1652eeedda23SJohnathan Mantey             return;
1653eeedda23SJohnathan Mantey         }
1654eeedda23SJohnathan Mantey 
1655914e2d5dSEd Tanous         health->inventory = resp;
16567a1dbc48SGeorge Liu         });
1657eeedda23SJohnathan Mantey 
1658eeedda23SJohnathan Mantey     health->populate();
1659eeedda23SJohnathan Mantey 
1660eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1661eeedda23SJohnathan Mantey     {
16620ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
16632c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1664029573d4SEd Tanous     }
1665029573d4SEd Tanous     else
1666029573d4SEd Tanous     {
16672c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
16682c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1669029573d4SEd Tanous     }
1670aa05fb27SJohnathan Mantey 
16712c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
167235fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
167382695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
16742c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
167582695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
167682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
167782695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
167882695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16791f8c7b5dSJohnathan Mantey 
16802c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
168182695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16821f8c7b5dSJohnathan Mantey                                                                : "Disabled";
168382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
168482695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
168582695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16862a133282Smanojkiraneda 
168782695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16884a0cb85cSEd Tanous     {
168982695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1690ab6554f1SJoshi-Mansi 
1691ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1692ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1693ab6554f1SJoshi-Mansi         // FQDN
169482695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1695d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1696d24bfc7aSJennifer Lee         {
16972c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1698d24bfc7aSJennifer Lee         }
16992c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17004a0cb85cSEd Tanous     }
17014a0cb85cSEd Tanous 
1702613dabeaSEd Tanous     jsonResponse["VLANs"]["@odata.id"] =
1703613dabeaSEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1704613dabeaSEd Tanous                                      "EthernetInterfaces", ifaceId, "VLANs");
1705fda13ad2SSunitha Harish 
17062c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17072c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17084a0cb85cSEd Tanous 
17092c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17102c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17112c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17122c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17139eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17144a0cb85cSEd Tanous     {
17152c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1716fa5053a6SGunnar Mills         if (gatewayStr.empty())
1717fa5053a6SGunnar Mills         {
1718fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1719fa5053a6SGunnar Mills         }
17201476687dSEd Tanous         nlohmann::json::object_t ipv4;
17211476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
17221476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
17231476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
17241476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1725fa5053a6SGunnar Mills 
17262c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1727d1d50814SRavi Teja         {
17281476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1729d1d50814SRavi Teja         }
17301476687dSEd Tanous 
17311476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
173201784826SJohnathan Mantey     }
1733d1d50814SRavi Teja 
173482695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
17357ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17367ea79e5eSRavi Teja     {
17377ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17387ea79e5eSRavi Teja     }
17397ea79e5eSRavi Teja 
17407ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1741e48c0fc5SRavi Teja 
17422c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17432c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17442c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17452c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17467f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17472c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17487f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17499eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1750e48c0fc5SRavi Teja     {
17511476687dSEd Tanous         nlohmann::json::object_t ipv6;
17521476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
17531476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
17541476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
17551476687dSEd Tanous         ipv6["AddressState"] = nullptr;
17561476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
17572c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1758e48c0fc5SRavi Teja         {
17591476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
17601476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
17611476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
17621476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
176301784826SJohnathan Mantey         }
1764e48c0fc5SRavi Teja     }
1765588c3f0dSKowalski, Kamil }
1766588c3f0dSKowalski, Kamil 
17674f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1768bf648f77SEd Tanous {
176911ba3979SEd Tanous     return iface.starts_with(parent + "_");
1770bf648f77SEd Tanous }
1771bf648f77SEd Tanous 
1772bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1773bf648f77SEd Tanous {
1774bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1775ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
17761476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17771476687dSEd Tanous             [&app](const crow::Request& req,
17781476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17793ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
178045ca1b86SEd Tanous         {
178145ca1b86SEd Tanous             return;
178245ca1b86SEd Tanous         }
178345ca1b86SEd Tanous 
1784bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1785bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1786bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1787bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1788bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1789bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1790bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1791bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1792bf648f77SEd Tanous 
1793bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1794bf648f77SEd Tanous         // preparation
1795002d39b4SEd Tanous         getEthernetIfaceList(
1796002d39b4SEd Tanous             [asyncResp](
17971476687dSEd Tanous                 const bool& success,
1798002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1799bf648f77SEd Tanous             if (!success)
18001abe55efSEd Tanous             {
1801f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18029391bb9cSRapkiewicz, Pawel                 return;
18039391bb9cSRapkiewicz, Pawel             }
18049391bb9cSRapkiewicz, Pawel 
1805002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1806bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1807bf648f77SEd Tanous             std::string tag = "_";
1808bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1809bf648f77SEd Tanous             {
1810bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1811bf648f77SEd Tanous                 if (found == std::string::npos)
1812bf648f77SEd Tanous                 {
18131476687dSEd Tanous                     nlohmann::json::object_t iface;
1814eddfc437SWilly Tu                     iface["@odata.id"] = crow::utility::urlFromPieces(
1815eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
1816eddfc437SWilly Tu                         "EthernetInterfaces", ifaceItem);
18171476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
1818bf648f77SEd Tanous                 }
1819bf648f77SEd Tanous             }
1820bf648f77SEd Tanous 
1821002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1822bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1823bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1824bf648f77SEd Tanous         });
1825bf648f77SEd Tanous         });
1826bf648f77SEd Tanous 
1827bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1828ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1829bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
183045ca1b86SEd Tanous             [&app](const crow::Request& req,
1831bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1832bf648f77SEd Tanous                    const std::string& ifaceId) {
18333ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
183445ca1b86SEd Tanous         {
183545ca1b86SEd Tanous             return;
183645ca1b86SEd Tanous         }
18374a0cb85cSEd Tanous         getEthernetIfaceData(
1838bf648f77SEd Tanous             ifaceId,
1839002d39b4SEd Tanous             [asyncResp, ifaceId](
1840002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1841002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1842002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18434a0cb85cSEd Tanous             if (!success)
18441abe55efSEd Tanous             {
1845bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1846bf648f77SEd Tanous                 // existing object, and other errors
1847002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1848002d39b4SEd Tanous                                            ifaceId);
18494a0cb85cSEd Tanous                 return;
18509391bb9cSRapkiewicz, Pawel             }
18514c9afe43SEd Tanous 
1852188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1853188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
18540f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1855188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1856002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
18570f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
18580f74e643SEd Tanous                 "Management Network Interface";
18590f74e643SEd Tanous 
1860002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
18619391bb9cSRapkiewicz, Pawel             });
1862bf648f77SEd Tanous         });
18639391bb9cSRapkiewicz, Pawel 
1864bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1865ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1866bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
186745ca1b86SEd Tanous             [&app](const crow::Request& req,
1868bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1869bf648f77SEd Tanous                    const std::string& ifaceId) {
18703ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
187145ca1b86SEd Tanous         {
187245ca1b86SEd Tanous             return;
187345ca1b86SEd Tanous         }
1874bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1875ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1876d577665bSRatan Gupta         std::optional<std::string> macAddress;
18779a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1878d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1879e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1880f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1881da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
18821f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1883eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
188435fb5311STejas Patil         std::optional<size_t> mtuSize;
18851f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18861f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18870627a2c7SEd Tanous 
188815ed6780SWilly Tu         if (!json_util::readJsonPatch(
18898d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1890002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1891002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1892002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1893ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1894002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18951abe55efSEd Tanous         {
1896588c3f0dSKowalski, Kamil             return;
1897588c3f0dSKowalski, Kamil         }
1898da131a9aSJennifer Lee         if (dhcpv4)
1899da131a9aSJennifer Lee         {
1900002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
19011f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
190282695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
190382695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
190482695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
19051f8c7b5dSJohnathan Mantey             {
19061f8c7b5dSJohnathan Mantey                 return;
19071f8c7b5dSJohnathan Mantey             }
19081f8c7b5dSJohnathan Mantey         }
19091f8c7b5dSJohnathan Mantey 
19101f8c7b5dSJohnathan Mantey         if (dhcpv6)
19111f8c7b5dSJohnathan Mantey         {
1912002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1913002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1914002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1915002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1916002d39b4SEd Tanous                                      "UseDomainName",
191782695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
19181f8c7b5dSJohnathan Mantey             {
19191f8c7b5dSJohnathan Mantey                 return;
19201f8c7b5dSJohnathan Mantey             }
1921da131a9aSJennifer Lee         }
1922da131a9aSJennifer Lee 
1923bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1924bf648f77SEd Tanous         // for JSON preparation
19254a0cb85cSEd Tanous         getEthernetIfaceData(
19262c70f800SEd Tanous             ifaceId,
1927bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1928ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1929d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19309a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1931e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19321f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1933bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1934bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1935f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1936002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1937002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1938002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19391abe55efSEd Tanous             if (!success)
19401abe55efSEd Tanous             {
1941588c3f0dSKowalski, Kamil                 // ... otherwise return error
1942bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1943bf648f77SEd Tanous                 // existing object, and other errors
1944002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1945002d39b4SEd Tanous                                            ifaceId);
1946588c3f0dSKowalski, Kamil                 return;
1947588c3f0dSKowalski, Kamil             }
1948588c3f0dSKowalski, Kamil 
19491f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
19501f8c7b5dSJohnathan Mantey             {
1951002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1952002d39b4SEd Tanous                                 asyncResp);
19531f8c7b5dSJohnathan Mantey             }
19541f8c7b5dSJohnathan Mantey 
19550627a2c7SEd Tanous             if (hostname)
19561abe55efSEd Tanous             {
19570627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
19581abe55efSEd Tanous             }
19590627a2c7SEd Tanous 
1960ab6554f1SJoshi-Mansi             if (fqdn)
1961ab6554f1SJoshi-Mansi             {
19622c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1963ab6554f1SJoshi-Mansi             }
1964ab6554f1SJoshi-Mansi 
1965d577665bSRatan Gupta             if (macAddress)
1966d577665bSRatan Gupta             {
1967002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1968d577665bSRatan Gupta             }
1969d577665bSRatan Gupta 
1970d1d50814SRavi Teja             if (ipv4StaticAddresses)
1971d1d50814SRavi Teja             {
1972bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1973bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1974bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1975bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1976bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1977bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1978bf648f77SEd Tanous                 // that, but could be done more efficiently
1979f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1980002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
19811abe55efSEd Tanous             }
19820627a2c7SEd Tanous 
1983f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1984f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1985002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1986002d39b4SEd Tanous                                              asyncResp);
1987f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19889a6fc6feSRavi Teja 
19899a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19909a6fc6feSRavi Teja             {
19919a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19929a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19939a6fc6feSRavi Teja             }
1994e48c0fc5SRavi Teja 
1995e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1996e48c0fc5SRavi Teja             {
1997002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1998002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1999002d39b4SEd Tanous                                                asyncResp);
2000e48c0fc5SRavi Teja             }
2001eeedda23SJohnathan Mantey 
2002eeedda23SJohnathan Mantey             if (interfaceEnabled)
2003eeedda23SJohnathan Mantey             {
2004002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2005002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
2006eeedda23SJohnathan Mantey             }
200735fb5311STejas Patil 
200835fb5311STejas Patil             if (mtuSize)
200935fb5311STejas Patil             {
201035fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
201135fb5311STejas Patil             }
2012588c3f0dSKowalski, Kamil             });
2013bf648f77SEd Tanous         });
20149391bb9cSRapkiewicz, Pawel 
2015bf648f77SEd Tanous     BMCWEB_ROUTE(
2016bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2017ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2018bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
201945ca1b86SEd Tanous             [&app](const crow::Request& req,
2020bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
202145ca1b86SEd Tanous                    const std::string& parentIfaceId,
202245ca1b86SEd Tanous                    const std::string& ifaceId) {
20233ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
202445ca1b86SEd Tanous         {
202545ca1b86SEd Tanous             return;
202645ca1b86SEd Tanous         }
20278d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
20280f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20298d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2030e439f0f8SKowalski, Kamil 
20312c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
20321abe55efSEd Tanous         {
2033a434f2bdSEd Tanous             return;
2034a434f2bdSEd Tanous         }
2035a434f2bdSEd Tanous 
2036bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2037bf648f77SEd Tanous         // for JSON preparation
20384a0cb85cSEd Tanous         getEthernetIfaceData(
2039bf648f77SEd Tanous             ifaceId,
2040002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2041002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2042cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2043cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
204417e22024SJiaqing Zhao             if (success && ethData.vlanId)
20451abe55efSEd Tanous             {
204622872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
204722872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["@odata.id"] =
2048eddfc437SWilly Tu                     crow::utility::urlFromPieces(
2049eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
2050eddfc437SWilly Tu                         "EthernetInterfaces", parentIfaceId, "VLANs", ifaceId);
205122872ff3SJiaqing Zhao 
205223a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
205322872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
20541abe55efSEd Tanous             }
20551abe55efSEd Tanous             else
20561abe55efSEd Tanous             {
2057e439f0f8SKowalski, Kamil                 // ... otherwise return error
2058bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2059bf648f77SEd Tanous                 // existing object, and other errors
2060bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2061d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2062e439f0f8SKowalski, Kamil             }
2063e439f0f8SKowalski, Kamil             });
2064bf648f77SEd Tanous         });
2065e439f0f8SKowalski, Kamil 
2066bf648f77SEd Tanous     BMCWEB_ROUTE(
2067bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20683d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2069bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
207045ca1b86SEd Tanous             [&app](const crow::Request& req,
2071bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207245ca1b86SEd Tanous                    const std::string& parentIfaceId,
207345ca1b86SEd Tanous                    const std::string& ifaceId) {
20743ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207545ca1b86SEd Tanous         {
207645ca1b86SEd Tanous             return;
207745ca1b86SEd Tanous         }
2078fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
20791abe55efSEd Tanous         {
2080d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2081002d39b4SEd Tanous                                        ifaceId);
2082927a505aSKowalski, Kamil             return;
2083927a505aSKowalski, Kamil         }
2084927a505aSKowalski, Kamil 
20853927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20863927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20870627a2c7SEd Tanous 
208815ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2089bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20901abe55efSEd Tanous         {
2091927a505aSKowalski, Kamil             return;
2092927a505aSKowalski, Kamil         }
2093927a505aSKowalski, Kamil 
20943927e13eSJiaqing Zhao         if (vlanId)
20953927e13eSJiaqing Zhao         {
20963927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20973927e13eSJiaqing Zhao             return;
20983927e13eSJiaqing Zhao         }
20993927e13eSJiaqing Zhao 
2100bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2101bf648f77SEd Tanous         // for JSON preparation
2102e48c0fc5SRavi Teja         getEthernetIfaceData(
2103bf648f77SEd Tanous             ifaceId,
21043927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2105002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2106cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2107cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
210817e22024SJiaqing Zhao             if (success && ethData.vlanId)
210908244d02SSunitha Harish             {
211023a06317SJiaqing Zhao                 if (vlanEnable)
211123a06317SJiaqing Zhao                 {
211223a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
2113*5e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
211408244d02SSunitha Harish                         if (ec)
211508244d02SSunitha Harish                         {
211608244d02SSunitha Harish                             messages::internalError(asyncResp->res);
21173927e13eSJiaqing Zhao                             return;
211808244d02SSunitha Harish                         }
211923a06317SJiaqing Zhao                         },
212023a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
212123a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
212223a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
212323a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
212423a06317SJiaqing Zhao                         "NICEnabled",
212523a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
212608244d02SSunitha Harish                 }
212708244d02SSunitha Harish             }
212808244d02SSunitha Harish             else
21291abe55efSEd Tanous             {
2130bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2131bf648f77SEd Tanous                 // existing object, and other errors
2132bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2133d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2134bf648f77SEd Tanous                 return;
2135bf648f77SEd Tanous             }
2136bf648f77SEd Tanous             });
2137bf648f77SEd Tanous         });
2138bf648f77SEd Tanous 
2139bf648f77SEd Tanous     BMCWEB_ROUTE(
2140bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21413d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2142bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
214345ca1b86SEd Tanous             [&app](const crow::Request& req,
2144bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
214545ca1b86SEd Tanous                    const std::string& parentIfaceId,
214645ca1b86SEd Tanous                    const std::string& ifaceId) {
21473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
214845ca1b86SEd Tanous         {
214945ca1b86SEd Tanous             return;
215045ca1b86SEd Tanous         }
2151bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2152bf648f77SEd Tanous         {
2153d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2154002d39b4SEd Tanous                                        ifaceId);
2155927a505aSKowalski, Kamil             return;
2156927a505aSKowalski, Kamil         }
2157e439f0f8SKowalski, Kamil 
2158bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2159bf648f77SEd Tanous         // for JSON preparation
2160f12894f8SJason M. Bills         getEthernetIfaceData(
2161bf648f77SEd Tanous             ifaceId,
2162002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2163002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2164cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2165cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
216617e22024SJiaqing Zhao             if (success && ethData.vlanId)
21671abe55efSEd Tanous             {
2168f12894f8SJason M. Bills                 auto callback =
2169*5e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
21701abe55efSEd Tanous                     if (ec)
21711abe55efSEd Tanous                     {
2172f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2173927a505aSKowalski, Kamil                     }
21744a0cb85cSEd Tanous                 };
21754a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2176002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2177002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
21784a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
21791abe55efSEd Tanous             }
21801abe55efSEd Tanous             else
21811abe55efSEd Tanous             {
2182927a505aSKowalski, Kamil                 // ... otherwise return error
2183bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2184bf648f77SEd Tanous                 // existing object, and other errors
2185bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2186d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2187927a505aSKowalski, Kamil             }
2188927a505aSKowalski, Kamil             });
2189bf648f77SEd Tanous         });
2190e439f0f8SKowalski, Kamil 
2191bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2192bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2193ed398213SEd Tanous 
2194ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21951476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21961476687dSEd Tanous             [&app](const crow::Request& req,
2197bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2198bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21993ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
220045ca1b86SEd Tanous         {
220145ca1b86SEd Tanous             return;
220245ca1b86SEd Tanous         }
22034a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
22041abe55efSEd Tanous         // preparation
2205002d39b4SEd Tanous         getEthernetIfaceList(
2206002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
22071abe55efSEd Tanous                 const bool& success,
2208002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
22094a0cb85cSEd Tanous             if (!success)
22101abe55efSEd Tanous             {
2211f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22124a0cb85cSEd Tanous                 return;
22131abe55efSEd Tanous             }
22144c9afe43SEd Tanous 
221581ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22164c9afe43SEd Tanous             {
2217002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2218002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
22194c9afe43SEd Tanous                                            rootInterfaceName);
22204c9afe43SEd Tanous                 return;
22214c9afe43SEd Tanous             }
22224c9afe43SEd Tanous 
22230f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
22240f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
22250f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
22260f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
22270f74e643SEd Tanous                 "VLAN Network Interface Collection";
22284a0cb85cSEd Tanous 
22292c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
22304a0cb85cSEd Tanous 
223181ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
22321abe55efSEd Tanous             {
223311ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
22344a0cb85cSEd Tanous                 {
22351476687dSEd Tanous                     nlohmann::json::object_t iface;
2236eddfc437SWilly Tu                     iface["@odata.id"] = crow::utility::urlFromPieces(
2237eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
2238eddfc437SWilly Tu                         "EthernetInterfaces", rootInterfaceName, "VLANs",
2239eddfc437SWilly Tu                         ifaceItem);
22401476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2241e439f0f8SKowalski, Kamil                 }
2242e439f0f8SKowalski, Kamil             }
2243e439f0f8SKowalski, Kamil 
2244002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
22452c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
22464a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2247eddfc437SWilly Tu                 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
2248eddfc437SWilly Tu                                              "EthernetInterfaces",
2249eddfc437SWilly Tu                                              rootInterfaceName, "VLANs");
2250e439f0f8SKowalski, Kamil         });
2251bf648f77SEd Tanous         });
2252e439f0f8SKowalski, Kamil 
2253bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2254bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
22553d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2256bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
225745ca1b86SEd Tanous             [&app](const crow::Request& req,
2258bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2259bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
22603ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
226145ca1b86SEd Tanous         {
226245ca1b86SEd Tanous             return;
226345ca1b86SEd Tanous         }
2264fda13ad2SSunitha Harish         bool vlanEnable = false;
22650627a2c7SEd Tanous         uint32_t vlanId = 0;
2266002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2267002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
22681abe55efSEd Tanous         {
22694a0cb85cSEd Tanous             return;
2270e439f0f8SKowalski, Kamil         }
2271fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2272dbb59d4dSEd Tanous         if (vlanId == 0U)
2273fda13ad2SSunitha Harish         {
2274fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2275fda13ad2SSunitha Harish         }
2276fda13ad2SSunitha Harish         if (!vlanEnable)
2277fda13ad2SSunitha Harish         {
2278fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2279fda13ad2SSunitha Harish         }
2280271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2281fda13ad2SSunitha Harish         {
2282fda13ad2SSunitha Harish             return;
2283fda13ad2SSunitha Harish         }
2284fda13ad2SSunitha Harish 
2285*5e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
22861abe55efSEd Tanous             if (ec)
22871abe55efSEd Tanous             {
2288bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2289bf648f77SEd Tanous                 // based on phosphor-network responses
2290f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22914a0cb85cSEd Tanous                 return;
22921abe55efSEd Tanous             }
2293f12894f8SJason M. Bills             messages::created(asyncResp->res);
2294e439f0f8SKowalski, Kamil         };
22954a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22964a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22974a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22984a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22990627a2c7SEd Tanous             rootInterfaceName, vlanId);
2300bf648f77SEd Tanous         });
23014a0cb85cSEd Tanous }
2302bf648f77SEd Tanous 
23039391bb9cSRapkiewicz, Pawel } // namespace redfish
2304