xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 11ba39793c016e8476e44be880fb267e059dd4eb)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
19*11ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
20*11ba3979SEd Tanous #include <boost/algorithm/string/split.hpp>
214a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
22179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
23168e20c1SEd Tanous #include <dbus_utility.hpp>
24588c3f0dSKowalski, Kamil #include <error_messages.hpp>
2545ca1b86SEd Tanous #include <query.hpp>
26ed398213SEd Tanous #include <registries/privilege_registry.hpp>
271214b7e7SGunnar Mills #include <utils/json_utils.hpp>
281214b7e7SGunnar Mills 
29a24526dcSEd Tanous #include <optional>
30ab6554f1SJoshi-Mansi #include <regex>
319391bb9cSRapkiewicz, Pawel 
321abe55efSEd Tanous namespace redfish
331abe55efSEd Tanous {
349391bb9cSRapkiewicz, Pawel 
354a0cb85cSEd Tanous enum class LinkType
364a0cb85cSEd Tanous {
374a0cb85cSEd Tanous     Local,
384a0cb85cSEd Tanous     Global
394a0cb85cSEd Tanous };
409391bb9cSRapkiewicz, Pawel 
419391bb9cSRapkiewicz, Pawel /**
429391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
439391bb9cSRapkiewicz, Pawel  */
441abe55efSEd Tanous struct IPv4AddressData
451abe55efSEd Tanous {
46179db1d7SKowalski, Kamil     std::string id;
474a0cb85cSEd Tanous     std::string address;
484a0cb85cSEd Tanous     std::string domain;
494a0cb85cSEd Tanous     std::string gateway;
509391bb9cSRapkiewicz, Pawel     std::string netmask;
519391bb9cSRapkiewicz, Pawel     std::string origin;
524a0cb85cSEd Tanous     LinkType linktype;
5301c6e858SSunitha Harish     bool isActive;
544a0cb85cSEd Tanous 
551abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
561abe55efSEd Tanous     {
574a0cb85cSEd Tanous         return id < obj.id;
581abe55efSEd Tanous     }
599391bb9cSRapkiewicz, Pawel };
609391bb9cSRapkiewicz, Pawel 
619391bb9cSRapkiewicz, Pawel /**
62e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
63e48c0fc5SRavi Teja  */
64e48c0fc5SRavi Teja struct IPv6AddressData
65e48c0fc5SRavi Teja {
66e48c0fc5SRavi Teja     std::string id;
67e48c0fc5SRavi Teja     std::string address;
68e48c0fc5SRavi Teja     std::string origin;
69e48c0fc5SRavi Teja     uint8_t prefixLength;
70e48c0fc5SRavi Teja 
71e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
72e48c0fc5SRavi Teja     {
73e48c0fc5SRavi Teja         return id < obj.id;
74e48c0fc5SRavi Teja     }
75e48c0fc5SRavi Teja };
76e48c0fc5SRavi Teja /**
779391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
789391bb9cSRapkiewicz, Pawel  * available from DBus
799391bb9cSRapkiewicz, Pawel  */
801abe55efSEd Tanous struct EthernetInterfaceData
811abe55efSEd Tanous {
824a0cb85cSEd Tanous     uint32_t speed;
8335fb5311STejas Patil     size_t mtuSize;
8482695a5bSJiaqing Zhao     bool autoNeg;
8582695a5bSJiaqing Zhao     bool dnsEnabled;
8682695a5bSJiaqing Zhao     bool ntpEnabled;
8782695a5bSJiaqing Zhao     bool hostNameEnabled;
88aa05fb27SJohnathan Mantey     bool linkUp;
89eeedda23SJohnathan Mantey     bool nicEnabled;
9082695a5bSJiaqing Zhao     std::string dhcpEnabled;
911f8c7b5dSJohnathan Mantey     std::string operatingMode;
9282695a5bSJiaqing Zhao     std::string hostName;
9382695a5bSJiaqing Zhao     std::string defaultGateway;
9482695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
9582695a5bSJiaqing Zhao     std::string macAddress;
9617e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
970f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
980f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
99d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1009391bb9cSRapkiewicz, Pawel };
1019391bb9cSRapkiewicz, Pawel 
1021f8c7b5dSJohnathan Mantey struct DHCPParameters
1031f8c7b5dSJohnathan Mantey {
1041f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
10582695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
10682695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
10782695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1081f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1091f8c7b5dSJohnathan Mantey };
1101f8c7b5dSJohnathan Mantey 
1119391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1129391bb9cSRapkiewicz, Pawel // into full dot notation
1131abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1141abe55efSEd Tanous {
1159391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1169391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1179391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1189391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1199391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1209391bb9cSRapkiewicz, Pawel     return netmask;
1219391bb9cSRapkiewicz, Pawel }
1229391bb9cSRapkiewicz, Pawel 
12382695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1241f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1251f8c7b5dSJohnathan Mantey {
1261f8c7b5dSJohnathan Mantey     if (isIPv4)
1271f8c7b5dSJohnathan Mantey     {
1281f8c7b5dSJohnathan Mantey         return (
1291f8c7b5dSJohnathan Mantey             (inputDHCP ==
1301f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1311f8c7b5dSJohnathan Mantey             (inputDHCP ==
1321f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1331f8c7b5dSJohnathan Mantey     }
1341f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1361f8c7b5dSJohnathan Mantey             (inputDHCP ==
1371f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1381f8c7b5dSJohnathan Mantey }
1391f8c7b5dSJohnathan Mantey 
1402c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1411f8c7b5dSJohnathan Mantey {
1421f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1431f8c7b5dSJohnathan Mantey     {
1441f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1451f8c7b5dSJohnathan Mantey     }
1463174e4dfSEd Tanous     if (isIPv4)
1471f8c7b5dSJohnathan Mantey     {
1481f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1491f8c7b5dSJohnathan Mantey     }
1503174e4dfSEd Tanous     if (isIPv6)
1511f8c7b5dSJohnathan Mantey     {
1521f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1531f8c7b5dSJohnathan Mantey     }
1541f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1551f8c7b5dSJohnathan Mantey }
1561f8c7b5dSJohnathan Mantey 
1574a0cb85cSEd Tanous inline std::string
1584a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1594a0cb85cSEd Tanous                                         bool isIPv4)
1601abe55efSEd Tanous {
1614a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1621abe55efSEd Tanous     {
1634a0cb85cSEd Tanous         return "Static";
1649391bb9cSRapkiewicz, Pawel     }
1654a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1661abe55efSEd Tanous     {
1674a0cb85cSEd Tanous         if (isIPv4)
1681abe55efSEd Tanous         {
1694a0cb85cSEd Tanous             return "IPv4LinkLocal";
1701abe55efSEd Tanous         }
1714a0cb85cSEd Tanous         return "LinkLocal";
1729391bb9cSRapkiewicz, Pawel     }
1734a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1741abe55efSEd Tanous     {
1754a0cb85cSEd Tanous         if (isIPv4)
1764a0cb85cSEd Tanous         {
1774a0cb85cSEd Tanous             return "DHCP";
1784a0cb85cSEd Tanous         }
1794a0cb85cSEd Tanous         return "DHCPv6";
1804a0cb85cSEd Tanous     }
1814a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1824a0cb85cSEd Tanous     {
1834a0cb85cSEd Tanous         return "SLAAC";
1844a0cb85cSEd Tanous     }
1854a0cb85cSEd Tanous     return "";
1864a0cb85cSEd Tanous }
1874a0cb85cSEd Tanous 
18802cad96eSEd Tanous inline bool extractEthernetInterfaceData(
18902cad96eSEd Tanous     const std::string& ethifaceId,
19002cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1914a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1924a0cb85cSEd Tanous {
1934c9afe43SEd Tanous     bool idFound = false;
19402cad96eSEd Tanous     for (const auto& objpath : dbusData)
1954a0cb85cSEd Tanous     {
19602cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
1974a0cb85cSEd Tanous         {
19881ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
199029573d4SEd Tanous             {
2004c9afe43SEd Tanous                 idFound = true;
2014a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2024a0cb85cSEd Tanous                 {
2034a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2044a0cb85cSEd Tanous                     {
2054a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2064a0cb85cSEd Tanous                         {
2074a0cb85cSEd Tanous                             const std::string* mac =
208abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2094a0cb85cSEd Tanous                             if (mac != nullptr)
2104a0cb85cSEd Tanous                             {
21182695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2124a0cb85cSEd Tanous                             }
2134a0cb85cSEd Tanous                         }
2144a0cb85cSEd Tanous                     }
2154a0cb85cSEd Tanous                 }
2164a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2174a0cb85cSEd Tanous                 {
2184a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2194a0cb85cSEd Tanous                     {
2204a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2214a0cb85cSEd Tanous                         {
2221b6b96c5SEd Tanous                             const uint32_t* id =
223abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2244a0cb85cSEd Tanous                             if (id != nullptr)
2254a0cb85cSEd Tanous                             {
22617e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2274a0cb85cSEd Tanous                             }
2284a0cb85cSEd Tanous                         }
2294a0cb85cSEd Tanous                     }
2304a0cb85cSEd Tanous                 }
2314a0cb85cSEd Tanous                 else if (ifacePair.first ==
2324a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2334a0cb85cSEd Tanous                 {
2344a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2354a0cb85cSEd Tanous                     {
2364a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2374a0cb85cSEd Tanous                         {
2382c70f800SEd Tanous                             const bool* autoNeg =
239abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2402c70f800SEd Tanous                             if (autoNeg != nullptr)
2414a0cb85cSEd Tanous                             {
24282695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2434a0cb85cSEd Tanous                             }
2444a0cb85cSEd Tanous                         }
2454a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2464a0cb85cSEd Tanous                         {
2474a0cb85cSEd Tanous                             const uint32_t* speed =
248abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2494a0cb85cSEd Tanous                             if (speed != nullptr)
2504a0cb85cSEd Tanous                             {
2514a0cb85cSEd Tanous                                 ethData.speed = *speed;
2524a0cb85cSEd Tanous                             }
2534a0cb85cSEd Tanous                         }
25435fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25535fb5311STejas Patil                         {
25635fb5311STejas Patil                             const uint32_t* mtuSize =
25735fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25835fb5311STejas Patil                             if (mtuSize != nullptr)
25935fb5311STejas Patil                             {
26035fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26135fb5311STejas Patil                             }
26235fb5311STejas Patil                         }
263aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
264aa05fb27SJohnathan Mantey                         {
265aa05fb27SJohnathan Mantey                             const bool* linkUp =
266aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
267aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
268aa05fb27SJohnathan Mantey                             {
269aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
270aa05fb27SJohnathan Mantey                             }
271aa05fb27SJohnathan Mantey                         }
272eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
273eeedda23SJohnathan Mantey                         {
274eeedda23SJohnathan Mantey                             const bool* nicEnabled =
275eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
276eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
277eeedda23SJohnathan Mantey                             {
278eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
279eeedda23SJohnathan Mantey                             }
280eeedda23SJohnathan Mantey                         }
281f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
282029573d4SEd Tanous                         {
283029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2848d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
285029573d4SEd Tanous                                     &propertyPair.second);
286029573d4SEd Tanous                             if (nameservers != nullptr)
287029573d4SEd Tanous                             {
288f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2890f6efdc1Smanojkiran.eda@gmail.com                             }
2900f6efdc1Smanojkiran.eda@gmail.com                         }
2910f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2920f6efdc1Smanojkiran.eda@gmail.com                         {
2930f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2948d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2950f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2960f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2970f6efdc1Smanojkiran.eda@gmail.com                             {
298f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2994a0cb85cSEd Tanous                             }
3004a0cb85cSEd Tanous                         }
3012a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3022a133282Smanojkiraneda                         {
3032c70f800SEd Tanous                             const std::string* dhcpEnabled =
3041f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3052c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3062a133282Smanojkiraneda                             {
30782695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3082a133282Smanojkiraneda                             }
3092a133282Smanojkiraneda                         }
310d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
311d24bfc7aSJennifer Lee                         {
312d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3138d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
314d24bfc7aSJennifer Lee                                     &propertyPair.second);
315d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
316d24bfc7aSJennifer Lee                             {
317f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
318d24bfc7aSJennifer Lee                             }
319d24bfc7aSJennifer Lee                         }
3209010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3219010ec2eSRavi Teja                         {
3229010ec2eSRavi Teja                             const std::string* defaultGateway =
3239010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3249010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3259010ec2eSRavi Teja                             {
3269010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3279010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3289010ec2eSRavi Teja                                 {
32982695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3309010ec2eSRavi Teja                                 }
3319010ec2eSRavi Teja                                 else
3329010ec2eSRavi Teja                                 {
33382695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3349010ec2eSRavi Teja                                 }
3359010ec2eSRavi Teja                             }
3369010ec2eSRavi Teja                         }
3379010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3389010ec2eSRavi Teja                         {
3399010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3409010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3419010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3429010ec2eSRavi Teja                             {
3439010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3449010ec2eSRavi Teja                                     *defaultGateway6;
3459010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3469010ec2eSRavi Teja                                 {
34782695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3489010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3499010ec2eSRavi Teja                                 }
3509010ec2eSRavi Teja                                 else
3519010ec2eSRavi Teja                                 {
35282695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3539010ec2eSRavi Teja                                         defaultGateway6Str;
3549010ec2eSRavi Teja                                 }
3559010ec2eSRavi Teja                             }
3569010ec2eSRavi Teja                         }
357029573d4SEd Tanous                     }
358029573d4SEd Tanous                 }
359029573d4SEd Tanous             }
3601f8c7b5dSJohnathan Mantey 
3611f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3621f8c7b5dSJohnathan Mantey             {
3631f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3641f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3651f8c7b5dSJohnathan Mantey                 {
3661f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3671f8c7b5dSJohnathan Mantey                     {
3681f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3691f8c7b5dSJohnathan Mantey                         {
3702c70f800SEd Tanous                             const bool* dnsEnabled =
3711f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3722c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3731f8c7b5dSJohnathan Mantey                             {
37482695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3751f8c7b5dSJohnathan Mantey                             }
3761f8c7b5dSJohnathan Mantey                         }
3771f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3781f8c7b5dSJohnathan Mantey                         {
3792c70f800SEd Tanous                             const bool* ntpEnabled =
3801f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3812c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3821f8c7b5dSJohnathan Mantey                             {
38382695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3841f8c7b5dSJohnathan Mantey                             }
3851f8c7b5dSJohnathan Mantey                         }
3861f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3871f8c7b5dSJohnathan Mantey                         {
3882c70f800SEd Tanous                             const bool* hostNameEnabled =
3891f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3902c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3911f8c7b5dSJohnathan Mantey                             {
39282695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3931f8c7b5dSJohnathan Mantey                             }
3941f8c7b5dSJohnathan Mantey                         }
3951f8c7b5dSJohnathan Mantey                     }
3961f8c7b5dSJohnathan Mantey                 }
3971f8c7b5dSJohnathan Mantey             }
398029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
399029573d4SEd Tanous             // to check eth number
400029573d4SEd Tanous             if (ifacePair.first ==
4014a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4024a0cb85cSEd Tanous             {
4034a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4044a0cb85cSEd Tanous                 {
4054a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4064a0cb85cSEd Tanous                     {
4074a0cb85cSEd Tanous                         const std::string* hostname =
4088d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4094a0cb85cSEd Tanous                         if (hostname != nullptr)
4104a0cb85cSEd Tanous                         {
41182695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4124a0cb85cSEd Tanous                         }
4134a0cb85cSEd Tanous                     }
4144a0cb85cSEd Tanous                 }
4154a0cb85cSEd Tanous             }
4164a0cb85cSEd Tanous         }
4174a0cb85cSEd Tanous     }
4184c9afe43SEd Tanous     return idFound;
4194a0cb85cSEd Tanous }
4204a0cb85cSEd Tanous 
421e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42201784826SJohnathan Mantey inline void
42381ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
424711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
42581ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
426e48c0fc5SRavi Teja {
427e48c0fc5SRavi Teja     const std::string ipv6PathStart =
42881ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
429e48c0fc5SRavi Teja 
430e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
431e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43281ce609eSEd Tanous     for (const auto& objpath : dbusData)
433e48c0fc5SRavi Teja     {
434e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
435*11ba3979SEd Tanous         if (objpath.first.str.starts_with(ipv6PathStart))
436e48c0fc5SRavi Teja         {
4379eb808c1SEd Tanous             for (const auto& interface : objpath.second)
438e48c0fc5SRavi Teja             {
439e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
440e48c0fc5SRavi Teja                 {
441e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
442e48c0fc5SRavi Teja                     // appropriate
443e48c0fc5SRavi Teja                     std::pair<
444e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
445e48c0fc5SRavi Teja                         bool>
44681ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4472c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4482c70f800SEd Tanous                     ipv6Address.id =
449271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4509eb808c1SEd Tanous                     for (const auto& property : interface.second)
451e48c0fc5SRavi Teja                     {
452e48c0fc5SRavi Teja                         if (property.first == "Address")
453e48c0fc5SRavi Teja                         {
454e48c0fc5SRavi Teja                             const std::string* address =
455e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
456e48c0fc5SRavi Teja                             if (address != nullptr)
457e48c0fc5SRavi Teja                             {
4582c70f800SEd Tanous                                 ipv6Address.address = *address;
459e48c0fc5SRavi Teja                             }
460e48c0fc5SRavi Teja                         }
461e48c0fc5SRavi Teja                         else if (property.first == "Origin")
462e48c0fc5SRavi Teja                         {
463e48c0fc5SRavi Teja                             const std::string* origin =
464e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
465e48c0fc5SRavi Teja                             if (origin != nullptr)
466e48c0fc5SRavi Teja                             {
4672c70f800SEd Tanous                                 ipv6Address.origin =
468e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
469e48c0fc5SRavi Teja                                                                         false);
470e48c0fc5SRavi Teja                             }
471e48c0fc5SRavi Teja                         }
472e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
473e48c0fc5SRavi Teja                         {
474e48c0fc5SRavi Teja                             const uint8_t* prefix =
475e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
476e48c0fc5SRavi Teja                             if (prefix != nullptr)
477e48c0fc5SRavi Teja                             {
4782c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
479e48c0fc5SRavi Teja                             }
480e48c0fc5SRavi Teja                         }
481889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
482889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
483889ff694SAsmitha Karunanithi                         {
484889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
485889ff694SAsmitha Karunanithi                         }
486e48c0fc5SRavi Teja                         else
487e48c0fc5SRavi Teja                         {
488e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
489e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
490e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
491e48c0fc5SRavi Teja                         }
492e48c0fc5SRavi Teja                     }
493e48c0fc5SRavi Teja                 }
494e48c0fc5SRavi Teja             }
495e48c0fc5SRavi Teja         }
496e48c0fc5SRavi Teja     }
497e48c0fc5SRavi Teja }
498e48c0fc5SRavi Teja 
4994a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50001784826SJohnathan Mantey inline void
50181ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
502711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
50381ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5044a0cb85cSEd Tanous {
5054a0cb85cSEd Tanous     const std::string ipv4PathStart =
50681ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5074a0cb85cSEd Tanous 
5084a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5094a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
51081ce609eSEd Tanous     for (const auto& objpath : dbusData)
5114a0cb85cSEd Tanous     {
5124a0cb85cSEd Tanous         // Check if proper pattern for object path appears
513*11ba3979SEd Tanous         if (objpath.first.str.starts_with(ipv4PathStart))
5144a0cb85cSEd Tanous         {
5159eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5164a0cb85cSEd Tanous             {
5174a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5184a0cb85cSEd Tanous                 {
5194a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5204a0cb85cSEd Tanous                     // appropriate
5214a0cb85cSEd Tanous                     std::pair<
5224a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5234a0cb85cSEd Tanous                         bool>
52481ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5252c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5262c70f800SEd Tanous                     ipv4Address.id =
527271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5289eb808c1SEd Tanous                     for (const auto& property : interface.second)
5294a0cb85cSEd Tanous                     {
5304a0cb85cSEd Tanous                         if (property.first == "Address")
5314a0cb85cSEd Tanous                         {
5324a0cb85cSEd Tanous                             const std::string* address =
533abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5344a0cb85cSEd Tanous                             if (address != nullptr)
5354a0cb85cSEd Tanous                             {
5362c70f800SEd Tanous                                 ipv4Address.address = *address;
5374a0cb85cSEd Tanous                             }
5384a0cb85cSEd Tanous                         }
5394a0cb85cSEd Tanous                         else if (property.first == "Origin")
5404a0cb85cSEd Tanous                         {
5414a0cb85cSEd Tanous                             const std::string* origin =
542abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5434a0cb85cSEd Tanous                             if (origin != nullptr)
5444a0cb85cSEd Tanous                             {
5452c70f800SEd Tanous                                 ipv4Address.origin =
5464a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5474a0cb85cSEd Tanous                                                                         true);
5484a0cb85cSEd Tanous                             }
5494a0cb85cSEd Tanous                         }
5504a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5514a0cb85cSEd Tanous                         {
5524a0cb85cSEd Tanous                             const uint8_t* mask =
553abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5544a0cb85cSEd Tanous                             if (mask != nullptr)
5554a0cb85cSEd Tanous                             {
5564a0cb85cSEd Tanous                                 // convert it to the string
5572c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5584a0cb85cSEd Tanous                             }
5594a0cb85cSEd Tanous                         }
560889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
561889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
562889ff694SAsmitha Karunanithi                         {
563889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
564889ff694SAsmitha Karunanithi                         }
5654a0cb85cSEd Tanous                         else
5664a0cb85cSEd Tanous                         {
5674a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5684a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5694a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5704a0cb85cSEd Tanous                         }
5714a0cb85cSEd Tanous                     }
5724a0cb85cSEd Tanous                     // Check if given address is local, or global
5732c70f800SEd Tanous                     ipv4Address.linktype =
574*11ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
57518659d10SJohnathan Mantey                             ? LinkType::Local
57618659d10SJohnathan Mantey                             : LinkType::Global;
5774a0cb85cSEd Tanous                 }
5784a0cb85cSEd Tanous             }
5794a0cb85cSEd Tanous         }
5804a0cb85cSEd Tanous     }
5814a0cb85cSEd Tanous }
582588c3f0dSKowalski, Kamil 
583588c3f0dSKowalski, Kamil /**
584179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
585179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
586179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
587179db1d7SKowalski, Kamil  *
588179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
589179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
590179db1d7SKowalski, Kamil  *
591179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
592179db1d7SKowalski, Kamil  */
5934a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
5941abe55efSEd Tanous                                        uint8_t* bits = nullptr)
5951abe55efSEd Tanous {
596179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
597179db1d7SKowalski, Kamil 
598179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
599179db1d7SKowalski, Kamil 
6004a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6011abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6021abe55efSEd Tanous     {
603179db1d7SKowalski, Kamil         return false;
604179db1d7SKowalski, Kamil     }
605179db1d7SKowalski, Kamil 
6061abe55efSEd Tanous     if (bits != nullptr)
6071abe55efSEd Tanous     {
608179db1d7SKowalski, Kamil         *bits = 0;
609179db1d7SKowalski, Kamil     }
610179db1d7SKowalski, Kamil 
611543f4400SEd Tanous     char* endPtr = nullptr;
612179db1d7SKowalski, Kamil     long previousValue = 255;
613543f4400SEd Tanous     bool firstZeroInByteHit = false;
6141abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6151abe55efSEd Tanous     {
6161abe55efSEd Tanous         if (byte.empty())
6171abe55efSEd Tanous         {
6181db9ca37SKowalski, Kamil             return false;
6191db9ca37SKowalski, Kamil         }
6201db9ca37SKowalski, Kamil 
621179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6221db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
623179db1d7SKowalski, Kamil 
6244a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6254a0cb85cSEd Tanous         // is not 100% number
6261abe55efSEd Tanous         if (*endPtr != '\0')
6271abe55efSEd Tanous         {
628179db1d7SKowalski, Kamil             return false;
629179db1d7SKowalski, Kamil         }
630179db1d7SKowalski, Kamil 
631179db1d7SKowalski, Kamil         // Value should be contained in byte
6321abe55efSEd Tanous         if (value < 0 || value > 255)
6331abe55efSEd Tanous         {
634179db1d7SKowalski, Kamil             return false;
635179db1d7SKowalski, Kamil         }
636179db1d7SKowalski, Kamil 
6371abe55efSEd Tanous         if (bits != nullptr)
6381abe55efSEd Tanous         {
639179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6401abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6411abe55efSEd Tanous             {
642179db1d7SKowalski, Kamil                 return false;
643179db1d7SKowalski, Kamil             }
644179db1d7SKowalski, Kamil 
645179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
646179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
647179db1d7SKowalski, Kamil 
648179db1d7SKowalski, Kamil             // Count bits
64923a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6501abe55efSEd Tanous             {
651e662eae8SEd Tanous                 if ((value & (1L << bitIdx)) != 0)
6521abe55efSEd Tanous                 {
6531abe55efSEd Tanous                     if (firstZeroInByteHit)
6541abe55efSEd Tanous                     {
655179db1d7SKowalski, Kamil                         // Continuity not preserved
656179db1d7SKowalski, Kamil                         return false;
6571abe55efSEd Tanous                     }
658179db1d7SKowalski, Kamil                     (*bits)++;
659179db1d7SKowalski, Kamil                 }
6601abe55efSEd Tanous                 else
6611abe55efSEd Tanous                 {
662179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
663179db1d7SKowalski, Kamil                 }
664179db1d7SKowalski, Kamil             }
665179db1d7SKowalski, Kamil         }
666179db1d7SKowalski, Kamil 
667179db1d7SKowalski, Kamil         previousValue = value;
668179db1d7SKowalski, Kamil     }
669179db1d7SKowalski, Kamil 
670179db1d7SKowalski, Kamil     return true;
671179db1d7SKowalski, Kamil }
672179db1d7SKowalski, Kamil 
673179db1d7SKowalski, Kamil /**
67401784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
675179db1d7SKowalski, Kamil  *
676179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
677179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
678179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
679179db1d7SKowalski, Kamil  *
680179db1d7SKowalski, Kamil  * @return None
681179db1d7SKowalski, Kamil  */
6824a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
6838d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6841abe55efSEd Tanous {
68555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
686286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
6871abe55efSEd Tanous         if (ec)
6881abe55efSEd Tanous         {
689a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6901abe55efSEd Tanous         }
691179db1d7SKowalski, Kamil         },
692179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
693179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
694179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
695179db1d7SKowalski, Kamil }
696179db1d7SKowalski, Kamil 
697244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
698244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
699244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7009010ec2eSRavi Teja {
7019010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7029010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7039010ec2eSRavi Teja         if (ec)
7049010ec2eSRavi Teja         {
7059010ec2eSRavi Teja             messages::internalError(asyncResp->res);
7069010ec2eSRavi Teja             return;
7079010ec2eSRavi Teja         }
7089010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
7099010ec2eSRavi Teja         },
7109010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7119010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7129010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7139010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
714168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7159010ec2eSRavi Teja }
716179db1d7SKowalski, Kamil /**
71701784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
718179db1d7SKowalski, Kamil  *
71901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
72001784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
72101784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
72201784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
723179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
724179db1d7SKowalski, Kamil  *
725179db1d7SKowalski, Kamil  * @return None
726179db1d7SKowalski, Kamil  */
727cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
728cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7298d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7301abe55efSEd Tanous {
731002d39b4SEd Tanous     auto createIpHandler =
732002d39b4SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
7331abe55efSEd Tanous         if (ec)
7341abe55efSEd Tanous         {
735a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7369010ec2eSRavi Teja             return;
737179db1d7SKowalski, Kamil         }
7389010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7399010ec2eSRavi Teja     };
7409010ec2eSRavi Teja 
7419010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7429010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
743179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
744179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
74501784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
746179db1d7SKowalski, Kamil         gateway);
747179db1d7SKowalski, Kamil }
748e48c0fc5SRavi Teja 
749e48c0fc5SRavi Teja /**
75001784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
75101784826SJohnathan Mantey  * static IPv4 entry
75201784826SJohnathan Mantey  *
75301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
75401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
75501784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75601784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75701784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
75801784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
75901784826SJohnathan Mantey  *
76001784826SJohnathan Mantey  * @return None
76101784826SJohnathan Mantey  */
7628d1b46d7Szhanghch05 inline void
7638d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7648d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
76501784826SJohnathan Mantey                         const std::string& address,
7668d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
76701784826SJohnathan Mantey {
76801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
76901784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
77001784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
77101784826SJohnathan Mantey         if (ec)
77201784826SJohnathan Mantey         {
77301784826SJohnathan Mantey             messages::internalError(asyncResp->res);
7749010ec2eSRavi Teja             return;
77501784826SJohnathan Mantey         }
7769010ec2eSRavi Teja 
77701784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
778002d39b4SEd Tanous             [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
77923a21a1cSEd Tanous             if (ec2)
78001784826SJohnathan Mantey             {
78101784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
7829010ec2eSRavi Teja                 return;
78301784826SJohnathan Mantey             }
7849010ec2eSRavi Teja             updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
78501784826SJohnathan Mantey             },
78601784826SJohnathan Mantey             "xyz.openbmc_project.Network",
78701784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
78801784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
78901784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
79001784826SJohnathan Mantey             prefixLength, gateway);
79101784826SJohnathan Mantey         },
79201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
79301784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
79401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
79501784826SJohnathan Mantey }
79601784826SJohnathan Mantey 
79701784826SJohnathan Mantey /**
798e48c0fc5SRavi Teja  * @brief Deletes given IPv6
799e48c0fc5SRavi Teja  *
800e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
801e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
802e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
803e48c0fc5SRavi Teja  *
804e48c0fc5SRavi Teja  * @return None
805e48c0fc5SRavi Teja  */
806e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8078d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
808e48c0fc5SRavi Teja {
809e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
810286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
811e48c0fc5SRavi Teja         if (ec)
812e48c0fc5SRavi Teja         {
813e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
814e48c0fc5SRavi Teja         }
815e48c0fc5SRavi Teja         },
816e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
817e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
818e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
819e48c0fc5SRavi Teja }
820e48c0fc5SRavi Teja 
821e48c0fc5SRavi Teja /**
82201784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
82301784826SJohnathan Mantey  * static IPv6 entry
82401784826SJohnathan Mantey  *
82501784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
82601784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
82701784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
82801784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
82901784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
83001784826SJohnathan Mantey  *
83101784826SJohnathan Mantey  * @return None
83201784826SJohnathan Mantey  */
8338d1b46d7Szhanghch05 inline void
8348d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8358d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8368d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
83701784826SJohnathan Mantey {
83801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
83901784826SJohnathan Mantey         [asyncResp, ifaceId, address,
84001784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
84101784826SJohnathan Mantey         if (ec)
84201784826SJohnathan Mantey         {
84301784826SJohnathan Mantey             messages::internalError(asyncResp->res);
84401784826SJohnathan Mantey         }
84501784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
84623a21a1cSEd Tanous             [asyncResp](const boost::system::error_code ec2) {
84723a21a1cSEd Tanous             if (ec2)
84801784826SJohnathan Mantey             {
84901784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
85001784826SJohnathan Mantey             }
85101784826SJohnathan Mantey             },
85201784826SJohnathan Mantey             "xyz.openbmc_project.Network",
85301784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
85401784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
85501784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
85601784826SJohnathan Mantey             prefixLength, "");
85701784826SJohnathan Mantey         },
85801784826SJohnathan Mantey         "xyz.openbmc_project.Network",
85901784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
86001784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
86101784826SJohnathan Mantey }
86201784826SJohnathan Mantey 
86301784826SJohnathan Mantey /**
864e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
865e48c0fc5SRavi Teja  *
866e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
867e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
868e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
869e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
870e48c0fc5SRavi Teja  *
871e48c0fc5SRavi Teja  * @return None
872e48c0fc5SRavi Teja  */
87301784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
87401784826SJohnathan Mantey                        const std::string& address,
8758d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
876e48c0fc5SRavi Teja {
877e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
878e48c0fc5SRavi Teja         if (ec)
879e48c0fc5SRavi Teja         {
880e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
881e48c0fc5SRavi Teja         }
882e48c0fc5SRavi Teja     };
883e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8844e0453b1SGunnar Mills     // does not have associated gateway property
885e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
886e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
887e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
888e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
889e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
890e48c0fc5SRavi Teja         "");
891e48c0fc5SRavi Teja }
892e48c0fc5SRavi Teja 
893179db1d7SKowalski, Kamil /**
894179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
895179db1d7SKowalski, Kamil  * Object
896179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8974a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
898179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
899179db1d7SKowalski, Kamil  * into JSON
900179db1d7SKowalski, Kamil  */
901179db1d7SKowalski, Kamil template <typename CallbackFunc>
90281ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9031abe55efSEd Tanous                           CallbackFunc&& callback)
9041abe55efSEd Tanous {
90555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
906f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
907f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
90881ce609eSEd Tanous             const boost::system::error_code errorCode,
90902cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
91055c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
9114a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
912e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
913179db1d7SKowalski, Kamil 
91481ce609eSEd Tanous         if (errorCode)
9151abe55efSEd Tanous         {
91601784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
917179db1d7SKowalski, Kamil             return;
918179db1d7SKowalski, Kamil         }
919179db1d7SKowalski, Kamil 
920002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
9214c9afe43SEd Tanous         if (!found)
9224c9afe43SEd Tanous         {
92301784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
9244c9afe43SEd Tanous             return;
9254c9afe43SEd Tanous         }
9264c9afe43SEd Tanous 
9272c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
928179db1d7SKowalski, Kamil         // Fix global GW
9291abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
9301abe55efSEd Tanous         {
931c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
932c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
9339010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9341abe55efSEd Tanous             {
93582695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
936179db1d7SKowalski, Kamil             }
937179db1d7SKowalski, Kamil         }
938179db1d7SKowalski, Kamil 
9392c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
9404e0453b1SGunnar Mills         // Finally make a callback with useful data
94101784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
942179db1d7SKowalski, Kamil         },
943179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
944179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
945271584abSEd Tanous }
946179db1d7SKowalski, Kamil 
947179db1d7SKowalski, Kamil /**
9489391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9499391bb9cSRapkiewicz, Pawel  * Manager
9501abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9511abe55efSEd Tanous  * into JSON.
9529391bb9cSRapkiewicz, Pawel  */
9539391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9541abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9551abe55efSEd Tanous {
95655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
957f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
95881ce609eSEd Tanous             const boost::system::error_code errorCode,
959711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9601abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
9611abe55efSEd Tanous         // ethernet interfaces
9622c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
9632c70f800SEd Tanous         ifaceList.reserve(resp.size());
96481ce609eSEd Tanous         if (errorCode)
9651abe55efSEd Tanous         {
9662c70f800SEd Tanous             callback(false, ifaceList);
9679391bb9cSRapkiewicz, Pawel             return;
9689391bb9cSRapkiewicz, Pawel         }
9699391bb9cSRapkiewicz, Pawel 
9709391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
9714a0cb85cSEd Tanous         for (const auto& objpath : resp)
9721abe55efSEd Tanous         {
9739391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
9744a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
9751abe55efSEd Tanous             {
9761abe55efSEd Tanous                 // If interface is
9774a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
9784a0cb85cSEd Tanous                 // what we're looking for.
9799391bb9cSRapkiewicz, Pawel                 if (interface.first ==
9801abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
9811abe55efSEd Tanous                 {
9822dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
9832dfd18efSEd Tanous                     if (ifaceId.empty())
9841abe55efSEd Tanous                     {
9852dfd18efSEd Tanous                         continue;
9869391bb9cSRapkiewicz, Pawel                     }
9872dfd18efSEd Tanous                     // and put it into output vector.
9882dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
9899391bb9cSRapkiewicz, Pawel                 }
9909391bb9cSRapkiewicz, Pawel             }
9919391bb9cSRapkiewicz, Pawel         }
992a434f2bdSEd Tanous         // Finally make a callback with useful data
9932c70f800SEd Tanous         callback(true, ifaceList);
9949391bb9cSRapkiewicz, Pawel         },
995aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
997271584abSEd Tanous }
9989391bb9cSRapkiewicz, Pawel 
9994f48d5f6SEd Tanous inline void
10004f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10018d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10021abe55efSEd Tanous {
1003ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1004ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1005ab6554f1SJoshi-Mansi     {
1006ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1007ab6554f1SJoshi-Mansi                                            "HostName");
1008ab6554f1SJoshi-Mansi         return;
1009ab6554f1SJoshi-Mansi     }
1010bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1011bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10124a0cb85cSEd Tanous         if (ec)
10134a0cb85cSEd Tanous         {
1014a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
10151abe55efSEd Tanous         }
1016bc0bd6e0SEd Tanous         },
1017bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1018bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1019bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1020168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1021588c3f0dSKowalski, Kamil }
1022588c3f0dSKowalski, Kamil 
10234f48d5f6SEd Tanous inline void
102435fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
102535fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
102635fb5311STejas Patil {
102735fb5311STejas Patil     sdbusplus::message::object_path objPath =
102835fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
102935fb5311STejas Patil     crow::connections::systemBus->async_method_call(
103035fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
103135fb5311STejas Patil         if (ec)
103235fb5311STejas Patil         {
103335fb5311STejas Patil             messages::internalError(asyncResp->res);
103435fb5311STejas Patil         }
103535fb5311STejas Patil         },
103635fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
103735fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
103835fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
103935fb5311STejas Patil         std::variant<size_t>(mtuSize));
104035fb5311STejas Patil }
104135fb5311STejas Patil 
104235fb5311STejas Patil inline void
10434f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1044bf648f77SEd Tanous                           const std::string& domainname,
10458d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1046ab6554f1SJoshi-Mansi {
1047ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1048ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1049ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1050ab6554f1SJoshi-Mansi         if (ec)
1051ab6554f1SJoshi-Mansi         {
1052ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
1053ab6554f1SJoshi-Mansi         }
1054ab6554f1SJoshi-Mansi         },
1055ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1056ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1057ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1058ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1059168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1060ab6554f1SJoshi-Mansi }
1061ab6554f1SJoshi-Mansi 
10624f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1063bf648f77SEd Tanous {
1064bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1065bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1066bf648f77SEd Tanous     {
1067bf648f77SEd Tanous         return false;
1068bf648f77SEd Tanous     }
1069bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1070bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1071bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1072bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1073bf648f77SEd Tanous     const std::regex pattern(
1074bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1075bf648f77SEd Tanous 
1076bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1077bf648f77SEd Tanous }
1078bf648f77SEd Tanous 
10794f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1080bf648f77SEd Tanous {
1081bf648f77SEd Tanous     // Can have multiple subdomains
1082bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
10830fda0f12SGeorge Liu     const std::regex pattern(
10840fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1085bf648f77SEd Tanous 
1086bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1087bf648f77SEd Tanous }
1088bf648f77SEd Tanous 
10894f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10908d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1091ab6554f1SJoshi-Mansi {
1092ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1093ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1094ab6554f1SJoshi-Mansi     {
1095ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1096ab6554f1SJoshi-Mansi         return;
1097ab6554f1SJoshi-Mansi     }
1098ab6554f1SJoshi-Mansi 
1099ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1100ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1101ab6554f1SJoshi-Mansi     {
1102ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1103ab6554f1SJoshi-Mansi         return;
1104ab6554f1SJoshi-Mansi     }
1105ab6554f1SJoshi-Mansi 
1106ab6554f1SJoshi-Mansi     std::string hostname;
1107ab6554f1SJoshi-Mansi     std::string domainname;
1108ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1109ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1110ab6554f1SJoshi-Mansi 
1111ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1112ab6554f1SJoshi-Mansi     {
1113ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1114ab6554f1SJoshi-Mansi         return;
1115ab6554f1SJoshi-Mansi     }
1116ab6554f1SJoshi-Mansi 
1117ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1118ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1119ab6554f1SJoshi-Mansi }
1120ab6554f1SJoshi-Mansi 
11214f48d5f6SEd Tanous inline void
11224f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1123bf648f77SEd Tanous                           const std::string& macAddress,
11248d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1125d577665bSRatan Gupta {
1126d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1127d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1128d577665bSRatan Gupta         if (ec)
1129d577665bSRatan Gupta         {
1130d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1131d577665bSRatan Gupta             return;
1132d577665bSRatan Gupta         }
1133d577665bSRatan Gupta         },
1134d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1135d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1136d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1137d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1138168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1139d577665bSRatan Gupta }
1140286b9118SJohnathan Mantey 
11414f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11424f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11434f48d5f6SEd Tanous                            const bool v6Value,
11448d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1145da131a9aSJennifer Lee {
11462c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1147da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1148da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1149da131a9aSJennifer Lee         if (ec)
1150da131a9aSJennifer Lee         {
1151da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1152da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1153da131a9aSJennifer Lee             return;
1154da131a9aSJennifer Lee         }
11558f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1156da131a9aSJennifer Lee         },
1157da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1158da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1159da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1160da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1161168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1162da131a9aSJennifer Lee }
11631f8c7b5dSJohnathan Mantey 
11644f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1165eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11668d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1167eeedda23SJohnathan Mantey {
1168eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1169eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1170eeedda23SJohnathan Mantey         if (ec)
1171eeedda23SJohnathan Mantey         {
1172eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1173eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1174eeedda23SJohnathan Mantey             return;
1175eeedda23SJohnathan Mantey         }
1176eeedda23SJohnathan Mantey         },
1177eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1178eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1179eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1180eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1181168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1182eeedda23SJohnathan Mantey }
1183eeedda23SJohnathan Mantey 
11844f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11858d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1186da131a9aSJennifer Lee {
1187da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1188da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1189da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1190da131a9aSJennifer Lee         if (ec)
1191da131a9aSJennifer Lee         {
1192da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1193da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1194da131a9aSJennifer Lee             return;
1195da131a9aSJennifer Lee         }
1196da131a9aSJennifer Lee         },
1197da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1198da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1199da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1200da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1201168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1202da131a9aSJennifer Lee }
1203d577665bSRatan Gupta 
12044f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12051f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1206f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1207f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12088d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1209da131a9aSJennifer Lee {
121082695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
121182695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1212da131a9aSJennifer Lee 
12131f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12141f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12151f8c7b5dSJohnathan Mantey 
12161f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12171f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1218da131a9aSJennifer Lee     {
12191f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12201f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12211f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12221f8c7b5dSJohnathan Mantey         {
1223bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1224bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12251f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1226da131a9aSJennifer Lee             return;
1227da131a9aSJennifer Lee         }
12281f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12291f8c7b5dSJohnathan Mantey     }
12301f8c7b5dSJohnathan Mantey     else
1231da131a9aSJennifer Lee     {
12321f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12331f8c7b5dSJohnathan Mantey     }
12341f8c7b5dSJohnathan Mantey 
12351f8c7b5dSJohnathan Mantey     bool nextDNS{};
123682695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
12371f8c7b5dSJohnathan Mantey     {
123882695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
12391f8c7b5dSJohnathan Mantey         {
12401f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12411f8c7b5dSJohnathan Mantey             return;
12421f8c7b5dSJohnathan Mantey         }
124382695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12441f8c7b5dSJohnathan Mantey     }
124582695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
12461f8c7b5dSJohnathan Mantey     {
124782695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12481f8c7b5dSJohnathan Mantey     }
124982695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
12501f8c7b5dSJohnathan Mantey     {
125182695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
12521f8c7b5dSJohnathan Mantey     }
12531f8c7b5dSJohnathan Mantey     else
12541f8c7b5dSJohnathan Mantey     {
125582695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
12561f8c7b5dSJohnathan Mantey     }
12571f8c7b5dSJohnathan Mantey 
12581f8c7b5dSJohnathan Mantey     bool nextNTP{};
125982695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
12601f8c7b5dSJohnathan Mantey     {
126182695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
12621f8c7b5dSJohnathan Mantey         {
12631f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12641f8c7b5dSJohnathan Mantey             return;
12651f8c7b5dSJohnathan Mantey         }
126682695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12671f8c7b5dSJohnathan Mantey     }
126882695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
12691f8c7b5dSJohnathan Mantey     {
127082695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12711f8c7b5dSJohnathan Mantey     }
127282695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12731f8c7b5dSJohnathan Mantey     {
127482695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12751f8c7b5dSJohnathan Mantey     }
12761f8c7b5dSJohnathan Mantey     else
12771f8c7b5dSJohnathan Mantey     {
127882695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12791f8c7b5dSJohnathan Mantey     }
12801f8c7b5dSJohnathan Mantey 
12811f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
128282695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12831f8c7b5dSJohnathan Mantey     {
128482695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
12851f8c7b5dSJohnathan Mantey         {
12861f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12871f8c7b5dSJohnathan Mantey             return;
12881f8c7b5dSJohnathan Mantey         }
128982695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12901f8c7b5dSJohnathan Mantey     }
129182695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12921f8c7b5dSJohnathan Mantey     {
129382695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12941f8c7b5dSJohnathan Mantey     }
129582695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12961f8c7b5dSJohnathan Mantey     {
129782695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12981f8c7b5dSJohnathan Mantey     }
12991f8c7b5dSJohnathan Mantey     else
13001f8c7b5dSJohnathan Mantey     {
130182695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
13021f8c7b5dSJohnathan Mantey     }
13031f8c7b5dSJohnathan Mantey 
1304da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13051f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13061f8c7b5dSJohnathan Mantey                    asyncResp);
1307da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13081f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1309da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13101f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13111f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13121f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1313da131a9aSJennifer Lee }
131401784826SJohnathan Mantey 
13154f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13162c70f800SEd Tanous     getNextStaticIpEntry(
1317bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1318bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
131901784826SJohnathan Mantey {
132017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
132117a897dfSManojkiran Eda         return value.origin == "Static";
132217a897dfSManojkiran Eda     });
132301784826SJohnathan Mantey }
132401784826SJohnathan Mantey 
13254f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13262c70f800SEd Tanous     getNextStaticIpEntry(
1327bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1328bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
132901784826SJohnathan Mantey {
133017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
133117a897dfSManojkiran Eda         return value.origin == "Static";
133217a897dfSManojkiran Eda     });
133301784826SJohnathan Mantey }
133401784826SJohnathan Mantey 
13354f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1336f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
133701784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13388d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13391abe55efSEd Tanous {
134001784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1341f476acbfSRatan Gupta     {
134271f52d96SEd Tanous         messages::propertyValueTypeError(
134371f52d96SEd Tanous             asyncResp->res,
1344bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1345d1d50814SRavi Teja             "IPv4StaticAddresses");
1346f476acbfSRatan Gupta         return;
1347f476acbfSRatan Gupta     }
1348f476acbfSRatan Gupta 
1349271584abSEd Tanous     unsigned entryIdx = 1;
135001784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
135101784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
135201784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
135301784826SJohnathan Mantey     // into the NIC.
135485ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13552c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
135601784826SJohnathan Mantey 
1357537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13581abe55efSEd Tanous     {
13594a0cb85cSEd Tanous         std::string pathString =
1360d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1361179db1d7SKowalski, Kamil 
136201784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1363f476acbfSRatan Gupta         {
1364537174c4SEd Tanous             std::optional<std::string> address;
1365537174c4SEd Tanous             std::optional<std::string> subnetMask;
1366537174c4SEd Tanous             std::optional<std::string> gateway;
1367537174c4SEd Tanous 
1368537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13697e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13707e27d832SJohnathan Mantey                                      "Gateway", gateway))
1371537174c4SEd Tanous             {
137201784826SJohnathan Mantey                 messages::propertyValueFormatError(
137371f52d96SEd Tanous                     asyncResp->res,
137471f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
137571f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
137671f52d96SEd Tanous                     pathString);
1377537174c4SEd Tanous                 return;
1378179db1d7SKowalski, Kamil             }
1379179db1d7SKowalski, Kamil 
138001784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
138101784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
138201784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
138301784826SJohnathan Mantey             // current request.
1384271584abSEd Tanous             const std::string* addr = nullptr;
1385271584abSEd Tanous             const std::string* gw = nullptr;
138601784826SJohnathan Mantey             uint8_t prefixLength = 0;
138701784826SJohnathan Mantey             bool errorInEntry = false;
1388537174c4SEd Tanous             if (address)
13891abe55efSEd Tanous             {
139001784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
13911abe55efSEd Tanous                 {
139201784826SJohnathan Mantey                     addr = &(*address);
13934a0cb85cSEd Tanous                 }
139401784826SJohnathan Mantey                 else
139501784826SJohnathan Mantey                 {
1396bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1397bf648f77SEd Tanous                                                        pathString + "/Address");
139801784826SJohnathan Mantey                     errorInEntry = true;
139901784826SJohnathan Mantey                 }
140001784826SJohnathan Mantey             }
140185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
140201784826SJohnathan Mantey             {
140385ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
140401784826SJohnathan Mantey             }
140501784826SJohnathan Mantey             else
140601784826SJohnathan Mantey             {
140701784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
140801784826SJohnathan Mantey                                           pathString + "/Address");
140901784826SJohnathan Mantey                 errorInEntry = true;
14104a0cb85cSEd Tanous             }
14114a0cb85cSEd Tanous 
1412537174c4SEd Tanous             if (subnetMask)
14134a0cb85cSEd Tanous             {
1414537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14154a0cb85cSEd Tanous                 {
1416f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1417537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14184a0cb85cSEd Tanous                         pathString + "/SubnetMask");
141901784826SJohnathan Mantey                     errorInEntry = true;
14204a0cb85cSEd Tanous                 }
14214a0cb85cSEd Tanous             }
142285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14234a0cb85cSEd Tanous             {
142485ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
142501784826SJohnathan Mantey                                                 &prefixLength))
14264a0cb85cSEd Tanous                 {
142701784826SJohnathan Mantey                     messages::propertyValueFormatError(
142885ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
142901784826SJohnathan Mantey                         pathString + "/SubnetMask");
143001784826SJohnathan Mantey                     errorInEntry = true;
14314a0cb85cSEd Tanous                 }
14324a0cb85cSEd Tanous             }
14331abe55efSEd Tanous             else
14341abe55efSEd Tanous             {
143501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
143601784826SJohnathan Mantey                                           pathString + "/SubnetMask");
143701784826SJohnathan Mantey                 errorInEntry = true;
143801784826SJohnathan Mantey             }
143901784826SJohnathan Mantey 
144001784826SJohnathan Mantey             if (gateway)
144101784826SJohnathan Mantey             {
144201784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
144301784826SJohnathan Mantey                 {
144401784826SJohnathan Mantey                     gw = &(*gateway);
144501784826SJohnathan Mantey                 }
144601784826SJohnathan Mantey                 else
144701784826SJohnathan Mantey                 {
1448bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1449bf648f77SEd Tanous                                                        pathString + "/Gateway");
145001784826SJohnathan Mantey                     errorInEntry = true;
145101784826SJohnathan Mantey                 }
145201784826SJohnathan Mantey             }
145385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
145401784826SJohnathan Mantey             {
145585ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
145601784826SJohnathan Mantey             }
145701784826SJohnathan Mantey             else
14581abe55efSEd Tanous             {
1459a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14604a0cb85cSEd Tanous                                           pathString + "/Gateway");
146101784826SJohnathan Mantey                 errorInEntry = true;
14624a0cb85cSEd Tanous             }
14634a0cb85cSEd Tanous 
146401784826SJohnathan Mantey             if (errorInEntry)
14651abe55efSEd Tanous             {
146601784826SJohnathan Mantey                 return;
14674a0cb85cSEd Tanous             }
14684a0cb85cSEd Tanous 
146985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14701abe55efSEd Tanous             {
147185ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1472bf648f77SEd Tanous                                     *addr, asyncResp);
147385ffe86aSJiaqing Zhao                 nicIpEntry =
147485ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1475588c3f0dSKowalski, Kamil             }
147601784826SJohnathan Mantey             else
147701784826SJohnathan Mantey             {
1478cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1479cb13a392SEd Tanous                            asyncResp);
14804a0cb85cSEd Tanous             }
14814a0cb85cSEd Tanous             entryIdx++;
14824a0cb85cSEd Tanous         }
148301784826SJohnathan Mantey         else
148401784826SJohnathan Mantey         {
148585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
148601784826SJohnathan Mantey             {
148701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
148801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
148901784826SJohnathan Mantey                 // in error, so bail out.
149001784826SJohnathan Mantey                 if (thisJson.is_null())
149101784826SJohnathan Mantey                 {
149201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
149301784826SJohnathan Mantey                     return;
149401784826SJohnathan Mantey                 }
149501784826SJohnathan Mantey                 messages::propertyValueFormatError(
149671f52d96SEd Tanous                     asyncResp->res,
149771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
149871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
149971f52d96SEd Tanous                     pathString);
150001784826SJohnathan Mantey                 return;
150101784826SJohnathan Mantey             }
150201784826SJohnathan Mantey 
150301784826SJohnathan Mantey             if (thisJson.is_null())
150401784826SJohnathan Mantey             {
150585ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
150601784826SJohnathan Mantey             }
150785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
150801784826SJohnathan Mantey             {
150985ffe86aSJiaqing Zhao                 nicIpEntry =
151085ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
151101784826SJohnathan Mantey             }
151201784826SJohnathan Mantey             entryIdx++;
151301784826SJohnathan Mantey         }
151401784826SJohnathan Mantey     }
15154a0cb85cSEd Tanous }
15164a0cb85cSEd Tanous 
15174f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1518f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1519f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15208d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1521f85837bfSRAJESWARAN THILLAIGOVINDAN {
1522f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1523286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1524f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1525f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1526f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1527f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1528f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1529f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1530f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1531f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1532f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1533bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1534168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1535f85837bfSRAJESWARAN THILLAIGOVINDAN }
1536f85837bfSRAJESWARAN THILLAIGOVINDAN 
15374f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1538f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
153901784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15408d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1541e48c0fc5SRavi Teja {
154201784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1543e48c0fc5SRavi Teja     {
154471f52d96SEd Tanous         messages::propertyValueTypeError(
154571f52d96SEd Tanous             asyncResp->res,
1546bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1547e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1548e48c0fc5SRavi Teja         return;
1549e48c0fc5SRavi Teja     }
1550271584abSEd Tanous     size_t entryIdx = 1;
155185ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15522c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1553f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1554e48c0fc5SRavi Teja     {
1555e48c0fc5SRavi Teja         std::string pathString =
1556e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1557e48c0fc5SRavi Teja 
155801784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1559e48c0fc5SRavi Teja         {
1560e48c0fc5SRavi Teja             std::optional<std::string> address;
1561e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1562f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1563bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1564bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1565e48c0fc5SRavi Teja             {
156601784826SJohnathan Mantey                 messages::propertyValueFormatError(
156771f52d96SEd Tanous                     asyncResp->res,
156871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
156971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
157071f52d96SEd Tanous                     pathString);
1571e48c0fc5SRavi Teja                 return;
1572e48c0fc5SRavi Teja             }
1573e48c0fc5SRavi Teja 
1574543f4400SEd Tanous             const std::string* addr = nullptr;
1575543f4400SEd Tanous             uint8_t prefix = 0;
157601784826SJohnathan Mantey 
157701784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
157801784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
157901784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
158001784826SJohnathan Mantey             // current request.
1581e48c0fc5SRavi Teja             if (address)
1582e48c0fc5SRavi Teja             {
158301784826SJohnathan Mantey                 addr = &(*address);
1584e48c0fc5SRavi Teja             }
158585ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
158601784826SJohnathan Mantey             {
158785ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
158801784826SJohnathan Mantey             }
158901784826SJohnathan Mantey             else
159001784826SJohnathan Mantey             {
159101784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
159201784826SJohnathan Mantey                                           pathString + "/Address");
159301784826SJohnathan Mantey                 return;
1594e48c0fc5SRavi Teja             }
1595e48c0fc5SRavi Teja 
1596e48c0fc5SRavi Teja             if (prefixLength)
1597e48c0fc5SRavi Teja             {
159801784826SJohnathan Mantey                 prefix = *prefixLength;
159901784826SJohnathan Mantey             }
160085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1601e48c0fc5SRavi Teja             {
160285ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1603e48c0fc5SRavi Teja             }
1604e48c0fc5SRavi Teja             else
1605e48c0fc5SRavi Teja             {
1606e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1607e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
160801784826SJohnathan Mantey                 return;
1609e48c0fc5SRavi Teja             }
1610e48c0fc5SRavi Teja 
161185ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1612e48c0fc5SRavi Teja             {
161385ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1614e48c0fc5SRavi Teja                                     asyncResp);
161585ffe86aSJiaqing Zhao                 nicIpEntry =
161685ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
161701784826SJohnathan Mantey             }
161801784826SJohnathan Mantey             else
161901784826SJohnathan Mantey             {
162001784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1621e48c0fc5SRavi Teja             }
1622e48c0fc5SRavi Teja             entryIdx++;
1623e48c0fc5SRavi Teja         }
162401784826SJohnathan Mantey         else
162501784826SJohnathan Mantey         {
162685ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
162701784826SJohnathan Mantey             {
162801784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
162901784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
163001784826SJohnathan Mantey                 // in error, so bail out.
163101784826SJohnathan Mantey                 if (thisJson.is_null())
163201784826SJohnathan Mantey                 {
163301784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
163401784826SJohnathan Mantey                     return;
163501784826SJohnathan Mantey                 }
163601784826SJohnathan Mantey                 messages::propertyValueFormatError(
163771f52d96SEd Tanous                     asyncResp->res,
163871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
163971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
164071f52d96SEd Tanous                     pathString);
164101784826SJohnathan Mantey                 return;
164201784826SJohnathan Mantey             }
164301784826SJohnathan Mantey 
164401784826SJohnathan Mantey             if (thisJson.is_null())
164501784826SJohnathan Mantey             {
164685ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
164701784826SJohnathan Mantey             }
164885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
164901784826SJohnathan Mantey             {
165085ffe86aSJiaqing Zhao                 nicIpEntry =
165185ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
165201784826SJohnathan Mantey             }
165301784826SJohnathan Mantey             entryIdx++;
165401784826SJohnathan Mantey         }
165501784826SJohnathan Mantey     }
1656e48c0fc5SRavi Teja }
1657e48c0fc5SRavi Teja 
16584f48d5f6SEd Tanous inline void parseInterfaceData(
16598d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16608d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1661e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
166201784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16634a0cb85cSEd Tanous {
1664eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1665eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1666eeedda23SJohnathan Mantey 
16672c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
166881ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16692c70f800SEd Tanous     jsonResponse["@odata.id"] =
167081ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16712c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1672eeedda23SJohnathan Mantey 
1673eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1674eeedda23SJohnathan Mantey 
1675eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1676eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1677b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1678eeedda23SJohnathan Mantey         if (ec)
1679029573d4SEd Tanous         {
1680eeedda23SJohnathan Mantey             return;
1681eeedda23SJohnathan Mantey         }
1682eeedda23SJohnathan Mantey 
1683914e2d5dSEd Tanous         health->inventory = resp;
1684eeedda23SJohnathan Mantey         },
1685eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1686eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1687bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1688bf648f77SEd Tanous         inventoryForEthernet);
1689eeedda23SJohnathan Mantey 
1690eeedda23SJohnathan Mantey     health->populate();
1691eeedda23SJohnathan Mantey 
1692eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1693eeedda23SJohnathan Mantey     {
16942c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
16952c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1696029573d4SEd Tanous     }
1697029573d4SEd Tanous     else
1698029573d4SEd Tanous     {
16992c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17002c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1701029573d4SEd Tanous     }
1702aa05fb27SJohnathan Mantey 
17032c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17042c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
170535fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
170682695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
17072c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
170882695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
170982695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
171082695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
171182695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
17121f8c7b5dSJohnathan Mantey 
17132c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
171482695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
17151f8c7b5dSJohnathan Mantey                                                                : "Disabled";
171682695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
171782695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
171882695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
17192a133282Smanojkiraneda 
172082695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
17214a0cb85cSEd Tanous     {
172282695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1723ab6554f1SJoshi-Mansi 
1724ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1725ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1726ab6554f1SJoshi-Mansi         // FQDN
172782695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1728d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1729d24bfc7aSJennifer Lee         {
17302c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1731d24bfc7aSJennifer Lee         }
17322c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17334a0cb85cSEd Tanous     }
17344a0cb85cSEd Tanous 
17352c70f800SEd Tanous     jsonResponse["VLANs"] = {
1736bf648f77SEd Tanous         {"@odata.id",
1737bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1738fda13ad2SSunitha Harish 
17392c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17402c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17414a0cb85cSEd Tanous 
17422c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17432c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17442c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17452c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17469eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17474a0cb85cSEd Tanous     {
1748fa5053a6SGunnar Mills 
17492c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1750fa5053a6SGunnar Mills         if (gatewayStr.empty())
1751fa5053a6SGunnar Mills         {
1752fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1753fa5053a6SGunnar Mills         }
17541476687dSEd Tanous         nlohmann::json::object_t ipv4;
17551476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
17561476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
17571476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
17581476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1759fa5053a6SGunnar Mills 
17602c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1761d1d50814SRavi Teja         {
17621476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1763d1d50814SRavi Teja         }
17641476687dSEd Tanous 
17651476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
176601784826SJohnathan Mantey     }
1767d1d50814SRavi Teja 
176882695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
17697ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17707ea79e5eSRavi Teja     {
17717ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17727ea79e5eSRavi Teja     }
17737ea79e5eSRavi Teja 
17747ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1775e48c0fc5SRavi Teja 
17762c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17772c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17782c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17792c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17807f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17812c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17827f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17839eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1784e48c0fc5SRavi Teja     {
17851476687dSEd Tanous         nlohmann::json::object_t ipv6;
17861476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
17871476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
17881476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
17891476687dSEd Tanous         ipv6["AddressState"] = nullptr;
17901476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
17912c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1792e48c0fc5SRavi Teja         {
17931476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
17941476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
17951476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
17961476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
179701784826SJohnathan Mantey         }
1798e48c0fc5SRavi Teja     }
1799588c3f0dSKowalski, Kamil }
1800588c3f0dSKowalski, Kamil 
18014f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1802bf648f77SEd Tanous                                const std::string& parentIfaceId,
1803bf648f77SEd Tanous                                const std::string& ifaceId,
1804bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18051abe55efSEd Tanous {
1806bf648f77SEd Tanous     // Fill out obvious data...
1807bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1808bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1809bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1810bf648f77SEd Tanous 
1811bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
181217e22024SJiaqing Zhao     if (ethData.vlanId)
1813bf648f77SEd Tanous     {
181417e22024SJiaqing Zhao         jsonResponse["VLANId"] = *ethData.vlanId;
1815bf648f77SEd Tanous     }
1816bf648f77SEd Tanous }
1817bf648f77SEd Tanous 
18184f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1819bf648f77SEd Tanous {
1820*11ba3979SEd Tanous     return iface.starts_with(parent + "_");
1821bf648f77SEd Tanous }
1822bf648f77SEd Tanous 
1823bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1824bf648f77SEd Tanous {
1825bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1826ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
18271476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
18281476687dSEd Tanous             [&app](const crow::Request& req,
18291476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18303ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
183145ca1b86SEd Tanous         {
183245ca1b86SEd Tanous             return;
183345ca1b86SEd Tanous         }
183445ca1b86SEd Tanous 
1835bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1836bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1837bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1838bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1839bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1840bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1841bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1842bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1843bf648f77SEd Tanous 
1844bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1845bf648f77SEd Tanous         // preparation
1846002d39b4SEd Tanous         getEthernetIfaceList(
1847002d39b4SEd Tanous             [asyncResp](
18481476687dSEd Tanous                 const bool& success,
1849002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1850bf648f77SEd Tanous             if (!success)
18511abe55efSEd Tanous             {
1852f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18539391bb9cSRapkiewicz, Pawel                 return;
18549391bb9cSRapkiewicz, Pawel             }
18559391bb9cSRapkiewicz, Pawel 
1856002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1857bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1858bf648f77SEd Tanous             std::string tag = "_";
1859bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1860bf648f77SEd Tanous             {
1861bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1862bf648f77SEd Tanous                 if (found == std::string::npos)
1863bf648f77SEd Tanous                 {
18641476687dSEd Tanous                     nlohmann::json::object_t iface;
18651476687dSEd Tanous                     iface["@odata.id"] =
1866bf648f77SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
18671476687dSEd Tanous                         ifaceItem;
18681476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
1869bf648f77SEd Tanous                 }
1870bf648f77SEd Tanous             }
1871bf648f77SEd Tanous 
1872002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1873bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1874bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1875bf648f77SEd Tanous         });
1876bf648f77SEd Tanous         });
1877bf648f77SEd Tanous 
1878bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1879ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1880bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
188145ca1b86SEd Tanous             [&app](const crow::Request& req,
1882bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1883bf648f77SEd Tanous                    const std::string& ifaceId) {
18843ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
188545ca1b86SEd Tanous         {
188645ca1b86SEd Tanous             return;
188745ca1b86SEd Tanous         }
18884a0cb85cSEd Tanous         getEthernetIfaceData(
1889bf648f77SEd Tanous             ifaceId,
1890002d39b4SEd Tanous             [asyncResp, ifaceId](
1891002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1892002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1893002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18944a0cb85cSEd Tanous             if (!success)
18951abe55efSEd Tanous             {
1896bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1897bf648f77SEd Tanous                 // existing object, and other errors
1898002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1899002d39b4SEd Tanous                                            ifaceId);
19004a0cb85cSEd Tanous                 return;
19019391bb9cSRapkiewicz, Pawel             }
19024c9afe43SEd Tanous 
19030f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1904fda13ad2SSunitha Harish                 "#EthernetInterface.v1_4_1.EthernetInterface";
1905002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19060f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
19070f74e643SEd Tanous                 "Management Network Interface";
19080f74e643SEd Tanous 
1909002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
19109391bb9cSRapkiewicz, Pawel             });
1911bf648f77SEd Tanous         });
19129391bb9cSRapkiewicz, Pawel 
1913bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1914ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1915bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
191645ca1b86SEd Tanous             [&app](const crow::Request& req,
1917bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1918bf648f77SEd Tanous                    const std::string& ifaceId) {
19193ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
192045ca1b86SEd Tanous         {
192145ca1b86SEd Tanous             return;
192245ca1b86SEd Tanous         }
1923bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1924ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1925d577665bSRatan Gupta         std::optional<std::string> macAddress;
19269a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1927d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1928e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1929f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1930da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19311f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1932eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
193335fb5311STejas Patil         std::optional<size_t> mtuSize;
19341f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19351f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19360627a2c7SEd Tanous 
193715ed6780SWilly Tu         if (!json_util::readJsonPatch(
19388d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1939002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1940002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1941002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1942ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1943002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
19441abe55efSEd Tanous         {
1945588c3f0dSKowalski, Kamil             return;
1946588c3f0dSKowalski, Kamil         }
1947da131a9aSJennifer Lee         if (dhcpv4)
1948da131a9aSJennifer Lee         {
1949002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
19501f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
195182695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
195282695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
195382695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
19541f8c7b5dSJohnathan Mantey             {
19551f8c7b5dSJohnathan Mantey                 return;
19561f8c7b5dSJohnathan Mantey             }
19571f8c7b5dSJohnathan Mantey         }
19581f8c7b5dSJohnathan Mantey 
19591f8c7b5dSJohnathan Mantey         if (dhcpv6)
19601f8c7b5dSJohnathan Mantey         {
1961002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1962002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1963002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1964002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1965002d39b4SEd Tanous                                      "UseDomainName",
196682695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
19671f8c7b5dSJohnathan Mantey             {
19681f8c7b5dSJohnathan Mantey                 return;
19691f8c7b5dSJohnathan Mantey             }
1970da131a9aSJennifer Lee         }
1971da131a9aSJennifer Lee 
1972bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1973bf648f77SEd Tanous         // for JSON preparation
19744a0cb85cSEd Tanous         getEthernetIfaceData(
19752c70f800SEd Tanous             ifaceId,
1976bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1977ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1978d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19799a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1980e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19811f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1982bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1983bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1984f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1985002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1986002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1987002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19881abe55efSEd Tanous             if (!success)
19891abe55efSEd Tanous             {
1990588c3f0dSKowalski, Kamil                 // ... otherwise return error
1991bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1992bf648f77SEd Tanous                 // existing object, and other errors
1993002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Ethernet Interface",
1994002d39b4SEd Tanous                                            ifaceId);
1995588c3f0dSKowalski, Kamil                 return;
1996588c3f0dSKowalski, Kamil             }
1997588c3f0dSKowalski, Kamil 
19981f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
19991f8c7b5dSJohnathan Mantey             {
2000002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2001002d39b4SEd Tanous                                 asyncResp);
20021f8c7b5dSJohnathan Mantey             }
20031f8c7b5dSJohnathan Mantey 
20040627a2c7SEd Tanous             if (hostname)
20051abe55efSEd Tanous             {
20060627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
20071abe55efSEd Tanous             }
20080627a2c7SEd Tanous 
2009ab6554f1SJoshi-Mansi             if (fqdn)
2010ab6554f1SJoshi-Mansi             {
20112c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2012ab6554f1SJoshi-Mansi             }
2013ab6554f1SJoshi-Mansi 
2014d577665bSRatan Gupta             if (macAddress)
2015d577665bSRatan Gupta             {
2016002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2017d577665bSRatan Gupta             }
2018d577665bSRatan Gupta 
2019d1d50814SRavi Teja             if (ipv4StaticAddresses)
2020d1d50814SRavi Teja             {
2021bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
2022bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
2023bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
2024bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
2025bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
2026bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
2027bf648f77SEd Tanous                 // that, but could be done more efficiently
2028f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2029002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
20301abe55efSEd Tanous             }
20310627a2c7SEd Tanous 
2032f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
2033f85837bfSRAJESWARAN THILLAIGOVINDAN             {
2034002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2035002d39b4SEd Tanous                                              asyncResp);
2036f85837bfSRAJESWARAN THILLAIGOVINDAN             }
20379a6fc6feSRavi Teja 
20389a6fc6feSRavi Teja             if (ipv6DefaultGateway)
20399a6fc6feSRavi Teja             {
20409a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
20419a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
20429a6fc6feSRavi Teja             }
2043e48c0fc5SRavi Teja 
2044e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
2045e48c0fc5SRavi Teja             {
2046002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
2047002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
2048002d39b4SEd Tanous                                                asyncResp);
2049e48c0fc5SRavi Teja             }
2050eeedda23SJohnathan Mantey 
2051eeedda23SJohnathan Mantey             if (interfaceEnabled)
2052eeedda23SJohnathan Mantey             {
2053002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2054002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
2055eeedda23SJohnathan Mantey             }
205635fb5311STejas Patil 
205735fb5311STejas Patil             if (mtuSize)
205835fb5311STejas Patil             {
205935fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
206035fb5311STejas Patil             }
2061588c3f0dSKowalski, Kamil             });
2062bf648f77SEd Tanous         });
20639391bb9cSRapkiewicz, Pawel 
2064bf648f77SEd Tanous     BMCWEB_ROUTE(
2065bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2066ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2067bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
206845ca1b86SEd Tanous             [&app](const crow::Request& req,
2069bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
207045ca1b86SEd Tanous                    const std::string& parentIfaceId,
207145ca1b86SEd Tanous                    const std::string& ifaceId) {
20723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207345ca1b86SEd Tanous         {
207445ca1b86SEd Tanous             return;
207545ca1b86SEd Tanous         }
20768d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
20770f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20788d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2079e439f0f8SKowalski, Kamil 
20802c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
20811abe55efSEd Tanous         {
2082a434f2bdSEd Tanous             return;
2083a434f2bdSEd Tanous         }
2084a434f2bdSEd Tanous 
2085bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2086bf648f77SEd Tanous         // for JSON preparation
20874a0cb85cSEd Tanous         getEthernetIfaceData(
2088bf648f77SEd Tanous             ifaceId,
2089002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2090002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2091cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2092cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
209317e22024SJiaqing Zhao             if (success && ethData.vlanId)
20941abe55efSEd Tanous             {
2095002d39b4SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2096002d39b4SEd Tanous                                    ifaceId, ethData);
20971abe55efSEd Tanous             }
20981abe55efSEd Tanous             else
20991abe55efSEd Tanous             {
2100e439f0f8SKowalski, Kamil                 // ... otherwise return error
2101bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2102bf648f77SEd Tanous                 // existing object, and other errors
2103bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2104002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2105e439f0f8SKowalski, Kamil             }
2106e439f0f8SKowalski, Kamil             });
2107bf648f77SEd Tanous         });
2108e439f0f8SKowalski, Kamil 
2109bf648f77SEd Tanous     BMCWEB_ROUTE(
2110bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21113d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2112bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
211345ca1b86SEd Tanous             [&app](const crow::Request& req,
2114bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
211545ca1b86SEd Tanous                    const std::string& parentIfaceId,
211645ca1b86SEd Tanous                    const std::string& ifaceId) {
21173ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
211845ca1b86SEd Tanous         {
211945ca1b86SEd Tanous             return;
212045ca1b86SEd Tanous         }
2121fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21221abe55efSEd Tanous         {
2123002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2124002d39b4SEd Tanous                                        ifaceId);
2125927a505aSKowalski, Kamil             return;
2126927a505aSKowalski, Kamil         }
2127927a505aSKowalski, Kamil 
21283927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
21293927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
21300627a2c7SEd Tanous 
213115ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2132bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
21331abe55efSEd Tanous         {
2134927a505aSKowalski, Kamil             return;
2135927a505aSKowalski, Kamil         }
2136927a505aSKowalski, Kamil 
21373927e13eSJiaqing Zhao         if (vlanId)
21383927e13eSJiaqing Zhao         {
21393927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
21403927e13eSJiaqing Zhao             return;
21413927e13eSJiaqing Zhao         }
21423927e13eSJiaqing Zhao 
2143bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2144bf648f77SEd Tanous         // for JSON preparation
2145e48c0fc5SRavi Teja         getEthernetIfaceData(
2146bf648f77SEd Tanous             ifaceId,
21473927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2148002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2149cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2150cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
215117e22024SJiaqing Zhao             if (success && ethData.vlanId)
215208244d02SSunitha Harish             {
215308244d02SSunitha Harish                 auto callback =
2154002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
215508244d02SSunitha Harish                     if (ec)
215608244d02SSunitha Harish                     {
215708244d02SSunitha Harish                         messages::internalError(asyncResp->res);
21583927e13eSJiaqing Zhao                         return;
215908244d02SSunitha Harish                     }
216008244d02SSunitha Harish                 };
216108244d02SSunitha Harish 
21623927e13eSJiaqing Zhao                 if (vlanEnable && !(*vlanEnable))
216308244d02SSunitha Harish                 {
2164002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2165e48c0fc5SRavi Teja                                         "vlan interface";
216608244d02SSunitha Harish                     crow::connections::systemBus->async_method_call(
2167002d39b4SEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
2168002d39b4SEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
2169002d39b4SEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
217008244d02SSunitha Harish                 }
217108244d02SSunitha Harish             }
217208244d02SSunitha Harish             else
21731abe55efSEd Tanous             {
2174bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2175bf648f77SEd Tanous                 // existing object, and other errors
2176bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2177002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2178bf648f77SEd Tanous                 return;
2179bf648f77SEd Tanous             }
2180bf648f77SEd Tanous             });
2181bf648f77SEd Tanous         });
2182bf648f77SEd Tanous 
2183bf648f77SEd Tanous     BMCWEB_ROUTE(
2184bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21853d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2186bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
218745ca1b86SEd Tanous             [&app](const crow::Request& req,
2188bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218945ca1b86SEd Tanous                    const std::string& parentIfaceId,
219045ca1b86SEd Tanous                    const std::string& ifaceId) {
21913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219245ca1b86SEd Tanous         {
219345ca1b86SEd Tanous             return;
219445ca1b86SEd Tanous         }
2195bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2196bf648f77SEd Tanous         {
2197002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2198002d39b4SEd Tanous                                        ifaceId);
2199927a505aSKowalski, Kamil             return;
2200927a505aSKowalski, Kamil         }
2201e439f0f8SKowalski, Kamil 
2202bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2203bf648f77SEd Tanous         // for JSON preparation
2204f12894f8SJason M. Bills         getEthernetIfaceData(
2205bf648f77SEd Tanous             ifaceId,
2206002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2207002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2208cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2209cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
221017e22024SJiaqing Zhao             if (success && ethData.vlanId)
22111abe55efSEd Tanous             {
2212f12894f8SJason M. Bills                 auto callback =
2213002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
22141abe55efSEd Tanous                     if (ec)
22151abe55efSEd Tanous                     {
2216f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2217927a505aSKowalski, Kamil                     }
22184a0cb85cSEd Tanous                 };
22194a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2220002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2221002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
22224a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
22231abe55efSEd Tanous             }
22241abe55efSEd Tanous             else
22251abe55efSEd Tanous             {
2226927a505aSKowalski, Kamil                 // ... otherwise return error
2227bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2228bf648f77SEd Tanous                 // existing object, and other errors
2229bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2230002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2231927a505aSKowalski, Kamil             }
2232927a505aSKowalski, Kamil             });
2233bf648f77SEd Tanous         });
2234e439f0f8SKowalski, Kamil 
2235bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2236bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2237ed398213SEd Tanous 
2238ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
22391476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
22401476687dSEd Tanous             [&app](const crow::Request& req,
2241bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2242bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
22433ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
224445ca1b86SEd Tanous         {
224545ca1b86SEd Tanous             return;
224645ca1b86SEd Tanous         }
22474a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
22481abe55efSEd Tanous         // preparation
2249002d39b4SEd Tanous         getEthernetIfaceList(
2250002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
22511abe55efSEd Tanous                 const bool& success,
2252002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
22534a0cb85cSEd Tanous             if (!success)
22541abe55efSEd Tanous             {
2255f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22564a0cb85cSEd Tanous                 return;
22571abe55efSEd Tanous             }
22584c9afe43SEd Tanous 
225981ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22604c9afe43SEd Tanous             {
2261002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2262002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
22634c9afe43SEd Tanous                                            rootInterfaceName);
22644c9afe43SEd Tanous                 return;
22654c9afe43SEd Tanous             }
22664c9afe43SEd Tanous 
22670f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
22680f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
22690f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
22700f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
22710f74e643SEd Tanous                 "VLAN Network Interface Collection";
22724a0cb85cSEd Tanous 
22732c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
22744a0cb85cSEd Tanous 
227581ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
22761abe55efSEd Tanous             {
2277*11ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
22784a0cb85cSEd Tanous                 {
2279f23b7296SEd Tanous                     std::string path =
2280f23b7296SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2281f23b7296SEd Tanous                     path += rootInterfaceName;
2282f23b7296SEd Tanous                     path += "/VLANs/";
2283f23b7296SEd Tanous                     path += ifaceItem;
22841476687dSEd Tanous                     nlohmann::json::object_t iface;
22851476687dSEd Tanous                     iface["@odata.id"] = std::move(path);
22861476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2287e439f0f8SKowalski, Kamil                 }
2288e439f0f8SKowalski, Kamil             }
2289e439f0f8SKowalski, Kamil 
2290002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
22912c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
22924a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22934a0cb85cSEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
22944a0cb85cSEd Tanous                 rootInterfaceName + "/VLANs";
2295e439f0f8SKowalski, Kamil         });
2296bf648f77SEd Tanous         });
2297e439f0f8SKowalski, Kamil 
2298bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2299bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
23003d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2301bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
230245ca1b86SEd Tanous             [&app](const crow::Request& req,
2303bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2304bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
23053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
230645ca1b86SEd Tanous         {
230745ca1b86SEd Tanous             return;
230845ca1b86SEd Tanous         }
2309fda13ad2SSunitha Harish         bool vlanEnable = false;
23100627a2c7SEd Tanous         uint32_t vlanId = 0;
2311002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2312002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
23131abe55efSEd Tanous         {
23144a0cb85cSEd Tanous             return;
2315e439f0f8SKowalski, Kamil         }
2316fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2317dbb59d4dSEd Tanous         if (vlanId == 0U)
2318fda13ad2SSunitha Harish         {
2319fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2320fda13ad2SSunitha Harish         }
2321fda13ad2SSunitha Harish         if (!vlanEnable)
2322fda13ad2SSunitha Harish         {
2323fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2324fda13ad2SSunitha Harish         }
2325271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2326fda13ad2SSunitha Harish         {
2327fda13ad2SSunitha Harish             return;
2328fda13ad2SSunitha Harish         }
2329fda13ad2SSunitha Harish 
2330002d39b4SEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
23311abe55efSEd Tanous             if (ec)
23321abe55efSEd Tanous             {
2333bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2334bf648f77SEd Tanous                 // based on phosphor-network responses
2335f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
23364a0cb85cSEd Tanous                 return;
23371abe55efSEd Tanous             }
2338f12894f8SJason M. Bills             messages::created(asyncResp->res);
2339e439f0f8SKowalski, Kamil         };
23404a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
23414a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
23424a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
23434a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23440627a2c7SEd Tanous             rootInterfaceName, vlanId);
2345bf648f77SEd Tanous         });
23464a0cb85cSEd Tanous }
2347bf648f77SEd Tanous 
23489391bb9cSRapkiewicz, Pawel } // namespace redfish
2349