xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 002d39b4a7a5ed7166e2acad84e0943c3def9492)
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 
187e860f15SJohn Edward Broadbent #include <app.hpp>
194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
21168e20c1SEd Tanous #include <dbus_utility.hpp>
22588c3f0dSKowalski, Kamil #include <error_messages.hpp>
2345ca1b86SEd Tanous #include <query.hpp>
24ed398213SEd Tanous #include <registries/privilege_registry.hpp>
251214b7e7SGunnar Mills #include <utils/json_utils.hpp>
261214b7e7SGunnar Mills 
27a24526dcSEd Tanous #include <optional>
28ab6554f1SJoshi-Mansi #include <regex>
299391bb9cSRapkiewicz, Pawel 
301abe55efSEd Tanous namespace redfish
311abe55efSEd Tanous {
329391bb9cSRapkiewicz, Pawel 
334a0cb85cSEd Tanous enum class LinkType
344a0cb85cSEd Tanous {
354a0cb85cSEd Tanous     Local,
364a0cb85cSEd Tanous     Global
374a0cb85cSEd Tanous };
389391bb9cSRapkiewicz, Pawel 
399391bb9cSRapkiewicz, Pawel /**
409391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
419391bb9cSRapkiewicz, Pawel  */
421abe55efSEd Tanous struct IPv4AddressData
431abe55efSEd Tanous {
44179db1d7SKowalski, Kamil     std::string id;
454a0cb85cSEd Tanous     std::string address;
464a0cb85cSEd Tanous     std::string domain;
474a0cb85cSEd Tanous     std::string gateway;
489391bb9cSRapkiewicz, Pawel     std::string netmask;
499391bb9cSRapkiewicz, Pawel     std::string origin;
504a0cb85cSEd Tanous     LinkType linktype;
5101c6e858SSunitha Harish     bool isActive;
524a0cb85cSEd Tanous 
531abe55efSEd Tanous     bool operator<(const IPv4AddressData& obj) const
541abe55efSEd Tanous     {
554a0cb85cSEd Tanous         return id < obj.id;
561abe55efSEd Tanous     }
579391bb9cSRapkiewicz, Pawel };
589391bb9cSRapkiewicz, Pawel 
599391bb9cSRapkiewicz, Pawel /**
60e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
61e48c0fc5SRavi Teja  */
62e48c0fc5SRavi Teja struct IPv6AddressData
63e48c0fc5SRavi Teja {
64e48c0fc5SRavi Teja     std::string id;
65e48c0fc5SRavi Teja     std::string address;
66e48c0fc5SRavi Teja     std::string origin;
67e48c0fc5SRavi Teja     uint8_t prefixLength;
68e48c0fc5SRavi Teja 
69e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData& obj) const
70e48c0fc5SRavi Teja     {
71e48c0fc5SRavi Teja         return id < obj.id;
72e48c0fc5SRavi Teja     }
73e48c0fc5SRavi Teja };
74e48c0fc5SRavi Teja /**
759391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
769391bb9cSRapkiewicz, Pawel  * available from DBus
779391bb9cSRapkiewicz, Pawel  */
781abe55efSEd Tanous struct EthernetInterfaceData
791abe55efSEd Tanous {
804a0cb85cSEd Tanous     uint32_t speed;
8135fb5311STejas Patil     size_t mtuSize;
8282695a5bSJiaqing Zhao     bool autoNeg;
8382695a5bSJiaqing Zhao     bool dnsEnabled;
8482695a5bSJiaqing Zhao     bool ntpEnabled;
8582695a5bSJiaqing Zhao     bool hostNameEnabled;
86aa05fb27SJohnathan Mantey     bool linkUp;
87eeedda23SJohnathan Mantey     bool nicEnabled;
8882695a5bSJiaqing Zhao     std::string dhcpEnabled;
891f8c7b5dSJohnathan Mantey     std::string operatingMode;
9082695a5bSJiaqing Zhao     std::string hostName;
9182695a5bSJiaqing Zhao     std::string defaultGateway;
9282695a5bSJiaqing Zhao     std::string ipv6DefaultGateway;
9382695a5bSJiaqing Zhao     std::string macAddress;
9417e22024SJiaqing Zhao     std::optional<uint32_t> vlanId;
950f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> nameServers;
960f6efdc1Smanojkiran.eda@gmail.com     std::vector<std::string> staticNameServers;
97d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
989391bb9cSRapkiewicz, Pawel };
999391bb9cSRapkiewicz, Pawel 
1001f8c7b5dSJohnathan Mantey struct DHCPParameters
1011f8c7b5dSJohnathan Mantey {
1021f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
10382695a5bSJiaqing Zhao     std::optional<bool> useDnsServers;
10482695a5bSJiaqing Zhao     std::optional<bool> useNtpServers;
10582695a5bSJiaqing Zhao     std::optional<bool> useDomainName;
1061f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1071f8c7b5dSJohnathan Mantey };
1081f8c7b5dSJohnathan Mantey 
1099391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1109391bb9cSRapkiewicz, Pawel // into full dot notation
1111abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1121abe55efSEd Tanous {
1139391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1149391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1159391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1169391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1179391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1189391bb9cSRapkiewicz, Pawel     return netmask;
1199391bb9cSRapkiewicz, Pawel }
1209391bb9cSRapkiewicz, Pawel 
12182695a5bSJiaqing Zhao inline bool translateDhcpEnabledToBool(const std::string& inputDHCP,
1221f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1231f8c7b5dSJohnathan Mantey {
1241f8c7b5dSJohnathan Mantey     if (isIPv4)
1251f8c7b5dSJohnathan Mantey     {
1261f8c7b5dSJohnathan Mantey         return (
1271f8c7b5dSJohnathan Mantey             (inputDHCP ==
1281f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1291f8c7b5dSJohnathan Mantey             (inputDHCP ==
1301f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1311f8c7b5dSJohnathan Mantey     }
1321f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1331f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1341f8c7b5dSJohnathan Mantey             (inputDHCP ==
1351f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1361f8c7b5dSJohnathan Mantey }
1371f8c7b5dSJohnathan Mantey 
1382c70f800SEd Tanous inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
1391f8c7b5dSJohnathan Mantey {
1401f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1411f8c7b5dSJohnathan Mantey     {
1421f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1431f8c7b5dSJohnathan Mantey     }
1443174e4dfSEd Tanous     if (isIPv4)
1451f8c7b5dSJohnathan Mantey     {
1461f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1471f8c7b5dSJohnathan Mantey     }
1483174e4dfSEd Tanous     if (isIPv6)
1491f8c7b5dSJohnathan Mantey     {
1501f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1511f8c7b5dSJohnathan Mantey     }
1521f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1531f8c7b5dSJohnathan Mantey }
1541f8c7b5dSJohnathan Mantey 
1554a0cb85cSEd Tanous inline std::string
1564a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
1574a0cb85cSEd Tanous                                         bool isIPv4)
1581abe55efSEd Tanous {
1594a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1601abe55efSEd Tanous     {
1614a0cb85cSEd Tanous         return "Static";
1629391bb9cSRapkiewicz, Pawel     }
1634a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1641abe55efSEd Tanous     {
1654a0cb85cSEd Tanous         if (isIPv4)
1661abe55efSEd Tanous         {
1674a0cb85cSEd Tanous             return "IPv4LinkLocal";
1681abe55efSEd Tanous         }
1694a0cb85cSEd Tanous         return "LinkLocal";
1709391bb9cSRapkiewicz, Pawel     }
1714a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1721abe55efSEd Tanous     {
1734a0cb85cSEd Tanous         if (isIPv4)
1744a0cb85cSEd Tanous         {
1754a0cb85cSEd Tanous             return "DHCP";
1764a0cb85cSEd Tanous         }
1774a0cb85cSEd Tanous         return "DHCPv6";
1784a0cb85cSEd Tanous     }
1794a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1804a0cb85cSEd Tanous     {
1814a0cb85cSEd Tanous         return "SLAAC";
1824a0cb85cSEd Tanous     }
1834a0cb85cSEd Tanous     return "";
1844a0cb85cSEd Tanous }
1854a0cb85cSEd Tanous 
186711ac7a9SEd Tanous inline bool
187711ac7a9SEd Tanous     extractEthernetInterfaceData(const std::string& ethifaceId,
188711ac7a9SEd Tanous                                  dbus::utility::ManagedObjectType& dbusData,
1894a0cb85cSEd Tanous                                  EthernetInterfaceData& ethData)
1904a0cb85cSEd Tanous {
1914c9afe43SEd Tanous     bool idFound = false;
19281ce609eSEd Tanous     for (auto& objpath : dbusData)
1934a0cb85cSEd Tanous     {
194f23b7296SEd Tanous         for (auto& ifacePair : objpath.second)
1954a0cb85cSEd Tanous         {
19681ce609eSEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethifaceId)
197029573d4SEd Tanous             {
1984c9afe43SEd Tanous                 idFound = true;
1994a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2004a0cb85cSEd Tanous                 {
2014a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2024a0cb85cSEd Tanous                     {
2034a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2044a0cb85cSEd Tanous                         {
2054a0cb85cSEd Tanous                             const std::string* mac =
206abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2074a0cb85cSEd Tanous                             if (mac != nullptr)
2084a0cb85cSEd Tanous                             {
20982695a5bSJiaqing Zhao                                 ethData.macAddress = *mac;
2104a0cb85cSEd Tanous                             }
2114a0cb85cSEd Tanous                         }
2124a0cb85cSEd Tanous                     }
2134a0cb85cSEd Tanous                 }
2144a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2154a0cb85cSEd Tanous                 {
2164a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2174a0cb85cSEd Tanous                     {
2184a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2194a0cb85cSEd Tanous                         {
2201b6b96c5SEd Tanous                             const uint32_t* id =
221abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2224a0cb85cSEd Tanous                             if (id != nullptr)
2234a0cb85cSEd Tanous                             {
22417e22024SJiaqing Zhao                                 ethData.vlanId = *id;
2254a0cb85cSEd Tanous                             }
2264a0cb85cSEd Tanous                         }
2274a0cb85cSEd Tanous                     }
2284a0cb85cSEd Tanous                 }
2294a0cb85cSEd Tanous                 else if (ifacePair.first ==
2304a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2314a0cb85cSEd Tanous                 {
2324a0cb85cSEd Tanous                     for (const auto& propertyPair : ifacePair.second)
2334a0cb85cSEd Tanous                     {
2344a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2354a0cb85cSEd Tanous                         {
2362c70f800SEd Tanous                             const bool* autoNeg =
237abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2382c70f800SEd Tanous                             if (autoNeg != nullptr)
2394a0cb85cSEd Tanous                             {
24082695a5bSJiaqing Zhao                                 ethData.autoNeg = *autoNeg;
2414a0cb85cSEd Tanous                             }
2424a0cb85cSEd Tanous                         }
2434a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2444a0cb85cSEd Tanous                         {
2454a0cb85cSEd Tanous                             const uint32_t* speed =
246abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2474a0cb85cSEd Tanous                             if (speed != nullptr)
2484a0cb85cSEd Tanous                             {
2494a0cb85cSEd Tanous                                 ethData.speed = *speed;
2504a0cb85cSEd Tanous                             }
2514a0cb85cSEd Tanous                         }
25235fb5311STejas Patil                         else if (propertyPair.first == "MTU")
25335fb5311STejas Patil                         {
25435fb5311STejas Patil                             const uint32_t* mtuSize =
25535fb5311STejas Patil                                 std::get_if<uint32_t>(&propertyPair.second);
25635fb5311STejas Patil                             if (mtuSize != nullptr)
25735fb5311STejas Patil                             {
25835fb5311STejas Patil                                 ethData.mtuSize = *mtuSize;
25935fb5311STejas Patil                             }
26035fb5311STejas Patil                         }
261aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
262aa05fb27SJohnathan Mantey                         {
263aa05fb27SJohnathan Mantey                             const bool* linkUp =
264aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
265aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
266aa05fb27SJohnathan Mantey                             {
267aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
268aa05fb27SJohnathan Mantey                             }
269aa05fb27SJohnathan Mantey                         }
270eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
271eeedda23SJohnathan Mantey                         {
272eeedda23SJohnathan Mantey                             const bool* nicEnabled =
273eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
274eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
275eeedda23SJohnathan Mantey                             {
276eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
277eeedda23SJohnathan Mantey                             }
278eeedda23SJohnathan Mantey                         }
279f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
280029573d4SEd Tanous                         {
281029573d4SEd Tanous                             const std::vector<std::string>* nameservers =
2828d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
283029573d4SEd Tanous                                     &propertyPair.second);
284029573d4SEd Tanous                             if (nameservers != nullptr)
285029573d4SEd Tanous                             {
286f23b7296SEd Tanous                                 ethData.nameServers = *nameservers;
2870f6efdc1Smanojkiran.eda@gmail.com                             }
2880f6efdc1Smanojkiran.eda@gmail.com                         }
2890f6efdc1Smanojkiran.eda@gmail.com                         else if (propertyPair.first == "StaticNameServers")
2900f6efdc1Smanojkiran.eda@gmail.com                         {
2910f6efdc1Smanojkiran.eda@gmail.com                             const std::vector<std::string>* staticNameServers =
2928d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
2930f6efdc1Smanojkiran.eda@gmail.com                                     &propertyPair.second);
2940f6efdc1Smanojkiran.eda@gmail.com                             if (staticNameServers != nullptr)
2950f6efdc1Smanojkiran.eda@gmail.com                             {
296f23b7296SEd Tanous                                 ethData.staticNameServers = *staticNameServers;
2974a0cb85cSEd Tanous                             }
2984a0cb85cSEd Tanous                         }
2992a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3002a133282Smanojkiraneda                         {
3012c70f800SEd Tanous                             const std::string* dhcpEnabled =
3021f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3032c70f800SEd Tanous                             if (dhcpEnabled != nullptr)
3042a133282Smanojkiraneda                             {
30582695a5bSJiaqing Zhao                                 ethData.dhcpEnabled = *dhcpEnabled;
3062a133282Smanojkiraneda                             }
3072a133282Smanojkiraneda                         }
308d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
309d24bfc7aSJennifer Lee                         {
310d24bfc7aSJennifer Lee                             const std::vector<std::string>* domainNames =
3118d78b7a9SPatrick Williams                                 std::get_if<std::vector<std::string>>(
312d24bfc7aSJennifer Lee                                     &propertyPair.second);
313d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
314d24bfc7aSJennifer Lee                             {
315f23b7296SEd Tanous                                 ethData.domainnames = *domainNames;
316d24bfc7aSJennifer Lee                             }
317d24bfc7aSJennifer Lee                         }
3189010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway")
3199010ec2eSRavi Teja                         {
3209010ec2eSRavi Teja                             const std::string* defaultGateway =
3219010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3229010ec2eSRavi Teja                             if (defaultGateway != nullptr)
3239010ec2eSRavi Teja                             {
3249010ec2eSRavi Teja                                 std::string defaultGatewayStr = *defaultGateway;
3259010ec2eSRavi Teja                                 if (defaultGatewayStr.empty())
3269010ec2eSRavi Teja                                 {
32782695a5bSJiaqing Zhao                                     ethData.defaultGateway = "0.0.0.0";
3289010ec2eSRavi Teja                                 }
3299010ec2eSRavi Teja                                 else
3309010ec2eSRavi Teja                                 {
33182695a5bSJiaqing Zhao                                     ethData.defaultGateway = defaultGatewayStr;
3329010ec2eSRavi Teja                                 }
3339010ec2eSRavi Teja                             }
3349010ec2eSRavi Teja                         }
3359010ec2eSRavi Teja                         else if (propertyPair.first == "DefaultGateway6")
3369010ec2eSRavi Teja                         {
3379010ec2eSRavi Teja                             const std::string* defaultGateway6 =
3389010ec2eSRavi Teja                                 std::get_if<std::string>(&propertyPair.second);
3399010ec2eSRavi Teja                             if (defaultGateway6 != nullptr)
3409010ec2eSRavi Teja                             {
3419010ec2eSRavi Teja                                 std::string defaultGateway6Str =
3429010ec2eSRavi Teja                                     *defaultGateway6;
3439010ec2eSRavi Teja                                 if (defaultGateway6Str.empty())
3449010ec2eSRavi Teja                                 {
34582695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3469010ec2eSRavi Teja                                         "0:0:0:0:0:0:0:0";
3479010ec2eSRavi Teja                                 }
3489010ec2eSRavi Teja                                 else
3499010ec2eSRavi Teja                                 {
35082695a5bSJiaqing Zhao                                     ethData.ipv6DefaultGateway =
3519010ec2eSRavi Teja                                         defaultGateway6Str;
3529010ec2eSRavi Teja                                 }
3539010ec2eSRavi Teja                             }
3549010ec2eSRavi Teja                         }
355029573d4SEd Tanous                     }
356029573d4SEd Tanous                 }
357029573d4SEd Tanous             }
3581f8c7b5dSJohnathan Mantey 
3591f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3601f8c7b5dSJohnathan Mantey             {
3611f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3621f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3631f8c7b5dSJohnathan Mantey                 {
3641f8c7b5dSJohnathan Mantey                     for (const auto& propertyPair : ifacePair.second)
3651f8c7b5dSJohnathan Mantey                     {
3661f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3671f8c7b5dSJohnathan Mantey                         {
3682c70f800SEd Tanous                             const bool* dnsEnabled =
3691f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3702c70f800SEd Tanous                             if (dnsEnabled != nullptr)
3711f8c7b5dSJohnathan Mantey                             {
37282695a5bSJiaqing Zhao                                 ethData.dnsEnabled = *dnsEnabled;
3731f8c7b5dSJohnathan Mantey                             }
3741f8c7b5dSJohnathan Mantey                         }
3751f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3761f8c7b5dSJohnathan Mantey                         {
3772c70f800SEd Tanous                             const bool* ntpEnabled =
3781f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3792c70f800SEd Tanous                             if (ntpEnabled != nullptr)
3801f8c7b5dSJohnathan Mantey                             {
38182695a5bSJiaqing Zhao                                 ethData.ntpEnabled = *ntpEnabled;
3821f8c7b5dSJohnathan Mantey                             }
3831f8c7b5dSJohnathan Mantey                         }
3841f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3851f8c7b5dSJohnathan Mantey                         {
3862c70f800SEd Tanous                             const bool* hostNameEnabled =
3871f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3882c70f800SEd Tanous                             if (hostNameEnabled != nullptr)
3891f8c7b5dSJohnathan Mantey                             {
39082695a5bSJiaqing Zhao                                 ethData.hostNameEnabled = *hostNameEnabled;
3911f8c7b5dSJohnathan Mantey                             }
3921f8c7b5dSJohnathan Mantey                         }
3931f8c7b5dSJohnathan Mantey                     }
3941f8c7b5dSJohnathan Mantey                 }
3951f8c7b5dSJohnathan Mantey             }
396029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
397029573d4SEd Tanous             // to check eth number
398029573d4SEd Tanous             if (ifacePair.first ==
3994a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
4004a0cb85cSEd Tanous             {
4014a0cb85cSEd Tanous                 for (const auto& propertyPair : ifacePair.second)
4024a0cb85cSEd Tanous                 {
4034a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
4044a0cb85cSEd Tanous                     {
4054a0cb85cSEd Tanous                         const std::string* hostname =
4068d78b7a9SPatrick Williams                             std::get_if<std::string>(&propertyPair.second);
4074a0cb85cSEd Tanous                         if (hostname != nullptr)
4084a0cb85cSEd Tanous                         {
40982695a5bSJiaqing Zhao                             ethData.hostName = *hostname;
4104a0cb85cSEd Tanous                         }
4114a0cb85cSEd Tanous                     }
4124a0cb85cSEd Tanous                 }
4134a0cb85cSEd Tanous             }
4144a0cb85cSEd Tanous         }
4154a0cb85cSEd Tanous     }
4164c9afe43SEd Tanous     return idFound;
4174a0cb85cSEd Tanous }
4184a0cb85cSEd Tanous 
419e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
42001784826SJohnathan Mantey inline void
42181ce609eSEd Tanous     extractIPV6Data(const std::string& ethifaceId,
422711ac7a9SEd Tanous                     const dbus::utility::ManagedObjectType& dbusData,
42381ce609eSEd Tanous                     boost::container::flat_set<IPv6AddressData>& ipv6Config)
424e48c0fc5SRavi Teja {
425e48c0fc5SRavi Teja     const std::string ipv6PathStart =
42681ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv6/";
427e48c0fc5SRavi Teja 
428e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
429e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
43081ce609eSEd Tanous     for (const auto& objpath : dbusData)
431e48c0fc5SRavi Teja     {
432e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
433e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
434e48c0fc5SRavi Teja         {
4359eb808c1SEd Tanous             for (const auto& interface : objpath.second)
436e48c0fc5SRavi Teja             {
437e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
438e48c0fc5SRavi Teja                 {
439e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
440e48c0fc5SRavi Teja                     // appropriate
441e48c0fc5SRavi Teja                     std::pair<
442e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
443e48c0fc5SRavi Teja                         bool>
44481ce609eSEd Tanous                         it = ipv6Config.insert(IPv6AddressData{});
4452c70f800SEd Tanous                     IPv6AddressData& ipv6Address = *it.first;
4462c70f800SEd Tanous                     ipv6Address.id =
447271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
4489eb808c1SEd Tanous                     for (const auto& property : interface.second)
449e48c0fc5SRavi Teja                     {
450e48c0fc5SRavi Teja                         if (property.first == "Address")
451e48c0fc5SRavi Teja                         {
452e48c0fc5SRavi Teja                             const std::string* address =
453e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
454e48c0fc5SRavi Teja                             if (address != nullptr)
455e48c0fc5SRavi Teja                             {
4562c70f800SEd Tanous                                 ipv6Address.address = *address;
457e48c0fc5SRavi Teja                             }
458e48c0fc5SRavi Teja                         }
459e48c0fc5SRavi Teja                         else if (property.first == "Origin")
460e48c0fc5SRavi Teja                         {
461e48c0fc5SRavi Teja                             const std::string* origin =
462e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
463e48c0fc5SRavi Teja                             if (origin != nullptr)
464e48c0fc5SRavi Teja                             {
4652c70f800SEd Tanous                                 ipv6Address.origin =
466e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
467e48c0fc5SRavi Teja                                                                         false);
468e48c0fc5SRavi Teja                             }
469e48c0fc5SRavi Teja                         }
470e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
471e48c0fc5SRavi Teja                         {
472e48c0fc5SRavi Teja                             const uint8_t* prefix =
473e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
474e48c0fc5SRavi Teja                             if (prefix != nullptr)
475e48c0fc5SRavi Teja                             {
4762c70f800SEd Tanous                                 ipv6Address.prefixLength = *prefix;
477e48c0fc5SRavi Teja                             }
478e48c0fc5SRavi Teja                         }
479889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
480889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
481889ff694SAsmitha Karunanithi                         {
482889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
483889ff694SAsmitha Karunanithi                         }
484e48c0fc5SRavi Teja                         else
485e48c0fc5SRavi Teja                         {
486e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
487e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
488e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
489e48c0fc5SRavi Teja                         }
490e48c0fc5SRavi Teja                     }
491e48c0fc5SRavi Teja                 }
492e48c0fc5SRavi Teja             }
493e48c0fc5SRavi Teja         }
494e48c0fc5SRavi Teja     }
495e48c0fc5SRavi Teja }
496e48c0fc5SRavi Teja 
4974a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
49801784826SJohnathan Mantey inline void
49981ce609eSEd Tanous     extractIPData(const std::string& ethifaceId,
500711ac7a9SEd Tanous                   const dbus::utility::ManagedObjectType& dbusData,
50181ce609eSEd Tanous                   boost::container::flat_set<IPv4AddressData>& ipv4Config)
5024a0cb85cSEd Tanous {
5034a0cb85cSEd Tanous     const std::string ipv4PathStart =
50481ce609eSEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
5054a0cb85cSEd Tanous 
5064a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
5074a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
50881ce609eSEd Tanous     for (const auto& objpath : dbusData)
5094a0cb85cSEd Tanous     {
5104a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5114a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5124a0cb85cSEd Tanous         {
5139eb808c1SEd Tanous             for (const auto& interface : objpath.second)
5144a0cb85cSEd Tanous             {
5154a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5164a0cb85cSEd Tanous                 {
5174a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5184a0cb85cSEd Tanous                     // appropriate
5194a0cb85cSEd Tanous                     std::pair<
5204a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5214a0cb85cSEd Tanous                         bool>
52281ce609eSEd Tanous                         it = ipv4Config.insert(IPv4AddressData{});
5232c70f800SEd Tanous                     IPv4AddressData& ipv4Address = *it.first;
5242c70f800SEd Tanous                     ipv4Address.id =
525271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5269eb808c1SEd Tanous                     for (const auto& property : interface.second)
5274a0cb85cSEd Tanous                     {
5284a0cb85cSEd Tanous                         if (property.first == "Address")
5294a0cb85cSEd Tanous                         {
5304a0cb85cSEd Tanous                             const std::string* address =
531abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5324a0cb85cSEd Tanous                             if (address != nullptr)
5334a0cb85cSEd Tanous                             {
5342c70f800SEd Tanous                                 ipv4Address.address = *address;
5354a0cb85cSEd Tanous                             }
5364a0cb85cSEd Tanous                         }
5374a0cb85cSEd Tanous                         else if (property.first == "Origin")
5384a0cb85cSEd Tanous                         {
5394a0cb85cSEd Tanous                             const std::string* origin =
540abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5414a0cb85cSEd Tanous                             if (origin != nullptr)
5424a0cb85cSEd Tanous                             {
5432c70f800SEd Tanous                                 ipv4Address.origin =
5444a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
5454a0cb85cSEd Tanous                                                                         true);
5464a0cb85cSEd Tanous                             }
5474a0cb85cSEd Tanous                         }
5484a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
5494a0cb85cSEd Tanous                         {
5504a0cb85cSEd Tanous                             const uint8_t* mask =
551abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
5524a0cb85cSEd Tanous                             if (mask != nullptr)
5534a0cb85cSEd Tanous                             {
5544a0cb85cSEd Tanous                                 // convert it to the string
5552c70f800SEd Tanous                                 ipv4Address.netmask = getNetmask(*mask);
5564a0cb85cSEd Tanous                             }
5574a0cb85cSEd Tanous                         }
558889ff694SAsmitha Karunanithi                         else if (property.first == "Type" ||
559889ff694SAsmitha Karunanithi                                  property.first == "Gateway")
560889ff694SAsmitha Karunanithi                         {
561889ff694SAsmitha Karunanithi                             // Type & Gateway is not used
562889ff694SAsmitha Karunanithi                         }
5634a0cb85cSEd Tanous                         else
5644a0cb85cSEd Tanous                         {
5654a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5664a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5674a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5684a0cb85cSEd Tanous                         }
5694a0cb85cSEd Tanous                     }
5704a0cb85cSEd Tanous                     // Check if given address is local, or global
5712c70f800SEd Tanous                     ipv4Address.linktype =
5722c70f800SEd Tanous                         boost::starts_with(ipv4Address.address, "169.254.")
57318659d10SJohnathan Mantey                             ? LinkType::Local
57418659d10SJohnathan Mantey                             : LinkType::Global;
5754a0cb85cSEd Tanous                 }
5764a0cb85cSEd Tanous             }
5774a0cb85cSEd Tanous         }
5784a0cb85cSEd Tanous     }
5794a0cb85cSEd Tanous }
580588c3f0dSKowalski, Kamil 
581588c3f0dSKowalski, Kamil /**
582179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
583179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
584179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
585179db1d7SKowalski, Kamil  *
586179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
587179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
588179db1d7SKowalski, Kamil  *
589179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
590179db1d7SKowalski, Kamil  */
5914a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
5921abe55efSEd Tanous                                        uint8_t* bits = nullptr)
5931abe55efSEd Tanous {
594179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
595179db1d7SKowalski, Kamil 
596179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
597179db1d7SKowalski, Kamil 
5984a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
5991abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6001abe55efSEd Tanous     {
601179db1d7SKowalski, Kamil         return false;
602179db1d7SKowalski, Kamil     }
603179db1d7SKowalski, Kamil 
6041abe55efSEd Tanous     if (bits != nullptr)
6051abe55efSEd Tanous     {
606179db1d7SKowalski, Kamil         *bits = 0;
607179db1d7SKowalski, Kamil     }
608179db1d7SKowalski, Kamil 
609543f4400SEd Tanous     char* endPtr = nullptr;
610179db1d7SKowalski, Kamil     long previousValue = 255;
611543f4400SEd Tanous     bool firstZeroInByteHit = false;
6121abe55efSEd Tanous     for (const std::string& byte : bytesInMask)
6131abe55efSEd Tanous     {
6141abe55efSEd Tanous         if (byte.empty())
6151abe55efSEd Tanous         {
6161db9ca37SKowalski, Kamil             return false;
6171db9ca37SKowalski, Kamil         }
6181db9ca37SKowalski, Kamil 
619179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6201db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
621179db1d7SKowalski, Kamil 
6224a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6234a0cb85cSEd Tanous         // is not 100% number
6241abe55efSEd Tanous         if (*endPtr != '\0')
6251abe55efSEd Tanous         {
626179db1d7SKowalski, Kamil             return false;
627179db1d7SKowalski, Kamil         }
628179db1d7SKowalski, Kamil 
629179db1d7SKowalski, Kamil         // Value should be contained in byte
6301abe55efSEd Tanous         if (value < 0 || value > 255)
6311abe55efSEd Tanous         {
632179db1d7SKowalski, Kamil             return false;
633179db1d7SKowalski, Kamil         }
634179db1d7SKowalski, Kamil 
6351abe55efSEd Tanous         if (bits != nullptr)
6361abe55efSEd Tanous         {
637179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6381abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6391abe55efSEd Tanous             {
640179db1d7SKowalski, Kamil                 return false;
641179db1d7SKowalski, Kamil             }
642179db1d7SKowalski, Kamil 
643179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
644179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
645179db1d7SKowalski, Kamil 
646179db1d7SKowalski, Kamil             // Count bits
64723a21a1cSEd Tanous             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
6481abe55efSEd Tanous             {
649e662eae8SEd Tanous                 if ((value & (1L << bitIdx)) != 0)
6501abe55efSEd Tanous                 {
6511abe55efSEd Tanous                     if (firstZeroInByteHit)
6521abe55efSEd Tanous                     {
653179db1d7SKowalski, Kamil                         // Continuity not preserved
654179db1d7SKowalski, Kamil                         return false;
6551abe55efSEd Tanous                     }
656179db1d7SKowalski, Kamil                     (*bits)++;
657179db1d7SKowalski, Kamil                 }
6581abe55efSEd Tanous                 else
6591abe55efSEd Tanous                 {
660179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
661179db1d7SKowalski, Kamil                 }
662179db1d7SKowalski, Kamil             }
663179db1d7SKowalski, Kamil         }
664179db1d7SKowalski, Kamil 
665179db1d7SKowalski, Kamil         previousValue = value;
666179db1d7SKowalski, Kamil     }
667179db1d7SKowalski, Kamil 
668179db1d7SKowalski, Kamil     return true;
669179db1d7SKowalski, Kamil }
670179db1d7SKowalski, Kamil 
671179db1d7SKowalski, Kamil /**
67201784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
673179db1d7SKowalski, Kamil  *
674179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
675179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
676179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
677179db1d7SKowalski, Kamil  *
678179db1d7SKowalski, Kamil  * @return None
679179db1d7SKowalski, Kamil  */
6804a0cb85cSEd Tanous inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
6818d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6821abe55efSEd Tanous {
68355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
684286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
6851abe55efSEd Tanous         if (ec)
6861abe55efSEd Tanous         {
687a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
6881abe55efSEd Tanous         }
689179db1d7SKowalski, Kamil         },
690179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
691179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
692179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
693179db1d7SKowalski, Kamil }
694179db1d7SKowalski, Kamil 
695244b6d5bSGunnar Mills inline void updateIPv4DefaultGateway(
696244b6d5bSGunnar Mills     const std::string& ifaceId, const std::string& gateway,
697244b6d5bSGunnar Mills     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
6989010ec2eSRavi Teja {
6999010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7009010ec2eSRavi Teja         [asyncResp](const boost::system::error_code ec) {
7019010ec2eSRavi Teja         if (ec)
7029010ec2eSRavi Teja         {
7039010ec2eSRavi Teja             messages::internalError(asyncResp->res);
7049010ec2eSRavi Teja             return;
7059010ec2eSRavi Teja         }
7069010ec2eSRavi Teja         asyncResp->res.result(boost::beast::http::status::no_content);
7079010ec2eSRavi Teja         },
7089010ec2eSRavi Teja         "xyz.openbmc_project.Network",
7099010ec2eSRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
7109010ec2eSRavi Teja         "org.freedesktop.DBus.Properties", "Set",
7119010ec2eSRavi Teja         "xyz.openbmc_project.Network.EthernetInterface", "DefaultGateway",
712168e20c1SEd Tanous         dbus::utility::DbusVariantType(gateway));
7139010ec2eSRavi Teja }
714179db1d7SKowalski, Kamil /**
71501784826SJohnathan Mantey  * @brief Creates a static IPv4 entry
716179db1d7SKowalski, Kamil  *
71701784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
71801784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
71901784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
72001784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
721179db1d7SKowalski, Kamil  * @param[io] asyncResp    Response object that will be returned to client
722179db1d7SKowalski, Kamil  *
723179db1d7SKowalski, Kamil  * @return None
724179db1d7SKowalski, Kamil  */
725cb13a392SEd Tanous inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
726cb13a392SEd Tanous                        const std::string& gateway, const std::string& address,
7278d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
7281abe55efSEd Tanous {
729*002d39b4SEd Tanous     auto createIpHandler =
730*002d39b4SEd Tanous         [asyncResp, ifaceId, gateway](const boost::system::error_code ec) {
7311abe55efSEd Tanous         if (ec)
7321abe55efSEd Tanous         {
733a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
7349010ec2eSRavi Teja             return;
735179db1d7SKowalski, Kamil         }
7369010ec2eSRavi Teja         updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
7379010ec2eSRavi Teja     };
7389010ec2eSRavi Teja 
7399010ec2eSRavi Teja     crow::connections::systemBus->async_method_call(
7409010ec2eSRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
741179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
742179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
74301784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
744179db1d7SKowalski, Kamil         gateway);
745179db1d7SKowalski, Kamil }
746e48c0fc5SRavi Teja 
747e48c0fc5SRavi Teja /**
74801784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
74901784826SJohnathan Mantey  * static IPv4 entry
75001784826SJohnathan Mantey  *
75101784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
75201784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
75301784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75401784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75501784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
75601784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
75701784826SJohnathan Mantey  *
75801784826SJohnathan Mantey  * @return None
75901784826SJohnathan Mantey  */
7608d1b46d7Szhanghch05 inline void
7618d1b46d7Szhanghch05     deleteAndCreateIPv4(const std::string& ifaceId, const std::string& id,
7628d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& gateway,
76301784826SJohnathan Mantey                         const std::string& address,
7648d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
76501784826SJohnathan Mantey {
76601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
76701784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
76801784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
76901784826SJohnathan Mantey         if (ec)
77001784826SJohnathan Mantey         {
77101784826SJohnathan Mantey             messages::internalError(asyncResp->res);
7729010ec2eSRavi Teja             return;
77301784826SJohnathan Mantey         }
7749010ec2eSRavi Teja 
77501784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
776*002d39b4SEd Tanous             [asyncResp, ifaceId, gateway](const boost::system::error_code ec2) {
77723a21a1cSEd Tanous             if (ec2)
77801784826SJohnathan Mantey             {
77901784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
7809010ec2eSRavi Teja                 return;
78101784826SJohnathan Mantey             }
7829010ec2eSRavi Teja             updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
78301784826SJohnathan Mantey             },
78401784826SJohnathan Mantey             "xyz.openbmc_project.Network",
78501784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
78601784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
78701784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
78801784826SJohnathan Mantey             prefixLength, gateway);
78901784826SJohnathan Mantey         },
79001784826SJohnathan Mantey         "xyz.openbmc_project.Network",
79101784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
79201784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
79301784826SJohnathan Mantey }
79401784826SJohnathan Mantey 
79501784826SJohnathan Mantey /**
796e48c0fc5SRavi Teja  * @brief Deletes given IPv6
797e48c0fc5SRavi Teja  *
798e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
799e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
800e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
801e48c0fc5SRavi Teja  *
802e48c0fc5SRavi Teja  * @return None
803e48c0fc5SRavi Teja  */
804e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8058d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
806e48c0fc5SRavi Teja {
807e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
808286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
809e48c0fc5SRavi Teja         if (ec)
810e48c0fc5SRavi Teja         {
811e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
812e48c0fc5SRavi Teja         }
813e48c0fc5SRavi Teja         },
814e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
815e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
816e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
817e48c0fc5SRavi Teja }
818e48c0fc5SRavi Teja 
819e48c0fc5SRavi Teja /**
82001784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
82101784826SJohnathan Mantey  * static IPv6 entry
82201784826SJohnathan Mantey  *
82301784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
82401784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
82501784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
82601784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
82701784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
82801784826SJohnathan Mantey  *
82901784826SJohnathan Mantey  * @return None
83001784826SJohnathan Mantey  */
8318d1b46d7Szhanghch05 inline void
8328d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8338d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8348d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
83501784826SJohnathan Mantey {
83601784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
83701784826SJohnathan Mantey         [asyncResp, ifaceId, address,
83801784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
83901784826SJohnathan Mantey         if (ec)
84001784826SJohnathan Mantey         {
84101784826SJohnathan Mantey             messages::internalError(asyncResp->res);
84201784826SJohnathan Mantey         }
84301784826SJohnathan Mantey         crow::connections::systemBus->async_method_call(
84423a21a1cSEd Tanous             [asyncResp](const boost::system::error_code ec2) {
84523a21a1cSEd Tanous             if (ec2)
84601784826SJohnathan Mantey             {
84701784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
84801784826SJohnathan Mantey             }
84901784826SJohnathan Mantey             },
85001784826SJohnathan Mantey             "xyz.openbmc_project.Network",
85101784826SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
85201784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Create", "IP",
85301784826SJohnathan Mantey             "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
85401784826SJohnathan Mantey             prefixLength, "");
85501784826SJohnathan Mantey         },
85601784826SJohnathan Mantey         "xyz.openbmc_project.Network",
85701784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
85801784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
85901784826SJohnathan Mantey }
86001784826SJohnathan Mantey 
86101784826SJohnathan Mantey /**
862e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
863e48c0fc5SRavi Teja  *
864e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
865e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
866e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
867e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
868e48c0fc5SRavi Teja  *
869e48c0fc5SRavi Teja  * @return None
870e48c0fc5SRavi Teja  */
87101784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
87201784826SJohnathan Mantey                        const std::string& address,
8738d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
874e48c0fc5SRavi Teja {
875e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
876e48c0fc5SRavi Teja         if (ec)
877e48c0fc5SRavi Teja         {
878e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
879e48c0fc5SRavi Teja         }
880e48c0fc5SRavi Teja     };
881e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8824e0453b1SGunnar Mills     // does not have associated gateway property
883e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
884e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
885e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
886e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
887e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
888e48c0fc5SRavi Teja         "");
889e48c0fc5SRavi Teja }
890e48c0fc5SRavi Teja 
891179db1d7SKowalski, Kamil /**
892179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
893179db1d7SKowalski, Kamil  * Object
894179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8954a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
896179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
897179db1d7SKowalski, Kamil  * into JSON
898179db1d7SKowalski, Kamil  */
899179db1d7SKowalski, Kamil template <typename CallbackFunc>
90081ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9011abe55efSEd Tanous                           CallbackFunc&& callback)
9021abe55efSEd Tanous {
90355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
904f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
905f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
90681ce609eSEd Tanous             const boost::system::error_code errorCode,
907711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
90855c7b7a2SEd Tanous         EthernetInterfaceData ethData{};
9094a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData> ipv4Data;
910e48c0fc5SRavi Teja         boost::container::flat_set<IPv6AddressData> ipv6Data;
911179db1d7SKowalski, Kamil 
91281ce609eSEd Tanous         if (errorCode)
9131abe55efSEd Tanous         {
91401784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
915179db1d7SKowalski, Kamil             return;
916179db1d7SKowalski, Kamil         }
917179db1d7SKowalski, Kamil 
918*002d39b4SEd Tanous         bool found = extractEthernetInterfaceData(ethifaceId, resp, ethData);
9194c9afe43SEd Tanous         if (!found)
9204c9afe43SEd Tanous         {
92101784826SJohnathan Mantey             callback(false, ethData, ipv4Data, ipv6Data);
9224c9afe43SEd Tanous             return;
9234c9afe43SEd Tanous         }
9244c9afe43SEd Tanous 
9252c70f800SEd Tanous         extractIPData(ethifaceId, resp, ipv4Data);
926179db1d7SKowalski, Kamil         // Fix global GW
9271abe55efSEd Tanous         for (IPv4AddressData& ipv4 : ipv4Data)
9281abe55efSEd Tanous         {
929c619141bSRavi Teja             if (((ipv4.linktype == LinkType::Global) &&
930c619141bSRavi Teja                  (ipv4.gateway == "0.0.0.0")) ||
9319010ec2eSRavi Teja                 (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9321abe55efSEd Tanous             {
93382695a5bSJiaqing Zhao                 ipv4.gateway = ethData.defaultGateway;
934179db1d7SKowalski, Kamil             }
935179db1d7SKowalski, Kamil         }
936179db1d7SKowalski, Kamil 
9372c70f800SEd Tanous         extractIPV6Data(ethifaceId, resp, ipv6Data);
9384e0453b1SGunnar Mills         // Finally make a callback with useful data
93901784826SJohnathan Mantey         callback(true, ethData, ipv4Data, ipv6Data);
940179db1d7SKowalski, Kamil         },
941179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
942179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
943271584abSEd Tanous }
944179db1d7SKowalski, Kamil 
945179db1d7SKowalski, Kamil /**
9469391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9479391bb9cSRapkiewicz, Pawel  * Manager
9481abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9491abe55efSEd Tanous  * into JSON.
9509391bb9cSRapkiewicz, Pawel  */
9519391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9521abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9531abe55efSEd Tanous {
95455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
955f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
95681ce609eSEd Tanous             const boost::system::error_code errorCode,
957711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9581abe55efSEd Tanous         // Callback requires vector<string> to retrieve all available
9591abe55efSEd Tanous         // ethernet interfaces
9602c70f800SEd Tanous         boost::container::flat_set<std::string> ifaceList;
9612c70f800SEd Tanous         ifaceList.reserve(resp.size());
96281ce609eSEd Tanous         if (errorCode)
9631abe55efSEd Tanous         {
9642c70f800SEd Tanous             callback(false, ifaceList);
9659391bb9cSRapkiewicz, Pawel             return;
9669391bb9cSRapkiewicz, Pawel         }
9679391bb9cSRapkiewicz, Pawel 
9689391bb9cSRapkiewicz, Pawel         // Iterate over all retrieved ObjectPaths.
9694a0cb85cSEd Tanous         for (const auto& objpath : resp)
9701abe55efSEd Tanous         {
9719391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
9724a0cb85cSEd Tanous             for (const auto& interface : objpath.second)
9731abe55efSEd Tanous             {
9741abe55efSEd Tanous                 // If interface is
9754a0cb85cSEd Tanous                 // xyz.openbmc_project.Network.EthernetInterface, this is
9764a0cb85cSEd Tanous                 // what we're looking for.
9779391bb9cSRapkiewicz, Pawel                 if (interface.first ==
9781abe55efSEd Tanous                     "xyz.openbmc_project.Network.EthernetInterface")
9791abe55efSEd Tanous                 {
9802dfd18efSEd Tanous                     std::string ifaceId = objpath.first.filename();
9812dfd18efSEd Tanous                     if (ifaceId.empty())
9821abe55efSEd Tanous                     {
9832dfd18efSEd Tanous                         continue;
9849391bb9cSRapkiewicz, Pawel                     }
9852dfd18efSEd Tanous                     // and put it into output vector.
9862dfd18efSEd Tanous                     ifaceList.emplace(ifaceId);
9879391bb9cSRapkiewicz, Pawel                 }
9889391bb9cSRapkiewicz, Pawel             }
9899391bb9cSRapkiewicz, Pawel         }
990a434f2bdSEd Tanous         // Finally make a callback with useful data
9912c70f800SEd Tanous         callback(true, ifaceList);
9929391bb9cSRapkiewicz, Pawel         },
993aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
994aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
995271584abSEd Tanous }
9969391bb9cSRapkiewicz, Pawel 
9974f48d5f6SEd Tanous inline void
9984f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
9998d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10001abe55efSEd Tanous {
1001ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1002ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1003ab6554f1SJoshi-Mansi     {
1004ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1005ab6554f1SJoshi-Mansi                                            "HostName");
1006ab6554f1SJoshi-Mansi         return;
1007ab6554f1SJoshi-Mansi     }
1008bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1009bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10104a0cb85cSEd Tanous         if (ec)
10114a0cb85cSEd Tanous         {
1012a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
10131abe55efSEd Tanous         }
1014bc0bd6e0SEd Tanous         },
1015bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1016bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1017bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1018168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1019588c3f0dSKowalski, Kamil }
1020588c3f0dSKowalski, Kamil 
10214f48d5f6SEd Tanous inline void
102235fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
102335fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
102435fb5311STejas Patil {
102535fb5311STejas Patil     sdbusplus::message::object_path objPath =
102635fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
102735fb5311STejas Patil     crow::connections::systemBus->async_method_call(
102835fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
102935fb5311STejas Patil         if (ec)
103035fb5311STejas Patil         {
103135fb5311STejas Patil             messages::internalError(asyncResp->res);
103235fb5311STejas Patil         }
103335fb5311STejas Patil         },
103435fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
103535fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
103635fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
103735fb5311STejas Patil         std::variant<size_t>(mtuSize));
103835fb5311STejas Patil }
103935fb5311STejas Patil 
104035fb5311STejas Patil inline void
10414f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1042bf648f77SEd Tanous                           const std::string& domainname,
10438d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1044ab6554f1SJoshi-Mansi {
1045ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1046ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1047ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1048ab6554f1SJoshi-Mansi         if (ec)
1049ab6554f1SJoshi-Mansi         {
1050ab6554f1SJoshi-Mansi             messages::internalError(asyncResp->res);
1051ab6554f1SJoshi-Mansi         }
1052ab6554f1SJoshi-Mansi         },
1053ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1054ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1055ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1056ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1057168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1058ab6554f1SJoshi-Mansi }
1059ab6554f1SJoshi-Mansi 
10604f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1061bf648f77SEd Tanous {
1062bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1063bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1064bf648f77SEd Tanous     {
1065bf648f77SEd Tanous         return false;
1066bf648f77SEd Tanous     }
1067bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1068bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1069bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1070bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1071bf648f77SEd Tanous     const std::regex pattern(
1072bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1073bf648f77SEd Tanous 
1074bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1075bf648f77SEd Tanous }
1076bf648f77SEd Tanous 
10774f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1078bf648f77SEd Tanous {
1079bf648f77SEd Tanous     // Can have multiple subdomains
1080bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
10810fda0f12SGeorge Liu     const std::regex pattern(
10820fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1083bf648f77SEd Tanous 
1084bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1085bf648f77SEd Tanous }
1086bf648f77SEd Tanous 
10874f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10888d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1089ab6554f1SJoshi-Mansi {
1090ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1091ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1092ab6554f1SJoshi-Mansi     {
1093ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1094ab6554f1SJoshi-Mansi         return;
1095ab6554f1SJoshi-Mansi     }
1096ab6554f1SJoshi-Mansi 
1097ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1098ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1099ab6554f1SJoshi-Mansi     {
1100ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1101ab6554f1SJoshi-Mansi         return;
1102ab6554f1SJoshi-Mansi     }
1103ab6554f1SJoshi-Mansi 
1104ab6554f1SJoshi-Mansi     std::string hostname;
1105ab6554f1SJoshi-Mansi     std::string domainname;
1106ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1107ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1108ab6554f1SJoshi-Mansi 
1109ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1110ab6554f1SJoshi-Mansi     {
1111ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1112ab6554f1SJoshi-Mansi         return;
1113ab6554f1SJoshi-Mansi     }
1114ab6554f1SJoshi-Mansi 
1115ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1116ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1117ab6554f1SJoshi-Mansi }
1118ab6554f1SJoshi-Mansi 
11194f48d5f6SEd Tanous inline void
11204f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1121bf648f77SEd Tanous                           const std::string& macAddress,
11228d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1123d577665bSRatan Gupta {
1124d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1125d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1126d577665bSRatan Gupta         if (ec)
1127d577665bSRatan Gupta         {
1128d577665bSRatan Gupta             messages::internalError(asyncResp->res);
1129d577665bSRatan Gupta             return;
1130d577665bSRatan Gupta         }
1131d577665bSRatan Gupta         },
1132d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1133d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1134d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1135d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1136168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1137d577665bSRatan Gupta }
1138286b9118SJohnathan Mantey 
11394f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11404f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11414f48d5f6SEd Tanous                            const bool v6Value,
11428d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1143da131a9aSJennifer Lee {
11442c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1145da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1146da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1147da131a9aSJennifer Lee         if (ec)
1148da131a9aSJennifer Lee         {
1149da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1150da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1151da131a9aSJennifer Lee             return;
1152da131a9aSJennifer Lee         }
11538f7e9c19SJayaprakash Mutyala         messages::success(asyncResp->res);
1154da131a9aSJennifer Lee         },
1155da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1156da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1157da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1158da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1159168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1160da131a9aSJennifer Lee }
11611f8c7b5dSJohnathan Mantey 
11624f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1163eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11648d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1165eeedda23SJohnathan Mantey {
1166eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1167eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1168eeedda23SJohnathan Mantey         if (ec)
1169eeedda23SJohnathan Mantey         {
1170eeedda23SJohnathan Mantey             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1171eeedda23SJohnathan Mantey             messages::internalError(asyncResp->res);
1172eeedda23SJohnathan Mantey             return;
1173eeedda23SJohnathan Mantey         }
1174eeedda23SJohnathan Mantey         },
1175eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1176eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1177eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1178eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1179168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1180eeedda23SJohnathan Mantey }
1181eeedda23SJohnathan Mantey 
11824f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11838d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1184da131a9aSJennifer Lee {
1185da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1186da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1187da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1188da131a9aSJennifer Lee         if (ec)
1189da131a9aSJennifer Lee         {
1190da131a9aSJennifer Lee             BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1191da131a9aSJennifer Lee             messages::internalError(asyncResp->res);
1192da131a9aSJennifer Lee             return;
1193da131a9aSJennifer Lee         }
1194da131a9aSJennifer Lee         },
1195da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1196da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1197da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1198da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1199168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1200da131a9aSJennifer Lee }
1201d577665bSRatan Gupta 
12024f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12031f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1204f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1205f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12068d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1207da131a9aSJennifer Lee {
120882695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
120982695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1210da131a9aSJennifer Lee 
12111f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12121f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12131f8c7b5dSJohnathan Mantey 
12141f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12151f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1216da131a9aSJennifer Lee     {
12171f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12181f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12191f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12201f8c7b5dSJohnathan Mantey         {
1221bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1222bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12231f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1224da131a9aSJennifer Lee             return;
1225da131a9aSJennifer Lee         }
12261f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12271f8c7b5dSJohnathan Mantey     }
12281f8c7b5dSJohnathan Mantey     else
1229da131a9aSJennifer Lee     {
12301f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12311f8c7b5dSJohnathan Mantey     }
12321f8c7b5dSJohnathan Mantey 
12331f8c7b5dSJohnathan Mantey     bool nextDNS{};
123482695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
12351f8c7b5dSJohnathan Mantey     {
123682695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
12371f8c7b5dSJohnathan Mantey         {
12381f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12391f8c7b5dSJohnathan Mantey             return;
12401f8c7b5dSJohnathan Mantey         }
124182695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12421f8c7b5dSJohnathan Mantey     }
124382695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
12441f8c7b5dSJohnathan Mantey     {
124582695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12461f8c7b5dSJohnathan Mantey     }
124782695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
12481f8c7b5dSJohnathan Mantey     {
124982695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
12501f8c7b5dSJohnathan Mantey     }
12511f8c7b5dSJohnathan Mantey     else
12521f8c7b5dSJohnathan Mantey     {
125382695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
12541f8c7b5dSJohnathan Mantey     }
12551f8c7b5dSJohnathan Mantey 
12561f8c7b5dSJohnathan Mantey     bool nextNTP{};
125782695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
12581f8c7b5dSJohnathan Mantey     {
125982695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
12601f8c7b5dSJohnathan Mantey         {
12611f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12621f8c7b5dSJohnathan Mantey             return;
12631f8c7b5dSJohnathan Mantey         }
126482695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12651f8c7b5dSJohnathan Mantey     }
126682695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
12671f8c7b5dSJohnathan Mantey     {
126882695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12691f8c7b5dSJohnathan Mantey     }
127082695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12711f8c7b5dSJohnathan Mantey     {
127282695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12731f8c7b5dSJohnathan Mantey     }
12741f8c7b5dSJohnathan Mantey     else
12751f8c7b5dSJohnathan Mantey     {
127682695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12771f8c7b5dSJohnathan Mantey     }
12781f8c7b5dSJohnathan Mantey 
12791f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
128082695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12811f8c7b5dSJohnathan Mantey     {
128282695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
12831f8c7b5dSJohnathan Mantey         {
12841f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12851f8c7b5dSJohnathan Mantey             return;
12861f8c7b5dSJohnathan Mantey         }
128782695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12881f8c7b5dSJohnathan Mantey     }
128982695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12901f8c7b5dSJohnathan Mantey     {
129182695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12921f8c7b5dSJohnathan Mantey     }
129382695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12941f8c7b5dSJohnathan Mantey     {
129582695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12961f8c7b5dSJohnathan Mantey     }
12971f8c7b5dSJohnathan Mantey     else
12981f8c7b5dSJohnathan Mantey     {
129982695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
13001f8c7b5dSJohnathan Mantey     }
13011f8c7b5dSJohnathan Mantey 
1302da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13031f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13041f8c7b5dSJohnathan Mantey                    asyncResp);
1305da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13061f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1307da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13081f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13091f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13101f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1311da131a9aSJennifer Lee }
131201784826SJohnathan Mantey 
13134f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13142c70f800SEd Tanous     getNextStaticIpEntry(
1315bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1316bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
131701784826SJohnathan Mantey {
131817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
131917a897dfSManojkiran Eda         return value.origin == "Static";
132017a897dfSManojkiran Eda     });
132101784826SJohnathan Mantey }
132201784826SJohnathan Mantey 
13234f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13242c70f800SEd Tanous     getNextStaticIpEntry(
1325bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1326bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
132701784826SJohnathan Mantey {
132817a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
132917a897dfSManojkiran Eda         return value.origin == "Static";
133017a897dfSManojkiran Eda     });
133101784826SJohnathan Mantey }
133201784826SJohnathan Mantey 
13334f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1334f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
133501784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13368d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13371abe55efSEd Tanous {
133801784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1339f476acbfSRatan Gupta     {
134071f52d96SEd Tanous         messages::propertyValueTypeError(
134171f52d96SEd Tanous             asyncResp->res,
1342bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1343d1d50814SRavi Teja             "IPv4StaticAddresses");
1344f476acbfSRatan Gupta         return;
1345f476acbfSRatan Gupta     }
1346f476acbfSRatan Gupta 
1347271584abSEd Tanous     unsigned entryIdx = 1;
134801784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
134901784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
135001784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
135101784826SJohnathan Mantey     // into the NIC.
135285ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13532c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
135401784826SJohnathan Mantey 
1355537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13561abe55efSEd Tanous     {
13574a0cb85cSEd Tanous         std::string pathString =
1358d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1359179db1d7SKowalski, Kamil 
136001784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1361f476acbfSRatan Gupta         {
1362537174c4SEd Tanous             std::optional<std::string> address;
1363537174c4SEd Tanous             std::optional<std::string> subnetMask;
1364537174c4SEd Tanous             std::optional<std::string> gateway;
1365537174c4SEd Tanous 
1366537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13677e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13687e27d832SJohnathan Mantey                                      "Gateway", gateway))
1369537174c4SEd Tanous             {
137001784826SJohnathan Mantey                 messages::propertyValueFormatError(
137171f52d96SEd Tanous                     asyncResp->res,
137271f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
137371f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
137471f52d96SEd Tanous                     pathString);
1375537174c4SEd Tanous                 return;
1376179db1d7SKowalski, Kamil             }
1377179db1d7SKowalski, Kamil 
137801784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
137901784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
138001784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
138101784826SJohnathan Mantey             // current request.
1382271584abSEd Tanous             const std::string* addr = nullptr;
1383271584abSEd Tanous             const std::string* gw = nullptr;
138401784826SJohnathan Mantey             uint8_t prefixLength = 0;
138501784826SJohnathan Mantey             bool errorInEntry = false;
1386537174c4SEd Tanous             if (address)
13871abe55efSEd Tanous             {
138801784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
13891abe55efSEd Tanous                 {
139001784826SJohnathan Mantey                     addr = &(*address);
13914a0cb85cSEd Tanous                 }
139201784826SJohnathan Mantey                 else
139301784826SJohnathan Mantey                 {
1394bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1395bf648f77SEd Tanous                                                        pathString + "/Address");
139601784826SJohnathan Mantey                     errorInEntry = true;
139701784826SJohnathan Mantey                 }
139801784826SJohnathan Mantey             }
139985ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
140001784826SJohnathan Mantey             {
140185ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
140201784826SJohnathan Mantey             }
140301784826SJohnathan Mantey             else
140401784826SJohnathan Mantey             {
140501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
140601784826SJohnathan Mantey                                           pathString + "/Address");
140701784826SJohnathan Mantey                 errorInEntry = true;
14084a0cb85cSEd Tanous             }
14094a0cb85cSEd Tanous 
1410537174c4SEd Tanous             if (subnetMask)
14114a0cb85cSEd Tanous             {
1412537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14134a0cb85cSEd Tanous                 {
1414f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1415537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14164a0cb85cSEd Tanous                         pathString + "/SubnetMask");
141701784826SJohnathan Mantey                     errorInEntry = true;
14184a0cb85cSEd Tanous                 }
14194a0cb85cSEd Tanous             }
142085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14214a0cb85cSEd Tanous             {
142285ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
142301784826SJohnathan Mantey                                                 &prefixLength))
14244a0cb85cSEd Tanous                 {
142501784826SJohnathan Mantey                     messages::propertyValueFormatError(
142685ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
142701784826SJohnathan Mantey                         pathString + "/SubnetMask");
142801784826SJohnathan Mantey                     errorInEntry = true;
14294a0cb85cSEd Tanous                 }
14304a0cb85cSEd Tanous             }
14311abe55efSEd Tanous             else
14321abe55efSEd Tanous             {
143301784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
143401784826SJohnathan Mantey                                           pathString + "/SubnetMask");
143501784826SJohnathan Mantey                 errorInEntry = true;
143601784826SJohnathan Mantey             }
143701784826SJohnathan Mantey 
143801784826SJohnathan Mantey             if (gateway)
143901784826SJohnathan Mantey             {
144001784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
144101784826SJohnathan Mantey                 {
144201784826SJohnathan Mantey                     gw = &(*gateway);
144301784826SJohnathan Mantey                 }
144401784826SJohnathan Mantey                 else
144501784826SJohnathan Mantey                 {
1446bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1447bf648f77SEd Tanous                                                        pathString + "/Gateway");
144801784826SJohnathan Mantey                     errorInEntry = true;
144901784826SJohnathan Mantey                 }
145001784826SJohnathan Mantey             }
145185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
145201784826SJohnathan Mantey             {
145385ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
145401784826SJohnathan Mantey             }
145501784826SJohnathan Mantey             else
14561abe55efSEd Tanous             {
1457a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14584a0cb85cSEd Tanous                                           pathString + "/Gateway");
145901784826SJohnathan Mantey                 errorInEntry = true;
14604a0cb85cSEd Tanous             }
14614a0cb85cSEd Tanous 
146201784826SJohnathan Mantey             if (errorInEntry)
14631abe55efSEd Tanous             {
146401784826SJohnathan Mantey                 return;
14654a0cb85cSEd Tanous             }
14664a0cb85cSEd Tanous 
146785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14681abe55efSEd Tanous             {
146985ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1470bf648f77SEd Tanous                                     *addr, asyncResp);
147185ffe86aSJiaqing Zhao                 nicIpEntry =
147285ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1473588c3f0dSKowalski, Kamil             }
147401784826SJohnathan Mantey             else
147501784826SJohnathan Mantey             {
1476cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1477cb13a392SEd Tanous                            asyncResp);
14784a0cb85cSEd Tanous             }
14794a0cb85cSEd Tanous             entryIdx++;
14804a0cb85cSEd Tanous         }
148101784826SJohnathan Mantey         else
148201784826SJohnathan Mantey         {
148385ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
148401784826SJohnathan Mantey             {
148501784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
148601784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
148701784826SJohnathan Mantey                 // in error, so bail out.
148801784826SJohnathan Mantey                 if (thisJson.is_null())
148901784826SJohnathan Mantey                 {
149001784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
149101784826SJohnathan Mantey                     return;
149201784826SJohnathan Mantey                 }
149301784826SJohnathan Mantey                 messages::propertyValueFormatError(
149471f52d96SEd Tanous                     asyncResp->res,
149571f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
149671f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
149771f52d96SEd Tanous                     pathString);
149801784826SJohnathan Mantey                 return;
149901784826SJohnathan Mantey             }
150001784826SJohnathan Mantey 
150101784826SJohnathan Mantey             if (thisJson.is_null())
150201784826SJohnathan Mantey             {
150385ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
150401784826SJohnathan Mantey             }
150585ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
150601784826SJohnathan Mantey             {
150785ffe86aSJiaqing Zhao                 nicIpEntry =
150885ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
150901784826SJohnathan Mantey             }
151001784826SJohnathan Mantey             entryIdx++;
151101784826SJohnathan Mantey         }
151201784826SJohnathan Mantey     }
15134a0cb85cSEd Tanous }
15144a0cb85cSEd Tanous 
15154f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1516f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1517f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15188d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1519f85837bfSRAJESWARAN THILLAIGOVINDAN {
1520f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1521286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1522f85837bfSRAJESWARAN THILLAIGOVINDAN         if (ec)
1523f85837bfSRAJESWARAN THILLAIGOVINDAN         {
1524f85837bfSRAJESWARAN THILLAIGOVINDAN             messages::internalError(asyncResp->res);
1525f85837bfSRAJESWARAN THILLAIGOVINDAN             return;
1526f85837bfSRAJESWARAN THILLAIGOVINDAN         }
1527f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1528f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1529f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1530f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1531bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1532168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1533f85837bfSRAJESWARAN THILLAIGOVINDAN }
1534f85837bfSRAJESWARAN THILLAIGOVINDAN 
15354f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1536f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
153701784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15388d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1539e48c0fc5SRavi Teja {
154001784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1541e48c0fc5SRavi Teja     {
154271f52d96SEd Tanous         messages::propertyValueTypeError(
154371f52d96SEd Tanous             asyncResp->res,
1544bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1545e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1546e48c0fc5SRavi Teja         return;
1547e48c0fc5SRavi Teja     }
1548271584abSEd Tanous     size_t entryIdx = 1;
154985ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15502c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1551f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1552e48c0fc5SRavi Teja     {
1553e48c0fc5SRavi Teja         std::string pathString =
1554e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1555e48c0fc5SRavi Teja 
155601784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1557e48c0fc5SRavi Teja         {
1558e48c0fc5SRavi Teja             std::optional<std::string> address;
1559e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1560f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1561bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1562bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1563e48c0fc5SRavi Teja             {
156401784826SJohnathan Mantey                 messages::propertyValueFormatError(
156571f52d96SEd Tanous                     asyncResp->res,
156671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
156771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
156871f52d96SEd Tanous                     pathString);
1569e48c0fc5SRavi Teja                 return;
1570e48c0fc5SRavi Teja             }
1571e48c0fc5SRavi Teja 
1572543f4400SEd Tanous             const std::string* addr = nullptr;
1573543f4400SEd Tanous             uint8_t prefix = 0;
157401784826SJohnathan Mantey 
157501784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
157601784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
157701784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
157801784826SJohnathan Mantey             // current request.
1579e48c0fc5SRavi Teja             if (address)
1580e48c0fc5SRavi Teja             {
158101784826SJohnathan Mantey                 addr = &(*address);
1582e48c0fc5SRavi Teja             }
158385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
158401784826SJohnathan Mantey             {
158585ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
158601784826SJohnathan Mantey             }
158701784826SJohnathan Mantey             else
158801784826SJohnathan Mantey             {
158901784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
159001784826SJohnathan Mantey                                           pathString + "/Address");
159101784826SJohnathan Mantey                 return;
1592e48c0fc5SRavi Teja             }
1593e48c0fc5SRavi Teja 
1594e48c0fc5SRavi Teja             if (prefixLength)
1595e48c0fc5SRavi Teja             {
159601784826SJohnathan Mantey                 prefix = *prefixLength;
159701784826SJohnathan Mantey             }
159885ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1599e48c0fc5SRavi Teja             {
160085ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1601e48c0fc5SRavi Teja             }
1602e48c0fc5SRavi Teja             else
1603e48c0fc5SRavi Teja             {
1604e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1605e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
160601784826SJohnathan Mantey                 return;
1607e48c0fc5SRavi Teja             }
1608e48c0fc5SRavi Teja 
160985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1610e48c0fc5SRavi Teja             {
161185ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1612e48c0fc5SRavi Teja                                     asyncResp);
161385ffe86aSJiaqing Zhao                 nicIpEntry =
161485ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
161501784826SJohnathan Mantey             }
161601784826SJohnathan Mantey             else
161701784826SJohnathan Mantey             {
161801784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1619e48c0fc5SRavi Teja             }
1620e48c0fc5SRavi Teja             entryIdx++;
1621e48c0fc5SRavi Teja         }
162201784826SJohnathan Mantey         else
162301784826SJohnathan Mantey         {
162485ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
162501784826SJohnathan Mantey             {
162601784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
162701784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
162801784826SJohnathan Mantey                 // in error, so bail out.
162901784826SJohnathan Mantey                 if (thisJson.is_null())
163001784826SJohnathan Mantey                 {
163101784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
163201784826SJohnathan Mantey                     return;
163301784826SJohnathan Mantey                 }
163401784826SJohnathan Mantey                 messages::propertyValueFormatError(
163571f52d96SEd Tanous                     asyncResp->res,
163671f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
163771f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
163871f52d96SEd Tanous                     pathString);
163901784826SJohnathan Mantey                 return;
164001784826SJohnathan Mantey             }
164101784826SJohnathan Mantey 
164201784826SJohnathan Mantey             if (thisJson.is_null())
164301784826SJohnathan Mantey             {
164485ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
164501784826SJohnathan Mantey             }
164685ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
164701784826SJohnathan Mantey             {
164885ffe86aSJiaqing Zhao                 nicIpEntry =
164985ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
165001784826SJohnathan Mantey             }
165101784826SJohnathan Mantey             entryIdx++;
165201784826SJohnathan Mantey         }
165301784826SJohnathan Mantey     }
1654e48c0fc5SRavi Teja }
1655e48c0fc5SRavi Teja 
16564f48d5f6SEd Tanous inline void parseInterfaceData(
16578d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16588d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1659e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
166001784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16614a0cb85cSEd Tanous {
1662eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1663eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1664eeedda23SJohnathan Mantey 
16652c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
166681ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16672c70f800SEd Tanous     jsonResponse["@odata.id"] =
166881ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16692c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1670eeedda23SJohnathan Mantey 
1671eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1672eeedda23SJohnathan Mantey 
1673eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1674eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1675b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1676eeedda23SJohnathan Mantey         if (ec)
1677029573d4SEd Tanous         {
1678eeedda23SJohnathan Mantey             return;
1679eeedda23SJohnathan Mantey         }
1680eeedda23SJohnathan Mantey 
1681914e2d5dSEd Tanous         health->inventory = resp;
1682eeedda23SJohnathan Mantey         },
1683eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1684eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1685bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1686bf648f77SEd Tanous         inventoryForEthernet);
1687eeedda23SJohnathan Mantey 
1688eeedda23SJohnathan Mantey     health->populate();
1689eeedda23SJohnathan Mantey 
1690eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1691eeedda23SJohnathan Mantey     {
16922c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
16932c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1694029573d4SEd Tanous     }
1695029573d4SEd Tanous     else
1696029573d4SEd Tanous     {
16972c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
16982c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1699029573d4SEd Tanous     }
1700aa05fb27SJohnathan Mantey 
17012c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17022c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
170335fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
170482695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
17052c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
170682695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
170782695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
170882695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
170982695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
17101f8c7b5dSJohnathan Mantey 
17112c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
171282695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
17131f8c7b5dSJohnathan Mantey                                                                : "Disabled";
171482695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
171582695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
171682695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
17172a133282Smanojkiraneda 
171882695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
17194a0cb85cSEd Tanous     {
172082695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1721ab6554f1SJoshi-Mansi 
1722ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1723ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1724ab6554f1SJoshi-Mansi         // FQDN
172582695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1726d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1727d24bfc7aSJennifer Lee         {
17282c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1729d24bfc7aSJennifer Lee         }
17302c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17314a0cb85cSEd Tanous     }
17324a0cb85cSEd Tanous 
17332c70f800SEd Tanous     jsonResponse["VLANs"] = {
1734bf648f77SEd Tanous         {"@odata.id",
1735bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1736fda13ad2SSunitha Harish 
17372c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17382c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17394a0cb85cSEd Tanous 
17402c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17412c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17422c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17432c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17449eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17454a0cb85cSEd Tanous     {
1746fa5053a6SGunnar Mills 
17472c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1748fa5053a6SGunnar Mills         if (gatewayStr.empty())
1749fa5053a6SGunnar Mills         {
1750fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1751fa5053a6SGunnar Mills         }
17521476687dSEd Tanous         nlohmann::json::object_t ipv4;
17531476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
17541476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
17551476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
17561476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1757fa5053a6SGunnar Mills 
17582c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1759d1d50814SRavi Teja         {
17601476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1761d1d50814SRavi Teja         }
17621476687dSEd Tanous 
17631476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
176401784826SJohnathan Mantey     }
1765d1d50814SRavi Teja 
176682695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
17677ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17687ea79e5eSRavi Teja     {
17697ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17707ea79e5eSRavi Teja     }
17717ea79e5eSRavi Teja 
17727ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1773e48c0fc5SRavi Teja 
17742c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17752c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17762c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17772c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17787f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17792c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17807f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17819eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1782e48c0fc5SRavi Teja     {
17831476687dSEd Tanous         nlohmann::json::object_t ipv6;
17841476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
17851476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
17861476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
17871476687dSEd Tanous         ipv6["AddressState"] = nullptr;
17881476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
17892c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1790e48c0fc5SRavi Teja         {
17911476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
17921476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
17931476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
17941476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
179501784826SJohnathan Mantey         }
1796e48c0fc5SRavi Teja     }
1797588c3f0dSKowalski, Kamil }
1798588c3f0dSKowalski, Kamil 
17994f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1800bf648f77SEd Tanous                                const std::string& parentIfaceId,
1801bf648f77SEd Tanous                                const std::string& ifaceId,
1802bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18031abe55efSEd Tanous {
1804bf648f77SEd Tanous     // Fill out obvious data...
1805bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1806bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1807bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1808bf648f77SEd Tanous 
1809bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
181017e22024SJiaqing Zhao     if (ethData.vlanId)
1811bf648f77SEd Tanous     {
181217e22024SJiaqing Zhao         jsonResponse["VLANId"] = *ethData.vlanId;
1813bf648f77SEd Tanous     }
1814bf648f77SEd Tanous }
1815bf648f77SEd Tanous 
18164f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1817bf648f77SEd Tanous {
1818dcf2ebc0SEd Tanous     return boost::starts_with(iface, parent + "_");
1819bf648f77SEd Tanous }
1820bf648f77SEd Tanous 
1821bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1822bf648f77SEd Tanous {
1823bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1824ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
18251476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
18261476687dSEd Tanous             [&app](const crow::Request& req,
18271476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
182845ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
182945ca1b86SEd Tanous         {
183045ca1b86SEd Tanous             return;
183145ca1b86SEd Tanous         }
183245ca1b86SEd Tanous 
1833bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.type"] =
1834bf648f77SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1835bf648f77SEd Tanous         asyncResp->res.jsonValue["@odata.id"] =
1836bf648f77SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1837bf648f77SEd Tanous         asyncResp->res.jsonValue["Name"] =
1838bf648f77SEd Tanous             "Ethernet Network Interface Collection";
1839bf648f77SEd Tanous         asyncResp->res.jsonValue["Description"] =
1840bf648f77SEd Tanous             "Collection of EthernetInterfaces for this Manager";
1841bf648f77SEd Tanous 
1842bf648f77SEd Tanous         // Get eth interface list, and call the below callback for JSON
1843bf648f77SEd Tanous         // preparation
1844*002d39b4SEd Tanous         getEthernetIfaceList(
1845*002d39b4SEd Tanous             [asyncResp](
18461476687dSEd Tanous                 const bool& success,
1847*002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
1848bf648f77SEd Tanous             if (!success)
18491abe55efSEd Tanous             {
1850f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
18519391bb9cSRapkiewicz, Pawel                 return;
18529391bb9cSRapkiewicz, Pawel             }
18539391bb9cSRapkiewicz, Pawel 
1854*002d39b4SEd Tanous             nlohmann::json& ifaceArray = asyncResp->res.jsonValue["Members"];
1855bf648f77SEd Tanous             ifaceArray = nlohmann::json::array();
1856bf648f77SEd Tanous             std::string tag = "_";
1857bf648f77SEd Tanous             for (const std::string& ifaceItem : ifaceList)
1858bf648f77SEd Tanous             {
1859bf648f77SEd Tanous                 std::size_t found = ifaceItem.find(tag);
1860bf648f77SEd Tanous                 if (found == std::string::npos)
1861bf648f77SEd Tanous                 {
18621476687dSEd Tanous                     nlohmann::json::object_t iface;
18631476687dSEd Tanous                     iface["@odata.id"] =
1864bf648f77SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
18651476687dSEd Tanous                         ifaceItem;
18661476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
1867bf648f77SEd Tanous                 }
1868bf648f77SEd Tanous             }
1869bf648f77SEd Tanous 
1870*002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
1871bf648f77SEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
1872bf648f77SEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1873bf648f77SEd Tanous         });
1874bf648f77SEd Tanous         });
1875bf648f77SEd Tanous 
1876bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1877ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1878bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
187945ca1b86SEd Tanous             [&app](const crow::Request& req,
1880bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1881bf648f77SEd Tanous                    const std::string& ifaceId) {
188245ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
188345ca1b86SEd Tanous         {
188445ca1b86SEd Tanous             return;
188545ca1b86SEd Tanous         }
18864a0cb85cSEd Tanous         getEthernetIfaceData(
1887bf648f77SEd Tanous             ifaceId,
1888*002d39b4SEd Tanous             [asyncResp, ifaceId](
1889*002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1890*002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1891*002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
18924a0cb85cSEd Tanous             if (!success)
18931abe55efSEd Tanous             {
1894bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1895bf648f77SEd Tanous                 // existing object, and other errors
1896*002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "EthernetInterface",
1897*002d39b4SEd Tanous                                            ifaceId);
18984a0cb85cSEd Tanous                 return;
18999391bb9cSRapkiewicz, Pawel             }
19004c9afe43SEd Tanous 
19010f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
1902fda13ad2SSunitha Harish                 "#EthernetInterface.v1_4_1.EthernetInterface";
1903*002d39b4SEd Tanous             asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19040f74e643SEd Tanous             asyncResp->res.jsonValue["Description"] =
19050f74e643SEd Tanous                 "Management Network Interface";
19060f74e643SEd Tanous 
1907*002d39b4SEd Tanous             parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data, ipv6Data);
19089391bb9cSRapkiewicz, Pawel             });
1909bf648f77SEd Tanous         });
19109391bb9cSRapkiewicz, Pawel 
1911bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1912ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1913bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
191445ca1b86SEd Tanous             [&app](const crow::Request& req,
1915bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1916bf648f77SEd Tanous                    const std::string& ifaceId) {
191745ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
191845ca1b86SEd Tanous         {
191945ca1b86SEd Tanous             return;
192045ca1b86SEd Tanous         }
1921bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1922ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1923d577665bSRatan Gupta         std::optional<std::string> macAddress;
19249a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1925d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1926e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1927f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1928da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19291f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1930eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
193135fb5311STejas Patil         std::optional<size_t> mtuSize;
19321f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19331f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19340627a2c7SEd Tanous 
193515ed6780SWilly Tu         if (!json_util::readJsonPatch(
19368d1b46d7Szhanghch05                 req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1937*002d39b4SEd Tanous                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1938*002d39b4SEd Tanous                 macAddress, "StaticNameServers", staticNameServers,
1939*002d39b4SEd Tanous                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1940ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1941*002d39b4SEd Tanous                 "MTUSize", mtuSize, "InterfaceEnabled", interfaceEnabled))
19421abe55efSEd Tanous         {
1943588c3f0dSKowalski, Kamil             return;
1944588c3f0dSKowalski, Kamil         }
1945da131a9aSJennifer Lee         if (dhcpv4)
1946da131a9aSJennifer Lee         {
1947*002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv4, asyncResp->res, "DHCPEnabled",
19481f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
194982695a5bSJiaqing Zhao                                      v4dhcpParms.useDnsServers, "UseNTPServers",
195082695a5bSJiaqing Zhao                                      v4dhcpParms.useNtpServers, "UseDomainName",
195182695a5bSJiaqing Zhao                                      v4dhcpParms.useDomainName))
19521f8c7b5dSJohnathan Mantey             {
19531f8c7b5dSJohnathan Mantey                 return;
19541f8c7b5dSJohnathan Mantey             }
19551f8c7b5dSJohnathan Mantey         }
19561f8c7b5dSJohnathan Mantey 
19571f8c7b5dSJohnathan Mantey         if (dhcpv6)
19581f8c7b5dSJohnathan Mantey         {
1959*002d39b4SEd Tanous             if (!json_util::readJson(*dhcpv6, asyncResp->res, "OperatingMode",
1960*002d39b4SEd Tanous                                      v6dhcpParms.dhcpv6OperatingMode,
1961*002d39b4SEd Tanous                                      "UseDNSServers", v6dhcpParms.useDnsServers,
1962*002d39b4SEd Tanous                                      "UseNTPServers", v6dhcpParms.useNtpServers,
1963*002d39b4SEd Tanous                                      "UseDomainName",
196482695a5bSJiaqing Zhao                                      v6dhcpParms.useDomainName))
19651f8c7b5dSJohnathan Mantey             {
19661f8c7b5dSJohnathan Mantey                 return;
19671f8c7b5dSJohnathan Mantey             }
1968da131a9aSJennifer Lee         }
1969da131a9aSJennifer Lee 
1970bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
1971bf648f77SEd Tanous         // for JSON preparation
19724a0cb85cSEd Tanous         getEthernetIfaceData(
19732c70f800SEd Tanous             ifaceId,
1974bf648f77SEd Tanous             [asyncResp, ifaceId, hostname = std::move(hostname),
1975ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1976d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19779a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1978e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19791f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
19801f8c7b5dSJohnathan Mantey              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
198135fb5311STejas Patil              mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
1982f23b7296SEd Tanous              v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1983*002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
1984*002d39b4SEd Tanous                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1985*002d39b4SEd Tanous                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
19861abe55efSEd Tanous             if (!success)
19871abe55efSEd Tanous             {
1988588c3f0dSKowalski, Kamil                 // ... otherwise return error
1989bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
1990bf648f77SEd Tanous                 // existing object, and other errors
1991*002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res, "Ethernet Interface",
1992*002d39b4SEd Tanous                                            ifaceId);
1993588c3f0dSKowalski, Kamil                 return;
1994588c3f0dSKowalski, Kamil             }
1995588c3f0dSKowalski, Kamil 
19961f8c7b5dSJohnathan Mantey             if (dhcpv4 || dhcpv6)
19971f8c7b5dSJohnathan Mantey             {
1998*002d39b4SEd Tanous                 handleDHCPPatch(ifaceId, ethData, v4dhcpParms, v6dhcpParms,
1999*002d39b4SEd Tanous                                 asyncResp);
20001f8c7b5dSJohnathan Mantey             }
20011f8c7b5dSJohnathan Mantey 
20020627a2c7SEd Tanous             if (hostname)
20031abe55efSEd Tanous             {
20040627a2c7SEd Tanous                 handleHostnamePatch(*hostname, asyncResp);
20051abe55efSEd Tanous             }
20060627a2c7SEd Tanous 
2007ab6554f1SJoshi-Mansi             if (fqdn)
2008ab6554f1SJoshi-Mansi             {
20092c70f800SEd Tanous                 handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2010ab6554f1SJoshi-Mansi             }
2011ab6554f1SJoshi-Mansi 
2012d577665bSRatan Gupta             if (macAddress)
2013d577665bSRatan Gupta             {
2014*002d39b4SEd Tanous                 handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
2015d577665bSRatan Gupta             }
2016d577665bSRatan Gupta 
2017d1d50814SRavi Teja             if (ipv4StaticAddresses)
2018d1d50814SRavi Teja             {
2019bf648f77SEd Tanous                 // TODO(ed) for some reason the capture of
2020bf648f77SEd Tanous                 // ipv4Addresses above is returning a const value,
2021bf648f77SEd Tanous                 // not a non-const value. This doesn't really work
2022bf648f77SEd Tanous                 // for us, as we need to be able to efficiently move
2023bf648f77SEd Tanous                 // out the intermedia nlohmann::json objects. This
2024bf648f77SEd Tanous                 // makes a copy of the structure, and operates on
2025bf648f77SEd Tanous                 // that, but could be done more efficiently
2026f23b7296SEd Tanous                 nlohmann::json ipv4Static = *ipv4StaticAddresses;
2027*002d39b4SEd Tanous                 handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
20281abe55efSEd Tanous             }
20290627a2c7SEd Tanous 
2030f85837bfSRAJESWARAN THILLAIGOVINDAN             if (staticNameServers)
2031f85837bfSRAJESWARAN THILLAIGOVINDAN             {
2032*002d39b4SEd Tanous                 handleStaticNameServersPatch(ifaceId, *staticNameServers,
2033*002d39b4SEd Tanous                                              asyncResp);
2034f85837bfSRAJESWARAN THILLAIGOVINDAN             }
20359a6fc6feSRavi Teja 
20369a6fc6feSRavi Teja             if (ipv6DefaultGateway)
20379a6fc6feSRavi Teja             {
20389a6fc6feSRavi Teja                 messages::propertyNotWritable(asyncResp->res,
20399a6fc6feSRavi Teja                                               "IPv6DefaultGateway");
20409a6fc6feSRavi Teja             }
2041e48c0fc5SRavi Teja 
2042e48c0fc5SRavi Teja             if (ipv6StaticAddresses)
2043e48c0fc5SRavi Teja             {
2044*002d39b4SEd Tanous                 const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
2045*002d39b4SEd Tanous                 handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
2046*002d39b4SEd Tanous                                                asyncResp);
2047e48c0fc5SRavi Teja             }
2048eeedda23SJohnathan Mantey 
2049eeedda23SJohnathan Mantey             if (interfaceEnabled)
2050eeedda23SJohnathan Mantey             {
2051*002d39b4SEd Tanous                 setEthernetInterfaceBoolProperty(ifaceId, "NICEnabled",
2052*002d39b4SEd Tanous                                                  *interfaceEnabled, asyncResp);
2053eeedda23SJohnathan Mantey             }
205435fb5311STejas Patil 
205535fb5311STejas Patil             if (mtuSize)
205635fb5311STejas Patil             {
205735fb5311STejas Patil                 handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
205835fb5311STejas Patil             }
2059588c3f0dSKowalski, Kamil             });
2060bf648f77SEd Tanous         });
20619391bb9cSRapkiewicz, Pawel 
2062bf648f77SEd Tanous     BMCWEB_ROUTE(
2063bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2064ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2065bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
206645ca1b86SEd Tanous             [&app](const crow::Request& req,
2067bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
206845ca1b86SEd Tanous                    const std::string& parentIfaceId,
206945ca1b86SEd Tanous                    const std::string& ifaceId) {
207045ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
207145ca1b86SEd Tanous         {
207245ca1b86SEd Tanous             return;
207345ca1b86SEd Tanous         }
20748d1b46d7Szhanghch05         asyncResp->res.jsonValue["@odata.type"] =
20750f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20768d1b46d7Szhanghch05         asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2077e439f0f8SKowalski, Kamil 
20782c70f800SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
20791abe55efSEd Tanous         {
2080a434f2bdSEd Tanous             return;
2081a434f2bdSEd Tanous         }
2082a434f2bdSEd Tanous 
2083bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2084bf648f77SEd Tanous         // for JSON preparation
20854a0cb85cSEd Tanous         getEthernetIfaceData(
2086bf648f77SEd Tanous             ifaceId,
2087*002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2088*002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2089cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2090cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
209117e22024SJiaqing Zhao             if (success && ethData.vlanId)
20921abe55efSEd Tanous             {
2093*002d39b4SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2094*002d39b4SEd Tanous                                    ifaceId, ethData);
20951abe55efSEd Tanous             }
20961abe55efSEd Tanous             else
20971abe55efSEd Tanous             {
2098e439f0f8SKowalski, Kamil                 // ... otherwise return error
2099bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2100bf648f77SEd Tanous                 // existing object, and other errors
2101bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2102*002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2103e439f0f8SKowalski, Kamil             }
2104e439f0f8SKowalski, Kamil             });
2105bf648f77SEd Tanous         });
2106e439f0f8SKowalski, Kamil 
2107bf648f77SEd Tanous     BMCWEB_ROUTE(
2108bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21093d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2110bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
211145ca1b86SEd Tanous             [&app](const crow::Request& req,
2112bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
211345ca1b86SEd Tanous                    const std::string& parentIfaceId,
211445ca1b86SEd Tanous                    const std::string& ifaceId) {
211545ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
211645ca1b86SEd Tanous         {
211745ca1b86SEd Tanous             return;
211845ca1b86SEd Tanous         }
2119fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21201abe55efSEd Tanous         {
2121*002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2122*002d39b4SEd Tanous                                        ifaceId);
2123927a505aSKowalski, Kamil             return;
2124927a505aSKowalski, Kamil         }
2125927a505aSKowalski, Kamil 
21263927e13eSJiaqing Zhao         std::optional<bool> vlanEnable;
21273927e13eSJiaqing Zhao         std::optional<uint32_t> vlanId;
21280627a2c7SEd Tanous 
212915ed6780SWilly Tu         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2130bf648f77SEd Tanous                                       vlanEnable, "VLANId", vlanId))
21311abe55efSEd Tanous         {
2132927a505aSKowalski, Kamil             return;
2133927a505aSKowalski, Kamil         }
2134927a505aSKowalski, Kamil 
21353927e13eSJiaqing Zhao         if (vlanId)
21363927e13eSJiaqing Zhao         {
21373927e13eSJiaqing Zhao             messages::propertyNotWritable(asyncResp->res, "VLANId");
21383927e13eSJiaqing Zhao             return;
21393927e13eSJiaqing Zhao         }
21403927e13eSJiaqing Zhao 
2141bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2142bf648f77SEd Tanous         // for JSON preparation
2143e48c0fc5SRavi Teja         getEthernetIfaceData(
2144bf648f77SEd Tanous             ifaceId,
21453927e13eSJiaqing Zhao             [asyncResp, parentIfaceId, ifaceId, vlanEnable](
2146*002d39b4SEd Tanous                 const bool& success, const EthernetInterfaceData& ethData,
2147cb13a392SEd Tanous                 const boost::container::flat_set<IPv4AddressData>&,
2148cb13a392SEd Tanous                 const boost::container::flat_set<IPv6AddressData>&) {
214917e22024SJiaqing Zhao             if (success && ethData.vlanId)
215008244d02SSunitha Harish             {
215108244d02SSunitha Harish                 auto callback =
2152*002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
215308244d02SSunitha Harish                     if (ec)
215408244d02SSunitha Harish                     {
215508244d02SSunitha Harish                         messages::internalError(asyncResp->res);
21563927e13eSJiaqing Zhao                         return;
215708244d02SSunitha Harish                     }
215808244d02SSunitha Harish                 };
215908244d02SSunitha Harish 
21603927e13eSJiaqing Zhao                 if (vlanEnable && !(*vlanEnable))
216108244d02SSunitha Harish                 {
2162*002d39b4SEd Tanous                     BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2163e48c0fc5SRavi Teja                                         "vlan interface";
216408244d02SSunitha Harish                     crow::connections::systemBus->async_method_call(
2165*002d39b4SEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
2166*002d39b4SEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
2167*002d39b4SEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
216808244d02SSunitha Harish                 }
216908244d02SSunitha Harish             }
217008244d02SSunitha Harish             else
21711abe55efSEd Tanous             {
2172bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2173bf648f77SEd Tanous                 // existing object, and other errors
2174bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2175*002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2176bf648f77SEd Tanous                 return;
2177bf648f77SEd Tanous             }
2178bf648f77SEd Tanous             });
2179bf648f77SEd Tanous         });
2180bf648f77SEd Tanous 
2181bf648f77SEd Tanous     BMCWEB_ROUTE(
2182bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21833d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2184bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
218545ca1b86SEd Tanous             [&app](const crow::Request& req,
2186bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
218745ca1b86SEd Tanous                    const std::string& parentIfaceId,
218845ca1b86SEd Tanous                    const std::string& ifaceId) {
218945ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
219045ca1b86SEd Tanous         {
219145ca1b86SEd Tanous             return;
219245ca1b86SEd Tanous         }
2193bf648f77SEd Tanous         if (!verifyNames(parentIfaceId, ifaceId))
2194bf648f77SEd Tanous         {
2195*002d39b4SEd Tanous             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2196*002d39b4SEd Tanous                                        ifaceId);
2197927a505aSKowalski, Kamil             return;
2198927a505aSKowalski, Kamil         }
2199e439f0f8SKowalski, Kamil 
2200bf648f77SEd Tanous         // Get single eth interface data, and call the below callback
2201bf648f77SEd Tanous         // for JSON preparation
2202f12894f8SJason M. Bills         getEthernetIfaceData(
2203bf648f77SEd Tanous             ifaceId,
2204*002d39b4SEd Tanous             [asyncResp, parentIfaceId,
2205*002d39b4SEd Tanous              ifaceId](const bool& success, const EthernetInterfaceData& ethData,
2206cb13a392SEd Tanous                       const boost::container::flat_set<IPv4AddressData>&,
2207cb13a392SEd Tanous                       const boost::container::flat_set<IPv6AddressData>&) {
220817e22024SJiaqing Zhao             if (success && ethData.vlanId)
22091abe55efSEd Tanous             {
2210f12894f8SJason M. Bills                 auto callback =
2211*002d39b4SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
22121abe55efSEd Tanous                     if (ec)
22131abe55efSEd Tanous                     {
2214f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
2215927a505aSKowalski, Kamil                     }
22164a0cb85cSEd Tanous                 };
22174a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
2218*002d39b4SEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
2219*002d39b4SEd Tanous                     std::string("/xyz/openbmc_project/network/") + ifaceId,
22204a0cb85cSEd Tanous                     "xyz.openbmc_project.Object.Delete", "Delete");
22211abe55efSEd Tanous             }
22221abe55efSEd Tanous             else
22231abe55efSEd Tanous             {
2224927a505aSKowalski, Kamil                 // ... otherwise return error
2225bf648f77SEd Tanous                 // TODO(Pawel)consider distinguish between non
2226bf648f77SEd Tanous                 // existing object, and other errors
2227bf648f77SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2228*002d39b4SEd Tanous                                            "VLAN Network Interface", ifaceId);
2229927a505aSKowalski, Kamil             }
2230927a505aSKowalski, Kamil             });
2231bf648f77SEd Tanous         });
2232e439f0f8SKowalski, Kamil 
2233bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2234bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2235ed398213SEd Tanous 
2236ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
22371476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
22381476687dSEd Tanous             [&app](const crow::Request& req,
2239bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2240bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
224145ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
224245ca1b86SEd Tanous         {
224345ca1b86SEd Tanous             return;
224445ca1b86SEd Tanous         }
22454a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
22461abe55efSEd Tanous         // preparation
2247*002d39b4SEd Tanous         getEthernetIfaceList(
2248*002d39b4SEd Tanous             [asyncResp, rootInterfaceName](
22491abe55efSEd Tanous                 const bool& success,
2250*002d39b4SEd Tanous                 const boost::container::flat_set<std::string>& ifaceList) {
22514a0cb85cSEd Tanous             if (!success)
22521abe55efSEd Tanous             {
2253f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
22544a0cb85cSEd Tanous                 return;
22551abe55efSEd Tanous             }
22564c9afe43SEd Tanous 
225781ce609eSEd Tanous             if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22584c9afe43SEd Tanous             {
2259*002d39b4SEd Tanous                 messages::resourceNotFound(asyncResp->res,
2260*002d39b4SEd Tanous                                            "VLanNetworkInterfaceCollection",
22614c9afe43SEd Tanous                                            rootInterfaceName);
22624c9afe43SEd Tanous                 return;
22634c9afe43SEd Tanous             }
22644c9afe43SEd Tanous 
22650f74e643SEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
22660f74e643SEd Tanous                 "#VLanNetworkInterfaceCollection."
22670f74e643SEd Tanous                 "VLanNetworkInterfaceCollection";
22680f74e643SEd Tanous             asyncResp->res.jsonValue["Name"] =
22690f74e643SEd Tanous                 "VLAN Network Interface Collection";
22704a0cb85cSEd Tanous 
22712c70f800SEd Tanous             nlohmann::json ifaceArray = nlohmann::json::array();
22724a0cb85cSEd Tanous 
227381ce609eSEd Tanous             for (const std::string& ifaceItem : ifaceList)
22741abe55efSEd Tanous             {
2275*002d39b4SEd Tanous                 if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
22764a0cb85cSEd Tanous                 {
2277f23b7296SEd Tanous                     std::string path =
2278f23b7296SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2279f23b7296SEd Tanous                     path += rootInterfaceName;
2280f23b7296SEd Tanous                     path += "/VLANs/";
2281f23b7296SEd Tanous                     path += ifaceItem;
22821476687dSEd Tanous                     nlohmann::json::object_t iface;
22831476687dSEd Tanous                     iface["@odata.id"] = std::move(path);
22841476687dSEd Tanous                     ifaceArray.push_back(std::move(iface));
2285e439f0f8SKowalski, Kamil                 }
2286e439f0f8SKowalski, Kamil             }
2287e439f0f8SKowalski, Kamil 
2288*002d39b4SEd Tanous             asyncResp->res.jsonValue["Members@odata.count"] = ifaceArray.size();
22892c70f800SEd Tanous             asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
22904a0cb85cSEd Tanous             asyncResp->res.jsonValue["@odata.id"] =
22914a0cb85cSEd Tanous                 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
22924a0cb85cSEd Tanous                 rootInterfaceName + "/VLANs";
2293e439f0f8SKowalski, Kamil         });
2294bf648f77SEd Tanous         });
2295e439f0f8SKowalski, Kamil 
2296bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2297bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
22983d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2299bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
230045ca1b86SEd Tanous             [&app](const crow::Request& req,
2301bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2302bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
230345ca1b86SEd Tanous         if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
230445ca1b86SEd Tanous         {
230545ca1b86SEd Tanous             return;
230645ca1b86SEd Tanous         }
2307fda13ad2SSunitha Harish         bool vlanEnable = false;
23080627a2c7SEd Tanous         uint32_t vlanId = 0;
2309*002d39b4SEd Tanous         if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId", vlanId,
2310*002d39b4SEd Tanous                                       "VLANEnable", vlanEnable))
23111abe55efSEd Tanous         {
23124a0cb85cSEd Tanous             return;
2313e439f0f8SKowalski, Kamil         }
2314fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2315dbb59d4dSEd Tanous         if (vlanId == 0U)
2316fda13ad2SSunitha Harish         {
2317fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2318fda13ad2SSunitha Harish         }
2319fda13ad2SSunitha Harish         if (!vlanEnable)
2320fda13ad2SSunitha Harish         {
2321fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2322fda13ad2SSunitha Harish         }
2323271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2324fda13ad2SSunitha Harish         {
2325fda13ad2SSunitha Harish             return;
2326fda13ad2SSunitha Harish         }
2327fda13ad2SSunitha Harish 
2328*002d39b4SEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
23291abe55efSEd Tanous             if (ec)
23301abe55efSEd Tanous             {
2331bf648f77SEd Tanous                 // TODO(ed) make more consistent error messages
2332bf648f77SEd Tanous                 // based on phosphor-network responses
2333f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
23344a0cb85cSEd Tanous                 return;
23351abe55efSEd Tanous             }
2336f12894f8SJason M. Bills             messages::created(asyncResp->res);
2337e439f0f8SKowalski, Kamil         };
23384a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
23394a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
23404a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
23414a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23420627a2c7SEd Tanous             rootInterfaceName, vlanId);
2343bf648f77SEd Tanous         });
23444a0cb85cSEd Tanous }
2345bf648f77SEd Tanous 
23469391bb9cSRapkiewicz, Pawel } // namespace redfish
2347