xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 77179532f2082be00b69d62b5ec5b52bf5860be2)
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>
36*77179532SEd 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;
58*77179532SEd Tanous     LinkType linktype{};
59*77179532SEd 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;
70*77179532SEd 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
418*77179532SEd Tanous inline void extractIPV6Data(const std::string& ethifaceId,
419711ac7a9SEd Tanous                             const dbus::utility::ManagedObjectType& dbusData,
420*77179532SEd 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
458*77179532SEd 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
511*77179532SEd Tanous inline void extractIPData(const std::string& ethifaceId,
512711ac7a9SEd Tanous                           const dbus::utility::ManagedObjectType& dbusData,
513*77179532SEd 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
551*77179532SEd 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{};
787*77179532SEd Tanous         std::vector<IPv4AddressData> ipv4Data;
788*77179532SEd 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
838*77179532SEd 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.
864*77179532SEd 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 
1203*77179532SEd Tanous inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
1204*77179532SEd Tanous     const std::vector<IPv4AddressData>::const_iterator& head,
1205*77179532SEd 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 
1212*77179532SEd Tanous inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
1213*77179532SEd Tanous     const std::vector<IPv6AddressData>::const_iterator& head,
1214*77179532SEd 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 
1221*77179532SEd Tanous inline void
1222*77179532SEd Tanous     handleIPv4StaticPatch(const std::string& ifaceId, nlohmann::json& input,
1223*77179532SEd Tanous                           const std::vector<IPv4AddressData>& ipv4Data,
12248d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12251abe55efSEd Tanous {
122601784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1227f476acbfSRatan Gupta     {
122871f52d96SEd Tanous         messages::propertyValueTypeError(
122971f52d96SEd Tanous             asyncResp->res,
1230bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1231d1d50814SRavi Teja             "IPv4StaticAddresses");
1232f476acbfSRatan Gupta         return;
1233f476acbfSRatan Gupta     }
1234f476acbfSRatan Gupta 
1235271584abSEd Tanous     unsigned entryIdx = 1;
123601784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
123701784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
123801784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
123901784826SJohnathan Mantey     // into the NIC.
1240*77179532SEd Tanous     std::vector<IPv4AddressData>::const_iterator nicIpEntry =
12412c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
124201784826SJohnathan Mantey 
1243537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12441abe55efSEd Tanous     {
124589492a15SPatrick Williams         std::string pathString = "IPv4StaticAddresses/" +
124689492a15SPatrick Williams                                  std::to_string(entryIdx);
1247179db1d7SKowalski, Kamil 
124801784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1249f476acbfSRatan Gupta         {
1250537174c4SEd Tanous             std::optional<std::string> address;
1251537174c4SEd Tanous             std::optional<std::string> subnetMask;
1252537174c4SEd Tanous             std::optional<std::string> gateway;
1253537174c4SEd Tanous 
1254537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12557e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12567e27d832SJohnathan Mantey                                      "Gateway", gateway))
1257537174c4SEd Tanous             {
125801784826SJohnathan Mantey                 messages::propertyValueFormatError(
125971f52d96SEd Tanous                     asyncResp->res,
126071f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
126171f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
126271f52d96SEd Tanous                     pathString);
1263537174c4SEd Tanous                 return;
1264179db1d7SKowalski, Kamil             }
1265179db1d7SKowalski, Kamil 
126601784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
126701784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
126801784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
126901784826SJohnathan Mantey             // current request.
1270271584abSEd Tanous             const std::string* addr = nullptr;
1271271584abSEd Tanous             const std::string* gw = nullptr;
127201784826SJohnathan Mantey             uint8_t prefixLength = 0;
127301784826SJohnathan Mantey             bool errorInEntry = false;
1274537174c4SEd Tanous             if (address)
12751abe55efSEd Tanous             {
1276033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
12771abe55efSEd Tanous                 {
127801784826SJohnathan Mantey                     addr = &(*address);
12794a0cb85cSEd Tanous                 }
128001784826SJohnathan Mantey                 else
128101784826SJohnathan Mantey                 {
1282bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1283bf648f77SEd Tanous                                                        pathString + "/Address");
128401784826SJohnathan Mantey                     errorInEntry = true;
128501784826SJohnathan Mantey                 }
128601784826SJohnathan Mantey             }
128785ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
128801784826SJohnathan Mantey             {
128985ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
129001784826SJohnathan Mantey             }
129101784826SJohnathan Mantey             else
129201784826SJohnathan Mantey             {
129301784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
129401784826SJohnathan Mantey                                           pathString + "/Address");
129501784826SJohnathan Mantey                 errorInEntry = true;
12964a0cb85cSEd Tanous             }
12974a0cb85cSEd Tanous 
1298537174c4SEd Tanous             if (subnetMask)
12994a0cb85cSEd Tanous             {
1300033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1301033f1e4dSEd Tanous                                                          &prefixLength))
13024a0cb85cSEd Tanous                 {
1303f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1304537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13054a0cb85cSEd Tanous                         pathString + "/SubnetMask");
130601784826SJohnathan Mantey                     errorInEntry = true;
13074a0cb85cSEd Tanous                 }
13084a0cb85cSEd Tanous             }
130985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13104a0cb85cSEd Tanous             {
1311033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
131201784826SJohnathan Mantey                                                          &prefixLength))
13134a0cb85cSEd Tanous                 {
131401784826SJohnathan Mantey                     messages::propertyValueFormatError(
131585ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
131601784826SJohnathan Mantey                         pathString + "/SubnetMask");
131701784826SJohnathan Mantey                     errorInEntry = true;
13184a0cb85cSEd Tanous                 }
13194a0cb85cSEd Tanous             }
13201abe55efSEd Tanous             else
13211abe55efSEd Tanous             {
132201784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
132301784826SJohnathan Mantey                                           pathString + "/SubnetMask");
132401784826SJohnathan Mantey                 errorInEntry = true;
132501784826SJohnathan Mantey             }
132601784826SJohnathan Mantey 
132701784826SJohnathan Mantey             if (gateway)
132801784826SJohnathan Mantey             {
1329033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
133001784826SJohnathan Mantey                 {
133101784826SJohnathan Mantey                     gw = &(*gateway);
133201784826SJohnathan Mantey                 }
133301784826SJohnathan Mantey                 else
133401784826SJohnathan Mantey                 {
1335bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1336bf648f77SEd Tanous                                                        pathString + "/Gateway");
133701784826SJohnathan Mantey                     errorInEntry = true;
133801784826SJohnathan Mantey                 }
133901784826SJohnathan Mantey             }
134085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
134101784826SJohnathan Mantey             {
134285ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
134301784826SJohnathan Mantey             }
134401784826SJohnathan Mantey             else
13451abe55efSEd Tanous             {
1346a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13474a0cb85cSEd Tanous                                           pathString + "/Gateway");
134801784826SJohnathan Mantey                 errorInEntry = true;
13494a0cb85cSEd Tanous             }
13504a0cb85cSEd Tanous 
135101784826SJohnathan Mantey             if (errorInEntry)
13521abe55efSEd Tanous             {
135301784826SJohnathan Mantey                 return;
13544a0cb85cSEd Tanous             }
13554a0cb85cSEd Tanous 
135685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13571abe55efSEd Tanous             {
13589c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
13599c5e585cSRavi Teja                                          nicIpEntry->id, prefixLength, *gw,
1360bf648f77SEd Tanous                                          *addr, asyncResp);
136189492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
136289492a15SPatrick Williams                                                   ipv4Data.cend());
1363588c3f0dSKowalski, Kamil             }
136401784826SJohnathan Mantey             else
136501784826SJohnathan Mantey             {
1366cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1367cb13a392SEd Tanous                            asyncResp);
13684a0cb85cSEd Tanous             }
13694a0cb85cSEd Tanous             entryIdx++;
13704a0cb85cSEd Tanous         }
137101784826SJohnathan Mantey         else
137201784826SJohnathan Mantey         {
137385ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
137401784826SJohnathan Mantey             {
137501784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
137601784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
137701784826SJohnathan Mantey                 // in error, so bail out.
137801784826SJohnathan Mantey                 if (thisJson.is_null())
137901784826SJohnathan Mantey                 {
138001784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
138101784826SJohnathan Mantey                     return;
138201784826SJohnathan Mantey                 }
138301784826SJohnathan Mantey                 messages::propertyValueFormatError(
138471f52d96SEd Tanous                     asyncResp->res,
138571f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
138671f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
138771f52d96SEd Tanous                     pathString);
138801784826SJohnathan Mantey                 return;
138901784826SJohnathan Mantey             }
139001784826SJohnathan Mantey 
139101784826SJohnathan Mantey             if (thisJson.is_null())
139201784826SJohnathan Mantey             {
13939c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
139401784826SJohnathan Mantey             }
139585ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
139601784826SJohnathan Mantey             {
139789492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
139889492a15SPatrick Williams                                                   ipv4Data.cend());
139901784826SJohnathan Mantey             }
140001784826SJohnathan Mantey             entryIdx++;
140101784826SJohnathan Mantey         }
140201784826SJohnathan Mantey     }
14034a0cb85cSEd Tanous }
14044a0cb85cSEd Tanous 
14054f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1406f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1407f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14088d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1409f85837bfSRAJESWARAN THILLAIGOVINDAN {
1410f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
14115e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1412f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1413f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1414f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1415f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1416f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1417f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1418f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1419f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1420f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1421bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1422168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1423f85837bfSRAJESWARAN THILLAIGOVINDAN }
1424f85837bfSRAJESWARAN THILLAIGOVINDAN 
14254f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1426f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
1427*77179532SEd Tanous     const std::vector<IPv6AddressData>& ipv6Data,
14288d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1429e48c0fc5SRavi Teja {
143001784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1431e48c0fc5SRavi Teja     {
143271f52d96SEd Tanous         messages::propertyValueTypeError(
143371f52d96SEd Tanous             asyncResp->res,
1434bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1435e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1436e48c0fc5SRavi Teja         return;
1437e48c0fc5SRavi Teja     }
1438271584abSEd Tanous     size_t entryIdx = 1;
1439*77179532SEd Tanous     std::vector<IPv6AddressData>::const_iterator nicIpEntry =
14402c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1441f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1442e48c0fc5SRavi Teja     {
144389492a15SPatrick Williams         std::string pathString = "IPv6StaticAddresses/" +
144489492a15SPatrick Williams                                  std::to_string(entryIdx);
1445e48c0fc5SRavi Teja 
144601784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1447e48c0fc5SRavi Teja         {
1448e48c0fc5SRavi Teja             std::optional<std::string> address;
1449e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1450f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1451bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1452bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1453e48c0fc5SRavi Teja             {
145401784826SJohnathan Mantey                 messages::propertyValueFormatError(
145571f52d96SEd Tanous                     asyncResp->res,
145671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
145771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
145871f52d96SEd Tanous                     pathString);
1459e48c0fc5SRavi Teja                 return;
1460e48c0fc5SRavi Teja             }
1461e48c0fc5SRavi Teja 
1462543f4400SEd Tanous             const std::string* addr = nullptr;
1463543f4400SEd Tanous             uint8_t prefix = 0;
146401784826SJohnathan Mantey 
146501784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
146601784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
146701784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
146801784826SJohnathan Mantey             // current request.
1469e48c0fc5SRavi Teja             if (address)
1470e48c0fc5SRavi Teja             {
147101784826SJohnathan Mantey                 addr = &(*address);
1472e48c0fc5SRavi Teja             }
147385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
147401784826SJohnathan Mantey             {
147585ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
147601784826SJohnathan Mantey             }
147701784826SJohnathan Mantey             else
147801784826SJohnathan Mantey             {
147901784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
148001784826SJohnathan Mantey                                           pathString + "/Address");
148101784826SJohnathan Mantey                 return;
1482e48c0fc5SRavi Teja             }
1483e48c0fc5SRavi Teja 
1484e48c0fc5SRavi Teja             if (prefixLength)
1485e48c0fc5SRavi Teja             {
148601784826SJohnathan Mantey                 prefix = *prefixLength;
148701784826SJohnathan Mantey             }
148885ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1489e48c0fc5SRavi Teja             {
149085ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1491e48c0fc5SRavi Teja             }
1492e48c0fc5SRavi Teja             else
1493e48c0fc5SRavi Teja             {
1494e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1495e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
149601784826SJohnathan Mantey                 return;
1497e48c0fc5SRavi Teja             }
1498e48c0fc5SRavi Teja 
149985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1500e48c0fc5SRavi Teja             {
15019c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
15029c5e585cSRavi Teja                                          nicIpEntry->id, prefix, "", *addr,
1503e48c0fc5SRavi Teja                                          asyncResp);
150489492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
150589492a15SPatrick Williams                                                   ipv6Data.cend());
150601784826SJohnathan Mantey             }
150701784826SJohnathan Mantey             else
150801784826SJohnathan Mantey             {
150901784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1510e48c0fc5SRavi Teja             }
1511e48c0fc5SRavi Teja             entryIdx++;
1512e48c0fc5SRavi Teja         }
151301784826SJohnathan Mantey         else
151401784826SJohnathan Mantey         {
151585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
151601784826SJohnathan Mantey             {
151701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
151801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
151901784826SJohnathan Mantey                 // in error, so bail out.
152001784826SJohnathan Mantey                 if (thisJson.is_null())
152101784826SJohnathan Mantey                 {
152201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
152301784826SJohnathan Mantey                     return;
152401784826SJohnathan Mantey                 }
152501784826SJohnathan Mantey                 messages::propertyValueFormatError(
152671f52d96SEd Tanous                     asyncResp->res,
152771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
152871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
152971f52d96SEd Tanous                     pathString);
153001784826SJohnathan Mantey                 return;
153101784826SJohnathan Mantey             }
153201784826SJohnathan Mantey 
153301784826SJohnathan Mantey             if (thisJson.is_null())
153401784826SJohnathan Mantey             {
15359c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
153601784826SJohnathan Mantey             }
153785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
153801784826SJohnathan Mantey             {
153989492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
154089492a15SPatrick Williams                                                   ipv6Data.cend());
154101784826SJohnathan Mantey             }
154201784826SJohnathan Mantey             entryIdx++;
154301784826SJohnathan Mantey         }
154401784826SJohnathan Mantey     }
1545e48c0fc5SRavi Teja }
1546e48c0fc5SRavi Teja 
1547*77179532SEd Tanous inline void
1548*77179532SEd Tanous     parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1549*77179532SEd Tanous                        const std::string& ifaceId,
1550*77179532SEd Tanous                        const EthernetInterfaceData& ethData,
1551*77179532SEd Tanous                        const std::vector<IPv4AddressData>& ipv4Data,
1552*77179532SEd Tanous                        const std::vector<IPv6AddressData>& ipv6Data)
15534a0cb85cSEd Tanous {
15547a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1555eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1556eeedda23SJohnathan Mantey 
15572c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
155881ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1559ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
1560ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
15612c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1562eeedda23SJohnathan Mantey 
1563eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1564eeedda23SJohnathan Mantey 
15657a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
15667a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
15677a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1568b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1569eeedda23SJohnathan Mantey         if (ec)
1570029573d4SEd Tanous         {
1571eeedda23SJohnathan Mantey             return;
1572eeedda23SJohnathan Mantey         }
1573eeedda23SJohnathan Mantey 
1574914e2d5dSEd Tanous         health->inventory = resp;
15757a1dbc48SGeorge Liu         });
1576eeedda23SJohnathan Mantey 
1577eeedda23SJohnathan Mantey     health->populate();
1578eeedda23SJohnathan Mantey 
1579eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1580eeedda23SJohnathan Mantey     {
15810ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
15822c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1583029573d4SEd Tanous     }
1584029573d4SEd Tanous     else
1585029573d4SEd Tanous     {
15862c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
15872c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1588029573d4SEd Tanous     }
1589aa05fb27SJohnathan Mantey 
15902c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
159135fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
159282695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
15932c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
159482695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
159582695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
159682695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
159782695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
15981f8c7b5dSJohnathan Mantey 
15992c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
160082695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16011f8c7b5dSJohnathan Mantey                                                                : "Disabled";
160282695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
160382695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
160482695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16052a133282Smanojkiraneda 
160682695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16074a0cb85cSEd Tanous     {
160882695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1609ab6554f1SJoshi-Mansi 
1610ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1611ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1612ab6554f1SJoshi-Mansi         // FQDN
161382695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1614d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1615d24bfc7aSJennifer Lee         {
16162c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1617d24bfc7aSJennifer Lee         }
16182c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16194a0cb85cSEd Tanous     }
16204a0cb85cSEd Tanous 
1621ef4c65b7SEd Tanous     jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1622ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
1623fda13ad2SSunitha Harish 
16242c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
16252c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
16264a0cb85cSEd Tanous 
16272c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
16282c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
16292c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
16302c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
16319eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
16324a0cb85cSEd Tanous     {
16332c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1634fa5053a6SGunnar Mills         if (gatewayStr.empty())
1635fa5053a6SGunnar Mills         {
1636fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1637fa5053a6SGunnar Mills         }
16381476687dSEd Tanous         nlohmann::json::object_t ipv4;
16391476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
16401476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
16411476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
16421476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1643fa5053a6SGunnar Mills 
16442c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1645d1d50814SRavi Teja         {
16461476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1647d1d50814SRavi Teja         }
16481476687dSEd Tanous 
1649b2ba3072SPatrick Williams         ipv4Array.emplace_back(std::move(ipv4));
165001784826SJohnathan Mantey     }
1651d1d50814SRavi Teja 
165282695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
16537ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
16547ea79e5eSRavi Teja     {
16557ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
16567ea79e5eSRavi Teja     }
16577ea79e5eSRavi Teja 
16587ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1659e48c0fc5SRavi Teja 
16602c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
16612c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
16622c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
16632c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
16647f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
16652c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
16667f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
16679eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1668e48c0fc5SRavi Teja     {
16691476687dSEd Tanous         nlohmann::json::object_t ipv6;
16701476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
16711476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
16721476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
1673f8361275SSunitha Harish 
1674b2ba3072SPatrick Williams         ipv6Array.emplace_back(std::move(ipv6));
16752c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1676e48c0fc5SRavi Teja         {
16771476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
16781476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
16791476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1680b2ba3072SPatrick Williams             ipv6StaticArray.emplace_back(std::move(ipv6Static));
168101784826SJohnathan Mantey         }
1682e48c0fc5SRavi Teja     }
1683588c3f0dSKowalski, Kamil }
1684588c3f0dSKowalski, Kamil 
16854f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1686bf648f77SEd Tanous {
168711ba3979SEd Tanous     return iface.starts_with(parent + "_");
1688bf648f77SEd Tanous }
1689bf648f77SEd Tanous 
1690bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1691bf648f77SEd Tanous {
1692bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1693ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
16941476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
16951476687dSEd Tanous             [&app](const crow::Request& req,
16961476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
16973ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
169845ca1b86SEd Tanous         {
169945ca1b86SEd Tanous             return;
170045ca1b86SEd Tanous         }
170145ca1b86SEd Tanous 
1702bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1703bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1704bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1705bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1706bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1707bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1708bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1709bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1710bf648f77SEd Tanous 
1711bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1712bf648f77SEd Tanous         // preparation
1713002d39b4SEd Tanous         getEthernetIfaceList(
1714*77179532SEd Tanous             [asyncResp](const bool& success,
1715*77179532SEd Tanous                         const std::vector<std::string>& ifaceList) {
1716bf648f77SEd Tanous             if (!success)
17171abe55efSEd Tanous             {
1718f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17199391bb9cSRapkiewicz, Pawel                 return;
17209391bb9cSRapkiewicz, Pawel             }
17219391bb9cSRapkiewicz, Pawel 
1722002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1723bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1724bf648f77SEd Tanous             std::string tag = "_";
1725bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1726bf648f77SEd Tanous             {
1727bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1728bf648f77SEd Tanous                 if (found == std::string::npos)
1729bf648f77SEd Tanous                 {
17301476687dSEd Tanous                     nlohmann::json::object_t iface;
1731ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
1732ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1733ef4c65b7SEd Tanous                         ifaceItem);
1734b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
1735bf648f77SEd Tanous                 }
1736bf648f77SEd Tanous             }
1737bf648f77SEd Tanous 
1738002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1739bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1740bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1741bf648f77SEd Tanous         });
1742bf648f77SEd Tanous         });
1743bf648f77SEd Tanous 
1744bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1745ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1746bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
174745ca1b86SEd Tanous             [&app](const crow::Request& req,
1748bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1749bf648f77SEd Tanous                    const std::string& ifaceId) {
17503ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
175145ca1b86SEd Tanous         {
175245ca1b86SEd Tanous             return;
175345ca1b86SEd Tanous         }
17544a0cb85cSEd Tanous         getEthernetIfaceData(
1755bf648f77SEd Tanous             ifaceId,
1756*77179532SEd Tanous             [asyncResp, ifaceId](const bool& success,
1757*77179532SEd Tanous                                  const EthernetInterfaceData& ethData,
1758*77179532SEd Tanous                                  const std::vector<IPv4AddressData>& ipv4Data,
1759*77179532SEd Tanous                                  const std::vector<IPv6AddressData>& ipv6Data) {
17604a0cb85cSEd Tanous             if (!success)
17611abe55efSEd Tanous             {
1762bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1763bf648f77SEd Tanous                 // existing object, and other errors
1764002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1765002d39b4SEd Tanous                                            ifaceId);
17664a0cb85cSEd Tanous                 return;
17679391bb9cSRapkiewicz, Pawel             }
17684c9afe43SEd Tanous 
1769188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1770188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
17710f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1772188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1773002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
17740f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
17750f74e643SEd Tanous                 "Management Network Interface";
17760f74e643SEd Tanous 
1777002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
17789391bb9cSRapkiewicz, Pawel             });
1779bf648f77SEd Tanous         });
17809391bb9cSRapkiewicz, Pawel 
1781bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1782ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1783bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
178445ca1b86SEd Tanous             [&app](const crow::Request& req,
1785bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1786bf648f77SEd Tanous                    const std::string& ifaceId) {
17873ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
178845ca1b86SEd Tanous         {
178945ca1b86SEd Tanous             return;
179045ca1b86SEd Tanous         }
1791bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1792ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1793d577665bSRatan Gupta         std::optional<std::string> macAddress;
17949a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1795d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1796e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1797f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1798da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
17991f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1800eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
180135fb5311STejas Patil         std::optional<size_t> mtuSize;
18021f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18031f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18040627a2c7SEd Tanous 
180515ed6780SWilly Tu         if (!json_util::readJsonPatch(
18068d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1807002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1808002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1809002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1810ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1811002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18121abe55efSEd Tanous         {
1813588c3f0dSKowalski, Kamil             return;
1814588c3f0dSKowalski, Kamil         }
1815da131a9aSJennifer Lee         if (dhcpv4)
1816da131a9aSJennifer Lee         {
1817002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
18181f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
181982695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
182082695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
182182695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
18221f8c7b5dSJohnathan Mantey             {
18231f8c7b5dSJohnathan Mantey                 return;
18241f8c7b5dSJohnathan Mantey             }
18251f8c7b5dSJohnathan Mantey         }
18261f8c7b5dSJohnathan Mantey 
18271f8c7b5dSJohnathan Mantey         if (dhcpv6)
18281f8c7b5dSJohnathan Mantey         {
1829002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1830002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1831002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1832002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1833002d39b4SEd Tanous                                      "UseDomainName",
183482695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
18351f8c7b5dSJohnathan Mantey             {
18361f8c7b5dSJohnathan Mantey                 return;
18371f8c7b5dSJohnathan Mantey             }
1838da131a9aSJennifer Lee         }
1839da131a9aSJennifer Lee 
1840bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1841bf648f77SEd Tanous         // for JSON preparation
18424a0cb85cSEd Tanous         getEthernetIfaceData(
18432c70f800SEd Tanous             ifaceId,
1844bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1845ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1846d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
18479a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1848e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
18491f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1850bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1851bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1852f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1853002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1854*77179532SEd Tanous                 const std::vector<IPv4AddressData>& ipv4Data,
1855*77179532SEd Tanous                 const std::vector<IPv6AddressData>& ipv6Data) {
18561abe55efSEd Tanous             if (!success)
18571abe55efSEd Tanous             {
1858588c3f0dSKowalski, Kamil                 // ... otherwise return error
1859bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1860bf648f77SEd Tanous                 // existing object, and other errors
1861002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1862002d39b4SEd Tanous                                            ifaceId);
1863588c3f0dSKowalski, Kamil                 return;
1864588c3f0dSKowalski, Kamil             }
1865588c3f0dSKowalski, Kamil 
18661f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
18671f8c7b5dSJohnathan Mantey             {
1868002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1869002d39b4SEd Tanous                                 asyncResp);
18701f8c7b5dSJohnathan Mantey             }
18711f8c7b5dSJohnathan Mantey 
18720627a2c7SEd Tanous             if (hostname)
18731abe55efSEd Tanous             {
18740627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
18751abe55efSEd Tanous             }
18760627a2c7SEd Tanous 
1877ab6554f1SJoshi-Mansi             if (fqdn)
1878ab6554f1SJoshi-Mansi             {
18792c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1880ab6554f1SJoshi-Mansi             }
1881ab6554f1SJoshi-Mansi 
1882d577665bSRatan Gupta             if (macAddress)
1883d577665bSRatan Gupta             {
1884002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1885d577665bSRatan Gupta             }
1886d577665bSRatan Gupta 
1887d1d50814SRavi Teja             if (ipv4StaticAddresses)
1888d1d50814SRavi Teja             {
1889bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1890bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1891bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1892bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1893bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1894bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1895bf648f77SEd Tanous                 // that, but could be done more efficiently
1896f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
1897002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
18981abe55efSEd Tanous             }
18990627a2c7SEd Tanous 
1900f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1901f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1902002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1903002d39b4SEd Tanous                                              asyncResp);
1904f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19059a6fc6feSRavi Teja 
19069a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19079a6fc6feSRavi Teja             {
19089a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19099a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19109a6fc6feSRavi Teja             }
1911e48c0fc5SRavi Teja 
1912e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1913e48c0fc5SRavi Teja             {
1914002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
1915002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
1916002d39b4SEd Tanous                                                asyncResp);
1917e48c0fc5SRavi Teja             }
1918eeedda23SJohnathan Mantey 
1919eeedda23SJohnathan Mantey             if (interfaceEnabled)
1920eeedda23SJohnathan Mantey             {
1921002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1922002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
1923eeedda23SJohnathan Mantey             }
192435fb5311STejas Patil 
192535fb5311STejas Patil             if (mtuSize)
192635fb5311STejas Patil             {
192735fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
192835fb5311STejas Patil             }
1929588c3f0dSKowalski, Kamil             });
1930bf648f77SEd Tanous         });
19319391bb9cSRapkiewicz, Pawel 
1932bf648f77SEd Tanous     BMCWEB_ROUTE(
1933bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
1934ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
1935bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
193645ca1b86SEd Tanous             [&app](const crow::Request& req,
1937bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
193845ca1b86SEd Tanous                    const std::string& parentIfaceId,
193945ca1b86SEd Tanous                    const std::string& ifaceId) {
19403ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194145ca1b86SEd Tanous         {
194245ca1b86SEd Tanous             return;
194345ca1b86SEd Tanous         }
19448d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
19450f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
19468d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
1947e439f0f8SKowalski, Kamil 
19482c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
19491abe55efSEd Tanous         {
1950a434f2bdSEd Tanous             return;
1951a434f2bdSEd Tanous         }
1952a434f2bdSEd Tanous 
1953bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1954bf648f77SEd Tanous         // for JSON preparation
1955*77179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
1956*77179532SEd Tanous                                           const bool& success,
1957*77179532SEd Tanous                                           const EthernetInterfaceData& ethData,
1958*77179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
1959*77179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
196017e22024SJiaqing Zhao             if (success && ethData.vlanId)
19611abe55efSEd Tanous             {
196222872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
1963ef4c65b7SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1964ef4c65b7SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1965ef4c65b7SEd Tanous                     parentIfaceId, ifaceId);
196622872ff3SJiaqing Zhao 
196723a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
196822872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
19691abe55efSEd Tanous             }
19701abe55efSEd Tanous             else
19711abe55efSEd Tanous             {
1972e439f0f8SKowalski, Kamil                 // ... otherwise return error
1973bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1974bf648f77SEd Tanous                 // existing object, and other errors
1975bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
1976d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
1977e439f0f8SKowalski, Kamil             }
1978e439f0f8SKowalski, Kamil         });
1979bf648f77SEd Tanous         });
1980e439f0f8SKowalski, Kamil 
1981bf648f77SEd Tanous     BMCWEB_ROUTE(
1982bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
19833d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
1984bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
198545ca1b86SEd Tanous             [&app](const crow::Request& req,
1986bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
198745ca1b86SEd Tanous                    const std::string& parentIfaceId,
198845ca1b86SEd Tanous                    const std::string& ifaceId) {
19893ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199045ca1b86SEd Tanous         {
199145ca1b86SEd Tanous             return;
199245ca1b86SEd Tanous         }
1993fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
19941abe55efSEd Tanous         {
1995d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
1996002d39b4SEd Tanous                                        ifaceId);
1997927a505aSKowalski, Kamil             return;
1998927a505aSKowalski, Kamil         }
1999927a505aSKowalski, Kamil 
20003927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20013927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20020627a2c7SEd Tanous 
200315ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2004bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20051abe55efSEd Tanous         {
2006927a505aSKowalski, Kamil             return;
2007927a505aSKowalski, Kamil         }
2008927a505aSKowalski, Kamil 
20093927e13eSJiaqing Zhao         if (vlanId)
20103927e13eSJiaqing Zhao         {
20113927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20123927e13eSJiaqing Zhao             return;
20133927e13eSJiaqing Zhao         }
20143927e13eSJiaqing Zhao 
2015bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2016bf648f77SEd Tanous         // for JSON preparation
2017*77179532SEd Tanous         getEthernetIfaceData(ifaceId,
2018*77179532SEd Tanous                              [asyncResp, parentIfaceId, ifaceId,
2019*77179532SEd Tanous                               vlanEnable](const bool& success,
2020*77179532SEd Tanous                                           const EthernetInterfaceData& ethData,
2021*77179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
2022*77179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
202317e22024SJiaqing Zhao             if (success && ethData.vlanId)
202408244d02SSunitha Harish             {
202523a06317SJiaqing Zhao                 if (vlanEnable)
202623a06317SJiaqing Zhao                 {
202723a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
20285e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
202908244d02SSunitha Harish                         if (ec)
203008244d02SSunitha Harish                         {
203108244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20323927e13eSJiaqing Zhao                             return;
203308244d02SSunitha Harish                         }
203423a06317SJiaqing Zhao                         },
203523a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
203623a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
203723a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
203823a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
203923a06317SJiaqing Zhao                         "NICEnabled",
204023a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
204108244d02SSunitha Harish                 }
204208244d02SSunitha Harish             }
204308244d02SSunitha Harish             else
20441abe55efSEd Tanous             {
2045bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2046bf648f77SEd Tanous                 // existing object, and other errors
2047bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2048d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2049bf648f77SEd Tanous                 return;
2050bf648f77SEd Tanous             }
2051bf648f77SEd Tanous         });
2052bf648f77SEd Tanous         });
2053bf648f77SEd Tanous 
2054bf648f77SEd Tanous     BMCWEB_ROUTE(
2055bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20563d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2057bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
205845ca1b86SEd Tanous             [&app](const crow::Request& req,
2059bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206045ca1b86SEd Tanous                    const std::string& parentIfaceId,
206145ca1b86SEd Tanous                    const std::string& ifaceId) {
20623ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
206345ca1b86SEd Tanous         {
206445ca1b86SEd Tanous             return;
206545ca1b86SEd Tanous         }
2066bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2067bf648f77SEd Tanous         {
2068d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2069002d39b4SEd Tanous                                        ifaceId);
2070927a505aSKowalski, Kamil             return;
2071927a505aSKowalski, Kamil         }
2072e439f0f8SKowalski, Kamil 
2073bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2074bf648f77SEd Tanous         // for JSON preparation
2075*77179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
2076*77179532SEd Tanous                                           const bool& success,
2077*77179532SEd Tanous                                           const EthernetInterfaceData& ethData,
2078*77179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
2079*77179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
208017e22024SJiaqing Zhao             if (success && ethData.vlanId)
20811abe55efSEd Tanous             {
2082f12894f8SJason M. Bills                 auto callback =
20835e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
20841abe55efSEd Tanous                     if (ec)
20851abe55efSEd Tanous                     {
2086f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2087927a505aSKowalski, Kamil                     }
20884a0cb85cSEd Tanous                 };
20894a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2090002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2091002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
20924a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
20931abe55efSEd Tanous             }
20941abe55efSEd Tanous             else
20951abe55efSEd Tanous             {
2096927a505aSKowalski, Kamil                 // ... otherwise return error
2097bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2098bf648f77SEd Tanous                 // existing object, and other errors
2099bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2100d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2101927a505aSKowalski, Kamil             }
2102927a505aSKowalski, Kamil         });
2103bf648f77SEd Tanous         });
2104e439f0f8SKowalski, Kamil 
2105bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2106bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2107ed398213SEd Tanous 
2108ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21091476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21101476687dSEd Tanous             [&app](const crow::Request& req,
2111bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2112bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21133ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
211445ca1b86SEd Tanous         {
211545ca1b86SEd Tanous             return;
211645ca1b86SEd Tanous         }
21174a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21181abe55efSEd Tanous         // preparation
2119*77179532SEd Tanous         getEthernetIfaceList([asyncResp, rootInterfaceName](
21201abe55efSEd Tanous                                  const bool& success,
2121*77179532SEd Tanous                                  const std::vector<std::string>& ifaceList) {
21224a0cb85cSEd Tanous             if (!success)
21231abe55efSEd Tanous             {
2124f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21254a0cb85cSEd Tanous                 return;
21261abe55efSEd Tanous             }
2127*77179532SEd Tanous             if (std::find(ifaceList.begin(), ifaceList.end(),
2128*77179532SEd Tanous                           rootInterfaceName) == ifaceList.end())
21294c9afe43SEd Tanous             {
2130002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2131002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21324c9afe43SEd Tanous                                            rootInterfaceName);
21334c9afe43SEd Tanous                 return;
21344c9afe43SEd Tanous             }
21354c9afe43SEd Tanous 
21360f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21370f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21380f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21390f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21400f74e643SEd Tanous                 "VLAN Network Interface Collection";
21414a0cb85cSEd Tanous 
21422c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21434a0cb85cSEd Tanous 
214481ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21451abe55efSEd Tanous             {
214611ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21474a0cb85cSEd Tanous                 {
21481476687dSEd Tanous                     nlohmann::json::object_t iface;
2149ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
2150ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2151ef4c65b7SEd Tanous                         rootInterfaceName, ifaceItem);
2152b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
2153e439f0f8SKowalski, Kamil                 }
2154e439f0f8SKowalski, Kamil             }
2155e439f0f8SKowalski, Kamil 
2156002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
21572c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2158ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2159ef4c65b7SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2160ef4c65b7SEd Tanous                 rootInterfaceName);
2161e439f0f8SKowalski, Kamil         });
2162bf648f77SEd Tanous         });
2163e439f0f8SKowalski, Kamil 
2164bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2165bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
21663d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2167bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
216845ca1b86SEd Tanous             [&app](const crow::Request& req,
2169bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2170bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21713ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
217245ca1b86SEd Tanous         {
217345ca1b86SEd Tanous             return;
217445ca1b86SEd Tanous         }
2175fda13ad2SSunitha Harish         bool vlanEnable = false;
21760627a2c7SEd Tanous         uint32_t vlanId = 0;
2177002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2178002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
21791abe55efSEd Tanous         {
21804a0cb85cSEd Tanous             return;
2181e439f0f8SKowalski, Kamil         }
2182fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2183dbb59d4dSEd Tanous         if (vlanId == 0U)
2184fda13ad2SSunitha Harish         {
2185fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2186fda13ad2SSunitha Harish         }
2187fda13ad2SSunitha Harish         if (!vlanEnable)
2188fda13ad2SSunitha Harish         {
2189fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2190fda13ad2SSunitha Harish         }
2191271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2192fda13ad2SSunitha Harish         {
2193fda13ad2SSunitha Harish             return;
2194fda13ad2SSunitha Harish         }
2195fda13ad2SSunitha Harish 
21965e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
21971abe55efSEd Tanous             if (ec)
21981abe55efSEd Tanous             {
2199bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2200bf648f77SEd Tanous                 // based on phosphor-network responses
2201f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22024a0cb85cSEd Tanous                 return;
22031abe55efSEd Tanous             }
2204f12894f8SJason M. Bills             messages::created(asyncResp->res);
2205e439f0f8SKowalski, Kamil         };
22064a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22074a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22084a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22094a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22100627a2c7SEd Tanous             rootInterfaceName, vlanId);
2211bf648f77SEd Tanous         });
22124a0cb85cSEd Tanous }
2213bf648f77SEd Tanous 
22149391bb9cSRapkiewicz, Pawel } // namespace redfish
2215