xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision bc0bd6e0039a839576a0a674239528335be1d3aa)
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 /**
584179db1d7SKowalski, Kamil  * @brief Deletes given IPv4
585179db1d7SKowalski, Kamil  *
586179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
5874a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
588179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
589179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
590179db1d7SKowalski, Kamil  *
591179db1d7SKowalski, Kamil  * @return None
592179db1d7SKowalski, Kamil  */
5934a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
594179db1d7SKowalski, Kamil                        unsigned int ipIdx,
5954a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
5961abe55efSEd Tanous {
59755c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
5984a0cb85cSEd Tanous         [ipIdx, asyncResp](const boost::system::error_code ec) {
5991abe55efSEd Tanous             if (ec)
6001abe55efSEd Tanous             {
601a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
6021abe55efSEd Tanous             }
6031abe55efSEd Tanous             else
6041abe55efSEd Tanous             {
60555c7b7a2SEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
606179db1d7SKowalski, Kamil             }
607179db1d7SKowalski, Kamil         },
608179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
609179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
610179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
611179db1d7SKowalski, Kamil }
612179db1d7SKowalski, Kamil 
613179db1d7SKowalski, Kamil /**
614179db1d7SKowalski, Kamil  * @brief Creates IPv4 with given data
615179db1d7SKowalski, Kamil  *
616179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6174a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
618179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
619179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
620179db1d7SKowalski, Kamil  *
621179db1d7SKowalski, Kamil  * @return None
622179db1d7SKowalski, Kamil  */
6234a0cb85cSEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
624179db1d7SKowalski, Kamil                        uint8_t subnetMask, const std::string &gateway,
625179db1d7SKowalski, Kamil                        const std::string &address,
6264a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
6271abe55efSEd Tanous {
6284a0cb85cSEd Tanous     auto createIpHandler = [ipIdx,
6294a0cb85cSEd Tanous                             asyncResp](const boost::system::error_code ec) {
6301abe55efSEd Tanous         if (ec)
6311abe55efSEd Tanous         {
632a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
633179db1d7SKowalski, Kamil         }
634179db1d7SKowalski, Kamil     };
635179db1d7SKowalski, Kamil 
63655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
637179db1d7SKowalski, Kamil         std::move(createIpHandler), "xyz.openbmc_project.Network",
638179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
639179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
640179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
641179db1d7SKowalski, Kamil         gateway);
642179db1d7SKowalski, Kamil }
643179db1d7SKowalski, Kamil 
644179db1d7SKowalski, Kamil /**
645179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
646179db1d7SKowalski, Kamil  * Object
647179db1d7SKowalski, Kamil  * from EntityManager Network Manager
6484a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
649179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
650179db1d7SKowalski, Kamil  * into JSON
651179db1d7SKowalski, Kamil  */
652179db1d7SKowalski, Kamil template <typename CallbackFunc>
6534a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
6541abe55efSEd Tanous                           CallbackFunc &&callback)
6551abe55efSEd Tanous {
65655c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6574a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
6581abe55efSEd Tanous             const boost::system::error_code error_code,
6594a0cb85cSEd Tanous             const GetManagedObjects &resp) {
66055c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
6614a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
662179db1d7SKowalski, Kamil 
6631abe55efSEd Tanous             if (error_code)
6641abe55efSEd Tanous             {
66555c7b7a2SEd Tanous                 callback(false, ethData, ipv4Data);
666179db1d7SKowalski, Kamil                 return;
667179db1d7SKowalski, Kamil             }
668179db1d7SKowalski, Kamil 
6694a0cb85cSEd Tanous             extractEthernetInterfaceData(ethiface_id, resp, ethData);
6704a0cb85cSEd Tanous             extractIPData(ethiface_id, resp, ipv4Data);
671179db1d7SKowalski, Kamil 
672179db1d7SKowalski, Kamil             // Fix global GW
6731abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
6741abe55efSEd Tanous             {
6754a0cb85cSEd Tanous                 if ((ipv4.linktype == LinkType::Global) &&
6764a0cb85cSEd Tanous                     (ipv4.gateway == "0.0.0.0"))
6771abe55efSEd Tanous                 {
6784a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
679179db1d7SKowalski, Kamil                 }
680179db1d7SKowalski, Kamil             }
681179db1d7SKowalski, Kamil 
6824a0cb85cSEd Tanous             // Finally make a callback with usefull data
68355c7b7a2SEd Tanous             callback(true, ethData, ipv4Data);
684179db1d7SKowalski, Kamil         },
685179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
686179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
687179db1d7SKowalski, Kamil };
688179db1d7SKowalski, Kamil 
689179db1d7SKowalski, Kamil /**
6909391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
6919391bb9cSRapkiewicz, Pawel  * Manager
6921abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
6931abe55efSEd Tanous  * into JSON.
6949391bb9cSRapkiewicz, Pawel  */
6959391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
6961abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
6971abe55efSEd Tanous {
69855c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6994a0cb85cSEd Tanous         [callback{std::move(callback)}](
7009391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
7014a0cb85cSEd Tanous             GetManagedObjects &resp) {
7021abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
7031abe55efSEd Tanous             // ethernet interfaces
7044a0cb85cSEd Tanous             std::vector<std::string> iface_list;
7054a0cb85cSEd Tanous             iface_list.reserve(resp.size());
7061abe55efSEd Tanous             if (error_code)
7071abe55efSEd Tanous             {
7084a0cb85cSEd Tanous                 callback(false, iface_list);
7099391bb9cSRapkiewicz, Pawel                 return;
7109391bb9cSRapkiewicz, Pawel             }
7119391bb9cSRapkiewicz, Pawel 
7129391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
7134a0cb85cSEd Tanous             for (const auto &objpath : resp)
7141abe55efSEd Tanous             {
7159391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
7164a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
7171abe55efSEd Tanous                 {
7181abe55efSEd Tanous                     // If interface is
7194a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
7204a0cb85cSEd Tanous                     // what we're looking for.
7219391bb9cSRapkiewicz, Pawel                     if (interface.first ==
7221abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
7231abe55efSEd Tanous                     {
7244a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
7254a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
7264a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
7274a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
7281abe55efSEd Tanous                         {
7299391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
7304a0cb85cSEd Tanous                             iface_list.emplace_back(
7314a0cb85cSEd Tanous                                 iface_id.substr(last_pos + 1));
7329391bb9cSRapkiewicz, Pawel                         }
7339391bb9cSRapkiewicz, Pawel                     }
7349391bb9cSRapkiewicz, Pawel                 }
7359391bb9cSRapkiewicz, Pawel             }
736a434f2bdSEd Tanous             // Finally make a callback with useful data
7374a0cb85cSEd Tanous             callback(true, iface_list);
7389391bb9cSRapkiewicz, Pawel         },
739aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
740aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
7419391bb9cSRapkiewicz, Pawel };
7429391bb9cSRapkiewicz, Pawel 
7439391bb9cSRapkiewicz, Pawel /**
7449391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
7459391bb9cSRapkiewicz, Pawel  */
7461abe55efSEd Tanous class EthernetCollection : public Node
7471abe55efSEd Tanous {
7489391bb9cSRapkiewicz, Pawel   public:
7494a0cb85cSEd Tanous     template <typename CrowApp>
7501abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
7514a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
7521abe55efSEd Tanous     {
753588c3f0dSKowalski, Kamil         entityPrivileges = {
754588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
755e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
756e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
757e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
758e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
759e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
7609391bb9cSRapkiewicz, Pawel     }
7619391bb9cSRapkiewicz, Pawel 
7629391bb9cSRapkiewicz, Pawel   private:
7639391bb9cSRapkiewicz, Pawel     /**
7649391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
7659391bb9cSRapkiewicz, Pawel      */
76655c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
7671abe55efSEd Tanous                const std::vector<std::string> &params) override
7681abe55efSEd Tanous     {
7690f74e643SEd Tanous         res.jsonValue["@odata.type"] =
7700f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
7710f74e643SEd Tanous         res.jsonValue["@odata.context"] =
7720f74e643SEd Tanous             "/redfish/v1/"
7730f74e643SEd Tanous             "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
7740f74e643SEd Tanous         res.jsonValue["@odata.id"] =
7750f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
7760f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
7770f74e643SEd Tanous         res.jsonValue["Description"] =
7780f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
7790f74e643SEd Tanous 
7804a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
7811abe55efSEd Tanous         // preparation
782f12894f8SJason M. Bills         getEthernetIfaceList(
783f12894f8SJason M. Bills             [&res](const bool &success,
7841abe55efSEd Tanous                    const std::vector<std::string> &iface_list) {
7854a0cb85cSEd Tanous                 if (!success)
7861abe55efSEd Tanous                 {
787f12894f8SJason M. Bills                     messages::internalError(res);
7884a0cb85cSEd Tanous                     res.end();
7894a0cb85cSEd Tanous                     return;
7904a0cb85cSEd Tanous                 }
7914a0cb85cSEd Tanous 
7924a0cb85cSEd Tanous                 nlohmann::json &iface_array = res.jsonValue["Members"];
7934a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
7944a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
7951abe55efSEd Tanous                 {
7964a0cb85cSEd Tanous                     iface_array.push_back(
7974a0cb85cSEd Tanous                         {{"@odata.id",
7984a0cb85cSEd Tanous                           "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
7994a0cb85cSEd Tanous                               iface_item}});
8009391bb9cSRapkiewicz, Pawel                 }
8014a0cb85cSEd Tanous 
8024a0cb85cSEd Tanous                 res.jsonValue["Members@odata.count"] = iface_array.size();
8034a0cb85cSEd Tanous                 res.jsonValue["@odata.id"] =
8044a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
8059391bb9cSRapkiewicz, Pawel                 res.end();
8069391bb9cSRapkiewicz, Pawel             });
8079391bb9cSRapkiewicz, Pawel     }
8089391bb9cSRapkiewicz, Pawel };
8099391bb9cSRapkiewicz, Pawel 
8109391bb9cSRapkiewicz, Pawel /**
8119391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
8129391bb9cSRapkiewicz, Pawel  */
8131abe55efSEd Tanous class EthernetInterface : public Node
8141abe55efSEd Tanous {
8159391bb9cSRapkiewicz, Pawel   public:
8169391bb9cSRapkiewicz, Pawel     /*
8179391bb9cSRapkiewicz, Pawel      * Default Constructor
8189391bb9cSRapkiewicz, Pawel      */
8194a0cb85cSEd Tanous     template <typename CrowApp>
8201abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
8214a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
8221abe55efSEd Tanous              std::string())
8231abe55efSEd Tanous     {
824588c3f0dSKowalski, Kamil         entityPrivileges = {
825588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
826e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
827e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
828e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
829e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
830e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8319391bb9cSRapkiewicz, Pawel     }
8329391bb9cSRapkiewicz, Pawel 
833e439f0f8SKowalski, Kamil     // TODO(kkowalsk) Find a suitable class/namespace for this
8340627a2c7SEd Tanous     static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable,
8350627a2c7SEd Tanous                                 uint64_t vlanId,
8364a0cb85cSEd Tanous                                 const EthernetInterfaceData &ethData,
8374a0cb85cSEd Tanous                                 const std::shared_ptr<AsyncResp> asyncResp)
8381abe55efSEd Tanous     {
8394a0cb85cSEd Tanous         if (!ethData.vlan_id)
8401abe55efSEd Tanous         {
841e439f0f8SKowalski, Kamil             // This interface is not a VLAN. Cannot do anything with it
842e439f0f8SKowalski, Kamil             // TODO(kkowalsk) Change this message
843a08b46ccSJason M. Bills             messages::propertyNotWritable(asyncResp->res, "VLANEnable");
844588c3f0dSKowalski, Kamil 
845588c3f0dSKowalski, Kamil             return;
846588c3f0dSKowalski, Kamil         }
847588c3f0dSKowalski, Kamil 
848588c3f0dSKowalski, Kamil         // VLAN is configured on the interface
8490627a2c7SEd Tanous         if (vlanEnable == true)
8501abe55efSEd Tanous         {
851588c3f0dSKowalski, Kamil             // Change VLAN Id
8520627a2c7SEd Tanous             asyncResp->res.jsonValue["VLANId"] = vlanId;
8534a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
8541abe55efSEd Tanous                 if (ec)
8551abe55efSEd Tanous                 {
856f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8571abe55efSEd Tanous                 }
8581abe55efSEd Tanous                 else
8591abe55efSEd Tanous                 {
8604a0cb85cSEd Tanous                     asyncResp->res.jsonValue["VLANEnable"] = true;
861e439f0f8SKowalski, Kamil                 }
8624a0cb85cSEd Tanous             };
8634a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
8644a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
8654a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
8664a0cb85cSEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
8674a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.VLAN", "Id",
8680627a2c7SEd Tanous                 sdbusplus::message::variant<uint32_t>(vlanId));
8691abe55efSEd Tanous         }
8704a0cb85cSEd Tanous         else
8711abe55efSEd Tanous         {
8724a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
8731abe55efSEd Tanous                 if (ec)
8741abe55efSEd Tanous                 {
875f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
8764a0cb85cSEd Tanous                     return;
8771abe55efSEd Tanous                 }
8784a0cb85cSEd Tanous                 asyncResp->res.jsonValue["VLANEnable"] = false;
8794a0cb85cSEd Tanous             };
8804a0cb85cSEd Tanous 
8814a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
8824a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
8834a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
8844a0cb85cSEd Tanous                 "xyz.openbmc_project.Object.Delete", "Delete");
885588c3f0dSKowalski, Kamil         }
886588c3f0dSKowalski, Kamil     }
887588c3f0dSKowalski, Kamil 
888e439f0f8SKowalski, Kamil   private:
889*bc0bd6e0SEd Tanous     void handleHostnamePatch(const std::string &hostname,
8904a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
8911abe55efSEd Tanous     {
892*bc0bd6e0SEd Tanous         asyncResp->res.jsonValue["HostName"] = hostname;
893*bc0bd6e0SEd Tanous         crow::connections::systemBus->async_method_call(
894*bc0bd6e0SEd Tanous             [asyncResp](const boost::system::error_code ec) {
8954a0cb85cSEd Tanous                 if (ec)
8964a0cb85cSEd Tanous                 {
897a08b46ccSJason M. Bills                     messages::internalError(asyncResp->res);
8981abe55efSEd Tanous                 }
899*bc0bd6e0SEd Tanous             },
900*bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network",
901*bc0bd6e0SEd Tanous             "/xyz/openbmc_project/network/config",
902*bc0bd6e0SEd Tanous             "org.freedesktop.DBus.Properties", "Set",
903*bc0bd6e0SEd Tanous             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
904*bc0bd6e0SEd Tanous             sdbusplus::message::variant<std::string>(hostname));
905588c3f0dSKowalski, Kamil     }
906588c3f0dSKowalski, Kamil 
9074a0cb85cSEd Tanous     void handleIPv4Patch(
9084a0cb85cSEd Tanous         const std::string &ifaceId, const nlohmann::json &input,
9094a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
9104a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
9111abe55efSEd Tanous     {
9121abe55efSEd Tanous         if (!input.is_array())
9131abe55efSEd Tanous         {
914f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
915a08b46ccSJason M. Bills                                              "IPv4Addresses");
916179db1d7SKowalski, Kamil             return;
917179db1d7SKowalski, Kamil         }
918179db1d7SKowalski, Kamil 
919179db1d7SKowalski, Kamil         // According to Redfish PATCH definition, size must be at least equal
9204a0cb85cSEd Tanous         if (input.size() < ipv4Data.size())
9211abe55efSEd Tanous         {
922a08b46ccSJason M. Bills             messages::propertyValueFormatError(asyncResp->res, input.dump(),
923a08b46ccSJason M. Bills                                                "IPv4Addresses");
924179db1d7SKowalski, Kamil             return;
925179db1d7SKowalski, Kamil         }
926179db1d7SKowalski, Kamil 
9274a0cb85cSEd Tanous         int entryIdx = 0;
9284a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
9294a0cb85cSEd Tanous             ipv4Data.begin();
9304a0cb85cSEd Tanous         for (const nlohmann::json &thisJson : input)
9311abe55efSEd Tanous         {
9324a0cb85cSEd Tanous             std::string pathString =
933a08b46ccSJason M. Bills                 "IPv4Addresses/" + std::to_string(entryIdx);
934179db1d7SKowalski, Kamil             // Check that entry is not of some unexpected type
9354a0cb85cSEd Tanous             if (!thisJson.is_object() && !thisJson.is_null())
9361abe55efSEd Tanous             {
937a08b46ccSJason M. Bills                 messages::propertyValueTypeError(asyncResp->res,
938a08b46ccSJason M. Bills                                                  thisJson.dump(),
939a08b46ccSJason M. Bills                                                  pathString + "/IPv4Address");
940179db1d7SKowalski, Kamil 
941179db1d7SKowalski, Kamil                 continue;
942179db1d7SKowalski, Kamil             }
943179db1d7SKowalski, Kamil 
9444a0cb85cSEd Tanous             nlohmann::json::const_iterator addressFieldIt =
9454a0cb85cSEd Tanous                 thisJson.find("Address");
9464a0cb85cSEd Tanous             const std::string *addressField = nullptr;
9474a0cb85cSEd Tanous             if (addressFieldIt != thisJson.end())
9481abe55efSEd Tanous             {
9494a0cb85cSEd Tanous                 addressField = addressFieldIt->get_ptr<const std::string *>();
9504a0cb85cSEd Tanous                 if (addressField == nullptr)
9511abe55efSEd Tanous                 {
952a08b46ccSJason M. Bills                     messages::propertyValueFormatError(asyncResp->res,
953a08b46ccSJason M. Bills                                                        addressFieldIt->dump(),
9544a0cb85cSEd Tanous                                                        pathString + "/Address");
955179db1d7SKowalski, Kamil                     continue;
956179db1d7SKowalski, Kamil                 }
9571abe55efSEd Tanous                 else
9581abe55efSEd Tanous                 {
9594a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*addressField))
9604a0cb85cSEd Tanous                     {
961f12894f8SJason M. Bills                         messages::propertyValueFormatError(
962a08b46ccSJason M. Bills                             asyncResp->res, *addressField,
9634a0cb85cSEd Tanous                             pathString + "/Address");
9644a0cb85cSEd Tanous                         continue;
9654a0cb85cSEd Tanous                     }
9664a0cb85cSEd Tanous                 }
9674a0cb85cSEd Tanous             }
9684a0cb85cSEd Tanous 
969a24526dcSEd Tanous             std::optional<uint8_t> prefixLength;
9704a0cb85cSEd Tanous             const std::string *subnetField = nullptr;
9714a0cb85cSEd Tanous             nlohmann::json::const_iterator subnetFieldIt =
9724a0cb85cSEd Tanous                 thisJson.find("SubnetMask");
9734a0cb85cSEd Tanous             if (subnetFieldIt != thisJson.end())
9744a0cb85cSEd Tanous             {
9754a0cb85cSEd Tanous                 subnetField = subnetFieldIt->get_ptr<const std::string *>();
9764a0cb85cSEd Tanous                 if (subnetField == nullptr)
9774a0cb85cSEd Tanous                 {
978f12894f8SJason M. Bills                     messages::propertyValueFormatError(
979a08b46ccSJason M. Bills                         asyncResp->res, *subnetField,
9804a0cb85cSEd Tanous                         pathString + "/SubnetMask");
9814a0cb85cSEd Tanous                     continue;
9824a0cb85cSEd Tanous                 }
9834a0cb85cSEd Tanous                 else
9844a0cb85cSEd Tanous                 {
9854a0cb85cSEd Tanous                     prefixLength = 0;
9864a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetField,
9874a0cb85cSEd Tanous                                                     &*prefixLength))
9884a0cb85cSEd Tanous                     {
989f12894f8SJason M. Bills                         messages::propertyValueFormatError(
990a08b46ccSJason M. Bills                             asyncResp->res, *subnetField,
9914a0cb85cSEd Tanous                             pathString + "/SubnetMask");
9924a0cb85cSEd Tanous                         continue;
9934a0cb85cSEd Tanous                     }
9944a0cb85cSEd Tanous                 }
9954a0cb85cSEd Tanous             }
9964a0cb85cSEd Tanous 
9974a0cb85cSEd Tanous             std::string addressOriginInDBusFormat;
9984a0cb85cSEd Tanous             const std::string *addressOriginField = nullptr;
9994a0cb85cSEd Tanous             nlohmann::json::const_iterator addressOriginFieldIt =
10004a0cb85cSEd Tanous                 thisJson.find("AddressOrigin");
10014a0cb85cSEd Tanous             if (addressOriginFieldIt != thisJson.end())
10024a0cb85cSEd Tanous             {
10034a0cb85cSEd Tanous                 const std::string *addressOriginField =
10044a0cb85cSEd Tanous                     addressOriginFieldIt->get_ptr<const std::string *>();
10054a0cb85cSEd Tanous                 if (addressOriginField == nullptr)
10064a0cb85cSEd Tanous                 {
1007f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1008a08b46ccSJason M. Bills                         asyncResp->res, *addressOriginField,
10094a0cb85cSEd Tanous                         pathString + "/AddressOrigin");
10104a0cb85cSEd Tanous                     continue;
10114a0cb85cSEd Tanous                 }
10124a0cb85cSEd Tanous                 else
10134a0cb85cSEd Tanous                 {
10144a0cb85cSEd Tanous                     // Get Address origin in proper format
10154a0cb85cSEd Tanous                     addressOriginInDBusFormat =
10164a0cb85cSEd Tanous                         translateAddressOriginRedfishToDbus(
10174a0cb85cSEd Tanous                             *addressOriginField);
10184a0cb85cSEd Tanous                     if (addressOriginInDBusFormat.empty())
10194a0cb85cSEd Tanous                     {
10204a0cb85cSEd Tanous                         messages::propertyValueNotInList(
1021f12894f8SJason M. Bills                             asyncResp->res, *addressOriginField,
1022a08b46ccSJason M. Bills                             pathString + "/AddressOrigin");
10234a0cb85cSEd Tanous                         continue;
10244a0cb85cSEd Tanous                     }
10254a0cb85cSEd Tanous                 }
10264a0cb85cSEd Tanous             }
10274a0cb85cSEd Tanous 
10284a0cb85cSEd Tanous             nlohmann::json::const_iterator gatewayFieldIt =
10294a0cb85cSEd Tanous                 thisJson.find("Gateway");
10304a0cb85cSEd Tanous             const std::string *gatewayField = nullptr;
10314a0cb85cSEd Tanous             if (gatewayFieldIt != thisJson.end())
10324a0cb85cSEd Tanous             {
10334a0cb85cSEd Tanous                 const std::string *gatewayField =
10344a0cb85cSEd Tanous                     gatewayFieldIt->get_ptr<const std::string *>();
10354a0cb85cSEd Tanous                 if (gatewayField == nullptr ||
10364a0cb85cSEd Tanous                     !ipv4VerifyIpAndGetBitcount(*gatewayField))
10374a0cb85cSEd Tanous                 {
1038a08b46ccSJason M. Bills                     messages::propertyValueFormatError(
1039a08b46ccSJason M. Bills                         asyncResp->res, *gatewayField, pathString + "/Gateway");
10404a0cb85cSEd Tanous                     continue;
10414a0cb85cSEd Tanous                 }
10424a0cb85cSEd Tanous             }
10434a0cb85cSEd Tanous 
10444a0cb85cSEd Tanous             // if a vlan already exists, modify the existing
10454a0cb85cSEd Tanous             if (thisData != ipv4Data.end())
10464a0cb85cSEd Tanous             {
10471abe55efSEd Tanous                 // Existing object that should be modified/deleted/remain
10481abe55efSEd Tanous                 // unchanged
10494a0cb85cSEd Tanous                 if (thisJson.is_null())
10501abe55efSEd Tanous                 {
10514a0cb85cSEd Tanous                     auto callback = [entryIdx{std::to_string(entryIdx)},
10524a0cb85cSEd Tanous                                      asyncResp](
10534a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
10544a0cb85cSEd Tanous                         if (ec)
10554a0cb85cSEd Tanous                         {
1056a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
10574a0cb85cSEd Tanous                             return;
10581abe55efSEd Tanous                         }
10594a0cb85cSEd Tanous                         asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
10604a0cb85cSEd Tanous                             nullptr;
10614a0cb85cSEd Tanous                     };
10624a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
10634a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
10644a0cb85cSEd Tanous                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
10654a0cb85cSEd Tanous                             thisData->id,
10664a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
1067179db1d7SKowalski, Kamil                 }
10684a0cb85cSEd Tanous                 else if (thisJson.is_object())
10694a0cb85cSEd Tanous                 {
1070179db1d7SKowalski, Kamil                     // Apply changes
10714a0cb85cSEd Tanous                     if (addressField != nullptr)
10721abe55efSEd Tanous                     {
10734a0cb85cSEd Tanous                         auto callback =
10744a0cb85cSEd Tanous                             [asyncResp, entryIdx,
10754a0cb85cSEd Tanous                              addressField{std::string(*addressField)}](
10764a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
10774a0cb85cSEd Tanous                                 if (ec)
10781abe55efSEd Tanous                                 {
1079a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
10804a0cb85cSEd Tanous                                     return;
10814a0cb85cSEd Tanous                                 }
10824a0cb85cSEd Tanous                                 asyncResp->res
10834a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
10844a0cb85cSEd Tanous                                         entryIdx)]["Address"] = addressField;
10854a0cb85cSEd Tanous                             };
10864a0cb85cSEd Tanous 
10874a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
10884a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
10894a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
10904a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
10914a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
10924a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Address",
10934a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
10944a0cb85cSEd Tanous                                 *addressField));
1095179db1d7SKowalski, Kamil                     }
1096179db1d7SKowalski, Kamil 
10974a0cb85cSEd Tanous                     if (prefixLength && subnetField != nullptr)
10981abe55efSEd Tanous                     {
10994a0cb85cSEd Tanous                         changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
11004a0cb85cSEd Tanous                                                      thisData->id, *subnetField,
11014a0cb85cSEd Tanous                                                      *prefixLength, asyncResp);
1102179db1d7SKowalski, Kamil                     }
1103179db1d7SKowalski, Kamil 
11044a0cb85cSEd Tanous                     if (!addressOriginInDBusFormat.empty() &&
11054a0cb85cSEd Tanous                         addressOriginField != nullptr)
11061abe55efSEd Tanous                     {
11074a0cb85cSEd Tanous                         changeIPv4Origin(ifaceId, entryIdx, thisData->id,
11084a0cb85cSEd Tanous                                          *addressOriginField,
11094a0cb85cSEd Tanous                                          addressOriginInDBusFormat, asyncResp);
1110179db1d7SKowalski, Kamil                     }
1111179db1d7SKowalski, Kamil 
11124a0cb85cSEd Tanous                     if (gatewayField != nullptr)
11131abe55efSEd Tanous                     {
11144a0cb85cSEd Tanous                         auto callback =
11154a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11164a0cb85cSEd Tanous                              gatewayField{std::string(*gatewayField)}](
11174a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11184a0cb85cSEd Tanous                                 if (ec)
11191abe55efSEd Tanous                                 {
1120a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11214a0cb85cSEd Tanous                                     return;
11224a0cb85cSEd Tanous                                 }
11234a0cb85cSEd Tanous                                 asyncResp->res
11244a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11254a0cb85cSEd Tanous                                         entryIdx)]["Gateway"] =
11264a0cb85cSEd Tanous                                     std::move(gatewayField);
11274a0cb85cSEd Tanous                             };
11284a0cb85cSEd Tanous 
11294a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11304a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11314a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11324a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11334a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11344a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Gateway",
11354a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11364a0cb85cSEd Tanous                                 *gatewayField));
11374a0cb85cSEd Tanous                     }
11384a0cb85cSEd Tanous                 }
11394a0cb85cSEd Tanous                 thisData++;
11401abe55efSEd Tanous             }
11411abe55efSEd Tanous             else
11421abe55efSEd Tanous             {
11434a0cb85cSEd Tanous                 // Create IPv4 with provided data
11444a0cb85cSEd Tanous                 if (gatewayField == nullptr)
11451abe55efSEd Tanous                 {
1146a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11474a0cb85cSEd Tanous                                               pathString + "/Gateway");
11484a0cb85cSEd Tanous                     continue;
11494a0cb85cSEd Tanous                 }
11504a0cb85cSEd Tanous 
11514a0cb85cSEd Tanous                 if (addressField == nullptr)
11521abe55efSEd Tanous                 {
1153a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11544a0cb85cSEd Tanous                                               pathString + "/Address");
11554a0cb85cSEd Tanous                     continue;
11564a0cb85cSEd Tanous                 }
11574a0cb85cSEd Tanous 
11584a0cb85cSEd Tanous                 if (!prefixLength)
11591abe55efSEd Tanous                 {
1160a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
11614a0cb85cSEd Tanous                                               pathString + "/SubnetMask");
11624a0cb85cSEd Tanous                     continue;
1163588c3f0dSKowalski, Kamil                 }
1164588c3f0dSKowalski, Kamil 
11654a0cb85cSEd Tanous                 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
11664a0cb85cSEd Tanous                            *addressField, asyncResp);
11674a0cb85cSEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
11684a0cb85cSEd Tanous             }
11694a0cb85cSEd Tanous             entryIdx++;
11704a0cb85cSEd Tanous         }
11714a0cb85cSEd Tanous     }
11724a0cb85cSEd Tanous 
11730f74e643SEd Tanous     void parseInterfaceData(
11740f74e643SEd Tanous         nlohmann::json &json_response, const std::string &iface_id,
11750f74e643SEd Tanous         const EthernetInterfaceData &ethData,
11764a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
11774a0cb85cSEd Tanous     {
11784a0cb85cSEd Tanous         json_response["Id"] = iface_id;
11794a0cb85cSEd Tanous         json_response["@odata.id"] =
11804a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
11814a0cb85cSEd Tanous 
11824a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
11834a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
11844a0cb85cSEd Tanous         if (!ethData.hostname.empty())
11854a0cb85cSEd Tanous         {
11864a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
11874a0cb85cSEd Tanous         }
11884a0cb85cSEd Tanous 
11894a0cb85cSEd Tanous         nlohmann::json &vlanObj = json_response["VLAN"];
11904a0cb85cSEd Tanous         if (ethData.vlan_id)
11914a0cb85cSEd Tanous         {
11924a0cb85cSEd Tanous             vlanObj["VLANEnable"] = true;
11934a0cb85cSEd Tanous             vlanObj["VLANId"] = *ethData.vlan_id;
11944a0cb85cSEd Tanous         }
11954a0cb85cSEd Tanous         else
11964a0cb85cSEd Tanous         {
11974a0cb85cSEd Tanous             vlanObj["VLANEnable"] = false;
11984a0cb85cSEd Tanous             vlanObj["VLANId"] = 0;
11994a0cb85cSEd Tanous         }
12004a0cb85cSEd Tanous 
12014a0cb85cSEd Tanous         if (ipv4Data.size() > 0)
12024a0cb85cSEd Tanous         {
12034a0cb85cSEd Tanous             nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
12044a0cb85cSEd Tanous             ipv4_array = nlohmann::json::array();
12054a0cb85cSEd Tanous             for (auto &ipv4_config : ipv4Data)
12064a0cb85cSEd Tanous             {
12074a0cb85cSEd Tanous                 if (!ipv4_config.address.empty())
12084a0cb85cSEd Tanous                 {
12094a0cb85cSEd Tanous                     ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
12104a0cb85cSEd Tanous                                           {"SubnetMask", ipv4_config.netmask},
12114a0cb85cSEd Tanous                                           {"Address", ipv4_config.address}});
12124a0cb85cSEd Tanous 
12134a0cb85cSEd Tanous                     if (!ipv4_config.gateway.empty())
12144a0cb85cSEd Tanous                     {
12154a0cb85cSEd Tanous                         ipv4_array.back()["Gateway"] = ipv4_config.gateway;
12164a0cb85cSEd Tanous                     }
12174a0cb85cSEd Tanous                 }
12184a0cb85cSEd Tanous             }
12194a0cb85cSEd Tanous         }
1220588c3f0dSKowalski, Kamil     }
1221588c3f0dSKowalski, Kamil 
12229391bb9cSRapkiewicz, Pawel     /**
12239391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
12249391bb9cSRapkiewicz, Pawel      */
122555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12261abe55efSEd Tanous                const std::vector<std::string> &params) override
12271abe55efSEd Tanous     {
12284a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12291abe55efSEd Tanous         if (params.size() != 1)
12301abe55efSEd Tanous         {
1231f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
12329391bb9cSRapkiewicz, Pawel             return;
12339391bb9cSRapkiewicz, Pawel         }
12349391bb9cSRapkiewicz, Pawel 
12354a0cb85cSEd Tanous         getEthernetIfaceData(
12364a0cb85cSEd Tanous             params[0],
12374a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
12384a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
12394a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
12404a0cb85cSEd Tanous                 if (!success)
12411abe55efSEd Tanous                 {
12421abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
12431abe55efSEd Tanous                     // object, and other errors
1244f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1245f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
12464a0cb85cSEd Tanous                     return;
12479391bb9cSRapkiewicz, Pawel                 }
12480f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
12490f74e643SEd Tanous                     "#EthernetInterface.v1_2_0.EthernetInterface";
12500f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
12510f74e643SEd Tanous                     "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
12520f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
12530f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
12540f74e643SEd Tanous                     "Management Network Interface";
12550f74e643SEd Tanous 
12560f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
12570f74e643SEd Tanous                                    ipv4Data);
12589391bb9cSRapkiewicz, Pawel             });
12599391bb9cSRapkiewicz, Pawel     }
12609391bb9cSRapkiewicz, Pawel 
126155c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
12621abe55efSEd Tanous                  const std::vector<std::string> &params) override
12631abe55efSEd Tanous     {
12644a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12651abe55efSEd Tanous         if (params.size() != 1)
12661abe55efSEd Tanous         {
1267f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1268588c3f0dSKowalski, Kamil             return;
1269588c3f0dSKowalski, Kamil         }
1270588c3f0dSKowalski, Kamil 
12714a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1272588c3f0dSKowalski, Kamil 
12730627a2c7SEd Tanous         std::optional<nlohmann::json> vlan;
1274*bc0bd6e0SEd Tanous         std::optional<std::string> hostname;
12750627a2c7SEd Tanous         std::optional<nlohmann::json> ipv4Addresses;
12760627a2c7SEd Tanous         std::optional<nlohmann::json> ipv6Addresses;
12770627a2c7SEd Tanous 
12780627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname,
12790627a2c7SEd Tanous                                  "IPv4Addresses", ipv4Addresses,
12800627a2c7SEd Tanous                                  "IPv6Addresses", ipv6Addresses))
12811abe55efSEd Tanous         {
1282588c3f0dSKowalski, Kamil             return;
1283588c3f0dSKowalski, Kamil         }
12840627a2c7SEd Tanous         std::optional<uint64_t> vlanId = 0;
12850627a2c7SEd Tanous         std::optional<bool> vlanEnable = false;
12860627a2c7SEd Tanous         if (vlan)
12870627a2c7SEd Tanous         {
12880627a2c7SEd Tanous             if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable,
12890627a2c7SEd Tanous                                      "VLANId", vlanId))
12900627a2c7SEd Tanous             {
12910627a2c7SEd Tanous                 return;
12920627a2c7SEd Tanous             }
12930627a2c7SEd Tanous             // Need both vlanId and vlanEnable to service this request
12940627a2c7SEd Tanous             if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
12950627a2c7SEd Tanous             {
12960627a2c7SEd Tanous                 if (vlanId)
12970627a2c7SEd Tanous                 {
12980627a2c7SEd Tanous                     messages::propertyMissing(asyncResp->res, "VLANEnable");
12990627a2c7SEd Tanous                 }
13000627a2c7SEd Tanous                 else
13010627a2c7SEd Tanous                 {
13020627a2c7SEd Tanous                     messages::propertyMissing(asyncResp->res, "VLANId");
13030627a2c7SEd Tanous                 }
13040627a2c7SEd Tanous 
13050627a2c7SEd Tanous                 return;
13060627a2c7SEd Tanous             }
13070627a2c7SEd Tanous         }
1308588c3f0dSKowalski, Kamil 
13094a0cb85cSEd Tanous         // Get single eth interface data, and call the below callback for JSON
1310588c3f0dSKowalski, Kamil         // preparation
13114a0cb85cSEd Tanous         getEthernetIfaceData(
13124a0cb85cSEd Tanous             iface_id,
13130627a2c7SEd Tanous             [this, asyncResp, iface_id, vlanId, vlanEnable,
13140627a2c7SEd Tanous              hostname = std::move(hostname),
13150627a2c7SEd Tanous              ipv4Addresses = std::move(ipv4Addresses),
13160627a2c7SEd Tanous              ipv6Addresses = std::move(ipv6Addresses)](
13174a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13184a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13191abe55efSEd Tanous                 if (!success)
13201abe55efSEd Tanous                 {
1321588c3f0dSKowalski, Kamil                     // ... otherwise return error
13221abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13231abe55efSEd Tanous                     // object, and other errors
1324f12894f8SJason M. Bills                     messages::resourceNotFound(
1325f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1326588c3f0dSKowalski, Kamil                     return;
1327588c3f0dSKowalski, Kamil                 }
1328588c3f0dSKowalski, Kamil 
13290f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
13300f74e643SEd Tanous                                    ipv4Data);
1331588c3f0dSKowalski, Kamil 
13320627a2c7SEd Tanous                 if (vlanId && vlanEnable)
13331abe55efSEd Tanous                 {
13340627a2c7SEd Tanous                     handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData,
13354a0cb85cSEd Tanous                                     asyncResp);
13361abe55efSEd Tanous                 }
13370627a2c7SEd Tanous 
13380627a2c7SEd Tanous                 if (hostname)
13391abe55efSEd Tanous                 {
13400627a2c7SEd Tanous                     handleHostnamePatch(*hostname, asyncResp);
13411abe55efSEd Tanous                 }
13420627a2c7SEd Tanous 
13430627a2c7SEd Tanous                 if (ipv4Addresses)
13441abe55efSEd Tanous                 {
13450627a2c7SEd Tanous                     handleIPv4Patch(iface_id, *ipv4Addresses, ipv4Data,
1346179db1d7SKowalski, Kamil                                     asyncResp);
13471abe55efSEd Tanous                 }
13480627a2c7SEd Tanous 
13490627a2c7SEd Tanous                 if (ipv6Addresses)
13501abe55efSEd Tanous                 {
1351179db1d7SKowalski, Kamil                     // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1352a08b46ccSJason M. Bills                     messages::propertyNotWritable(asyncResp->res,
13530627a2c7SEd Tanous                                                   "IPv6Addresses");
1354588c3f0dSKowalski, Kamil                 }
1355588c3f0dSKowalski, Kamil             });
1356588c3f0dSKowalski, Kamil     }
13579391bb9cSRapkiewicz, Pawel };
13589391bb9cSRapkiewicz, Pawel 
1359e439f0f8SKowalski, Kamil /**
13604a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
13614a0cb85cSEd Tanous  * Schema
1362e439f0f8SKowalski, Kamil  */
13631abe55efSEd Tanous class VlanNetworkInterface : public Node
13641abe55efSEd Tanous {
1365e439f0f8SKowalski, Kamil   public:
1366e439f0f8SKowalski, Kamil     /*
1367e439f0f8SKowalski, Kamil      * Default Constructor
1368e439f0f8SKowalski, Kamil      */
1369e439f0f8SKowalski, Kamil     template <typename CrowApp>
13701abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
13714a0cb85cSEd Tanous         Node(app,
13720f74e643SEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
13731abe55efSEd Tanous              std::string(), std::string())
13741abe55efSEd Tanous     {
1375e439f0f8SKowalski, Kamil         entityPrivileges = {
1376e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1377e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1378e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1379e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1380e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1381e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1382e439f0f8SKowalski, Kamil     }
1383e439f0f8SKowalski, Kamil 
1384e439f0f8SKowalski, Kamil   private:
13850f74e643SEd Tanous     void parseInterfaceData(
13860f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
13870f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
13884a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
13891abe55efSEd Tanous     {
1390e439f0f8SKowalski, Kamil         // Fill out obvious data...
13914a0cb85cSEd Tanous         json_response["Id"] = iface_id;
13924a0cb85cSEd Tanous         json_response["@odata.id"] =
13934a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
13944a0cb85cSEd Tanous             "/VLANs/" + iface_id;
1395e439f0f8SKowalski, Kamil 
13964a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
13974a0cb85cSEd Tanous         if (ethData.vlan_id)
13984a0cb85cSEd Tanous         {
13994a0cb85cSEd Tanous             json_response["VLANId"] = *ethData.vlan_id;
14004a0cb85cSEd Tanous         }
1401e439f0f8SKowalski, Kamil     }
1402e439f0f8SKowalski, Kamil 
140355c7b7a2SEd Tanous     bool verifyNames(crow::Response &res, const std::string &parent,
14041abe55efSEd Tanous                      const std::string &iface)
14051abe55efSEd Tanous     {
1406f12894f8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14071abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
14081abe55efSEd Tanous         {
1409f12894f8SJason M. Bills             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1410f12894f8SJason M. Bills                                        iface);
1411927a505aSKowalski, Kamil             return false;
14121abe55efSEd Tanous         }
14131abe55efSEd Tanous         else
14141abe55efSEd Tanous         {
1415927a505aSKowalski, Kamil             return true;
1416927a505aSKowalski, Kamil         }
1417927a505aSKowalski, Kamil     }
1418927a505aSKowalski, Kamil 
1419e439f0f8SKowalski, Kamil     /**
1420e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1421e439f0f8SKowalski, Kamil      */
142255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14231abe55efSEd Tanous                const std::vector<std::string> &params) override
14241abe55efSEd Tanous     {
14254a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14264a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
1427e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1428e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
1429e439f0f8SKowalski, Kamil         // impossible.
14301abe55efSEd Tanous         if (params.size() != 2)
14311abe55efSEd Tanous         {
1432f12894f8SJason M. Bills             messages::internalError(res);
1433e439f0f8SKowalski, Kamil             res.end();
1434e439f0f8SKowalski, Kamil             return;
1435e439f0f8SKowalski, Kamil         }
1436e439f0f8SKowalski, Kamil 
14374a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
14384a0cb85cSEd Tanous         const std::string &iface_id = params[1];
14390f74e643SEd Tanous         res.jsonValue["@odata.type"] =
14400f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
14410f74e643SEd Tanous         res.jsonValue["@odata.context"] =
14420f74e643SEd Tanous             "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
14430f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
1444e439f0f8SKowalski, Kamil 
14454a0cb85cSEd Tanous         if (!verifyNames(res, parent_iface_id, iface_id))
14461abe55efSEd Tanous         {
1447a434f2bdSEd Tanous             return;
1448a434f2bdSEd Tanous         }
1449a434f2bdSEd Tanous 
1450e439f0f8SKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1451e439f0f8SKowalski, Kamil         // preparation
14524a0cb85cSEd Tanous         getEthernetIfaceData(
14534a0cb85cSEd Tanous             iface_id,
14544a0cb85cSEd Tanous             [this, asyncResp, parent_iface_id, iface_id](
14554a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
14564a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
14574a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
14581abe55efSEd Tanous                 {
14590f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
14600f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
14610f74e643SEd Tanous                                        ipv4Data);
14621abe55efSEd Tanous                 }
14631abe55efSEd Tanous                 else
14641abe55efSEd Tanous                 {
1465e439f0f8SKowalski, Kamil                     // ... otherwise return error
14661abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
14671abe55efSEd Tanous                     // object, and other errors
1468f12894f8SJason M. Bills                     messages::resourceNotFound(
1469f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1470e439f0f8SKowalski, Kamil                 }
1471e439f0f8SKowalski, Kamil             });
1472e439f0f8SKowalski, Kamil     }
1473e439f0f8SKowalski, Kamil 
147455c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
14751abe55efSEd Tanous                  const std::vector<std::string> &params) override
14761abe55efSEd Tanous     {
14774a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14781abe55efSEd Tanous         if (params.size() != 2)
14791abe55efSEd Tanous         {
1480f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1481e439f0f8SKowalski, Kamil             return;
1482e439f0f8SKowalski, Kamil         }
1483e439f0f8SKowalski, Kamil 
1484d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
148555c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1486927a505aSKowalski, Kamil 
14871abe55efSEd Tanous         if (!verifyNames(res, parentIfaceId, ifaceId))
14881abe55efSEd Tanous         {
1489927a505aSKowalski, Kamil             return;
1490927a505aSKowalski, Kamil         }
1491927a505aSKowalski, Kamil 
14920627a2c7SEd Tanous         bool vlanEnable = false;
14930627a2c7SEd Tanous         uint64_t vlanId = 0;
14940627a2c7SEd Tanous 
14950627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
14960627a2c7SEd Tanous                                  vlanId))
14971abe55efSEd Tanous         {
1498927a505aSKowalski, Kamil             return;
1499927a505aSKowalski, Kamil         }
1500927a505aSKowalski, Kamil 
1501927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1502927a505aSKowalski, Kamil         // preparation
15034a0cb85cSEd Tanous         getEthernetIfaceData(
15041abe55efSEd Tanous             ifaceId,
15050627a2c7SEd Tanous             [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId](
15064a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15074a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15081abe55efSEd Tanous                 if (!success)
15091abe55efSEd Tanous                 {
15101abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
15111abe55efSEd Tanous                     // object, and other errors
1512f12894f8SJason M. Bills                     messages::resourceNotFound(
1513f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1514927a505aSKowalski, Kamil 
1515927a505aSKowalski, Kamil                     return;
1516927a505aSKowalski, Kamil                 }
1517927a505aSKowalski, Kamil 
15180f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
15190f74e643SEd Tanous                                    ifaceId, ethData, ipv4Data);
1520927a505aSKowalski, Kamil 
15210627a2c7SEd Tanous                 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable,
15220627a2c7SEd Tanous                                                    ethData, asyncResp);
1523927a505aSKowalski, Kamil             });
1524e439f0f8SKowalski, Kamil     }
1525e439f0f8SKowalski, Kamil 
152655c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
15271abe55efSEd Tanous                   const std::vector<std::string> &params) override
15281abe55efSEd Tanous     {
15294a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15301abe55efSEd Tanous         if (params.size() != 2)
15311abe55efSEd Tanous         {
1532f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1533e439f0f8SKowalski, Kamil             return;
1534e439f0f8SKowalski, Kamil         }
1535e439f0f8SKowalski, Kamil 
1536d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
153755c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1538927a505aSKowalski, Kamil 
15394a0cb85cSEd Tanous         if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
15401abe55efSEd Tanous         {
1541927a505aSKowalski, Kamil             return;
1542927a505aSKowalski, Kamil         }
1543927a505aSKowalski, Kamil 
1544927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1545927a505aSKowalski, Kamil         // preparation
1546f12894f8SJason M. Bills         getEthernetIfaceData(
1547f12894f8SJason M. Bills             ifaceId,
1548f12894f8SJason M. Bills             [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
15494a0cb85cSEd Tanous              ifaceId{std::string(ifaceId)}](
1550f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
1551f12894f8SJason M. Bills                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15524a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
15531abe55efSEd Tanous                 {
15540f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
15550f74e643SEd Tanous                                        ifaceId, ethData, ipv4Data);
1556927a505aSKowalski, Kamil 
1557f12894f8SJason M. Bills                     auto callback =
1558f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
15591abe55efSEd Tanous                             if (ec)
15601abe55efSEd Tanous                             {
1561f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1562927a505aSKowalski, Kamil                             }
15634a0cb85cSEd Tanous                         };
15644a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
15654a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
15664a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
15674a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
15681abe55efSEd Tanous                 }
15691abe55efSEd Tanous                 else
15701abe55efSEd Tanous                 {
1571927a505aSKowalski, Kamil                     // ... otherwise return error
1572f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
1573f12894f8SJason M. Bills                     // object, and other errors
1574f12894f8SJason M. Bills                     messages::resourceNotFound(
1575f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1576927a505aSKowalski, Kamil                 }
1577927a505aSKowalski, Kamil             });
1578e439f0f8SKowalski, Kamil     }
1579e439f0f8SKowalski, Kamil };
1580e439f0f8SKowalski, Kamil 
1581e439f0f8SKowalski, Kamil /**
1582e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1583e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1584e439f0f8SKowalski, Kamil  */
15851abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
15861abe55efSEd Tanous {
1587e439f0f8SKowalski, Kamil   public:
1588e439f0f8SKowalski, Kamil     template <typename CrowApp>
15891abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
15904a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
15914a0cb85cSEd Tanous              std::string())
15921abe55efSEd Tanous     {
1593e439f0f8SKowalski, Kamil         entityPrivileges = {
1594e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1595e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1596e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1597e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1598e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1599e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1600e439f0f8SKowalski, Kamil     }
1601e439f0f8SKowalski, Kamil 
1602e439f0f8SKowalski, Kamil   private:
1603e439f0f8SKowalski, Kamil     /**
1604e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1605e439f0f8SKowalski, Kamil      */
160655c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16071abe55efSEd Tanous                const std::vector<std::string> &params) override
16081abe55efSEd Tanous     {
16094a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16101abe55efSEd Tanous         if (params.size() != 1)
16111abe55efSEd Tanous         {
1612e439f0f8SKowalski, Kamil             // This means there is a problem with the router
1613f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1614e439f0f8SKowalski, Kamil             return;
1615e439f0f8SKowalski, Kamil         }
1616e439f0f8SKowalski, Kamil 
16174a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1618e439f0f8SKowalski, Kamil 
16194a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
16201abe55efSEd Tanous         // preparation
1621f12894f8SJason M. Bills         getEthernetIfaceList(
1622f12894f8SJason M. Bills             [this, asyncResp,
1623f12894f8SJason M. Bills              rootInterfaceName{std::string(rootInterfaceName)}](
16241abe55efSEd Tanous                 const bool &success,
16251abe55efSEd Tanous                 const std::vector<std::string> &iface_list) {
16264a0cb85cSEd Tanous                 if (!success)
16271abe55efSEd Tanous                 {
1628f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16294a0cb85cSEd Tanous                     return;
16301abe55efSEd Tanous                 }
16310f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
16320f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16330f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16340f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
16350f74e643SEd Tanous                     "/redfish/v1/$metadata"
16360f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
16370f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
16380f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
16390f74e643SEd Tanous                     "VLAN Network Interface Collection";
16404a0cb85cSEd Tanous 
16414a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
16424a0cb85cSEd Tanous 
16434a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
16441abe55efSEd Tanous                 {
16454a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
16464a0cb85cSEd Tanous                     {
16474a0cb85cSEd Tanous                         iface_array.push_back(
16484a0cb85cSEd Tanous                             {{"@odata.id",
16494a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
16504a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
1651e439f0f8SKowalski, Kamil                     }
1652e439f0f8SKowalski, Kamil                 }
1653e439f0f8SKowalski, Kamil 
16544a0cb85cSEd Tanous                 if (iface_array.empty())
16551abe55efSEd Tanous                 {
1656f12894f8SJason M. Bills                     messages::resourceNotFound(
1657f12894f8SJason M. Bills                         asyncResp->res, "EthernetInterface", rootInterfaceName);
16584a0cb85cSEd Tanous                     return;
1659e439f0f8SKowalski, Kamil                 }
16604a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
16614a0cb85cSEd Tanous                     iface_array.size();
16624a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
16634a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
16644a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
16654a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
1666e439f0f8SKowalski, Kamil             });
1667e439f0f8SKowalski, Kamil     }
1668e439f0f8SKowalski, Kamil 
166955c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
16701abe55efSEd Tanous                 const std::vector<std::string> &params) override
16711abe55efSEd Tanous     {
16724a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16731abe55efSEd Tanous         if (params.size() != 1)
16741abe55efSEd Tanous         {
1675f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1676e439f0f8SKowalski, Kamil             return;
1677e439f0f8SKowalski, Kamil         }
1678e439f0f8SKowalski, Kamil 
16790627a2c7SEd Tanous         uint32_t vlanId = 0;
16800627a2c7SEd Tanous         if (!json_util::readJson(req, res, "VLANId", vlanId))
16811abe55efSEd Tanous         {
16824a0cb85cSEd Tanous             return;
1683e439f0f8SKowalski, Kamil         }
16844a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
16854a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
16861abe55efSEd Tanous             if (ec)
16871abe55efSEd Tanous             {
16884a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
16894a0cb85cSEd Tanous                 // phosphor-network responses
1690f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
16914a0cb85cSEd Tanous                 return;
16921abe55efSEd Tanous             }
1693f12894f8SJason M. Bills             messages::created(asyncResp->res);
1694e439f0f8SKowalski, Kamil         };
16954a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
16964a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
16974a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
16984a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
16990627a2c7SEd Tanous             rootInterfaceName, vlanId);
17004a0cb85cSEd Tanous     }
17014a0cb85cSEd Tanous };
17029391bb9cSRapkiewicz, Pawel } // namespace redfish
1703