xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 58283f41276f86666aca6943e7405a20a1ff38b6)
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>
1911ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
2011ba3979SEd 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
43511ba3979SEd 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
51311ba3979SEd 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 =
57411ba3979SEd 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 {
1126*58283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
1127*58283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
1128*58283f41SJohnathan Mantey 
1129d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1130*58283f41SJohnathan Mantey         [asyncResp, macAddress](const boost::system::error_code ec,
1131*58283f41SJohnathan Mantey                                 const sdbusplus::message::message& msg) {
1132d577665bSRatan Gupta         if (ec)
1133d577665bSRatan Gupta         {
1134*58283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
1135*58283f41SJohnathan Mantey             if (err == nullptr)
1136*58283f41SJohnathan Mantey             {
1137*58283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
1138*58283f41SJohnathan Mantey                 return;
1139*58283f41SJohnathan Mantey             }
1140*58283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
1141*58283f41SJohnathan Mantey             {
1142*58283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
1143*58283f41SJohnathan Mantey                 return;
1144*58283f41SJohnathan Mantey             }
1145d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1146d577665bSRatan Gupta             return;
1147d577665bSRatan Gupta         }
1148d577665bSRatan Gupta         },
1149d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1150d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1151d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1152d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1153168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1154d577665bSRatan Gupta }
1155286b9118SJohnathan Mantey 
11564f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11574f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11584f48d5f6SEd Tanous                            const bool v6Value,
11598d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1160da131a9aSJennifer Lee {
11612c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1162da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1163da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1164da131a9aSJennifer Lee         if (ec)
1165da131a9aSJennifer Lee         {
1166da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1167da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1168da131a9aSJennifer Lee             return;
1169da131a9aSJennifer Lee         }
11708f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1171da131a9aSJennifer Lee         },
1172da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1173da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1174da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1175da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1176168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1177da131a9aSJennifer Lee }
11781f8c7b5dSJohnathan Mantey 
11794f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1180eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11818d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1182eeedda23SJohnathan Mantey {
1183eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1184eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1185eeedda23SJohnathan Mantey         if (ec)
1186eeedda23SJohnathan Mantey         {
1187eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1188eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1189eeedda23SJohnathan Mantey             return;
1190eeedda23SJohnathan Mantey         }
1191eeedda23SJohnathan Mantey         },
1192eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1193eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1194eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1195eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1196168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1197eeedda23SJohnathan Mantey }
1198eeedda23SJohnathan Mantey 
11994f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
12008d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1201da131a9aSJennifer Lee {
1202da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1203da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1204da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1205da131a9aSJennifer Lee         if (ec)
1206da131a9aSJennifer Lee         {
1207da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1208da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1209da131a9aSJennifer Lee             return;
1210da131a9aSJennifer Lee         }
1211da131a9aSJennifer Lee         },
1212da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1213da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1214da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1215da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1216168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1217da131a9aSJennifer Lee }
1218d577665bSRatan Gupta 
12194f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12201f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1221f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1222f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12238d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1224da131a9aSJennifer Lee {
122582695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
122682695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1227da131a9aSJennifer Lee 
12281f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12291f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12301f8c7b5dSJohnathan Mantey 
12311f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12321f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1233da131a9aSJennifer Lee     {
12341f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12351f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12361f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12371f8c7b5dSJohnathan Mantey         {
1238bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1239bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12401f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1241da131a9aSJennifer Lee             return;
1242da131a9aSJennifer Lee         }
12431f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12441f8c7b5dSJohnathan Mantey     }
12451f8c7b5dSJohnathan Mantey     else
1246da131a9aSJennifer Lee     {
12471f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12481f8c7b5dSJohnathan Mantey     }
12491f8c7b5dSJohnathan Mantey 
12501f8c7b5dSJohnathan Mantey     bool nextDNS{};
125182695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
12521f8c7b5dSJohnathan Mantey     {
125382695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
12541f8c7b5dSJohnathan Mantey         {
12551f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12561f8c7b5dSJohnathan Mantey             return;
12571f8c7b5dSJohnathan Mantey         }
125882695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12591f8c7b5dSJohnathan Mantey     }
126082695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
12611f8c7b5dSJohnathan Mantey     {
126282695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12631f8c7b5dSJohnathan Mantey     }
126482695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
12651f8c7b5dSJohnathan Mantey     {
126682695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
12671f8c7b5dSJohnathan Mantey     }
12681f8c7b5dSJohnathan Mantey     else
12691f8c7b5dSJohnathan Mantey     {
127082695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
12711f8c7b5dSJohnathan Mantey     }
12721f8c7b5dSJohnathan Mantey 
12731f8c7b5dSJohnathan Mantey     bool nextNTP{};
127482695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
12751f8c7b5dSJohnathan Mantey     {
127682695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
12771f8c7b5dSJohnathan Mantey         {
12781f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12791f8c7b5dSJohnathan Mantey             return;
12801f8c7b5dSJohnathan Mantey         }
128182695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12821f8c7b5dSJohnathan Mantey     }
128382695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
12841f8c7b5dSJohnathan Mantey     {
128582695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12861f8c7b5dSJohnathan Mantey     }
128782695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12881f8c7b5dSJohnathan Mantey     {
128982695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12901f8c7b5dSJohnathan Mantey     }
12911f8c7b5dSJohnathan Mantey     else
12921f8c7b5dSJohnathan Mantey     {
129382695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12941f8c7b5dSJohnathan Mantey     }
12951f8c7b5dSJohnathan Mantey 
12961f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
129782695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12981f8c7b5dSJohnathan Mantey     {
129982695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
13001f8c7b5dSJohnathan Mantey         {
13011f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13021f8c7b5dSJohnathan Mantey             return;
13031f8c7b5dSJohnathan Mantey         }
130482695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
13051f8c7b5dSJohnathan Mantey     }
130682695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
13071f8c7b5dSJohnathan Mantey     {
130882695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
13091f8c7b5dSJohnathan Mantey     }
131082695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
13111f8c7b5dSJohnathan Mantey     {
131282695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
13131f8c7b5dSJohnathan Mantey     }
13141f8c7b5dSJohnathan Mantey     else
13151f8c7b5dSJohnathan Mantey     {
131682695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
13171f8c7b5dSJohnathan Mantey     }
13181f8c7b5dSJohnathan Mantey 
1319da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13201f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13211f8c7b5dSJohnathan Mantey                    asyncResp);
1322da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13231f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1324da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13251f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13261f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13271f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1328da131a9aSJennifer Lee }
132901784826SJohnathan Mantey 
13304f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13312c70f800SEd Tanous     getNextStaticIpEntry(
1332bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1333bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
133401784826SJohnathan Mantey {
133517a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
133617a897dfSManojkiran Eda         return value.origin == "Static";
133717a897dfSManojkiran Eda     });
133801784826SJohnathan Mantey }
133901784826SJohnathan Mantey 
13404f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13412c70f800SEd Tanous     getNextStaticIpEntry(
1342bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1343bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
134401784826SJohnathan Mantey {
134517a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
134617a897dfSManojkiran Eda         return value.origin == "Static";
134717a897dfSManojkiran Eda     });
134801784826SJohnathan Mantey }
134901784826SJohnathan Mantey 
13504f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1351f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
135201784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13538d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13541abe55efSEd Tanous {
135501784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1356f476acbfSRatan Gupta     {
135771f52d96SEd Tanous         messages::propertyValueTypeError(
135871f52d96SEd Tanous             asyncResp->res,
1359bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1360d1d50814SRavi Teja             "IPv4StaticAddresses");
1361f476acbfSRatan Gupta         return;
1362f476acbfSRatan Gupta     }
1363f476acbfSRatan Gupta 
1364271584abSEd Tanous     unsigned entryIdx = 1;
136501784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
136601784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
136701784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
136801784826SJohnathan Mantey     // into the NIC.
136985ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13702c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
137101784826SJohnathan Mantey 
1372537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13731abe55efSEd Tanous     {
13744a0cb85cSEd Tanous         std::string pathString =
1375d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1376179db1d7SKowalski, Kamil 
137701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1378f476acbfSRatan Gupta         {
1379537174c4SEd Tanous             std::optional<std::string> address;
1380537174c4SEd Tanous             std::optional<std::string> subnetMask;
1381537174c4SEd Tanous             std::optional<std::string> gateway;
1382537174c4SEd Tanous 
1383537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13847e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13857e27d832SJohnathan Mantey                                      "Gateway", gateway))
1386537174c4SEd Tanous             {
138701784826SJohnathan Mantey                 messages::propertyValueFormatError(
138871f52d96SEd Tanous                     asyncResp->res,
138971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
139071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
139171f52d96SEd Tanous                     pathString);
1392537174c4SEd Tanous                 return;
1393179db1d7SKowalski, Kamil             }
1394179db1d7SKowalski, Kamil 
139501784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
139601784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
139701784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
139801784826SJohnathan Mantey             // current request.
1399271584abSEd Tanous             const std::string* addr = nullptr;
1400271584abSEd Tanous             const std::string* gw = nullptr;
140101784826SJohnathan Mantey             uint8_t prefixLength = 0;
140201784826SJohnathan Mantey             bool errorInEntry = false;
1403537174c4SEd Tanous             if (address)
14041abe55efSEd Tanous             {
140501784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14061abe55efSEd Tanous                 {
140701784826SJohnathan Mantey                     addr = &(*address);
14084a0cb85cSEd Tanous                 }
140901784826SJohnathan Mantey                 else
141001784826SJohnathan Mantey                 {
1411bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1412bf648f77SEd Tanous                                                        pathString + "/Address");
141301784826SJohnathan Mantey                     errorInEntry = true;
141401784826SJohnathan Mantey                 }
141501784826SJohnathan Mantey             }
141685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
141701784826SJohnathan Mantey             {
141885ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
141901784826SJohnathan Mantey             }
142001784826SJohnathan Mantey             else
142101784826SJohnathan Mantey             {
142201784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
142301784826SJohnathan Mantey                                           pathString + "/Address");
142401784826SJohnathan Mantey                 errorInEntry = true;
14254a0cb85cSEd Tanous             }
14264a0cb85cSEd Tanous 
1427537174c4SEd Tanous             if (subnetMask)
14284a0cb85cSEd Tanous             {
1429537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14304a0cb85cSEd Tanous                 {
1431f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1432537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14334a0cb85cSEd Tanous                         pathString + "/SubnetMask");
143401784826SJohnathan Mantey                     errorInEntry = true;
14354a0cb85cSEd Tanous                 }
14364a0cb85cSEd Tanous             }
143785ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14384a0cb85cSEd Tanous             {
143985ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
144001784826SJohnathan Mantey                                                 &prefixLength))
14414a0cb85cSEd Tanous                 {
144201784826SJohnathan Mantey                     messages::propertyValueFormatError(
144385ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
144401784826SJohnathan Mantey                         pathString + "/SubnetMask");
144501784826SJohnathan Mantey                     errorInEntry = true;
14464a0cb85cSEd Tanous                 }
14474a0cb85cSEd Tanous             }
14481abe55efSEd Tanous             else
14491abe55efSEd Tanous             {
145001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
145101784826SJohnathan Mantey                                           pathString + "/SubnetMask");
145201784826SJohnathan Mantey                 errorInEntry = true;
145301784826SJohnathan Mantey             }
145401784826SJohnathan Mantey 
145501784826SJohnathan Mantey             if (gateway)
145601784826SJohnathan Mantey             {
145701784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
145801784826SJohnathan Mantey                 {
145901784826SJohnathan Mantey                     gw = &(*gateway);
146001784826SJohnathan Mantey                 }
146101784826SJohnathan Mantey                 else
146201784826SJohnathan Mantey                 {
1463bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1464bf648f77SEd Tanous                                                        pathString + "/Gateway");
146501784826SJohnathan Mantey                     errorInEntry = true;
146601784826SJohnathan Mantey                 }
146701784826SJohnathan Mantey             }
146885ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
146901784826SJohnathan Mantey             {
147085ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
147101784826SJohnathan Mantey             }
147201784826SJohnathan Mantey             else
14731abe55efSEd Tanous             {
1474a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14754a0cb85cSEd Tanous                                           pathString + "/Gateway");
147601784826SJohnathan Mantey                 errorInEntry = true;
14774a0cb85cSEd Tanous             }
14784a0cb85cSEd Tanous 
147901784826SJohnathan Mantey             if (errorInEntry)
14801abe55efSEd Tanous             {
148101784826SJohnathan Mantey                 return;
14824a0cb85cSEd Tanous             }
14834a0cb85cSEd Tanous 
148485ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14851abe55efSEd Tanous             {
148685ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1487bf648f77SEd Tanous                                     *addr, asyncResp);
148885ffe86aSJiaqing Zhao                 nicIpEntry =
148985ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1490588c3f0dSKowalski, Kamil             }
149101784826SJohnathan Mantey             else
149201784826SJohnathan Mantey             {
1493cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1494cb13a392SEd Tanous                            asyncResp);
14954a0cb85cSEd Tanous             }
14964a0cb85cSEd Tanous             entryIdx++;
14974a0cb85cSEd Tanous         }
149801784826SJohnathan Mantey         else
149901784826SJohnathan Mantey         {
150085ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
150101784826SJohnathan Mantey             {
150201784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
150301784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
150401784826SJohnathan Mantey                 // in error, so bail out.
150501784826SJohnathan Mantey                 if (thisJson.is_null())
150601784826SJohnathan Mantey                 {
150701784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
150801784826SJohnathan Mantey                     return;
150901784826SJohnathan Mantey                 }
151001784826SJohnathan Mantey                 messages::propertyValueFormatError(
151171f52d96SEd Tanous                     asyncResp->res,
151271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
151371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
151471f52d96SEd Tanous                     pathString);
151501784826SJohnathan Mantey                 return;
151601784826SJohnathan Mantey             }
151701784826SJohnathan Mantey 
151801784826SJohnathan Mantey             if (thisJson.is_null())
151901784826SJohnathan Mantey             {
152085ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
152101784826SJohnathan Mantey             }
152285ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
152301784826SJohnathan Mantey             {
152485ffe86aSJiaqing Zhao                 nicIpEntry =
152585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
152601784826SJohnathan Mantey             }
152701784826SJohnathan Mantey             entryIdx++;
152801784826SJohnathan Mantey         }
152901784826SJohnathan Mantey     }
15304a0cb85cSEd Tanous }
15314a0cb85cSEd Tanous 
15324f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1533f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1534f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15358d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1536f85837bfSRAJESWARAN THILLAIGOVINDAN {
1537f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1538286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1539f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1540f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1541f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1542f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1543f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1544f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1545f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1546f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1547f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1548bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1549168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1550f85837bfSRAJESWARAN THILLAIGOVINDAN }
1551f85837bfSRAJESWARAN THILLAIGOVINDAN 
15524f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1553f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
155401784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15558d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1556e48c0fc5SRavi Teja {
155701784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1558e48c0fc5SRavi Teja     {
155971f52d96SEd Tanous         messages::propertyValueTypeError(
156071f52d96SEd Tanous             asyncResp->res,
1561bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1562e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1563e48c0fc5SRavi Teja         return;
1564e48c0fc5SRavi Teja     }
1565271584abSEd Tanous     size_t entryIdx = 1;
156685ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15672c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1568f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1569e48c0fc5SRavi Teja     {
1570e48c0fc5SRavi Teja         std::string pathString =
1571e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1572e48c0fc5SRavi Teja 
157301784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1574e48c0fc5SRavi Teja         {
1575e48c0fc5SRavi Teja             std::optional<std::string> address;
1576e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1577f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1578bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1579bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1580e48c0fc5SRavi Teja             {
158101784826SJohnathan Mantey                 messages::propertyValueFormatError(
158271f52d96SEd Tanous                     asyncResp->res,
158371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
158471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
158571f52d96SEd Tanous                     pathString);
1586e48c0fc5SRavi Teja                 return;
1587e48c0fc5SRavi Teja             }
1588e48c0fc5SRavi Teja 
1589543f4400SEd Tanous             const std::string* addr = nullptr;
1590543f4400SEd Tanous             uint8_t prefix = 0;
159101784826SJohnathan Mantey 
159201784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
159301784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
159401784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
159501784826SJohnathan Mantey             // current request.
1596e48c0fc5SRavi Teja             if (address)
1597e48c0fc5SRavi Teja             {
159801784826SJohnathan Mantey                 addr = &(*address);
1599e48c0fc5SRavi Teja             }
160085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
160101784826SJohnathan Mantey             {
160285ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
160301784826SJohnathan Mantey             }
160401784826SJohnathan Mantey             else
160501784826SJohnathan Mantey             {
160601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
160701784826SJohnathan Mantey                                           pathString + "/Address");
160801784826SJohnathan Mantey                 return;
1609e48c0fc5SRavi Teja             }
1610e48c0fc5SRavi Teja 
1611e48c0fc5SRavi Teja             if (prefixLength)
1612e48c0fc5SRavi Teja             {
161301784826SJohnathan Mantey                 prefix = *prefixLength;
161401784826SJohnathan Mantey             }
161585ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1616e48c0fc5SRavi Teja             {
161785ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1618e48c0fc5SRavi Teja             }
1619e48c0fc5SRavi Teja             else
1620e48c0fc5SRavi Teja             {
1621e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1622e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
162301784826SJohnathan Mantey                 return;
1624e48c0fc5SRavi Teja             }
1625e48c0fc5SRavi Teja 
162685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1627e48c0fc5SRavi Teja             {
162885ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1629e48c0fc5SRavi Teja                                     asyncResp);
163085ffe86aSJiaqing Zhao                 nicIpEntry =
163185ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
163201784826SJohnathan Mantey             }
163301784826SJohnathan Mantey             else
163401784826SJohnathan Mantey             {
163501784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1636e48c0fc5SRavi Teja             }
1637e48c0fc5SRavi Teja             entryIdx++;
1638e48c0fc5SRavi Teja         }
163901784826SJohnathan Mantey         else
164001784826SJohnathan Mantey         {
164185ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
164201784826SJohnathan Mantey             {
164301784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
164401784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
164501784826SJohnathan Mantey                 // in error, so bail out.
164601784826SJohnathan Mantey                 if (thisJson.is_null())
164701784826SJohnathan Mantey                 {
164801784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
164901784826SJohnathan Mantey                     return;
165001784826SJohnathan Mantey                 }
165101784826SJohnathan Mantey                 messages::propertyValueFormatError(
165271f52d96SEd Tanous                     asyncResp->res,
165371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
165471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
165571f52d96SEd Tanous                     pathString);
165601784826SJohnathan Mantey                 return;
165701784826SJohnathan Mantey             }
165801784826SJohnathan Mantey 
165901784826SJohnathan Mantey             if (thisJson.is_null())
166001784826SJohnathan Mantey             {
166185ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
166201784826SJohnathan Mantey             }
166385ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
166401784826SJohnathan Mantey             {
166585ffe86aSJiaqing Zhao                 nicIpEntry =
166685ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
166701784826SJohnathan Mantey             }
166801784826SJohnathan Mantey             entryIdx++;
166901784826SJohnathan Mantey         }
167001784826SJohnathan Mantey     }
1671e48c0fc5SRavi Teja }
1672e48c0fc5SRavi Teja 
16734f48d5f6SEd Tanous inline void parseInterfaceData(
16748d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16758d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1676e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
167701784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16784a0cb85cSEd Tanous {
1679eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1680eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1681eeedda23SJohnathan Mantey 
16822c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
168381ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16842c70f800SEd Tanous     jsonResponse["@odata.id"] =
168581ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16862c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1687eeedda23SJohnathan Mantey 
1688eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1689eeedda23SJohnathan Mantey 
1690eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1691eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1692b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1693eeedda23SJohnathan Mantey         if (ec)
1694029573d4SEd Tanous         {
1695eeedda23SJohnathan Mantey             return;
1696eeedda23SJohnathan Mantey         }
1697eeedda23SJohnathan Mantey 
1698914e2d5dSEd Tanous         health->inventory = resp;
1699eeedda23SJohnathan Mantey         },
1700eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1701eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1702bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1703bf648f77SEd Tanous         inventoryForEthernet);
1704eeedda23SJohnathan Mantey 
1705eeedda23SJohnathan Mantey     health->populate();
1706eeedda23SJohnathan Mantey 
1707eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1708eeedda23SJohnathan Mantey     {
17092c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17102c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1711029573d4SEd Tanous     }
1712029573d4SEd Tanous     else
1713029573d4SEd Tanous     {
17142c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17152c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1716029573d4SEd Tanous     }
1717aa05fb27SJohnathan Mantey 
17182c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17192c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
172035fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
172182695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
17222c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
172382695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
172482695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
172582695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
172682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
17271f8c7b5dSJohnathan Mantey 
17282c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
172982695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
17301f8c7b5dSJohnathan Mantey                                                                : "Disabled";
173182695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
173282695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
173382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
17342a133282Smanojkiraneda 
173582695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
17364a0cb85cSEd Tanous     {
173782695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1738ab6554f1SJoshi-Mansi 
1739ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1740ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1741ab6554f1SJoshi-Mansi         // FQDN
174282695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1743d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1744d24bfc7aSJennifer Lee         {
17452c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1746d24bfc7aSJennifer Lee         }
17472c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17484a0cb85cSEd Tanous     }
17494a0cb85cSEd Tanous 
17502c70f800SEd Tanous     jsonResponse["VLANs"] = {
1751bf648f77SEd Tanous         {"@odata.id",
1752bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1753fda13ad2SSunitha Harish 
17542c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17552c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17564a0cb85cSEd Tanous 
17572c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17582c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17592c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17602c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17619eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17624a0cb85cSEd Tanous     {
1763fa5053a6SGunnar Mills 
17642c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1765fa5053a6SGunnar Mills         if (gatewayStr.empty())
1766fa5053a6SGunnar Mills         {
1767fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1768fa5053a6SGunnar Mills         }
17691476687dSEd Tanous         nlohmann::json::object_t ipv4;
17701476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
17711476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
17721476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
17731476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1774fa5053a6SGunnar Mills 
17752c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1776d1d50814SRavi Teja         {
17771476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1778d1d50814SRavi Teja         }
17791476687dSEd Tanous 
17801476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
178101784826SJohnathan Mantey     }
1782d1d50814SRavi Teja 
178382695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
17847ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17857ea79e5eSRavi Teja     {
17867ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17877ea79e5eSRavi Teja     }
17887ea79e5eSRavi Teja 
17897ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1790e48c0fc5SRavi Teja 
17912c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17922c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17932c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17942c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17957f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17962c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17977f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17989eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1799e48c0fc5SRavi Teja     {
18001476687dSEd Tanous         nlohmann::json::object_t ipv6;
18011476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
18021476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
18031476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
18041476687dSEd Tanous         ipv6["AddressState"] = nullptr;
18051476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
18062c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1807e48c0fc5SRavi Teja         {
18081476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
18091476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
18101476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
18111476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
181201784826SJohnathan Mantey         }
1813e48c0fc5SRavi Teja     }
1814588c3f0dSKowalski, Kamil }
1815588c3f0dSKowalski, Kamil 
18164f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1817bf648f77SEd Tanous                                const std::string& parentIfaceId,
1818bf648f77SEd Tanous                                const std::string& ifaceId,
1819bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18201abe55efSEd Tanous {
1821bf648f77SEd Tanous     // Fill out obvious data...
1822bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1823bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1824bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1825bf648f77SEd Tanous 
1826bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
182717e22024SJiaqing Zhao     if (ethData.vlanId)
1828bf648f77SEd Tanous     {
182917e22024SJiaqing Zhao         jsonResponse["VLANId"] = *ethData.vlanId;
1830bf648f77SEd Tanous     }
1831bf648f77SEd Tanous }
1832bf648f77SEd Tanous 
18334f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1834bf648f77SEd Tanous {
183511ba3979SEd Tanous     return iface.starts_with(parent + "_");
1836bf648f77SEd Tanous }
1837bf648f77SEd Tanous 
1838bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1839bf648f77SEd Tanous {
1840bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1841ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
18421476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
18431476687dSEd Tanous             [&app](const crow::Request& req,
18441476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
18453ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
184645ca1b86SEd Tanous         {
184745ca1b86SEd Tanous             return;
184845ca1b86SEd Tanous         }
184945ca1b86SEd Tanous 
1850bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1851bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1852bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1853bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1854bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1855bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1856bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1857bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1858bf648f77SEd Tanous 
1859bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1860bf648f77SEd Tanous         // preparation
1861002d39b4SEd Tanous         getEthernetIfaceList(
1862002d39b4SEd Tanous             [asyncResp](
18631476687dSEd Tanous                 const bool& success,
1864002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1865bf648f77SEd Tanous             if (!success)
18661abe55efSEd Tanous             {
1867f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18689391bb9cSRapkiewicz, Pawel                 return;
18699391bb9cSRapkiewicz, Pawel             }
18709391bb9cSRapkiewicz, Pawel 
1871002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1872bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1873bf648f77SEd Tanous             std::string tag = "_";
1874bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1875bf648f77SEd Tanous             {
1876bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1877bf648f77SEd Tanous                 if (found == std::string::npos)
1878bf648f77SEd Tanous                 {
18791476687dSEd Tanous                     nlohmann::json::object_t iface;
18801476687dSEd Tanous                     iface["@odata.id"] =
1881bf648f77SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
18821476687dSEd Tanous                         ifaceItem;
18831476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
1884bf648f77SEd Tanous                 }
1885bf648f77SEd Tanous             }
1886bf648f77SEd Tanous 
1887002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1888bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1889bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1890bf648f77SEd Tanous         });
1891bf648f77SEd Tanous         });
1892bf648f77SEd Tanous 
1893bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1894ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1895bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
189645ca1b86SEd Tanous             [&app](const crow::Request& req,
1897bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1898bf648f77SEd Tanous                    const std::string& ifaceId) {
18993ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
190045ca1b86SEd Tanous         {
190145ca1b86SEd Tanous             return;
190245ca1b86SEd Tanous         }
19034a0cb85cSEd Tanous         getEthernetIfaceData(
1904bf648f77SEd Tanous             ifaceId,
1905002d39b4SEd Tanous             [asyncResp, ifaceId](
1906002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1907002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1908002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19094a0cb85cSEd Tanous             if (!success)
19101abe55efSEd Tanous             {
1911bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1912bf648f77SEd Tanous                 // existing object, and other errors
1913002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1914002d39b4SEd Tanous                                            ifaceId);
19154a0cb85cSEd Tanous                 return;
19169391bb9cSRapkiewicz, Pawel             }
19174c9afe43SEd Tanous 
19180f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1919fda13ad2SSunitha Harish                 "#EthernetInterface.v1_4_1.EthernetInterface";
1920002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19210f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
19220f74e643SEd Tanous                 "Management Network Interface";
19230f74e643SEd Tanous 
1924002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
19259391bb9cSRapkiewicz, Pawel             });
1926bf648f77SEd Tanous         });
19279391bb9cSRapkiewicz, Pawel 
1928bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1929ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1930bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
193145ca1b86SEd Tanous             [&app](const crow::Request& req,
1932bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1933bf648f77SEd Tanous                    const std::string& ifaceId) {
19343ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
193545ca1b86SEd Tanous         {
193645ca1b86SEd Tanous             return;
193745ca1b86SEd Tanous         }
1938bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1939ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1940d577665bSRatan Gupta         std::optional<std::string> macAddress;
19419a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1942d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1943e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1944f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1945da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19461f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1947eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
194835fb5311STejas Patil         std::optional<size_t> mtuSize;
19491f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19501f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19510627a2c7SEd Tanous 
195215ed6780SWilly Tu         if (!json_util::readJsonPatch(
19538d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1954002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1955002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1956002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1957ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1958002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
19591abe55efSEd Tanous         {
1960588c3f0dSKowalski, Kamil             return;
1961588c3f0dSKowalski, Kamil         }
1962da131a9aSJennifer Lee         if (dhcpv4)
1963da131a9aSJennifer Lee         {
1964002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
19651f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
196682695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
196782695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
196882695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
19691f8c7b5dSJohnathan Mantey             {
19701f8c7b5dSJohnathan Mantey                 return;
19711f8c7b5dSJohnathan Mantey             }
19721f8c7b5dSJohnathan Mantey         }
19731f8c7b5dSJohnathan Mantey 
19741f8c7b5dSJohnathan Mantey         if (dhcpv6)
19751f8c7b5dSJohnathan Mantey         {
1976002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1977002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1978002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1979002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1980002d39b4SEd Tanous                                      "UseDomainName",
198182695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
19821f8c7b5dSJohnathan Mantey             {
19831f8c7b5dSJohnathan Mantey                 return;
19841f8c7b5dSJohnathan Mantey             }
1985da131a9aSJennifer Lee         }
1986da131a9aSJennifer Lee 
1987bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1988bf648f77SEd Tanous         // for JSON preparation
19894a0cb85cSEd Tanous         getEthernetIfaceData(
19902c70f800SEd Tanous             ifaceId,
1991bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1992ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1993d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19949a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1995e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19961f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1997bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1998bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1999f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2000002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2001002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
2002002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
20031abe55efSEd Tanous             if (!success)
20041abe55efSEd Tanous             {
2005588c3f0dSKowalski, Kamil                 // ... otherwise return error
2006bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2007bf648f77SEd Tanous                 // existing object, and other errors
2008002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Ethernet Interface",
2009002d39b4SEd Tanous                                            ifaceId);
2010588c3f0dSKowalski, Kamil                 return;
2011588c3f0dSKowalski, Kamil             }
2012588c3f0dSKowalski, Kamil 
20131f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
20141f8c7b5dSJohnathan Mantey             {
2015002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
2016002d39b4SEd Tanous                                 asyncResp);
20171f8c7b5dSJohnathan Mantey             }
20181f8c7b5dSJohnathan Mantey 
20190627a2c7SEd Tanous             if (hostname)
20201abe55efSEd Tanous             {
20210627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
20221abe55efSEd Tanous             }
20230627a2c7SEd Tanous 
2024ab6554f1SJoshi-Mansi             if (fqdn)
2025ab6554f1SJoshi-Mansi             {
20262c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2027ab6554f1SJoshi-Mansi             }
2028ab6554f1SJoshi-Mansi 
2029d577665bSRatan Gupta             if (macAddress)
2030d577665bSRatan Gupta             {
2031002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2032d577665bSRatan Gupta             }
2033d577665bSRatan Gupta 
2034d1d50814SRavi Teja             if (ipv4StaticAddresses)
2035d1d50814SRavi Teja             {
2036bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
2037bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
2038bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
2039bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
2040bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
2041bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
2042bf648f77SEd Tanous                 // that, but could be done more efficiently
2043f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2044002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
20451abe55efSEd Tanous             }
20460627a2c7SEd Tanous 
2047f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
2048f85837bfSRAJESWARAN THILLAIGOVINDAN             {
2049002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2050002d39b4SEd Tanous                                              asyncResp);
2051f85837bfSRAJESWARAN THILLAIGOVINDAN             }
20529a6fc6feSRavi Teja 
20539a6fc6feSRavi Teja             if (ipv6DefaultGateway)
20549a6fc6feSRavi Teja             {
20559a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
20569a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
20579a6fc6feSRavi Teja             }
2058e48c0fc5SRavi Teja 
2059e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
2060e48c0fc5SRavi Teja             {
2061002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
2062002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
2063002d39b4SEd Tanous                                                asyncResp);
2064e48c0fc5SRavi Teja             }
2065eeedda23SJohnathan Mantey 
2066eeedda23SJohnathan Mantey             if (interfaceEnabled)
2067eeedda23SJohnathan Mantey             {
2068002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2069002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
2070eeedda23SJohnathan Mantey             }
207135fb5311STejas Patil 
207235fb5311STejas Patil             if (mtuSize)
207335fb5311STejas Patil             {
207435fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
207535fb5311STejas Patil             }
2076588c3f0dSKowalski, Kamil             });
2077bf648f77SEd Tanous         });
20789391bb9cSRapkiewicz, Pawel 
2079bf648f77SEd Tanous     BMCWEB_ROUTE(
2080bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2081ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2082bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
208345ca1b86SEd Tanous             [&app](const crow::Request& req,
2084bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
208545ca1b86SEd Tanous                    const std::string& parentIfaceId,
208645ca1b86SEd Tanous                    const std::string& ifaceId) {
20873ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
208845ca1b86SEd Tanous         {
208945ca1b86SEd Tanous             return;
209045ca1b86SEd Tanous         }
20918d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
20920f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20938d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2094e439f0f8SKowalski, Kamil 
20952c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
20961abe55efSEd Tanous         {
2097a434f2bdSEd Tanous             return;
2098a434f2bdSEd Tanous         }
2099a434f2bdSEd Tanous 
2100bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2101bf648f77SEd Tanous         // for JSON preparation
21024a0cb85cSEd Tanous         getEthernetIfaceData(
2103bf648f77SEd Tanous             ifaceId,
2104002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2105002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2106cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2107cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
210817e22024SJiaqing Zhao             if (success && ethData.vlanId)
21091abe55efSEd Tanous             {
2110002d39b4SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2111002d39b4SEd Tanous                                    ifaceId, ethData);
21121abe55efSEd Tanous             }
21131abe55efSEd Tanous             else
21141abe55efSEd Tanous             {
2115e439f0f8SKowalski, Kamil                 // ... otherwise return error
2116bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2117bf648f77SEd Tanous                 // existing object, and other errors
2118bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2119002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2120e439f0f8SKowalski, Kamil             }
2121e439f0f8SKowalski, Kamil             });
2122bf648f77SEd Tanous         });
2123e439f0f8SKowalski, Kamil 
2124bf648f77SEd Tanous     BMCWEB_ROUTE(
2125bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21263d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2127bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
212845ca1b86SEd Tanous             [&app](const crow::Request& req,
2129bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
213045ca1b86SEd Tanous                    const std::string& parentIfaceId,
213145ca1b86SEd Tanous                    const std::string& ifaceId) {
21323ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
213345ca1b86SEd Tanous         {
213445ca1b86SEd Tanous             return;
213545ca1b86SEd Tanous         }
2136fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21371abe55efSEd Tanous         {
2138002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2139002d39b4SEd Tanous                                        ifaceId);
2140927a505aSKowalski, Kamil             return;
2141927a505aSKowalski, Kamil         }
2142927a505aSKowalski, Kamil 
21433927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
21443927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
21450627a2c7SEd Tanous 
214615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2147bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
21481abe55efSEd Tanous         {
2149927a505aSKowalski, Kamil             return;
2150927a505aSKowalski, Kamil         }
2151927a505aSKowalski, Kamil 
21523927e13eSJiaqing Zhao         if (vlanId)
21533927e13eSJiaqing Zhao         {
21543927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
21553927e13eSJiaqing Zhao             return;
21563927e13eSJiaqing Zhao         }
21573927e13eSJiaqing Zhao 
2158bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2159bf648f77SEd Tanous         // for JSON preparation
2160e48c0fc5SRavi Teja         getEthernetIfaceData(
2161bf648f77SEd Tanous             ifaceId,
21623927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2163002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2164cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2165cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
216617e22024SJiaqing Zhao             if (success && ethData.vlanId)
216708244d02SSunitha Harish             {
216808244d02SSunitha Harish                 auto callback =
2169002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
217008244d02SSunitha Harish                     if (ec)
217108244d02SSunitha Harish                     {
217208244d02SSunitha Harish                         messages::internalError(asyncResp->res);
21733927e13eSJiaqing Zhao                         return;
217408244d02SSunitha Harish                     }
217508244d02SSunitha Harish                 };
217608244d02SSunitha Harish 
21773927e13eSJiaqing Zhao                 if (vlanEnable && !(*vlanEnable))
217808244d02SSunitha Harish                 {
2179002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2180e48c0fc5SRavi Teja                                         "vlan interface";
218108244d02SSunitha Harish                     crow::connections::systemBus->async_method_call(
2182002d39b4SEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
2183002d39b4SEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
2184002d39b4SEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
218508244d02SSunitha Harish                 }
218608244d02SSunitha Harish             }
218708244d02SSunitha Harish             else
21881abe55efSEd Tanous             {
2189bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2190bf648f77SEd Tanous                 // existing object, and other errors
2191bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2192002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2193bf648f77SEd Tanous                 return;
2194bf648f77SEd Tanous             }
2195bf648f77SEd Tanous             });
2196bf648f77SEd Tanous         });
2197bf648f77SEd Tanous 
2198bf648f77SEd Tanous     BMCWEB_ROUTE(
2199bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
22003d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2201bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
220245ca1b86SEd Tanous             [&app](const crow::Request& req,
2203bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
220445ca1b86SEd Tanous                    const std::string& parentIfaceId,
220545ca1b86SEd Tanous                    const std::string& ifaceId) {
22063ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
220745ca1b86SEd Tanous         {
220845ca1b86SEd Tanous             return;
220945ca1b86SEd Tanous         }
2210bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2211bf648f77SEd Tanous         {
2212002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2213002d39b4SEd Tanous                                        ifaceId);
2214927a505aSKowalski, Kamil             return;
2215927a505aSKowalski, Kamil         }
2216e439f0f8SKowalski, Kamil 
2217bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2218bf648f77SEd Tanous         // for JSON preparation
2219f12894f8SJason M. Bills         getEthernetIfaceData(
2220bf648f77SEd Tanous             ifaceId,
2221002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2222002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2223cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2224cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
222517e22024SJiaqing Zhao             if (success && ethData.vlanId)
22261abe55efSEd Tanous             {
2227f12894f8SJason M. Bills                 auto callback =
2228002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
22291abe55efSEd Tanous                     if (ec)
22301abe55efSEd Tanous                     {
2231f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2232927a505aSKowalski, Kamil                     }
22334a0cb85cSEd Tanous                 };
22344a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2235002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2236002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
22374a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
22381abe55efSEd Tanous             }
22391abe55efSEd Tanous             else
22401abe55efSEd Tanous             {
2241927a505aSKowalski, Kamil                 // ... otherwise return error
2242bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2243bf648f77SEd Tanous                 // existing object, and other errors
2244bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2245002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2246927a505aSKowalski, Kamil             }
2247927a505aSKowalski, Kamil             });
2248bf648f77SEd Tanous         });
2249e439f0f8SKowalski, Kamil 
2250bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2251bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2252ed398213SEd Tanous 
2253ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
22541476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
22551476687dSEd Tanous             [&app](const crow::Request& req,
2256bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2257bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
22583ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
225945ca1b86SEd Tanous         {
226045ca1b86SEd Tanous             return;
226145ca1b86SEd Tanous         }
22624a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
22631abe55efSEd Tanous         // preparation
2264002d39b4SEd Tanous         getEthernetIfaceList(
2265002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
22661abe55efSEd Tanous                 const bool& success,
2267002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
22684a0cb85cSEd Tanous             if (!success)
22691abe55efSEd Tanous             {
2270f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22714a0cb85cSEd Tanous                 return;
22721abe55efSEd Tanous             }
22734c9afe43SEd Tanous 
227481ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22754c9afe43SEd Tanous             {
2276002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2277002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
22784c9afe43SEd Tanous                                            rootInterfaceName);
22794c9afe43SEd Tanous                 return;
22804c9afe43SEd Tanous             }
22814c9afe43SEd Tanous 
22820f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
22830f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
22840f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
22850f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
22860f74e643SEd Tanous                 "VLAN Network Interface Collection";
22874a0cb85cSEd Tanous 
22882c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
22894a0cb85cSEd Tanous 
229081ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
22911abe55efSEd Tanous             {
229211ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
22934a0cb85cSEd Tanous                 {
2294f23b7296SEd Tanous                     std::string path =
2295f23b7296SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2296f23b7296SEd Tanous                     path += rootInterfaceName;
2297f23b7296SEd Tanous                     path += "/VLANs/";
2298f23b7296SEd Tanous                     path += ifaceItem;
22991476687dSEd Tanous                     nlohmann::json::object_t iface;
23001476687dSEd Tanous                     iface["@odata.id"] = std::move(path);
23011476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2302e439f0f8SKowalski, Kamil                 }
2303e439f0f8SKowalski, Kamil             }
2304e439f0f8SKowalski, Kamil 
2305002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
23062c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23074a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
23084a0cb85cSEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23094a0cb85cSEd Tanous                 rootInterfaceName + "/VLANs";
2310e439f0f8SKowalski, Kamil         });
2311bf648f77SEd Tanous         });
2312e439f0f8SKowalski, Kamil 
2313bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2314bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
23153d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2316bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
231745ca1b86SEd Tanous             [&app](const crow::Request& req,
2318bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2319bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
23203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
232145ca1b86SEd Tanous         {
232245ca1b86SEd Tanous             return;
232345ca1b86SEd Tanous         }
2324fda13ad2SSunitha Harish         bool vlanEnable = false;
23250627a2c7SEd Tanous         uint32_t vlanId = 0;
2326002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2327002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
23281abe55efSEd Tanous         {
23294a0cb85cSEd Tanous             return;
2330e439f0f8SKowalski, Kamil         }
2331fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2332dbb59d4dSEd Tanous         if (vlanId == 0U)
2333fda13ad2SSunitha Harish         {
2334fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2335fda13ad2SSunitha Harish         }
2336fda13ad2SSunitha Harish         if (!vlanEnable)
2337fda13ad2SSunitha Harish         {
2338fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2339fda13ad2SSunitha Harish         }
2340271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2341fda13ad2SSunitha Harish         {
2342fda13ad2SSunitha Harish             return;
2343fda13ad2SSunitha Harish         }
2344fda13ad2SSunitha Harish 
2345002d39b4SEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
23461abe55efSEd Tanous             if (ec)
23471abe55efSEd Tanous             {
2348bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2349bf648f77SEd Tanous                 // based on phosphor-network responses
2350f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
23514a0cb85cSEd Tanous                 return;
23521abe55efSEd Tanous             }
2353f12894f8SJason M. Bills             messages::created(asyncResp->res);
2354e439f0f8SKowalski, Kamil         };
23554a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
23564a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
23574a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
23584a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23590627a2c7SEd Tanous             rootInterfaceName, vlanId);
2360bf648f77SEd Tanous         });
23614a0cb85cSEd Tanous }
2362bf648f77SEd Tanous 
23639391bb9cSRapkiewicz, Pawel } // namespace redfish
2364