xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision ab6554f1d8197753cfb430a72798dde6f4461552)
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 
181abe55efSEd Tanous #include <boost/container/flat_map.hpp>
194a0cb85cSEd Tanous #include <boost/container/flat_set.hpp>
20179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
21588c3f0dSKowalski, Kamil #include <error_messages.hpp>
22179db1d7SKowalski, Kamil #include <node.hpp>
23a24526dcSEd Tanous #include <optional>
24*ab6554f1SJoshi-Mansi #include <regex>
25588c3f0dSKowalski, Kamil #include <utils/json_utils.hpp>
26abf2add6SEd Tanous #include <variant>
279391bb9cSRapkiewicz, Pawel 
281abe55efSEd Tanous namespace redfish
291abe55efSEd Tanous {
309391bb9cSRapkiewicz, Pawel 
319391bb9cSRapkiewicz, Pawel /**
329391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
339391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
349391bb9cSRapkiewicz, Pawel  */
35aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
36abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
37aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
389391bb9cSRapkiewicz, Pawel 
394a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
40aa2e59c1SEd Tanous     sdbusplus::message::object_path,
414a0cb85cSEd Tanous     std::vector<std::pair<
42aa2e59c1SEd Tanous         std::string,
43aa2e59c1SEd Tanous         boost::container::flat_map<
44029573d4SEd Tanous             std::string, sdbusplus::message::variant<
45029573d4SEd Tanous                              std::string, bool, uint8_t, int16_t, uint16_t,
46029573d4SEd Tanous                              int32_t, uint32_t, int64_t, uint64_t, double,
47029573d4SEd Tanous                              std::vector<std::string>>>>>>>;
484a0cb85cSEd Tanous 
494a0cb85cSEd Tanous enum class LinkType
504a0cb85cSEd Tanous {
514a0cb85cSEd Tanous     Local,
524a0cb85cSEd Tanous     Global
534a0cb85cSEd Tanous };
549391bb9cSRapkiewicz, Pawel 
559391bb9cSRapkiewicz, Pawel /**
569391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
579391bb9cSRapkiewicz, Pawel  */
581abe55efSEd Tanous struct IPv4AddressData
591abe55efSEd Tanous {
60179db1d7SKowalski, Kamil     std::string id;
614a0cb85cSEd Tanous     std::string address;
624a0cb85cSEd Tanous     std::string domain;
634a0cb85cSEd Tanous     std::string gateway;
649391bb9cSRapkiewicz, Pawel     std::string netmask;
659391bb9cSRapkiewicz, Pawel     std::string origin;
664a0cb85cSEd Tanous     LinkType linktype;
674a0cb85cSEd Tanous 
681abe55efSEd Tanous     bool operator<(const IPv4AddressData &obj) const
691abe55efSEd Tanous     {
704a0cb85cSEd Tanous         return id < obj.id;
711abe55efSEd Tanous     }
729391bb9cSRapkiewicz, Pawel };
739391bb9cSRapkiewicz, Pawel 
749391bb9cSRapkiewicz, Pawel /**
75e48c0fc5SRavi Teja  * Structure for keeping IPv6 data required by Redfish
76e48c0fc5SRavi Teja  */
77e48c0fc5SRavi Teja struct IPv6AddressData
78e48c0fc5SRavi Teja {
79e48c0fc5SRavi Teja     std::string id;
80e48c0fc5SRavi Teja     std::string address;
81e48c0fc5SRavi Teja     std::string origin;
82e48c0fc5SRavi Teja     uint8_t prefixLength;
83e48c0fc5SRavi Teja 
84e48c0fc5SRavi Teja     bool operator<(const IPv6AddressData &obj) const
85e48c0fc5SRavi Teja     {
86e48c0fc5SRavi Teja         return id < obj.id;
87e48c0fc5SRavi Teja     }
88e48c0fc5SRavi Teja };
89e48c0fc5SRavi Teja /**
909391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
919391bb9cSRapkiewicz, Pawel  * available from DBus
929391bb9cSRapkiewicz, Pawel  */
931abe55efSEd Tanous struct EthernetInterfaceData
941abe55efSEd Tanous {
954a0cb85cSEd Tanous     uint32_t speed;
964a0cb85cSEd Tanous     bool auto_neg;
971f8c7b5dSJohnathan Mantey     bool DNSEnabled;
981f8c7b5dSJohnathan Mantey     bool NTPEnabled;
991f8c7b5dSJohnathan Mantey     bool HostNameEnabled;
1001f8c7b5dSJohnathan Mantey     bool SendHostNameEnabled;
101aa05fb27SJohnathan Mantey     bool linkUp;
102eeedda23SJohnathan Mantey     bool nicEnabled;
1031f8c7b5dSJohnathan Mantey     std::string DHCPEnabled;
1041f8c7b5dSJohnathan Mantey     std::string operatingMode;
1054a0cb85cSEd Tanous     std::string hostname;
1064a0cb85cSEd Tanous     std::string default_gateway;
1079a6fc6feSRavi Teja     std::string ipv6_default_gateway;
1084a0cb85cSEd Tanous     std::string mac_address;
109fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
110029573d4SEd Tanous     std::vector<std::string> nameservers;
111d24bfc7aSJennifer Lee     std::vector<std::string> domainnames;
1129391bb9cSRapkiewicz, Pawel };
1139391bb9cSRapkiewicz, Pawel 
1141f8c7b5dSJohnathan Mantey struct DHCPParameters
1151f8c7b5dSJohnathan Mantey {
1161f8c7b5dSJohnathan Mantey     std::optional<bool> dhcpv4Enabled;
1171f8c7b5dSJohnathan Mantey     std::optional<bool> useDNSServers;
1181f8c7b5dSJohnathan Mantey     std::optional<bool> useNTPServers;
1191f8c7b5dSJohnathan Mantey     std::optional<bool> useUseDomainName;
1201f8c7b5dSJohnathan Mantey     std::optional<std::string> dhcpv6OperatingMode;
1211f8c7b5dSJohnathan Mantey };
1221f8c7b5dSJohnathan Mantey 
1239391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
1249391bb9cSRapkiewicz, Pawel // into full dot notation
1251abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
1261abe55efSEd Tanous {
1279391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1289391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1299391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1309391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1319391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1329391bb9cSRapkiewicz, Pawel     return netmask;
1339391bb9cSRapkiewicz, Pawel }
1349391bb9cSRapkiewicz, Pawel 
1351f8c7b5dSJohnathan Mantey inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
1361f8c7b5dSJohnathan Mantey                                        bool isIPv4)
1371f8c7b5dSJohnathan Mantey {
1381f8c7b5dSJohnathan Mantey     if (isIPv4)
1391f8c7b5dSJohnathan Mantey     {
1401f8c7b5dSJohnathan Mantey         return (
1411f8c7b5dSJohnathan Mantey             (inputDHCP ==
1421f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
1431f8c7b5dSJohnathan Mantey             (inputDHCP ==
1441f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1451f8c7b5dSJohnathan Mantey     }
1461f8c7b5dSJohnathan Mantey     return ((inputDHCP ==
1471f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
1481f8c7b5dSJohnathan Mantey             (inputDHCP ==
1491f8c7b5dSJohnathan Mantey              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
1501f8c7b5dSJohnathan Mantey }
1511f8c7b5dSJohnathan Mantey 
1521f8c7b5dSJohnathan Mantey inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
1531f8c7b5dSJohnathan Mantey {
1541f8c7b5dSJohnathan Mantey     if (isIPv4 && isIPv6)
1551f8c7b5dSJohnathan Mantey     {
1561f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
1571f8c7b5dSJohnathan Mantey     }
1581f8c7b5dSJohnathan Mantey     else if (isIPv4)
1591f8c7b5dSJohnathan Mantey     {
1601f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
1611f8c7b5dSJohnathan Mantey     }
1621f8c7b5dSJohnathan Mantey     else if (isIPv6)
1631f8c7b5dSJohnathan Mantey     {
1641f8c7b5dSJohnathan Mantey         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
1651f8c7b5dSJohnathan Mantey     }
1661f8c7b5dSJohnathan Mantey     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
1671f8c7b5dSJohnathan Mantey }
1681f8c7b5dSJohnathan Mantey 
1694a0cb85cSEd Tanous inline std::string
1704a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
1714a0cb85cSEd Tanous                                         bool isIPv4)
1721abe55efSEd Tanous {
1734a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1741abe55efSEd Tanous     {
1754a0cb85cSEd Tanous         return "Static";
1769391bb9cSRapkiewicz, Pawel     }
1774a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1781abe55efSEd Tanous     {
1794a0cb85cSEd Tanous         if (isIPv4)
1801abe55efSEd Tanous         {
1814a0cb85cSEd Tanous             return "IPv4LinkLocal";
1821abe55efSEd Tanous         }
1831abe55efSEd Tanous         else
1841abe55efSEd Tanous         {
1854a0cb85cSEd Tanous             return "LinkLocal";
1869391bb9cSRapkiewicz, Pawel         }
1879391bb9cSRapkiewicz, Pawel     }
1884a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1891abe55efSEd Tanous     {
1904a0cb85cSEd Tanous         if (isIPv4)
1914a0cb85cSEd Tanous         {
1924a0cb85cSEd Tanous             return "DHCP";
1934a0cb85cSEd Tanous         }
1944a0cb85cSEd Tanous         else
1954a0cb85cSEd Tanous         {
1964a0cb85cSEd Tanous             return "DHCPv6";
1974a0cb85cSEd Tanous         }
1984a0cb85cSEd Tanous     }
1994a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
2004a0cb85cSEd Tanous     {
2014a0cb85cSEd Tanous         return "SLAAC";
2024a0cb85cSEd Tanous     }
2034a0cb85cSEd Tanous     return "";
2044a0cb85cSEd Tanous }
2054a0cb85cSEd Tanous 
2064c9afe43SEd Tanous inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
2074a0cb85cSEd Tanous                                          const GetManagedObjects &dbus_data,
2084a0cb85cSEd Tanous                                          EthernetInterfaceData &ethData)
2094a0cb85cSEd Tanous {
2104c9afe43SEd Tanous     bool idFound = false;
2114a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
2124a0cb85cSEd Tanous     {
2134a0cb85cSEd Tanous         for (const auto &ifacePair : objpath.second)
2144a0cb85cSEd Tanous         {
215029573d4SEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
216029573d4SEd Tanous             {
2174c9afe43SEd Tanous                 idFound = true;
2184a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
2194a0cb85cSEd Tanous                 {
2204a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2214a0cb85cSEd Tanous                     {
2224a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
2234a0cb85cSEd Tanous                         {
2244a0cb85cSEd Tanous                             const std::string *mac =
225abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
2264a0cb85cSEd Tanous                             if (mac != nullptr)
2274a0cb85cSEd Tanous                             {
2284a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
2294a0cb85cSEd Tanous                             }
2304a0cb85cSEd Tanous                         }
2314a0cb85cSEd Tanous                     }
2324a0cb85cSEd Tanous                 }
2334a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
2344a0cb85cSEd Tanous                 {
2354a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2364a0cb85cSEd Tanous                     {
2374a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
2384a0cb85cSEd Tanous                         {
2391b6b96c5SEd Tanous                             const uint32_t *id =
240abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2414a0cb85cSEd Tanous                             if (id != nullptr)
2424a0cb85cSEd Tanous                             {
243fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
2444a0cb85cSEd Tanous                             }
2454a0cb85cSEd Tanous                         }
2464a0cb85cSEd Tanous                     }
2474a0cb85cSEd Tanous                 }
2484a0cb85cSEd Tanous                 else if (ifacePair.first ==
2494a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2504a0cb85cSEd Tanous                 {
2514a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2524a0cb85cSEd Tanous                     {
2534a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2544a0cb85cSEd Tanous                         {
2554a0cb85cSEd Tanous                             const bool *auto_neg =
256abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2574a0cb85cSEd Tanous                             if (auto_neg != nullptr)
2584a0cb85cSEd Tanous                             {
2594a0cb85cSEd Tanous                                 ethData.auto_neg = *auto_neg;
2604a0cb85cSEd Tanous                             }
2614a0cb85cSEd Tanous                         }
2624a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2634a0cb85cSEd Tanous                         {
2644a0cb85cSEd Tanous                             const uint32_t *speed =
265abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2664a0cb85cSEd Tanous                             if (speed != nullptr)
2674a0cb85cSEd Tanous                             {
2684a0cb85cSEd Tanous                                 ethData.speed = *speed;
2694a0cb85cSEd Tanous                             }
2704a0cb85cSEd Tanous                         }
271aa05fb27SJohnathan Mantey                         else if (propertyPair.first == "LinkUp")
272aa05fb27SJohnathan Mantey                         {
273aa05fb27SJohnathan Mantey                             const bool *linkUp =
274aa05fb27SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
275aa05fb27SJohnathan Mantey                             if (linkUp != nullptr)
276aa05fb27SJohnathan Mantey                             {
277aa05fb27SJohnathan Mantey                                 ethData.linkUp = *linkUp;
278aa05fb27SJohnathan Mantey                             }
279aa05fb27SJohnathan Mantey                         }
280eeedda23SJohnathan Mantey                         else if (propertyPair.first == "NICEnabled")
281eeedda23SJohnathan Mantey                         {
282eeedda23SJohnathan Mantey                             const bool *nicEnabled =
283eeedda23SJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
284eeedda23SJohnathan Mantey                             if (nicEnabled != nullptr)
285eeedda23SJohnathan Mantey                             {
286eeedda23SJohnathan Mantey                                 ethData.nicEnabled = *nicEnabled;
287eeedda23SJohnathan Mantey                             }
288eeedda23SJohnathan Mantey                         }
289f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
290029573d4SEd Tanous                         {
291029573d4SEd Tanous                             const std::vector<std::string> *nameservers =
292029573d4SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
293029573d4SEd Tanous                                     std::vector<std::string>>(
294029573d4SEd Tanous                                     &propertyPair.second);
295029573d4SEd Tanous                             if (nameservers != nullptr)
296029573d4SEd Tanous                             {
297029573d4SEd Tanous                                 ethData.nameservers = std::move(*nameservers);
2984a0cb85cSEd Tanous                             }
2994a0cb85cSEd Tanous                         }
3002a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
3012a133282Smanojkiraneda                         {
3021f8c7b5dSJohnathan Mantey                             const std::string *DHCPEnabled =
3031f8c7b5dSJohnathan Mantey                                 std::get_if<std::string>(&propertyPair.second);
3042a133282Smanojkiraneda                             if (DHCPEnabled != nullptr)
3052a133282Smanojkiraneda                             {
3062a133282Smanojkiraneda                                 ethData.DHCPEnabled = *DHCPEnabled;
3072a133282Smanojkiraneda                             }
3082a133282Smanojkiraneda                         }
309d24bfc7aSJennifer Lee                         else if (propertyPair.first == "DomainName")
310d24bfc7aSJennifer Lee                         {
311d24bfc7aSJennifer Lee                             const std::vector<std::string> *domainNames =
312d24bfc7aSJennifer Lee                                 sdbusplus::message::variant_ns::get_if<
313d24bfc7aSJennifer Lee                                     std::vector<std::string>>(
314d24bfc7aSJennifer Lee                                     &propertyPair.second);
315d24bfc7aSJennifer Lee                             if (domainNames != nullptr)
316d24bfc7aSJennifer Lee                             {
317d24bfc7aSJennifer Lee                                 ethData.domainnames = std::move(*domainNames);
318d24bfc7aSJennifer Lee                             }
319d24bfc7aSJennifer Lee                         }
320029573d4SEd Tanous                     }
321029573d4SEd Tanous                 }
322029573d4SEd Tanous             }
3231f8c7b5dSJohnathan Mantey 
3241f8c7b5dSJohnathan Mantey             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
3251f8c7b5dSJohnathan Mantey             {
3261f8c7b5dSJohnathan Mantey                 if (ifacePair.first ==
3271f8c7b5dSJohnathan Mantey                     "xyz.openbmc_project.Network.DHCPConfiguration")
3281f8c7b5dSJohnathan Mantey                 {
3291f8c7b5dSJohnathan Mantey                     for (const auto &propertyPair : ifacePair.second)
3301f8c7b5dSJohnathan Mantey                     {
3311f8c7b5dSJohnathan Mantey                         if (propertyPair.first == "DNSEnabled")
3321f8c7b5dSJohnathan Mantey                         {
3331f8c7b5dSJohnathan Mantey                             const bool *DNSEnabled =
3341f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3351f8c7b5dSJohnathan Mantey                             if (DNSEnabled != nullptr)
3361f8c7b5dSJohnathan Mantey                             {
3371f8c7b5dSJohnathan Mantey                                 ethData.DNSEnabled = *DNSEnabled;
3381f8c7b5dSJohnathan Mantey                             }
3391f8c7b5dSJohnathan Mantey                         }
3401f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "NTPEnabled")
3411f8c7b5dSJohnathan Mantey                         {
3421f8c7b5dSJohnathan Mantey                             const bool *NTPEnabled =
3431f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3441f8c7b5dSJohnathan Mantey                             if (NTPEnabled != nullptr)
3451f8c7b5dSJohnathan Mantey                             {
3461f8c7b5dSJohnathan Mantey                                 ethData.NTPEnabled = *NTPEnabled;
3471f8c7b5dSJohnathan Mantey                             }
3481f8c7b5dSJohnathan Mantey                         }
3491f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "HostNameEnabled")
3501f8c7b5dSJohnathan Mantey                         {
3511f8c7b5dSJohnathan Mantey                             const bool *HostNameEnabled =
3521f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3531f8c7b5dSJohnathan Mantey                             if (HostNameEnabled != nullptr)
3541f8c7b5dSJohnathan Mantey                             {
3551f8c7b5dSJohnathan Mantey                                 ethData.HostNameEnabled = *HostNameEnabled;
3561f8c7b5dSJohnathan Mantey                             }
3571f8c7b5dSJohnathan Mantey                         }
3581f8c7b5dSJohnathan Mantey                         else if (propertyPair.first == "SendHostNameEnabled")
3591f8c7b5dSJohnathan Mantey                         {
3601f8c7b5dSJohnathan Mantey                             const bool *SendHostNameEnabled =
3611f8c7b5dSJohnathan Mantey                                 std::get_if<bool>(&propertyPair.second);
3621f8c7b5dSJohnathan Mantey                             if (SendHostNameEnabled != nullptr)
3631f8c7b5dSJohnathan Mantey                             {
3641f8c7b5dSJohnathan Mantey                                 ethData.SendHostNameEnabled =
3651f8c7b5dSJohnathan Mantey                                     *SendHostNameEnabled;
3661f8c7b5dSJohnathan Mantey                             }
3671f8c7b5dSJohnathan Mantey                         }
3681f8c7b5dSJohnathan Mantey                     }
3691f8c7b5dSJohnathan Mantey                 }
3701f8c7b5dSJohnathan Mantey             }
371029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
372029573d4SEd Tanous             // to check eth number
373029573d4SEd Tanous             if (ifacePair.first ==
3744a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
3754a0cb85cSEd Tanous             {
3764a0cb85cSEd Tanous                 for (const auto &propertyPair : ifacePair.second)
3774a0cb85cSEd Tanous                 {
3784a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
3794a0cb85cSEd Tanous                     {
3804a0cb85cSEd Tanous                         const std::string *hostname =
381029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
382029573d4SEd Tanous                                 &propertyPair.second);
3834a0cb85cSEd Tanous                         if (hostname != nullptr)
3844a0cb85cSEd Tanous                         {
3854a0cb85cSEd Tanous                             ethData.hostname = *hostname;
3864a0cb85cSEd Tanous                         }
3874a0cb85cSEd Tanous                     }
3884a0cb85cSEd Tanous                     else if (propertyPair.first == "DefaultGateway")
3894a0cb85cSEd Tanous                     {
3904a0cb85cSEd Tanous                         const std::string *defaultGateway =
391029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
392029573d4SEd Tanous                                 &propertyPair.second);
3934a0cb85cSEd Tanous                         if (defaultGateway != nullptr)
3944a0cb85cSEd Tanous                         {
3954a0cb85cSEd Tanous                             ethData.default_gateway = *defaultGateway;
3964a0cb85cSEd Tanous                         }
3974a0cb85cSEd Tanous                     }
3989a6fc6feSRavi Teja                     else if (propertyPair.first == "DefaultGateway6")
3999a6fc6feSRavi Teja                     {
4009a6fc6feSRavi Teja                         const std::string *defaultGateway6 =
4019a6fc6feSRavi Teja                             sdbusplus::message::variant_ns::get_if<std::string>(
4029a6fc6feSRavi Teja                                 &propertyPair.second);
4039a6fc6feSRavi Teja                         if (defaultGateway6 != nullptr)
4049a6fc6feSRavi Teja                         {
4059a6fc6feSRavi Teja                             ethData.ipv6_default_gateway = *defaultGateway6;
4069a6fc6feSRavi Teja                         }
4079a6fc6feSRavi Teja                     }
4084a0cb85cSEd Tanous                 }
4094a0cb85cSEd Tanous             }
4104a0cb85cSEd Tanous         }
4114a0cb85cSEd Tanous     }
4124c9afe43SEd Tanous     return idFound;
4134a0cb85cSEd Tanous }
4144a0cb85cSEd Tanous 
415e48c0fc5SRavi Teja // Helper function that extracts data for single ethernet ipv6 address
41601784826SJohnathan Mantey inline void
41701784826SJohnathan Mantey     extractIPV6Data(const std::string &ethiface_id,
41801784826SJohnathan Mantey                     const GetManagedObjects &dbus_data,
41901784826SJohnathan Mantey                     boost::container::flat_set<IPv6AddressData> &ipv6_config)
420e48c0fc5SRavi Teja {
421e48c0fc5SRavi Teja     const std::string ipv6PathStart =
422e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
423e48c0fc5SRavi Teja 
424e48c0fc5SRavi Teja     // Since there might be several IPv6 configurations aligned with
425e48c0fc5SRavi Teja     // single ethernet interface, loop over all of them
426e48c0fc5SRavi Teja     for (const auto &objpath : dbus_data)
427e48c0fc5SRavi Teja     {
428e48c0fc5SRavi Teja         // Check if proper pattern for object path appears
429e48c0fc5SRavi Teja         if (boost::starts_with(objpath.first.str, ipv6PathStart))
430e48c0fc5SRavi Teja         {
431e48c0fc5SRavi Teja             for (auto &interface : objpath.second)
432e48c0fc5SRavi Teja             {
433e48c0fc5SRavi Teja                 if (interface.first == "xyz.openbmc_project.Network.IP")
434e48c0fc5SRavi Teja                 {
435e48c0fc5SRavi Teja                     // Instance IPv6AddressData structure, and set as
436e48c0fc5SRavi Teja                     // appropriate
437e48c0fc5SRavi Teja                     std::pair<
438e48c0fc5SRavi Teja                         boost::container::flat_set<IPv6AddressData>::iterator,
439e48c0fc5SRavi Teja                         bool>
440271584abSEd Tanous                         it = ipv6_config.insert(IPv6AddressData{});
441e48c0fc5SRavi Teja                     IPv6AddressData &ipv6_address = *it.first;
442271584abSEd Tanous                     ipv6_address.id =
443271584abSEd Tanous                         objpath.first.str.substr(ipv6PathStart.size());
444e48c0fc5SRavi Teja                     for (auto &property : interface.second)
445e48c0fc5SRavi Teja                     {
446e48c0fc5SRavi Teja                         if (property.first == "Address")
447e48c0fc5SRavi Teja                         {
448e48c0fc5SRavi Teja                             const std::string *address =
449e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
450e48c0fc5SRavi Teja                             if (address != nullptr)
451e48c0fc5SRavi Teja                             {
452e48c0fc5SRavi Teja                                 ipv6_address.address = *address;
453e48c0fc5SRavi Teja                             }
454e48c0fc5SRavi Teja                         }
455e48c0fc5SRavi Teja                         else if (property.first == "Origin")
456e48c0fc5SRavi Teja                         {
457e48c0fc5SRavi Teja                             const std::string *origin =
458e48c0fc5SRavi Teja                                 std::get_if<std::string>(&property.second);
459e48c0fc5SRavi Teja                             if (origin != nullptr)
460e48c0fc5SRavi Teja                             {
461e48c0fc5SRavi Teja                                 ipv6_address.origin =
462e48c0fc5SRavi Teja                                     translateAddressOriginDbusToRedfish(*origin,
463e48c0fc5SRavi Teja                                                                         false);
464e48c0fc5SRavi Teja                             }
465e48c0fc5SRavi Teja                         }
466e48c0fc5SRavi Teja                         else if (property.first == "PrefixLength")
467e48c0fc5SRavi Teja                         {
468e48c0fc5SRavi Teja                             const uint8_t *prefix =
469e48c0fc5SRavi Teja                                 std::get_if<uint8_t>(&property.second);
470e48c0fc5SRavi Teja                             if (prefix != nullptr)
471e48c0fc5SRavi Teja                             {
472e48c0fc5SRavi Teja                                 ipv6_address.prefixLength = *prefix;
473e48c0fc5SRavi Teja                             }
474e48c0fc5SRavi Teja                         }
475e48c0fc5SRavi Teja                         else
476e48c0fc5SRavi Teja                         {
477e48c0fc5SRavi Teja                             BMCWEB_LOG_ERROR
478e48c0fc5SRavi Teja                                 << "Got extra property: " << property.first
479e48c0fc5SRavi Teja                                 << " on the " << objpath.first.str << " object";
480e48c0fc5SRavi Teja                         }
481e48c0fc5SRavi Teja                     }
482e48c0fc5SRavi Teja                 }
483e48c0fc5SRavi Teja             }
484e48c0fc5SRavi Teja         }
485e48c0fc5SRavi Teja     }
486e48c0fc5SRavi Teja }
487e48c0fc5SRavi Teja 
4884a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
48901784826SJohnathan Mantey inline void
49001784826SJohnathan Mantey     extractIPData(const std::string &ethiface_id,
49101784826SJohnathan Mantey                   const GetManagedObjects &dbus_data,
49201784826SJohnathan Mantey                   boost::container::flat_set<IPv4AddressData> &ipv4_config)
4934a0cb85cSEd Tanous {
4944a0cb85cSEd Tanous     const std::string ipv4PathStart =
4954a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
4964a0cb85cSEd Tanous 
4974a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
4984a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
4994a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
5004a0cb85cSEd Tanous     {
5014a0cb85cSEd Tanous         // Check if proper pattern for object path appears
5024a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
5034a0cb85cSEd Tanous         {
5044a0cb85cSEd Tanous             for (auto &interface : objpath.second)
5054a0cb85cSEd Tanous             {
5064a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
5074a0cb85cSEd Tanous                 {
5084a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
5094a0cb85cSEd Tanous                     // appropriate
5104a0cb85cSEd Tanous                     std::pair<
5114a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
5124a0cb85cSEd Tanous                         bool>
513271584abSEd Tanous                         it = ipv4_config.insert(IPv4AddressData{});
5144a0cb85cSEd Tanous                     IPv4AddressData &ipv4_address = *it.first;
515271584abSEd Tanous                     ipv4_address.id =
516271584abSEd Tanous                         objpath.first.str.substr(ipv4PathStart.size());
5174a0cb85cSEd Tanous                     for (auto &property : interface.second)
5184a0cb85cSEd Tanous                     {
5194a0cb85cSEd Tanous                         if (property.first == "Address")
5204a0cb85cSEd Tanous                         {
5214a0cb85cSEd Tanous                             const std::string *address =
522abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5234a0cb85cSEd Tanous                             if (address != nullptr)
5244a0cb85cSEd Tanous                             {
5254a0cb85cSEd Tanous                                 ipv4_address.address = *address;
5264a0cb85cSEd Tanous                             }
5274a0cb85cSEd Tanous                         }
5284a0cb85cSEd Tanous                         else if (property.first == "Gateway")
5294a0cb85cSEd Tanous                         {
5304a0cb85cSEd Tanous                             const std::string *gateway =
531abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
5324a0cb85cSEd Tanous                             if (gateway != nullptr)
5334a0cb85cSEd Tanous                             {
5344a0cb85cSEd Tanous                                 ipv4_address.gateway = *gateway;
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                             {
5434a0cb85cSEd Tanous                                 ipv4_address.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
5554a0cb85cSEd Tanous                                 ipv4_address.netmask = getNetmask(*mask);
5564a0cb85cSEd Tanous                             }
5574a0cb85cSEd Tanous                         }
5584a0cb85cSEd Tanous                         else
5594a0cb85cSEd Tanous                         {
5604a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
5614a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
5624a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
5634a0cb85cSEd Tanous                         }
5644a0cb85cSEd Tanous                     }
5654a0cb85cSEd Tanous                     // Check if given address is local, or global
5664a0cb85cSEd Tanous                     ipv4_address.linktype =
5674a0cb85cSEd Tanous                         boost::starts_with(ipv4_address.address, "169.254.")
56818659d10SJohnathan Mantey                             ? LinkType::Local
56918659d10SJohnathan Mantey                             : LinkType::Global;
5704a0cb85cSEd Tanous                 }
5714a0cb85cSEd Tanous             }
5724a0cb85cSEd Tanous         }
5734a0cb85cSEd Tanous     }
5744a0cb85cSEd Tanous }
575588c3f0dSKowalski, Kamil 
576588c3f0dSKowalski, Kamil /**
577588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
578588c3f0dSKowalski, Kamil  *
579588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
580588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
581588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
582588c3f0dSKowalski, Kamil  *
583588c3f0dSKowalski, Kamil  * @return None.
584588c3f0dSKowalski, Kamil  */
585588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5864a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
5871abe55efSEd Tanous                   CallbackFunc &&callback)
5881abe55efSEd Tanous {
58955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
590588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
591588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
592588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
593588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
594abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
5954a0cb85cSEd Tanous }
596588c3f0dSKowalski, Kamil 
597588c3f0dSKowalski, Kamil /**
598179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
599179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
600179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
601179db1d7SKowalski, Kamil  *
602179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
603179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
604179db1d7SKowalski, Kamil  *
605179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
606179db1d7SKowalski, Kamil  */
6074a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
6081abe55efSEd Tanous                                        uint8_t *bits = nullptr)
6091abe55efSEd Tanous {
610179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
611179db1d7SKowalski, Kamil 
612179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
613179db1d7SKowalski, Kamil 
6144a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
6151abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
6161abe55efSEd Tanous     {
617179db1d7SKowalski, Kamil         return false;
618179db1d7SKowalski, Kamil     }
619179db1d7SKowalski, Kamil 
6201abe55efSEd Tanous     if (bits != nullptr)
6211abe55efSEd Tanous     {
622179db1d7SKowalski, Kamil         *bits = 0;
623179db1d7SKowalski, Kamil     }
624179db1d7SKowalski, Kamil 
625179db1d7SKowalski, Kamil     char *endPtr;
626179db1d7SKowalski, Kamil     long previousValue = 255;
627179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
6281abe55efSEd Tanous     for (const std::string &byte : bytesInMask)
6291abe55efSEd Tanous     {
6301abe55efSEd Tanous         if (byte.empty())
6311abe55efSEd Tanous         {
6321db9ca37SKowalski, Kamil             return false;
6331db9ca37SKowalski, Kamil         }
6341db9ca37SKowalski, Kamil 
635179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
6361db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
637179db1d7SKowalski, Kamil 
6384a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
6394a0cb85cSEd Tanous         // is not 100% number
6401abe55efSEd Tanous         if (*endPtr != '\0')
6411abe55efSEd Tanous         {
642179db1d7SKowalski, Kamil             return false;
643179db1d7SKowalski, Kamil         }
644179db1d7SKowalski, Kamil 
645179db1d7SKowalski, Kamil         // Value should be contained in byte
6461abe55efSEd Tanous         if (value < 0 || value > 255)
6471abe55efSEd Tanous         {
648179db1d7SKowalski, Kamil             return false;
649179db1d7SKowalski, Kamil         }
650179db1d7SKowalski, Kamil 
6511abe55efSEd Tanous         if (bits != nullptr)
6521abe55efSEd Tanous         {
653179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
6541abe55efSEd Tanous             if (previousValue != 255 && value != 0)
6551abe55efSEd Tanous             {
656179db1d7SKowalski, Kamil                 return false;
657179db1d7SKowalski, Kamil             }
658179db1d7SKowalski, Kamil 
659179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
660179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
661179db1d7SKowalski, Kamil 
662179db1d7SKowalski, Kamil             // Count bits
6631abe55efSEd Tanous             for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
6641abe55efSEd Tanous             {
6651abe55efSEd Tanous                 if (value & (1 << bitIdx))
6661abe55efSEd Tanous                 {
6671abe55efSEd Tanous                     if (firstZeroInByteHit)
6681abe55efSEd Tanous                     {
669179db1d7SKowalski, Kamil                         // Continuity not preserved
670179db1d7SKowalski, Kamil                         return false;
6711abe55efSEd Tanous                     }
6721abe55efSEd Tanous                     else
6731abe55efSEd Tanous                     {
674179db1d7SKowalski, Kamil                         (*bits)++;
675179db1d7SKowalski, Kamil                     }
6761abe55efSEd Tanous                 }
6771abe55efSEd Tanous                 else
6781abe55efSEd Tanous                 {
679179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
680179db1d7SKowalski, Kamil                 }
681179db1d7SKowalski, Kamil             }
682179db1d7SKowalski, Kamil         }
683179db1d7SKowalski, Kamil 
684179db1d7SKowalski, Kamil         previousValue = value;
685179db1d7SKowalski, Kamil     }
686179db1d7SKowalski, Kamil 
687179db1d7SKowalski, Kamil     return true;
688179db1d7SKowalski, Kamil }
689179db1d7SKowalski, Kamil 
690179db1d7SKowalski, Kamil /**
69101784826SJohnathan Mantey  * @brief Deletes given IPv4 interface
692179db1d7SKowalski, Kamil  *
693179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
694179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
695179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
696179db1d7SKowalski, Kamil  *
697179db1d7SKowalski, Kamil  * @return None
698179db1d7SKowalski, Kamil  */
6994a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
7004a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
7011abe55efSEd Tanous {
70255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
703286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7041abe55efSEd Tanous             if (ec)
7051abe55efSEd Tanous             {
706a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
7071abe55efSEd Tanous             }
708179db1d7SKowalski, Kamil         },
709179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
710179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
711179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
712179db1d7SKowalski, Kamil }
713179db1d7SKowalski, Kamil 
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  */
725b01bf299SEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
72601784826SJohnathan Mantey                        uint8_t prefixLength, const std::string &gateway,
727b01bf299SEd Tanous                        const std::string &address,
7284a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
7291abe55efSEd Tanous {
73001784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
73101784826SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
7321abe55efSEd Tanous             if (ec)
7331abe55efSEd Tanous             {
734a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
735179db1d7SKowalski, Kamil             }
73601784826SJohnathan Mantey         },
73701784826SJohnathan Mantey         "xyz.openbmc_project.Network",
738179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
739179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
74001784826SJohnathan Mantey         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
741179db1d7SKowalski, Kamil         gateway);
742179db1d7SKowalski, Kamil }
743e48c0fc5SRavi Teja 
744e48c0fc5SRavi Teja /**
74501784826SJohnathan Mantey  * @brief Deletes the IPv4 entry for this interface and creates a replacement
74601784826SJohnathan Mantey  * static IPv4 entry
74701784826SJohnathan Mantey  *
74801784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
74901784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
75001784826SJohnathan Mantey  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
75101784826SJohnathan Mantey  * @param[in] gateway      IPv4 address of this interfaces gateway
75201784826SJohnathan Mantey  * @param[in] address      IPv4 address to assign to this interface
75301784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
75401784826SJohnathan Mantey  *
75501784826SJohnathan Mantey  * @return None
75601784826SJohnathan Mantey  */
75701784826SJohnathan Mantey inline void deleteAndCreateIPv4(const std::string &ifaceId,
75801784826SJohnathan Mantey                                 const std::string &id, uint8_t prefixLength,
75901784826SJohnathan Mantey                                 const std::string &gateway,
76001784826SJohnathan Mantey                                 const std::string &address,
76101784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
76201784826SJohnathan Mantey {
76301784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
76401784826SJohnathan Mantey         [asyncResp, ifaceId, address, prefixLength,
76501784826SJohnathan Mantey          gateway](const boost::system::error_code ec) {
76601784826SJohnathan Mantey             if (ec)
76701784826SJohnathan Mantey             {
76801784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
76901784826SJohnathan Mantey             }
77001784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
77101784826SJohnathan Mantey                 [asyncResp](const boost::system::error_code ec) {
77201784826SJohnathan Mantey                     if (ec)
77301784826SJohnathan Mantey                     {
77401784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
77501784826SJohnathan Mantey                     }
77601784826SJohnathan Mantey                 },
77701784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
77801784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
77901784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
78001784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
78101784826SJohnathan Mantey                 prefixLength, gateway);
78201784826SJohnathan Mantey         },
78301784826SJohnathan Mantey         "xyz.openbmc_project.Network",
78401784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
78501784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
78601784826SJohnathan Mantey }
78701784826SJohnathan Mantey 
78801784826SJohnathan Mantey /**
789e48c0fc5SRavi Teja  * @brief Deletes given IPv6
790e48c0fc5SRavi Teja  *
791e48c0fc5SRavi Teja  * @param[in] ifaceId     Id of interface whose IP should be deleted
792e48c0fc5SRavi Teja  * @param[in] ipHash      DBus Hash id of IP that should be deleted
793e48c0fc5SRavi Teja  * @param[io] asyncResp   Response object that will be returned to client
794e48c0fc5SRavi Teja  *
795e48c0fc5SRavi Teja  * @return None
796e48c0fc5SRavi Teja  */
797e48c0fc5SRavi Teja inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
798e48c0fc5SRavi Teja                        const std::shared_ptr<AsyncResp> asyncResp)
799e48c0fc5SRavi Teja {
800e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
801286b9118SJohnathan Mantey         [asyncResp](const boost::system::error_code ec) {
802e48c0fc5SRavi Teja             if (ec)
803e48c0fc5SRavi Teja             {
804e48c0fc5SRavi Teja                 messages::internalError(asyncResp->res);
805e48c0fc5SRavi Teja             }
806e48c0fc5SRavi Teja         },
807e48c0fc5SRavi Teja         "xyz.openbmc_project.Network",
808e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
809e48c0fc5SRavi Teja         "xyz.openbmc_project.Object.Delete", "Delete");
810e48c0fc5SRavi Teja }
811e48c0fc5SRavi Teja 
812e48c0fc5SRavi Teja /**
81301784826SJohnathan Mantey  * @brief Deletes the IPv6 entry for this interface and creates a replacement
81401784826SJohnathan Mantey  * static IPv6 entry
81501784826SJohnathan Mantey  *
81601784826SJohnathan Mantey  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
81701784826SJohnathan Mantey  * @param[in] id           The unique hash entry identifying the DBus entry
81801784826SJohnathan Mantey  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
81901784826SJohnathan Mantey  * @param[in] address      IPv6 address to assign to this interface
82001784826SJohnathan Mantey  * @param[io] asyncResp    Response object that will be returned to client
82101784826SJohnathan Mantey  *
82201784826SJohnathan Mantey  * @return None
82301784826SJohnathan Mantey  */
82401784826SJohnathan Mantey inline void deleteAndCreateIPv6(const std::string &ifaceId,
82501784826SJohnathan Mantey                                 const std::string &id, uint8_t prefixLength,
82601784826SJohnathan Mantey                                 const std::string &address,
82701784826SJohnathan Mantey                                 std::shared_ptr<AsyncResp> asyncResp)
82801784826SJohnathan Mantey {
82901784826SJohnathan Mantey     crow::connections::systemBus->async_method_call(
83001784826SJohnathan Mantey         [asyncResp, ifaceId, address,
83101784826SJohnathan Mantey          prefixLength](const boost::system::error_code ec) {
83201784826SJohnathan Mantey             if (ec)
83301784826SJohnathan Mantey             {
83401784826SJohnathan Mantey                 messages::internalError(asyncResp->res);
83501784826SJohnathan Mantey             }
83601784826SJohnathan Mantey             crow::connections::systemBus->async_method_call(
83701784826SJohnathan Mantey                 [asyncResp](const boost::system::error_code ec) {
83801784826SJohnathan Mantey                     if (ec)
83901784826SJohnathan Mantey                     {
84001784826SJohnathan Mantey                         messages::internalError(asyncResp->res);
84101784826SJohnathan Mantey                     }
84201784826SJohnathan Mantey                 },
84301784826SJohnathan Mantey                 "xyz.openbmc_project.Network",
84401784826SJohnathan Mantey                 "/xyz/openbmc_project/network/" + ifaceId,
84501784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Create", "IP",
84601784826SJohnathan Mantey                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
84701784826SJohnathan Mantey                 prefixLength, "");
84801784826SJohnathan Mantey         },
84901784826SJohnathan Mantey         "xyz.openbmc_project.Network",
85001784826SJohnathan Mantey         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
85101784826SJohnathan Mantey         "xyz.openbmc_project.Object.Delete", "Delete");
85201784826SJohnathan Mantey }
85301784826SJohnathan Mantey 
85401784826SJohnathan Mantey /**
855e48c0fc5SRavi Teja  * @brief Creates IPv6 with given data
856e48c0fc5SRavi Teja  *
857e48c0fc5SRavi Teja  * @param[in] ifaceId      Id of interface whose IP should be added
858e48c0fc5SRavi Teja  * @param[in] prefixLength Prefix length that needs to be added
859e48c0fc5SRavi Teja  * @param[in] address      IP address that needs to be added
860e48c0fc5SRavi Teja  * @param[io] asyncResp    Response object that will be returned to client
861e48c0fc5SRavi Teja  *
862e48c0fc5SRavi Teja  * @return None
863e48c0fc5SRavi Teja  */
86401784826SJohnathan Mantey inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
86501784826SJohnathan Mantey                        const std::string &address,
866e48c0fc5SRavi Teja                        std::shared_ptr<AsyncResp> asyncResp)
867e48c0fc5SRavi Teja {
868e48c0fc5SRavi Teja     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
869e48c0fc5SRavi Teja         if (ec)
870e48c0fc5SRavi Teja         {
871e48c0fc5SRavi Teja             messages::internalError(asyncResp->res);
872e48c0fc5SRavi Teja         }
873e48c0fc5SRavi Teja     };
874e48c0fc5SRavi Teja     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
875e48c0fc5SRavi Teja     // does not have assosiated gateway property
876e48c0fc5SRavi Teja     crow::connections::systemBus->async_method_call(
877e48c0fc5SRavi Teja         std::move(createIpHandler), "xyz.openbmc_project.Network",
878e48c0fc5SRavi Teja         "/xyz/openbmc_project/network/" + ifaceId,
879e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Create", "IP",
880e48c0fc5SRavi Teja         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
881e48c0fc5SRavi Teja         "");
882e48c0fc5SRavi Teja }
883e48c0fc5SRavi Teja 
884179db1d7SKowalski, Kamil /**
885179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
886179db1d7SKowalski, Kamil  * Object
887179db1d7SKowalski, Kamil  * from EntityManager Network Manager
8884a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
889179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
890179db1d7SKowalski, Kamil  * into JSON
891179db1d7SKowalski, Kamil  */
892179db1d7SKowalski, Kamil template <typename CallbackFunc>
8934a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
8941abe55efSEd Tanous                           CallbackFunc &&callback)
8951abe55efSEd Tanous {
89655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
8974a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
8981abe55efSEd Tanous             const boost::system::error_code error_code,
8994a0cb85cSEd Tanous             const GetManagedObjects &resp) {
90055c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
9014a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
902e48c0fc5SRavi Teja             boost::container::flat_set<IPv6AddressData> ipv6Data;
903179db1d7SKowalski, Kamil 
9041abe55efSEd Tanous             if (error_code)
9051abe55efSEd Tanous             {
90601784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
907179db1d7SKowalski, Kamil                 return;
908179db1d7SKowalski, Kamil             }
909179db1d7SKowalski, Kamil 
9104c9afe43SEd Tanous             bool found =
9114a0cb85cSEd Tanous                 extractEthernetInterfaceData(ethiface_id, resp, ethData);
9124c9afe43SEd Tanous             if (!found)
9134c9afe43SEd Tanous             {
91401784826SJohnathan Mantey                 callback(false, ethData, ipv4Data, ipv6Data);
9154c9afe43SEd Tanous                 return;
9164c9afe43SEd Tanous             }
9174c9afe43SEd Tanous 
91801784826SJohnathan Mantey             extractIPData(ethiface_id, resp, ipv4Data);
919179db1d7SKowalski, Kamil             // Fix global GW
9201abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
9211abe55efSEd Tanous             {
922c619141bSRavi Teja                 if (((ipv4.linktype == LinkType::Global) &&
923c619141bSRavi Teja                      (ipv4.gateway == "0.0.0.0")) ||
924c619141bSRavi Teja                     (ipv4.origin == "DHCP"))
9251abe55efSEd Tanous                 {
9264a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
927179db1d7SKowalski, Kamil                 }
928179db1d7SKowalski, Kamil             }
929179db1d7SKowalski, Kamil 
93001784826SJohnathan Mantey             extractIPV6Data(ethiface_id, resp, ipv6Data);
9314a0cb85cSEd Tanous             // Finally make a callback with usefull data
93201784826SJohnathan Mantey             callback(true, ethData, ipv4Data, ipv6Data);
933179db1d7SKowalski, Kamil         },
934179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
935179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
936271584abSEd Tanous }
937179db1d7SKowalski, Kamil 
938179db1d7SKowalski, Kamil /**
9399391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
9409391bb9cSRapkiewicz, Pawel  * Manager
9411abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
9421abe55efSEd Tanous  * into JSON.
9439391bb9cSRapkiewicz, Pawel  */
9449391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
9451abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
9461abe55efSEd Tanous {
94755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
9484a0cb85cSEd Tanous         [callback{std::move(callback)}](
9499391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
9504a0cb85cSEd Tanous             GetManagedObjects &resp) {
9511abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
9521abe55efSEd Tanous             // ethernet interfaces
9534c9afe43SEd Tanous             boost::container::flat_set<std::string> iface_list;
9544a0cb85cSEd Tanous             iface_list.reserve(resp.size());
9551abe55efSEd Tanous             if (error_code)
9561abe55efSEd Tanous             {
9574a0cb85cSEd Tanous                 callback(false, iface_list);
9589391bb9cSRapkiewicz, Pawel                 return;
9599391bb9cSRapkiewicz, Pawel             }
9609391bb9cSRapkiewicz, Pawel 
9619391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
9624a0cb85cSEd Tanous             for (const auto &objpath : resp)
9631abe55efSEd Tanous             {
9649391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
9654a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
9661abe55efSEd Tanous                 {
9671abe55efSEd Tanous                     // If interface is
9684a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
9694a0cb85cSEd Tanous                     // what we're looking for.
9709391bb9cSRapkiewicz, Pawel                     if (interface.first ==
9711abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
9721abe55efSEd Tanous                     {
9734a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
9744a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
9754a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
9764a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
9771abe55efSEd Tanous                         {
9789391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
9794c9afe43SEd Tanous                             iface_list.emplace(iface_id.substr(last_pos + 1));
9809391bb9cSRapkiewicz, Pawel                         }
9819391bb9cSRapkiewicz, Pawel                     }
9829391bb9cSRapkiewicz, Pawel                 }
9839391bb9cSRapkiewicz, Pawel             }
984a434f2bdSEd Tanous             // Finally make a callback with useful data
9854a0cb85cSEd Tanous             callback(true, iface_list);
9869391bb9cSRapkiewicz, Pawel         },
987aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
988aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
989271584abSEd Tanous }
9909391bb9cSRapkiewicz, Pawel 
9919391bb9cSRapkiewicz, Pawel /**
9929391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
9939391bb9cSRapkiewicz, Pawel  */
9941abe55efSEd Tanous class EthernetCollection : public Node
9951abe55efSEd Tanous {
9969391bb9cSRapkiewicz, Pawel   public:
9974a0cb85cSEd Tanous     template <typename CrowApp>
9981abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
9994a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
10001abe55efSEd Tanous     {
1001588c3f0dSKowalski, Kamil         entityPrivileges = {
1002588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1003e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1004e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1005e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1006e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1007e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10089391bb9cSRapkiewicz, Pawel     }
10099391bb9cSRapkiewicz, Pawel 
10109391bb9cSRapkiewicz, Pawel   private:
10119391bb9cSRapkiewicz, Pawel     /**
10129391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
10139391bb9cSRapkiewicz, Pawel      */
101455c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
10151abe55efSEd Tanous                const std::vector<std::string> &params) override
10161abe55efSEd Tanous     {
10170f74e643SEd Tanous         res.jsonValue["@odata.type"] =
10180f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
10190f74e643SEd Tanous         res.jsonValue["@odata.id"] =
10200f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
10210f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
10220f74e643SEd Tanous         res.jsonValue["Description"] =
10230f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
10244c9afe43SEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
10254a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
10261abe55efSEd Tanous         // preparation
1027f12894f8SJason M. Bills         getEthernetIfaceList(
10284c9afe43SEd Tanous             [asyncResp](
10294c9afe43SEd Tanous                 const bool &success,
10304c9afe43SEd Tanous                 const boost::container::flat_set<std::string> &iface_list) {
10314a0cb85cSEd Tanous                 if (!success)
10321abe55efSEd Tanous                 {
10334c9afe43SEd Tanous                     messages::internalError(asyncResp->res);
10344a0cb85cSEd Tanous                     return;
10354a0cb85cSEd Tanous                 }
10364a0cb85cSEd Tanous 
10374c9afe43SEd Tanous                 nlohmann::json &iface_array =
10384c9afe43SEd Tanous                     asyncResp->res.jsonValue["Members"];
10394a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
1040fda13ad2SSunitha Harish                 std::string tag = "_";
10414a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
10421abe55efSEd Tanous                 {
1043fda13ad2SSunitha Harish                     std::size_t found = iface_item.find(tag);
1044fda13ad2SSunitha Harish                     if (found == std::string::npos)
1045fda13ad2SSunitha Harish                     {
10464a0cb85cSEd Tanous                         iface_array.push_back(
10474a0cb85cSEd Tanous                             {{"@odata.id",
10484a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
10494a0cb85cSEd Tanous                                   iface_item}});
10509391bb9cSRapkiewicz, Pawel                     }
1051fda13ad2SSunitha Harish                 }
10524a0cb85cSEd Tanous 
10534c9afe43SEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
10544c9afe43SEd Tanous                     iface_array.size();
10554c9afe43SEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
10564a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
10579391bb9cSRapkiewicz, Pawel             });
10589391bb9cSRapkiewicz, Pawel     }
10599391bb9cSRapkiewicz, Pawel };
10609391bb9cSRapkiewicz, Pawel 
10619391bb9cSRapkiewicz, Pawel /**
10629391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
10639391bb9cSRapkiewicz, Pawel  */
10641abe55efSEd Tanous class EthernetInterface : public Node
10651abe55efSEd Tanous {
10669391bb9cSRapkiewicz, Pawel   public:
10679391bb9cSRapkiewicz, Pawel     /*
10689391bb9cSRapkiewicz, Pawel      * Default Constructor
10699391bb9cSRapkiewicz, Pawel      */
10704a0cb85cSEd Tanous     template <typename CrowApp>
10711abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
10724a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
10731abe55efSEd Tanous              std::string())
10741abe55efSEd Tanous     {
1075588c3f0dSKowalski, Kamil         entityPrivileges = {
1076588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1077e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
1078e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1079e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1080e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1081e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
10829391bb9cSRapkiewicz, Pawel     }
10839391bb9cSRapkiewicz, Pawel 
1084e439f0f8SKowalski, Kamil   private:
1085bc0bd6e0SEd Tanous     void handleHostnamePatch(const std::string &hostname,
10864a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
10871abe55efSEd Tanous     {
1088*ab6554f1SJoshi-Mansi         // SHOULD handle host names of up to 255 characters(RFC 1123)
1089*ab6554f1SJoshi-Mansi         if (hostname.length() > 255)
1090*ab6554f1SJoshi-Mansi         {
1091*ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, hostname,
1092*ab6554f1SJoshi-Mansi                                                "HostName");
1093*ab6554f1SJoshi-Mansi             return;
1094*ab6554f1SJoshi-Mansi         }
1095bc0bd6e0SEd Tanous         crow::connections::systemBus->async_method_call(
1096bc0bd6e0SEd Tanous             [asyncResp](const boost::system::error_code ec) {
10974a0cb85cSEd Tanous                 if (ec)
10984a0cb85cSEd Tanous                 {
1099a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
11001abe55efSEd Tanous                 }
1101bc0bd6e0SEd Tanous             },
1102bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network",
1103bc0bd6e0SEd Tanous             "/xyz/openbmc_project/network/config",
1104bc0bd6e0SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
1105bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1106abf2add6SEd Tanous             std::variant<std::string>(hostname));
1107588c3f0dSKowalski, Kamil     }
1108588c3f0dSKowalski, Kamil 
1109*ab6554f1SJoshi-Mansi     void handleDomainnamePatch(const std::string &ifaceId,
1110*ab6554f1SJoshi-Mansi                                const std::string &domainname,
1111*ab6554f1SJoshi-Mansi                                const std::shared_ptr<AsyncResp> asyncResp)
1112*ab6554f1SJoshi-Mansi     {
1113*ab6554f1SJoshi-Mansi         std::vector<std::string> vectorDomainname = {domainname};
1114*ab6554f1SJoshi-Mansi         crow::connections::systemBus->async_method_call(
1115*ab6554f1SJoshi-Mansi             [asyncResp](const boost::system::error_code ec) {
1116*ab6554f1SJoshi-Mansi                 if (ec)
1117*ab6554f1SJoshi-Mansi                 {
1118*ab6554f1SJoshi-Mansi                     messages::internalError(asyncResp->res);
1119*ab6554f1SJoshi-Mansi                 }
1120*ab6554f1SJoshi-Mansi             },
1121*ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network",
1122*ab6554f1SJoshi-Mansi             "/xyz/openbmc_project/network/" + ifaceId,
1123*ab6554f1SJoshi-Mansi             "org.freedesktop.DBus.Properties", "Set",
1124*ab6554f1SJoshi-Mansi             "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1125*ab6554f1SJoshi-Mansi             std::variant<std::vector<std::string>>(vectorDomainname));
1126*ab6554f1SJoshi-Mansi     }
1127*ab6554f1SJoshi-Mansi 
1128*ab6554f1SJoshi-Mansi     void handleFqdnPatch(const std::string &ifaceId, const std::string &fqdn,
1129*ab6554f1SJoshi-Mansi                          const std::shared_ptr<AsyncResp> asyncResp)
1130*ab6554f1SJoshi-Mansi     {
1131*ab6554f1SJoshi-Mansi         // Total length of FQDN must not exceed 255 characters(RFC 1035)
1132*ab6554f1SJoshi-Mansi         if (fqdn.length() > 255)
1133*ab6554f1SJoshi-Mansi         {
1134*ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135*ab6554f1SJoshi-Mansi             return;
1136*ab6554f1SJoshi-Mansi         }
1137*ab6554f1SJoshi-Mansi 
1138*ab6554f1SJoshi-Mansi         size_t pos = fqdn.find('.');
1139*ab6554f1SJoshi-Mansi         if (pos == std::string::npos)
1140*ab6554f1SJoshi-Mansi         {
1141*ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1142*ab6554f1SJoshi-Mansi             return;
1143*ab6554f1SJoshi-Mansi         }
1144*ab6554f1SJoshi-Mansi 
1145*ab6554f1SJoshi-Mansi         std::string hostname;
1146*ab6554f1SJoshi-Mansi         std::string domainname;
1147*ab6554f1SJoshi-Mansi         domainname = (fqdn).substr(pos + 1);
1148*ab6554f1SJoshi-Mansi         hostname = (fqdn).substr(0, pos);
1149*ab6554f1SJoshi-Mansi 
1150*ab6554f1SJoshi-Mansi         if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1151*ab6554f1SJoshi-Mansi         {
1152*ab6554f1SJoshi-Mansi             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1153*ab6554f1SJoshi-Mansi             return;
1154*ab6554f1SJoshi-Mansi         }
1155*ab6554f1SJoshi-Mansi 
1156*ab6554f1SJoshi-Mansi         handleHostnamePatch(hostname, asyncResp);
1157*ab6554f1SJoshi-Mansi         handleDomainnamePatch(ifaceId, domainname, asyncResp);
1158*ab6554f1SJoshi-Mansi     }
1159*ab6554f1SJoshi-Mansi 
1160*ab6554f1SJoshi-Mansi     bool isHostnameValid(const std::string &hostname)
1161*ab6554f1SJoshi-Mansi     {
1162*ab6554f1SJoshi-Mansi         // A valid host name can never have the dotted-decimal form (RFC 1123)
1163*ab6554f1SJoshi-Mansi         if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1164*ab6554f1SJoshi-Mansi         {
1165*ab6554f1SJoshi-Mansi             return false;
1166*ab6554f1SJoshi-Mansi         }
1167*ab6554f1SJoshi-Mansi         // Each label(hostname/subdomains) within a valid FQDN
1168*ab6554f1SJoshi-Mansi         // MUST handle host names of up to 63 characters (RFC 1123)
1169*ab6554f1SJoshi-Mansi         // labels cannot start or end with hyphens (RFC 952)
1170*ab6554f1SJoshi-Mansi         // labels can start with numbers (RFC 1123)
1171*ab6554f1SJoshi-Mansi         const std::regex pattern(
1172*ab6554f1SJoshi-Mansi             "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1173*ab6554f1SJoshi-Mansi 
1174*ab6554f1SJoshi-Mansi         return std::regex_match(hostname, pattern);
1175*ab6554f1SJoshi-Mansi     }
1176*ab6554f1SJoshi-Mansi 
1177*ab6554f1SJoshi-Mansi     bool isDomainnameValid(const std::string &domainname)
1178*ab6554f1SJoshi-Mansi     {
1179*ab6554f1SJoshi-Mansi         // Can have multiple subdomains
1180*ab6554f1SJoshi-Mansi         // Top Level Domain's min length is 2 character
1181*ab6554f1SJoshi-Mansi         const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1182*ab6554f1SJoshi-Mansi                                  "{1,30}\\.)*[a-zA-Z]{2,}$");
1183*ab6554f1SJoshi-Mansi 
1184*ab6554f1SJoshi-Mansi         return std::regex_match(domainname, pattern);
1185*ab6554f1SJoshi-Mansi     }
1186*ab6554f1SJoshi-Mansi 
1187d577665bSRatan Gupta     void handleMACAddressPatch(const std::string &ifaceId,
1188d577665bSRatan Gupta                                const std::string &macAddress,
1189d577665bSRatan Gupta                                const std::shared_ptr<AsyncResp> &asyncResp)
1190d577665bSRatan Gupta     {
1191d577665bSRatan Gupta         crow::connections::systemBus->async_method_call(
1192d577665bSRatan Gupta             [asyncResp, macAddress](const boost::system::error_code ec) {
1193d577665bSRatan Gupta                 if (ec)
1194d577665bSRatan Gupta                 {
1195d577665bSRatan Gupta                     messages::internalError(asyncResp->res);
1196d577665bSRatan Gupta                     return;
1197d577665bSRatan Gupta                 }
1198d577665bSRatan Gupta             },
1199d577665bSRatan Gupta             "xyz.openbmc_project.Network",
1200d577665bSRatan Gupta             "/xyz/openbmc_project/network/" + ifaceId,
1201d577665bSRatan Gupta             "org.freedesktop.DBus.Properties", "Set",
1202d577665bSRatan Gupta             "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1203d577665bSRatan Gupta             std::variant<std::string>(macAddress));
1204d577665bSRatan Gupta     }
1205286b9118SJohnathan Mantey 
1206da131a9aSJennifer Lee     void setDHCPEnabled(const std::string &ifaceId,
12071f8c7b5dSJohnathan Mantey                         const std::string &propertyName, const bool v4Value,
12081f8c7b5dSJohnathan Mantey                         const bool v6Value,
1209da131a9aSJennifer Lee                         const std::shared_ptr<AsyncResp> asyncResp)
1210da131a9aSJennifer Lee     {
12111f8c7b5dSJohnathan Mantey         const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
1212da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1213da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1214da131a9aSJennifer Lee                 if (ec)
1215da131a9aSJennifer Lee                 {
1216da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1217da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1218da131a9aSJennifer Lee                     return;
1219da131a9aSJennifer Lee                 }
1220da131a9aSJennifer Lee             },
1221da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1222da131a9aSJennifer Lee             "/xyz/openbmc_project/network/" + ifaceId,
1223da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1224da131a9aSJennifer Lee             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
12251f8c7b5dSJohnathan Mantey             std::variant<std::string>{dhcp});
1226da131a9aSJennifer Lee     }
12271f8c7b5dSJohnathan Mantey 
1228eeedda23SJohnathan Mantey     void setEthernetInterfaceBoolProperty(
1229eeedda23SJohnathan Mantey         const std::string &ifaceId, const std::string &propertyName,
1230eeedda23SJohnathan Mantey         const bool &value, const std::shared_ptr<AsyncResp> asyncResp)
1231eeedda23SJohnathan Mantey     {
1232eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1233eeedda23SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1234eeedda23SJohnathan Mantey                 if (ec)
1235eeedda23SJohnathan Mantey                 {
1236eeedda23SJohnathan Mantey                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1237eeedda23SJohnathan Mantey                     messages::internalError(asyncResp->res);
1238eeedda23SJohnathan Mantey                     return;
1239eeedda23SJohnathan Mantey                 }
1240eeedda23SJohnathan Mantey             },
1241eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network",
1242eeedda23SJohnathan Mantey             "/xyz/openbmc_project/network/" + ifaceId,
1243eeedda23SJohnathan Mantey             "org.freedesktop.DBus.Properties", "Set",
1244eeedda23SJohnathan Mantey             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1245eeedda23SJohnathan Mantey             std::variant<bool>{value});
1246eeedda23SJohnathan Mantey     }
1247eeedda23SJohnathan Mantey 
1248da131a9aSJennifer Lee     void setDHCPv4Config(const std::string &propertyName, const bool &value,
1249da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1250da131a9aSJennifer Lee     {
1251da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1252da131a9aSJennifer Lee         crow::connections::systemBus->async_method_call(
1253da131a9aSJennifer Lee             [asyncResp](const boost::system::error_code ec) {
1254da131a9aSJennifer Lee                 if (ec)
1255da131a9aSJennifer Lee                 {
1256da131a9aSJennifer Lee                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1257da131a9aSJennifer Lee                     messages::internalError(asyncResp->res);
1258da131a9aSJennifer Lee                     return;
1259da131a9aSJennifer Lee                 }
1260da131a9aSJennifer Lee             },
1261da131a9aSJennifer Lee             "xyz.openbmc_project.Network",
1262da131a9aSJennifer Lee             "/xyz/openbmc_project/network/config/dhcp",
1263da131a9aSJennifer Lee             "org.freedesktop.DBus.Properties", "Set",
1264da131a9aSJennifer Lee             "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1265da131a9aSJennifer Lee             std::variant<bool>{value});
1266da131a9aSJennifer Lee     }
1267d577665bSRatan Gupta 
12681f8c7b5dSJohnathan Mantey     void handleDHCPPatch(const std::string &ifaceId,
12691f8c7b5dSJohnathan Mantey                          const EthernetInterfaceData &ethData,
12701f8c7b5dSJohnathan Mantey                          DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1271da131a9aSJennifer Lee                          const std::shared_ptr<AsyncResp> asyncResp)
1272da131a9aSJennifer Lee     {
12731f8c7b5dSJohnathan Mantey         bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
12741f8c7b5dSJohnathan Mantey         bool ipv6Active =
12751f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1276da131a9aSJennifer Lee 
12771f8c7b5dSJohnathan Mantey         bool nextv4DHCPState =
12781f8c7b5dSJohnathan Mantey             v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
12791f8c7b5dSJohnathan Mantey 
12801f8c7b5dSJohnathan Mantey         bool nextv6DHCPState{};
12811f8c7b5dSJohnathan Mantey         if (v6dhcpParms.dhcpv6OperatingMode)
1282da131a9aSJennifer Lee         {
12831f8c7b5dSJohnathan Mantey             if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
12841f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
12851f8c7b5dSJohnathan Mantey                 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
12861f8c7b5dSJohnathan Mantey             {
12871f8c7b5dSJohnathan Mantey                 messages::propertyValueFormatError(
12881f8c7b5dSJohnathan Mantey                     asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
12891f8c7b5dSJohnathan Mantey                     "OperatingMode");
1290da131a9aSJennifer Lee                 return;
1291da131a9aSJennifer Lee             }
12921f8c7b5dSJohnathan Mantey             nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
12931f8c7b5dSJohnathan Mantey         }
12941f8c7b5dSJohnathan Mantey         else
1295da131a9aSJennifer Lee         {
12961f8c7b5dSJohnathan Mantey             nextv6DHCPState = ipv6Active;
12971f8c7b5dSJohnathan Mantey         }
12981f8c7b5dSJohnathan Mantey 
12991f8c7b5dSJohnathan Mantey         bool nextDNS{};
13001f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
13011f8c7b5dSJohnathan Mantey         {
13021f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
13031f8c7b5dSJohnathan Mantey             {
13041f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13051f8c7b5dSJohnathan Mantey                 return;
13061f8c7b5dSJohnathan Mantey             }
13071f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13081f8c7b5dSJohnathan Mantey         }
13091f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useDNSServers)
13101f8c7b5dSJohnathan Mantey         {
13111f8c7b5dSJohnathan Mantey             nextDNS = *v4dhcpParms.useDNSServers;
13121f8c7b5dSJohnathan Mantey         }
13131f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useDNSServers)
13141f8c7b5dSJohnathan Mantey         {
13151f8c7b5dSJohnathan Mantey             nextDNS = *v6dhcpParms.useDNSServers;
13161f8c7b5dSJohnathan Mantey         }
13171f8c7b5dSJohnathan Mantey         else
13181f8c7b5dSJohnathan Mantey         {
13191f8c7b5dSJohnathan Mantey             nextDNS = ethData.DNSEnabled;
13201f8c7b5dSJohnathan Mantey         }
13211f8c7b5dSJohnathan Mantey 
13221f8c7b5dSJohnathan Mantey         bool nextNTP{};
13231f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
13241f8c7b5dSJohnathan Mantey         {
13251f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
13261f8c7b5dSJohnathan Mantey             {
13271f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13281f8c7b5dSJohnathan Mantey                 return;
13291f8c7b5dSJohnathan Mantey             }
13301f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13311f8c7b5dSJohnathan Mantey         }
13321f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useNTPServers)
13331f8c7b5dSJohnathan Mantey         {
13341f8c7b5dSJohnathan Mantey             nextNTP = *v4dhcpParms.useNTPServers;
13351f8c7b5dSJohnathan Mantey         }
13361f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useNTPServers)
13371f8c7b5dSJohnathan Mantey         {
13381f8c7b5dSJohnathan Mantey             nextNTP = *v6dhcpParms.useNTPServers;
13391f8c7b5dSJohnathan Mantey         }
13401f8c7b5dSJohnathan Mantey         else
13411f8c7b5dSJohnathan Mantey         {
13421f8c7b5dSJohnathan Mantey             nextNTP = ethData.NTPEnabled;
13431f8c7b5dSJohnathan Mantey         }
13441f8c7b5dSJohnathan Mantey 
13451f8c7b5dSJohnathan Mantey         bool nextUseDomain{};
13461f8c7b5dSJohnathan Mantey         if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
13471f8c7b5dSJohnathan Mantey         {
13481f8c7b5dSJohnathan Mantey             if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
13491f8c7b5dSJohnathan Mantey             {
13501f8c7b5dSJohnathan Mantey                 messages::generalError(asyncResp->res);
13511f8c7b5dSJohnathan Mantey                 return;
13521f8c7b5dSJohnathan Mantey             }
13531f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13541f8c7b5dSJohnathan Mantey         }
13551f8c7b5dSJohnathan Mantey         else if (v4dhcpParms.useUseDomainName)
13561f8c7b5dSJohnathan Mantey         {
13571f8c7b5dSJohnathan Mantey             nextUseDomain = *v4dhcpParms.useUseDomainName;
13581f8c7b5dSJohnathan Mantey         }
13591f8c7b5dSJohnathan Mantey         else if (v6dhcpParms.useUseDomainName)
13601f8c7b5dSJohnathan Mantey         {
13611f8c7b5dSJohnathan Mantey             nextUseDomain = *v6dhcpParms.useUseDomainName;
13621f8c7b5dSJohnathan Mantey         }
13631f8c7b5dSJohnathan Mantey         else
13641f8c7b5dSJohnathan Mantey         {
13651f8c7b5dSJohnathan Mantey             nextUseDomain = ethData.HostNameEnabled;
13661f8c7b5dSJohnathan Mantey         }
13671f8c7b5dSJohnathan Mantey 
1368da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
13691f8c7b5dSJohnathan Mantey         setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
13701f8c7b5dSJohnathan Mantey                        asyncResp);
1371da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set DNSEnabled...";
13721f8c7b5dSJohnathan Mantey         setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1373da131a9aSJennifer Lee         BMCWEB_LOG_DEBUG << "set NTPEnabled...";
13741f8c7b5dSJohnathan Mantey         setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
13751f8c7b5dSJohnathan Mantey         BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
13761f8c7b5dSJohnathan Mantey         setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1377da131a9aSJennifer Lee     }
137801784826SJohnathan Mantey 
137901784826SJohnathan Mantey     boost::container::flat_set<IPv4AddressData>::const_iterator
138001784826SJohnathan Mantey         GetNextStaticIPEntry(
138101784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator head,
138201784826SJohnathan Mantey             boost::container::flat_set<IPv4AddressData>::const_iterator end)
138301784826SJohnathan Mantey     {
138401784826SJohnathan Mantey         for (; head != end; head++)
138501784826SJohnathan Mantey         {
138601784826SJohnathan Mantey             if (head->origin == "Static")
138701784826SJohnathan Mantey             {
138801784826SJohnathan Mantey                 return head;
138901784826SJohnathan Mantey             }
139001784826SJohnathan Mantey         }
139101784826SJohnathan Mantey         return end;
139201784826SJohnathan Mantey     }
139301784826SJohnathan Mantey 
139401784826SJohnathan Mantey     boost::container::flat_set<IPv6AddressData>::const_iterator
139501784826SJohnathan Mantey         GetNextStaticIPEntry(
139601784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator head,
139701784826SJohnathan Mantey             boost::container::flat_set<IPv6AddressData>::const_iterator end)
139801784826SJohnathan Mantey     {
139901784826SJohnathan Mantey         for (; head != end; head++)
140001784826SJohnathan Mantey         {
140101784826SJohnathan Mantey             if (head->origin == "Static")
140201784826SJohnathan Mantey             {
140301784826SJohnathan Mantey                 return head;
140401784826SJohnathan Mantey             }
140501784826SJohnathan Mantey         }
140601784826SJohnathan Mantey         return end;
140701784826SJohnathan Mantey     }
140801784826SJohnathan Mantey 
1409d1d50814SRavi Teja     void handleIPv4StaticPatch(
1410f476acbfSRatan Gupta         const std::string &ifaceId, nlohmann::json &input,
141101784826SJohnathan Mantey         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
14124a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
14131abe55efSEd Tanous     {
141401784826SJohnathan Mantey         if ((!input.is_array()) || input.empty())
1415f476acbfSRatan Gupta         {
1416f476acbfSRatan Gupta             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1417d1d50814SRavi Teja                                              "IPv4StaticAddresses");
1418f476acbfSRatan Gupta             return;
1419f476acbfSRatan Gupta         }
1420f476acbfSRatan Gupta 
1421271584abSEd Tanous         unsigned entryIdx = 1;
142201784826SJohnathan Mantey         // Find the first static IP address currently active on the NIC and
142301784826SJohnathan Mantey         // match it to the first JSON element in the IPv4StaticAddresses array.
142401784826SJohnathan Mantey         // Match each subsequent JSON element to the next static IP programmed
142501784826SJohnathan Mantey         // into the NIC.
142601784826SJohnathan Mantey         boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
142701784826SJohnathan Mantey             GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
142801784826SJohnathan Mantey 
1429537174c4SEd Tanous         for (nlohmann::json &thisJson : input)
14301abe55efSEd Tanous         {
14314a0cb85cSEd Tanous             std::string pathString =
1432d1d50814SRavi Teja                 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1433179db1d7SKowalski, Kamil 
143401784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1435f476acbfSRatan Gupta             {
1436537174c4SEd Tanous                 std::optional<std::string> address;
1437537174c4SEd Tanous                 std::optional<std::string> subnetMask;
1438537174c4SEd Tanous                 std::optional<std::string> gateway;
1439537174c4SEd Tanous 
1440537174c4SEd Tanous                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
14417e27d832SJohnathan Mantey                                          address, "SubnetMask", subnetMask,
14427e27d832SJohnathan Mantey                                          "Gateway", gateway))
1443537174c4SEd Tanous                 {
144401784826SJohnathan Mantey                     messages::propertyValueFormatError(
144501784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1446537174c4SEd Tanous                     return;
1447179db1d7SKowalski, Kamil                 }
1448179db1d7SKowalski, Kamil 
144901784826SJohnathan Mantey                 // Find the address/subnet/gateway values. Any values that are
145001784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
145101784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
145201784826SJohnathan Mantey                 // current request.
1453271584abSEd Tanous                 const std::string *addr = nullptr;
1454271584abSEd Tanous                 const std::string *gw = nullptr;
145501784826SJohnathan Mantey                 uint8_t prefixLength = 0;
145601784826SJohnathan Mantey                 bool errorInEntry = false;
1457537174c4SEd Tanous                 if (address)
14581abe55efSEd Tanous                 {
145901784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*address))
14601abe55efSEd Tanous                     {
146101784826SJohnathan Mantey                         addr = &(*address);
14624a0cb85cSEd Tanous                     }
146301784826SJohnathan Mantey                     else
146401784826SJohnathan Mantey                     {
146501784826SJohnathan Mantey                         messages::propertyValueFormatError(
146601784826SJohnathan Mantey                             asyncResp->res, *address, pathString + "/Address");
146701784826SJohnathan Mantey                         errorInEntry = true;
146801784826SJohnathan Mantey                     }
146901784826SJohnathan Mantey                 }
147001784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
147101784826SJohnathan Mantey                 {
147201784826SJohnathan Mantey                     addr = &(NICIPentry->address);
147301784826SJohnathan Mantey                 }
147401784826SJohnathan Mantey                 else
147501784826SJohnathan Mantey                 {
147601784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
147701784826SJohnathan Mantey                                               pathString + "/Address");
147801784826SJohnathan Mantey                     errorInEntry = true;
14794a0cb85cSEd Tanous                 }
14804a0cb85cSEd Tanous 
1481537174c4SEd Tanous                 if (subnetMask)
14824a0cb85cSEd Tanous                 {
1483537174c4SEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
14844a0cb85cSEd Tanous                     {
1485f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1486537174c4SEd Tanous                             asyncResp->res, *subnetMask,
14874a0cb85cSEd Tanous                             pathString + "/SubnetMask");
148801784826SJohnathan Mantey                         errorInEntry = true;
14894a0cb85cSEd Tanous                     }
14904a0cb85cSEd Tanous                 }
149101784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
14924a0cb85cSEd Tanous                 {
149301784826SJohnathan Mantey                     if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
149401784826SJohnathan Mantey                                                     &prefixLength))
14954a0cb85cSEd Tanous                     {
149601784826SJohnathan Mantey                         messages::propertyValueFormatError(
149701784826SJohnathan Mantey                             asyncResp->res, NICIPentry->netmask,
149801784826SJohnathan Mantey                             pathString + "/SubnetMask");
149901784826SJohnathan Mantey                         errorInEntry = true;
15004a0cb85cSEd Tanous                     }
15014a0cb85cSEd Tanous                 }
15021abe55efSEd Tanous                 else
15031abe55efSEd Tanous                 {
150401784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
150501784826SJohnathan Mantey                                               pathString + "/SubnetMask");
150601784826SJohnathan Mantey                     errorInEntry = true;
150701784826SJohnathan Mantey                 }
150801784826SJohnathan Mantey 
150901784826SJohnathan Mantey                 if (gateway)
151001784826SJohnathan Mantey                 {
151101784826SJohnathan Mantey                     if (ipv4VerifyIpAndGetBitcount(*gateway))
151201784826SJohnathan Mantey                     {
151301784826SJohnathan Mantey                         gw = &(*gateway);
151401784826SJohnathan Mantey                     }
151501784826SJohnathan Mantey                     else
151601784826SJohnathan Mantey                     {
151701784826SJohnathan Mantey                         messages::propertyValueFormatError(
151801784826SJohnathan Mantey                             asyncResp->res, *gateway, pathString + "/Gateway");
151901784826SJohnathan Mantey                         errorInEntry = true;
152001784826SJohnathan Mantey                     }
152101784826SJohnathan Mantey                 }
152201784826SJohnathan Mantey                 else if (NICIPentry != ipv4Data.cend())
152301784826SJohnathan Mantey                 {
152401784826SJohnathan Mantey                     gw = &NICIPentry->gateway;
152501784826SJohnathan Mantey                 }
152601784826SJohnathan Mantey                 else
15271abe55efSEd Tanous                 {
1528a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
15294a0cb85cSEd Tanous                                               pathString + "/Gateway");
153001784826SJohnathan Mantey                     errorInEntry = true;
15314a0cb85cSEd Tanous                 }
15324a0cb85cSEd Tanous 
153301784826SJohnathan Mantey                 if (errorInEntry)
15341abe55efSEd Tanous                 {
153501784826SJohnathan Mantey                     return;
15364a0cb85cSEd Tanous                 }
15374a0cb85cSEd Tanous 
153801784826SJohnathan Mantey                 if (NICIPentry != ipv4Data.cend())
15391abe55efSEd Tanous                 {
1540271584abSEd Tanous                     if (gw != nullptr || addr != nullptr)
1541271584abSEd Tanous                     {
1542271584abSEd Tanous                         // Shouldn't be possible based on errorInEntry, but
1543271584abSEd Tanous                         // it flags -wmaybe-uninitialized in the compiler,
1544271584abSEd Tanous                         // so defend against that
1545271584abSEd Tanous                         return;
1546271584abSEd Tanous                     }
154701784826SJohnathan Mantey                     deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
154801784826SJohnathan Mantey                                         *gw, *addr, asyncResp);
154901784826SJohnathan Mantey                     NICIPentry =
155001784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1551588c3f0dSKowalski, Kamil                 }
155201784826SJohnathan Mantey                 else
155301784826SJohnathan Mantey                 {
155401784826SJohnathan Mantey                     createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
155501784826SJohnathan Mantey                                *address, asyncResp);
15564a0cb85cSEd Tanous                 }
15574a0cb85cSEd Tanous                 entryIdx++;
15584a0cb85cSEd Tanous             }
155901784826SJohnathan Mantey             else
156001784826SJohnathan Mantey             {
156101784826SJohnathan Mantey                 if (NICIPentry == ipv4Data.cend())
156201784826SJohnathan Mantey                 {
156301784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
156401784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
156501784826SJohnathan Mantey                     // in error, so bail out.
156601784826SJohnathan Mantey                     if (thisJson.is_null())
156701784826SJohnathan Mantey                     {
156801784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
156901784826SJohnathan Mantey                         return;
157001784826SJohnathan Mantey                     }
157101784826SJohnathan Mantey                     else
157201784826SJohnathan Mantey                     {
157301784826SJohnathan Mantey                         messages::propertyValueFormatError(
157401784826SJohnathan Mantey                             asyncResp->res, thisJson.dump(), pathString);
157501784826SJohnathan Mantey                         return;
157601784826SJohnathan Mantey                     }
157701784826SJohnathan Mantey                 }
157801784826SJohnathan Mantey 
157901784826SJohnathan Mantey                 if (thisJson.is_null())
158001784826SJohnathan Mantey                 {
158101784826SJohnathan Mantey                     deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
158201784826SJohnathan Mantey                 }
158301784826SJohnathan Mantey                 if (NICIPentry != ipv4Data.cend())
158401784826SJohnathan Mantey                 {
158501784826SJohnathan Mantey                     NICIPentry =
158601784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
158701784826SJohnathan Mantey                 }
158801784826SJohnathan Mantey                 entryIdx++;
158901784826SJohnathan Mantey             }
159001784826SJohnathan Mantey         }
15914a0cb85cSEd Tanous     }
15924a0cb85cSEd Tanous 
1593f85837bfSRAJESWARAN THILLAIGOVINDAN     void handleStaticNameServersPatch(
1594f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::string &ifaceId,
1595f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::vector<std::string> &updatedStaticNameServers,
1596f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::shared_ptr<AsyncResp> &asyncResp)
1597f85837bfSRAJESWARAN THILLAIGOVINDAN     {
1598f85837bfSRAJESWARAN THILLAIGOVINDAN         crow::connections::systemBus->async_method_call(
1599286b9118SJohnathan Mantey             [asyncResp](const boost::system::error_code ec) {
1600f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (ec)
1601f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1602f85837bfSRAJESWARAN THILLAIGOVINDAN                     messages::internalError(asyncResp->res);
1603f85837bfSRAJESWARAN THILLAIGOVINDAN                     return;
1604f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1605f85837bfSRAJESWARAN THILLAIGOVINDAN             },
1606f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network",
1607f85837bfSRAJESWARAN THILLAIGOVINDAN             "/xyz/openbmc_project/network/" + ifaceId,
1608f85837bfSRAJESWARAN THILLAIGOVINDAN             "org.freedesktop.DBus.Properties", "Set",
1609f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1610f85837bfSRAJESWARAN THILLAIGOVINDAN             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1611f85837bfSRAJESWARAN THILLAIGOVINDAN     }
1612f85837bfSRAJESWARAN THILLAIGOVINDAN 
1613e48c0fc5SRavi Teja     void handleIPv6StaticAddressesPatch(
1614e48c0fc5SRavi Teja         const std::string &ifaceId, nlohmann::json &input,
161501784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data,
1616e48c0fc5SRavi Teja         const std::shared_ptr<AsyncResp> asyncResp)
1617e48c0fc5SRavi Teja     {
161801784826SJohnathan Mantey         if (!input.is_array() || input.empty())
1619e48c0fc5SRavi Teja         {
1620e48c0fc5SRavi Teja             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1621e48c0fc5SRavi Teja                                              "IPv6StaticAddresses");
1622e48c0fc5SRavi Teja             return;
1623e48c0fc5SRavi Teja         }
1624271584abSEd Tanous         size_t entryIdx = 1;
162501784826SJohnathan Mantey         boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
162601784826SJohnathan Mantey             GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
1627e48c0fc5SRavi Teja         for (nlohmann::json &thisJson : input)
1628e48c0fc5SRavi Teja         {
1629e48c0fc5SRavi Teja             std::string pathString =
1630e48c0fc5SRavi Teja                 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1631e48c0fc5SRavi Teja 
163201784826SJohnathan Mantey             if (!thisJson.is_null() && !thisJson.empty())
1633e48c0fc5SRavi Teja             {
1634e48c0fc5SRavi Teja                 std::optional<std::string> address;
1635e48c0fc5SRavi Teja                 std::optional<uint8_t> prefixLength;
1636e48c0fc5SRavi Teja 
1637e48c0fc5SRavi Teja                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1638e48c0fc5SRavi Teja                                          address, "PrefixLength", prefixLength))
1639e48c0fc5SRavi Teja                 {
164001784826SJohnathan Mantey                     messages::propertyValueFormatError(
164101784826SJohnathan Mantey                         asyncResp->res, thisJson.dump(), pathString);
1642e48c0fc5SRavi Teja                     return;
1643e48c0fc5SRavi Teja                 }
1644e48c0fc5SRavi Teja 
164501784826SJohnathan Mantey                 const std::string *addr;
164601784826SJohnathan Mantey                 uint8_t prefix;
164701784826SJohnathan Mantey 
164801784826SJohnathan Mantey                 // Find the address and prefixLength values. Any values that are
164901784826SJohnathan Mantey                 // not explicitly provided are assumed to be unmodified from the
165001784826SJohnathan Mantey                 // current state of the interface. Merge existing state into the
165101784826SJohnathan Mantey                 // current request.
1652e48c0fc5SRavi Teja                 if (address)
1653e48c0fc5SRavi Teja                 {
165401784826SJohnathan Mantey                     addr = &(*address);
1655e48c0fc5SRavi Teja                 }
165601784826SJohnathan Mantey                 else if (NICIPentry != ipv6Data.end())
165701784826SJohnathan Mantey                 {
165801784826SJohnathan Mantey                     addr = &(NICIPentry->address);
165901784826SJohnathan Mantey                 }
166001784826SJohnathan Mantey                 else
166101784826SJohnathan Mantey                 {
166201784826SJohnathan Mantey                     messages::propertyMissing(asyncResp->res,
166301784826SJohnathan Mantey                                               pathString + "/Address");
166401784826SJohnathan Mantey                     return;
1665e48c0fc5SRavi Teja                 }
1666e48c0fc5SRavi Teja 
1667e48c0fc5SRavi Teja                 if (prefixLength)
1668e48c0fc5SRavi Teja                 {
166901784826SJohnathan Mantey                     prefix = *prefixLength;
167001784826SJohnathan Mantey                 }
167101784826SJohnathan Mantey                 else if (NICIPentry != ipv6Data.end())
1672e48c0fc5SRavi Teja                 {
167301784826SJohnathan Mantey                     prefix = NICIPentry->prefixLength;
1674e48c0fc5SRavi Teja                 }
1675e48c0fc5SRavi Teja                 else
1676e48c0fc5SRavi Teja                 {
1677e48c0fc5SRavi Teja                     messages::propertyMissing(asyncResp->res,
1678e48c0fc5SRavi Teja                                               pathString + "/PrefixLength");
167901784826SJohnathan Mantey                     return;
1680e48c0fc5SRavi Teja                 }
1681e48c0fc5SRavi Teja 
168201784826SJohnathan Mantey                 if (NICIPentry != ipv6Data.end())
1683e48c0fc5SRavi Teja                 {
168401784826SJohnathan Mantey                     deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1685e48c0fc5SRavi Teja                                         asyncResp);
168601784826SJohnathan Mantey                     NICIPentry =
168701784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
168801784826SJohnathan Mantey                 }
168901784826SJohnathan Mantey                 else
169001784826SJohnathan Mantey                 {
169101784826SJohnathan Mantey                     createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1692e48c0fc5SRavi Teja                 }
1693e48c0fc5SRavi Teja                 entryIdx++;
1694e48c0fc5SRavi Teja             }
169501784826SJohnathan Mantey             else
169601784826SJohnathan Mantey             {
169701784826SJohnathan Mantey                 if (NICIPentry == ipv6Data.end())
169801784826SJohnathan Mantey                 {
169901784826SJohnathan Mantey                     // Requesting a DELETE/DO NOT MODIFY action for an item
170001784826SJohnathan Mantey                     // that isn't present on the eth(n) interface. Input JSON is
170101784826SJohnathan Mantey                     // in error, so bail out.
170201784826SJohnathan Mantey                     if (thisJson.is_null())
170301784826SJohnathan Mantey                     {
170401784826SJohnathan Mantey                         messages::resourceCannotBeDeleted(asyncResp->res);
170501784826SJohnathan Mantey                         return;
170601784826SJohnathan Mantey                     }
170701784826SJohnathan Mantey                     else
170801784826SJohnathan Mantey                     {
170901784826SJohnathan Mantey                         messages::propertyValueFormatError(
171001784826SJohnathan Mantey                             asyncResp->res, thisJson.dump(), pathString);
171101784826SJohnathan Mantey                         return;
171201784826SJohnathan Mantey                     }
171301784826SJohnathan Mantey                 }
171401784826SJohnathan Mantey 
171501784826SJohnathan Mantey                 if (thisJson.is_null())
171601784826SJohnathan Mantey                 {
171701784826SJohnathan Mantey                     deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
171801784826SJohnathan Mantey                 }
171901784826SJohnathan Mantey                 if (NICIPentry != ipv6Data.cend())
172001784826SJohnathan Mantey                 {
172101784826SJohnathan Mantey                     NICIPentry =
172201784826SJohnathan Mantey                         GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
172301784826SJohnathan Mantey                 }
172401784826SJohnathan Mantey                 entryIdx++;
172501784826SJohnathan Mantey             }
172601784826SJohnathan Mantey         }
1727e48c0fc5SRavi Teja     }
1728e48c0fc5SRavi Teja 
17290f74e643SEd Tanous     void parseInterfaceData(
1730eeedda23SJohnathan Mantey         std::shared_ptr<AsyncResp> asyncResp, const std::string &iface_id,
17310f74e643SEd Tanous         const EthernetInterfaceData &ethData,
1732e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
173301784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data)
17344a0cb85cSEd Tanous     {
1735eeedda23SJohnathan Mantey         constexpr const std::array<const char *, 1> inventoryForEthernet = {
1736eeedda23SJohnathan Mantey             "xyz.openbmc_project.Inventory.Item.Ethernet"};
1737eeedda23SJohnathan Mantey 
1738eeedda23SJohnathan Mantey         nlohmann::json &json_response = asyncResp->res.jsonValue;
17394a0cb85cSEd Tanous         json_response["Id"] = iface_id;
17404a0cb85cSEd Tanous         json_response["@odata.id"] =
17414a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
1742eeedda23SJohnathan Mantey         json_response["InterfaceEnabled"] = ethData.nicEnabled;
1743eeedda23SJohnathan Mantey 
1744eeedda23SJohnathan Mantey         auto health = std::make_shared<HealthPopulate>(asyncResp);
1745eeedda23SJohnathan Mantey 
1746eeedda23SJohnathan Mantey         crow::connections::systemBus->async_method_call(
1747eeedda23SJohnathan Mantey             [health](const boost::system::error_code ec,
1748eeedda23SJohnathan Mantey                      std::vector<std::string> &resp) {
1749eeedda23SJohnathan Mantey                 if (ec)
1750029573d4SEd Tanous                 {
1751eeedda23SJohnathan Mantey                     return;
1752eeedda23SJohnathan Mantey                 }
1753eeedda23SJohnathan Mantey 
1754eeedda23SJohnathan Mantey                 health->inventory = std::move(resp);
1755eeedda23SJohnathan Mantey             },
1756eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper",
1757eeedda23SJohnathan Mantey             "/xyz/openbmc_project/object_mapper",
1758eeedda23SJohnathan Mantey             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1759eeedda23SJohnathan Mantey             int32_t(0), inventoryForEthernet);
1760eeedda23SJohnathan Mantey 
1761eeedda23SJohnathan Mantey         health->populate();
1762eeedda23SJohnathan Mantey 
1763eeedda23SJohnathan Mantey         if (ethData.nicEnabled)
1764eeedda23SJohnathan Mantey         {
1765eeedda23SJohnathan Mantey             json_response["LinkStatus"] = "LinkUp";
1766eeedda23SJohnathan Mantey             json_response["Status"]["State"] = "Enabled";
1767029573d4SEd Tanous         }
1768029573d4SEd Tanous         else
1769029573d4SEd Tanous         {
1770eeedda23SJohnathan Mantey             json_response["LinkStatus"] = "NoLink";
1771eeedda23SJohnathan Mantey             json_response["Status"]["State"] = "Disabled";
1772029573d4SEd Tanous         }
1773aa05fb27SJohnathan Mantey 
1774aa05fb27SJohnathan Mantey         json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
17754a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
17764a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
17771f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["DHCPEnabled"] =
17781f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
17791f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
17801f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
17811f8c7b5dSJohnathan Mantey         json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
17821f8c7b5dSJohnathan Mantey 
17831f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["OperatingMode"] =
17841f8c7b5dSJohnathan Mantey             translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
17851f8c7b5dSJohnathan Mantey                                                                    : "Disabled";
17861f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
17871f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
17881f8c7b5dSJohnathan Mantey         json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
17892a133282Smanojkiraneda 
17904a0cb85cSEd Tanous         if (!ethData.hostname.empty())
17914a0cb85cSEd Tanous         {
17924a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
1793*ab6554f1SJoshi-Mansi 
1794*ab6554f1SJoshi-Mansi             // When domain name is empty then it means, that it is a network
1795*ab6554f1SJoshi-Mansi             // without domain names, and the host name itself must be treated as
1796*ab6554f1SJoshi-Mansi             // FQDN
1797*ab6554f1SJoshi-Mansi             std::string FQDN = std::move(ethData.hostname);
1798d24bfc7aSJennifer Lee             if (!ethData.domainnames.empty())
1799d24bfc7aSJennifer Lee             {
1800*ab6554f1SJoshi-Mansi                 FQDN += "." + ethData.domainnames[0];
1801d24bfc7aSJennifer Lee             }
1802*ab6554f1SJoshi-Mansi             json_response["FQDN"] = FQDN;
18034a0cb85cSEd Tanous         }
18044a0cb85cSEd Tanous 
1805fda13ad2SSunitha Harish         json_response["VLANs"] = {
1806fda13ad2SSunitha Harish             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1807fda13ad2SSunitha Harish                               iface_id + "/VLANs"}};
1808fda13ad2SSunitha Harish 
18091f8c7b5dSJohnathan Mantey         if (translateDHCPEnabledToBool(ethData.DHCPEnabled, true) &&
18101f8c7b5dSJohnathan Mantey             ethData.DNSEnabled)
181195f8646eSManojkiran Eda         {
18121f8c7b5dSJohnathan Mantey             json_response["StaticNameServers"] = nlohmann::json::array();
181395f8646eSManojkiran Eda         }
181495f8646eSManojkiran Eda         else
181595f8646eSManojkiran Eda         {
18161f8c7b5dSJohnathan Mantey             json_response["StaticNameServers"] = ethData.nameservers;
181795f8646eSManojkiran Eda         }
18184a0cb85cSEd Tanous 
18194a0cb85cSEd Tanous         nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
182001784826SJohnathan Mantey         nlohmann::json &ipv4_static_array =
182101784826SJohnathan Mantey             json_response["IPv4StaticAddresses"];
18224a0cb85cSEd Tanous         ipv4_array = nlohmann::json::array();
182301784826SJohnathan Mantey         ipv4_static_array = nlohmann::json::array();
18244a0cb85cSEd Tanous         for (auto &ipv4_config : ipv4Data)
18254a0cb85cSEd Tanous         {
1826fa5053a6SGunnar Mills 
1827fa5053a6SGunnar Mills             std::string gatewayStr = ipv4_config.gateway;
1828fa5053a6SGunnar Mills             if (gatewayStr.empty())
1829fa5053a6SGunnar Mills             {
1830fa5053a6SGunnar Mills                 gatewayStr = "0.0.0.0";
1831fa5053a6SGunnar Mills             }
1832fa5053a6SGunnar Mills 
18334a0cb85cSEd Tanous             ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
18344a0cb85cSEd Tanous                                   {"SubnetMask", ipv4_config.netmask},
1835029573d4SEd Tanous                                   {"Address", ipv4_config.address},
1836fa5053a6SGunnar Mills                                   {"Gateway", gatewayStr}});
183701784826SJohnathan Mantey             if (ipv4_config.origin == "Static")
1838d1d50814SRavi Teja             {
1839d1d50814SRavi Teja                 ipv4_static_array.push_back(
184001784826SJohnathan Mantey                     {{"AddressOrigin", ipv4_config.origin},
184101784826SJohnathan Mantey                      {"SubnetMask", ipv4_config.netmask},
184201784826SJohnathan Mantey                      {"Address", ipv4_config.address},
1843d1d50814SRavi Teja                      {"Gateway", gatewayStr}});
1844d1d50814SRavi Teja             }
184501784826SJohnathan Mantey         }
1846d1d50814SRavi Teja 
18479a6fc6feSRavi Teja         json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
1848e48c0fc5SRavi Teja 
1849e48c0fc5SRavi Teja         nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
185001784826SJohnathan Mantey         nlohmann::json &ipv6_static_array =
185101784826SJohnathan Mantey             json_response["IPv6StaticAddresses"];
1852e48c0fc5SRavi Teja         ipv6_array = nlohmann::json::array();
185301784826SJohnathan Mantey         ipv6_static_array = nlohmann::json::array();
1854e48c0fc5SRavi Teja         for (auto &ipv6_config : ipv6Data)
1855e48c0fc5SRavi Teja         {
1856e48c0fc5SRavi Teja             ipv6_array.push_back({{"Address", ipv6_config.address},
1857e48c0fc5SRavi Teja                                   {"PrefixLength", ipv6_config.prefixLength},
1858e48c0fc5SRavi Teja                                   {"AddressOrigin", ipv6_config.origin}});
185901784826SJohnathan Mantey             if (ipv6_config.origin == "Static")
1860e48c0fc5SRavi Teja             {
1861e48c0fc5SRavi Teja                 ipv6_static_array.push_back(
186201784826SJohnathan Mantey                     {{"Address", ipv6_config.address},
186301784826SJohnathan Mantey                      {"PrefixLength", ipv6_config.prefixLength},
186401784826SJohnathan Mantey                      {"AddressOrigin", ipv6_config.origin}});
186501784826SJohnathan Mantey             }
1866e48c0fc5SRavi Teja         }
1867588c3f0dSKowalski, Kamil     }
1868588c3f0dSKowalski, Kamil 
18699391bb9cSRapkiewicz, Pawel     /**
18709391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
18719391bb9cSRapkiewicz, Pawel      */
187255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
18731abe55efSEd Tanous                const std::vector<std::string> &params) override
18741abe55efSEd Tanous     {
18754a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
18761abe55efSEd Tanous         if (params.size() != 1)
18771abe55efSEd Tanous         {
1878f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
18799391bb9cSRapkiewicz, Pawel             return;
18809391bb9cSRapkiewicz, Pawel         }
18819391bb9cSRapkiewicz, Pawel 
18824a0cb85cSEd Tanous         getEthernetIfaceData(
18834a0cb85cSEd Tanous             params[0],
18844a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
18854a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
1886e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
188701784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
18884a0cb85cSEd Tanous                 if (!success)
18891abe55efSEd Tanous                 {
18901abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
18911abe55efSEd Tanous                     // object, and other errors
1892f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1893f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
18944a0cb85cSEd Tanous                     return;
18959391bb9cSRapkiewicz, Pawel                 }
18964c9afe43SEd Tanous 
18970f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1898fda13ad2SSunitha Harish                     "#EthernetInterface.v1_4_1.EthernetInterface";
18990f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
19000f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
19010f74e643SEd Tanous                     "Management Network Interface";
19020f74e643SEd Tanous 
1903eeedda23SJohnathan Mantey                 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1904eeedda23SJohnathan Mantey                                    ipv6Data);
19059391bb9cSRapkiewicz, Pawel             });
19069391bb9cSRapkiewicz, Pawel     }
19079391bb9cSRapkiewicz, Pawel 
190855c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
19091abe55efSEd Tanous                  const std::vector<std::string> &params) override
19101abe55efSEd Tanous     {
19114a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
19121abe55efSEd Tanous         if (params.size() != 1)
19131abe55efSEd Tanous         {
1914f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1915588c3f0dSKowalski, Kamil             return;
1916588c3f0dSKowalski, Kamil         }
1917588c3f0dSKowalski, Kamil 
19184a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1919588c3f0dSKowalski, Kamil 
1920bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1921*ab6554f1SJoshi-Mansi         std::optional<std::string> fqdn;
1922d577665bSRatan Gupta         std::optional<std::string> macAddress;
19239a6fc6feSRavi Teja         std::optional<std::string> ipv6DefaultGateway;
1924d1d50814SRavi Teja         std::optional<nlohmann::json> ipv4StaticAddresses;
1925e48c0fc5SRavi Teja         std::optional<nlohmann::json> ipv6StaticAddresses;
1926f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
1927da131a9aSJennifer Lee         std::optional<nlohmann::json> dhcpv4;
19281f8c7b5dSJohnathan Mantey         std::optional<nlohmann::json> dhcpv6;
1929eeedda23SJohnathan Mantey         std::optional<bool> interfaceEnabled;
19301f8c7b5dSJohnathan Mantey         DHCPParameters v4dhcpParms;
19311f8c7b5dSJohnathan Mantey         DHCPParameters v6dhcpParms;
19320627a2c7SEd Tanous 
19331f8c7b5dSJohnathan Mantey         if (!json_util::readJson(
1934*ab6554f1SJoshi-Mansi                 req, res, "HostName", hostname, "FQDN", fqdn,
1935*ab6554f1SJoshi-Mansi                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1936*ab6554f1SJoshi-Mansi                 macAddress, "StaticNameServers", staticNameServers,
1937*ab6554f1SJoshi-Mansi                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1938*ab6554f1SJoshi-Mansi                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1939*ab6554f1SJoshi-Mansi                 "InterfaceEnabled", interfaceEnabled))
19401abe55efSEd Tanous         {
1941588c3f0dSKowalski, Kamil             return;
1942588c3f0dSKowalski, Kamil         }
1943da131a9aSJennifer Lee         if (dhcpv4)
1944da131a9aSJennifer Lee         {
19451f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
19461f8c7b5dSJohnathan Mantey                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
19471f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useDNSServers, "UseNTPServers",
19481f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useNTPServers, "UseDomainName",
19491f8c7b5dSJohnathan Mantey                                      v4dhcpParms.useUseDomainName))
19501f8c7b5dSJohnathan Mantey             {
19511f8c7b5dSJohnathan Mantey                 return;
19521f8c7b5dSJohnathan Mantey             }
19531f8c7b5dSJohnathan Mantey         }
19541f8c7b5dSJohnathan Mantey 
19551f8c7b5dSJohnathan Mantey         if (dhcpv6)
19561f8c7b5dSJohnathan Mantey         {
19571f8c7b5dSJohnathan Mantey             if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
19581f8c7b5dSJohnathan Mantey                                      v6dhcpParms.dhcpv6OperatingMode,
19591f8c7b5dSJohnathan Mantey                                      "UseDNSServers", v6dhcpParms.useDNSServers,
19601f8c7b5dSJohnathan Mantey                                      "UseNTPServers", v6dhcpParms.useNTPServers,
19611f8c7b5dSJohnathan Mantey                                      "UseDomainName",
19621f8c7b5dSJohnathan Mantey                                      v6dhcpParms.useUseDomainName))
19631f8c7b5dSJohnathan Mantey             {
19641f8c7b5dSJohnathan Mantey                 return;
19651f8c7b5dSJohnathan Mantey             }
1966da131a9aSJennifer Lee         }
1967da131a9aSJennifer Lee 
196801784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
196901784826SJohnathan Mantey         // JSON preparation
19704a0cb85cSEd Tanous         getEthernetIfaceData(
19714a0cb85cSEd Tanous             iface_id,
1972fda13ad2SSunitha Harish             [this, asyncResp, iface_id, hostname = std::move(hostname),
1973*ab6554f1SJoshi-Mansi              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1974d1d50814SRavi Teja              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
19759a6fc6feSRavi Teja              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1976e48c0fc5SRavi Teja              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
19771f8c7b5dSJohnathan Mantey              staticNameServers = std::move(staticNameServers),
19781f8c7b5dSJohnathan Mantey              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
19791f8c7b5dSJohnathan Mantey              v4dhcpParms = std::move(v4dhcpParms),
1980eeedda23SJohnathan Mantey              v6dhcpParms = std::move(v6dhcpParms),
1981eeedda23SJohnathan Mantey              interfaceEnabled = std::move(interfaceEnabled)](
19824a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
1983e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
198401784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
19851abe55efSEd Tanous                 if (!success)
19861abe55efSEd Tanous                 {
1987588c3f0dSKowalski, Kamil                     // ... otherwise return error
19881abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
19891abe55efSEd Tanous                     // object, and other errors
1990fda13ad2SSunitha Harish                     messages::resourceNotFound(asyncResp->res,
1991fda13ad2SSunitha Harish                                                "Ethernet Interface", iface_id);
1992588c3f0dSKowalski, Kamil                     return;
1993588c3f0dSKowalski, Kamil                 }
1994588c3f0dSKowalski, Kamil 
19951f8c7b5dSJohnathan Mantey                 if (dhcpv4 || dhcpv6)
19961f8c7b5dSJohnathan Mantey                 {
19971f8c7b5dSJohnathan Mantey                     handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
19981f8c7b5dSJohnathan Mantey                                     std::move(v6dhcpParms), asyncResp);
19991f8c7b5dSJohnathan Mantey                 }
20001f8c7b5dSJohnathan Mantey 
20010627a2c7SEd Tanous                 if (hostname)
20021abe55efSEd Tanous                 {
20030627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
20041abe55efSEd Tanous                 }
20050627a2c7SEd Tanous 
2006*ab6554f1SJoshi-Mansi                 if (fqdn)
2007*ab6554f1SJoshi-Mansi                 {
2008*ab6554f1SJoshi-Mansi                     handleFqdnPatch(iface_id, *fqdn, asyncResp);
2009*ab6554f1SJoshi-Mansi                 }
2010*ab6554f1SJoshi-Mansi 
2011d577665bSRatan Gupta                 if (macAddress)
2012d577665bSRatan Gupta                 {
2013d577665bSRatan Gupta                     handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2014d577665bSRatan Gupta                 }
2015d577665bSRatan Gupta 
2016d1d50814SRavi Teja                 if (ipv4StaticAddresses)
2017d1d50814SRavi Teja                 {
2018537174c4SEd Tanous                     // TODO(ed) for some reason the capture of ipv4Addresses
201901784826SJohnathan Mantey                     // above is returning a const value, not a non-const
202001784826SJohnathan Mantey                     // value. This doesn't really work for us, as we need to
202101784826SJohnathan Mantey                     // be able to efficiently move out the intermedia
202201784826SJohnathan Mantey                     // nlohmann::json objects. This makes a copy of the
202301784826SJohnathan Mantey                     // structure, and operates on that, but could be done
202401784826SJohnathan Mantey                     // more efficiently
2025d1d50814SRavi Teja                     nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
202601784826SJohnathan Mantey                     handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
2027d1d50814SRavi Teja                                           asyncResp);
20281abe55efSEd Tanous                 }
20290627a2c7SEd Tanous 
2030f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (staticNameServers)
2031f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
2032f85837bfSRAJESWARAN THILLAIGOVINDAN                     handleStaticNameServersPatch(iface_id, *staticNameServers,
2033f85837bfSRAJESWARAN THILLAIGOVINDAN                                                  asyncResp);
2034f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
20359a6fc6feSRavi Teja 
20369a6fc6feSRavi Teja                 if (ipv6DefaultGateway)
20379a6fc6feSRavi Teja                 {
20389a6fc6feSRavi Teja                     messages::propertyNotWritable(asyncResp->res,
20399a6fc6feSRavi Teja                                                   "IPv6DefaultGateway");
20409a6fc6feSRavi Teja                 }
2041e48c0fc5SRavi Teja 
2042e48c0fc5SRavi Teja                 if (ipv6StaticAddresses)
2043e48c0fc5SRavi Teja                 {
2044e48c0fc5SRavi Teja                     nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2045e48c0fc5SRavi Teja                     handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
204601784826SJohnathan Mantey                                                    ipv6Data, asyncResp);
2047e48c0fc5SRavi Teja                 }
2048eeedda23SJohnathan Mantey 
2049eeedda23SJohnathan Mantey                 if (interfaceEnabled)
2050eeedda23SJohnathan Mantey                 {
2051eeedda23SJohnathan Mantey                     setEthernetInterfaceBoolProperty(
2052eeedda23SJohnathan Mantey                         iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2053eeedda23SJohnathan Mantey                 }
2054588c3f0dSKowalski, Kamil             });
2055588c3f0dSKowalski, Kamil     }
20569391bb9cSRapkiewicz, Pawel };
20579391bb9cSRapkiewicz, Pawel 
2058e439f0f8SKowalski, Kamil /**
20594a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
20604a0cb85cSEd Tanous  * Schema
2061e439f0f8SKowalski, Kamil  */
20621abe55efSEd Tanous class VlanNetworkInterface : public Node
20631abe55efSEd Tanous {
2064e439f0f8SKowalski, Kamil   public:
2065e439f0f8SKowalski, Kamil     /*
2066e439f0f8SKowalski, Kamil      * Default Constructor
2067e439f0f8SKowalski, Kamil      */
2068e439f0f8SKowalski, Kamil     template <typename CrowApp>
20691abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
20704a0cb85cSEd Tanous         Node(app,
20710f74e643SEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
20721abe55efSEd Tanous              std::string(), std::string())
20731abe55efSEd Tanous     {
2074e439f0f8SKowalski, Kamil         entityPrivileges = {
2075e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2076e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2077e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2078e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2079e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2081e439f0f8SKowalski, Kamil     }
2082e439f0f8SKowalski, Kamil 
2083e439f0f8SKowalski, Kamil   private:
20840f74e643SEd Tanous     void parseInterfaceData(
20850f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
20860f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
2087e48c0fc5SRavi Teja         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
208801784826SJohnathan Mantey         const boost::container::flat_set<IPv6AddressData> &ipv6Data)
20891abe55efSEd Tanous     {
2090e439f0f8SKowalski, Kamil         // Fill out obvious data...
20914a0cb85cSEd Tanous         json_response["Id"] = iface_id;
20924a0cb85cSEd Tanous         json_response["@odata.id"] =
20934a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
20944a0cb85cSEd Tanous             "/VLANs/" + iface_id;
2095e439f0f8SKowalski, Kamil 
20964a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
2097fda13ad2SSunitha Harish         if (!ethData.vlan_id.empty())
20984a0cb85cSEd Tanous         {
2099fda13ad2SSunitha Harish             json_response["VLANId"] = ethData.vlan_id.back();
21004a0cb85cSEd Tanous         }
2101e439f0f8SKowalski, Kamil     }
2102e439f0f8SKowalski, Kamil 
2103fda13ad2SSunitha Harish     bool verifyNames(const std::string &parent, const std::string &iface)
21041abe55efSEd Tanous     {
21051abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
21061abe55efSEd Tanous         {
2107927a505aSKowalski, Kamil             return false;
21081abe55efSEd Tanous         }
21091abe55efSEd Tanous         else
21101abe55efSEd Tanous         {
2111927a505aSKowalski, Kamil             return true;
2112927a505aSKowalski, Kamil         }
2113927a505aSKowalski, Kamil     }
2114927a505aSKowalski, Kamil 
2115e439f0f8SKowalski, Kamil     /**
2116e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2117e439f0f8SKowalski, Kamil      */
211855c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
21191abe55efSEd Tanous                const std::vector<std::string> &params) override
21201abe55efSEd Tanous     {
21214a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21224a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
2123e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2124e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
2125e439f0f8SKowalski, Kamil         // impossible.
21261abe55efSEd Tanous         if (params.size() != 2)
21271abe55efSEd Tanous         {
2128f12894f8SJason M. Bills             messages::internalError(res);
2129e439f0f8SKowalski, Kamil             res.end();
2130e439f0f8SKowalski, Kamil             return;
2131e439f0f8SKowalski, Kamil         }
2132e439f0f8SKowalski, Kamil 
21334a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
21344a0cb85cSEd Tanous         const std::string &iface_id = params[1];
21350f74e643SEd Tanous         res.jsonValue["@odata.type"] =
21360f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
21370f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
2138e439f0f8SKowalski, Kamil 
2139fda13ad2SSunitha Harish         if (!verifyNames(parent_iface_id, iface_id))
21401abe55efSEd Tanous         {
2141a434f2bdSEd Tanous             return;
2142a434f2bdSEd Tanous         }
2143a434f2bdSEd Tanous 
214401784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
214501784826SJohnathan Mantey         // JSON preparation
21464a0cb85cSEd Tanous         getEthernetIfaceData(
2147fda13ad2SSunitha Harish             params[1],
2148fda13ad2SSunitha Harish             [this, asyncResp, parent_iface_id{std::string(params[0])},
2149fda13ad2SSunitha Harish              iface_id{std::string(params[1])}](
21504a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
2151e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
215201784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
2153fda13ad2SSunitha Harish                 if (success && ethData.vlan_id.size() != 0)
21541abe55efSEd Tanous                 {
21550f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
21560f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
215701784826SJohnathan Mantey                                        ipv4Data, ipv6Data);
21581abe55efSEd Tanous                 }
21591abe55efSEd Tanous                 else
21601abe55efSEd Tanous                 {
2161e439f0f8SKowalski, Kamil                     // ... otherwise return error
21621abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
21631abe55efSEd Tanous                     // object, and other errors
2164f12894f8SJason M. Bills                     messages::resourceNotFound(
2165f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
2166e439f0f8SKowalski, Kamil                 }
2167e439f0f8SKowalski, Kamil             });
2168e439f0f8SKowalski, Kamil     }
2169e439f0f8SKowalski, Kamil 
217055c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
21711abe55efSEd Tanous                  const std::vector<std::string> &params) override
21721abe55efSEd Tanous     {
21734a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
21741abe55efSEd Tanous         if (params.size() != 2)
21751abe55efSEd Tanous         {
2176f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2177e439f0f8SKowalski, Kamil             return;
2178e439f0f8SKowalski, Kamil         }
2179e439f0f8SKowalski, Kamil 
2180d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
218155c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
2182927a505aSKowalski, Kamil 
2183fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
21841abe55efSEd Tanous         {
2185fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2186fda13ad2SSunitha Harish                                        ifaceId);
2187927a505aSKowalski, Kamil             return;
2188927a505aSKowalski, Kamil         }
2189927a505aSKowalski, Kamil 
21900627a2c7SEd Tanous         bool vlanEnable = false;
21910627a2c7SEd Tanous         uint64_t vlanId = 0;
21920627a2c7SEd Tanous 
21930627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
21940627a2c7SEd Tanous                                  vlanId))
21951abe55efSEd Tanous         {
2196927a505aSKowalski, Kamil             return;
2197927a505aSKowalski, Kamil         }
2198927a505aSKowalski, Kamil 
219901784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
220001784826SJohnathan Mantey         // JSON preparation
2201e48c0fc5SRavi Teja         getEthernetIfaceData(
2202e48c0fc5SRavi Teja             params[1],
2203271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2204e48c0fc5SRavi Teja              ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2205e48c0fc5SRavi Teja                 const bool &success, const EthernetInterfaceData &ethData,
2206e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
220701784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
220808244d02SSunitha Harish                 if (success && !ethData.vlan_id.empty())
220908244d02SSunitha Harish                 {
221008244d02SSunitha Harish                     auto callback =
221108244d02SSunitha Harish                         [asyncResp](const boost::system::error_code ec) {
221208244d02SSunitha Harish                             if (ec)
221308244d02SSunitha Harish                             {
221408244d02SSunitha Harish                                 messages::internalError(asyncResp->res);
221508244d02SSunitha Harish                             }
221608244d02SSunitha Harish                         };
221708244d02SSunitha Harish 
221808244d02SSunitha Harish                     if (vlanEnable == true)
221908244d02SSunitha Harish                     {
222008244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
222108244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
222208244d02SSunitha Harish                             "/xyz/openbmc_project/network/" + ifaceId,
222308244d02SSunitha Harish                             "org.freedesktop.DBus.Properties", "Set",
222408244d02SSunitha Harish                             "xyz.openbmc_project.Network.VLAN", "Id",
222508244d02SSunitha Harish                             std::variant<uint32_t>(vlanId));
222608244d02SSunitha Harish                     }
222708244d02SSunitha Harish                     else
222808244d02SSunitha Harish                     {
2229e48c0fc5SRavi Teja                         BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2230e48c0fc5SRavi Teja                                             "vlan interface";
223108244d02SSunitha Harish                         crow::connections::systemBus->async_method_call(
223208244d02SSunitha Harish                             std::move(callback), "xyz.openbmc_project.Network",
2233e48c0fc5SRavi Teja                             std::string("/xyz/openbmc_project/network/") +
2234e48c0fc5SRavi Teja                                 ifaceId,
223508244d02SSunitha Harish                             "xyz.openbmc_project.Object.Delete", "Delete");
223608244d02SSunitha Harish                     }
223708244d02SSunitha Harish                 }
223808244d02SSunitha Harish                 else
22391abe55efSEd Tanous                 {
22401abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
22411abe55efSEd Tanous                     // object, and other errors
2242e48c0fc5SRavi Teja                     messages::resourceNotFound(
2243e48c0fc5SRavi Teja                         asyncResp->res, "VLAN Network Interface", ifaceId);
2244927a505aSKowalski, Kamil                     return;
2245927a505aSKowalski, Kamil                 }
2246927a505aSKowalski, Kamil             });
2247e439f0f8SKowalski, Kamil     }
2248e439f0f8SKowalski, Kamil 
224955c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
22501abe55efSEd Tanous                   const std::vector<std::string> &params) override
22511abe55efSEd Tanous     {
22524a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
22531abe55efSEd Tanous         if (params.size() != 2)
22541abe55efSEd Tanous         {
2255f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2256e439f0f8SKowalski, Kamil             return;
2257e439f0f8SKowalski, Kamil         }
2258e439f0f8SKowalski, Kamil 
2259d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
226055c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
2261927a505aSKowalski, Kamil 
2262fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
22631abe55efSEd Tanous         {
2264fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2265fda13ad2SSunitha Harish                                        ifaceId);
2266927a505aSKowalski, Kamil             return;
2267927a505aSKowalski, Kamil         }
2268927a505aSKowalski, Kamil 
226901784826SJohnathan Mantey         // Get single eth interface data, and call the below callback for
227001784826SJohnathan Mantey         // JSON preparation
2271f12894f8SJason M. Bills         getEthernetIfaceData(
2272fda13ad2SSunitha Harish             params[1],
2273271584abSEd Tanous             [asyncResp, parentIfaceId{std::string(params[0])},
2274fda13ad2SSunitha Harish              ifaceId{std::string(params[1])}](
2275f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
2276e48c0fc5SRavi Teja                 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
227701784826SJohnathan Mantey                 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
2278fda13ad2SSunitha Harish                 if (success && !ethData.vlan_id.empty())
22791abe55efSEd Tanous                 {
2280f12894f8SJason M. Bills                     auto callback =
2281f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
22821abe55efSEd Tanous                             if (ec)
22831abe55efSEd Tanous                             {
2284f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
2285927a505aSKowalski, Kamil                             }
22864a0cb85cSEd Tanous                         };
22874a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
22884a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
22894a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
22904a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
22911abe55efSEd Tanous                 }
22921abe55efSEd Tanous                 else
22931abe55efSEd Tanous                 {
2294927a505aSKowalski, Kamil                     // ... otherwise return error
2295f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
2296f12894f8SJason M. Bills                     // object, and other errors
2297f12894f8SJason M. Bills                     messages::resourceNotFound(
2298f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
2299927a505aSKowalski, Kamil                 }
2300927a505aSKowalski, Kamil             });
2301e439f0f8SKowalski, Kamil     }
2302e439f0f8SKowalski, Kamil };
2303e439f0f8SKowalski, Kamil 
2304e439f0f8SKowalski, Kamil /**
2305e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
2306e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
2307e439f0f8SKowalski, Kamil  */
23081abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
23091abe55efSEd Tanous {
2310e439f0f8SKowalski, Kamil   public:
2311e439f0f8SKowalski, Kamil     template <typename CrowApp>
23121abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
23134a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
23144a0cb85cSEd Tanous              std::string())
23151abe55efSEd Tanous     {
2316e439f0f8SKowalski, Kamil         entityPrivileges = {
2317e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
2318e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
2319e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2320e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2321e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2322e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2323e439f0f8SKowalski, Kamil     }
2324e439f0f8SKowalski, Kamil 
2325e439f0f8SKowalski, Kamil   private:
2326e439f0f8SKowalski, Kamil     /**
2327e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
2328e439f0f8SKowalski, Kamil      */
232955c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
23301abe55efSEd Tanous                const std::vector<std::string> &params) override
23311abe55efSEd Tanous     {
23324a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23331abe55efSEd Tanous         if (params.size() != 1)
23341abe55efSEd Tanous         {
2335e439f0f8SKowalski, Kamil             // This means there is a problem with the router
2336f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2337e439f0f8SKowalski, Kamil             return;
2338e439f0f8SKowalski, Kamil         }
2339e439f0f8SKowalski, Kamil 
23404a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
2341e439f0f8SKowalski, Kamil 
23424a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
23431abe55efSEd Tanous         // preparation
2344f12894f8SJason M. Bills         getEthernetIfaceList(
234543b761d0SEd Tanous             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
23461abe55efSEd Tanous                 const bool &success,
23474c9afe43SEd Tanous                 const boost::container::flat_set<std::string> &iface_list) {
23484a0cb85cSEd Tanous                 if (!success)
23491abe55efSEd Tanous                 {
2350f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
23514a0cb85cSEd Tanous                     return;
23521abe55efSEd Tanous                 }
23534c9afe43SEd Tanous 
23544c9afe43SEd Tanous                 if (iface_list.find(rootInterfaceName) == iface_list.end())
23554c9afe43SEd Tanous                 {
23564c9afe43SEd Tanous                     messages::resourceNotFound(asyncResp->res,
23574c9afe43SEd Tanous                                                "VLanNetworkInterfaceCollection",
23584c9afe43SEd Tanous                                                rootInterfaceName);
23594c9afe43SEd Tanous                     return;
23604c9afe43SEd Tanous                 }
23614c9afe43SEd Tanous 
23620f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
23630f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
23640f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
23650f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
23660f74e643SEd Tanous                     "VLAN Network Interface Collection";
23674a0cb85cSEd Tanous 
23684a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
23694a0cb85cSEd Tanous 
23704a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
23711abe55efSEd Tanous                 {
23724a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
23734a0cb85cSEd Tanous                     {
23744a0cb85cSEd Tanous                         iface_array.push_back(
23754a0cb85cSEd Tanous                             {{"@odata.id",
23764a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23774a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
2378e439f0f8SKowalski, Kamil                     }
2379e439f0f8SKowalski, Kamil                 }
2380e439f0f8SKowalski, Kamil 
23814a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
23824a0cb85cSEd Tanous                     iface_array.size();
23834a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
23844a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
23854a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
23864a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
2387e439f0f8SKowalski, Kamil             });
2388e439f0f8SKowalski, Kamil     }
2389e439f0f8SKowalski, Kamil 
239055c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
23911abe55efSEd Tanous                 const std::vector<std::string> &params) override
23921abe55efSEd Tanous     {
23934a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
23941abe55efSEd Tanous         if (params.size() != 1)
23951abe55efSEd Tanous         {
2396f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
2397e439f0f8SKowalski, Kamil             return;
2398e439f0f8SKowalski, Kamil         }
2399fda13ad2SSunitha Harish         bool vlanEnable = false;
24000627a2c7SEd Tanous         uint32_t vlanId = 0;
2401fda13ad2SSunitha Harish         if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2402fda13ad2SSunitha Harish                                  vlanEnable))
24031abe55efSEd Tanous         {
24044a0cb85cSEd Tanous             return;
2405e439f0f8SKowalski, Kamil         }
2406fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
2407fda13ad2SSunitha Harish         if (!vlanId)
2408fda13ad2SSunitha Harish         {
2409fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
2410fda13ad2SSunitha Harish         }
2411fda13ad2SSunitha Harish         if (!vlanEnable)
2412fda13ad2SSunitha Harish         {
2413fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
2414fda13ad2SSunitha Harish         }
2415271584abSEd Tanous         if (static_cast<bool>(vlanId) ^ vlanEnable)
2416fda13ad2SSunitha Harish         {
2417fda13ad2SSunitha Harish             return;
2418fda13ad2SSunitha Harish         }
2419fda13ad2SSunitha Harish 
24204a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
24214a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
24221abe55efSEd Tanous             if (ec)
24231abe55efSEd Tanous             {
24244a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
24254a0cb85cSEd Tanous                 // phosphor-network responses
2426f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
24274a0cb85cSEd Tanous                 return;
24281abe55efSEd Tanous             }
2429f12894f8SJason M. Bills             messages::created(asyncResp->res);
2430e439f0f8SKowalski, Kamil         };
24314a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
24324a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
24334a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
24344a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
24350627a2c7SEd Tanous             rootInterfaceName, vlanId);
24364a0cb85cSEd Tanous     }
24374a0cb85cSEd Tanous };
24389391bb9cSRapkiewicz, Pawel } // namespace redfish
2439