xref: /openbmc/bmcweb/redfish-core/lib/ethernet.hpp (revision ddd70dcaa56bd51eaa0b192cd2c5c7bdcc1c3576)
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>
30ef4c65b7SEd Tanous #include <boost/url/format.hpp>
311214b7e7SGunnar Mills 
327a1dbc48SGeorge Liu #include <array>
33a24526dcSEd Tanous #include <optional>
34ab6554f1SJoshi-Mansi #include <regex>
357a1dbc48SGeorge Liu #include <string_view>
3677179532SEd Tanous #include <vector>
379391bb9cSRapkiewicz, Pawel 
381abe55efSEd Tanous namespace redfish
391abe55efSEd Tanous {
409391bb9cSRapkiewicz, Pawel 
414a0cb85cSEd Tanous enum class LinkType
424a0cb85cSEd Tanous {
434a0cb85cSEd Tanous     Local,
444a0cb85cSEd Tanous     Global
454a0cb85cSEd Tanous };
469391bb9cSRapkiewicz, Pawel 
479391bb9cSRapkiewicz, Pawel /**
489391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
499391bb9cSRapkiewicz, Pawel  */
501abe55efSEd Tanous struct IPv4AddressData
511abe55efSEd Tanous {
52179db1d7SKowalski, Kamil     std::string id;
534a0cb85cSEd Tanous     std::string address;
544a0cb85cSEd Tanous     std::string domain;
554a0cb85cSEd Tanous     std::string gateway;
569391bb9cSRapkiewicz, Pawel     std::string netmask;
579391bb9cSRapkiewicz, Pawel     std::string origin;
5877179532SEd Tanous     LinkType linktype{};
5977179532SEd Tanous     bool isActive{};
609391bb9cSRapkiewicz, Pawel };
619391bb9cSRapkiewicz, Pawel 
629391bb9cSRapkiewicz, Pawel /**
63e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
64e48c0fc5SRavi Teja  */
65e48c0fc5SRavi Teja struct IPv6AddressData
66e48c0fc5SRavi Teja {
67e48c0fc5SRavi Teja     std::string id;
68e48c0fc5SRavi Teja     std::string address;
69e48c0fc5SRavi Teja     std::string origin;
7077179532SEd Tanous     uint8_t prefixLength = 0;
71e48c0fc5SRavi Teja };
72e48c0fc5SRavi Teja /**
739391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
749391bb9cSRapkiewicz, Pawel  * available from DBus
759391bb9cSRapkiewicz, Pawel  */
761abe55efSEd Tanous struct EthernetInterfaceData
771abe55efSEd Tanous {
784a0cb85cSEd Tanous     uint32_t speed;
7935fb5311STejas Patil     size_t mtuSize;
8082695a5bSJiaqing Zhao     bool autoNeg;
8182695a5bSJiaqing Zhao     bool dnsEnabled;
8282695a5bSJiaqing Zhao     bool ntpEnabled;
8382695a5bSJiaqing Zhao     bool hostNameEnabled;
84aa05fb27SJohnathan Mantey     bool linkUp;
85eeedda23SJohnathan Mantey     bool nicEnabled;
8682695a5bSJiaqing Zhao     std::string dhcpEnabled;
871f8c7b5dSJohnathan Mantey     std::string operatingMode;
8882695a5bSJiaqing Zhao     std::string hostName;
8982695a5bSJiaqing Zhao     std::string defaultGateway;
9082695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
9182695a5bSJiaqing Zhao     std::string macAddress;
9217e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
930f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
940f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
95d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
969391bb9cSRapkiewicz, Pawel };
979391bb9cSRapkiewicz, Pawel 
981f8c7b5dSJohnathan Mantey struct DHCPParameters
991f8c7b5dSJohnathan Mantey {
1001f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
10182695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
10282695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
10382695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1041f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1051f8c7b5dSJohnathan Mantey };
1061f8c7b5dSJohnathan Mantey 
1079391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1089391bb9cSRapkiewicz, Pawel // into full dot notation
1091abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1101abe55efSEd Tanous {
1119391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1129391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1139391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1149391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1159391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1169391bb9cSRapkiewicz, Pawel     return netmask;
1179391bb9cSRapkiewicz, Pawel }
1189391bb9cSRapkiewicz, Pawel 
11982695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1201f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1211f8c7b5dSJohnathan Mantey {
1221f8c7b5dSJohnathan Mantey     if (isIPv4)
1231f8c7b5dSJohnathan Mantey     {
1241f8c7b5dSJohnathan Mantey         return (
1251f8c7b5dSJohnathan Mantey             (inputDHCP ==
1261f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1271f8c7b5dSJohnathan Mantey             (inputDHCP ==
1281f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1291f8c7b5dSJohnathan Mantey     }
1301f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1311f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1321f8c7b5dSJohnathan Mantey             (inputDHCP ==
1331f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1341f8c7b5dSJohnathan Mantey }
1351f8c7b5dSJohnathan Mantey 
1362c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1371f8c7b5dSJohnathan Mantey {
1381f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1391f8c7b5dSJohnathan Mantey     {
1401f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1411f8c7b5dSJohnathan Mantey     }
1423174e4dfSEd Tanous     if (isIPv4)
1431f8c7b5dSJohnathan Mantey     {
1441f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1451f8c7b5dSJohnathan Mantey     }
1463174e4dfSEd Tanous     if (isIPv6)
1471f8c7b5dSJohnathan Mantey     {
1481f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1491f8c7b5dSJohnathan Mantey     }
1501f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1511f8c7b5dSJohnathan Mantey }
1521f8c7b5dSJohnathan Mantey 
1534a0cb85cSEd Tanous inline std::string
1544a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1554a0cb85cSEd Tanous                                         bool isIPv4)
1561abe55efSEd Tanous {
1574a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1581abe55efSEd Tanous     {
1594a0cb85cSEd Tanous         return "Static";
1609391bb9cSRapkiewicz, Pawel     }
1614a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1621abe55efSEd Tanous     {
1634a0cb85cSEd Tanous         if (isIPv4)
1641abe55efSEd Tanous         {
1654a0cb85cSEd Tanous             return "IPv4LinkLocal";
1661abe55efSEd Tanous         }
1674a0cb85cSEd Tanous         return "LinkLocal";
1689391bb9cSRapkiewicz, Pawel     }
1694a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1701abe55efSEd Tanous     {
1714a0cb85cSEd Tanous         if (isIPv4)
1724a0cb85cSEd Tanous         {
1734a0cb85cSEd Tanous             return "DHCP";
1744a0cb85cSEd Tanous         }
1754a0cb85cSEd Tanous         return "DHCPv6";
1764a0cb85cSEd Tanous     }
1774a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1784a0cb85cSEd Tanous     {
1794a0cb85cSEd Tanous         return "SLAAC";
1804a0cb85cSEd Tanous     }
1814a0cb85cSEd Tanous     return "";
1824a0cb85cSEd Tanous }
1834a0cb85cSEd Tanous 
18402cad96eSEd Tanous inline bool extractEthernetInterfaceData(
18502cad96eSEd Tanous     const std::string& ethifaceId,
18602cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1874a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1884a0cb85cSEd Tanous {
1894c9afe43SEd Tanous     bool idFound = false;
19002cad96eSEd Tanous     for (const auto& objpath : dbusData)
1914a0cb85cSEd Tanous     {
19202cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
1934a0cb85cSEd Tanous         {
19481ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
195029573d4SEd Tanous             {
1964c9afe43SEd Tanous                 idFound = true;
1974a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
1984a0cb85cSEd Tanous                 {
1994a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2004a0cb85cSEd Tanous                     {
2014a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2024a0cb85cSEd Tanous                         {
2034a0cb85cSEd Tanous                             const std::string* mac =
204abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2054a0cb85cSEd Tanous                             if (mac != nullptr)
2064a0cb85cSEd Tanous                             {
20782695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2084a0cb85cSEd Tanous                             }
2094a0cb85cSEd Tanous                         }
2104a0cb85cSEd Tanous                     }
2114a0cb85cSEd Tanous                 }
2124a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2134a0cb85cSEd Tanous                 {
2144a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2154a0cb85cSEd Tanous                     {
2164a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2174a0cb85cSEd Tanous                         {
2181b6b96c5SEd Tanous                             const uint32_t* id =
219abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2204a0cb85cSEd Tanous                             if (id != nullptr)
2214a0cb85cSEd Tanous                             {
22217e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2234a0cb85cSEd Tanous                             }
2244a0cb85cSEd Tanous                         }
2254a0cb85cSEd Tanous                     }
2264a0cb85cSEd Tanous                 }
2274a0cb85cSEd Tanous                 else if (ifacePair.first ==
2284a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2294a0cb85cSEd Tanous                 {
2304a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2314a0cb85cSEd Tanous                     {
2324a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2334a0cb85cSEd Tanous                         {
2342c70f800SEd Tanous                             const bool* autoNeg =
235abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2362c70f800SEd Tanous                             if (autoNeg != nullptr)
2374a0cb85cSEd Tanous                             {
23882695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2394a0cb85cSEd Tanous                             }
2404a0cb85cSEd Tanous                         }
2414a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2424a0cb85cSEd Tanous                         {
2434a0cb85cSEd Tanous                             const uint32_t* speed =
244abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2454a0cb85cSEd Tanous                             if (speed != nullptr)
2464a0cb85cSEd Tanous                             {
2474a0cb85cSEd Tanous                                 ethData.speed = *speed;
2484a0cb85cSEd Tanous                             }
2494a0cb85cSEd Tanous                         }
25035fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25135fb5311STejas Patil                         {
25235fb5311STejas Patil                             const uint32_t* mtuSize =
25335fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25435fb5311STejas Patil                             if (mtuSize != nullptr)
25535fb5311STejas Patil                             {
25635fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
25735fb5311STejas Patil                             }
25835fb5311STejas Patil                         }
259aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
260aa05fb27SJohnathan Mantey                         {
261aa05fb27SJohnathan Mantey                             const bool* linkUp =
262aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
263aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
264aa05fb27SJohnathan Mantey                             {
265aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
266aa05fb27SJohnathan Mantey                             }
267aa05fb27SJohnathan Mantey                         }
268eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
269eeedda23SJohnathan Mantey                         {
270eeedda23SJohnathan Mantey                             const bool* nicEnabled =
271eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
272eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
273eeedda23SJohnathan Mantey                             {
274eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
275eeedda23SJohnathan Mantey                             }
276eeedda23SJohnathan Mantey                         }
277f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
278029573d4SEd Tanous                         {
279029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2808d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
281029573d4SEd Tanous                                     &propertyPair.second);
282029573d4SEd Tanous                             if (nameservers != nullptr)
283029573d4SEd Tanous                             {
284f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2850f6efdc1Smanojkiran.eda@gmail.com                             }
2860f6efdc1Smanojkiran.eda@gmail.com                         }
2870f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2880f6efdc1Smanojkiran.eda@gmail.com                         {
2890f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2908d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2910f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2920f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2930f6efdc1Smanojkiran.eda@gmail.com                             {
294f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2954a0cb85cSEd Tanous                             }
2964a0cb85cSEd Tanous                         }
2972a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
2982a133282Smanojkiraneda                         {
2992c70f800SEd Tanous                             const std::string* dhcpEnabled =
3001f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3012c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3022a133282Smanojkiraneda                             {
30382695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3042a133282Smanojkiraneda                             }
3052a133282Smanojkiraneda                         }
306d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
307d24bfc7aSJennifer Lee                         {
308d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3098d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
310d24bfc7aSJennifer Lee                                     &propertyPair.second);
311d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
312d24bfc7aSJennifer Lee                             {
313f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
314d24bfc7aSJennifer Lee                             }
315d24bfc7aSJennifer Lee                         }
3169010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3179010ec2eSRavi Teja                         {
3189010ec2eSRavi Teja                             const std::string* defaultGateway =
3199010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3209010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3219010ec2eSRavi Teja                             {
3229010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3239010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3249010ec2eSRavi Teja                                 {
32582695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3269010ec2eSRavi Teja                                 }
3279010ec2eSRavi Teja                                 else
3289010ec2eSRavi Teja                                 {
32982695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3309010ec2eSRavi Teja                                 }
3319010ec2eSRavi Teja                             }
3329010ec2eSRavi Teja                         }
3339010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3349010ec2eSRavi Teja                         {
3359010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3369010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3379010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3389010ec2eSRavi Teja                             {
3399010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3409010ec2eSRavi Teja                                     *defaultGateway6;
3419010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3429010ec2eSRavi Teja                                 {
34382695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3449010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3459010ec2eSRavi Teja                                 }
3469010ec2eSRavi Teja                                 else
3479010ec2eSRavi Teja                                 {
34882695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3499010ec2eSRavi Teja                                         defaultGateway6Str;
3509010ec2eSRavi Teja                                 }
3519010ec2eSRavi Teja                             }
3529010ec2eSRavi Teja                         }
353029573d4SEd Tanous                     }
354029573d4SEd Tanous                 }
355029573d4SEd Tanous             }
3561f8c7b5dSJohnathan Mantey 
3571e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3581f8c7b5dSJohnathan Mantey             {
3591f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3601f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3611f8c7b5dSJohnathan Mantey                 {
3621f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3631f8c7b5dSJohnathan Mantey                     {
3641f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3651f8c7b5dSJohnathan Mantey                         {
3662c70f800SEd Tanous                             const bool* dnsEnabled =
3671f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3682c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3691f8c7b5dSJohnathan Mantey                             {
37082695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3711f8c7b5dSJohnathan Mantey                             }
3721f8c7b5dSJohnathan Mantey                         }
3731f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3741f8c7b5dSJohnathan Mantey                         {
3752c70f800SEd Tanous                             const bool* ntpEnabled =
3761f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3772c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3781f8c7b5dSJohnathan Mantey                             {
37982695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3801f8c7b5dSJohnathan Mantey                             }
3811f8c7b5dSJohnathan Mantey                         }
3821f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3831f8c7b5dSJohnathan Mantey                         {
3842c70f800SEd Tanous                             const bool* hostNameEnabled =
3851f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3862c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3871f8c7b5dSJohnathan Mantey                             {
38882695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3891f8c7b5dSJohnathan Mantey                             }
3901f8c7b5dSJohnathan Mantey                         }
3911f8c7b5dSJohnathan Mantey                     }
3921f8c7b5dSJohnathan Mantey                 }
3931f8c7b5dSJohnathan Mantey             }
394029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
395029573d4SEd Tanous             // to check eth number
396029573d4SEd Tanous             if (ifacePair.first ==
3974a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3984a0cb85cSEd Tanous             {
3994a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4004a0cb85cSEd Tanous                 {
4014a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4024a0cb85cSEd Tanous                     {
4034a0cb85cSEd Tanous                         const std::string* hostname =
4048d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4054a0cb85cSEd Tanous                         if (hostname != nullptr)
4064a0cb85cSEd Tanous                         {
40782695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4084a0cb85cSEd Tanous                         }
4094a0cb85cSEd Tanous                     }
4104a0cb85cSEd Tanous                 }
4114a0cb85cSEd Tanous             }
4124a0cb85cSEd Tanous         }
4134a0cb85cSEd Tanous     }
4144c9afe43SEd Tanous     return idFound;
4154a0cb85cSEd Tanous }
4164a0cb85cSEd Tanous 
417e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
41877179532SEd Tanous inline void extractIPV6Data(const std::string& ethifaceId,
419711ac7a9SEd Tanous                             const dbus::utility::ManagedObjectType& dbusData,
42077179532SEd Tanous                             std::vector<IPv6AddressData>& ipv6Config)
421e48c0fc5SRavi Teja {
42289492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
42389492a15SPatrick Williams                                     ethifaceId;
424e48c0fc5SRavi Teja 
425e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
426e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
42781ce609eSEd Tanous     for (const auto& objpath : dbusData)
428e48c0fc5SRavi Teja     {
429e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
430353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
431e48c0fc5SRavi Teja         {
4329eb808c1SEd Tanous             for (const auto& interface : objpath.second)
433e48c0fc5SRavi Teja             {
434e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
435e48c0fc5SRavi Teja                 {
436353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
437353163e9STony Lee                                              interface.second.end(),
438353163e9STony Lee                                              [](const auto& property) {
439353163e9STony Lee                         return property.first == "Type";
440353163e9STony Lee                     });
441353163e9STony Lee                     if (type == interface.second.end())
442353163e9STony Lee                     {
443353163e9STony Lee                         continue;
444353163e9STony Lee                     }
445353163e9STony Lee 
446353163e9STony Lee                     const std::string* typeStr =
447353163e9STony Lee                         std::get_if<std::string>(&type->second);
448353163e9STony Lee 
449353163e9STony Lee                     if (typeStr == nullptr ||
450353163e9STony Lee                         (*typeStr !=
451353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
452353163e9STony Lee                     {
453353163e9STony Lee                         continue;
454353163e9STony Lee                     }
455353163e9STony Lee 
456e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
457e48c0fc5SRavi Teja                     // appropriate
45877179532SEd Tanous                     IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
4592c70f800SEd Tanous                     ipv6Address.id =
460353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
4619eb808c1SEd Tanous                     for (const auto& property : interface.second)
462e48c0fc5SRavi Teja                     {
463e48c0fc5SRavi Teja                         if (property.first == "Address")
464e48c0fc5SRavi Teja                         {
465e48c0fc5SRavi Teja                             const std::string* address =
466e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
467e48c0fc5SRavi Teja                             if (address != nullptr)
468e48c0fc5SRavi Teja                             {
4692c70f800SEd Tanous                                 ipv6Address.address = *address;
470e48c0fc5SRavi Teja                             }
471e48c0fc5SRavi Teja                         }
472e48c0fc5SRavi Teja                         else if (property.first == "Origin")
473e48c0fc5SRavi Teja                         {
474e48c0fc5SRavi Teja                             const std::string* origin =
475e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
476e48c0fc5SRavi Teja                             if (origin != nullptr)
477e48c0fc5SRavi Teja                             {
4782c70f800SEd Tanous                                 ipv6Address.origin =
479e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
480e48c0fc5SRavi Teja                                                                         false);
481e48c0fc5SRavi Teja                             }
482e48c0fc5SRavi Teja                         }
483e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
484e48c0fc5SRavi Teja                         {
485e48c0fc5SRavi Teja                             const uint8_t* prefix =
486e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
487e48c0fc5SRavi Teja                             if (prefix != nullptr)
488e48c0fc5SRavi Teja                             {
4892c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
490e48c0fc5SRavi Teja                             }
491e48c0fc5SRavi Teja                         }
492889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
493889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
494889ff694SAsmitha Karunanithi                         {
495889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
496889ff694SAsmitha Karunanithi                         }
497e48c0fc5SRavi Teja                         else
498e48c0fc5SRavi Teja                         {
499e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
500e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
501e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
502e48c0fc5SRavi Teja                         }
503e48c0fc5SRavi Teja                     }
504e48c0fc5SRavi Teja                 }
505e48c0fc5SRavi Teja             }
506e48c0fc5SRavi Teja         }
507e48c0fc5SRavi Teja     }
508e48c0fc5SRavi Teja }
509e48c0fc5SRavi Teja 
5104a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
51177179532SEd Tanous inline void extractIPData(const std::string& ethifaceId,
512711ac7a9SEd Tanous                           const dbus::utility::ManagedObjectType& dbusData,
51377179532SEd Tanous                           std::vector<IPv4AddressData>& ipv4Config)
5144a0cb85cSEd Tanous {
51589492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
51689492a15SPatrick Williams                                     ethifaceId;
5174a0cb85cSEd Tanous 
5184a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5194a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
52081ce609eSEd Tanous     for (const auto& objpath : dbusData)
5214a0cb85cSEd Tanous     {
5224a0cb85cSEd Tanous         // Check if proper pattern for object path appears
523353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
5244a0cb85cSEd Tanous         {
5259eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5264a0cb85cSEd Tanous             {
5274a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5284a0cb85cSEd Tanous                 {
529353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
530353163e9STony Lee                                              interface.second.end(),
531353163e9STony Lee                                              [](const auto& property) {
532353163e9STony Lee                         return property.first == "Type";
533353163e9STony Lee                     });
534353163e9STony Lee                     if (type == interface.second.end())
535353163e9STony Lee                     {
536353163e9STony Lee                         continue;
537353163e9STony Lee                     }
538353163e9STony Lee 
539353163e9STony Lee                     const std::string* typeStr =
540353163e9STony Lee                         std::get_if<std::string>(&type->second);
541353163e9STony Lee 
542353163e9STony Lee                     if (typeStr == nullptr ||
543353163e9STony Lee                         (*typeStr !=
544353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
545353163e9STony Lee                     {
546353163e9STony Lee                         continue;
547353163e9STony Lee                     }
548353163e9STony Lee 
5494a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5504a0cb85cSEd Tanous                     // appropriate
55177179532SEd Tanous                     IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
5522c70f800SEd Tanous                     ipv4Address.id =
553353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
5549eb808c1SEd Tanous                     for (const auto& property : interface.second)
5554a0cb85cSEd Tanous                     {
5564a0cb85cSEd Tanous                         if (property.first == "Address")
5574a0cb85cSEd Tanous                         {
5584a0cb85cSEd Tanous                             const std::string* address =
559abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5604a0cb85cSEd Tanous                             if (address != nullptr)
5614a0cb85cSEd Tanous                             {
5622c70f800SEd Tanous                                 ipv4Address.address = *address;
5634a0cb85cSEd Tanous                             }
5644a0cb85cSEd Tanous                         }
5654a0cb85cSEd Tanous                         else if (property.first == "Origin")
5664a0cb85cSEd Tanous                         {
5674a0cb85cSEd Tanous                             const std::string* origin =
568abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5694a0cb85cSEd Tanous                             if (origin != nullptr)
5704a0cb85cSEd Tanous                             {
5712c70f800SEd Tanous                                 ipv4Address.origin =
5724a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5734a0cb85cSEd Tanous                                                                         true);
5744a0cb85cSEd Tanous                             }
5754a0cb85cSEd Tanous                         }
5764a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5774a0cb85cSEd Tanous                         {
5784a0cb85cSEd Tanous                             const uint8_t* mask =
579abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5804a0cb85cSEd Tanous                             if (mask != nullptr)
5814a0cb85cSEd Tanous                             {
5824a0cb85cSEd Tanous                                 // convert it to the string
5832c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5844a0cb85cSEd Tanous                             }
5854a0cb85cSEd Tanous                         }
586889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
587889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
588889ff694SAsmitha Karunanithi                         {
589889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
590889ff694SAsmitha Karunanithi                         }
5914a0cb85cSEd Tanous                         else
5924a0cb85cSEd Tanous                         {
5934a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5944a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5954a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5964a0cb85cSEd Tanous                         }
5974a0cb85cSEd Tanous                     }
5984a0cb85cSEd Tanous                     // Check if given address is local, or global
5992c70f800SEd Tanous                     ipv4Address.linktype =
60011ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
60118659d10SJohnathan Mantey                             ? LinkType::Local
60218659d10SJohnathan Mantey                             : LinkType::Global;
6034a0cb85cSEd Tanous                 }
6044a0cb85cSEd Tanous             }
6054a0cb85cSEd Tanous         }
6064a0cb85cSEd Tanous     }
6074a0cb85cSEd Tanous }
608588c3f0dSKowalski, Kamil 
609588c3f0dSKowalski, Kamil /**
61001784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
611179db1d7SKowalski, Kamil  *
612179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
613179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
614179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
615179db1d7SKowalski, Kamil  *
616179db1d7SKowalski, Kamil  * @return None
617179db1d7SKowalski, Kamil  */
6189c5e585cSRavi Teja inline void deleteIPAddress(const std::string& ifaceId,
6199c5e585cSRavi Teja                             const std::string& ipHash,
6208d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6211abe55efSEd Tanous {
62255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6235e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6241abe55efSEd Tanous         if (ec)
6251abe55efSEd Tanous         {
626a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6271abe55efSEd Tanous         }
628179db1d7SKowalski, Kamil         },
629179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
6309c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + ipHash,
631179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
632179db1d7SKowalski, Kamil }
633179db1d7SKowalski, Kamil 
634244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
635244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
636244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6379010ec2eSRavi Teja {
6389010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6395e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6409010ec2eSRavi Teja         if (ec)
6419010ec2eSRavi Teja         {
6429010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6439010ec2eSRavi Teja             return;
6449010ec2eSRavi Teja         }
6459010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6469010ec2eSRavi Teja         },
6479010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6489010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6499010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6509010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
651168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6529010ec2eSRavi Teja }
653179db1d7SKowalski, Kamil /**
65401784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
655179db1d7SKowalski, Kamil  *
65601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
65701784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
65801784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
65901784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
660179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
661179db1d7SKowalski, Kamil  *
662179db1d7SKowalski, Kamil  * @return None
663179db1d7SKowalski, Kamil  */
664cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
665cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6668d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6671abe55efSEd Tanous {
668002d39b4SEd Tanous     auto createIpHandler =
6695e7e2dc5SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
6701abe55efSEd Tanous         if (ec)
6711abe55efSEd Tanous         {
672a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6739010ec2eSRavi Teja             return;
674179db1d7SKowalski, Kamil         }
6759010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6769010ec2eSRavi Teja     };
6779010ec2eSRavi Teja 
6789010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6799010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
680179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
681179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
68201784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
683179db1d7SKowalski, Kamil         gateway);
684179db1d7SKowalski, Kamil }
685e48c0fc5SRavi Teja 
686e48c0fc5SRavi Teja /**
68701784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
68801784826SJohnathan Mantey  * static IPv6 entry
68901784826SJohnathan Mantey  *
69001784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
69101784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
69201784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
69301784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
69401784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
69501784826SJohnathan Mantey  *
69601784826SJohnathan Mantey  * @return None
69701784826SJohnathan Mantey  */
6989c5e585cSRavi Teja 
6999c5e585cSRavi Teja enum class IpVersion
7009c5e585cSRavi Teja {
7019c5e585cSRavi Teja     IpV4,
7029c5e585cSRavi Teja     IpV6
7039c5e585cSRavi Teja };
7049c5e585cSRavi Teja 
7059c5e585cSRavi Teja inline void deleteAndCreateIPAddress(
7069c5e585cSRavi Teja     IpVersion version, const std::string& ifaceId, const std::string& id,
7078d1b46d7Szhanghch05     uint8_t prefixLength, const std::string& address,
7089c5e585cSRavi Teja     const std::string& gateway,
7098d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
71001784826SJohnathan Mantey {
71101784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
7129c5e585cSRavi Teja         [asyncResp, version, ifaceId, address, prefixLength,
7139c5e585cSRavi Teja          gateway](const boost::system::error_code& ec) {
71401784826SJohnathan Mantey         if (ec)
71501784826SJohnathan Mantey         {
71601784826SJohnathan Mantey             messages::internalError(asyncResp->res);
71701784826SJohnathan Mantey         }
7189c5e585cSRavi Teja         std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
7199c5e585cSRavi Teja         protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
72001784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
7215e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
72223a21a1cSEd Tanous             if (ec2)
72301784826SJohnathan Mantey             {
72401784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
72501784826SJohnathan Mantey             }
72601784826SJohnathan Mantey             },
72701784826SJohnathan Mantey             "xyz.openbmc_project.Network",
72801784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
7299c5e585cSRavi Teja             "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
7309c5e585cSRavi Teja             prefixLength, gateway);
73101784826SJohnathan Mantey         },
73201784826SJohnathan Mantey         "xyz.openbmc_project.Network",
7339c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + id,
73401784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
73501784826SJohnathan Mantey }
73601784826SJohnathan Mantey 
73701784826SJohnathan Mantey /**
738e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
739e48c0fc5SRavi Teja  *
740e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
741e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
742e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
743e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
744e48c0fc5SRavi Teja  *
745e48c0fc5SRavi Teja  * @return None
746e48c0fc5SRavi Teja  */
74701784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
74801784826SJohnathan Mantey                        const std::string& address,
7498d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
750e48c0fc5SRavi Teja {
7515e7e2dc5SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
752e48c0fc5SRavi Teja         if (ec)
753e48c0fc5SRavi Teja         {
754e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
755e48c0fc5SRavi Teja         }
756e48c0fc5SRavi Teja     };
757e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
7584e0453b1SGunnar Mills     // does not have associated gateway property
759e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
760e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
761e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
762e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
763e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
764e48c0fc5SRavi Teja         "");
765e48c0fc5SRavi Teja }
766e48c0fc5SRavi Teja 
767179db1d7SKowalski, Kamil /**
768179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
769179db1d7SKowalski, Kamil  * Object
770179db1d7SKowalski, Kamil  * from EntityManager Network Manager
7714a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
772179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
773179db1d7SKowalski, Kamil  * into JSON
774179db1d7SKowalski, Kamil  */
775179db1d7SKowalski, Kamil template <typename CallbackFunc>
77681ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
7771abe55efSEd Tanous                           CallbackFunc&& callback)
7781abe55efSEd Tanous {
779f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
780f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
781f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
782f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
783f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
7845e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
78502cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
78655c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
78777179532SEd Tanous         std::vector<IPv4AddressData> ipv4Data;
78877179532SEd Tanous         std::vector<IPv6AddressData> ipv6Data;
789179db1d7SKowalski, Kamil 
79081ce609eSEd Tanous         if (errorCode)
7911abe55efSEd Tanous         {
79201784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
793179db1d7SKowalski, Kamil             return;
794179db1d7SKowalski, Kamil         }
795179db1d7SKowalski, Kamil 
796002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
7974c9afe43SEd Tanous         if (!found)
7984c9afe43SEd Tanous         {
79901784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8004c9afe43SEd Tanous             return;
8014c9afe43SEd Tanous         }
8024c9afe43SEd Tanous 
8032c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
804179db1d7SKowalski, Kamil         // Fix global GW
8051abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8061abe55efSEd Tanous         {
807c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
808c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8099010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8101abe55efSEd Tanous             {
81182695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
812179db1d7SKowalski, Kamil             }
813179db1d7SKowalski, Kamil         }
814179db1d7SKowalski, Kamil 
8152c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8164e0453b1SGunnar Mills         // Finally make a callback with useful data
81701784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
818f5892d0dSGeorge Liu         });
819271584abSEd Tanous }
820179db1d7SKowalski, Kamil 
821179db1d7SKowalski, Kamil /**
8229391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
8239391bb9cSRapkiewicz, Pawel  * Manager
8241abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
8251abe55efSEd Tanous  * into JSON.
8269391bb9cSRapkiewicz, Pawel  */
8279391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
8281abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
8291abe55efSEd Tanous {
830f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
831f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
832f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
833f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
8345e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
835f5892d0dSGeorge Liu             const dbus::utility::ManagedObjectType& resp) {
8361abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
8371abe55efSEd Tanous         // ethernet interfaces
83877179532SEd Tanous         std::vector<std::string> ifaceList;
8392c70f800SEd Tanous         ifaceList.reserve(resp.size());
84081ce609eSEd Tanous         if (errorCode)
8411abe55efSEd Tanous         {
8422c70f800SEd Tanous             callback(false, ifaceList);
8439391bb9cSRapkiewicz, Pawel             return;
8449391bb9cSRapkiewicz, Pawel         }
8459391bb9cSRapkiewicz, Pawel 
8469391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
8474a0cb85cSEd Tanous         for (const auto& objpath : resp)
8481abe55efSEd Tanous         {
8499391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
8504a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
8511abe55efSEd Tanous             {
8521abe55efSEd Tanous                 // If interface is
8534a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
8544a0cb85cSEd Tanous                 // what we're looking for.
8559391bb9cSRapkiewicz, Pawel                 if (interface.first ==
8561abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
8571abe55efSEd Tanous                 {
8582dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
8592dfd18efSEd Tanous                     if (ifaceId.empty())
8601abe55efSEd Tanous                     {
8612dfd18efSEd Tanous                         continue;
8629391bb9cSRapkiewicz, Pawel                     }
8632dfd18efSEd Tanous                     // and put it into output vector.
86477179532SEd Tanous                     ifaceList.emplace_back(ifaceId);
8659391bb9cSRapkiewicz, Pawel                 }
8669391bb9cSRapkiewicz, Pawel             }
8679391bb9cSRapkiewicz, Pawel         }
868a434f2bdSEd Tanous         // Finally make a callback with useful data
8692c70f800SEd Tanous         callback(true, ifaceList);
870f5892d0dSGeorge Liu         });
871271584abSEd Tanous }
8729391bb9cSRapkiewicz, Pawel 
8734f48d5f6SEd Tanous inline void
8744f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
8758d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
8761abe55efSEd Tanous {
877ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
878ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
879ab6554f1SJoshi-Mansi     {
880ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
881ab6554f1SJoshi-Mansi                                            "HostName");
882ab6554f1SJoshi-Mansi         return;
883ab6554f1SJoshi-Mansi     }
884bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
8855e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
8864a0cb85cSEd Tanous         if (ec)
8874a0cb85cSEd Tanous         {
888a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
8891abe55efSEd Tanous         }
890bc0bd6e0SEd Tanous         },
891bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
892bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
893bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
894168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
895588c3f0dSKowalski, Kamil }
896588c3f0dSKowalski, Kamil 
8974f48d5f6SEd Tanous inline void
89835fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
89935fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
90035fb5311STejas Patil {
90189492a15SPatrick Williams     sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
90289492a15SPatrick Williams                                               ifaceId;
90335fb5311STejas Patil     crow::connections::systemBus->async_method_call(
9045e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
90535fb5311STejas Patil         if (ec)
90635fb5311STejas Patil         {
90735fb5311STejas Patil             messages::internalError(asyncResp->res);
90835fb5311STejas Patil         }
90935fb5311STejas Patil         },
91035fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
91135fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
91235fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
91335fb5311STejas Patil         std::variant<size_t>(mtuSize));
91435fb5311STejas Patil }
91535fb5311STejas Patil 
91635fb5311STejas Patil inline void
9174f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
918bf648f77SEd Tanous                           const std::string& domainname,
9198d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
920ab6554f1SJoshi-Mansi {
921ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
922ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
9235e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
924ab6554f1SJoshi-Mansi         if (ec)
925ab6554f1SJoshi-Mansi         {
926ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
927ab6554f1SJoshi-Mansi         }
928ab6554f1SJoshi-Mansi         },
929ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
930ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
931ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
932ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
933168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
934ab6554f1SJoshi-Mansi }
935ab6554f1SJoshi-Mansi 
9364f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
937bf648f77SEd Tanous {
938bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
939bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
940bf648f77SEd Tanous     {
941bf648f77SEd Tanous         return false;
942bf648f77SEd Tanous     }
943bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
944bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
945bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
946bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
947bf648f77SEd Tanous     const std::regex pattern(
948bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
949bf648f77SEd Tanous 
950bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
951bf648f77SEd Tanous }
952bf648f77SEd Tanous 
9534f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
954bf648f77SEd Tanous {
955bf648f77SEd Tanous     // Can have multiple subdomains
956bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
9570fda0f12SGeorge Liu     const std::regex pattern(
9580fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
959bf648f77SEd Tanous 
960bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
961bf648f77SEd Tanous }
962bf648f77SEd Tanous 
9634f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
9648d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
965ab6554f1SJoshi-Mansi {
966ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
967ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
968ab6554f1SJoshi-Mansi     {
969ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
970ab6554f1SJoshi-Mansi         return;
971ab6554f1SJoshi-Mansi     }
972ab6554f1SJoshi-Mansi 
973ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
974ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
975ab6554f1SJoshi-Mansi     {
976ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
977ab6554f1SJoshi-Mansi         return;
978ab6554f1SJoshi-Mansi     }
979ab6554f1SJoshi-Mansi 
980ab6554f1SJoshi-Mansi     std::string hostname;
981ab6554f1SJoshi-Mansi     std::string domainname;
982ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
983ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
984ab6554f1SJoshi-Mansi 
985ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
986ab6554f1SJoshi-Mansi     {
987ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
988ab6554f1SJoshi-Mansi         return;
989ab6554f1SJoshi-Mansi     }
990ab6554f1SJoshi-Mansi 
991ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
992ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
993ab6554f1SJoshi-Mansi }
994ab6554f1SJoshi-Mansi 
9954f48d5f6SEd Tanous inline void
9964f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
997bf648f77SEd Tanous                           const std::string& macAddress,
9988d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
999d577665bSRatan Gupta {
100058283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
100158283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
100258283f41SJohnathan Mantey 
1003d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
10045e7e2dc5SEd Tanous         [asyncResp, macAddress](const boost::system::error_code& ec,
10055b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1006d577665bSRatan Gupta         if (ec)
1007d577665bSRatan Gupta         {
100858283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
100958283f41SJohnathan Mantey             if (err == nullptr)
101058283f41SJohnathan Mantey             {
101158283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
101258283f41SJohnathan Mantey                 return;
101358283f41SJohnathan Mantey             }
101458283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
101558283f41SJohnathan Mantey             {
101658283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
101758283f41SJohnathan Mantey                 return;
101858283f41SJohnathan Mantey             }
1019d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1020d577665bSRatan Gupta             return;
1021d577665bSRatan Gupta         }
1022d577665bSRatan Gupta         },
1023d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1024d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1025d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1026d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1027168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1028d577665bSRatan Gupta }
1029286b9118SJohnathan Mantey 
10304f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
10314f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
10324f48d5f6SEd Tanous                            const bool v6Value,
10338d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1034da131a9aSJennifer Lee {
10352c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1036da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10375e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1038da131a9aSJennifer Lee         if (ec)
1039da131a9aSJennifer Lee         {
1040da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1041da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1042da131a9aSJennifer Lee             return;
1043da131a9aSJennifer Lee         }
10448f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1045da131a9aSJennifer Lee         },
1046da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1047da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1048da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1049da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1050168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1051da131a9aSJennifer Lee }
10521f8c7b5dSJohnathan Mantey 
10534f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1054eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
10558d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1056eeedda23SJohnathan Mantey {
1057eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
10585e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1059eeedda23SJohnathan Mantey         if (ec)
1060eeedda23SJohnathan Mantey         {
1061eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1062eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1063eeedda23SJohnathan Mantey             return;
1064eeedda23SJohnathan Mantey         }
1065eeedda23SJohnathan Mantey         },
1066eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1067eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1068eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1069eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1070168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1071eeedda23SJohnathan Mantey }
1072eeedda23SJohnathan Mantey 
10734f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
10748d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1075da131a9aSJennifer Lee {
1076da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1077da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10785e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1079da131a9aSJennifer Lee         if (ec)
1080da131a9aSJennifer Lee         {
1081da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1082da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1083da131a9aSJennifer Lee             return;
1084da131a9aSJennifer Lee         }
1085da131a9aSJennifer Lee         },
10861e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1087da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1088da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1089168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1090da131a9aSJennifer Lee }
1091d577665bSRatan Gupta 
10924f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
10931f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1094f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1095f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
10968d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1097da131a9aSJennifer Lee {
109882695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
109982695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1100da131a9aSJennifer Lee 
11011f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11021f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11031f8c7b5dSJohnathan Mantey 
11041f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11051f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1106da131a9aSJennifer Lee     {
11071f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11081f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11091f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11101f8c7b5dSJohnathan Mantey         {
1111bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1112bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11131f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1114da131a9aSJennifer Lee             return;
1115da131a9aSJennifer Lee         }
11161f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11171f8c7b5dSJohnathan Mantey     }
11181f8c7b5dSJohnathan Mantey     else
1119da131a9aSJennifer Lee     {
11201f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
11211f8c7b5dSJohnathan Mantey     }
11221f8c7b5dSJohnathan Mantey 
11231f8c7b5dSJohnathan Mantey     bool nextDNS{};
112482695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
11251f8c7b5dSJohnathan Mantey     {
112682695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
11271f8c7b5dSJohnathan Mantey         {
11281f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11291f8c7b5dSJohnathan Mantey             return;
11301f8c7b5dSJohnathan Mantey         }
113182695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11321f8c7b5dSJohnathan Mantey     }
113382695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
11341f8c7b5dSJohnathan Mantey     {
113582695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11361f8c7b5dSJohnathan Mantey     }
113782695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
11381f8c7b5dSJohnathan Mantey     {
113982695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
11401f8c7b5dSJohnathan Mantey     }
11411f8c7b5dSJohnathan Mantey     else
11421f8c7b5dSJohnathan Mantey     {
114382695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
11441f8c7b5dSJohnathan Mantey     }
11451f8c7b5dSJohnathan Mantey 
11461f8c7b5dSJohnathan Mantey     bool nextNTP{};
114782695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
11481f8c7b5dSJohnathan Mantey     {
114982695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
11501f8c7b5dSJohnathan Mantey         {
11511f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11521f8c7b5dSJohnathan Mantey             return;
11531f8c7b5dSJohnathan Mantey         }
115482695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11551f8c7b5dSJohnathan Mantey     }
115682695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
11571f8c7b5dSJohnathan Mantey     {
115882695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11591f8c7b5dSJohnathan Mantey     }
116082695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
11611f8c7b5dSJohnathan Mantey     {
116282695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
11631f8c7b5dSJohnathan Mantey     }
11641f8c7b5dSJohnathan Mantey     else
11651f8c7b5dSJohnathan Mantey     {
116682695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
11671f8c7b5dSJohnathan Mantey     }
11681f8c7b5dSJohnathan Mantey 
11691f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
117082695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
11711f8c7b5dSJohnathan Mantey     {
117282695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
11731f8c7b5dSJohnathan Mantey         {
11741f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11751f8c7b5dSJohnathan Mantey             return;
11761f8c7b5dSJohnathan Mantey         }
117782695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11781f8c7b5dSJohnathan Mantey     }
117982695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
11801f8c7b5dSJohnathan Mantey     {
118182695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11821f8c7b5dSJohnathan Mantey     }
118382695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
11841f8c7b5dSJohnathan Mantey     {
118582695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
11861f8c7b5dSJohnathan Mantey     }
11871f8c7b5dSJohnathan Mantey     else
11881f8c7b5dSJohnathan Mantey     {
118982695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
11901f8c7b5dSJohnathan Mantey     }
11911f8c7b5dSJohnathan Mantey 
1192da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
11931f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
11941f8c7b5dSJohnathan Mantey                    asyncResp);
1195da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
11961f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1197da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
11981f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
11991f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12001f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1201da131a9aSJennifer Lee }
120201784826SJohnathan Mantey 
120377179532SEd Tanous inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
120477179532SEd Tanous     const std::vector<IPv4AddressData>::const_iterator& head,
120577179532SEd Tanous     const std::vector<IPv4AddressData>::const_iterator& end)
120601784826SJohnathan Mantey {
120717a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
120817a897dfSManojkiran Eda         return value.origin == "Static";
120917a897dfSManojkiran Eda     });
121001784826SJohnathan Mantey }
121101784826SJohnathan Mantey 
121277179532SEd Tanous inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
121377179532SEd Tanous     const std::vector<IPv6AddressData>::const_iterator& head,
121477179532SEd Tanous     const std::vector<IPv6AddressData>::const_iterator& end)
121501784826SJohnathan Mantey {
121617a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
121717a897dfSManojkiran Eda         return value.origin == "Static";
121817a897dfSManojkiran Eda     });
121901784826SJohnathan Mantey }
122001784826SJohnathan Mantey 
122177179532SEd Tanous inline void
1222*ddd70dcaSEd Tanous     handleIPv4StaticPatch(const std::string& ifaceId,
1223*ddd70dcaSEd Tanous                           nlohmann::json::array_t& input,
122477179532SEd Tanous                           const std::vector<IPv4AddressData>& ipv4Data,
12258d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12261abe55efSEd Tanous {
1227*ddd70dcaSEd Tanous     if (input.empty())
1228f476acbfSRatan Gupta     {
122971f52d96SEd Tanous         messages::propertyValueTypeError(
123071f52d96SEd Tanous             asyncResp->res,
1231*ddd70dcaSEd Tanous             nlohmann::json(input).dump(
1232*ddd70dcaSEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace),
1233d1d50814SRavi Teja             "IPv4StaticAddresses");
1234f476acbfSRatan Gupta         return;
1235f476acbfSRatan Gupta     }
1236f476acbfSRatan Gupta 
1237271584abSEd Tanous     unsigned entryIdx = 1;
123801784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
123901784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
124001784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
124101784826SJohnathan Mantey     // into the NIC.
124277179532SEd Tanous     std::vector<IPv4AddressData>::const_iterator nicIpEntry =
12432c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
124401784826SJohnathan Mantey 
1245537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12461abe55efSEd Tanous     {
124789492a15SPatrick Williams         std::string pathString = "IPv4StaticAddresses/" +
124889492a15SPatrick Williams                                  std::to_string(entryIdx);
1249179db1d7SKowalski, Kamil 
125001784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1251f476acbfSRatan Gupta         {
1252537174c4SEd Tanous             std::optional<std::string> address;
1253537174c4SEd Tanous             std::optional<std::string> subnetMask;
1254537174c4SEd Tanous             std::optional<std::string> gateway;
1255537174c4SEd Tanous 
1256537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12577e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12587e27d832SJohnathan Mantey                                      "Gateway", gateway))
1259537174c4SEd Tanous             {
126001784826SJohnathan Mantey                 messages::propertyValueFormatError(
126171f52d96SEd Tanous                     asyncResp->res,
126271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
126371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
126471f52d96SEd Tanous                     pathString);
1265537174c4SEd Tanous                 return;
1266179db1d7SKowalski, Kamil             }
1267179db1d7SKowalski, Kamil 
126801784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
126901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
127001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
127101784826SJohnathan Mantey             // current request.
1272271584abSEd Tanous             const std::string* addr = nullptr;
1273271584abSEd Tanous             const std::string* gw = nullptr;
127401784826SJohnathan Mantey             uint8_t prefixLength = 0;
127501784826SJohnathan Mantey             bool errorInEntry = false;
1276537174c4SEd Tanous             if (address)
12771abe55efSEd Tanous             {
1278033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
12791abe55efSEd Tanous                 {
128001784826SJohnathan Mantey                     addr = &(*address);
12814a0cb85cSEd Tanous                 }
128201784826SJohnathan Mantey                 else
128301784826SJohnathan Mantey                 {
1284bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1285bf648f77SEd Tanous                                                        pathString + "/Address");
128601784826SJohnathan Mantey                     errorInEntry = true;
128701784826SJohnathan Mantey                 }
128801784826SJohnathan Mantey             }
128985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
129001784826SJohnathan Mantey             {
129185ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
129201784826SJohnathan Mantey             }
129301784826SJohnathan Mantey             else
129401784826SJohnathan Mantey             {
129501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
129601784826SJohnathan Mantey                                           pathString + "/Address");
129701784826SJohnathan Mantey                 errorInEntry = true;
12984a0cb85cSEd Tanous             }
12994a0cb85cSEd Tanous 
1300537174c4SEd Tanous             if (subnetMask)
13014a0cb85cSEd Tanous             {
1302033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1303033f1e4dSEd Tanous                                                          &prefixLength))
13044a0cb85cSEd Tanous                 {
1305f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1306537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13074a0cb85cSEd Tanous                         pathString + "/SubnetMask");
130801784826SJohnathan Mantey                     errorInEntry = true;
13094a0cb85cSEd Tanous                 }
13104a0cb85cSEd Tanous             }
131185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13124a0cb85cSEd Tanous             {
1313033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
131401784826SJohnathan Mantey                                                          &prefixLength))
13154a0cb85cSEd Tanous                 {
131601784826SJohnathan Mantey                     messages::propertyValueFormatError(
131785ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
131801784826SJohnathan Mantey                         pathString + "/SubnetMask");
131901784826SJohnathan Mantey                     errorInEntry = true;
13204a0cb85cSEd Tanous                 }
13214a0cb85cSEd Tanous             }
13221abe55efSEd Tanous             else
13231abe55efSEd Tanous             {
132401784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
132501784826SJohnathan Mantey                                           pathString + "/SubnetMask");
132601784826SJohnathan Mantey                 errorInEntry = true;
132701784826SJohnathan Mantey             }
132801784826SJohnathan Mantey 
132901784826SJohnathan Mantey             if (gateway)
133001784826SJohnathan Mantey             {
1331033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
133201784826SJohnathan Mantey                 {
133301784826SJohnathan Mantey                     gw = &(*gateway);
133401784826SJohnathan Mantey                 }
133501784826SJohnathan Mantey                 else
133601784826SJohnathan Mantey                 {
1337bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1338bf648f77SEd Tanous                                                        pathString + "/Gateway");
133901784826SJohnathan Mantey                     errorInEntry = true;
134001784826SJohnathan Mantey                 }
134101784826SJohnathan Mantey             }
134285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
134301784826SJohnathan Mantey             {
134485ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
134501784826SJohnathan Mantey             }
134601784826SJohnathan Mantey             else
13471abe55efSEd Tanous             {
1348a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13494a0cb85cSEd Tanous                                           pathString + "/Gateway");
135001784826SJohnathan Mantey                 errorInEntry = true;
13514a0cb85cSEd Tanous             }
13524a0cb85cSEd Tanous 
135301784826SJohnathan Mantey             if (errorInEntry)
13541abe55efSEd Tanous             {
135501784826SJohnathan Mantey                 return;
13564a0cb85cSEd Tanous             }
13574a0cb85cSEd Tanous 
135885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13591abe55efSEd Tanous             {
13609c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
13619c5e585cSRavi Teja                                          nicIpEntry->id, prefixLength, *gw,
1362bf648f77SEd Tanous                                          *addr, asyncResp);
136389492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
136489492a15SPatrick Williams                                                   ipv4Data.cend());
1365588c3f0dSKowalski, Kamil             }
136601784826SJohnathan Mantey             else
136701784826SJohnathan Mantey             {
1368cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1369cb13a392SEd Tanous                            asyncResp);
13704a0cb85cSEd Tanous             }
13714a0cb85cSEd Tanous             entryIdx++;
13724a0cb85cSEd Tanous         }
137301784826SJohnathan Mantey         else
137401784826SJohnathan Mantey         {
137585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
137601784826SJohnathan Mantey             {
137701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
137801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
137901784826SJohnathan Mantey                 // in error, so bail out.
138001784826SJohnathan Mantey                 if (thisJson.is_null())
138101784826SJohnathan Mantey                 {
138201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
138301784826SJohnathan Mantey                     return;
138401784826SJohnathan Mantey                 }
138501784826SJohnathan Mantey                 messages::propertyValueFormatError(
138671f52d96SEd Tanous                     asyncResp->res,
138771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
138871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
138971f52d96SEd Tanous                     pathString);
139001784826SJohnathan Mantey                 return;
139101784826SJohnathan Mantey             }
139201784826SJohnathan Mantey 
139301784826SJohnathan Mantey             if (thisJson.is_null())
139401784826SJohnathan Mantey             {
13959c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
139601784826SJohnathan Mantey             }
139785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
139801784826SJohnathan Mantey             {
139989492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
140089492a15SPatrick Williams                                                   ipv4Data.cend());
140101784826SJohnathan Mantey             }
140201784826SJohnathan Mantey             entryIdx++;
140301784826SJohnathan Mantey         }
140401784826SJohnathan Mantey     }
14054a0cb85cSEd Tanous }
14064a0cb85cSEd Tanous 
14074f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1408f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1409f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14108d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1411f85837bfSRAJESWARAN THILLAIGOVINDAN {
1412f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
14135e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1414f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1415f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1416f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1417f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1418f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1419f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1420f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1421f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1422f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1423bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1424168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1425f85837bfSRAJESWARAN THILLAIGOVINDAN }
1426f85837bfSRAJESWARAN THILLAIGOVINDAN 
14274f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1428*ddd70dcaSEd Tanous     const std::string& ifaceId, const nlohmann::json::array_t& input,
142977179532SEd Tanous     const std::vector<IPv6AddressData>& ipv6Data,
14308d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1431e48c0fc5SRavi Teja {
1432*ddd70dcaSEd Tanous     if (input.empty())
1433e48c0fc5SRavi Teja     {
143471f52d96SEd Tanous         messages::propertyValueTypeError(
143571f52d96SEd Tanous             asyncResp->res,
1436*ddd70dcaSEd Tanous             nlohmann::json(input).dump(
1437*ddd70dcaSEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace),
1438e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1439e48c0fc5SRavi Teja         return;
1440e48c0fc5SRavi Teja     }
1441271584abSEd Tanous     size_t entryIdx = 1;
144277179532SEd Tanous     std::vector<IPv6AddressData>::const_iterator nicIpEntry =
14432c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1444f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1445e48c0fc5SRavi Teja     {
144689492a15SPatrick Williams         std::string pathString = "IPv6StaticAddresses/" +
144789492a15SPatrick Williams                                  std::to_string(entryIdx);
1448e48c0fc5SRavi Teja 
144901784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1450e48c0fc5SRavi Teja         {
1451e48c0fc5SRavi Teja             std::optional<std::string> address;
1452e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1453f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1454bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1455bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1456e48c0fc5SRavi Teja             {
145701784826SJohnathan Mantey                 messages::propertyValueFormatError(
145871f52d96SEd Tanous                     asyncResp->res,
145971f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
146071f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
146171f52d96SEd Tanous                     pathString);
1462e48c0fc5SRavi Teja                 return;
1463e48c0fc5SRavi Teja             }
1464e48c0fc5SRavi Teja 
1465543f4400SEd Tanous             const std::string* addr = nullptr;
1466543f4400SEd Tanous             uint8_t prefix = 0;
146701784826SJohnathan Mantey 
146801784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
146901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
147001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
147101784826SJohnathan Mantey             // current request.
1472e48c0fc5SRavi Teja             if (address)
1473e48c0fc5SRavi Teja             {
147401784826SJohnathan Mantey                 addr = &(*address);
1475e48c0fc5SRavi Teja             }
147685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
147701784826SJohnathan Mantey             {
147885ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
147901784826SJohnathan Mantey             }
148001784826SJohnathan Mantey             else
148101784826SJohnathan Mantey             {
148201784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
148301784826SJohnathan Mantey                                           pathString + "/Address");
148401784826SJohnathan Mantey                 return;
1485e48c0fc5SRavi Teja             }
1486e48c0fc5SRavi Teja 
1487e48c0fc5SRavi Teja             if (prefixLength)
1488e48c0fc5SRavi Teja             {
148901784826SJohnathan Mantey                 prefix = *prefixLength;
149001784826SJohnathan Mantey             }
149185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1492e48c0fc5SRavi Teja             {
149385ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1494e48c0fc5SRavi Teja             }
1495e48c0fc5SRavi Teja             else
1496e48c0fc5SRavi Teja             {
1497e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1498e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
149901784826SJohnathan Mantey                 return;
1500e48c0fc5SRavi Teja             }
1501e48c0fc5SRavi Teja 
150285ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1503e48c0fc5SRavi Teja             {
15049c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
15059c5e585cSRavi Teja                                          nicIpEntry->id, prefix, "", *addr,
1506e48c0fc5SRavi Teja                                          asyncResp);
150789492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
150889492a15SPatrick Williams                                                   ipv6Data.cend());
150901784826SJohnathan Mantey             }
151001784826SJohnathan Mantey             else
151101784826SJohnathan Mantey             {
151201784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1513e48c0fc5SRavi Teja             }
1514e48c0fc5SRavi Teja             entryIdx++;
1515e48c0fc5SRavi Teja         }
151601784826SJohnathan Mantey         else
151701784826SJohnathan Mantey         {
151885ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
151901784826SJohnathan Mantey             {
152001784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
152101784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
152201784826SJohnathan Mantey                 // in error, so bail out.
152301784826SJohnathan Mantey                 if (thisJson.is_null())
152401784826SJohnathan Mantey                 {
152501784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
152601784826SJohnathan Mantey                     return;
152701784826SJohnathan Mantey                 }
152801784826SJohnathan Mantey                 messages::propertyValueFormatError(
152971f52d96SEd Tanous                     asyncResp->res,
153071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
153171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
153271f52d96SEd Tanous                     pathString);
153301784826SJohnathan Mantey                 return;
153401784826SJohnathan Mantey             }
153501784826SJohnathan Mantey 
153601784826SJohnathan Mantey             if (thisJson.is_null())
153701784826SJohnathan Mantey             {
15389c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
153901784826SJohnathan Mantey             }
154085ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
154101784826SJohnathan Mantey             {
154289492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
154389492a15SPatrick Williams                                                   ipv6Data.cend());
154401784826SJohnathan Mantey             }
154501784826SJohnathan Mantey             entryIdx++;
154601784826SJohnathan Mantey         }
154701784826SJohnathan Mantey     }
1548e48c0fc5SRavi Teja }
1549e48c0fc5SRavi Teja 
155077179532SEd Tanous inline void
155177179532SEd Tanous     parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
155277179532SEd Tanous                        const std::string& ifaceId,
155377179532SEd Tanous                        const EthernetInterfaceData& ethData,
155477179532SEd Tanous                        const std::vector<IPv4AddressData>& ipv4Data,
155577179532SEd Tanous                        const std::vector<IPv6AddressData>& ipv6Data)
15564a0cb85cSEd Tanous {
15577a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1558eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1559eeedda23SJohnathan Mantey 
15602c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
156181ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1562ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
1563ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
15642c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1565eeedda23SJohnathan Mantey 
1566eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1567eeedda23SJohnathan Mantey 
15687a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
15697a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
15707a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1571b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1572eeedda23SJohnathan Mantey         if (ec)
1573029573d4SEd Tanous         {
1574eeedda23SJohnathan Mantey             return;
1575eeedda23SJohnathan Mantey         }
1576eeedda23SJohnathan Mantey 
1577914e2d5dSEd Tanous         health->inventory = resp;
15787a1dbc48SGeorge Liu         });
1579eeedda23SJohnathan Mantey 
1580eeedda23SJohnathan Mantey     health->populate();
1581eeedda23SJohnathan Mantey 
1582eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1583eeedda23SJohnathan Mantey     {
15840ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
15852c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1586029573d4SEd Tanous     }
1587029573d4SEd Tanous     else
1588029573d4SEd Tanous     {
15892c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
15902c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1591029573d4SEd Tanous     }
1592aa05fb27SJohnathan Mantey 
15932c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
159435fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
159582695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
15962c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
159782695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
159882695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
159982695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
160082695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16011f8c7b5dSJohnathan Mantey 
16022c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
160382695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16041f8c7b5dSJohnathan Mantey                                                                : "Disabled";
160582695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
160682695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
160782695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16082a133282Smanojkiraneda 
160982695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16104a0cb85cSEd Tanous     {
161182695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1612ab6554f1SJoshi-Mansi 
1613ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1614ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1615ab6554f1SJoshi-Mansi         // FQDN
161682695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1617d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1618d24bfc7aSJennifer Lee         {
16192c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1620d24bfc7aSJennifer Lee         }
16212c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16224a0cb85cSEd Tanous     }
16234a0cb85cSEd Tanous 
1624ef4c65b7SEd Tanous     jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1625ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
1626fda13ad2SSunitha Harish 
16272c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
16282c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
16294a0cb85cSEd Tanous 
16302c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
16312c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
16322c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
16332c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
16349eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
16354a0cb85cSEd Tanous     {
16362c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1637fa5053a6SGunnar Mills         if (gatewayStr.empty())
1638fa5053a6SGunnar Mills         {
1639fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1640fa5053a6SGunnar Mills         }
16411476687dSEd Tanous         nlohmann::json::object_t ipv4;
16421476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
16431476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
16441476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
16451476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1646fa5053a6SGunnar Mills 
16472c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1648d1d50814SRavi Teja         {
16491476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1650d1d50814SRavi Teja         }
16511476687dSEd Tanous 
1652b2ba3072SPatrick Williams         ipv4Array.emplace_back(std::move(ipv4));
165301784826SJohnathan Mantey     }
1654d1d50814SRavi Teja 
165582695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
16567ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
16577ea79e5eSRavi Teja     {
16587ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
16597ea79e5eSRavi Teja     }
16607ea79e5eSRavi Teja 
16617ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1662e48c0fc5SRavi Teja 
16632c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
16642c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
16652c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
16662c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
16677f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
16682c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
16697f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
16709eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1671e48c0fc5SRavi Teja     {
16721476687dSEd Tanous         nlohmann::json::object_t ipv6;
16731476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
16741476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
16751476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
1676f8361275SSunitha Harish 
1677b2ba3072SPatrick Williams         ipv6Array.emplace_back(std::move(ipv6));
16782c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1679e48c0fc5SRavi Teja         {
16801476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
16811476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
16821476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1683b2ba3072SPatrick Williams             ipv6StaticArray.emplace_back(std::move(ipv6Static));
168401784826SJohnathan Mantey         }
1685e48c0fc5SRavi Teja     }
1686588c3f0dSKowalski, Kamil }
1687588c3f0dSKowalski, Kamil 
16884f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1689bf648f77SEd Tanous {
169011ba3979SEd Tanous     return iface.starts_with(parent + "_");
1691bf648f77SEd Tanous }
1692bf648f77SEd Tanous 
1693bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1694bf648f77SEd Tanous {
1695bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1696ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
16971476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
16981476687dSEd Tanous             [&app](const crow::Request& req,
16991476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17003ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
170145ca1b86SEd Tanous         {
170245ca1b86SEd Tanous             return;
170345ca1b86SEd Tanous         }
170445ca1b86SEd Tanous 
1705bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1706bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1707bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1708bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1709bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1710bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1711bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1712bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1713bf648f77SEd Tanous 
1714bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1715bf648f77SEd Tanous         // preparation
1716002d39b4SEd Tanous         getEthernetIfaceList(
171777179532SEd Tanous             [asyncResp](const bool& success,
171877179532SEd Tanous                         const std::vector<std::string>& ifaceList) {
1719bf648f77SEd Tanous             if (!success)
17201abe55efSEd Tanous             {
1721f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17229391bb9cSRapkiewicz, Pawel                 return;
17239391bb9cSRapkiewicz, Pawel             }
17249391bb9cSRapkiewicz, Pawel 
1725002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1726bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1727bf648f77SEd Tanous             std::string tag = "_";
1728bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1729bf648f77SEd Tanous             {
1730bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1731bf648f77SEd Tanous                 if (found == std::string::npos)
1732bf648f77SEd Tanous                 {
17331476687dSEd Tanous                     nlohmann::json::object_t iface;
1734ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
1735ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1736ef4c65b7SEd Tanous                         ifaceItem);
1737b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
1738bf648f77SEd Tanous                 }
1739bf648f77SEd Tanous             }
1740bf648f77SEd Tanous 
1741002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1742bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1743bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1744bf648f77SEd Tanous         });
1745bf648f77SEd Tanous         });
1746bf648f77SEd Tanous 
1747bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1748ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1749bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
175045ca1b86SEd Tanous             [&app](const crow::Request& req,
1751bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1752bf648f77SEd Tanous                    const std::string& ifaceId) {
17533ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
175445ca1b86SEd Tanous         {
175545ca1b86SEd Tanous             return;
175645ca1b86SEd Tanous         }
17574a0cb85cSEd Tanous         getEthernetIfaceData(
1758bf648f77SEd Tanous             ifaceId,
175977179532SEd Tanous             [asyncResp, ifaceId](const bool& success,
176077179532SEd Tanous                                  const EthernetInterfaceData& ethData,
176177179532SEd Tanous                                  const std::vector<IPv4AddressData>& ipv4Data,
176277179532SEd Tanous                                  const std::vector<IPv6AddressData>& ipv6Data) {
17634a0cb85cSEd Tanous             if (!success)
17641abe55efSEd Tanous             {
1765bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1766bf648f77SEd Tanous                 // existing object, and other errors
1767002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1768002d39b4SEd Tanous                                            ifaceId);
17694a0cb85cSEd Tanous                 return;
17709391bb9cSRapkiewicz, Pawel             }
17714c9afe43SEd Tanous 
1772188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1773188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
17740f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1775188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1776002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
17770f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
17780f74e643SEd Tanous                 "Management Network Interface";
17790f74e643SEd Tanous 
1780002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
17819391bb9cSRapkiewicz, Pawel             });
1782bf648f77SEd Tanous         });
17839391bb9cSRapkiewicz, Pawel 
1784bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1785ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1786bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
178745ca1b86SEd Tanous             [&app](const crow::Request& req,
1788bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1789bf648f77SEd Tanous                    const std::string& ifaceId) {
17903ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179145ca1b86SEd Tanous         {
179245ca1b86SEd Tanous             return;
179345ca1b86SEd Tanous         }
1794bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1795ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1796d577665bSRatan Gupta         std::optional<std::string> macAddress;
17979a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1798*ddd70dcaSEd Tanous         std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
1799*ddd70dcaSEd Tanous         std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
1800f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1801da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
18021f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1803eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
180435fb5311STejas Patil         std::optional<size_t> mtuSize;
18051f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18061f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18070627a2c7SEd Tanous 
180815ed6780SWilly Tu         if (!json_util::readJsonPatch(
18098d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1810002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1811002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1812002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1813ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1814002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18151abe55efSEd Tanous         {
1816588c3f0dSKowalski, Kamil             return;
1817588c3f0dSKowalski, Kamil         }
1818da131a9aSJennifer Lee         if (dhcpv4)
1819da131a9aSJennifer Lee         {
1820002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
18211f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
182282695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
182382695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
182482695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
18251f8c7b5dSJohnathan Mantey             {
18261f8c7b5dSJohnathan Mantey                 return;
18271f8c7b5dSJohnathan Mantey             }
18281f8c7b5dSJohnathan Mantey         }
18291f8c7b5dSJohnathan Mantey 
18301f8c7b5dSJohnathan Mantey         if (dhcpv6)
18311f8c7b5dSJohnathan Mantey         {
1832002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1833002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1834002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1835002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1836002d39b4SEd Tanous                                      "UseDomainName",
183782695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
18381f8c7b5dSJohnathan Mantey             {
18391f8c7b5dSJohnathan Mantey                 return;
18401f8c7b5dSJohnathan Mantey             }
1841da131a9aSJennifer Lee         }
1842da131a9aSJennifer Lee 
1843bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1844bf648f77SEd Tanous         // for JSON preparation
18454a0cb85cSEd Tanous         getEthernetIfaceData(
18462c70f800SEd Tanous             ifaceId,
1847bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1848ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1849d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
18509a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1851e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
18521f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1853bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1854bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1855f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1856002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
185777179532SEd Tanous                 const std::vector<IPv4AddressData>& ipv4Data,
185877179532SEd Tanous                 const std::vector<IPv6AddressData>& ipv6Data) {
18591abe55efSEd Tanous             if (!success)
18601abe55efSEd Tanous             {
1861588c3f0dSKowalski, Kamil                 // ... otherwise return error
1862bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1863bf648f77SEd Tanous                 // existing object, and other errors
1864002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1865002d39b4SEd Tanous                                            ifaceId);
1866588c3f0dSKowalski, Kamil                 return;
1867588c3f0dSKowalski, Kamil             }
1868588c3f0dSKowalski, Kamil 
18691f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
18701f8c7b5dSJohnathan Mantey             {
1871002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1872002d39b4SEd Tanous                                 asyncResp);
18731f8c7b5dSJohnathan Mantey             }
18741f8c7b5dSJohnathan Mantey 
18750627a2c7SEd Tanous             if (hostname)
18761abe55efSEd Tanous             {
18770627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
18781abe55efSEd Tanous             }
18790627a2c7SEd Tanous 
1880ab6554f1SJoshi-Mansi             if (fqdn)
1881ab6554f1SJoshi-Mansi             {
18822c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1883ab6554f1SJoshi-Mansi             }
1884ab6554f1SJoshi-Mansi 
1885d577665bSRatan Gupta             if (macAddress)
1886d577665bSRatan Gupta             {
1887002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1888d577665bSRatan Gupta             }
1889d577665bSRatan Gupta 
1890d1d50814SRavi Teja             if (ipv4StaticAddresses)
1891d1d50814SRavi Teja             {
1892bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1893bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1894bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1895bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1896bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1897bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1898bf648f77SEd Tanous                 // that, but could be done more efficiently
1899*ddd70dcaSEd Tanous                 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
1900002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
19011abe55efSEd Tanous             }
19020627a2c7SEd Tanous 
1903f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1904f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1905002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1906002d39b4SEd Tanous                                              asyncResp);
1907f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19089a6fc6feSRavi Teja 
19099a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19109a6fc6feSRavi Teja             {
19119a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19129a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19139a6fc6feSRavi Teja             }
1914e48c0fc5SRavi Teja 
1915e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1916e48c0fc5SRavi Teja             {
1917*ddd70dcaSEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
1918*ddd70dcaSEd Tanous                                                ipv6Data, asyncResp);
1919e48c0fc5SRavi Teja             }
1920eeedda23SJohnathan Mantey 
1921eeedda23SJohnathan Mantey             if (interfaceEnabled)
1922eeedda23SJohnathan Mantey             {
1923002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1924002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
1925eeedda23SJohnathan Mantey             }
192635fb5311STejas Patil 
192735fb5311STejas Patil             if (mtuSize)
192835fb5311STejas Patil             {
192935fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
193035fb5311STejas Patil             }
1931588c3f0dSKowalski, Kamil             });
1932bf648f77SEd Tanous         });
19339391bb9cSRapkiewicz, Pawel 
1934bf648f77SEd Tanous     BMCWEB_ROUTE(
1935bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
1936ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
1937bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
193845ca1b86SEd Tanous             [&app](const crow::Request& req,
1939bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194045ca1b86SEd Tanous                    const std::string& parentIfaceId,
194145ca1b86SEd Tanous                    const std::string& ifaceId) {
19423ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194345ca1b86SEd Tanous         {
194445ca1b86SEd Tanous             return;
194545ca1b86SEd Tanous         }
19468d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
19470f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
19488d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
1949e439f0f8SKowalski, Kamil 
19502c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
19511abe55efSEd Tanous         {
1952a434f2bdSEd Tanous             return;
1953a434f2bdSEd Tanous         }
1954a434f2bdSEd Tanous 
1955bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1956bf648f77SEd Tanous         // for JSON preparation
195777179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
195877179532SEd Tanous                                           const bool& success,
195977179532SEd Tanous                                           const EthernetInterfaceData& ethData,
196077179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
196177179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
196217e22024SJiaqing Zhao             if (success && ethData.vlanId)
19631abe55efSEd Tanous             {
196422872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
1965ef4c65b7SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1966ef4c65b7SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1967ef4c65b7SEd Tanous                     parentIfaceId, ifaceId);
196822872ff3SJiaqing Zhao 
196923a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
197022872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
19711abe55efSEd Tanous             }
19721abe55efSEd Tanous             else
19731abe55efSEd Tanous             {
1974e439f0f8SKowalski, Kamil                 // ... otherwise return error
1975bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1976bf648f77SEd Tanous                 // existing object, and other errors
1977bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
1978d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
1979e439f0f8SKowalski, Kamil             }
1980e439f0f8SKowalski, Kamil         });
1981bf648f77SEd Tanous         });
1982e439f0f8SKowalski, Kamil 
1983bf648f77SEd Tanous     BMCWEB_ROUTE(
1984bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
19853d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
1986bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
198745ca1b86SEd Tanous             [&app](const crow::Request& req,
1988bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
198945ca1b86SEd Tanous                    const std::string& parentIfaceId,
199045ca1b86SEd Tanous                    const std::string& ifaceId) {
19913ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199245ca1b86SEd Tanous         {
199345ca1b86SEd Tanous             return;
199445ca1b86SEd Tanous         }
1995fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
19961abe55efSEd Tanous         {
1997d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
1998002d39b4SEd Tanous                                        ifaceId);
1999927a505aSKowalski, Kamil             return;
2000927a505aSKowalski, Kamil         }
2001927a505aSKowalski, Kamil 
20023927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20033927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20040627a2c7SEd Tanous 
200515ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2006bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20071abe55efSEd Tanous         {
2008927a505aSKowalski, Kamil             return;
2009927a505aSKowalski, Kamil         }
2010927a505aSKowalski, Kamil 
20113927e13eSJiaqing Zhao         if (vlanId)
20123927e13eSJiaqing Zhao         {
20133927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20143927e13eSJiaqing Zhao             return;
20153927e13eSJiaqing Zhao         }
20163927e13eSJiaqing Zhao 
2017bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2018bf648f77SEd Tanous         // for JSON preparation
201977179532SEd Tanous         getEthernetIfaceData(ifaceId,
202077179532SEd Tanous                              [asyncResp, parentIfaceId, ifaceId,
202177179532SEd Tanous                               vlanEnable](const bool& success,
202277179532SEd Tanous                                           const EthernetInterfaceData& ethData,
202377179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
202477179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
202517e22024SJiaqing Zhao             if (success && ethData.vlanId)
202608244d02SSunitha Harish             {
202723a06317SJiaqing Zhao                 if (vlanEnable)
202823a06317SJiaqing Zhao                 {
202923a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
20305e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
203108244d02SSunitha Harish                         if (ec)
203208244d02SSunitha Harish                         {
203308244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20343927e13eSJiaqing Zhao                             return;
203508244d02SSunitha Harish                         }
203623a06317SJiaqing Zhao                         },
203723a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
203823a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
203923a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
204023a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
204123a06317SJiaqing Zhao                         "NICEnabled",
204223a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
204308244d02SSunitha Harish                 }
204408244d02SSunitha Harish             }
204508244d02SSunitha Harish             else
20461abe55efSEd Tanous             {
2047bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2048bf648f77SEd Tanous                 // existing object, and other errors
2049bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2050d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2051bf648f77SEd Tanous                 return;
2052bf648f77SEd Tanous             }
2053bf648f77SEd Tanous         });
2054bf648f77SEd Tanous         });
2055bf648f77SEd Tanous 
2056bf648f77SEd Tanous     BMCWEB_ROUTE(
2057bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20583d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2059bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
206045ca1b86SEd Tanous             [&app](const crow::Request& req,
2061bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206245ca1b86SEd Tanous                    const std::string& parentIfaceId,
206345ca1b86SEd Tanous                    const std::string& ifaceId) {
20643ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
206545ca1b86SEd Tanous         {
206645ca1b86SEd Tanous             return;
206745ca1b86SEd Tanous         }
2068bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2069bf648f77SEd Tanous         {
2070d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2071002d39b4SEd Tanous                                        ifaceId);
2072927a505aSKowalski, Kamil             return;
2073927a505aSKowalski, Kamil         }
2074e439f0f8SKowalski, Kamil 
2075bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2076bf648f77SEd Tanous         // for JSON preparation
207777179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
207877179532SEd Tanous                                           const bool& success,
207977179532SEd Tanous                                           const EthernetInterfaceData& ethData,
208077179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
208177179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
208217e22024SJiaqing Zhao             if (success && ethData.vlanId)
20831abe55efSEd Tanous             {
2084f12894f8SJason M. Bills                 auto callback =
20855e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
20861abe55efSEd Tanous                     if (ec)
20871abe55efSEd Tanous                     {
2088f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2089927a505aSKowalski, Kamil                     }
20904a0cb85cSEd Tanous                 };
20914a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2092002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2093002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
20944a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
20951abe55efSEd Tanous             }
20961abe55efSEd Tanous             else
20971abe55efSEd Tanous             {
2098927a505aSKowalski, Kamil                 // ... otherwise return error
2099bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2100bf648f77SEd Tanous                 // existing object, and other errors
2101bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2102d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2103927a505aSKowalski, Kamil             }
2104927a505aSKowalski, Kamil         });
2105bf648f77SEd Tanous         });
2106e439f0f8SKowalski, Kamil 
2107bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2108bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2109ed398213SEd Tanous 
2110ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21111476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21121476687dSEd Tanous             [&app](const crow::Request& req,
2113bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2114bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21153ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
211645ca1b86SEd Tanous         {
211745ca1b86SEd Tanous             return;
211845ca1b86SEd Tanous         }
21194a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21201abe55efSEd Tanous         // preparation
212177179532SEd Tanous         getEthernetIfaceList([asyncResp, rootInterfaceName](
21221abe55efSEd Tanous                                  const bool& success,
212377179532SEd Tanous                                  const std::vector<std::string>& ifaceList) {
21244a0cb85cSEd Tanous             if (!success)
21251abe55efSEd Tanous             {
2126f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21274a0cb85cSEd Tanous                 return;
21281abe55efSEd Tanous             }
212977179532SEd Tanous             if (std::find(ifaceList.begin(), ifaceList.end(),
213077179532SEd Tanous                           rootInterfaceName) == ifaceList.end())
21314c9afe43SEd Tanous             {
2132002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2133002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21344c9afe43SEd Tanous                                            rootInterfaceName);
21354c9afe43SEd Tanous                 return;
21364c9afe43SEd Tanous             }
21374c9afe43SEd Tanous 
21380f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21390f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21400f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21410f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21420f74e643SEd Tanous                 "VLAN Network Interface Collection";
21434a0cb85cSEd Tanous 
21442c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21454a0cb85cSEd Tanous 
214681ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21471abe55efSEd Tanous             {
214811ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21494a0cb85cSEd Tanous                 {
21501476687dSEd Tanous                     nlohmann::json::object_t iface;
2151ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
2152ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2153ef4c65b7SEd Tanous                         rootInterfaceName, ifaceItem);
2154b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
2155e439f0f8SKowalski, Kamil                 }
2156e439f0f8SKowalski, Kamil             }
2157e439f0f8SKowalski, Kamil 
2158002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
21592c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2160ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2161ef4c65b7SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2162ef4c65b7SEd Tanous                 rootInterfaceName);
2163e439f0f8SKowalski, Kamil         });
2164bf648f77SEd Tanous         });
2165e439f0f8SKowalski, Kamil 
2166bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2167bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
21683d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2169bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
217045ca1b86SEd Tanous             [&app](const crow::Request& req,
2171bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2172bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21733ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
217445ca1b86SEd Tanous         {
217545ca1b86SEd Tanous             return;
217645ca1b86SEd Tanous         }
2177fda13ad2SSunitha Harish         bool vlanEnable = false;
21780627a2c7SEd Tanous         uint32_t vlanId = 0;
2179002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2180002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
21811abe55efSEd Tanous         {
21824a0cb85cSEd Tanous             return;
2183e439f0f8SKowalski, Kamil         }
2184fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2185dbb59d4dSEd Tanous         if (vlanId == 0U)
2186fda13ad2SSunitha Harish         {
2187fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2188fda13ad2SSunitha Harish         }
2189fda13ad2SSunitha Harish         if (!vlanEnable)
2190fda13ad2SSunitha Harish         {
2191fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2192fda13ad2SSunitha Harish         }
2193271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2194fda13ad2SSunitha Harish         {
2195fda13ad2SSunitha Harish             return;
2196fda13ad2SSunitha Harish         }
2197fda13ad2SSunitha Harish 
21985e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
21991abe55efSEd Tanous             if (ec)
22001abe55efSEd Tanous             {
2201bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2202bf648f77SEd Tanous                 // based on phosphor-network responses
2203f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22044a0cb85cSEd Tanous                 return;
22051abe55efSEd Tanous             }
2206f12894f8SJason M. Bills             messages::created(asyncResp->res);
2207e439f0f8SKowalski, Kamil         };
22084a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22094a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22104a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22114a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22120627a2c7SEd Tanous             rootInterfaceName, vlanId);
2213bf648f77SEd Tanous         });
22144a0cb85cSEd Tanous }
2215bf648f77SEd Tanous 
22169391bb9cSRapkiewicz, Pawel } // namespace redfish
2217