xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 4b242749b31b16c663fe2e95ad7dbc6a2f3ca973)
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"
232c5875a2SEd Tanous #include "human_sort.hpp"
243ccb3adbSEd Tanous #include "query.hpp"
253ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
26033f1e4dSEd Tanous #include "utils/ip_utils.hpp"
273ccb3adbSEd Tanous #include "utils/json_utils.hpp"
28033f1e4dSEd Tanous 
2911ba3979SEd Tanous #include <boost/algorithm/string/classification.hpp>
3011ba3979SEd Tanous #include <boost/algorithm/string/split.hpp>
31ef4c65b7SEd Tanous #include <boost/url/format.hpp>
321214b7e7SGunnar Mills 
337a1dbc48SGeorge Liu #include <array>
34a24526dcSEd Tanous #include <optional>
35ab6554f1SJoshi-Mansi #include <regex>
367a1dbc48SGeorge Liu #include <string_view>
3777179532SEd Tanous #include <vector>
389391bb9cSRapkiewicz, Pawel 
391abe55efSEd Tanous namespace redfish
401abe55efSEd Tanous {
419391bb9cSRapkiewicz, Pawel 
424a0cb85cSEd Tanous enum class LinkType
434a0cb85cSEd Tanous {
444a0cb85cSEd Tanous     Local,
454a0cb85cSEd Tanous     Global
464a0cb85cSEd Tanous };
479391bb9cSRapkiewicz, Pawel 
489391bb9cSRapkiewicz, Pawel /**
499391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
509391bb9cSRapkiewicz, Pawel  */
511abe55efSEd Tanous struct IPv4AddressData
521abe55efSEd Tanous {
53179db1d7SKowalski, Kamil     std::string id;
544a0cb85cSEd Tanous     std::string address;
554a0cb85cSEd Tanous     std::string domain;
564a0cb85cSEd Tanous     std::string gateway;
579391bb9cSRapkiewicz, Pawel     std::string netmask;
589391bb9cSRapkiewicz, Pawel     std::string origin;
5977179532SEd Tanous     LinkType linktype{};
6077179532SEd Tanous     bool isActive{};
619391bb9cSRapkiewicz, Pawel };
629391bb9cSRapkiewicz, Pawel 
639391bb9cSRapkiewicz, Pawel /**
64e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
65e48c0fc5SRavi Teja  */
66e48c0fc5SRavi Teja struct IPv6AddressData
67e48c0fc5SRavi Teja {
68e48c0fc5SRavi Teja     std::string id;
69e48c0fc5SRavi Teja     std::string address;
70e48c0fc5SRavi Teja     std::string origin;
7177179532SEd Tanous     uint8_t prefixLength = 0;
72e48c0fc5SRavi Teja };
73e48c0fc5SRavi Teja /**
749391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
759391bb9cSRapkiewicz, Pawel  * available from DBus
769391bb9cSRapkiewicz, Pawel  */
771abe55efSEd Tanous struct EthernetInterfaceData
781abe55efSEd Tanous {
794a0cb85cSEd Tanous     uint32_t speed;
8035fb5311STejas Patil     size_t mtuSize;
8182695a5bSJiaqing Zhao     bool autoNeg;
8282695a5bSJiaqing Zhao     bool dnsEnabled;
8382695a5bSJiaqing Zhao     bool ntpEnabled;
8482695a5bSJiaqing Zhao     bool hostNameEnabled;
85aa05fb27SJohnathan Mantey     bool linkUp;
86eeedda23SJohnathan Mantey     bool nicEnabled;
8782695a5bSJiaqing Zhao     std::string dhcpEnabled;
881f8c7b5dSJohnathan Mantey     std::string operatingMode;
8982695a5bSJiaqing Zhao     std::string hostName;
9082695a5bSJiaqing Zhao     std::string defaultGateway;
9182695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
9282695a5bSJiaqing Zhao     std::string macAddress;
9317e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
940f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
950f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
96d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
979391bb9cSRapkiewicz, Pawel };
989391bb9cSRapkiewicz, Pawel 
991f8c7b5dSJohnathan Mantey struct DHCPParameters
1001f8c7b5dSJohnathan Mantey {
1011f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
10282695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
10382695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
10482695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1051f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1061f8c7b5dSJohnathan Mantey };
1071f8c7b5dSJohnathan Mantey 
1089391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1099391bb9cSRapkiewicz, Pawel // into full dot notation
1101abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1111abe55efSEd Tanous {
1129391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1139391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1149391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1159391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1169391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1179391bb9cSRapkiewicz, Pawel     return netmask;
1189391bb9cSRapkiewicz, Pawel }
1199391bb9cSRapkiewicz, Pawel 
12082695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1211f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1221f8c7b5dSJohnathan Mantey {
1231f8c7b5dSJohnathan Mantey     if (isIPv4)
1241f8c7b5dSJohnathan Mantey     {
1251f8c7b5dSJohnathan Mantey         return (
1261f8c7b5dSJohnathan Mantey             (inputDHCP ==
1271f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1281f8c7b5dSJohnathan Mantey             (inputDHCP ==
1291f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1301f8c7b5dSJohnathan Mantey     }
1311f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1321f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1331f8c7b5dSJohnathan Mantey             (inputDHCP ==
1341f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1351f8c7b5dSJohnathan Mantey }
1361f8c7b5dSJohnathan Mantey 
1372c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1381f8c7b5dSJohnathan Mantey {
1391f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1401f8c7b5dSJohnathan Mantey     {
1411f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1421f8c7b5dSJohnathan Mantey     }
1433174e4dfSEd Tanous     if (isIPv4)
1441f8c7b5dSJohnathan Mantey     {
1451f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1461f8c7b5dSJohnathan Mantey     }
1473174e4dfSEd Tanous     if (isIPv6)
1481f8c7b5dSJohnathan Mantey     {
1491f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1501f8c7b5dSJohnathan Mantey     }
1511f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1521f8c7b5dSJohnathan Mantey }
1531f8c7b5dSJohnathan Mantey 
1544a0cb85cSEd Tanous inline std::string
1554a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1564a0cb85cSEd Tanous                                         bool isIPv4)
1571abe55efSEd Tanous {
1584a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1591abe55efSEd Tanous     {
1604a0cb85cSEd Tanous         return "Static";
1619391bb9cSRapkiewicz, Pawel     }
1624a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1631abe55efSEd Tanous     {
1644a0cb85cSEd Tanous         if (isIPv4)
1651abe55efSEd Tanous         {
1664a0cb85cSEd Tanous             return "IPv4LinkLocal";
1671abe55efSEd Tanous         }
1684a0cb85cSEd Tanous         return "LinkLocal";
1699391bb9cSRapkiewicz, Pawel     }
1704a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1711abe55efSEd Tanous     {
1724a0cb85cSEd Tanous         if (isIPv4)
1734a0cb85cSEd Tanous         {
1744a0cb85cSEd Tanous             return "DHCP";
1754a0cb85cSEd Tanous         }
1764a0cb85cSEd Tanous         return "DHCPv6";
1774a0cb85cSEd Tanous     }
1784a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1794a0cb85cSEd Tanous     {
1804a0cb85cSEd Tanous         return "SLAAC";
1814a0cb85cSEd Tanous     }
1824a0cb85cSEd Tanous     return "";
1834a0cb85cSEd Tanous }
1844a0cb85cSEd Tanous 
18502cad96eSEd Tanous inline bool extractEthernetInterfaceData(
18602cad96eSEd Tanous     const std::string& ethifaceId,
18702cad96eSEd Tanous     const dbus::utility::ManagedObjectType& dbusData,
1884a0cb85cSEd Tanous     EthernetInterfaceData& ethData)
1894a0cb85cSEd Tanous {
1904c9afe43SEd Tanous     bool idFound = false;
19102cad96eSEd Tanous     for (const auto& objpath : dbusData)
1924a0cb85cSEd Tanous     {
19302cad96eSEd Tanous         for (const auto& ifacePair : objpath.second)
1944a0cb85cSEd Tanous         {
19581ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
196029573d4SEd Tanous             {
1974c9afe43SEd Tanous                 idFound = true;
1984a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
1994a0cb85cSEd Tanous                 {
2004a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2014a0cb85cSEd Tanous                     {
2024a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2034a0cb85cSEd Tanous                         {
2044a0cb85cSEd Tanous                             const std::string* mac =
205abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2064a0cb85cSEd Tanous                             if (mac != nullptr)
2074a0cb85cSEd Tanous                             {
20882695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2094a0cb85cSEd Tanous                             }
2104a0cb85cSEd Tanous                         }
2114a0cb85cSEd Tanous                     }
2124a0cb85cSEd Tanous                 }
2134a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2144a0cb85cSEd Tanous                 {
2154a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2164a0cb85cSEd Tanous                     {
2174a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2184a0cb85cSEd Tanous                         {
2191b6b96c5SEd Tanous                             const uint32_t* id =
220abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2214a0cb85cSEd Tanous                             if (id != nullptr)
2224a0cb85cSEd Tanous                             {
22317e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2244a0cb85cSEd Tanous                             }
2254a0cb85cSEd Tanous                         }
2264a0cb85cSEd Tanous                     }
2274a0cb85cSEd Tanous                 }
2284a0cb85cSEd Tanous                 else if (ifacePair.first ==
2294a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2304a0cb85cSEd Tanous                 {
2314a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2324a0cb85cSEd Tanous                     {
2334a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2344a0cb85cSEd Tanous                         {
2352c70f800SEd Tanous                             const bool* autoNeg =
236abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2372c70f800SEd Tanous                             if (autoNeg != nullptr)
2384a0cb85cSEd Tanous                             {
23982695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2404a0cb85cSEd Tanous                             }
2414a0cb85cSEd Tanous                         }
2424a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2434a0cb85cSEd Tanous                         {
2444a0cb85cSEd Tanous                             const uint32_t* speed =
245abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2464a0cb85cSEd Tanous                             if (speed != nullptr)
2474a0cb85cSEd Tanous                             {
2484a0cb85cSEd Tanous                                 ethData.speed = *speed;
2494a0cb85cSEd Tanous                             }
2504a0cb85cSEd Tanous                         }
25135fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25235fb5311STejas Patil                         {
25335fb5311STejas Patil                             const uint32_t* mtuSize =
25435fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25535fb5311STejas Patil                             if (mtuSize != nullptr)
25635fb5311STejas Patil                             {
25735fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
25835fb5311STejas Patil                             }
25935fb5311STejas Patil                         }
260aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
261aa05fb27SJohnathan Mantey                         {
262aa05fb27SJohnathan Mantey                             const bool* linkUp =
263aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
264aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
265aa05fb27SJohnathan Mantey                             {
266aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
267aa05fb27SJohnathan Mantey                             }
268aa05fb27SJohnathan Mantey                         }
269eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
270eeedda23SJohnathan Mantey                         {
271eeedda23SJohnathan Mantey                             const bool* nicEnabled =
272eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
273eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
274eeedda23SJohnathan Mantey                             {
275eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
276eeedda23SJohnathan Mantey                             }
277eeedda23SJohnathan Mantey                         }
278f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
279029573d4SEd Tanous                         {
280029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2818d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
282029573d4SEd Tanous                                     &propertyPair.second);
283029573d4SEd Tanous                             if (nameservers != nullptr)
284029573d4SEd Tanous                             {
285f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2860f6efdc1Smanojkiran.eda@gmail.com                             }
2870f6efdc1Smanojkiran.eda@gmail.com                         }
2880f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2890f6efdc1Smanojkiran.eda@gmail.com                         {
2900f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2918d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2920f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2930f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2940f6efdc1Smanojkiran.eda@gmail.com                             {
295f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2964a0cb85cSEd Tanous                             }
2974a0cb85cSEd Tanous                         }
2982a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
2992a133282Smanojkiraneda                         {
3002c70f800SEd Tanous                             const std::string* dhcpEnabled =
3011f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3022c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3032a133282Smanojkiraneda                             {
30482695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3052a133282Smanojkiraneda                             }
3062a133282Smanojkiraneda                         }
307d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
308d24bfc7aSJennifer Lee                         {
309d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3108d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
311d24bfc7aSJennifer Lee                                     &propertyPair.second);
312d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
313d24bfc7aSJennifer Lee                             {
314f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
315d24bfc7aSJennifer Lee                             }
316d24bfc7aSJennifer Lee                         }
3179010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3189010ec2eSRavi Teja                         {
3199010ec2eSRavi Teja                             const std::string* defaultGateway =
3209010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3219010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3229010ec2eSRavi Teja                             {
3239010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3249010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3259010ec2eSRavi Teja                                 {
32682695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3279010ec2eSRavi Teja                                 }
3289010ec2eSRavi Teja                                 else
3299010ec2eSRavi Teja                                 {
33082695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3319010ec2eSRavi Teja                                 }
3329010ec2eSRavi Teja                             }
3339010ec2eSRavi Teja                         }
3349010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3359010ec2eSRavi Teja                         {
3369010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3379010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3389010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3399010ec2eSRavi Teja                             {
3409010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3419010ec2eSRavi Teja                                     *defaultGateway6;
3429010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3439010ec2eSRavi Teja                                 {
34482695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3459010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3469010ec2eSRavi Teja                                 }
3479010ec2eSRavi Teja                                 else
3489010ec2eSRavi Teja                                 {
34982695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3509010ec2eSRavi Teja                                         defaultGateway6Str;
3519010ec2eSRavi Teja                                 }
3529010ec2eSRavi Teja                             }
3539010ec2eSRavi Teja                         }
354029573d4SEd Tanous                     }
355029573d4SEd Tanous                 }
356029573d4SEd Tanous             }
3571f8c7b5dSJohnathan Mantey 
3581e3f85e6SJian Zhang             if (objpath.first == "/xyz/openbmc_project/network/dhcp")
3591f8c7b5dSJohnathan Mantey             {
3601f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3611f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3621f8c7b5dSJohnathan Mantey                 {
3631f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3641f8c7b5dSJohnathan Mantey                     {
3651f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3661f8c7b5dSJohnathan Mantey                         {
3672c70f800SEd Tanous                             const bool* dnsEnabled =
3681f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3692c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3701f8c7b5dSJohnathan Mantey                             {
37182695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3721f8c7b5dSJohnathan Mantey                             }
3731f8c7b5dSJohnathan Mantey                         }
3741f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3751f8c7b5dSJohnathan Mantey                         {
3762c70f800SEd Tanous                             const bool* ntpEnabled =
3771f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3782c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3791f8c7b5dSJohnathan Mantey                             {
38082695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3811f8c7b5dSJohnathan Mantey                             }
3821f8c7b5dSJohnathan Mantey                         }
3831f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3841f8c7b5dSJohnathan Mantey                         {
3852c70f800SEd Tanous                             const bool* hostNameEnabled =
3861f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3872c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3881f8c7b5dSJohnathan Mantey                             {
38982695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3901f8c7b5dSJohnathan Mantey                             }
3911f8c7b5dSJohnathan Mantey                         }
3921f8c7b5dSJohnathan Mantey                     }
3931f8c7b5dSJohnathan Mantey                 }
3941f8c7b5dSJohnathan Mantey             }
395029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
396029573d4SEd Tanous             // to check eth number
397029573d4SEd Tanous             if (ifacePair.first ==
3984a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3994a0cb85cSEd Tanous             {
4004a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4014a0cb85cSEd Tanous                 {
4024a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4034a0cb85cSEd Tanous                     {
4044a0cb85cSEd Tanous                         const std::string* hostname =
4058d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4064a0cb85cSEd Tanous                         if (hostname != nullptr)
4074a0cb85cSEd Tanous                         {
40882695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4094a0cb85cSEd Tanous                         }
4104a0cb85cSEd Tanous                     }
4114a0cb85cSEd Tanous                 }
4124a0cb85cSEd Tanous             }
4134a0cb85cSEd Tanous         }
4144a0cb85cSEd Tanous     }
4154c9afe43SEd Tanous     return idFound;
4164a0cb85cSEd Tanous }
4174a0cb85cSEd Tanous 
418e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
41977179532SEd Tanous inline void extractIPV6Data(const std::string& ethifaceId,
420711ac7a9SEd Tanous                             const dbus::utility::ManagedObjectType& dbusData,
42177179532SEd Tanous                             std::vector<IPv6AddressData>& ipv6Config)
422e48c0fc5SRavi Teja {
42389492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
42489492a15SPatrick Williams                                     ethifaceId;
425e48c0fc5SRavi Teja 
426e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
427e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
42881ce609eSEd Tanous     for (const auto& objpath : dbusData)
429e48c0fc5SRavi Teja     {
430e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
431353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
432e48c0fc5SRavi Teja         {
4339eb808c1SEd Tanous             for (const auto& interface : objpath.second)
434e48c0fc5SRavi Teja             {
435e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
436e48c0fc5SRavi Teja                 {
437353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
438353163e9STony Lee                                              interface.second.end(),
439353163e9STony Lee                                              [](const auto& property) {
440353163e9STony Lee                         return property.first == "Type";
441353163e9STony Lee                     });
442353163e9STony Lee                     if (type == interface.second.end())
443353163e9STony Lee                     {
444353163e9STony Lee                         continue;
445353163e9STony Lee                     }
446353163e9STony Lee 
447353163e9STony Lee                     const std::string* typeStr =
448353163e9STony Lee                         std::get_if<std::string>(&type->second);
449353163e9STony Lee 
450353163e9STony Lee                     if (typeStr == nullptr ||
451353163e9STony Lee                         (*typeStr !=
452353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv6"))
453353163e9STony Lee                     {
454353163e9STony Lee                         continue;
455353163e9STony Lee                     }
456353163e9STony Lee 
457e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
458e48c0fc5SRavi Teja                     // appropriate
45977179532SEd Tanous                     IPv6AddressData& ipv6Address = ipv6Config.emplace_back();
4602c70f800SEd Tanous                     ipv6Address.id =
461353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
4629eb808c1SEd Tanous                     for (const auto& property : interface.second)
463e48c0fc5SRavi Teja                     {
464e48c0fc5SRavi Teja                         if (property.first == "Address")
465e48c0fc5SRavi Teja                         {
466e48c0fc5SRavi Teja                             const std::string* address =
467e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
468e48c0fc5SRavi Teja                             if (address != nullptr)
469e48c0fc5SRavi Teja                             {
4702c70f800SEd Tanous                                 ipv6Address.address = *address;
471e48c0fc5SRavi Teja                             }
472e48c0fc5SRavi Teja                         }
473e48c0fc5SRavi Teja                         else if (property.first == "Origin")
474e48c0fc5SRavi Teja                         {
475e48c0fc5SRavi Teja                             const std::string* origin =
476e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
477e48c0fc5SRavi Teja                             if (origin != nullptr)
478e48c0fc5SRavi Teja                             {
4792c70f800SEd Tanous                                 ipv6Address.origin =
480e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
481e48c0fc5SRavi Teja                                                                         false);
482e48c0fc5SRavi Teja                             }
483e48c0fc5SRavi Teja                         }
484e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
485e48c0fc5SRavi Teja                         {
486e48c0fc5SRavi Teja                             const uint8_t* prefix =
487e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
488e48c0fc5SRavi Teja                             if (prefix != nullptr)
489e48c0fc5SRavi Teja                             {
4902c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
491e48c0fc5SRavi Teja                             }
492e48c0fc5SRavi Teja                         }
493889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
494889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
495889ff694SAsmitha Karunanithi                         {
496889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
497889ff694SAsmitha Karunanithi                         }
498e48c0fc5SRavi Teja                         else
499e48c0fc5SRavi Teja                         {
500e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
501e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
502e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
503e48c0fc5SRavi Teja                         }
504e48c0fc5SRavi Teja                     }
505e48c0fc5SRavi Teja                 }
506e48c0fc5SRavi Teja             }
507e48c0fc5SRavi Teja         }
508e48c0fc5SRavi Teja     }
509e48c0fc5SRavi Teja }
510e48c0fc5SRavi Teja 
5114a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
51277179532SEd Tanous inline void extractIPData(const std::string& ethifaceId,
513711ac7a9SEd Tanous                           const dbus::utility::ManagedObjectType& dbusData,
51477179532SEd Tanous                           std::vector<IPv4AddressData>& ipv4Config)
5154a0cb85cSEd Tanous {
51689492a15SPatrick Williams     const std::string ipPathStart = "/xyz/openbmc_project/network/" +
51789492a15SPatrick Williams                                     ethifaceId;
5184a0cb85cSEd Tanous 
5194a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5204a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
52181ce609eSEd Tanous     for (const auto& objpath : dbusData)
5224a0cb85cSEd Tanous     {
5234a0cb85cSEd Tanous         // Check if proper pattern for object path appears
524353163e9STony Lee         if (objpath.first.str.starts_with(ipPathStart + "/"))
5254a0cb85cSEd Tanous         {
5269eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5274a0cb85cSEd Tanous             {
5284a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5294a0cb85cSEd Tanous                 {
530353163e9STony Lee                     auto type = std::find_if(interface.second.begin(),
531353163e9STony Lee                                              interface.second.end(),
532353163e9STony Lee                                              [](const auto& property) {
533353163e9STony Lee                         return property.first == "Type";
534353163e9STony Lee                     });
535353163e9STony Lee                     if (type == interface.second.end())
536353163e9STony Lee                     {
537353163e9STony Lee                         continue;
538353163e9STony Lee                     }
539353163e9STony Lee 
540353163e9STony Lee                     const std::string* typeStr =
541353163e9STony Lee                         std::get_if<std::string>(&type->second);
542353163e9STony Lee 
543353163e9STony Lee                     if (typeStr == nullptr ||
544353163e9STony Lee                         (*typeStr !=
545353163e9STony Lee                          "xyz.openbmc_project.Network.IP.Protocol.IPv4"))
546353163e9STony Lee                     {
547353163e9STony Lee                         continue;
548353163e9STony Lee                     }
549353163e9STony Lee 
5504a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5514a0cb85cSEd Tanous                     // appropriate
55277179532SEd Tanous                     IPv4AddressData& ipv4Address = ipv4Config.emplace_back();
5532c70f800SEd Tanous                     ipv4Address.id =
554353163e9STony Lee                         objpath.first.str.substr(ipPathStart.size());
5559eb808c1SEd Tanous                     for (const auto& property : interface.second)
5564a0cb85cSEd Tanous                     {
5574a0cb85cSEd Tanous                         if (property.first == "Address")
5584a0cb85cSEd Tanous                         {
5594a0cb85cSEd Tanous                             const std::string* address =
560abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5614a0cb85cSEd Tanous                             if (address != nullptr)
5624a0cb85cSEd Tanous                             {
5632c70f800SEd Tanous                                 ipv4Address.address = *address;
5644a0cb85cSEd Tanous                             }
5654a0cb85cSEd Tanous                         }
5664a0cb85cSEd Tanous                         else if (property.first == "Origin")
5674a0cb85cSEd Tanous                         {
5684a0cb85cSEd Tanous                             const std::string* origin =
569abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5704a0cb85cSEd Tanous                             if (origin != nullptr)
5714a0cb85cSEd Tanous                             {
5722c70f800SEd Tanous                                 ipv4Address.origin =
5734a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5744a0cb85cSEd Tanous                                                                         true);
5754a0cb85cSEd Tanous                             }
5764a0cb85cSEd Tanous                         }
5774a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5784a0cb85cSEd Tanous                         {
5794a0cb85cSEd Tanous                             const uint8_t* mask =
580abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5814a0cb85cSEd Tanous                             if (mask != nullptr)
5824a0cb85cSEd Tanous                             {
5834a0cb85cSEd Tanous                                 // convert it to the string
5842c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5854a0cb85cSEd Tanous                             }
5864a0cb85cSEd Tanous                         }
587889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
588889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
589889ff694SAsmitha Karunanithi                         {
590889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
591889ff694SAsmitha Karunanithi                         }
5924a0cb85cSEd Tanous                         else
5934a0cb85cSEd Tanous                         {
5944a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5954a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5964a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5974a0cb85cSEd Tanous                         }
5984a0cb85cSEd Tanous                     }
5994a0cb85cSEd Tanous                     // Check if given address is local, or global
6002c70f800SEd Tanous                     ipv4Address.linktype =
60111ba3979SEd Tanous                         ipv4Address.address.starts_with("169.254.")
60218659d10SJohnathan Mantey                             ? LinkType::Local
60318659d10SJohnathan Mantey                             : LinkType::Global;
6044a0cb85cSEd Tanous                 }
6054a0cb85cSEd Tanous             }
6064a0cb85cSEd Tanous         }
6074a0cb85cSEd Tanous     }
6084a0cb85cSEd Tanous }
609588c3f0dSKowalski, Kamil 
610588c3f0dSKowalski, Kamil /**
61101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
612179db1d7SKowalski, Kamil  *
613179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
614179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
615179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
616179db1d7SKowalski, Kamil  *
617179db1d7SKowalski, Kamil  * @return None
618179db1d7SKowalski, Kamil  */
6199c5e585cSRavi Teja inline void deleteIPAddress(const std::string& ifaceId,
6209c5e585cSRavi Teja                             const std::string& ipHash,
6218d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6221abe55efSEd Tanous {
62355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6245e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6251abe55efSEd Tanous         if (ec)
6261abe55efSEd Tanous         {
627a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6281abe55efSEd Tanous         }
629179db1d7SKowalski, Kamil         },
630179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
6319c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + ipHash,
632179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
633179db1d7SKowalski, Kamil }
634179db1d7SKowalski, Kamil 
635244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
636244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
637244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6389010ec2eSRavi Teja {
6399010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6405e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
6419010ec2eSRavi Teja         if (ec)
6429010ec2eSRavi Teja         {
6439010ec2eSRavi Teja             messages::internalError(asyncResp->res);
6449010ec2eSRavi Teja             return;
6459010ec2eSRavi Teja         }
6469010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
6479010ec2eSRavi Teja         },
6489010ec2eSRavi Teja         "xyz.openbmc_project.Network",
6499010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
6509010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
6519010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
652168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
6539010ec2eSRavi Teja }
654179db1d7SKowalski, Kamil /**
65501784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
656179db1d7SKowalski, Kamil  *
65701784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
65801784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
65901784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
66001784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
661179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
662179db1d7SKowalski, Kamil  *
663179db1d7SKowalski, Kamil  * @return None
664179db1d7SKowalski, Kamil  */
665cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
666cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
6678d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6681abe55efSEd Tanous {
669002d39b4SEd Tanous     auto createIpHandler =
6705e7e2dc5SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code& ec) {
6711abe55efSEd Tanous         if (ec)
6721abe55efSEd Tanous         {
673a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6749010ec2eSRavi Teja             return;
675179db1d7SKowalski, Kamil         }
6769010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
6779010ec2eSRavi Teja     };
6789010ec2eSRavi Teja 
6799010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
6809010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
681179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
682179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
68301784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
684179db1d7SKowalski, Kamil         gateway);
685179db1d7SKowalski, Kamil }
686e48c0fc5SRavi Teja 
687e48c0fc5SRavi Teja /**
68801784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
68901784826SJohnathan Mantey  * static IPv6 entry
69001784826SJohnathan Mantey  *
69101784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
69201784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
69301784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
69401784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
69501784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
69601784826SJohnathan Mantey  *
69701784826SJohnathan Mantey  * @return None
69801784826SJohnathan Mantey  */
6999c5e585cSRavi Teja 
7009c5e585cSRavi Teja enum class IpVersion
7019c5e585cSRavi Teja {
7029c5e585cSRavi Teja     IpV4,
7039c5e585cSRavi Teja     IpV6
7049c5e585cSRavi Teja };
7059c5e585cSRavi Teja 
7069c5e585cSRavi Teja inline void deleteAndCreateIPAddress(
7079c5e585cSRavi Teja     IpVersion version, const std::string& ifaceId, const std::string& id,
7088d1b46d7Szhanghch05     uint8_t prefixLength, const std::string& address,
7099c5e585cSRavi Teja     const std::string& gateway,
7108d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
71101784826SJohnathan Mantey {
71201784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
7139c5e585cSRavi Teja         [asyncResp, version, ifaceId, address, prefixLength,
7149c5e585cSRavi Teja          gateway](const boost::system::error_code& ec) {
71501784826SJohnathan Mantey         if (ec)
71601784826SJohnathan Mantey         {
71701784826SJohnathan Mantey             messages::internalError(asyncResp->res);
71801784826SJohnathan Mantey         }
7199c5e585cSRavi Teja         std::string protocol = "xyz.openbmc_project.Network.IP.Protocol.";
7209c5e585cSRavi Teja         protocol += version == IpVersion::IpV4 ? "IPv4" : "IPv6";
72101784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
7225e7e2dc5SEd Tanous             [asyncResp](const boost::system::error_code& ec2) {
72323a21a1cSEd Tanous             if (ec2)
72401784826SJohnathan Mantey             {
72501784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
72601784826SJohnathan Mantey             }
72701784826SJohnathan Mantey             },
72801784826SJohnathan Mantey             "xyz.openbmc_project.Network",
72901784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
7309c5e585cSRavi Teja             "xyz.openbmc_project.Network.IP.Create", "IP", protocol, address,
7319c5e585cSRavi Teja             prefixLength, gateway);
73201784826SJohnathan Mantey         },
73301784826SJohnathan Mantey         "xyz.openbmc_project.Network",
7349c5e585cSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + id,
73501784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
73601784826SJohnathan Mantey }
73701784826SJohnathan Mantey 
73801784826SJohnathan Mantey /**
739e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
740e48c0fc5SRavi Teja  *
741e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
742e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
743e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
744e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
745e48c0fc5SRavi Teja  *
746e48c0fc5SRavi Teja  * @return None
747e48c0fc5SRavi Teja  */
74801784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
74901784826SJohnathan Mantey                        const std::string& address,
7508d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
751e48c0fc5SRavi Teja {
7525e7e2dc5SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code& ec) {
753e48c0fc5SRavi Teja         if (ec)
754e48c0fc5SRavi Teja         {
755e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
756e48c0fc5SRavi Teja         }
757e48c0fc5SRavi Teja     };
758e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
7594e0453b1SGunnar Mills     // does not have associated gateway property
760e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
761e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
762e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
763e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
764e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
765e48c0fc5SRavi Teja         "");
766e48c0fc5SRavi Teja }
767e48c0fc5SRavi Teja 
768179db1d7SKowalski, Kamil /**
769179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
770179db1d7SKowalski, Kamil  * Object
771179db1d7SKowalski, Kamil  * from EntityManager Network Manager
7724a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
773179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
774179db1d7SKowalski, Kamil  * into JSON
775179db1d7SKowalski, Kamil  */
776179db1d7SKowalski, Kamil template <typename CallbackFunc>
77781ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
7781abe55efSEd Tanous                           CallbackFunc&& callback)
7791abe55efSEd Tanous {
780f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
781f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
782f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
783f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
784f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
7855e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
78602cad96eSEd Tanous             const dbus::utility::ManagedObjectType& resp) {
78755c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
78877179532SEd Tanous         std::vector<IPv4AddressData> ipv4Data;
78977179532SEd Tanous         std::vector<IPv6AddressData> ipv6Data;
790179db1d7SKowalski, Kamil 
79181ce609eSEd Tanous         if (errorCode)
7921abe55efSEd Tanous         {
79301784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
794179db1d7SKowalski, Kamil             return;
795179db1d7SKowalski, Kamil         }
796179db1d7SKowalski, Kamil 
797002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
7984c9afe43SEd Tanous         if (!found)
7994c9afe43SEd Tanous         {
80001784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
8014c9afe43SEd Tanous             return;
8024c9afe43SEd Tanous         }
8034c9afe43SEd Tanous 
8042c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
805179db1d7SKowalski, Kamil         // Fix global GW
8061abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
8071abe55efSEd Tanous         {
808c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
809c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
8109010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
8111abe55efSEd Tanous             {
81282695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
813179db1d7SKowalski, Kamil             }
814179db1d7SKowalski, Kamil         }
815179db1d7SKowalski, Kamil 
8162c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
8174e0453b1SGunnar Mills         // Finally make a callback with useful data
81801784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
819f5892d0dSGeorge Liu         });
820271584abSEd Tanous }
821179db1d7SKowalski, Kamil 
822179db1d7SKowalski, Kamil /**
8239391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
8249391bb9cSRapkiewicz, Pawel  * Manager
8251abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
8261abe55efSEd Tanous  * into JSON.
8279391bb9cSRapkiewicz, Pawel  */
8289391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
8291abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
8301abe55efSEd Tanous {
831f5892d0dSGeorge Liu     sdbusplus::message::object_path path("/xyz/openbmc_project/network");
832f5892d0dSGeorge Liu     dbus::utility::getManagedObjects(
833f5892d0dSGeorge Liu         "xyz.openbmc_project.Network", path,
834f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
8355e7e2dc5SEd Tanous             const boost::system::error_code& errorCode,
836f5892d0dSGeorge Liu             const dbus::utility::ManagedObjectType& resp) {
8371abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
8381abe55efSEd Tanous         // ethernet interfaces
83977179532SEd Tanous         std::vector<std::string> ifaceList;
8402c70f800SEd Tanous         ifaceList.reserve(resp.size());
84181ce609eSEd Tanous         if (errorCode)
8421abe55efSEd Tanous         {
8432c70f800SEd Tanous             callback(false, ifaceList);
8449391bb9cSRapkiewicz, Pawel             return;
8459391bb9cSRapkiewicz, Pawel         }
8469391bb9cSRapkiewicz, Pawel 
8479391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
8484a0cb85cSEd Tanous         for (const auto& objpath : resp)
8491abe55efSEd Tanous         {
8509391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
8514a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
8521abe55efSEd Tanous             {
8531abe55efSEd Tanous                 // If interface is
8544a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
8554a0cb85cSEd Tanous                 // what we're looking for.
8569391bb9cSRapkiewicz, Pawel                 if (interface.first ==
8571abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
8581abe55efSEd Tanous                 {
8592dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
8602dfd18efSEd Tanous                     if (ifaceId.empty())
8611abe55efSEd Tanous                     {
8622dfd18efSEd Tanous                         continue;
8639391bb9cSRapkiewicz, Pawel                     }
8642dfd18efSEd Tanous                     // and put it into output vector.
86577179532SEd Tanous                     ifaceList.emplace_back(ifaceId);
8669391bb9cSRapkiewicz, Pawel                 }
8679391bb9cSRapkiewicz, Pawel             }
8689391bb9cSRapkiewicz, Pawel         }
8692c5875a2SEd Tanous 
8702c5875a2SEd Tanous         std::sort(ifaceList.begin(), ifaceList.end(),
8712c5875a2SEd Tanous                   AlphanumLess<std::string>());
8722c5875a2SEd Tanous 
873a434f2bdSEd Tanous         // Finally make a callback with useful data
8742c70f800SEd Tanous         callback(true, ifaceList);
875f5892d0dSGeorge Liu         });
876271584abSEd Tanous }
8779391bb9cSRapkiewicz, Pawel 
8784f48d5f6SEd Tanous inline void
8794f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
8808d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
8811abe55efSEd Tanous {
882ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
883ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
884ab6554f1SJoshi-Mansi     {
885ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
886ab6554f1SJoshi-Mansi                                            "HostName");
887ab6554f1SJoshi-Mansi         return;
888ab6554f1SJoshi-Mansi     }
889bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
8905e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
8914a0cb85cSEd Tanous         if (ec)
8924a0cb85cSEd Tanous         {
893a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
8941abe55efSEd Tanous         }
895bc0bd6e0SEd Tanous         },
896bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
897bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
898bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
899168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
900588c3f0dSKowalski, Kamil }
901588c3f0dSKowalski, Kamil 
9024f48d5f6SEd Tanous inline void
90335fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
90435fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
90535fb5311STejas Patil {
90689492a15SPatrick Williams     sdbusplus::message::object_path objPath = "/xyz/openbmc_project/network/" +
90789492a15SPatrick Williams                                               ifaceId;
90835fb5311STejas Patil     crow::connections::systemBus->async_method_call(
9095e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
91035fb5311STejas Patil         if (ec)
91135fb5311STejas Patil         {
91235fb5311STejas Patil             messages::internalError(asyncResp->res);
91335fb5311STejas Patil         }
91435fb5311STejas Patil         },
91535fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
91635fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
91735fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
91835fb5311STejas Patil         std::variant<size_t>(mtuSize));
91935fb5311STejas Patil }
92035fb5311STejas Patil 
92135fb5311STejas Patil inline void
9224f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
923bf648f77SEd Tanous                           const std::string& domainname,
9248d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
925ab6554f1SJoshi-Mansi {
926ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
927ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
9285e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
929ab6554f1SJoshi-Mansi         if (ec)
930ab6554f1SJoshi-Mansi         {
931ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
932ab6554f1SJoshi-Mansi         }
933ab6554f1SJoshi-Mansi         },
934ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
935ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
936ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
937ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
938168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
939ab6554f1SJoshi-Mansi }
940ab6554f1SJoshi-Mansi 
9414f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
942bf648f77SEd Tanous {
943bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
944bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
945bf648f77SEd Tanous     {
946bf648f77SEd Tanous         return false;
947bf648f77SEd Tanous     }
948bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
949bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
950bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
951bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
952*4b242749SEd Tanous     const static std::regex pattern(
953bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
954bf648f77SEd Tanous 
955bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
956bf648f77SEd Tanous }
957bf648f77SEd Tanous 
9584f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
959bf648f77SEd Tanous {
960bf648f77SEd Tanous     // Can have multiple subdomains
961bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
962*4b242749SEd Tanous     const static std::regex pattern(
9630fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
964bf648f77SEd Tanous 
965bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
966bf648f77SEd Tanous }
967bf648f77SEd Tanous 
9684f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
9698d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
970ab6554f1SJoshi-Mansi {
971ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
972ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
973ab6554f1SJoshi-Mansi     {
974ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
975ab6554f1SJoshi-Mansi         return;
976ab6554f1SJoshi-Mansi     }
977ab6554f1SJoshi-Mansi 
978ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
979ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
980ab6554f1SJoshi-Mansi     {
981ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
982ab6554f1SJoshi-Mansi         return;
983ab6554f1SJoshi-Mansi     }
984ab6554f1SJoshi-Mansi 
985ab6554f1SJoshi-Mansi     std::string hostname;
986ab6554f1SJoshi-Mansi     std::string domainname;
987ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
988ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
989ab6554f1SJoshi-Mansi 
990ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
991ab6554f1SJoshi-Mansi     {
992ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
993ab6554f1SJoshi-Mansi         return;
994ab6554f1SJoshi-Mansi     }
995ab6554f1SJoshi-Mansi 
996ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
997ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
998ab6554f1SJoshi-Mansi }
999ab6554f1SJoshi-Mansi 
10004f48d5f6SEd Tanous inline void
10014f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1002bf648f77SEd Tanous                           const std::string& macAddress,
10038d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1004d577665bSRatan Gupta {
100558283f41SJohnathan Mantey     static constexpr std::string_view dbusNotAllowedError =
100658283f41SJohnathan Mantey         "xyz.openbmc_project.Common.Error.NotAllowed";
100758283f41SJohnathan Mantey 
1008d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
10095e7e2dc5SEd Tanous         [asyncResp, macAddress](const boost::system::error_code& ec,
10105b378546SPatrick Williams                                 const sdbusplus::message_t& msg) {
1011d577665bSRatan Gupta         if (ec)
1012d577665bSRatan Gupta         {
101358283f41SJohnathan Mantey             const sd_bus_error* err = msg.get_error();
101458283f41SJohnathan Mantey             if (err == nullptr)
101558283f41SJohnathan Mantey             {
101658283f41SJohnathan Mantey                 messages::internalError(asyncResp->res);
101758283f41SJohnathan Mantey                 return;
101858283f41SJohnathan Mantey             }
101958283f41SJohnathan Mantey             if (err->name == dbusNotAllowedError)
102058283f41SJohnathan Mantey             {
102158283f41SJohnathan Mantey                 messages::propertyNotWritable(asyncResp->res, "MACAddress");
102258283f41SJohnathan Mantey                 return;
102358283f41SJohnathan Mantey             }
1024d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1025d577665bSRatan Gupta             return;
1026d577665bSRatan Gupta         }
1027d577665bSRatan Gupta         },
1028d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1029d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1030d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1031d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1032168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1033d577665bSRatan Gupta }
1034286b9118SJohnathan Mantey 
10354f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
10364f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
10374f48d5f6SEd Tanous                            const bool v6Value,
10388d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1039da131a9aSJennifer Lee {
10402c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1041da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10425e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1043da131a9aSJennifer Lee         if (ec)
1044da131a9aSJennifer Lee         {
1045da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1046da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1047da131a9aSJennifer Lee             return;
1048da131a9aSJennifer Lee         }
10498f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1050da131a9aSJennifer Lee         },
1051da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1052da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1053da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1054da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1055168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1056da131a9aSJennifer Lee }
10571f8c7b5dSJohnathan Mantey 
10584f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1059eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
10608d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1061eeedda23SJohnathan Mantey {
1062eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
10635e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1064eeedda23SJohnathan Mantey         if (ec)
1065eeedda23SJohnathan Mantey         {
1066eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1067eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1068eeedda23SJohnathan Mantey             return;
1069eeedda23SJohnathan Mantey         }
1070eeedda23SJohnathan Mantey         },
1071eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1072eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1073eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1074eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1075168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1076eeedda23SJohnathan Mantey }
1077eeedda23SJohnathan Mantey 
10784f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
10798d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1080da131a9aSJennifer Lee {
1081da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1082da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
10835e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1084da131a9aSJennifer Lee         if (ec)
1085da131a9aSJennifer Lee         {
1086da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1087da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1088da131a9aSJennifer Lee             return;
1089da131a9aSJennifer Lee         }
1090da131a9aSJennifer Lee         },
10911e3f85e6SJian Zhang         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/dhcp",
1092da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1093da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1094168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1095da131a9aSJennifer Lee }
1096d577665bSRatan Gupta 
10974f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
10981f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1099f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1100f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
11018d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1102da131a9aSJennifer Lee {
110382695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
110482695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1105da131a9aSJennifer Lee 
11061f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
11071f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
11081f8c7b5dSJohnathan Mantey 
11091f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
11101f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1111da131a9aSJennifer Lee     {
11121f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
11131f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
11141f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
11151f8c7b5dSJohnathan Mantey         {
1116bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1117bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
11181f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1119da131a9aSJennifer Lee             return;
1120da131a9aSJennifer Lee         }
11211f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
11221f8c7b5dSJohnathan Mantey     }
11231f8c7b5dSJohnathan Mantey     else
1124da131a9aSJennifer Lee     {
11251f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
11261f8c7b5dSJohnathan Mantey     }
11271f8c7b5dSJohnathan Mantey 
11281f8c7b5dSJohnathan Mantey     bool nextDNS{};
112982695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
11301f8c7b5dSJohnathan Mantey     {
113182695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
11321f8c7b5dSJohnathan Mantey         {
11331f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11341f8c7b5dSJohnathan Mantey             return;
11351f8c7b5dSJohnathan Mantey         }
113682695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11371f8c7b5dSJohnathan Mantey     }
113882695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
11391f8c7b5dSJohnathan Mantey     {
114082695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
11411f8c7b5dSJohnathan Mantey     }
114282695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
11431f8c7b5dSJohnathan Mantey     {
114482695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
11451f8c7b5dSJohnathan Mantey     }
11461f8c7b5dSJohnathan Mantey     else
11471f8c7b5dSJohnathan Mantey     {
114882695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
11491f8c7b5dSJohnathan Mantey     }
11501f8c7b5dSJohnathan Mantey 
11511f8c7b5dSJohnathan Mantey     bool nextNTP{};
115282695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
11531f8c7b5dSJohnathan Mantey     {
115482695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
11551f8c7b5dSJohnathan Mantey         {
11561f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11571f8c7b5dSJohnathan Mantey             return;
11581f8c7b5dSJohnathan Mantey         }
115982695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11601f8c7b5dSJohnathan Mantey     }
116182695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
11621f8c7b5dSJohnathan Mantey     {
116382695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
11641f8c7b5dSJohnathan Mantey     }
116582695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
11661f8c7b5dSJohnathan Mantey     {
116782695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
11681f8c7b5dSJohnathan Mantey     }
11691f8c7b5dSJohnathan Mantey     else
11701f8c7b5dSJohnathan Mantey     {
117182695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
11721f8c7b5dSJohnathan Mantey     }
11731f8c7b5dSJohnathan Mantey 
11741f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
117582695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
11761f8c7b5dSJohnathan Mantey     {
117782695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
11781f8c7b5dSJohnathan Mantey         {
11791f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
11801f8c7b5dSJohnathan Mantey             return;
11811f8c7b5dSJohnathan Mantey         }
118282695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11831f8c7b5dSJohnathan Mantey     }
118482695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
11851f8c7b5dSJohnathan Mantey     {
118682695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
11871f8c7b5dSJohnathan Mantey     }
118882695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
11891f8c7b5dSJohnathan Mantey     {
119082695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
11911f8c7b5dSJohnathan Mantey     }
11921f8c7b5dSJohnathan Mantey     else
11931f8c7b5dSJohnathan Mantey     {
119482695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
11951f8c7b5dSJohnathan Mantey     }
11961f8c7b5dSJohnathan Mantey 
1197da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
11981f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
11991f8c7b5dSJohnathan Mantey                    asyncResp);
1200da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
12011f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1202da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
12031f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
12041f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
12051f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1206da131a9aSJennifer Lee }
120701784826SJohnathan Mantey 
120877179532SEd Tanous inline std::vector<IPv4AddressData>::const_iterator getNextStaticIpEntry(
120977179532SEd Tanous     const std::vector<IPv4AddressData>::const_iterator& head,
121077179532SEd Tanous     const std::vector<IPv4AddressData>::const_iterator& end)
121101784826SJohnathan Mantey {
121217a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
121317a897dfSManojkiran Eda         return value.origin == "Static";
121417a897dfSManojkiran Eda     });
121501784826SJohnathan Mantey }
121601784826SJohnathan Mantey 
121777179532SEd Tanous inline std::vector<IPv6AddressData>::const_iterator getNextStaticIpEntry(
121877179532SEd Tanous     const std::vector<IPv6AddressData>::const_iterator& head,
121977179532SEd Tanous     const std::vector<IPv6AddressData>::const_iterator& end)
122001784826SJohnathan Mantey {
122117a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
122217a897dfSManojkiran Eda         return value.origin == "Static";
122317a897dfSManojkiran Eda     });
122401784826SJohnathan Mantey }
122501784826SJohnathan Mantey 
122677179532SEd Tanous inline void
1227ddd70dcaSEd Tanous     handleIPv4StaticPatch(const std::string& ifaceId,
1228ddd70dcaSEd Tanous                           nlohmann::json::array_t& input,
122977179532SEd Tanous                           const std::vector<IPv4AddressData>& ipv4Data,
12308d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
12311abe55efSEd Tanous {
1232ddd70dcaSEd Tanous     if (input.empty())
1233f476acbfSRatan Gupta     {
123471f52d96SEd Tanous         messages::propertyValueTypeError(
123571f52d96SEd Tanous             asyncResp->res,
1236ddd70dcaSEd Tanous             nlohmann::json(input).dump(
1237ddd70dcaSEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace),
1238d1d50814SRavi Teja             "IPv4StaticAddresses");
1239f476acbfSRatan Gupta         return;
1240f476acbfSRatan Gupta     }
1241f476acbfSRatan Gupta 
1242271584abSEd Tanous     unsigned entryIdx = 1;
124301784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
124401784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
124501784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
124601784826SJohnathan Mantey     // into the NIC.
124777179532SEd Tanous     std::vector<IPv4AddressData>::const_iterator nicIpEntry =
12482c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
124901784826SJohnathan Mantey 
1250537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
12511abe55efSEd Tanous     {
125289492a15SPatrick Williams         std::string pathString = "IPv4StaticAddresses/" +
125389492a15SPatrick Williams                                  std::to_string(entryIdx);
1254179db1d7SKowalski, Kamil 
125501784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1256f476acbfSRatan Gupta         {
1257537174c4SEd Tanous             std::optional<std::string> address;
1258537174c4SEd Tanous             std::optional<std::string> subnetMask;
1259537174c4SEd Tanous             std::optional<std::string> gateway;
1260537174c4SEd Tanous 
1261537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
12627e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
12637e27d832SJohnathan Mantey                                      "Gateway", gateway))
1264537174c4SEd Tanous             {
126501784826SJohnathan Mantey                 messages::propertyValueFormatError(
126671f52d96SEd Tanous                     asyncResp->res,
126771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
126871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
126971f52d96SEd Tanous                     pathString);
1270537174c4SEd Tanous                 return;
1271179db1d7SKowalski, Kamil             }
1272179db1d7SKowalski, Kamil 
127301784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
127401784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
127501784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
127601784826SJohnathan Mantey             // current request.
1277271584abSEd Tanous             const std::string* addr = nullptr;
1278271584abSEd Tanous             const std::string* gw = nullptr;
127901784826SJohnathan Mantey             uint8_t prefixLength = 0;
128001784826SJohnathan Mantey             bool errorInEntry = false;
1281537174c4SEd Tanous             if (address)
12821abe55efSEd Tanous             {
1283033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*address))
12841abe55efSEd Tanous                 {
128501784826SJohnathan Mantey                     addr = &(*address);
12864a0cb85cSEd Tanous                 }
128701784826SJohnathan Mantey                 else
128801784826SJohnathan Mantey                 {
1289bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1290bf648f77SEd Tanous                                                        pathString + "/Address");
129101784826SJohnathan Mantey                     errorInEntry = true;
129201784826SJohnathan Mantey                 }
129301784826SJohnathan Mantey             }
129485ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
129501784826SJohnathan Mantey             {
129685ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
129701784826SJohnathan Mantey             }
129801784826SJohnathan Mantey             else
129901784826SJohnathan Mantey             {
130001784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
130101784826SJohnathan Mantey                                           pathString + "/Address");
130201784826SJohnathan Mantey                 errorInEntry = true;
13034a0cb85cSEd Tanous             }
13044a0cb85cSEd Tanous 
1305537174c4SEd Tanous             if (subnetMask)
13064a0cb85cSEd Tanous             {
1307033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(*subnetMask,
1308033f1e4dSEd Tanous                                                          &prefixLength))
13094a0cb85cSEd Tanous                 {
1310f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1311537174c4SEd Tanous                         asyncResp->res, *subnetMask,
13124a0cb85cSEd Tanous                         pathString + "/SubnetMask");
131301784826SJohnathan Mantey                     errorInEntry = true;
13144a0cb85cSEd Tanous                 }
13154a0cb85cSEd Tanous             }
131685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
13174a0cb85cSEd Tanous             {
1318033f1e4dSEd Tanous                 if (!ip_util::ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
131901784826SJohnathan Mantey                                                          &prefixLength))
13204a0cb85cSEd Tanous                 {
132101784826SJohnathan Mantey                     messages::propertyValueFormatError(
132285ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
132301784826SJohnathan Mantey                         pathString + "/SubnetMask");
132401784826SJohnathan Mantey                     errorInEntry = true;
13254a0cb85cSEd Tanous                 }
13264a0cb85cSEd Tanous             }
13271abe55efSEd Tanous             else
13281abe55efSEd Tanous             {
132901784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
133001784826SJohnathan Mantey                                           pathString + "/SubnetMask");
133101784826SJohnathan Mantey                 errorInEntry = true;
133201784826SJohnathan Mantey             }
133301784826SJohnathan Mantey 
133401784826SJohnathan Mantey             if (gateway)
133501784826SJohnathan Mantey             {
1336033f1e4dSEd Tanous                 if (ip_util::ipv4VerifyIpAndGetBitcount(*gateway))
133701784826SJohnathan Mantey                 {
133801784826SJohnathan Mantey                     gw = &(*gateway);
133901784826SJohnathan Mantey                 }
134001784826SJohnathan Mantey                 else
134101784826SJohnathan Mantey                 {
1342bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1343bf648f77SEd Tanous                                                        pathString + "/Gateway");
134401784826SJohnathan Mantey                     errorInEntry = true;
134501784826SJohnathan Mantey                 }
134601784826SJohnathan Mantey             }
134785ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
134801784826SJohnathan Mantey             {
134985ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
135001784826SJohnathan Mantey             }
135101784826SJohnathan Mantey             else
13521abe55efSEd Tanous             {
1353a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
13544a0cb85cSEd Tanous                                           pathString + "/Gateway");
135501784826SJohnathan Mantey                 errorInEntry = true;
13564a0cb85cSEd Tanous             }
13574a0cb85cSEd Tanous 
135801784826SJohnathan Mantey             if (errorInEntry)
13591abe55efSEd Tanous             {
136001784826SJohnathan Mantey                 return;
13614a0cb85cSEd Tanous             }
13624a0cb85cSEd Tanous 
136385ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
13641abe55efSEd Tanous             {
13659c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV4, ifaceId,
13669c5e585cSRavi Teja                                          nicIpEntry->id, prefixLength, *gw,
1367bf648f77SEd Tanous                                          *addr, asyncResp);
136889492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
136989492a15SPatrick Williams                                                   ipv4Data.cend());
1370588c3f0dSKowalski, Kamil             }
137101784826SJohnathan Mantey             else
137201784826SJohnathan Mantey             {
1373cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1374cb13a392SEd Tanous                            asyncResp);
13754a0cb85cSEd Tanous             }
13764a0cb85cSEd Tanous             entryIdx++;
13774a0cb85cSEd Tanous         }
137801784826SJohnathan Mantey         else
137901784826SJohnathan Mantey         {
138085ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
138101784826SJohnathan Mantey             {
138201784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
138301784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
138401784826SJohnathan Mantey                 // in error, so bail out.
138501784826SJohnathan Mantey                 if (thisJson.is_null())
138601784826SJohnathan Mantey                 {
138701784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
138801784826SJohnathan Mantey                     return;
138901784826SJohnathan Mantey                 }
139001784826SJohnathan Mantey                 messages::propertyValueFormatError(
139171f52d96SEd Tanous                     asyncResp->res,
139271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
139371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
139471f52d96SEd Tanous                     pathString);
139501784826SJohnathan Mantey                 return;
139601784826SJohnathan Mantey             }
139701784826SJohnathan Mantey 
139801784826SJohnathan Mantey             if (thisJson.is_null())
139901784826SJohnathan Mantey             {
14009c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
140101784826SJohnathan Mantey             }
140285ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
140301784826SJohnathan Mantey             {
140489492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
140589492a15SPatrick Williams                                                   ipv4Data.cend());
140601784826SJohnathan Mantey             }
140701784826SJohnathan Mantey             entryIdx++;
140801784826SJohnathan Mantey         }
140901784826SJohnathan Mantey     }
14104a0cb85cSEd Tanous }
14114a0cb85cSEd Tanous 
14124f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1413f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1414f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
14158d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1416f85837bfSRAJESWARAN THILLAIGOVINDAN {
1417f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
14185e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec) {
1419f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1420f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1421f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1422f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1423f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1424f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1425f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1426f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1427f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1428bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1429168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1430f85837bfSRAJESWARAN THILLAIGOVINDAN }
1431f85837bfSRAJESWARAN THILLAIGOVINDAN 
14324f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1433ddd70dcaSEd Tanous     const std::string& ifaceId, const nlohmann::json::array_t& input,
143477179532SEd Tanous     const std::vector<IPv6AddressData>& ipv6Data,
14358d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1436e48c0fc5SRavi Teja {
1437ddd70dcaSEd Tanous     if (input.empty())
1438e48c0fc5SRavi Teja     {
143971f52d96SEd Tanous         messages::propertyValueTypeError(
144071f52d96SEd Tanous             asyncResp->res,
1441ddd70dcaSEd Tanous             nlohmann::json(input).dump(
1442ddd70dcaSEd Tanous                 2, ' ', true, nlohmann::json::error_handler_t::replace),
1443e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1444e48c0fc5SRavi Teja         return;
1445e48c0fc5SRavi Teja     }
1446271584abSEd Tanous     size_t entryIdx = 1;
144777179532SEd Tanous     std::vector<IPv6AddressData>::const_iterator nicIpEntry =
14482c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1449f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1450e48c0fc5SRavi Teja     {
145189492a15SPatrick Williams         std::string pathString = "IPv6StaticAddresses/" +
145289492a15SPatrick Williams                                  std::to_string(entryIdx);
1453e48c0fc5SRavi Teja 
145401784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1455e48c0fc5SRavi Teja         {
1456e48c0fc5SRavi Teja             std::optional<std::string> address;
1457e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1458f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1459bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1460bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1461e48c0fc5SRavi Teja             {
146201784826SJohnathan Mantey                 messages::propertyValueFormatError(
146371f52d96SEd Tanous                     asyncResp->res,
146471f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
146571f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
146671f52d96SEd Tanous                     pathString);
1467e48c0fc5SRavi Teja                 return;
1468e48c0fc5SRavi Teja             }
1469e48c0fc5SRavi Teja 
1470543f4400SEd Tanous             const std::string* addr = nullptr;
1471543f4400SEd Tanous             uint8_t prefix = 0;
147201784826SJohnathan Mantey 
147301784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
147401784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
147501784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
147601784826SJohnathan Mantey             // current request.
1477e48c0fc5SRavi Teja             if (address)
1478e48c0fc5SRavi Teja             {
147901784826SJohnathan Mantey                 addr = &(*address);
1480e48c0fc5SRavi Teja             }
148185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
148201784826SJohnathan Mantey             {
148385ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
148401784826SJohnathan Mantey             }
148501784826SJohnathan Mantey             else
148601784826SJohnathan Mantey             {
148701784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
148801784826SJohnathan Mantey                                           pathString + "/Address");
148901784826SJohnathan Mantey                 return;
1490e48c0fc5SRavi Teja             }
1491e48c0fc5SRavi Teja 
1492e48c0fc5SRavi Teja             if (prefixLength)
1493e48c0fc5SRavi Teja             {
149401784826SJohnathan Mantey                 prefix = *prefixLength;
149501784826SJohnathan Mantey             }
149685ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1497e48c0fc5SRavi Teja             {
149885ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1499e48c0fc5SRavi Teja             }
1500e48c0fc5SRavi Teja             else
1501e48c0fc5SRavi Teja             {
1502e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1503e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
150401784826SJohnathan Mantey                 return;
1505e48c0fc5SRavi Teja             }
1506e48c0fc5SRavi Teja 
150785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1508e48c0fc5SRavi Teja             {
15099c5e585cSRavi Teja                 deleteAndCreateIPAddress(IpVersion::IpV6, ifaceId,
15109c5e585cSRavi Teja                                          nicIpEntry->id, prefix, "", *addr,
1511e48c0fc5SRavi Teja                                          asyncResp);
151289492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
151389492a15SPatrick Williams                                                   ipv6Data.cend());
151401784826SJohnathan Mantey             }
151501784826SJohnathan Mantey             else
151601784826SJohnathan Mantey             {
151701784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1518e48c0fc5SRavi Teja             }
1519e48c0fc5SRavi Teja             entryIdx++;
1520e48c0fc5SRavi Teja         }
152101784826SJohnathan Mantey         else
152201784826SJohnathan Mantey         {
152385ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
152401784826SJohnathan Mantey             {
152501784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
152601784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
152701784826SJohnathan Mantey                 // in error, so bail out.
152801784826SJohnathan Mantey                 if (thisJson.is_null())
152901784826SJohnathan Mantey                 {
153001784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
153101784826SJohnathan Mantey                     return;
153201784826SJohnathan Mantey                 }
153301784826SJohnathan Mantey                 messages::propertyValueFormatError(
153471f52d96SEd Tanous                     asyncResp->res,
153571f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
153671f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
153771f52d96SEd Tanous                     pathString);
153801784826SJohnathan Mantey                 return;
153901784826SJohnathan Mantey             }
154001784826SJohnathan Mantey 
154101784826SJohnathan Mantey             if (thisJson.is_null())
154201784826SJohnathan Mantey             {
15439c5e585cSRavi Teja                 deleteIPAddress(ifaceId, nicIpEntry->id, asyncResp);
154401784826SJohnathan Mantey             }
154585ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
154601784826SJohnathan Mantey             {
154789492a15SPatrick Williams                 nicIpEntry = getNextStaticIpEntry(++nicIpEntry,
154889492a15SPatrick Williams                                                   ipv6Data.cend());
154901784826SJohnathan Mantey             }
155001784826SJohnathan Mantey             entryIdx++;
155101784826SJohnathan Mantey         }
155201784826SJohnathan Mantey     }
1553e48c0fc5SRavi Teja }
1554e48c0fc5SRavi Teja 
155577179532SEd Tanous inline void
155677179532SEd Tanous     parseInterfaceData(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
155777179532SEd Tanous                        const std::string& ifaceId,
155877179532SEd Tanous                        const EthernetInterfaceData& ethData,
155977179532SEd Tanous                        const std::vector<IPv4AddressData>& ipv4Data,
156077179532SEd Tanous                        const std::vector<IPv6AddressData>& ipv6Data)
15614a0cb85cSEd Tanous {
15627a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> inventoryForEthernet = {
1563eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1564eeedda23SJohnathan Mantey 
15652c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
156681ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
1567ef4c65b7SEd Tanous     jsonResponse["@odata.id"] = boost::urls::format(
1568ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}", ifaceId);
15692c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1570eeedda23SJohnathan Mantey 
1571eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1572eeedda23SJohnathan Mantey 
15737a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
15747a1dbc48SGeorge Liu         "/", 0, inventoryForEthernet,
15757a1dbc48SGeorge Liu         [health](const boost::system::error_code& ec,
1576b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1577eeedda23SJohnathan Mantey         if (ec)
1578029573d4SEd Tanous         {
1579eeedda23SJohnathan Mantey             return;
1580eeedda23SJohnathan Mantey         }
1581eeedda23SJohnathan Mantey 
1582914e2d5dSEd Tanous         health->inventory = resp;
15837a1dbc48SGeorge Liu         });
1584eeedda23SJohnathan Mantey 
1585eeedda23SJohnathan Mantey     health->populate();
1586eeedda23SJohnathan Mantey 
1587eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1588eeedda23SJohnathan Mantey     {
15890ef0e289SJohnathan Mantey         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
15902c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1591029573d4SEd Tanous     }
1592029573d4SEd Tanous     else
1593029573d4SEd Tanous     {
15942c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
15952c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1596029573d4SEd Tanous     }
1597aa05fb27SJohnathan Mantey 
15982c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
159935fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
160082695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
16012c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
160282695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
160382695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
160482695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
160582695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
16061f8c7b5dSJohnathan Mantey 
16072c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
160882695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
16091f8c7b5dSJohnathan Mantey                                                                : "Disabled";
161082695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
161182695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
161282695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
16132a133282Smanojkiraneda 
161482695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
16154a0cb85cSEd Tanous     {
161682695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1617ab6554f1SJoshi-Mansi 
1618ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1619ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1620ab6554f1SJoshi-Mansi         // FQDN
162182695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1622d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1623d24bfc7aSJennifer Lee         {
16242c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1625d24bfc7aSJennifer Lee         }
16262c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
16274a0cb85cSEd Tanous     }
16284a0cb85cSEd Tanous 
1629ef4c65b7SEd Tanous     jsonResponse["VLANs"]["@odata.id"] = boost::urls::format(
1630ef4c65b7SEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs", ifaceId);
1631fda13ad2SSunitha Harish 
16322c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
16332c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
16344a0cb85cSEd Tanous 
16352c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
16362c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
16372c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
16382c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
16399eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
16404a0cb85cSEd Tanous     {
16412c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1642fa5053a6SGunnar Mills         if (gatewayStr.empty())
1643fa5053a6SGunnar Mills         {
1644fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1645fa5053a6SGunnar Mills         }
16461476687dSEd Tanous         nlohmann::json::object_t ipv4;
16471476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
16481476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
16491476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
16501476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1651fa5053a6SGunnar Mills 
16522c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1653d1d50814SRavi Teja         {
16541476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1655d1d50814SRavi Teja         }
16561476687dSEd Tanous 
1657b2ba3072SPatrick Williams         ipv4Array.emplace_back(std::move(ipv4));
165801784826SJohnathan Mantey     }
1659d1d50814SRavi Teja 
166082695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
16617ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
16627ea79e5eSRavi Teja     {
16637ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
16647ea79e5eSRavi Teja     }
16657ea79e5eSRavi Teja 
16667ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1667e48c0fc5SRavi Teja 
16682c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
16692c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
16702c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
16712c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
16727f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
16732c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
16747f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
16759eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1676e48c0fc5SRavi Teja     {
16771476687dSEd Tanous         nlohmann::json::object_t ipv6;
16781476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
16791476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
16801476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
1681f8361275SSunitha Harish 
1682b2ba3072SPatrick Williams         ipv6Array.emplace_back(std::move(ipv6));
16832c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1684e48c0fc5SRavi Teja         {
16851476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
16861476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
16871476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1688b2ba3072SPatrick Williams             ipv6StaticArray.emplace_back(std::move(ipv6Static));
168901784826SJohnathan Mantey         }
1690e48c0fc5SRavi Teja     }
1691588c3f0dSKowalski, Kamil }
1692588c3f0dSKowalski, Kamil 
16934f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1694bf648f77SEd Tanous {
169511ba3979SEd Tanous     return iface.starts_with(parent + "_");
1696bf648f77SEd Tanous }
1697bf648f77SEd Tanous 
1698bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1699bf648f77SEd Tanous {
1700bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1701ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
17021476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
17031476687dSEd Tanous             [&app](const crow::Request& req,
17041476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
17053ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
170645ca1b86SEd Tanous         {
170745ca1b86SEd Tanous             return;
170845ca1b86SEd Tanous         }
170945ca1b86SEd Tanous 
1710bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1711bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1712bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1713bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1714bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1715bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1716bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1717bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1718bf648f77SEd Tanous 
1719bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1720bf648f77SEd Tanous         // preparation
1721002d39b4SEd Tanous         getEthernetIfaceList(
172277179532SEd Tanous             [asyncResp](const bool& success,
172377179532SEd Tanous                         const std::vector<std::string>& ifaceList) {
1724bf648f77SEd Tanous             if (!success)
17251abe55efSEd Tanous             {
1726f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17279391bb9cSRapkiewicz, Pawel                 return;
17289391bb9cSRapkiewicz, Pawel             }
17299391bb9cSRapkiewicz, Pawel 
1730002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1731bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1732bf648f77SEd Tanous             std::string tag = "_";
1733bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1734bf648f77SEd Tanous             {
1735bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1736bf648f77SEd Tanous                 if (found == std::string::npos)
1737bf648f77SEd Tanous                 {
17381476687dSEd Tanous                     nlohmann::json::object_t iface;
1739ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
1740ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}",
1741ef4c65b7SEd Tanous                         ifaceItem);
1742b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
1743bf648f77SEd Tanous                 }
1744bf648f77SEd Tanous             }
1745bf648f77SEd Tanous 
1746002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1747bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1748bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1749bf648f77SEd Tanous         });
1750bf648f77SEd Tanous         });
1751bf648f77SEd Tanous 
1752bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1753ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1754bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
175545ca1b86SEd Tanous             [&app](const crow::Request& req,
1756bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1757bf648f77SEd Tanous                    const std::string& ifaceId) {
17583ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
175945ca1b86SEd Tanous         {
176045ca1b86SEd Tanous             return;
176145ca1b86SEd Tanous         }
17624a0cb85cSEd Tanous         getEthernetIfaceData(
1763bf648f77SEd Tanous             ifaceId,
176477179532SEd Tanous             [asyncResp, ifaceId](const bool& success,
176577179532SEd Tanous                                  const EthernetInterfaceData& ethData,
176677179532SEd Tanous                                  const std::vector<IPv4AddressData>& ipv4Data,
176777179532SEd Tanous                                  const std::vector<IPv6AddressData>& ipv6Data) {
17684a0cb85cSEd Tanous             if (!success)
17691abe55efSEd Tanous             {
1770bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1771bf648f77SEd Tanous                 // existing object, and other errors
1772002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1773002d39b4SEd Tanous                                            ifaceId);
17744a0cb85cSEd Tanous                 return;
17759391bb9cSRapkiewicz, Pawel             }
17764c9afe43SEd Tanous 
1777188cb629SJiaqing Zhao             // Keep using the v1.6.0 schema here as currently bmcweb have to use
1778188cb629SJiaqing Zhao             // "VLANs" property deprecated in v1.7.0 for VLAN creation/deletion.
17790f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1780188cb629SJiaqing Zhao                 "#EthernetInterface.v1_6_0.EthernetInterface";
1781002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
17820f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
17830f74e643SEd Tanous                 "Management Network Interface";
17840f74e643SEd Tanous 
1785002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
17869391bb9cSRapkiewicz, Pawel             });
1787bf648f77SEd Tanous         });
17889391bb9cSRapkiewicz, Pawel 
1789bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1790ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1791bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
179245ca1b86SEd Tanous             [&app](const crow::Request& req,
1793bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1794bf648f77SEd Tanous                    const std::string& ifaceId) {
17953ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
179645ca1b86SEd Tanous         {
179745ca1b86SEd Tanous             return;
179845ca1b86SEd Tanous         }
1799bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1800ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1801d577665bSRatan Gupta         std::optional<std::string> macAddress;
18029a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1803ddd70dcaSEd Tanous         std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
1804ddd70dcaSEd Tanous         std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
1805f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1806da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
18071f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1808eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
180935fb5311STejas Patil         std::optional<size_t> mtuSize;
18101f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
18111f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
18120627a2c7SEd Tanous 
181315ed6780SWilly Tu         if (!json_util::readJsonPatch(
18148d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1815002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1816002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1817002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1818ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1819002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
18201abe55efSEd Tanous         {
1821588c3f0dSKowalski, Kamil             return;
1822588c3f0dSKowalski, Kamil         }
1823da131a9aSJennifer Lee         if (dhcpv4)
1824da131a9aSJennifer Lee         {
1825002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
18261f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
182782695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
182882695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
182982695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
18301f8c7b5dSJohnathan Mantey             {
18311f8c7b5dSJohnathan Mantey                 return;
18321f8c7b5dSJohnathan Mantey             }
18331f8c7b5dSJohnathan Mantey         }
18341f8c7b5dSJohnathan Mantey 
18351f8c7b5dSJohnathan Mantey         if (dhcpv6)
18361f8c7b5dSJohnathan Mantey         {
1837002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1838002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1839002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1840002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1841002d39b4SEd Tanous                                      "UseDomainName",
184282695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
18431f8c7b5dSJohnathan Mantey             {
18441f8c7b5dSJohnathan Mantey                 return;
18451f8c7b5dSJohnathan Mantey             }
1846da131a9aSJennifer Lee         }
1847da131a9aSJennifer Lee 
1848bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1849bf648f77SEd Tanous         // for JSON preparation
18504a0cb85cSEd Tanous         getEthernetIfaceData(
18512c70f800SEd Tanous             ifaceId,
1852bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1853ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1854d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
18559a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1856e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
18571f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
1858bc20089aSEd Tanous              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6), mtuSize,
1859bc20089aSEd Tanous              v4dhcpParms = std::move(v4dhcpParms),
1860f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1861002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
186277179532SEd Tanous                 const std::vector<IPv4AddressData>& ipv4Data,
186377179532SEd Tanous                 const std::vector<IPv6AddressData>& ipv6Data) {
18641abe55efSEd Tanous             if (!success)
18651abe55efSEd Tanous             {
1866588c3f0dSKowalski, Kamil                 // ... otherwise return error
1867bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1868bf648f77SEd Tanous                 // existing object, and other errors
1869002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1870002d39b4SEd Tanous                                            ifaceId);
1871588c3f0dSKowalski, Kamil                 return;
1872588c3f0dSKowalski, Kamil             }
1873588c3f0dSKowalski, Kamil 
18741f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
18751f8c7b5dSJohnathan Mantey             {
1876002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1877002d39b4SEd Tanous                                 asyncResp);
18781f8c7b5dSJohnathan Mantey             }
18791f8c7b5dSJohnathan Mantey 
18800627a2c7SEd Tanous             if (hostname)
18811abe55efSEd Tanous             {
18820627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
18831abe55efSEd Tanous             }
18840627a2c7SEd Tanous 
1885ab6554f1SJoshi-Mansi             if (fqdn)
1886ab6554f1SJoshi-Mansi             {
18872c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1888ab6554f1SJoshi-Mansi             }
1889ab6554f1SJoshi-Mansi 
1890d577665bSRatan Gupta             if (macAddress)
1891d577665bSRatan Gupta             {
1892002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1893d577665bSRatan Gupta             }
1894d577665bSRatan Gupta 
1895d1d50814SRavi Teja             if (ipv4StaticAddresses)
1896d1d50814SRavi Teja             {
1897bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
1898bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
1899bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
1900bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
1901bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
1902bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
1903bf648f77SEd Tanous                 // that, but could be done more efficiently
1904ddd70dcaSEd Tanous                 nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
1905002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
19061abe55efSEd Tanous             }
19070627a2c7SEd Tanous 
1908f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
1909f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1910002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
1911002d39b4SEd Tanous                                              asyncResp);
1912f85837bfSRAJESWARAN THILLAIGOVINDAN             }
19139a6fc6feSRavi Teja 
19149a6fc6feSRavi Teja             if (ipv6DefaultGateway)
19159a6fc6feSRavi Teja             {
19169a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
19179a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
19189a6fc6feSRavi Teja             }
1919e48c0fc5SRavi Teja 
1920e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
1921e48c0fc5SRavi Teja             {
1922ddd70dcaSEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
1923ddd70dcaSEd Tanous                                                ipv6Data, asyncResp);
1924e48c0fc5SRavi Teja             }
1925eeedda23SJohnathan Mantey 
1926eeedda23SJohnathan Mantey             if (interfaceEnabled)
1927eeedda23SJohnathan Mantey             {
1928002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
1929002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
1930eeedda23SJohnathan Mantey             }
193135fb5311STejas Patil 
193235fb5311STejas Patil             if (mtuSize)
193335fb5311STejas Patil             {
193435fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
193535fb5311STejas Patil             }
1936588c3f0dSKowalski, Kamil             });
1937bf648f77SEd Tanous         });
19389391bb9cSRapkiewicz, Pawel 
1939bf648f77SEd Tanous     BMCWEB_ROUTE(
1940bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
1941ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
1942bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
194345ca1b86SEd Tanous             [&app](const crow::Request& req,
1944bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
194545ca1b86SEd Tanous                    const std::string& parentIfaceId,
194645ca1b86SEd Tanous                    const std::string& ifaceId) {
19473ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
194845ca1b86SEd Tanous         {
194945ca1b86SEd Tanous             return;
195045ca1b86SEd Tanous         }
19518d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
19520f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
19538d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
1954e439f0f8SKowalski, Kamil 
19552c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
19561abe55efSEd Tanous         {
1957a434f2bdSEd Tanous             return;
1958a434f2bdSEd Tanous         }
1959a434f2bdSEd Tanous 
1960bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1961bf648f77SEd Tanous         // for JSON preparation
196277179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
196377179532SEd Tanous                                           const bool& success,
196477179532SEd Tanous                                           const EthernetInterfaceData& ethData,
196577179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
196677179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
196717e22024SJiaqing Zhao             if (success && ethData.vlanId)
19681abe55efSEd Tanous             {
196922872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["Id"] = ifaceId;
1970ef4c65b7SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
1971ef4c65b7SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
1972ef4c65b7SEd Tanous                     parentIfaceId, ifaceId);
197322872ff3SJiaqing Zhao 
197423a06317SJiaqing Zhao                 asyncResp->res.jsonValue["VLANEnable"] = ethData.nicEnabled;
197522872ff3SJiaqing Zhao                 asyncResp->res.jsonValue["VLANId"] = *ethData.vlanId;
19761abe55efSEd Tanous             }
19771abe55efSEd Tanous             else
19781abe55efSEd Tanous             {
1979e439f0f8SKowalski, Kamil                 // ... otherwise return error
1980bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1981bf648f77SEd Tanous                 // existing object, and other errors
1982bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
1983d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
1984e439f0f8SKowalski, Kamil             }
1985e439f0f8SKowalski, Kamil         });
1986bf648f77SEd Tanous         });
1987e439f0f8SKowalski, Kamil 
1988bf648f77SEd Tanous     BMCWEB_ROUTE(
1989bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
19903d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
1991bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
199245ca1b86SEd Tanous             [&app](const crow::Request& req,
1993bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
199445ca1b86SEd Tanous                    const std::string& parentIfaceId,
199545ca1b86SEd Tanous                    const std::string& ifaceId) {
19963ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
199745ca1b86SEd Tanous         {
199845ca1b86SEd Tanous             return;
199945ca1b86SEd Tanous         }
2000fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
20011abe55efSEd Tanous         {
2002d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2003002d39b4SEd Tanous                                        ifaceId);
2004927a505aSKowalski, Kamil             return;
2005927a505aSKowalski, Kamil         }
2006927a505aSKowalski, Kamil 
20073927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
20083927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
20090627a2c7SEd Tanous 
201015ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2011bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
20121abe55efSEd Tanous         {
2013927a505aSKowalski, Kamil             return;
2014927a505aSKowalski, Kamil         }
2015927a505aSKowalski, Kamil 
20163927e13eSJiaqing Zhao         if (vlanId)
20173927e13eSJiaqing Zhao         {
20183927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
20193927e13eSJiaqing Zhao             return;
20203927e13eSJiaqing Zhao         }
20213927e13eSJiaqing Zhao 
2022bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2023bf648f77SEd Tanous         // for JSON preparation
202477179532SEd Tanous         getEthernetIfaceData(ifaceId,
202577179532SEd Tanous                              [asyncResp, parentIfaceId, ifaceId,
202677179532SEd Tanous                               vlanEnable](const bool& success,
202777179532SEd Tanous                                           const EthernetInterfaceData& ethData,
202877179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
202977179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
203017e22024SJiaqing Zhao             if (success && ethData.vlanId)
203108244d02SSunitha Harish             {
203223a06317SJiaqing Zhao                 if (vlanEnable)
203323a06317SJiaqing Zhao                 {
203423a06317SJiaqing Zhao                     crow::connections::systemBus->async_method_call(
20355e7e2dc5SEd Tanous                         [asyncResp](const boost::system::error_code& ec) {
203608244d02SSunitha Harish                         if (ec)
203708244d02SSunitha Harish                         {
203808244d02SSunitha Harish                             messages::internalError(asyncResp->res);
20393927e13eSJiaqing Zhao                             return;
204008244d02SSunitha Harish                         }
204123a06317SJiaqing Zhao                         },
204223a06317SJiaqing Zhao                         "xyz.openbmc_project.Network",
204323a06317SJiaqing Zhao                         "/xyz/openbmc_project/network/" + ifaceId,
204423a06317SJiaqing Zhao                         "org.freedesktop.DBus.Properties", "Set",
204523a06317SJiaqing Zhao                         "xyz.openbmc_project.Network.EthernetInterface",
204623a06317SJiaqing Zhao                         "NICEnabled",
204723a06317SJiaqing Zhao                         dbus::utility::DbusVariantType(*vlanEnable));
204808244d02SSunitha Harish                 }
204908244d02SSunitha Harish             }
205008244d02SSunitha Harish             else
20511abe55efSEd Tanous             {
2052bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2053bf648f77SEd Tanous                 // existing object, and other errors
2054bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2055d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2056bf648f77SEd Tanous                 return;
2057bf648f77SEd Tanous             }
2058bf648f77SEd Tanous         });
2059bf648f77SEd Tanous         });
2060bf648f77SEd Tanous 
2061bf648f77SEd Tanous     BMCWEB_ROUTE(
2062bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
20633d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2064bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
206545ca1b86SEd Tanous             [&app](const crow::Request& req,
2066bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206745ca1b86SEd Tanous                    const std::string& parentIfaceId,
206845ca1b86SEd Tanous                    const std::string& ifaceId) {
20693ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
207045ca1b86SEd Tanous         {
207145ca1b86SEd Tanous             return;
207245ca1b86SEd Tanous         }
2073bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2074bf648f77SEd Tanous         {
2075d8a5d5d8SJiaqing Zhao             messages::resourceNotFound(asyncResp->res, "VLanNetworkInterface",
2076002d39b4SEd Tanous                                        ifaceId);
2077927a505aSKowalski, Kamil             return;
2078927a505aSKowalski, Kamil         }
2079e439f0f8SKowalski, Kamil 
2080bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2081bf648f77SEd Tanous         // for JSON preparation
208277179532SEd Tanous         getEthernetIfaceData(ifaceId, [asyncResp, parentIfaceId, ifaceId](
208377179532SEd Tanous                                           const bool& success,
208477179532SEd Tanous                                           const EthernetInterfaceData& ethData,
208577179532SEd Tanous                                           const std::vector<IPv4AddressData>&,
208677179532SEd Tanous                                           const std::vector<IPv6AddressData>&) {
208717e22024SJiaqing Zhao             if (success && ethData.vlanId)
20881abe55efSEd Tanous             {
2089f12894f8SJason M. Bills                 auto callback =
20905e7e2dc5SEd Tanous                     [asyncResp](const boost::system::error_code& ec) {
20911abe55efSEd Tanous                     if (ec)
20921abe55efSEd Tanous                     {
2093f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2094927a505aSKowalski, Kamil                     }
20954a0cb85cSEd Tanous                 };
20964a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2097002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2098002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
20994a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
21001abe55efSEd Tanous             }
21011abe55efSEd Tanous             else
21021abe55efSEd Tanous             {
2103927a505aSKowalski, Kamil                 // ... otherwise return error
2104bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2105bf648f77SEd Tanous                 // existing object, and other errors
2106bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2107d8a5d5d8SJiaqing Zhao                                            "VLanNetworkInterface", ifaceId);
2108927a505aSKowalski, Kamil             }
2109927a505aSKowalski, Kamil         });
2110bf648f77SEd Tanous         });
2111e439f0f8SKowalski, Kamil 
2112bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2113bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2114ed398213SEd Tanous 
2115ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
21161476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
21171476687dSEd Tanous             [&app](const crow::Request& req,
2118bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2119bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21203ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
212145ca1b86SEd Tanous         {
212245ca1b86SEd Tanous             return;
212345ca1b86SEd Tanous         }
21244a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
21251abe55efSEd Tanous         // preparation
212677179532SEd Tanous         getEthernetIfaceList([asyncResp, rootInterfaceName](
21271abe55efSEd Tanous                                  const bool& success,
212877179532SEd Tanous                                  const std::vector<std::string>& ifaceList) {
21294a0cb85cSEd Tanous             if (!success)
21301abe55efSEd Tanous             {
2131f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
21324a0cb85cSEd Tanous                 return;
21331abe55efSEd Tanous             }
213477179532SEd Tanous             if (std::find(ifaceList.begin(), ifaceList.end(),
213577179532SEd Tanous                           rootInterfaceName) == ifaceList.end())
21364c9afe43SEd Tanous             {
2137002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2138002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
21394c9afe43SEd Tanous                                            rootInterfaceName);
21404c9afe43SEd Tanous                 return;
21414c9afe43SEd Tanous             }
21424c9afe43SEd Tanous 
21430f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
21440f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
21450f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
21460f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
21470f74e643SEd Tanous                 "VLAN Network Interface Collection";
21484a0cb85cSEd Tanous 
21492c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
21504a0cb85cSEd Tanous 
215181ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
21521abe55efSEd Tanous             {
215311ba3979SEd Tanous                 if (ifaceItem.starts_with(rootInterfaceName + "_"))
21544a0cb85cSEd Tanous                 {
21551476687dSEd Tanous                     nlohmann::json::object_t iface;
2156ef4c65b7SEd Tanous                     iface["@odata.id"] = boost::urls::format(
2157ef4c65b7SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs/{}",
2158ef4c65b7SEd Tanous                         rootInterfaceName, ifaceItem);
2159b2ba3072SPatrick Williams                     ifaceArray.emplace_back(std::move(iface));
2160e439f0f8SKowalski, Kamil                 }
2161e439f0f8SKowalski, Kamil             }
2162e439f0f8SKowalski, Kamil 
2163002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
21642c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2165ef4c65b7SEd Tanous             asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
2166ef4c65b7SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/{}/VLANs",
2167ef4c65b7SEd Tanous                 rootInterfaceName);
2168e439f0f8SKowalski, Kamil         });
2169bf648f77SEd Tanous         });
2170e439f0f8SKowalski, Kamil 
2171bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2172bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
21733d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2174bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
217545ca1b86SEd Tanous             [&app](const crow::Request& req,
2176bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2177bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
21783ba00073SCarson Labrado         if (!redfish::setUpRedfishRoute(app, req, asyncResp))
217945ca1b86SEd Tanous         {
218045ca1b86SEd Tanous             return;
218145ca1b86SEd Tanous         }
2182fda13ad2SSunitha Harish         bool vlanEnable = false;
21830627a2c7SEd Tanous         uint32_t vlanId = 0;
2184002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2185002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
21861abe55efSEd Tanous         {
21874a0cb85cSEd Tanous             return;
2188e439f0f8SKowalski, Kamil         }
2189fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2190dbb59d4dSEd Tanous         if (vlanId == 0U)
2191fda13ad2SSunitha Harish         {
2192fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2193fda13ad2SSunitha Harish         }
2194fda13ad2SSunitha Harish         if (!vlanEnable)
2195fda13ad2SSunitha Harish         {
2196fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2197fda13ad2SSunitha Harish         }
2198271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2199fda13ad2SSunitha Harish         {
2200fda13ad2SSunitha Harish             return;
2201fda13ad2SSunitha Harish         }
2202fda13ad2SSunitha Harish 
22035e7e2dc5SEd Tanous         auto callback = [asyncResp](const boost::system::error_code& ec) {
22041abe55efSEd Tanous             if (ec)
22051abe55efSEd Tanous             {
2206bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2207bf648f77SEd Tanous                 // based on phosphor-network responses
2208f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22094a0cb85cSEd Tanous                 return;
22101abe55efSEd Tanous             }
2211f12894f8SJason M. Bills             messages::created(asyncResp->res);
2212e439f0f8SKowalski, Kamil         };
22134a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
22144a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
22154a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
22164a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
22170627a2c7SEd Tanous             rootInterfaceName, vlanId);
2218bf648f77SEd Tanous         });
22194a0cb85cSEd Tanous }
2220bf648f77SEd Tanous 
22219391bb9cSRapkiewicz, Pawel } // namespace redfish
2222