xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 7a1dbc4803bf78bfc0c574e6676b3c5def4cdae3)
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 
18*7a1dbc48SGeorge Liu #include "dbus_utility.hpp"
19033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
20033f1e4dSEd Tanous 
217e860f15SJohn Edward Broadbent #include <app.hpp>
2211ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
2311ba3979SEd Tanous #include <boost/algorithm/string/split.hpp>
244a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
25179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
26588c3f0dSKowalski, Kamil #include <error_messages.hpp>
2745ca1b86SEd Tanous #include <query.hpp>
28ed398213SEd Tanous #include <registries/privilege_registry.hpp>
291214b7e7SGunnar Mills #include <utils/json_utils.hpp>
301214b7e7SGunnar Mills 
31*7a1dbc48SGeorge Liu #include <array>
32a24526dcSEd Tanous #include <optional>
33ab6554f1SJoshi-Mansi #include <regex>
34*7a1dbc48SGeorge Liu #include <string_view>
359391bb9cSRapkiewicz, Pawel 
361abe55efSEd Tanous namespace redfish
371abe55efSEd Tanous {
389391bb9cSRapkiewicz, Pawel 
394a0cb85cSEd Tanous enum class LinkType
404a0cb85cSEd Tanous {
414a0cb85cSEd Tanous     Local,
424a0cb85cSEd Tanous     Global
434a0cb85cSEd Tanous };
449391bb9cSRapkiewicz, Pawel 
459391bb9cSRapkiewicz, Pawel /**
469391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
479391bb9cSRapkiewicz, Pawel  */
481abe55efSEd Tanous struct IPv4AddressData
491abe55efSEd Tanous {
50179db1d7SKowalski, Kamil     std::string id;
514a0cb85cSEd Tanous     std::string address;
524a0cb85cSEd Tanous     std::string domain;
534a0cb85cSEd Tanous     std::string gateway;
549391bb9cSRapkiewicz, Pawel     std::string netmask;
559391bb9cSRapkiewicz, Pawel     std::string origin;
564a0cb85cSEd Tanous     LinkType linktype;
5701c6e858SSunitha Harish     bool isActive;
584a0cb85cSEd Tanous 
591abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
601abe55efSEd Tanous     {
614a0cb85cSEd Tanous         return id < obj.id;
621abe55efSEd Tanous     }
639391bb9cSRapkiewicz, Pawel };
649391bb9cSRapkiewicz, Pawel 
659391bb9cSRapkiewicz, Pawel /**
66e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
67e48c0fc5SRavi Teja  */
68e48c0fc5SRavi Teja struct IPv6AddressData
69e48c0fc5SRavi Teja {
70e48c0fc5SRavi Teja     std::string id;
71e48c0fc5SRavi Teja     std::string address;
72e48c0fc5SRavi Teja     std::string origin;
73e48c0fc5SRavi Teja     uint8_t prefixLength;
74e48c0fc5SRavi Teja 
75e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
76e48c0fc5SRavi Teja     {
77e48c0fc5SRavi Teja         return id < obj.id;
78e48c0fc5SRavi Teja     }
79e48c0fc5SRavi Teja };
80e48c0fc5SRavi Teja /**
819391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
829391bb9cSRapkiewicz, Pawel  * available from DBus
839391bb9cSRapkiewicz, Pawel  */
841abe55efSEd Tanous struct EthernetInterfaceData
851abe55efSEd Tanous {
864a0cb85cSEd Tanous     uint32_t speed;
8735fb5311STejas Patil     size_t mtuSize;
8882695a5bSJiaqing Zhao     bool autoNeg;
8982695a5bSJiaqing Zhao     bool dnsEnabled;
9082695a5bSJiaqing Zhao     bool ntpEnabled;
9182695a5bSJiaqing Zhao     bool hostNameEnabled;
92aa05fb27SJohnathan Mantey     bool linkUp;
93eeedda23SJohnathan Mantey     bool nicEnabled;
9482695a5bSJiaqing Zhao     std::string dhcpEnabled;
951f8c7b5dSJohnathan Mantey     std::string operatingMode;
9682695a5bSJiaqing Zhao     std::string hostName;
9782695a5bSJiaqing Zhao     std::string defaultGateway;
9882695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
9982695a5bSJiaqing Zhao     std::string macAddress;
10017e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
1010f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1020f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
103d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1049391bb9cSRapkiewicz, Pawel };
1059391bb9cSRapkiewicz, Pawel 
1061f8c7b5dSJohnathan Mantey struct DHCPParameters
1071f8c7b5dSJohnathan Mantey {
1081f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
10982695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
11082695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
11182695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1121f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1131f8c7b5dSJohnathan Mantey };
1141f8c7b5dSJohnathan Mantey 
1159391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1169391bb9cSRapkiewicz, Pawel // into full dot notation
1171abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1181abe55efSEd Tanous {
1199391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1209391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1219391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1229391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1249391bb9cSRapkiewicz, Pawel     return netmask;
1259391bb9cSRapkiewicz, Pawel }
1269391bb9cSRapkiewicz, Pawel 
12782695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1281f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1291f8c7b5dSJohnathan Mantey {
1301f8c7b5dSJohnathan Mantey     if (isIPv4)
1311f8c7b5dSJohnathan Mantey     {
1321f8c7b5dSJohnathan Mantey         return (
1331f8c7b5dSJohnathan Mantey             (inputDHCP ==
1341f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1351f8c7b5dSJohnathan Mantey             (inputDHCP ==
1361f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1371f8c7b5dSJohnathan Mantey     }
1381f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1391f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1401f8c7b5dSJohnathan Mantey             (inputDHCP ==
1411f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1421f8c7b5dSJohnathan Mantey }
1431f8c7b5dSJohnathan Mantey 
1442c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1451f8c7b5dSJohnathan Mantey {
1461f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1471f8c7b5dSJohnathan Mantey     {
1481f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1491f8c7b5dSJohnathan Mantey     }
1503174e4dfSEd Tanous     if (isIPv4)
1511f8c7b5dSJohnathan Mantey     {
1521f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1531f8c7b5dSJohnathan Mantey     }
1543174e4dfSEd Tanous     if (isIPv6)
1551f8c7b5dSJohnathan Mantey     {
1561f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1571f8c7b5dSJohnathan Mantey     }
1581f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1591f8c7b5dSJohnathan Mantey }
1601f8c7b5dSJohnathan Mantey 
1614a0cb85cSEd Tanous inline std::string
1624a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1634a0cb85cSEd Tanous                                         bool isIPv4)
1641abe55efSEd Tanous {
1654a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1661abe55efSEd Tanous     {
1674a0cb85cSEd Tanous         return "Static";
1689391bb9cSRapkiewicz, Pawel     }
1694a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1701abe55efSEd Tanous     {
1714a0cb85cSEd Tanous         if (isIPv4)
1721abe55efSEd Tanous         {
1734a0cb85cSEd Tanous             return "IPv4LinkLocal";
1741abe55efSEd Tanous         }
1754a0cb85cSEd Tanous         return "LinkLocal";
1769391bb9cSRapkiewicz, Pawel     }
1774a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1781abe55efSEd Tanous     {
1794a0cb85cSEd Tanous         if (isIPv4)
1804a0cb85cSEd Tanous         {
1814a0cb85cSEd Tanous             return "DHCP";
1824a0cb85cSEd Tanous         }
1834a0cb85cSEd Tanous         return "DHCPv6";
1844a0cb85cSEd Tanous     }
1854a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1864a0cb85cSEd Tanous     {
1874a0cb85cSEd Tanous         return "SLAAC";
1884a0cb85cSEd Tanous     }
1894a0cb85cSEd Tanous     return "";
1904a0cb85cSEd Tanous }
1914a0cb85cSEd Tanous 
19202cad96eSEd Tanous inline bool extractEthernetInterfaceData(
19302cad96eSEd Tanous     const std::string& ethifaceId,
19402cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1954a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1964a0cb85cSEd Tanous {
1974c9afe43SEd Tanous     bool idFound = false;
19802cad96eSEd Tanous     for (const auto& objpath : dbusData)
1994a0cb85cSEd Tanous     {
20002cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
2014a0cb85cSEd Tanous         {
20281ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
203029573d4SEd Tanous             {
2044c9afe43SEd Tanous                 idFound = true;
2054a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2064a0cb85cSEd Tanous                 {
2074a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2084a0cb85cSEd Tanous                     {
2094a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2104a0cb85cSEd Tanous                         {
2114a0cb85cSEd Tanous                             const std::string* mac =
212abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2134a0cb85cSEd Tanous                             if (mac != nullptr)
2144a0cb85cSEd Tanous                             {
21582695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2164a0cb85cSEd Tanous                             }
2174a0cb85cSEd Tanous                         }
2184a0cb85cSEd Tanous                     }
2194a0cb85cSEd Tanous                 }
2204a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2214a0cb85cSEd Tanous                 {
2224a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2234a0cb85cSEd Tanous                     {
2244a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2254a0cb85cSEd Tanous                         {
2261b6b96c5SEd Tanous                             const uint32_t* id =
227abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2284a0cb85cSEd Tanous                             if (id != nullptr)
2294a0cb85cSEd Tanous                             {
23017e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2314a0cb85cSEd Tanous                             }
2324a0cb85cSEd Tanous                         }
2334a0cb85cSEd Tanous                     }
2344a0cb85cSEd Tanous                 }
2354a0cb85cSEd Tanous                 else if (ifacePair.first ==
2364a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2374a0cb85cSEd Tanous                 {
2384a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2394a0cb85cSEd Tanous                     {
2404a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2414a0cb85cSEd Tanous                         {
2422c70f800SEd Tanous                             const bool* autoNeg =
243abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2442c70f800SEd Tanous                             if (autoNeg != nullptr)
2454a0cb85cSEd Tanous                             {
24682695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2474a0cb85cSEd Tanous                             }
2484a0cb85cSEd Tanous                         }
2494a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2504a0cb85cSEd Tanous                         {
2514a0cb85cSEd Tanous                             const uint32_t* speed =
252abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2534a0cb85cSEd Tanous                             if (speed != nullptr)
2544a0cb85cSEd Tanous                             {
2554a0cb85cSEd Tanous                                 ethData.speed = *speed;
2564a0cb85cSEd Tanous                             }
2574a0cb85cSEd Tanous                         }
25835fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25935fb5311STejas Patil                         {
26035fb5311STejas Patil                             const uint32_t* mtuSize =
26135fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
26235fb5311STejas Patil                             if (mtuSize != nullptr)
26335fb5311STejas Patil                             {
26435fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26535fb5311STejas Patil                             }
26635fb5311STejas Patil                         }
267aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
268aa05fb27SJohnathan Mantey                         {
269aa05fb27SJohnathan Mantey                             const bool* linkUp =
270aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
271aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
272aa05fb27SJohnathan Mantey                             {
273aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
274aa05fb27SJohnathan Mantey                             }
275aa05fb27SJohnathan Mantey                         }
276eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
277eeedda23SJohnathan Mantey                         {
278eeedda23SJohnathan Mantey                             const bool* nicEnabled =
279eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
280eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
281eeedda23SJohnathan Mantey                             {
282eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
283eeedda23SJohnathan Mantey                             }
284eeedda23SJohnathan Mantey                         }
285f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
286029573d4SEd Tanous                         {
287029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2888d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
289029573d4SEd Tanous                                     &propertyPair.second);
290029573d4SEd Tanous                             if (nameservers != nullptr)
291029573d4SEd Tanous                             {
292f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2930f6efdc1Smanojkiran.eda@gmail.com                             }
2940f6efdc1Smanojkiran.eda@gmail.com                         }
2950f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2960f6efdc1Smanojkiran.eda@gmail.com                         {
2970f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2988d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2990f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3000f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3010f6efdc1Smanojkiran.eda@gmail.com                             {
302f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3034a0cb85cSEd Tanous                             }
3044a0cb85cSEd Tanous                         }
3052a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3062a133282Smanojkiraneda                         {
3072c70f800SEd Tanous                             const std::string* dhcpEnabled =
3081f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3092c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3102a133282Smanojkiraneda                             {
31182695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3122a133282Smanojkiraneda                             }
3132a133282Smanojkiraneda                         }
314d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
315d24bfc7aSJennifer Lee                         {
316d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3178d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
318d24bfc7aSJennifer Lee                                     &propertyPair.second);
319d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
320d24bfc7aSJennifer Lee                             {
321f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
322d24bfc7aSJennifer Lee                             }
323d24bfc7aSJennifer Lee                         }
3249010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3259010ec2eSRavi Teja                         {
3269010ec2eSRavi Teja                             const std::string* defaultGateway =
3279010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3289010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3299010ec2eSRavi Teja                             {
3309010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3319010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3329010ec2eSRavi Teja                                 {
33382695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3349010ec2eSRavi Teja                                 }
3359010ec2eSRavi Teja                                 else
3369010ec2eSRavi Teja                                 {
33782695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3389010ec2eSRavi Teja                                 }
3399010ec2eSRavi Teja                             }
3409010ec2eSRavi Teja                         }
3419010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3429010ec2eSRavi Teja                         {
3439010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3449010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3459010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3469010ec2eSRavi Teja                             {
3479010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3489010ec2eSRavi Teja                                     *defaultGateway6;
3499010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3509010ec2eSRavi Teja                                 {
35182695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3529010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3539010ec2eSRavi Teja                                 }
3549010ec2eSRavi Teja                                 else
3559010ec2eSRavi Teja                                 {
35682695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3579010ec2eSRavi Teja                                         defaultGateway6Str;
3589010ec2eSRavi Teja                                 }
3599010ec2eSRavi Teja                             }
3609010ec2eSRavi Teja                         }
361029573d4SEd Tanous                     }
362029573d4SEd Tanous                 }
363029573d4SEd Tanous             }
3641f8c7b5dSJohnathan Mantey 
3651e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3661f8c7b5dSJohnathan Mantey             {
3671f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3681f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3691f8c7b5dSJohnathan Mantey                 {
3701f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3711f8c7b5dSJohnathan Mantey                     {
3721f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3731f8c7b5dSJohnathan Mantey                         {
3742c70f800SEd Tanous                             const bool* dnsEnabled =
3751f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3762c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3771f8c7b5dSJohnathan Mantey                             {
37882695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3791f8c7b5dSJohnathan Mantey                             }
3801f8c7b5dSJohnathan Mantey                         }
3811f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3821f8c7b5dSJohnathan Mantey                         {
3832c70f800SEd Tanous                             const bool* ntpEnabled =
3841f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3852c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3861f8c7b5dSJohnathan Mantey                             {
38782695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3881f8c7b5dSJohnathan Mantey                             }
3891f8c7b5dSJohnathan Mantey                         }
3901f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3911f8c7b5dSJohnathan Mantey                         {
3922c70f800SEd Tanous                             const bool* hostNameEnabled =
3931f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3942c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3951f8c7b5dSJohnathan Mantey                             {
39682695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3971f8c7b5dSJohnathan Mantey                             }
3981f8c7b5dSJohnathan Mantey                         }
3991f8c7b5dSJohnathan Mantey                     }
4001f8c7b5dSJohnathan Mantey                 }
4011f8c7b5dSJohnathan Mantey             }
402029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
403029573d4SEd Tanous             // to check eth number
404029573d4SEd Tanous             if (ifacePair.first ==
4054a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4064a0cb85cSEd Tanous             {
4074a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4084a0cb85cSEd Tanous                 {
4094a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4104a0cb85cSEd Tanous                     {
4114a0cb85cSEd Tanous                         const std::string* hostname =
4128d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4134a0cb85cSEd Tanous                         if (hostname != nullptr)
4144a0cb85cSEd Tanous                         {
41582695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4164a0cb85cSEd Tanous                         }
4174a0cb85cSEd Tanous                     }
4184a0cb85cSEd Tanous                 }
4194a0cb85cSEd Tanous             }
4204a0cb85cSEd Tanous         }
4214a0cb85cSEd Tanous     }
4224c9afe43SEd Tanous     return idFound;
4234a0cb85cSEd Tanous }
4244a0cb85cSEd Tanous 
425e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42601784826SJohnathan Mantey inline void
42781ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
428711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
42981ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
430e48c0fc5SRavi Teja {
431e48c0fc5SRavi Teja     const std::string ipv6PathStart =
43281ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
433e48c0fc5SRavi Teja 
434e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
435e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43681ce609eSEd Tanous     for (const auto& objpath : dbusData)
437e48c0fc5SRavi Teja     {
438e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
43911ba3979SEd Tanous         if (objpath.first.str.starts_with(ipv6PathStart))
440e48c0fc5SRavi Teja         {
4419eb808c1SEd Tanous             for (const auto& interface : objpath.second)
442e48c0fc5SRavi Teja             {
443e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
444e48c0fc5SRavi Teja                 {
445e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
446e48c0fc5SRavi Teja                     // appropriate
447e48c0fc5SRavi Teja                     std::pair<
448e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
449e48c0fc5SRavi Teja                         bool>
45081ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4512c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4522c70f800SEd Tanous                     ipv6Address.id =
453271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4549eb808c1SEd Tanous                     for (const auto& property : interface.second)
455e48c0fc5SRavi Teja                     {
456e48c0fc5SRavi Teja                         if (property.first == "Address")
457e48c0fc5SRavi Teja                         {
458e48c0fc5SRavi Teja                             const std::string* address =
459e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
460e48c0fc5SRavi Teja                             if (address != nullptr)
461e48c0fc5SRavi Teja                             {
4622c70f800SEd Tanous                                 ipv6Address.address = *address;
463e48c0fc5SRavi Teja                             }
464e48c0fc5SRavi Teja                         }
465e48c0fc5SRavi Teja                         else if (property.first == "Origin")
466e48c0fc5SRavi Teja                         {
467e48c0fc5SRavi Teja                             const std::string* origin =
468e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
469e48c0fc5SRavi Teja                             if (origin != nullptr)
470e48c0fc5SRavi Teja                             {
4712c70f800SEd Tanous                                 ipv6Address.origin =
472e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
473e48c0fc5SRavi Teja                                                                         false);
474e48c0fc5SRavi Teja                             }
475e48c0fc5SRavi Teja                         }
476e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
477e48c0fc5SRavi Teja                         {
478e48c0fc5SRavi Teja                             const uint8_t* prefix =
479e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
480e48c0fc5SRavi Teja                             if (prefix != nullptr)
481e48c0fc5SRavi Teja                             {
4822c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
483e48c0fc5SRavi Teja                             }
484e48c0fc5SRavi Teja                         }
485889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
486889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
487889ff694SAsmitha Karunanithi                         {
488889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
489889ff694SAsmitha Karunanithi                         }
490e48c0fc5SRavi Teja                         else
491e48c0fc5SRavi Teja                         {
492e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
493e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
494e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
495e48c0fc5SRavi Teja                         }
496e48c0fc5SRavi Teja                     }
497e48c0fc5SRavi Teja                 }
498e48c0fc5SRavi Teja             }
499e48c0fc5SRavi Teja         }
500e48c0fc5SRavi Teja     }
501e48c0fc5SRavi Teja }
502e48c0fc5SRavi Teja 
5034a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
50401784826SJohnathan Mantey inline void
50581ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
506711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
50781ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5084a0cb85cSEd Tanous {
5094a0cb85cSEd Tanous     const std::string ipv4PathStart =
51081ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5114a0cb85cSEd Tanous 
5124a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5134a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
51481ce609eSEd Tanous     for (const auto& objpath : dbusData)
5154a0cb85cSEd Tanous     {
5164a0cb85cSEd Tanous         // Check if proper pattern for object path appears
51711ba3979SEd Tanous         if (objpath.first.str.starts_with(ipv4PathStart))
5184a0cb85cSEd Tanous         {
5199eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5204a0cb85cSEd Tanous             {
5214a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5224a0cb85cSEd Tanous                 {
5234a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5244a0cb85cSEd Tanous                     // appropriate
5254a0cb85cSEd Tanous                     std::pair<
5264a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5274a0cb85cSEd Tanous                         bool>
52881ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5292c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5302c70f800SEd Tanous                     ipv4Address.id =
531271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5329eb808c1SEd Tanous                     for (const auto& property : interface.second)
5334a0cb85cSEd Tanous                     {
5344a0cb85cSEd Tanous                         if (property.first == "Address")
5354a0cb85cSEd Tanous                         {
5364a0cb85cSEd Tanous                             const std::string* address =
537abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5384a0cb85cSEd Tanous                             if (address != nullptr)
5394a0cb85cSEd Tanous                             {
5402c70f800SEd Tanous                                 ipv4Address.address = *address;
5414a0cb85cSEd Tanous                             }
5424a0cb85cSEd Tanous                         }
5434a0cb85cSEd Tanous                         else if (property.first == "Origin")
5444a0cb85cSEd Tanous                         {
5454a0cb85cSEd Tanous                             const std::string* origin =
546abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5474a0cb85cSEd Tanous                             if (origin != nullptr)
5484a0cb85cSEd Tanous                             {
5492c70f800SEd Tanous                                 ipv4Address.origin =
5504a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5514a0cb85cSEd Tanous                                                                         true);
5524a0cb85cSEd Tanous                             }
5534a0cb85cSEd Tanous                         }
5544a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5554a0cb85cSEd Tanous                         {
5564a0cb85cSEd Tanous                             const uint8_t* mask =
557abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5584a0cb85cSEd Tanous                             if (mask != nullptr)
5594a0cb85cSEd Tanous                             {
5604a0cb85cSEd Tanous                                 // convert it to the string
5612c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5624a0cb85cSEd Tanous                             }
5634a0cb85cSEd Tanous                         }
564889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
565889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
566889ff694SAsmitha Karunanithi                         {
567889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
568889ff694SAsmitha Karunanithi                         }
5694a0cb85cSEd Tanous                         else
5704a0cb85cSEd Tanous                         {
5714a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5724a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5734a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5744a0cb85cSEd Tanous                         }
5754a0cb85cSEd Tanous                     }
5764a0cb85cSEd Tanous                     // Check if given address is local, or global
5772c70f800SEd Tanous                     ipv4Address.linktype =
57811ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
57918659d10SJohnathan Mantey                             ? LinkType::Local
58018659d10SJohnathan Mantey                             : LinkType::Global;
5814a0cb85cSEd Tanous                 }
5824a0cb85cSEd Tanous             }
5834a0cb85cSEd Tanous         }
5844a0cb85cSEd Tanous     }
5854a0cb85cSEd Tanous }
586588c3f0dSKowalski, Kamil 
587588c3f0dSKowalski, Kamil /**
58801784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
589179db1d7SKowalski, Kamil  *
590179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
591179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
592179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
593179db1d7SKowalski, Kamil  *
594179db1d7SKowalski, Kamil  * @return None
595179db1d7SKowalski, Kamil  */
5964a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
5978d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
5981abe55efSEd Tanous {
59955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
600286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
6011abe55efSEd Tanous         if (ec)
6021abe55efSEd Tanous         {
603a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6041abe55efSEd Tanous         }
605179db1d7SKowalski, Kamil         },
606179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
607179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
608179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
609179db1d7SKowalski, Kamil }
610179db1d7SKowalski, Kamil 
611244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
612244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
613244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6149010ec2eSRavi Teja {
6159010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6169010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
6179010ec2eSRavi Teja         if (ec)
6189010ec2eSRavi Teja         {
6199010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6209010ec2eSRavi Teja             return;
6219010ec2eSRavi Teja         }
6229010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6239010ec2eSRavi Teja         },
6249010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6259010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6269010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6279010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
628168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6299010ec2eSRavi Teja }
630179db1d7SKowalski, Kamil /**
63101784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
632179db1d7SKowalski, Kamil  *
63301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
63401784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
63501784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
63601784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
637179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
638179db1d7SKowalski, Kamil  *
639179db1d7SKowalski, Kamil  * @return None
640179db1d7SKowalski, Kamil  */
641cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
642cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6438d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6441abe55efSEd Tanous {
645002d39b4SEd Tanous     auto createIpHandler =
646002d39b4SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
6471abe55efSEd Tanous         if (ec)
6481abe55efSEd Tanous         {
649a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6509010ec2eSRavi Teja             return;
651179db1d7SKowalski, Kamil         }
6529010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6539010ec2eSRavi Teja     };
6549010ec2eSRavi Teja 
6559010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6569010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
657179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
658179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
65901784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
660179db1d7SKowalski, Kamil         gateway);
661179db1d7SKowalski, Kamil }
662e48c0fc5SRavi Teja 
663e48c0fc5SRavi Teja /**
66401784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
66501784826SJohnathan Mantey  * static IPv4 entry
66601784826SJohnathan Mantey  *
66701784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
66801784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
66901784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
67001784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
67101784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
67201784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
67301784826SJohnathan Mantey  *
67401784826SJohnathan Mantey  * @return None
67501784826SJohnathan Mantey  */
6768d1b46d7Szhanghch05 inline void
6778d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
6788d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
67901784826SJohnathan Mantey                         const std::string& address,
6808d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
68101784826SJohnathan Mantey {
68201784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
68301784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
68401784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
68501784826SJohnathan Mantey         if (ec)
68601784826SJohnathan Mantey         {
68701784826SJohnathan Mantey             messages::internalError(asyncResp->res);
6889010ec2eSRavi Teja             return;
68901784826SJohnathan Mantey         }
6909010ec2eSRavi Teja 
69101784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
692002d39b4SEd Tanous             [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
69323a21a1cSEd Tanous             if (ec2)
69401784826SJohnathan Mantey             {
69501784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
6969010ec2eSRavi Teja                 return;
69701784826SJohnathan Mantey             }
6989010ec2eSRavi Teja             updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
69901784826SJohnathan Mantey             },
70001784826SJohnathan Mantey             "xyz.openbmc_project.Network",
70101784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
70201784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
70301784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
70401784826SJohnathan Mantey             prefixLength, gateway);
70501784826SJohnathan Mantey         },
70601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
70701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
70801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
70901784826SJohnathan Mantey }
71001784826SJohnathan Mantey 
71101784826SJohnathan Mantey /**
712e48c0fc5SRavi Teja  * @brief Deletes given IPv6
713e48c0fc5SRavi Teja  *
714e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
715e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
716e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
717e48c0fc5SRavi Teja  *
718e48c0fc5SRavi Teja  * @return None
719e48c0fc5SRavi Teja  */
720e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
7218d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
722e48c0fc5SRavi Teja {
723e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
724286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
725e48c0fc5SRavi Teja         if (ec)
726e48c0fc5SRavi Teja         {
727e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
728e48c0fc5SRavi Teja         }
729e48c0fc5SRavi Teja         },
730e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
731e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
732e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
733e48c0fc5SRavi Teja }
734e48c0fc5SRavi Teja 
735e48c0fc5SRavi Teja /**
73601784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
73701784826SJohnathan Mantey  * static IPv6 entry
73801784826SJohnathan Mantey  *
73901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
74001784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
74101784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
74201784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
74301784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
74401784826SJohnathan Mantey  *
74501784826SJohnathan Mantey  * @return None
74601784826SJohnathan Mantey  */
7478d1b46d7Szhanghch05 inline void
7488d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
7498d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
7508d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
75101784826SJohnathan Mantey {
75201784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
75301784826SJohnathan Mantey         [asyncResp, ifaceId, address,
75401784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
75501784826SJohnathan Mantey         if (ec)
75601784826SJohnathan Mantey         {
75701784826SJohnathan Mantey             messages::internalError(asyncResp->res);
75801784826SJohnathan Mantey         }
75901784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
76023a21a1cSEd Tanous             [asyncResp](const boost::system::error_code ec2) {
76123a21a1cSEd Tanous             if (ec2)
76201784826SJohnathan Mantey             {
76301784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
76401784826SJohnathan Mantey             }
76501784826SJohnathan Mantey             },
76601784826SJohnathan Mantey             "xyz.openbmc_project.Network",
76701784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
76801784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
76901784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
77001784826SJohnathan Mantey             prefixLength, "");
77101784826SJohnathan Mantey         },
77201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
77301784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
77401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
77501784826SJohnathan Mantey }
77601784826SJohnathan Mantey 
77701784826SJohnathan Mantey /**
778e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
779e48c0fc5SRavi Teja  *
780e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
781e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
782e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
783e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
784e48c0fc5SRavi Teja  *
785e48c0fc5SRavi Teja  * @return None
786e48c0fc5SRavi Teja  */
78701784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
78801784826SJohnathan Mantey                        const std::string& address,
7898d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
790e48c0fc5SRavi Teja {
791e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
792e48c0fc5SRavi Teja         if (ec)
793e48c0fc5SRavi Teja         {
794e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
795e48c0fc5SRavi Teja         }
796e48c0fc5SRavi Teja     };
797e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
7984e0453b1SGunnar Mills     // does not have associated gateway property
799e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
800e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
801e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
802e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
803e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
804e48c0fc5SRavi Teja         "");
805e48c0fc5SRavi Teja }
806e48c0fc5SRavi Teja 
807179db1d7SKowalski, Kamil /**
808179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
809179db1d7SKowalski, Kamil  * Object
810179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8114a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
812179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
813179db1d7SKowalski, Kamil  * into JSON
814179db1d7SKowalski, Kamil  */
815179db1d7SKowalski, Kamil template <typename CallbackFunc>
81681ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
8171abe55efSEd Tanous                           CallbackFunc&& callback)
8181abe55efSEd Tanous {
81955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
820f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
821f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
82281ce609eSEd Tanous             const boost::system::error_code errorCode,
82302cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
82455c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
8254a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
826e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
827179db1d7SKowalski, Kamil 
82881ce609eSEd Tanous         if (errorCode)
8291abe55efSEd Tanous         {
83001784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
831179db1d7SKowalski, Kamil             return;
832179db1d7SKowalski, Kamil         }
833179db1d7SKowalski, Kamil 
834002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
8354c9afe43SEd Tanous         if (!found)
8364c9afe43SEd Tanous         {
83701784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8384c9afe43SEd Tanous             return;
8394c9afe43SEd Tanous         }
8404c9afe43SEd Tanous 
8412c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
842179db1d7SKowalski, Kamil         // Fix global GW
8431abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8441abe55efSEd Tanous         {
845c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
846c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8479010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8481abe55efSEd Tanous             {
84982695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
850179db1d7SKowalski, Kamil             }
851179db1d7SKowalski, Kamil         }
852179db1d7SKowalski, Kamil 
8532c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8544e0453b1SGunnar Mills         // Finally make a callback with useful data
85501784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
856179db1d7SKowalski, Kamil         },
857179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
858179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
859271584abSEd Tanous }
860179db1d7SKowalski, Kamil 
861179db1d7SKowalski, Kamil /**
8629391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
8639391bb9cSRapkiewicz, Pawel  * Manager
8641abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
8651abe55efSEd Tanous  * into JSON.
8669391bb9cSRapkiewicz, Pawel  */
8679391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
8681abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
8691abe55efSEd Tanous {
87055c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
871f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
87281ce609eSEd Tanous             const boost::system::error_code errorCode,
873711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
8741abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
8751abe55efSEd Tanous         // ethernet interfaces
8762c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
8772c70f800SEd Tanous         ifaceList.reserve(resp.size());
87881ce609eSEd Tanous         if (errorCode)
8791abe55efSEd Tanous         {
8802c70f800SEd Tanous             callback(false, ifaceList);
8819391bb9cSRapkiewicz, Pawel             return;
8829391bb9cSRapkiewicz, Pawel         }
8839391bb9cSRapkiewicz, Pawel 
8849391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
8854a0cb85cSEd Tanous         for (const auto& objpath : resp)
8861abe55efSEd Tanous         {
8879391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
8884a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
8891abe55efSEd Tanous             {
8901abe55efSEd Tanous                 // If interface is
8914a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
8924a0cb85cSEd Tanous                 // what we're looking for.
8939391bb9cSRapkiewicz, Pawel                 if (interface.first ==
8941abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
8951abe55efSEd Tanous                 {
8962dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
8972dfd18efSEd Tanous                     if (ifaceId.empty())
8981abe55efSEd Tanous                     {
8992dfd18efSEd Tanous                         continue;
9009391bb9cSRapkiewicz, Pawel                     }
9012dfd18efSEd Tanous                     // and put it into output vector.
9022dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
9039391bb9cSRapkiewicz, Pawel                 }
9049391bb9cSRapkiewicz, Pawel             }
9059391bb9cSRapkiewicz, Pawel         }
906a434f2bdSEd Tanous         // Finally make a callback with useful data
9072c70f800SEd Tanous         callback(true, ifaceList);
9089391bb9cSRapkiewicz, Pawel         },
909aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
910aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
911271584abSEd Tanous }
9129391bb9cSRapkiewicz, Pawel 
9134f48d5f6SEd Tanous inline void
9144f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
9158d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
9161abe55efSEd Tanous {
917ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
918ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
919ab6554f1SJoshi-Mansi     {
920ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
921ab6554f1SJoshi-Mansi                                            "HostName");
922ab6554f1SJoshi-Mansi         return;
923ab6554f1SJoshi-Mansi     }
924bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
925bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
9264a0cb85cSEd Tanous         if (ec)
9274a0cb85cSEd Tanous         {
928a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
9291abe55efSEd Tanous         }
930bc0bd6e0SEd Tanous         },
931bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
932bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
933bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
934168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
935588c3f0dSKowalski, Kamil }
936588c3f0dSKowalski, Kamil 
9374f48d5f6SEd Tanous inline void
93835fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
93935fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
94035fb5311STejas Patil {
94135fb5311STejas Patil     sdbusplus::message::object_path objPath =
94235fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
94335fb5311STejas Patil     crow::connections::systemBus->async_method_call(
94435fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
94535fb5311STejas Patil         if (ec)
94635fb5311STejas Patil         {
94735fb5311STejas Patil             messages::internalError(asyncResp->res);
94835fb5311STejas Patil         }
94935fb5311STejas Patil         },
95035fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
95135fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
95235fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
95335fb5311STejas Patil         std::variant<size_t>(mtuSize));
95435fb5311STejas Patil }
95535fb5311STejas Patil 
95635fb5311STejas Patil inline void
9574f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
958bf648f77SEd Tanous                           const std::string& domainname,
9598d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
960ab6554f1SJoshi-Mansi {
961ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
962ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
963ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
964ab6554f1SJoshi-Mansi         if (ec)
965ab6554f1SJoshi-Mansi         {
966ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
967ab6554f1SJoshi-Mansi         }
968ab6554f1SJoshi-Mansi         },
969ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
970ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
971ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
972ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
973168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
974ab6554f1SJoshi-Mansi }
975ab6554f1SJoshi-Mansi 
9764f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
977bf648f77SEd Tanous {
978bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
979bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
980bf648f77SEd Tanous     {
981bf648f77SEd Tanous         return false;
982bf648f77SEd Tanous     }
983bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
984bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
985bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
986bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
987bf648f77SEd Tanous     const std::regex pattern(
988bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
989bf648f77SEd Tanous 
990bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
991bf648f77SEd Tanous }
992bf648f77SEd Tanous 
9934f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
994bf648f77SEd Tanous {
995bf648f77SEd Tanous     // Can have multiple subdomains
996bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
9970fda0f12SGeorge Liu     const std::regex pattern(
9980fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
999bf648f77SEd Tanous 
1000bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1001bf648f77SEd Tanous }
1002bf648f77SEd Tanous 
10034f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10048d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1005ab6554f1SJoshi-Mansi {
1006ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1007ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1008ab6554f1SJoshi-Mansi     {
1009ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1010ab6554f1SJoshi-Mansi         return;
1011ab6554f1SJoshi-Mansi     }
1012ab6554f1SJoshi-Mansi 
1013ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1014ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1015ab6554f1SJoshi-Mansi     {
1016ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1017ab6554f1SJoshi-Mansi         return;
1018ab6554f1SJoshi-Mansi     }
1019ab6554f1SJoshi-Mansi 
1020ab6554f1SJoshi-Mansi     std::string hostname;
1021ab6554f1SJoshi-Mansi     std::string domainname;
1022ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1023ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1024ab6554f1SJoshi-Mansi 
1025ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1026ab6554f1SJoshi-Mansi     {
1027ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1028ab6554f1SJoshi-Mansi         return;
1029ab6554f1SJoshi-Mansi     }
1030ab6554f1SJoshi-Mansi 
1031ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1032ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1033ab6554f1SJoshi-Mansi }
1034ab6554f1SJoshi-Mansi 
10354f48d5f6SEd Tanous inline void
10364f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1037bf648f77SEd Tanous                           const std::string& macAddress,
10388d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1039d577665bSRatan Gupta {
104058283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
104158283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
104258283f41SJohnathan Mantey 
1043d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
104458283f41SJohnathan Mantey         [asyncResp, macAddress](const boost::system::error_code ec,
10455b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1046d577665bSRatan Gupta         if (ec)
1047d577665bSRatan Gupta         {
104858283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
104958283f41SJohnathan Mantey             if (err == nullptr)
105058283f41SJohnathan Mantey             {
105158283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
105258283f41SJohnathan Mantey                 return;
105358283f41SJohnathan Mantey             }
105458283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
105558283f41SJohnathan Mantey             {
105658283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
105758283f41SJohnathan Mantey                 return;
105858283f41SJohnathan Mantey             }
1059d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1060d577665bSRatan Gupta             return;
1061d577665bSRatan Gupta         }
1062d577665bSRatan Gupta         },
1063d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1064d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1065d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1066d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1067168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1068d577665bSRatan Gupta }
1069286b9118SJohnathan Mantey 
10704f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
10714f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
10724f48d5f6SEd Tanous                            const bool v6Value,
10738d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1074da131a9aSJennifer Lee {
10752c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1076da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1077da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1078da131a9aSJennifer Lee         if (ec)
1079da131a9aSJennifer Lee         {
1080da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1081da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1082da131a9aSJennifer Lee             return;
1083da131a9aSJennifer Lee         }
10848f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1085da131a9aSJennifer Lee         },
1086da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1087da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1088da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1089da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1090168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1091da131a9aSJennifer Lee }
10921f8c7b5dSJohnathan Mantey 
10934f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1094eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
10958d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1096eeedda23SJohnathan Mantey {
1097eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1098eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1099eeedda23SJohnathan Mantey         if (ec)
1100eeedda23SJohnathan Mantey         {
1101eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1102eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1103eeedda23SJohnathan Mantey             return;
1104eeedda23SJohnathan Mantey         }
1105eeedda23SJohnathan Mantey         },
1106eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1107eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1108eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1109eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1110168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1111eeedda23SJohnathan Mantey }
1112eeedda23SJohnathan Mantey 
11134f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11148d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1115da131a9aSJennifer Lee {
1116da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1117da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1118da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1119da131a9aSJennifer Lee         if (ec)
1120da131a9aSJennifer Lee         {
1121da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1122da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1123da131a9aSJennifer Lee             return;
1124da131a9aSJennifer Lee         }
1125da131a9aSJennifer Lee         },
11261e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1127da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1128da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1129168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1130da131a9aSJennifer Lee }
1131d577665bSRatan Gupta 
11324f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
11331f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1134f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1135f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
11368d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1137da131a9aSJennifer Lee {
113882695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
113982695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1140da131a9aSJennifer Lee 
11411f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11421f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11431f8c7b5dSJohnathan Mantey 
11441f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11451f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1146da131a9aSJennifer Lee     {
11471f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11481f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11491f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11501f8c7b5dSJohnathan Mantey         {
1151bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1152bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11531f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1154da131a9aSJennifer Lee             return;
1155da131a9aSJennifer Lee         }
11561f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11571f8c7b5dSJohnathan Mantey     }
11581f8c7b5dSJohnathan Mantey     else
1159da131a9aSJennifer Lee     {
11601f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
11611f8c7b5dSJohnathan Mantey     }
11621f8c7b5dSJohnathan Mantey 
11631f8c7b5dSJohnathan Mantey     bool nextDNS{};
116482695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
11651f8c7b5dSJohnathan Mantey     {
116682695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
11671f8c7b5dSJohnathan Mantey         {
11681f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11691f8c7b5dSJohnathan Mantey             return;
11701f8c7b5dSJohnathan Mantey         }
117182695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11721f8c7b5dSJohnathan Mantey     }
117382695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
11741f8c7b5dSJohnathan Mantey     {
117582695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11761f8c7b5dSJohnathan Mantey     }
117782695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
11781f8c7b5dSJohnathan Mantey     {
117982695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
11801f8c7b5dSJohnathan Mantey     }
11811f8c7b5dSJohnathan Mantey     else
11821f8c7b5dSJohnathan Mantey     {
118382695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
11841f8c7b5dSJohnathan Mantey     }
11851f8c7b5dSJohnathan Mantey 
11861f8c7b5dSJohnathan Mantey     bool nextNTP{};
118782695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
11881f8c7b5dSJohnathan Mantey     {
118982695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
11901f8c7b5dSJohnathan Mantey         {
11911f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11921f8c7b5dSJohnathan Mantey             return;
11931f8c7b5dSJohnathan Mantey         }
119482695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11951f8c7b5dSJohnathan Mantey     }
119682695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
11971f8c7b5dSJohnathan Mantey     {
119882695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11991f8c7b5dSJohnathan Mantey     }
120082695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12011f8c7b5dSJohnathan Mantey     {
120282695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12031f8c7b5dSJohnathan Mantey     }
12041f8c7b5dSJohnathan Mantey     else
12051f8c7b5dSJohnathan Mantey     {
120682695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12071f8c7b5dSJohnathan Mantey     }
12081f8c7b5dSJohnathan Mantey 
12091f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
121082695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12111f8c7b5dSJohnathan Mantey     {
121282695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
12131f8c7b5dSJohnathan Mantey         {
12141f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12151f8c7b5dSJohnathan Mantey             return;
12161f8c7b5dSJohnathan Mantey         }
121782695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12181f8c7b5dSJohnathan Mantey     }
121982695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12201f8c7b5dSJohnathan Mantey     {
122182695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12221f8c7b5dSJohnathan Mantey     }
122382695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12241f8c7b5dSJohnathan Mantey     {
122582695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12261f8c7b5dSJohnathan Mantey     }
12271f8c7b5dSJohnathan Mantey     else
12281f8c7b5dSJohnathan Mantey     {
122982695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
12301f8c7b5dSJohnathan Mantey     }
12311f8c7b5dSJohnathan Mantey 
1232da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
12331f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
12341f8c7b5dSJohnathan Mantey                    asyncResp);
1235da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
12361f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1237da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
12381f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
12391f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12401f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1241da131a9aSJennifer Lee }
124201784826SJohnathan Mantey 
12434f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
12442c70f800SEd Tanous     getNextStaticIpEntry(
1245bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1246bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
124701784826SJohnathan Mantey {
124817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
124917a897dfSManojkiran Eda         return value.origin == "Static";
125017a897dfSManojkiran Eda     });
125101784826SJohnathan Mantey }
125201784826SJohnathan Mantey 
12534f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
12542c70f800SEd Tanous     getNextStaticIpEntry(
1255bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1256bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
125701784826SJohnathan Mantey {
125817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
125917a897dfSManojkiran Eda         return value.origin == "Static";
126017a897dfSManojkiran Eda     });
126101784826SJohnathan Mantey }
126201784826SJohnathan Mantey 
12634f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1264f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
126501784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
12668d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12671abe55efSEd Tanous {
126801784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1269f476acbfSRatan Gupta     {
127071f52d96SEd Tanous         messages::propertyValueTypeError(
127171f52d96SEd Tanous             asyncResp->res,
1272bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1273d1d50814SRavi Teja             "IPv4StaticAddresses");
1274f476acbfSRatan Gupta         return;
1275f476acbfSRatan Gupta     }
1276f476acbfSRatan Gupta 
1277271584abSEd Tanous     unsigned entryIdx = 1;
127801784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
127901784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
128001784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
128101784826SJohnathan Mantey     // into the NIC.
128285ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
12832c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
128401784826SJohnathan Mantey 
1285537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12861abe55efSEd Tanous     {
12874a0cb85cSEd Tanous         std::string pathString =
1288d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1289179db1d7SKowalski, Kamil 
129001784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1291f476acbfSRatan Gupta         {
1292537174c4SEd Tanous             std::optional<std::string> address;
1293537174c4SEd Tanous             std::optional<std::string> subnetMask;
1294537174c4SEd Tanous             std::optional<std::string> gateway;
1295537174c4SEd Tanous 
1296537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12977e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12987e27d832SJohnathan Mantey                                      "Gateway", gateway))
1299537174c4SEd Tanous             {
130001784826SJohnathan Mantey                 messages::propertyValueFormatError(
130171f52d96SEd Tanous                     asyncResp->res,
130271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
130371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
130471f52d96SEd Tanous                     pathString);
1305537174c4SEd Tanous                 return;
1306179db1d7SKowalski, Kamil             }
1307179db1d7SKowalski, Kamil 
130801784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
130901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
131001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
131101784826SJohnathan Mantey             // current request.
1312271584abSEd Tanous             const std::string* addr = nullptr;
1313271584abSEd Tanous             const std::string* gw = nullptr;
131401784826SJohnathan Mantey             uint8_t prefixLength = 0;
131501784826SJohnathan Mantey             bool errorInEntry = false;
1316537174c4SEd Tanous             if (address)
13171abe55efSEd Tanous             {
1318033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
13191abe55efSEd Tanous                 {
132001784826SJohnathan Mantey                     addr = &(*address);
13214a0cb85cSEd Tanous                 }
132201784826SJohnathan Mantey                 else
132301784826SJohnathan Mantey                 {
1324bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1325bf648f77SEd Tanous                                                        pathString + "/Address");
132601784826SJohnathan Mantey                     errorInEntry = true;
132701784826SJohnathan Mantey                 }
132801784826SJohnathan Mantey             }
132985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
133001784826SJohnathan Mantey             {
133185ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
133201784826SJohnathan Mantey             }
133301784826SJohnathan Mantey             else
133401784826SJohnathan Mantey             {
133501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
133601784826SJohnathan Mantey                                           pathString + "/Address");
133701784826SJohnathan Mantey                 errorInEntry = true;
13384a0cb85cSEd Tanous             }
13394a0cb85cSEd Tanous 
1340537174c4SEd Tanous             if (subnetMask)
13414a0cb85cSEd Tanous             {
1342033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1343033f1e4dSEd Tanous                                                          &prefixLength))
13444a0cb85cSEd Tanous                 {
1345f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1346537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13474a0cb85cSEd Tanous                         pathString + "/SubnetMask");
134801784826SJohnathan Mantey                     errorInEntry = true;
13494a0cb85cSEd Tanous                 }
13504a0cb85cSEd Tanous             }
135185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13524a0cb85cSEd Tanous             {
1353033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
135401784826SJohnathan Mantey                                                          &prefixLength))
13554a0cb85cSEd Tanous                 {
135601784826SJohnathan Mantey                     messages::propertyValueFormatError(
135785ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
135801784826SJohnathan Mantey                         pathString + "/SubnetMask");
135901784826SJohnathan Mantey                     errorInEntry = true;
13604a0cb85cSEd Tanous                 }
13614a0cb85cSEd Tanous             }
13621abe55efSEd Tanous             else
13631abe55efSEd Tanous             {
136401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
136501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
136601784826SJohnathan Mantey                 errorInEntry = true;
136701784826SJohnathan Mantey             }
136801784826SJohnathan Mantey 
136901784826SJohnathan Mantey             if (gateway)
137001784826SJohnathan Mantey             {
1371033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
137201784826SJohnathan Mantey                 {
137301784826SJohnathan Mantey                     gw = &(*gateway);
137401784826SJohnathan Mantey                 }
137501784826SJohnathan Mantey                 else
137601784826SJohnathan Mantey                 {
1377bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1378bf648f77SEd Tanous                                                        pathString + "/Gateway");
137901784826SJohnathan Mantey                     errorInEntry = true;
138001784826SJohnathan Mantey                 }
138101784826SJohnathan Mantey             }
138285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
138301784826SJohnathan Mantey             {
138485ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
138501784826SJohnathan Mantey             }
138601784826SJohnathan Mantey             else
13871abe55efSEd Tanous             {
1388a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13894a0cb85cSEd Tanous                                           pathString + "/Gateway");
139001784826SJohnathan Mantey                 errorInEntry = true;
13914a0cb85cSEd Tanous             }
13924a0cb85cSEd Tanous 
139301784826SJohnathan Mantey             if (errorInEntry)
13941abe55efSEd Tanous             {
139501784826SJohnathan Mantey                 return;
13964a0cb85cSEd Tanous             }
13974a0cb85cSEd Tanous 
139885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13991abe55efSEd Tanous             {
140085ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1401bf648f77SEd Tanous                                     *addr, asyncResp);
140285ffe86aSJiaqing Zhao                 nicIpEntry =
140385ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1404588c3f0dSKowalski, Kamil             }
140501784826SJohnathan Mantey             else
140601784826SJohnathan Mantey             {
1407cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1408cb13a392SEd Tanous                            asyncResp);
14094a0cb85cSEd Tanous             }
14104a0cb85cSEd Tanous             entryIdx++;
14114a0cb85cSEd Tanous         }
141201784826SJohnathan Mantey         else
141301784826SJohnathan Mantey         {
141485ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
141501784826SJohnathan Mantey             {
141601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
141701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
141801784826SJohnathan Mantey                 // in error, so bail out.
141901784826SJohnathan Mantey                 if (thisJson.is_null())
142001784826SJohnathan Mantey                 {
142101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
142201784826SJohnathan Mantey                     return;
142301784826SJohnathan Mantey                 }
142401784826SJohnathan Mantey                 messages::propertyValueFormatError(
142571f52d96SEd Tanous                     asyncResp->res,
142671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
142771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
142871f52d96SEd Tanous                     pathString);
142901784826SJohnathan Mantey                 return;
143001784826SJohnathan Mantey             }
143101784826SJohnathan Mantey 
143201784826SJohnathan Mantey             if (thisJson.is_null())
143301784826SJohnathan Mantey             {
143485ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
143501784826SJohnathan Mantey             }
143685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
143701784826SJohnathan Mantey             {
143885ffe86aSJiaqing Zhao                 nicIpEntry =
143985ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
144001784826SJohnathan Mantey             }
144101784826SJohnathan Mantey             entryIdx++;
144201784826SJohnathan Mantey         }
144301784826SJohnathan Mantey     }
14444a0cb85cSEd Tanous }
14454a0cb85cSEd Tanous 
14464f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1447f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1448f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14498d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1450f85837bfSRAJESWARAN THILLAIGOVINDAN {
1451f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1452286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1453f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1454f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1455f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1456f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1457f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1458f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1459f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1460f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1461f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1462bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1463168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1464f85837bfSRAJESWARAN THILLAIGOVINDAN }
1465f85837bfSRAJESWARAN THILLAIGOVINDAN 
14664f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1467f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
146801784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
14698d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1470e48c0fc5SRavi Teja {
147101784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1472e48c0fc5SRavi Teja     {
147371f52d96SEd Tanous         messages::propertyValueTypeError(
147471f52d96SEd Tanous             asyncResp->res,
1475bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1476e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1477e48c0fc5SRavi Teja         return;
1478e48c0fc5SRavi Teja     }
1479271584abSEd Tanous     size_t entryIdx = 1;
148085ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
14812c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1482f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1483e48c0fc5SRavi Teja     {
1484e48c0fc5SRavi Teja         std::string pathString =
1485e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1486e48c0fc5SRavi Teja 
148701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1488e48c0fc5SRavi Teja         {
1489e48c0fc5SRavi Teja             std::optional<std::string> address;
1490e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1491f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1492bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1493bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1494e48c0fc5SRavi Teja             {
149501784826SJohnathan Mantey                 messages::propertyValueFormatError(
149671f52d96SEd Tanous                     asyncResp->res,
149771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
149871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
149971f52d96SEd Tanous                     pathString);
1500e48c0fc5SRavi Teja                 return;
1501e48c0fc5SRavi Teja             }
1502e48c0fc5SRavi Teja 
1503543f4400SEd Tanous             const std::string* addr = nullptr;
1504543f4400SEd Tanous             uint8_t prefix = 0;
150501784826SJohnathan Mantey 
150601784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
150701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
150801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
150901784826SJohnathan Mantey             // current request.
1510e48c0fc5SRavi Teja             if (address)
1511e48c0fc5SRavi Teja             {
151201784826SJohnathan Mantey                 addr = &(*address);
1513e48c0fc5SRavi Teja             }
151485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
151501784826SJohnathan Mantey             {
151685ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
151701784826SJohnathan Mantey             }
151801784826SJohnathan Mantey             else
151901784826SJohnathan Mantey             {
152001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
152101784826SJohnathan Mantey                                           pathString + "/Address");
152201784826SJohnathan Mantey                 return;
1523e48c0fc5SRavi Teja             }
1524e48c0fc5SRavi Teja 
1525e48c0fc5SRavi Teja             if (prefixLength)
1526e48c0fc5SRavi Teja             {
152701784826SJohnathan Mantey                 prefix = *prefixLength;
152801784826SJohnathan Mantey             }
152985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1530e48c0fc5SRavi Teja             {
153185ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1532e48c0fc5SRavi Teja             }
1533e48c0fc5SRavi Teja             else
1534e48c0fc5SRavi Teja             {
1535e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1536e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
153701784826SJohnathan Mantey                 return;
1538e48c0fc5SRavi Teja             }
1539e48c0fc5SRavi Teja 
154085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1541e48c0fc5SRavi Teja             {
154285ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1543e48c0fc5SRavi Teja                                     asyncResp);
154485ffe86aSJiaqing Zhao                 nicIpEntry =
154585ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
154601784826SJohnathan Mantey             }
154701784826SJohnathan Mantey             else
154801784826SJohnathan Mantey             {
154901784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1550e48c0fc5SRavi Teja             }
1551e48c0fc5SRavi Teja             entryIdx++;
1552e48c0fc5SRavi Teja         }
155301784826SJohnathan Mantey         else
155401784826SJohnathan Mantey         {
155585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
155601784826SJohnathan Mantey             {
155701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
155801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
155901784826SJohnathan Mantey                 // in error, so bail out.
156001784826SJohnathan Mantey                 if (thisJson.is_null())
156101784826SJohnathan Mantey                 {
156201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
156301784826SJohnathan Mantey                     return;
156401784826SJohnathan Mantey                 }
156501784826SJohnathan Mantey                 messages::propertyValueFormatError(
156671f52d96SEd Tanous                     asyncResp->res,
156771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
156871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
156971f52d96SEd Tanous                     pathString);
157001784826SJohnathan Mantey                 return;
157101784826SJohnathan Mantey             }
157201784826SJohnathan Mantey 
157301784826SJohnathan Mantey             if (thisJson.is_null())
157401784826SJohnathan Mantey             {
157585ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
157601784826SJohnathan Mantey             }
157785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
157801784826SJohnathan Mantey             {
157985ffe86aSJiaqing Zhao                 nicIpEntry =
158085ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
158101784826SJohnathan Mantey             }
158201784826SJohnathan Mantey             entryIdx++;
158301784826SJohnathan Mantey         }
158401784826SJohnathan Mantey     }
1585e48c0fc5SRavi Teja }
1586e48c0fc5SRavi Teja 
15874f48d5f6SEd Tanous inline void parseInterfaceData(
15888d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15898d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1590e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
159101784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
15924a0cb85cSEd Tanous {
1593*7a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1594eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1595eeedda23SJohnathan Mantey 
15962c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
159781ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
15982c70f800SEd Tanous     jsonResponse["@odata.id"] =
159981ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16002c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1601eeedda23SJohnathan Mantey 
1602eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1603eeedda23SJohnathan Mantey 
1604*7a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
1605*7a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
1606*7a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1607b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1608eeedda23SJohnathan Mantey         if (ec)
1609029573d4SEd Tanous         {
1610eeedda23SJohnathan Mantey             return;
1611eeedda23SJohnathan Mantey         }
1612eeedda23SJohnathan Mantey 
1613914e2d5dSEd Tanous         health->inventory = resp;
1614*7a1dbc48SGeorge Liu         });
1615eeedda23SJohnathan Mantey 
1616eeedda23SJohnathan Mantey     health->populate();
1617eeedda23SJohnathan Mantey 
1618eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1619eeedda23SJohnathan Mantey     {
16200ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
16212c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1622029573d4SEd Tanous     }
1623029573d4SEd Tanous     else
1624029573d4SEd Tanous     {
16252c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
16262c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1627029573d4SEd Tanous     }
1628aa05fb27SJohnathan Mantey 
16292c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
163035fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
163182695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
16322c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
163382695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
163482695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
163582695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
163682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16371f8c7b5dSJohnathan Mantey 
16382c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
163982695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16401f8c7b5dSJohnathan Mantey                                                                : "Disabled";
164182695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
164282695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
164382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16442a133282Smanojkiraneda 
164582695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16464a0cb85cSEd Tanous     {
164782695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1648ab6554f1SJoshi-Mansi 
1649ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1650ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1651ab6554f1SJoshi-Mansi         // FQDN
165282695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1653d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1654d24bfc7aSJennifer Lee         {
16552c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1656d24bfc7aSJennifer Lee         }
16572c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16584a0cb85cSEd Tanous     }
16594a0cb85cSEd Tanous 
1660613dabeaSEd Tanous     jsonResponse["VLANs"]["@odata.id"] =
1661613dabeaSEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1662613dabeaSEd Tanous                                      "EthernetInterfaces", ifaceId, "VLANs");
1663fda13ad2SSunitha Harish 
16642c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
16652c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
16664a0cb85cSEd Tanous 
16672c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
16682c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
16692c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
16702c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
16719eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
16724a0cb85cSEd Tanous     {
16732c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1674fa5053a6SGunnar Mills         if (gatewayStr.empty())
1675fa5053a6SGunnar Mills         {
1676fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1677fa5053a6SGunnar Mills         }
16781476687dSEd Tanous         nlohmann::json::object_t ipv4;
16791476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
16801476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
16811476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
16821476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1683fa5053a6SGunnar Mills 
16842c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1685d1d50814SRavi Teja         {
16861476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1687d1d50814SRavi Teja         }
16881476687dSEd Tanous 
16891476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
169001784826SJohnathan Mantey     }
1691d1d50814SRavi Teja 
169282695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
16937ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
16947ea79e5eSRavi Teja     {
16957ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
16967ea79e5eSRavi Teja     }
16977ea79e5eSRavi Teja 
16987ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1699e48c0fc5SRavi Teja 
17002c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17012c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17022c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17032c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17047f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17052c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17067f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17079eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1708e48c0fc5SRavi Teja     {
17091476687dSEd Tanous         nlohmann::json::object_t ipv6;
17101476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
17111476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
17121476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
17131476687dSEd Tanous         ipv6["AddressState"] = nullptr;
17141476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
17152c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1716e48c0fc5SRavi Teja         {
17171476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
17181476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
17191476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
17201476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
172101784826SJohnathan Mantey         }
1722e48c0fc5SRavi Teja     }
1723588c3f0dSKowalski, Kamil }
1724588c3f0dSKowalski, Kamil 
17254f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1726bf648f77SEd Tanous {
172711ba3979SEd Tanous     return iface.starts_with(parent + "_");
1728bf648f77SEd Tanous }
1729bf648f77SEd Tanous 
1730bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1731bf648f77SEd Tanous {
1732bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1733ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
17341476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17351476687dSEd Tanous             [&app](const crow::Request& req,
17361476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
173845ca1b86SEd Tanous         {
173945ca1b86SEd Tanous             return;
174045ca1b86SEd Tanous         }
174145ca1b86SEd Tanous 
1742bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1743bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1744bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1745bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1746bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1747bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1748bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1749bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1750bf648f77SEd Tanous 
1751bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1752bf648f77SEd Tanous         // preparation
1753002d39b4SEd Tanous         getEthernetIfaceList(
1754002d39b4SEd Tanous             [asyncResp](
17551476687dSEd Tanous                 const bool& success,
1756002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1757bf648f77SEd Tanous             if (!success)
17581abe55efSEd Tanous             {
1759f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17609391bb9cSRapkiewicz, Pawel                 return;
17619391bb9cSRapkiewicz, Pawel             }
17629391bb9cSRapkiewicz, Pawel 
1763002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1764bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1765bf648f77SEd Tanous             std::string tag = "_";
1766bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1767bf648f77SEd Tanous             {
1768bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1769bf648f77SEd Tanous                 if (found == std::string::npos)
1770bf648f77SEd Tanous                 {
17711476687dSEd Tanous                     nlohmann::json::object_t iface;
17721476687dSEd Tanous                     iface["@odata.id"] =
1773bf648f77SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17741476687dSEd Tanous                         ifaceItem;
17751476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
1776bf648f77SEd Tanous                 }
1777bf648f77SEd Tanous             }
1778bf648f77SEd Tanous 
1779002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1780bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1781bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1782bf648f77SEd Tanous         });
1783bf648f77SEd Tanous         });
1784bf648f77SEd Tanous 
1785bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1786ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1787bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
178845ca1b86SEd Tanous             [&app](const crow::Request& req,
1789bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1790bf648f77SEd Tanous                    const std::string& ifaceId) {
17913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179245ca1b86SEd Tanous         {
179345ca1b86SEd Tanous             return;
179445ca1b86SEd Tanous         }
17954a0cb85cSEd Tanous         getEthernetIfaceData(
1796bf648f77SEd Tanous             ifaceId,
1797002d39b4SEd Tanous             [asyncResp, ifaceId](
1798002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1799002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1800002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18014a0cb85cSEd Tanous             if (!success)
18021abe55efSEd Tanous             {
1803bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1804bf648f77SEd Tanous                 // existing object, and other errors
1805002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1806002d39b4SEd Tanous                                            ifaceId);
18074a0cb85cSEd Tanous                 return;
18089391bb9cSRapkiewicz, Pawel             }
18094c9afe43SEd Tanous 
1810188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1811188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
18120f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1813188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1814002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
18150f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
18160f74e643SEd Tanous                 "Management Network Interface";
18170f74e643SEd Tanous 
1818002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
18199391bb9cSRapkiewicz, Pawel             });
1820bf648f77SEd Tanous         });
18219391bb9cSRapkiewicz, Pawel 
1822bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1823ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1824bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
182545ca1b86SEd Tanous             [&app](const crow::Request& req,
1826bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1827bf648f77SEd Tanous                    const std::string& ifaceId) {
18283ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
182945ca1b86SEd Tanous         {
183045ca1b86SEd Tanous             return;
183145ca1b86SEd Tanous         }
1832bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1833ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1834d577665bSRatan Gupta         std::optional<std::string> macAddress;
18359a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1836d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1837e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1838f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1839da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
18401f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1841eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
184235fb5311STejas Patil         std::optional<size_t> mtuSize;
18431f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18441f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18450627a2c7SEd Tanous 
184615ed6780SWilly Tu         if (!json_util::readJsonPatch(
18478d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1848002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1849002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1850002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1851ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1852002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18531abe55efSEd Tanous         {
1854588c3f0dSKowalski, Kamil             return;
1855588c3f0dSKowalski, Kamil         }
1856da131a9aSJennifer Lee         if (dhcpv4)
1857da131a9aSJennifer Lee         {
1858002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
18591f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
186082695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
186182695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
186282695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
18631f8c7b5dSJohnathan Mantey             {
18641f8c7b5dSJohnathan Mantey                 return;
18651f8c7b5dSJohnathan Mantey             }
18661f8c7b5dSJohnathan Mantey         }
18671f8c7b5dSJohnathan Mantey 
18681f8c7b5dSJohnathan Mantey         if (dhcpv6)
18691f8c7b5dSJohnathan Mantey         {
1870002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1871002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1872002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1873002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1874002d39b4SEd Tanous                                      "UseDomainName",
187582695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
18761f8c7b5dSJohnathan Mantey             {
18771f8c7b5dSJohnathan Mantey                 return;
18781f8c7b5dSJohnathan Mantey             }
1879da131a9aSJennifer Lee         }
1880da131a9aSJennifer Lee 
1881bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1882bf648f77SEd Tanous         // for JSON preparation
18834a0cb85cSEd Tanous         getEthernetIfaceData(
18842c70f800SEd Tanous             ifaceId,
1885bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1886ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1887d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
18889a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1889e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
18901f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1891bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1892bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1893f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1894002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1895002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1896002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18971abe55efSEd Tanous             if (!success)
18981abe55efSEd Tanous             {
1899588c3f0dSKowalski, Kamil                 // ... otherwise return error
1900bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1901bf648f77SEd Tanous                 // existing object, and other errors
1902002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1903002d39b4SEd Tanous                                            ifaceId);
1904588c3f0dSKowalski, Kamil                 return;
1905588c3f0dSKowalski, Kamil             }
1906588c3f0dSKowalski, Kamil 
19071f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
19081f8c7b5dSJohnathan Mantey             {
1909002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1910002d39b4SEd Tanous                                 asyncResp);
19111f8c7b5dSJohnathan Mantey             }
19121f8c7b5dSJohnathan Mantey 
19130627a2c7SEd Tanous             if (hostname)
19141abe55efSEd Tanous             {
19150627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
19161abe55efSEd Tanous             }
19170627a2c7SEd Tanous 
1918ab6554f1SJoshi-Mansi             if (fqdn)
1919ab6554f1SJoshi-Mansi             {
19202c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1921ab6554f1SJoshi-Mansi             }
1922ab6554f1SJoshi-Mansi 
1923d577665bSRatan Gupta             if (macAddress)
1924d577665bSRatan Gupta             {
1925002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1926d577665bSRatan Gupta             }
1927d577665bSRatan Gupta 
1928d1d50814SRavi Teja             if (ipv4StaticAddresses)
1929d1d50814SRavi Teja             {
1930bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1931bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1932bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1933bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1934bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1935bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1936bf648f77SEd Tanous                 // that, but could be done more efficiently
1937f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1938002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
19391abe55efSEd Tanous             }
19400627a2c7SEd Tanous 
1941f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1942f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1943002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1944002d39b4SEd Tanous                                              asyncResp);
1945f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19469a6fc6feSRavi Teja 
19479a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19489a6fc6feSRavi Teja             {
19499a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19509a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19519a6fc6feSRavi Teja             }
1952e48c0fc5SRavi Teja 
1953e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1954e48c0fc5SRavi Teja             {
1955002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1956002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1957002d39b4SEd Tanous                                                asyncResp);
1958e48c0fc5SRavi Teja             }
1959eeedda23SJohnathan Mantey 
1960eeedda23SJohnathan Mantey             if (interfaceEnabled)
1961eeedda23SJohnathan Mantey             {
1962002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1963002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
1964eeedda23SJohnathan Mantey             }
196535fb5311STejas Patil 
196635fb5311STejas Patil             if (mtuSize)
196735fb5311STejas Patil             {
196835fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
196935fb5311STejas Patil             }
1970588c3f0dSKowalski, Kamil             });
1971bf648f77SEd Tanous         });
19729391bb9cSRapkiewicz, Pawel 
1973bf648f77SEd Tanous     BMCWEB_ROUTE(
1974bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
1975ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
1976bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
197745ca1b86SEd Tanous             [&app](const crow::Request& req,
1978bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
197945ca1b86SEd Tanous                    const std::string& parentIfaceId,
198045ca1b86SEd Tanous                    const std::string& ifaceId) {
19813ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
198245ca1b86SEd Tanous         {
198345ca1b86SEd Tanous             return;
198445ca1b86SEd Tanous         }
19858d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
19860f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
19878d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
1988e439f0f8SKowalski, Kamil 
19892c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
19901abe55efSEd Tanous         {
1991a434f2bdSEd Tanous             return;
1992a434f2bdSEd Tanous         }
1993a434f2bdSEd Tanous 
1994bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1995bf648f77SEd Tanous         // for JSON preparation
19964a0cb85cSEd Tanous         getEthernetIfaceData(
1997bf648f77SEd Tanous             ifaceId,
1998002d39b4SEd Tanous             [asyncResp, parentIfaceId,
1999002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2000cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2001cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
200217e22024SJiaqing Zhao             if (success && ethData.vlanId)
20031abe55efSEd Tanous             {
200422872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
200522872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["@odata.id"] =
200622872ff3SJiaqing Zhao                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
200722872ff3SJiaqing Zhao                     parentIfaceId + "/VLANs/" + ifaceId;
200822872ff3SJiaqing Zhao 
200923a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
201022872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
20111abe55efSEd Tanous             }
20121abe55efSEd Tanous             else
20131abe55efSEd Tanous             {
2014e439f0f8SKowalski, Kamil                 // ... otherwise return error
2015bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2016bf648f77SEd Tanous                 // existing object, and other errors
2017bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2018d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2019e439f0f8SKowalski, Kamil             }
2020e439f0f8SKowalski, Kamil             });
2021bf648f77SEd Tanous         });
2022e439f0f8SKowalski, Kamil 
2023bf648f77SEd Tanous     BMCWEB_ROUTE(
2024bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20253d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2026bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
202745ca1b86SEd Tanous             [&app](const crow::Request& req,
2028bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
202945ca1b86SEd Tanous                    const std::string& parentIfaceId,
203045ca1b86SEd Tanous                    const std::string& ifaceId) {
20313ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
203245ca1b86SEd Tanous         {
203345ca1b86SEd Tanous             return;
203445ca1b86SEd Tanous         }
2035fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
20361abe55efSEd Tanous         {
2037d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2038002d39b4SEd Tanous                                        ifaceId);
2039927a505aSKowalski, Kamil             return;
2040927a505aSKowalski, Kamil         }
2041927a505aSKowalski, Kamil 
20423927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20433927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20440627a2c7SEd Tanous 
204515ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2046bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20471abe55efSEd Tanous         {
2048927a505aSKowalski, Kamil             return;
2049927a505aSKowalski, Kamil         }
2050927a505aSKowalski, Kamil 
20513927e13eSJiaqing Zhao         if (vlanId)
20523927e13eSJiaqing Zhao         {
20533927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20543927e13eSJiaqing Zhao             return;
20553927e13eSJiaqing Zhao         }
20563927e13eSJiaqing Zhao 
2057bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2058bf648f77SEd Tanous         // for JSON preparation
2059e48c0fc5SRavi Teja         getEthernetIfaceData(
2060bf648f77SEd Tanous             ifaceId,
20613927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2062002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2063cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2064cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
206517e22024SJiaqing Zhao             if (success && ethData.vlanId)
206608244d02SSunitha Harish             {
206723a06317SJiaqing Zhao                 if (vlanEnable)
206823a06317SJiaqing Zhao                 {
206923a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
2070002d39b4SEd Tanous                         [asyncResp](const boost::system::error_code ec) {
207108244d02SSunitha Harish                         if (ec)
207208244d02SSunitha Harish                         {
207308244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20743927e13eSJiaqing Zhao                             return;
207508244d02SSunitha Harish                         }
207623a06317SJiaqing Zhao                         },
207723a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
207823a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
207923a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
208023a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
208123a06317SJiaqing Zhao                         "NICEnabled",
208223a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
208308244d02SSunitha Harish                 }
208408244d02SSunitha Harish             }
208508244d02SSunitha Harish             else
20861abe55efSEd Tanous             {
2087bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2088bf648f77SEd Tanous                 // existing object, and other errors
2089bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2090d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2091bf648f77SEd Tanous                 return;
2092bf648f77SEd Tanous             }
2093bf648f77SEd Tanous             });
2094bf648f77SEd Tanous         });
2095bf648f77SEd Tanous 
2096bf648f77SEd Tanous     BMCWEB_ROUTE(
2097bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20983d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2099bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
210045ca1b86SEd Tanous             [&app](const crow::Request& req,
2101bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
210245ca1b86SEd Tanous                    const std::string& parentIfaceId,
210345ca1b86SEd Tanous                    const std::string& ifaceId) {
21043ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
210545ca1b86SEd Tanous         {
210645ca1b86SEd Tanous             return;
210745ca1b86SEd Tanous         }
2108bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2109bf648f77SEd Tanous         {
2110d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2111002d39b4SEd Tanous                                        ifaceId);
2112927a505aSKowalski, Kamil             return;
2113927a505aSKowalski, Kamil         }
2114e439f0f8SKowalski, Kamil 
2115bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2116bf648f77SEd Tanous         // for JSON preparation
2117f12894f8SJason M. Bills         getEthernetIfaceData(
2118bf648f77SEd Tanous             ifaceId,
2119002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2120002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2121cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2122cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
212317e22024SJiaqing Zhao             if (success && ethData.vlanId)
21241abe55efSEd Tanous             {
2125f12894f8SJason M. Bills                 auto callback =
2126002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
21271abe55efSEd Tanous                     if (ec)
21281abe55efSEd Tanous                     {
2129f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2130927a505aSKowalski, Kamil                     }
21314a0cb85cSEd Tanous                 };
21324a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2133002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2134002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
21354a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
21361abe55efSEd Tanous             }
21371abe55efSEd Tanous             else
21381abe55efSEd Tanous             {
2139927a505aSKowalski, Kamil                 // ... otherwise return error
2140bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2141bf648f77SEd Tanous                 // existing object, and other errors
2142bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2143d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2144927a505aSKowalski, Kamil             }
2145927a505aSKowalski, Kamil             });
2146bf648f77SEd Tanous         });
2147e439f0f8SKowalski, Kamil 
2148bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2149bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2150ed398213SEd Tanous 
2151ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21521476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21531476687dSEd Tanous             [&app](const crow::Request& req,
2154bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2155bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21563ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
215745ca1b86SEd Tanous         {
215845ca1b86SEd Tanous             return;
215945ca1b86SEd Tanous         }
21604a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21611abe55efSEd Tanous         // preparation
2162002d39b4SEd Tanous         getEthernetIfaceList(
2163002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
21641abe55efSEd Tanous                 const bool& success,
2165002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
21664a0cb85cSEd Tanous             if (!success)
21671abe55efSEd Tanous             {
2168f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21694a0cb85cSEd Tanous                 return;
21701abe55efSEd Tanous             }
21714c9afe43SEd Tanous 
217281ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
21734c9afe43SEd Tanous             {
2174002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2175002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21764c9afe43SEd Tanous                                            rootInterfaceName);
21774c9afe43SEd Tanous                 return;
21784c9afe43SEd Tanous             }
21794c9afe43SEd Tanous 
21800f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21810f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21820f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21830f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21840f74e643SEd Tanous                 "VLAN Network Interface Collection";
21854a0cb85cSEd Tanous 
21862c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21874a0cb85cSEd Tanous 
218881ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21891abe55efSEd Tanous             {
219011ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21914a0cb85cSEd Tanous                 {
2192f23b7296SEd Tanous                     std::string path =
2193f23b7296SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2194f23b7296SEd Tanous                     path += rootInterfaceName;
2195f23b7296SEd Tanous                     path += "/VLANs/";
2196f23b7296SEd Tanous                     path += ifaceItem;
21971476687dSEd Tanous                     nlohmann::json::object_t iface;
21981476687dSEd Tanous                     iface["@odata.id"] = std::move(path);
21991476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2200e439f0f8SKowalski, Kamil                 }
2201e439f0f8SKowalski, Kamil             }
2202e439f0f8SKowalski, Kamil 
2203002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
22042c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
22054a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22064a0cb85cSEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
22074a0cb85cSEd Tanous                 rootInterfaceName + "/VLANs";
2208e439f0f8SKowalski, Kamil         });
2209bf648f77SEd Tanous         });
2210e439f0f8SKowalski, Kamil 
2211bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2212bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
22133d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2214bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
221545ca1b86SEd Tanous             [&app](const crow::Request& req,
2216bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2217bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
22183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
221945ca1b86SEd Tanous         {
222045ca1b86SEd Tanous             return;
222145ca1b86SEd Tanous         }
2222fda13ad2SSunitha Harish         bool vlanEnable = false;
22230627a2c7SEd Tanous         uint32_t vlanId = 0;
2224002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2225002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
22261abe55efSEd Tanous         {
22274a0cb85cSEd Tanous             return;
2228e439f0f8SKowalski, Kamil         }
2229fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2230dbb59d4dSEd Tanous         if (vlanId == 0U)
2231fda13ad2SSunitha Harish         {
2232fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2233fda13ad2SSunitha Harish         }
2234fda13ad2SSunitha Harish         if (!vlanEnable)
2235fda13ad2SSunitha Harish         {
2236fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2237fda13ad2SSunitha Harish         }
2238271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2239fda13ad2SSunitha Harish         {
2240fda13ad2SSunitha Harish             return;
2241fda13ad2SSunitha Harish         }
2242fda13ad2SSunitha Harish 
2243002d39b4SEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
22441abe55efSEd Tanous             if (ec)
22451abe55efSEd Tanous             {
2246bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2247bf648f77SEd Tanous                 // based on phosphor-network responses
2248f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22494a0cb85cSEd Tanous                 return;
22501abe55efSEd Tanous             }
2251f12894f8SJason M. Bills             messages::created(asyncResp->res);
2252e439f0f8SKowalski, Kamil         };
22534a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22544a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22554a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22564a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22570627a2c7SEd Tanous             rootInterfaceName, vlanId);
2258bf648f77SEd Tanous         });
22594a0cb85cSEd Tanous }
2260bf648f77SEd Tanous 
22619391bb9cSRapkiewicz, Pawel } // namespace redfish
2262