xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 0f74e643ec246c333ef4724af1ecd5adeb1b6658)
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>
204a0cb85cSEd Tanous #include <boost/optional.hpp>
21179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
22588c3f0dSKowalski, Kamil #include <error_messages.hpp>
23179db1d7SKowalski, Kamil #include <node.hpp>
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;
834a0cb85cSEd Tanous     boost::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 =
1744a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
1754a0cb85cSEd Tanous                                     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                         {
1894a0cb85cSEd Tanous                             const uint32_t *id = mapbox::getPtr<const uint32_t>(
1904a0cb85cSEd Tanous                                 propertyPair.second);
1914a0cb85cSEd Tanous                             if (id != nullptr)
1924a0cb85cSEd Tanous                             {
1934a0cb85cSEd Tanous                                 ethData.vlan_id = *id;
1944a0cb85cSEd Tanous                             }
1954a0cb85cSEd Tanous                         }
1964a0cb85cSEd Tanous                     }
1974a0cb85cSEd Tanous                 }
1984a0cb85cSEd Tanous                 else if (ifacePair.first ==
1994a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.EthernetInterface")
2004a0cb85cSEd Tanous                 {
2014a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2024a0cb85cSEd Tanous                     {
2034a0cb85cSEd Tanous                         if (propertyPair.first == "AutoNeg")
2044a0cb85cSEd Tanous                         {
2054a0cb85cSEd Tanous                             const bool *auto_neg =
2064a0cb85cSEd Tanous                                 mapbox::getPtr<const bool>(propertyPair.second);
2074a0cb85cSEd Tanous                             if (auto_neg != nullptr)
2084a0cb85cSEd Tanous                             {
2094a0cb85cSEd Tanous                                 ethData.auto_neg = *auto_neg;
2104a0cb85cSEd Tanous                             }
2114a0cb85cSEd Tanous                         }
2124a0cb85cSEd Tanous                         else if (propertyPair.first == "Speed")
2134a0cb85cSEd Tanous                         {
2144a0cb85cSEd Tanous                             const uint32_t *speed =
2154a0cb85cSEd Tanous                                 mapbox::getPtr<const uint32_t>(
2164a0cb85cSEd Tanous                                     propertyPair.second);
2174a0cb85cSEd Tanous                             if (speed != nullptr)
2184a0cb85cSEd Tanous                             {
2194a0cb85cSEd Tanous                                 ethData.speed = *speed;
2204a0cb85cSEd Tanous                             }
2214a0cb85cSEd Tanous                         }
2224a0cb85cSEd Tanous                     }
2234a0cb85cSEd Tanous                 }
2244a0cb85cSEd Tanous                 else if (ifacePair.first ==
2254a0cb85cSEd Tanous                          "xyz.openbmc_project.Network.SystemConfiguration")
2264a0cb85cSEd Tanous                 {
2274a0cb85cSEd Tanous                     for (const auto &propertyPair : ifacePair.second)
2284a0cb85cSEd Tanous                     {
2294a0cb85cSEd Tanous                         if (propertyPair.first == "HostName")
2304a0cb85cSEd Tanous                         {
2314a0cb85cSEd Tanous                             const std::string *hostname =
2324a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
2334a0cb85cSEd Tanous                                     propertyPair.second);
2344a0cb85cSEd Tanous                             if (hostname != nullptr)
2354a0cb85cSEd Tanous                             {
2364a0cb85cSEd Tanous                                 ethData.hostname = *hostname;
2374a0cb85cSEd Tanous                             }
2384a0cb85cSEd Tanous                         }
2394a0cb85cSEd Tanous                         else if (propertyPair.first == "DefaultGateway")
2404a0cb85cSEd Tanous                         {
2414a0cb85cSEd Tanous                             const std::string *defaultGateway =
2424a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
2434a0cb85cSEd Tanous                                     propertyPair.second);
2444a0cb85cSEd Tanous                             if (defaultGateway != nullptr)
2454a0cb85cSEd Tanous                             {
2464a0cb85cSEd Tanous                                 ethData.default_gateway = *defaultGateway;
2474a0cb85cSEd Tanous                             }
2484a0cb85cSEd Tanous                         }
2494a0cb85cSEd Tanous                     }
2504a0cb85cSEd Tanous                 }
2514a0cb85cSEd Tanous             }
2524a0cb85cSEd Tanous         }
2534a0cb85cSEd Tanous     }
2544a0cb85cSEd Tanous }
2554a0cb85cSEd Tanous 
2564a0cb85cSEd Tanous // Helper function that extracts data for single ethernet ipv4 address
2574a0cb85cSEd Tanous inline void
2584a0cb85cSEd Tanous     extractIPData(const std::string &ethiface_id,
2594a0cb85cSEd Tanous                   const GetManagedObjects &dbus_data,
2604a0cb85cSEd Tanous                   boost::container::flat_set<IPv4AddressData> &ipv4_config)
2614a0cb85cSEd Tanous {
2624a0cb85cSEd Tanous     const std::string ipv4PathStart =
2634a0cb85cSEd Tanous         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
2644a0cb85cSEd Tanous 
2654a0cb85cSEd Tanous     // Since there might be several IPv4 configurations aligned with
2664a0cb85cSEd Tanous     // single ethernet interface, loop over all of them
2674a0cb85cSEd Tanous     for (const auto &objpath : dbus_data)
2684a0cb85cSEd Tanous     {
2694a0cb85cSEd Tanous         // Check if proper pattern for object path appears
2704a0cb85cSEd Tanous         if (boost::starts_with(objpath.first.str, ipv4PathStart))
2714a0cb85cSEd Tanous         {
2724a0cb85cSEd Tanous             for (auto &interface : objpath.second)
2734a0cb85cSEd Tanous             {
2744a0cb85cSEd Tanous                 if (interface.first == "xyz.openbmc_project.Network.IP")
2754a0cb85cSEd Tanous                 {
2764a0cb85cSEd Tanous                     // Instance IPv4AddressData structure, and set as
2774a0cb85cSEd Tanous                     // appropriate
2784a0cb85cSEd Tanous                     std::pair<
2794a0cb85cSEd Tanous                         boost::container::flat_set<IPv4AddressData>::iterator,
2804a0cb85cSEd Tanous                         bool>
2814a0cb85cSEd Tanous                         it = ipv4_config.insert(
2824a0cb85cSEd Tanous                             {objpath.first.str.substr(ipv4PathStart.size())});
2834a0cb85cSEd Tanous                     IPv4AddressData &ipv4_address = *it.first;
2844a0cb85cSEd Tanous                     for (auto &property : interface.second)
2854a0cb85cSEd Tanous                     {
2864a0cb85cSEd Tanous                         if (property.first == "Address")
2874a0cb85cSEd Tanous                         {
2884a0cb85cSEd Tanous                             const std::string *address =
2894a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
2904a0cb85cSEd Tanous                                     property.second);
2914a0cb85cSEd Tanous                             if (address != nullptr)
2924a0cb85cSEd Tanous                             {
2934a0cb85cSEd Tanous                                 ipv4_address.address = *address;
2944a0cb85cSEd Tanous                             }
2954a0cb85cSEd Tanous                         }
2964a0cb85cSEd Tanous                         else if (property.first == "Gateway")
2974a0cb85cSEd Tanous                         {
2984a0cb85cSEd Tanous                             const std::string *gateway =
2994a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
3004a0cb85cSEd Tanous                                     property.second);
3014a0cb85cSEd Tanous                             if (gateway != nullptr)
3024a0cb85cSEd Tanous                             {
3034a0cb85cSEd Tanous                                 ipv4_address.gateway = *gateway;
3044a0cb85cSEd Tanous                             }
3054a0cb85cSEd Tanous                         }
3064a0cb85cSEd Tanous                         else if (property.first == "Origin")
3074a0cb85cSEd Tanous                         {
3084a0cb85cSEd Tanous                             const std::string *origin =
3094a0cb85cSEd Tanous                                 mapbox::getPtr<const std::string>(
3104a0cb85cSEd Tanous                                     property.second);
3114a0cb85cSEd Tanous                             if (origin != nullptr)
3124a0cb85cSEd Tanous                             {
3134a0cb85cSEd Tanous                                 ipv4_address.origin =
3144a0cb85cSEd Tanous                                     translateAddressOriginDbusToRedfish(*origin,
3154a0cb85cSEd Tanous                                                                         true);
3164a0cb85cSEd Tanous                             }
3174a0cb85cSEd Tanous                         }
3184a0cb85cSEd Tanous                         else if (property.first == "PrefixLength")
3194a0cb85cSEd Tanous                         {
3204a0cb85cSEd Tanous                             const uint8_t *mask =
3214a0cb85cSEd Tanous                                 mapbox::getPtr<uint8_t>(property.second);
3224a0cb85cSEd Tanous                             if (mask != nullptr)
3234a0cb85cSEd Tanous                             {
3244a0cb85cSEd Tanous                                 // convert it to the string
3254a0cb85cSEd Tanous                                 ipv4_address.netmask = getNetmask(*mask);
3264a0cb85cSEd Tanous                             }
3274a0cb85cSEd Tanous                         }
3284a0cb85cSEd Tanous                         else
3294a0cb85cSEd Tanous                         {
3304a0cb85cSEd Tanous                             BMCWEB_LOG_ERROR
3314a0cb85cSEd Tanous                                 << "Got extra property: " << property.first
3324a0cb85cSEd Tanous                                 << " on the " << objpath.first.str << " object";
3334a0cb85cSEd Tanous                         }
3344a0cb85cSEd Tanous                     }
3354a0cb85cSEd Tanous                     // Check if given address is local, or global
3364a0cb85cSEd Tanous                     ipv4_address.linktype =
3374a0cb85cSEd Tanous                         boost::starts_with(ipv4_address.address, "169.254.")
3384a0cb85cSEd Tanous                             ? LinkType::Global
3394a0cb85cSEd Tanous                             : LinkType::Local;
3404a0cb85cSEd Tanous                 }
3414a0cb85cSEd Tanous             }
3424a0cb85cSEd Tanous         }
3434a0cb85cSEd Tanous     }
3444a0cb85cSEd Tanous }
345588c3f0dSKowalski, Kamil 
346588c3f0dSKowalski, Kamil /**
347588c3f0dSKowalski, Kamil  * @brief Sets given Id on the given VLAN interface through D-Bus
348588c3f0dSKowalski, Kamil  *
349588c3f0dSKowalski, Kamil  * @param[in] ifaceId       Id of VLAN interface that should be modified
350588c3f0dSKowalski, Kamil  * @param[in] inputVlanId   New ID of the VLAN
351588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
352588c3f0dSKowalski, Kamil  *
353588c3f0dSKowalski, Kamil  * @return None.
354588c3f0dSKowalski, Kamil  */
355588c3f0dSKowalski, Kamil template <typename CallbackFunc>
3564a0cb85cSEd Tanous void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
3571abe55efSEd Tanous                   CallbackFunc &&callback)
3581abe55efSEd Tanous {
35955c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
360588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
361588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
362588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
363588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
364588c3f0dSKowalski, Kamil         sdbusplus::message::variant<uint32_t>(inputVlanId));
3654a0cb85cSEd Tanous }
366588c3f0dSKowalski, Kamil 
367588c3f0dSKowalski, Kamil /**
368179db1d7SKowalski, Kamil  * @brief Helper function that verifies IP address to check if it is in
369179db1d7SKowalski, Kamil  *        proper format. If bits pointer is provided, also calculates active
370179db1d7SKowalski, Kamil  *        bit count for Subnet Mask.
371179db1d7SKowalski, Kamil  *
372179db1d7SKowalski, Kamil  * @param[in]  ip     IP that will be verified
373179db1d7SKowalski, Kamil  * @param[out] bits   Calculated mask in bits notation
374179db1d7SKowalski, Kamil  *
375179db1d7SKowalski, Kamil  * @return true in case of success, false otherwise
376179db1d7SKowalski, Kamil  */
3774a0cb85cSEd Tanous inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
3781abe55efSEd Tanous                                        uint8_t *bits = nullptr)
3791abe55efSEd Tanous {
380179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
381179db1d7SKowalski, Kamil 
382179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
383179db1d7SKowalski, Kamil 
3844a0cb85cSEd Tanous     static const constexpr int ipV4AddressSectionsCount = 4;
3851abe55efSEd Tanous     if (bytesInMask.size() != ipV4AddressSectionsCount)
3861abe55efSEd Tanous     {
387179db1d7SKowalski, Kamil         return false;
388179db1d7SKowalski, Kamil     }
389179db1d7SKowalski, Kamil 
3901abe55efSEd Tanous     if (bits != nullptr)
3911abe55efSEd Tanous     {
392179db1d7SKowalski, Kamil         *bits = 0;
393179db1d7SKowalski, Kamil     }
394179db1d7SKowalski, Kamil 
395179db1d7SKowalski, Kamil     char *endPtr;
396179db1d7SKowalski, Kamil     long previousValue = 255;
397179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
3981abe55efSEd Tanous     for (const std::string &byte : bytesInMask)
3991abe55efSEd Tanous     {
4001abe55efSEd Tanous         if (byte.empty())
4011abe55efSEd Tanous         {
4021db9ca37SKowalski, Kamil             return false;
4031db9ca37SKowalski, Kamil         }
4041db9ca37SKowalski, Kamil 
405179db1d7SKowalski, Kamil         // Use strtol instead of stroi to avoid exceptions
4061db9ca37SKowalski, Kamil         long value = std::strtol(byte.c_str(), &endPtr, 10);
407179db1d7SKowalski, Kamil 
4084a0cb85cSEd Tanous         // endPtr should point to the end of the string, otherwise given string
4094a0cb85cSEd Tanous         // is not 100% number
4101abe55efSEd Tanous         if (*endPtr != '\0')
4111abe55efSEd Tanous         {
412179db1d7SKowalski, Kamil             return false;
413179db1d7SKowalski, Kamil         }
414179db1d7SKowalski, Kamil 
415179db1d7SKowalski, Kamil         // Value should be contained in byte
4161abe55efSEd Tanous         if (value < 0 || value > 255)
4171abe55efSEd Tanous         {
418179db1d7SKowalski, Kamil             return false;
419179db1d7SKowalski, Kamil         }
420179db1d7SKowalski, Kamil 
4211abe55efSEd Tanous         if (bits != nullptr)
4221abe55efSEd Tanous         {
423179db1d7SKowalski, Kamil             // Mask has to be continuous between bytes
4241abe55efSEd Tanous             if (previousValue != 255 && value != 0)
4251abe55efSEd Tanous             {
426179db1d7SKowalski, Kamil                 return false;
427179db1d7SKowalski, Kamil             }
428179db1d7SKowalski, Kamil 
429179db1d7SKowalski, Kamil             // Mask has to be continuous inside bytes
430179db1d7SKowalski, Kamil             firstZeroInByteHit = false;
431179db1d7SKowalski, Kamil 
432179db1d7SKowalski, Kamil             // Count bits
4331abe55efSEd Tanous             for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
4341abe55efSEd Tanous             {
4351abe55efSEd Tanous                 if (value & (1 << bitIdx))
4361abe55efSEd Tanous                 {
4371abe55efSEd Tanous                     if (firstZeroInByteHit)
4381abe55efSEd Tanous                     {
439179db1d7SKowalski, Kamil                         // Continuity not preserved
440179db1d7SKowalski, Kamil                         return false;
4411abe55efSEd Tanous                     }
4421abe55efSEd Tanous                     else
4431abe55efSEd Tanous                     {
444179db1d7SKowalski, Kamil                         (*bits)++;
445179db1d7SKowalski, Kamil                     }
4461abe55efSEd Tanous                 }
4471abe55efSEd Tanous                 else
4481abe55efSEd Tanous                 {
449179db1d7SKowalski, Kamil                     firstZeroInByteHit = true;
450179db1d7SKowalski, Kamil                 }
451179db1d7SKowalski, Kamil             }
452179db1d7SKowalski, Kamil         }
453179db1d7SKowalski, Kamil 
454179db1d7SKowalski, Kamil         previousValue = value;
455179db1d7SKowalski, Kamil     }
456179db1d7SKowalski, Kamil 
457179db1d7SKowalski, Kamil     return true;
458179db1d7SKowalski, Kamil }
459179db1d7SKowalski, Kamil 
460179db1d7SKowalski, Kamil /**
461179db1d7SKowalski, Kamil  * @brief Changes IPv4 address type property (Address, Gateway)
462179db1d7SKowalski, Kamil  *
463179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be modified
4644a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be modified
465179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of modified IP
466179db1d7SKowalski, Kamil  * @param[in] name        Name of field in JSON representation
467179db1d7SKowalski, Kamil  * @param[in] newValue    New value that should be written
468179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
469179db1d7SKowalski, Kamil  *
470179db1d7SKowalski, Kamil  * @return true if give IP is valid and has been sent do D-Bus, false
471179db1d7SKowalski, Kamil  * otherwise
472179db1d7SKowalski, Kamil  */
4734a0cb85cSEd Tanous inline void changeIPv4AddressProperty(
4744a0cb85cSEd Tanous     const std::string &ifaceId, int ipIdx, const std::string &ipHash,
4754a0cb85cSEd Tanous     const std::string &name, const std::string &newValue,
4764a0cb85cSEd Tanous     const std::shared_ptr<AsyncResp> asyncResp)
4771abe55efSEd Tanous {
4784a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, name{std::string(name)},
4794a0cb85cSEd Tanous                      newValue{std::move(newValue)}](
4801abe55efSEd Tanous                         const boost::system::error_code ec) {
4811abe55efSEd Tanous         if (ec)
4821abe55efSEd Tanous         {
483a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
4841abe55efSEd Tanous         }
4851abe55efSEd Tanous         else
4861abe55efSEd Tanous         {
4874a0cb85cSEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
488179db1d7SKowalski, Kamil         }
489179db1d7SKowalski, Kamil     };
490179db1d7SKowalski, Kamil 
49155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
492179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
493179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
494179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
495179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", name,
496179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValue));
4974a0cb85cSEd Tanous }
498179db1d7SKowalski, Kamil 
499179db1d7SKowalski, Kamil /**
500179db1d7SKowalski, Kamil  * @brief Changes IPv4 address origin property
501179db1d7SKowalski, Kamil  *
502179db1d7SKowalski, Kamil  * @param[in] ifaceId       Id of interface whose IP should be modified
5034a0cb85cSEd Tanous  * @param[in] ipIdx         Index of IP in input array that should be
5041abe55efSEd Tanous  * modified
505179db1d7SKowalski, Kamil  * @param[in] ipHash        DBus Hash id of modified IP
506179db1d7SKowalski, Kamil  * @param[in] newValue      New value in Redfish format
507179db1d7SKowalski, Kamil  * @param[in] newValueDbus  New value in D-Bus format
508179db1d7SKowalski, Kamil  * @param[io] asyncResp     Response object that will be returned to client
509179db1d7SKowalski, Kamil  *
510179db1d7SKowalski, Kamil  * @return true if give IP is valid and has been sent do D-Bus, false
511179db1d7SKowalski, Kamil  * otherwise
512179db1d7SKowalski, Kamil  */
5134a0cb85cSEd Tanous inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
5141abe55efSEd Tanous                              const std::string &ipHash,
5151abe55efSEd Tanous                              const std::string &newValue,
516179db1d7SKowalski, Kamil                              const std::string &newValueDbus,
5174a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
5181abe55efSEd Tanous {
5194a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
5201abe55efSEd Tanous                         const boost::system::error_code ec) {
5211abe55efSEd Tanous         if (ec)
5221abe55efSEd Tanous         {
523a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5241abe55efSEd Tanous         }
5251abe55efSEd Tanous         else
5261abe55efSEd Tanous         {
5274a0cb85cSEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
528179db1d7SKowalski, Kamil                 newValue;
529179db1d7SKowalski, Kamil         }
530179db1d7SKowalski, Kamil     };
531179db1d7SKowalski, Kamil 
53255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
533179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
534179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
535179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
536179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "Origin",
537179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValueDbus));
5384a0cb85cSEd Tanous }
539179db1d7SKowalski, Kamil 
540179db1d7SKowalski, Kamil /**
541179db1d7SKowalski, Kamil  * @brief Modifies SubnetMask for given IP
542179db1d7SKowalski, Kamil  *
543179db1d7SKowalski, Kamil  * @param[in] ifaceId      Id of interface whose IP should be modified
5444a0cb85cSEd Tanous  * @param[in] ipIdx        Index of IP in input array that should be
5451abe55efSEd Tanous  * modified
546179db1d7SKowalski, Kamil  * @param[in] ipHash       DBus Hash id of modified IP
547179db1d7SKowalski, Kamil  * @param[in] newValueStr  Mask in dot notation as string
548179db1d7SKowalski, Kamil  * @param[in] newValue     Mask as PrefixLength in bitcount
549179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
550179db1d7SKowalski, Kamil  *
551179db1d7SKowalski, Kamil  * @return None
552179db1d7SKowalski, Kamil  */
5534a0cb85cSEd Tanous inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
5544a0cb85cSEd Tanous                                          const std::string &ipHash,
5554a0cb85cSEd Tanous                                          const std::string &newValueStr,
5564a0cb85cSEd Tanous                                          uint8_t &newValue,
5574a0cb85cSEd Tanous                                          std::shared_ptr<AsyncResp> asyncResp)
5581abe55efSEd Tanous {
5594a0cb85cSEd Tanous     auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
5601abe55efSEd Tanous                         const boost::system::error_code ec) {
5611abe55efSEd Tanous         if (ec)
5621abe55efSEd Tanous         {
563a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
5641abe55efSEd Tanous         }
5651abe55efSEd Tanous         else
5661abe55efSEd Tanous         {
56755c7b7a2SEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
568179db1d7SKowalski, Kamil                 newValueStr;
569179db1d7SKowalski, Kamil         }
570179db1d7SKowalski, Kamil     };
571179db1d7SKowalski, Kamil 
57255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
573179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
574179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
575179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
576179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "PrefixLength",
577179db1d7SKowalski, Kamil         sdbusplus::message::variant<uint8_t>(newValue));
5784a0cb85cSEd Tanous }
579588c3f0dSKowalski, Kamil 
580588c3f0dSKowalski, Kamil /**
581588c3f0dSKowalski, Kamil  * @brief Sets given HostName of the machine through D-Bus
582588c3f0dSKowalski, Kamil  *
583588c3f0dSKowalski, Kamil  * @param[in] newHostname   New name that HostName will be changed to
584588c3f0dSKowalski, Kamil  * @param[in] callback      Function that will be called after the operation
585588c3f0dSKowalski, Kamil  *
586588c3f0dSKowalski, Kamil  * @return None.
587588c3f0dSKowalski, Kamil  */
588588c3f0dSKowalski, Kamil template <typename CallbackFunc>
5891abe55efSEd Tanous void setHostName(const std::string &newHostname, CallbackFunc &&callback)
5901abe55efSEd Tanous {
59155c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
592588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
593588c3f0dSKowalski, Kamil         "/xyz/openbmc_project/network/config",
594588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
595588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
596588c3f0dSKowalski, Kamil         sdbusplus::message::variant<std::string>(newHostname));
5974a0cb85cSEd Tanous }
598588c3f0dSKowalski, Kamil 
599588c3f0dSKowalski, Kamil /**
600179db1d7SKowalski, Kamil  * @brief Deletes given IPv4
601179db1d7SKowalski, Kamil  *
602179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6034a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
604179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
605179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
606179db1d7SKowalski, Kamil  *
607179db1d7SKowalski, Kamil  * @return None
608179db1d7SKowalski, Kamil  */
6094a0cb85cSEd Tanous inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
610179db1d7SKowalski, Kamil                        unsigned int ipIdx,
6114a0cb85cSEd Tanous                        const std::shared_ptr<AsyncResp> asyncResp)
6121abe55efSEd Tanous {
61355c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6144a0cb85cSEd Tanous         [ipIdx, asyncResp](const boost::system::error_code ec) {
6151abe55efSEd Tanous             if (ec)
6161abe55efSEd Tanous             {
617a08b46ccSJason M. Bills                 messages::internalError(asyncResp->res);
6181abe55efSEd Tanous             }
6191abe55efSEd Tanous             else
6201abe55efSEd Tanous             {
62155c7b7a2SEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
622179db1d7SKowalski, Kamil             }
623179db1d7SKowalski, Kamil         },
624179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
625179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
626179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
627179db1d7SKowalski, Kamil }
628179db1d7SKowalski, Kamil 
629179db1d7SKowalski, Kamil /**
630179db1d7SKowalski, Kamil  * @brief Creates IPv4 with given data
631179db1d7SKowalski, Kamil  *
632179db1d7SKowalski, Kamil  * @param[in] ifaceId     Id of interface whose IP should be deleted
6334a0cb85cSEd Tanous  * @param[in] ipIdx       Index of IP in input array that should be deleted
634179db1d7SKowalski, Kamil  * @param[in] ipHash      DBus Hash id of IP that should be deleted
635179db1d7SKowalski, Kamil  * @param[io] asyncResp   Response object that will be returned to client
636179db1d7SKowalski, Kamil  *
637179db1d7SKowalski, Kamil  * @return None
638179db1d7SKowalski, Kamil  */
6394a0cb85cSEd Tanous inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
640179db1d7SKowalski, Kamil                        uint8_t subnetMask, const std::string &gateway,
641179db1d7SKowalski, Kamil                        const std::string &address,
6424a0cb85cSEd Tanous                        std::shared_ptr<AsyncResp> asyncResp)
6431abe55efSEd Tanous {
6444a0cb85cSEd Tanous     auto createIpHandler = [ipIdx,
6454a0cb85cSEd Tanous                             asyncResp](const boost::system::error_code ec) {
6461abe55efSEd Tanous         if (ec)
6471abe55efSEd Tanous         {
648a08b46ccSJason M. Bills             messages::internalError(asyncResp->res);
649179db1d7SKowalski, Kamil         }
650179db1d7SKowalski, Kamil     };
651179db1d7SKowalski, Kamil 
65255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
653179db1d7SKowalski, Kamil         std::move(createIpHandler), "xyz.openbmc_project.Network",
654179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
655179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
656179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
657179db1d7SKowalski, Kamil         gateway);
658179db1d7SKowalski, Kamil }
659179db1d7SKowalski, Kamil 
660179db1d7SKowalski, Kamil /**
661179db1d7SKowalski, Kamil  * Function that retrieves all properties for given Ethernet Interface
662179db1d7SKowalski, Kamil  * Object
663179db1d7SKowalski, Kamil  * from EntityManager Network Manager
6644a0cb85cSEd Tanous  * @param ethiface_id a eth interface id to query on DBus
665179db1d7SKowalski, Kamil  * @param callback a function that shall be called to convert Dbus output
666179db1d7SKowalski, Kamil  * into JSON
667179db1d7SKowalski, Kamil  */
668179db1d7SKowalski, Kamil template <typename CallbackFunc>
6694a0cb85cSEd Tanous void getEthernetIfaceData(const std::string &ethiface_id,
6701abe55efSEd Tanous                           CallbackFunc &&callback)
6711abe55efSEd Tanous {
67255c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
6734a0cb85cSEd Tanous         [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
6741abe55efSEd Tanous             const boost::system::error_code error_code,
6754a0cb85cSEd Tanous             const GetManagedObjects &resp) {
67655c7b7a2SEd Tanous             EthernetInterfaceData ethData{};
6774a0cb85cSEd Tanous             boost::container::flat_set<IPv4AddressData> ipv4Data;
678179db1d7SKowalski, Kamil 
6791abe55efSEd Tanous             if (error_code)
6801abe55efSEd Tanous             {
68155c7b7a2SEd Tanous                 callback(false, ethData, ipv4Data);
682179db1d7SKowalski, Kamil                 return;
683179db1d7SKowalski, Kamil             }
684179db1d7SKowalski, Kamil 
6854a0cb85cSEd Tanous             extractEthernetInterfaceData(ethiface_id, resp, ethData);
6864a0cb85cSEd Tanous             extractIPData(ethiface_id, resp, ipv4Data);
687179db1d7SKowalski, Kamil 
688179db1d7SKowalski, Kamil             // Fix global GW
6891abe55efSEd Tanous             for (IPv4AddressData &ipv4 : ipv4Data)
6901abe55efSEd Tanous             {
6914a0cb85cSEd Tanous                 if ((ipv4.linktype == LinkType::Global) &&
6924a0cb85cSEd Tanous                     (ipv4.gateway == "0.0.0.0"))
6931abe55efSEd Tanous                 {
6944a0cb85cSEd Tanous                     ipv4.gateway = ethData.default_gateway;
695179db1d7SKowalski, Kamil                 }
696179db1d7SKowalski, Kamil             }
697179db1d7SKowalski, Kamil 
6984a0cb85cSEd Tanous             // Finally make a callback with usefull data
69955c7b7a2SEd Tanous             callback(true, ethData, ipv4Data);
700179db1d7SKowalski, Kamil         },
701179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
702179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
703179db1d7SKowalski, Kamil };
704179db1d7SKowalski, Kamil 
705179db1d7SKowalski, Kamil /**
7069391bb9cSRapkiewicz, Pawel  * Function that retrieves all Ethernet Interfaces available through Network
7079391bb9cSRapkiewicz, Pawel  * Manager
7081abe55efSEd Tanous  * @param callback a function that shall be called to convert Dbus output
7091abe55efSEd Tanous  * into JSON.
7109391bb9cSRapkiewicz, Pawel  */
7119391bb9cSRapkiewicz, Pawel template <typename CallbackFunc>
7121abe55efSEd Tanous void getEthernetIfaceList(CallbackFunc &&callback)
7131abe55efSEd Tanous {
71455c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
7154a0cb85cSEd Tanous         [callback{std::move(callback)}](
7169391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
7174a0cb85cSEd Tanous             GetManagedObjects &resp) {
7181abe55efSEd Tanous             // Callback requires vector<string> to retrieve all available
7191abe55efSEd Tanous             // ethernet interfaces
7204a0cb85cSEd Tanous             std::vector<std::string> iface_list;
7214a0cb85cSEd Tanous             iface_list.reserve(resp.size());
7221abe55efSEd Tanous             if (error_code)
7231abe55efSEd Tanous             {
7244a0cb85cSEd Tanous                 callback(false, iface_list);
7259391bb9cSRapkiewicz, Pawel                 return;
7269391bb9cSRapkiewicz, Pawel             }
7279391bb9cSRapkiewicz, Pawel 
7289391bb9cSRapkiewicz, Pawel             // Iterate over all retrieved ObjectPaths.
7294a0cb85cSEd Tanous             for (const auto &objpath : resp)
7301abe55efSEd Tanous             {
7319391bb9cSRapkiewicz, Pawel                 // And all interfaces available for certain ObjectPath.
7324a0cb85cSEd Tanous                 for (const auto &interface : objpath.second)
7331abe55efSEd Tanous                 {
7341abe55efSEd Tanous                     // If interface is
7354a0cb85cSEd Tanous                     // xyz.openbmc_project.Network.EthernetInterface, this is
7364a0cb85cSEd Tanous                     // what we're looking for.
7379391bb9cSRapkiewicz, Pawel                     if (interface.first ==
7381abe55efSEd Tanous                         "xyz.openbmc_project.Network.EthernetInterface")
7391abe55efSEd Tanous                     {
7404a0cb85cSEd Tanous                         // Cut out everyting until last "/", ...
7414a0cb85cSEd Tanous                         const std::string &iface_id = objpath.first.str;
7424a0cb85cSEd Tanous                         std::size_t last_pos = iface_id.rfind("/");
7434a0cb85cSEd Tanous                         if (last_pos != std::string::npos)
7441abe55efSEd Tanous                         {
7459391bb9cSRapkiewicz, Pawel                             // and put it into output vector.
7464a0cb85cSEd Tanous                             iface_list.emplace_back(
7474a0cb85cSEd Tanous                                 iface_id.substr(last_pos + 1));
7489391bb9cSRapkiewicz, Pawel                         }
7499391bb9cSRapkiewicz, Pawel                     }
7509391bb9cSRapkiewicz, Pawel                 }
7519391bb9cSRapkiewicz, Pawel             }
752a434f2bdSEd Tanous             // Finally make a callback with useful data
7534a0cb85cSEd Tanous             callback(true, iface_list);
7549391bb9cSRapkiewicz, Pawel         },
755aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
756aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
7579391bb9cSRapkiewicz, Pawel };
7589391bb9cSRapkiewicz, Pawel 
7599391bb9cSRapkiewicz, Pawel /**
7609391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
7619391bb9cSRapkiewicz, Pawel  */
7621abe55efSEd Tanous class EthernetCollection : public Node
7631abe55efSEd Tanous {
7649391bb9cSRapkiewicz, Pawel   public:
7654a0cb85cSEd Tanous     template <typename CrowApp>
7661abe55efSEd Tanous     EthernetCollection(CrowApp &app) :
7674a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
7681abe55efSEd Tanous     {
769588c3f0dSKowalski, Kamil         entityPrivileges = {
770588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
771e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
772e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
773e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
774e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
775e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
7769391bb9cSRapkiewicz, Pawel     }
7779391bb9cSRapkiewicz, Pawel 
7789391bb9cSRapkiewicz, Pawel   private:
7799391bb9cSRapkiewicz, Pawel     /**
7809391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
7819391bb9cSRapkiewicz, Pawel      */
78255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
7831abe55efSEd Tanous                const std::vector<std::string> &params) override
7841abe55efSEd Tanous     {
785*0f74e643SEd Tanous         res.jsonValue["@odata.type"] =
786*0f74e643SEd Tanous             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
787*0f74e643SEd Tanous         res.jsonValue["@odata.context"] =
788*0f74e643SEd Tanous             "/redfish/v1/"
789*0f74e643SEd Tanous             "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
790*0f74e643SEd Tanous         res.jsonValue["@odata.id"] =
791*0f74e643SEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces";
792*0f74e643SEd Tanous         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
793*0f74e643SEd Tanous         res.jsonValue["Description"] =
794*0f74e643SEd Tanous             "Collection of EthernetInterfaces for this Manager";
795*0f74e643SEd Tanous 
7964a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
7971abe55efSEd Tanous         // preparation
798f12894f8SJason M. Bills         getEthernetIfaceList(
799f12894f8SJason M. Bills             [&res](const bool &success,
8001abe55efSEd Tanous                    const std::vector<std::string> &iface_list) {
8014a0cb85cSEd Tanous                 if (!success)
8021abe55efSEd Tanous                 {
803f12894f8SJason M. Bills                     messages::internalError(res);
8044a0cb85cSEd Tanous                     res.end();
8054a0cb85cSEd Tanous                     return;
8064a0cb85cSEd Tanous                 }
8074a0cb85cSEd Tanous 
8084a0cb85cSEd Tanous                 nlohmann::json &iface_array = res.jsonValue["Members"];
8094a0cb85cSEd Tanous                 iface_array = nlohmann::json::array();
8104a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
8111abe55efSEd Tanous                 {
8124a0cb85cSEd Tanous                     iface_array.push_back(
8134a0cb85cSEd Tanous                         {{"@odata.id",
8144a0cb85cSEd Tanous                           "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
8154a0cb85cSEd Tanous                               iface_item}});
8169391bb9cSRapkiewicz, Pawel                 }
8174a0cb85cSEd Tanous 
8184a0cb85cSEd Tanous                 res.jsonValue["Members@odata.count"] = iface_array.size();
8194a0cb85cSEd Tanous                 res.jsonValue["@odata.id"] =
8204a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
8219391bb9cSRapkiewicz, Pawel                 res.end();
8229391bb9cSRapkiewicz, Pawel             });
8239391bb9cSRapkiewicz, Pawel     }
8249391bb9cSRapkiewicz, Pawel };
8259391bb9cSRapkiewicz, Pawel 
8269391bb9cSRapkiewicz, Pawel /**
8279391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
8289391bb9cSRapkiewicz, Pawel  */
8291abe55efSEd Tanous class EthernetInterface : public Node
8301abe55efSEd Tanous {
8319391bb9cSRapkiewicz, Pawel   public:
8329391bb9cSRapkiewicz, Pawel     /*
8339391bb9cSRapkiewicz, Pawel      * Default Constructor
8349391bb9cSRapkiewicz, Pawel      */
8354a0cb85cSEd Tanous     template <typename CrowApp>
8361abe55efSEd Tanous     EthernetInterface(CrowApp &app) :
8374a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
8381abe55efSEd Tanous              std::string())
8391abe55efSEd Tanous     {
840588c3f0dSKowalski, Kamil         entityPrivileges = {
841588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
842e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
843e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
844e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
845e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
846e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8479391bb9cSRapkiewicz, Pawel     }
8489391bb9cSRapkiewicz, Pawel 
849e439f0f8SKowalski, Kamil     // TODO(kkowalsk) Find a suitable class/namespace for this
850e439f0f8SKowalski, Kamil     static void handleVlanPatch(const std::string &ifaceId,
851e439f0f8SKowalski, Kamil                                 const nlohmann::json &input,
8524a0cb85cSEd Tanous                                 const EthernetInterfaceData &ethData,
8534a0cb85cSEd Tanous                                 const std::shared_ptr<AsyncResp> asyncResp)
8541abe55efSEd Tanous     {
8551abe55efSEd Tanous         if (!input.is_object())
8561abe55efSEd Tanous         {
857f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
858a08b46ccSJason M. Bills                                              "VLAN");
859588c3f0dSKowalski, Kamil             return;
860588c3f0dSKowalski, Kamil         }
861588c3f0dSKowalski, Kamil 
8624a0cb85cSEd Tanous         nlohmann::json::const_iterator vlanEnable = input.find("VLANEnable");
8634a0cb85cSEd Tanous         if (vlanEnable == input.end())
8641abe55efSEd Tanous         {
865a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANEnable");
8664a0cb85cSEd Tanous             return;
8674a0cb85cSEd Tanous         }
8684a0cb85cSEd Tanous         const bool *vlanEnableBool = vlanEnable->get_ptr<const bool *>();
8694a0cb85cSEd Tanous         if (vlanEnableBool == nullptr)
8704a0cb85cSEd Tanous         {
871f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanEnable->dump(),
872a08b46ccSJason M. Bills                                              "VLANEnable");
873588c3f0dSKowalski, Kamil             return;
874588c3f0dSKowalski, Kamil         }
875588c3f0dSKowalski, Kamil 
8764a0cb85cSEd Tanous         nlohmann::json::const_iterator vlanId = input.find("VLANId");
8774a0cb85cSEd Tanous         if (vlanId == input.end())
8784a0cb85cSEd Tanous         {
879a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANId");
8804a0cb85cSEd Tanous             return;
8814a0cb85cSEd Tanous         }
8824a0cb85cSEd Tanous         const uint64_t *vlanIdUint = vlanId->get_ptr<const uint64_t *>();
8834a0cb85cSEd Tanous         if (vlanIdUint == nullptr)
8844a0cb85cSEd Tanous         {
885f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanId->dump(),
886a08b46ccSJason M. Bills                                              "VLANId");
8874a0cb85cSEd Tanous             return;
8884a0cb85cSEd Tanous         }
8894a0cb85cSEd Tanous 
8904a0cb85cSEd Tanous         if (!ethData.vlan_id)
8911abe55efSEd Tanous         {
892e439f0f8SKowalski, Kamil             // This interface is not a VLAN. Cannot do anything with it
893e439f0f8SKowalski, Kamil             // TODO(kkowalsk) Change this message
894a08b46ccSJason M. Bills             messages::propertyNotWritable(asyncResp->res, "VLANEnable");
895588c3f0dSKowalski, Kamil 
896588c3f0dSKowalski, Kamil             return;
897588c3f0dSKowalski, Kamil         }
898588c3f0dSKowalski, Kamil 
899588c3f0dSKowalski, Kamil         // VLAN is configured on the interface
9004a0cb85cSEd Tanous         if (*vlanEnableBool == true)
9011abe55efSEd Tanous         {
902588c3f0dSKowalski, Kamil             // Change VLAN Id
9034a0cb85cSEd Tanous             asyncResp->res.jsonValue["VLANId"] = *vlanIdUint;
9044a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
9051abe55efSEd Tanous                 if (ec)
9061abe55efSEd Tanous                 {
907f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
9081abe55efSEd Tanous                 }
9091abe55efSEd Tanous                 else
9101abe55efSEd Tanous                 {
9114a0cb85cSEd Tanous                     asyncResp->res.jsonValue["VLANEnable"] = true;
912e439f0f8SKowalski, Kamil                 }
9134a0cb85cSEd Tanous             };
9144a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
9154a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
9164a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
9174a0cb85cSEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
9184a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.VLAN", "Id",
9194a0cb85cSEd Tanous                 sdbusplus::message::variant<uint32_t>(*vlanIdUint));
9201abe55efSEd Tanous         }
9214a0cb85cSEd Tanous         else
9221abe55efSEd Tanous         {
9234a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
9241abe55efSEd Tanous                 if (ec)
9251abe55efSEd Tanous                 {
926f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
9274a0cb85cSEd Tanous                     return;
9281abe55efSEd Tanous                 }
9294a0cb85cSEd Tanous                 asyncResp->res.jsonValue["VLANEnable"] = false;
9304a0cb85cSEd Tanous             };
9314a0cb85cSEd Tanous 
9324a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
9334a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
9344a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
9354a0cb85cSEd Tanous                 "xyz.openbmc_project.Object.Delete", "Delete");
936588c3f0dSKowalski, Kamil         }
937588c3f0dSKowalski, Kamil     }
938588c3f0dSKowalski, Kamil 
939e439f0f8SKowalski, Kamil   private:
940588c3f0dSKowalski, Kamil     void handleHostnamePatch(const nlohmann::json &input,
9414a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
9421abe55efSEd Tanous     {
9434a0cb85cSEd Tanous         const std::string *newHostname = input.get_ptr<const std::string *>();
9444a0cb85cSEd Tanous         if (newHostname == nullptr)
9451abe55efSEd Tanous         {
946f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
947a08b46ccSJason M. Bills                                              "HostName");
9484a0cb85cSEd Tanous             return;
9494a0cb85cSEd Tanous         }
9504a0cb85cSEd Tanous 
9514a0cb85cSEd Tanous         // Change hostname
952a08b46ccSJason M. Bills         setHostName(*newHostname,
953a08b46ccSJason M. Bills                     [asyncResp, newHostname{std::string(*newHostname)}](
9544a0cb85cSEd Tanous                         const boost::system::error_code ec) {
9554a0cb85cSEd Tanous                         if (ec)
9564a0cb85cSEd Tanous                         {
957a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
9581abe55efSEd Tanous                         }
9591abe55efSEd Tanous                         else
9601abe55efSEd Tanous                         {
96155c7b7a2SEd Tanous                             asyncResp->res.jsonValue["HostName"] = newHostname;
962588c3f0dSKowalski, Kamil                         }
963588c3f0dSKowalski, Kamil                     });
964588c3f0dSKowalski, Kamil     }
965588c3f0dSKowalski, Kamil 
9664a0cb85cSEd Tanous     void handleIPv4Patch(
9674a0cb85cSEd Tanous         const std::string &ifaceId, const nlohmann::json &input,
9684a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
9694a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
9701abe55efSEd Tanous     {
9711abe55efSEd Tanous         if (!input.is_array())
9721abe55efSEd Tanous         {
973f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
974a08b46ccSJason M. Bills                                              "IPv4Addresses");
975179db1d7SKowalski, Kamil             return;
976179db1d7SKowalski, Kamil         }
977179db1d7SKowalski, Kamil 
978179db1d7SKowalski, Kamil         // According to Redfish PATCH definition, size must be at least equal
9794a0cb85cSEd Tanous         if (input.size() < ipv4Data.size())
9801abe55efSEd Tanous         {
981a08b46ccSJason M. Bills             messages::propertyValueFormatError(asyncResp->res, input.dump(),
982a08b46ccSJason M. Bills                                                "IPv4Addresses");
983179db1d7SKowalski, Kamil             return;
984179db1d7SKowalski, Kamil         }
985179db1d7SKowalski, Kamil 
9864a0cb85cSEd Tanous         int entryIdx = 0;
9874a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
9884a0cb85cSEd Tanous             ipv4Data.begin();
9894a0cb85cSEd Tanous         for (const nlohmann::json &thisJson : input)
9901abe55efSEd Tanous         {
9914a0cb85cSEd Tanous             std::string pathString =
992a08b46ccSJason M. Bills                 "IPv4Addresses/" + std::to_string(entryIdx);
993179db1d7SKowalski, Kamil             // Check that entry is not of some unexpected type
9944a0cb85cSEd Tanous             if (!thisJson.is_object() && !thisJson.is_null())
9951abe55efSEd Tanous             {
996a08b46ccSJason M. Bills                 messages::propertyValueTypeError(asyncResp->res,
997a08b46ccSJason M. Bills                                                  thisJson.dump(),
998a08b46ccSJason M. Bills                                                  pathString + "/IPv4Address");
999179db1d7SKowalski, Kamil 
1000179db1d7SKowalski, Kamil                 continue;
1001179db1d7SKowalski, Kamil             }
1002179db1d7SKowalski, Kamil 
10034a0cb85cSEd Tanous             nlohmann::json::const_iterator addressFieldIt =
10044a0cb85cSEd Tanous                 thisJson.find("Address");
10054a0cb85cSEd Tanous             const std::string *addressField = nullptr;
10064a0cb85cSEd Tanous             if (addressFieldIt != thisJson.end())
10071abe55efSEd Tanous             {
10084a0cb85cSEd Tanous                 addressField = addressFieldIt->get_ptr<const std::string *>();
10094a0cb85cSEd Tanous                 if (addressField == nullptr)
10101abe55efSEd Tanous                 {
1011a08b46ccSJason M. Bills                     messages::propertyValueFormatError(asyncResp->res,
1012a08b46ccSJason M. Bills                                                        addressFieldIt->dump(),
10134a0cb85cSEd Tanous                                                        pathString + "/Address");
1014179db1d7SKowalski, Kamil                     continue;
1015179db1d7SKowalski, Kamil                 }
10161abe55efSEd Tanous                 else
10171abe55efSEd Tanous                 {
10184a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*addressField))
10194a0cb85cSEd Tanous                     {
1020f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1021a08b46ccSJason M. Bills                             asyncResp->res, *addressField,
10224a0cb85cSEd Tanous                             pathString + "/Address");
10234a0cb85cSEd Tanous                         continue;
10244a0cb85cSEd Tanous                     }
10254a0cb85cSEd Tanous                 }
10264a0cb85cSEd Tanous             }
10274a0cb85cSEd Tanous 
10284a0cb85cSEd Tanous             boost::optional<uint8_t> prefixLength;
10294a0cb85cSEd Tanous             const std::string *subnetField = nullptr;
10304a0cb85cSEd Tanous             nlohmann::json::const_iterator subnetFieldIt =
10314a0cb85cSEd Tanous                 thisJson.find("SubnetMask");
10324a0cb85cSEd Tanous             if (subnetFieldIt != thisJson.end())
10334a0cb85cSEd Tanous             {
10344a0cb85cSEd Tanous                 subnetField = subnetFieldIt->get_ptr<const std::string *>();
10354a0cb85cSEd Tanous                 if (subnetField == nullptr)
10364a0cb85cSEd Tanous                 {
1037f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1038a08b46ccSJason M. Bills                         asyncResp->res, *subnetField,
10394a0cb85cSEd Tanous                         pathString + "/SubnetMask");
10404a0cb85cSEd Tanous                     continue;
10414a0cb85cSEd Tanous                 }
10424a0cb85cSEd Tanous                 else
10434a0cb85cSEd Tanous                 {
10444a0cb85cSEd Tanous                     prefixLength = 0;
10454a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetField,
10464a0cb85cSEd Tanous                                                     &*prefixLength))
10474a0cb85cSEd Tanous                     {
1048f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1049a08b46ccSJason M. Bills                             asyncResp->res, *subnetField,
10504a0cb85cSEd Tanous                             pathString + "/SubnetMask");
10514a0cb85cSEd Tanous                         continue;
10524a0cb85cSEd Tanous                     }
10534a0cb85cSEd Tanous                 }
10544a0cb85cSEd Tanous             }
10554a0cb85cSEd Tanous 
10564a0cb85cSEd Tanous             std::string addressOriginInDBusFormat;
10574a0cb85cSEd Tanous             const std::string *addressOriginField = nullptr;
10584a0cb85cSEd Tanous             nlohmann::json::const_iterator addressOriginFieldIt =
10594a0cb85cSEd Tanous                 thisJson.find("AddressOrigin");
10604a0cb85cSEd Tanous             if (addressOriginFieldIt != thisJson.end())
10614a0cb85cSEd Tanous             {
10624a0cb85cSEd Tanous                 const std::string *addressOriginField =
10634a0cb85cSEd Tanous                     addressOriginFieldIt->get_ptr<const std::string *>();
10644a0cb85cSEd Tanous                 if (addressOriginField == nullptr)
10654a0cb85cSEd Tanous                 {
1066f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1067a08b46ccSJason M. Bills                         asyncResp->res, *addressOriginField,
10684a0cb85cSEd Tanous                         pathString + "/AddressOrigin");
10694a0cb85cSEd Tanous                     continue;
10704a0cb85cSEd Tanous                 }
10714a0cb85cSEd Tanous                 else
10724a0cb85cSEd Tanous                 {
10734a0cb85cSEd Tanous                     // Get Address origin in proper format
10744a0cb85cSEd Tanous                     addressOriginInDBusFormat =
10754a0cb85cSEd Tanous                         translateAddressOriginRedfishToDbus(
10764a0cb85cSEd Tanous                             *addressOriginField);
10774a0cb85cSEd Tanous                     if (addressOriginInDBusFormat.empty())
10784a0cb85cSEd Tanous                     {
10794a0cb85cSEd Tanous                         messages::propertyValueNotInList(
1080f12894f8SJason M. Bills                             asyncResp->res, *addressOriginField,
1081a08b46ccSJason M. Bills                             pathString + "/AddressOrigin");
10824a0cb85cSEd Tanous                         continue;
10834a0cb85cSEd Tanous                     }
10844a0cb85cSEd Tanous                 }
10854a0cb85cSEd Tanous             }
10864a0cb85cSEd Tanous 
10874a0cb85cSEd Tanous             nlohmann::json::const_iterator gatewayFieldIt =
10884a0cb85cSEd Tanous                 thisJson.find("Gateway");
10894a0cb85cSEd Tanous             const std::string *gatewayField = nullptr;
10904a0cb85cSEd Tanous             if (gatewayFieldIt != thisJson.end())
10914a0cb85cSEd Tanous             {
10924a0cb85cSEd Tanous                 const std::string *gatewayField =
10934a0cb85cSEd Tanous                     gatewayFieldIt->get_ptr<const std::string *>();
10944a0cb85cSEd Tanous                 if (gatewayField == nullptr ||
10954a0cb85cSEd Tanous                     !ipv4VerifyIpAndGetBitcount(*gatewayField))
10964a0cb85cSEd Tanous                 {
1097a08b46ccSJason M. Bills                     messages::propertyValueFormatError(
1098a08b46ccSJason M. Bills                         asyncResp->res, *gatewayField, pathString + "/Gateway");
10994a0cb85cSEd Tanous                     continue;
11004a0cb85cSEd Tanous                 }
11014a0cb85cSEd Tanous             }
11024a0cb85cSEd Tanous 
11034a0cb85cSEd Tanous             // if a vlan already exists, modify the existing
11044a0cb85cSEd Tanous             if (thisData != ipv4Data.end())
11054a0cb85cSEd Tanous             {
11061abe55efSEd Tanous                 // Existing object that should be modified/deleted/remain
11071abe55efSEd Tanous                 // unchanged
11084a0cb85cSEd Tanous                 if (thisJson.is_null())
11091abe55efSEd Tanous                 {
11104a0cb85cSEd Tanous                     auto callback = [entryIdx{std::to_string(entryIdx)},
11114a0cb85cSEd Tanous                                      asyncResp](
11124a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
11134a0cb85cSEd Tanous                         if (ec)
11144a0cb85cSEd Tanous                         {
1115a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
11164a0cb85cSEd Tanous                             return;
11171abe55efSEd Tanous                         }
11184a0cb85cSEd Tanous                         asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
11194a0cb85cSEd Tanous                             nullptr;
11204a0cb85cSEd Tanous                     };
11214a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
11224a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
11234a0cb85cSEd Tanous                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
11244a0cb85cSEd Tanous                             thisData->id,
11254a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
1126179db1d7SKowalski, Kamil                 }
11274a0cb85cSEd Tanous                 else if (thisJson.is_object())
11284a0cb85cSEd Tanous                 {
1129179db1d7SKowalski, Kamil                     // Apply changes
11304a0cb85cSEd Tanous                     if (addressField != nullptr)
11311abe55efSEd Tanous                     {
11324a0cb85cSEd Tanous                         auto callback =
11334a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11344a0cb85cSEd Tanous                              addressField{std::string(*addressField)}](
11354a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11364a0cb85cSEd Tanous                                 if (ec)
11371abe55efSEd Tanous                                 {
1138a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11394a0cb85cSEd Tanous                                     return;
11404a0cb85cSEd Tanous                                 }
11414a0cb85cSEd Tanous                                 asyncResp->res
11424a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11434a0cb85cSEd Tanous                                         entryIdx)]["Address"] = addressField;
11444a0cb85cSEd Tanous                             };
11454a0cb85cSEd Tanous 
11464a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11474a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11484a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11494a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11504a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11514a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Address",
11524a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11534a0cb85cSEd Tanous                                 *addressField));
1154179db1d7SKowalski, Kamil                     }
1155179db1d7SKowalski, Kamil 
11564a0cb85cSEd Tanous                     if (prefixLength && subnetField != nullptr)
11571abe55efSEd Tanous                     {
11584a0cb85cSEd Tanous                         changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
11594a0cb85cSEd Tanous                                                      thisData->id, *subnetField,
11604a0cb85cSEd Tanous                                                      *prefixLength, asyncResp);
1161179db1d7SKowalski, Kamil                     }
1162179db1d7SKowalski, Kamil 
11634a0cb85cSEd Tanous                     if (!addressOriginInDBusFormat.empty() &&
11644a0cb85cSEd Tanous                         addressOriginField != nullptr)
11651abe55efSEd Tanous                     {
11664a0cb85cSEd Tanous                         changeIPv4Origin(ifaceId, entryIdx, thisData->id,
11674a0cb85cSEd Tanous                                          *addressOriginField,
11684a0cb85cSEd Tanous                                          addressOriginInDBusFormat, asyncResp);
1169179db1d7SKowalski, Kamil                     }
1170179db1d7SKowalski, Kamil 
11714a0cb85cSEd Tanous                     if (gatewayField != nullptr)
11721abe55efSEd Tanous                     {
11734a0cb85cSEd Tanous                         auto callback =
11744a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11754a0cb85cSEd Tanous                              gatewayField{std::string(*gatewayField)}](
11764a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11774a0cb85cSEd Tanous                                 if (ec)
11781abe55efSEd Tanous                                 {
1179a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11804a0cb85cSEd Tanous                                     return;
11814a0cb85cSEd Tanous                                 }
11824a0cb85cSEd Tanous                                 asyncResp->res
11834a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11844a0cb85cSEd Tanous                                         entryIdx)]["Gateway"] =
11854a0cb85cSEd Tanous                                     std::move(gatewayField);
11864a0cb85cSEd Tanous                             };
11874a0cb85cSEd Tanous 
11884a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11894a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11904a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11914a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11924a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11934a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Gateway",
11944a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11954a0cb85cSEd Tanous                                 *gatewayField));
11964a0cb85cSEd Tanous                     }
11974a0cb85cSEd Tanous                 }
11984a0cb85cSEd Tanous                 thisData++;
11991abe55efSEd Tanous             }
12001abe55efSEd Tanous             else
12011abe55efSEd Tanous             {
12024a0cb85cSEd Tanous                 // Create IPv4 with provided data
12034a0cb85cSEd Tanous                 if (gatewayField == nullptr)
12041abe55efSEd Tanous                 {
1205a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12064a0cb85cSEd Tanous                                               pathString + "/Gateway");
12074a0cb85cSEd Tanous                     continue;
12084a0cb85cSEd Tanous                 }
12094a0cb85cSEd Tanous 
12104a0cb85cSEd Tanous                 if (addressField == nullptr)
12111abe55efSEd Tanous                 {
1212a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12134a0cb85cSEd Tanous                                               pathString + "/Address");
12144a0cb85cSEd Tanous                     continue;
12154a0cb85cSEd Tanous                 }
12164a0cb85cSEd Tanous 
12174a0cb85cSEd Tanous                 if (!prefixLength)
12181abe55efSEd Tanous                 {
1219a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12204a0cb85cSEd Tanous                                               pathString + "/SubnetMask");
12214a0cb85cSEd Tanous                     continue;
1222588c3f0dSKowalski, Kamil                 }
1223588c3f0dSKowalski, Kamil 
12244a0cb85cSEd Tanous                 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
12254a0cb85cSEd Tanous                            *addressField, asyncResp);
12264a0cb85cSEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
12274a0cb85cSEd Tanous             }
12284a0cb85cSEd Tanous             entryIdx++;
12294a0cb85cSEd Tanous         }
12304a0cb85cSEd Tanous     }
12314a0cb85cSEd Tanous 
1232*0f74e643SEd Tanous     void parseInterfaceData(
1233*0f74e643SEd Tanous         nlohmann::json &json_response, const std::string &iface_id,
1234*0f74e643SEd Tanous         const EthernetInterfaceData &ethData,
12354a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
12364a0cb85cSEd Tanous     {
12374a0cb85cSEd Tanous         json_response["Id"] = iface_id;
12384a0cb85cSEd Tanous         json_response["@odata.id"] =
12394a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
12404a0cb85cSEd Tanous 
12414a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
12424a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
12434a0cb85cSEd Tanous         if (!ethData.hostname.empty())
12444a0cb85cSEd Tanous         {
12454a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
12464a0cb85cSEd Tanous         }
12474a0cb85cSEd Tanous 
12484a0cb85cSEd Tanous         nlohmann::json &vlanObj = json_response["VLAN"];
12494a0cb85cSEd Tanous         if (ethData.vlan_id)
12504a0cb85cSEd Tanous         {
12514a0cb85cSEd Tanous             vlanObj["VLANEnable"] = true;
12524a0cb85cSEd Tanous             vlanObj["VLANId"] = *ethData.vlan_id;
12534a0cb85cSEd Tanous         }
12544a0cb85cSEd Tanous         else
12554a0cb85cSEd Tanous         {
12564a0cb85cSEd Tanous             vlanObj["VLANEnable"] = false;
12574a0cb85cSEd Tanous             vlanObj["VLANId"] = 0;
12584a0cb85cSEd Tanous         }
12594a0cb85cSEd Tanous 
12604a0cb85cSEd Tanous         if (ipv4Data.size() > 0)
12614a0cb85cSEd Tanous         {
12624a0cb85cSEd Tanous             nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
12634a0cb85cSEd Tanous             ipv4_array = nlohmann::json::array();
12644a0cb85cSEd Tanous             for (auto &ipv4_config : ipv4Data)
12654a0cb85cSEd Tanous             {
12664a0cb85cSEd Tanous                 if (!ipv4_config.address.empty())
12674a0cb85cSEd Tanous                 {
12684a0cb85cSEd Tanous                     ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
12694a0cb85cSEd Tanous                                           {"SubnetMask", ipv4_config.netmask},
12704a0cb85cSEd Tanous                                           {"Address", ipv4_config.address}});
12714a0cb85cSEd Tanous 
12724a0cb85cSEd Tanous                     if (!ipv4_config.gateway.empty())
12734a0cb85cSEd Tanous                     {
12744a0cb85cSEd Tanous                         ipv4_array.back()["Gateway"] = ipv4_config.gateway;
12754a0cb85cSEd Tanous                     }
12764a0cb85cSEd Tanous                 }
12774a0cb85cSEd Tanous             }
12784a0cb85cSEd Tanous         }
1279588c3f0dSKowalski, Kamil     }
1280588c3f0dSKowalski, Kamil 
12819391bb9cSRapkiewicz, Pawel     /**
12829391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
12839391bb9cSRapkiewicz, Pawel      */
128455c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12851abe55efSEd Tanous                const std::vector<std::string> &params) override
12861abe55efSEd Tanous     {
12874a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12881abe55efSEd Tanous         if (params.size() != 1)
12891abe55efSEd Tanous         {
1290f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
12919391bb9cSRapkiewicz, Pawel             return;
12929391bb9cSRapkiewicz, Pawel         }
12939391bb9cSRapkiewicz, Pawel 
12944a0cb85cSEd Tanous         getEthernetIfaceData(
12954a0cb85cSEd Tanous             params[0],
12964a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
12974a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
12984a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
12994a0cb85cSEd Tanous                 if (!success)
13001abe55efSEd Tanous                 {
13011abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13021abe55efSEd Tanous                     // object, and other errors
1303f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1304f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
13054a0cb85cSEd Tanous                     return;
13069391bb9cSRapkiewicz, Pawel                 }
1307*0f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1308*0f74e643SEd Tanous                     "#EthernetInterface.v1_2_0.EthernetInterface";
1309*0f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
1310*0f74e643SEd Tanous                     "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
1311*0f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1312*0f74e643SEd Tanous                 asyncResp->res.jsonValue["Description"] =
1313*0f74e643SEd Tanous                     "Management Network Interface";
1314*0f74e643SEd Tanous 
1315*0f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1316*0f74e643SEd Tanous                                    ipv4Data);
13179391bb9cSRapkiewicz, Pawel             });
13189391bb9cSRapkiewicz, Pawel     }
13199391bb9cSRapkiewicz, Pawel 
132055c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
13211abe55efSEd Tanous                  const std::vector<std::string> &params) override
13221abe55efSEd Tanous     {
13234a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
13241abe55efSEd Tanous         if (params.size() != 1)
13251abe55efSEd Tanous         {
1326f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1327588c3f0dSKowalski, Kamil             return;
1328588c3f0dSKowalski, Kamil         }
1329588c3f0dSKowalski, Kamil 
13304a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1331588c3f0dSKowalski, Kamil 
1332179db1d7SKowalski, Kamil         nlohmann::json patchReq;
13331abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, patchReq))
13341abe55efSEd Tanous         {
1335588c3f0dSKowalski, Kamil             return;
1336588c3f0dSKowalski, Kamil         }
1337588c3f0dSKowalski, Kamil 
13384a0cb85cSEd Tanous         // Get single eth interface data, and call the below callback for JSON
1339588c3f0dSKowalski, Kamil         // preparation
13404a0cb85cSEd Tanous         getEthernetIfaceData(
13414a0cb85cSEd Tanous             iface_id,
13424a0cb85cSEd Tanous             [this, asyncResp, iface_id, patchReq = std::move(patchReq)](
13434a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13444a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13451abe55efSEd Tanous                 if (!success)
13461abe55efSEd Tanous                 {
1347588c3f0dSKowalski, Kamil                     // ... otherwise return error
13481abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13491abe55efSEd Tanous                     // object, and other errors
1350f12894f8SJason M. Bills                     messages::resourceNotFound(
1351f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1352588c3f0dSKowalski, Kamil                     return;
1353588c3f0dSKowalski, Kamil                 }
1354588c3f0dSKowalski, Kamil 
1355*0f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1356*0f74e643SEd Tanous                                    ipv4Data);
1357588c3f0dSKowalski, Kamil 
13584a0cb85cSEd Tanous                 for (auto propertyIt : patchReq.items())
13591abe55efSEd Tanous                 {
13601abe55efSEd Tanous                     if (propertyIt.key() == "VLAN")
13611abe55efSEd Tanous                     {
13624a0cb85cSEd Tanous                         handleVlanPatch(iface_id, propertyIt.value(), ethData,
13634a0cb85cSEd Tanous                                         asyncResp);
13641abe55efSEd Tanous                     }
13651abe55efSEd Tanous                     else if (propertyIt.key() == "HostName")
13661abe55efSEd Tanous                     {
13674a0cb85cSEd Tanous                         handleHostnamePatch(propertyIt.value(), asyncResp);
13681abe55efSEd Tanous                     }
13691abe55efSEd Tanous                     else if (propertyIt.key() == "IPv4Addresses")
13701abe55efSEd Tanous                     {
13714a0cb85cSEd Tanous                         handleIPv4Patch(iface_id, propertyIt.value(), ipv4Data,
1372179db1d7SKowalski, Kamil                                         asyncResp);
13731abe55efSEd Tanous                     }
13741abe55efSEd Tanous                     else if (propertyIt.key() == "IPv6Addresses")
13751abe55efSEd Tanous                     {
1376179db1d7SKowalski, Kamil                         // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1377a08b46ccSJason M. Bills                         messages::propertyNotWritable(asyncResp->res,
1378a08b46ccSJason M. Bills                                                       propertyIt.key());
13791abe55efSEd Tanous                     }
13801abe55efSEd Tanous                     else
13811abe55efSEd Tanous                     {
13821abe55efSEd Tanous                         auto fieldInJsonIt =
13834a0cb85cSEd Tanous                             asyncResp->res.jsonValue.find(propertyIt.key());
1384588c3f0dSKowalski, Kamil 
13854a0cb85cSEd Tanous                         if (fieldInJsonIt == asyncResp->res.jsonValue.end())
13861abe55efSEd Tanous                         {
1387588c3f0dSKowalski, Kamil                             // Field not in scope of defined fields
1388f12894f8SJason M. Bills                             messages::propertyUnknown(asyncResp->res,
1389f12894f8SJason M. Bills                                                       propertyIt.key());
13901abe55efSEd Tanous                         }
13914a0cb85cSEd Tanous                         else
13921abe55efSEd Tanous                         {
1393588c3f0dSKowalski, Kamil                             // User attempted to modify non-writable field
1394f12894f8SJason M. Bills                             messages::propertyNotWritable(asyncResp->res,
1395f12894f8SJason M. Bills                                                           propertyIt.key());
1396588c3f0dSKowalski, Kamil                         }
1397588c3f0dSKowalski, Kamil                     }
1398588c3f0dSKowalski, Kamil                 }
1399588c3f0dSKowalski, Kamil             });
1400588c3f0dSKowalski, Kamil     }
14019391bb9cSRapkiewicz, Pawel };
14029391bb9cSRapkiewicz, Pawel 
1403e439f0f8SKowalski, Kamil /**
14044a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
14054a0cb85cSEd Tanous  * Schema
1406e439f0f8SKowalski, Kamil  */
14071abe55efSEd Tanous class VlanNetworkInterface : public Node
14081abe55efSEd Tanous {
1409e439f0f8SKowalski, Kamil   public:
1410e439f0f8SKowalski, Kamil     /*
1411e439f0f8SKowalski, Kamil      * Default Constructor
1412e439f0f8SKowalski, Kamil      */
1413e439f0f8SKowalski, Kamil     template <typename CrowApp>
14141abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
14154a0cb85cSEd Tanous         Node(app,
1416*0f74e643SEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
14171abe55efSEd Tanous              std::string(), std::string())
14181abe55efSEd Tanous     {
1419e439f0f8SKowalski, Kamil         entityPrivileges = {
1420e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1421e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1422e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1423e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1424e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1425e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1426e439f0f8SKowalski, Kamil     }
1427e439f0f8SKowalski, Kamil 
1428e439f0f8SKowalski, Kamil   private:
1429*0f74e643SEd Tanous     void parseInterfaceData(
1430*0f74e643SEd Tanous         nlohmann::json &json_response, const std::string &parent_iface_id,
1431*0f74e643SEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
14324a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
14331abe55efSEd Tanous     {
1434e439f0f8SKowalski, Kamil         // Fill out obvious data...
14354a0cb85cSEd Tanous         json_response["Id"] = iface_id;
14364a0cb85cSEd Tanous         json_response["@odata.id"] =
14374a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
14384a0cb85cSEd Tanous             "/VLANs/" + iface_id;
1439e439f0f8SKowalski, Kamil 
14404a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
14414a0cb85cSEd Tanous         if (ethData.vlan_id)
14424a0cb85cSEd Tanous         {
14434a0cb85cSEd Tanous             json_response["VLANId"] = *ethData.vlan_id;
14444a0cb85cSEd Tanous         }
1445e439f0f8SKowalski, Kamil     }
1446e439f0f8SKowalski, Kamil 
144755c7b7a2SEd Tanous     bool verifyNames(crow::Response &res, const std::string &parent,
14481abe55efSEd Tanous                      const std::string &iface)
14491abe55efSEd Tanous     {
1450f12894f8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14511abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
14521abe55efSEd Tanous         {
1453f12894f8SJason M. Bills             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1454f12894f8SJason M. Bills                                        iface);
1455927a505aSKowalski, Kamil             return false;
14561abe55efSEd Tanous         }
14571abe55efSEd Tanous         else
14581abe55efSEd Tanous         {
1459927a505aSKowalski, Kamil             return true;
1460927a505aSKowalski, Kamil         }
1461927a505aSKowalski, Kamil     }
1462927a505aSKowalski, Kamil 
1463e439f0f8SKowalski, Kamil     /**
1464e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1465e439f0f8SKowalski, Kamil      */
146655c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14671abe55efSEd Tanous                const std::vector<std::string> &params) override
14681abe55efSEd Tanous     {
14694a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14704a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
1471e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1472e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
1473e439f0f8SKowalski, Kamil         // impossible.
14741abe55efSEd Tanous         if (params.size() != 2)
14751abe55efSEd Tanous         {
1476f12894f8SJason M. Bills             messages::internalError(res);
1477e439f0f8SKowalski, Kamil             res.end();
1478e439f0f8SKowalski, Kamil             return;
1479e439f0f8SKowalski, Kamil         }
1480e439f0f8SKowalski, Kamil 
14814a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
14824a0cb85cSEd Tanous         const std::string &iface_id = params[1];
1483*0f74e643SEd Tanous         res.jsonValue["@odata.type"] =
1484*0f74e643SEd Tanous             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1485*0f74e643SEd Tanous         res.jsonValue["@odata.context"] =
1486*0f74e643SEd Tanous             "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1487*0f74e643SEd Tanous         res.jsonValue["Name"] = "VLAN Network Interface";
1488e439f0f8SKowalski, Kamil 
14894a0cb85cSEd Tanous         if (!verifyNames(res, parent_iface_id, iface_id))
14901abe55efSEd Tanous         {
1491a434f2bdSEd Tanous             return;
1492a434f2bdSEd Tanous         }
1493a434f2bdSEd Tanous 
1494e439f0f8SKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1495e439f0f8SKowalski, Kamil         // preparation
14964a0cb85cSEd Tanous         getEthernetIfaceData(
14974a0cb85cSEd Tanous             iface_id,
14984a0cb85cSEd Tanous             [this, asyncResp, parent_iface_id, iface_id](
14994a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15004a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15014a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
15021abe55efSEd Tanous                 {
1503*0f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue,
1504*0f74e643SEd Tanous                                        parent_iface_id, iface_id, ethData,
1505*0f74e643SEd Tanous                                        ipv4Data);
15061abe55efSEd Tanous                 }
15071abe55efSEd Tanous                 else
15081abe55efSEd Tanous                 {
1509e439f0f8SKowalski, Kamil                     // ... otherwise return error
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", iface_id);
1514e439f0f8SKowalski, Kamil                 }
1515e439f0f8SKowalski, Kamil             });
1516e439f0f8SKowalski, Kamil     }
1517e439f0f8SKowalski, Kamil 
151855c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
15191abe55efSEd Tanous                  const std::vector<std::string> &params) override
15201abe55efSEd Tanous     {
15214a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15221abe55efSEd Tanous         if (params.size() != 2)
15231abe55efSEd Tanous         {
1524f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1525e439f0f8SKowalski, Kamil             return;
1526e439f0f8SKowalski, Kamil         }
1527e439f0f8SKowalski, Kamil 
1528d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
152955c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1530927a505aSKowalski, Kamil 
15311abe55efSEd Tanous         if (!verifyNames(res, parentIfaceId, ifaceId))
15321abe55efSEd Tanous         {
1533927a505aSKowalski, Kamil             return;
1534927a505aSKowalski, Kamil         }
1535927a505aSKowalski, Kamil 
1536927a505aSKowalski, Kamil         nlohmann::json patchReq;
15371abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, patchReq))
15381abe55efSEd Tanous         {
1539927a505aSKowalski, Kamil             return;
1540927a505aSKowalski, Kamil         }
1541927a505aSKowalski, Kamil 
1542927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1543927a505aSKowalski, Kamil         // preparation
15444a0cb85cSEd Tanous         getEthernetIfaceData(
15451abe55efSEd Tanous             ifaceId,
15464a0cb85cSEd Tanous             [this, asyncResp, parentIfaceId, ifaceId,
15474a0cb85cSEd Tanous              patchReq{std::move(patchReq)}](
15484a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15494a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15501abe55efSEd Tanous                 if (!success)
15511abe55efSEd Tanous                 {
15521abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
15531abe55efSEd Tanous                     // object, and other errors
1554f12894f8SJason M. Bills                     messages::resourceNotFound(
1555f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1556927a505aSKowalski, Kamil 
1557927a505aSKowalski, Kamil                     return;
1558927a505aSKowalski, Kamil                 }
1559927a505aSKowalski, Kamil 
1560*0f74e643SEd Tanous                 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1561*0f74e643SEd Tanous                                    ifaceId, ethData, ipv4Data);
1562927a505aSKowalski, Kamil 
15634a0cb85cSEd Tanous                 for (auto propertyIt : patchReq.items())
15641abe55efSEd Tanous                 {
1565927a505aSKowalski, Kamil                     if (propertyIt.key() != "VLANEnable" &&
15661abe55efSEd Tanous                         propertyIt.key() != "VLANId")
15671abe55efSEd Tanous                     {
15681abe55efSEd Tanous                         auto fieldInJsonIt =
15694a0cb85cSEd Tanous                             asyncResp->res.jsonValue.find(propertyIt.key());
15704a0cb85cSEd Tanous                         if (fieldInJsonIt == asyncResp->res.jsonValue.end())
15711abe55efSEd Tanous                         {
1572927a505aSKowalski, Kamil                             // Field not in scope of defined fields
1573f12894f8SJason M. Bills                             messages::propertyUnknown(asyncResp->res,
1574f12894f8SJason M. Bills                                                       propertyIt.key());
15751abe55efSEd Tanous                         }
15764a0cb85cSEd Tanous                         else
15771abe55efSEd Tanous                         {
1578927a505aSKowalski, Kamil                             // User attempted to modify non-writable field
1579f12894f8SJason M. Bills                             messages::propertyNotWritable(asyncResp->res,
1580f12894f8SJason M. Bills                                                           propertyIt.key());
1581927a505aSKowalski, Kamil                         }
1582927a505aSKowalski, Kamil                     }
1583927a505aSKowalski, Kamil                 }
1584927a505aSKowalski, Kamil 
15854a0cb85cSEd Tanous                 EthernetInterface::handleVlanPatch(ifaceId, patchReq, ethData,
15864a0cb85cSEd Tanous                                                    asyncResp);
1587927a505aSKowalski, Kamil             });
1588e439f0f8SKowalski, Kamil     }
1589e439f0f8SKowalski, Kamil 
159055c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
15911abe55efSEd Tanous                   const std::vector<std::string> &params) override
15921abe55efSEd Tanous     {
15934a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15941abe55efSEd Tanous         if (params.size() != 2)
15951abe55efSEd Tanous         {
1596f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1597e439f0f8SKowalski, Kamil             return;
1598e439f0f8SKowalski, Kamil         }
1599e439f0f8SKowalski, Kamil 
1600d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
160155c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1602927a505aSKowalski, Kamil 
16034a0cb85cSEd Tanous         if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
16041abe55efSEd Tanous         {
1605927a505aSKowalski, Kamil             return;
1606927a505aSKowalski, Kamil         }
1607927a505aSKowalski, Kamil 
1608927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1609927a505aSKowalski, Kamil         // preparation
1610f12894f8SJason M. Bills         getEthernetIfaceData(
1611f12894f8SJason M. Bills             ifaceId,
1612f12894f8SJason M. Bills             [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
16134a0cb85cSEd Tanous              ifaceId{std::string(ifaceId)}](
1614f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
1615f12894f8SJason M. Bills                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
16164a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
16171abe55efSEd Tanous                 {
1618*0f74e643SEd Tanous                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1619*0f74e643SEd Tanous                                        ifaceId, ethData, ipv4Data);
1620927a505aSKowalski, Kamil 
1621f12894f8SJason M. Bills                     auto callback =
1622f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
16231abe55efSEd Tanous                             if (ec)
16241abe55efSEd Tanous                             {
1625f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1626927a505aSKowalski, Kamil                             }
16274a0cb85cSEd Tanous                         };
16284a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
16294a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
16304a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
16314a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
16321abe55efSEd Tanous                 }
16331abe55efSEd Tanous                 else
16341abe55efSEd Tanous                 {
1635927a505aSKowalski, Kamil                     // ... otherwise return error
1636f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
1637f12894f8SJason M. Bills                     // object, and other errors
1638f12894f8SJason M. Bills                     messages::resourceNotFound(
1639f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1640927a505aSKowalski, Kamil                 }
1641927a505aSKowalski, Kamil             });
1642e439f0f8SKowalski, Kamil     }
1643e439f0f8SKowalski, Kamil };
1644e439f0f8SKowalski, Kamil 
1645e439f0f8SKowalski, Kamil /**
1646e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1647e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1648e439f0f8SKowalski, Kamil  */
16491abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
16501abe55efSEd Tanous {
1651e439f0f8SKowalski, Kamil   public:
1652e439f0f8SKowalski, Kamil     template <typename CrowApp>
16531abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
16544a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
16554a0cb85cSEd Tanous              std::string())
16561abe55efSEd Tanous     {
1657e439f0f8SKowalski, Kamil         entityPrivileges = {
1658e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1659e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1660e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1661e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1662e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1663e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1664e439f0f8SKowalski, Kamil     }
1665e439f0f8SKowalski, Kamil 
1666e439f0f8SKowalski, Kamil   private:
1667e439f0f8SKowalski, Kamil     /**
1668e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1669e439f0f8SKowalski, Kamil      */
167055c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16711abe55efSEd Tanous                const std::vector<std::string> &params) override
16721abe55efSEd Tanous     {
16734a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16741abe55efSEd Tanous         if (params.size() != 1)
16751abe55efSEd Tanous         {
1676e439f0f8SKowalski, Kamil             // This means there is a problem with the router
1677f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1678e439f0f8SKowalski, Kamil             return;
1679e439f0f8SKowalski, Kamil         }
1680e439f0f8SKowalski, Kamil 
16814a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1682e439f0f8SKowalski, Kamil 
16834a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
16841abe55efSEd Tanous         // preparation
1685f12894f8SJason M. Bills         getEthernetIfaceList(
1686f12894f8SJason M. Bills             [this, asyncResp,
1687f12894f8SJason M. Bills              rootInterfaceName{std::string(rootInterfaceName)}](
16881abe55efSEd Tanous                 const bool &success,
16891abe55efSEd Tanous                 const std::vector<std::string> &iface_list) {
16904a0cb85cSEd Tanous                 if (!success)
16911abe55efSEd Tanous                 {
1692f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
16934a0cb85cSEd Tanous                     return;
16941abe55efSEd Tanous                 }
1695*0f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.type"] =
1696*0f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
1697*0f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
1698*0f74e643SEd Tanous                 asyncResp->res.jsonValue["@odata.context"] =
1699*0f74e643SEd Tanous                     "/redfish/v1/$metadata"
1700*0f74e643SEd Tanous                     "#VLanNetworkInterfaceCollection."
1701*0f74e643SEd Tanous                     "VLanNetworkInterfaceCollection";
1702*0f74e643SEd Tanous                 asyncResp->res.jsonValue["Name"] =
1703*0f74e643SEd Tanous                     "VLAN Network Interface Collection";
17044a0cb85cSEd Tanous 
17054a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
17064a0cb85cSEd Tanous 
17074a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
17081abe55efSEd Tanous                 {
17094a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
17104a0cb85cSEd Tanous                     {
17114a0cb85cSEd Tanous                         iface_array.push_back(
17124a0cb85cSEd Tanous                             {{"@odata.id",
17134a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17144a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
1715e439f0f8SKowalski, Kamil                     }
1716e439f0f8SKowalski, Kamil                 }
1717e439f0f8SKowalski, Kamil 
17184a0cb85cSEd Tanous                 if (iface_array.empty())
17191abe55efSEd Tanous                 {
1720f12894f8SJason M. Bills                     messages::resourceNotFound(
1721f12894f8SJason M. Bills                         asyncResp->res, "EthernetInterface", rootInterfaceName);
17224a0cb85cSEd Tanous                     return;
1723e439f0f8SKowalski, Kamil                 }
17244a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
17254a0cb85cSEd Tanous                     iface_array.size();
17264a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
17274a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
17284a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17294a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
1730e439f0f8SKowalski, Kamil             });
1731e439f0f8SKowalski, Kamil     }
1732e439f0f8SKowalski, Kamil 
173355c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
17341abe55efSEd Tanous                 const std::vector<std::string> &params) override
17351abe55efSEd Tanous     {
17364a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17371abe55efSEd Tanous         if (params.size() != 1)
17381abe55efSEd Tanous         {
1739f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1740e439f0f8SKowalski, Kamil             return;
1741e439f0f8SKowalski, Kamil         }
1742e439f0f8SKowalski, Kamil 
1743e439f0f8SKowalski, Kamil         nlohmann::json postReq;
17441abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, postReq))
17451abe55efSEd Tanous         {
1746e439f0f8SKowalski, Kamil             return;
1747e439f0f8SKowalski, Kamil         }
1748e439f0f8SKowalski, Kamil 
1749*0f74e643SEd Tanous         auto vlanIdJson = postReq.find("VLANId");
1750*0f74e643SEd Tanous         if (vlanIdJson == postReq.end())
17511abe55efSEd Tanous         {
1752a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANId");
1753e439f0f8SKowalski, Kamil             return;
1754e439f0f8SKowalski, Kamil         }
1755e439f0f8SKowalski, Kamil 
17564a0cb85cSEd Tanous         const uint64_t *vlanId = vlanIdJson->get_ptr<const uint64_t *>();
17574a0cb85cSEd Tanous         if (vlanId == nullptr)
17581abe55efSEd Tanous         {
1759f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanIdJson->dump(),
1760a08b46ccSJason M. Bills                                              "VLANId");
17614a0cb85cSEd Tanous             return;
1762e439f0f8SKowalski, Kamil         }
17634a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1764e439f0f8SKowalski, Kamil 
17654a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
17661abe55efSEd Tanous             if (ec)
17671abe55efSEd Tanous             {
17684a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
17694a0cb85cSEd Tanous                 // phosphor-network responses
1770f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17714a0cb85cSEd Tanous                 return;
17721abe55efSEd Tanous             }
1773f12894f8SJason M. Bills             messages::created(asyncResp->res);
1774e439f0f8SKowalski, Kamil         };
17754a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
17764a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
17774a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
17784a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
17794a0cb85cSEd Tanous             rootInterfaceName, static_cast<uint32_t>(*vlanId));
17804a0cb85cSEd Tanous     }
17814a0cb85cSEd Tanous };
17829391bb9cSRapkiewicz, Pawel } // namespace redfish
1783