xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 711ac7a931dd3f151fc4064063b5ea90404b9054)
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>
191abe55efSEd Tanous #include <boost/container/flat_map.hpp>
204a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
21179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
22168e20c1SEd Tanous #include <dbus_utility.hpp>
23588c3f0dSKowalski, Kamil #include <error_messages.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251214b7e7SGunnar Mills #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
27a24526dcSEd Tanous #include <optional>
28ab6554f1SJoshi-Mansi #include <regex>
299391bb9cSRapkiewicz, Pawel 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
329391bb9cSRapkiewicz, Pawel 
339391bb9cSRapkiewicz, Pawel /**
349391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
359391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
369391bb9cSRapkiewicz, Pawel  */
37168e20c1SEd Tanous using PropertiesMapType =
38168e20c1SEd Tanous     boost::container::flat_map<std::string, dbus::utility::DbusVariantType>;
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous enum class LinkType
414a0cb85cSEd Tanous {
424a0cb85cSEd Tanous     Local,
434a0cb85cSEd Tanous     Global
444a0cb85cSEd Tanous };
459391bb9cSRapkiewicz, Pawel 
469391bb9cSRapkiewicz, Pawel /**
479391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
489391bb9cSRapkiewicz, Pawel  */
491abe55efSEd Tanous struct IPv4AddressData
501abe55efSEd Tanous {
51179db1d7SKowalski, Kamil     std::string id;
524a0cb85cSEd Tanous     std::string address;
534a0cb85cSEd Tanous     std::string domain;
544a0cb85cSEd Tanous     std::string gateway;
559391bb9cSRapkiewicz, Pawel     std::string netmask;
569391bb9cSRapkiewicz, Pawel     std::string origin;
574a0cb85cSEd Tanous     LinkType linktype;
5801c6e858SSunitha Harish     bool isActive;
594a0cb85cSEd Tanous 
601abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
611abe55efSEd Tanous     {
624a0cb85cSEd Tanous         return id < obj.id;
631abe55efSEd Tanous     }
649391bb9cSRapkiewicz, Pawel };
659391bb9cSRapkiewicz, Pawel 
669391bb9cSRapkiewicz, Pawel /**
67e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
68e48c0fc5SRavi Teja  */
69e48c0fc5SRavi Teja struct IPv6AddressData
70e48c0fc5SRavi Teja {
71e48c0fc5SRavi Teja     std::string id;
72e48c0fc5SRavi Teja     std::string address;
73e48c0fc5SRavi Teja     std::string origin;
74e48c0fc5SRavi Teja     uint8_t prefixLength;
75e48c0fc5SRavi Teja 
76e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
77e48c0fc5SRavi Teja     {
78e48c0fc5SRavi Teja         return id < obj.id;
79e48c0fc5SRavi Teja     }
80e48c0fc5SRavi Teja };
81e48c0fc5SRavi Teja /**
829391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
839391bb9cSRapkiewicz, Pawel  * available from DBus
849391bb9cSRapkiewicz, Pawel  */
851abe55efSEd Tanous struct EthernetInterfaceData
861abe55efSEd Tanous {
874a0cb85cSEd Tanous     uint32_t speed;
884a0cb85cSEd Tanous     bool auto_neg;
891f8c7b5dSJohnathan Mantey     bool DNSEnabled;
901f8c7b5dSJohnathan Mantey     bool NTPEnabled;
911f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
921f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
93aa05fb27SJohnathan Mantey     bool linkUp;
94eeedda23SJohnathan Mantey     bool nicEnabled;
951f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
961f8c7b5dSJohnathan Mantey     std::string operatingMode;
974a0cb85cSEd Tanous     std::string hostname;
984a0cb85cSEd Tanous     std::string default_gateway;
999a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1004a0cb85cSEd Tanous     std::string mac_address;
101fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
1020f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1030f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
104d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1059391bb9cSRapkiewicz, Pawel };
1069391bb9cSRapkiewicz, Pawel 
1071f8c7b5dSJohnathan Mantey struct DHCPParameters
1081f8c7b5dSJohnathan Mantey {
1091f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1101f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1111f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1121f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1131f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1141f8c7b5dSJohnathan Mantey };
1151f8c7b5dSJohnathan Mantey 
1169391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1179391bb9cSRapkiewicz, Pawel // into full dot notation
1181abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1191abe55efSEd Tanous {
1209391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1219391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1229391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1249391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1259391bb9cSRapkiewicz, Pawel     return netmask;
1269391bb9cSRapkiewicz, Pawel }
1279391bb9cSRapkiewicz, Pawel 
1281f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1291f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1301f8c7b5dSJohnathan Mantey {
1311f8c7b5dSJohnathan Mantey     if (isIPv4)
1321f8c7b5dSJohnathan Mantey     {
1331f8c7b5dSJohnathan Mantey         return (
1341f8c7b5dSJohnathan Mantey             (inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1361f8c7b5dSJohnathan Mantey             (inputDHCP ==
1371f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1381f8c7b5dSJohnathan Mantey     }
1391f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1401f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1411f8c7b5dSJohnathan Mantey             (inputDHCP ==
1421f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1431f8c7b5dSJohnathan Mantey }
1441f8c7b5dSJohnathan Mantey 
1452c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1461f8c7b5dSJohnathan Mantey {
1471f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1481f8c7b5dSJohnathan Mantey     {
1491f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1501f8c7b5dSJohnathan Mantey     }
1513174e4dfSEd Tanous     if (isIPv4)
1521f8c7b5dSJohnathan Mantey     {
1531f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1541f8c7b5dSJohnathan Mantey     }
1553174e4dfSEd Tanous     if (isIPv6)
1561f8c7b5dSJohnathan Mantey     {
1571f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1581f8c7b5dSJohnathan Mantey     }
1591f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1601f8c7b5dSJohnathan Mantey }
1611f8c7b5dSJohnathan Mantey 
1624a0cb85cSEd Tanous inline std::string
1634a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1644a0cb85cSEd Tanous                                         bool isIPv4)
1651abe55efSEd Tanous {
1664a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1671abe55efSEd Tanous     {
1684a0cb85cSEd Tanous         return "Static";
1699391bb9cSRapkiewicz, Pawel     }
1704a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1711abe55efSEd Tanous     {
1724a0cb85cSEd Tanous         if (isIPv4)
1731abe55efSEd Tanous         {
1744a0cb85cSEd Tanous             return "IPv4LinkLocal";
1751abe55efSEd Tanous         }
1764a0cb85cSEd Tanous         return "LinkLocal";
1779391bb9cSRapkiewicz, Pawel     }
1784a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1791abe55efSEd Tanous     {
1804a0cb85cSEd Tanous         if (isIPv4)
1814a0cb85cSEd Tanous         {
1824a0cb85cSEd Tanous             return "DHCP";
1834a0cb85cSEd Tanous         }
1844a0cb85cSEd Tanous         return "DHCPv6";
1854a0cb85cSEd Tanous     }
1864a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1874a0cb85cSEd Tanous     {
1884a0cb85cSEd Tanous         return "SLAAC";
1894a0cb85cSEd Tanous     }
1904a0cb85cSEd Tanous     return "";
1914a0cb85cSEd Tanous }
1924a0cb85cSEd Tanous 
193*711ac7a9SEd Tanous inline bool
194*711ac7a9SEd Tanous     extractEthernetInterfaceData(const std::string& ethifaceId,
195*711ac7a9SEd Tanous                                  dbus::utility::ManagedObjectType& dbusData,
1964a0cb85cSEd Tanous                                  EthernetInterfaceData& ethData)
1974a0cb85cSEd Tanous {
1984c9afe43SEd Tanous     bool idFound = false;
19981ce609eSEd Tanous     for (auto& objpath : dbusData)
2004a0cb85cSEd Tanous     {
201f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
2024a0cb85cSEd Tanous         {
20381ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
204029573d4SEd Tanous             {
2054c9afe43SEd Tanous                 idFound = true;
2064a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2074a0cb85cSEd Tanous                 {
2084a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2094a0cb85cSEd Tanous                     {
2104a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2114a0cb85cSEd Tanous                         {
2124a0cb85cSEd Tanous                             const std::string* mac =
213abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2144a0cb85cSEd Tanous                             if (mac != nullptr)
2154a0cb85cSEd Tanous                             {
2164a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2174a0cb85cSEd Tanous                             }
2184a0cb85cSEd Tanous                         }
2194a0cb85cSEd Tanous                     }
2204a0cb85cSEd Tanous                 }
2214a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2224a0cb85cSEd Tanous                 {
2234a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2244a0cb85cSEd Tanous                     {
2254a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2264a0cb85cSEd Tanous                         {
2271b6b96c5SEd Tanous                             const uint32_t* id =
228abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2294a0cb85cSEd Tanous                             if (id != nullptr)
2304a0cb85cSEd Tanous                             {
231fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2324a0cb85cSEd Tanous                             }
2334a0cb85cSEd Tanous                         }
2344a0cb85cSEd Tanous                     }
2354a0cb85cSEd Tanous                 }
2364a0cb85cSEd Tanous                 else if (ifacePair.first ==
2374a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2384a0cb85cSEd Tanous                 {
2394a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2404a0cb85cSEd Tanous                     {
2414a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2424a0cb85cSEd Tanous                         {
2432c70f800SEd Tanous                             const bool* autoNeg =
244abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2452c70f800SEd Tanous                             if (autoNeg != nullptr)
2464a0cb85cSEd Tanous                             {
2472c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2484a0cb85cSEd Tanous                             }
2494a0cb85cSEd Tanous                         }
2504a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2514a0cb85cSEd Tanous                         {
2524a0cb85cSEd Tanous                             const uint32_t* speed =
253abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2544a0cb85cSEd Tanous                             if (speed != nullptr)
2554a0cb85cSEd Tanous                             {
2564a0cb85cSEd Tanous                                 ethData.speed = *speed;
2574a0cb85cSEd Tanous                             }
2584a0cb85cSEd Tanous                         }
259aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
260aa05fb27SJohnathan Mantey                         {
261aa05fb27SJohnathan Mantey                             const bool* linkUp =
262aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
263aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
264aa05fb27SJohnathan Mantey                             {
265aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
266aa05fb27SJohnathan Mantey                             }
267aa05fb27SJohnathan Mantey                         }
268eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
269eeedda23SJohnathan Mantey                         {
270eeedda23SJohnathan Mantey                             const bool* nicEnabled =
271eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
273eeedda23SJohnathan Mantey                             {
274eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
275eeedda23SJohnathan Mantey                             }
276eeedda23SJohnathan Mantey                         }
277f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
278029573d4SEd Tanous                         {
279029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2808d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
281029573d4SEd Tanous                                     &propertyPair.second);
282029573d4SEd Tanous                             if (nameservers != nullptr)
283029573d4SEd Tanous                             {
284f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2850f6efdc1Smanojkiran.eda@gmail.com                             }
2860f6efdc1Smanojkiran.eda@gmail.com                         }
2870f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2880f6efdc1Smanojkiran.eda@gmail.com                         {
2890f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2908d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2910f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2920f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2930f6efdc1Smanojkiran.eda@gmail.com                             {
294f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2954a0cb85cSEd Tanous                             }
2964a0cb85cSEd Tanous                         }
2972a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
2982a133282Smanojkiraneda                         {
2992c70f800SEd Tanous                             const std::string* dhcpEnabled =
3001f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3012c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3022a133282Smanojkiraneda                             {
3032c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3042a133282Smanojkiraneda                             }
3052a133282Smanojkiraneda                         }
306d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
307d24bfc7aSJennifer Lee                         {
308d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3098d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
310d24bfc7aSJennifer Lee                                     &propertyPair.second);
311d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
312d24bfc7aSJennifer Lee                             {
313f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
314d24bfc7aSJennifer Lee                             }
315d24bfc7aSJennifer Lee                         }
3169010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3179010ec2eSRavi Teja                         {
3189010ec2eSRavi Teja                             const std::string* defaultGateway =
3199010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3209010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3219010ec2eSRavi Teja                             {
3229010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3239010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3249010ec2eSRavi Teja                                 {
3259010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3269010ec2eSRavi Teja                                 }
3279010ec2eSRavi Teja                                 else
3289010ec2eSRavi Teja                                 {
3299010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3309010ec2eSRavi Teja                                 }
3319010ec2eSRavi Teja                             }
3329010ec2eSRavi Teja                         }
3339010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3349010ec2eSRavi Teja                         {
3359010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3369010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3379010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3389010ec2eSRavi Teja                             {
3399010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3409010ec2eSRavi Teja                                     *defaultGateway6;
3419010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3429010ec2eSRavi Teja                                 {
3439010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3449010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3459010ec2eSRavi Teja                                 }
3469010ec2eSRavi Teja                                 else
3479010ec2eSRavi Teja                                 {
3489010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3499010ec2eSRavi Teja                                         defaultGateway6Str;
3509010ec2eSRavi Teja                                 }
3519010ec2eSRavi Teja                             }
3529010ec2eSRavi Teja                         }
353029573d4SEd Tanous                     }
354029573d4SEd Tanous                 }
355029573d4SEd Tanous             }
3561f8c7b5dSJohnathan Mantey 
3571f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3581f8c7b5dSJohnathan Mantey             {
3591f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3601f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3611f8c7b5dSJohnathan Mantey                 {
3621f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3631f8c7b5dSJohnathan Mantey                     {
3641f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3651f8c7b5dSJohnathan Mantey                         {
3662c70f800SEd Tanous                             const bool* dnsEnabled =
3671f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3682c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3691f8c7b5dSJohnathan Mantey                             {
3702c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3711f8c7b5dSJohnathan Mantey                             }
3721f8c7b5dSJohnathan Mantey                         }
3731f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3741f8c7b5dSJohnathan Mantey                         {
3752c70f800SEd Tanous                             const bool* ntpEnabled =
3761f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3772c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3781f8c7b5dSJohnathan Mantey                             {
3792c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3801f8c7b5dSJohnathan Mantey                             }
3811f8c7b5dSJohnathan Mantey                         }
3821f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3831f8c7b5dSJohnathan Mantey                         {
3842c70f800SEd Tanous                             const bool* hostNameEnabled =
3851f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3862c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3871f8c7b5dSJohnathan Mantey                             {
3882c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3891f8c7b5dSJohnathan Mantey                             }
3901f8c7b5dSJohnathan Mantey                         }
3911f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3921f8c7b5dSJohnathan Mantey                         {
3932c70f800SEd Tanous                             const bool* sendHostNameEnabled =
3941f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3952c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
3961f8c7b5dSJohnathan Mantey                             {
3971f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
3982c70f800SEd Tanous                                     *sendHostNameEnabled;
3991f8c7b5dSJohnathan Mantey                             }
4001f8c7b5dSJohnathan Mantey                         }
4011f8c7b5dSJohnathan Mantey                     }
4021f8c7b5dSJohnathan Mantey                 }
4031f8c7b5dSJohnathan Mantey             }
404029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
405029573d4SEd Tanous             // to check eth number
406029573d4SEd Tanous             if (ifacePair.first ==
4074a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4084a0cb85cSEd Tanous             {
4094a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4104a0cb85cSEd Tanous                 {
4114a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4124a0cb85cSEd Tanous                     {
4134a0cb85cSEd Tanous                         const std::string* hostname =
4148d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4154a0cb85cSEd Tanous                         if (hostname != nullptr)
4164a0cb85cSEd Tanous                         {
4174a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4184a0cb85cSEd Tanous                         }
4194a0cb85cSEd Tanous                     }
4204a0cb85cSEd Tanous                 }
4214a0cb85cSEd Tanous             }
4224a0cb85cSEd Tanous         }
4234a0cb85cSEd Tanous     }
4244c9afe43SEd Tanous     return idFound;
4254a0cb85cSEd Tanous }
4264a0cb85cSEd Tanous 
427e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42801784826SJohnathan Mantey inline void
42981ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
430*711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43181ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
432e48c0fc5SRavi Teja {
433e48c0fc5SRavi Teja     const std::string ipv6PathStart =
43481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
435e48c0fc5SRavi Teja 
436e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
437e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43881ce609eSEd Tanous     for (const auto& objpath : dbusData)
439e48c0fc5SRavi Teja     {
440e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
441e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
442e48c0fc5SRavi Teja         {
443e48c0fc5SRavi Teja             for (auto& interface : objpath.second)
444e48c0fc5SRavi Teja             {
445e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
446e48c0fc5SRavi Teja                 {
447e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
448e48c0fc5SRavi Teja                     // appropriate
449e48c0fc5SRavi Teja                     std::pair<
450e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
451e48c0fc5SRavi Teja                         bool>
45281ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4532c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4542c70f800SEd Tanous                     ipv6Address.id =
455271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
456e48c0fc5SRavi Teja                     for (auto& property : interface.second)
457e48c0fc5SRavi Teja                     {
458e48c0fc5SRavi Teja                         if (property.first == "Address")
459e48c0fc5SRavi Teja                         {
460e48c0fc5SRavi Teja                             const std::string* address =
461e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
462e48c0fc5SRavi Teja                             if (address != nullptr)
463e48c0fc5SRavi Teja                             {
4642c70f800SEd Tanous                                 ipv6Address.address = *address;
465e48c0fc5SRavi Teja                             }
466e48c0fc5SRavi Teja                         }
467e48c0fc5SRavi Teja                         else if (property.first == "Origin")
468e48c0fc5SRavi Teja                         {
469e48c0fc5SRavi Teja                             const std::string* origin =
470e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
471e48c0fc5SRavi Teja                             if (origin != nullptr)
472e48c0fc5SRavi Teja                             {
4732c70f800SEd Tanous                                 ipv6Address.origin =
474e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
475e48c0fc5SRavi Teja                                                                         false);
476e48c0fc5SRavi Teja                             }
477e48c0fc5SRavi Teja                         }
478e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
479e48c0fc5SRavi Teja                         {
480e48c0fc5SRavi Teja                             const uint8_t* prefix =
481e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
482e48c0fc5SRavi Teja                             if (prefix != nullptr)
483e48c0fc5SRavi Teja                             {
4842c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
485e48c0fc5SRavi Teja                             }
486e48c0fc5SRavi Teja                         }
487889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
488889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
489889ff694SAsmitha Karunanithi                         {
490889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
491889ff694SAsmitha Karunanithi                         }
492e48c0fc5SRavi Teja                         else
493e48c0fc5SRavi Teja                         {
494e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
495e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
496e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
497e48c0fc5SRavi Teja                         }
498e48c0fc5SRavi Teja                     }
499e48c0fc5SRavi Teja                 }
500e48c0fc5SRavi Teja             }
501e48c0fc5SRavi Teja         }
502e48c0fc5SRavi Teja     }
503e48c0fc5SRavi Teja }
504e48c0fc5SRavi Teja 
5054a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50601784826SJohnathan Mantey inline void
50781ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
508*711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
50981ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5104a0cb85cSEd Tanous {
5114a0cb85cSEd Tanous     const std::string ipv4PathStart =
51281ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5134a0cb85cSEd Tanous 
5144a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5154a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
51681ce609eSEd Tanous     for (const auto& objpath : dbusData)
5174a0cb85cSEd Tanous     {
5184a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5194a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5204a0cb85cSEd Tanous         {
5214a0cb85cSEd Tanous             for (auto& interface : objpath.second)
5224a0cb85cSEd Tanous             {
5234a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5244a0cb85cSEd Tanous                 {
5254a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5264a0cb85cSEd Tanous                     // appropriate
5274a0cb85cSEd Tanous                     std::pair<
5284a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5294a0cb85cSEd Tanous                         bool>
53081ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5312c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5322c70f800SEd Tanous                     ipv4Address.id =
533271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5344a0cb85cSEd Tanous                     for (auto& property : interface.second)
5354a0cb85cSEd Tanous                     {
5364a0cb85cSEd Tanous                         if (property.first == "Address")
5374a0cb85cSEd Tanous                         {
5384a0cb85cSEd Tanous                             const std::string* address =
539abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5404a0cb85cSEd Tanous                             if (address != nullptr)
5414a0cb85cSEd Tanous                             {
5422c70f800SEd Tanous                                 ipv4Address.address = *address;
5434a0cb85cSEd Tanous                             }
5444a0cb85cSEd Tanous                         }
5454a0cb85cSEd Tanous                         else if (property.first == "Origin")
5464a0cb85cSEd Tanous                         {
5474a0cb85cSEd Tanous                             const std::string* origin =
548abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5494a0cb85cSEd Tanous                             if (origin != nullptr)
5504a0cb85cSEd Tanous                             {
5512c70f800SEd Tanous                                 ipv4Address.origin =
5524a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5534a0cb85cSEd Tanous                                                                         true);
5544a0cb85cSEd Tanous                             }
5554a0cb85cSEd Tanous                         }
5564a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5574a0cb85cSEd Tanous                         {
5584a0cb85cSEd Tanous                             const uint8_t* mask =
559abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5604a0cb85cSEd Tanous                             if (mask != nullptr)
5614a0cb85cSEd Tanous                             {
5624a0cb85cSEd Tanous                                 // convert it to the string
5632c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5644a0cb85cSEd Tanous                             }
5654a0cb85cSEd Tanous                         }
566889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
567889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
568889ff694SAsmitha Karunanithi                         {
569889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
570889ff694SAsmitha Karunanithi                         }
5714a0cb85cSEd Tanous                         else
5724a0cb85cSEd Tanous                         {
5734a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5744a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5754a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5764a0cb85cSEd Tanous                         }
5774a0cb85cSEd Tanous                     }
5784a0cb85cSEd Tanous                     // Check if given address is local, or global
5792c70f800SEd Tanous                     ipv4Address.linktype =
5802c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
58118659d10SJohnathan Mantey                             ? LinkType::Local
58218659d10SJohnathan Mantey                             : LinkType::Global;
5834a0cb85cSEd Tanous                 }
5844a0cb85cSEd Tanous             }
5854a0cb85cSEd Tanous         }
5864a0cb85cSEd Tanous     }
5874a0cb85cSEd Tanous }
588588c3f0dSKowalski, Kamil 
589588c3f0dSKowalski, Kamil /**
590588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
591588c3f0dSKowalski, Kamil  *
592588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
593588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
594588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
595588c3f0dSKowalski, Kamil  *
596588c3f0dSKowalski, Kamil  * @return None.
597588c3f0dSKowalski, Kamil  */
598588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5994a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
6001abe55efSEd Tanous                   CallbackFunc&& callback)
6011abe55efSEd Tanous {
60255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
603588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
604588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
605588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
606588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
607168e20c1SEd Tanous         dbus::utility::DbusVariantType(inputVlanId));
6084a0cb85cSEd Tanous }
609588c3f0dSKowalski, Kamil 
610588c3f0dSKowalski, Kamil /**
611179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
612179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
613179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
614179db1d7SKowalski, Kamil  *
615179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
616179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
617179db1d7SKowalski, Kamil  *
618179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
619179db1d7SKowalski, Kamil  */
6204a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6211abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6221abe55efSEd Tanous {
623179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
624179db1d7SKowalski, Kamil 
625179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
626179db1d7SKowalski, Kamil 
6274a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6281abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6291abe55efSEd Tanous     {
630179db1d7SKowalski, Kamil         return false;
631179db1d7SKowalski, Kamil     }
632179db1d7SKowalski, Kamil 
6331abe55efSEd Tanous     if (bits != nullptr)
6341abe55efSEd Tanous     {
635179db1d7SKowalski, Kamil         *bits = 0;
636179db1d7SKowalski, Kamil     }
637179db1d7SKowalski, Kamil 
638179db1d7SKowalski, Kamil     char* endPtr;
639179db1d7SKowalski, Kamil     long previousValue = 255;
640179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6411abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6421abe55efSEd Tanous     {
6431abe55efSEd Tanous         if (byte.empty())
6441abe55efSEd Tanous         {
6451db9ca37SKowalski, Kamil             return false;
6461db9ca37SKowalski, Kamil         }
6471db9ca37SKowalski, Kamil 
648179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6491db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
650179db1d7SKowalski, Kamil 
6514a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6524a0cb85cSEd Tanous         // is not 100% number
6531abe55efSEd Tanous         if (*endPtr != '\0')
6541abe55efSEd Tanous         {
655179db1d7SKowalski, Kamil             return false;
656179db1d7SKowalski, Kamil         }
657179db1d7SKowalski, Kamil 
658179db1d7SKowalski, Kamil         // Value should be contained in byte
6591abe55efSEd Tanous         if (value < 0 || value > 255)
6601abe55efSEd Tanous         {
661179db1d7SKowalski, Kamil             return false;
662179db1d7SKowalski, Kamil         }
663179db1d7SKowalski, Kamil 
6641abe55efSEd Tanous         if (bits != nullptr)
6651abe55efSEd Tanous         {
666179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6671abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6681abe55efSEd Tanous             {
669179db1d7SKowalski, Kamil                 return false;
670179db1d7SKowalski, Kamil             }
671179db1d7SKowalski, Kamil 
672179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
673179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
674179db1d7SKowalski, Kamil 
675179db1d7SKowalski, Kamil             // Count bits
67623a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6771abe55efSEd Tanous             {
67823a21a1cSEd Tanous                 if (value & (1L << bitIdx))
6791abe55efSEd Tanous                 {
6801abe55efSEd Tanous                     if (firstZeroInByteHit)
6811abe55efSEd Tanous                     {
682179db1d7SKowalski, Kamil                         // Continuity not preserved
683179db1d7SKowalski, Kamil                         return false;
6841abe55efSEd Tanous                     }
685179db1d7SKowalski, Kamil                     (*bits)++;
686179db1d7SKowalski, Kamil                 }
6871abe55efSEd Tanous                 else
6881abe55efSEd Tanous                 {
689179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
690179db1d7SKowalski, Kamil                 }
691179db1d7SKowalski, Kamil             }
692179db1d7SKowalski, Kamil         }
693179db1d7SKowalski, Kamil 
694179db1d7SKowalski, Kamil         previousValue = value;
695179db1d7SKowalski, Kamil     }
696179db1d7SKowalski, Kamil 
697179db1d7SKowalski, Kamil     return true;
698179db1d7SKowalski, Kamil }
699179db1d7SKowalski, Kamil 
700179db1d7SKowalski, Kamil /**
70101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
702179db1d7SKowalski, Kamil  *
703179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
704179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
705179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
706179db1d7SKowalski, Kamil  *
707179db1d7SKowalski, Kamil  * @return None
708179db1d7SKowalski, Kamil  */
7094a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7108d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7111abe55efSEd Tanous {
71255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
713286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7141abe55efSEd Tanous             if (ec)
7151abe55efSEd Tanous             {
716a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7171abe55efSEd Tanous             }
718179db1d7SKowalski, Kamil         },
719179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
720179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
721179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
722179db1d7SKowalski, Kamil }
723179db1d7SKowalski, Kamil 
724244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
725244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
726244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7279010ec2eSRavi Teja {
7289010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7299010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7309010ec2eSRavi Teja             if (ec)
7319010ec2eSRavi Teja             {
7329010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7339010ec2eSRavi Teja                 return;
7349010ec2eSRavi Teja             }
7359010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7369010ec2eSRavi Teja         },
7379010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7389010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7399010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7409010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
741168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7429010ec2eSRavi Teja }
743179db1d7SKowalski, Kamil /**
74401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
745179db1d7SKowalski, Kamil  *
74601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
74801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
74901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
750179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
751179db1d7SKowalski, Kamil  *
752179db1d7SKowalski, Kamil  * @return None
753179db1d7SKowalski, Kamil  */
754cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
755cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7568d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7571abe55efSEd Tanous {
7589010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7599010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7601abe55efSEd Tanous         if (ec)
7611abe55efSEd Tanous         {
762a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7639010ec2eSRavi Teja             return;
764179db1d7SKowalski, Kamil         }
7659010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7669010ec2eSRavi Teja     };
7679010ec2eSRavi Teja 
7689010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7699010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
770179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
771179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
77201784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
773179db1d7SKowalski, Kamil         gateway);
774179db1d7SKowalski, Kamil }
775e48c0fc5SRavi Teja 
776e48c0fc5SRavi Teja /**
77701784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
77801784826SJohnathan Mantey  * static IPv4 entry
77901784826SJohnathan Mantey  *
78001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
78101784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
78201784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
78301784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
78401784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
78501784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
78601784826SJohnathan Mantey  *
78701784826SJohnathan Mantey  * @return None
78801784826SJohnathan Mantey  */
7898d1b46d7Szhanghch05 inline void
7908d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7918d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
79201784826SJohnathan Mantey                         const std::string& address,
7938d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
79401784826SJohnathan Mantey {
79501784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
79601784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
79701784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
79801784826SJohnathan Mantey             if (ec)
79901784826SJohnathan Mantey             {
80001784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8019010ec2eSRavi Teja                 return;
80201784826SJohnathan Mantey             }
8039010ec2eSRavi Teja 
80401784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8059010ec2eSRavi Teja                 [asyncResp, ifaceId,
8069010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
80723a21a1cSEd Tanous                     if (ec2)
80801784826SJohnathan Mantey                     {
80901784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8109010ec2eSRavi Teja                         return;
81101784826SJohnathan Mantey                     }
8129010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
81301784826SJohnathan Mantey                 },
81401784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
81501784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
81601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
81701784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
81801784826SJohnathan Mantey                 prefixLength, gateway);
81901784826SJohnathan Mantey         },
82001784826SJohnathan Mantey         "xyz.openbmc_project.Network",
82101784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
82201784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
82301784826SJohnathan Mantey }
82401784826SJohnathan Mantey 
82501784826SJohnathan Mantey /**
826e48c0fc5SRavi Teja  * @brief Deletes given IPv6
827e48c0fc5SRavi Teja  *
828e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
829e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
830e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
831e48c0fc5SRavi Teja  *
832e48c0fc5SRavi Teja  * @return None
833e48c0fc5SRavi Teja  */
834e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8358d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
836e48c0fc5SRavi Teja {
837e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
838286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
839e48c0fc5SRavi Teja             if (ec)
840e48c0fc5SRavi Teja             {
841e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
842e48c0fc5SRavi Teja             }
843e48c0fc5SRavi Teja         },
844e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
845e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
846e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
847e48c0fc5SRavi Teja }
848e48c0fc5SRavi Teja 
849e48c0fc5SRavi Teja /**
85001784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
85101784826SJohnathan Mantey  * static IPv6 entry
85201784826SJohnathan Mantey  *
85301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
85401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
85501784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
85601784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
85701784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
85801784826SJohnathan Mantey  *
85901784826SJohnathan Mantey  * @return None
86001784826SJohnathan Mantey  */
8618d1b46d7Szhanghch05 inline void
8628d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8638d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8648d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86501784826SJohnathan Mantey {
86601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
86701784826SJohnathan Mantey         [asyncResp, ifaceId, address,
86801784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
86901784826SJohnathan Mantey             if (ec)
87001784826SJohnathan Mantey             {
87101784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
87201784826SJohnathan Mantey             }
87301784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
87423a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
87523a21a1cSEd Tanous                     if (ec2)
87601784826SJohnathan Mantey                     {
87701784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
87801784826SJohnathan Mantey                     }
87901784826SJohnathan Mantey                 },
88001784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
88101784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
88201784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
88301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
88401784826SJohnathan Mantey                 prefixLength, "");
88501784826SJohnathan Mantey         },
88601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
88701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
88801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
88901784826SJohnathan Mantey }
89001784826SJohnathan Mantey 
89101784826SJohnathan Mantey /**
892e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
893e48c0fc5SRavi Teja  *
894e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
895e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
896e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
897e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
898e48c0fc5SRavi Teja  *
899e48c0fc5SRavi Teja  * @return None
900e48c0fc5SRavi Teja  */
90101784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
90201784826SJohnathan Mantey                        const std::string& address,
9038d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
904e48c0fc5SRavi Teja {
905e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
906e48c0fc5SRavi Teja         if (ec)
907e48c0fc5SRavi Teja         {
908e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
909e48c0fc5SRavi Teja         }
910e48c0fc5SRavi Teja     };
911e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9124e0453b1SGunnar Mills     // does not have associated gateway property
913e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
914e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
915e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
916e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
917e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
918e48c0fc5SRavi Teja         "");
919e48c0fc5SRavi Teja }
920e48c0fc5SRavi Teja 
921179db1d7SKowalski, Kamil /**
922179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
923179db1d7SKowalski, Kamil  * Object
924179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9254a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
926179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
927179db1d7SKowalski, Kamil  * into JSON
928179db1d7SKowalski, Kamil  */
929179db1d7SKowalski, Kamil template <typename CallbackFunc>
93081ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9311abe55efSEd Tanous                           CallbackFunc&& callback)
9321abe55efSEd Tanous {
93355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
93481ce609eSEd Tanous         [ethifaceId{std::string{ethifaceId}}, callback{std::move(callback)}](
93581ce609eSEd Tanous             const boost::system::error_code errorCode,
936*711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
93755c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9384a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
939e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
940179db1d7SKowalski, Kamil 
94181ce609eSEd Tanous             if (errorCode)
9421abe55efSEd Tanous             {
94301784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
944179db1d7SKowalski, Kamil                 return;
945179db1d7SKowalski, Kamil             }
946179db1d7SKowalski, Kamil 
9474c9afe43SEd Tanous             bool found =
9482c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9494c9afe43SEd Tanous             if (!found)
9504c9afe43SEd Tanous             {
95101784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9524c9afe43SEd Tanous                 return;
9534c9afe43SEd Tanous             }
9544c9afe43SEd Tanous 
9552c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
956179db1d7SKowalski, Kamil             // Fix global GW
9571abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9581abe55efSEd Tanous             {
959c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
960c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9619010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9621abe55efSEd Tanous                 {
9634a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
964179db1d7SKowalski, Kamil                 }
965179db1d7SKowalski, Kamil             }
966179db1d7SKowalski, Kamil 
9672c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9684e0453b1SGunnar Mills             // Finally make a callback with useful data
96901784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
970179db1d7SKowalski, Kamil         },
971179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
972179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
973271584abSEd Tanous }
974179db1d7SKowalski, Kamil 
975179db1d7SKowalski, Kamil /**
9769391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9779391bb9cSRapkiewicz, Pawel  * Manager
9781abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9791abe55efSEd Tanous  * into JSON.
9809391bb9cSRapkiewicz, Pawel  */
9819391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9821abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9831abe55efSEd Tanous {
98455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9854a0cb85cSEd Tanous         [callback{std::move(callback)}](
98681ce609eSEd Tanous             const boost::system::error_code errorCode,
987*711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9881abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9891abe55efSEd Tanous             // ethernet interfaces
9902c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9912c70f800SEd Tanous             ifaceList.reserve(resp.size());
99281ce609eSEd Tanous             if (errorCode)
9931abe55efSEd Tanous             {
9942c70f800SEd Tanous                 callback(false, ifaceList);
9959391bb9cSRapkiewicz, Pawel                 return;
9969391bb9cSRapkiewicz, Pawel             }
9979391bb9cSRapkiewicz, Pawel 
9989391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9994a0cb85cSEd Tanous             for (const auto& objpath : resp)
10001abe55efSEd Tanous             {
10019391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10024a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10031abe55efSEd Tanous                 {
10041abe55efSEd Tanous                     // If interface is
10054a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10064a0cb85cSEd Tanous                     // what we're looking for.
10079391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10081abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10091abe55efSEd Tanous                     {
10102dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10112dfd18efSEd Tanous                         if (ifaceId.empty())
10121abe55efSEd Tanous                         {
10132dfd18efSEd Tanous                             continue;
10149391bb9cSRapkiewicz, Pawel                         }
10152dfd18efSEd Tanous                         // and put it into output vector.
10162dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10179391bb9cSRapkiewicz, Pawel                     }
10189391bb9cSRapkiewicz, Pawel                 }
10199391bb9cSRapkiewicz, Pawel             }
1020a434f2bdSEd Tanous             // Finally make a callback with useful data
10212c70f800SEd Tanous             callback(true, ifaceList);
10229391bb9cSRapkiewicz, Pawel         },
1023aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1024aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1025271584abSEd Tanous }
10269391bb9cSRapkiewicz, Pawel 
10274f48d5f6SEd Tanous inline void
10284f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10298d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10301abe55efSEd Tanous {
1031ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1032ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1033ab6554f1SJoshi-Mansi     {
1034ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1035ab6554f1SJoshi-Mansi                                            "HostName");
1036ab6554f1SJoshi-Mansi         return;
1037ab6554f1SJoshi-Mansi     }
1038bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1039bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10404a0cb85cSEd Tanous             if (ec)
10414a0cb85cSEd Tanous             {
1042a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10431abe55efSEd Tanous             }
1044bc0bd6e0SEd Tanous         },
1045bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1046bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1047bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1048168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1049588c3f0dSKowalski, Kamil }
1050588c3f0dSKowalski, Kamil 
10514f48d5f6SEd Tanous inline void
10524f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1053bf648f77SEd Tanous                           const std::string& domainname,
10548d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1055ab6554f1SJoshi-Mansi {
1056ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1057ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1058ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1059ab6554f1SJoshi-Mansi             if (ec)
1060ab6554f1SJoshi-Mansi             {
1061ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1062ab6554f1SJoshi-Mansi             }
1063ab6554f1SJoshi-Mansi         },
1064ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1065ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1066ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1067ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1068168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1069ab6554f1SJoshi-Mansi }
1070ab6554f1SJoshi-Mansi 
10714f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1072bf648f77SEd Tanous {
1073bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1074bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1075bf648f77SEd Tanous     {
1076bf648f77SEd Tanous         return false;
1077bf648f77SEd Tanous     }
1078bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1079bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1080bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1081bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1082bf648f77SEd Tanous     const std::regex pattern(
1083bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1084bf648f77SEd Tanous 
1085bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1086bf648f77SEd Tanous }
1087bf648f77SEd Tanous 
10884f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1089bf648f77SEd Tanous {
1090bf648f77SEd Tanous     // Can have multiple subdomains
1091bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
10920fda0f12SGeorge Liu     const std::regex pattern(
10930fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1094bf648f77SEd Tanous 
1095bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1096bf648f77SEd Tanous }
1097bf648f77SEd Tanous 
10984f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10998d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1100ab6554f1SJoshi-Mansi {
1101ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1102ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1103ab6554f1SJoshi-Mansi     {
1104ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1105ab6554f1SJoshi-Mansi         return;
1106ab6554f1SJoshi-Mansi     }
1107ab6554f1SJoshi-Mansi 
1108ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1109ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1110ab6554f1SJoshi-Mansi     {
1111ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1112ab6554f1SJoshi-Mansi         return;
1113ab6554f1SJoshi-Mansi     }
1114ab6554f1SJoshi-Mansi 
1115ab6554f1SJoshi-Mansi     std::string hostname;
1116ab6554f1SJoshi-Mansi     std::string domainname;
1117ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1118ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1119ab6554f1SJoshi-Mansi 
1120ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1121ab6554f1SJoshi-Mansi     {
1122ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1123ab6554f1SJoshi-Mansi         return;
1124ab6554f1SJoshi-Mansi     }
1125ab6554f1SJoshi-Mansi 
1126ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1127ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1128ab6554f1SJoshi-Mansi }
1129ab6554f1SJoshi-Mansi 
11304f48d5f6SEd Tanous inline void
11314f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1132bf648f77SEd Tanous                           const std::string& macAddress,
11338d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1134d577665bSRatan Gupta {
1135d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1136d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1137d577665bSRatan Gupta             if (ec)
1138d577665bSRatan Gupta             {
1139d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1140d577665bSRatan Gupta                 return;
1141d577665bSRatan Gupta             }
1142d577665bSRatan Gupta         },
1143d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1144d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1145d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1146d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1147168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1148d577665bSRatan Gupta }
1149286b9118SJohnathan Mantey 
11504f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11514f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11524f48d5f6SEd Tanous                            const bool v6Value,
11538d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1154da131a9aSJennifer Lee {
11552c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1156da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1157da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1158da131a9aSJennifer Lee             if (ec)
1159da131a9aSJennifer Lee             {
1160da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1161da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1162da131a9aSJennifer Lee                 return;
1163da131a9aSJennifer Lee             }
11648f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1165da131a9aSJennifer Lee         },
1166da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1167da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1168da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1169da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1170168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1171da131a9aSJennifer Lee }
11721f8c7b5dSJohnathan Mantey 
11734f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1174eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11758d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1176eeedda23SJohnathan Mantey {
1177eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1178eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1179eeedda23SJohnathan Mantey             if (ec)
1180eeedda23SJohnathan Mantey             {
1181eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1182eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1183eeedda23SJohnathan Mantey                 return;
1184eeedda23SJohnathan Mantey             }
1185eeedda23SJohnathan Mantey         },
1186eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1187eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1188eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1189eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1190168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1191eeedda23SJohnathan Mantey }
1192eeedda23SJohnathan Mantey 
11934f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11948d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1195da131a9aSJennifer Lee {
1196da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1197da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1198da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1199da131a9aSJennifer Lee             if (ec)
1200da131a9aSJennifer Lee             {
1201da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1202da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1203da131a9aSJennifer Lee                 return;
1204da131a9aSJennifer Lee             }
1205da131a9aSJennifer Lee         },
1206da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1207da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1208da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1209da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1210168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1211da131a9aSJennifer Lee }
1212d577665bSRatan Gupta 
12134f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12141f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1215f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1216f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12178d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1218da131a9aSJennifer Lee {
12191f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1220bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1221da131a9aSJennifer Lee 
12221f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12231f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12241f8c7b5dSJohnathan Mantey 
12251f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12261f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1227da131a9aSJennifer Lee     {
12281f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12291f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12301f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12311f8c7b5dSJohnathan Mantey         {
1232bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1233bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12341f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1235da131a9aSJennifer Lee             return;
1236da131a9aSJennifer Lee         }
12371f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12381f8c7b5dSJohnathan Mantey     }
12391f8c7b5dSJohnathan Mantey     else
1240da131a9aSJennifer Lee     {
12411f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12421f8c7b5dSJohnathan Mantey     }
12431f8c7b5dSJohnathan Mantey 
12441f8c7b5dSJohnathan Mantey     bool nextDNS{};
12451f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12461f8c7b5dSJohnathan Mantey     {
12471f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12481f8c7b5dSJohnathan Mantey         {
12491f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12501f8c7b5dSJohnathan Mantey             return;
12511f8c7b5dSJohnathan Mantey         }
12521f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12531f8c7b5dSJohnathan Mantey     }
12541f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12551f8c7b5dSJohnathan Mantey     {
12561f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12571f8c7b5dSJohnathan Mantey     }
12581f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12591f8c7b5dSJohnathan Mantey     {
12601f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12611f8c7b5dSJohnathan Mantey     }
12621f8c7b5dSJohnathan Mantey     else
12631f8c7b5dSJohnathan Mantey     {
12641f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12651f8c7b5dSJohnathan Mantey     }
12661f8c7b5dSJohnathan Mantey 
12671f8c7b5dSJohnathan Mantey     bool nextNTP{};
12681f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12691f8c7b5dSJohnathan Mantey     {
12701f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
12711f8c7b5dSJohnathan Mantey         {
12721f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12731f8c7b5dSJohnathan Mantey             return;
12741f8c7b5dSJohnathan Mantey         }
12751f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12761f8c7b5dSJohnathan Mantey     }
12771f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
12781f8c7b5dSJohnathan Mantey     {
12791f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12801f8c7b5dSJohnathan Mantey     }
12811f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
12821f8c7b5dSJohnathan Mantey     {
12831f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
12841f8c7b5dSJohnathan Mantey     }
12851f8c7b5dSJohnathan Mantey     else
12861f8c7b5dSJohnathan Mantey     {
12871f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
12881f8c7b5dSJohnathan Mantey     }
12891f8c7b5dSJohnathan Mantey 
12901f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
12911f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
12921f8c7b5dSJohnathan Mantey     {
12931f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
12941f8c7b5dSJohnathan Mantey         {
12951f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12961f8c7b5dSJohnathan Mantey             return;
12971f8c7b5dSJohnathan Mantey         }
12981f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
12991f8c7b5dSJohnathan Mantey     }
13001f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
13011f8c7b5dSJohnathan Mantey     {
13021f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13031f8c7b5dSJohnathan Mantey     }
13041f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13051f8c7b5dSJohnathan Mantey     {
13061f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13071f8c7b5dSJohnathan Mantey     }
13081f8c7b5dSJohnathan Mantey     else
13091f8c7b5dSJohnathan Mantey     {
13101f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13111f8c7b5dSJohnathan Mantey     }
13121f8c7b5dSJohnathan Mantey 
1313da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13141f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13151f8c7b5dSJohnathan Mantey                    asyncResp);
1316da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13171f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1318da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13191f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13201f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13211f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1322da131a9aSJennifer Lee }
132301784826SJohnathan Mantey 
13244f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13252c70f800SEd Tanous     getNextStaticIpEntry(
1326bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1327bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
132801784826SJohnathan Mantey {
132917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
133017a897dfSManojkiran Eda         return value.origin == "Static";
133117a897dfSManojkiran Eda     });
133201784826SJohnathan Mantey }
133301784826SJohnathan Mantey 
13344f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13352c70f800SEd Tanous     getNextStaticIpEntry(
1336bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1337bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
133801784826SJohnathan Mantey {
133917a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
134017a897dfSManojkiran Eda         return value.origin == "Static";
134117a897dfSManojkiran Eda     });
134201784826SJohnathan Mantey }
134301784826SJohnathan Mantey 
13444f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1345f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
134601784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13478d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13481abe55efSEd Tanous {
134901784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1350f476acbfSRatan Gupta     {
135171f52d96SEd Tanous         messages::propertyValueTypeError(
135271f52d96SEd Tanous             asyncResp->res,
1353bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1354d1d50814SRavi Teja             "IPv4StaticAddresses");
1355f476acbfSRatan Gupta         return;
1356f476acbfSRatan Gupta     }
1357f476acbfSRatan Gupta 
1358271584abSEd Tanous     unsigned entryIdx = 1;
135901784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
136001784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
136101784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
136201784826SJohnathan Mantey     // into the NIC.
136385ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13642c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
136501784826SJohnathan Mantey 
1366537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13671abe55efSEd Tanous     {
13684a0cb85cSEd Tanous         std::string pathString =
1369d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1370179db1d7SKowalski, Kamil 
137101784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1372f476acbfSRatan Gupta         {
1373537174c4SEd Tanous             std::optional<std::string> address;
1374537174c4SEd Tanous             std::optional<std::string> subnetMask;
1375537174c4SEd Tanous             std::optional<std::string> gateway;
1376537174c4SEd Tanous 
1377537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13787e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13797e27d832SJohnathan Mantey                                      "Gateway", gateway))
1380537174c4SEd Tanous             {
138101784826SJohnathan Mantey                 messages::propertyValueFormatError(
138271f52d96SEd Tanous                     asyncResp->res,
138371f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
138471f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
138571f52d96SEd Tanous                     pathString);
1386537174c4SEd Tanous                 return;
1387179db1d7SKowalski, Kamil             }
1388179db1d7SKowalski, Kamil 
138901784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
139001784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
139101784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
139201784826SJohnathan Mantey             // current request.
1393271584abSEd Tanous             const std::string* addr = nullptr;
1394271584abSEd Tanous             const std::string* gw = nullptr;
139501784826SJohnathan Mantey             uint8_t prefixLength = 0;
139601784826SJohnathan Mantey             bool errorInEntry = false;
1397537174c4SEd Tanous             if (address)
13981abe55efSEd Tanous             {
139901784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14001abe55efSEd Tanous                 {
140101784826SJohnathan Mantey                     addr = &(*address);
14024a0cb85cSEd Tanous                 }
140301784826SJohnathan Mantey                 else
140401784826SJohnathan Mantey                 {
1405bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1406bf648f77SEd Tanous                                                        pathString + "/Address");
140701784826SJohnathan Mantey                     errorInEntry = true;
140801784826SJohnathan Mantey                 }
140901784826SJohnathan Mantey             }
141085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
141101784826SJohnathan Mantey             {
141285ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
141301784826SJohnathan Mantey             }
141401784826SJohnathan Mantey             else
141501784826SJohnathan Mantey             {
141601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
141701784826SJohnathan Mantey                                           pathString + "/Address");
141801784826SJohnathan Mantey                 errorInEntry = true;
14194a0cb85cSEd Tanous             }
14204a0cb85cSEd Tanous 
1421537174c4SEd Tanous             if (subnetMask)
14224a0cb85cSEd Tanous             {
1423537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14244a0cb85cSEd Tanous                 {
1425f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1426537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14274a0cb85cSEd Tanous                         pathString + "/SubnetMask");
142801784826SJohnathan Mantey                     errorInEntry = true;
14294a0cb85cSEd Tanous                 }
14304a0cb85cSEd Tanous             }
143185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14324a0cb85cSEd Tanous             {
143385ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
143401784826SJohnathan Mantey                                                 &prefixLength))
14354a0cb85cSEd Tanous                 {
143601784826SJohnathan Mantey                     messages::propertyValueFormatError(
143785ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
143801784826SJohnathan Mantey                         pathString + "/SubnetMask");
143901784826SJohnathan Mantey                     errorInEntry = true;
14404a0cb85cSEd Tanous                 }
14414a0cb85cSEd Tanous             }
14421abe55efSEd Tanous             else
14431abe55efSEd Tanous             {
144401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
144501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
144601784826SJohnathan Mantey                 errorInEntry = true;
144701784826SJohnathan Mantey             }
144801784826SJohnathan Mantey 
144901784826SJohnathan Mantey             if (gateway)
145001784826SJohnathan Mantey             {
145101784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
145201784826SJohnathan Mantey                 {
145301784826SJohnathan Mantey                     gw = &(*gateway);
145401784826SJohnathan Mantey                 }
145501784826SJohnathan Mantey                 else
145601784826SJohnathan Mantey                 {
1457bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1458bf648f77SEd Tanous                                                        pathString + "/Gateway");
145901784826SJohnathan Mantey                     errorInEntry = true;
146001784826SJohnathan Mantey                 }
146101784826SJohnathan Mantey             }
146285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
146301784826SJohnathan Mantey             {
146485ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
146501784826SJohnathan Mantey             }
146601784826SJohnathan Mantey             else
14671abe55efSEd Tanous             {
1468a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14694a0cb85cSEd Tanous                                           pathString + "/Gateway");
147001784826SJohnathan Mantey                 errorInEntry = true;
14714a0cb85cSEd Tanous             }
14724a0cb85cSEd Tanous 
147301784826SJohnathan Mantey             if (errorInEntry)
14741abe55efSEd Tanous             {
147501784826SJohnathan Mantey                 return;
14764a0cb85cSEd Tanous             }
14774a0cb85cSEd Tanous 
147885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14791abe55efSEd Tanous             {
148085ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1481bf648f77SEd Tanous                                     *addr, asyncResp);
148285ffe86aSJiaqing Zhao                 nicIpEntry =
148385ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1484588c3f0dSKowalski, Kamil             }
148501784826SJohnathan Mantey             else
148601784826SJohnathan Mantey             {
1487cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1488cb13a392SEd Tanous                            asyncResp);
14894a0cb85cSEd Tanous             }
14904a0cb85cSEd Tanous             entryIdx++;
14914a0cb85cSEd Tanous         }
149201784826SJohnathan Mantey         else
149301784826SJohnathan Mantey         {
149485ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
149501784826SJohnathan Mantey             {
149601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
149701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
149801784826SJohnathan Mantey                 // in error, so bail out.
149901784826SJohnathan Mantey                 if (thisJson.is_null())
150001784826SJohnathan Mantey                 {
150101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
150201784826SJohnathan Mantey                     return;
150301784826SJohnathan Mantey                 }
150401784826SJohnathan Mantey                 messages::propertyValueFormatError(
150571f52d96SEd Tanous                     asyncResp->res,
150671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
150771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
150871f52d96SEd Tanous                     pathString);
150901784826SJohnathan Mantey                 return;
151001784826SJohnathan Mantey             }
151101784826SJohnathan Mantey 
151201784826SJohnathan Mantey             if (thisJson.is_null())
151301784826SJohnathan Mantey             {
151485ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
151501784826SJohnathan Mantey             }
151685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
151701784826SJohnathan Mantey             {
151885ffe86aSJiaqing Zhao                 nicIpEntry =
151985ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
152001784826SJohnathan Mantey             }
152101784826SJohnathan Mantey             entryIdx++;
152201784826SJohnathan Mantey         }
152301784826SJohnathan Mantey     }
15244a0cb85cSEd Tanous }
15254a0cb85cSEd Tanous 
15264f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1527f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1528f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15298d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1530f85837bfSRAJESWARAN THILLAIGOVINDAN {
1531f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1532286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1533f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1534f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1535f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1536f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1537f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1538f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1539f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1540f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1541f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1542bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1543168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1544f85837bfSRAJESWARAN THILLAIGOVINDAN }
1545f85837bfSRAJESWARAN THILLAIGOVINDAN 
15464f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1547f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
154801784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15498d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1550e48c0fc5SRavi Teja {
155101784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1552e48c0fc5SRavi Teja     {
155371f52d96SEd Tanous         messages::propertyValueTypeError(
155471f52d96SEd Tanous             asyncResp->res,
1555bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1556e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1557e48c0fc5SRavi Teja         return;
1558e48c0fc5SRavi Teja     }
1559271584abSEd Tanous     size_t entryIdx = 1;
156085ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15612c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1562f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1563e48c0fc5SRavi Teja     {
1564e48c0fc5SRavi Teja         std::string pathString =
1565e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1566e48c0fc5SRavi Teja 
156701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1568e48c0fc5SRavi Teja         {
1569e48c0fc5SRavi Teja             std::optional<std::string> address;
1570e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1571f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1572bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1573bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1574e48c0fc5SRavi Teja             {
157501784826SJohnathan Mantey                 messages::propertyValueFormatError(
157671f52d96SEd Tanous                     asyncResp->res,
157771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
157871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
157971f52d96SEd Tanous                     pathString);
1580e48c0fc5SRavi Teja                 return;
1581e48c0fc5SRavi Teja             }
1582e48c0fc5SRavi Teja 
158301784826SJohnathan Mantey             const std::string* addr;
158401784826SJohnathan Mantey             uint8_t prefix;
158501784826SJohnathan Mantey 
158601784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
158701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
158801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
158901784826SJohnathan Mantey             // current request.
1590e48c0fc5SRavi Teja             if (address)
1591e48c0fc5SRavi Teja             {
159201784826SJohnathan Mantey                 addr = &(*address);
1593e48c0fc5SRavi Teja             }
159485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
159501784826SJohnathan Mantey             {
159685ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
159701784826SJohnathan Mantey             }
159801784826SJohnathan Mantey             else
159901784826SJohnathan Mantey             {
160001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
160101784826SJohnathan Mantey                                           pathString + "/Address");
160201784826SJohnathan Mantey                 return;
1603e48c0fc5SRavi Teja             }
1604e48c0fc5SRavi Teja 
1605e48c0fc5SRavi Teja             if (prefixLength)
1606e48c0fc5SRavi Teja             {
160701784826SJohnathan Mantey                 prefix = *prefixLength;
160801784826SJohnathan Mantey             }
160985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1610e48c0fc5SRavi Teja             {
161185ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1612e48c0fc5SRavi Teja             }
1613e48c0fc5SRavi Teja             else
1614e48c0fc5SRavi Teja             {
1615e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1616e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
161701784826SJohnathan Mantey                 return;
1618e48c0fc5SRavi Teja             }
1619e48c0fc5SRavi Teja 
162085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1621e48c0fc5SRavi Teja             {
162285ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1623e48c0fc5SRavi Teja                                     asyncResp);
162485ffe86aSJiaqing Zhao                 nicIpEntry =
162585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
162601784826SJohnathan Mantey             }
162701784826SJohnathan Mantey             else
162801784826SJohnathan Mantey             {
162901784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1630e48c0fc5SRavi Teja             }
1631e48c0fc5SRavi Teja             entryIdx++;
1632e48c0fc5SRavi Teja         }
163301784826SJohnathan Mantey         else
163401784826SJohnathan Mantey         {
163585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
163601784826SJohnathan Mantey             {
163701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
163801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
163901784826SJohnathan Mantey                 // in error, so bail out.
164001784826SJohnathan Mantey                 if (thisJson.is_null())
164101784826SJohnathan Mantey                 {
164201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
164301784826SJohnathan Mantey                     return;
164401784826SJohnathan Mantey                 }
164501784826SJohnathan Mantey                 messages::propertyValueFormatError(
164671f52d96SEd Tanous                     asyncResp->res,
164771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
164871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
164971f52d96SEd Tanous                     pathString);
165001784826SJohnathan Mantey                 return;
165101784826SJohnathan Mantey             }
165201784826SJohnathan Mantey 
165301784826SJohnathan Mantey             if (thisJson.is_null())
165401784826SJohnathan Mantey             {
165585ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
165601784826SJohnathan Mantey             }
165785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
165801784826SJohnathan Mantey             {
165985ffe86aSJiaqing Zhao                 nicIpEntry =
166085ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
166101784826SJohnathan Mantey             }
166201784826SJohnathan Mantey             entryIdx++;
166301784826SJohnathan Mantey         }
166401784826SJohnathan Mantey     }
1665e48c0fc5SRavi Teja }
1666e48c0fc5SRavi Teja 
16674f48d5f6SEd Tanous inline void parseInterfaceData(
16688d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16698d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1670e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
167101784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16724a0cb85cSEd Tanous {
1673eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1674eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1675eeedda23SJohnathan Mantey 
16762c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
167781ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16782c70f800SEd Tanous     jsonResponse["@odata.id"] =
167981ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16802c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1681eeedda23SJohnathan Mantey 
1682eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1683eeedda23SJohnathan Mantey 
1684eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1685eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1686eeedda23SJohnathan Mantey                  std::vector<std::string>& resp) {
1687eeedda23SJohnathan Mantey             if (ec)
1688029573d4SEd Tanous             {
1689eeedda23SJohnathan Mantey                 return;
1690eeedda23SJohnathan Mantey             }
1691eeedda23SJohnathan Mantey 
1692eeedda23SJohnathan Mantey             health->inventory = std::move(resp);
1693eeedda23SJohnathan Mantey         },
1694eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1695eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1696bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1697bf648f77SEd Tanous         inventoryForEthernet);
1698eeedda23SJohnathan Mantey 
1699eeedda23SJohnathan Mantey     health->populate();
1700eeedda23SJohnathan Mantey 
1701eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1702eeedda23SJohnathan Mantey     {
17032c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17042c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1705029573d4SEd Tanous     }
1706029573d4SEd Tanous     else
1707029573d4SEd Tanous     {
17082c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17092c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1710029573d4SEd Tanous     }
1711aa05fb27SJohnathan Mantey 
17122c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17132c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
17142c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17152c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17161f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17172c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17182c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17192c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17201f8c7b5dSJohnathan Mantey 
17212c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17221f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17231f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17242c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17252c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17262c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17272a133282Smanojkiraneda 
17284a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17294a0cb85cSEd Tanous     {
17302c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1731ab6554f1SJoshi-Mansi 
1732ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1733ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1734ab6554f1SJoshi-Mansi         // FQDN
1735f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1736d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1737d24bfc7aSJennifer Lee         {
17382c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1739d24bfc7aSJennifer Lee         }
17402c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17414a0cb85cSEd Tanous     }
17424a0cb85cSEd Tanous 
17432c70f800SEd Tanous     jsonResponse["VLANs"] = {
1744bf648f77SEd Tanous         {"@odata.id",
1745bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1746fda13ad2SSunitha Harish 
17472c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17482c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17494a0cb85cSEd Tanous 
17502c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17512c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17522c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17532c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17542c70f800SEd Tanous     for (auto& ipv4Config : ipv4Data)
17554a0cb85cSEd Tanous     {
1756fa5053a6SGunnar Mills 
17572c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1758fa5053a6SGunnar Mills         if (gatewayStr.empty())
1759fa5053a6SGunnar Mills         {
1760fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1761fa5053a6SGunnar Mills         }
1762fa5053a6SGunnar Mills 
17632c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17642c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17652c70f800SEd Tanous                              {"Address", ipv4Config.address},
1766fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17672c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1768d1d50814SRavi Teja         {
17692c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
17702c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
17712c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1772d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1773d1d50814SRavi Teja         }
177401784826SJohnathan Mantey     }
1775d1d50814SRavi Teja 
17767ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
17777ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17787ea79e5eSRavi Teja     {
17797ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17807ea79e5eSRavi Teja     }
17817ea79e5eSRavi Teja 
17827ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1783e48c0fc5SRavi Teja 
17842c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17852c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17862c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17872c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17887f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17892c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17907f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17912c70f800SEd Tanous     for (auto& ipv6Config : ipv6Data)
1792e48c0fc5SRavi Teja     {
17932c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
17942c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
17952c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
17965fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
17972c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1798e48c0fc5SRavi Teja         {
17992c70f800SEd Tanous             ipv6StaticArray.push_back(
18002c70f800SEd Tanous                 {{"Address", ipv6Config.address},
18017a474a5fSJiaqing Zhao                  {"PrefixLength", ipv6Config.prefixLength}});
180201784826SJohnathan Mantey         }
1803e48c0fc5SRavi Teja     }
1804588c3f0dSKowalski, Kamil }
1805588c3f0dSKowalski, Kamil 
18064f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1807bf648f77SEd Tanous                                const std::string& parentIfaceId,
1808bf648f77SEd Tanous                                const std::string& ifaceId,
1809bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18101abe55efSEd Tanous {
1811bf648f77SEd Tanous     // Fill out obvious data...
1812bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1813bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1814bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1815bf648f77SEd Tanous 
1816bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1817bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1818bf648f77SEd Tanous     {
1819bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1820bf648f77SEd Tanous     }
1821bf648f77SEd Tanous }
1822bf648f77SEd Tanous 
18234f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1824bf648f77SEd Tanous {
1825bf648f77SEd Tanous     if (!boost::starts_with(iface, parent + "_"))
1826bf648f77SEd Tanous     {
1827bf648f77SEd Tanous         return false;
1828bf648f77SEd Tanous     }
1829bf648f77SEd Tanous     return true;
1830bf648f77SEd Tanous }
1831bf648f77SEd Tanous 
1832bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1833bf648f77SEd Tanous {
1834bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1835ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1836bf648f77SEd Tanous         .methods(
1837bf648f77SEd Tanous             boost::beast::http::verb::
1838bf648f77SEd Tanous                 get)([](const crow::Request&,
1839bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1840bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1841bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1842bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1843bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1844bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1845bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1846bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1847bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1848bf648f77SEd Tanous 
1849bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1850bf648f77SEd Tanous             // preparation
1851bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1852bf648f77SEd Tanous                                              const boost::container::flat_set<
1853bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1854bf648f77SEd Tanous                 if (!success)
18551abe55efSEd Tanous                 {
1856f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18579391bb9cSRapkiewicz, Pawel                     return;
18589391bb9cSRapkiewicz, Pawel                 }
18599391bb9cSRapkiewicz, Pawel 
1860bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1861bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1862bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1863bf648f77SEd Tanous                 std::string tag = "_";
1864bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1865bf648f77SEd Tanous                 {
1866bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1867bf648f77SEd Tanous                     if (found == std::string::npos)
1868bf648f77SEd Tanous                     {
1869bf648f77SEd Tanous                         ifaceArray.push_back(
1870bf648f77SEd Tanous                             {{"@odata.id",
1871bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1872bf648f77SEd Tanous                                   ifaceItem}});
1873bf648f77SEd Tanous                     }
1874bf648f77SEd Tanous                 }
1875bf648f77SEd Tanous 
1876bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1877bf648f77SEd Tanous                     ifaceArray.size();
1878bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1879bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1880bf648f77SEd Tanous             });
1881bf648f77SEd Tanous         });
1882bf648f77SEd Tanous 
1883bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1884ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1885bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1886bf648f77SEd Tanous             [](const crow::Request&,
1887bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1888bf648f77SEd Tanous                const std::string& ifaceId) {
18894a0cb85cSEd Tanous                 getEthernetIfaceData(
1890bf648f77SEd Tanous                     ifaceId,
1891bf648f77SEd Tanous                     [asyncResp,
1892bf648f77SEd Tanous                      ifaceId](const bool& success,
1893bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1894bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1895bf648f77SEd Tanous                                   ipv4Data,
1896bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1897bf648f77SEd Tanous                                   ipv6Data) {
18984a0cb85cSEd Tanous                         if (!success)
18991abe55efSEd Tanous                         {
1900bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1901bf648f77SEd Tanous                             // existing object, and other errors
1902bf648f77SEd Tanous                             messages::resourceNotFound(
1903bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19044a0cb85cSEd Tanous                             return;
19059391bb9cSRapkiewicz, Pawel                         }
19064c9afe43SEd Tanous 
19070f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1908fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1909bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1910bf648f77SEd Tanous                             "Manager Ethernet Interface";
19110f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19120f74e643SEd Tanous                             "Management Network Interface";
19130f74e643SEd Tanous 
1914bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1915bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19169391bb9cSRapkiewicz, Pawel                     });
1917bf648f77SEd Tanous             });
19189391bb9cSRapkiewicz, Pawel 
1919bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1920ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1921ed398213SEd Tanous 
1922bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1923bf648f77SEd Tanous             [](const crow::Request& req,
1924bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1925bf648f77SEd Tanous                const std::string& ifaceId) {
1926bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1927ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1928d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19299a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1930d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1931e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1932f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1933da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19341f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1935eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
19361f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19371f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19380627a2c7SEd Tanous 
19391f8c7b5dSJohnathan Mantey                 if (!json_util::readJson(
19408d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1941bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1942bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1943bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1944bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1945ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1946ab6554f1SJoshi-Mansi                         "InterfaceEnabled", interfaceEnabled))
19471abe55efSEd Tanous                 {
1948588c3f0dSKowalski, Kamil                     return;
1949588c3f0dSKowalski, Kamil                 }
1950da131a9aSJennifer Lee                 if (dhcpv4)
1951da131a9aSJennifer Lee                 {
1952bf648f77SEd Tanous                     if (!json_util::readJson(
1953bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19541f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19551f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19561f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19571f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19581f8c7b5dSJohnathan Mantey                     {
19591f8c7b5dSJohnathan Mantey                         return;
19601f8c7b5dSJohnathan Mantey                     }
19611f8c7b5dSJohnathan Mantey                 }
19621f8c7b5dSJohnathan Mantey 
19631f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19641f8c7b5dSJohnathan Mantey                 {
1965bf648f77SEd Tanous                     if (!json_util::readJson(
1966bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1967bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1968bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
1969bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
19701f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
19711f8c7b5dSJohnathan Mantey                     {
19721f8c7b5dSJohnathan Mantey                         return;
19731f8c7b5dSJohnathan Mantey                     }
1974da131a9aSJennifer Lee                 }
1975da131a9aSJennifer Lee 
1976bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
1977bf648f77SEd Tanous                 // for JSON preparation
19784a0cb85cSEd Tanous                 getEthernetIfaceData(
19792c70f800SEd Tanous                     ifaceId,
1980bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
1981ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1982d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19839a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1984e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19851f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
19861f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19871f8c7b5dSJohnathan Mantey                      v4dhcpParms = std::move(v4dhcpParms),
1988f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1989bf648f77SEd Tanous                         const bool& success,
1990bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
1991bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
1992bf648f77SEd Tanous                             ipv4Data,
1993bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
1994bf648f77SEd Tanous                             ipv6Data) {
19951abe55efSEd Tanous                         if (!success)
19961abe55efSEd Tanous                         {
1997588c3f0dSKowalski, Kamil                             // ... otherwise return error
1998bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1999bf648f77SEd Tanous                             // existing object, and other errors
2000bf648f77SEd Tanous                             messages::resourceNotFound(
2001bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2002588c3f0dSKowalski, Kamil                             return;
2003588c3f0dSKowalski, Kamil                         }
2004588c3f0dSKowalski, Kamil 
20051f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20061f8c7b5dSJohnathan Mantey                         {
2007bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2008bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20091f8c7b5dSJohnathan Mantey                         }
20101f8c7b5dSJohnathan Mantey 
20110627a2c7SEd Tanous                         if (hostname)
20121abe55efSEd Tanous                         {
20130627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20141abe55efSEd Tanous                         }
20150627a2c7SEd Tanous 
2016ab6554f1SJoshi-Mansi                         if (fqdn)
2017ab6554f1SJoshi-Mansi                         {
20182c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2019ab6554f1SJoshi-Mansi                         }
2020ab6554f1SJoshi-Mansi 
2021d577665bSRatan Gupta                         if (macAddress)
2022d577665bSRatan Gupta                         {
2023bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2024bf648f77SEd Tanous                                                   asyncResp);
2025d577665bSRatan Gupta                         }
2026d577665bSRatan Gupta 
2027d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2028d1d50814SRavi Teja                         {
2029bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2030bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2031bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2032bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2033bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2034bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2035bf648f77SEd Tanous                             // that, but could be done more efficiently
2036f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20372c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2038d1d50814SRavi Teja                                                   asyncResp);
20391abe55efSEd Tanous                         }
20400627a2c7SEd Tanous 
2041f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2042f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2043bf648f77SEd Tanous                             handleStaticNameServersPatch(
2044bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2045f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20469a6fc6feSRavi Teja 
20479a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20489a6fc6feSRavi Teja                         {
20499a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20509a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20519a6fc6feSRavi Teja                         }
2052e48c0fc5SRavi Teja 
2053e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2054e48c0fc5SRavi Teja                         {
2055f23b7296SEd Tanous                             nlohmann::json ipv6Static = *ipv6StaticAddresses;
20562c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
205701784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2058e48c0fc5SRavi Teja                         }
2059eeedda23SJohnathan Mantey 
2060eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2061eeedda23SJohnathan Mantey                         {
2062eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2063bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2064bf648f77SEd Tanous                                 asyncResp);
2065eeedda23SJohnathan Mantey                         }
2066588c3f0dSKowalski, Kamil                     });
2067bf648f77SEd Tanous             });
20689391bb9cSRapkiewicz, Pawel 
2069bf648f77SEd Tanous     BMCWEB_ROUTE(
2070bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2071ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2072ed398213SEd Tanous 
2073bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2074bf648f77SEd Tanous             [](const crow::Request& /* req */,
2075bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2076bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
20778d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
20780f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20798d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2080e439f0f8SKowalski, Kamil 
20812c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
20821abe55efSEd Tanous                 {
2083a434f2bdSEd Tanous                     return;
2084a434f2bdSEd Tanous                 }
2085a434f2bdSEd Tanous 
2086bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2087bf648f77SEd Tanous                 // for JSON preparation
20884a0cb85cSEd Tanous                 getEthernetIfaceData(
2089bf648f77SEd Tanous                     ifaceId,
2090bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2091bf648f77SEd Tanous                         const bool& success,
2092bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2093cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2094cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2095fda13ad2SSunitha Harish                         if (success && ethData.vlan_id.size() != 0)
20961abe55efSEd Tanous                         {
2097bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2098bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
20991abe55efSEd Tanous                         }
21001abe55efSEd Tanous                         else
21011abe55efSEd Tanous                         {
2102e439f0f8SKowalski, Kamil                             // ... otherwise return error
2103bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2104bf648f77SEd Tanous                             // existing object, and other errors
2105bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2106bf648f77SEd Tanous                                                        "VLAN Network Interface",
2107bf648f77SEd Tanous                                                        ifaceId);
2108e439f0f8SKowalski, Kamil                         }
2109e439f0f8SKowalski, Kamil                     });
2110bf648f77SEd Tanous             });
2111e439f0f8SKowalski, Kamil 
2112bf648f77SEd Tanous     BMCWEB_ROUTE(
2113bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2114ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2115ed398213SEd Tanous         //.privileges(redfish::privileges::patchVLanNetworkInterface)
2116432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2117bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2118bf648f77SEd Tanous             [](const crow::Request& req,
2119bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2120bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2121fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21221abe55efSEd Tanous                 {
2123bf648f77SEd Tanous                     messages::resourceNotFound(
2124bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2125927a505aSKowalski, Kamil                     return;
2126927a505aSKowalski, Kamil                 }
2127927a505aSKowalski, Kamil 
21280627a2c7SEd Tanous                 bool vlanEnable = false;
212938268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21300627a2c7SEd Tanous 
2131bf648f77SEd Tanous                 if (!json_util::readJson(req, asyncResp->res, "VLANEnable",
2132bf648f77SEd Tanous                                          vlanEnable, "VLANId", vlanId))
21331abe55efSEd Tanous                 {
2134927a505aSKowalski, Kamil                     return;
2135927a505aSKowalski, Kamil                 }
2136927a505aSKowalski, Kamil 
2137bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2138bf648f77SEd Tanous                 // for JSON preparation
2139e48c0fc5SRavi Teja                 getEthernetIfaceData(
2140bf648f77SEd Tanous                     ifaceId,
2141bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2142bf648f77SEd Tanous                         const bool& success,
2143bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2144cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2145cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
214608244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
214708244d02SSunitha Harish                         {
214808244d02SSunitha Harish                             auto callback =
2149bf648f77SEd Tanous                                 [asyncResp](
2150bf648f77SEd Tanous                                     const boost::system::error_code ec) {
215108244d02SSunitha Harish                                     if (ec)
215208244d02SSunitha Harish                                     {
215308244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
215408244d02SSunitha Harish                                     }
215508244d02SSunitha Harish                                 };
215608244d02SSunitha Harish 
215708244d02SSunitha Harish                             if (vlanEnable == true)
215808244d02SSunitha Harish                             {
215908244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2160bf648f77SEd Tanous                                     std::move(callback),
2161bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
216208244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
216308244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
216408244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
2165168e20c1SEd Tanous                                     dbus::utility::DbusVariantType(vlanId));
216608244d02SSunitha Harish                             }
216708244d02SSunitha Harish                             else
216808244d02SSunitha Harish                             {
2169bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2170bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2171e48c0fc5SRavi Teja                                        "vlan interface";
217208244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2173bf648f77SEd Tanous                                     std::move(callback),
2174bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2175bf648f77SEd Tanous                                     std::string(
2176bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2177e48c0fc5SRavi Teja                                         ifaceId,
2178bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2179bf648f77SEd Tanous                                     "Delete");
218008244d02SSunitha Harish                             }
218108244d02SSunitha Harish                         }
218208244d02SSunitha Harish                         else
21831abe55efSEd Tanous                         {
2184bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2185bf648f77SEd Tanous                             // existing object, and other errors
2186bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2187bf648f77SEd Tanous                                                        "VLAN Network Interface",
2188bf648f77SEd Tanous                                                        ifaceId);
2189bf648f77SEd Tanous                             return;
2190bf648f77SEd Tanous                         }
2191bf648f77SEd Tanous                     });
2192bf648f77SEd Tanous             });
2193bf648f77SEd Tanous 
2194bf648f77SEd Tanous     BMCWEB_ROUTE(
2195bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2196ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2197ed398213SEd Tanous         //.privileges(redfish::privileges::deleteVLanNetworkInterface)
2198432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2199bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2200bf648f77SEd Tanous             [](const crow::Request& /* req */,
2201bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2202bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2203bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2204bf648f77SEd Tanous                 {
2205e48c0fc5SRavi Teja                     messages::resourceNotFound(
2206e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2207927a505aSKowalski, Kamil                     return;
2208927a505aSKowalski, Kamil                 }
2209e439f0f8SKowalski, Kamil 
2210bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2211bf648f77SEd Tanous                 // for JSON preparation
2212f12894f8SJason M. Bills                 getEthernetIfaceData(
2213bf648f77SEd Tanous                     ifaceId,
2214bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2215bf648f77SEd Tanous                         const bool& success,
2216bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2217cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2218cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2219fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22201abe55efSEd Tanous                         {
2221f12894f8SJason M. Bills                             auto callback =
2222bf648f77SEd Tanous                                 [asyncResp](
2223bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22241abe55efSEd Tanous                                     if (ec)
22251abe55efSEd Tanous                                     {
2226f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2227927a505aSKowalski, Kamil                                     }
22284a0cb85cSEd Tanous                                 };
22294a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2230bf648f77SEd Tanous                                 std::move(callback),
2231bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2232bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2233bf648f77SEd Tanous                                     ifaceId,
22344a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22351abe55efSEd Tanous                         }
22361abe55efSEd Tanous                         else
22371abe55efSEd Tanous                         {
2238927a505aSKowalski, Kamil                             // ... otherwise return error
2239bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2240bf648f77SEd Tanous                             // existing object, and other errors
2241bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2242bf648f77SEd Tanous                                                        "VLAN Network Interface",
2243bf648f77SEd Tanous                                                        ifaceId);
2244927a505aSKowalski, Kamil                         }
2245927a505aSKowalski, Kamil                     });
2246bf648f77SEd Tanous             });
2247e439f0f8SKowalski, Kamil 
2248bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2249bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2250ed398213SEd Tanous 
2251ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2252bf648f77SEd Tanous         .methods(
2253bf648f77SEd Tanous             boost::beast::http::verb::
2254bf648f77SEd Tanous                 get)([](const crow::Request& /* req */,
2255bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2256bf648f77SEd Tanous                         const std::string& rootInterfaceName) {
22574a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
22581abe55efSEd Tanous             // preparation
2259bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
22601abe55efSEd Tanous                                      const bool& success,
2261bf648f77SEd Tanous                                      const boost::container::flat_set<
2262bf648f77SEd Tanous                                          std::string>& ifaceList) {
22634a0cb85cSEd Tanous                 if (!success)
22641abe55efSEd Tanous                 {
2265f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
22664a0cb85cSEd Tanous                     return;
22671abe55efSEd Tanous                 }
22684c9afe43SEd Tanous 
226981ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22704c9afe43SEd Tanous                 {
22714c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
22724c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
22734c9afe43SEd Tanous                                                rootInterfaceName);
22744c9afe43SEd Tanous                     return;
22754c9afe43SEd Tanous                 }
22764c9afe43SEd Tanous 
22770f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
22780f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
22790f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
22800f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
22810f74e643SEd Tanous                     "VLAN Network Interface Collection";
22824a0cb85cSEd Tanous 
22832c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
22844a0cb85cSEd Tanous 
228581ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
22861abe55efSEd Tanous                 {
22872c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
22884a0cb85cSEd Tanous                     {
2289f23b7296SEd Tanous                         std::string path =
2290f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2291f23b7296SEd Tanous                         path += rootInterfaceName;
2292f23b7296SEd Tanous                         path += "/VLANs/";
2293f23b7296SEd Tanous                         path += ifaceItem;
2294f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2295e439f0f8SKowalski, Kamil                     }
2296e439f0f8SKowalski, Kamil                 }
2297e439f0f8SKowalski, Kamil 
22984a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
22992c70f800SEd Tanous                     ifaceArray.size();
23002c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23014a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23024a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23034a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2304e439f0f8SKowalski, Kamil             });
2305bf648f77SEd Tanous         });
2306e439f0f8SKowalski, Kamil 
2307bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2308bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2309ed398213SEd Tanous         // This privilege is wrong, it should be ConfigureManager
2310ed398213SEd Tanous         //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2311432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2312bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2313bf648f77SEd Tanous             [](const crow::Request& req,
2314bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2315bf648f77SEd Tanous                const std::string& rootInterfaceName) {
2316fda13ad2SSunitha Harish                 bool vlanEnable = false;
23170627a2c7SEd Tanous                 uint32_t vlanId = 0;
23188d1b46d7Szhanghch05                 if (!json_util::readJson(req, asyncResp->res, "VLANId", vlanId,
23198d1b46d7Szhanghch05                                          "VLANEnable", vlanEnable))
23201abe55efSEd Tanous                 {
23214a0cb85cSEd Tanous                     return;
2322e439f0f8SKowalski, Kamil                 }
2323fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2324fda13ad2SSunitha Harish                 if (!vlanId)
2325fda13ad2SSunitha Harish                 {
2326fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2327fda13ad2SSunitha Harish                 }
2328fda13ad2SSunitha Harish                 if (!vlanEnable)
2329fda13ad2SSunitha Harish                 {
2330fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2331fda13ad2SSunitha Harish                 }
2332271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2333fda13ad2SSunitha Harish                 {
2334fda13ad2SSunitha Harish                     return;
2335fda13ad2SSunitha Harish                 }
2336fda13ad2SSunitha Harish 
2337bf648f77SEd Tanous                 auto callback =
2338bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23391abe55efSEd Tanous                         if (ec)
23401abe55efSEd Tanous                         {
2341bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2342bf648f77SEd Tanous                             // based on phosphor-network responses
2343f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23444a0cb85cSEd Tanous                             return;
23451abe55efSEd Tanous                         }
2346f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2347e439f0f8SKowalski, Kamil                     };
23484a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23494a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23504a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23514a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23520627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2353bf648f77SEd Tanous             });
23544a0cb85cSEd Tanous }
2355bf648f77SEd Tanous 
23569391bb9cSRapkiewicz, Pawel } // namespace redfish
2357