xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 89492a157c9cf972b342421e24d41fd382510251)
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>
311214b7e7SGunnar Mills 
327a1dbc48SGeorge Liu #include <array>
33a24526dcSEd Tanous #include <optional>
34ab6554f1SJoshi-Mansi #include <regex>
357a1dbc48SGeorge Liu #include <string_view>
369391bb9cSRapkiewicz, Pawel 
371abe55efSEd Tanous namespace redfish
381abe55efSEd Tanous {
399391bb9cSRapkiewicz, Pawel 
404a0cb85cSEd Tanous enum class LinkType
414a0cb85cSEd Tanous {
424a0cb85cSEd Tanous     Local,
434a0cb85cSEd Tanous     Global
444a0cb85cSEd Tanous };
459391bb9cSRapkiewicz, Pawel 
469391bb9cSRapkiewicz, Pawel /**
479391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
489391bb9cSRapkiewicz, Pawel  */
491abe55efSEd Tanous struct IPv4AddressData
501abe55efSEd Tanous {
51179db1d7SKowalski, Kamil     std::string id;
524a0cb85cSEd Tanous     std::string address;
534a0cb85cSEd Tanous     std::string domain;
544a0cb85cSEd Tanous     std::string gateway;
559391bb9cSRapkiewicz, Pawel     std::string netmask;
569391bb9cSRapkiewicz, Pawel     std::string origin;
574a0cb85cSEd Tanous     LinkType linktype;
5801c6e858SSunitha Harish     bool isActive;
594a0cb85cSEd Tanous 
601abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
611abe55efSEd Tanous     {
624a0cb85cSEd Tanous         return id < obj.id;
631abe55efSEd Tanous     }
649391bb9cSRapkiewicz, Pawel };
659391bb9cSRapkiewicz, Pawel 
669391bb9cSRapkiewicz, Pawel /**
67e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
68e48c0fc5SRavi Teja  */
69e48c0fc5SRavi Teja struct IPv6AddressData
70e48c0fc5SRavi Teja {
71e48c0fc5SRavi Teja     std::string id;
72e48c0fc5SRavi Teja     std::string address;
73e48c0fc5SRavi Teja     std::string origin;
74e48c0fc5SRavi Teja     uint8_t prefixLength;
75e48c0fc5SRavi Teja 
76e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
77e48c0fc5SRavi Teja     {
78e48c0fc5SRavi Teja         return id < obj.id;
79e48c0fc5SRavi Teja     }
80e48c0fc5SRavi Teja };
81e48c0fc5SRavi Teja /**
829391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
839391bb9cSRapkiewicz, Pawel  * available from DBus
849391bb9cSRapkiewicz, Pawel  */
851abe55efSEd Tanous struct EthernetInterfaceData
861abe55efSEd Tanous {
874a0cb85cSEd Tanous     uint32_t speed;
8835fb5311STejas Patil     size_t mtuSize;
8982695a5bSJiaqing Zhao     bool autoNeg;
9082695a5bSJiaqing Zhao     bool dnsEnabled;
9182695a5bSJiaqing Zhao     bool ntpEnabled;
9282695a5bSJiaqing Zhao     bool hostNameEnabled;
93aa05fb27SJohnathan Mantey     bool linkUp;
94eeedda23SJohnathan Mantey     bool nicEnabled;
9582695a5bSJiaqing Zhao     std::string dhcpEnabled;
961f8c7b5dSJohnathan Mantey     std::string operatingMode;
9782695a5bSJiaqing Zhao     std::string hostName;
9882695a5bSJiaqing Zhao     std::string defaultGateway;
9982695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
10082695a5bSJiaqing Zhao     std::string macAddress;
10117e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
1020f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
1030f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
104d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1059391bb9cSRapkiewicz, Pawel };
1069391bb9cSRapkiewicz, Pawel 
1071f8c7b5dSJohnathan Mantey struct DHCPParameters
1081f8c7b5dSJohnathan Mantey {
1091f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
11082695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
11182695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
11282695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1131f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1141f8c7b5dSJohnathan Mantey };
1151f8c7b5dSJohnathan Mantey 
1169391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1179391bb9cSRapkiewicz, Pawel // into full dot notation
1181abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1191abe55efSEd Tanous {
1209391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1219391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1229391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1239391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1249391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1259391bb9cSRapkiewicz, Pawel     return netmask;
1269391bb9cSRapkiewicz, Pawel }
1279391bb9cSRapkiewicz, Pawel 
12882695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1291f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1301f8c7b5dSJohnathan Mantey {
1311f8c7b5dSJohnathan Mantey     if (isIPv4)
1321f8c7b5dSJohnathan Mantey     {
1331f8c7b5dSJohnathan Mantey         return (
1341f8c7b5dSJohnathan Mantey             (inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1361f8c7b5dSJohnathan Mantey             (inputDHCP ==
1371f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1381f8c7b5dSJohnathan Mantey     }
1391f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1401f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1411f8c7b5dSJohnathan Mantey             (inputDHCP ==
1421f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1431f8c7b5dSJohnathan Mantey }
1441f8c7b5dSJohnathan Mantey 
1452c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1461f8c7b5dSJohnathan Mantey {
1471f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1481f8c7b5dSJohnathan Mantey     {
1491f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1501f8c7b5dSJohnathan Mantey     }
1513174e4dfSEd Tanous     if (isIPv4)
1521f8c7b5dSJohnathan Mantey     {
1531f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1541f8c7b5dSJohnathan Mantey     }
1553174e4dfSEd Tanous     if (isIPv6)
1561f8c7b5dSJohnathan Mantey     {
1571f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1581f8c7b5dSJohnathan Mantey     }
1591f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1601f8c7b5dSJohnathan Mantey }
1611f8c7b5dSJohnathan Mantey 
1624a0cb85cSEd Tanous inline std::string
1634a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1644a0cb85cSEd Tanous                                         bool isIPv4)
1651abe55efSEd Tanous {
1664a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1671abe55efSEd Tanous     {
1684a0cb85cSEd Tanous         return "Static";
1699391bb9cSRapkiewicz, Pawel     }
1704a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1711abe55efSEd Tanous     {
1724a0cb85cSEd Tanous         if (isIPv4)
1731abe55efSEd Tanous         {
1744a0cb85cSEd Tanous             return "IPv4LinkLocal";
1751abe55efSEd Tanous         }
1764a0cb85cSEd Tanous         return "LinkLocal";
1779391bb9cSRapkiewicz, Pawel     }
1784a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1791abe55efSEd Tanous     {
1804a0cb85cSEd Tanous         if (isIPv4)
1814a0cb85cSEd Tanous         {
1824a0cb85cSEd Tanous             return "DHCP";
1834a0cb85cSEd Tanous         }
1844a0cb85cSEd Tanous         return "DHCPv6";
1854a0cb85cSEd Tanous     }
1864a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1874a0cb85cSEd Tanous     {
1884a0cb85cSEd Tanous         return "SLAAC";
1894a0cb85cSEd Tanous     }
1904a0cb85cSEd Tanous     return "";
1914a0cb85cSEd Tanous }
1924a0cb85cSEd Tanous 
19302cad96eSEd Tanous inline bool extractEthernetInterfaceData(
19402cad96eSEd Tanous     const std::string& ethifaceId,
19502cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1964a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1974a0cb85cSEd Tanous {
1984c9afe43SEd Tanous     bool idFound = false;
19902cad96eSEd Tanous     for (const auto& objpath : dbusData)
2004a0cb85cSEd Tanous     {
20102cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
2024a0cb85cSEd Tanous         {
20381ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
204029573d4SEd Tanous             {
2054c9afe43SEd Tanous                 idFound = true;
2064a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2074a0cb85cSEd Tanous                 {
2084a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2094a0cb85cSEd Tanous                     {
2104a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2114a0cb85cSEd Tanous                         {
2124a0cb85cSEd Tanous                             const std::string* mac =
213abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2144a0cb85cSEd Tanous                             if (mac != nullptr)
2154a0cb85cSEd Tanous                             {
21682695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2174a0cb85cSEd Tanous                             }
2184a0cb85cSEd Tanous                         }
2194a0cb85cSEd Tanous                     }
2204a0cb85cSEd Tanous                 }
2214a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2224a0cb85cSEd Tanous                 {
2234a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2244a0cb85cSEd Tanous                     {
2254a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2264a0cb85cSEd Tanous                         {
2271b6b96c5SEd Tanous                             const uint32_t* id =
228abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2294a0cb85cSEd Tanous                             if (id != nullptr)
2304a0cb85cSEd Tanous                             {
23117e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2324a0cb85cSEd Tanous                             }
2334a0cb85cSEd Tanous                         }
2344a0cb85cSEd Tanous                     }
2354a0cb85cSEd Tanous                 }
2364a0cb85cSEd Tanous                 else if (ifacePair.first ==
2374a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2384a0cb85cSEd Tanous                 {
2394a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2404a0cb85cSEd Tanous                     {
2414a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2424a0cb85cSEd Tanous                         {
2432c70f800SEd Tanous                             const bool* autoNeg =
244abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2452c70f800SEd Tanous                             if (autoNeg != nullptr)
2464a0cb85cSEd Tanous                             {
24782695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2484a0cb85cSEd Tanous                             }
2494a0cb85cSEd Tanous                         }
2504a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2514a0cb85cSEd Tanous                         {
2524a0cb85cSEd Tanous                             const uint32_t* speed =
253abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2544a0cb85cSEd Tanous                             if (speed != nullptr)
2554a0cb85cSEd Tanous                             {
2564a0cb85cSEd Tanous                                 ethData.speed = *speed;
2574a0cb85cSEd Tanous                             }
2584a0cb85cSEd Tanous                         }
25935fb5311STejas Patil                         else if (propertyPair.first == "MTU")
26035fb5311STejas Patil                         {
26135fb5311STejas Patil                             const uint32_t* mtuSize =
26235fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
26335fb5311STejas Patil                             if (mtuSize != nullptr)
26435fb5311STejas Patil                             {
26535fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
26635fb5311STejas Patil                             }
26735fb5311STejas Patil                         }
268aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
269aa05fb27SJohnathan Mantey                         {
270aa05fb27SJohnathan Mantey                             const bool* linkUp =
271aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
273aa05fb27SJohnathan Mantey                             {
274aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
275aa05fb27SJohnathan Mantey                             }
276aa05fb27SJohnathan Mantey                         }
277eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
278eeedda23SJohnathan Mantey                         {
279eeedda23SJohnathan Mantey                             const bool* nicEnabled =
280eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
281eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
282eeedda23SJohnathan Mantey                             {
283eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
284eeedda23SJohnathan Mantey                             }
285eeedda23SJohnathan Mantey                         }
286f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
287029573d4SEd Tanous                         {
288029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2898d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
290029573d4SEd Tanous                                     &propertyPair.second);
291029573d4SEd Tanous                             if (nameservers != nullptr)
292029573d4SEd Tanous                             {
293f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2940f6efdc1Smanojkiran.eda@gmail.com                             }
2950f6efdc1Smanojkiran.eda@gmail.com                         }
2960f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2970f6efdc1Smanojkiran.eda@gmail.com                         {
2980f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2998d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
3000f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
3010f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
3020f6efdc1Smanojkiran.eda@gmail.com                             {
303f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
3044a0cb85cSEd Tanous                             }
3054a0cb85cSEd Tanous                         }
3062a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3072a133282Smanojkiraneda                         {
3082c70f800SEd Tanous                             const std::string* dhcpEnabled =
3091f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3102c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3112a133282Smanojkiraneda                             {
31282695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3132a133282Smanojkiraneda                             }
3142a133282Smanojkiraneda                         }
315d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
316d24bfc7aSJennifer Lee                         {
317d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3188d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
319d24bfc7aSJennifer Lee                                     &propertyPair.second);
320d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
321d24bfc7aSJennifer Lee                             {
322f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
323d24bfc7aSJennifer Lee                             }
324d24bfc7aSJennifer Lee                         }
3259010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3269010ec2eSRavi Teja                         {
3279010ec2eSRavi Teja                             const std::string* defaultGateway =
3289010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3299010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3309010ec2eSRavi Teja                             {
3319010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3329010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3339010ec2eSRavi Teja                                 {
33482695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3359010ec2eSRavi Teja                                 }
3369010ec2eSRavi Teja                                 else
3379010ec2eSRavi Teja                                 {
33882695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3399010ec2eSRavi Teja                                 }
3409010ec2eSRavi Teja                             }
3419010ec2eSRavi Teja                         }
3429010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3439010ec2eSRavi Teja                         {
3449010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3459010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3469010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3479010ec2eSRavi Teja                             {
3489010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3499010ec2eSRavi Teja                                     *defaultGateway6;
3509010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3519010ec2eSRavi Teja                                 {
35282695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3539010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3549010ec2eSRavi Teja                                 }
3559010ec2eSRavi Teja                                 else
3569010ec2eSRavi Teja                                 {
35782695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3589010ec2eSRavi Teja                                         defaultGateway6Str;
3599010ec2eSRavi Teja                                 }
3609010ec2eSRavi Teja                             }
3619010ec2eSRavi Teja                         }
362029573d4SEd Tanous                     }
363029573d4SEd Tanous                 }
364029573d4SEd Tanous             }
3651f8c7b5dSJohnathan Mantey 
3661e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3671f8c7b5dSJohnathan Mantey             {
3681f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3691f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3701f8c7b5dSJohnathan Mantey                 {
3711f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3721f8c7b5dSJohnathan Mantey                     {
3731f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3741f8c7b5dSJohnathan Mantey                         {
3752c70f800SEd Tanous                             const bool* dnsEnabled =
3761f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3772c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3781f8c7b5dSJohnathan Mantey                             {
37982695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3801f8c7b5dSJohnathan Mantey                             }
3811f8c7b5dSJohnathan Mantey                         }
3821f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3831f8c7b5dSJohnathan Mantey                         {
3842c70f800SEd Tanous                             const bool* ntpEnabled =
3851f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3862c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3871f8c7b5dSJohnathan Mantey                             {
38882695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3891f8c7b5dSJohnathan Mantey                             }
3901f8c7b5dSJohnathan Mantey                         }
3911f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3921f8c7b5dSJohnathan Mantey                         {
3932c70f800SEd Tanous                             const bool* hostNameEnabled =
3941f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3952c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3961f8c7b5dSJohnathan Mantey                             {
39782695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3981f8c7b5dSJohnathan Mantey                             }
3991f8c7b5dSJohnathan Mantey                         }
4001f8c7b5dSJohnathan Mantey                     }
4011f8c7b5dSJohnathan Mantey                 }
4021f8c7b5dSJohnathan Mantey             }
403029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
404029573d4SEd Tanous             // to check eth number
405029573d4SEd Tanous             if (ifacePair.first ==
4064a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4074a0cb85cSEd Tanous             {
4084a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4094a0cb85cSEd Tanous                 {
4104a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4114a0cb85cSEd Tanous                     {
4124a0cb85cSEd Tanous                         const std::string* hostname =
4138d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4144a0cb85cSEd Tanous                         if (hostname != nullptr)
4154a0cb85cSEd Tanous                         {
41682695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4174a0cb85cSEd Tanous                         }
4184a0cb85cSEd Tanous                     }
4194a0cb85cSEd Tanous                 }
4204a0cb85cSEd Tanous             }
4214a0cb85cSEd Tanous         }
4224a0cb85cSEd Tanous     }
4234c9afe43SEd Tanous     return idFound;
4244a0cb85cSEd Tanous }
4254a0cb85cSEd Tanous 
426e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42701784826SJohnathan Mantey inline void
42881ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
429711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
43081ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
431e48c0fc5SRavi Teja {
432*89492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
433*89492a15SPatrick Williams                                     ethifaceId;
434e48c0fc5SRavi Teja 
435e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
436e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43781ce609eSEd Tanous     for (const auto& objpath : dbusData)
438e48c0fc5SRavi Teja     {
439e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
440353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
441e48c0fc5SRavi Teja         {
4429eb808c1SEd Tanous             for (const auto& interface : objpath.second)
443e48c0fc5SRavi Teja             {
444e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
445e48c0fc5SRavi Teja                 {
446353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
447353163e9STony Lee                                              interface.second.end(),
448353163e9STony Lee                                              [](const auto& property) {
449353163e9STony Lee                         return property.first == "Type";
450353163e9STony Lee                     });
451353163e9STony Lee                     if (type == interface.second.end())
452353163e9STony Lee                     {
453353163e9STony Lee                         continue;
454353163e9STony Lee                     }
455353163e9STony Lee 
456353163e9STony Lee                     const std::string* typeStr =
457353163e9STony Lee                         std::get_if<std::string>(&type->second);
458353163e9STony Lee 
459353163e9STony Lee                     if (typeStr == nullptr ||
460353163e9STony Lee                         (*typeStr !=
461353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
462353163e9STony Lee                     {
463353163e9STony Lee                         continue;
464353163e9STony Lee                     }
465353163e9STony Lee 
466e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
467e48c0fc5SRavi Teja                     // appropriate
468e48c0fc5SRavi Teja                     std::pair<
469e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
470e48c0fc5SRavi Teja                         bool>
47181ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4722c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4732c70f800SEd Tanous                     ipv6Address.id =
474353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
4759eb808c1SEd Tanous                     for (const auto& property : interface.second)
476e48c0fc5SRavi Teja                     {
477e48c0fc5SRavi Teja                         if (property.first == "Address")
478e48c0fc5SRavi Teja                         {
479e48c0fc5SRavi Teja                             const std::string* address =
480e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
481e48c0fc5SRavi Teja                             if (address != nullptr)
482e48c0fc5SRavi Teja                             {
4832c70f800SEd Tanous                                 ipv6Address.address = *address;
484e48c0fc5SRavi Teja                             }
485e48c0fc5SRavi Teja                         }
486e48c0fc5SRavi Teja                         else if (property.first == "Origin")
487e48c0fc5SRavi Teja                         {
488e48c0fc5SRavi Teja                             const std::string* origin =
489e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
490e48c0fc5SRavi Teja                             if (origin != nullptr)
491e48c0fc5SRavi Teja                             {
4922c70f800SEd Tanous                                 ipv6Address.origin =
493e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
494e48c0fc5SRavi Teja                                                                         false);
495e48c0fc5SRavi Teja                             }
496e48c0fc5SRavi Teja                         }
497e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
498e48c0fc5SRavi Teja                         {
499e48c0fc5SRavi Teja                             const uint8_t* prefix =
500e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
501e48c0fc5SRavi Teja                             if (prefix != nullptr)
502e48c0fc5SRavi Teja                             {
5032c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
504e48c0fc5SRavi Teja                             }
505e48c0fc5SRavi Teja                         }
506889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
507889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
508889ff694SAsmitha Karunanithi                         {
509889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
510889ff694SAsmitha Karunanithi                         }
511e48c0fc5SRavi Teja                         else
512e48c0fc5SRavi Teja                         {
513e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
514e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
515e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
516e48c0fc5SRavi Teja                         }
517e48c0fc5SRavi Teja                     }
518e48c0fc5SRavi Teja                 }
519e48c0fc5SRavi Teja             }
520e48c0fc5SRavi Teja         }
521e48c0fc5SRavi Teja     }
522e48c0fc5SRavi Teja }
523e48c0fc5SRavi Teja 
5244a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
52501784826SJohnathan Mantey inline void
52681ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
527711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
52881ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5294a0cb85cSEd Tanous {
530*89492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
531*89492a15SPatrick Williams                                     ethifaceId;
5324a0cb85cSEd Tanous 
5334a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5344a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
53581ce609eSEd Tanous     for (const auto& objpath : dbusData)
5364a0cb85cSEd Tanous     {
5374a0cb85cSEd Tanous         // Check if proper pattern for object path appears
538353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
5394a0cb85cSEd Tanous         {
5409eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5414a0cb85cSEd Tanous             {
5424a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5434a0cb85cSEd Tanous                 {
544353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
545353163e9STony Lee                                              interface.second.end(),
546353163e9STony Lee                                              [](const auto& property) {
547353163e9STony Lee                         return property.first == "Type";
548353163e9STony Lee                     });
549353163e9STony Lee                     if (type == interface.second.end())
550353163e9STony Lee                     {
551353163e9STony Lee                         continue;
552353163e9STony Lee                     }
553353163e9STony Lee 
554353163e9STony Lee                     const std::string* typeStr =
555353163e9STony Lee                         std::get_if<std::string>(&type->second);
556353163e9STony Lee 
557353163e9STony Lee                     if (typeStr == nullptr ||
558353163e9STony Lee                         (*typeStr !=
559353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
560353163e9STony Lee                     {
561353163e9STony Lee                         continue;
562353163e9STony Lee                     }
563353163e9STony Lee 
5644a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5654a0cb85cSEd Tanous                     // appropriate
5664a0cb85cSEd Tanous                     std::pair<
5674a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5684a0cb85cSEd Tanous                         bool>
56981ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5702c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5712c70f800SEd Tanous                     ipv4Address.id =
572353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
5739eb808c1SEd Tanous                     for (const auto& property : interface.second)
5744a0cb85cSEd Tanous                     {
5754a0cb85cSEd Tanous                         if (property.first == "Address")
5764a0cb85cSEd Tanous                         {
5774a0cb85cSEd Tanous                             const std::string* address =
578abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5794a0cb85cSEd Tanous                             if (address != nullptr)
5804a0cb85cSEd Tanous                             {
5812c70f800SEd Tanous                                 ipv4Address.address = *address;
5824a0cb85cSEd Tanous                             }
5834a0cb85cSEd Tanous                         }
5844a0cb85cSEd Tanous                         else if (property.first == "Origin")
5854a0cb85cSEd Tanous                         {
5864a0cb85cSEd Tanous                             const std::string* origin =
587abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5884a0cb85cSEd Tanous                             if (origin != nullptr)
5894a0cb85cSEd Tanous                             {
5902c70f800SEd Tanous                                 ipv4Address.origin =
5914a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5924a0cb85cSEd Tanous                                                                         true);
5934a0cb85cSEd Tanous                             }
5944a0cb85cSEd Tanous                         }
5954a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5964a0cb85cSEd Tanous                         {
5974a0cb85cSEd Tanous                             const uint8_t* mask =
598abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5994a0cb85cSEd Tanous                             if (mask != nullptr)
6004a0cb85cSEd Tanous                             {
6014a0cb85cSEd Tanous                                 // convert it to the string
6022c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
6034a0cb85cSEd Tanous                             }
6044a0cb85cSEd Tanous                         }
605889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
606889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
607889ff694SAsmitha Karunanithi                         {
608889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
609889ff694SAsmitha Karunanithi                         }
6104a0cb85cSEd Tanous                         else
6114a0cb85cSEd Tanous                         {
6124a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
6134a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
6144a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
6154a0cb85cSEd Tanous                         }
6164a0cb85cSEd Tanous                     }
6174a0cb85cSEd Tanous                     // Check if given address is local, or global
6182c70f800SEd Tanous                     ipv4Address.linktype =
61911ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
62018659d10SJohnathan Mantey                             ? LinkType::Local
62118659d10SJohnathan Mantey                             : LinkType::Global;
6224a0cb85cSEd Tanous                 }
6234a0cb85cSEd Tanous             }
6244a0cb85cSEd Tanous         }
6254a0cb85cSEd Tanous     }
6264a0cb85cSEd Tanous }
627588c3f0dSKowalski, Kamil 
628588c3f0dSKowalski, Kamil /**
62901784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
630179db1d7SKowalski, Kamil  *
631179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
632179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
633179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
634179db1d7SKowalski, Kamil  *
635179db1d7SKowalski, Kamil  * @return None
636179db1d7SKowalski, Kamil  */
6379c5e585cSRavi Teja inline void deleteIPAddress(const std::string& ifaceId,
6389c5e585cSRavi Teja                             const std::string& ipHash,
6398d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6401abe55efSEd Tanous {
64155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6425e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6431abe55efSEd Tanous         if (ec)
6441abe55efSEd Tanous         {
645a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6461abe55efSEd Tanous         }
647179db1d7SKowalski, Kamil         },
648179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
6499c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + ipHash,
650179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
651179db1d7SKowalski, Kamil }
652179db1d7SKowalski, Kamil 
653244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
654244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
655244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6569010ec2eSRavi Teja {
6579010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6585e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6599010ec2eSRavi Teja         if (ec)
6609010ec2eSRavi Teja         {
6619010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6629010ec2eSRavi Teja             return;
6639010ec2eSRavi Teja         }
6649010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6659010ec2eSRavi Teja         },
6669010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6679010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6689010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6699010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
670168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6719010ec2eSRavi Teja }
672179db1d7SKowalski, Kamil /**
67301784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
674179db1d7SKowalski, Kamil  *
67501784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
67601784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
67701784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
67801784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
679179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
680179db1d7SKowalski, Kamil  *
681179db1d7SKowalski, Kamil  * @return None
682179db1d7SKowalski, Kamil  */
683cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
684cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6858d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6861abe55efSEd Tanous {
687002d39b4SEd Tanous     auto createIpHandler =
6885e7e2dc5SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
6891abe55efSEd Tanous         if (ec)
6901abe55efSEd Tanous         {
691a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6929010ec2eSRavi Teja             return;
693179db1d7SKowalski, Kamil         }
6949010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6959010ec2eSRavi Teja     };
6969010ec2eSRavi Teja 
6979010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6989010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
699179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
700179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
70101784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
702179db1d7SKowalski, Kamil         gateway);
703179db1d7SKowalski, Kamil }
704e48c0fc5SRavi Teja 
705e48c0fc5SRavi Teja /**
70601784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
70701784826SJohnathan Mantey  * static IPv6 entry
70801784826SJohnathan Mantey  *
70901784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
71001784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
71101784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
71201784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
71301784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
71401784826SJohnathan Mantey  *
71501784826SJohnathan Mantey  * @return None
71601784826SJohnathan Mantey  */
7179c5e585cSRavi Teja 
7189c5e585cSRavi Teja enum class IpVersion
7199c5e585cSRavi Teja {
7209c5e585cSRavi Teja     IpV4,
7219c5e585cSRavi Teja     IpV6
7229c5e585cSRavi Teja };
7239c5e585cSRavi Teja 
7249c5e585cSRavi Teja inline void deleteAndCreateIPAddress(
7259c5e585cSRavi Teja     IpVersion version, const std::string& ifaceId, const std::string& id,
7268d1b46d7Szhanghch05     uint8_t prefixLength, const std::string& address,
7279c5e585cSRavi Teja     const std::string& gateway,
7288d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
72901784826SJohnathan Mantey {
73001784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
7319c5e585cSRavi Teja         [asyncResp, version, ifaceId, address, prefixLength,
7329c5e585cSRavi Teja          gateway](const boost::system::error_code& ec) {
73301784826SJohnathan Mantey         if (ec)
73401784826SJohnathan Mantey         {
73501784826SJohnathan Mantey             messages::internalError(asyncResp->res);
73601784826SJohnathan Mantey         }
7379c5e585cSRavi Teja         std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
7389c5e585cSRavi Teja         protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
73901784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
7405e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
74123a21a1cSEd Tanous             if (ec2)
74201784826SJohnathan Mantey             {
74301784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
74401784826SJohnathan Mantey             }
74501784826SJohnathan Mantey             },
74601784826SJohnathan Mantey             "xyz.openbmc_project.Network",
74701784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
7489c5e585cSRavi Teja             "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
7499c5e585cSRavi Teja             prefixLength, gateway);
75001784826SJohnathan Mantey         },
75101784826SJohnathan Mantey         "xyz.openbmc_project.Network",
7529c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + id,
75301784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
75401784826SJohnathan Mantey }
75501784826SJohnathan Mantey 
75601784826SJohnathan Mantey /**
757e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
758e48c0fc5SRavi Teja  *
759e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
760e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
761e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
762e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
763e48c0fc5SRavi Teja  *
764e48c0fc5SRavi Teja  * @return None
765e48c0fc5SRavi Teja  */
76601784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
76701784826SJohnathan Mantey                        const std::string& address,
7688d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
769e48c0fc5SRavi Teja {
7705e7e2dc5SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
771e48c0fc5SRavi Teja         if (ec)
772e48c0fc5SRavi Teja         {
773e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
774e48c0fc5SRavi Teja         }
775e48c0fc5SRavi Teja     };
776e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
7774e0453b1SGunnar Mills     // does not have associated gateway property
778e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
779e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
780e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
781e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
782e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
783e48c0fc5SRavi Teja         "");
784e48c0fc5SRavi Teja }
785e48c0fc5SRavi Teja 
786179db1d7SKowalski, Kamil /**
787179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
788179db1d7SKowalski, Kamil  * Object
789179db1d7SKowalski, Kamil  * from EntityManager Network Manager
7904a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
791179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
792179db1d7SKowalski, Kamil  * into JSON
793179db1d7SKowalski, Kamil  */
794179db1d7SKowalski, Kamil template <typename CallbackFunc>
79581ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
7961abe55efSEd Tanous                           CallbackFunc&& callback)
7971abe55efSEd Tanous {
798f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
799f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
800f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
801f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
802f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
8035e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
80402cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
80555c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
8064a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
807e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
808179db1d7SKowalski, Kamil 
80981ce609eSEd Tanous         if (errorCode)
8101abe55efSEd Tanous         {
81101784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
812179db1d7SKowalski, Kamil             return;
813179db1d7SKowalski, Kamil         }
814179db1d7SKowalski, Kamil 
815002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
8164c9afe43SEd Tanous         if (!found)
8174c9afe43SEd Tanous         {
81801784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8194c9afe43SEd Tanous             return;
8204c9afe43SEd Tanous         }
8214c9afe43SEd Tanous 
8222c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
823179db1d7SKowalski, Kamil         // Fix global GW
8241abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8251abe55efSEd Tanous         {
826c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
827c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8289010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8291abe55efSEd Tanous             {
83082695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
831179db1d7SKowalski, Kamil             }
832179db1d7SKowalski, Kamil         }
833179db1d7SKowalski, Kamil 
8342c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8354e0453b1SGunnar Mills         // Finally make a callback with useful data
83601784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
837f5892d0dSGeorge Liu         });
838271584abSEd Tanous }
839179db1d7SKowalski, Kamil 
840179db1d7SKowalski, Kamil /**
8419391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
8429391bb9cSRapkiewicz, Pawel  * Manager
8431abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
8441abe55efSEd Tanous  * into JSON.
8459391bb9cSRapkiewicz, Pawel  */
8469391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
8471abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
8481abe55efSEd Tanous {
849f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
850f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
851f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
852f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
8535e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
854f5892d0dSGeorge Liu             const dbus::utility::ManagedObjectType& resp) {
8551abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
8561abe55efSEd Tanous         // ethernet interfaces
8572c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
8582c70f800SEd Tanous         ifaceList.reserve(resp.size());
85981ce609eSEd Tanous         if (errorCode)
8601abe55efSEd Tanous         {
8612c70f800SEd Tanous             callback(false, ifaceList);
8629391bb9cSRapkiewicz, Pawel             return;
8639391bb9cSRapkiewicz, Pawel         }
8649391bb9cSRapkiewicz, Pawel 
8659391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
8664a0cb85cSEd Tanous         for (const auto& objpath : resp)
8671abe55efSEd Tanous         {
8689391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
8694a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
8701abe55efSEd Tanous             {
8711abe55efSEd Tanous                 // If interface is
8724a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
8734a0cb85cSEd Tanous                 // what we're looking for.
8749391bb9cSRapkiewicz, Pawel                 if (interface.first ==
8751abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
8761abe55efSEd Tanous                 {
8772dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
8782dfd18efSEd Tanous                     if (ifaceId.empty())
8791abe55efSEd Tanous                     {
8802dfd18efSEd Tanous                         continue;
8819391bb9cSRapkiewicz, Pawel                     }
8822dfd18efSEd Tanous                     // and put it into output vector.
8832dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
8849391bb9cSRapkiewicz, Pawel                 }
8859391bb9cSRapkiewicz, Pawel             }
8869391bb9cSRapkiewicz, Pawel         }
887a434f2bdSEd Tanous         // Finally make a callback with useful data
8882c70f800SEd Tanous         callback(true, ifaceList);
889f5892d0dSGeorge Liu         });
890271584abSEd Tanous }
8919391bb9cSRapkiewicz, Pawel 
8924f48d5f6SEd Tanous inline void
8934f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
8948d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
8951abe55efSEd Tanous {
896ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
897ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
898ab6554f1SJoshi-Mansi     {
899ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
900ab6554f1SJoshi-Mansi                                            "HostName");
901ab6554f1SJoshi-Mansi         return;
902ab6554f1SJoshi-Mansi     }
903bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
9045e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
9054a0cb85cSEd Tanous         if (ec)
9064a0cb85cSEd Tanous         {
907a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
9081abe55efSEd Tanous         }
909bc0bd6e0SEd Tanous         },
910bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
911bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
912bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
913168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
914588c3f0dSKowalski, Kamil }
915588c3f0dSKowalski, Kamil 
9164f48d5f6SEd Tanous inline void
91735fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
91835fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
91935fb5311STejas Patil {
920*89492a15SPatrick Williams     sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
921*89492a15SPatrick Williams                                               ifaceId;
92235fb5311STejas Patil     crow::connections::systemBus->async_method_call(
9235e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
92435fb5311STejas Patil         if (ec)
92535fb5311STejas Patil         {
92635fb5311STejas Patil             messages::internalError(asyncResp->res);
92735fb5311STejas Patil         }
92835fb5311STejas Patil         },
92935fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
93035fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
93135fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
93235fb5311STejas Patil         std::variant<size_t>(mtuSize));
93335fb5311STejas Patil }
93435fb5311STejas Patil 
93535fb5311STejas Patil inline void
9364f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
937bf648f77SEd Tanous                           const std::string& domainname,
9388d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
939ab6554f1SJoshi-Mansi {
940ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
941ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
9425e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
943ab6554f1SJoshi-Mansi         if (ec)
944ab6554f1SJoshi-Mansi         {
945ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
946ab6554f1SJoshi-Mansi         }
947ab6554f1SJoshi-Mansi         },
948ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
949ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
950ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
951ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
952168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
953ab6554f1SJoshi-Mansi }
954ab6554f1SJoshi-Mansi 
9554f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
956bf648f77SEd Tanous {
957bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
958bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
959bf648f77SEd Tanous     {
960bf648f77SEd Tanous         return false;
961bf648f77SEd Tanous     }
962bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
963bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
964bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
965bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
966bf648f77SEd Tanous     const std::regex pattern(
967bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
968bf648f77SEd Tanous 
969bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
970bf648f77SEd Tanous }
971bf648f77SEd Tanous 
9724f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
973bf648f77SEd Tanous {
974bf648f77SEd Tanous     // Can have multiple subdomains
975bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
9760fda0f12SGeorge Liu     const std::regex pattern(
9770fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
978bf648f77SEd Tanous 
979bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
980bf648f77SEd Tanous }
981bf648f77SEd Tanous 
9824f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
9838d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
984ab6554f1SJoshi-Mansi {
985ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
986ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
987ab6554f1SJoshi-Mansi     {
988ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
989ab6554f1SJoshi-Mansi         return;
990ab6554f1SJoshi-Mansi     }
991ab6554f1SJoshi-Mansi 
992ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
993ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
994ab6554f1SJoshi-Mansi     {
995ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
996ab6554f1SJoshi-Mansi         return;
997ab6554f1SJoshi-Mansi     }
998ab6554f1SJoshi-Mansi 
999ab6554f1SJoshi-Mansi     std::string hostname;
1000ab6554f1SJoshi-Mansi     std::string domainname;
1001ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1002ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1003ab6554f1SJoshi-Mansi 
1004ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1005ab6554f1SJoshi-Mansi     {
1006ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1007ab6554f1SJoshi-Mansi         return;
1008ab6554f1SJoshi-Mansi     }
1009ab6554f1SJoshi-Mansi 
1010ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1011ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1012ab6554f1SJoshi-Mansi }
1013ab6554f1SJoshi-Mansi 
10144f48d5f6SEd Tanous inline void
10154f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1016bf648f77SEd Tanous                           const std::string& macAddress,
10178d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1018d577665bSRatan Gupta {
101958283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
102058283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
102158283f41SJohnathan Mantey 
1022d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
10235e7e2dc5SEd Tanous         [asyncResp, macAddress](const boost::system::error_code& ec,
10245b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1025d577665bSRatan Gupta         if (ec)
1026d577665bSRatan Gupta         {
102758283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
102858283f41SJohnathan Mantey             if (err == nullptr)
102958283f41SJohnathan Mantey             {
103058283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
103158283f41SJohnathan Mantey                 return;
103258283f41SJohnathan Mantey             }
103358283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
103458283f41SJohnathan Mantey             {
103558283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
103658283f41SJohnathan Mantey                 return;
103758283f41SJohnathan Mantey             }
1038d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1039d577665bSRatan Gupta             return;
1040d577665bSRatan Gupta         }
1041d577665bSRatan Gupta         },
1042d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1043d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1044d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1045d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1046168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1047d577665bSRatan Gupta }
1048286b9118SJohnathan Mantey 
10494f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
10504f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
10514f48d5f6SEd Tanous                            const bool v6Value,
10528d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1053da131a9aSJennifer Lee {
10542c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1055da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10565e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1057da131a9aSJennifer Lee         if (ec)
1058da131a9aSJennifer Lee         {
1059da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1060da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1061da131a9aSJennifer Lee             return;
1062da131a9aSJennifer Lee         }
10638f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1064da131a9aSJennifer Lee         },
1065da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1066da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1067da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1068da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1069168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1070da131a9aSJennifer Lee }
10711f8c7b5dSJohnathan Mantey 
10724f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1073eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
10748d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1075eeedda23SJohnathan Mantey {
1076eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
10775e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1078eeedda23SJohnathan Mantey         if (ec)
1079eeedda23SJohnathan Mantey         {
1080eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1081eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1082eeedda23SJohnathan Mantey             return;
1083eeedda23SJohnathan Mantey         }
1084eeedda23SJohnathan Mantey         },
1085eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1086eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1087eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1088eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1089168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1090eeedda23SJohnathan Mantey }
1091eeedda23SJohnathan Mantey 
10924f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
10938d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1094da131a9aSJennifer Lee {
1095da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1096da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10975e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1098da131a9aSJennifer Lee         if (ec)
1099da131a9aSJennifer Lee         {
1100da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1101da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1102da131a9aSJennifer Lee             return;
1103da131a9aSJennifer Lee         }
1104da131a9aSJennifer Lee         },
11051e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1106da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1107da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1108168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1109da131a9aSJennifer Lee }
1110d577665bSRatan Gupta 
11114f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
11121f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1113f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1114f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
11158d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1116da131a9aSJennifer Lee {
111782695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
111882695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1119da131a9aSJennifer Lee 
11201f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11211f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11221f8c7b5dSJohnathan Mantey 
11231f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11241f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1125da131a9aSJennifer Lee     {
11261f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11271f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11281f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11291f8c7b5dSJohnathan Mantey         {
1130bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1131bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11321f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1133da131a9aSJennifer Lee             return;
1134da131a9aSJennifer Lee         }
11351f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11361f8c7b5dSJohnathan Mantey     }
11371f8c7b5dSJohnathan Mantey     else
1138da131a9aSJennifer Lee     {
11391f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
11401f8c7b5dSJohnathan Mantey     }
11411f8c7b5dSJohnathan Mantey 
11421f8c7b5dSJohnathan Mantey     bool nextDNS{};
114382695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
11441f8c7b5dSJohnathan Mantey     {
114582695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
11461f8c7b5dSJohnathan Mantey         {
11471f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11481f8c7b5dSJohnathan Mantey             return;
11491f8c7b5dSJohnathan Mantey         }
115082695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11511f8c7b5dSJohnathan Mantey     }
115282695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
11531f8c7b5dSJohnathan Mantey     {
115482695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11551f8c7b5dSJohnathan Mantey     }
115682695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
11571f8c7b5dSJohnathan Mantey     {
115882695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
11591f8c7b5dSJohnathan Mantey     }
11601f8c7b5dSJohnathan Mantey     else
11611f8c7b5dSJohnathan Mantey     {
116282695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
11631f8c7b5dSJohnathan Mantey     }
11641f8c7b5dSJohnathan Mantey 
11651f8c7b5dSJohnathan Mantey     bool nextNTP{};
116682695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
11671f8c7b5dSJohnathan Mantey     {
116882695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
11691f8c7b5dSJohnathan Mantey         {
11701f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11711f8c7b5dSJohnathan Mantey             return;
11721f8c7b5dSJohnathan Mantey         }
117382695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11741f8c7b5dSJohnathan Mantey     }
117582695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
11761f8c7b5dSJohnathan Mantey     {
117782695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11781f8c7b5dSJohnathan Mantey     }
117982695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
11801f8c7b5dSJohnathan Mantey     {
118182695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
11821f8c7b5dSJohnathan Mantey     }
11831f8c7b5dSJohnathan Mantey     else
11841f8c7b5dSJohnathan Mantey     {
118582695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
11861f8c7b5dSJohnathan Mantey     }
11871f8c7b5dSJohnathan Mantey 
11881f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
118982695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
11901f8c7b5dSJohnathan Mantey     {
119182695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
11921f8c7b5dSJohnathan Mantey         {
11931f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11941f8c7b5dSJohnathan Mantey             return;
11951f8c7b5dSJohnathan Mantey         }
119682695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11971f8c7b5dSJohnathan Mantey     }
119882695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
11991f8c7b5dSJohnathan Mantey     {
120082695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12011f8c7b5dSJohnathan Mantey     }
120282695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12031f8c7b5dSJohnathan Mantey     {
120482695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12051f8c7b5dSJohnathan Mantey     }
12061f8c7b5dSJohnathan Mantey     else
12071f8c7b5dSJohnathan Mantey     {
120882695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
12091f8c7b5dSJohnathan Mantey     }
12101f8c7b5dSJohnathan Mantey 
1211da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
12121f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
12131f8c7b5dSJohnathan Mantey                    asyncResp);
1214da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
12151f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1216da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
12171f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
12181f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12191f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1220da131a9aSJennifer Lee }
122101784826SJohnathan Mantey 
12224f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
12232c70f800SEd Tanous     getNextStaticIpEntry(
1224bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1225bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
122601784826SJohnathan Mantey {
122717a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
122817a897dfSManojkiran Eda         return value.origin == "Static";
122917a897dfSManojkiran Eda     });
123001784826SJohnathan Mantey }
123101784826SJohnathan Mantey 
12324f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
12332c70f800SEd Tanous     getNextStaticIpEntry(
1234bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1235bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
123601784826SJohnathan Mantey {
123717a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
123817a897dfSManojkiran Eda         return value.origin == "Static";
123917a897dfSManojkiran Eda     });
124001784826SJohnathan Mantey }
124101784826SJohnathan Mantey 
12424f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1243f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
124401784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
12458d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12461abe55efSEd Tanous {
124701784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1248f476acbfSRatan Gupta     {
124971f52d96SEd Tanous         messages::propertyValueTypeError(
125071f52d96SEd Tanous             asyncResp->res,
1251bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1252d1d50814SRavi Teja             "IPv4StaticAddresses");
1253f476acbfSRatan Gupta         return;
1254f476acbfSRatan Gupta     }
1255f476acbfSRatan Gupta 
1256271584abSEd Tanous     unsigned entryIdx = 1;
125701784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
125801784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
125901784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
126001784826SJohnathan Mantey     // into the NIC.
126185ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
12622c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
126301784826SJohnathan Mantey 
1264537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12651abe55efSEd Tanous     {
1266*89492a15SPatrick Williams         std::string pathString = "IPv4StaticAddresses/" +
1267*89492a15SPatrick Williams                                  std::to_string(entryIdx);
1268179db1d7SKowalski, Kamil 
126901784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1270f476acbfSRatan Gupta         {
1271537174c4SEd Tanous             std::optional<std::string> address;
1272537174c4SEd Tanous             std::optional<std::string> subnetMask;
1273537174c4SEd Tanous             std::optional<std::string> gateway;
1274537174c4SEd Tanous 
1275537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12767e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12777e27d832SJohnathan Mantey                                      "Gateway", gateway))
1278537174c4SEd Tanous             {
127901784826SJohnathan Mantey                 messages::propertyValueFormatError(
128071f52d96SEd Tanous                     asyncResp->res,
128171f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
128271f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
128371f52d96SEd Tanous                     pathString);
1284537174c4SEd Tanous                 return;
1285179db1d7SKowalski, Kamil             }
1286179db1d7SKowalski, Kamil 
128701784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
128801784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
128901784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
129001784826SJohnathan Mantey             // current request.
1291271584abSEd Tanous             const std::string* addr = nullptr;
1292271584abSEd Tanous             const std::string* gw = nullptr;
129301784826SJohnathan Mantey             uint8_t prefixLength = 0;
129401784826SJohnathan Mantey             bool errorInEntry = false;
1295537174c4SEd Tanous             if (address)
12961abe55efSEd Tanous             {
1297033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
12981abe55efSEd Tanous                 {
129901784826SJohnathan Mantey                     addr = &(*address);
13004a0cb85cSEd Tanous                 }
130101784826SJohnathan Mantey                 else
130201784826SJohnathan Mantey                 {
1303bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1304bf648f77SEd Tanous                                                        pathString + "/Address");
130501784826SJohnathan Mantey                     errorInEntry = true;
130601784826SJohnathan Mantey                 }
130701784826SJohnathan Mantey             }
130885ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
130901784826SJohnathan Mantey             {
131085ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
131101784826SJohnathan Mantey             }
131201784826SJohnathan Mantey             else
131301784826SJohnathan Mantey             {
131401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
131501784826SJohnathan Mantey                                           pathString + "/Address");
131601784826SJohnathan Mantey                 errorInEntry = true;
13174a0cb85cSEd Tanous             }
13184a0cb85cSEd Tanous 
1319537174c4SEd Tanous             if (subnetMask)
13204a0cb85cSEd Tanous             {
1321033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1322033f1e4dSEd Tanous                                                          &prefixLength))
13234a0cb85cSEd Tanous                 {
1324f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1325537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13264a0cb85cSEd Tanous                         pathString + "/SubnetMask");
132701784826SJohnathan Mantey                     errorInEntry = true;
13284a0cb85cSEd Tanous                 }
13294a0cb85cSEd Tanous             }
133085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13314a0cb85cSEd Tanous             {
1332033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
133301784826SJohnathan Mantey                                                          &prefixLength))
13344a0cb85cSEd Tanous                 {
133501784826SJohnathan Mantey                     messages::propertyValueFormatError(
133685ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
133701784826SJohnathan Mantey                         pathString + "/SubnetMask");
133801784826SJohnathan Mantey                     errorInEntry = true;
13394a0cb85cSEd Tanous                 }
13404a0cb85cSEd Tanous             }
13411abe55efSEd Tanous             else
13421abe55efSEd Tanous             {
134301784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
134401784826SJohnathan Mantey                                           pathString + "/SubnetMask");
134501784826SJohnathan Mantey                 errorInEntry = true;
134601784826SJohnathan Mantey             }
134701784826SJohnathan Mantey 
134801784826SJohnathan Mantey             if (gateway)
134901784826SJohnathan Mantey             {
1350033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
135101784826SJohnathan Mantey                 {
135201784826SJohnathan Mantey                     gw = &(*gateway);
135301784826SJohnathan Mantey                 }
135401784826SJohnathan Mantey                 else
135501784826SJohnathan Mantey                 {
1356bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1357bf648f77SEd Tanous                                                        pathString + "/Gateway");
135801784826SJohnathan Mantey                     errorInEntry = true;
135901784826SJohnathan Mantey                 }
136001784826SJohnathan Mantey             }
136185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
136201784826SJohnathan Mantey             {
136385ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
136401784826SJohnathan Mantey             }
136501784826SJohnathan Mantey             else
13661abe55efSEd Tanous             {
1367a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13684a0cb85cSEd Tanous                                           pathString + "/Gateway");
136901784826SJohnathan Mantey                 errorInEntry = true;
13704a0cb85cSEd Tanous             }
13714a0cb85cSEd Tanous 
137201784826SJohnathan Mantey             if (errorInEntry)
13731abe55efSEd Tanous             {
137401784826SJohnathan Mantey                 return;
13754a0cb85cSEd Tanous             }
13764a0cb85cSEd Tanous 
137785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13781abe55efSEd Tanous             {
13799c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
13809c5e585cSRavi Teja                                          nicIpEntry->id, prefixLength, *gw,
1381bf648f77SEd Tanous                                          *addr, asyncResp);
1382*89492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1383*89492a15SPatrick Williams                                                   ipv4Data.cend());
1384588c3f0dSKowalski, Kamil             }
138501784826SJohnathan Mantey             else
138601784826SJohnathan Mantey             {
1387cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1388cb13a392SEd Tanous                            asyncResp);
13894a0cb85cSEd Tanous             }
13904a0cb85cSEd Tanous             entryIdx++;
13914a0cb85cSEd Tanous         }
139201784826SJohnathan Mantey         else
139301784826SJohnathan Mantey         {
139485ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
139501784826SJohnathan Mantey             {
139601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
139701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
139801784826SJohnathan Mantey                 // in error, so bail out.
139901784826SJohnathan Mantey                 if (thisJson.is_null())
140001784826SJohnathan Mantey                 {
140101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
140201784826SJohnathan Mantey                     return;
140301784826SJohnathan Mantey                 }
140401784826SJohnathan Mantey                 messages::propertyValueFormatError(
140571f52d96SEd Tanous                     asyncResp->res,
140671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
140771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
140871f52d96SEd Tanous                     pathString);
140901784826SJohnathan Mantey                 return;
141001784826SJohnathan Mantey             }
141101784826SJohnathan Mantey 
141201784826SJohnathan Mantey             if (thisJson.is_null())
141301784826SJohnathan Mantey             {
14149c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
141501784826SJohnathan Mantey             }
141685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
141701784826SJohnathan Mantey             {
1418*89492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1419*89492a15SPatrick Williams                                                   ipv4Data.cend());
142001784826SJohnathan Mantey             }
142101784826SJohnathan Mantey             entryIdx++;
142201784826SJohnathan Mantey         }
142301784826SJohnathan Mantey     }
14244a0cb85cSEd Tanous }
14254a0cb85cSEd Tanous 
14264f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1427f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1428f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14298d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1430f85837bfSRAJESWARAN THILLAIGOVINDAN {
1431f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
14325e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1433f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1434f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1435f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1436f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1437f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1438f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1439f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1440f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1441f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1442bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1443168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1444f85837bfSRAJESWARAN THILLAIGOVINDAN }
1445f85837bfSRAJESWARAN THILLAIGOVINDAN 
14464f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1447f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
144801784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
14498d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1450e48c0fc5SRavi Teja {
145101784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1452e48c0fc5SRavi Teja     {
145371f52d96SEd Tanous         messages::propertyValueTypeError(
145471f52d96SEd Tanous             asyncResp->res,
1455bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1456e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1457e48c0fc5SRavi Teja         return;
1458e48c0fc5SRavi Teja     }
1459271584abSEd Tanous     size_t entryIdx = 1;
146085ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
14612c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1462f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1463e48c0fc5SRavi Teja     {
1464*89492a15SPatrick Williams         std::string pathString = "IPv6StaticAddresses/" +
1465*89492a15SPatrick Williams                                  std::to_string(entryIdx);
1466e48c0fc5SRavi Teja 
146701784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1468e48c0fc5SRavi Teja         {
1469e48c0fc5SRavi Teja             std::optional<std::string> address;
1470e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1471f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1472bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1473bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1474e48c0fc5SRavi Teja             {
147501784826SJohnathan Mantey                 messages::propertyValueFormatError(
147671f52d96SEd Tanous                     asyncResp->res,
147771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
147871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
147971f52d96SEd Tanous                     pathString);
1480e48c0fc5SRavi Teja                 return;
1481e48c0fc5SRavi Teja             }
1482e48c0fc5SRavi Teja 
1483543f4400SEd Tanous             const std::string* addr = nullptr;
1484543f4400SEd Tanous             uint8_t prefix = 0;
148501784826SJohnathan Mantey 
148601784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
148701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
148801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
148901784826SJohnathan Mantey             // current request.
1490e48c0fc5SRavi Teja             if (address)
1491e48c0fc5SRavi Teja             {
149201784826SJohnathan Mantey                 addr = &(*address);
1493e48c0fc5SRavi Teja             }
149485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
149501784826SJohnathan Mantey             {
149685ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
149701784826SJohnathan Mantey             }
149801784826SJohnathan Mantey             else
149901784826SJohnathan Mantey             {
150001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
150101784826SJohnathan Mantey                                           pathString + "/Address");
150201784826SJohnathan Mantey                 return;
1503e48c0fc5SRavi Teja             }
1504e48c0fc5SRavi Teja 
1505e48c0fc5SRavi Teja             if (prefixLength)
1506e48c0fc5SRavi Teja             {
150701784826SJohnathan Mantey                 prefix = *prefixLength;
150801784826SJohnathan Mantey             }
150985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1510e48c0fc5SRavi Teja             {
151185ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1512e48c0fc5SRavi Teja             }
1513e48c0fc5SRavi Teja             else
1514e48c0fc5SRavi Teja             {
1515e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1516e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
151701784826SJohnathan Mantey                 return;
1518e48c0fc5SRavi Teja             }
1519e48c0fc5SRavi Teja 
152085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1521e48c0fc5SRavi Teja             {
15229c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
15239c5e585cSRavi Teja                                          nicIpEntry->id, prefix, "", *addr,
1524e48c0fc5SRavi Teja                                          asyncResp);
1525*89492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1526*89492a15SPatrick Williams                                                   ipv6Data.cend());
152701784826SJohnathan Mantey             }
152801784826SJohnathan Mantey             else
152901784826SJohnathan Mantey             {
153001784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1531e48c0fc5SRavi Teja             }
1532e48c0fc5SRavi Teja             entryIdx++;
1533e48c0fc5SRavi Teja         }
153401784826SJohnathan Mantey         else
153501784826SJohnathan Mantey         {
153685ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
153701784826SJohnathan Mantey             {
153801784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
153901784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
154001784826SJohnathan Mantey                 // in error, so bail out.
154101784826SJohnathan Mantey                 if (thisJson.is_null())
154201784826SJohnathan Mantey                 {
154301784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
154401784826SJohnathan Mantey                     return;
154501784826SJohnathan Mantey                 }
154601784826SJohnathan Mantey                 messages::propertyValueFormatError(
154771f52d96SEd Tanous                     asyncResp->res,
154871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
154971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
155071f52d96SEd Tanous                     pathString);
155101784826SJohnathan Mantey                 return;
155201784826SJohnathan Mantey             }
155301784826SJohnathan Mantey 
155401784826SJohnathan Mantey             if (thisJson.is_null())
155501784826SJohnathan Mantey             {
15569c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
155701784826SJohnathan Mantey             }
155885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
155901784826SJohnathan Mantey             {
1560*89492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
1561*89492a15SPatrick Williams                                                   ipv6Data.cend());
156201784826SJohnathan Mantey             }
156301784826SJohnathan Mantey             entryIdx++;
156401784826SJohnathan Mantey         }
156501784826SJohnathan Mantey     }
1566e48c0fc5SRavi Teja }
1567e48c0fc5SRavi Teja 
15684f48d5f6SEd Tanous inline void parseInterfaceData(
15698d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
15708d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1571e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
157201784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
15734a0cb85cSEd Tanous {
15747a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1575eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1576eeedda23SJohnathan Mantey 
15772c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
157881ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1579eddfc437SWilly Tu     jsonResponse["@odata.id"] = crow::utility::urlFromPieces(
1580eddfc437SWilly Tu         "redfish", "v1", "Managers", "bmc", "EthernetInterfaces", ifaceId);
15812c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1582eeedda23SJohnathan Mantey 
1583eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1584eeedda23SJohnathan Mantey 
15857a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
15867a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
15877a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1588b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1589eeedda23SJohnathan Mantey         if (ec)
1590029573d4SEd Tanous         {
1591eeedda23SJohnathan Mantey             return;
1592eeedda23SJohnathan Mantey         }
1593eeedda23SJohnathan Mantey 
1594914e2d5dSEd Tanous         health->inventory = resp;
15957a1dbc48SGeorge Liu         });
1596eeedda23SJohnathan Mantey 
1597eeedda23SJohnathan Mantey     health->populate();
1598eeedda23SJohnathan Mantey 
1599eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1600eeedda23SJohnathan Mantey     {
16010ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
16022c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1603029573d4SEd Tanous     }
1604029573d4SEd Tanous     else
1605029573d4SEd Tanous     {
16062c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
16072c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1608029573d4SEd Tanous     }
1609aa05fb27SJohnathan Mantey 
16102c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
161135fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
161282695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
16132c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
161482695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
161582695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
161682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
161782695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16181f8c7b5dSJohnathan Mantey 
16192c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
162082695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16211f8c7b5dSJohnathan Mantey                                                                : "Disabled";
162282695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
162382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
162482695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16252a133282Smanojkiraneda 
162682695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16274a0cb85cSEd Tanous     {
162882695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1629ab6554f1SJoshi-Mansi 
1630ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1631ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1632ab6554f1SJoshi-Mansi         // FQDN
163382695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1634d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1635d24bfc7aSJennifer Lee         {
16362c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1637d24bfc7aSJennifer Lee         }
16382c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16394a0cb85cSEd Tanous     }
16404a0cb85cSEd Tanous 
1641613dabeaSEd Tanous     jsonResponse["VLANs"]["@odata.id"] =
1642613dabeaSEd Tanous         crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
1643613dabeaSEd Tanous                                      "EthernetInterfaces", ifaceId, "VLANs");
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 
16701476687dSEd Tanous         ipv4Array.push_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 
16951476687dSEd Tanous         ipv6Array.push_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;
17011476687dSEd Tanous             ipv6StaticArray.push_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;
1753eddfc437SWilly Tu                     iface["@odata.id"] = crow::utility::urlFromPieces(
1754eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
1755eddfc437SWilly Tu                         "EthernetInterfaces", ifaceItem);
17561476687dSEd Tanous                     ifaceArray.push_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;
198622872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["@odata.id"] =
1987eddfc437SWilly Tu                     crow::utility::urlFromPieces(
1988eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
1989eddfc437SWilly Tu                         "EthernetInterfaces", parentIfaceId, "VLANs", ifaceId);
199022872ff3SJiaqing Zhao 
199123a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
199222872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
19931abe55efSEd Tanous             }
19941abe55efSEd Tanous             else
19951abe55efSEd Tanous             {
1996e439f0f8SKowalski, Kamil                 // ... otherwise return error
1997bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1998bf648f77SEd Tanous                 // existing object, and other errors
1999bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2000d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2001e439f0f8SKowalski, Kamil             }
2002e439f0f8SKowalski, Kamil             });
2003bf648f77SEd Tanous         });
2004e439f0f8SKowalski, Kamil 
2005bf648f77SEd Tanous     BMCWEB_ROUTE(
2006bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20073d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2008bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
200945ca1b86SEd Tanous             [&app](const crow::Request& req,
2010bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
201145ca1b86SEd Tanous                    const std::string& parentIfaceId,
201245ca1b86SEd Tanous                    const std::string& ifaceId) {
20133ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
201445ca1b86SEd Tanous         {
201545ca1b86SEd Tanous             return;
201645ca1b86SEd Tanous         }
2017fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
20181abe55efSEd Tanous         {
2019d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2020002d39b4SEd Tanous                                        ifaceId);
2021927a505aSKowalski, Kamil             return;
2022927a505aSKowalski, Kamil         }
2023927a505aSKowalski, Kamil 
20243927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20253927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20260627a2c7SEd Tanous 
202715ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2028bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20291abe55efSEd Tanous         {
2030927a505aSKowalski, Kamil             return;
2031927a505aSKowalski, Kamil         }
2032927a505aSKowalski, Kamil 
20333927e13eSJiaqing Zhao         if (vlanId)
20343927e13eSJiaqing Zhao         {
20353927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20363927e13eSJiaqing Zhao             return;
20373927e13eSJiaqing Zhao         }
20383927e13eSJiaqing Zhao 
2039bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2040bf648f77SEd Tanous         // for JSON preparation
2041e48c0fc5SRavi Teja         getEthernetIfaceData(
2042bf648f77SEd Tanous             ifaceId,
20433927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2044002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2045cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2046cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
204717e22024SJiaqing Zhao             if (success && ethData.vlanId)
204808244d02SSunitha Harish             {
204923a06317SJiaqing Zhao                 if (vlanEnable)
205023a06317SJiaqing Zhao                 {
205123a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
20525e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
205308244d02SSunitha Harish                         if (ec)
205408244d02SSunitha Harish                         {
205508244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20563927e13eSJiaqing Zhao                             return;
205708244d02SSunitha Harish                         }
205823a06317SJiaqing Zhao                         },
205923a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
206023a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
206123a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
206223a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
206323a06317SJiaqing Zhao                         "NICEnabled",
206423a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
206508244d02SSunitha Harish                 }
206608244d02SSunitha Harish             }
206708244d02SSunitha Harish             else
20681abe55efSEd Tanous             {
2069bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2070bf648f77SEd Tanous                 // existing object, and other errors
2071bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2072d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2073bf648f77SEd Tanous                 return;
2074bf648f77SEd Tanous             }
2075bf648f77SEd Tanous             });
2076bf648f77SEd Tanous         });
2077bf648f77SEd Tanous 
2078bf648f77SEd Tanous     BMCWEB_ROUTE(
2079bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20803d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2081bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
208245ca1b86SEd Tanous             [&app](const crow::Request& req,
2083bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
208445ca1b86SEd Tanous                    const std::string& parentIfaceId,
208545ca1b86SEd Tanous                    const std::string& ifaceId) {
20863ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
208745ca1b86SEd Tanous         {
208845ca1b86SEd Tanous             return;
208945ca1b86SEd Tanous         }
2090bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2091bf648f77SEd Tanous         {
2092d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2093002d39b4SEd Tanous                                        ifaceId);
2094927a505aSKowalski, Kamil             return;
2095927a505aSKowalski, Kamil         }
2096e439f0f8SKowalski, Kamil 
2097bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2098bf648f77SEd Tanous         // for JSON preparation
2099f12894f8SJason M. Bills         getEthernetIfaceData(
2100bf648f77SEd Tanous             ifaceId,
2101002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2102002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2103cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2104cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
210517e22024SJiaqing Zhao             if (success && ethData.vlanId)
21061abe55efSEd Tanous             {
2107f12894f8SJason M. Bills                 auto callback =
21085e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
21091abe55efSEd Tanous                     if (ec)
21101abe55efSEd Tanous                     {
2111f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2112927a505aSKowalski, Kamil                     }
21134a0cb85cSEd Tanous                 };
21144a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2115002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2116002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
21174a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
21181abe55efSEd Tanous             }
21191abe55efSEd Tanous             else
21201abe55efSEd Tanous             {
2121927a505aSKowalski, Kamil                 // ... otherwise return error
2122bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2123bf648f77SEd Tanous                 // existing object, and other errors
2124bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2125d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2126927a505aSKowalski, Kamil             }
2127927a505aSKowalski, Kamil             });
2128bf648f77SEd Tanous         });
2129e439f0f8SKowalski, Kamil 
2130bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2131bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2132ed398213SEd Tanous 
2133ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21341476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21351476687dSEd Tanous             [&app](const crow::Request& req,
2136bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2137bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21383ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
213945ca1b86SEd Tanous         {
214045ca1b86SEd Tanous             return;
214145ca1b86SEd Tanous         }
21424a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21431abe55efSEd Tanous         // preparation
2144002d39b4SEd Tanous         getEthernetIfaceList(
2145002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
21461abe55efSEd Tanous                 const bool& success,
2147002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
21484a0cb85cSEd Tanous             if (!success)
21491abe55efSEd Tanous             {
2150f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21514a0cb85cSEd Tanous                 return;
21521abe55efSEd Tanous             }
21534c9afe43SEd Tanous 
215481ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
21554c9afe43SEd Tanous             {
2156002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2157002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21584c9afe43SEd Tanous                                            rootInterfaceName);
21594c9afe43SEd Tanous                 return;
21604c9afe43SEd Tanous             }
21614c9afe43SEd Tanous 
21620f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21630f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21640f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21650f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21660f74e643SEd Tanous                 "VLAN Network Interface Collection";
21674a0cb85cSEd Tanous 
21682c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21694a0cb85cSEd Tanous 
217081ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21711abe55efSEd Tanous             {
217211ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21734a0cb85cSEd Tanous                 {
21741476687dSEd Tanous                     nlohmann::json::object_t iface;
2175eddfc437SWilly Tu                     iface["@odata.id"] = crow::utility::urlFromPieces(
2176eddfc437SWilly Tu                         "redfish", "v1", "Managers", "bmc",
2177eddfc437SWilly Tu                         "EthernetInterfaces", rootInterfaceName, "VLANs",
2178eddfc437SWilly Tu                         ifaceItem);
21791476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2180e439f0f8SKowalski, Kamil                 }
2181e439f0f8SKowalski, Kamil             }
2182e439f0f8SKowalski, Kamil 
2183002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
21842c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
21854a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
2186eddfc437SWilly Tu                 crow::utility::urlFromPieces("redfish", "v1", "Managers", "bmc",
2187eddfc437SWilly Tu                                              "EthernetInterfaces",
2188eddfc437SWilly Tu                                              rootInterfaceName, "VLANs");
2189e439f0f8SKowalski, Kamil         });
2190bf648f77SEd Tanous         });
2191e439f0f8SKowalski, Kamil 
2192bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2193bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
21943d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2195bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
219645ca1b86SEd Tanous             [&app](const crow::Request& req,
2197bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2198bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21993ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
220045ca1b86SEd Tanous         {
220145ca1b86SEd Tanous             return;
220245ca1b86SEd Tanous         }
2203fda13ad2SSunitha Harish         bool vlanEnable = false;
22040627a2c7SEd Tanous         uint32_t vlanId = 0;
2205002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2206002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
22071abe55efSEd Tanous         {
22084a0cb85cSEd Tanous             return;
2209e439f0f8SKowalski, Kamil         }
2210fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2211dbb59d4dSEd Tanous         if (vlanId == 0U)
2212fda13ad2SSunitha Harish         {
2213fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2214fda13ad2SSunitha Harish         }
2215fda13ad2SSunitha Harish         if (!vlanEnable)
2216fda13ad2SSunitha Harish         {
2217fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2218fda13ad2SSunitha Harish         }
2219271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2220fda13ad2SSunitha Harish         {
2221fda13ad2SSunitha Harish             return;
2222fda13ad2SSunitha Harish         }
2223fda13ad2SSunitha Harish 
22245e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
22251abe55efSEd Tanous             if (ec)
22261abe55efSEd Tanous             {
2227bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2228bf648f77SEd Tanous                 // based on phosphor-network responses
2229f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22304a0cb85cSEd Tanous                 return;
22311abe55efSEd Tanous             }
2232f12894f8SJason M. Bills             messages::created(asyncResp->res);
2233e439f0f8SKowalski, Kamil         };
22344a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22354a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22364a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22374a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22380627a2c7SEd Tanous             rootInterfaceName, vlanId);
2239bf648f77SEd Tanous         });
22404a0cb85cSEd Tanous }
2241bf648f77SEd Tanous 
22429391bb9cSRapkiewicz, Pawel } // namespace redfish
2243