xref: /openbmc/bmcweb/redfish-core/lib/ethernet.hpp (revision fa5053a6b1f1cc821e97a84b49a4f82adf580594)
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>
24588c3f0dSKowalski, Kamil #include <utils/json_utils.hpp>
25abf2add6SEd Tanous #include <variant>
269391bb9cSRapkiewicz, Pawel 
271abe55efSEd Tanous namespace redfish
281abe55efSEd Tanous {
299391bb9cSRapkiewicz, Pawel 
309391bb9cSRapkiewicz, Pawel /**
319391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
329391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
339391bb9cSRapkiewicz, Pawel  */
34aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
35abf2add6SEd Tanous     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
36aa2e59c1SEd Tanous                               int32_t, uint32_t, int64_t, uint64_t, double>>;
379391bb9cSRapkiewicz, Pawel 
384a0cb85cSEd Tanous using GetManagedObjects = std::vector<std::pair<
39aa2e59c1SEd Tanous     sdbusplus::message::object_path,
404a0cb85cSEd Tanous     std::vector<std::pair<
41aa2e59c1SEd Tanous         std::string,
42aa2e59c1SEd Tanous         boost::container::flat_map<
43029573d4SEd Tanous             std::string, sdbusplus::message::variant<
44029573d4SEd Tanous                              std::string, bool, uint8_t, int16_t, uint16_t,
45029573d4SEd Tanous                              int32_t, uint32_t, int64_t, uint64_t, double,
46029573d4SEd Tanous                              std::vector<std::string>>>>>>>;
474a0cb85cSEd Tanous 
484a0cb85cSEd Tanous enum class LinkType
494a0cb85cSEd Tanous {
504a0cb85cSEd Tanous     Local,
514a0cb85cSEd Tanous     Global
524a0cb85cSEd Tanous };
539391bb9cSRapkiewicz, Pawel 
549391bb9cSRapkiewicz, Pawel /**
559391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
569391bb9cSRapkiewicz, Pawel  */
571abe55efSEd Tanous struct IPv4AddressData
581abe55efSEd Tanous {
59179db1d7SKowalski, Kamil     std::string id;
604a0cb85cSEd Tanous     std::string address;
614a0cb85cSEd Tanous     std::string domain;
624a0cb85cSEd Tanous     std::string gateway;
639391bb9cSRapkiewicz, Pawel     std::string netmask;
649391bb9cSRapkiewicz, Pawel     std::string origin;
654a0cb85cSEd Tanous     LinkType linktype;
664a0cb85cSEd Tanous 
671abe55efSEd Tanous     bool operator<(const IPv4AddressData &obj) const
681abe55efSEd Tanous     {
694a0cb85cSEd Tanous         return id < obj.id;
701abe55efSEd Tanous     }
719391bb9cSRapkiewicz, Pawel };
729391bb9cSRapkiewicz, Pawel 
739391bb9cSRapkiewicz, Pawel /**
749391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
759391bb9cSRapkiewicz, Pawel  * available from DBus
769391bb9cSRapkiewicz, Pawel  */
771abe55efSEd Tanous struct EthernetInterfaceData
781abe55efSEd Tanous {
794a0cb85cSEd Tanous     uint32_t speed;
804a0cb85cSEd Tanous     bool auto_neg;
812a133282Smanojkiraneda     bool DHCPEnabled;
824a0cb85cSEd Tanous     std::string hostname;
834a0cb85cSEd Tanous     std::string default_gateway;
844a0cb85cSEd Tanous     std::string mac_address;
85fda13ad2SSunitha Harish     std::vector<std::uint32_t> vlan_id;
86029573d4SEd Tanous     std::vector<std::string> nameservers;
879391bb9cSRapkiewicz, Pawel };
889391bb9cSRapkiewicz, Pawel 
899391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
909391bb9cSRapkiewicz, Pawel // into full dot notation
911abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
921abe55efSEd Tanous {
939391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
949391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
959391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
969391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
979391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
989391bb9cSRapkiewicz, Pawel     return netmask;
999391bb9cSRapkiewicz, Pawel }
1009391bb9cSRapkiewicz, Pawel 
1014a0cb85cSEd Tanous inline std::string
1024a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
1034a0cb85cSEd Tanous                                         bool isIPv4)
1041abe55efSEd Tanous {
1054a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1061abe55efSEd Tanous     {
1074a0cb85cSEd Tanous         return "Static";
1089391bb9cSRapkiewicz, Pawel     }
1094a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1101abe55efSEd Tanous     {
1114a0cb85cSEd Tanous         if (isIPv4)
1121abe55efSEd Tanous         {
1134a0cb85cSEd Tanous             return "IPv4LinkLocal";
1141abe55efSEd Tanous         }
1151abe55efSEd Tanous         else
1161abe55efSEd Tanous         {
1174a0cb85cSEd Tanous             return "LinkLocal";
1189391bb9cSRapkiewicz, Pawel         }
1199391bb9cSRapkiewicz, Pawel     }
1204a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1211abe55efSEd Tanous     {
1224a0cb85cSEd Tanous         if (isIPv4)
1234a0cb85cSEd Tanous         {
1244a0cb85cSEd Tanous             return "DHCP";
1254a0cb85cSEd Tanous         }
1264a0cb85cSEd Tanous         else
1274a0cb85cSEd Tanous         {
1284a0cb85cSEd Tanous             return "DHCPv6";
1294a0cb85cSEd Tanous         }
1304a0cb85cSEd Tanous     }
1314a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1324a0cb85cSEd Tanous     {
1334a0cb85cSEd Tanous         return "SLAAC";
1344a0cb85cSEd Tanous     }
1354a0cb85cSEd Tanous     return "";
1364a0cb85cSEd Tanous }
1374a0cb85cSEd Tanous 
1384a0cb85cSEd Tanous inline std::string
1394a0cb85cSEd Tanous     translateAddressOriginRedfishToDbus(const std::string &inputOrigin)
1404a0cb85cSEd Tanous {
1414a0cb85cSEd Tanous     if (inputOrigin == "Static")
1424a0cb85cSEd Tanous     {
1434a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
1444a0cb85cSEd Tanous     }
1454a0cb85cSEd Tanous     if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6")
1464a0cb85cSEd Tanous     {
1474a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
1484a0cb85cSEd Tanous     }
1494a0cb85cSEd Tanous     if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal")
1504a0cb85cSEd Tanous     {
1514a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal";
1524a0cb85cSEd Tanous     }
1534a0cb85cSEd Tanous     if (inputOrigin == "SLAAC")
1544a0cb85cSEd Tanous     {
1554a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC";
1564a0cb85cSEd Tanous     }
1574a0cb85cSEd Tanous     return "";
1584a0cb85cSEd Tanous }
1594a0cb85cSEd Tanous 
1604a0cb85cSEd Tanous inline void extractEthernetInterfaceData(const std::string &ethiface_id,
1614a0cb85cSEd Tanous                                          const GetManagedObjects &dbus_data,
1624a0cb85cSEd Tanous                                          EthernetInterfaceData &ethData)
1634a0cb85cSEd Tanous {
1644a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
1654a0cb85cSEd Tanous     {
1664a0cb85cSEd Tanous         for (const auto &ifacePair : objpath.second)
1674a0cb85cSEd Tanous         {
168029573d4SEd Tanous             if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
169029573d4SEd Tanous             {
1704a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
1714a0cb85cSEd Tanous                 {
1724a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
1734a0cb85cSEd Tanous                     {
1744a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
1754a0cb85cSEd Tanous                         {
1764a0cb85cSEd Tanous                             const std::string *mac =
177abf2add6SEd Tanous                                 std::get_if<std::string>(&propertyPair.second);
1784a0cb85cSEd Tanous                             if (mac != nullptr)
1794a0cb85cSEd Tanous                             {
1804a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
1814a0cb85cSEd Tanous                             }
1824a0cb85cSEd Tanous                         }
1834a0cb85cSEd Tanous                     }
1844a0cb85cSEd Tanous                 }
1854a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
1864a0cb85cSEd Tanous                 {
1874a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
1884a0cb85cSEd Tanous                     {
1894a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
1904a0cb85cSEd Tanous                         {
1911b6b96c5SEd Tanous                             const uint32_t *id =
192abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
1934a0cb85cSEd Tanous                             if (id != nullptr)
1944a0cb85cSEd Tanous                             {
195fda13ad2SSunitha Harish                                 ethData.vlan_id.push_back(*id);
1964a0cb85cSEd Tanous                             }
1974a0cb85cSEd Tanous                         }
1984a0cb85cSEd Tanous                     }
1994a0cb85cSEd Tanous                 }
2004a0cb85cSEd Tanous                 else if (ifacePair.first ==
2014a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2024a0cb85cSEd Tanous                 {
2034a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2044a0cb85cSEd Tanous                     {
2054a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2064a0cb85cSEd Tanous                         {
2074a0cb85cSEd Tanous                             const bool *auto_neg =
208abf2add6SEd Tanous                                 std::get_if<bool>(&propertyPair.second);
2094a0cb85cSEd Tanous                             if (auto_neg != nullptr)
2104a0cb85cSEd Tanous                             {
2114a0cb85cSEd Tanous                                 ethData.auto_neg = *auto_neg;
2124a0cb85cSEd Tanous                             }
2134a0cb85cSEd Tanous                         }
2144a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2154a0cb85cSEd Tanous                         {
2164a0cb85cSEd Tanous                             const uint32_t *speed =
217abf2add6SEd Tanous                                 std::get_if<uint32_t>(&propertyPair.second);
2184a0cb85cSEd Tanous                             if (speed != nullptr)
2194a0cb85cSEd Tanous                             {
2204a0cb85cSEd Tanous                                 ethData.speed = *speed;
2214a0cb85cSEd Tanous                             }
2224a0cb85cSEd Tanous                         }
223f85837bfSRAJESWARAN THILLAIGOVINDAN                         else if (propertyPair.first == "Nameservers")
224029573d4SEd Tanous                         {
225029573d4SEd Tanous                             const std::vector<std::string> *nameservers =
226029573d4SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
227029573d4SEd Tanous                                     std::vector<std::string>>(
228029573d4SEd Tanous                                     &propertyPair.second);
229029573d4SEd Tanous                             if (nameservers != nullptr)
230029573d4SEd Tanous                             {
231029573d4SEd Tanous                                 ethData.nameservers = std::move(*nameservers);
2324a0cb85cSEd Tanous                             }
2334a0cb85cSEd Tanous                         }
2342a133282Smanojkiraneda                         else if (propertyPair.first == "DHCPEnabled")
2352a133282Smanojkiraneda                         {
2362a133282Smanojkiraneda                             const bool *DHCPEnabled =
2372a133282Smanojkiraneda                                 std::get_if<bool>(&propertyPair.second);
2382a133282Smanojkiraneda                             if (DHCPEnabled != nullptr)
2392a133282Smanojkiraneda                             {
2402a133282Smanojkiraneda                                 ethData.DHCPEnabled = *DHCPEnabled;
2412a133282Smanojkiraneda                             }
2422a133282Smanojkiraneda                         }
243029573d4SEd Tanous                     }
244029573d4SEd Tanous                 }
245029573d4SEd Tanous             }
246029573d4SEd Tanous             // System configuration shows up in the global namespace, so no need
247029573d4SEd Tanous             // to check eth number
248029573d4SEd Tanous             if (ifacePair.first ==
2494a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.SystemConfiguration")
2504a0cb85cSEd Tanous             {
2514a0cb85cSEd Tanous                 for (const auto &propertyPair : ifacePair.second)
2524a0cb85cSEd Tanous                 {
2534a0cb85cSEd Tanous                     if (propertyPair.first == "HostName")
2544a0cb85cSEd Tanous                     {
2554a0cb85cSEd Tanous                         const std::string *hostname =
256029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
257029573d4SEd Tanous                                 &propertyPair.second);
2584a0cb85cSEd Tanous                         if (hostname != nullptr)
2594a0cb85cSEd Tanous                         {
2604a0cb85cSEd Tanous                             ethData.hostname = *hostname;
2614a0cb85cSEd Tanous                         }
2624a0cb85cSEd Tanous                     }
2634a0cb85cSEd Tanous                     else if (propertyPair.first == "DefaultGateway")
2644a0cb85cSEd Tanous                     {
2654a0cb85cSEd Tanous                         const std::string *defaultGateway =
266029573d4SEd Tanous                             sdbusplus::message::variant_ns::get_if<std::string>(
267029573d4SEd Tanous                                 &propertyPair.second);
2684a0cb85cSEd Tanous                         if (defaultGateway != nullptr)
2694a0cb85cSEd Tanous                         {
2704a0cb85cSEd Tanous                             ethData.default_gateway = *defaultGateway;
2714a0cb85cSEd Tanous                         }
2724a0cb85cSEd Tanous                     }
2734a0cb85cSEd Tanous                 }
2744a0cb85cSEd Tanous             }
2754a0cb85cSEd Tanous         }
2764a0cb85cSEd Tanous     }
2774a0cb85cSEd Tanous }
2784a0cb85cSEd Tanous 
2794a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
2804a0cb85cSEd Tanous inline void
2814a0cb85cSEd Tanous     extractIPData(const std::string &ethiface_id,
2824a0cb85cSEd Tanous                   const GetManagedObjects &dbus_data,
2834a0cb85cSEd Tanous                   boost::container::flat_set<IPv4AddressData> &ipv4_config)
2844a0cb85cSEd Tanous {
2854a0cb85cSEd Tanous     const std::string ipv4PathStart =
2864a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
2874a0cb85cSEd Tanous 
2884a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
2894a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
2904a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
2914a0cb85cSEd Tanous     {
2924a0cb85cSEd Tanous         // Check if proper pattern for object path appears
2934a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
2944a0cb85cSEd Tanous         {
2954a0cb85cSEd Tanous             for (auto &interface : objpath.second)
2964a0cb85cSEd Tanous             {
2974a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
2984a0cb85cSEd Tanous                 {
2994a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
3004a0cb85cSEd Tanous                     // appropriate
3014a0cb85cSEd Tanous                     std::pair<
3024a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
3034a0cb85cSEd Tanous                         bool>
3044a0cb85cSEd Tanous                         it = ipv4_config.insert(
305b01bf299SEd Tanous                             {objpath.first.str.substr(ipv4PathStart.size())});
3064a0cb85cSEd Tanous                     IPv4AddressData &ipv4_address = *it.first;
3074a0cb85cSEd Tanous                     for (auto &property : interface.second)
3084a0cb85cSEd Tanous                     {
3094a0cb85cSEd Tanous                         if (property.first == "Address")
3104a0cb85cSEd Tanous                         {
3114a0cb85cSEd Tanous                             const std::string *address =
312abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
3134a0cb85cSEd Tanous                             if (address != nullptr)
3144a0cb85cSEd Tanous                             {
3154a0cb85cSEd Tanous                                 ipv4_address.address = *address;
3164a0cb85cSEd Tanous                             }
3174a0cb85cSEd Tanous                         }
3184a0cb85cSEd Tanous                         else if (property.first == "Gateway")
3194a0cb85cSEd Tanous                         {
3204a0cb85cSEd Tanous                             const std::string *gateway =
321abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
3224a0cb85cSEd Tanous                             if (gateway != nullptr)
3234a0cb85cSEd Tanous                             {
3244a0cb85cSEd Tanous                                 ipv4_address.gateway = *gateway;
3254a0cb85cSEd Tanous                             }
3264a0cb85cSEd Tanous                         }
3274a0cb85cSEd Tanous                         else if (property.first == "Origin")
3284a0cb85cSEd Tanous                         {
3294a0cb85cSEd Tanous                             const std::string *origin =
330abf2add6SEd Tanous                                 std::get_if<std::string>(&property.second);
3314a0cb85cSEd Tanous                             if (origin != nullptr)
3324a0cb85cSEd Tanous                             {
3334a0cb85cSEd Tanous                                 ipv4_address.origin =
3344a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
3354a0cb85cSEd Tanous                                                                         true);
3364a0cb85cSEd Tanous                             }
3374a0cb85cSEd Tanous                         }
3384a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
3394a0cb85cSEd Tanous                         {
3404a0cb85cSEd Tanous                             const uint8_t *mask =
341abf2add6SEd Tanous                                 std::get_if<uint8_t>(&property.second);
3424a0cb85cSEd Tanous                             if (mask != nullptr)
3434a0cb85cSEd Tanous                             {
3444a0cb85cSEd Tanous                                 // convert it to the string
3454a0cb85cSEd Tanous                                 ipv4_address.netmask = getNetmask(*mask);
3464a0cb85cSEd Tanous                             }
3474a0cb85cSEd Tanous                         }
3484a0cb85cSEd Tanous                         else
3494a0cb85cSEd Tanous                         {
3504a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
3514a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
3524a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
3534a0cb85cSEd Tanous                         }
3544a0cb85cSEd Tanous                     }
3554a0cb85cSEd Tanous                     // Check if given address is local, or global
3564a0cb85cSEd Tanous                     ipv4_address.linktype =
3574a0cb85cSEd Tanous                         boost::starts_with(ipv4_address.address, "169.254.")
3584a0cb85cSEd Tanous                             ? LinkType::Global
3594a0cb85cSEd Tanous                             : LinkType::Local;
3604a0cb85cSEd Tanous                 }
3614a0cb85cSEd Tanous             }
3624a0cb85cSEd Tanous         }
3634a0cb85cSEd Tanous     }
3644a0cb85cSEd Tanous }
365588c3f0dSKowalski, Kamil 
366588c3f0dSKowalski, Kamil /**
367588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
368588c3f0dSKowalski, Kamil  *
369588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
370588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
371588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
372588c3f0dSKowalski, Kamil  *
373588c3f0dSKowalski, Kamil  * @return None.
374588c3f0dSKowalski, Kamil  */
375588c3f0dSKowalski, Kamil template <typename CallbackFunc>
3764a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
3771abe55efSEd Tanous                   CallbackFunc &&callback)
3781abe55efSEd Tanous {
37955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
380588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
381588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
382588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
383588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
384abf2add6SEd Tanous         std::variant<uint32_t>(inputVlanId));
3854a0cb85cSEd Tanous }
386588c3f0dSKowalski, Kamil 
387588c3f0dSKowalski, Kamil /**
388179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
389179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
390179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
391179db1d7SKowalski, Kamil  *
392179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
393179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
394179db1d7SKowalski, Kamil  *
395179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
396179db1d7SKowalski, Kamil  */
3974a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
3981abe55efSEd Tanous                                        uint8_t *bits = nullptr)
3991abe55efSEd Tanous {
400179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
401179db1d7SKowalski, Kamil 
402179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
403179db1d7SKowalski, Kamil 
4044a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
4051abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
4061abe55efSEd Tanous     {
407179db1d7SKowalski, Kamil         return false;
408179db1d7SKowalski, Kamil     }
409179db1d7SKowalski, Kamil 
4101abe55efSEd Tanous     if (bits != nullptr)
4111abe55efSEd Tanous     {
412179db1d7SKowalski, Kamil         *bits = 0;
413179db1d7SKowalski, Kamil     }
414179db1d7SKowalski, Kamil 
415179db1d7SKowalski, Kamil     char *endPtr;
416179db1d7SKowalski, Kamil     long previousValue = 255;
417179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
4181abe55efSEd Tanous     for (const std::string &byte : bytesInMask)
4191abe55efSEd Tanous     {
4201abe55efSEd Tanous         if (byte.empty())
4211abe55efSEd Tanous         {
4221db9ca37SKowalski, Kamil             return false;
4231db9ca37SKowalski, Kamil         }
4241db9ca37SKowalski, Kamil 
425179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
4261db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
427179db1d7SKowalski, Kamil 
4284a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
4294a0cb85cSEd Tanous         // is not 100% number
4301abe55efSEd Tanous         if (*endPtr != '\0')
4311abe55efSEd Tanous         {
432179db1d7SKowalski, Kamil             return false;
433179db1d7SKowalski, Kamil         }
434179db1d7SKowalski, Kamil 
435179db1d7SKowalski, Kamil         // Value should be contained in byte
4361abe55efSEd Tanous         if (value < 0 || value > 255)
4371abe55efSEd Tanous         {
438179db1d7SKowalski, Kamil             return false;
439179db1d7SKowalski, Kamil         }
440179db1d7SKowalski, Kamil 
4411abe55efSEd Tanous         if (bits != nullptr)
4421abe55efSEd Tanous         {
443179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
4441abe55efSEd Tanous             if (previousValue != 255 && value != 0)
4451abe55efSEd Tanous             {
446179db1d7SKowalski, Kamil                 return false;
447179db1d7SKowalski, Kamil             }
448179db1d7SKowalski, Kamil 
449179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
450179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
451179db1d7SKowalski, Kamil 
452179db1d7SKowalski, Kamil             // Count bits
4531abe55efSEd Tanous             for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
4541abe55efSEd Tanous             {
4551abe55efSEd Tanous                 if (value & (1 << bitIdx))
4561abe55efSEd Tanous                 {
4571abe55efSEd Tanous                     if (firstZeroInByteHit)
4581abe55efSEd Tanous                     {
459179db1d7SKowalski, Kamil                         // Continuity not preserved
460179db1d7SKowalski, Kamil                         return false;
4611abe55efSEd Tanous                     }
4621abe55efSEd Tanous                     else
4631abe55efSEd Tanous                     {
464179db1d7SKowalski, Kamil                         (*bits)++;
465179db1d7SKowalski, Kamil                     }
4661abe55efSEd Tanous                 }
4671abe55efSEd Tanous                 else
4681abe55efSEd Tanous                 {
469179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
470179db1d7SKowalski, Kamil                 }
471179db1d7SKowalski, Kamil             }
472179db1d7SKowalski, Kamil         }
473179db1d7SKowalski, Kamil 
474179db1d7SKowalski, Kamil         previousValue = value;
475179db1d7SKowalski, Kamil     }
476179db1d7SKowalski, Kamil 
477179db1d7SKowalski, Kamil     return true;
478179db1d7SKowalski, Kamil }
479179db1d7SKowalski, Kamil 
480179db1d7SKowalski, Kamil /**
481b01bf299SEd Tanous  * @brief Changes IPv4 address type property (Address, Gateway)
482b01bf299SEd Tanous  *
483b01bf299SEd Tanous  * @param[in] ifaceId     Id of interface whose IP should be modified
484b01bf299SEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be modified
485b01bf299SEd Tanous  * @param[in] ipHash      DBus Hash id of modified IP
486b01bf299SEd Tanous  * @param[in] name        Name of field in JSON representation
487b01bf299SEd Tanous  * @param[in] newValue    New value that should be written
488b01bf299SEd Tanous  * @param[io] asyncResp   Response object that will be returned to client
489b01bf299SEd Tanous  *
490b01bf299SEd Tanous  * @return true if give IP is valid and has been sent do D-Bus, false
491b01bf299SEd Tanous  * otherwise
492b01bf299SEd Tanous  */
493b01bf299SEd Tanous inline void changeIPv4AddressProperty(
494b01bf299SEd Tanous     const std::string &ifaceId, int ipIdx, const std::string &ipHash,
495b01bf299SEd Tanous     const std::string &name, const std::string &newValue,
496b01bf299SEd Tanous     const std::shared_ptr<AsyncResp> asyncResp)
497b01bf299SEd Tanous {
498b01bf299SEd Tanous     auto callback = [asyncResp, ipIdx, name{std::string(name)},
499b01bf299SEd Tanous                      newValue{std::move(newValue)}](
500b01bf299SEd Tanous                         const boost::system::error_code ec) {
501b01bf299SEd Tanous         if (ec)
502b01bf299SEd Tanous         {
503b01bf299SEd Tanous             messages::internalError(asyncResp->res);
504b01bf299SEd Tanous         }
505b01bf299SEd Tanous         else
506b01bf299SEd Tanous         {
507b01bf299SEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
508b01bf299SEd Tanous         }
509b01bf299SEd Tanous     };
510b01bf299SEd Tanous 
511b01bf299SEd Tanous     crow::connections::systemBus->async_method_call(
512b01bf299SEd Tanous         std::move(callback), "xyz.openbmc_project.Network",
513b01bf299SEd Tanous         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
514b01bf299SEd Tanous         "org.freedesktop.DBus.Properties", "Set",
515b01bf299SEd Tanous         "xyz.openbmc_project.Network.IP", name,
516b01bf299SEd Tanous         std::variant<std::string>(newValue));
517b01bf299SEd Tanous }
518b01bf299SEd Tanous 
519b01bf299SEd Tanous /**
520179db1d7SKowalski, Kamil  * @brief Changes IPv4 address origin property
521179db1d7SKowalski, Kamil  *
522179db1d7SKowalski, Kamil  * @param[in] ifaceId       Id of interface whose IP should be modified
5234a0cb85cSEd Tanous  * @param[in] ipIdx         Index of IP in input array that should be
5241abe55efSEd Tanous  * modified
525179db1d7SKowalski, Kamil  * @param[in] ipHash        DBus Hash id of modified IP
526179db1d7SKowalski, Kamil  * @param[in] newValue      New value in Redfish format
527179db1d7SKowalski, Kamil  * @param[in] newValueDbus  New value in D-Bus format
528179db1d7SKowalski, Kamil  * @param[io] asyncResp     Response object that will be returned to client
529179db1d7SKowalski, Kamil  *
530179db1d7SKowalski, Kamil  * @return true if give IP is valid and has been sent do D-Bus, false
531179db1d7SKowalski, Kamil  * otherwise
532179db1d7SKowalski, Kamil  */
533b01bf299SEd Tanous inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
5341abe55efSEd Tanous                              const std::string &ipHash,
5351abe55efSEd Tanous                              const std::string &newValue,
536179db1d7SKowalski, Kamil                              const std::string &newValueDbus,
5374a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
5381abe55efSEd Tanous {
5394a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
5401abe55efSEd Tanous                         const boost::system::error_code ec) {
5411abe55efSEd Tanous         if (ec)
5421abe55efSEd Tanous         {
543a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5441abe55efSEd Tanous         }
5451abe55efSEd Tanous         else
5461abe55efSEd Tanous         {
5474a0cb85cSEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
548179db1d7SKowalski, Kamil                 newValue;
549179db1d7SKowalski, Kamil         }
550179db1d7SKowalski, Kamil     };
551179db1d7SKowalski, Kamil 
55255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
553179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
554179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
555179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
556179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "Origin",
557abf2add6SEd Tanous         std::variant<std::string>(newValueDbus));
5584a0cb85cSEd Tanous }
559179db1d7SKowalski, Kamil 
560179db1d7SKowalski, Kamil /**
561179db1d7SKowalski, Kamil  * @brief Modifies SubnetMask for given IP
562179db1d7SKowalski, Kamil  *
563179db1d7SKowalski, Kamil  * @param[in] ifaceId      Id of interface whose IP should be modified
5644a0cb85cSEd Tanous  * @param[in] ipIdx        Index of IP in input array that should be
5651abe55efSEd Tanous  * modified
566179db1d7SKowalski, Kamil  * @param[in] ipHash       DBus Hash id of modified IP
567179db1d7SKowalski, Kamil  * @param[in] newValueStr  Mask in dot notation as string
568179db1d7SKowalski, Kamil  * @param[in] newValue     Mask as PrefixLength in bitcount
569179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
570179db1d7SKowalski, Kamil  *
571179db1d7SKowalski, Kamil  * @return None
572179db1d7SKowalski, Kamil  */
573b01bf299SEd Tanous inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
5744a0cb85cSEd Tanous                                          const std::string &ipHash,
5754a0cb85cSEd Tanous                                          const std::string &newValueStr,
5764a0cb85cSEd Tanous                                          uint8_t &newValue,
5774a0cb85cSEd Tanous                                          std::shared_ptr<AsyncResp> asyncResp)
5781abe55efSEd Tanous {
5794a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
5801abe55efSEd Tanous                         const boost::system::error_code ec) {
5811abe55efSEd Tanous         if (ec)
5821abe55efSEd Tanous         {
583a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5841abe55efSEd Tanous         }
5851abe55efSEd Tanous         else
5861abe55efSEd Tanous         {
58755c7b7a2SEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
588179db1d7SKowalski, Kamil                 newValueStr;
589179db1d7SKowalski, Kamil         }
590179db1d7SKowalski, Kamil     };
591179db1d7SKowalski, Kamil 
59255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
593179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
594179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
595179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
596179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "PrefixLength",
597abf2add6SEd Tanous         std::variant<uint8_t>(newValue));
5984a0cb85cSEd Tanous }
599588c3f0dSKowalski, Kamil 
600588c3f0dSKowalski, Kamil /**
601179db1d7SKowalski, Kamil  * @brief Deletes given IPv4
602179db1d7SKowalski, Kamil  *
603179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6044a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
605179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
606179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
607179db1d7SKowalski, Kamil  *
608179db1d7SKowalski, Kamil  * @return None
609179db1d7SKowalski, Kamil  */
6104a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
611179db1d7SKowalski, Kamil                        unsigned int ipIdx,
6124a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
6131abe55efSEd Tanous {
61455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6154a0cb85cSEd Tanous         [ipIdx, asyncResp](const boost::system::error_code ec) {
6161abe55efSEd Tanous             if (ec)
6171abe55efSEd Tanous             {
618a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
6191abe55efSEd Tanous             }
6201abe55efSEd Tanous             else
6211abe55efSEd Tanous             {
62255c7b7a2SEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
623179db1d7SKowalski, Kamil             }
624179db1d7SKowalski, Kamil         },
625179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
626179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
627179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
628179db1d7SKowalski, Kamil }
629179db1d7SKowalski, Kamil 
630179db1d7SKowalski, Kamil /**
631179db1d7SKowalski, Kamil  * @brief Creates IPv4 with given data
632179db1d7SKowalski, Kamil  *
633179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6344a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
635179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
636179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
637179db1d7SKowalski, Kamil  *
638179db1d7SKowalski, Kamil  * @return None
639179db1d7SKowalski, Kamil  */
640b01bf299SEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
641b01bf299SEd Tanous                        uint8_t subnetMask, const std::string &gateway,
642b01bf299SEd Tanous                        const std::string &address,
6434a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
6441abe55efSEd Tanous {
64543b761d0SEd Tanous     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
6461abe55efSEd Tanous         if (ec)
6471abe55efSEd Tanous         {
648a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
649179db1d7SKowalski, Kamil         }
650179db1d7SKowalski, Kamil     };
651179db1d7SKowalski, Kamil 
65255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
653179db1d7SKowalski, Kamil         std::move(createIpHandler), "xyz.openbmc_project.Network",
654179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
655179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
656179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
657179db1d7SKowalski, Kamil         gateway);
658179db1d7SKowalski, Kamil }
6592a133282Smanojkiraneda using GetAllPropertiesType =
6602a133282Smanojkiraneda     boost::container::flat_map<std::string, sdbusplus::message::variant<bool>>;
6612a133282Smanojkiraneda 
6622a133282Smanojkiraneda inline void getDHCPConfigData(const std::shared_ptr<AsyncResp> asyncResp)
6632a133282Smanojkiraneda {
6642a133282Smanojkiraneda     auto getConfig = [asyncResp](const boost::system::error_code error_code,
6652a133282Smanojkiraneda                                  const GetAllPropertiesType &dbus_data) {
6662a133282Smanojkiraneda         if (error_code)
6672a133282Smanojkiraneda         {
6682a133282Smanojkiraneda             BMCWEB_LOG_ERROR << "D-Bus response error: " << error_code;
6692a133282Smanojkiraneda             messages::internalError(asyncResp->res);
6702a133282Smanojkiraneda             return;
6712a133282Smanojkiraneda         }
672fda13ad2SSunitha Harish         nlohmann::json &DHCPConfigTypeJson = asyncResp->res.jsonValue["DHCPv4"];
6732a133282Smanojkiraneda         for (const auto &property : dbus_data)
6742a133282Smanojkiraneda         {
6752a133282Smanojkiraneda             auto value =
6762a133282Smanojkiraneda                 sdbusplus::message::variant_ns::get_if<bool>(&property.second);
6772a133282Smanojkiraneda 
6782a133282Smanojkiraneda             if (value == nullptr)
6792a133282Smanojkiraneda             {
6802a133282Smanojkiraneda                 continue;
6812a133282Smanojkiraneda             }
6822a133282Smanojkiraneda             if (property.first == "DNSEnabled")
6832a133282Smanojkiraneda             {
6842a133282Smanojkiraneda                 DHCPConfigTypeJson["UseDNSServers"] = *value;
6852a133282Smanojkiraneda             }
6862a133282Smanojkiraneda             else if (property.first == "HostNameEnabled")
6872a133282Smanojkiraneda             {
6882a133282Smanojkiraneda                 DHCPConfigTypeJson["UseDomainName"] = *value;
6892a133282Smanojkiraneda             }
6902a133282Smanojkiraneda             else if (property.first == "NTPEnabled")
6912a133282Smanojkiraneda             {
6922a133282Smanojkiraneda                 DHCPConfigTypeJson["UseNTPServers"] = *value;
6932a133282Smanojkiraneda             }
6942a133282Smanojkiraneda         }
6952a133282Smanojkiraneda     };
6962a133282Smanojkiraneda     crow::connections::systemBus->async_method_call(
6972a133282Smanojkiraneda         std::move(getConfig), "xyz.openbmc_project.Network",
6982a133282Smanojkiraneda         "/xyz/openbmc_project/network/config/dhcp",
6992a133282Smanojkiraneda         "org.freedesktop.DBus.Properties", "GetAll",
7002a133282Smanojkiraneda         "xyz.openbmc_project.Network.DHCPConfiguration");
7012a133282Smanojkiraneda }
702179db1d7SKowalski, Kamil 
703179db1d7SKowalski, Kamil /**
704179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
705179db1d7SKowalski, Kamil  * Object
706179db1d7SKowalski, Kamil  * from EntityManager Network Manager
7074a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
708179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
709179db1d7SKowalski, Kamil  * into JSON
710179db1d7SKowalski, Kamil  */
711179db1d7SKowalski, Kamil template <typename CallbackFunc>
7124a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
7131abe55efSEd Tanous                           CallbackFunc &&callback)
7141abe55efSEd Tanous {
71555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
7164a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
7171abe55efSEd Tanous             const boost::system::error_code error_code,
7184a0cb85cSEd Tanous             const GetManagedObjects &resp) {
71955c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
7204a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
721179db1d7SKowalski, Kamil 
7221abe55efSEd Tanous             if (error_code)
7231abe55efSEd Tanous             {
72455c7b7a2SEd Tanous                 callback(false, ethData, ipv4Data);
725179db1d7SKowalski, Kamil                 return;
726179db1d7SKowalski, Kamil             }
727179db1d7SKowalski, Kamil 
7284a0cb85cSEd Tanous             extractEthernetInterfaceData(ethiface_id, resp, ethData);
7294a0cb85cSEd Tanous             extractIPData(ethiface_id, resp, ipv4Data);
730179db1d7SKowalski, Kamil 
731179db1d7SKowalski, Kamil             // Fix global GW
7321abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
7331abe55efSEd Tanous             {
7344a0cb85cSEd Tanous                 if ((ipv4.linktype == LinkType::Global) &&
7354a0cb85cSEd Tanous                     (ipv4.gateway == "0.0.0.0"))
7361abe55efSEd Tanous                 {
7374a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
738179db1d7SKowalski, Kamil                 }
739179db1d7SKowalski, Kamil             }
740179db1d7SKowalski, Kamil 
7414a0cb85cSEd Tanous             // Finally make a callback with usefull data
74255c7b7a2SEd Tanous             callback(true, ethData, ipv4Data);
743179db1d7SKowalski, Kamil         },
744179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
745179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
746179db1d7SKowalski, Kamil };
747179db1d7SKowalski, Kamil 
748179db1d7SKowalski, Kamil /**
7499391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
7509391bb9cSRapkiewicz, Pawel  * Manager
7511abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
7521abe55efSEd Tanous  * into JSON.
7539391bb9cSRapkiewicz, Pawel  */
7549391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
7551abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
7561abe55efSEd Tanous {
75755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
7584a0cb85cSEd Tanous         [callback{std::move(callback)}](
7599391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
7604a0cb85cSEd Tanous             GetManagedObjects &resp) {
7611abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
7621abe55efSEd Tanous             // ethernet interfaces
7634a0cb85cSEd Tanous             std::vector<std::string> iface_list;
7644a0cb85cSEd Tanous             iface_list.reserve(resp.size());
7651abe55efSEd Tanous             if (error_code)
7661abe55efSEd Tanous             {
7674a0cb85cSEd Tanous                 callback(false, iface_list);
7689391bb9cSRapkiewicz, Pawel                 return;
7699391bb9cSRapkiewicz, Pawel             }
7709391bb9cSRapkiewicz, Pawel 
7719391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
7724a0cb85cSEd Tanous             for (const auto &objpath : resp)
7731abe55efSEd Tanous             {
7749391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
7754a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
7761abe55efSEd Tanous                 {
7771abe55efSEd Tanous                     // If interface is
7784a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
7794a0cb85cSEd Tanous                     // what we're looking for.
7809391bb9cSRapkiewicz, Pawel                     if (interface.first ==
7811abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
7821abe55efSEd Tanous                     {
7834a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
7844a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
7854a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
7864a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
7871abe55efSEd Tanous                         {
7889391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
7894a0cb85cSEd Tanous                             iface_list.emplace_back(
7904a0cb85cSEd Tanous                                 iface_id.substr(last_pos + 1));
7919391bb9cSRapkiewicz, Pawel                         }
7929391bb9cSRapkiewicz, Pawel                     }
7939391bb9cSRapkiewicz, Pawel                 }
7949391bb9cSRapkiewicz, Pawel             }
795a434f2bdSEd Tanous             // Finally make a callback with useful data
7964a0cb85cSEd Tanous             callback(true, iface_list);
7979391bb9cSRapkiewicz, Pawel         },
798aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
799aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
8009391bb9cSRapkiewicz, Pawel };
8019391bb9cSRapkiewicz, Pawel 
8029391bb9cSRapkiewicz, Pawel /**
8039391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
8049391bb9cSRapkiewicz, Pawel  */
8051abe55efSEd Tanous class EthernetCollection : public Node
8061abe55efSEd Tanous {
8079391bb9cSRapkiewicz, Pawel   public:
8084a0cb85cSEd Tanous     template <typename CrowApp>
8091abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
8104a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
8111abe55efSEd Tanous     {
812588c3f0dSKowalski, Kamil         entityPrivileges = {
813588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
814e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
815e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
816e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
817e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
818e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8199391bb9cSRapkiewicz, Pawel     }
8209391bb9cSRapkiewicz, Pawel 
8219391bb9cSRapkiewicz, Pawel   private:
8229391bb9cSRapkiewicz, Pawel     /**
8239391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
8249391bb9cSRapkiewicz, Pawel      */
82555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
8261abe55efSEd Tanous                const std::vector<std::string> &params) override
8271abe55efSEd Tanous     {
8280f74e643SEd Tanous         res.jsonValue["@odata.type"] =
8290f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
8300f74e643SEd Tanous         res.jsonValue["@odata.context"] =
8310f74e643SEd Tanous             "/redfish/v1/"
8320f74e643SEd Tanous             "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
8330f74e643SEd Tanous         res.jsonValue["@odata.id"] =
8340f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
8350f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
8360f74e643SEd Tanous         res.jsonValue["Description"] =
8370f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
8380f74e643SEd Tanous 
8394a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
8401abe55efSEd Tanous         // preparation
841f12894f8SJason M. Bills         getEthernetIfaceList(
842f12894f8SJason M. Bills             [&res](const bool &success,
8431abe55efSEd Tanous                    const std::vector<std::string> &iface_list) {
8444a0cb85cSEd Tanous                 if (!success)
8451abe55efSEd Tanous                 {
846f12894f8SJason M. Bills                     messages::internalError(res);
8474a0cb85cSEd Tanous                     res.end();
8484a0cb85cSEd Tanous                     return;
8494a0cb85cSEd Tanous                 }
8504a0cb85cSEd Tanous 
8514a0cb85cSEd Tanous                 nlohmann::json &iface_array = res.jsonValue["Members"];
8524a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
853fda13ad2SSunitha Harish                 std::string tag = "_";
8544a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
8551abe55efSEd Tanous                 {
856fda13ad2SSunitha Harish                     std::size_t found = iface_item.find(tag);
857fda13ad2SSunitha Harish                     if (found == std::string::npos)
858fda13ad2SSunitha Harish                     {
8594a0cb85cSEd Tanous                         iface_array.push_back(
8604a0cb85cSEd Tanous                             {{"@odata.id",
8614a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
8624a0cb85cSEd Tanous                                   iface_item}});
8639391bb9cSRapkiewicz, Pawel                     }
864fda13ad2SSunitha Harish                 }
8654a0cb85cSEd Tanous 
8664a0cb85cSEd Tanous                 res.jsonValue["Members@odata.count"] = iface_array.size();
8674a0cb85cSEd Tanous                 res.jsonValue["@odata.id"] =
8684a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
8699391bb9cSRapkiewicz, Pawel                 res.end();
8709391bb9cSRapkiewicz, Pawel             });
8719391bb9cSRapkiewicz, Pawel     }
8729391bb9cSRapkiewicz, Pawel };
8739391bb9cSRapkiewicz, Pawel 
8749391bb9cSRapkiewicz, Pawel /**
8759391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
8769391bb9cSRapkiewicz, Pawel  */
8771abe55efSEd Tanous class EthernetInterface : public Node
8781abe55efSEd Tanous {
8799391bb9cSRapkiewicz, Pawel   public:
8809391bb9cSRapkiewicz, Pawel     /*
8819391bb9cSRapkiewicz, Pawel      * Default Constructor
8829391bb9cSRapkiewicz, Pawel      */
8834a0cb85cSEd Tanous     template <typename CrowApp>
8841abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
8854a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
8861abe55efSEd Tanous              std::string())
8871abe55efSEd Tanous     {
888588c3f0dSKowalski, Kamil         entityPrivileges = {
889588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
890e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
891e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
892e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
893e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
894e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8959391bb9cSRapkiewicz, Pawel     }
8969391bb9cSRapkiewicz, Pawel 
897e439f0f8SKowalski, Kamil   private:
898bc0bd6e0SEd Tanous     void handleHostnamePatch(const std::string &hostname,
8994a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
9001abe55efSEd Tanous     {
901bc0bd6e0SEd Tanous         asyncResp->res.jsonValue["HostName"] = hostname;
902bc0bd6e0SEd Tanous         crow::connections::systemBus->async_method_call(
903bc0bd6e0SEd Tanous             [asyncResp](const boost::system::error_code ec) {
9044a0cb85cSEd Tanous                 if (ec)
9054a0cb85cSEd Tanous                 {
906a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
9071abe55efSEd Tanous                 }
908bc0bd6e0SEd Tanous             },
909bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network",
910bc0bd6e0SEd Tanous             "/xyz/openbmc_project/network/config",
911bc0bd6e0SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
912bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
913abf2add6SEd Tanous             std::variant<std::string>(hostname));
914588c3f0dSKowalski, Kamil     }
915588c3f0dSKowalski, Kamil 
916d577665bSRatan Gupta     void handleMACAddressPatch(const std::string &ifaceId,
917d577665bSRatan Gupta                                const std::string &macAddress,
918d577665bSRatan Gupta                                const std::shared_ptr<AsyncResp> &asyncResp)
919d577665bSRatan Gupta     {
920d577665bSRatan Gupta         crow::connections::systemBus->async_method_call(
921d577665bSRatan Gupta             [asyncResp, macAddress](const boost::system::error_code ec) {
922d577665bSRatan Gupta                 if (ec)
923d577665bSRatan Gupta                 {
924d577665bSRatan Gupta                     messages::internalError(asyncResp->res);
925d577665bSRatan Gupta                     return;
926d577665bSRatan Gupta                 }
927d577665bSRatan Gupta                 asyncResp->res.jsonValue["MACAddress"] = std::move(macAddress);
928d577665bSRatan Gupta             },
929d577665bSRatan Gupta             "xyz.openbmc_project.Network",
930d577665bSRatan Gupta             "/xyz/openbmc_project/network/" + ifaceId,
931d577665bSRatan Gupta             "org.freedesktop.DBus.Properties", "Set",
932d577665bSRatan Gupta             "xyz.openbmc_project.Network.MACAddress", "MACAddress",
933d577665bSRatan Gupta             std::variant<std::string>(macAddress));
934d577665bSRatan Gupta     }
935d577665bSRatan Gupta 
9364a0cb85cSEd Tanous     void handleIPv4Patch(
937f476acbfSRatan Gupta         const std::string &ifaceId, nlohmann::json &input,
9384a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
9394a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
9401abe55efSEd Tanous     {
941f476acbfSRatan Gupta         if (!input.is_array())
942f476acbfSRatan Gupta         {
943f476acbfSRatan Gupta             messages::propertyValueTypeError(asyncResp->res, input.dump(),
944f476acbfSRatan Gupta                                              "IPv4Addresses");
945f476acbfSRatan Gupta             return;
946f476acbfSRatan Gupta         }
947f476acbfSRatan Gupta 
9484a0cb85cSEd Tanous         int entryIdx = 0;
9494a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
9504a0cb85cSEd Tanous             ipv4Data.begin();
951537174c4SEd Tanous         for (nlohmann::json &thisJson : input)
9521abe55efSEd Tanous         {
9534a0cb85cSEd Tanous             std::string pathString =
954a08b46ccSJason M. Bills                 "IPv4Addresses/" + std::to_string(entryIdx);
955179db1d7SKowalski, Kamil 
956f476acbfSRatan Gupta             if (thisJson.is_null())
957f476acbfSRatan Gupta             {
958f476acbfSRatan Gupta                 if (thisData != ipv4Data.end())
959f476acbfSRatan Gupta                 {
960f476acbfSRatan Gupta                     deleteIPv4(ifaceId, thisData->id, entryIdx, asyncResp);
961f476acbfSRatan Gupta                     thisData++;
962f476acbfSRatan Gupta                 }
963f476acbfSRatan Gupta                 else
964f476acbfSRatan Gupta                 {
965f476acbfSRatan Gupta                     messages::propertyValueFormatError(
966f476acbfSRatan Gupta                         asyncResp->res, input.dump(), pathString);
967f476acbfSRatan Gupta                     return;
968f476acbfSRatan Gupta                     // TODO(ratagupt) Not sure about the property where value is
969f476acbfSRatan Gupta                     // list and if unable to update one of the
970f476acbfSRatan Gupta                     // list value then should we proceed further or
971f476acbfSRatan Gupta                     // break there, would ask in the redfish forum
972f476acbfSRatan Gupta                     // till then we stop processing the next list item.
973f476acbfSRatan Gupta                 }
974f476acbfSRatan Gupta                 entryIdx++;
975f476acbfSRatan Gupta                 continue; // not an error as per the redfish spec.
976f476acbfSRatan Gupta             }
977f476acbfSRatan Gupta 
9789474b378SRatan Gupta             if (thisJson.empty())
9799474b378SRatan Gupta             {
9809474b378SRatan Gupta                 if (thisData != ipv4Data.end())
9819474b378SRatan Gupta                 {
9829474b378SRatan Gupta                     thisData++;
9839474b378SRatan Gupta                 }
9849474b378SRatan Gupta                 else
9859474b378SRatan Gupta                 {
9869474b378SRatan Gupta                     messages::propertyMissing(asyncResp->res,
9879474b378SRatan Gupta                                               pathString + "/Address");
9889474b378SRatan Gupta                     return;
989f476acbfSRatan Gupta                     // TODO(ratagupt) Not sure about the property where value is
9909474b378SRatan Gupta                     // list and if unable to update one of the
9919474b378SRatan Gupta                     // list value then should we proceed further or
9929474b378SRatan Gupta                     // break there, would ask in the redfish forum
9939474b378SRatan Gupta                     // till then we stop processing the next list item.
9949474b378SRatan Gupta                 }
9959474b378SRatan Gupta                 entryIdx++;
9969474b378SRatan Gupta                 continue; // not an error as per the redfish spec.
9979474b378SRatan Gupta             }
9989474b378SRatan Gupta 
999537174c4SEd Tanous             std::optional<std::string> address;
1000537174c4SEd Tanous             std::optional<std::string> addressOrigin;
1001537174c4SEd Tanous             std::optional<std::string> subnetMask;
1002537174c4SEd Tanous             std::optional<std::string> gateway;
1003537174c4SEd Tanous 
1004537174c4SEd Tanous             if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1005537174c4SEd Tanous                                      address, "AddressOrigin", addressOrigin,
1006537174c4SEd Tanous                                      "SubnetMask", subnetMask, "Gateway",
1007537174c4SEd Tanous                                      gateway))
1008537174c4SEd Tanous             {
1009537174c4SEd Tanous                 return;
1010179db1d7SKowalski, Kamil             }
1011179db1d7SKowalski, Kamil 
1012537174c4SEd Tanous             if (address)
10131abe55efSEd Tanous             {
1014537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*address))
10151abe55efSEd Tanous                 {
1016537174c4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *address,
10174a0cb85cSEd Tanous                                                        pathString + "/Address");
1018537174c4SEd Tanous                     return;
10194a0cb85cSEd Tanous                 }
10204a0cb85cSEd Tanous             }
10214a0cb85cSEd Tanous 
1022537174c4SEd Tanous             uint8_t prefixLength = 0;
1023537174c4SEd Tanous             if (subnetMask)
10244a0cb85cSEd Tanous             {
1025537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
10264a0cb85cSEd Tanous                 {
1027f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1028537174c4SEd Tanous                         asyncResp->res, *subnetMask,
10294a0cb85cSEd Tanous                         pathString + "/SubnetMask");
1030537174c4SEd Tanous                     return;
10314a0cb85cSEd Tanous                 }
10324a0cb85cSEd Tanous             }
10334a0cb85cSEd Tanous             std::string addressOriginInDBusFormat;
1034537174c4SEd Tanous             if (addressOrigin)
10354a0cb85cSEd Tanous             {
10364a0cb85cSEd Tanous                 // Get Address origin in proper format
10374a0cb85cSEd Tanous                 addressOriginInDBusFormat =
1038537174c4SEd Tanous                     translateAddressOriginRedfishToDbus(*addressOrigin);
10394a0cb85cSEd Tanous                 if (addressOriginInDBusFormat.empty())
10404a0cb85cSEd Tanous                 {
10414a0cb85cSEd Tanous                     messages::propertyValueNotInList(
1042537174c4SEd Tanous                         asyncResp->res, *addressOrigin,
1043a08b46ccSJason M. Bills                         pathString + "/AddressOrigin");
1044537174c4SEd Tanous                     return;
10454a0cb85cSEd Tanous                 }
10464a0cb85cSEd Tanous             }
10474a0cb85cSEd Tanous 
1048537174c4SEd Tanous             if (gateway)
10494a0cb85cSEd Tanous             {
1050537174c4SEd Tanous                 if (!ipv4VerifyIpAndGetBitcount(*gateway))
10514a0cb85cSEd Tanous                 {
1052537174c4SEd Tanous                     messages::propertyValueFormatError(asyncResp->res, *gateway,
1053537174c4SEd Tanous                                                        pathString + "/Gateway");
1054537174c4SEd Tanous                     return;
10554a0cb85cSEd Tanous                 }
10564a0cb85cSEd Tanous             }
10574a0cb85cSEd Tanous 
1058f476acbfSRatan Gupta             // if IP address exist then  modify it.
10594a0cb85cSEd Tanous             if (thisData != ipv4Data.end())
10604a0cb85cSEd Tanous             {
1061179db1d7SKowalski, Kamil                 // Apply changes
1062537174c4SEd Tanous                 if (address)
10631abe55efSEd Tanous                 {
1064f476acbfSRatan Gupta                     auto callback = [asyncResp, entryIdx,
1065f476acbfSRatan Gupta                                      address{std::string(*address)}](
10664a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
10674a0cb85cSEd Tanous                         if (ec)
10681abe55efSEd Tanous                         {
1069a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
10704a0cb85cSEd Tanous                             return;
10714a0cb85cSEd Tanous                         }
1072f476acbfSRatan Gupta                         asyncResp->res
1073f476acbfSRatan Gupta                             .jsonValue["IPv4Addresses"][entryIdx]["Address"] =
1074f476acbfSRatan Gupta                             std::move(address);
10754a0cb85cSEd Tanous                     };
10764a0cb85cSEd Tanous 
10774a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
10784a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
1079f476acbfSRatan Gupta                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1080f476acbfSRatan Gupta                             thisData->id,
10814a0cb85cSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
10824a0cb85cSEd Tanous                         "xyz.openbmc_project.Network.IP", "Address",
1083537174c4SEd Tanous                         std::variant<std::string>(*address));
1084179db1d7SKowalski, Kamil                 }
1085179db1d7SKowalski, Kamil 
1086537174c4SEd Tanous                 if (subnetMask)
10871abe55efSEd Tanous                 {
10884a0cb85cSEd Tanous                     changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
1089537174c4SEd Tanous                                                  thisData->id, *subnetMask,
1090537174c4SEd Tanous                                                  prefixLength, asyncResp);
1091179db1d7SKowalski, Kamil                 }
1092179db1d7SKowalski, Kamil 
1093537174c4SEd Tanous                 if (addressOrigin)
10941abe55efSEd Tanous                 {
10954a0cb85cSEd Tanous                     changeIPv4Origin(ifaceId, entryIdx, thisData->id,
1096f476acbfSRatan Gupta                                      *addressOrigin, addressOriginInDBusFormat,
1097f476acbfSRatan Gupta                                      asyncResp);
1098179db1d7SKowalski, Kamil                 }
1099179db1d7SKowalski, Kamil 
1100537174c4SEd Tanous                 if (gateway)
11011abe55efSEd Tanous                 {
1102f476acbfSRatan Gupta                     auto callback = [asyncResp, entryIdx,
1103537174c4SEd Tanous                                      gateway{std::string(*gateway)}](
11044a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
11054a0cb85cSEd Tanous                         if (ec)
11061abe55efSEd Tanous                         {
1107a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
11084a0cb85cSEd Tanous                             return;
11094a0cb85cSEd Tanous                         }
1110f476acbfSRatan Gupta                         asyncResp->res
1111f476acbfSRatan Gupta                             .jsonValue["IPv4Addresses"][entryIdx]["Gateway"] =
1112537174c4SEd Tanous                             std::move(gateway);
11134a0cb85cSEd Tanous                     };
11144a0cb85cSEd Tanous 
11154a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
11164a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
1117f476acbfSRatan Gupta                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1118f476acbfSRatan Gupta                             thisData->id,
11194a0cb85cSEd Tanous                         "org.freedesktop.DBus.Properties", "Set",
11204a0cb85cSEd Tanous                         "xyz.openbmc_project.Network.IP", "Gateway",
1121537174c4SEd Tanous                         std::variant<std::string>(*gateway));
11224a0cb85cSEd Tanous                 }
1123f476acbfSRatan Gupta 
11244a0cb85cSEd Tanous                 thisData++;
11251abe55efSEd Tanous             }
11261abe55efSEd Tanous             else
11271abe55efSEd Tanous             {
11284a0cb85cSEd Tanous                 // Create IPv4 with provided data
1129537174c4SEd Tanous                 if (!gateway)
11301abe55efSEd Tanous                 {
1131a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11324a0cb85cSEd Tanous                                               pathString + "/Gateway");
11334a0cb85cSEd Tanous                     continue;
11344a0cb85cSEd Tanous                 }
11354a0cb85cSEd Tanous 
1136537174c4SEd Tanous                 if (!address)
11371abe55efSEd Tanous                 {
1138a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11394a0cb85cSEd Tanous                                               pathString + "/Address");
11404a0cb85cSEd Tanous                     continue;
11414a0cb85cSEd Tanous                 }
11424a0cb85cSEd Tanous 
1143537174c4SEd Tanous                 if (!subnetMask)
11441abe55efSEd Tanous                 {
1145a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11464a0cb85cSEd Tanous                                               pathString + "/SubnetMask");
11474a0cb85cSEd Tanous                     continue;
1148588c3f0dSKowalski, Kamil                 }
1149588c3f0dSKowalski, Kamil 
1150b01bf299SEd Tanous                 createIPv4(ifaceId, entryIdx, prefixLength, *gateway, *address,
1151b01bf299SEd Tanous                            asyncResp);
115295897b20SRatan Gupta 
115395897b20SRatan Gupta                 nlohmann::json &ipv4AddressJson =
115495897b20SRatan Gupta                     asyncResp->res.jsonValue["IPv4Addresses"][entryIdx];
115595897b20SRatan Gupta                 ipv4AddressJson["Address"] = *address;
115695897b20SRatan Gupta                 ipv4AddressJson["SubnetMask"] = *subnetMask;
115795897b20SRatan Gupta                 ipv4AddressJson["Gateway"] = *gateway;
11584a0cb85cSEd Tanous             }
11594a0cb85cSEd Tanous             entryIdx++;
11604a0cb85cSEd Tanous         }
11614a0cb85cSEd Tanous     }
11624a0cb85cSEd Tanous 
1163f85837bfSRAJESWARAN THILLAIGOVINDAN     void handleStaticNameServersPatch(
1164f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::string &ifaceId,
1165f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::vector<std::string> &updatedStaticNameServers,
1166f85837bfSRAJESWARAN THILLAIGOVINDAN         const std::shared_ptr<AsyncResp> &asyncResp)
1167f85837bfSRAJESWARAN THILLAIGOVINDAN     {
1168f85837bfSRAJESWARAN THILLAIGOVINDAN         crow::connections::systemBus->async_method_call(
1169f85837bfSRAJESWARAN THILLAIGOVINDAN             [asyncResp,
1170f85837bfSRAJESWARAN THILLAIGOVINDAN              updatedStaticNameServers](const boost::system::error_code ec) {
1171f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (ec)
1172f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1173f85837bfSRAJESWARAN THILLAIGOVINDAN                     messages::internalError(asyncResp->res);
1174f85837bfSRAJESWARAN THILLAIGOVINDAN                     return;
1175f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1176f85837bfSRAJESWARAN THILLAIGOVINDAN                 asyncResp->res.jsonValue["NameServers"] =
1177f85837bfSRAJESWARAN THILLAIGOVINDAN                     updatedStaticNameServers;
1178f85837bfSRAJESWARAN THILLAIGOVINDAN                 asyncResp->res.jsonValue["StaticNameServers"] =
1179f85837bfSRAJESWARAN THILLAIGOVINDAN                     updatedStaticNameServers;
1180f85837bfSRAJESWARAN THILLAIGOVINDAN             },
1181f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network",
1182f85837bfSRAJESWARAN THILLAIGOVINDAN             "/xyz/openbmc_project/network/" + ifaceId,
1183f85837bfSRAJESWARAN THILLAIGOVINDAN             "org.freedesktop.DBus.Properties", "Set",
1184f85837bfSRAJESWARAN THILLAIGOVINDAN             "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1185f85837bfSRAJESWARAN THILLAIGOVINDAN             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1186f85837bfSRAJESWARAN THILLAIGOVINDAN     }
1187f85837bfSRAJESWARAN THILLAIGOVINDAN 
11880f74e643SEd Tanous     void parseInterfaceData(
11890f74e643SEd Tanous         nlohmann::json &json_response, const std::string &iface_id,
11900f74e643SEd Tanous         const EthernetInterfaceData &ethData,
11914a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
11924a0cb85cSEd Tanous     {
11934a0cb85cSEd Tanous         json_response["Id"] = iface_id;
11944a0cb85cSEd Tanous         json_response["@odata.id"] =
11954a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
1196029573d4SEd Tanous         json_response["InterfaceEnabled"] = true;
1197029573d4SEd Tanous         if (ethData.speed == 0)
1198029573d4SEd Tanous         {
1199029573d4SEd Tanous             json_response["LinkStatus"] = "NoLink";
1200029573d4SEd Tanous             json_response["Status"] = {
1201029573d4SEd Tanous                 {"Health", "OK"},
1202029573d4SEd Tanous                 {"State", "Disabled"},
1203029573d4SEd Tanous             };
1204029573d4SEd Tanous         }
1205029573d4SEd Tanous         else
1206029573d4SEd Tanous         {
1207029573d4SEd Tanous             json_response["LinkStatus"] = "LinkUp";
1208029573d4SEd Tanous             json_response["Status"] = {
1209029573d4SEd Tanous                 {"Health", "OK"},
1210029573d4SEd Tanous                 {"State", "Enabled"},
1211029573d4SEd Tanous             };
1212029573d4SEd Tanous         }
12134a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
12144a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
1215fda13ad2SSunitha Harish         json_response["DHCPv4"]["DHCPEnabled"] = ethData.DHCPEnabled;
12162a133282Smanojkiraneda 
12174a0cb85cSEd Tanous         if (!ethData.hostname.empty())
12184a0cb85cSEd Tanous         {
12194a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
12204a0cb85cSEd Tanous         }
12214a0cb85cSEd Tanous 
1222fda13ad2SSunitha Harish         json_response["VLANs"] = {
1223fda13ad2SSunitha Harish             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1224fda13ad2SSunitha Harish                               iface_id + "/VLANs"}};
1225fda13ad2SSunitha Harish 
1226029573d4SEd Tanous         json_response["NameServers"] = ethData.nameservers;
1227f85837bfSRAJESWARAN THILLAIGOVINDAN         json_response["StaticNameServers"] = ethData.nameservers;
12284a0cb85cSEd Tanous 
12294a0cb85cSEd Tanous         if (ipv4Data.size() > 0)
12304a0cb85cSEd Tanous         {
12314a0cb85cSEd Tanous             nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
12324a0cb85cSEd Tanous             ipv4_array = nlohmann::json::array();
12334a0cb85cSEd Tanous             for (auto &ipv4_config : ipv4Data)
12344a0cb85cSEd Tanous             {
1235*fa5053a6SGunnar Mills 
1236*fa5053a6SGunnar Mills                 std::string gatewayStr = ipv4_config.gateway;
1237*fa5053a6SGunnar Mills                 if (gatewayStr.empty())
1238*fa5053a6SGunnar Mills                 {
1239*fa5053a6SGunnar Mills                     gatewayStr = "0.0.0.0";
1240*fa5053a6SGunnar Mills                 }
1241*fa5053a6SGunnar Mills 
12424a0cb85cSEd Tanous                 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
12434a0cb85cSEd Tanous                                       {"SubnetMask", ipv4_config.netmask},
1244029573d4SEd Tanous                                       {"Address", ipv4_config.address},
1245*fa5053a6SGunnar Mills                                       {"Gateway", gatewayStr}});
12464a0cb85cSEd Tanous             }
12474a0cb85cSEd Tanous         }
1248588c3f0dSKowalski, Kamil     }
1249588c3f0dSKowalski, Kamil 
12509391bb9cSRapkiewicz, Pawel     /**
12519391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
12529391bb9cSRapkiewicz, Pawel      */
125355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12541abe55efSEd Tanous                const std::vector<std::string> &params) override
12551abe55efSEd Tanous     {
12564a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12571abe55efSEd Tanous         if (params.size() != 1)
12581abe55efSEd Tanous         {
1259f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
12609391bb9cSRapkiewicz, Pawel             return;
12619391bb9cSRapkiewicz, Pawel         }
12629391bb9cSRapkiewicz, Pawel 
12634a0cb85cSEd Tanous         getEthernetIfaceData(
12644a0cb85cSEd Tanous             params[0],
12654a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
12664a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
12674a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
12684a0cb85cSEd Tanous                 if (!success)
12691abe55efSEd Tanous                 {
12701abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
12711abe55efSEd Tanous                     // object, and other errors
1272f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1273f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
12744a0cb85cSEd Tanous                     return;
12759391bb9cSRapkiewicz, Pawel                 }
12760f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1277fda13ad2SSunitha Harish                     "#EthernetInterface.v1_4_1.EthernetInterface";
12780f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
12790f74e643SEd Tanous                     "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
12800f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
12810f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
12820f74e643SEd Tanous                     "Management Network Interface";
12830f74e643SEd Tanous 
12840f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
12850f74e643SEd Tanous                                    ipv4Data);
12869391bb9cSRapkiewicz, Pawel             });
12872a133282Smanojkiraneda         getDHCPConfigData(asyncResp);
12889391bb9cSRapkiewicz, Pawel     }
12899391bb9cSRapkiewicz, Pawel 
129055c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
12911abe55efSEd Tanous                  const std::vector<std::string> &params) override
12921abe55efSEd Tanous     {
12934a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12941abe55efSEd Tanous         if (params.size() != 1)
12951abe55efSEd Tanous         {
1296f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1297588c3f0dSKowalski, Kamil             return;
1298588c3f0dSKowalski, Kamil         }
1299588c3f0dSKowalski, Kamil 
13004a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1301588c3f0dSKowalski, Kamil 
1302bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
1303d577665bSRatan Gupta         std::optional<std::string> macAddress;
1304f476acbfSRatan Gupta         std::optional<nlohmann::json> ipv4Addresses;
1305f476acbfSRatan Gupta         std::optional<nlohmann::json> ipv6Addresses;
1306f85837bfSRAJESWARAN THILLAIGOVINDAN         std::optional<std::vector<std::string>> staticNameServers;
13070627a2c7SEd Tanous 
1308fda13ad2SSunitha Harish         if (!json_util::readJson(
1309fda13ad2SSunitha Harish                 req, res, "HostName", hostname, "IPv4Addresses", ipv4Addresses,
1310f85837bfSRAJESWARAN THILLAIGOVINDAN                 "IPv6Addresses", ipv6Addresses, "MACAddress", macAddress,
1311f85837bfSRAJESWARAN THILLAIGOVINDAN                 "StaticNameServers", staticNameServers))
13121abe55efSEd Tanous         {
1313588c3f0dSKowalski, Kamil             return;
1314588c3f0dSKowalski, Kamil         }
1315f15aad37SRatan Gupta 
13164a0cb85cSEd Tanous         // Get single eth interface data, and call the below callback for JSON
1317588c3f0dSKowalski, Kamil         // preparation
13184a0cb85cSEd Tanous         getEthernetIfaceData(
13194a0cb85cSEd Tanous             iface_id,
1320fda13ad2SSunitha Harish             [this, asyncResp, iface_id, hostname = std::move(hostname),
1321fda13ad2SSunitha Harish              macAddress = std::move(macAddress),
13220627a2c7SEd Tanous              ipv4Addresses = std::move(ipv4Addresses),
1323f85837bfSRAJESWARAN THILLAIGOVINDAN              ipv6Addresses = std::move(ipv6Addresses),
1324f85837bfSRAJESWARAN THILLAIGOVINDAN              staticNameServers = std::move(staticNameServers)](
13254a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13264a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13271abe55efSEd Tanous                 if (!success)
13281abe55efSEd Tanous                 {
1329588c3f0dSKowalski, Kamil                     // ... otherwise return error
13301abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13311abe55efSEd Tanous                     // object, and other errors
1332fda13ad2SSunitha Harish                     messages::resourceNotFound(asyncResp->res,
1333fda13ad2SSunitha Harish                                                "Ethernet Interface", iface_id);
1334588c3f0dSKowalski, Kamil                     return;
1335588c3f0dSKowalski, Kamil                 }
1336588c3f0dSKowalski, Kamil 
13370f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
13380f74e643SEd Tanous                                    ipv4Data);
1339588c3f0dSKowalski, Kamil 
13400627a2c7SEd Tanous                 if (hostname)
13411abe55efSEd Tanous                 {
13420627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
13431abe55efSEd Tanous                 }
13440627a2c7SEd Tanous 
1345d577665bSRatan Gupta                 if (macAddress)
1346d577665bSRatan Gupta                 {
1347d577665bSRatan Gupta                     handleMACAddressPatch(iface_id, *macAddress, asyncResp);
1348d577665bSRatan Gupta                 }
1349d577665bSRatan Gupta 
13500627a2c7SEd Tanous                 if (ipv4Addresses)
13511abe55efSEd Tanous                 {
1352537174c4SEd Tanous                     // TODO(ed) for some reason the capture of ipv4Addresses
1353537174c4SEd Tanous                     // above is returning a const value, not a non-const value.
1354537174c4SEd Tanous                     // This doesn't really work for us, as we need to be able to
1355537174c4SEd Tanous                     // efficiently move out the intermedia nlohmann::json
1356537174c4SEd Tanous                     // objects. This makes a copy of the structure, and operates
1357537174c4SEd Tanous                     // on that, but could be done more efficiently
1358f476acbfSRatan Gupta                     nlohmann::json ipv4 = std::move(*ipv4Addresses);
1359537174c4SEd Tanous                     handleIPv4Patch(iface_id, ipv4, ipv4Data, asyncResp);
13601abe55efSEd Tanous                 }
13610627a2c7SEd Tanous 
13620627a2c7SEd Tanous                 if (ipv6Addresses)
13631abe55efSEd Tanous                 {
1364179db1d7SKowalski, Kamil                     // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1365a08b46ccSJason M. Bills                     messages::propertyNotWritable(asyncResp->res,
13660627a2c7SEd Tanous                                                   "IPv6Addresses");
1367588c3f0dSKowalski, Kamil                 }
1368f85837bfSRAJESWARAN THILLAIGOVINDAN 
1369f85837bfSRAJESWARAN THILLAIGOVINDAN                 if (staticNameServers)
1370f85837bfSRAJESWARAN THILLAIGOVINDAN                 {
1371f85837bfSRAJESWARAN THILLAIGOVINDAN                     handleStaticNameServersPatch(iface_id, *staticNameServers,
1372f85837bfSRAJESWARAN THILLAIGOVINDAN                                                  asyncResp);
1373f85837bfSRAJESWARAN THILLAIGOVINDAN                 }
1374588c3f0dSKowalski, Kamil             });
1375588c3f0dSKowalski, Kamil     }
13769391bb9cSRapkiewicz, Pawel };
13779391bb9cSRapkiewicz, Pawel 
1378e439f0f8SKowalski, Kamil /**
13794a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
13804a0cb85cSEd Tanous  * Schema
1381e439f0f8SKowalski, Kamil  */
13821abe55efSEd Tanous class VlanNetworkInterface : public Node
13831abe55efSEd Tanous {
1384e439f0f8SKowalski, Kamil   public:
1385e439f0f8SKowalski, Kamil     /*
1386e439f0f8SKowalski, Kamil      * Default Constructor
1387e439f0f8SKowalski, Kamil      */
1388e439f0f8SKowalski, Kamil     template <typename CrowApp>
13891abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
13904a0cb85cSEd Tanous         Node(app,
13910f74e643SEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
13921abe55efSEd Tanous              std::string(), std::string())
13931abe55efSEd Tanous     {
1394e439f0f8SKowalski, Kamil         entityPrivileges = {
1395e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1396e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1397e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1398e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1399e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1400e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1401e439f0f8SKowalski, Kamil     }
1402e439f0f8SKowalski, Kamil 
1403e439f0f8SKowalski, Kamil   private:
14040f74e643SEd Tanous     void parseInterfaceData(
14050f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
14060f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
14074a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
14081abe55efSEd Tanous     {
1409e439f0f8SKowalski, Kamil         // Fill out obvious data...
14104a0cb85cSEd Tanous         json_response["Id"] = iface_id;
14114a0cb85cSEd Tanous         json_response["@odata.id"] =
14124a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
14134a0cb85cSEd Tanous             "/VLANs/" + iface_id;
1414e439f0f8SKowalski, Kamil 
14154a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
1416fda13ad2SSunitha Harish         if (!ethData.vlan_id.empty())
14174a0cb85cSEd Tanous         {
1418fda13ad2SSunitha Harish             json_response["VLANId"] = ethData.vlan_id.back();
14194a0cb85cSEd Tanous         }
1420e439f0f8SKowalski, Kamil     }
1421e439f0f8SKowalski, Kamil 
1422fda13ad2SSunitha Harish     bool verifyNames(const std::string &parent, const std::string &iface)
14231abe55efSEd Tanous     {
14241abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
14251abe55efSEd Tanous         {
1426927a505aSKowalski, Kamil             return false;
14271abe55efSEd Tanous         }
14281abe55efSEd Tanous         else
14291abe55efSEd Tanous         {
1430927a505aSKowalski, Kamil             return true;
1431927a505aSKowalski, Kamil         }
1432927a505aSKowalski, Kamil     }
1433927a505aSKowalski, Kamil 
1434e439f0f8SKowalski, Kamil     /**
1435e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1436e439f0f8SKowalski, Kamil      */
143755c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14381abe55efSEd Tanous                const std::vector<std::string> &params) override
14391abe55efSEd Tanous     {
14404a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14414a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
1442e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1443e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
1444e439f0f8SKowalski, Kamil         // impossible.
14451abe55efSEd Tanous         if (params.size() != 2)
14461abe55efSEd Tanous         {
1447f12894f8SJason M. Bills             messages::internalError(res);
1448e439f0f8SKowalski, Kamil             res.end();
1449e439f0f8SKowalski, Kamil             return;
1450e439f0f8SKowalski, Kamil         }
1451e439f0f8SKowalski, Kamil 
14524a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
14534a0cb85cSEd Tanous         const std::string &iface_id = params[1];
14540f74e643SEd Tanous         res.jsonValue["@odata.type"] =
14550f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
14560f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14570f74e643SEd Tanous             "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
14580f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
1459e439f0f8SKowalski, Kamil 
1460fda13ad2SSunitha Harish         if (!verifyNames(parent_iface_id, iface_id))
14611abe55efSEd Tanous         {
1462a434f2bdSEd Tanous             return;
1463a434f2bdSEd Tanous         }
1464a434f2bdSEd Tanous 
1465e439f0f8SKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1466e439f0f8SKowalski, Kamil         // preparation
14674a0cb85cSEd Tanous         getEthernetIfaceData(
1468fda13ad2SSunitha Harish             params[1],
1469fda13ad2SSunitha Harish             [this, asyncResp, parent_iface_id{std::string(params[0])},
1470fda13ad2SSunitha Harish              iface_id{std::string(params[1])}](
14714a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
14724a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1473fda13ad2SSunitha Harish                 if (success && ethData.vlan_id.size() != 0)
14741abe55efSEd Tanous                 {
14750f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
14760f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
14770f74e643SEd Tanous                                        ipv4Data);
14781abe55efSEd Tanous                 }
14791abe55efSEd Tanous                 else
14801abe55efSEd Tanous                 {
1481e439f0f8SKowalski, Kamil                     // ... otherwise return error
14821abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
14831abe55efSEd Tanous                     // object, and other errors
1484f12894f8SJason M. Bills                     messages::resourceNotFound(
1485f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1486e439f0f8SKowalski, Kamil                 }
1487e439f0f8SKowalski, Kamil             });
1488e439f0f8SKowalski, Kamil     }
1489e439f0f8SKowalski, Kamil 
149055c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
14911abe55efSEd Tanous                  const std::vector<std::string> &params) override
14921abe55efSEd Tanous     {
14934a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14941abe55efSEd Tanous         if (params.size() != 2)
14951abe55efSEd Tanous         {
1496f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1497e439f0f8SKowalski, Kamil             return;
1498e439f0f8SKowalski, Kamil         }
1499e439f0f8SKowalski, Kamil 
1500d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
150155c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1502927a505aSKowalski, Kamil 
1503fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
15041abe55efSEd Tanous         {
1505fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1506fda13ad2SSunitha Harish                                        ifaceId);
1507927a505aSKowalski, Kamil             return;
1508927a505aSKowalski, Kamil         }
1509927a505aSKowalski, Kamil 
15100627a2c7SEd Tanous         bool vlanEnable = false;
15110627a2c7SEd Tanous         uint64_t vlanId = 0;
15120627a2c7SEd Tanous 
15130627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
15140627a2c7SEd Tanous                                  vlanId))
15151abe55efSEd Tanous         {
1516927a505aSKowalski, Kamil             return;
1517927a505aSKowalski, Kamil         }
1518927a505aSKowalski, Kamil 
1519927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1520927a505aSKowalski, Kamil         // preparation
152108244d02SSunitha Harish         getEthernetIfaceData(params[1], [this, asyncResp,
152208244d02SSunitha Harish                                          parentIfaceId{std::string(params[0])},
152308244d02SSunitha Harish                                          ifaceId{std::string(params[1])},
152408244d02SSunitha Harish                                          &vlanEnable, &vlanId](
152508244d02SSunitha Harish                                             const bool &success,
152608244d02SSunitha Harish                                             const EthernetInterfaceData
152708244d02SSunitha Harish                                                 &ethData,
152808244d02SSunitha Harish                                             const boost::container::flat_set<
152908244d02SSunitha Harish                                                 IPv4AddressData> &ipv4Data) {
153008244d02SSunitha Harish             if (success && !ethData.vlan_id.empty())
153108244d02SSunitha Harish             {
153208244d02SSunitha Harish                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
153308244d02SSunitha Harish                                    ifaceId, ethData, ipv4Data);
153408244d02SSunitha Harish                 auto callback =
153508244d02SSunitha Harish                     [asyncResp](const boost::system::error_code ec) {
153608244d02SSunitha Harish                         if (ec)
153708244d02SSunitha Harish                         {
153808244d02SSunitha Harish                             messages::internalError(asyncResp->res);
153908244d02SSunitha Harish                         }
154008244d02SSunitha Harish                     };
154108244d02SSunitha Harish 
154208244d02SSunitha Harish                 if (vlanEnable == true)
154308244d02SSunitha Harish                 {
154408244d02SSunitha Harish                     crow::connections::systemBus->async_method_call(
154508244d02SSunitha Harish                         std::move(callback), "xyz.openbmc_project.Network",
154608244d02SSunitha Harish                         "/xyz/openbmc_project/network/" + ifaceId,
154708244d02SSunitha Harish                         "org.freedesktop.DBus.Properties", "Set",
154808244d02SSunitha Harish                         "xyz.openbmc_project.Network.VLAN", "Id",
154908244d02SSunitha Harish                         std::variant<uint32_t>(vlanId));
155008244d02SSunitha Harish                 }
155108244d02SSunitha Harish                 else
155208244d02SSunitha Harish                 {
155308244d02SSunitha Harish                     BMCWEB_LOG_DEBUG
155408244d02SSunitha Harish                         << "vlanEnable is false. Deleting the vlan interface";
155508244d02SSunitha Harish                     crow::connections::systemBus->async_method_call(
155608244d02SSunitha Harish                         std::move(callback), "xyz.openbmc_project.Network",
155708244d02SSunitha Harish                         std::string("/xyz/openbmc_project/network/") + ifaceId,
155808244d02SSunitha Harish                         "xyz.openbmc_project.Object.Delete", "Delete");
155908244d02SSunitha Harish                 }
156008244d02SSunitha Harish             }
156108244d02SSunitha Harish             else
15621abe55efSEd Tanous             {
15631abe55efSEd Tanous                 // TODO(Pawel)consider distinguish between non existing
15641abe55efSEd Tanous                 // object, and other errors
156508244d02SSunitha Harish                 messages::resourceNotFound(asyncResp->res,
156608244d02SSunitha Harish                                            "VLAN Network Interface", ifaceId);
1567927a505aSKowalski, Kamil                 return;
1568927a505aSKowalski, Kamil             }
1569927a505aSKowalski, Kamil         });
1570e439f0f8SKowalski, Kamil     }
1571e439f0f8SKowalski, Kamil 
157255c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
15731abe55efSEd Tanous                   const std::vector<std::string> &params) override
15741abe55efSEd Tanous     {
15754a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15761abe55efSEd Tanous         if (params.size() != 2)
15771abe55efSEd Tanous         {
1578f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1579e439f0f8SKowalski, Kamil             return;
1580e439f0f8SKowalski, Kamil         }
1581e439f0f8SKowalski, Kamil 
1582d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
158355c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1584927a505aSKowalski, Kamil 
1585fda13ad2SSunitha Harish         if (!verifyNames(parentIfaceId, ifaceId))
15861abe55efSEd Tanous         {
1587fda13ad2SSunitha Harish             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1588fda13ad2SSunitha Harish                                        ifaceId);
1589927a505aSKowalski, Kamil             return;
1590927a505aSKowalski, Kamil         }
1591927a505aSKowalski, Kamil 
1592927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1593927a505aSKowalski, Kamil         // preparation
1594f12894f8SJason M. Bills         getEthernetIfaceData(
1595fda13ad2SSunitha Harish             params[1],
1596fda13ad2SSunitha Harish             [this, asyncResp, parentIfaceId{std::string(params[0])},
1597fda13ad2SSunitha Harish              ifaceId{std::string(params[1])}](
1598f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
1599f12894f8SJason M. Bills                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1600fda13ad2SSunitha Harish                 if (success && !ethData.vlan_id.empty())
16011abe55efSEd Tanous                 {
16020f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
16030f74e643SEd Tanous                                        ifaceId, ethData, ipv4Data);
1604927a505aSKowalski, Kamil 
1605f12894f8SJason M. Bills                     auto callback =
1606f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
16071abe55efSEd Tanous                             if (ec)
16081abe55efSEd Tanous                             {
1609f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1610927a505aSKowalski, Kamil                             }
16114a0cb85cSEd Tanous                         };
16124a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
16134a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
16144a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
16154a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
16161abe55efSEd Tanous                 }
16171abe55efSEd Tanous                 else
16181abe55efSEd Tanous                 {
1619927a505aSKowalski, Kamil                     // ... otherwise return error
1620f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
1621f12894f8SJason M. Bills                     // object, and other errors
1622f12894f8SJason M. Bills                     messages::resourceNotFound(
1623f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1624927a505aSKowalski, Kamil                 }
1625927a505aSKowalski, Kamil             });
1626e439f0f8SKowalski, Kamil     }
1627e439f0f8SKowalski, Kamil };
1628e439f0f8SKowalski, Kamil 
1629e439f0f8SKowalski, Kamil /**
1630e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1631e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1632e439f0f8SKowalski, Kamil  */
16331abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
16341abe55efSEd Tanous {
1635e439f0f8SKowalski, Kamil   public:
1636e439f0f8SKowalski, Kamil     template <typename CrowApp>
16371abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
16384a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
16394a0cb85cSEd Tanous              std::string())
16401abe55efSEd Tanous     {
1641e439f0f8SKowalski, Kamil         entityPrivileges = {
1642e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1643e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1644e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1645e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1646e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1647e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1648e439f0f8SKowalski, Kamil     }
1649e439f0f8SKowalski, Kamil 
1650e439f0f8SKowalski, Kamil   private:
1651e439f0f8SKowalski, Kamil     /**
1652e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1653e439f0f8SKowalski, Kamil      */
165455c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16551abe55efSEd Tanous                const std::vector<std::string> &params) override
16561abe55efSEd Tanous     {
16574a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16581abe55efSEd Tanous         if (params.size() != 1)
16591abe55efSEd Tanous         {
1660e439f0f8SKowalski, Kamil             // This means there is a problem with the router
1661f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1662e439f0f8SKowalski, Kamil             return;
1663e439f0f8SKowalski, Kamil         }
1664e439f0f8SKowalski, Kamil 
16654a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1666e439f0f8SKowalski, Kamil 
16674a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
16681abe55efSEd Tanous         // preparation
1669f12894f8SJason M. Bills         getEthernetIfaceList(
167043b761d0SEd Tanous             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
16711abe55efSEd Tanous                 const bool &success,
16721abe55efSEd Tanous                 const std::vector<std::string> &iface_list) {
16734a0cb85cSEd Tanous                 if (!success)
16741abe55efSEd Tanous                 {
1675f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16764a0cb85cSEd Tanous                     return;
16771abe55efSEd Tanous                 }
16780f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
16790f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16800f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16810f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
16820f74e643SEd Tanous                     "/redfish/v1/$metadata"
16830f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16840f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16850f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
16860f74e643SEd Tanous                     "VLAN Network Interface Collection";
16874a0cb85cSEd Tanous 
16884a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
16894a0cb85cSEd Tanous 
16904a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
16911abe55efSEd Tanous                 {
16924a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
16934a0cb85cSEd Tanous                     {
16944a0cb85cSEd Tanous                         iface_array.push_back(
16954a0cb85cSEd Tanous                             {{"@odata.id",
16964a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
16974a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
1698e439f0f8SKowalski, Kamil                     }
1699e439f0f8SKowalski, Kamil                 }
1700e439f0f8SKowalski, Kamil 
17014a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
17024a0cb85cSEd Tanous                     iface_array.size();
17034a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
17044a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
17054a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17064a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
1707e439f0f8SKowalski, Kamil             });
1708e439f0f8SKowalski, Kamil     }
1709e439f0f8SKowalski, Kamil 
171055c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
17111abe55efSEd Tanous                 const std::vector<std::string> &params) override
17121abe55efSEd Tanous     {
17134a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17141abe55efSEd Tanous         if (params.size() != 1)
17151abe55efSEd Tanous         {
1716f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1717e439f0f8SKowalski, Kamil             return;
1718e439f0f8SKowalski, Kamil         }
1719fda13ad2SSunitha Harish         bool vlanEnable = false;
17200627a2c7SEd Tanous         uint32_t vlanId = 0;
1721fda13ad2SSunitha Harish         if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
1722fda13ad2SSunitha Harish                                  vlanEnable))
17231abe55efSEd Tanous         {
17244a0cb85cSEd Tanous             return;
1725e439f0f8SKowalski, Kamil         }
1726fda13ad2SSunitha Harish         // Need both vlanId and vlanEnable to service this request
1727fda13ad2SSunitha Harish         if (!vlanId)
1728fda13ad2SSunitha Harish         {
1729fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANId");
1730fda13ad2SSunitha Harish         }
1731fda13ad2SSunitha Harish         if (!vlanEnable)
1732fda13ad2SSunitha Harish         {
1733fda13ad2SSunitha Harish             messages::propertyMissing(asyncResp->res, "VLANEnable");
1734fda13ad2SSunitha Harish         }
1735fda13ad2SSunitha Harish         if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
1736fda13ad2SSunitha Harish         {
1737fda13ad2SSunitha Harish             return;
1738fda13ad2SSunitha Harish         }
1739fda13ad2SSunitha Harish 
17404a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
17414a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
17421abe55efSEd Tanous             if (ec)
17431abe55efSEd Tanous             {
17444a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
17454a0cb85cSEd Tanous                 // phosphor-network responses
1746f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17474a0cb85cSEd Tanous                 return;
17481abe55efSEd Tanous             }
1749f12894f8SJason M. Bills             messages::created(asyncResp->res);
1750e439f0f8SKowalski, Kamil         };
17514a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
17524a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
17534a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
17544a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
17550627a2c7SEd Tanous             rootInterfaceName, vlanId);
17564a0cb85cSEd Tanous     }
17574a0cb85cSEd Tanous };
17589391bb9cSRapkiewicz, Pawel } // namespace redfish
1759