xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 1476687deb1697d865b20458a0097c9ab5fd44e2)
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;
9482695a5bSJiaqing Zhao     std::vector<std::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                             {
22482695a5bSJiaqing Zhao                                 ethData.vlanId.push_back(*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 {
7299010ec2eSRavi Teja     auto createIpHandler = [asyncResp, ifaceId,
7309010ec2eSRavi Teja                             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(
7769010ec2eSRavi Teja                 [asyncResp, ifaceId,
7779010ec2eSRavi Teja                  gateway](const boost::system::error_code ec2) {
77823a21a1cSEd Tanous                     if (ec2)
77901784826SJohnathan Mantey                     {
78001784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
7819010ec2eSRavi Teja                         return;
78201784826SJohnathan Mantey                     }
7839010ec2eSRavi Teja                     updateIPv4DefaultGateway(ifaceId, gateway, asyncResp);
78401784826SJohnathan Mantey                 },
78501784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
78601784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
78701784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
78801784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
78901784826SJohnathan Mantey                 prefixLength, gateway);
79001784826SJohnathan Mantey         },
79101784826SJohnathan Mantey         "xyz.openbmc_project.Network",
79201784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
79301784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
79401784826SJohnathan Mantey }
79501784826SJohnathan Mantey 
79601784826SJohnathan Mantey /**
797e48c0fc5SRavi Teja  * @brief Deletes given IPv6
798e48c0fc5SRavi Teja  *
799e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
800e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
801e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
802e48c0fc5SRavi Teja  *
803e48c0fc5SRavi Teja  * @return None
804e48c0fc5SRavi Teja  */
805e48c0fc5SRavi Teja inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
8068d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
807e48c0fc5SRavi Teja {
808e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
809286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
810e48c0fc5SRavi Teja             if (ec)
811e48c0fc5SRavi Teja             {
812e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
813e48c0fc5SRavi Teja             }
814e48c0fc5SRavi Teja         },
815e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
816e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
817e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
818e48c0fc5SRavi Teja }
819e48c0fc5SRavi Teja 
820e48c0fc5SRavi Teja /**
82101784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
82201784826SJohnathan Mantey  * static IPv6 entry
82301784826SJohnathan Mantey  *
82401784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
82501784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
82601784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
82701784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
82801784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
82901784826SJohnathan Mantey  *
83001784826SJohnathan Mantey  * @return None
83101784826SJohnathan Mantey  */
8328d1b46d7Szhanghch05 inline void
8338d1b46d7Szhanghch05     deleteAndCreateIPv6(const std::string& ifaceId, const std::string& id,
8348d1b46d7Szhanghch05                         uint8_t prefixLength, const std::string& address,
8358d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
83601784826SJohnathan Mantey {
83701784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
83801784826SJohnathan Mantey         [asyncResp, ifaceId, address,
83901784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
84001784826SJohnathan Mantey             if (ec)
84101784826SJohnathan Mantey             {
84201784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
84301784826SJohnathan Mantey             }
84401784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
84523a21a1cSEd Tanous                 [asyncResp](const boost::system::error_code ec2) {
84623a21a1cSEd Tanous                     if (ec2)
84701784826SJohnathan Mantey                     {
84801784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
84901784826SJohnathan Mantey                     }
85001784826SJohnathan Mantey                 },
85101784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
85201784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
85301784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
85401784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
85501784826SJohnathan Mantey                 prefixLength, "");
85601784826SJohnathan Mantey         },
85701784826SJohnathan Mantey         "xyz.openbmc_project.Network",
85801784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
85901784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
86001784826SJohnathan Mantey }
86101784826SJohnathan Mantey 
86201784826SJohnathan Mantey /**
863e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
864e48c0fc5SRavi Teja  *
865e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
866e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
867e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
868e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
869e48c0fc5SRavi Teja  *
870e48c0fc5SRavi Teja  * @return None
871e48c0fc5SRavi Teja  */
87201784826SJohnathan Mantey inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
87301784826SJohnathan Mantey                        const std::string& address,
8748d1b46d7Szhanghch05                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
875e48c0fc5SRavi Teja {
876e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
877e48c0fc5SRavi Teja         if (ec)
878e48c0fc5SRavi Teja         {
879e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
880e48c0fc5SRavi Teja         }
881e48c0fc5SRavi Teja     };
882e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
8834e0453b1SGunnar Mills     // does not have associated gateway property
884e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
885e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
886e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
887e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
888e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
889e48c0fc5SRavi Teja         "");
890e48c0fc5SRavi Teja }
891e48c0fc5SRavi Teja 
892179db1d7SKowalski, Kamil /**
893179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
894179db1d7SKowalski, Kamil  * Object
895179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8964a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
897179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
898179db1d7SKowalski, Kamil  * into JSON
899179db1d7SKowalski, Kamil  */
900179db1d7SKowalski, Kamil template <typename CallbackFunc>
90181ce609eSEd Tanous void getEthernetIfaceData(const std::string& ethifaceId,
9021abe55efSEd Tanous                           CallbackFunc&& callback)
9031abe55efSEd Tanous {
90455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
905f94c4ecfSEd Tanous         [ethifaceId{std::string{ethifaceId}},
906f94c4ecfSEd Tanous          callback{std::forward<CallbackFunc>(callback)}](
90781ce609eSEd Tanous             const boost::system::error_code errorCode,
908711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
90955c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9104a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
911e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
912179db1d7SKowalski, Kamil 
91381ce609eSEd Tanous             if (errorCode)
9141abe55efSEd Tanous             {
91501784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
916179db1d7SKowalski, Kamil                 return;
917179db1d7SKowalski, Kamil             }
918179db1d7SKowalski, Kamil 
9194c9afe43SEd Tanous             bool found =
9202c70f800SEd Tanous                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
9214c9afe43SEd Tanous             if (!found)
9224c9afe43SEd Tanous             {
92301784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9244c9afe43SEd Tanous                 return;
9254c9afe43SEd Tanous             }
9264c9afe43SEd Tanous 
9272c70f800SEd Tanous             extractIPData(ethifaceId, resp, ipv4Data);
928179db1d7SKowalski, Kamil             // Fix global GW
9291abe55efSEd Tanous             for (IPv4AddressData& ipv4 : ipv4Data)
9301abe55efSEd Tanous             {
931c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
932c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
9339010ec2eSRavi Teja                     (ipv4.origin == "DHCP") || (ipv4.origin == "Static"))
9341abe55efSEd Tanous                 {
93582695a5bSJiaqing Zhao                     ipv4.gateway = ethData.defaultGateway;
936179db1d7SKowalski, Kamil                 }
937179db1d7SKowalski, Kamil             }
938179db1d7SKowalski, Kamil 
9392c70f800SEd Tanous             extractIPV6Data(ethifaceId, resp, ipv6Data);
9404e0453b1SGunnar Mills             // Finally make a callback with useful data
94101784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
942179db1d7SKowalski, Kamil         },
943179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
944179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
945271584abSEd Tanous }
946179db1d7SKowalski, Kamil 
947179db1d7SKowalski, Kamil /**
9489391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9499391bb9cSRapkiewicz, Pawel  * Manager
9501abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9511abe55efSEd Tanous  * into JSON.
9529391bb9cSRapkiewicz, Pawel  */
9539391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9541abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc&& callback)
9551abe55efSEd Tanous {
95655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
957f94c4ecfSEd Tanous         [callback{std::forward<CallbackFunc>(callback)}](
95881ce609eSEd Tanous             const boost::system::error_code errorCode,
959711ac7a9SEd Tanous             dbus::utility::ManagedObjectType& resp) {
9601abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9611abe55efSEd Tanous             // ethernet interfaces
9622c70f800SEd Tanous             boost::container::flat_set<std::string> ifaceList;
9632c70f800SEd Tanous             ifaceList.reserve(resp.size());
96481ce609eSEd Tanous             if (errorCode)
9651abe55efSEd Tanous             {
9662c70f800SEd Tanous                 callback(false, ifaceList);
9679391bb9cSRapkiewicz, Pawel                 return;
9689391bb9cSRapkiewicz, Pawel             }
9699391bb9cSRapkiewicz, Pawel 
9709391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9714a0cb85cSEd Tanous             for (const auto& objpath : resp)
9721abe55efSEd Tanous             {
9739391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
9744a0cb85cSEd Tanous                 for (const auto& interface : objpath.second)
9751abe55efSEd Tanous                 {
9761abe55efSEd Tanous                     // If interface is
9774a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
9784a0cb85cSEd Tanous                     // what we're looking for.
9799391bb9cSRapkiewicz, Pawel                     if (interface.first ==
9801abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
9811abe55efSEd Tanous                     {
9822dfd18efSEd Tanous                         std::string ifaceId = objpath.first.filename();
9832dfd18efSEd Tanous                         if (ifaceId.empty())
9841abe55efSEd Tanous                         {
9852dfd18efSEd Tanous                             continue;
9869391bb9cSRapkiewicz, Pawel                         }
9872dfd18efSEd Tanous                         // and put it into output vector.
9882dfd18efSEd Tanous                         ifaceList.emplace(ifaceId);
9899391bb9cSRapkiewicz, Pawel                     }
9909391bb9cSRapkiewicz, Pawel                 }
9919391bb9cSRapkiewicz, Pawel             }
992a434f2bdSEd Tanous             // Finally make a callback with useful data
9932c70f800SEd Tanous             callback(true, ifaceList);
9949391bb9cSRapkiewicz, Pawel         },
995aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
996aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
997271584abSEd Tanous }
9989391bb9cSRapkiewicz, Pawel 
9994f48d5f6SEd Tanous inline void
10004f48d5f6SEd Tanous     handleHostnamePatch(const std::string& hostname,
10018d1b46d7Szhanghch05                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
10021abe55efSEd Tanous {
1003ab6554f1SJoshi-Mansi     // SHOULD handle host names of up to 255 characters(RFC 1123)
1004ab6554f1SJoshi-Mansi     if (hostname.length() > 255)
1005ab6554f1SJoshi-Mansi     {
1006ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, hostname,
1007ab6554f1SJoshi-Mansi                                            "HostName");
1008ab6554f1SJoshi-Mansi         return;
1009ab6554f1SJoshi-Mansi     }
1010bc0bd6e0SEd Tanous     crow::connections::systemBus->async_method_call(
1011bc0bd6e0SEd Tanous         [asyncResp](const boost::system::error_code ec) {
10124a0cb85cSEd Tanous             if (ec)
10134a0cb85cSEd Tanous             {
1014a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
10151abe55efSEd Tanous             }
1016bc0bd6e0SEd Tanous         },
1017bf648f77SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network/config",
1018bc0bd6e0SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
1019bc0bd6e0SEd Tanous         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1020168e20c1SEd Tanous         dbus::utility::DbusVariantType(hostname));
1021588c3f0dSKowalski, Kamil }
1022588c3f0dSKowalski, Kamil 
10234f48d5f6SEd Tanous inline void
102435fb5311STejas Patil     handleMTUSizePatch(const std::string& ifaceId, const size_t mtuSize,
102535fb5311STejas Patil                        const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
102635fb5311STejas Patil {
102735fb5311STejas Patil     sdbusplus::message::object_path objPath =
102835fb5311STejas Patil         "/xyz/openbmc_project/network/" + ifaceId;
102935fb5311STejas Patil     crow::connections::systemBus->async_method_call(
103035fb5311STejas Patil         [asyncResp](const boost::system::error_code ec) {
103135fb5311STejas Patil             if (ec)
103235fb5311STejas Patil             {
103335fb5311STejas Patil                 messages::internalError(asyncResp->res);
103435fb5311STejas Patil             }
103535fb5311STejas Patil         },
103635fb5311STejas Patil         "xyz.openbmc_project.Network", objPath,
103735fb5311STejas Patil         "org.freedesktop.DBus.Properties", "Set",
103835fb5311STejas Patil         "xyz.openbmc_project.Network.EthernetInterface", "MTU",
103935fb5311STejas Patil         std::variant<size_t>(mtuSize));
104035fb5311STejas Patil }
104135fb5311STejas Patil 
104235fb5311STejas Patil inline void
10434f48d5f6SEd Tanous     handleDomainnamePatch(const std::string& ifaceId,
1044bf648f77SEd Tanous                           const std::string& domainname,
10458d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1046ab6554f1SJoshi-Mansi {
1047ab6554f1SJoshi-Mansi     std::vector<std::string> vectorDomainname = {domainname};
1048ab6554f1SJoshi-Mansi     crow::connections::systemBus->async_method_call(
1049ab6554f1SJoshi-Mansi         [asyncResp](const boost::system::error_code ec) {
1050ab6554f1SJoshi-Mansi             if (ec)
1051ab6554f1SJoshi-Mansi             {
1052ab6554f1SJoshi-Mansi                 messages::internalError(asyncResp->res);
1053ab6554f1SJoshi-Mansi             }
1054ab6554f1SJoshi-Mansi         },
1055ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network",
1056ab6554f1SJoshi-Mansi         "/xyz/openbmc_project/network/" + ifaceId,
1057ab6554f1SJoshi-Mansi         "org.freedesktop.DBus.Properties", "Set",
1058ab6554f1SJoshi-Mansi         "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1059168e20c1SEd Tanous         dbus::utility::DbusVariantType(vectorDomainname));
1060ab6554f1SJoshi-Mansi }
1061ab6554f1SJoshi-Mansi 
10624f48d5f6SEd Tanous inline bool isHostnameValid(const std::string& hostname)
1063bf648f77SEd Tanous {
1064bf648f77SEd Tanous     // A valid host name can never have the dotted-decimal form (RFC 1123)
1065bf648f77SEd Tanous     if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1066bf648f77SEd Tanous     {
1067bf648f77SEd Tanous         return false;
1068bf648f77SEd Tanous     }
1069bf648f77SEd Tanous     // Each label(hostname/subdomains) within a valid FQDN
1070bf648f77SEd Tanous     // MUST handle host names of up to 63 characters (RFC 1123)
1071bf648f77SEd Tanous     // labels cannot start or end with hyphens (RFC 952)
1072bf648f77SEd Tanous     // labels can start with numbers (RFC 1123)
1073bf648f77SEd Tanous     const std::regex pattern(
1074bf648f77SEd Tanous         "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1075bf648f77SEd Tanous 
1076bf648f77SEd Tanous     return std::regex_match(hostname, pattern);
1077bf648f77SEd Tanous }
1078bf648f77SEd Tanous 
10794f48d5f6SEd Tanous inline bool isDomainnameValid(const std::string& domainname)
1080bf648f77SEd Tanous {
1081bf648f77SEd Tanous     // Can have multiple subdomains
1082bf648f77SEd Tanous     // Top Level Domain's min length is 2 character
10830fda0f12SGeorge Liu     const std::regex pattern(
10840fda0f12SGeorge Liu         "^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]{1,30}\\.)*[a-zA-Z]{2,}$");
1085bf648f77SEd Tanous 
1086bf648f77SEd Tanous     return std::regex_match(domainname, pattern);
1087bf648f77SEd Tanous }
1088bf648f77SEd Tanous 
10894f48d5f6SEd Tanous inline void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
10908d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1091ab6554f1SJoshi-Mansi {
1092ab6554f1SJoshi-Mansi     // Total length of FQDN must not exceed 255 characters(RFC 1035)
1093ab6554f1SJoshi-Mansi     if (fqdn.length() > 255)
1094ab6554f1SJoshi-Mansi     {
1095ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1096ab6554f1SJoshi-Mansi         return;
1097ab6554f1SJoshi-Mansi     }
1098ab6554f1SJoshi-Mansi 
1099ab6554f1SJoshi-Mansi     size_t pos = fqdn.find('.');
1100ab6554f1SJoshi-Mansi     if (pos == std::string::npos)
1101ab6554f1SJoshi-Mansi     {
1102ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1103ab6554f1SJoshi-Mansi         return;
1104ab6554f1SJoshi-Mansi     }
1105ab6554f1SJoshi-Mansi 
1106ab6554f1SJoshi-Mansi     std::string hostname;
1107ab6554f1SJoshi-Mansi     std::string domainname;
1108ab6554f1SJoshi-Mansi     domainname = (fqdn).substr(pos + 1);
1109ab6554f1SJoshi-Mansi     hostname = (fqdn).substr(0, pos);
1110ab6554f1SJoshi-Mansi 
1111ab6554f1SJoshi-Mansi     if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1112ab6554f1SJoshi-Mansi     {
1113ab6554f1SJoshi-Mansi         messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1114ab6554f1SJoshi-Mansi         return;
1115ab6554f1SJoshi-Mansi     }
1116ab6554f1SJoshi-Mansi 
1117ab6554f1SJoshi-Mansi     handleHostnamePatch(hostname, asyncResp);
1118ab6554f1SJoshi-Mansi     handleDomainnamePatch(ifaceId, domainname, asyncResp);
1119ab6554f1SJoshi-Mansi }
1120ab6554f1SJoshi-Mansi 
11214f48d5f6SEd Tanous inline void
11224f48d5f6SEd Tanous     handleMACAddressPatch(const std::string& ifaceId,
1123bf648f77SEd Tanous                           const std::string& macAddress,
11248d1b46d7Szhanghch05                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1125d577665bSRatan Gupta {
1126d577665bSRatan Gupta     crow::connections::systemBus->async_method_call(
1127d577665bSRatan Gupta         [asyncResp, macAddress](const boost::system::error_code ec) {
1128d577665bSRatan Gupta             if (ec)
1129d577665bSRatan Gupta             {
1130d577665bSRatan Gupta                 messages::internalError(asyncResp->res);
1131d577665bSRatan Gupta                 return;
1132d577665bSRatan Gupta             }
1133d577665bSRatan Gupta         },
1134d577665bSRatan Gupta         "xyz.openbmc_project.Network",
1135d577665bSRatan Gupta         "/xyz/openbmc_project/network/" + ifaceId,
1136d577665bSRatan Gupta         "org.freedesktop.DBus.Properties", "Set",
1137d577665bSRatan Gupta         "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1138168e20c1SEd Tanous         dbus::utility::DbusVariantType(macAddress));
1139d577665bSRatan Gupta }
1140286b9118SJohnathan Mantey 
11414f48d5f6SEd Tanous inline void setDHCPEnabled(const std::string& ifaceId,
11424f48d5f6SEd Tanous                            const std::string& propertyName, const bool v4Value,
11434f48d5f6SEd Tanous                            const bool v6Value,
11448d1b46d7Szhanghch05                            const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1145da131a9aSJennifer Lee {
11462c70f800SEd Tanous     const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1147da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1148da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1149da131a9aSJennifer Lee             if (ec)
1150da131a9aSJennifer Lee             {
1151da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1152da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1153da131a9aSJennifer Lee                 return;
1154da131a9aSJennifer Lee             }
11558f7e9c19SJayaprakash Mutyala             messages::success(asyncResp->res);
1156da131a9aSJennifer Lee         },
1157da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1158da131a9aSJennifer Lee         "/xyz/openbmc_project/network/" + ifaceId,
1159da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1160da131a9aSJennifer Lee         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1161168e20c1SEd Tanous         dbus::utility::DbusVariantType{dhcp});
1162da131a9aSJennifer Lee }
11631f8c7b5dSJohnathan Mantey 
11644f48d5f6SEd Tanous inline void setEthernetInterfaceBoolProperty(
1165eeedda23SJohnathan Mantey     const std::string& ifaceId, const std::string& propertyName,
11668d1b46d7Szhanghch05     const bool& value, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1167eeedda23SJohnathan Mantey {
1168eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1169eeedda23SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1170eeedda23SJohnathan Mantey             if (ec)
1171eeedda23SJohnathan Mantey             {
1172eeedda23SJohnathan Mantey                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1173eeedda23SJohnathan Mantey                 messages::internalError(asyncResp->res);
1174eeedda23SJohnathan Mantey                 return;
1175eeedda23SJohnathan Mantey             }
1176eeedda23SJohnathan Mantey         },
1177eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network",
1178eeedda23SJohnathan Mantey         "/xyz/openbmc_project/network/" + ifaceId,
1179eeedda23SJohnathan Mantey         "org.freedesktop.DBus.Properties", "Set",
1180eeedda23SJohnathan Mantey         "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1181168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1182eeedda23SJohnathan Mantey }
1183eeedda23SJohnathan Mantey 
11844f48d5f6SEd Tanous inline void setDHCPv4Config(const std::string& propertyName, const bool& value,
11858d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1186da131a9aSJennifer Lee {
1187da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1188da131a9aSJennifer Lee     crow::connections::systemBus->async_method_call(
1189da131a9aSJennifer Lee         [asyncResp](const boost::system::error_code ec) {
1190da131a9aSJennifer Lee             if (ec)
1191da131a9aSJennifer Lee             {
1192da131a9aSJennifer Lee                 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1193da131a9aSJennifer Lee                 messages::internalError(asyncResp->res);
1194da131a9aSJennifer Lee                 return;
1195da131a9aSJennifer Lee             }
1196da131a9aSJennifer Lee         },
1197da131a9aSJennifer Lee         "xyz.openbmc_project.Network",
1198da131a9aSJennifer Lee         "/xyz/openbmc_project/network/config/dhcp",
1199da131a9aSJennifer Lee         "org.freedesktop.DBus.Properties", "Set",
1200da131a9aSJennifer Lee         "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1201168e20c1SEd Tanous         dbus::utility::DbusVariantType{value});
1202da131a9aSJennifer Lee }
1203d577665bSRatan Gupta 
12044f48d5f6SEd Tanous inline void handleDHCPPatch(const std::string& ifaceId,
12051f8c7b5dSJohnathan Mantey                             const EthernetInterfaceData& ethData,
1206f23b7296SEd Tanous                             const DHCPParameters& v4dhcpParms,
1207f23b7296SEd Tanous                             const DHCPParameters& v6dhcpParms,
12088d1b46d7Szhanghch05                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1209da131a9aSJennifer Lee {
121082695a5bSJiaqing Zhao     bool ipv4Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
121182695a5bSJiaqing Zhao     bool ipv6Active = translateDhcpEnabledToBool(ethData.dhcpEnabled, false);
1212da131a9aSJennifer Lee 
12131f8c7b5dSJohnathan Mantey     bool nextv4DHCPState =
12141f8c7b5dSJohnathan Mantey         v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12151f8c7b5dSJohnathan Mantey 
12161f8c7b5dSJohnathan Mantey     bool nextv6DHCPState{};
12171f8c7b5dSJohnathan Mantey     if (v6dhcpParms.dhcpv6OperatingMode)
1218da131a9aSJennifer Lee     {
12191f8c7b5dSJohnathan Mantey         if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12201f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12211f8c7b5dSJohnathan Mantey             (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12221f8c7b5dSJohnathan Mantey         {
1223bf648f77SEd Tanous             messages::propertyValueFormatError(asyncResp->res,
1224bf648f77SEd Tanous                                                *v6dhcpParms.dhcpv6OperatingMode,
12251f8c7b5dSJohnathan Mantey                                                "OperatingMode");
1226da131a9aSJennifer Lee             return;
1227da131a9aSJennifer Lee         }
12281f8c7b5dSJohnathan Mantey         nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12291f8c7b5dSJohnathan Mantey     }
12301f8c7b5dSJohnathan Mantey     else
1231da131a9aSJennifer Lee     {
12321f8c7b5dSJohnathan Mantey         nextv6DHCPState = ipv6Active;
12331f8c7b5dSJohnathan Mantey     }
12341f8c7b5dSJohnathan Mantey 
12351f8c7b5dSJohnathan Mantey     bool nextDNS{};
123682695a5bSJiaqing Zhao     if (v4dhcpParms.useDnsServers && v6dhcpParms.useDnsServers)
12371f8c7b5dSJohnathan Mantey     {
123882695a5bSJiaqing Zhao         if (*v4dhcpParms.useDnsServers != *v6dhcpParms.useDnsServers)
12391f8c7b5dSJohnathan Mantey         {
12401f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12411f8c7b5dSJohnathan Mantey             return;
12421f8c7b5dSJohnathan Mantey         }
124382695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12441f8c7b5dSJohnathan Mantey     }
124582695a5bSJiaqing Zhao     else if (v4dhcpParms.useDnsServers)
12461f8c7b5dSJohnathan Mantey     {
124782695a5bSJiaqing Zhao         nextDNS = *v4dhcpParms.useDnsServers;
12481f8c7b5dSJohnathan Mantey     }
124982695a5bSJiaqing Zhao     else if (v6dhcpParms.useDnsServers)
12501f8c7b5dSJohnathan Mantey     {
125182695a5bSJiaqing Zhao         nextDNS = *v6dhcpParms.useDnsServers;
12521f8c7b5dSJohnathan Mantey     }
12531f8c7b5dSJohnathan Mantey     else
12541f8c7b5dSJohnathan Mantey     {
125582695a5bSJiaqing Zhao         nextDNS = ethData.dnsEnabled;
12561f8c7b5dSJohnathan Mantey     }
12571f8c7b5dSJohnathan Mantey 
12581f8c7b5dSJohnathan Mantey     bool nextNTP{};
125982695a5bSJiaqing Zhao     if (v4dhcpParms.useNtpServers && v6dhcpParms.useNtpServers)
12601f8c7b5dSJohnathan Mantey     {
126182695a5bSJiaqing Zhao         if (*v4dhcpParms.useNtpServers != *v6dhcpParms.useNtpServers)
12621f8c7b5dSJohnathan Mantey         {
12631f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12641f8c7b5dSJohnathan Mantey             return;
12651f8c7b5dSJohnathan Mantey         }
126682695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12671f8c7b5dSJohnathan Mantey     }
126882695a5bSJiaqing Zhao     else if (v4dhcpParms.useNtpServers)
12691f8c7b5dSJohnathan Mantey     {
127082695a5bSJiaqing Zhao         nextNTP = *v4dhcpParms.useNtpServers;
12711f8c7b5dSJohnathan Mantey     }
127282695a5bSJiaqing Zhao     else if (v6dhcpParms.useNtpServers)
12731f8c7b5dSJohnathan Mantey     {
127482695a5bSJiaqing Zhao         nextNTP = *v6dhcpParms.useNtpServers;
12751f8c7b5dSJohnathan Mantey     }
12761f8c7b5dSJohnathan Mantey     else
12771f8c7b5dSJohnathan Mantey     {
127882695a5bSJiaqing Zhao         nextNTP = ethData.ntpEnabled;
12791f8c7b5dSJohnathan Mantey     }
12801f8c7b5dSJohnathan Mantey 
12811f8c7b5dSJohnathan Mantey     bool nextUseDomain{};
128282695a5bSJiaqing Zhao     if (v4dhcpParms.useDomainName && v6dhcpParms.useDomainName)
12831f8c7b5dSJohnathan Mantey     {
128482695a5bSJiaqing Zhao         if (*v4dhcpParms.useDomainName != *v6dhcpParms.useDomainName)
12851f8c7b5dSJohnathan Mantey         {
12861f8c7b5dSJohnathan Mantey             messages::generalError(asyncResp->res);
12871f8c7b5dSJohnathan Mantey             return;
12881f8c7b5dSJohnathan Mantey         }
128982695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12901f8c7b5dSJohnathan Mantey     }
129182695a5bSJiaqing Zhao     else if (v4dhcpParms.useDomainName)
12921f8c7b5dSJohnathan Mantey     {
129382695a5bSJiaqing Zhao         nextUseDomain = *v4dhcpParms.useDomainName;
12941f8c7b5dSJohnathan Mantey     }
129582695a5bSJiaqing Zhao     else if (v6dhcpParms.useDomainName)
12961f8c7b5dSJohnathan Mantey     {
129782695a5bSJiaqing Zhao         nextUseDomain = *v6dhcpParms.useDomainName;
12981f8c7b5dSJohnathan Mantey     }
12991f8c7b5dSJohnathan Mantey     else
13001f8c7b5dSJohnathan Mantey     {
130182695a5bSJiaqing Zhao         nextUseDomain = ethData.hostNameEnabled;
13021f8c7b5dSJohnathan Mantey     }
13031f8c7b5dSJohnathan Mantey 
1304da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13051f8c7b5dSJohnathan Mantey     setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13061f8c7b5dSJohnathan Mantey                    asyncResp);
1307da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13081f8c7b5dSJohnathan Mantey     setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1309da131a9aSJennifer Lee     BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13101f8c7b5dSJohnathan Mantey     setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13111f8c7b5dSJohnathan Mantey     BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13121f8c7b5dSJohnathan Mantey     setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1313da131a9aSJennifer Lee }
131401784826SJohnathan Mantey 
13154f48d5f6SEd Tanous inline boost::container::flat_set<IPv4AddressData>::const_iterator
13162c70f800SEd Tanous     getNextStaticIpEntry(
1317bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& head,
1318bf648f77SEd Tanous         const boost::container::flat_set<IPv4AddressData>::const_iterator& end)
131901784826SJohnathan Mantey {
132017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv4AddressData& value) {
132117a897dfSManojkiran Eda         return value.origin == "Static";
132217a897dfSManojkiran Eda     });
132301784826SJohnathan Mantey }
132401784826SJohnathan Mantey 
13254f48d5f6SEd Tanous inline boost::container::flat_set<IPv6AddressData>::const_iterator
13262c70f800SEd Tanous     getNextStaticIpEntry(
1327bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& head,
1328bf648f77SEd Tanous         const boost::container::flat_set<IPv6AddressData>::const_iterator& end)
132901784826SJohnathan Mantey {
133017a897dfSManojkiran Eda     return std::find_if(head, end, [](const IPv6AddressData& value) {
133117a897dfSManojkiran Eda         return value.origin == "Static";
133217a897dfSManojkiran Eda     });
133301784826SJohnathan Mantey }
133401784826SJohnathan Mantey 
13354f48d5f6SEd Tanous inline void handleIPv4StaticPatch(
1336f476acbfSRatan Gupta     const std::string& ifaceId, nlohmann::json& input,
133701784826SJohnathan Mantey     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
13388d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
13391abe55efSEd Tanous {
134001784826SJohnathan Mantey     if ((!input.is_array()) || input.empty())
1341f476acbfSRatan Gupta     {
134271f52d96SEd Tanous         messages::propertyValueTypeError(
134371f52d96SEd Tanous             asyncResp->res,
1344bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1345d1d50814SRavi Teja             "IPv4StaticAddresses");
1346f476acbfSRatan Gupta         return;
1347f476acbfSRatan Gupta     }
1348f476acbfSRatan Gupta 
1349271584abSEd Tanous     unsigned entryIdx = 1;
135001784826SJohnathan Mantey     // Find the first static IP address currently active on the NIC and
135101784826SJohnathan Mantey     // match it to the first JSON element in the IPv4StaticAddresses array.
135201784826SJohnathan Mantey     // Match each subsequent JSON element to the next static IP programmed
135301784826SJohnathan Mantey     // into the NIC.
135485ffe86aSJiaqing Zhao     boost::container::flat_set<IPv4AddressData>::const_iterator nicIpEntry =
13552c70f800SEd Tanous         getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
135601784826SJohnathan Mantey 
1357537174c4SEd Tanous     for (nlohmann::json& thisJson : input)
13581abe55efSEd Tanous     {
13594a0cb85cSEd Tanous         std::string pathString =
1360d1d50814SRavi Teja             "IPv4StaticAddresses/" + std::to_string(entryIdx);
1361179db1d7SKowalski, Kamil 
136201784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1363f476acbfSRatan Gupta         {
1364537174c4SEd Tanous             std::optional<std::string> address;
1365537174c4SEd Tanous             std::optional<std::string> subnetMask;
1366537174c4SEd Tanous             std::optional<std::string> gateway;
1367537174c4SEd Tanous 
1368537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
13697e27d832SJohnathan Mantey                                      address, "SubnetMask", subnetMask,
13707e27d832SJohnathan Mantey                                      "Gateway", gateway))
1371537174c4SEd Tanous             {
137201784826SJohnathan Mantey                 messages::propertyValueFormatError(
137371f52d96SEd Tanous                     asyncResp->res,
137471f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
137571f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
137671f52d96SEd Tanous                     pathString);
1377537174c4SEd Tanous                 return;
1378179db1d7SKowalski, Kamil             }
1379179db1d7SKowalski, Kamil 
138001784826SJohnathan Mantey             // Find the address/subnet/gateway values. Any values that are
138101784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
138201784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
138301784826SJohnathan Mantey             // current request.
1384271584abSEd Tanous             const std::string* addr = nullptr;
1385271584abSEd Tanous             const std::string* gw = nullptr;
138601784826SJohnathan Mantey             uint8_t prefixLength = 0;
138701784826SJohnathan Mantey             bool errorInEntry = false;
1388537174c4SEd Tanous             if (address)
13891abe55efSEd Tanous             {
139001784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*address))
13911abe55efSEd Tanous                 {
139201784826SJohnathan Mantey                     addr = &(*address);
13934a0cb85cSEd Tanous                 }
139401784826SJohnathan Mantey                 else
139501784826SJohnathan Mantey                 {
1396bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
1397bf648f77SEd Tanous                                                        pathString + "/Address");
139801784826SJohnathan Mantey                     errorInEntry = true;
139901784826SJohnathan Mantey                 }
140001784826SJohnathan Mantey             }
140185ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
140201784826SJohnathan Mantey             {
140385ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
140401784826SJohnathan Mantey             }
140501784826SJohnathan Mantey             else
140601784826SJohnathan Mantey             {
140701784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
140801784826SJohnathan Mantey                                           pathString + "/Address");
140901784826SJohnathan Mantey                 errorInEntry = true;
14104a0cb85cSEd Tanous             }
14114a0cb85cSEd Tanous 
1412537174c4SEd Tanous             if (subnetMask)
14134a0cb85cSEd Tanous             {
1414537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14154a0cb85cSEd Tanous                 {
1416f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1417537174c4SEd Tanous                         asyncResp->res, *subnetMask,
14184a0cb85cSEd Tanous                         pathString + "/SubnetMask");
141901784826SJohnathan Mantey                     errorInEntry = true;
14204a0cb85cSEd Tanous                 }
14214a0cb85cSEd Tanous             }
142285ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
14234a0cb85cSEd Tanous             {
142485ffe86aSJiaqing Zhao                 if (!ipv4VerifyIpAndGetBitcount(nicIpEntry->netmask,
142501784826SJohnathan Mantey                                                 &prefixLength))
14264a0cb85cSEd Tanous                 {
142701784826SJohnathan Mantey                     messages::propertyValueFormatError(
142885ffe86aSJiaqing Zhao                         asyncResp->res, nicIpEntry->netmask,
142901784826SJohnathan Mantey                         pathString + "/SubnetMask");
143001784826SJohnathan Mantey                     errorInEntry = true;
14314a0cb85cSEd Tanous                 }
14324a0cb85cSEd Tanous             }
14331abe55efSEd Tanous             else
14341abe55efSEd Tanous             {
143501784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
143601784826SJohnathan Mantey                                           pathString + "/SubnetMask");
143701784826SJohnathan Mantey                 errorInEntry = true;
143801784826SJohnathan Mantey             }
143901784826SJohnathan Mantey 
144001784826SJohnathan Mantey             if (gateway)
144101784826SJohnathan Mantey             {
144201784826SJohnathan Mantey                 if (ipv4VerifyIpAndGetBitcount(*gateway))
144301784826SJohnathan Mantey                 {
144401784826SJohnathan Mantey                     gw = &(*gateway);
144501784826SJohnathan Mantey                 }
144601784826SJohnathan Mantey                 else
144701784826SJohnathan Mantey                 {
1448bf648f77SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1449bf648f77SEd Tanous                                                        pathString + "/Gateway");
145001784826SJohnathan Mantey                     errorInEntry = true;
145101784826SJohnathan Mantey                 }
145201784826SJohnathan Mantey             }
145385ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv4Data.cend())
145401784826SJohnathan Mantey             {
145585ffe86aSJiaqing Zhao                 gw = &nicIpEntry->gateway;
145601784826SJohnathan Mantey             }
145701784826SJohnathan Mantey             else
14581abe55efSEd Tanous             {
1459a08b46ccSJason M. Bills                 messages::propertyMissing(asyncResp->res,
14604a0cb85cSEd Tanous                                           pathString + "/Gateway");
146101784826SJohnathan Mantey                 errorInEntry = true;
14624a0cb85cSEd Tanous             }
14634a0cb85cSEd Tanous 
146401784826SJohnathan Mantey             if (errorInEntry)
14651abe55efSEd Tanous             {
146601784826SJohnathan Mantey                 return;
14674a0cb85cSEd Tanous             }
14684a0cb85cSEd Tanous 
146985ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
14701abe55efSEd Tanous             {
147185ffe86aSJiaqing Zhao                 deleteAndCreateIPv4(ifaceId, nicIpEntry->id, prefixLength, *gw,
1472bf648f77SEd Tanous                                     *addr, asyncResp);
147385ffe86aSJiaqing Zhao                 nicIpEntry =
147485ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
1475588c3f0dSKowalski, Kamil             }
147601784826SJohnathan Mantey             else
147701784826SJohnathan Mantey             {
1478cb13a392SEd Tanous                 createIPv4(ifaceId, prefixLength, *gateway, *address,
1479cb13a392SEd Tanous                            asyncResp);
14804a0cb85cSEd Tanous             }
14814a0cb85cSEd Tanous             entryIdx++;
14824a0cb85cSEd Tanous         }
148301784826SJohnathan Mantey         else
148401784826SJohnathan Mantey         {
148585ffe86aSJiaqing Zhao             if (nicIpEntry == ipv4Data.cend())
148601784826SJohnathan Mantey             {
148701784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
148801784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
148901784826SJohnathan Mantey                 // in error, so bail out.
149001784826SJohnathan Mantey                 if (thisJson.is_null())
149101784826SJohnathan Mantey                 {
149201784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
149301784826SJohnathan Mantey                     return;
149401784826SJohnathan Mantey                 }
149501784826SJohnathan Mantey                 messages::propertyValueFormatError(
149671f52d96SEd Tanous                     asyncResp->res,
149771f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
149871f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
149971f52d96SEd Tanous                     pathString);
150001784826SJohnathan Mantey                 return;
150101784826SJohnathan Mantey             }
150201784826SJohnathan Mantey 
150301784826SJohnathan Mantey             if (thisJson.is_null())
150401784826SJohnathan Mantey             {
150585ffe86aSJiaqing Zhao                 deleteIPv4(ifaceId, nicIpEntry->id, asyncResp);
150601784826SJohnathan Mantey             }
150785ffe86aSJiaqing Zhao             if (nicIpEntry != ipv4Data.cend())
150801784826SJohnathan Mantey             {
150985ffe86aSJiaqing Zhao                 nicIpEntry =
151085ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv4Data.cend());
151101784826SJohnathan Mantey             }
151201784826SJohnathan Mantey             entryIdx++;
151301784826SJohnathan Mantey         }
151401784826SJohnathan Mantey     }
15154a0cb85cSEd Tanous }
15164a0cb85cSEd Tanous 
15174f48d5f6SEd Tanous inline void handleStaticNameServersPatch(
1518f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::string& ifaceId,
1519f85837bfSRAJESWARAN THILLAIGOVINDAN     const std::vector<std::string>& updatedStaticNameServers,
15208d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1521f85837bfSRAJESWARAN THILLAIGOVINDAN {
1522f85837bfSRAJESWARAN THILLAIGOVINDAN     crow::connections::systemBus->async_method_call(
1523286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
1524f85837bfSRAJESWARAN THILLAIGOVINDAN             if (ec)
1525f85837bfSRAJESWARAN THILLAIGOVINDAN             {
1526f85837bfSRAJESWARAN THILLAIGOVINDAN                 messages::internalError(asyncResp->res);
1527f85837bfSRAJESWARAN THILLAIGOVINDAN                 return;
1528f85837bfSRAJESWARAN THILLAIGOVINDAN             }
1529f85837bfSRAJESWARAN THILLAIGOVINDAN         },
1530f85837bfSRAJESWARAN THILLAIGOVINDAN         "xyz.openbmc_project.Network",
1531f85837bfSRAJESWARAN THILLAIGOVINDAN         "/xyz/openbmc_project/network/" + ifaceId,
1532f85837bfSRAJESWARAN THILLAIGOVINDAN         "org.freedesktop.DBus.Properties", "Set",
1533bf648f77SEd Tanous         "xyz.openbmc_project.Network.EthernetInterface", "StaticNameServers",
1534168e20c1SEd Tanous         dbus::utility::DbusVariantType{updatedStaticNameServers});
1535f85837bfSRAJESWARAN THILLAIGOVINDAN }
1536f85837bfSRAJESWARAN THILLAIGOVINDAN 
15374f48d5f6SEd Tanous inline void handleIPv6StaticAddressesPatch(
1538f23b7296SEd Tanous     const std::string& ifaceId, const nlohmann::json& input,
153901784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data,
15408d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1541e48c0fc5SRavi Teja {
154201784826SJohnathan Mantey     if (!input.is_array() || input.empty())
1543e48c0fc5SRavi Teja     {
154471f52d96SEd Tanous         messages::propertyValueTypeError(
154571f52d96SEd Tanous             asyncResp->res,
1546bf648f77SEd Tanous             input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
1547e48c0fc5SRavi Teja             "IPv6StaticAddresses");
1548e48c0fc5SRavi Teja         return;
1549e48c0fc5SRavi Teja     }
1550271584abSEd Tanous     size_t entryIdx = 1;
155185ffe86aSJiaqing Zhao     boost::container::flat_set<IPv6AddressData>::const_iterator nicIpEntry =
15522c70f800SEd Tanous         getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1553f23b7296SEd Tanous     for (const nlohmann::json& thisJson : input)
1554e48c0fc5SRavi Teja     {
1555e48c0fc5SRavi Teja         std::string pathString =
1556e48c0fc5SRavi Teja             "IPv6StaticAddresses/" + std::to_string(entryIdx);
1557e48c0fc5SRavi Teja 
155801784826SJohnathan Mantey         if (!thisJson.is_null() && !thisJson.empty())
1559e48c0fc5SRavi Teja         {
1560e48c0fc5SRavi Teja             std::optional<std::string> address;
1561e48c0fc5SRavi Teja             std::optional<uint8_t> prefixLength;
1562f23b7296SEd Tanous             nlohmann::json thisJsonCopy = thisJson;
1563bf648f77SEd Tanous             if (!json_util::readJson(thisJsonCopy, asyncResp->res, "Address",
1564bf648f77SEd Tanous                                      address, "PrefixLength", prefixLength))
1565e48c0fc5SRavi Teja             {
156601784826SJohnathan Mantey                 messages::propertyValueFormatError(
156771f52d96SEd Tanous                     asyncResp->res,
156871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
156971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
157071f52d96SEd Tanous                     pathString);
1571e48c0fc5SRavi Teja                 return;
1572e48c0fc5SRavi Teja             }
1573e48c0fc5SRavi Teja 
1574543f4400SEd Tanous             const std::string* addr = nullptr;
1575543f4400SEd Tanous             uint8_t prefix = 0;
157601784826SJohnathan Mantey 
157701784826SJohnathan Mantey             // Find the address and prefixLength values. Any values that are
157801784826SJohnathan Mantey             // not explicitly provided are assumed to be unmodified from the
157901784826SJohnathan Mantey             // current state of the interface. Merge existing state into the
158001784826SJohnathan Mantey             // current request.
1581e48c0fc5SRavi Teja             if (address)
1582e48c0fc5SRavi Teja             {
158301784826SJohnathan Mantey                 addr = &(*address);
1584e48c0fc5SRavi Teja             }
158585ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
158601784826SJohnathan Mantey             {
158785ffe86aSJiaqing Zhao                 addr = &(nicIpEntry->address);
158801784826SJohnathan Mantey             }
158901784826SJohnathan Mantey             else
159001784826SJohnathan Mantey             {
159101784826SJohnathan Mantey                 messages::propertyMissing(asyncResp->res,
159201784826SJohnathan Mantey                                           pathString + "/Address");
159301784826SJohnathan Mantey                 return;
1594e48c0fc5SRavi Teja             }
1595e48c0fc5SRavi Teja 
1596e48c0fc5SRavi Teja             if (prefixLength)
1597e48c0fc5SRavi Teja             {
159801784826SJohnathan Mantey                 prefix = *prefixLength;
159901784826SJohnathan Mantey             }
160085ffe86aSJiaqing Zhao             else if (nicIpEntry != ipv6Data.end())
1601e48c0fc5SRavi Teja             {
160285ffe86aSJiaqing Zhao                 prefix = nicIpEntry->prefixLength;
1603e48c0fc5SRavi Teja             }
1604e48c0fc5SRavi Teja             else
1605e48c0fc5SRavi Teja             {
1606e48c0fc5SRavi Teja                 messages::propertyMissing(asyncResp->res,
1607e48c0fc5SRavi Teja                                           pathString + "/PrefixLength");
160801784826SJohnathan Mantey                 return;
1609e48c0fc5SRavi Teja             }
1610e48c0fc5SRavi Teja 
161185ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.end())
1612e48c0fc5SRavi Teja             {
161385ffe86aSJiaqing Zhao                 deleteAndCreateIPv6(ifaceId, nicIpEntry->id, prefix, *addr,
1614e48c0fc5SRavi Teja                                     asyncResp);
161585ffe86aSJiaqing Zhao                 nicIpEntry =
161685ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
161701784826SJohnathan Mantey             }
161801784826SJohnathan Mantey             else
161901784826SJohnathan Mantey             {
162001784826SJohnathan Mantey                 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1621e48c0fc5SRavi Teja             }
1622e48c0fc5SRavi Teja             entryIdx++;
1623e48c0fc5SRavi Teja         }
162401784826SJohnathan Mantey         else
162501784826SJohnathan Mantey         {
162685ffe86aSJiaqing Zhao             if (nicIpEntry == ipv6Data.end())
162701784826SJohnathan Mantey             {
162801784826SJohnathan Mantey                 // Requesting a DELETE/DO NOT MODIFY action for an item
162901784826SJohnathan Mantey                 // that isn't present on the eth(n) interface. Input JSON is
163001784826SJohnathan Mantey                 // in error, so bail out.
163101784826SJohnathan Mantey                 if (thisJson.is_null())
163201784826SJohnathan Mantey                 {
163301784826SJohnathan Mantey                     messages::resourceCannotBeDeleted(asyncResp->res);
163401784826SJohnathan Mantey                     return;
163501784826SJohnathan Mantey                 }
163601784826SJohnathan Mantey                 messages::propertyValueFormatError(
163771f52d96SEd Tanous                     asyncResp->res,
163871f52d96SEd Tanous                     thisJson.dump(2, ' ', true,
163971f52d96SEd Tanous                                   nlohmann::json::error_handler_t::replace),
164071f52d96SEd Tanous                     pathString);
164101784826SJohnathan Mantey                 return;
164201784826SJohnathan Mantey             }
164301784826SJohnathan Mantey 
164401784826SJohnathan Mantey             if (thisJson.is_null())
164501784826SJohnathan Mantey             {
164685ffe86aSJiaqing Zhao                 deleteIPv6(ifaceId, nicIpEntry->id, asyncResp);
164701784826SJohnathan Mantey             }
164885ffe86aSJiaqing Zhao             if (nicIpEntry != ipv6Data.cend())
164901784826SJohnathan Mantey             {
165085ffe86aSJiaqing Zhao                 nicIpEntry =
165185ffe86aSJiaqing Zhao                     getNextStaticIpEntry(++nicIpEntry, ipv6Data.cend());
165201784826SJohnathan Mantey             }
165301784826SJohnathan Mantey             entryIdx++;
165401784826SJohnathan Mantey         }
165501784826SJohnathan Mantey     }
1656e48c0fc5SRavi Teja }
1657e48c0fc5SRavi Teja 
16584f48d5f6SEd Tanous inline void parseInterfaceData(
16598d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
16608d1b46d7Szhanghch05     const std::string& ifaceId, const EthernetInterfaceData& ethData,
1661e48c0fc5SRavi Teja     const boost::container::flat_set<IPv4AddressData>& ipv4Data,
166201784826SJohnathan Mantey     const boost::container::flat_set<IPv6AddressData>& ipv6Data)
16634a0cb85cSEd Tanous {
1664eeedda23SJohnathan Mantey     constexpr const std::array<const char*, 1> inventoryForEthernet = {
1665eeedda23SJohnathan Mantey         "xyz.openbmc_project.Inventory.Item.Ethernet"};
1666eeedda23SJohnathan Mantey 
16672c70f800SEd Tanous     nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
166881ce609eSEd Tanous     jsonResponse["Id"] = ifaceId;
16692c70f800SEd Tanous     jsonResponse["@odata.id"] =
167081ce609eSEd Tanous         "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId;
16712c70f800SEd Tanous     jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1672eeedda23SJohnathan Mantey 
1673eeedda23SJohnathan Mantey     auto health = std::make_shared<HealthPopulate>(asyncResp);
1674eeedda23SJohnathan Mantey 
1675eeedda23SJohnathan Mantey     crow::connections::systemBus->async_method_call(
1676eeedda23SJohnathan Mantey         [health](const boost::system::error_code ec,
1677b9d36b47SEd Tanous                  const dbus::utility::MapperGetSubTreePathsResponse& resp) {
1678eeedda23SJohnathan Mantey             if (ec)
1679029573d4SEd Tanous             {
1680eeedda23SJohnathan Mantey                 return;
1681eeedda23SJohnathan Mantey             }
1682eeedda23SJohnathan Mantey 
1683914e2d5dSEd Tanous             health->inventory = resp;
1684eeedda23SJohnathan Mantey         },
1685eeedda23SJohnathan Mantey         "xyz.openbmc_project.ObjectMapper",
1686eeedda23SJohnathan Mantey         "/xyz/openbmc_project/object_mapper",
1687bf648f77SEd Tanous         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/", int32_t(0),
1688bf648f77SEd Tanous         inventoryForEthernet);
1689eeedda23SJohnathan Mantey 
1690eeedda23SJohnathan Mantey     health->populate();
1691eeedda23SJohnathan Mantey 
1692eeedda23SJohnathan Mantey     if (ethData.nicEnabled)
1693eeedda23SJohnathan Mantey     {
16942c70f800SEd Tanous         jsonResponse["LinkStatus"] = "LinkUp";
16952c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Enabled";
1696029573d4SEd Tanous     }
1697029573d4SEd Tanous     else
1698029573d4SEd Tanous     {
16992c70f800SEd Tanous         jsonResponse["LinkStatus"] = "NoLink";
17002c70f800SEd Tanous         jsonResponse["Status"]["State"] = "Disabled";
1701029573d4SEd Tanous     }
1702aa05fb27SJohnathan Mantey 
17032c70f800SEd Tanous     jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17042c70f800SEd Tanous     jsonResponse["SpeedMbps"] = ethData.speed;
170535fb5311STejas Patil     jsonResponse["MTUSize"] = ethData.mtuSize;
170682695a5bSJiaqing Zhao     jsonResponse["MACAddress"] = ethData.macAddress;
17072c70f800SEd Tanous     jsonResponse["DHCPv4"]["DHCPEnabled"] =
170882695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, true);
170982695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.ntpEnabled;
171082695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.dnsEnabled;
171182695a5bSJiaqing Zhao     jsonResponse["DHCPv4"]["UseDomainName"] = ethData.hostNameEnabled;
17121f8c7b5dSJohnathan Mantey 
17132c70f800SEd Tanous     jsonResponse["DHCPv6"]["OperatingMode"] =
171482695a5bSJiaqing Zhao         translateDhcpEnabledToBool(ethData.dhcpEnabled, false) ? "Stateful"
17151f8c7b5dSJohnathan Mantey                                                                : "Disabled";
171682695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.ntpEnabled;
171782695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.dnsEnabled;
171882695a5bSJiaqing Zhao     jsonResponse["DHCPv6"]["UseDomainName"] = ethData.hostNameEnabled;
17192a133282Smanojkiraneda 
172082695a5bSJiaqing Zhao     if (!ethData.hostName.empty())
17214a0cb85cSEd Tanous     {
172282695a5bSJiaqing Zhao         jsonResponse["HostName"] = ethData.hostName;
1723ab6554f1SJoshi-Mansi 
1724ab6554f1SJoshi-Mansi         // When domain name is empty then it means, that it is a network
1725ab6554f1SJoshi-Mansi         // without domain names, and the host name itself must be treated as
1726ab6554f1SJoshi-Mansi         // FQDN
172782695a5bSJiaqing Zhao         std::string fqdn = ethData.hostName;
1728d24bfc7aSJennifer Lee         if (!ethData.domainnames.empty())
1729d24bfc7aSJennifer Lee         {
17302c70f800SEd Tanous             fqdn += "." + ethData.domainnames[0];
1731d24bfc7aSJennifer Lee         }
17322c70f800SEd Tanous         jsonResponse["FQDN"] = fqdn;
17334a0cb85cSEd Tanous     }
17344a0cb85cSEd Tanous 
17352c70f800SEd Tanous     jsonResponse["VLANs"] = {
1736bf648f77SEd Tanous         {"@odata.id",
1737bf648f77SEd Tanous          "/redfish/v1/Managers/bmc/EthernetInterfaces/" + ifaceId + "/VLANs"}};
1738fda13ad2SSunitha Harish 
17392c70f800SEd Tanous     jsonResponse["NameServers"] = ethData.nameServers;
17402c70f800SEd Tanous     jsonResponse["StaticNameServers"] = ethData.staticNameServers;
17414a0cb85cSEd Tanous 
17422c70f800SEd Tanous     nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
17432c70f800SEd Tanous     nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
17442c70f800SEd Tanous     ipv4Array = nlohmann::json::array();
17452c70f800SEd Tanous     ipv4StaticArray = nlohmann::json::array();
17469eb808c1SEd Tanous     for (const auto& ipv4Config : ipv4Data)
17474a0cb85cSEd Tanous     {
1748fa5053a6SGunnar Mills 
17492c70f800SEd Tanous         std::string gatewayStr = ipv4Config.gateway;
1750fa5053a6SGunnar Mills         if (gatewayStr.empty())
1751fa5053a6SGunnar Mills         {
1752fa5053a6SGunnar Mills             gatewayStr = "0.0.0.0";
1753fa5053a6SGunnar Mills         }
1754*1476687dSEd Tanous         nlohmann::json::object_t ipv4;
1755*1476687dSEd Tanous         ipv4["AddressOrigin"] = ipv4Config.origin;
1756*1476687dSEd Tanous         ipv4["SubnetMask"] = ipv4Config.netmask;
1757*1476687dSEd Tanous         ipv4["Address"] = ipv4Config.address;
1758*1476687dSEd Tanous         ipv4["Gateway"] = gatewayStr;
1759fa5053a6SGunnar Mills 
17602c70f800SEd Tanous         if (ipv4Config.origin == "Static")
1761d1d50814SRavi Teja         {
1762*1476687dSEd Tanous             ipv4StaticArray.push_back(ipv4);
1763d1d50814SRavi Teja         }
1764*1476687dSEd Tanous 
1765*1476687dSEd Tanous         ipv4Array.push_back(std::move(ipv4));
176601784826SJohnathan Mantey     }
1767d1d50814SRavi Teja 
176882695a5bSJiaqing Zhao     std::string ipv6GatewayStr = ethData.ipv6DefaultGateway;
17697ea79e5eSRavi Teja     if (ipv6GatewayStr.empty())
17707ea79e5eSRavi Teja     {
17717ea79e5eSRavi Teja         ipv6GatewayStr = "0:0:0:0:0:0:0:0";
17727ea79e5eSRavi Teja     }
17737ea79e5eSRavi Teja 
17747ea79e5eSRavi Teja     jsonResponse["IPv6DefaultGateway"] = ipv6GatewayStr;
1775e48c0fc5SRavi Teja 
17762c70f800SEd Tanous     nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
17772c70f800SEd Tanous     nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
17782c70f800SEd Tanous     ipv6Array = nlohmann::json::array();
17792c70f800SEd Tanous     ipv6StaticArray = nlohmann::json::array();
17807f2e23e9SJohnathan Mantey     nlohmann::json& ipv6AddrPolicyTable =
17812c70f800SEd Tanous         jsonResponse["IPv6AddressPolicyTable"];
17827f2e23e9SJohnathan Mantey     ipv6AddrPolicyTable = nlohmann::json::array();
17839eb808c1SEd Tanous     for (const auto& ipv6Config : ipv6Data)
1784e48c0fc5SRavi Teja     {
1785*1476687dSEd Tanous         nlohmann::json::object_t ipv6;
1786*1476687dSEd Tanous         ipv6["Address"] = ipv6Config.address;
1787*1476687dSEd Tanous         ipv6["PrefixLength"] = ipv6Config.prefixLength;
1788*1476687dSEd Tanous         ipv6["AddressOrigin"] = ipv6Config.origin;
1789*1476687dSEd Tanous         ipv6["AddressState"] = nullptr;
1790*1476687dSEd Tanous         ipv6Array.push_back(std::move(ipv6));
17912c70f800SEd Tanous         if (ipv6Config.origin == "Static")
1792e48c0fc5SRavi Teja         {
1793*1476687dSEd Tanous             nlohmann::json::object_t ipv6Static;
1794*1476687dSEd Tanous             ipv6Static["Address"] = ipv6Config.address;
1795*1476687dSEd Tanous             ipv6Static["PrefixLength"] = ipv6Config.prefixLength;
1796*1476687dSEd Tanous             ipv6StaticArray.push_back(std::move(ipv6Static));
179701784826SJohnathan Mantey         }
1798e48c0fc5SRavi Teja     }
1799588c3f0dSKowalski, Kamil }
1800588c3f0dSKowalski, Kamil 
18014f48d5f6SEd Tanous inline void parseInterfaceData(nlohmann::json& jsonResponse,
1802bf648f77SEd Tanous                                const std::string& parentIfaceId,
1803bf648f77SEd Tanous                                const std::string& ifaceId,
1804bf648f77SEd Tanous                                const EthernetInterfaceData& ethData)
18051abe55efSEd Tanous {
1806bf648f77SEd Tanous     // Fill out obvious data...
1807bf648f77SEd Tanous     jsonResponse["Id"] = ifaceId;
1808bf648f77SEd Tanous     jsonResponse["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1809bf648f77SEd Tanous                                 parentIfaceId + "/VLANs/" + ifaceId;
1810bf648f77SEd Tanous 
1811bf648f77SEd Tanous     jsonResponse["VLANEnable"] = true;
181282695a5bSJiaqing Zhao     if (!ethData.vlanId.empty())
1813bf648f77SEd Tanous     {
181482695a5bSJiaqing Zhao         jsonResponse["VLANId"] = ethData.vlanId.back();
1815bf648f77SEd Tanous     }
1816bf648f77SEd Tanous }
1817bf648f77SEd Tanous 
18184f48d5f6SEd Tanous inline bool verifyNames(const std::string& parent, const std::string& iface)
1819bf648f77SEd Tanous {
1820dcf2ebc0SEd Tanous     return boost::starts_with(iface, parent + "_");
1821bf648f77SEd Tanous }
1822bf648f77SEd Tanous 
1823bf648f77SEd Tanous inline void requestEthernetInterfacesRoutes(App& app)
1824bf648f77SEd Tanous {
1825bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
1826ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterfaceCollection)
1827*1476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
1828*1476687dSEd Tanous             [&app](const crow::Request& req,
1829*1476687dSEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) {
183045ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
183145ca1b86SEd Tanous                 {
183245ca1b86SEd Tanous                     return;
183345ca1b86SEd Tanous                 }
183445ca1b86SEd Tanous 
1835bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1836bf648f77SEd Tanous                     "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1837bf648f77SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
1838bf648f77SEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1839bf648f77SEd Tanous                 asyncResp->res.jsonValue["Name"] =
1840bf648f77SEd Tanous                     "Ethernet Network Interface Collection";
1841bf648f77SEd Tanous                 asyncResp->res.jsonValue["Description"] =
1842bf648f77SEd Tanous                     "Collection of EthernetInterfaces for this Manager";
1843bf648f77SEd Tanous 
1844bf648f77SEd Tanous                 // Get eth interface list, and call the below callback for JSON
1845bf648f77SEd Tanous                 // preparation
1846*1476687dSEd Tanous                 getEthernetIfaceList([asyncResp](
1847*1476687dSEd Tanous                                          const bool& success,
1848bf648f77SEd Tanous                                          const boost::container::flat_set<
1849bf648f77SEd Tanous                                              std::string>& ifaceList) {
1850bf648f77SEd Tanous                     if (!success)
18511abe55efSEd Tanous                     {
1852f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
18539391bb9cSRapkiewicz, Pawel                         return;
18549391bb9cSRapkiewicz, Pawel                     }
18559391bb9cSRapkiewicz, Pawel 
1856bf648f77SEd Tanous                     nlohmann::json& ifaceArray =
1857bf648f77SEd Tanous                         asyncResp->res.jsonValue["Members"];
1858bf648f77SEd Tanous                     ifaceArray = nlohmann::json::array();
1859bf648f77SEd Tanous                     std::string tag = "_";
1860bf648f77SEd Tanous                     for (const std::string& ifaceItem : ifaceList)
1861bf648f77SEd Tanous                     {
1862bf648f77SEd Tanous                         std::size_t found = ifaceItem.find(tag);
1863bf648f77SEd Tanous                         if (found == std::string::npos)
1864bf648f77SEd Tanous                         {
1865*1476687dSEd Tanous                             nlohmann::json::object_t iface;
1866*1476687dSEd Tanous                             iface["@odata.id"] =
1867bf648f77SEd Tanous                                 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1868*1476687dSEd Tanous                                 ifaceItem;
1869*1476687dSEd Tanous                             ifaceArray.push_back(std::move(iface));
1870bf648f77SEd Tanous                         }
1871bf648f77SEd Tanous                     }
1872bf648f77SEd Tanous 
1873bf648f77SEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
1874bf648f77SEd Tanous                         ifaceArray.size();
1875bf648f77SEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
1876bf648f77SEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces";
1877bf648f77SEd Tanous                 });
1878bf648f77SEd Tanous             });
1879bf648f77SEd Tanous 
1880bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1881ed398213SEd Tanous         .privileges(redfish::privileges::getEthernetInterface)
1882bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
188345ca1b86SEd Tanous             [&app](const crow::Request& req,
1884bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1885bf648f77SEd Tanous                    const std::string& ifaceId) {
188645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
188745ca1b86SEd Tanous                 {
188845ca1b86SEd Tanous                     return;
188945ca1b86SEd Tanous                 }
18904a0cb85cSEd Tanous                 getEthernetIfaceData(
1891bf648f77SEd Tanous                     ifaceId,
1892bf648f77SEd Tanous                     [asyncResp,
1893bf648f77SEd Tanous                      ifaceId](const bool& success,
1894bf648f77SEd Tanous                               const EthernetInterfaceData& ethData,
1895bf648f77SEd Tanous                               const boost::container::flat_set<IPv4AddressData>&
1896bf648f77SEd Tanous                                   ipv4Data,
1897bf648f77SEd Tanous                               const boost::container::flat_set<IPv6AddressData>&
1898bf648f77SEd Tanous                                   ipv6Data) {
18994a0cb85cSEd Tanous                         if (!success)
19001abe55efSEd Tanous                         {
1901bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
1902bf648f77SEd Tanous                             // existing object, and other errors
1903bf648f77SEd Tanous                             messages::resourceNotFound(
1904bf648f77SEd Tanous                                 asyncResp->res, "EthernetInterface", ifaceId);
19054a0cb85cSEd Tanous                             return;
19069391bb9cSRapkiewicz, Pawel                         }
19074c9afe43SEd Tanous 
19080f74e643SEd Tanous                         asyncResp->res.jsonValue["@odata.type"] =
1909fda13ad2SSunitha Harish                             "#EthernetInterface.v1_4_1.EthernetInterface";
1910bf648f77SEd Tanous                         asyncResp->res.jsonValue["Name"] =
1911bf648f77SEd Tanous                             "Manager Ethernet Interface";
19120f74e643SEd Tanous                         asyncResp->res.jsonValue["Description"] =
19130f74e643SEd Tanous                             "Management Network Interface";
19140f74e643SEd Tanous 
1915bf648f77SEd Tanous                         parseInterfaceData(asyncResp, ifaceId, ethData,
1916bf648f77SEd Tanous                                            ipv4Data, ipv6Data);
19179391bb9cSRapkiewicz, Pawel                     });
1918bf648f77SEd Tanous             });
19199391bb9cSRapkiewicz, Pawel 
1920bf648f77SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/")
1921ed398213SEd Tanous         .privileges(redfish::privileges::patchEthernetInterface)
1922bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
192345ca1b86SEd Tanous             [&app](const crow::Request& req,
1924bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1925bf648f77SEd Tanous                    const std::string& ifaceId) {
192645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
192745ca1b86SEd Tanous                 {
192845ca1b86SEd Tanous                     return;
192945ca1b86SEd Tanous                 }
1930bc0bd6e0SEd Tanous                 std::optional<std::string> hostname;
1931ab6554f1SJoshi-Mansi                 std::optional<std::string> fqdn;
1932d577665bSRatan Gupta                 std::optional<std::string> macAddress;
19339a6fc6feSRavi Teja                 std::optional<std::string> ipv6DefaultGateway;
1934d1d50814SRavi Teja                 std::optional<nlohmann::json> ipv4StaticAddresses;
1935e48c0fc5SRavi Teja                 std::optional<nlohmann::json> ipv6StaticAddresses;
1936f85837bfSRAJESWARAN THILLAIGOVINDAN                 std::optional<std::vector<std::string>> staticNameServers;
1937da131a9aSJennifer Lee                 std::optional<nlohmann::json> dhcpv4;
19381f8c7b5dSJohnathan Mantey                 std::optional<nlohmann::json> dhcpv6;
1939eeedda23SJohnathan Mantey                 std::optional<bool> interfaceEnabled;
194035fb5311STejas Patil                 std::optional<size_t> mtuSize;
19411f8c7b5dSJohnathan Mantey                 DHCPParameters v4dhcpParms;
19421f8c7b5dSJohnathan Mantey                 DHCPParameters v6dhcpParms;
19430627a2c7SEd Tanous 
194415ed6780SWilly Tu                 if (!json_util::readJsonPatch(
19458d1b46d7Szhanghch05                         req, asyncResp->res, "HostName", hostname, "FQDN", fqdn,
1946bf648f77SEd Tanous                         "IPv4StaticAddresses", ipv4StaticAddresses,
1947bf648f77SEd Tanous                         "MACAddress", macAddress, "StaticNameServers",
1948bf648f77SEd Tanous                         staticNameServers, "IPv6DefaultGateway",
1949bf648f77SEd Tanous                         ipv6DefaultGateway, "IPv6StaticAddresses",
1950ab6554f1SJoshi-Mansi                         ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
195135fb5311STejas Patil                         "MTUSize", mtuSize, "InterfaceEnabled",
195235fb5311STejas Patil                         interfaceEnabled))
19531abe55efSEd Tanous                 {
1954588c3f0dSKowalski, Kamil                     return;
1955588c3f0dSKowalski, Kamil                 }
1956da131a9aSJennifer Lee                 if (dhcpv4)
1957da131a9aSJennifer Lee                 {
1958bf648f77SEd Tanous                     if (!json_util::readJson(
1959bf648f77SEd Tanous                             *dhcpv4, asyncResp->res, "DHCPEnabled",
19601f8c7b5dSJohnathan Mantey                             v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
196182695a5bSJiaqing Zhao                             v4dhcpParms.useDnsServers, "UseNTPServers",
196282695a5bSJiaqing Zhao                             v4dhcpParms.useNtpServers, "UseDomainName",
196382695a5bSJiaqing Zhao                             v4dhcpParms.useDomainName))
19641f8c7b5dSJohnathan Mantey                     {
19651f8c7b5dSJohnathan Mantey                         return;
19661f8c7b5dSJohnathan Mantey                     }
19671f8c7b5dSJohnathan Mantey                 }
19681f8c7b5dSJohnathan Mantey 
19691f8c7b5dSJohnathan Mantey                 if (dhcpv6)
19701f8c7b5dSJohnathan Mantey                 {
1971bf648f77SEd Tanous                     if (!json_util::readJson(
1972bf648f77SEd Tanous                             *dhcpv6, asyncResp->res, "OperatingMode",
1973bf648f77SEd Tanous                             v6dhcpParms.dhcpv6OperatingMode, "UseDNSServers",
197482695a5bSJiaqing Zhao                             v6dhcpParms.useDnsServers, "UseNTPServers",
197582695a5bSJiaqing Zhao                             v6dhcpParms.useNtpServers, "UseDomainName",
197682695a5bSJiaqing Zhao                             v6dhcpParms.useDomainName))
19771f8c7b5dSJohnathan Mantey                     {
19781f8c7b5dSJohnathan Mantey                         return;
19791f8c7b5dSJohnathan Mantey                     }
1980da131a9aSJennifer Lee                 }
1981da131a9aSJennifer Lee 
1982bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
1983bf648f77SEd Tanous                 // for JSON preparation
19844a0cb85cSEd Tanous                 getEthernetIfaceData(
19852c70f800SEd Tanous                     ifaceId,
1986bf648f77SEd Tanous                     [asyncResp, ifaceId, hostname = std::move(hostname),
1987ab6554f1SJoshi-Mansi                      fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1988d1d50814SRavi Teja                      ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19899a6fc6feSRavi Teja                      ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1990e48c0fc5SRavi Teja                      ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19911f8c7b5dSJohnathan Mantey                      staticNameServers = std::move(staticNameServers),
19921f8c7b5dSJohnathan Mantey                      dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
199335fb5311STejas Patil                      mtuSize = mtuSize, v4dhcpParms = std::move(v4dhcpParms),
1994f23b7296SEd Tanous                      v6dhcpParms = std::move(v6dhcpParms), interfaceEnabled](
1995bf648f77SEd Tanous                         const bool& success,
1996bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
1997bf648f77SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&
1998bf648f77SEd Tanous                             ipv4Data,
1999bf648f77SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&
2000bf648f77SEd Tanous                             ipv6Data) {
20011abe55efSEd Tanous                         if (!success)
20021abe55efSEd Tanous                         {
2003588c3f0dSKowalski, Kamil                             // ... otherwise return error
2004bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2005bf648f77SEd Tanous                             // existing object, and other errors
2006bf648f77SEd Tanous                             messages::resourceNotFound(
2007bf648f77SEd Tanous                                 asyncResp->res, "Ethernet Interface", ifaceId);
2008588c3f0dSKowalski, Kamil                             return;
2009588c3f0dSKowalski, Kamil                         }
2010588c3f0dSKowalski, Kamil 
20111f8c7b5dSJohnathan Mantey                         if (dhcpv4 || dhcpv6)
20121f8c7b5dSJohnathan Mantey                         {
2013bf648f77SEd Tanous                             handleDHCPPatch(ifaceId, ethData, v4dhcpParms,
2014bf648f77SEd Tanous                                             v6dhcpParms, asyncResp);
20151f8c7b5dSJohnathan Mantey                         }
20161f8c7b5dSJohnathan Mantey 
20170627a2c7SEd Tanous                         if (hostname)
20181abe55efSEd Tanous                         {
20190627a2c7SEd Tanous                             handleHostnamePatch(*hostname, asyncResp);
20201abe55efSEd Tanous                         }
20210627a2c7SEd Tanous 
2022ab6554f1SJoshi-Mansi                         if (fqdn)
2023ab6554f1SJoshi-Mansi                         {
20242c70f800SEd Tanous                             handleFqdnPatch(ifaceId, *fqdn, asyncResp);
2025ab6554f1SJoshi-Mansi                         }
2026ab6554f1SJoshi-Mansi 
2027d577665bSRatan Gupta                         if (macAddress)
2028d577665bSRatan Gupta                         {
2029bf648f77SEd Tanous                             handleMACAddressPatch(ifaceId, *macAddress,
2030bf648f77SEd Tanous                                                   asyncResp);
2031d577665bSRatan Gupta                         }
2032d577665bSRatan Gupta 
2033d1d50814SRavi Teja                         if (ipv4StaticAddresses)
2034d1d50814SRavi Teja                         {
2035bf648f77SEd Tanous                             // TODO(ed) for some reason the capture of
2036bf648f77SEd Tanous                             // ipv4Addresses above is returning a const value,
2037bf648f77SEd Tanous                             // not a non-const value. This doesn't really work
2038bf648f77SEd Tanous                             // for us, as we need to be able to efficiently move
2039bf648f77SEd Tanous                             // out the intermedia nlohmann::json objects. This
2040bf648f77SEd Tanous                             // makes a copy of the structure, and operates on
2041bf648f77SEd Tanous                             // that, but could be done more efficiently
2042f23b7296SEd Tanous                             nlohmann::json ipv4Static = *ipv4StaticAddresses;
20432c70f800SEd Tanous                             handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2044d1d50814SRavi Teja                                                   asyncResp);
20451abe55efSEd Tanous                         }
20460627a2c7SEd Tanous 
2047f85837bfSRAJESWARAN THILLAIGOVINDAN                         if (staticNameServers)
2048f85837bfSRAJESWARAN THILLAIGOVINDAN                         {
2049bf648f77SEd Tanous                             handleStaticNameServersPatch(
2050bf648f77SEd Tanous                                 ifaceId, *staticNameServers, asyncResp);
2051f85837bfSRAJESWARAN THILLAIGOVINDAN                         }
20529a6fc6feSRavi Teja 
20539a6fc6feSRavi Teja                         if (ipv6DefaultGateway)
20549a6fc6feSRavi Teja                         {
20559a6fc6feSRavi Teja                             messages::propertyNotWritable(asyncResp->res,
20569a6fc6feSRavi Teja                                                           "IPv6DefaultGateway");
20579a6fc6feSRavi Teja                         }
2058e48c0fc5SRavi Teja 
2059e48c0fc5SRavi Teja                         if (ipv6StaticAddresses)
2060e48c0fc5SRavi Teja                         {
2061f5b191a6SEd Tanous                             const nlohmann::json& ipv6Static =
2062f5b191a6SEd Tanous                                 *ipv6StaticAddresses;
20632c70f800SEd Tanous                             handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
206401784826SJohnathan Mantey                                                            ipv6Data, asyncResp);
2065e48c0fc5SRavi Teja                         }
2066eeedda23SJohnathan Mantey 
2067eeedda23SJohnathan Mantey                         if (interfaceEnabled)
2068eeedda23SJohnathan Mantey                         {
2069eeedda23SJohnathan Mantey                             setEthernetInterfaceBoolProperty(
2070bf648f77SEd Tanous                                 ifaceId, "NICEnabled", *interfaceEnabled,
2071bf648f77SEd Tanous                                 asyncResp);
2072eeedda23SJohnathan Mantey                         }
207335fb5311STejas Patil 
207435fb5311STejas Patil                         if (mtuSize)
207535fb5311STejas Patil                         {
207635fb5311STejas Patil                             handleMTUSizePatch(ifaceId, *mtuSize, asyncResp);
207735fb5311STejas Patil                         }
2078588c3f0dSKowalski, Kamil                     });
2079bf648f77SEd Tanous             });
20809391bb9cSRapkiewicz, Pawel 
2081bf648f77SEd Tanous     BMCWEB_ROUTE(
2082bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
2083ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterface)
2084bf648f77SEd Tanous         .methods(boost::beast::http::verb::get)(
208545ca1b86SEd Tanous             [&app](const crow::Request& req,
2086bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
208745ca1b86SEd Tanous                    const std::string& parentIfaceId,
208845ca1b86SEd Tanous                    const std::string& ifaceId) {
208945ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
209045ca1b86SEd Tanous                 {
209145ca1b86SEd Tanous                     return;
209245ca1b86SEd Tanous                 }
20938d1b46d7Szhanghch05                 asyncResp->res.jsonValue["@odata.type"] =
20940f74e643SEd Tanous                     "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
20958d1b46d7Szhanghch05                 asyncResp->res.jsonValue["Name"] = "VLAN Network Interface";
2096e439f0f8SKowalski, Kamil 
20972c70f800SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
20981abe55efSEd Tanous                 {
2099a434f2bdSEd Tanous                     return;
2100a434f2bdSEd Tanous                 }
2101a434f2bdSEd Tanous 
2102bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2103bf648f77SEd Tanous                 // for JSON preparation
21044a0cb85cSEd Tanous                 getEthernetIfaceData(
2105bf648f77SEd Tanous                     ifaceId,
2106bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2107bf648f77SEd Tanous                         const bool& success,
2108bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2109cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2110cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
211182695a5bSJiaqing Zhao                         if (success && !ethData.vlanId.empty())
21121abe55efSEd Tanous                         {
2113bf648f77SEd Tanous                             parseInterfaceData(asyncResp->res.jsonValue,
2114bf648f77SEd Tanous                                                parentIfaceId, ifaceId, ethData);
21151abe55efSEd Tanous                         }
21161abe55efSEd Tanous                         else
21171abe55efSEd Tanous                         {
2118e439f0f8SKowalski, Kamil                             // ... otherwise return error
2119bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2120bf648f77SEd Tanous                             // existing object, and other errors
2121bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2122bf648f77SEd Tanous                                                        "VLAN Network Interface",
2123bf648f77SEd Tanous                                                        ifaceId);
2124e439f0f8SKowalski, Kamil                         }
2125e439f0f8SKowalski, Kamil                     });
2126bf648f77SEd Tanous             });
2127e439f0f8SKowalski, Kamil 
2128bf648f77SEd Tanous     BMCWEB_ROUTE(
2129bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
21303d768a16SAbhishek Patel         .privileges(redfish::privileges::patchVLanNetworkInterface)
2131bf648f77SEd Tanous         .methods(boost::beast::http::verb::patch)(
213245ca1b86SEd Tanous             [&app](const crow::Request& req,
2133bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
213445ca1b86SEd Tanous                    const std::string& parentIfaceId,
213545ca1b86SEd Tanous                    const std::string& ifaceId) {
213645ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
213745ca1b86SEd Tanous                 {
213845ca1b86SEd Tanous                     return;
213945ca1b86SEd Tanous                 }
2140fda13ad2SSunitha Harish                 if (!verifyNames(parentIfaceId, ifaceId))
21411abe55efSEd Tanous                 {
2142bf648f77SEd Tanous                     messages::resourceNotFound(
2143bf648f77SEd Tanous                         asyncResp->res, "VLAN Network Interface", ifaceId);
2144927a505aSKowalski, Kamil                     return;
2145927a505aSKowalski, Kamil                 }
2146927a505aSKowalski, Kamil 
21470627a2c7SEd Tanous                 bool vlanEnable = false;
214838268fa8SAndrew Geissler                 uint32_t vlanId = 0;
21490627a2c7SEd Tanous 
215015ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANEnable",
2151bf648f77SEd Tanous                                               vlanEnable, "VLANId", vlanId))
21521abe55efSEd Tanous                 {
2153927a505aSKowalski, Kamil                     return;
2154927a505aSKowalski, Kamil                 }
2155927a505aSKowalski, Kamil 
2156bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2157bf648f77SEd Tanous                 // for JSON preparation
2158e48c0fc5SRavi Teja                 getEthernetIfaceData(
2159bf648f77SEd Tanous                     ifaceId,
2160bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId, &vlanEnable, &vlanId](
2161bf648f77SEd Tanous                         const bool& success,
2162bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2163cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2164cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
216582695a5bSJiaqing Zhao                         if (success && !ethData.vlanId.empty())
216608244d02SSunitha Harish                         {
216708244d02SSunitha Harish                             auto callback =
2168bf648f77SEd Tanous                                 [asyncResp](
2169bf648f77SEd Tanous                                     const boost::system::error_code ec) {
217008244d02SSunitha Harish                                     if (ec)
217108244d02SSunitha Harish                                     {
217208244d02SSunitha Harish                                         messages::internalError(asyncResp->res);
217308244d02SSunitha Harish                                     }
217408244d02SSunitha Harish                                 };
217508244d02SSunitha Harish 
2176e05aec50SEd Tanous                             if (vlanEnable)
217708244d02SSunitha Harish                             {
217808244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2179bf648f77SEd Tanous                                     std::move(callback),
2180bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
218108244d02SSunitha Harish                                     "/xyz/openbmc_project/network/" + ifaceId,
218208244d02SSunitha Harish                                     "org.freedesktop.DBus.Properties", "Set",
218308244d02SSunitha Harish                                     "xyz.openbmc_project.Network.VLAN", "Id",
2184168e20c1SEd Tanous                                     dbus::utility::DbusVariantType(vlanId));
218508244d02SSunitha Harish                             }
218608244d02SSunitha Harish                             else
218708244d02SSunitha Harish                             {
2188bf648f77SEd Tanous                                 BMCWEB_LOG_DEBUG
2189bf648f77SEd Tanous                                     << "vlanEnable is false. Deleting the "
2190e48c0fc5SRavi Teja                                        "vlan interface";
219108244d02SSunitha Harish                                 crow::connections::systemBus->async_method_call(
2192bf648f77SEd Tanous                                     std::move(callback),
2193bf648f77SEd Tanous                                     "xyz.openbmc_project.Network",
2194bf648f77SEd Tanous                                     std::string(
2195bf648f77SEd Tanous                                         "/xyz/openbmc_project/network/") +
2196e48c0fc5SRavi Teja                                         ifaceId,
2197bf648f77SEd Tanous                                     "xyz.openbmc_project.Object.Delete",
2198bf648f77SEd Tanous                                     "Delete");
219908244d02SSunitha Harish                             }
220008244d02SSunitha Harish                         }
220108244d02SSunitha Harish                         else
22021abe55efSEd Tanous                         {
2203bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2204bf648f77SEd Tanous                             // existing object, and other errors
2205bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2206bf648f77SEd Tanous                                                        "VLAN Network Interface",
2207bf648f77SEd Tanous                                                        ifaceId);
2208bf648f77SEd Tanous                             return;
2209bf648f77SEd Tanous                         }
2210bf648f77SEd Tanous                     });
2211bf648f77SEd Tanous             });
2212bf648f77SEd Tanous 
2213bf648f77SEd Tanous     BMCWEB_ROUTE(
2214bf648f77SEd Tanous         app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/")
22153d768a16SAbhishek Patel         .privileges(redfish::privileges::deleteVLanNetworkInterface)
2216bf648f77SEd Tanous         .methods(boost::beast::http::verb::delete_)(
221745ca1b86SEd Tanous             [&app](const crow::Request& req,
2218bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
221945ca1b86SEd Tanous                    const std::string& parentIfaceId,
222045ca1b86SEd Tanous                    const std::string& ifaceId) {
222145ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
222245ca1b86SEd Tanous                 {
222345ca1b86SEd Tanous                     return;
222445ca1b86SEd Tanous                 }
2225bf648f77SEd Tanous                 if (!verifyNames(parentIfaceId, ifaceId))
2226bf648f77SEd Tanous                 {
2227e48c0fc5SRavi Teja                     messages::resourceNotFound(
2228e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2229927a505aSKowalski, Kamil                     return;
2230927a505aSKowalski, Kamil                 }
2231e439f0f8SKowalski, Kamil 
2232bf648f77SEd Tanous                 // Get single eth interface data, and call the below callback
2233bf648f77SEd Tanous                 // for JSON preparation
2234f12894f8SJason M. Bills                 getEthernetIfaceData(
2235bf648f77SEd Tanous                     ifaceId,
2236bf648f77SEd Tanous                     [asyncResp, parentIfaceId, ifaceId](
2237bf648f77SEd Tanous                         const bool& success,
2238bf648f77SEd Tanous                         const EthernetInterfaceData& ethData,
2239cb13a392SEd Tanous                         const boost::container::flat_set<IPv4AddressData>&,
2240cb13a392SEd Tanous                         const boost::container::flat_set<IPv6AddressData>&) {
224182695a5bSJiaqing Zhao                         if (success && !ethData.vlanId.empty())
22421abe55efSEd Tanous                         {
2243f12894f8SJason M. Bills                             auto callback =
2244bf648f77SEd Tanous                                 [asyncResp](
2245bf648f77SEd Tanous                                     const boost::system::error_code ec) {
22461abe55efSEd Tanous                                     if (ec)
22471abe55efSEd Tanous                                     {
2248f12894f8SJason M. Bills                                         messages::internalError(asyncResp->res);
2249927a505aSKowalski, Kamil                                     }
22504a0cb85cSEd Tanous                                 };
22514a0cb85cSEd Tanous                             crow::connections::systemBus->async_method_call(
2252bf648f77SEd Tanous                                 std::move(callback),
2253bf648f77SEd Tanous                                 "xyz.openbmc_project.Network",
2254bf648f77SEd Tanous                                 std::string("/xyz/openbmc_project/network/") +
2255bf648f77SEd Tanous                                     ifaceId,
22564a0cb85cSEd Tanous                                 "xyz.openbmc_project.Object.Delete", "Delete");
22571abe55efSEd Tanous                         }
22581abe55efSEd Tanous                         else
22591abe55efSEd Tanous                         {
2260927a505aSKowalski, Kamil                             // ... otherwise return error
2261bf648f77SEd Tanous                             // TODO(Pawel)consider distinguish between non
2262bf648f77SEd Tanous                             // existing object, and other errors
2263bf648f77SEd Tanous                             messages::resourceNotFound(asyncResp->res,
2264bf648f77SEd Tanous                                                        "VLAN Network Interface",
2265bf648f77SEd Tanous                                                        ifaceId);
2266927a505aSKowalski, Kamil                         }
2267927a505aSKowalski, Kamil                     });
2268bf648f77SEd Tanous             });
2269e439f0f8SKowalski, Kamil 
2270bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2271bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
2272ed398213SEd Tanous 
2273ed398213SEd Tanous         .privileges(redfish::privileges::getVLanNetworkInterfaceCollection)
2274*1476687dSEd Tanous         .methods(boost::beast::http::verb::get)(
2275*1476687dSEd Tanous             [&app](const crow::Request& req,
2276bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2277bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
227845ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
227945ca1b86SEd Tanous                 {
228045ca1b86SEd Tanous                     return;
228145ca1b86SEd Tanous                 }
22824a0cb85cSEd Tanous                 // Get eth interface list, and call the below callback for JSON
22831abe55efSEd Tanous                 // preparation
2284bf648f77SEd Tanous                 getEthernetIfaceList([asyncResp, rootInterfaceName](
22851abe55efSEd Tanous                                          const bool& success,
2286bf648f77SEd Tanous                                          const boost::container::flat_set<
2287bf648f77SEd Tanous                                              std::string>& ifaceList) {
22884a0cb85cSEd Tanous                     if (!success)
22891abe55efSEd Tanous                     {
2290f12894f8SJason M. Bills                         messages::internalError(asyncResp->res);
22914a0cb85cSEd Tanous                         return;
22921abe55efSEd Tanous                     }
22934c9afe43SEd Tanous 
229481ce609eSEd Tanous                     if (ifaceList.find(rootInterfaceName) == ifaceList.end())
22954c9afe43SEd Tanous                     {
2296*1476687dSEd Tanous                         messages::resourceNotFound(
2297*1476687dSEd Tanous                             asyncResp->res, "VLanNetworkInterfaceCollection",
22984c9afe43SEd Tanous                             rootInterfaceName);
22994c9afe43SEd Tanous                         return;
23004c9afe43SEd Tanous                     }
23014c9afe43SEd Tanous 
23020f74e643SEd Tanous                     asyncResp->res.jsonValue["@odata.type"] =
23030f74e643SEd Tanous                         "#VLanNetworkInterfaceCollection."
23040f74e643SEd Tanous                         "VLanNetworkInterfaceCollection";
23050f74e643SEd Tanous                     asyncResp->res.jsonValue["Name"] =
23060f74e643SEd Tanous                         "VLAN Network Interface Collection";
23074a0cb85cSEd Tanous 
23082c70f800SEd Tanous                     nlohmann::json ifaceArray = nlohmann::json::array();
23094a0cb85cSEd Tanous 
231081ce609eSEd Tanous                     for (const std::string& ifaceItem : ifaceList)
23111abe55efSEd Tanous                     {
2312*1476687dSEd Tanous                         if (boost::starts_with(ifaceItem,
2313*1476687dSEd Tanous                                                rootInterfaceName + "_"))
23144a0cb85cSEd Tanous                         {
2315f23b7296SEd Tanous                             std::string path =
2316f23b7296SEd Tanous                                 "/redfish/v1/Managers/bmc/EthernetInterfaces/";
2317f23b7296SEd Tanous                             path += rootInterfaceName;
2318f23b7296SEd Tanous                             path += "/VLANs/";
2319f23b7296SEd Tanous                             path += ifaceItem;
2320*1476687dSEd Tanous                             nlohmann::json::object_t iface;
2321*1476687dSEd Tanous                             iface["@odata.id"] = std::move(path);
2322*1476687dSEd Tanous                             ifaceArray.push_back(std::move(iface));
2323e439f0f8SKowalski, Kamil                         }
2324e439f0f8SKowalski, Kamil                     }
2325e439f0f8SKowalski, Kamil 
23264a0cb85cSEd Tanous                     asyncResp->res.jsonValue["Members@odata.count"] =
23272c70f800SEd Tanous                         ifaceArray.size();
23282c70f800SEd Tanous                     asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
23294a0cb85cSEd Tanous                     asyncResp->res.jsonValue["@odata.id"] =
23304a0cb85cSEd Tanous                         "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23314a0cb85cSEd Tanous                         rootInterfaceName + "/VLANs";
2332e439f0f8SKowalski, Kamil                 });
2333bf648f77SEd Tanous             });
2334e439f0f8SKowalski, Kamil 
2335bf648f77SEd Tanous     BMCWEB_ROUTE(app,
2336bf648f77SEd Tanous                  "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/")
23373d768a16SAbhishek Patel         .privileges(redfish::privileges::postVLanNetworkInterfaceCollection)
2338bf648f77SEd Tanous         .methods(boost::beast::http::verb::post)(
233945ca1b86SEd Tanous             [&app](const crow::Request& req,
2340bf648f77SEd Tanous                    const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
2341bf648f77SEd Tanous                    const std::string& rootInterfaceName) {
234245ca1b86SEd Tanous                 if (!redfish::setUpRedfishRoute(app, req, asyncResp->res))
234345ca1b86SEd Tanous                 {
234445ca1b86SEd Tanous                     return;
234545ca1b86SEd Tanous                 }
2346fda13ad2SSunitha Harish                 bool vlanEnable = false;
23470627a2c7SEd Tanous                 uint32_t vlanId = 0;
234815ed6780SWilly Tu                 if (!json_util::readJsonPatch(req, asyncResp->res, "VLANId",
234915ed6780SWilly Tu                                               vlanId, "VLANEnable", vlanEnable))
23501abe55efSEd Tanous                 {
23514a0cb85cSEd Tanous                     return;
2352e439f0f8SKowalski, Kamil                 }
2353fda13ad2SSunitha Harish                 // Need both vlanId and vlanEnable to service this request
2354dbb59d4dSEd Tanous                 if (vlanId == 0U)
2355fda13ad2SSunitha Harish                 {
2356fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANId");
2357fda13ad2SSunitha Harish                 }
2358fda13ad2SSunitha Harish                 if (!vlanEnable)
2359fda13ad2SSunitha Harish                 {
2360fda13ad2SSunitha Harish                     messages::propertyMissing(asyncResp->res, "VLANEnable");
2361fda13ad2SSunitha Harish                 }
2362271584abSEd Tanous                 if (static_cast<bool>(vlanId) ^ vlanEnable)
2363fda13ad2SSunitha Harish                 {
2364fda13ad2SSunitha Harish                     return;
2365fda13ad2SSunitha Harish                 }
2366fda13ad2SSunitha Harish 
2367bf648f77SEd Tanous                 auto callback =
2368bf648f77SEd Tanous                     [asyncResp](const boost::system::error_code ec) {
23691abe55efSEd Tanous                         if (ec)
23701abe55efSEd Tanous                         {
2371bf648f77SEd Tanous                             // TODO(ed) make more consistent error messages
2372bf648f77SEd Tanous                             // based on phosphor-network responses
2373f12894f8SJason M. Bills                             messages::internalError(asyncResp->res);
23744a0cb85cSEd Tanous                             return;
23751abe55efSEd Tanous                         }
2376f12894f8SJason M. Bills                         messages::created(asyncResp->res);
2377e439f0f8SKowalski, Kamil                     };
23784a0cb85cSEd Tanous                 crow::connections::systemBus->async_method_call(
23794a0cb85cSEd Tanous                     std::move(callback), "xyz.openbmc_project.Network",
23804a0cb85cSEd Tanous                     "/xyz/openbmc_project/network",
23814a0cb85cSEd Tanous                     "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
23820627a2c7SEd Tanous                     rootInterfaceName, vlanId);
2383bf648f77SEd Tanous             });
23844a0cb85cSEd Tanous }
2385bf648f77SEd Tanous 
23869391bb9cSRapkiewicz, Pawel } // namespace redfish
2387