xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision b9d36b4791d77a47e1f3c5c4564fcdf7cc68c115)
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>
194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
21168e20c1SEd Tanous #include <dbus_utility.hpp>
22588c3f0dSKowalski, Kamil #include <error_messages.hpp>
23ed398213SEd Tanous #include <registries/privilege_registry.hpp>
241214b7e7SGunnar Mills #include <utils/json_utils.hpp>
251214b7e7SGunnar Mills 
26a24526dcSEd Tanous #include <optional>
27ab6554f1SJoshi-Mansi #include <regex>
289391bb9cSRapkiewicz, Pawel 
291abe55efSEd Tanous namespace redfish
301abe55efSEd Tanous {
319391bb9cSRapkiewicz, Pawel 
324a0cb85cSEd Tanous enum class LinkType
334a0cb85cSEd Tanous {
344a0cb85cSEd Tanous     Local,
354a0cb85cSEd Tanous     Global
364a0cb85cSEd Tanous };
379391bb9cSRapkiewicz, Pawel 
389391bb9cSRapkiewicz, Pawel /**
399391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
409391bb9cSRapkiewicz, Pawel  */
411abe55efSEd Tanous struct IPv4AddressData
421abe55efSEd Tanous {
43179db1d7SKowalski, Kamil     std::string id;
444a0cb85cSEd Tanous     std::string address;
454a0cb85cSEd Tanous     std::string domain;
464a0cb85cSEd Tanous     std::string gateway;
479391bb9cSRapkiewicz, Pawel     std::string netmask;
489391bb9cSRapkiewicz, Pawel     std::string origin;
494a0cb85cSEd Tanous     LinkType linktype;
5001c6e858SSunitha Harish     bool isActive;
514a0cb85cSEd Tanous 
521abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
531abe55efSEd Tanous     {
544a0cb85cSEd Tanous         return id < obj.id;
551abe55efSEd Tanous     }
569391bb9cSRapkiewicz, Pawel };
579391bb9cSRapkiewicz, Pawel 
589391bb9cSRapkiewicz, Pawel /**
59e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
60e48c0fc5SRavi Teja  */
61e48c0fc5SRavi Teja struct IPv6AddressData
62e48c0fc5SRavi Teja {
63e48c0fc5SRavi Teja     std::string id;
64e48c0fc5SRavi Teja     std::string address;
65e48c0fc5SRavi Teja     std::string origin;
66e48c0fc5SRavi Teja     uint8_t prefixLength;
67e48c0fc5SRavi Teja 
68e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
69e48c0fc5SRavi Teja     {
70e48c0fc5SRavi Teja         return id < obj.id;
71e48c0fc5SRavi Teja     }
72e48c0fc5SRavi Teja };
73e48c0fc5SRavi Teja /**
749391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
759391bb9cSRapkiewicz, Pawel  * available from DBus
769391bb9cSRapkiewicz, Pawel  */
771abe55efSEd Tanous struct EthernetInterfaceData
781abe55efSEd Tanous {
794a0cb85cSEd Tanous     uint32_t speed;
8035fb5311STejas Patil     size_t mtuSize;
814a0cb85cSEd Tanous     bool auto_neg;
821f8c7b5dSJohnathan Mantey     bool DNSEnabled;
831f8c7b5dSJohnathan Mantey     bool NTPEnabled;
841f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
851f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
86aa05fb27SJohnathan Mantey     bool linkUp;
87eeedda23SJohnathan Mantey     bool nicEnabled;
881f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
891f8c7b5dSJohnathan Mantey     std::string operatingMode;
904a0cb85cSEd Tanous     std::string hostname;
914a0cb85cSEd Tanous     std::string default_gateway;
929a6fc6feSRavi Teja     std::string ipv6_default_gateway;
934a0cb85cSEd Tanous     std::string mac_address;
94fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
950f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
960f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
97d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
989391bb9cSRapkiewicz, Pawel };
999391bb9cSRapkiewicz, Pawel 
1001f8c7b5dSJohnathan Mantey struct DHCPParameters
1011f8c7b5dSJohnathan Mantey {
1021f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1031f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1041f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1051f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1061f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1071f8c7b5dSJohnathan Mantey };
1081f8c7b5dSJohnathan Mantey 
1099391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1109391bb9cSRapkiewicz, Pawel // into full dot notation
1111abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1121abe55efSEd Tanous {
1139391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1149391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1159391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1169391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1179391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1189391bb9cSRapkiewicz, Pawel     return netmask;
1199391bb9cSRapkiewicz, Pawel }
1209391bb9cSRapkiewicz, Pawel 
1211f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
1221f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1231f8c7b5dSJohnathan Mantey {
1241f8c7b5dSJohnathan Mantey     if (isIPv4)
1251f8c7b5dSJohnathan Mantey     {
1261f8c7b5dSJohnathan Mantey         return (
1271f8c7b5dSJohnathan Mantey             (inputDHCP ==
1281f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1291f8c7b5dSJohnathan Mantey             (inputDHCP ==
1301f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1311f8c7b5dSJohnathan Mantey     }
1321f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1331f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1341f8c7b5dSJohnathan Mantey             (inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1361f8c7b5dSJohnathan Mantey }
1371f8c7b5dSJohnathan Mantey 
1382c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1391f8c7b5dSJohnathan Mantey {
1401f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1411f8c7b5dSJohnathan Mantey     {
1421f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1431f8c7b5dSJohnathan Mantey     }
1443174e4dfSEd Tanous     if (isIPv4)
1451f8c7b5dSJohnathan Mantey     {
1461f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1471f8c7b5dSJohnathan Mantey     }
1483174e4dfSEd Tanous     if (isIPv6)
1491f8c7b5dSJohnathan Mantey     {
1501f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1511f8c7b5dSJohnathan Mantey     }
1521f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1531f8c7b5dSJohnathan Mantey }
1541f8c7b5dSJohnathan Mantey 
1554a0cb85cSEd Tanous inline std::string
1564a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1574a0cb85cSEd Tanous                                         bool isIPv4)
1581abe55efSEd Tanous {
1594a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1601abe55efSEd Tanous     {
1614a0cb85cSEd Tanous         return "Static";
1629391bb9cSRapkiewicz, Pawel     }
1634a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1641abe55efSEd Tanous     {
1654a0cb85cSEd Tanous         if (isIPv4)
1661abe55efSEd Tanous         {
1674a0cb85cSEd Tanous             return "IPv4LinkLocal";
1681abe55efSEd Tanous         }
1694a0cb85cSEd Tanous         return "LinkLocal";
1709391bb9cSRapkiewicz, Pawel     }
1714a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1721abe55efSEd Tanous     {
1734a0cb85cSEd Tanous         if (isIPv4)
1744a0cb85cSEd Tanous         {
1754a0cb85cSEd Tanous             return "DHCP";
1764a0cb85cSEd Tanous         }
1774a0cb85cSEd Tanous         return "DHCPv6";
1784a0cb85cSEd Tanous     }
1794a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1804a0cb85cSEd Tanous     {
1814a0cb85cSEd Tanous         return "SLAAC";
1824a0cb85cSEd Tanous     }
1834a0cb85cSEd Tanous     return "";
1844a0cb85cSEd Tanous }
1854a0cb85cSEd Tanous 
186711ac7a9SEd Tanous inline bool
187711ac7a9SEd Tanous     extractEthernetInterfaceData(const std::string& ethifaceId,
188711ac7a9SEd Tanous                                  dbus::utility::ManagedObjectType& dbusData,
1894a0cb85cSEd Tanous                                  EthernetInterfaceData& ethData)
1904a0cb85cSEd Tanous {
1914c9afe43SEd Tanous     bool idFound = false;
19281ce609eSEd Tanous     for (auto& objpath : dbusData)
1934a0cb85cSEd Tanous     {
194f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
1954a0cb85cSEd Tanous         {
19681ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
197029573d4SEd Tanous             {
1984c9afe43SEd Tanous                 idFound = true;
1994a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2004a0cb85cSEd Tanous                 {
2014a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2024a0cb85cSEd Tanous                     {
2034a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2044a0cb85cSEd Tanous                         {
2054a0cb85cSEd Tanous                             const std::string* mac =
206abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2074a0cb85cSEd Tanous                             if (mac != nullptr)
2084a0cb85cSEd Tanous                             {
2094a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2104a0cb85cSEd Tanous                             }
2114a0cb85cSEd Tanous                         }
2124a0cb85cSEd Tanous                     }
2134a0cb85cSEd Tanous                 }
2144a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2154a0cb85cSEd Tanous                 {
2164a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2174a0cb85cSEd Tanous                     {
2184a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2194a0cb85cSEd Tanous                         {
2201b6b96c5SEd Tanous                             const uint32_t* id =
221abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2224a0cb85cSEd Tanous                             if (id != nullptr)
2234a0cb85cSEd Tanous                             {
224fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2254a0cb85cSEd Tanous                             }
2264a0cb85cSEd Tanous                         }
2274a0cb85cSEd Tanous                     }
2284a0cb85cSEd Tanous                 }
2294a0cb85cSEd Tanous                 else if (ifacePair.first ==
2304a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2314a0cb85cSEd Tanous                 {
2324a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2334a0cb85cSEd Tanous                     {
2344a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2354a0cb85cSEd Tanous                         {
2362c70f800SEd Tanous                             const bool* autoNeg =
237abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2382c70f800SEd Tanous                             if (autoNeg != nullptr)
2394a0cb85cSEd Tanous                             {
2402c70f800SEd Tanous                                 ethData.auto_neg = *autoNeg;
2414a0cb85cSEd Tanous                             }
2424a0cb85cSEd Tanous                         }
2434a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2444a0cb85cSEd Tanous                         {
2454a0cb85cSEd Tanous                             const uint32_t* speed =
246abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2474a0cb85cSEd Tanous                             if (speed != nullptr)
2484a0cb85cSEd Tanous                             {
2494a0cb85cSEd Tanous                                 ethData.speed = *speed;
2504a0cb85cSEd Tanous                             }
2514a0cb85cSEd Tanous                         }
25235fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25335fb5311STejas Patil                         {
25435fb5311STejas Patil                             const uint32_t* mtuSize =
25535fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25635fb5311STejas Patil                             if (mtuSize != nullptr)
25735fb5311STejas Patil                             {
25835fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
25935fb5311STejas Patil                             }
26035fb5311STejas Patil                         }
261aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
262aa05fb27SJohnathan Mantey                         {
263aa05fb27SJohnathan Mantey                             const bool* linkUp =
264aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
265aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
266aa05fb27SJohnathan Mantey                             {
267aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
268aa05fb27SJohnathan Mantey                             }
269aa05fb27SJohnathan Mantey                         }
270eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
271eeedda23SJohnathan Mantey                         {
272eeedda23SJohnathan Mantey                             const bool* nicEnabled =
273eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
274eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
275eeedda23SJohnathan Mantey                             {
276eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
277eeedda23SJohnathan Mantey                             }
278eeedda23SJohnathan Mantey                         }
279f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
280029573d4SEd Tanous                         {
281029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2828d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
283029573d4SEd Tanous                                     &propertyPair.second);
284029573d4SEd Tanous                             if (nameservers != nullptr)
285029573d4SEd Tanous                             {
286f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2870f6efdc1Smanojkiran.eda@gmail.com                             }
2880f6efdc1Smanojkiran.eda@gmail.com                         }
2890f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2900f6efdc1Smanojkiran.eda@gmail.com                         {
2910f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2928d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2930f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2940f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2950f6efdc1Smanojkiran.eda@gmail.com                             {
296f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2974a0cb85cSEd Tanous                             }
2984a0cb85cSEd Tanous                         }
2992a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3002a133282Smanojkiraneda                         {
3012c70f800SEd Tanous                             const std::string* dhcpEnabled =
3021f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3032c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3042a133282Smanojkiraneda                             {
3052c70f800SEd Tanous                                 ethData.DHCPEnabled = *dhcpEnabled;
3062a133282Smanojkiraneda                             }
3072a133282Smanojkiraneda                         }
308d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
309d24bfc7aSJennifer Lee                         {
310d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3118d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
312d24bfc7aSJennifer Lee                                     &propertyPair.second);
313d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
314d24bfc7aSJennifer Lee                             {
315f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
316d24bfc7aSJennifer Lee                             }
317d24bfc7aSJennifer Lee                         }
3189010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3199010ec2eSRavi Teja                         {
3209010ec2eSRavi Teja                             const std::string* defaultGateway =
3219010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3229010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3239010ec2eSRavi Teja                             {
3249010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3259010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3269010ec2eSRavi Teja                                 {
3279010ec2eSRavi Teja                                     ethData.default_gateway = "0.0.0.0";
3289010ec2eSRavi Teja                                 }
3299010ec2eSRavi Teja                                 else
3309010ec2eSRavi Teja                                 {
3319010ec2eSRavi Teja                                     ethData.default_gateway = defaultGatewayStr;
3329010ec2eSRavi Teja                                 }
3339010ec2eSRavi Teja                             }
3349010ec2eSRavi Teja                         }
3359010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3369010ec2eSRavi Teja                         {
3379010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3389010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3399010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3409010ec2eSRavi Teja                             {
3419010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3429010ec2eSRavi Teja                                     *defaultGateway6;
3439010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3449010ec2eSRavi Teja                                 {
3459010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3469010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3479010ec2eSRavi Teja                                 }
3489010ec2eSRavi Teja                                 else
3499010ec2eSRavi Teja                                 {
3509010ec2eSRavi Teja                                     ethData.ipv6_default_gateway =
3519010ec2eSRavi Teja                                         defaultGateway6Str;
3529010ec2eSRavi Teja                                 }
3539010ec2eSRavi Teja                             }
3549010ec2eSRavi Teja                         }
355029573d4SEd Tanous                     }
356029573d4SEd Tanous                 }
357029573d4SEd Tanous             }
3581f8c7b5dSJohnathan Mantey 
3591f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3601f8c7b5dSJohnathan Mantey             {
3611f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3621f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3631f8c7b5dSJohnathan Mantey                 {
3641f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3651f8c7b5dSJohnathan Mantey                     {
3661f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3671f8c7b5dSJohnathan Mantey                         {
3682c70f800SEd Tanous                             const bool* dnsEnabled =
3691f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3702c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3711f8c7b5dSJohnathan Mantey                             {
3722c70f800SEd Tanous                                 ethData.DNSEnabled = *dnsEnabled;
3731f8c7b5dSJohnathan Mantey                             }
3741f8c7b5dSJohnathan Mantey                         }
3751f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3761f8c7b5dSJohnathan Mantey                         {
3772c70f800SEd Tanous                             const bool* ntpEnabled =
3781f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3792c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3801f8c7b5dSJohnathan Mantey                             {
3812c70f800SEd Tanous                                 ethData.NTPEnabled = *ntpEnabled;
3821f8c7b5dSJohnathan Mantey                             }
3831f8c7b5dSJohnathan Mantey                         }
3841f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3851f8c7b5dSJohnathan Mantey                         {
3862c70f800SEd Tanous                             const bool* hostNameEnabled =
3871f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3882c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3891f8c7b5dSJohnathan Mantey                             {
3902c70f800SEd Tanous                                 ethData.HostNameEnabled = *hostNameEnabled;
3911f8c7b5dSJohnathan Mantey                             }
3921f8c7b5dSJohnathan Mantey                         }
3931f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3941f8c7b5dSJohnathan Mantey                         {
3952c70f800SEd Tanous                             const bool* sendHostNameEnabled =
3961f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3972c70f800SEd Tanous                             if (sendHostNameEnabled != nullptr)
3981f8c7b5dSJohnathan Mantey                             {
3991f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
4002c70f800SEd Tanous                                     *sendHostNameEnabled;
4011f8c7b5dSJohnathan Mantey                             }
4021f8c7b5dSJohnathan Mantey                         }
4031f8c7b5dSJohnathan Mantey                     }
4041f8c7b5dSJohnathan Mantey                 }
4051f8c7b5dSJohnathan Mantey             }
406029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
407029573d4SEd Tanous             // to check eth number
408029573d4SEd Tanous             if (ifacePair.first ==
4094a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4104a0cb85cSEd Tanous             {
4114a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4124a0cb85cSEd Tanous                 {
4134a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4144a0cb85cSEd Tanous                     {
4154a0cb85cSEd Tanous                         const std::string* hostname =
4168d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4174a0cb85cSEd Tanous                         if (hostname != nullptr)
4184a0cb85cSEd Tanous                         {
4194a0cb85cSEd Tanous                             ethData.hostname = *hostname;
4204a0cb85cSEd Tanous                         }
4214a0cb85cSEd Tanous                     }
4224a0cb85cSEd Tanous                 }
4234a0cb85cSEd Tanous             }
4244a0cb85cSEd Tanous         }
4254a0cb85cSEd Tanous     }
4264c9afe43SEd Tanous     return idFound;
4274a0cb85cSEd Tanous }
4284a0cb85cSEd Tanous 
429e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
43001784826SJohnathan Mantey inline void
43181ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
432711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43381ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
434e48c0fc5SRavi Teja {
435e48c0fc5SRavi Teja     const std::string ipv6PathStart =
43681ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
437e48c0fc5SRavi Teja 
438e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
439e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
44081ce609eSEd Tanous     for (const auto& objpath : dbusData)
441e48c0fc5SRavi Teja     {
442e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
443e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
444e48c0fc5SRavi Teja         {
4459eb808c1SEd Tanous             for (const auto& interface : objpath.second)
446e48c0fc5SRavi Teja             {
447e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
448e48c0fc5SRavi Teja                 {
449e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
450e48c0fc5SRavi Teja                     // appropriate
451e48c0fc5SRavi Teja                     std::pair<
452e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
453e48c0fc5SRavi Teja                         bool>
45481ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4552c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4562c70f800SEd Tanous                     ipv6Address.id =
457271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4589eb808c1SEd Tanous                     for (const auto& property : interface.second)
459e48c0fc5SRavi Teja                     {
460e48c0fc5SRavi Teja                         if (property.first == "Address")
461e48c0fc5SRavi Teja                         {
462e48c0fc5SRavi Teja                             const std::string* address =
463e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
464e48c0fc5SRavi Teja                             if (address != nullptr)
465e48c0fc5SRavi Teja                             {
4662c70f800SEd Tanous                                 ipv6Address.address = *address;
467e48c0fc5SRavi Teja                             }
468e48c0fc5SRavi Teja                         }
469e48c0fc5SRavi Teja                         else if (property.first == "Origin")
470e48c0fc5SRavi Teja                         {
471e48c0fc5SRavi Teja                             const std::string* origin =
472e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
473e48c0fc5SRavi Teja                             if (origin != nullptr)
474e48c0fc5SRavi Teja                             {
4752c70f800SEd Tanous                                 ipv6Address.origin =
476e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
477e48c0fc5SRavi Teja                                                                         false);
478e48c0fc5SRavi Teja                             }
479e48c0fc5SRavi Teja                         }
480e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
481e48c0fc5SRavi Teja                         {
482e48c0fc5SRavi Teja                             const uint8_t* prefix =
483e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
484e48c0fc5SRavi Teja                             if (prefix != nullptr)
485e48c0fc5SRavi Teja                             {
4862c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
487e48c0fc5SRavi Teja                             }
488e48c0fc5SRavi Teja                         }
489889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
490889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
491889ff694SAsmitha Karunanithi                         {
492889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
493889ff694SAsmitha Karunanithi                         }
494e48c0fc5SRavi Teja                         else
495e48c0fc5SRavi Teja                         {
496e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
497e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
498e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
499e48c0fc5SRavi Teja                         }
500e48c0fc5SRavi Teja                     }
501e48c0fc5SRavi Teja                 }
502e48c0fc5SRavi Teja             }
503e48c0fc5SRavi Teja         }
504e48c0fc5SRavi Teja     }
505e48c0fc5SRavi Teja }
506e48c0fc5SRavi Teja 
5074a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50801784826SJohnathan Mantey inline void
50981ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
510711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
51181ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5124a0cb85cSEd Tanous {
5134a0cb85cSEd Tanous     const std::string ipv4PathStart =
51481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5154a0cb85cSEd Tanous 
5164a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5174a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
51881ce609eSEd Tanous     for (const auto& objpath : dbusData)
5194a0cb85cSEd Tanous     {
5204a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5214a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5224a0cb85cSEd Tanous         {
5239eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5244a0cb85cSEd Tanous             {
5254a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5264a0cb85cSEd Tanous                 {
5274a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5284a0cb85cSEd Tanous                     // appropriate
5294a0cb85cSEd Tanous                     std::pair<
5304a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5314a0cb85cSEd Tanous                         bool>
53281ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5332c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5342c70f800SEd Tanous                     ipv4Address.id =
535271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5369eb808c1SEd Tanous                     for (const auto& property : interface.second)
5374a0cb85cSEd Tanous                     {
5384a0cb85cSEd Tanous                         if (property.first == "Address")
5394a0cb85cSEd Tanous                         {
5404a0cb85cSEd Tanous                             const std::string* address =
541abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5424a0cb85cSEd Tanous                             if (address != nullptr)
5434a0cb85cSEd Tanous                             {
5442c70f800SEd Tanous                                 ipv4Address.address = *address;
5454a0cb85cSEd Tanous                             }
5464a0cb85cSEd Tanous                         }
5474a0cb85cSEd Tanous                         else if (property.first == "Origin")
5484a0cb85cSEd Tanous                         {
5494a0cb85cSEd Tanous                             const std::string* origin =
550abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5514a0cb85cSEd Tanous                             if (origin != nullptr)
5524a0cb85cSEd Tanous                             {
5532c70f800SEd Tanous                                 ipv4Address.origin =
5544a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5554a0cb85cSEd Tanous                                                                         true);
5564a0cb85cSEd Tanous                             }
5574a0cb85cSEd Tanous                         }
5584a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5594a0cb85cSEd Tanous                         {
5604a0cb85cSEd Tanous                             const uint8_t* mask =
561abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5624a0cb85cSEd Tanous                             if (mask != nullptr)
5634a0cb85cSEd Tanous                             {
5644a0cb85cSEd Tanous                                 // convert it to the string
5652c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5664a0cb85cSEd Tanous                             }
5674a0cb85cSEd Tanous                         }
568889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
569889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
570889ff694SAsmitha Karunanithi                         {
571889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
572889ff694SAsmitha Karunanithi                         }
5734a0cb85cSEd Tanous                         else
5744a0cb85cSEd Tanous                         {
5754a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5764a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5774a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5784a0cb85cSEd Tanous                         }
5794a0cb85cSEd Tanous                     }
5804a0cb85cSEd Tanous                     // Check if given address is local, or global
5812c70f800SEd Tanous                     ipv4Address.linktype =
5822c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
58318659d10SJohnathan Mantey                             ? LinkType::Local
58418659d10SJohnathan Mantey                             : LinkType::Global;
5854a0cb85cSEd Tanous                 }
5864a0cb85cSEd Tanous             }
5874a0cb85cSEd Tanous         }
5884a0cb85cSEd Tanous     }
5894a0cb85cSEd Tanous }
590588c3f0dSKowalski, Kamil 
591588c3f0dSKowalski, Kamil /**
592588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
593588c3f0dSKowalski, Kamil  *
594588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
595588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
596588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
597588c3f0dSKowalski, Kamil  *
598588c3f0dSKowalski, Kamil  * @return None.
599588c3f0dSKowalski, Kamil  */
600588c3f0dSKowalski, Kamil template <typename CallbackFunc>
6014a0cb85cSEd Tanous void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
6021abe55efSEd Tanous                   CallbackFunc&& callback)
6031abe55efSEd Tanous {
60455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
605588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
606588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
607588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
608588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
609168e20c1SEd Tanous         dbus::utility::DbusVariantType(inputVlanId));
6104a0cb85cSEd Tanous }
611588c3f0dSKowalski, Kamil 
612588c3f0dSKowalski, Kamil /**
613179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
614179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
615179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
616179db1d7SKowalski, Kamil  *
617179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
618179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
619179db1d7SKowalski, Kamil  *
620179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
621179db1d7SKowalski, Kamil  */
6224a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
6231abe55efSEd Tanous                                        uint8_t* bits = nullptr)
6241abe55efSEd Tanous {
625179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
626179db1d7SKowalski, Kamil 
627179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
628179db1d7SKowalski, Kamil 
6294a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6301abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6311abe55efSEd Tanous     {
632179db1d7SKowalski, Kamil         return false;
633179db1d7SKowalski, Kamil     }
634179db1d7SKowalski, Kamil 
6351abe55efSEd Tanous     if (bits != nullptr)
6361abe55efSEd Tanous     {
637179db1d7SKowalski, Kamil         *bits = 0;
638179db1d7SKowalski, Kamil     }
639179db1d7SKowalski, Kamil 
640543f4400SEd Tanous     char* endPtr = nullptr;
641179db1d7SKowalski, Kamil     long previousValue = 255;
642543f4400SEd Tanous     bool firstZeroInByteHit = false;
6431abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6441abe55efSEd Tanous     {
6451abe55efSEd Tanous         if (byte.empty())
6461abe55efSEd Tanous         {
6471db9ca37SKowalski, Kamil             return false;
6481db9ca37SKowalski, Kamil         }
6491db9ca37SKowalski, Kamil 
650179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6511db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
652179db1d7SKowalski, Kamil 
6534a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6544a0cb85cSEd Tanous         // is not 100% number
6551abe55efSEd Tanous         if (*endPtr != '\0')
6561abe55efSEd Tanous         {
657179db1d7SKowalski, Kamil             return false;
658179db1d7SKowalski, Kamil         }
659179db1d7SKowalski, Kamil 
660179db1d7SKowalski, Kamil         // Value should be contained in byte
6611abe55efSEd Tanous         if (value < 0 || value > 255)
6621abe55efSEd Tanous         {
663179db1d7SKowalski, Kamil             return false;
664179db1d7SKowalski, Kamil         }
665179db1d7SKowalski, Kamil 
6661abe55efSEd Tanous         if (bits != nullptr)
6671abe55efSEd Tanous         {
668179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6691abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6701abe55efSEd Tanous             {
671179db1d7SKowalski, Kamil                 return false;
672179db1d7SKowalski, Kamil             }
673179db1d7SKowalski, Kamil 
674179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
675179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
676179db1d7SKowalski, Kamil 
677179db1d7SKowalski, Kamil             // Count bits
67823a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6791abe55efSEd Tanous             {
680e662eae8SEd Tanous                 if ((value & (1L << bitIdx)) != 0)
6811abe55efSEd Tanous                 {
6821abe55efSEd Tanous                     if (firstZeroInByteHit)
6831abe55efSEd Tanous                     {
684179db1d7SKowalski, Kamil                         // Continuity not preserved
685179db1d7SKowalski, Kamil                         return false;
6861abe55efSEd Tanous                     }
687179db1d7SKowalski, Kamil                     (*bits)++;
688179db1d7SKowalski, Kamil                 }
6891abe55efSEd Tanous                 else
6901abe55efSEd Tanous                 {
691179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
692179db1d7SKowalski, Kamil                 }
693179db1d7SKowalski, Kamil             }
694179db1d7SKowalski, Kamil         }
695179db1d7SKowalski, Kamil 
696179db1d7SKowalski, Kamil         previousValue = value;
697179db1d7SKowalski, Kamil     }
698179db1d7SKowalski, Kamil 
699179db1d7SKowalski, Kamil     return true;
700179db1d7SKowalski, Kamil }
701179db1d7SKowalski, Kamil 
702179db1d7SKowalski, Kamil /**
70301784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
704179db1d7SKowalski, Kamil  *
705179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
706179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
707179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
708179db1d7SKowalski, Kamil  *
709179db1d7SKowalski, Kamil  * @return None
710179db1d7SKowalski, Kamil  */
7114a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
7128d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7131abe55efSEd Tanous {
71455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
715286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7161abe55efSEd Tanous             if (ec)
7171abe55efSEd Tanous             {
718a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7191abe55efSEd Tanous             }
720179db1d7SKowalski, Kamil         },
721179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
722179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
723179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
724179db1d7SKowalski, Kamil }
725179db1d7SKowalski, Kamil 
726244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
727244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
728244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7299010ec2eSRavi Teja {
7309010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7319010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7329010ec2eSRavi Teja             if (ec)
7339010ec2eSRavi Teja             {
7349010ec2eSRavi Teja                 messages::internalError(asyncResp->res);
7359010ec2eSRavi Teja                 return;
7369010ec2eSRavi Teja             }
7379010ec2eSRavi Teja             asyncResp->res.result(boost::beast::http::status::no_content);
7389010ec2eSRavi Teja         },
7399010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7409010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7419010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7429010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
743168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7449010ec2eSRavi Teja }
745179db1d7SKowalski, Kamil /**
74601784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
747179db1d7SKowalski, Kamil  *
74801784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74901784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75001784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75101784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
752179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
753179db1d7SKowalski, Kamil  *
754179db1d7SKowalski, Kamil  * @return None
755179db1d7SKowalski, Kamil  */
756cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
757cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7588d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7591abe55efSEd Tanous {
7609010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7619010ec2eSRavi Teja                             gateway](const boost::system::error_code ec) {
7621abe55efSEd Tanous         if (ec)
7631abe55efSEd Tanous         {
764a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7659010ec2eSRavi Teja             return;
766179db1d7SKowalski, Kamil         }
7679010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7689010ec2eSRavi Teja     };
7699010ec2eSRavi Teja 
7709010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7719010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
772179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
773179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
77401784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
775179db1d7SKowalski, Kamil         gateway);
776179db1d7SKowalski, Kamil }
777e48c0fc5SRavi Teja 
778e48c0fc5SRavi Teja /**
77901784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
78001784826SJohnathan Mantey  * static IPv4 entry
78101784826SJohnathan Mantey  *
78201784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
78301784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
78401784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
78501784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
78601784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
78701784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
78801784826SJohnathan Mantey  *
78901784826SJohnathan Mantey  * @return None
79001784826SJohnathan Mantey  */
7918d1b46d7Szhanghch05 inline void
7928d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7938d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
79401784826SJohnathan Mantey                         const std::string& address,
7958d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
79601784826SJohnathan Mantey {
79701784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
79801784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
79901784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
80001784826SJohnathan Mantey             if (ec)
80101784826SJohnathan Mantey             {
80201784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
8039010ec2eSRavi Teja                 return;
80401784826SJohnathan Mantey             }
8059010ec2eSRavi Teja 
80601784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
8079010ec2eSRavi Teja                 [asyncResp, ifaceId,
8089010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
80923a21a1cSEd Tanous                     if (ec2)
81001784826SJohnathan Mantey                     {
81101784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
8129010ec2eSRavi Teja                         return;
81301784826SJohnathan Mantey                     }
8149010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
81501784826SJohnathan Mantey                 },
81601784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
81701784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
81801784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
81901784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
82001784826SJohnathan Mantey                 prefixLength, gateway);
82101784826SJohnathan Mantey         },
82201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
82301784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
82401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
82501784826SJohnathan Mantey }
82601784826SJohnathan Mantey 
82701784826SJohnathan Mantey /**
828e48c0fc5SRavi Teja  * @brief Deletes given IPv6
829e48c0fc5SRavi Teja  *
830e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
831e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
832e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
833e48c0fc5SRavi Teja  *
834e48c0fc5SRavi Teja  * @return None
835e48c0fc5SRavi Teja  */
836e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8378d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
838e48c0fc5SRavi Teja {
839e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
840286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
841e48c0fc5SRavi Teja             if (ec)
842e48c0fc5SRavi Teja             {
843e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
844e48c0fc5SRavi Teja             }
845e48c0fc5SRavi Teja         },
846e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
847e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
848e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
849e48c0fc5SRavi Teja }
850e48c0fc5SRavi Teja 
851e48c0fc5SRavi Teja /**
85201784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
85301784826SJohnathan Mantey  * static IPv6 entry
85401784826SJohnathan Mantey  *
85501784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
85601784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
85701784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
85801784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
85901784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
86001784826SJohnathan Mantey  *
86101784826SJohnathan Mantey  * @return None
86201784826SJohnathan Mantey  */
8638d1b46d7Szhanghch05 inline void
8648d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8658d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8668d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86701784826SJohnathan Mantey {
86801784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
86901784826SJohnathan Mantey         [asyncResp, ifaceId, address,
87001784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
87101784826SJohnathan Mantey             if (ec)
87201784826SJohnathan Mantey             {
87301784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
87401784826SJohnathan Mantey             }
87501784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
87623a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
87723a21a1cSEd Tanous                     if (ec2)
87801784826SJohnathan Mantey                     {
87901784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
88001784826SJohnathan Mantey                     }
88101784826SJohnathan Mantey                 },
88201784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
88301784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
88401784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
88501784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
88601784826SJohnathan Mantey                 prefixLength, "");
88701784826SJohnathan Mantey         },
88801784826SJohnathan Mantey         "xyz.openbmc_project.Network",
88901784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
89001784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
89101784826SJohnathan Mantey }
89201784826SJohnathan Mantey 
89301784826SJohnathan Mantey /**
894e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
895e48c0fc5SRavi Teja  *
896e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
897e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
898e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
899e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
900e48c0fc5SRavi Teja  *
901e48c0fc5SRavi Teja  * @return None
902e48c0fc5SRavi Teja  */
90301784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
90401784826SJohnathan Mantey                        const std::string& address,
9058d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
906e48c0fc5SRavi Teja {
907e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
908e48c0fc5SRavi Teja         if (ec)
909e48c0fc5SRavi Teja         {
910e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
911e48c0fc5SRavi Teja         }
912e48c0fc5SRavi Teja     };
913e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
9144e0453b1SGunnar Mills     // does not have associated gateway property
915e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
916e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
917e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
918e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
919e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
920e48c0fc5SRavi Teja         "");
921e48c0fc5SRavi Teja }
922e48c0fc5SRavi Teja 
923179db1d7SKowalski, Kamil /**
924179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
925179db1d7SKowalski, Kamil  * Object
926179db1d7SKowalski, Kamil  * from EntityManager Network Manager
9274a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
928179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
929179db1d7SKowalski, Kamil  * into JSON
930179db1d7SKowalski, Kamil  */
931179db1d7SKowalski, Kamil template <typename CallbackFunc>
93281ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9331abe55efSEd Tanous                           CallbackFunc&& callback)
9341abe55efSEd Tanous {
93555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
936f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
937f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
93881ce609eSEd Tanous             const boost::system::error_code errorCode,
939711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
94055c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9414a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
942e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
943179db1d7SKowalski, Kamil 
94481ce609eSEd Tanous             if (errorCode)
9451abe55efSEd Tanous             {
94601784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
947179db1d7SKowalski, Kamil                 return;
948179db1d7SKowalski, Kamil             }
949179db1d7SKowalski, Kamil 
9504c9afe43SEd Tanous             bool found =
9512c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9524c9afe43SEd Tanous             if (!found)
9534c9afe43SEd Tanous             {
95401784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9554c9afe43SEd Tanous                 return;
9564c9afe43SEd Tanous             }
9574c9afe43SEd Tanous 
9582c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
959179db1d7SKowalski, Kamil             // Fix global GW
9601abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9611abe55efSEd Tanous             {
962c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
963c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9649010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9651abe55efSEd Tanous                 {
9664a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
967179db1d7SKowalski, Kamil                 }
968179db1d7SKowalski, Kamil             }
969179db1d7SKowalski, Kamil 
9702c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9714e0453b1SGunnar Mills             // Finally make a callback with useful data
97201784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
973179db1d7SKowalski, Kamil         },
974179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
975179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
976271584abSEd Tanous }
977179db1d7SKowalski, Kamil 
978179db1d7SKowalski, Kamil /**
9799391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9809391bb9cSRapkiewicz, Pawel  * Manager
9811abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9821abe55efSEd Tanous  * into JSON.
9839391bb9cSRapkiewicz, Pawel  */
9849391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9851abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9861abe55efSEd Tanous {
98755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
988f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
98981ce609eSEd Tanous             const boost::system::error_code errorCode,
990711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9911abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9921abe55efSEd Tanous             // ethernet interfaces
9932c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9942c70f800SEd Tanous             ifaceList.reserve(resp.size());
99581ce609eSEd Tanous             if (errorCode)
9961abe55efSEd Tanous             {
9972c70f800SEd Tanous                 callback(false, ifaceList);
9989391bb9cSRapkiewicz, Pawel                 return;
9999391bb9cSRapkiewicz, Pawel             }
10009391bb9cSRapkiewicz, Pawel 
10019391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
10024a0cb85cSEd Tanous             for (const auto& objpath : resp)
10031abe55efSEd Tanous             {
10049391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
10054a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
10061abe55efSEd Tanous                 {
10071abe55efSEd Tanous                     // If interface is
10084a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
10094a0cb85cSEd Tanous                     // what we're looking for.
10109391bb9cSRapkiewicz, Pawel                     if (interface.first ==
10111abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
10121abe55efSEd Tanous                     {
10132dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
10142dfd18efSEd Tanous                         if (ifaceId.empty())
10151abe55efSEd Tanous                         {
10162dfd18efSEd Tanous                             continue;
10179391bb9cSRapkiewicz, Pawel                         }
10182dfd18efSEd Tanous                         // and put it into output vector.
10192dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
10209391bb9cSRapkiewicz, Pawel                     }
10219391bb9cSRapkiewicz, Pawel                 }
10229391bb9cSRapkiewicz, Pawel             }
1023a434f2bdSEd Tanous             // Finally make a callback with useful data
10242c70f800SEd Tanous             callback(true, ifaceList);
10259391bb9cSRapkiewicz, Pawel         },
1026aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1027aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
1028271584abSEd Tanous }
10299391bb9cSRapkiewicz, Pawel 
10304f48d5f6SEd Tanous inline void
10314f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10328d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10331abe55efSEd Tanous {
1034ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1035ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1036ab6554f1SJoshi-Mansi     {
1037ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1038ab6554f1SJoshi-Mansi                                            "HostName");
1039ab6554f1SJoshi-Mansi         return;
1040ab6554f1SJoshi-Mansi     }
1041bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1042bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10434a0cb85cSEd Tanous             if (ec)
10444a0cb85cSEd Tanous             {
1045a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10461abe55efSEd Tanous             }
1047bc0bd6e0SEd Tanous         },
1048bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1049bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1050bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1051168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1052588c3f0dSKowalski, Kamil }
1053588c3f0dSKowalski, Kamil 
10544f48d5f6SEd Tanous inline void
105535fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
105635fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
105735fb5311STejas Patil {
105835fb5311STejas Patil     sdbusplus::message::object_path objPath =
105935fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
106035fb5311STejas Patil     crow::connections::systemBus->async_method_call(
106135fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
106235fb5311STejas Patil             if (ec)
106335fb5311STejas Patil             {
106435fb5311STejas Patil                 messages::internalError(asyncResp->res);
106535fb5311STejas Patil             }
106635fb5311STejas Patil         },
106735fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
106835fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
106935fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
107035fb5311STejas Patil         std::variant<size_t>(mtuSize));
107135fb5311STejas Patil }
107235fb5311STejas Patil 
107335fb5311STejas Patil inline void
10744f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1075bf648f77SEd Tanous                           const std::string& domainname,
10768d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1077ab6554f1SJoshi-Mansi {
1078ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1079ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1080ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1081ab6554f1SJoshi-Mansi             if (ec)
1082ab6554f1SJoshi-Mansi             {
1083ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1084ab6554f1SJoshi-Mansi             }
1085ab6554f1SJoshi-Mansi         },
1086ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1087ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1088ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1089ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1090168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1091ab6554f1SJoshi-Mansi }
1092ab6554f1SJoshi-Mansi 
10934f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1094bf648f77SEd Tanous {
1095bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1096bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1097bf648f77SEd Tanous     {
1098bf648f77SEd Tanous         return false;
1099bf648f77SEd Tanous     }
1100bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1101bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1102bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1103bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1104bf648f77SEd Tanous     const std::regex pattern(
1105bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1106bf648f77SEd Tanous 
1107bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1108bf648f77SEd Tanous }
1109bf648f77SEd Tanous 
11104f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1111bf648f77SEd Tanous {
1112bf648f77SEd Tanous     // Can have multiple subdomains
1113bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
11140fda0f12SGeorge Liu     const std::regex pattern(
11150fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1116bf648f77SEd Tanous 
1117bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1118bf648f77SEd Tanous }
1119bf648f77SEd Tanous 
11204f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
11218d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1122ab6554f1SJoshi-Mansi {
1123ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1124ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1125ab6554f1SJoshi-Mansi     {
1126ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1127ab6554f1SJoshi-Mansi         return;
1128ab6554f1SJoshi-Mansi     }
1129ab6554f1SJoshi-Mansi 
1130ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1131ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1132ab6554f1SJoshi-Mansi     {
1133ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1134ab6554f1SJoshi-Mansi         return;
1135ab6554f1SJoshi-Mansi     }
1136ab6554f1SJoshi-Mansi 
1137ab6554f1SJoshi-Mansi     std::string hostname;
1138ab6554f1SJoshi-Mansi     std::string domainname;
1139ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1140ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1141ab6554f1SJoshi-Mansi 
1142ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1143ab6554f1SJoshi-Mansi     {
1144ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1145ab6554f1SJoshi-Mansi         return;
1146ab6554f1SJoshi-Mansi     }
1147ab6554f1SJoshi-Mansi 
1148ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1149ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1150ab6554f1SJoshi-Mansi }
1151ab6554f1SJoshi-Mansi 
11524f48d5f6SEd Tanous inline void
11534f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1154bf648f77SEd Tanous                           const std::string& macAddress,
11558d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1156d577665bSRatan Gupta {
1157d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1158d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1159d577665bSRatan Gupta             if (ec)
1160d577665bSRatan Gupta             {
1161d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1162d577665bSRatan Gupta                 return;
1163d577665bSRatan Gupta             }
1164d577665bSRatan Gupta         },
1165d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1166d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1167d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1168d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1169168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1170d577665bSRatan Gupta }
1171286b9118SJohnathan Mantey 
11724f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11734f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11744f48d5f6SEd Tanous                            const bool v6Value,
11758d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1176da131a9aSJennifer Lee {
11772c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1178da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1179da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1180da131a9aSJennifer Lee             if (ec)
1181da131a9aSJennifer Lee             {
1182da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1183da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1184da131a9aSJennifer Lee                 return;
1185da131a9aSJennifer Lee             }
11868f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1187da131a9aSJennifer Lee         },
1188da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1189da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1190da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1191da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1192168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1193da131a9aSJennifer Lee }
11941f8c7b5dSJohnathan Mantey 
11954f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1196eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11978d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1198eeedda23SJohnathan Mantey {
1199eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1200eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1201eeedda23SJohnathan Mantey             if (ec)
1202eeedda23SJohnathan Mantey             {
1203eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1204eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1205eeedda23SJohnathan Mantey                 return;
1206eeedda23SJohnathan Mantey             }
1207eeedda23SJohnathan Mantey         },
1208eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1209eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1210eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1211eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1212168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1213eeedda23SJohnathan Mantey }
1214eeedda23SJohnathan Mantey 
12154f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
12168d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1217da131a9aSJennifer Lee {
1218da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1219da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1220da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1221da131a9aSJennifer Lee             if (ec)
1222da131a9aSJennifer Lee             {
1223da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1224da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1225da131a9aSJennifer Lee                 return;
1226da131a9aSJennifer Lee             }
1227da131a9aSJennifer Lee         },
1228da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1229da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1230da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1231da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1232168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1233da131a9aSJennifer Lee }
1234d577665bSRatan Gupta 
12354f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12361f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1237f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1238f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12398d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1240da131a9aSJennifer Lee {
12411f8c7b5dSJohnathan Mantey     bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1242bf648f77SEd Tanous     bool ipv6Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1243da131a9aSJennifer Lee 
12441f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12451f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12461f8c7b5dSJohnathan Mantey 
12471f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12481f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1249da131a9aSJennifer Lee     {
12501f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12511f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12521f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12531f8c7b5dSJohnathan Mantey         {
1254bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1255bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12561f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1257da131a9aSJennifer Lee             return;
1258da131a9aSJennifer Lee         }
12591f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12601f8c7b5dSJohnathan Mantey     }
12611f8c7b5dSJohnathan Mantey     else
1262da131a9aSJennifer Lee     {
12631f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12641f8c7b5dSJohnathan Mantey     }
12651f8c7b5dSJohnathan Mantey 
12661f8c7b5dSJohnathan Mantey     bool nextDNS{};
12671f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
12681f8c7b5dSJohnathan Mantey     {
12691f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
12701f8c7b5dSJohnathan Mantey         {
12711f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12721f8c7b5dSJohnathan Mantey             return;
12731f8c7b5dSJohnathan Mantey         }
12741f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12751f8c7b5dSJohnathan Mantey     }
12761f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useDNSServers)
12771f8c7b5dSJohnathan Mantey     {
12781f8c7b5dSJohnathan Mantey         nextDNS = *v4dhcpParms.useDNSServers;
12791f8c7b5dSJohnathan Mantey     }
12801f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useDNSServers)
12811f8c7b5dSJohnathan Mantey     {
12821f8c7b5dSJohnathan Mantey         nextDNS = *v6dhcpParms.useDNSServers;
12831f8c7b5dSJohnathan Mantey     }
12841f8c7b5dSJohnathan Mantey     else
12851f8c7b5dSJohnathan Mantey     {
12861f8c7b5dSJohnathan Mantey         nextDNS = ethData.DNSEnabled;
12871f8c7b5dSJohnathan Mantey     }
12881f8c7b5dSJohnathan Mantey 
12891f8c7b5dSJohnathan Mantey     bool nextNTP{};
12901f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
12911f8c7b5dSJohnathan Mantey     {
12921f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
12931f8c7b5dSJohnathan Mantey         {
12941f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12951f8c7b5dSJohnathan Mantey             return;
12961f8c7b5dSJohnathan Mantey         }
12971f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
12981f8c7b5dSJohnathan Mantey     }
12991f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useNTPServers)
13001f8c7b5dSJohnathan Mantey     {
13011f8c7b5dSJohnathan Mantey         nextNTP = *v4dhcpParms.useNTPServers;
13021f8c7b5dSJohnathan Mantey     }
13031f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useNTPServers)
13041f8c7b5dSJohnathan Mantey     {
13051f8c7b5dSJohnathan Mantey         nextNTP = *v6dhcpParms.useNTPServers;
13061f8c7b5dSJohnathan Mantey     }
13071f8c7b5dSJohnathan Mantey     else
13081f8c7b5dSJohnathan Mantey     {
13091f8c7b5dSJohnathan Mantey         nextNTP = ethData.NTPEnabled;
13101f8c7b5dSJohnathan Mantey     }
13111f8c7b5dSJohnathan Mantey 
13121f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
13131f8c7b5dSJohnathan Mantey     if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13141f8c7b5dSJohnathan Mantey     {
13151f8c7b5dSJohnathan Mantey         if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13161f8c7b5dSJohnathan Mantey         {
13171f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
13181f8c7b5dSJohnathan Mantey             return;
13191f8c7b5dSJohnathan Mantey         }
13201f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13211f8c7b5dSJohnathan Mantey     }
13221f8c7b5dSJohnathan Mantey     else if (v4dhcpParms.useUseDomainName)
13231f8c7b5dSJohnathan Mantey     {
13241f8c7b5dSJohnathan Mantey         nextUseDomain = *v4dhcpParms.useUseDomainName;
13251f8c7b5dSJohnathan Mantey     }
13261f8c7b5dSJohnathan Mantey     else if (v6dhcpParms.useUseDomainName)
13271f8c7b5dSJohnathan Mantey     {
13281f8c7b5dSJohnathan Mantey         nextUseDomain = *v6dhcpParms.useUseDomainName;
13291f8c7b5dSJohnathan Mantey     }
13301f8c7b5dSJohnathan Mantey     else
13311f8c7b5dSJohnathan Mantey     {
13321f8c7b5dSJohnathan Mantey         nextUseDomain = ethData.HostNameEnabled;
13331f8c7b5dSJohnathan Mantey     }
13341f8c7b5dSJohnathan Mantey 
1335da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13361f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13371f8c7b5dSJohnathan Mantey                    asyncResp);
1338da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13391f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1340da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13411f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13421f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13431f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1344da131a9aSJennifer Lee }
134501784826SJohnathan Mantey 
13464f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13472c70f800SEd Tanous     getNextStaticIpEntry(
1348bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1349bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
135001784826SJohnathan Mantey {
135117a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
135217a897dfSManojkiran Eda         return value.origin == "Static";
135317a897dfSManojkiran Eda     });
135401784826SJohnathan Mantey }
135501784826SJohnathan Mantey 
13564f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13572c70f800SEd Tanous     getNextStaticIpEntry(
1358bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1359bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
136001784826SJohnathan Mantey {
136117a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
136217a897dfSManojkiran Eda         return value.origin == "Static";
136317a897dfSManojkiran Eda     });
136401784826SJohnathan Mantey }
136501784826SJohnathan Mantey 
13664f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1367f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
136801784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13698d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13701abe55efSEd Tanous {
137101784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1372f476acbfSRatan Gupta     {
137371f52d96SEd Tanous         messages::propertyValueTypeError(
137471f52d96SEd Tanous             asyncResp->res,
1375bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1376d1d50814SRavi Teja             "IPv4StaticAddresses");
1377f476acbfSRatan Gupta         return;
1378f476acbfSRatan Gupta     }
1379f476acbfSRatan Gupta 
1380271584abSEd Tanous     unsigned entryIdx = 1;
138101784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
138201784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
138301784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
138401784826SJohnathan Mantey     // into the NIC.
138585ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13862c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
138701784826SJohnathan Mantey 
1388537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13891abe55efSEd Tanous     {
13904a0cb85cSEd Tanous         std::string pathString =
1391d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1392179db1d7SKowalski, Kamil 
139301784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1394f476acbfSRatan Gupta         {
1395537174c4SEd Tanous             std::optional<std::string> address;
1396537174c4SEd Tanous             std::optional<std::string> subnetMask;
1397537174c4SEd Tanous             std::optional<std::string> gateway;
1398537174c4SEd Tanous 
1399537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14007e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
14017e27d832SJohnathan Mantey                                      "Gateway", gateway))
1402537174c4SEd Tanous             {
140301784826SJohnathan Mantey                 messages::propertyValueFormatError(
140471f52d96SEd Tanous                     asyncResp->res,
140571f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
140671f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
140771f52d96SEd Tanous                     pathString);
1408537174c4SEd Tanous                 return;
1409179db1d7SKowalski, Kamil             }
1410179db1d7SKowalski, Kamil 
141101784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
141201784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
141301784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
141401784826SJohnathan Mantey             // current request.
1415271584abSEd Tanous             const std::string* addr = nullptr;
1416271584abSEd Tanous             const std::string* gw = nullptr;
141701784826SJohnathan Mantey             uint8_t prefixLength = 0;
141801784826SJohnathan Mantey             bool errorInEntry = false;
1419537174c4SEd Tanous             if (address)
14201abe55efSEd Tanous             {
142101784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
14221abe55efSEd Tanous                 {
142301784826SJohnathan Mantey                     addr = &(*address);
14244a0cb85cSEd Tanous                 }
142501784826SJohnathan Mantey                 else
142601784826SJohnathan Mantey                 {
1427bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1428bf648f77SEd Tanous                                                        pathString + "/Address");
142901784826SJohnathan Mantey                     errorInEntry = true;
143001784826SJohnathan Mantey                 }
143101784826SJohnathan Mantey             }
143285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
143301784826SJohnathan Mantey             {
143485ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
143501784826SJohnathan Mantey             }
143601784826SJohnathan Mantey             else
143701784826SJohnathan Mantey             {
143801784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
143901784826SJohnathan Mantey                                           pathString + "/Address");
144001784826SJohnathan Mantey                 errorInEntry = true;
14414a0cb85cSEd Tanous             }
14424a0cb85cSEd Tanous 
1443537174c4SEd Tanous             if (subnetMask)
14444a0cb85cSEd Tanous             {
1445537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14464a0cb85cSEd Tanous                 {
1447f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1448537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14494a0cb85cSEd Tanous                         pathString + "/SubnetMask");
145001784826SJohnathan Mantey                     errorInEntry = true;
14514a0cb85cSEd Tanous                 }
14524a0cb85cSEd Tanous             }
145385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14544a0cb85cSEd Tanous             {
145585ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
145601784826SJohnathan Mantey                                                 &prefixLength))
14574a0cb85cSEd Tanous                 {
145801784826SJohnathan Mantey                     messages::propertyValueFormatError(
145985ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
146001784826SJohnathan Mantey                         pathString + "/SubnetMask");
146101784826SJohnathan Mantey                     errorInEntry = true;
14624a0cb85cSEd Tanous                 }
14634a0cb85cSEd Tanous             }
14641abe55efSEd Tanous             else
14651abe55efSEd Tanous             {
146601784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
146701784826SJohnathan Mantey                                           pathString + "/SubnetMask");
146801784826SJohnathan Mantey                 errorInEntry = true;
146901784826SJohnathan Mantey             }
147001784826SJohnathan Mantey 
147101784826SJohnathan Mantey             if (gateway)
147201784826SJohnathan Mantey             {
147301784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
147401784826SJohnathan Mantey                 {
147501784826SJohnathan Mantey                     gw = &(*gateway);
147601784826SJohnathan Mantey                 }
147701784826SJohnathan Mantey                 else
147801784826SJohnathan Mantey                 {
1479bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1480bf648f77SEd Tanous                                                        pathString + "/Gateway");
148101784826SJohnathan Mantey                     errorInEntry = true;
148201784826SJohnathan Mantey                 }
148301784826SJohnathan Mantey             }
148485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
148501784826SJohnathan Mantey             {
148685ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
148701784826SJohnathan Mantey             }
148801784826SJohnathan Mantey             else
14891abe55efSEd Tanous             {
1490a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14914a0cb85cSEd Tanous                                           pathString + "/Gateway");
149201784826SJohnathan Mantey                 errorInEntry = true;
14934a0cb85cSEd Tanous             }
14944a0cb85cSEd Tanous 
149501784826SJohnathan Mantey             if (errorInEntry)
14961abe55efSEd Tanous             {
149701784826SJohnathan Mantey                 return;
14984a0cb85cSEd Tanous             }
14994a0cb85cSEd Tanous 
150085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
15011abe55efSEd Tanous             {
150285ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1503bf648f77SEd Tanous                                     *addr, asyncResp);
150485ffe86aSJiaqing Zhao                 nicIpEntry =
150585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1506588c3f0dSKowalski, Kamil             }
150701784826SJohnathan Mantey             else
150801784826SJohnathan Mantey             {
1509cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1510cb13a392SEd Tanous                            asyncResp);
15114a0cb85cSEd Tanous             }
15124a0cb85cSEd Tanous             entryIdx++;
15134a0cb85cSEd Tanous         }
151401784826SJohnathan Mantey         else
151501784826SJohnathan Mantey         {
151685ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
151701784826SJohnathan Mantey             {
151801784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
151901784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
152001784826SJohnathan Mantey                 // in error, so bail out.
152101784826SJohnathan Mantey                 if (thisJson.is_null())
152201784826SJohnathan Mantey                 {
152301784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
152401784826SJohnathan Mantey                     return;
152501784826SJohnathan Mantey                 }
152601784826SJohnathan Mantey                 messages::propertyValueFormatError(
152771f52d96SEd Tanous                     asyncResp->res,
152871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
152971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
153071f52d96SEd Tanous                     pathString);
153101784826SJohnathan Mantey                 return;
153201784826SJohnathan Mantey             }
153301784826SJohnathan Mantey 
153401784826SJohnathan Mantey             if (thisJson.is_null())
153501784826SJohnathan Mantey             {
153685ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
153701784826SJohnathan Mantey             }
153885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
153901784826SJohnathan Mantey             {
154085ffe86aSJiaqing Zhao                 nicIpEntry =
154185ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
154201784826SJohnathan Mantey             }
154301784826SJohnathan Mantey             entryIdx++;
154401784826SJohnathan Mantey         }
154501784826SJohnathan Mantey     }
15464a0cb85cSEd Tanous }
15474a0cb85cSEd Tanous 
15484f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1549f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1550f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15518d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1552f85837bfSRAJESWARAN THILLAIGOVINDAN {
1553f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1554286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1555f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1556f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1557f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1558f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1559f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1560f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1561f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1562f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1563f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1564bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1565168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1566f85837bfSRAJESWARAN THILLAIGOVINDAN }
1567f85837bfSRAJESWARAN THILLAIGOVINDAN 
15684f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1569f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
157001784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15718d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1572e48c0fc5SRavi Teja {
157301784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1574e48c0fc5SRavi Teja     {
157571f52d96SEd Tanous         messages::propertyValueTypeError(
157671f52d96SEd Tanous             asyncResp->res,
1577bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1578e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1579e48c0fc5SRavi Teja         return;
1580e48c0fc5SRavi Teja     }
1581271584abSEd Tanous     size_t entryIdx = 1;
158285ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15832c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1584f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1585e48c0fc5SRavi Teja     {
1586e48c0fc5SRavi Teja         std::string pathString =
1587e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1588e48c0fc5SRavi Teja 
158901784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1590e48c0fc5SRavi Teja         {
1591e48c0fc5SRavi Teja             std::optional<std::string> address;
1592e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1593f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1594bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1595bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1596e48c0fc5SRavi Teja             {
159701784826SJohnathan Mantey                 messages::propertyValueFormatError(
159871f52d96SEd Tanous                     asyncResp->res,
159971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
160071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
160171f52d96SEd Tanous                     pathString);
1602e48c0fc5SRavi Teja                 return;
1603e48c0fc5SRavi Teja             }
1604e48c0fc5SRavi Teja 
1605543f4400SEd Tanous             const std::string* addr = nullptr;
1606543f4400SEd Tanous             uint8_t prefix = 0;
160701784826SJohnathan Mantey 
160801784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
160901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
161001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
161101784826SJohnathan Mantey             // current request.
1612e48c0fc5SRavi Teja             if (address)
1613e48c0fc5SRavi Teja             {
161401784826SJohnathan Mantey                 addr = &(*address);
1615e48c0fc5SRavi Teja             }
161685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
161701784826SJohnathan Mantey             {
161885ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
161901784826SJohnathan Mantey             }
162001784826SJohnathan Mantey             else
162101784826SJohnathan Mantey             {
162201784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
162301784826SJohnathan Mantey                                           pathString + "/Address");
162401784826SJohnathan Mantey                 return;
1625e48c0fc5SRavi Teja             }
1626e48c0fc5SRavi Teja 
1627e48c0fc5SRavi Teja             if (prefixLength)
1628e48c0fc5SRavi Teja             {
162901784826SJohnathan Mantey                 prefix = *prefixLength;
163001784826SJohnathan Mantey             }
163185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1632e48c0fc5SRavi Teja             {
163385ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1634e48c0fc5SRavi Teja             }
1635e48c0fc5SRavi Teja             else
1636e48c0fc5SRavi Teja             {
1637e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1638e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
163901784826SJohnathan Mantey                 return;
1640e48c0fc5SRavi Teja             }
1641e48c0fc5SRavi Teja 
164285ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1643e48c0fc5SRavi Teja             {
164485ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1645e48c0fc5SRavi Teja                                     asyncResp);
164685ffe86aSJiaqing Zhao                 nicIpEntry =
164785ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
164801784826SJohnathan Mantey             }
164901784826SJohnathan Mantey             else
165001784826SJohnathan Mantey             {
165101784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1652e48c0fc5SRavi Teja             }
1653e48c0fc5SRavi Teja             entryIdx++;
1654e48c0fc5SRavi Teja         }
165501784826SJohnathan Mantey         else
165601784826SJohnathan Mantey         {
165785ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
165801784826SJohnathan Mantey             {
165901784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
166001784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
166101784826SJohnathan Mantey                 // in error, so bail out.
166201784826SJohnathan Mantey                 if (thisJson.is_null())
166301784826SJohnathan Mantey                 {
166401784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
166501784826SJohnathan Mantey                     return;
166601784826SJohnathan Mantey                 }
166701784826SJohnathan Mantey                 messages::propertyValueFormatError(
166871f52d96SEd Tanous                     asyncResp->res,
166971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
167071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
167171f52d96SEd Tanous                     pathString);
167201784826SJohnathan Mantey                 return;
167301784826SJohnathan Mantey             }
167401784826SJohnathan Mantey 
167501784826SJohnathan Mantey             if (thisJson.is_null())
167601784826SJohnathan Mantey             {
167785ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
167801784826SJohnathan Mantey             }
167985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
168001784826SJohnathan Mantey             {
168185ffe86aSJiaqing Zhao                 nicIpEntry =
168285ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
168301784826SJohnathan Mantey             }
168401784826SJohnathan Mantey             entryIdx++;
168501784826SJohnathan Mantey         }
168601784826SJohnathan Mantey     }
1687e48c0fc5SRavi Teja }
1688e48c0fc5SRavi Teja 
16894f48d5f6SEd Tanous inline void parseInterfaceData(
16908d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16918d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1692e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
169301784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16944a0cb85cSEd Tanous {
1695eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1696eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1697eeedda23SJohnathan Mantey 
16982c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
169981ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
17002c70f800SEd Tanous     jsonResponse["@odata.id"] =
170181ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
17022c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1703eeedda23SJohnathan Mantey 
1704eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1705eeedda23SJohnathan Mantey 
1706eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1707eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1708*b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1709eeedda23SJohnathan Mantey             if (ec)
1710029573d4SEd Tanous             {
1711eeedda23SJohnathan Mantey                 return;
1712eeedda23SJohnathan Mantey             }
1713eeedda23SJohnathan Mantey 
1714914e2d5dSEd Tanous             health->inventory = resp;
1715eeedda23SJohnathan Mantey         },
1716eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1717eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1718bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1719bf648f77SEd Tanous         inventoryForEthernet);
1720eeedda23SJohnathan Mantey 
1721eeedda23SJohnathan Mantey     health->populate();
1722eeedda23SJohnathan Mantey 
1723eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1724eeedda23SJohnathan Mantey     {
17252c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
17262c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1727029573d4SEd Tanous     }
1728029573d4SEd Tanous     else
1729029573d4SEd Tanous     {
17302c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17312c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1732029573d4SEd Tanous     }
1733aa05fb27SJohnathan Mantey 
17342c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17352c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
173635fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
17372c70f800SEd Tanous     jsonResponse["MACAddress"] = ethData.mac_address;
17382c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
17391f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17402c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17412c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17422c70f800SEd Tanous     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17431f8c7b5dSJohnathan Mantey 
17442c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
17451f8c7b5dSJohnathan Mantey         translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17461f8c7b5dSJohnathan Mantey                                                                : "Disabled";
17472c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17482c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17492c70f800SEd Tanous     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17502a133282Smanojkiraneda 
17514a0cb85cSEd Tanous     if (!ethData.hostname.empty())
17524a0cb85cSEd Tanous     {
17532c70f800SEd Tanous         jsonResponse["HostName"] = ethData.hostname;
1754ab6554f1SJoshi-Mansi 
1755ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1756ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1757ab6554f1SJoshi-Mansi         // FQDN
1758f23b7296SEd Tanous         std::string fqdn = ethData.hostname;
1759d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1760d24bfc7aSJennifer Lee         {
17612c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1762d24bfc7aSJennifer Lee         }
17632c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17644a0cb85cSEd Tanous     }
17654a0cb85cSEd Tanous 
17662c70f800SEd Tanous     jsonResponse["VLANs"] = {
1767bf648f77SEd Tanous         {"@odata.id",
1768bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1769fda13ad2SSunitha Harish 
17702c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17712c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17724a0cb85cSEd Tanous 
17732c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17742c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17752c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17762c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17779eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17784a0cb85cSEd Tanous     {
1779fa5053a6SGunnar Mills 
17802c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1781fa5053a6SGunnar Mills         if (gatewayStr.empty())
1782fa5053a6SGunnar Mills         {
1783fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1784fa5053a6SGunnar Mills         }
1785fa5053a6SGunnar Mills 
17862c70f800SEd Tanous         ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
17872c70f800SEd Tanous                              {"SubnetMask", ipv4Config.netmask},
17882c70f800SEd Tanous                              {"Address", ipv4Config.address},
1789fa5053a6SGunnar Mills                              {"Gateway", gatewayStr}});
17902c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1791d1d50814SRavi Teja         {
17922c70f800SEd Tanous             ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
17932c70f800SEd Tanous                                        {"SubnetMask", ipv4Config.netmask},
17942c70f800SEd Tanous                                        {"Address", ipv4Config.address},
1795d1d50814SRavi Teja                                        {"Gateway", gatewayStr}});
1796d1d50814SRavi Teja         }
179701784826SJohnathan Mantey     }
1798d1d50814SRavi Teja 
17997ea79e5eSRavi Teja     std::string ipv6GatewayStr = ethData.ipv6_default_gateway;
18007ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
18017ea79e5eSRavi Teja     {
18027ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
18037ea79e5eSRavi Teja     }
18047ea79e5eSRavi Teja 
18057ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1806e48c0fc5SRavi Teja 
18072c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
18082c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
18092c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
18102c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
18117f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
18122c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
18137f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
18149eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1815e48c0fc5SRavi Teja     {
18162c70f800SEd Tanous         ipv6Array.push_back({{"Address", ipv6Config.address},
18172c70f800SEd Tanous                              {"PrefixLength", ipv6Config.prefixLength},
18182c70f800SEd Tanous                              {"AddressOrigin", ipv6Config.origin},
18195fd16e4bSJohnathan Mantey                              {"AddressState", nullptr}});
18202c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1821e48c0fc5SRavi Teja         {
18222c70f800SEd Tanous             ipv6StaticArray.push_back(
18232c70f800SEd Tanous                 {{"Address", ipv6Config.address},
18247a474a5fSJiaqing Zhao                  {"PrefixLength", ipv6Config.prefixLength}});
182501784826SJohnathan Mantey         }
1826e48c0fc5SRavi Teja     }
1827588c3f0dSKowalski, Kamil }
1828588c3f0dSKowalski, Kamil 
18294f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1830bf648f77SEd Tanous                                const std::string& parentIfaceId,
1831bf648f77SEd Tanous                                const std::string& ifaceId,
1832bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18331abe55efSEd Tanous {
1834bf648f77SEd Tanous     // Fill out obvious data...
1835bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1836bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1837bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1838bf648f77SEd Tanous 
1839bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
1840bf648f77SEd Tanous     if (!ethData.vlan_id.empty())
1841bf648f77SEd Tanous     {
1842bf648f77SEd Tanous         jsonResponse["VLANId"] = ethData.vlan_id.back();
1843bf648f77SEd Tanous     }
1844bf648f77SEd Tanous }
1845bf648f77SEd Tanous 
18464f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1847bf648f77SEd Tanous {
1848dcf2ebc0SEd Tanous     return boost::starts_with(iface, parent + "_");
1849bf648f77SEd Tanous }
1850bf648f77SEd Tanous 
1851bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1852bf648f77SEd Tanous {
1853bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1854ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1855bf648f77SEd Tanous         .methods(
1856bf648f77SEd Tanous             boost::beast::http::verb::
1857bf648f77SEd Tanous                 get)([](const crow::Request&,
1858bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
1859bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1860bf648f77SEd Tanous                 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1861bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1862bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1863bf648f77SEd Tanous             asyncResp->res.jsonValue["Name"] =
1864bf648f77SEd Tanous                 "Ethernet Network Interface Collection";
1865bf648f77SEd Tanous             asyncResp->res.jsonValue["Description"] =
1866bf648f77SEd Tanous                 "Collection of EthernetInterfaces for this Manager";
1867bf648f77SEd Tanous 
1868bf648f77SEd Tanous             // Get eth interface list, and call the below callback for JSON
1869bf648f77SEd Tanous             // preparation
1870bf648f77SEd Tanous             getEthernetIfaceList([asyncResp](const bool& success,
1871bf648f77SEd Tanous                                              const boost::container::flat_set<
1872bf648f77SEd Tanous                                                  std::string>& ifaceList) {
1873bf648f77SEd Tanous                 if (!success)
18741abe55efSEd Tanous                 {
1875f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
18769391bb9cSRapkiewicz, Pawel                     return;
18779391bb9cSRapkiewicz, Pawel                 }
18789391bb9cSRapkiewicz, Pawel 
1879bf648f77SEd Tanous                 nlohmann::json& ifaceArray =
1880bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members"];
1881bf648f77SEd Tanous                 ifaceArray = nlohmann::json::array();
1882bf648f77SEd Tanous                 std::string tag = "_";
1883bf648f77SEd Tanous                 for (const std::string& ifaceItem : ifaceList)
1884bf648f77SEd Tanous                 {
1885bf648f77SEd Tanous                     std::size_t found = ifaceItem.find(tag);
1886bf648f77SEd Tanous                     if (found == std::string::npos)
1887bf648f77SEd Tanous                     {
1888bf648f77SEd Tanous                         ifaceArray.push_back(
1889bf648f77SEd Tanous                             {{"@odata.id",
1890bf648f77SEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1891bf648f77SEd Tanous                                   ifaceItem}});
1892bf648f77SEd Tanous                     }
1893bf648f77SEd Tanous                 }
1894bf648f77SEd Tanous 
1895bf648f77SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
1896bf648f77SEd Tanous                     ifaceArray.size();
1897bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1898bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1899bf648f77SEd Tanous             });
1900bf648f77SEd Tanous         });
1901bf648f77SEd Tanous 
1902bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1903ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1904bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
1905bf648f77SEd Tanous             [](const crow::Request&,
1906bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1907bf648f77SEd Tanous                const std::string& ifaceId) {
19084a0cb85cSEd Tanous                 getEthernetIfaceData(
1909bf648f77SEd Tanous                     ifaceId,
1910bf648f77SEd Tanous                     [asyncResp,
1911bf648f77SEd Tanous                      ifaceId](const bool& success,
1912bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1913bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1914bf648f77SEd Tanous                                   ipv4Data,
1915bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1916bf648f77SEd Tanous                                   ipv6Data) {
19174a0cb85cSEd Tanous                         if (!success)
19181abe55efSEd Tanous                         {
1919bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1920bf648f77SEd Tanous                             // existing object, and other errors
1921bf648f77SEd Tanous                             messages::resourceNotFound(
1922bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19234a0cb85cSEd Tanous                             return;
19249391bb9cSRapkiewicz, Pawel                         }
19254c9afe43SEd Tanous 
19260f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1927fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1928bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1929bf648f77SEd Tanous                             "Manager Ethernet Interface";
19300f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19310f74e643SEd Tanous                             "Management Network Interface";
19320f74e643SEd Tanous 
1933bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1934bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19359391bb9cSRapkiewicz, Pawel                     });
1936bf648f77SEd Tanous             });
19379391bb9cSRapkiewicz, Pawel 
1938bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1939ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1940ed398213SEd Tanous 
1941bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
1942bf648f77SEd Tanous             [](const crow::Request& req,
1943bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1944bf648f77SEd Tanous                const std::string& ifaceId) {
1945bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1946ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1947d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19489a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1949d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1950e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1951f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1952da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19531f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1954eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
195535fb5311STejas Patil                 std::optional<size_t> mtuSize;
19561f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19571f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19580627a2c7SEd Tanous 
195915ed6780SWilly Tu                 if (!json_util::readJsonPatch(
19608d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1961bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1962bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1963bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1964bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1965ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
196635fb5311STejas Patil                         "MTUSize", mtuSize, "InterfaceEnabled",
196735fb5311STejas Patil                         interfaceEnabled))
19681abe55efSEd Tanous                 {
1969588c3f0dSKowalski, Kamil                     return;
1970588c3f0dSKowalski, Kamil                 }
1971da131a9aSJennifer Lee                 if (dhcpv4)
1972da131a9aSJennifer Lee                 {
1973bf648f77SEd Tanous                     if (!json_util::readJson(
1974bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19751f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19761f8c7b5dSJohnathan Mantey                             v4dhcpParms.useDNSServers, "UseNTPServers",
19771f8c7b5dSJohnathan Mantey                             v4dhcpParms.useNTPServers, "UseDomainName",
19781f8c7b5dSJohnathan Mantey                             v4dhcpParms.useUseDomainName))
19791f8c7b5dSJohnathan Mantey                     {
19801f8c7b5dSJohnathan Mantey                         return;
19811f8c7b5dSJohnathan Mantey                     }
19821f8c7b5dSJohnathan Mantey                 }
19831f8c7b5dSJohnathan Mantey 
19841f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19851f8c7b5dSJohnathan Mantey                 {
1986bf648f77SEd Tanous                     if (!json_util::readJson(
1987bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1988bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
1989bf648f77SEd Tanous                             v6dhcpParms.useDNSServers, "UseNTPServers",
1990bf648f77SEd Tanous                             v6dhcpParms.useNTPServers, "UseDomainName",
19911f8c7b5dSJohnathan Mantey                             v6dhcpParms.useUseDomainName))
19921f8c7b5dSJohnathan Mantey                     {
19931f8c7b5dSJohnathan Mantey                         return;
19941f8c7b5dSJohnathan Mantey                     }
1995da131a9aSJennifer Lee                 }
1996da131a9aSJennifer Lee 
1997bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
1998bf648f77SEd Tanous                 // for JSON preparation
19994a0cb85cSEd Tanous                 getEthernetIfaceData(
20002c70f800SEd Tanous                     ifaceId,
2001bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
2002ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
2003d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
20049a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
2005e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
20061f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
20071f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
200835fb5311STejas Patil                      mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
2009f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
2010bf648f77SEd Tanous                         const bool& success,
2011bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2012bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
2013bf648f77SEd Tanous                             ipv4Data,
2014bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
2015bf648f77SEd Tanous                             ipv6Data) {
20161abe55efSEd Tanous                         if (!success)
20171abe55efSEd Tanous                         {
2018588c3f0dSKowalski, Kamil                             // ... otherwise return error
2019bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2020bf648f77SEd Tanous                             // existing object, and other errors
2021bf648f77SEd Tanous                             messages::resourceNotFound(
2022bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2023588c3f0dSKowalski, Kamil                             return;
2024588c3f0dSKowalski, Kamil                         }
2025588c3f0dSKowalski, Kamil 
20261f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20271f8c7b5dSJohnathan Mantey                         {
2028bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2029bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20301f8c7b5dSJohnathan Mantey                         }
20311f8c7b5dSJohnathan Mantey 
20320627a2c7SEd Tanous                         if (hostname)
20331abe55efSEd Tanous                         {
20340627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20351abe55efSEd Tanous                         }
20360627a2c7SEd Tanous 
2037ab6554f1SJoshi-Mansi                         if (fqdn)
2038ab6554f1SJoshi-Mansi                         {
20392c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2040ab6554f1SJoshi-Mansi                         }
2041ab6554f1SJoshi-Mansi 
2042d577665bSRatan Gupta                         if (macAddress)
2043d577665bSRatan Gupta                         {
2044bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2045bf648f77SEd Tanous                                                   asyncResp);
2046d577665bSRatan Gupta                         }
2047d577665bSRatan Gupta 
2048d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2049d1d50814SRavi Teja                         {
2050bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2051bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2052bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2053bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2054bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2055bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2056bf648f77SEd Tanous                             // that, but could be done more efficiently
2057f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20582c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2059d1d50814SRavi Teja                                                   asyncResp);
20601abe55efSEd Tanous                         }
20610627a2c7SEd Tanous 
2062f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2063f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2064bf648f77SEd Tanous                             handleStaticNameServersPatch(
2065bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2066f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20679a6fc6feSRavi Teja 
20689a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20699a6fc6feSRavi Teja                         {
20709a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20719a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20729a6fc6feSRavi Teja                         }
2073e48c0fc5SRavi Teja 
2074e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2075e48c0fc5SRavi Teja                         {
2076f5b191a6SEd Tanous                             const nlohmann::json& ipv6Static =
2077f5b191a6SEd Tanous                                 *ipv6StaticAddresses;
20782c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
207901784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2080e48c0fc5SRavi Teja                         }
2081eeedda23SJohnathan Mantey 
2082eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2083eeedda23SJohnathan Mantey                         {
2084eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2085bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2086bf648f77SEd Tanous                                 asyncResp);
2087eeedda23SJohnathan Mantey                         }
208835fb5311STejas Patil 
208935fb5311STejas Patil                         if (mtuSize)
209035fb5311STejas Patil                         {
209135fb5311STejas Patil                             handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
209235fb5311STejas Patil                         }
2093588c3f0dSKowalski, Kamil                     });
2094bf648f77SEd Tanous             });
20959391bb9cSRapkiewicz, Pawel 
2096bf648f77SEd Tanous     BMCWEB_ROUTE(
2097bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2098ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2099ed398213SEd Tanous 
2100bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
2101bf648f77SEd Tanous             [](const crow::Request& /* req */,
2102bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2103bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
21048d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
21050f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21068d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2107e439f0f8SKowalski, Kamil 
21082c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
21091abe55efSEd Tanous                 {
2110a434f2bdSEd Tanous                     return;
2111a434f2bdSEd Tanous                 }
2112a434f2bdSEd Tanous 
2113bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2114bf648f77SEd Tanous                 // for JSON preparation
21154a0cb85cSEd Tanous                 getEthernetIfaceData(
2116bf648f77SEd Tanous                     ifaceId,
2117bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2118bf648f77SEd Tanous                         const bool& success,
2119bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2120cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2121cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
212226f6976fSEd Tanous                         if (success && !ethData.vlan_id.empty())
21231abe55efSEd Tanous                         {
2124bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2125bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
21261abe55efSEd Tanous                         }
21271abe55efSEd Tanous                         else
21281abe55efSEd Tanous                         {
2129e439f0f8SKowalski, Kamil                             // ... otherwise return error
2130bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2131bf648f77SEd Tanous                             // existing object, and other errors
2132bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2133bf648f77SEd Tanous                                                        "VLAN Network Interface",
2134bf648f77SEd Tanous                                                        ifaceId);
2135e439f0f8SKowalski, Kamil                         }
2136e439f0f8SKowalski, Kamil                     });
2137bf648f77SEd Tanous             });
2138e439f0f8SKowalski, Kamil 
2139bf648f77SEd Tanous     BMCWEB_ROUTE(
2140bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2141ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2142ed398213SEd Tanous         //.privileges(redfish::privileges::patchVLanNetworkInterface)
2143432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2144bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
2145bf648f77SEd Tanous             [](const crow::Request& req,
2146bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2147bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2148fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21491abe55efSEd Tanous                 {
2150bf648f77SEd Tanous                     messages::resourceNotFound(
2151bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2152927a505aSKowalski, Kamil                     return;
2153927a505aSKowalski, Kamil                 }
2154927a505aSKowalski, Kamil 
21550627a2c7SEd Tanous                 bool vlanEnable = false;
215638268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21570627a2c7SEd Tanous 
215815ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2159bf648f77SEd Tanous                                               vlanEnable, "VLANId", vlanId))
21601abe55efSEd Tanous                 {
2161927a505aSKowalski, Kamil                     return;
2162927a505aSKowalski, Kamil                 }
2163927a505aSKowalski, Kamil 
2164bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2165bf648f77SEd Tanous                 // for JSON preparation
2166e48c0fc5SRavi Teja                 getEthernetIfaceData(
2167bf648f77SEd Tanous                     ifaceId,
2168bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2169bf648f77SEd Tanous                         const bool& success,
2170bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2171cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2172cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
217308244d02SSunitha Harish                         if (success && !ethData.vlan_id.empty())
217408244d02SSunitha Harish                         {
217508244d02SSunitha Harish                             auto callback =
2176bf648f77SEd Tanous                                 [asyncResp](
2177bf648f77SEd Tanous                                     const boost::system::error_code ec) {
217808244d02SSunitha Harish                                     if (ec)
217908244d02SSunitha Harish                                     {
218008244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
218108244d02SSunitha Harish                                     }
218208244d02SSunitha Harish                                 };
218308244d02SSunitha Harish 
2184e05aec50SEd Tanous                             if (vlanEnable)
218508244d02SSunitha Harish                             {
218608244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2187bf648f77SEd Tanous                                     std::move(callback),
2188bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
218908244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
219008244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
219108244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
2192168e20c1SEd Tanous                                     dbus::utility::DbusVariantType(vlanId));
219308244d02SSunitha Harish                             }
219408244d02SSunitha Harish                             else
219508244d02SSunitha Harish                             {
2196bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2197bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2198e48c0fc5SRavi Teja                                        "vlan interface";
219908244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2200bf648f77SEd Tanous                                     std::move(callback),
2201bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2202bf648f77SEd Tanous                                     std::string(
2203bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2204e48c0fc5SRavi Teja                                         ifaceId,
2205bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2206bf648f77SEd Tanous                                     "Delete");
220708244d02SSunitha Harish                             }
220808244d02SSunitha Harish                         }
220908244d02SSunitha Harish                         else
22101abe55efSEd Tanous                         {
2211bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2212bf648f77SEd Tanous                             // existing object, and other errors
2213bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2214bf648f77SEd Tanous                                                        "VLAN Network Interface",
2215bf648f77SEd Tanous                                                        ifaceId);
2216bf648f77SEd Tanous                             return;
2217bf648f77SEd Tanous                         }
2218bf648f77SEd Tanous                     });
2219bf648f77SEd Tanous             });
2220bf648f77SEd Tanous 
2221bf648f77SEd Tanous     BMCWEB_ROUTE(
2222bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2223ed398213SEd Tanous         // This privilege is incorrect, it should be ConfigureManager
2224ed398213SEd Tanous         //.privileges(redfish::privileges::deleteVLanNetworkInterface)
2225432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2226bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
2227bf648f77SEd Tanous             [](const crow::Request& /* req */,
2228bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2229bf648f77SEd Tanous                const std::string& parentIfaceId, const std::string& ifaceId) {
2230bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2231bf648f77SEd Tanous                 {
2232e48c0fc5SRavi Teja                     messages::resourceNotFound(
2233e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2234927a505aSKowalski, Kamil                     return;
2235927a505aSKowalski, Kamil                 }
2236e439f0f8SKowalski, Kamil 
2237bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2238bf648f77SEd Tanous                 // for JSON preparation
2239f12894f8SJason M. Bills                 getEthernetIfaceData(
2240bf648f77SEd Tanous                     ifaceId,
2241bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2242bf648f77SEd Tanous                         const bool& success,
2243bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2244cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2245cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
2246fda13ad2SSunitha Harish                         if (success && !ethData.vlan_id.empty())
22471abe55efSEd Tanous                         {
2248f12894f8SJason M. Bills                             auto callback =
2249bf648f77SEd Tanous                                 [asyncResp](
2250bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22511abe55efSEd Tanous                                     if (ec)
22521abe55efSEd Tanous                                     {
2253f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2254927a505aSKowalski, Kamil                                     }
22554a0cb85cSEd Tanous                                 };
22564a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2257bf648f77SEd Tanous                                 std::move(callback),
2258bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2259bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2260bf648f77SEd Tanous                                     ifaceId,
22614a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22621abe55efSEd Tanous                         }
22631abe55efSEd Tanous                         else
22641abe55efSEd Tanous                         {
2265927a505aSKowalski, Kamil                             // ... otherwise return error
2266bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2267bf648f77SEd Tanous                             // existing object, and other errors
2268bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2269bf648f77SEd Tanous                                                        "VLAN Network Interface",
2270bf648f77SEd Tanous                                                        ifaceId);
2271927a505aSKowalski, Kamil                         }
2272927a505aSKowalski, Kamil                     });
2273bf648f77SEd Tanous             });
2274e439f0f8SKowalski, Kamil 
2275bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2276bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2277ed398213SEd Tanous 
2278ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2279bf648f77SEd Tanous         .methods(
2280bf648f77SEd Tanous             boost::beast::http::verb::
2281bf648f77SEd Tanous                 get)([](const crow::Request& /* req */,
2282bf648f77SEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2283bf648f77SEd Tanous                         const std::string& rootInterfaceName) {
22844a0cb85cSEd Tanous             // Get eth interface list, and call the below callback for JSON
22851abe55efSEd Tanous             // preparation
2286bf648f77SEd Tanous             getEthernetIfaceList([asyncResp, rootInterfaceName](
22871abe55efSEd Tanous                                      const bool& success,
2288bf648f77SEd Tanous                                      const boost::container::flat_set<
2289bf648f77SEd Tanous                                          std::string>& ifaceList) {
22904a0cb85cSEd Tanous                 if (!success)
22911abe55efSEd Tanous                 {
2292f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
22934a0cb85cSEd Tanous                     return;
22941abe55efSEd Tanous                 }
22954c9afe43SEd Tanous 
229681ce609eSEd Tanous                 if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22974c9afe43SEd Tanous                 {
22984c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
22994c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23004c9afe43SEd Tanous                                                rootInterfaceName);
23014c9afe43SEd Tanous                     return;
23024c9afe43SEd Tanous                 }
23034c9afe43SEd Tanous 
23040f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23050f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23060f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23070f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23080f74e643SEd Tanous                     "VLAN Network Interface Collection";
23094a0cb85cSEd Tanous 
23102c70f800SEd Tanous                 nlohmann::json ifaceArray = nlohmann::json::array();
23114a0cb85cSEd Tanous 
231281ce609eSEd Tanous                 for (const std::string& ifaceItem : ifaceList)
23131abe55efSEd Tanous                 {
23142c70f800SEd Tanous                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
23154a0cb85cSEd Tanous                     {
2316f23b7296SEd Tanous                         std::string path =
2317f23b7296SEd Tanous                             "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2318f23b7296SEd Tanous                         path += rootInterfaceName;
2319f23b7296SEd Tanous                         path += "/VLANs/";
2320f23b7296SEd Tanous                         path += ifaceItem;
2321f23b7296SEd Tanous                         ifaceArray.push_back({{"@odata.id", std::move(path)}});
2322e439f0f8SKowalski, Kamil                     }
2323e439f0f8SKowalski, Kamil                 }
2324e439f0f8SKowalski, Kamil 
23254a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23262c70f800SEd Tanous                     ifaceArray.size();
23272c70f800SEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23284a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23294a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23304a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2331e439f0f8SKowalski, Kamil             });
2332bf648f77SEd Tanous         });
2333e439f0f8SKowalski, Kamil 
2334bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2335bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2336ed398213SEd Tanous         // This privilege is wrong, it should be ConfigureManager
2337ed398213SEd Tanous         //.privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2338432a890cSEd Tanous         .privileges({{"ConfigureComponents"}})
2339bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
2340bf648f77SEd Tanous             [](const crow::Request& req,
2341bf648f77SEd Tanous                const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2342bf648f77SEd Tanous                const std::string& rootInterfaceName) {
2343fda13ad2SSunitha Harish                 bool vlanEnable = false;
23440627a2c7SEd Tanous                 uint32_t vlanId = 0;
234515ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
234615ed6780SWilly Tu                                               vlanId, "VLANEnable", vlanEnable))
23471abe55efSEd Tanous                 {
23484a0cb85cSEd Tanous                     return;
2349e439f0f8SKowalski, Kamil                 }
2350fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2351dbb59d4dSEd Tanous                 if (vlanId == 0U)
2352fda13ad2SSunitha Harish                 {
2353fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2354fda13ad2SSunitha Harish                 }
2355fda13ad2SSunitha Harish                 if (!vlanEnable)
2356fda13ad2SSunitha Harish                 {
2357fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2358fda13ad2SSunitha Harish                 }
2359271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2360fda13ad2SSunitha Harish                 {
2361fda13ad2SSunitha Harish                     return;
2362fda13ad2SSunitha Harish                 }
2363fda13ad2SSunitha Harish 
2364bf648f77SEd Tanous                 auto callback =
2365bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23661abe55efSEd Tanous                         if (ec)
23671abe55efSEd Tanous                         {
2368bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2369bf648f77SEd Tanous                             // based on phosphor-network responses
2370f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23714a0cb85cSEd Tanous                             return;
23721abe55efSEd Tanous                         }
2373f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2374e439f0f8SKowalski, Kamil                     };
23754a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23764a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23774a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23784a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23790627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2380bf648f77SEd Tanous             });
23814a0cb85cSEd Tanous }
2382bf648f77SEd Tanous 
23839391bb9cSRapkiewicz, Pawel } // namespace redfish
2384