xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 0627a2c77b7600a53c84bbfec49d04588c260608)
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>
259391bb9cSRapkiewicz, Pawel 
261abe55efSEd Tanous namespace redfish
271abe55efSEd Tanous {
289391bb9cSRapkiewicz, Pawel 
299391bb9cSRapkiewicz, Pawel /**
309391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
319391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
329391bb9cSRapkiewicz, Pawel  */
33aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
34aa2e59c1SEd Tanous     std::string,
35aa2e59c1SEd Tanous     sdbusplus::message::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<
43aa2e59c1SEd Tanous             std::string, sdbusplus::message::variant<
44aa2e59c1SEd Tanous                              std::string, bool, uint8_t, int16_t, uint16_t,
454a0cb85cSEd Tanous                              int32_t, uint32_t, int64_t, uint64_t, double>>>>>>;
464a0cb85cSEd Tanous 
474a0cb85cSEd Tanous enum class LinkType
484a0cb85cSEd Tanous {
494a0cb85cSEd Tanous     Local,
504a0cb85cSEd Tanous     Global
514a0cb85cSEd Tanous };
529391bb9cSRapkiewicz, Pawel 
539391bb9cSRapkiewicz, Pawel /**
549391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
559391bb9cSRapkiewicz, Pawel  */
561abe55efSEd Tanous struct IPv4AddressData
571abe55efSEd Tanous {
58179db1d7SKowalski, Kamil     std::string id;
594a0cb85cSEd Tanous     std::string address;
604a0cb85cSEd Tanous     std::string domain;
614a0cb85cSEd Tanous     std::string gateway;
629391bb9cSRapkiewicz, Pawel     std::string netmask;
639391bb9cSRapkiewicz, Pawel     std::string origin;
644a0cb85cSEd Tanous     LinkType linktype;
654a0cb85cSEd Tanous 
661abe55efSEd Tanous     bool operator<(const IPv4AddressData &obj) const
671abe55efSEd Tanous     {
684a0cb85cSEd Tanous         return id < obj.id;
691abe55efSEd Tanous     }
709391bb9cSRapkiewicz, Pawel };
719391bb9cSRapkiewicz, Pawel 
729391bb9cSRapkiewicz, Pawel /**
739391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
749391bb9cSRapkiewicz, Pawel  * available from DBus
759391bb9cSRapkiewicz, Pawel  */
761abe55efSEd Tanous struct EthernetInterfaceData
771abe55efSEd Tanous {
784a0cb85cSEd Tanous     uint32_t speed;
794a0cb85cSEd Tanous     bool auto_neg;
804a0cb85cSEd Tanous     std::string hostname;
814a0cb85cSEd Tanous     std::string default_gateway;
824a0cb85cSEd Tanous     std::string mac_address;
83a24526dcSEd Tanous     std::optional<uint32_t> vlan_id;
849391bb9cSRapkiewicz, Pawel };
859391bb9cSRapkiewicz, Pawel 
869391bb9cSRapkiewicz, Pawel // Helper function that changes bits netmask notation (i.e. /24)
879391bb9cSRapkiewicz, Pawel // into full dot notation
881abe55efSEd Tanous inline std::string getNetmask(unsigned int bits)
891abe55efSEd Tanous {
909391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
919391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
929391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
939391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
949391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
959391bb9cSRapkiewicz, Pawel     return netmask;
969391bb9cSRapkiewicz, Pawel }
979391bb9cSRapkiewicz, Pawel 
984a0cb85cSEd Tanous inline std::string
994a0cb85cSEd Tanous     translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
1004a0cb85cSEd Tanous                                         bool isIPv4)
1011abe55efSEd Tanous {
1024a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
1031abe55efSEd Tanous     {
1044a0cb85cSEd Tanous         return "Static";
1059391bb9cSRapkiewicz, Pawel     }
1064a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
1071abe55efSEd Tanous     {
1084a0cb85cSEd Tanous         if (isIPv4)
1091abe55efSEd Tanous         {
1104a0cb85cSEd Tanous             return "IPv4LinkLocal";
1111abe55efSEd Tanous         }
1121abe55efSEd Tanous         else
1131abe55efSEd Tanous         {
1144a0cb85cSEd Tanous             return "LinkLocal";
1159391bb9cSRapkiewicz, Pawel         }
1169391bb9cSRapkiewicz, Pawel     }
1174a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
1181abe55efSEd Tanous     {
1194a0cb85cSEd Tanous         if (isIPv4)
1204a0cb85cSEd Tanous         {
1214a0cb85cSEd Tanous             return "DHCP";
1224a0cb85cSEd Tanous         }
1234a0cb85cSEd Tanous         else
1244a0cb85cSEd Tanous         {
1254a0cb85cSEd Tanous             return "DHCPv6";
1264a0cb85cSEd Tanous         }
1274a0cb85cSEd Tanous     }
1284a0cb85cSEd Tanous     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
1294a0cb85cSEd Tanous     {
1304a0cb85cSEd Tanous         return "SLAAC";
1314a0cb85cSEd Tanous     }
1324a0cb85cSEd Tanous     return "";
1334a0cb85cSEd Tanous }
1344a0cb85cSEd Tanous 
1354a0cb85cSEd Tanous inline std::string
1364a0cb85cSEd Tanous     translateAddressOriginRedfishToDbus(const std::string &inputOrigin)
1374a0cb85cSEd Tanous {
1384a0cb85cSEd Tanous     if (inputOrigin == "Static")
1394a0cb85cSEd Tanous     {
1404a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
1414a0cb85cSEd Tanous     }
1424a0cb85cSEd Tanous     if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6")
1434a0cb85cSEd Tanous     {
1444a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
1454a0cb85cSEd Tanous     }
1464a0cb85cSEd Tanous     if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal")
1474a0cb85cSEd Tanous     {
1484a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal";
1494a0cb85cSEd Tanous     }
1504a0cb85cSEd Tanous     if (inputOrigin == "SLAAC")
1514a0cb85cSEd Tanous     {
1524a0cb85cSEd Tanous         return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC";
1534a0cb85cSEd Tanous     }
1544a0cb85cSEd Tanous     return "";
1554a0cb85cSEd Tanous }
1564a0cb85cSEd Tanous 
1574a0cb85cSEd Tanous inline void extractEthernetInterfaceData(const std::string &ethiface_id,
1584a0cb85cSEd Tanous                                          const GetManagedObjects &dbus_data,
1594a0cb85cSEd Tanous                                          EthernetInterfaceData &ethData)
1604a0cb85cSEd Tanous {
1614a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
1624a0cb85cSEd Tanous     {
1634a0cb85cSEd Tanous         if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
1644a0cb85cSEd Tanous         {
1654a0cb85cSEd Tanous             for (const auto &ifacePair : objpath.second)
1664a0cb85cSEd Tanous             {
1674a0cb85cSEd Tanous                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
1684a0cb85cSEd Tanous                 {
1694a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
1704a0cb85cSEd Tanous                     {
1714a0cb85cSEd Tanous                         if (propertyPair.first == "MACAddress")
1724a0cb85cSEd Tanous                         {
1734a0cb85cSEd Tanous                             const std::string *mac =
1741b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
1751b6b96c5SEd Tanous                                     std::string>(&propertyPair.second);
1764a0cb85cSEd Tanous                             if (mac != nullptr)
1774a0cb85cSEd Tanous                             {
1784a0cb85cSEd Tanous                                 ethData.mac_address = *mac;
1794a0cb85cSEd Tanous                             }
1804a0cb85cSEd Tanous                         }
1814a0cb85cSEd Tanous                     }
1824a0cb85cSEd Tanous                 }
1834a0cb85cSEd Tanous                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
1844a0cb85cSEd Tanous                 {
1854a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
1864a0cb85cSEd Tanous                     {
1874a0cb85cSEd Tanous                         if (propertyPair.first == "Id")
1884a0cb85cSEd Tanous                         {
1891b6b96c5SEd Tanous                             const uint32_t *id =
1901b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
1911b6b96c5SEd Tanous                                     uint32_t>(&propertyPair.second);
1924a0cb85cSEd Tanous                             if (id != nullptr)
1934a0cb85cSEd Tanous                             {
1944a0cb85cSEd Tanous                                 ethData.vlan_id = *id;
1954a0cb85cSEd Tanous                             }
1964a0cb85cSEd Tanous                         }
1974a0cb85cSEd Tanous                     }
1984a0cb85cSEd Tanous                 }
1994a0cb85cSEd Tanous                 else if (ifacePair.first ==
2004a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2014a0cb85cSEd Tanous                 {
2024a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2034a0cb85cSEd Tanous                     {
2044a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2054a0cb85cSEd Tanous                         {
2064a0cb85cSEd Tanous                             const bool *auto_neg =
2071b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<bool>(
2081b6b96c5SEd Tanous                                     &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 =
2171b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
2181b6b96c5SEd Tanous                                     uint32_t>(&propertyPair.second);
2194a0cb85cSEd Tanous                             if (speed != nullptr)
2204a0cb85cSEd Tanous                             {
2214a0cb85cSEd Tanous                                 ethData.speed = *speed;
2224a0cb85cSEd Tanous                             }
2234a0cb85cSEd Tanous                         }
2244a0cb85cSEd Tanous                     }
2254a0cb85cSEd Tanous                 }
2264a0cb85cSEd Tanous                 else if (ifacePair.first ==
2274a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.SystemConfiguration")
2284a0cb85cSEd Tanous                 {
2294a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2304a0cb85cSEd Tanous                     {
2314a0cb85cSEd Tanous                         if (propertyPair.first == "HostName")
2324a0cb85cSEd Tanous                         {
2334a0cb85cSEd Tanous                             const std::string *hostname =
2341b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
2351b6b96c5SEd Tanous                                     std::string>(&propertyPair.second);
2364a0cb85cSEd Tanous                             if (hostname != nullptr)
2374a0cb85cSEd Tanous                             {
2384a0cb85cSEd Tanous                                 ethData.hostname = *hostname;
2394a0cb85cSEd Tanous                             }
2404a0cb85cSEd Tanous                         }
2414a0cb85cSEd Tanous                         else if (propertyPair.first == "DefaultGateway")
2424a0cb85cSEd Tanous                         {
2434a0cb85cSEd Tanous                             const std::string *defaultGateway =
2441b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
2451b6b96c5SEd Tanous                                     std::string>(&propertyPair.second);
2464a0cb85cSEd Tanous                             if (defaultGateway != nullptr)
2474a0cb85cSEd Tanous                             {
2484a0cb85cSEd Tanous                                 ethData.default_gateway = *defaultGateway;
2494a0cb85cSEd Tanous                             }
2504a0cb85cSEd Tanous                         }
2514a0cb85cSEd Tanous                     }
2524a0cb85cSEd Tanous                 }
2534a0cb85cSEd Tanous             }
2544a0cb85cSEd Tanous         }
2554a0cb85cSEd Tanous     }
2564a0cb85cSEd Tanous }
2574a0cb85cSEd Tanous 
2584a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
2594a0cb85cSEd Tanous inline void
2604a0cb85cSEd Tanous     extractIPData(const std::string &ethiface_id,
2614a0cb85cSEd Tanous                   const GetManagedObjects &dbus_data,
2624a0cb85cSEd Tanous                   boost::container::flat_set<IPv4AddressData> &ipv4_config)
2634a0cb85cSEd Tanous {
2644a0cb85cSEd Tanous     const std::string ipv4PathStart =
2654a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
2664a0cb85cSEd Tanous 
2674a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
2684a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
2694a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
2704a0cb85cSEd Tanous     {
2714a0cb85cSEd Tanous         // Check if proper pattern for object path appears
2724a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
2734a0cb85cSEd Tanous         {
2744a0cb85cSEd Tanous             for (auto &interface : objpath.second)
2754a0cb85cSEd Tanous             {
2764a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
2774a0cb85cSEd Tanous                 {
2784a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
2794a0cb85cSEd Tanous                     // appropriate
2804a0cb85cSEd Tanous                     std::pair<
2814a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
2824a0cb85cSEd Tanous                         bool>
2834a0cb85cSEd Tanous                         it = ipv4_config.insert(
2844a0cb85cSEd Tanous                             {objpath.first.str.substr(ipv4PathStart.size())});
2854a0cb85cSEd Tanous                     IPv4AddressData &ipv4_address = *it.first;
2864a0cb85cSEd Tanous                     for (auto &property : interface.second)
2874a0cb85cSEd Tanous                     {
2884a0cb85cSEd Tanous                         if (property.first == "Address")
2894a0cb85cSEd Tanous                         {
2904a0cb85cSEd Tanous                             const std::string *address =
2911b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
2921b6b96c5SEd Tanous                                     std::string>(&property.second);
2934a0cb85cSEd Tanous                             if (address != nullptr)
2944a0cb85cSEd Tanous                             {
2954a0cb85cSEd Tanous                                 ipv4_address.address = *address;
2964a0cb85cSEd Tanous                             }
2974a0cb85cSEd Tanous                         }
2984a0cb85cSEd Tanous                         else if (property.first == "Gateway")
2994a0cb85cSEd Tanous                         {
3004a0cb85cSEd Tanous                             const std::string *gateway =
3011b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
3021b6b96c5SEd Tanous                                     std::string>(&property.second);
3034a0cb85cSEd Tanous                             if (gateway != nullptr)
3044a0cb85cSEd Tanous                             {
3054a0cb85cSEd Tanous                                 ipv4_address.gateway = *gateway;
3064a0cb85cSEd Tanous                             }
3074a0cb85cSEd Tanous                         }
3084a0cb85cSEd Tanous                         else if (property.first == "Origin")
3094a0cb85cSEd Tanous                         {
3104a0cb85cSEd Tanous                             const std::string *origin =
3111b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<
3121b6b96c5SEd Tanous                                     std::string>(&property.second);
3134a0cb85cSEd Tanous                             if (origin != nullptr)
3144a0cb85cSEd Tanous                             {
3154a0cb85cSEd Tanous                                 ipv4_address.origin =
3164a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
3174a0cb85cSEd Tanous                                                                         true);
3184a0cb85cSEd Tanous                             }
3194a0cb85cSEd Tanous                         }
3204a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
3214a0cb85cSEd Tanous                         {
3224a0cb85cSEd Tanous                             const uint8_t *mask =
3231b6b96c5SEd Tanous                                 sdbusplus::message::variant_ns::get_if<uint8_t>(
3241b6b96c5SEd Tanous                                     &property.second);
3254a0cb85cSEd Tanous                             if (mask != nullptr)
3264a0cb85cSEd Tanous                             {
3274a0cb85cSEd Tanous                                 // convert it to the string
3284a0cb85cSEd Tanous                                 ipv4_address.netmask = getNetmask(*mask);
3294a0cb85cSEd Tanous                             }
3304a0cb85cSEd Tanous                         }
3314a0cb85cSEd Tanous                         else
3324a0cb85cSEd Tanous                         {
3334a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
3344a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
3354a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
3364a0cb85cSEd Tanous                         }
3374a0cb85cSEd Tanous                     }
3384a0cb85cSEd Tanous                     // Check if given address is local, or global
3394a0cb85cSEd Tanous                     ipv4_address.linktype =
3404a0cb85cSEd Tanous                         boost::starts_with(ipv4_address.address, "169.254.")
3414a0cb85cSEd Tanous                             ? LinkType::Global
3424a0cb85cSEd Tanous                             : LinkType::Local;
3434a0cb85cSEd Tanous                 }
3444a0cb85cSEd Tanous             }
3454a0cb85cSEd Tanous         }
3464a0cb85cSEd Tanous     }
3474a0cb85cSEd Tanous }
348588c3f0dSKowalski, Kamil 
349588c3f0dSKowalski, Kamil /**
350588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
351588c3f0dSKowalski, Kamil  *
352588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
353588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
354588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
355588c3f0dSKowalski, Kamil  *
356588c3f0dSKowalski, Kamil  * @return None.
357588c3f0dSKowalski, Kamil  */
358588c3f0dSKowalski, Kamil template <typename CallbackFunc>
3594a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
3601abe55efSEd Tanous                   CallbackFunc &&callback)
3611abe55efSEd Tanous {
36255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
363588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
364588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
365588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
366588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
367588c3f0dSKowalski, Kamil         sdbusplus::message::variant<uint32_t>(inputVlanId));
3684a0cb85cSEd Tanous }
369588c3f0dSKowalski, Kamil 
370588c3f0dSKowalski, Kamil /**
371179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
372179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
373179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
374179db1d7SKowalski, Kamil  *
375179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
376179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
377179db1d7SKowalski, Kamil  *
378179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
379179db1d7SKowalski, Kamil  */
3804a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
3811abe55efSEd Tanous                                        uint8_t *bits = nullptr)
3821abe55efSEd Tanous {
383179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
384179db1d7SKowalski, Kamil 
385179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
386179db1d7SKowalski, Kamil 
3874a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
3881abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
3891abe55efSEd Tanous     {
390179db1d7SKowalski, Kamil         return false;
391179db1d7SKowalski, Kamil     }
392179db1d7SKowalski, Kamil 
3931abe55efSEd Tanous     if (bits != nullptr)
3941abe55efSEd Tanous     {
395179db1d7SKowalski, Kamil         *bits = 0;
396179db1d7SKowalski, Kamil     }
397179db1d7SKowalski, Kamil 
398179db1d7SKowalski, Kamil     char *endPtr;
399179db1d7SKowalski, Kamil     long previousValue = 255;
400179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
4011abe55efSEd Tanous     for (const std::string &byte : bytesInMask)
4021abe55efSEd Tanous     {
4031abe55efSEd Tanous         if (byte.empty())
4041abe55efSEd Tanous         {
4051db9ca37SKowalski, Kamil             return false;
4061db9ca37SKowalski, Kamil         }
4071db9ca37SKowalski, Kamil 
408179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
4091db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
410179db1d7SKowalski, Kamil 
4114a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
4124a0cb85cSEd Tanous         // is not 100% number
4131abe55efSEd Tanous         if (*endPtr != '\0')
4141abe55efSEd Tanous         {
415179db1d7SKowalski, Kamil             return false;
416179db1d7SKowalski, Kamil         }
417179db1d7SKowalski, Kamil 
418179db1d7SKowalski, Kamil         // Value should be contained in byte
4191abe55efSEd Tanous         if (value < 0 || value > 255)
4201abe55efSEd Tanous         {
421179db1d7SKowalski, Kamil             return false;
422179db1d7SKowalski, Kamil         }
423179db1d7SKowalski, Kamil 
4241abe55efSEd Tanous         if (bits != nullptr)
4251abe55efSEd Tanous         {
426179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
4271abe55efSEd Tanous             if (previousValue != 255 && value != 0)
4281abe55efSEd Tanous             {
429179db1d7SKowalski, Kamil                 return false;
430179db1d7SKowalski, Kamil             }
431179db1d7SKowalski, Kamil 
432179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
433179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
434179db1d7SKowalski, Kamil 
435179db1d7SKowalski, Kamil             // Count bits
4361abe55efSEd Tanous             for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
4371abe55efSEd Tanous             {
4381abe55efSEd Tanous                 if (value & (1 << bitIdx))
4391abe55efSEd Tanous                 {
4401abe55efSEd Tanous                     if (firstZeroInByteHit)
4411abe55efSEd Tanous                     {
442179db1d7SKowalski, Kamil                         // Continuity not preserved
443179db1d7SKowalski, Kamil                         return false;
4441abe55efSEd Tanous                     }
4451abe55efSEd Tanous                     else
4461abe55efSEd Tanous                     {
447179db1d7SKowalski, Kamil                         (*bits)++;
448179db1d7SKowalski, Kamil                     }
4491abe55efSEd Tanous                 }
4501abe55efSEd Tanous                 else
4511abe55efSEd Tanous                 {
452179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
453179db1d7SKowalski, Kamil                 }
454179db1d7SKowalski, Kamil             }
455179db1d7SKowalski, Kamil         }
456179db1d7SKowalski, Kamil 
457179db1d7SKowalski, Kamil         previousValue = value;
458179db1d7SKowalski, Kamil     }
459179db1d7SKowalski, Kamil 
460179db1d7SKowalski, Kamil     return true;
461179db1d7SKowalski, Kamil }
462179db1d7SKowalski, Kamil 
463179db1d7SKowalski, Kamil /**
464179db1d7SKowalski, Kamil  * @brief Changes IPv4 address type property (Address, Gateway)
465179db1d7SKowalski, Kamil  *
466179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be modified
4674a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be modified
468179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of modified IP
469179db1d7SKowalski, Kamil  * @param[in] name        Name of field in JSON representation
470179db1d7SKowalski, Kamil  * @param[in] newValue    New value that should be written
471179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
472179db1d7SKowalski, Kamil  *
473179db1d7SKowalski, Kamil  * @return true if give IP is valid and has been sent do D-Bus, false
474179db1d7SKowalski, Kamil  * otherwise
475179db1d7SKowalski, Kamil  */
4764a0cb85cSEd Tanous inline void changeIPv4AddressProperty(
4774a0cb85cSEd Tanous     const std::string &ifaceId, int ipIdx, const std::string &ipHash,
4784a0cb85cSEd Tanous     const std::string &name, const std::string &newValue,
4794a0cb85cSEd Tanous     const std::shared_ptr<AsyncResp> asyncResp)
4801abe55efSEd Tanous {
4814a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, name{std::string(name)},
4824a0cb85cSEd Tanous                      newValue{std::move(newValue)}](
4831abe55efSEd Tanous                         const boost::system::error_code ec) {
4841abe55efSEd Tanous         if (ec)
4851abe55efSEd Tanous         {
486a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
4871abe55efSEd Tanous         }
4881abe55efSEd Tanous         else
4891abe55efSEd Tanous         {
4904a0cb85cSEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
491179db1d7SKowalski, Kamil         }
492179db1d7SKowalski, Kamil     };
493179db1d7SKowalski, Kamil 
49455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
495179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
496179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
497179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
498179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", name,
499179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValue));
5004a0cb85cSEd Tanous }
501179db1d7SKowalski, Kamil 
502179db1d7SKowalski, Kamil /**
503179db1d7SKowalski, Kamil  * @brief Changes IPv4 address origin property
504179db1d7SKowalski, Kamil  *
505179db1d7SKowalski, Kamil  * @param[in] ifaceId       Id of interface whose IP should be modified
5064a0cb85cSEd Tanous  * @param[in] ipIdx         Index of IP in input array that should be
5071abe55efSEd Tanous  * modified
508179db1d7SKowalski, Kamil  * @param[in] ipHash        DBus Hash id of modified IP
509179db1d7SKowalski, Kamil  * @param[in] newValue      New value in Redfish format
510179db1d7SKowalski, Kamil  * @param[in] newValueDbus  New value in D-Bus format
511179db1d7SKowalski, Kamil  * @param[io] asyncResp     Response object that will be returned to client
512179db1d7SKowalski, Kamil  *
513179db1d7SKowalski, Kamil  * @return true if give IP is valid and has been sent do D-Bus, false
514179db1d7SKowalski, Kamil  * otherwise
515179db1d7SKowalski, Kamil  */
5164a0cb85cSEd Tanous inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
5171abe55efSEd Tanous                              const std::string &ipHash,
5181abe55efSEd Tanous                              const std::string &newValue,
519179db1d7SKowalski, Kamil                              const std::string &newValueDbus,
5204a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
5211abe55efSEd Tanous {
5224a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
5231abe55efSEd Tanous                         const boost::system::error_code ec) {
5241abe55efSEd Tanous         if (ec)
5251abe55efSEd Tanous         {
526a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5271abe55efSEd Tanous         }
5281abe55efSEd Tanous         else
5291abe55efSEd Tanous         {
5304a0cb85cSEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
531179db1d7SKowalski, Kamil                 newValue;
532179db1d7SKowalski, Kamil         }
533179db1d7SKowalski, Kamil     };
534179db1d7SKowalski, Kamil 
53555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
536179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
537179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
538179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
539179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "Origin",
540179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValueDbus));
5414a0cb85cSEd Tanous }
542179db1d7SKowalski, Kamil 
543179db1d7SKowalski, Kamil /**
544179db1d7SKowalski, Kamil  * @brief Modifies SubnetMask for given IP
545179db1d7SKowalski, Kamil  *
546179db1d7SKowalski, Kamil  * @param[in] ifaceId      Id of interface whose IP should be modified
5474a0cb85cSEd Tanous  * @param[in] ipIdx        Index of IP in input array that should be
5481abe55efSEd Tanous  * modified
549179db1d7SKowalski, Kamil  * @param[in] ipHash       DBus Hash id of modified IP
550179db1d7SKowalski, Kamil  * @param[in] newValueStr  Mask in dot notation as string
551179db1d7SKowalski, Kamil  * @param[in] newValue     Mask as PrefixLength in bitcount
552179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
553179db1d7SKowalski, Kamil  *
554179db1d7SKowalski, Kamil  * @return None
555179db1d7SKowalski, Kamil  */
5564a0cb85cSEd Tanous inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
5574a0cb85cSEd Tanous                                          const std::string &ipHash,
5584a0cb85cSEd Tanous                                          const std::string &newValueStr,
5594a0cb85cSEd Tanous                                          uint8_t &newValue,
5604a0cb85cSEd Tanous                                          std::shared_ptr<AsyncResp> asyncResp)
5611abe55efSEd Tanous {
5624a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
5631abe55efSEd Tanous                         const boost::system::error_code ec) {
5641abe55efSEd Tanous         if (ec)
5651abe55efSEd Tanous         {
566a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5671abe55efSEd Tanous         }
5681abe55efSEd Tanous         else
5691abe55efSEd Tanous         {
57055c7b7a2SEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
571179db1d7SKowalski, Kamil                 newValueStr;
572179db1d7SKowalski, Kamil         }
573179db1d7SKowalski, Kamil     };
574179db1d7SKowalski, Kamil 
57555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
576179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
577179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
578179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
579179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "PrefixLength",
580179db1d7SKowalski, Kamil         sdbusplus::message::variant<uint8_t>(newValue));
5814a0cb85cSEd Tanous }
582588c3f0dSKowalski, Kamil 
583588c3f0dSKowalski, Kamil /**
584588c3f0dSKowalski, Kamil  * @brief Sets given HostName of the machine through D-Bus
585588c3f0dSKowalski, Kamil  *
586588c3f0dSKowalski, Kamil  * @param[in] newHostname   New name that HostName will be changed to
587588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
588588c3f0dSKowalski, Kamil  *
589588c3f0dSKowalski, Kamil  * @return None.
590588c3f0dSKowalski, Kamil  */
591588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5921abe55efSEd Tanous void setHostName(const std::string &newHostname, CallbackFunc &&callback)
5931abe55efSEd Tanous {
59455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
595588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
596588c3f0dSKowalski, Kamil         "/xyz/openbmc_project/network/config",
597588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
598588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
599588c3f0dSKowalski, Kamil         sdbusplus::message::variant<std::string>(newHostname));
6004a0cb85cSEd Tanous }
601588c3f0dSKowalski, Kamil 
602588c3f0dSKowalski, Kamil /**
603179db1d7SKowalski, Kamil  * @brief Deletes given IPv4
604179db1d7SKowalski, Kamil  *
605179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6064a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
607179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
608179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
609179db1d7SKowalski, Kamil  *
610179db1d7SKowalski, Kamil  * @return None
611179db1d7SKowalski, Kamil  */
6124a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
613179db1d7SKowalski, Kamil                        unsigned int ipIdx,
6144a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
6151abe55efSEd Tanous {
61655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6174a0cb85cSEd Tanous         [ipIdx, asyncResp](const boost::system::error_code ec) {
6181abe55efSEd Tanous             if (ec)
6191abe55efSEd Tanous             {
620a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
6211abe55efSEd Tanous             }
6221abe55efSEd Tanous             else
6231abe55efSEd Tanous             {
62455c7b7a2SEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
625179db1d7SKowalski, Kamil             }
626179db1d7SKowalski, Kamil         },
627179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
628179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
629179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
630179db1d7SKowalski, Kamil }
631179db1d7SKowalski, Kamil 
632179db1d7SKowalski, Kamil /**
633179db1d7SKowalski, Kamil  * @brief Creates IPv4 with given data
634179db1d7SKowalski, Kamil  *
635179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6364a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
637179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
638179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
639179db1d7SKowalski, Kamil  *
640179db1d7SKowalski, Kamil  * @return None
641179db1d7SKowalski, Kamil  */
6424a0cb85cSEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
643179db1d7SKowalski, Kamil                        uint8_t subnetMask, const std::string &gateway,
644179db1d7SKowalski, Kamil                        const std::string &address,
6454a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
6461abe55efSEd Tanous {
6474a0cb85cSEd Tanous     auto createIpHandler = [ipIdx,
6484a0cb85cSEd Tanous                             asyncResp](const boost::system::error_code ec) {
6491abe55efSEd Tanous         if (ec)
6501abe55efSEd Tanous         {
651a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
652179db1d7SKowalski, Kamil         }
653179db1d7SKowalski, Kamil     };
654179db1d7SKowalski, Kamil 
65555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
656179db1d7SKowalski, Kamil         std::move(createIpHandler), "xyz.openbmc_project.Network",
657179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
658179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
659179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
660179db1d7SKowalski, Kamil         gateway);
661179db1d7SKowalski, Kamil }
662179db1d7SKowalski, Kamil 
663179db1d7SKowalski, Kamil /**
664179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
665179db1d7SKowalski, Kamil  * Object
666179db1d7SKowalski, Kamil  * from EntityManager Network Manager
6674a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
668179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
669179db1d7SKowalski, Kamil  * into JSON
670179db1d7SKowalski, Kamil  */
671179db1d7SKowalski, Kamil template <typename CallbackFunc>
6724a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
6731abe55efSEd Tanous                           CallbackFunc &&callback)
6741abe55efSEd Tanous {
67555c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6764a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
6771abe55efSEd Tanous             const boost::system::error_code error_code,
6784a0cb85cSEd Tanous             const GetManagedObjects &resp) {
67955c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
6804a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
681179db1d7SKowalski, Kamil 
6821abe55efSEd Tanous             if (error_code)
6831abe55efSEd Tanous             {
68455c7b7a2SEd Tanous                 callback(false, ethData, ipv4Data);
685179db1d7SKowalski, Kamil                 return;
686179db1d7SKowalski, Kamil             }
687179db1d7SKowalski, Kamil 
6884a0cb85cSEd Tanous             extractEthernetInterfaceData(ethiface_id, resp, ethData);
6894a0cb85cSEd Tanous             extractIPData(ethiface_id, resp, ipv4Data);
690179db1d7SKowalski, Kamil 
691179db1d7SKowalski, Kamil             // Fix global GW
6921abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
6931abe55efSEd Tanous             {
6944a0cb85cSEd Tanous                 if ((ipv4.linktype == LinkType::Global) &&
6954a0cb85cSEd Tanous                     (ipv4.gateway == "0.0.0.0"))
6961abe55efSEd Tanous                 {
6974a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
698179db1d7SKowalski, Kamil                 }
699179db1d7SKowalski, Kamil             }
700179db1d7SKowalski, Kamil 
7014a0cb85cSEd Tanous             // Finally make a callback with usefull data
70255c7b7a2SEd Tanous             callback(true, ethData, ipv4Data);
703179db1d7SKowalski, Kamil         },
704179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
705179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
706179db1d7SKowalski, Kamil };
707179db1d7SKowalski, Kamil 
708179db1d7SKowalski, Kamil /**
7099391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
7109391bb9cSRapkiewicz, Pawel  * Manager
7111abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
7121abe55efSEd Tanous  * into JSON.
7139391bb9cSRapkiewicz, Pawel  */
7149391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
7151abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
7161abe55efSEd Tanous {
71755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
7184a0cb85cSEd Tanous         [callback{std::move(callback)}](
7199391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
7204a0cb85cSEd Tanous             GetManagedObjects &resp) {
7211abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
7221abe55efSEd Tanous             // ethernet interfaces
7234a0cb85cSEd Tanous             std::vector<std::string> iface_list;
7244a0cb85cSEd Tanous             iface_list.reserve(resp.size());
7251abe55efSEd Tanous             if (error_code)
7261abe55efSEd Tanous             {
7274a0cb85cSEd Tanous                 callback(false, iface_list);
7289391bb9cSRapkiewicz, Pawel                 return;
7299391bb9cSRapkiewicz, Pawel             }
7309391bb9cSRapkiewicz, Pawel 
7319391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
7324a0cb85cSEd Tanous             for (const auto &objpath : resp)
7331abe55efSEd Tanous             {
7349391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
7354a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
7361abe55efSEd Tanous                 {
7371abe55efSEd Tanous                     // If interface is
7384a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
7394a0cb85cSEd Tanous                     // what we're looking for.
7409391bb9cSRapkiewicz, Pawel                     if (interface.first ==
7411abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
7421abe55efSEd Tanous                     {
7434a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
7444a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
7454a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
7464a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
7471abe55efSEd Tanous                         {
7489391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
7494a0cb85cSEd Tanous                             iface_list.emplace_back(
7504a0cb85cSEd Tanous                                 iface_id.substr(last_pos + 1));
7519391bb9cSRapkiewicz, Pawel                         }
7529391bb9cSRapkiewicz, Pawel                     }
7539391bb9cSRapkiewicz, Pawel                 }
7549391bb9cSRapkiewicz, Pawel             }
755a434f2bdSEd Tanous             // Finally make a callback with useful data
7564a0cb85cSEd Tanous             callback(true, iface_list);
7579391bb9cSRapkiewicz, Pawel         },
758aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
759aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
7609391bb9cSRapkiewicz, Pawel };
7619391bb9cSRapkiewicz, Pawel 
7629391bb9cSRapkiewicz, Pawel /**
7639391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
7649391bb9cSRapkiewicz, Pawel  */
7651abe55efSEd Tanous class EthernetCollection : public Node
7661abe55efSEd Tanous {
7679391bb9cSRapkiewicz, Pawel   public:
7684a0cb85cSEd Tanous     template <typename CrowApp>
7691abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
7704a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
7711abe55efSEd Tanous     {
772588c3f0dSKowalski, Kamil         entityPrivileges = {
773588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
774e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
775e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
776e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
777e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
778e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
7799391bb9cSRapkiewicz, Pawel     }
7809391bb9cSRapkiewicz, Pawel 
7819391bb9cSRapkiewicz, Pawel   private:
7829391bb9cSRapkiewicz, Pawel     /**
7839391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
7849391bb9cSRapkiewicz, Pawel      */
78555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
7861abe55efSEd Tanous                const std::vector<std::string> &params) override
7871abe55efSEd Tanous     {
7880f74e643SEd Tanous         res.jsonValue["@odata.type"] =
7890f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
7900f74e643SEd Tanous         res.jsonValue["@odata.context"] =
7910f74e643SEd Tanous             "/redfish/v1/"
7920f74e643SEd Tanous             "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
7930f74e643SEd Tanous         res.jsonValue["@odata.id"] =
7940f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
7950f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
7960f74e643SEd Tanous         res.jsonValue["Description"] =
7970f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
7980f74e643SEd Tanous 
7994a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
8001abe55efSEd Tanous         // preparation
801f12894f8SJason M. Bills         getEthernetIfaceList(
802f12894f8SJason M. Bills             [&res](const bool &success,
8031abe55efSEd Tanous                    const std::vector<std::string> &iface_list) {
8044a0cb85cSEd Tanous                 if (!success)
8051abe55efSEd Tanous                 {
806f12894f8SJason M. Bills                     messages::internalError(res);
8074a0cb85cSEd Tanous                     res.end();
8084a0cb85cSEd Tanous                     return;
8094a0cb85cSEd Tanous                 }
8104a0cb85cSEd Tanous 
8114a0cb85cSEd Tanous                 nlohmann::json &iface_array = res.jsonValue["Members"];
8124a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
8134a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
8141abe55efSEd Tanous                 {
8154a0cb85cSEd Tanous                     iface_array.push_back(
8164a0cb85cSEd Tanous                         {{"@odata.id",
8174a0cb85cSEd Tanous                           "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
8184a0cb85cSEd Tanous                               iface_item}});
8199391bb9cSRapkiewicz, Pawel                 }
8204a0cb85cSEd Tanous 
8214a0cb85cSEd Tanous                 res.jsonValue["Members@odata.count"] = iface_array.size();
8224a0cb85cSEd Tanous                 res.jsonValue["@odata.id"] =
8234a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
8249391bb9cSRapkiewicz, Pawel                 res.end();
8259391bb9cSRapkiewicz, Pawel             });
8269391bb9cSRapkiewicz, Pawel     }
8279391bb9cSRapkiewicz, Pawel };
8289391bb9cSRapkiewicz, Pawel 
8299391bb9cSRapkiewicz, Pawel /**
8309391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
8319391bb9cSRapkiewicz, Pawel  */
8321abe55efSEd Tanous class EthernetInterface : public Node
8331abe55efSEd Tanous {
8349391bb9cSRapkiewicz, Pawel   public:
8359391bb9cSRapkiewicz, Pawel     /*
8369391bb9cSRapkiewicz, Pawel      * Default Constructor
8379391bb9cSRapkiewicz, Pawel      */
8384a0cb85cSEd Tanous     template <typename CrowApp>
8391abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
8404a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
8411abe55efSEd Tanous              std::string())
8421abe55efSEd Tanous     {
843588c3f0dSKowalski, Kamil         entityPrivileges = {
844588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
845e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
846e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
847e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
848e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
849e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8509391bb9cSRapkiewicz, Pawel     }
8519391bb9cSRapkiewicz, Pawel 
852e439f0f8SKowalski, Kamil     // TODO(kkowalsk) Find a suitable class/namespace for this
853*0627a2c7SEd Tanous     static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable,
854*0627a2c7SEd Tanous                                 uint64_t vlanId,
8554a0cb85cSEd Tanous                                 const EthernetInterfaceData &ethData,
8564a0cb85cSEd Tanous                                 const std::shared_ptr<AsyncResp> asyncResp)
8571abe55efSEd Tanous     {
8584a0cb85cSEd Tanous         if (!ethData.vlan_id)
8591abe55efSEd Tanous         {
860e439f0f8SKowalski, Kamil             // This interface is not a VLAN. Cannot do anything with it
861e439f0f8SKowalski, Kamil             // TODO(kkowalsk) Change this message
862a08b46ccSJason M. Bills             messages::propertyNotWritable(asyncResp->res, "VLANEnable");
863588c3f0dSKowalski, Kamil 
864588c3f0dSKowalski, Kamil             return;
865588c3f0dSKowalski, Kamil         }
866588c3f0dSKowalski, Kamil 
867588c3f0dSKowalski, Kamil         // VLAN is configured on the interface
868*0627a2c7SEd Tanous         if (vlanEnable == true)
8691abe55efSEd Tanous         {
870588c3f0dSKowalski, Kamil             // Change VLAN Id
871*0627a2c7SEd Tanous             asyncResp->res.jsonValue["VLANId"] = vlanId;
8724a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
8731abe55efSEd Tanous                 if (ec)
8741abe55efSEd Tanous                 {
875f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8761abe55efSEd Tanous                 }
8771abe55efSEd Tanous                 else
8781abe55efSEd Tanous                 {
8794a0cb85cSEd Tanous                     asyncResp->res.jsonValue["VLANEnable"] = true;
880e439f0f8SKowalski, Kamil                 }
8814a0cb85cSEd Tanous             };
8824a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
8834a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
8844a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
8854a0cb85cSEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
8864a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.VLAN", "Id",
887*0627a2c7SEd Tanous                 sdbusplus::message::variant<uint32_t>(vlanId));
8881abe55efSEd Tanous         }
8894a0cb85cSEd Tanous         else
8901abe55efSEd Tanous         {
8914a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
8921abe55efSEd Tanous                 if (ec)
8931abe55efSEd Tanous                 {
894f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8954a0cb85cSEd Tanous                     return;
8961abe55efSEd Tanous                 }
8974a0cb85cSEd Tanous                 asyncResp->res.jsonValue["VLANEnable"] = false;
8984a0cb85cSEd Tanous             };
8994a0cb85cSEd Tanous 
9004a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
9014a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
9024a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
9034a0cb85cSEd Tanous                 "xyz.openbmc_project.Object.Delete", "Delete");
904588c3f0dSKowalski, Kamil         }
905588c3f0dSKowalski, Kamil     }
906588c3f0dSKowalski, Kamil 
907e439f0f8SKowalski, Kamil   private:
908588c3f0dSKowalski, Kamil     void handleHostnamePatch(const nlohmann::json &input,
9094a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
9101abe55efSEd Tanous     {
9114a0cb85cSEd Tanous         const std::string *newHostname = input.get_ptr<const std::string *>();
9124a0cb85cSEd Tanous         if (newHostname == nullptr)
9131abe55efSEd Tanous         {
914f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
915a08b46ccSJason M. Bills                                              "HostName");
9164a0cb85cSEd Tanous             return;
9174a0cb85cSEd Tanous         }
9184a0cb85cSEd Tanous 
9194a0cb85cSEd Tanous         // Change hostname
920a08b46ccSJason M. Bills         setHostName(*newHostname,
921a08b46ccSJason M. Bills                     [asyncResp, newHostname{std::string(*newHostname)}](
9224a0cb85cSEd Tanous                         const boost::system::error_code ec) {
9234a0cb85cSEd Tanous                         if (ec)
9244a0cb85cSEd Tanous                         {
925a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
9261abe55efSEd Tanous                         }
9271abe55efSEd Tanous                         else
9281abe55efSEd Tanous                         {
92955c7b7a2SEd Tanous                             asyncResp->res.jsonValue["HostName"] = newHostname;
930588c3f0dSKowalski, Kamil                         }
931588c3f0dSKowalski, Kamil                     });
932588c3f0dSKowalski, Kamil     }
933588c3f0dSKowalski, Kamil 
9344a0cb85cSEd Tanous     void handleIPv4Patch(
9354a0cb85cSEd Tanous         const std::string &ifaceId, const nlohmann::json &input,
9364a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
9374a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
9381abe55efSEd Tanous     {
9391abe55efSEd Tanous         if (!input.is_array())
9401abe55efSEd Tanous         {
941f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
942a08b46ccSJason M. Bills                                              "IPv4Addresses");
943179db1d7SKowalski, Kamil             return;
944179db1d7SKowalski, Kamil         }
945179db1d7SKowalski, Kamil 
946179db1d7SKowalski, Kamil         // According to Redfish PATCH definition, size must be at least equal
9474a0cb85cSEd Tanous         if (input.size() < ipv4Data.size())
9481abe55efSEd Tanous         {
949a08b46ccSJason M. Bills             messages::propertyValueFormatError(asyncResp->res, input.dump(),
950a08b46ccSJason M. Bills                                                "IPv4Addresses");
951179db1d7SKowalski, Kamil             return;
952179db1d7SKowalski, Kamil         }
953179db1d7SKowalski, Kamil 
9544a0cb85cSEd Tanous         int entryIdx = 0;
9554a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
9564a0cb85cSEd Tanous             ipv4Data.begin();
9574a0cb85cSEd Tanous         for (const nlohmann::json &thisJson : input)
9581abe55efSEd Tanous         {
9594a0cb85cSEd Tanous             std::string pathString =
960a08b46ccSJason M. Bills                 "IPv4Addresses/" + std::to_string(entryIdx);
961179db1d7SKowalski, Kamil             // Check that entry is not of some unexpected type
9624a0cb85cSEd Tanous             if (!thisJson.is_object() && !thisJson.is_null())
9631abe55efSEd Tanous             {
964a08b46ccSJason M. Bills                 messages::propertyValueTypeError(asyncResp->res,
965a08b46ccSJason M. Bills                                                  thisJson.dump(),
966a08b46ccSJason M. Bills                                                  pathString + "/IPv4Address");
967179db1d7SKowalski, Kamil 
968179db1d7SKowalski, Kamil                 continue;
969179db1d7SKowalski, Kamil             }
970179db1d7SKowalski, Kamil 
9714a0cb85cSEd Tanous             nlohmann::json::const_iterator addressFieldIt =
9724a0cb85cSEd Tanous                 thisJson.find("Address");
9734a0cb85cSEd Tanous             const std::string *addressField = nullptr;
9744a0cb85cSEd Tanous             if (addressFieldIt != thisJson.end())
9751abe55efSEd Tanous             {
9764a0cb85cSEd Tanous                 addressField = addressFieldIt->get_ptr<const std::string *>();
9774a0cb85cSEd Tanous                 if (addressField == nullptr)
9781abe55efSEd Tanous                 {
979a08b46ccSJason M. Bills                     messages::propertyValueFormatError(asyncResp->res,
980a08b46ccSJason M. Bills                                                        addressFieldIt->dump(),
9814a0cb85cSEd Tanous                                                        pathString + "/Address");
982179db1d7SKowalski, Kamil                     continue;
983179db1d7SKowalski, Kamil                 }
9841abe55efSEd Tanous                 else
9851abe55efSEd Tanous                 {
9864a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*addressField))
9874a0cb85cSEd Tanous                     {
988f12894f8SJason M. Bills                         messages::propertyValueFormatError(
989a08b46ccSJason M. Bills                             asyncResp->res, *addressField,
9904a0cb85cSEd Tanous                             pathString + "/Address");
9914a0cb85cSEd Tanous                         continue;
9924a0cb85cSEd Tanous                     }
9934a0cb85cSEd Tanous                 }
9944a0cb85cSEd Tanous             }
9954a0cb85cSEd Tanous 
996a24526dcSEd Tanous             std::optional<uint8_t> prefixLength;
9974a0cb85cSEd Tanous             const std::string *subnetField = nullptr;
9984a0cb85cSEd Tanous             nlohmann::json::const_iterator subnetFieldIt =
9994a0cb85cSEd Tanous                 thisJson.find("SubnetMask");
10004a0cb85cSEd Tanous             if (subnetFieldIt != thisJson.end())
10014a0cb85cSEd Tanous             {
10024a0cb85cSEd Tanous                 subnetField = subnetFieldIt->get_ptr<const std::string *>();
10034a0cb85cSEd Tanous                 if (subnetField == nullptr)
10044a0cb85cSEd Tanous                 {
1005f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1006a08b46ccSJason M. Bills                         asyncResp->res, *subnetField,
10074a0cb85cSEd Tanous                         pathString + "/SubnetMask");
10084a0cb85cSEd Tanous                     continue;
10094a0cb85cSEd Tanous                 }
10104a0cb85cSEd Tanous                 else
10114a0cb85cSEd Tanous                 {
10124a0cb85cSEd Tanous                     prefixLength = 0;
10134a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetField,
10144a0cb85cSEd Tanous                                                     &*prefixLength))
10154a0cb85cSEd Tanous                     {
1016f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1017a08b46ccSJason M. Bills                             asyncResp->res, *subnetField,
10184a0cb85cSEd Tanous                             pathString + "/SubnetMask");
10194a0cb85cSEd Tanous                         continue;
10204a0cb85cSEd Tanous                     }
10214a0cb85cSEd Tanous                 }
10224a0cb85cSEd Tanous             }
10234a0cb85cSEd Tanous 
10244a0cb85cSEd Tanous             std::string addressOriginInDBusFormat;
10254a0cb85cSEd Tanous             const std::string *addressOriginField = nullptr;
10264a0cb85cSEd Tanous             nlohmann::json::const_iterator addressOriginFieldIt =
10274a0cb85cSEd Tanous                 thisJson.find("AddressOrigin");
10284a0cb85cSEd Tanous             if (addressOriginFieldIt != thisJson.end())
10294a0cb85cSEd Tanous             {
10304a0cb85cSEd Tanous                 const std::string *addressOriginField =
10314a0cb85cSEd Tanous                     addressOriginFieldIt->get_ptr<const std::string *>();
10324a0cb85cSEd Tanous                 if (addressOriginField == nullptr)
10334a0cb85cSEd Tanous                 {
1034f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1035a08b46ccSJason M. Bills                         asyncResp->res, *addressOriginField,
10364a0cb85cSEd Tanous                         pathString + "/AddressOrigin");
10374a0cb85cSEd Tanous                     continue;
10384a0cb85cSEd Tanous                 }
10394a0cb85cSEd Tanous                 else
10404a0cb85cSEd Tanous                 {
10414a0cb85cSEd Tanous                     // Get Address origin in proper format
10424a0cb85cSEd Tanous                     addressOriginInDBusFormat =
10434a0cb85cSEd Tanous                         translateAddressOriginRedfishToDbus(
10444a0cb85cSEd Tanous                             *addressOriginField);
10454a0cb85cSEd Tanous                     if (addressOriginInDBusFormat.empty())
10464a0cb85cSEd Tanous                     {
10474a0cb85cSEd Tanous                         messages::propertyValueNotInList(
1048f12894f8SJason M. Bills                             asyncResp->res, *addressOriginField,
1049a08b46ccSJason M. Bills                             pathString + "/AddressOrigin");
10504a0cb85cSEd Tanous                         continue;
10514a0cb85cSEd Tanous                     }
10524a0cb85cSEd Tanous                 }
10534a0cb85cSEd Tanous             }
10544a0cb85cSEd Tanous 
10554a0cb85cSEd Tanous             nlohmann::json::const_iterator gatewayFieldIt =
10564a0cb85cSEd Tanous                 thisJson.find("Gateway");
10574a0cb85cSEd Tanous             const std::string *gatewayField = nullptr;
10584a0cb85cSEd Tanous             if (gatewayFieldIt != thisJson.end())
10594a0cb85cSEd Tanous             {
10604a0cb85cSEd Tanous                 const std::string *gatewayField =
10614a0cb85cSEd Tanous                     gatewayFieldIt->get_ptr<const std::string *>();
10624a0cb85cSEd Tanous                 if (gatewayField == nullptr ||
10634a0cb85cSEd Tanous                     !ipv4VerifyIpAndGetBitcount(*gatewayField))
10644a0cb85cSEd Tanous                 {
1065a08b46ccSJason M. Bills                     messages::propertyValueFormatError(
1066a08b46ccSJason M. Bills                         asyncResp->res, *gatewayField, pathString + "/Gateway");
10674a0cb85cSEd Tanous                     continue;
10684a0cb85cSEd Tanous                 }
10694a0cb85cSEd Tanous             }
10704a0cb85cSEd Tanous 
10714a0cb85cSEd Tanous             // if a vlan already exists, modify the existing
10724a0cb85cSEd Tanous             if (thisData != ipv4Data.end())
10734a0cb85cSEd Tanous             {
10741abe55efSEd Tanous                 // Existing object that should be modified/deleted/remain
10751abe55efSEd Tanous                 // unchanged
10764a0cb85cSEd Tanous                 if (thisJson.is_null())
10771abe55efSEd Tanous                 {
10784a0cb85cSEd Tanous                     auto callback = [entryIdx{std::to_string(entryIdx)},
10794a0cb85cSEd Tanous                                      asyncResp](
10804a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
10814a0cb85cSEd Tanous                         if (ec)
10824a0cb85cSEd Tanous                         {
1083a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
10844a0cb85cSEd Tanous                             return;
10851abe55efSEd Tanous                         }
10864a0cb85cSEd Tanous                         asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
10874a0cb85cSEd Tanous                             nullptr;
10884a0cb85cSEd Tanous                     };
10894a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
10904a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
10914a0cb85cSEd Tanous                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
10924a0cb85cSEd Tanous                             thisData->id,
10934a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
1094179db1d7SKowalski, Kamil                 }
10954a0cb85cSEd Tanous                 else if (thisJson.is_object())
10964a0cb85cSEd Tanous                 {
1097179db1d7SKowalski, Kamil                     // Apply changes
10984a0cb85cSEd Tanous                     if (addressField != nullptr)
10991abe55efSEd Tanous                     {
11004a0cb85cSEd Tanous                         auto callback =
11014a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11024a0cb85cSEd Tanous                              addressField{std::string(*addressField)}](
11034a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11044a0cb85cSEd Tanous                                 if (ec)
11051abe55efSEd Tanous                                 {
1106a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11074a0cb85cSEd Tanous                                     return;
11084a0cb85cSEd Tanous                                 }
11094a0cb85cSEd Tanous                                 asyncResp->res
11104a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11114a0cb85cSEd Tanous                                         entryIdx)]["Address"] = addressField;
11124a0cb85cSEd Tanous                             };
11134a0cb85cSEd Tanous 
11144a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11154a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11164a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11174a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11184a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11194a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Address",
11204a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11214a0cb85cSEd Tanous                                 *addressField));
1122179db1d7SKowalski, Kamil                     }
1123179db1d7SKowalski, Kamil 
11244a0cb85cSEd Tanous                     if (prefixLength && subnetField != nullptr)
11251abe55efSEd Tanous                     {
11264a0cb85cSEd Tanous                         changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
11274a0cb85cSEd Tanous                                                      thisData->id, *subnetField,
11284a0cb85cSEd Tanous                                                      *prefixLength, asyncResp);
1129179db1d7SKowalski, Kamil                     }
1130179db1d7SKowalski, Kamil 
11314a0cb85cSEd Tanous                     if (!addressOriginInDBusFormat.empty() &&
11324a0cb85cSEd Tanous                         addressOriginField != nullptr)
11331abe55efSEd Tanous                     {
11344a0cb85cSEd Tanous                         changeIPv4Origin(ifaceId, entryIdx, thisData->id,
11354a0cb85cSEd Tanous                                          *addressOriginField,
11364a0cb85cSEd Tanous                                          addressOriginInDBusFormat, asyncResp);
1137179db1d7SKowalski, Kamil                     }
1138179db1d7SKowalski, Kamil 
11394a0cb85cSEd Tanous                     if (gatewayField != nullptr)
11401abe55efSEd Tanous                     {
11414a0cb85cSEd Tanous                         auto callback =
11424a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11434a0cb85cSEd Tanous                              gatewayField{std::string(*gatewayField)}](
11444a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11454a0cb85cSEd Tanous                                 if (ec)
11461abe55efSEd Tanous                                 {
1147a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11484a0cb85cSEd Tanous                                     return;
11494a0cb85cSEd Tanous                                 }
11504a0cb85cSEd Tanous                                 asyncResp->res
11514a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11524a0cb85cSEd Tanous                                         entryIdx)]["Gateway"] =
11534a0cb85cSEd Tanous                                     std::move(gatewayField);
11544a0cb85cSEd Tanous                             };
11554a0cb85cSEd Tanous 
11564a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11574a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11584a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11594a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11604a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11614a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Gateway",
11624a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11634a0cb85cSEd Tanous                                 *gatewayField));
11644a0cb85cSEd Tanous                     }
11654a0cb85cSEd Tanous                 }
11664a0cb85cSEd Tanous                 thisData++;
11671abe55efSEd Tanous             }
11681abe55efSEd Tanous             else
11691abe55efSEd Tanous             {
11704a0cb85cSEd Tanous                 // Create IPv4 with provided data
11714a0cb85cSEd Tanous                 if (gatewayField == nullptr)
11721abe55efSEd Tanous                 {
1173a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11744a0cb85cSEd Tanous                                               pathString + "/Gateway");
11754a0cb85cSEd Tanous                     continue;
11764a0cb85cSEd Tanous                 }
11774a0cb85cSEd Tanous 
11784a0cb85cSEd Tanous                 if (addressField == nullptr)
11791abe55efSEd Tanous                 {
1180a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11814a0cb85cSEd Tanous                                               pathString + "/Address");
11824a0cb85cSEd Tanous                     continue;
11834a0cb85cSEd Tanous                 }
11844a0cb85cSEd Tanous 
11854a0cb85cSEd Tanous                 if (!prefixLength)
11861abe55efSEd Tanous                 {
1187a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11884a0cb85cSEd Tanous                                               pathString + "/SubnetMask");
11894a0cb85cSEd Tanous                     continue;
1190588c3f0dSKowalski, Kamil                 }
1191588c3f0dSKowalski, Kamil 
11924a0cb85cSEd Tanous                 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
11934a0cb85cSEd Tanous                            *addressField, asyncResp);
11944a0cb85cSEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
11954a0cb85cSEd Tanous             }
11964a0cb85cSEd Tanous             entryIdx++;
11974a0cb85cSEd Tanous         }
11984a0cb85cSEd Tanous     }
11994a0cb85cSEd Tanous 
12000f74e643SEd Tanous     void parseInterfaceData(
12010f74e643SEd Tanous         nlohmann::json &json_response, const std::string &iface_id,
12020f74e643SEd Tanous         const EthernetInterfaceData &ethData,
12034a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
12044a0cb85cSEd Tanous     {
12054a0cb85cSEd Tanous         json_response["Id"] = iface_id;
12064a0cb85cSEd Tanous         json_response["@odata.id"] =
12074a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
12084a0cb85cSEd Tanous 
12094a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
12104a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
12114a0cb85cSEd Tanous         if (!ethData.hostname.empty())
12124a0cb85cSEd Tanous         {
12134a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
12144a0cb85cSEd Tanous         }
12154a0cb85cSEd Tanous 
12164a0cb85cSEd Tanous         nlohmann::json &vlanObj = json_response["VLAN"];
12174a0cb85cSEd Tanous         if (ethData.vlan_id)
12184a0cb85cSEd Tanous         {
12194a0cb85cSEd Tanous             vlanObj["VLANEnable"] = true;
12204a0cb85cSEd Tanous             vlanObj["VLANId"] = *ethData.vlan_id;
12214a0cb85cSEd Tanous         }
12224a0cb85cSEd Tanous         else
12234a0cb85cSEd Tanous         {
12244a0cb85cSEd Tanous             vlanObj["VLANEnable"] = false;
12254a0cb85cSEd Tanous             vlanObj["VLANId"] = 0;
12264a0cb85cSEd Tanous         }
12274a0cb85cSEd Tanous 
12284a0cb85cSEd Tanous         if (ipv4Data.size() > 0)
12294a0cb85cSEd Tanous         {
12304a0cb85cSEd Tanous             nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
12314a0cb85cSEd Tanous             ipv4_array = nlohmann::json::array();
12324a0cb85cSEd Tanous             for (auto &ipv4_config : ipv4Data)
12334a0cb85cSEd Tanous             {
12344a0cb85cSEd Tanous                 if (!ipv4_config.address.empty())
12354a0cb85cSEd Tanous                 {
12364a0cb85cSEd Tanous                     ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
12374a0cb85cSEd Tanous                                           {"SubnetMask", ipv4_config.netmask},
12384a0cb85cSEd Tanous                                           {"Address", ipv4_config.address}});
12394a0cb85cSEd Tanous 
12404a0cb85cSEd Tanous                     if (!ipv4_config.gateway.empty())
12414a0cb85cSEd Tanous                     {
12424a0cb85cSEd Tanous                         ipv4_array.back()["Gateway"] = ipv4_config.gateway;
12434a0cb85cSEd Tanous                     }
12444a0cb85cSEd Tanous                 }
12454a0cb85cSEd Tanous             }
12464a0cb85cSEd Tanous         }
1247588c3f0dSKowalski, Kamil     }
1248588c3f0dSKowalski, Kamil 
12499391bb9cSRapkiewicz, Pawel     /**
12509391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
12519391bb9cSRapkiewicz, Pawel      */
125255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12531abe55efSEd Tanous                const std::vector<std::string> &params) override
12541abe55efSEd Tanous     {
12554a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12561abe55efSEd Tanous         if (params.size() != 1)
12571abe55efSEd Tanous         {
1258f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
12599391bb9cSRapkiewicz, Pawel             return;
12609391bb9cSRapkiewicz, Pawel         }
12619391bb9cSRapkiewicz, Pawel 
12624a0cb85cSEd Tanous         getEthernetIfaceData(
12634a0cb85cSEd Tanous             params[0],
12644a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
12654a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
12664a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
12674a0cb85cSEd Tanous                 if (!success)
12681abe55efSEd Tanous                 {
12691abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
12701abe55efSEd Tanous                     // object, and other errors
1271f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1272f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
12734a0cb85cSEd Tanous                     return;
12749391bb9cSRapkiewicz, Pawel                 }
12750f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
12760f74e643SEd Tanous                     "#EthernetInterface.v1_2_0.EthernetInterface";
12770f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
12780f74e643SEd Tanous                     "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
12790f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
12800f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
12810f74e643SEd Tanous                     "Management Network Interface";
12820f74e643SEd Tanous 
12830f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
12840f74e643SEd Tanous                                    ipv4Data);
12859391bb9cSRapkiewicz, Pawel             });
12869391bb9cSRapkiewicz, Pawel     }
12879391bb9cSRapkiewicz, Pawel 
128855c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
12891abe55efSEd Tanous                  const std::vector<std::string> &params) override
12901abe55efSEd Tanous     {
12914a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12921abe55efSEd Tanous         if (params.size() != 1)
12931abe55efSEd Tanous         {
1294f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1295588c3f0dSKowalski, Kamil             return;
1296588c3f0dSKowalski, Kamil         }
1297588c3f0dSKowalski, Kamil 
12984a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1299588c3f0dSKowalski, Kamil 
1300*0627a2c7SEd Tanous         std::optional<nlohmann::json> vlan;
1301*0627a2c7SEd Tanous         std::optional<nlohmann::json> hostname;
1302*0627a2c7SEd Tanous         std::optional<nlohmann::json> ipv4Addresses;
1303*0627a2c7SEd Tanous         std::optional<nlohmann::json> ipv6Addresses;
1304*0627a2c7SEd Tanous 
1305*0627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname,
1306*0627a2c7SEd Tanous                                  "IPv4Addresses", ipv4Addresses,
1307*0627a2c7SEd Tanous                                  "IPv6Addresses", ipv6Addresses))
13081abe55efSEd Tanous         {
1309588c3f0dSKowalski, Kamil             return;
1310588c3f0dSKowalski, Kamil         }
1311*0627a2c7SEd Tanous         std::optional<uint64_t> vlanId = 0;
1312*0627a2c7SEd Tanous         std::optional<bool> vlanEnable = false;
1313*0627a2c7SEd Tanous         if (vlan)
1314*0627a2c7SEd Tanous         {
1315*0627a2c7SEd Tanous             if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable,
1316*0627a2c7SEd Tanous                                      "VLANId", vlanId))
1317*0627a2c7SEd Tanous             {
1318*0627a2c7SEd Tanous                 return;
1319*0627a2c7SEd Tanous             }
1320*0627a2c7SEd Tanous             // Need both vlanId and vlanEnable to service this request
1321*0627a2c7SEd Tanous             if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
1322*0627a2c7SEd Tanous             {
1323*0627a2c7SEd Tanous                 if (vlanId)
1324*0627a2c7SEd Tanous                 {
1325*0627a2c7SEd Tanous                     messages::propertyMissing(asyncResp->res, "VLANEnable");
1326*0627a2c7SEd Tanous                 }
1327*0627a2c7SEd Tanous                 else
1328*0627a2c7SEd Tanous                 {
1329*0627a2c7SEd Tanous                     messages::propertyMissing(asyncResp->res, "VLANId");
1330*0627a2c7SEd Tanous                 }
1331*0627a2c7SEd Tanous 
1332*0627a2c7SEd Tanous                 return;
1333*0627a2c7SEd Tanous             }
1334*0627a2c7SEd Tanous         }
1335588c3f0dSKowalski, Kamil 
13364a0cb85cSEd Tanous         // Get single eth interface data, and call the below callback for JSON
1337588c3f0dSKowalski, Kamil         // preparation
13384a0cb85cSEd Tanous         getEthernetIfaceData(
13394a0cb85cSEd Tanous             iface_id,
1340*0627a2c7SEd Tanous             [this, asyncResp, iface_id, vlanId, vlanEnable,
1341*0627a2c7SEd Tanous              hostname = std::move(hostname),
1342*0627a2c7SEd Tanous              ipv4Addresses = std::move(ipv4Addresses),
1343*0627a2c7SEd Tanous              ipv6Addresses = std::move(ipv6Addresses)](
13444a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13454a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13461abe55efSEd Tanous                 if (!success)
13471abe55efSEd Tanous                 {
1348588c3f0dSKowalski, Kamil                     // ... otherwise return error
13491abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13501abe55efSEd Tanous                     // object, and other errors
1351f12894f8SJason M. Bills                     messages::resourceNotFound(
1352f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1353588c3f0dSKowalski, Kamil                     return;
1354588c3f0dSKowalski, Kamil                 }
1355588c3f0dSKowalski, Kamil 
13560f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
13570f74e643SEd Tanous                                    ipv4Data);
1358588c3f0dSKowalski, Kamil 
1359*0627a2c7SEd Tanous                 if (vlanId && vlanEnable)
13601abe55efSEd Tanous                 {
1361*0627a2c7SEd Tanous                     handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData,
13624a0cb85cSEd Tanous                                     asyncResp);
13631abe55efSEd Tanous                 }
1364*0627a2c7SEd Tanous 
1365*0627a2c7SEd Tanous                 if (hostname)
13661abe55efSEd Tanous                 {
1367*0627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
13681abe55efSEd Tanous                 }
1369*0627a2c7SEd Tanous 
1370*0627a2c7SEd Tanous                 if (ipv4Addresses)
13711abe55efSEd Tanous                 {
1372*0627a2c7SEd Tanous                     handleIPv4Patch(iface_id, *ipv4Addresses, ipv4Data,
1373179db1d7SKowalski, Kamil                                     asyncResp);
13741abe55efSEd Tanous                 }
1375*0627a2c7SEd Tanous 
1376*0627a2c7SEd Tanous                 if (ipv6Addresses)
13771abe55efSEd Tanous                 {
1378179db1d7SKowalski, Kamil                     // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1379a08b46ccSJason M. Bills                     messages::propertyNotWritable(asyncResp->res,
1380*0627a2c7SEd Tanous                                                   "IPv6Addresses");
1381588c3f0dSKowalski, Kamil                 }
1382588c3f0dSKowalski, Kamil             });
1383588c3f0dSKowalski, Kamil     }
13849391bb9cSRapkiewicz, Pawel };
13859391bb9cSRapkiewicz, Pawel 
1386e439f0f8SKowalski, Kamil /**
13874a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
13884a0cb85cSEd Tanous  * Schema
1389e439f0f8SKowalski, Kamil  */
13901abe55efSEd Tanous class VlanNetworkInterface : public Node
13911abe55efSEd Tanous {
1392e439f0f8SKowalski, Kamil   public:
1393e439f0f8SKowalski, Kamil     /*
1394e439f0f8SKowalski, Kamil      * Default Constructor
1395e439f0f8SKowalski, Kamil      */
1396e439f0f8SKowalski, Kamil     template <typename CrowApp>
13971abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
13984a0cb85cSEd Tanous         Node(app,
13990f74e643SEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
14001abe55efSEd Tanous              std::string(), std::string())
14011abe55efSEd Tanous     {
1402e439f0f8SKowalski, Kamil         entityPrivileges = {
1403e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1404e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1405e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1406e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1407e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1408e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1409e439f0f8SKowalski, Kamil     }
1410e439f0f8SKowalski, Kamil 
1411e439f0f8SKowalski, Kamil   private:
14120f74e643SEd Tanous     void parseInterfaceData(
14130f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
14140f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
14154a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
14161abe55efSEd Tanous     {
1417e439f0f8SKowalski, Kamil         // Fill out obvious data...
14184a0cb85cSEd Tanous         json_response["Id"] = iface_id;
14194a0cb85cSEd Tanous         json_response["@odata.id"] =
14204a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
14214a0cb85cSEd Tanous             "/VLANs/" + iface_id;
1422e439f0f8SKowalski, Kamil 
14234a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
14244a0cb85cSEd Tanous         if (ethData.vlan_id)
14254a0cb85cSEd Tanous         {
14264a0cb85cSEd Tanous             json_response["VLANId"] = *ethData.vlan_id;
14274a0cb85cSEd Tanous         }
1428e439f0f8SKowalski, Kamil     }
1429e439f0f8SKowalski, Kamil 
143055c7b7a2SEd Tanous     bool verifyNames(crow::Response &res, const std::string &parent,
14311abe55efSEd Tanous                      const std::string &iface)
14321abe55efSEd Tanous     {
1433f12894f8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14341abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
14351abe55efSEd Tanous         {
1436f12894f8SJason M. Bills             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1437f12894f8SJason M. Bills                                        iface);
1438927a505aSKowalski, Kamil             return false;
14391abe55efSEd Tanous         }
14401abe55efSEd Tanous         else
14411abe55efSEd Tanous         {
1442927a505aSKowalski, Kamil             return true;
1443927a505aSKowalski, Kamil         }
1444927a505aSKowalski, Kamil     }
1445927a505aSKowalski, Kamil 
1446e439f0f8SKowalski, Kamil     /**
1447e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1448e439f0f8SKowalski, Kamil      */
144955c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14501abe55efSEd Tanous                const std::vector<std::string> &params) override
14511abe55efSEd Tanous     {
14524a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14534a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
1454e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1455e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
1456e439f0f8SKowalski, Kamil         // impossible.
14571abe55efSEd Tanous         if (params.size() != 2)
14581abe55efSEd Tanous         {
1459f12894f8SJason M. Bills             messages::internalError(res);
1460e439f0f8SKowalski, Kamil             res.end();
1461e439f0f8SKowalski, Kamil             return;
1462e439f0f8SKowalski, Kamil         }
1463e439f0f8SKowalski, Kamil 
14644a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
14654a0cb85cSEd Tanous         const std::string &iface_id = params[1];
14660f74e643SEd Tanous         res.jsonValue["@odata.type"] =
14670f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
14680f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14690f74e643SEd Tanous             "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
14700f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
1471e439f0f8SKowalski, Kamil 
14724a0cb85cSEd Tanous         if (!verifyNames(res, parent_iface_id, iface_id))
14731abe55efSEd Tanous         {
1474a434f2bdSEd Tanous             return;
1475a434f2bdSEd Tanous         }
1476a434f2bdSEd Tanous 
1477e439f0f8SKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1478e439f0f8SKowalski, Kamil         // preparation
14794a0cb85cSEd Tanous         getEthernetIfaceData(
14804a0cb85cSEd Tanous             iface_id,
14814a0cb85cSEd Tanous             [this, asyncResp, parent_iface_id, iface_id](
14824a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
14834a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
14844a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
14851abe55efSEd Tanous                 {
14860f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
14870f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
14880f74e643SEd Tanous                                        ipv4Data);
14891abe55efSEd Tanous                 }
14901abe55efSEd Tanous                 else
14911abe55efSEd Tanous                 {
1492e439f0f8SKowalski, Kamil                     // ... otherwise return error
14931abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
14941abe55efSEd Tanous                     // object, and other errors
1495f12894f8SJason M. Bills                     messages::resourceNotFound(
1496f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1497e439f0f8SKowalski, Kamil                 }
1498e439f0f8SKowalski, Kamil             });
1499e439f0f8SKowalski, Kamil     }
1500e439f0f8SKowalski, Kamil 
150155c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
15021abe55efSEd Tanous                  const std::vector<std::string> &params) override
15031abe55efSEd Tanous     {
15044a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15051abe55efSEd Tanous         if (params.size() != 2)
15061abe55efSEd Tanous         {
1507f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1508e439f0f8SKowalski, Kamil             return;
1509e439f0f8SKowalski, Kamil         }
1510e439f0f8SKowalski, Kamil 
1511d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
151255c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1513927a505aSKowalski, Kamil 
15141abe55efSEd Tanous         if (!verifyNames(res, parentIfaceId, ifaceId))
15151abe55efSEd Tanous         {
1516927a505aSKowalski, Kamil             return;
1517927a505aSKowalski, Kamil         }
1518927a505aSKowalski, Kamil 
1519*0627a2c7SEd Tanous         bool vlanEnable = false;
1520*0627a2c7SEd Tanous         uint64_t vlanId = 0;
1521*0627a2c7SEd Tanous 
1522*0627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
1523*0627a2c7SEd Tanous                                  vlanId))
15241abe55efSEd Tanous         {
1525927a505aSKowalski, Kamil             return;
1526927a505aSKowalski, Kamil         }
1527927a505aSKowalski, Kamil 
1528927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1529927a505aSKowalski, Kamil         // preparation
15304a0cb85cSEd Tanous         getEthernetIfaceData(
15311abe55efSEd Tanous             ifaceId,
1532*0627a2c7SEd Tanous             [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId](
15334a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15344a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15351abe55efSEd Tanous                 if (!success)
15361abe55efSEd Tanous                 {
15371abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
15381abe55efSEd Tanous                     // object, and other errors
1539f12894f8SJason M. Bills                     messages::resourceNotFound(
1540f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1541927a505aSKowalski, Kamil 
1542927a505aSKowalski, Kamil                     return;
1543927a505aSKowalski, Kamil                 }
1544927a505aSKowalski, Kamil 
15450f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
15460f74e643SEd Tanous                                    ifaceId, ethData, ipv4Data);
1547927a505aSKowalski, Kamil 
1548*0627a2c7SEd Tanous                 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable,
1549*0627a2c7SEd Tanous                                                    ethData, asyncResp);
1550927a505aSKowalski, Kamil             });
1551e439f0f8SKowalski, Kamil     }
1552e439f0f8SKowalski, Kamil 
155355c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
15541abe55efSEd Tanous                   const std::vector<std::string> &params) override
15551abe55efSEd Tanous     {
15564a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15571abe55efSEd Tanous         if (params.size() != 2)
15581abe55efSEd Tanous         {
1559f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1560e439f0f8SKowalski, Kamil             return;
1561e439f0f8SKowalski, Kamil         }
1562e439f0f8SKowalski, Kamil 
1563d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
156455c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1565927a505aSKowalski, Kamil 
15664a0cb85cSEd Tanous         if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
15671abe55efSEd Tanous         {
1568927a505aSKowalski, Kamil             return;
1569927a505aSKowalski, Kamil         }
1570927a505aSKowalski, Kamil 
1571927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1572927a505aSKowalski, Kamil         // preparation
1573f12894f8SJason M. Bills         getEthernetIfaceData(
1574f12894f8SJason M. Bills             ifaceId,
1575f12894f8SJason M. Bills             [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
15764a0cb85cSEd Tanous              ifaceId{std::string(ifaceId)}](
1577f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
1578f12894f8SJason M. Bills                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15794a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
15801abe55efSEd Tanous                 {
15810f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
15820f74e643SEd Tanous                                        ifaceId, ethData, ipv4Data);
1583927a505aSKowalski, Kamil 
1584f12894f8SJason M. Bills                     auto callback =
1585f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
15861abe55efSEd Tanous                             if (ec)
15871abe55efSEd Tanous                             {
1588f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1589927a505aSKowalski, Kamil                             }
15904a0cb85cSEd Tanous                         };
15914a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
15924a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
15934a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
15944a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
15951abe55efSEd Tanous                 }
15961abe55efSEd Tanous                 else
15971abe55efSEd Tanous                 {
1598927a505aSKowalski, Kamil                     // ... otherwise return error
1599f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
1600f12894f8SJason M. Bills                     // object, and other errors
1601f12894f8SJason M. Bills                     messages::resourceNotFound(
1602f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1603927a505aSKowalski, Kamil                 }
1604927a505aSKowalski, Kamil             });
1605e439f0f8SKowalski, Kamil     }
1606e439f0f8SKowalski, Kamil };
1607e439f0f8SKowalski, Kamil 
1608e439f0f8SKowalski, Kamil /**
1609e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1610e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1611e439f0f8SKowalski, Kamil  */
16121abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
16131abe55efSEd Tanous {
1614e439f0f8SKowalski, Kamil   public:
1615e439f0f8SKowalski, Kamil     template <typename CrowApp>
16161abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
16174a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
16184a0cb85cSEd Tanous              std::string())
16191abe55efSEd Tanous     {
1620e439f0f8SKowalski, Kamil         entityPrivileges = {
1621e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1622e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1623e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1624e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1625e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1626e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1627e439f0f8SKowalski, Kamil     }
1628e439f0f8SKowalski, Kamil 
1629e439f0f8SKowalski, Kamil   private:
1630e439f0f8SKowalski, Kamil     /**
1631e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1632e439f0f8SKowalski, Kamil      */
163355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16341abe55efSEd Tanous                const std::vector<std::string> &params) override
16351abe55efSEd Tanous     {
16364a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16371abe55efSEd Tanous         if (params.size() != 1)
16381abe55efSEd Tanous         {
1639e439f0f8SKowalski, Kamil             // This means there is a problem with the router
1640f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1641e439f0f8SKowalski, Kamil             return;
1642e439f0f8SKowalski, Kamil         }
1643e439f0f8SKowalski, Kamil 
16444a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1645e439f0f8SKowalski, Kamil 
16464a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
16471abe55efSEd Tanous         // preparation
1648f12894f8SJason M. Bills         getEthernetIfaceList(
1649f12894f8SJason M. Bills             [this, asyncResp,
1650f12894f8SJason M. Bills              rootInterfaceName{std::string(rootInterfaceName)}](
16511abe55efSEd Tanous                 const bool &success,
16521abe55efSEd Tanous                 const std::vector<std::string> &iface_list) {
16534a0cb85cSEd Tanous                 if (!success)
16541abe55efSEd Tanous                 {
1655f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16564a0cb85cSEd Tanous                     return;
16571abe55efSEd Tanous                 }
16580f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
16590f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16600f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16610f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
16620f74e643SEd Tanous                     "/redfish/v1/$metadata"
16630f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16640f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16650f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
16660f74e643SEd Tanous                     "VLAN Network Interface Collection";
16674a0cb85cSEd Tanous 
16684a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
16694a0cb85cSEd Tanous 
16704a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
16711abe55efSEd Tanous                 {
16724a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
16734a0cb85cSEd Tanous                     {
16744a0cb85cSEd Tanous                         iface_array.push_back(
16754a0cb85cSEd Tanous                             {{"@odata.id",
16764a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
16774a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
1678e439f0f8SKowalski, Kamil                     }
1679e439f0f8SKowalski, Kamil                 }
1680e439f0f8SKowalski, Kamil 
16814a0cb85cSEd Tanous                 if (iface_array.empty())
16821abe55efSEd Tanous                 {
1683f12894f8SJason M. Bills                     messages::resourceNotFound(
1684f12894f8SJason M. Bills                         asyncResp->res, "EthernetInterface", rootInterfaceName);
16854a0cb85cSEd Tanous                     return;
1686e439f0f8SKowalski, Kamil                 }
16874a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
16884a0cb85cSEd Tanous                     iface_array.size();
16894a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
16904a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
16914a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
16924a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
1693e439f0f8SKowalski, Kamil             });
1694e439f0f8SKowalski, Kamil     }
1695e439f0f8SKowalski, Kamil 
169655c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
16971abe55efSEd Tanous                 const std::vector<std::string> &params) override
16981abe55efSEd Tanous     {
16994a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17001abe55efSEd Tanous         if (params.size() != 1)
17011abe55efSEd Tanous         {
1702f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1703e439f0f8SKowalski, Kamil             return;
1704e439f0f8SKowalski, Kamil         }
1705e439f0f8SKowalski, Kamil 
1706*0627a2c7SEd Tanous         uint32_t vlanId = 0;
1707*0627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANId", vlanId))
17081abe55efSEd Tanous         {
17094a0cb85cSEd Tanous             return;
1710e439f0f8SKowalski, Kamil         }
17114a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
17124a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
17131abe55efSEd Tanous             if (ec)
17141abe55efSEd Tanous             {
17154a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
17164a0cb85cSEd Tanous                 // phosphor-network responses
1717f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17184a0cb85cSEd Tanous                 return;
17191abe55efSEd Tanous             }
1720f12894f8SJason M. Bills             messages::created(asyncResp->res);
1721e439f0f8SKowalski, Kamil         };
17224a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
17234a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
17244a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
17254a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
1726*0627a2c7SEd Tanous             rootInterfaceName, vlanId);
17274a0cb85cSEd Tanous     }
17284a0cb85cSEd Tanous };
17299391bb9cSRapkiewicz, Pawel } // namespace redfish
1730