xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision ef4c65b741724d724452a3a0efe8dff0d450514a)
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 
183ccb3adbSEd Tanous #include "app.hpp"
193ccb3adbSEd Tanous #include "dbus_singleton.hpp"
207a1dbc48SGeorge Liu #include "dbus_utility.hpp"
213ccb3adbSEd Tanous #include "error_messages.hpp"
223ccb3adbSEd Tanous #include "health.hpp"
233ccb3adbSEd Tanous #include "query.hpp"
243ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
25033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
263ccb3adbSEd Tanous #include "utils/json_utils.hpp"
27033f1e4dSEd Tanous 
2811ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
2911ba3979SEd Tanous #include <boost/algorithm/string/split.hpp>
304a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
31*ef4c65b7SEd Tanous #include <boost/url/format.hpp>
321214b7e7SGunnar Mills 
337a1dbc48SGeorge Liu #include <array>
34a24526dcSEd Tanous #include <optional>
35ab6554f1SJoshi-Mansi #include <regex>
367a1dbc48SGeorge Liu #include <string_view>
379391bb9cSRapkiewicz, Pawel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
409391bb9cSRapkiewicz, Pawel 
414a0cb85cSEd Tanous enum class LinkType
424a0cb85cSEd Tanous {
434a0cb85cSEd Tanous     Local,
444a0cb85cSEd Tanous     Global
454a0cb85cSEd Tanous };
469391bb9cSRapkiewicz, Pawel 
479391bb9cSRapkiewicz, Pawel /**
489391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
499391bb9cSRapkiewicz, Pawel  */
501abe55efSEd Tanous struct IPv4AddressData
511abe55efSEd Tanous {
52179db1d7SKowalski, Kamil     std::string id;
534a0cb85cSEd Tanous     std::string address;
544a0cb85cSEd Tanous     std::string domain;
554a0cb85cSEd Tanous     std::string gateway;
569391bb9cSRapkiewicz, Pawel     std::string netmask;
579391bb9cSRapkiewicz, Pawel     std::string origin;
584a0cb85cSEd Tanous     LinkType linktype;
5901c6e858SSunitha Harish     bool isActive;
604a0cb85cSEd Tanous 
611abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
621abe55efSEd Tanous     {
634a0cb85cSEd Tanous         return id < obj.id;
641abe55efSEd Tanous     }
659391bb9cSRapkiewicz, Pawel };
669391bb9cSRapkiewicz, Pawel 
679391bb9cSRapkiewicz, Pawel /**
68e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
69e48c0fc5SRavi Teja  */
70e48c0fc5SRavi Teja struct IPv6AddressData
71e48c0fc5SRavi Teja {
72e48c0fc5SRavi Teja     std::string id;
73e48c0fc5SRavi Teja     std::string address;
74e48c0fc5SRavi Teja     std::string origin;
75e48c0fc5SRavi Teja     uint8_t prefixLength;
76e48c0fc5SRavi Teja 
77e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
78e48c0fc5SRavi Teja     {
79e48c0fc5SRavi Teja         return id < obj.id;
80e48c0fc5SRavi Teja     }
81e48c0fc5SRavi Teja };
82e48c0fc5SRavi Teja /**
839391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
849391bb9cSRapkiewicz, Pawel  * available from DBus
859391bb9cSRapkiewicz, Pawel  */
861abe55efSEd Tanous struct EthernetInterfaceData
871abe55efSEd Tanous {
884a0cb85cSEd Tanous     uint32_t speed;
8935fb5311STejas Patil     size_t mtuSize;
9082695a5bSJiaqing Zhao     bool autoNeg;
9182695a5bSJiaqing Zhao     bool dnsEnabled;
9282695a5bSJiaqing Zhao     bool ntpEnabled;
9382695a5bSJiaqing Zhao     bool hostNameEnabled;
94aa05fb27SJohnathan Mantey     bool linkUp;
95eeedda23SJohnathan Mantey     bool nicEnabled;
9682695a5bSJiaqing Zhao     std::string dhcpEnabled;
971f8c7b5dSJohnathan Mantey     std::string operatingMode;
9882695a5bSJiaqing Zhao     std::string hostName;
9982695a5bSJiaqing Zhao     std::string defaultGateway;
10082695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
10182695a5bSJiaqing Zhao     std::string macAddress;
10217e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
1030f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1040f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
105d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1069391bb9cSRapkiewicz, Pawel };
1079391bb9cSRapkiewicz, Pawel 
1081f8c7b5dSJohnathan Mantey struct DHCPParameters
1091f8c7b5dSJohnathan Mantey {
1101f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
11182695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
11282695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
11382695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1141f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1151f8c7b5dSJohnathan Mantey };
1161f8c7b5dSJohnathan Mantey 
1179391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1189391bb9cSRapkiewicz, Pawel // into full dot notation
1191abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1201abe55efSEd Tanous {
1219391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1229391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1249391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1259391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1269391bb9cSRapkiewicz, Pawel     return netmask;
1279391bb9cSRapkiewicz, Pawel }
1289391bb9cSRapkiewicz, Pawel 
12982695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1301f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1311f8c7b5dSJohnathan Mantey {
1321f8c7b5dSJohnathan Mantey     if (isIPv4)
1331f8c7b5dSJohnathan Mantey     {
1341f8c7b5dSJohnathan Mantey         return (
1351f8c7b5dSJohnathan Mantey             (inputDHCP ==
1361f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1371f8c7b5dSJohnathan Mantey             (inputDHCP ==
1381f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1391f8c7b5dSJohnathan Mantey     }
1401f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1411f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1421f8c7b5dSJohnathan Mantey             (inputDHCP ==
1431f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1441f8c7b5dSJohnathan Mantey }
1451f8c7b5dSJohnathan Mantey 
1462c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1471f8c7b5dSJohnathan Mantey {
1481f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1491f8c7b5dSJohnathan Mantey     {
1501f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1511f8c7b5dSJohnathan Mantey     }
1523174e4dfSEd Tanous     if (isIPv4)
1531f8c7b5dSJohnathan Mantey     {
1541f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1551f8c7b5dSJohnathan Mantey     }
1563174e4dfSEd Tanous     if (isIPv6)
1571f8c7b5dSJohnathan Mantey     {
1581f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1591f8c7b5dSJohnathan Mantey     }
1601f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1611f8c7b5dSJohnathan Mantey }
1621f8c7b5dSJohnathan Mantey 
1634a0cb85cSEd Tanous inline std::string
1644a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1654a0cb85cSEd Tanous                                         bool isIPv4)
1661abe55efSEd Tanous {
1674a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1681abe55efSEd Tanous     {
1694a0cb85cSEd Tanous         return "Static";
1709391bb9cSRapkiewicz, Pawel     }
1714a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1721abe55efSEd Tanous     {
1734a0cb85cSEd Tanous         if (isIPv4)
1741abe55efSEd Tanous         {
1754a0cb85cSEd Tanous             return "IPv4LinkLocal";
1761abe55efSEd Tanous         }
1774a0cb85cSEd Tanous         return "LinkLocal";
1789391bb9cSRapkiewicz, Pawel     }
1794a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1801abe55efSEd Tanous     {
1814a0cb85cSEd Tanous         if (isIPv4)
1824a0cb85cSEd Tanous         {
1834a0cb85cSEd Tanous             return "DHCP";
1844a0cb85cSEd Tanous         }
1854a0cb85cSEd Tanous         return "DHCPv6";
1864a0cb85cSEd Tanous     }
1874a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1884a0cb85cSEd Tanous     {
1894a0cb85cSEd Tanous         return "SLAAC";
1904a0cb85cSEd Tanous     }
1914a0cb85cSEd Tanous     return "";
1924a0cb85cSEd Tanous }
1934a0cb85cSEd Tanous 
19402cad96eSEd Tanous inline bool extractEthernetInterfaceData(
19502cad96eSEd Tanous     const std::string& ethifaceId,
19602cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1974a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1984a0cb85cSEd Tanous {
1994c9afe43SEd Tanous     bool idFound = false;
20002cad96eSEd Tanous     for (const auto& objpath : dbusData)
2014a0cb85cSEd Tanous     {
20202cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
2034a0cb85cSEd Tanous         {
20481ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
205029573d4SEd Tanous             {
2064c9afe43SEd Tanous                 idFound = true;
2074a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2084a0cb85cSEd Tanous                 {
2094a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2104a0cb85cSEd Tanous                     {
2114a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2124a0cb85cSEd Tanous                         {
2134a0cb85cSEd Tanous                             const std::string* mac =
214abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2154a0cb85cSEd Tanous                             if (mac != nullptr)
2164a0cb85cSEd Tanous                             {
21782695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2184a0cb85cSEd Tanous                             }
2194a0cb85cSEd Tanous                         }
2204a0cb85cSEd Tanous                     }
2214a0cb85cSEd Tanous                 }
2224a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2234a0cb85cSEd Tanous                 {
2244a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2254a0cb85cSEd Tanous                     {
2264a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2274a0cb85cSEd Tanous                         {
2281b6b96c5SEd Tanous                             const uint32_t* id =
229abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2304a0cb85cSEd Tanous                             if (id != nullptr)
2314a0cb85cSEd Tanous                             {
23217e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2334a0cb85cSEd Tanous                             }
2344a0cb85cSEd Tanous                         }
2354a0cb85cSEd Tanous                     }
2364a0cb85cSEd Tanous                 }
2374a0cb85cSEd Tanous                 else if (ifacePair.first ==
2384a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2394a0cb85cSEd Tanous                 {
2404a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2414a0cb85cSEd Tanous                     {
2424a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2434a0cb85cSEd Tanous                         {
2442c70f800SEd Tanous                             const bool* autoNeg =
245abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2462c70f800SEd Tanous                             if (autoNeg != nullptr)
2474a0cb85cSEd Tanous                             {
24882695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2494a0cb85cSEd Tanous                             }
2504a0cb85cSEd Tanous                         }
2514a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2524a0cb85cSEd Tanous                         {
2534a0cb85cSEd Tanous                             const uint32_t* speed =
254abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2554a0cb85cSEd Tanous                             if (speed != nullptr)
2564a0cb85cSEd Tanous                             {
2574a0cb85cSEd Tanous                                 ethData.speed = *speed;
2584a0cb85cSEd Tanous                             }
2594a0cb85cSEd Tanous                         }
26035fb5311STejas Patil                         else if (propertyPair.first == "MTU")
26135fb5311STejas Patil                         {
26235fb5311STejas Patil                             const uint32_t* mtuSize =
26335fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
26435fb5311STejas Patil                             if (mtuSize != nullptr)
26535fb5311STejas Patil                             {
26635fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26735fb5311STejas Patil                             }
26835fb5311STejas Patil                         }
269aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
270aa05fb27SJohnathan Mantey                         {
271aa05fb27SJohnathan Mantey                             const bool* linkUp =
272aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
273aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
274aa05fb27SJohnathan Mantey                             {
275aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
276aa05fb27SJohnathan Mantey                             }
277aa05fb27SJohnathan Mantey                         }
278eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
279eeedda23SJohnathan Mantey                         {
280eeedda23SJohnathan Mantey                             const bool* nicEnabled =
281eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
282eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
283eeedda23SJohnathan Mantey                             {
284eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
285eeedda23SJohnathan Mantey                             }
286eeedda23SJohnathan Mantey                         }
287f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
288029573d4SEd Tanous                         {
289029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2908d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
291029573d4SEd Tanous                                     &propertyPair.second);
292029573d4SEd Tanous                             if (nameservers != nullptr)
293029573d4SEd Tanous                             {
294f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2950f6efdc1Smanojkiran.eda@gmail.com                             }
2960f6efdc1Smanojkiran.eda@gmail.com                         }
2970f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2980f6efdc1Smanojkiran.eda@gmail.com                         {
2990f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
3008d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3010f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3020f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3030f6efdc1Smanojkiran.eda@gmail.com                             {
304f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3054a0cb85cSEd Tanous                             }
3064a0cb85cSEd Tanous                         }
3072a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3082a133282Smanojkiraneda                         {
3092c70f800SEd Tanous                             const std::string* dhcpEnabled =
3101f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3112c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3122a133282Smanojkiraneda                             {
31382695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3142a133282Smanojkiraneda                             }
3152a133282Smanojkiraneda                         }
316d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
317d24bfc7aSJennifer Lee                         {
318d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3198d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
320d24bfc7aSJennifer Lee                                     &propertyPair.second);
321d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
322d24bfc7aSJennifer Lee                             {
323f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
324d24bfc7aSJennifer Lee                             }
325d24bfc7aSJennifer Lee                         }
3269010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3279010ec2eSRavi Teja                         {
3289010ec2eSRavi Teja                             const std::string* defaultGateway =
3299010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3309010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3319010ec2eSRavi Teja                             {
3329010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3339010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3349010ec2eSRavi Teja                                 {
33582695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3369010ec2eSRavi Teja                                 }
3379010ec2eSRavi Teja                                 else
3389010ec2eSRavi Teja                                 {
33982695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3409010ec2eSRavi Teja                                 }
3419010ec2eSRavi Teja                             }
3429010ec2eSRavi Teja                         }
3439010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3449010ec2eSRavi Teja                         {
3459010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3469010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3479010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3489010ec2eSRavi Teja                             {
3499010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3509010ec2eSRavi Teja                                     *defaultGateway6;
3519010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3529010ec2eSRavi Teja                                 {
35382695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3549010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3559010ec2eSRavi Teja                                 }
3569010ec2eSRavi Teja                                 else
3579010ec2eSRavi Teja                                 {
35882695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3599010ec2eSRavi Teja                                         defaultGateway6Str;
3609010ec2eSRavi Teja                                 }
3619010ec2eSRavi Teja                             }
3629010ec2eSRavi Teja                         }
363029573d4SEd Tanous                     }
364029573d4SEd Tanous                 }
365029573d4SEd Tanous             }
3661f8c7b5dSJohnathan Mantey 
3671e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3681f8c7b5dSJohnathan Mantey             {
3691f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3701f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3711f8c7b5dSJohnathan Mantey                 {
3721f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3731f8c7b5dSJohnathan Mantey                     {
3741f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3751f8c7b5dSJohnathan Mantey                         {
3762c70f800SEd Tanous                             const bool* dnsEnabled =
3771f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3782c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3791f8c7b5dSJohnathan Mantey                             {
38082695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3811f8c7b5dSJohnathan Mantey                             }
3821f8c7b5dSJohnathan Mantey                         }
3831f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3841f8c7b5dSJohnathan Mantey                         {
3852c70f800SEd Tanous                             const bool* ntpEnabled =
3861f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3872c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3881f8c7b5dSJohnathan Mantey                             {
38982695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3901f8c7b5dSJohnathan Mantey                             }
3911f8c7b5dSJohnathan Mantey                         }
3921f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3931f8c7b5dSJohnathan Mantey                         {
3942c70f800SEd Tanous                             const bool* hostNameEnabled =
3951f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3962c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3971f8c7b5dSJohnathan Mantey                             {
39882695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3991f8c7b5dSJohnathan Mantey                             }
4001f8c7b5dSJohnathan Mantey                         }
4011f8c7b5dSJohnathan Mantey                     }
4021f8c7b5dSJohnathan Mantey                 }
4031f8c7b5dSJohnathan Mantey             }
404029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
405029573d4SEd Tanous             // to check eth number
406029573d4SEd Tanous             if (ifacePair.first ==
4074a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4084a0cb85cSEd Tanous             {
4094a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4104a0cb85cSEd Tanous                 {
4114a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4124a0cb85cSEd Tanous                     {
4134a0cb85cSEd Tanous                         const std::string* hostname =
4148d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4154a0cb85cSEd Tanous                         if (hostname != nullptr)
4164a0cb85cSEd Tanous                         {
41782695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4184a0cb85cSEd Tanous                         }
4194a0cb85cSEd Tanous                     }
4204a0cb85cSEd Tanous                 }
4214a0cb85cSEd Tanous             }
4224a0cb85cSEd Tanous         }
4234a0cb85cSEd Tanous     }
4244c9afe43SEd Tanous     return idFound;
4254a0cb85cSEd Tanous }
4264a0cb85cSEd Tanous 
427e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42801784826SJohnathan Mantey inline void
42981ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
430711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43181ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
432e48c0fc5SRavi Teja {
43389492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
43489492a15SPatrick Williams                                     ethifaceId;
435e48c0fc5SRavi Teja 
436e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
437e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43881ce609eSEd Tanous     for (const auto& objpath : dbusData)
439e48c0fc5SRavi Teja     {
440e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
441353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
442e48c0fc5SRavi Teja         {
4439eb808c1SEd Tanous             for (const auto& interface : objpath.second)
444e48c0fc5SRavi Teja             {
445e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
446e48c0fc5SRavi Teja                 {
447353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
448353163e9STony Lee                                              interface.second.end(),
449353163e9STony Lee                                              [](const auto& property) {
450353163e9STony Lee                         return property.first == "Type";
451353163e9STony Lee                     });
452353163e9STony Lee                     if (type == interface.second.end())
453353163e9STony Lee                     {
454353163e9STony Lee                         continue;
455353163e9STony Lee                     }
456353163e9STony Lee 
457353163e9STony Lee                     const std::string* typeStr =
458353163e9STony Lee                         std::get_if<std::string>(&type->second);
459353163e9STony Lee 
460353163e9STony Lee                     if (typeStr == nullptr ||
461353163e9STony Lee                         (*typeStr !=
462353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
463353163e9STony Lee                     {
464353163e9STony Lee                         continue;
465353163e9STony Lee                     }
466353163e9STony Lee 
467e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
468e48c0fc5SRavi Teja                     // appropriate
469e48c0fc5SRavi Teja                     std::pair<
470e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
471e48c0fc5SRavi Teja                         bool>
47281ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4732c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4742c70f800SEd Tanous                     ipv6Address.id =
475353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
4769eb808c1SEd Tanous                     for (const auto& property : interface.second)
477e48c0fc5SRavi Teja                     {
478e48c0fc5SRavi Teja                         if (property.first == "Address")
479e48c0fc5SRavi Teja                         {
480e48c0fc5SRavi Teja                             const std::string* address =
481e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
482e48c0fc5SRavi Teja                             if (address != nullptr)
483e48c0fc5SRavi Teja                             {
4842c70f800SEd Tanous                                 ipv6Address.address = *address;
485e48c0fc5SRavi Teja                             }
486e48c0fc5SRavi Teja                         }
487e48c0fc5SRavi Teja                         else if (property.first == "Origin")
488e48c0fc5SRavi Teja                         {
489e48c0fc5SRavi Teja                             const std::string* origin =
490e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
491e48c0fc5SRavi Teja                             if (origin != nullptr)
492e48c0fc5SRavi Teja                             {
4932c70f800SEd Tanous                                 ipv6Address.origin =
494e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
495e48c0fc5SRavi Teja                                                                         false);
496e48c0fc5SRavi Teja                             }
497e48c0fc5SRavi Teja                         }
498e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
499e48c0fc5SRavi Teja                         {
500e48c0fc5SRavi Teja                             const uint8_t* prefix =
501e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
502e48c0fc5SRavi Teja                             if (prefix != nullptr)
503e48c0fc5SRavi Teja                             {
5042c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
505e48c0fc5SRavi Teja                             }
506e48c0fc5SRavi Teja                         }
507889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
508889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
509889ff694SAsmitha Karunanithi                         {
510889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
511889ff694SAsmitha Karunanithi                         }
512e48c0fc5SRavi Teja                         else
513e48c0fc5SRavi Teja                         {
514e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
515e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
516e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
517e48c0fc5SRavi Teja                         }
518e48c0fc5SRavi Teja                     }
519e48c0fc5SRavi Teja                 }
520e48c0fc5SRavi Teja             }
521e48c0fc5SRavi Teja         }
522e48c0fc5SRavi Teja     }
523e48c0fc5SRavi Teja }
524e48c0fc5SRavi Teja 
5254a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
52601784826SJohnathan Mantey inline void
52781ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
528711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
52981ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5304a0cb85cSEd Tanous {
53189492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
53289492a15SPatrick Williams                                     ethifaceId;
5334a0cb85cSEd Tanous 
5344a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5354a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
53681ce609eSEd Tanous     for (const auto& objpath : dbusData)
5374a0cb85cSEd Tanous     {
5384a0cb85cSEd Tanous         // Check if proper pattern for object path appears
539353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
5404a0cb85cSEd Tanous         {
5419eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5424a0cb85cSEd Tanous             {
5434a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5444a0cb85cSEd Tanous                 {
545353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
546353163e9STony Lee                                              interface.second.end(),
547353163e9STony Lee                                              [](const auto& property) {
548353163e9STony Lee                         return property.first == "Type";
549353163e9STony Lee                     });
550353163e9STony Lee                     if (type == interface.second.end())
551353163e9STony Lee                     {
552353163e9STony Lee                         continue;
553353163e9STony Lee                     }
554353163e9STony Lee 
555353163e9STony Lee                     const std::string* typeStr =
556353163e9STony Lee                         std::get_if<std::string>(&type->second);
557353163e9STony Lee 
558353163e9STony Lee                     if (typeStr == nullptr ||
559353163e9STony Lee                         (*typeStr !=
560353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
561353163e9STony Lee                     {
562353163e9STony Lee                         continue;
563353163e9STony Lee                     }
564353163e9STony Lee 
5654a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5664a0cb85cSEd Tanous                     // appropriate
5674a0cb85cSEd Tanous                     std::pair<
5684a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5694a0cb85cSEd Tanous                         bool>
57081ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5712c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5722c70f800SEd Tanous                     ipv4Address.id =
573353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
5749eb808c1SEd Tanous                     for (const auto& property : interface.second)
5754a0cb85cSEd Tanous                     {
5764a0cb85cSEd Tanous                         if (property.first == "Address")
5774a0cb85cSEd Tanous                         {
5784a0cb85cSEd Tanous                             const std::string* address =
579abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5804a0cb85cSEd Tanous                             if (address != nullptr)
5814a0cb85cSEd Tanous                             {
5822c70f800SEd Tanous                                 ipv4Address.address = *address;
5834a0cb85cSEd Tanous                             }
5844a0cb85cSEd Tanous                         }
5854a0cb85cSEd Tanous                         else if (property.first == "Origin")
5864a0cb85cSEd Tanous                         {
5874a0cb85cSEd Tanous                             const std::string* origin =
588abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5894a0cb85cSEd Tanous                             if (origin != nullptr)
5904a0cb85cSEd Tanous                             {
5912c70f800SEd Tanous                                 ipv4Address.origin =
5924a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5934a0cb85cSEd Tanous                                                                         true);
5944a0cb85cSEd Tanous                             }
5954a0cb85cSEd Tanous                         }
5964a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5974a0cb85cSEd Tanous                         {
5984a0cb85cSEd Tanous                             const uint8_t* mask =
599abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
6004a0cb85cSEd Tanous                             if (mask != nullptr)
6014a0cb85cSEd Tanous                             {
6024a0cb85cSEd Tanous                                 // convert it to the string
6032c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
6044a0cb85cSEd Tanous                             }
6054a0cb85cSEd Tanous                         }
606889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
607889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
608889ff694SAsmitha Karunanithi                         {
609889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
610889ff694SAsmitha Karunanithi                         }
6114a0cb85cSEd Tanous                         else
6124a0cb85cSEd Tanous                         {
6134a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
6144a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
6154a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
6164a0cb85cSEd Tanous                         }
6174a0cb85cSEd Tanous                     }
6184a0cb85cSEd Tanous                     // Check if given address is local, or global
6192c70f800SEd Tanous                     ipv4Address.linktype =
62011ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
62118659d10SJohnathan Mantey                             ? LinkType::Local
62218659d10SJohnathan Mantey                             : LinkType::Global;
6234a0cb85cSEd Tanous                 }
6244a0cb85cSEd Tanous             }
6254a0cb85cSEd Tanous         }
6264a0cb85cSEd Tanous     }
6274a0cb85cSEd Tanous }
628588c3f0dSKowalski, Kamil 
629588c3f0dSKowalski, Kamil /**
63001784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
631179db1d7SKowalski, Kamil  *
632179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
633179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
634179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
635179db1d7SKowalski, Kamil  *
636179db1d7SKowalski, Kamil  * @return None
637179db1d7SKowalski, Kamil  */
6389c5e585cSRavi Teja inline void deleteIPAddress(const std::string& ifaceId,
6399c5e585cSRavi Teja                             const std::string& ipHash,
6408d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6411abe55efSEd Tanous {
64255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6435e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6441abe55efSEd Tanous         if (ec)
6451abe55efSEd Tanous         {
646a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6471abe55efSEd Tanous         }
648179db1d7SKowalski, Kamil         },
649179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
6509c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + ipHash,
651179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
652179db1d7SKowalski, Kamil }
653179db1d7SKowalski, Kamil 
654244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
655244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
656244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6579010ec2eSRavi Teja {
6589010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6595e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6609010ec2eSRavi Teja         if (ec)
6619010ec2eSRavi Teja         {
6629010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6639010ec2eSRavi Teja             return;
6649010ec2eSRavi Teja         }
6659010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6669010ec2eSRavi Teja         },
6679010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6689010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6699010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6709010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
671168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6729010ec2eSRavi Teja }
673179db1d7SKowalski, Kamil /**
67401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
675179db1d7SKowalski, Kamil  *
67601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
67701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
67801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
67901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
680179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
681179db1d7SKowalski, Kamil  *
682179db1d7SKowalski, Kamil  * @return None
683179db1d7SKowalski, Kamil  */
684cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
685cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6868d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6871abe55efSEd Tanous {
688002d39b4SEd Tanous     auto createIpHandler =
6895e7e2dc5SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
6901abe55efSEd Tanous         if (ec)
6911abe55efSEd Tanous         {
692a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6939010ec2eSRavi Teja             return;
694179db1d7SKowalski, Kamil         }
6959010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6969010ec2eSRavi Teja     };
6979010ec2eSRavi Teja 
6989010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6999010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
700179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
701179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
70201784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
703179db1d7SKowalski, Kamil         gateway);
704179db1d7SKowalski, Kamil }
705e48c0fc5SRavi Teja 
706e48c0fc5SRavi Teja /**
70701784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
70801784826SJohnathan Mantey  * static IPv6 entry
70901784826SJohnathan Mantey  *
71001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
71101784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
71201784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
71301784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
71401784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
71501784826SJohnathan Mantey  *
71601784826SJohnathan Mantey  * @return None
71701784826SJohnathan Mantey  */
7189c5e585cSRavi Teja 
7199c5e585cSRavi Teja enum class IpVersion
7209c5e585cSRavi Teja {
7219c5e585cSRavi Teja     IpV4,
7229c5e585cSRavi Teja     IpV6
7239c5e585cSRavi Teja };
7249c5e585cSRavi Teja 
7259c5e585cSRavi Teja inline void deleteAndCreateIPAddress(
7269c5e585cSRavi Teja     IpVersion version, const std::string& ifaceId, const std::string& id,
7278d1b46d7Szhanghch05     uint8_t prefixLength, const std::string& address,
7289c5e585cSRavi Teja     const std::string& gateway,
7298d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
73001784826SJohnathan Mantey {
73101784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
7329c5e585cSRavi Teja         [asyncResp, version, ifaceId, address, prefixLength,
7339c5e585cSRavi Teja          gateway](const boost::system::error_code& ec) {
73401784826SJohnathan Mantey         if (ec)
73501784826SJohnathan Mantey         {
73601784826SJohnathan Mantey             messages::internalError(asyncResp->res);
73701784826SJohnathan Mantey         }
7389c5e585cSRavi Teja         std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
7399c5e585cSRavi Teja         protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
74001784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
7415e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
74223a21a1cSEd Tanous             if (ec2)
74301784826SJohnathan Mantey             {
74401784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
74501784826SJohnathan Mantey             }
74601784826SJohnathan Mantey             },
74701784826SJohnathan Mantey             "xyz.openbmc_project.Network",
74801784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
7499c5e585cSRavi Teja             "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
7509c5e585cSRavi Teja             prefixLength, gateway);
75101784826SJohnathan Mantey         },
75201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
7539c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + id,
75401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
75501784826SJohnathan Mantey }
75601784826SJohnathan Mantey 
75701784826SJohnathan Mantey /**
758e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
759e48c0fc5SRavi Teja  *
760e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
761e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
762e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
763e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
764e48c0fc5SRavi Teja  *
765e48c0fc5SRavi Teja  * @return None
766e48c0fc5SRavi Teja  */
76701784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
76801784826SJohnathan Mantey                        const std::string& address,
7698d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
770e48c0fc5SRavi Teja {
7715e7e2dc5SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
772e48c0fc5SRavi Teja         if (ec)
773e48c0fc5SRavi Teja         {
774e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
775e48c0fc5SRavi Teja         }
776e48c0fc5SRavi Teja     };
777e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
7784e0453b1SGunnar Mills     // does not have associated gateway property
779e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
780e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
781e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
782e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
783e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
784e48c0fc5SRavi Teja         "");
785e48c0fc5SRavi Teja }
786e48c0fc5SRavi Teja 
787179db1d7SKowalski, Kamil /**
788179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
789179db1d7SKowalski, Kamil  * Object
790179db1d7SKowalski, Kamil  * from EntityManager Network Manager
7914a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
792179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
793179db1d7SKowalski, Kamil  * into JSON
794179db1d7SKowalski, Kamil  */
795179db1d7SKowalski, Kamil template <typename CallbackFunc>
79681ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
7971abe55efSEd Tanous                           CallbackFunc&& callback)
7981abe55efSEd Tanous {
799f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
800f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
801f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
802f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
803f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
8045e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
80502cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
80655c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
8074a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
808e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
809179db1d7SKowalski, Kamil 
81081ce609eSEd Tanous         if (errorCode)
8111abe55efSEd Tanous         {
81201784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
813179db1d7SKowalski, Kamil             return;
814179db1d7SKowalski, Kamil         }
815179db1d7SKowalski, Kamil 
816002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
8174c9afe43SEd Tanous         if (!found)
8184c9afe43SEd Tanous         {
81901784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8204c9afe43SEd Tanous             return;
8214c9afe43SEd Tanous         }
8224c9afe43SEd Tanous 
8232c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
824179db1d7SKowalski, Kamil         // Fix global GW
8251abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8261abe55efSEd Tanous         {
827c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
828c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8299010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8301abe55efSEd Tanous             {
83182695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
832179db1d7SKowalski, Kamil             }
833179db1d7SKowalski, Kamil         }
834179db1d7SKowalski, Kamil 
8352c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8364e0453b1SGunnar Mills         // Finally make a callback with useful data
83701784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
838f5892d0dSGeorge Liu         });
839271584abSEd Tanous }
840179db1d7SKowalski, Kamil 
841179db1d7SKowalski, Kamil /**
8429391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
8439391bb9cSRapkiewicz, Pawel  * Manager
8441abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
8451abe55efSEd Tanous  * into JSON.
8469391bb9cSRapkiewicz, Pawel  */
8479391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
8481abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
8491abe55efSEd Tanous {
850f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
851f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
852f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
853f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
8545e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
855f5892d0dSGeorge Liu             const dbus::utility::ManagedObjectType& resp) {
8561abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
8571abe55efSEd Tanous         // ethernet interfaces
8582c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
8592c70f800SEd Tanous         ifaceList.reserve(resp.size());
86081ce609eSEd Tanous         if (errorCode)
8611abe55efSEd Tanous         {
8622c70f800SEd Tanous             callback(false, ifaceList);
8639391bb9cSRapkiewicz, Pawel             return;
8649391bb9cSRapkiewicz, Pawel         }
8659391bb9cSRapkiewicz, Pawel 
8669391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
8674a0cb85cSEd Tanous         for (const auto& objpath : resp)
8681abe55efSEd Tanous         {
8699391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
8704a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
8711abe55efSEd Tanous             {
8721abe55efSEd Tanous                 // If interface is
8734a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
8744a0cb85cSEd Tanous                 // what we're looking for.
8759391bb9cSRapkiewicz, Pawel                 if (interface.first ==
8761abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
8771abe55efSEd Tanous                 {
8782dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
8792dfd18efSEd Tanous                     if (ifaceId.empty())
8801abe55efSEd Tanous                     {
8812dfd18efSEd Tanous                         continue;
8829391bb9cSRapkiewicz, Pawel                     }
8832dfd18efSEd Tanous                     // and put it into output vector.
8842dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
8859391bb9cSRapkiewicz, Pawel                 }
8869391bb9cSRapkiewicz, Pawel             }
8879391bb9cSRapkiewicz, Pawel         }
888a434f2bdSEd Tanous         // Finally make a callback with useful data
8892c70f800SEd Tanous         callback(true, ifaceList);
890f5892d0dSGeorge Liu         });
891271584abSEd Tanous }
8929391bb9cSRapkiewicz, Pawel 
8934f48d5f6SEd Tanous inline void
8944f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
8958d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
8961abe55efSEd Tanous {
897ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
898ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
899ab6554f1SJoshi-Mansi     {
900ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
901ab6554f1SJoshi-Mansi                                            "HostName");
902ab6554f1SJoshi-Mansi         return;
903ab6554f1SJoshi-Mansi     }
904bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
9055e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
9064a0cb85cSEd Tanous         if (ec)
9074a0cb85cSEd Tanous         {
908a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
9091abe55efSEd Tanous         }
910bc0bd6e0SEd Tanous         },
911bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
912bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
913bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
914168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
915588c3f0dSKowalski, Kamil }
916588c3f0dSKowalski, Kamil 
9174f48d5f6SEd Tanous inline void
91835fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
91935fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
92035fb5311STejas Patil {
92189492a15SPatrick Williams     sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
92289492a15SPatrick Williams                                               ifaceId;
92335fb5311STejas Patil     crow::connections::systemBus->async_method_call(
9245e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
92535fb5311STejas Patil         if (ec)
92635fb5311STejas Patil         {
92735fb5311STejas Patil             messages::internalError(asyncResp->res);
92835fb5311STejas Patil         }
92935fb5311STejas Patil         },
93035fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
93135fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
93235fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
93335fb5311STejas Patil         std::variant<size_t>(mtuSize));
93435fb5311STejas Patil }
93535fb5311STejas Patil 
93635fb5311STejas Patil inline void
9374f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
938bf648f77SEd Tanous                           const std::string& domainname,
9398d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
940ab6554f1SJoshi-Mansi {
941ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
942ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
9435e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
944ab6554f1SJoshi-Mansi         if (ec)
945ab6554f1SJoshi-Mansi         {
946ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
947ab6554f1SJoshi-Mansi         }
948ab6554f1SJoshi-Mansi         },
949ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
950ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
951ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
952ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
953168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
954ab6554f1SJoshi-Mansi }
955ab6554f1SJoshi-Mansi 
9564f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
957bf648f77SEd Tanous {
958bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
959bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
960bf648f77SEd Tanous     {
961bf648f77SEd Tanous         return false;
962bf648f77SEd Tanous     }
963bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
964bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
965bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
966bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
967bf648f77SEd Tanous     const std::regex pattern(
968bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
969bf648f77SEd Tanous 
970bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
971bf648f77SEd Tanous }
972bf648f77SEd Tanous 
9734f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
974bf648f77SEd Tanous {
975bf648f77SEd Tanous     // Can have multiple subdomains
976bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
9770fda0f12SGeorge Liu     const std::regex pattern(
9780fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
979bf648f77SEd Tanous 
980bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
981bf648f77SEd Tanous }
982bf648f77SEd Tanous 
9834f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
9848d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
985ab6554f1SJoshi-Mansi {
986ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
987ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
988ab6554f1SJoshi-Mansi     {
989ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
990ab6554f1SJoshi-Mansi         return;
991ab6554f1SJoshi-Mansi     }
992ab6554f1SJoshi-Mansi 
993ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
994ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
995ab6554f1SJoshi-Mansi     {
996ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
997ab6554f1SJoshi-Mansi         return;
998ab6554f1SJoshi-Mansi     }
999ab6554f1SJoshi-Mansi 
1000ab6554f1SJoshi-Mansi     std::string hostname;
1001ab6554f1SJoshi-Mansi     std::string domainname;
1002ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1003ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1004ab6554f1SJoshi-Mansi 
1005ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1006ab6554f1SJoshi-Mansi     {
1007ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1008ab6554f1SJoshi-Mansi         return;
1009ab6554f1SJoshi-Mansi     }
1010ab6554f1SJoshi-Mansi 
1011ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1012ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1013ab6554f1SJoshi-Mansi }
1014ab6554f1SJoshi-Mansi 
10154f48d5f6SEd Tanous inline void
10164f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1017bf648f77SEd Tanous                           const std::string& macAddress,
10188d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1019d577665bSRatan Gupta {
102058283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
102158283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
102258283f41SJohnathan Mantey 
1023d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
10245e7e2dc5SEd Tanous         [asyncResp, macAddress](const boost::system::error_code& ec,
10255b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1026d577665bSRatan Gupta         if (ec)
1027d577665bSRatan Gupta         {
102858283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
102958283f41SJohnathan Mantey             if (err == nullptr)
103058283f41SJohnathan Mantey             {
103158283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
103258283f41SJohnathan Mantey                 return;
103358283f41SJohnathan Mantey             }
103458283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
103558283f41SJohnathan Mantey             {
103658283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
103758283f41SJohnathan Mantey                 return;
103858283f41SJohnathan Mantey             }
1039d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1040d577665bSRatan Gupta             return;
1041d577665bSRatan Gupta         }
1042d577665bSRatan Gupta         },
1043d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1044d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1045d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1046d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1047168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1048d577665bSRatan Gupta }
1049286b9118SJohnathan Mantey 
10504f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
10514f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
10524f48d5f6SEd Tanous                            const bool v6Value,
10538d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1054da131a9aSJennifer Lee {
10552c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1056da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10575e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1058da131a9aSJennifer Lee         if (ec)
1059da131a9aSJennifer Lee         {
1060da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1061da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1062da131a9aSJennifer Lee             return;
1063da131a9aSJennifer Lee         }
10648f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1065da131a9aSJennifer Lee         },
1066da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1067da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1068da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1069da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1070168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1071da131a9aSJennifer Lee }
10721f8c7b5dSJohnathan Mantey 
10734f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1074eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
10758d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1076eeedda23SJohnathan Mantey {
1077eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
10785e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1079eeedda23SJohnathan Mantey         if (ec)
1080eeedda23SJohnathan Mantey         {
1081eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1082eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1083eeedda23SJohnathan Mantey             return;
1084eeedda23SJohnathan Mantey         }
1085eeedda23SJohnathan Mantey         },
1086eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1087eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1088eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1089eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1090168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1091eeedda23SJohnathan Mantey }
1092eeedda23SJohnathan Mantey 
10934f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
10948d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1095da131a9aSJennifer Lee {
1096da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1097da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10985e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1099da131a9aSJennifer Lee         if (ec)
1100da131a9aSJennifer Lee         {
1101da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1102da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1103da131a9aSJennifer Lee             return;
1104da131a9aSJennifer Lee         }
1105da131a9aSJennifer Lee         },
11061e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1107da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1108da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1109168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1110da131a9aSJennifer Lee }
1111d577665bSRatan Gupta 
11124f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
11131f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1114f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1115f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
11168d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1117da131a9aSJennifer Lee {
111882695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
111982695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1120da131a9aSJennifer Lee 
11211f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11221f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11231f8c7b5dSJohnathan Mantey 
11241f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11251f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1126da131a9aSJennifer Lee     {
11271f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11281f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11291f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11301f8c7b5dSJohnathan Mantey         {
1131bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1132bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11331f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1134da131a9aSJennifer Lee             return;
1135da131a9aSJennifer Lee         }
11361f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11371f8c7b5dSJohnathan Mantey     }
11381f8c7b5dSJohnathan Mantey     else
1139da131a9aSJennifer Lee     {
11401f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
11411f8c7b5dSJohnathan Mantey     }
11421f8c7b5dSJohnathan Mantey 
11431f8c7b5dSJohnathan Mantey     bool nextDNS{};
114482695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
11451f8c7b5dSJohnathan Mantey     {
114682695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
11471f8c7b5dSJohnathan Mantey         {
11481f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11491f8c7b5dSJohnathan Mantey             return;
11501f8c7b5dSJohnathan Mantey         }
115182695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11521f8c7b5dSJohnathan Mantey     }
115382695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
11541f8c7b5dSJohnathan Mantey     {
115582695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11561f8c7b5dSJohnathan Mantey     }
115782695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
11581f8c7b5dSJohnathan Mantey     {
115982695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
11601f8c7b5dSJohnathan Mantey     }
11611f8c7b5dSJohnathan Mantey     else
11621f8c7b5dSJohnathan Mantey     {
116382695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
11641f8c7b5dSJohnathan Mantey     }
11651f8c7b5dSJohnathan Mantey 
11661f8c7b5dSJohnathan Mantey     bool nextNTP{};
116782695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
11681f8c7b5dSJohnathan Mantey     {
116982695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
11701f8c7b5dSJohnathan Mantey         {
11711f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11721f8c7b5dSJohnathan Mantey             return;
11731f8c7b5dSJohnathan Mantey         }
117482695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11751f8c7b5dSJohnathan Mantey     }
117682695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
11771f8c7b5dSJohnathan Mantey     {
117882695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11791f8c7b5dSJohnathan Mantey     }
118082695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
11811f8c7b5dSJohnathan Mantey     {
118282695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
11831f8c7b5dSJohnathan Mantey     }
11841f8c7b5dSJohnathan Mantey     else
11851f8c7b5dSJohnathan Mantey     {
118682695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
11871f8c7b5dSJohnathan Mantey     }
11881f8c7b5dSJohnathan Mantey 
11891f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
119082695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
11911f8c7b5dSJohnathan Mantey     {
119282695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
11931f8c7b5dSJohnathan Mantey         {
11941f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11951f8c7b5dSJohnathan Mantey             return;
11961f8c7b5dSJohnathan Mantey         }
119782695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11981f8c7b5dSJohnathan Mantey     }
119982695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12001f8c7b5dSJohnathan Mantey     {
120182695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12021f8c7b5dSJohnathan Mantey     }
120382695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12041f8c7b5dSJohnathan Mantey     {
120582695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12061f8c7b5dSJohnathan Mantey     }
12071f8c7b5dSJohnathan Mantey     else
12081f8c7b5dSJohnathan Mantey     {
120982695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
12101f8c7b5dSJohnathan Mantey     }
12111f8c7b5dSJohnathan Mantey 
1212da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
12131f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
12141f8c7b5dSJohnathan Mantey                    asyncResp);
1215da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
12161f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1217da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
12181f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
12191f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12201f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1221da131a9aSJennifer Lee }
122201784826SJohnathan Mantey 
12234f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
12242c70f800SEd Tanous     getNextStaticIpEntry(
1225bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1226bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
122701784826SJohnathan Mantey {
122817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
122917a897dfSManojkiran Eda         return value.origin == "Static";
123017a897dfSManojkiran Eda     });
123101784826SJohnathan Mantey }
123201784826SJohnathan Mantey 
12334f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
12342c70f800SEd Tanous     getNextStaticIpEntry(
1235bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1236bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
123701784826SJohnathan Mantey {
123817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
123917a897dfSManojkiran Eda         return value.origin == "Static";
124017a897dfSManojkiran Eda     });
124101784826SJohnathan Mantey }
124201784826SJohnathan Mantey 
12434f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1244f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
124501784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
12468d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12471abe55efSEd Tanous {
124801784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1249f476acbfSRatan Gupta     {
125071f52d96SEd Tanous         messages::propertyValueTypeError(
125171f52d96SEd Tanous             asyncResp->res,
1252bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1253d1d50814SRavi Teja             "IPv4StaticAddresses");
1254f476acbfSRatan Gupta         return;
1255f476acbfSRatan Gupta     }
1256f476acbfSRatan Gupta 
1257271584abSEd Tanous     unsigned entryIdx = 1;
125801784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
125901784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
126001784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
126101784826SJohnathan Mantey     // into the NIC.
126285ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
12632c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
126401784826SJohnathan Mantey 
1265537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12661abe55efSEd Tanous     {
126789492a15SPatrick Williams         std::string pathString = "IPv4StaticAddresses/" +
126889492a15SPatrick Williams                                  std::to_string(entryIdx);
1269179db1d7SKowalski, Kamil 
127001784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1271f476acbfSRatan Gupta         {
1272537174c4SEd Tanous             std::optional<std::string> address;
1273537174c4SEd Tanous             std::optional<std::string> subnetMask;
1274537174c4SEd Tanous             std::optional<std::string> gateway;
1275537174c4SEd Tanous 
1276537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12777e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12787e27d832SJohnathan Mantey                                      "Gateway", gateway))
1279537174c4SEd Tanous             {
128001784826SJohnathan Mantey                 messages::propertyValueFormatError(
128171f52d96SEd Tanous                     asyncResp->res,
128271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
128371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
128471f52d96SEd Tanous                     pathString);
1285537174c4SEd Tanous                 return;
1286179db1d7SKowalski, Kamil             }
1287179db1d7SKowalski, Kamil 
128801784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
128901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
129001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
129101784826SJohnathan Mantey             // current request.
1292271584abSEd Tanous             const std::string* addr = nullptr;
1293271584abSEd Tanous             const std::string* gw = nullptr;
129401784826SJohnathan Mantey             uint8_t prefixLength = 0;
129501784826SJohnathan Mantey             bool errorInEntry = false;
1296537174c4SEd Tanous             if (address)
12971abe55efSEd Tanous             {
1298033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
12991abe55efSEd Tanous                 {
130001784826SJohnathan Mantey                     addr = &(*address);
13014a0cb85cSEd Tanous                 }
130201784826SJohnathan Mantey                 else
130301784826SJohnathan Mantey                 {
1304bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1305bf648f77SEd Tanous                                                        pathString + "/Address");
130601784826SJohnathan Mantey                     errorInEntry = true;
130701784826SJohnathan Mantey                 }
130801784826SJohnathan Mantey             }
130985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
131001784826SJohnathan Mantey             {
131185ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
131201784826SJohnathan Mantey             }
131301784826SJohnathan Mantey             else
131401784826SJohnathan Mantey             {
131501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
131601784826SJohnathan Mantey                                           pathString + "/Address");
131701784826SJohnathan Mantey                 errorInEntry = true;
13184a0cb85cSEd Tanous             }
13194a0cb85cSEd Tanous 
1320537174c4SEd Tanous             if (subnetMask)
13214a0cb85cSEd Tanous             {
1322033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1323033f1e4dSEd Tanous                                                          &prefixLength))
13244a0cb85cSEd Tanous                 {
1325f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1326537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13274a0cb85cSEd Tanous                         pathString + "/SubnetMask");
132801784826SJohnathan Mantey                     errorInEntry = true;
13294a0cb85cSEd Tanous                 }
13304a0cb85cSEd Tanous             }
133185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13324a0cb85cSEd Tanous             {
1333033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
133401784826SJohnathan Mantey                                                          &prefixLength))
13354a0cb85cSEd Tanous                 {
133601784826SJohnathan Mantey                     messages::propertyValueFormatError(
133785ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
133801784826SJohnathan Mantey                         pathString + "/SubnetMask");
133901784826SJohnathan Mantey                     errorInEntry = true;
13404a0cb85cSEd Tanous                 }
13414a0cb85cSEd Tanous             }
13421abe55efSEd Tanous             else
13431abe55efSEd Tanous             {
134401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
134501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
134601784826SJohnathan Mantey                 errorInEntry = true;
134701784826SJohnathan Mantey             }
134801784826SJohnathan Mantey 
134901784826SJohnathan Mantey             if (gateway)
135001784826SJohnathan Mantey             {
1351033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
135201784826SJohnathan Mantey                 {
135301784826SJohnathan Mantey                     gw = &(*gateway);
135401784826SJohnathan Mantey                 }
135501784826SJohnathan Mantey                 else
135601784826SJohnathan Mantey                 {
1357bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1358bf648f77SEd Tanous                                                        pathString + "/Gateway");
135901784826SJohnathan Mantey                     errorInEntry = true;
136001784826SJohnathan Mantey                 }
136101784826SJohnathan Mantey             }
136285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
136301784826SJohnathan Mantey             {
136485ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
136501784826SJohnathan Mantey             }
136601784826SJohnathan Mantey             else
13671abe55efSEd Tanous             {
1368a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13694a0cb85cSEd Tanous                                           pathString + "/Gateway");
137001784826SJohnathan Mantey                 errorInEntry = true;
13714a0cb85cSEd Tanous             }
13724a0cb85cSEd Tanous 
137301784826SJohnathan Mantey             if (errorInEntry)
13741abe55efSEd Tanous             {
137501784826SJohnathan Mantey                 return;
13764a0cb85cSEd Tanous             }
13774a0cb85cSEd Tanous 
137885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13791abe55efSEd Tanous             {
13809c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
13819c5e585cSRavi Teja                                          nicIpEntry->id, prefixLength, *gw,
1382bf648f77SEd Tanous                                          *addr, asyncResp);
138389492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
138489492a15SPatrick Williams                                                   ipv4Data.cend());
1385588c3f0dSKowalski, Kamil             }
138601784826SJohnathan Mantey             else
138701784826SJohnathan Mantey             {
1388cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1389cb13a392SEd Tanous                            asyncResp);
13904a0cb85cSEd Tanous             }
13914a0cb85cSEd Tanous             entryIdx++;
13924a0cb85cSEd Tanous         }
139301784826SJohnathan Mantey         else
139401784826SJohnathan Mantey         {
139585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
139601784826SJohnathan Mantey             {
139701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
139801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
139901784826SJohnathan Mantey                 // in error, so bail out.
140001784826SJohnathan Mantey                 if (thisJson.is_null())
140101784826SJohnathan Mantey                 {
140201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
140301784826SJohnathan Mantey                     return;
140401784826SJohnathan Mantey                 }
140501784826SJohnathan Mantey                 messages::propertyValueFormatError(
140671f52d96SEd Tanous                     asyncResp->res,
140771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
140871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
140971f52d96SEd Tanous                     pathString);
141001784826SJohnathan Mantey                 return;
141101784826SJohnathan Mantey             }
141201784826SJohnathan Mantey 
141301784826SJohnathan Mantey             if (thisJson.is_null())
141401784826SJohnathan Mantey             {
14159c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
141601784826SJohnathan Mantey             }
141785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
141801784826SJohnathan Mantey             {
141989492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
142089492a15SPatrick Williams                                                   ipv4Data.cend());
142101784826SJohnathan Mantey             }
142201784826SJohnathan Mantey             entryIdx++;
142301784826SJohnathan Mantey         }
142401784826SJohnathan Mantey     }
14254a0cb85cSEd Tanous }
14264a0cb85cSEd Tanous 
14274f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1428f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1429f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14308d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1431f85837bfSRAJESWARAN THILLAIGOVINDAN {
1432f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
14335e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1434f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1435f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1436f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1437f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1438f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1439f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1440f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1441f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1442f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1443bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1444168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1445f85837bfSRAJESWARAN THILLAIGOVINDAN }
1446f85837bfSRAJESWARAN THILLAIGOVINDAN 
14474f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1448f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
144901784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
14508d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1451e48c0fc5SRavi Teja {
145201784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1453e48c0fc5SRavi Teja     {
145471f52d96SEd Tanous         messages::propertyValueTypeError(
145571f52d96SEd Tanous             asyncResp->res,
1456bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1457e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1458e48c0fc5SRavi Teja         return;
1459e48c0fc5SRavi Teja     }
1460271584abSEd Tanous     size_t entryIdx = 1;
146185ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
14622c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1463f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1464e48c0fc5SRavi Teja     {
146589492a15SPatrick Williams         std::string pathString = "IPv6StaticAddresses/" +
146689492a15SPatrick Williams                                  std::to_string(entryIdx);
1467e48c0fc5SRavi Teja 
146801784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1469e48c0fc5SRavi Teja         {
1470e48c0fc5SRavi Teja             std::optional<std::string> address;
1471e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1472f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1473bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1474bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1475e48c0fc5SRavi Teja             {
147601784826SJohnathan Mantey                 messages::propertyValueFormatError(
147771f52d96SEd Tanous                     asyncResp->res,
147871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
147971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
148071f52d96SEd Tanous                     pathString);
1481e48c0fc5SRavi Teja                 return;
1482e48c0fc5SRavi Teja             }
1483e48c0fc5SRavi Teja 
1484543f4400SEd Tanous             const std::string* addr = nullptr;
1485543f4400SEd Tanous             uint8_t prefix = 0;
148601784826SJohnathan Mantey 
148701784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
148801784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
148901784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
149001784826SJohnathan Mantey             // current request.
1491e48c0fc5SRavi Teja             if (address)
1492e48c0fc5SRavi Teja             {
149301784826SJohnathan Mantey                 addr = &(*address);
1494e48c0fc5SRavi Teja             }
149585ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
149601784826SJohnathan Mantey             {
149785ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
149801784826SJohnathan Mantey             }
149901784826SJohnathan Mantey             else
150001784826SJohnathan Mantey             {
150101784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
150201784826SJohnathan Mantey                                           pathString + "/Address");
150301784826SJohnathan Mantey                 return;
1504e48c0fc5SRavi Teja             }
1505e48c0fc5SRavi Teja 
1506e48c0fc5SRavi Teja             if (prefixLength)
1507e48c0fc5SRavi Teja             {
150801784826SJohnathan Mantey                 prefix = *prefixLength;
150901784826SJohnathan Mantey             }
151085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1511e48c0fc5SRavi Teja             {
151285ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1513e48c0fc5SRavi Teja             }
1514e48c0fc5SRavi Teja             else
1515e48c0fc5SRavi Teja             {
1516e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1517e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
151801784826SJohnathan Mantey                 return;
1519e48c0fc5SRavi Teja             }
1520e48c0fc5SRavi Teja 
152185ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1522e48c0fc5SRavi Teja             {
15239c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
15249c5e585cSRavi Teja                                          nicIpEntry->id, prefix, "", *addr,
1525e48c0fc5SRavi Teja                                          asyncResp);
152689492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
152789492a15SPatrick Williams                                                   ipv6Data.cend());
152801784826SJohnathan Mantey             }
152901784826SJohnathan Mantey             else
153001784826SJohnathan Mantey             {
153101784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1532e48c0fc5SRavi Teja             }
1533e48c0fc5SRavi Teja             entryIdx++;
1534e48c0fc5SRavi Teja         }
153501784826SJohnathan Mantey         else
153601784826SJohnathan Mantey         {
153785ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
153801784826SJohnathan Mantey             {
153901784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
154001784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
154101784826SJohnathan Mantey                 // in error, so bail out.
154201784826SJohnathan Mantey                 if (thisJson.is_null())
154301784826SJohnathan Mantey                 {
154401784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
154501784826SJohnathan Mantey                     return;
154601784826SJohnathan Mantey                 }
154701784826SJohnathan Mantey                 messages::propertyValueFormatError(
154871f52d96SEd Tanous                     asyncResp->res,
154971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
155071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
155171f52d96SEd Tanous                     pathString);
155201784826SJohnathan Mantey                 return;
155301784826SJohnathan Mantey             }
155401784826SJohnathan Mantey 
155501784826SJohnathan Mantey             if (thisJson.is_null())
155601784826SJohnathan Mantey             {
15579c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
155801784826SJohnathan Mantey             }
155985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
156001784826SJohnathan Mantey             {
156189492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
156289492a15SPatrick Williams                                                   ipv6Data.cend());
156301784826SJohnathan Mantey             }
156401784826SJohnathan Mantey             entryIdx++;
156501784826SJohnathan Mantey         }
156601784826SJohnathan Mantey     }
1567e48c0fc5SRavi Teja }
1568e48c0fc5SRavi Teja 
15694f48d5f6SEd Tanous inline void parseInterfaceData(
15708d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15718d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1572e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
157301784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
15744a0cb85cSEd Tanous {
15757a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1576eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1577eeedda23SJohnathan Mantey 
15782c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
157981ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1580*ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
1581*ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
15822c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1583eeedda23SJohnathan Mantey 
1584eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1585eeedda23SJohnathan Mantey 
15867a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
15877a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
15887a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1589b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1590eeedda23SJohnathan Mantey         if (ec)
1591029573d4SEd Tanous         {
1592eeedda23SJohnathan Mantey             return;
1593eeedda23SJohnathan Mantey         }
1594eeedda23SJohnathan Mantey 
1595914e2d5dSEd Tanous         health->inventory = resp;
15967a1dbc48SGeorge Liu         });
1597eeedda23SJohnathan Mantey 
1598eeedda23SJohnathan Mantey     health->populate();
1599eeedda23SJohnathan Mantey 
1600eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1601eeedda23SJohnathan Mantey     {
16020ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
16032c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1604029573d4SEd Tanous     }
1605029573d4SEd Tanous     else
1606029573d4SEd Tanous     {
16072c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
16082c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1609029573d4SEd Tanous     }
1610aa05fb27SJohnathan Mantey 
16112c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
161235fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
161382695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
16142c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
161582695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
161682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
161782695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
161882695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16191f8c7b5dSJohnathan Mantey 
16202c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
162182695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16221f8c7b5dSJohnathan Mantey                                                                : "Disabled";
162382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
162482695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
162582695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16262a133282Smanojkiraneda 
162782695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16284a0cb85cSEd Tanous     {
162982695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1630ab6554f1SJoshi-Mansi 
1631ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1632ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1633ab6554f1SJoshi-Mansi         // FQDN
163482695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1635d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1636d24bfc7aSJennifer Lee         {
16372c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1638d24bfc7aSJennifer Lee         }
16392c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16404a0cb85cSEd Tanous     }
16414a0cb85cSEd Tanous 
1642*ef4c65b7SEd Tanous     jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1643*ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
1644fda13ad2SSunitha Harish 
16452c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
16462c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
16474a0cb85cSEd Tanous 
16482c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
16492c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
16502c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
16512c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
16529eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
16534a0cb85cSEd Tanous     {
16542c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1655fa5053a6SGunnar Mills         if (gatewayStr.empty())
1656fa5053a6SGunnar Mills         {
1657fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1658fa5053a6SGunnar Mills         }
16591476687dSEd Tanous         nlohmann::json::object_t ipv4;
16601476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
16611476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
16621476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
16631476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1664fa5053a6SGunnar Mills 
16652c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1666d1d50814SRavi Teja         {
16671476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1668d1d50814SRavi Teja         }
16691476687dSEd Tanous 
1670b2ba3072SPatrick Williams         ipv4Array.emplace_back(std::move(ipv4));
167101784826SJohnathan Mantey     }
1672d1d50814SRavi Teja 
167382695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
16747ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
16757ea79e5eSRavi Teja     {
16767ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
16777ea79e5eSRavi Teja     }
16787ea79e5eSRavi Teja 
16797ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1680e48c0fc5SRavi Teja 
16812c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
16822c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
16832c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
16842c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
16857f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
16862c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
16877f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
16889eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1689e48c0fc5SRavi Teja     {
16901476687dSEd Tanous         nlohmann::json::object_t ipv6;
16911476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
16921476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
16931476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
1694f8361275SSunitha Harish 
1695b2ba3072SPatrick Williams         ipv6Array.emplace_back(std::move(ipv6));
16962c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1697e48c0fc5SRavi Teja         {
16981476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
16991476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
17001476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1701b2ba3072SPatrick Williams             ipv6StaticArray.emplace_back(std::move(ipv6Static));
170201784826SJohnathan Mantey         }
1703e48c0fc5SRavi Teja     }
1704588c3f0dSKowalski, Kamil }
1705588c3f0dSKowalski, Kamil 
17064f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1707bf648f77SEd Tanous {
170811ba3979SEd Tanous     return iface.starts_with(parent + "_");
1709bf648f77SEd Tanous }
1710bf648f77SEd Tanous 
1711bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1712bf648f77SEd Tanous {
1713bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1714ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
17151476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17161476687dSEd Tanous             [&app](const crow::Request& req,
17171476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17183ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
171945ca1b86SEd Tanous         {
172045ca1b86SEd Tanous             return;
172145ca1b86SEd Tanous         }
172245ca1b86SEd Tanous 
1723bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1724bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1725bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1726bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1727bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1728bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1729bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1730bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1731bf648f77SEd Tanous 
1732bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1733bf648f77SEd Tanous         // preparation
1734002d39b4SEd Tanous         getEthernetIfaceList(
1735002d39b4SEd Tanous             [asyncResp](
17361476687dSEd Tanous                 const bool& success,
1737002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1738bf648f77SEd Tanous             if (!success)
17391abe55efSEd Tanous             {
1740f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17419391bb9cSRapkiewicz, Pawel                 return;
17429391bb9cSRapkiewicz, Pawel             }
17439391bb9cSRapkiewicz, Pawel 
1744002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1745bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1746bf648f77SEd Tanous             std::string tag = "_";
1747bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1748bf648f77SEd Tanous             {
1749bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1750bf648f77SEd Tanous                 if (found == std::string::npos)
1751bf648f77SEd Tanous                 {
17521476687dSEd Tanous                     nlohmann::json::object_t iface;
1753*ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
1754*ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1755*ef4c65b7SEd Tanous                         ifaceItem);
1756b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
1757bf648f77SEd Tanous                 }
1758bf648f77SEd Tanous             }
1759bf648f77SEd Tanous 
1760002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1761bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1762bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1763bf648f77SEd Tanous         });
1764bf648f77SEd Tanous         });
1765bf648f77SEd Tanous 
1766bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1767ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1768bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
176945ca1b86SEd Tanous             [&app](const crow::Request& req,
1770bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1771bf648f77SEd Tanous                    const std::string& ifaceId) {
17723ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
177345ca1b86SEd Tanous         {
177445ca1b86SEd Tanous             return;
177545ca1b86SEd Tanous         }
17764a0cb85cSEd Tanous         getEthernetIfaceData(
1777bf648f77SEd Tanous             ifaceId,
1778002d39b4SEd Tanous             [asyncResp, ifaceId](
1779002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1780002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1781002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
17824a0cb85cSEd Tanous             if (!success)
17831abe55efSEd Tanous             {
1784bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1785bf648f77SEd Tanous                 // existing object, and other errors
1786002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1787002d39b4SEd Tanous                                            ifaceId);
17884a0cb85cSEd Tanous                 return;
17899391bb9cSRapkiewicz, Pawel             }
17904c9afe43SEd Tanous 
1791188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1792188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
17930f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1794188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1795002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
17960f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
17970f74e643SEd Tanous                 "Management Network Interface";
17980f74e643SEd Tanous 
1799002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
18009391bb9cSRapkiewicz, Pawel             });
1801bf648f77SEd Tanous         });
18029391bb9cSRapkiewicz, Pawel 
1803bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1804ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1805bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
180645ca1b86SEd Tanous             [&app](const crow::Request& req,
1807bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1808bf648f77SEd Tanous                    const std::string& ifaceId) {
18093ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
181045ca1b86SEd Tanous         {
181145ca1b86SEd Tanous             return;
181245ca1b86SEd Tanous         }
1813bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1814ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1815d577665bSRatan Gupta         std::optional<std::string> macAddress;
18169a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1817d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1818e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1819f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1820da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
18211f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1822eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
182335fb5311STejas Patil         std::optional<size_t> mtuSize;
18241f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18251f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18260627a2c7SEd Tanous 
182715ed6780SWilly Tu         if (!json_util::readJsonPatch(
18288d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1829002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1830002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1831002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1832ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1833002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18341abe55efSEd Tanous         {
1835588c3f0dSKowalski, Kamil             return;
1836588c3f0dSKowalski, Kamil         }
1837da131a9aSJennifer Lee         if (dhcpv4)
1838da131a9aSJennifer Lee         {
1839002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
18401f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
184182695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
184282695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
184382695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
18441f8c7b5dSJohnathan Mantey             {
18451f8c7b5dSJohnathan Mantey                 return;
18461f8c7b5dSJohnathan Mantey             }
18471f8c7b5dSJohnathan Mantey         }
18481f8c7b5dSJohnathan Mantey 
18491f8c7b5dSJohnathan Mantey         if (dhcpv6)
18501f8c7b5dSJohnathan Mantey         {
1851002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1852002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1853002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1854002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1855002d39b4SEd Tanous                                      "UseDomainName",
185682695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
18571f8c7b5dSJohnathan Mantey             {
18581f8c7b5dSJohnathan Mantey                 return;
18591f8c7b5dSJohnathan Mantey             }
1860da131a9aSJennifer Lee         }
1861da131a9aSJennifer Lee 
1862bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1863bf648f77SEd Tanous         // for JSON preparation
18644a0cb85cSEd Tanous         getEthernetIfaceData(
18652c70f800SEd Tanous             ifaceId,
1866bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1867ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1868d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
18699a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1870e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
18711f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1872bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1873bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1874f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1875002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1876002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1877002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18781abe55efSEd Tanous             if (!success)
18791abe55efSEd Tanous             {
1880588c3f0dSKowalski, Kamil                 // ... otherwise return error
1881bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1882bf648f77SEd Tanous                 // existing object, and other errors
1883002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1884002d39b4SEd Tanous                                            ifaceId);
1885588c3f0dSKowalski, Kamil                 return;
1886588c3f0dSKowalski, Kamil             }
1887588c3f0dSKowalski, Kamil 
18881f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
18891f8c7b5dSJohnathan Mantey             {
1890002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1891002d39b4SEd Tanous                                 asyncResp);
18921f8c7b5dSJohnathan Mantey             }
18931f8c7b5dSJohnathan Mantey 
18940627a2c7SEd Tanous             if (hostname)
18951abe55efSEd Tanous             {
18960627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
18971abe55efSEd Tanous             }
18980627a2c7SEd Tanous 
1899ab6554f1SJoshi-Mansi             if (fqdn)
1900ab6554f1SJoshi-Mansi             {
19012c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1902ab6554f1SJoshi-Mansi             }
1903ab6554f1SJoshi-Mansi 
1904d577665bSRatan Gupta             if (macAddress)
1905d577665bSRatan Gupta             {
1906002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1907d577665bSRatan Gupta             }
1908d577665bSRatan Gupta 
1909d1d50814SRavi Teja             if (ipv4StaticAddresses)
1910d1d50814SRavi Teja             {
1911bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1912bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1913bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1914bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1915bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1916bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1917bf648f77SEd Tanous                 // that, but could be done more efficiently
1918f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1919002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
19201abe55efSEd Tanous             }
19210627a2c7SEd Tanous 
1922f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1923f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1924002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1925002d39b4SEd Tanous                                              asyncResp);
1926f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19279a6fc6feSRavi Teja 
19289a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19299a6fc6feSRavi Teja             {
19309a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19319a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19329a6fc6feSRavi Teja             }
1933e48c0fc5SRavi Teja 
1934e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1935e48c0fc5SRavi Teja             {
1936002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1937002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1938002d39b4SEd Tanous                                                asyncResp);
1939e48c0fc5SRavi Teja             }
1940eeedda23SJohnathan Mantey 
1941eeedda23SJohnathan Mantey             if (interfaceEnabled)
1942eeedda23SJohnathan Mantey             {
1943002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1944002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
1945eeedda23SJohnathan Mantey             }
194635fb5311STejas Patil 
194735fb5311STejas Patil             if (mtuSize)
194835fb5311STejas Patil             {
194935fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
195035fb5311STejas Patil             }
1951588c3f0dSKowalski, Kamil             });
1952bf648f77SEd Tanous         });
19539391bb9cSRapkiewicz, Pawel 
1954bf648f77SEd Tanous     BMCWEB_ROUTE(
1955bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
1956ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
1957bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
195845ca1b86SEd Tanous             [&app](const crow::Request& req,
1959bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
196045ca1b86SEd Tanous                    const std::string& parentIfaceId,
196145ca1b86SEd Tanous                    const std::string& ifaceId) {
19623ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
196345ca1b86SEd Tanous         {
196445ca1b86SEd Tanous             return;
196545ca1b86SEd Tanous         }
19668d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
19670f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
19688d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
1969e439f0f8SKowalski, Kamil 
19702c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
19711abe55efSEd Tanous         {
1972a434f2bdSEd Tanous             return;
1973a434f2bdSEd Tanous         }
1974a434f2bdSEd Tanous 
1975bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1976bf648f77SEd Tanous         // for JSON preparation
19774a0cb85cSEd Tanous         getEthernetIfaceData(
1978bf648f77SEd Tanous             ifaceId,
1979002d39b4SEd Tanous             [asyncResp, parentIfaceId,
1980002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
1981cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
1982cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
198317e22024SJiaqing Zhao             if (success && ethData.vlanId)
19841abe55efSEd Tanous             {
198522872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
1986*ef4c65b7SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1987*ef4c65b7SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1988*ef4c65b7SEd Tanous                     parentIfaceId, ifaceId);
198922872ff3SJiaqing Zhao 
199023a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
199122872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
19921abe55efSEd Tanous             }
19931abe55efSEd Tanous             else
19941abe55efSEd Tanous             {
1995e439f0f8SKowalski, Kamil                 // ... otherwise return error
1996bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1997bf648f77SEd Tanous                 // existing object, and other errors
1998bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
1999d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2000e439f0f8SKowalski, Kamil             }
2001e439f0f8SKowalski, Kamil             });
2002bf648f77SEd Tanous         });
2003e439f0f8SKowalski, Kamil 
2004bf648f77SEd Tanous     BMCWEB_ROUTE(
2005bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20063d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2007bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
200845ca1b86SEd Tanous             [&app](const crow::Request& req,
2009bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
201045ca1b86SEd Tanous                    const std::string& parentIfaceId,
201145ca1b86SEd Tanous                    const std::string& ifaceId) {
20123ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
201345ca1b86SEd Tanous         {
201445ca1b86SEd Tanous             return;
201545ca1b86SEd Tanous         }
2016fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
20171abe55efSEd Tanous         {
2018d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2019002d39b4SEd Tanous                                        ifaceId);
2020927a505aSKowalski, Kamil             return;
2021927a505aSKowalski, Kamil         }
2022927a505aSKowalski, Kamil 
20233927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20243927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20250627a2c7SEd Tanous 
202615ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2027bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20281abe55efSEd Tanous         {
2029927a505aSKowalski, Kamil             return;
2030927a505aSKowalski, Kamil         }
2031927a505aSKowalski, Kamil 
20323927e13eSJiaqing Zhao         if (vlanId)
20333927e13eSJiaqing Zhao         {
20343927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20353927e13eSJiaqing Zhao             return;
20363927e13eSJiaqing Zhao         }
20373927e13eSJiaqing Zhao 
2038bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2039bf648f77SEd Tanous         // for JSON preparation
2040e48c0fc5SRavi Teja         getEthernetIfaceData(
2041bf648f77SEd Tanous             ifaceId,
20423927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2043002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2044cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2045cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
204617e22024SJiaqing Zhao             if (success && ethData.vlanId)
204708244d02SSunitha Harish             {
204823a06317SJiaqing Zhao                 if (vlanEnable)
204923a06317SJiaqing Zhao                 {
205023a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
20515e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
205208244d02SSunitha Harish                         if (ec)
205308244d02SSunitha Harish                         {
205408244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20553927e13eSJiaqing Zhao                             return;
205608244d02SSunitha Harish                         }
205723a06317SJiaqing Zhao                         },
205823a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
205923a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
206023a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
206123a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
206223a06317SJiaqing Zhao                         "NICEnabled",
206323a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
206408244d02SSunitha Harish                 }
206508244d02SSunitha Harish             }
206608244d02SSunitha Harish             else
20671abe55efSEd Tanous             {
2068bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2069bf648f77SEd Tanous                 // existing object, and other errors
2070bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2071d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2072bf648f77SEd Tanous                 return;
2073bf648f77SEd Tanous             }
2074bf648f77SEd Tanous             });
2075bf648f77SEd Tanous         });
2076bf648f77SEd Tanous 
2077bf648f77SEd Tanous     BMCWEB_ROUTE(
2078bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20793d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2080bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
208145ca1b86SEd Tanous             [&app](const crow::Request& req,
2082bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
208345ca1b86SEd Tanous                    const std::string& parentIfaceId,
208445ca1b86SEd Tanous                    const std::string& ifaceId) {
20853ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
208645ca1b86SEd Tanous         {
208745ca1b86SEd Tanous             return;
208845ca1b86SEd Tanous         }
2089bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2090bf648f77SEd Tanous         {
2091d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2092002d39b4SEd Tanous                                        ifaceId);
2093927a505aSKowalski, Kamil             return;
2094927a505aSKowalski, Kamil         }
2095e439f0f8SKowalski, Kamil 
2096bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2097bf648f77SEd Tanous         // for JSON preparation
2098f12894f8SJason M. Bills         getEthernetIfaceData(
2099bf648f77SEd Tanous             ifaceId,
2100002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2101002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2102cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2103cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
210417e22024SJiaqing Zhao             if (success && ethData.vlanId)
21051abe55efSEd Tanous             {
2106f12894f8SJason M. Bills                 auto callback =
21075e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
21081abe55efSEd Tanous                     if (ec)
21091abe55efSEd Tanous                     {
2110f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2111927a505aSKowalski, Kamil                     }
21124a0cb85cSEd Tanous                 };
21134a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2114002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2115002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
21164a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
21171abe55efSEd Tanous             }
21181abe55efSEd Tanous             else
21191abe55efSEd Tanous             {
2120927a505aSKowalski, Kamil                 // ... otherwise return error
2121bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2122bf648f77SEd Tanous                 // existing object, and other errors
2123bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2124d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2125927a505aSKowalski, Kamil             }
2126927a505aSKowalski, Kamil             });
2127bf648f77SEd Tanous         });
2128e439f0f8SKowalski, Kamil 
2129bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2130bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2131ed398213SEd Tanous 
2132ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21331476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21341476687dSEd Tanous             [&app](const crow::Request& req,
2135bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2136bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21373ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
213845ca1b86SEd Tanous         {
213945ca1b86SEd Tanous             return;
214045ca1b86SEd Tanous         }
21414a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21421abe55efSEd Tanous         // preparation
2143002d39b4SEd Tanous         getEthernetIfaceList(
2144002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
21451abe55efSEd Tanous                 const bool& success,
2146002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
21474a0cb85cSEd Tanous             if (!success)
21481abe55efSEd Tanous             {
2149f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21504a0cb85cSEd Tanous                 return;
21511abe55efSEd Tanous             }
21524c9afe43SEd Tanous 
215381ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
21544c9afe43SEd Tanous             {
2155002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2156002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21574c9afe43SEd Tanous                                            rootInterfaceName);
21584c9afe43SEd Tanous                 return;
21594c9afe43SEd Tanous             }
21604c9afe43SEd Tanous 
21610f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21620f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21630f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21640f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21650f74e643SEd Tanous                 "VLAN Network Interface Collection";
21664a0cb85cSEd Tanous 
21672c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21684a0cb85cSEd Tanous 
216981ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21701abe55efSEd Tanous             {
217111ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21724a0cb85cSEd Tanous                 {
21731476687dSEd Tanous                     nlohmann::json::object_t iface;
2174*ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
2175*ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2176*ef4c65b7SEd Tanous                         rootInterfaceName, ifaceItem);
2177b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
2178e439f0f8SKowalski, Kamil                 }
2179e439f0f8SKowalski, Kamil             }
2180e439f0f8SKowalski, Kamil 
2181002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
21822c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2183*ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2184*ef4c65b7SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2185*ef4c65b7SEd Tanous                 rootInterfaceName);
2186e439f0f8SKowalski, Kamil         });
2187bf648f77SEd Tanous         });
2188e439f0f8SKowalski, Kamil 
2189bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2190bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
21913d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2192bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
219345ca1b86SEd Tanous             [&app](const crow::Request& req,
2194bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2195bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
219745ca1b86SEd Tanous         {
219845ca1b86SEd Tanous             return;
219945ca1b86SEd Tanous         }
2200fda13ad2SSunitha Harish         bool vlanEnable = false;
22010627a2c7SEd Tanous         uint32_t vlanId = 0;
2202002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2203002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
22041abe55efSEd Tanous         {
22054a0cb85cSEd Tanous             return;
2206e439f0f8SKowalski, Kamil         }
2207fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2208dbb59d4dSEd Tanous         if (vlanId == 0U)
2209fda13ad2SSunitha Harish         {
2210fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2211fda13ad2SSunitha Harish         }
2212fda13ad2SSunitha Harish         if (!vlanEnable)
2213fda13ad2SSunitha Harish         {
2214fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2215fda13ad2SSunitha Harish         }
2216271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2217fda13ad2SSunitha Harish         {
2218fda13ad2SSunitha Harish             return;
2219fda13ad2SSunitha Harish         }
2220fda13ad2SSunitha Harish 
22215e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
22221abe55efSEd Tanous             if (ec)
22231abe55efSEd Tanous             {
2224bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2225bf648f77SEd Tanous                 // based on phosphor-network responses
2226f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22274a0cb85cSEd Tanous                 return;
22281abe55efSEd Tanous             }
2229f12894f8SJason M. Bills             messages::created(asyncResp->res);
2230e439f0f8SKowalski, Kamil         };
22314a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22324a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22334a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22344a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22350627a2c7SEd Tanous             rootInterfaceName, vlanId);
2236bf648f77SEd Tanous         });
22374a0cb85cSEd Tanous }
2238bf648f77SEd Tanous 
22399391bb9cSRapkiewicz, Pawel } // namespace redfish
2240