xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision a08b46ccf0fc0081cecc4843484c4f0eb13f5a9a)
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         {
483*a08b46ccSJason 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         {
523*a08b46ccSJason 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         {
563*a08b46ccSJason 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             {
617*a08b46ccSJason 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         {
648*a08b46ccSJason 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     {
7699391bb9cSRapkiewicz, Pawel         Node::json["@odata.type"] =
7709391bb9cSRapkiewicz, Pawel             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
7719391bb9cSRapkiewicz, Pawel         Node::json["@odata.context"] =
7729391bb9cSRapkiewicz, Pawel             "/redfish/v1/"
7739391bb9cSRapkiewicz, Pawel             "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
7744a0cb85cSEd Tanous         Node::json["@odata.id"] = "/redfish/v1/Managers/bmc/EthernetInterfaces";
7759391bb9cSRapkiewicz, Pawel         Node::json["Name"] = "Ethernet Network Interface Collection";
7769391bb9cSRapkiewicz, Pawel         Node::json["Description"] =
7779391bb9cSRapkiewicz, Pawel             "Collection of EthernetInterfaces for this Manager";
7789391bb9cSRapkiewicz, Pawel 
779588c3f0dSKowalski, Kamil         entityPrivileges = {
780588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
781e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
782e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
783e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
784e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
785e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
7869391bb9cSRapkiewicz, Pawel     }
7879391bb9cSRapkiewicz, Pawel 
7889391bb9cSRapkiewicz, Pawel   private:
7899391bb9cSRapkiewicz, Pawel     /**
7909391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
7919391bb9cSRapkiewicz, Pawel      */
79255c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
7931abe55efSEd Tanous                const std::vector<std::string> &params) override
7941abe55efSEd Tanous     {
7954a0cb85cSEd Tanous         res.jsonValue = Node::json;
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     {
8401abe55efSEd Tanous         Node::json["@odata.type"] =
8411abe55efSEd Tanous             "#EthernetInterface.v1_2_0.EthernetInterface";
8429391bb9cSRapkiewicz, Pawel         Node::json["@odata.context"] =
8439391bb9cSRapkiewicz, Pawel             "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
8449391bb9cSRapkiewicz, Pawel         Node::json["Name"] = "Manager Ethernet Interface";
8459391bb9cSRapkiewicz, Pawel         Node::json["Description"] = "Management Network Interface";
8469391bb9cSRapkiewicz, Pawel 
847588c3f0dSKowalski, Kamil         entityPrivileges = {
848588c3f0dSKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
849e0d918bcSEd Tanous             {boost::beast::http::verb::head, {{"Login"}}},
850e0d918bcSEd Tanous             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
851e0d918bcSEd Tanous             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
852e0d918bcSEd Tanous             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
853e0d918bcSEd Tanous             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8549391bb9cSRapkiewicz, Pawel     }
8559391bb9cSRapkiewicz, Pawel 
856e439f0f8SKowalski, Kamil     // TODO(kkowalsk) Find a suitable class/namespace for this
857e439f0f8SKowalski, Kamil     static void handleVlanPatch(const std::string &ifaceId,
858e439f0f8SKowalski, Kamil                                 const nlohmann::json &input,
8594a0cb85cSEd Tanous                                 const EthernetInterfaceData &ethData,
8604a0cb85cSEd Tanous                                 const std::shared_ptr<AsyncResp> asyncResp)
8611abe55efSEd Tanous     {
8621abe55efSEd Tanous         if (!input.is_object())
8631abe55efSEd Tanous         {
864f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
865*a08b46ccSJason M. Bills                                              "VLAN");
866588c3f0dSKowalski, Kamil             return;
867588c3f0dSKowalski, Kamil         }
868588c3f0dSKowalski, Kamil 
8694a0cb85cSEd Tanous         nlohmann::json::const_iterator vlanEnable = input.find("VLANEnable");
8704a0cb85cSEd Tanous         if (vlanEnable == input.end())
8711abe55efSEd Tanous         {
872*a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANEnable");
8734a0cb85cSEd Tanous             return;
8744a0cb85cSEd Tanous         }
8754a0cb85cSEd Tanous         const bool *vlanEnableBool = vlanEnable->get_ptr<const bool *>();
8764a0cb85cSEd Tanous         if (vlanEnableBool == nullptr)
8774a0cb85cSEd Tanous         {
878f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanEnable->dump(),
879*a08b46ccSJason M. Bills                                              "VLANEnable");
880588c3f0dSKowalski, Kamil             return;
881588c3f0dSKowalski, Kamil         }
882588c3f0dSKowalski, Kamil 
8834a0cb85cSEd Tanous         nlohmann::json::const_iterator vlanId = input.find("VLANId");
8844a0cb85cSEd Tanous         if (vlanId == input.end())
8854a0cb85cSEd Tanous         {
886*a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANId");
8874a0cb85cSEd Tanous             return;
8884a0cb85cSEd Tanous         }
8894a0cb85cSEd Tanous         const uint64_t *vlanIdUint = vlanId->get_ptr<const uint64_t *>();
8904a0cb85cSEd Tanous         if (vlanIdUint == nullptr)
8914a0cb85cSEd Tanous         {
892f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanId->dump(),
893*a08b46ccSJason M. Bills                                              "VLANId");
8944a0cb85cSEd Tanous             return;
8954a0cb85cSEd Tanous         }
8964a0cb85cSEd Tanous 
8974a0cb85cSEd Tanous         if (!ethData.vlan_id)
8981abe55efSEd Tanous         {
899e439f0f8SKowalski, Kamil             // This interface is not a VLAN. Cannot do anything with it
900e439f0f8SKowalski, Kamil             // TODO(kkowalsk) Change this message
901*a08b46ccSJason M. Bills             messages::propertyNotWritable(asyncResp->res, "VLANEnable");
902588c3f0dSKowalski, Kamil 
903588c3f0dSKowalski, Kamil             return;
904588c3f0dSKowalski, Kamil         }
905588c3f0dSKowalski, Kamil 
906588c3f0dSKowalski, Kamil         // VLAN is configured on the interface
9074a0cb85cSEd Tanous         if (*vlanEnableBool == true)
9081abe55efSEd Tanous         {
909588c3f0dSKowalski, Kamil             // Change VLAN Id
9104a0cb85cSEd Tanous             asyncResp->res.jsonValue["VLANId"] = *vlanIdUint;
9114a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
9121abe55efSEd Tanous                 if (ec)
9131abe55efSEd Tanous                 {
914f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
9151abe55efSEd Tanous                 }
9161abe55efSEd Tanous                 else
9171abe55efSEd Tanous                 {
9184a0cb85cSEd Tanous                     asyncResp->res.jsonValue["VLANEnable"] = true;
919e439f0f8SKowalski, Kamil                 }
9204a0cb85cSEd Tanous             };
9214a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
9224a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
9234a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
9244a0cb85cSEd Tanous                 "org.freedesktop.DBus.Properties", "Set",
9254a0cb85cSEd Tanous                 "xyz.openbmc_project.Network.VLAN", "Id",
9264a0cb85cSEd Tanous                 sdbusplus::message::variant<uint32_t>(*vlanIdUint));
9271abe55efSEd Tanous         }
9284a0cb85cSEd Tanous         else
9291abe55efSEd Tanous         {
9304a0cb85cSEd Tanous             auto callback = [asyncResp](const boost::system::error_code ec) {
9311abe55efSEd Tanous                 if (ec)
9321abe55efSEd Tanous                 {
933f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
9344a0cb85cSEd Tanous                     return;
9351abe55efSEd Tanous                 }
9364a0cb85cSEd Tanous                 asyncResp->res.jsonValue["VLANEnable"] = false;
9374a0cb85cSEd Tanous             };
9384a0cb85cSEd Tanous 
9394a0cb85cSEd Tanous             crow::connections::systemBus->async_method_call(
9404a0cb85cSEd Tanous                 std::move(callback), "xyz.openbmc_project.Network",
9414a0cb85cSEd Tanous                 "/xyz/openbmc_project/network/" + ifaceId,
9424a0cb85cSEd Tanous                 "xyz.openbmc_project.Object.Delete", "Delete");
943588c3f0dSKowalski, Kamil         }
944588c3f0dSKowalski, Kamil     }
945588c3f0dSKowalski, Kamil 
946e439f0f8SKowalski, Kamil   private:
947588c3f0dSKowalski, Kamil     void handleHostnamePatch(const nlohmann::json &input,
9484a0cb85cSEd Tanous                              const std::shared_ptr<AsyncResp> asyncResp)
9491abe55efSEd Tanous     {
9504a0cb85cSEd Tanous         const std::string *newHostname = input.get_ptr<const std::string *>();
9514a0cb85cSEd Tanous         if (newHostname == nullptr)
9521abe55efSEd Tanous         {
953f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
954*a08b46ccSJason M. Bills                                              "HostName");
9554a0cb85cSEd Tanous             return;
9564a0cb85cSEd Tanous         }
9574a0cb85cSEd Tanous 
9584a0cb85cSEd Tanous         // Change hostname
959*a08b46ccSJason M. Bills         setHostName(*newHostname,
960*a08b46ccSJason M. Bills                     [asyncResp, newHostname{std::string(*newHostname)}](
9614a0cb85cSEd Tanous                         const boost::system::error_code ec) {
9624a0cb85cSEd Tanous                         if (ec)
9634a0cb85cSEd Tanous                         {
964*a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
9651abe55efSEd Tanous                         }
9661abe55efSEd Tanous                         else
9671abe55efSEd Tanous                         {
96855c7b7a2SEd Tanous                             asyncResp->res.jsonValue["HostName"] = newHostname;
969588c3f0dSKowalski, Kamil                         }
970588c3f0dSKowalski, Kamil                     });
971588c3f0dSKowalski, Kamil     }
972588c3f0dSKowalski, Kamil 
9734a0cb85cSEd Tanous     void handleIPv4Patch(
9744a0cb85cSEd Tanous         const std::string &ifaceId, const nlohmann::json &input,
9754a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data,
9764a0cb85cSEd Tanous         const std::shared_ptr<AsyncResp> asyncResp)
9771abe55efSEd Tanous     {
9781abe55efSEd Tanous         if (!input.is_array())
9791abe55efSEd Tanous         {
980f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, input.dump(),
981*a08b46ccSJason M. Bills                                              "IPv4Addresses");
982179db1d7SKowalski, Kamil             return;
983179db1d7SKowalski, Kamil         }
984179db1d7SKowalski, Kamil 
985179db1d7SKowalski, Kamil         // According to Redfish PATCH definition, size must be at least equal
9864a0cb85cSEd Tanous         if (input.size() < ipv4Data.size())
9871abe55efSEd Tanous         {
988*a08b46ccSJason M. Bills             messages::propertyValueFormatError(asyncResp->res, input.dump(),
989*a08b46ccSJason M. Bills                                                "IPv4Addresses");
990179db1d7SKowalski, Kamil             return;
991179db1d7SKowalski, Kamil         }
992179db1d7SKowalski, Kamil 
9934a0cb85cSEd Tanous         int entryIdx = 0;
9944a0cb85cSEd Tanous         boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
9954a0cb85cSEd Tanous             ipv4Data.begin();
9964a0cb85cSEd Tanous         for (const nlohmann::json &thisJson : input)
9971abe55efSEd Tanous         {
9984a0cb85cSEd Tanous             std::string pathString =
999*a08b46ccSJason M. Bills                 "IPv4Addresses/" + std::to_string(entryIdx);
1000179db1d7SKowalski, Kamil             // Check that entry is not of some unexpected type
10014a0cb85cSEd Tanous             if (!thisJson.is_object() && !thisJson.is_null())
10021abe55efSEd Tanous             {
1003*a08b46ccSJason M. Bills                 messages::propertyValueTypeError(asyncResp->res,
1004*a08b46ccSJason M. Bills                                                  thisJson.dump(),
1005*a08b46ccSJason M. Bills                                                  pathString + "/IPv4Address");
1006179db1d7SKowalski, Kamil 
1007179db1d7SKowalski, Kamil                 continue;
1008179db1d7SKowalski, Kamil             }
1009179db1d7SKowalski, Kamil 
10104a0cb85cSEd Tanous             nlohmann::json::const_iterator addressFieldIt =
10114a0cb85cSEd Tanous                 thisJson.find("Address");
10124a0cb85cSEd Tanous             const std::string *addressField = nullptr;
10134a0cb85cSEd Tanous             if (addressFieldIt != thisJson.end())
10141abe55efSEd Tanous             {
10154a0cb85cSEd Tanous                 addressField = addressFieldIt->get_ptr<const std::string *>();
10164a0cb85cSEd Tanous                 if (addressField == nullptr)
10171abe55efSEd Tanous                 {
1018*a08b46ccSJason M. Bills                     messages::propertyValueFormatError(asyncResp->res,
1019*a08b46ccSJason M. Bills                                                        addressFieldIt->dump(),
10204a0cb85cSEd Tanous                                                        pathString + "/Address");
1021179db1d7SKowalski, Kamil                     continue;
1022179db1d7SKowalski, Kamil                 }
10231abe55efSEd Tanous                 else
10241abe55efSEd Tanous                 {
10254a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*addressField))
10264a0cb85cSEd Tanous                     {
1027f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1028*a08b46ccSJason M. Bills                             asyncResp->res, *addressField,
10294a0cb85cSEd Tanous                             pathString + "/Address");
10304a0cb85cSEd Tanous                         continue;
10314a0cb85cSEd Tanous                     }
10324a0cb85cSEd Tanous                 }
10334a0cb85cSEd Tanous             }
10344a0cb85cSEd Tanous 
10354a0cb85cSEd Tanous             boost::optional<uint8_t> prefixLength;
10364a0cb85cSEd Tanous             const std::string *subnetField = nullptr;
10374a0cb85cSEd Tanous             nlohmann::json::const_iterator subnetFieldIt =
10384a0cb85cSEd Tanous                 thisJson.find("SubnetMask");
10394a0cb85cSEd Tanous             if (subnetFieldIt != thisJson.end())
10404a0cb85cSEd Tanous             {
10414a0cb85cSEd Tanous                 subnetField = subnetFieldIt->get_ptr<const std::string *>();
10424a0cb85cSEd Tanous                 if (subnetField == nullptr)
10434a0cb85cSEd Tanous                 {
1044f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1045*a08b46ccSJason M. Bills                         asyncResp->res, *subnetField,
10464a0cb85cSEd Tanous                         pathString + "/SubnetMask");
10474a0cb85cSEd Tanous                     continue;
10484a0cb85cSEd Tanous                 }
10494a0cb85cSEd Tanous                 else
10504a0cb85cSEd Tanous                 {
10514a0cb85cSEd Tanous                     prefixLength = 0;
10524a0cb85cSEd Tanous                     if (!ipv4VerifyIpAndGetBitcount(*subnetField,
10534a0cb85cSEd Tanous                                                     &*prefixLength))
10544a0cb85cSEd Tanous                     {
1055f12894f8SJason M. Bills                         messages::propertyValueFormatError(
1056*a08b46ccSJason M. Bills                             asyncResp->res, *subnetField,
10574a0cb85cSEd Tanous                             pathString + "/SubnetMask");
10584a0cb85cSEd Tanous                         continue;
10594a0cb85cSEd Tanous                     }
10604a0cb85cSEd Tanous                 }
10614a0cb85cSEd Tanous             }
10624a0cb85cSEd Tanous 
10634a0cb85cSEd Tanous             std::string addressOriginInDBusFormat;
10644a0cb85cSEd Tanous             const std::string *addressOriginField = nullptr;
10654a0cb85cSEd Tanous             nlohmann::json::const_iterator addressOriginFieldIt =
10664a0cb85cSEd Tanous                 thisJson.find("AddressOrigin");
10674a0cb85cSEd Tanous             if (addressOriginFieldIt != thisJson.end())
10684a0cb85cSEd Tanous             {
10694a0cb85cSEd Tanous                 const std::string *addressOriginField =
10704a0cb85cSEd Tanous                     addressOriginFieldIt->get_ptr<const std::string *>();
10714a0cb85cSEd Tanous                 if (addressOriginField == nullptr)
10724a0cb85cSEd Tanous                 {
1073f12894f8SJason M. Bills                     messages::propertyValueFormatError(
1074*a08b46ccSJason M. Bills                         asyncResp->res, *addressOriginField,
10754a0cb85cSEd Tanous                         pathString + "/AddressOrigin");
10764a0cb85cSEd Tanous                     continue;
10774a0cb85cSEd Tanous                 }
10784a0cb85cSEd Tanous                 else
10794a0cb85cSEd Tanous                 {
10804a0cb85cSEd Tanous                     // Get Address origin in proper format
10814a0cb85cSEd Tanous                     addressOriginInDBusFormat =
10824a0cb85cSEd Tanous                         translateAddressOriginRedfishToDbus(
10834a0cb85cSEd Tanous                             *addressOriginField);
10844a0cb85cSEd Tanous                     if (addressOriginInDBusFormat.empty())
10854a0cb85cSEd Tanous                     {
10864a0cb85cSEd Tanous                         messages::propertyValueNotInList(
1087f12894f8SJason M. Bills                             asyncResp->res, *addressOriginField,
1088*a08b46ccSJason M. Bills                             pathString + "/AddressOrigin");
10894a0cb85cSEd Tanous                         continue;
10904a0cb85cSEd Tanous                     }
10914a0cb85cSEd Tanous                 }
10924a0cb85cSEd Tanous             }
10934a0cb85cSEd Tanous 
10944a0cb85cSEd Tanous             nlohmann::json::const_iterator gatewayFieldIt =
10954a0cb85cSEd Tanous                 thisJson.find("Gateway");
10964a0cb85cSEd Tanous             const std::string *gatewayField = nullptr;
10974a0cb85cSEd Tanous             if (gatewayFieldIt != thisJson.end())
10984a0cb85cSEd Tanous             {
10994a0cb85cSEd Tanous                 const std::string *gatewayField =
11004a0cb85cSEd Tanous                     gatewayFieldIt->get_ptr<const std::string *>();
11014a0cb85cSEd Tanous                 if (gatewayField == nullptr ||
11024a0cb85cSEd Tanous                     !ipv4VerifyIpAndGetBitcount(*gatewayField))
11034a0cb85cSEd Tanous                 {
1104*a08b46ccSJason M. Bills                     messages::propertyValueFormatError(
1105*a08b46ccSJason M. Bills                         asyncResp->res, *gatewayField, pathString + "/Gateway");
11064a0cb85cSEd Tanous                     continue;
11074a0cb85cSEd Tanous                 }
11084a0cb85cSEd Tanous             }
11094a0cb85cSEd Tanous 
11104a0cb85cSEd Tanous             // if a vlan already exists, modify the existing
11114a0cb85cSEd Tanous             if (thisData != ipv4Data.end())
11124a0cb85cSEd Tanous             {
11131abe55efSEd Tanous                 // Existing object that should be modified/deleted/remain
11141abe55efSEd Tanous                 // unchanged
11154a0cb85cSEd Tanous                 if (thisJson.is_null())
11161abe55efSEd Tanous                 {
11174a0cb85cSEd Tanous                     auto callback = [entryIdx{std::to_string(entryIdx)},
11184a0cb85cSEd Tanous                                      asyncResp](
11194a0cb85cSEd Tanous                                         const boost::system::error_code ec) {
11204a0cb85cSEd Tanous                         if (ec)
11214a0cb85cSEd Tanous                         {
1122*a08b46ccSJason M. Bills                             messages::internalError(asyncResp->res);
11234a0cb85cSEd Tanous                             return;
11241abe55efSEd Tanous                         }
11254a0cb85cSEd Tanous                         asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
11264a0cb85cSEd Tanous                             nullptr;
11274a0cb85cSEd Tanous                     };
11284a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
11294a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
11304a0cb85cSEd Tanous                         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
11314a0cb85cSEd Tanous                             thisData->id,
11324a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
1133179db1d7SKowalski, Kamil                 }
11344a0cb85cSEd Tanous                 else if (thisJson.is_object())
11354a0cb85cSEd Tanous                 {
1136179db1d7SKowalski, Kamil                     // Apply changes
11374a0cb85cSEd Tanous                     if (addressField != nullptr)
11381abe55efSEd Tanous                     {
11394a0cb85cSEd Tanous                         auto callback =
11404a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11414a0cb85cSEd Tanous                              addressField{std::string(*addressField)}](
11424a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11434a0cb85cSEd Tanous                                 if (ec)
11441abe55efSEd Tanous                                 {
1145*a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11464a0cb85cSEd Tanous                                     return;
11474a0cb85cSEd Tanous                                 }
11484a0cb85cSEd Tanous                                 asyncResp->res
11494a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11504a0cb85cSEd Tanous                                         entryIdx)]["Address"] = addressField;
11514a0cb85cSEd Tanous                             };
11524a0cb85cSEd Tanous 
11534a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11544a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11554a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11564a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11574a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
11584a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Address",
11594a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
11604a0cb85cSEd Tanous                                 *addressField));
1161179db1d7SKowalski, Kamil                     }
1162179db1d7SKowalski, Kamil 
11634a0cb85cSEd Tanous                     if (prefixLength && subnetField != nullptr)
11641abe55efSEd Tanous                     {
11654a0cb85cSEd Tanous                         changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
11664a0cb85cSEd Tanous                                                      thisData->id, *subnetField,
11674a0cb85cSEd Tanous                                                      *prefixLength, asyncResp);
1168179db1d7SKowalski, Kamil                     }
1169179db1d7SKowalski, Kamil 
11704a0cb85cSEd Tanous                     if (!addressOriginInDBusFormat.empty() &&
11714a0cb85cSEd Tanous                         addressOriginField != nullptr)
11721abe55efSEd Tanous                     {
11734a0cb85cSEd Tanous                         changeIPv4Origin(ifaceId, entryIdx, thisData->id,
11744a0cb85cSEd Tanous                                          *addressOriginField,
11754a0cb85cSEd Tanous                                          addressOriginInDBusFormat, asyncResp);
1176179db1d7SKowalski, Kamil                     }
1177179db1d7SKowalski, Kamil 
11784a0cb85cSEd Tanous                     if (gatewayField != nullptr)
11791abe55efSEd Tanous                     {
11804a0cb85cSEd Tanous                         auto callback =
11814a0cb85cSEd Tanous                             [asyncResp, entryIdx,
11824a0cb85cSEd Tanous                              gatewayField{std::string(*gatewayField)}](
11834a0cb85cSEd Tanous                                 const boost::system::error_code ec) {
11844a0cb85cSEd Tanous                                 if (ec)
11851abe55efSEd Tanous                                 {
1186*a08b46ccSJason M. Bills                                     messages::internalError(asyncResp->res);
11874a0cb85cSEd Tanous                                     return;
11884a0cb85cSEd Tanous                                 }
11894a0cb85cSEd Tanous                                 asyncResp->res
11904a0cb85cSEd Tanous                                     .jsonValue["IPv4Addresses"][std::to_string(
11914a0cb85cSEd Tanous                                         entryIdx)]["Gateway"] =
11924a0cb85cSEd Tanous                                     std::move(gatewayField);
11934a0cb85cSEd Tanous                             };
11944a0cb85cSEd Tanous 
11954a0cb85cSEd Tanous                         crow::connections::systemBus->async_method_call(
11964a0cb85cSEd Tanous                             std::move(callback), "xyz.openbmc_project.Network",
11974a0cb85cSEd Tanous                             "/xyz/openbmc_project/network/" + ifaceId +
11984a0cb85cSEd Tanous                                 "/ipv4/" + thisData->id,
11994a0cb85cSEd Tanous                             "org.freedesktop.DBus.Properties", "Set",
12004a0cb85cSEd Tanous                             "xyz.openbmc_project.Network.IP", "Gateway",
12014a0cb85cSEd Tanous                             sdbusplus::message::variant<std::string>(
12024a0cb85cSEd Tanous                                 *gatewayField));
12034a0cb85cSEd Tanous                     }
12044a0cb85cSEd Tanous                 }
12054a0cb85cSEd Tanous                 thisData++;
12061abe55efSEd Tanous             }
12071abe55efSEd Tanous             else
12081abe55efSEd Tanous             {
12094a0cb85cSEd Tanous                 // Create IPv4 with provided data
12104a0cb85cSEd Tanous                 if (gatewayField == nullptr)
12111abe55efSEd Tanous                 {
1212*a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12134a0cb85cSEd Tanous                                               pathString + "/Gateway");
12144a0cb85cSEd Tanous                     continue;
12154a0cb85cSEd Tanous                 }
12164a0cb85cSEd Tanous 
12174a0cb85cSEd Tanous                 if (addressField == nullptr)
12181abe55efSEd Tanous                 {
1219*a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12204a0cb85cSEd Tanous                                               pathString + "/Address");
12214a0cb85cSEd Tanous                     continue;
12224a0cb85cSEd Tanous                 }
12234a0cb85cSEd Tanous 
12244a0cb85cSEd Tanous                 if (!prefixLength)
12251abe55efSEd Tanous                 {
1226*a08b46ccSJason M. Bills                     messages::propertyMissing(asyncResp->res,
12274a0cb85cSEd Tanous                                               pathString + "/SubnetMask");
12284a0cb85cSEd Tanous                     continue;
1229588c3f0dSKowalski, Kamil                 }
1230588c3f0dSKowalski, Kamil 
12314a0cb85cSEd Tanous                 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
12324a0cb85cSEd Tanous                            *addressField, asyncResp);
12334a0cb85cSEd Tanous                 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
12344a0cb85cSEd Tanous             }
12354a0cb85cSEd Tanous             entryIdx++;
12364a0cb85cSEd Tanous         }
12374a0cb85cSEd Tanous     }
12384a0cb85cSEd Tanous 
12394a0cb85cSEd Tanous     nlohmann::json parseInterfaceData(
12404a0cb85cSEd Tanous         const std::string &iface_id, const EthernetInterfaceData &ethData,
12414a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
12424a0cb85cSEd Tanous     {
12434a0cb85cSEd Tanous         // Copy JSON object to avoid race condition
12444a0cb85cSEd Tanous         nlohmann::json json_response(Node::json);
12454a0cb85cSEd Tanous 
12464a0cb85cSEd Tanous         json_response["Id"] = iface_id;
12474a0cb85cSEd Tanous         json_response["@odata.id"] =
12484a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
12494a0cb85cSEd Tanous 
12504a0cb85cSEd Tanous         json_response["SpeedMbps"] = ethData.speed;
12514a0cb85cSEd Tanous         json_response["MACAddress"] = ethData.mac_address;
12524a0cb85cSEd Tanous         if (!ethData.hostname.empty())
12534a0cb85cSEd Tanous         {
12544a0cb85cSEd Tanous             json_response["HostName"] = ethData.hostname;
12554a0cb85cSEd Tanous         }
12564a0cb85cSEd Tanous 
12574a0cb85cSEd Tanous         nlohmann::json &vlanObj = json_response["VLAN"];
12584a0cb85cSEd Tanous         if (ethData.vlan_id)
12594a0cb85cSEd Tanous         {
12604a0cb85cSEd Tanous             vlanObj["VLANEnable"] = true;
12614a0cb85cSEd Tanous             vlanObj["VLANId"] = *ethData.vlan_id;
12624a0cb85cSEd Tanous         }
12634a0cb85cSEd Tanous         else
12644a0cb85cSEd Tanous         {
12654a0cb85cSEd Tanous             vlanObj["VLANEnable"] = false;
12664a0cb85cSEd Tanous             vlanObj["VLANId"] = 0;
12674a0cb85cSEd Tanous         }
12684a0cb85cSEd Tanous 
12694a0cb85cSEd Tanous         if (ipv4Data.size() > 0)
12704a0cb85cSEd Tanous         {
12714a0cb85cSEd Tanous             nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
12724a0cb85cSEd Tanous             ipv4_array = nlohmann::json::array();
12734a0cb85cSEd Tanous             for (auto &ipv4_config : ipv4Data)
12744a0cb85cSEd Tanous             {
12754a0cb85cSEd Tanous                 if (!ipv4_config.address.empty())
12764a0cb85cSEd Tanous                 {
12774a0cb85cSEd Tanous                     ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
12784a0cb85cSEd Tanous                                           {"SubnetMask", ipv4_config.netmask},
12794a0cb85cSEd Tanous                                           {"Address", ipv4_config.address}});
12804a0cb85cSEd Tanous 
12814a0cb85cSEd Tanous                     if (!ipv4_config.gateway.empty())
12824a0cb85cSEd Tanous                     {
12834a0cb85cSEd Tanous                         ipv4_array.back()["Gateway"] = ipv4_config.gateway;
12844a0cb85cSEd Tanous                     }
12854a0cb85cSEd Tanous                 }
12864a0cb85cSEd Tanous             }
12874a0cb85cSEd Tanous         }
12884a0cb85cSEd Tanous 
12894a0cb85cSEd Tanous         return json_response;
1290588c3f0dSKowalski, Kamil     }
1291588c3f0dSKowalski, Kamil 
12929391bb9cSRapkiewicz, Pawel     /**
12939391bb9cSRapkiewicz, Pawel      * Functions triggers appropriate requests on DBus
12949391bb9cSRapkiewicz, Pawel      */
129555c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
12961abe55efSEd Tanous                const std::vector<std::string> &params) override
12971abe55efSEd Tanous     {
12984a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
12991abe55efSEd Tanous         if (params.size() != 1)
13001abe55efSEd Tanous         {
1301f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
13029391bb9cSRapkiewicz, Pawel             return;
13039391bb9cSRapkiewicz, Pawel         }
13049391bb9cSRapkiewicz, Pawel 
13054a0cb85cSEd Tanous         getEthernetIfaceData(
13064a0cb85cSEd Tanous             params[0],
13074a0cb85cSEd Tanous             [this, asyncResp, iface_id{std::string(params[0])}](
13084a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13094a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13104a0cb85cSEd Tanous                 if (!success)
13111abe55efSEd Tanous                 {
13121abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13131abe55efSEd Tanous                     // object, and other errors
1314f12894f8SJason M. Bills                     messages::resourceNotFound(asyncResp->res,
1315f12894f8SJason M. Bills                                                "EthernetInterface", iface_id);
13164a0cb85cSEd Tanous                     return;
13179391bb9cSRapkiewicz, Pawel                 }
13184a0cb85cSEd Tanous                 asyncResp->res.jsonValue =
13194a0cb85cSEd Tanous                     parseInterfaceData(iface_id, ethData, ipv4Data);
13209391bb9cSRapkiewicz, Pawel             });
13219391bb9cSRapkiewicz, Pawel     }
13229391bb9cSRapkiewicz, Pawel 
132355c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
13241abe55efSEd Tanous                  const std::vector<std::string> &params) override
13251abe55efSEd Tanous     {
13264a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
13271abe55efSEd Tanous         if (params.size() != 1)
13281abe55efSEd Tanous         {
1329f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1330588c3f0dSKowalski, Kamil             return;
1331588c3f0dSKowalski, Kamil         }
1332588c3f0dSKowalski, Kamil 
13334a0cb85cSEd Tanous         const std::string &iface_id = params[0];
1334588c3f0dSKowalski, Kamil 
1335179db1d7SKowalski, Kamil         nlohmann::json patchReq;
13361abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, patchReq))
13371abe55efSEd Tanous         {
1338588c3f0dSKowalski, Kamil             return;
1339588c3f0dSKowalski, Kamil         }
1340588c3f0dSKowalski, Kamil 
13414a0cb85cSEd Tanous         // Get single eth interface data, and call the below callback for JSON
1342588c3f0dSKowalski, Kamil         // preparation
13434a0cb85cSEd Tanous         getEthernetIfaceData(
13444a0cb85cSEd Tanous             iface_id,
13454a0cb85cSEd Tanous             [this, asyncResp, iface_id, patchReq = std::move(patchReq)](
13464a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
13474a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
13481abe55efSEd Tanous                 if (!success)
13491abe55efSEd Tanous                 {
1350588c3f0dSKowalski, Kamil                     // ... otherwise return error
13511abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
13521abe55efSEd Tanous                     // object, and other errors
1353f12894f8SJason M. Bills                     messages::resourceNotFound(
1354f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1355588c3f0dSKowalski, Kamil                     return;
1356588c3f0dSKowalski, Kamil                 }
1357588c3f0dSKowalski, Kamil 
13584a0cb85cSEd Tanous                 asyncResp->res.jsonValue =
13594a0cb85cSEd Tanous                     parseInterfaceData(iface_id, ethData, ipv4Data);
1360588c3f0dSKowalski, Kamil 
13614a0cb85cSEd Tanous                 for (auto propertyIt : patchReq.items())
13621abe55efSEd Tanous                 {
13631abe55efSEd Tanous                     if (propertyIt.key() == "VLAN")
13641abe55efSEd Tanous                     {
13654a0cb85cSEd Tanous                         handleVlanPatch(iface_id, propertyIt.value(), ethData,
13664a0cb85cSEd Tanous                                         asyncResp);
13671abe55efSEd Tanous                     }
13681abe55efSEd Tanous                     else if (propertyIt.key() == "HostName")
13691abe55efSEd Tanous                     {
13704a0cb85cSEd Tanous                         handleHostnamePatch(propertyIt.value(), asyncResp);
13711abe55efSEd Tanous                     }
13721abe55efSEd Tanous                     else if (propertyIt.key() == "IPv4Addresses")
13731abe55efSEd Tanous                     {
13744a0cb85cSEd Tanous                         handleIPv4Patch(iface_id, propertyIt.value(), ipv4Data,
1375179db1d7SKowalski, Kamil                                         asyncResp);
13761abe55efSEd Tanous                     }
13771abe55efSEd Tanous                     else if (propertyIt.key() == "IPv6Addresses")
13781abe55efSEd Tanous                     {
1379179db1d7SKowalski, Kamil                         // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1380*a08b46ccSJason M. Bills                         messages::propertyNotWritable(asyncResp->res,
1381*a08b46ccSJason M. Bills                                                       propertyIt.key());
13821abe55efSEd Tanous                     }
13831abe55efSEd Tanous                     else
13841abe55efSEd Tanous                     {
13851abe55efSEd Tanous                         auto fieldInJsonIt =
13864a0cb85cSEd Tanous                             asyncResp->res.jsonValue.find(propertyIt.key());
1387588c3f0dSKowalski, Kamil 
13884a0cb85cSEd Tanous                         if (fieldInJsonIt == asyncResp->res.jsonValue.end())
13891abe55efSEd Tanous                         {
1390588c3f0dSKowalski, Kamil                             // Field not in scope of defined fields
1391f12894f8SJason M. Bills                             messages::propertyUnknown(asyncResp->res,
1392f12894f8SJason M. Bills                                                       propertyIt.key());
13931abe55efSEd Tanous                         }
13944a0cb85cSEd Tanous                         else
13951abe55efSEd Tanous                         {
1396588c3f0dSKowalski, Kamil                             // User attempted to modify non-writable field
1397f12894f8SJason M. Bills                             messages::propertyNotWritable(asyncResp->res,
1398f12894f8SJason M. Bills                                                           propertyIt.key());
1399588c3f0dSKowalski, Kamil                         }
1400588c3f0dSKowalski, Kamil                     }
1401588c3f0dSKowalski, Kamil                 }
1402588c3f0dSKowalski, Kamil             });
1403588c3f0dSKowalski, Kamil     }
14049391bb9cSRapkiewicz, Pawel };
14059391bb9cSRapkiewicz, Pawel 
1406e439f0f8SKowalski, Kamil /**
14074a0cb85cSEd Tanous  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
14084a0cb85cSEd Tanous  * Schema
1409e439f0f8SKowalski, Kamil  */
14101abe55efSEd Tanous class VlanNetworkInterface : public Node
14111abe55efSEd Tanous {
1412e439f0f8SKowalski, Kamil   public:
1413e439f0f8SKowalski, Kamil     /*
1414e439f0f8SKowalski, Kamil      * Default Constructor
1415e439f0f8SKowalski, Kamil      */
1416e439f0f8SKowalski, Kamil     template <typename CrowApp>
14171abe55efSEd Tanous     // TODO(Pawel) Remove line from below, where we assume that there is only
14184a0cb85cSEd Tanous     // one manager called openbmc.  This shall be generic, but requires to
14194a0cb85cSEd Tanous     // update GetSubroutes method
14201abe55efSEd Tanous     VlanNetworkInterface(CrowApp &app) :
14214a0cb85cSEd Tanous         Node(app,
14224a0cb85cSEd Tanous              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>>/VLANs/"
14234a0cb85cSEd Tanous              "<str>",
14241abe55efSEd Tanous              std::string(), std::string())
14251abe55efSEd Tanous     {
1426e439f0f8SKowalski, Kamil         Node::json["@odata.type"] =
1427e439f0f8SKowalski, Kamil             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1428e439f0f8SKowalski, Kamil         Node::json["@odata.context"] =
1429e439f0f8SKowalski, Kamil             "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1430e439f0f8SKowalski, Kamil         Node::json["Name"] = "VLAN Network Interface";
1431e439f0f8SKowalski, Kamil 
1432e439f0f8SKowalski, Kamil         entityPrivileges = {
1433e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1434e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1435e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1436e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1437e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1438e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1439e439f0f8SKowalski, Kamil     }
1440e439f0f8SKowalski, Kamil 
1441e439f0f8SKowalski, Kamil   private:
14424a0cb85cSEd Tanous     nlohmann::json parseInterfaceData(
14434a0cb85cSEd Tanous         const std::string &parent_iface_id, const std::string &iface_id,
14444a0cb85cSEd Tanous         const EthernetInterfaceData &ethData,
14454a0cb85cSEd Tanous         const boost::container::flat_set<IPv4AddressData> &ipv4Data)
14461abe55efSEd Tanous     {
1447e439f0f8SKowalski, Kamil         // Copy JSON object to avoid race condition
14484a0cb85cSEd Tanous         nlohmann::json json_response(Node::json);
1449e439f0f8SKowalski, Kamil 
1450e439f0f8SKowalski, Kamil         // Fill out obvious data...
14514a0cb85cSEd Tanous         json_response["Id"] = iface_id;
14524a0cb85cSEd Tanous         json_response["@odata.id"] =
14534a0cb85cSEd Tanous             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
14544a0cb85cSEd Tanous             "/VLANs/" + iface_id;
1455e439f0f8SKowalski, Kamil 
14564a0cb85cSEd Tanous         json_response["VLANEnable"] = true;
14574a0cb85cSEd Tanous         if (ethData.vlan_id)
14584a0cb85cSEd Tanous         {
14594a0cb85cSEd Tanous             json_response["VLANId"] = *ethData.vlan_id;
14604a0cb85cSEd Tanous         }
14614a0cb85cSEd Tanous         return json_response;
1462e439f0f8SKowalski, Kamil     }
1463e439f0f8SKowalski, Kamil 
146455c7b7a2SEd Tanous     bool verifyNames(crow::Response &res, const std::string &parent,
14651abe55efSEd Tanous                      const std::string &iface)
14661abe55efSEd Tanous     {
1467f12894f8SJason M. Bills         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14681abe55efSEd Tanous         if (!boost::starts_with(iface, parent + "_"))
14691abe55efSEd Tanous         {
1470f12894f8SJason M. Bills             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1471f12894f8SJason M. Bills                                        iface);
1472927a505aSKowalski, Kamil             return false;
14731abe55efSEd Tanous         }
14741abe55efSEd Tanous         else
14751abe55efSEd Tanous         {
1476927a505aSKowalski, Kamil             return true;
1477927a505aSKowalski, Kamil         }
1478927a505aSKowalski, Kamil     }
1479927a505aSKowalski, Kamil 
1480e439f0f8SKowalski, Kamil     /**
1481e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1482e439f0f8SKowalski, Kamil      */
148355c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
14841abe55efSEd Tanous                const std::vector<std::string> &params) override
14851abe55efSEd Tanous     {
14864a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
14874a0cb85cSEd Tanous         // TODO(Pawel) this shall be parameterized call (two params) to get
1488e439f0f8SKowalski, Kamil         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1489e439f0f8SKowalski, Kamil         // Check if there is required param, truly entering this shall be
1490e439f0f8SKowalski, Kamil         // impossible.
14911abe55efSEd Tanous         if (params.size() != 2)
14921abe55efSEd Tanous         {
1493f12894f8SJason M. Bills             messages::internalError(res);
1494e439f0f8SKowalski, Kamil             res.end();
1495e439f0f8SKowalski, Kamil             return;
1496e439f0f8SKowalski, Kamil         }
1497e439f0f8SKowalski, Kamil 
14984a0cb85cSEd Tanous         const std::string &parent_iface_id = params[0];
14994a0cb85cSEd Tanous         const std::string &iface_id = params[1];
1500e439f0f8SKowalski, Kamil 
15014a0cb85cSEd Tanous         if (!verifyNames(res, parent_iface_id, iface_id))
15021abe55efSEd Tanous         {
1503a434f2bdSEd Tanous             return;
1504a434f2bdSEd Tanous         }
1505a434f2bdSEd Tanous 
1506e439f0f8SKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1507e439f0f8SKowalski, Kamil         // preparation
15084a0cb85cSEd Tanous         getEthernetIfaceData(
15094a0cb85cSEd Tanous             iface_id,
15104a0cb85cSEd Tanous             [this, asyncResp, parent_iface_id, iface_id](
15114a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15124a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15134a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
15141abe55efSEd Tanous                 {
15154a0cb85cSEd Tanous                     asyncResp->res.jsonValue = parseInterfaceData(
15164a0cb85cSEd Tanous                         parent_iface_id, iface_id, ethData, ipv4Data);
15171abe55efSEd Tanous                 }
15181abe55efSEd Tanous                 else
15191abe55efSEd Tanous                 {
1520e439f0f8SKowalski, Kamil                     // ... otherwise return error
15211abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
15221abe55efSEd Tanous                     // object, and other errors
1523f12894f8SJason M. Bills                     messages::resourceNotFound(
1524f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", iface_id);
1525e439f0f8SKowalski, Kamil                 }
1526e439f0f8SKowalski, Kamil             });
1527e439f0f8SKowalski, Kamil     }
1528e439f0f8SKowalski, Kamil 
152955c7b7a2SEd Tanous     void doPatch(crow::Response &res, const crow::Request &req,
15301abe55efSEd Tanous                  const std::vector<std::string> &params) override
15311abe55efSEd Tanous     {
15324a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
15331abe55efSEd Tanous         if (params.size() != 2)
15341abe55efSEd Tanous         {
1535f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1536e439f0f8SKowalski, Kamil             return;
1537e439f0f8SKowalski, Kamil         }
1538e439f0f8SKowalski, Kamil 
1539d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
154055c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1541927a505aSKowalski, Kamil 
15421abe55efSEd Tanous         if (!verifyNames(res, parentIfaceId, ifaceId))
15431abe55efSEd Tanous         {
1544927a505aSKowalski, Kamil             return;
1545927a505aSKowalski, Kamil         }
1546927a505aSKowalski, Kamil 
1547927a505aSKowalski, Kamil         nlohmann::json patchReq;
15481abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, patchReq))
15491abe55efSEd Tanous         {
1550927a505aSKowalski, Kamil             return;
1551927a505aSKowalski, Kamil         }
1552927a505aSKowalski, Kamil 
1553927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1554927a505aSKowalski, Kamil         // preparation
15554a0cb85cSEd Tanous         getEthernetIfaceData(
15561abe55efSEd Tanous             ifaceId,
15574a0cb85cSEd Tanous             [this, asyncResp, parentIfaceId, ifaceId,
15584a0cb85cSEd Tanous              patchReq{std::move(patchReq)}](
15594a0cb85cSEd Tanous                 const bool &success, const EthernetInterfaceData &ethData,
15604a0cb85cSEd Tanous                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
15611abe55efSEd Tanous                 if (!success)
15621abe55efSEd Tanous                 {
15631abe55efSEd Tanous                     // TODO(Pawel)consider distinguish between non existing
15641abe55efSEd Tanous                     // object, and other errors
1565f12894f8SJason M. Bills                     messages::resourceNotFound(
1566f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1567927a505aSKowalski, Kamil 
1568927a505aSKowalski, Kamil                     return;
1569927a505aSKowalski, Kamil                 }
1570927a505aSKowalski, Kamil 
15714a0cb85cSEd Tanous                 asyncResp->res.jsonValue = parseInterfaceData(
15724a0cb85cSEd Tanous                     parentIfaceId, ifaceId, ethData, ipv4Data);
1573927a505aSKowalski, Kamil 
15744a0cb85cSEd Tanous                 for (auto propertyIt : patchReq.items())
15751abe55efSEd Tanous                 {
1576927a505aSKowalski, Kamil                     if (propertyIt.key() != "VLANEnable" &&
15771abe55efSEd Tanous                         propertyIt.key() != "VLANId")
15781abe55efSEd Tanous                     {
15791abe55efSEd Tanous                         auto fieldInJsonIt =
15804a0cb85cSEd Tanous                             asyncResp->res.jsonValue.find(propertyIt.key());
15814a0cb85cSEd Tanous                         if (fieldInJsonIt == asyncResp->res.jsonValue.end())
15821abe55efSEd Tanous                         {
1583927a505aSKowalski, Kamil                             // Field not in scope of defined fields
1584f12894f8SJason M. Bills                             messages::propertyUnknown(asyncResp->res,
1585f12894f8SJason M. Bills                                                       propertyIt.key());
15861abe55efSEd Tanous                         }
15874a0cb85cSEd Tanous                         else
15881abe55efSEd Tanous                         {
1589927a505aSKowalski, Kamil                             // User attempted to modify non-writable field
1590f12894f8SJason M. Bills                             messages::propertyNotWritable(asyncResp->res,
1591f12894f8SJason M. Bills                                                           propertyIt.key());
1592927a505aSKowalski, Kamil                         }
1593927a505aSKowalski, Kamil                     }
1594927a505aSKowalski, Kamil                 }
1595927a505aSKowalski, Kamil 
15964a0cb85cSEd Tanous                 EthernetInterface::handleVlanPatch(ifaceId, patchReq, ethData,
15974a0cb85cSEd Tanous                                                    asyncResp);
1598927a505aSKowalski, Kamil             });
1599e439f0f8SKowalski, Kamil     }
1600e439f0f8SKowalski, Kamil 
160155c7b7a2SEd Tanous     void doDelete(crow::Response &res, const crow::Request &req,
16021abe55efSEd Tanous                   const std::vector<std::string> &params) override
16031abe55efSEd Tanous     {
16044a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16051abe55efSEd Tanous         if (params.size() != 2)
16061abe55efSEd Tanous         {
1607f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1608e439f0f8SKowalski, Kamil             return;
1609e439f0f8SKowalski, Kamil         }
1610e439f0f8SKowalski, Kamil 
1611d76323e5SEd Tanous         const std::string &parentIfaceId = params[0];
161255c7b7a2SEd Tanous         const std::string &ifaceId = params[1];
1613927a505aSKowalski, Kamil 
16144a0cb85cSEd Tanous         if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
16151abe55efSEd Tanous         {
1616927a505aSKowalski, Kamil             return;
1617927a505aSKowalski, Kamil         }
1618927a505aSKowalski, Kamil 
1619927a505aSKowalski, Kamil         // Get single eth interface data, and call the below callback for JSON
1620927a505aSKowalski, Kamil         // preparation
1621f12894f8SJason M. Bills         getEthernetIfaceData(
1622f12894f8SJason M. Bills             ifaceId,
1623f12894f8SJason M. Bills             [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
16244a0cb85cSEd Tanous              ifaceId{std::string(ifaceId)}](
1625f12894f8SJason M. Bills                 const bool &success, const EthernetInterfaceData &ethData,
1626f12894f8SJason M. Bills                 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
16274a0cb85cSEd Tanous                 if (success && ethData.vlan_id)
16281abe55efSEd Tanous                 {
16294a0cb85cSEd Tanous                     asyncResp->res.jsonValue = parseInterfaceData(
16304a0cb85cSEd Tanous                         parentIfaceId, ifaceId, ethData, ipv4Data);
1631927a505aSKowalski, Kamil 
1632f12894f8SJason M. Bills                     auto callback =
1633f12894f8SJason M. Bills                         [asyncResp](const boost::system::error_code ec) {
16341abe55efSEd Tanous                             if (ec)
16351abe55efSEd Tanous                             {
1636f12894f8SJason M. Bills                                 messages::internalError(asyncResp->res);
1637927a505aSKowalski, Kamil                             }
16384a0cb85cSEd Tanous                         };
16394a0cb85cSEd Tanous                     crow::connections::systemBus->async_method_call(
16404a0cb85cSEd Tanous                         std::move(callback), "xyz.openbmc_project.Network",
16414a0cb85cSEd Tanous                         std::string("/xyz/openbmc_project/network/") + ifaceId,
16424a0cb85cSEd Tanous                         "xyz.openbmc_project.Object.Delete", "Delete");
16431abe55efSEd Tanous                 }
16441abe55efSEd Tanous                 else
16451abe55efSEd Tanous                 {
1646927a505aSKowalski, Kamil                     // ... otherwise return error
1647f12894f8SJason M. Bills                     // TODO(Pawel)consider distinguish between non existing
1648f12894f8SJason M. Bills                     // object, and other errors
1649f12894f8SJason M. Bills                     messages::resourceNotFound(
1650f12894f8SJason M. Bills                         asyncResp->res, "VLAN Network Interface", ifaceId);
1651927a505aSKowalski, Kamil                 }
1652927a505aSKowalski, Kamil             });
1653e439f0f8SKowalski, Kamil     }
1654e439f0f8SKowalski, Kamil };
1655e439f0f8SKowalski, Kamil 
1656e439f0f8SKowalski, Kamil /**
1657e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1658e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1659e439f0f8SKowalski, Kamil  */
16601abe55efSEd Tanous class VlanNetworkInterfaceCollection : public Node
16611abe55efSEd Tanous {
1662e439f0f8SKowalski, Kamil   public:
1663e439f0f8SKowalski, Kamil     template <typename CrowApp>
16641abe55efSEd Tanous     // TODO(Pawel) Remove line from below, where we assume that there is only
16654a0cb85cSEd Tanous     // one manager called openbmc.  This shall be generic, but requires to
16664a0cb85cSEd Tanous     // update GetSubroutes method
16671abe55efSEd Tanous     VlanNetworkInterfaceCollection(CrowApp &app) :
16684a0cb85cSEd Tanous         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
16694a0cb85cSEd Tanous              std::string())
16701abe55efSEd Tanous     {
1671e439f0f8SKowalski, Kamil         Node::json["@odata.type"] =
1672e439f0f8SKowalski, Kamil             "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1673e439f0f8SKowalski, Kamil         Node::json["@odata.context"] =
1674e439f0f8SKowalski, Kamil             "/redfish/v1/$metadata"
1675e439f0f8SKowalski, Kamil             "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1676e439f0f8SKowalski, Kamil         Node::json["Name"] = "VLAN Network Interface Collection";
1677e439f0f8SKowalski, Kamil 
1678e439f0f8SKowalski, Kamil         entityPrivileges = {
1679e439f0f8SKowalski, Kamil             {boost::beast::http::verb::get, {{"Login"}}},
1680e439f0f8SKowalski, Kamil             {boost::beast::http::verb::head, {{"Login"}}},
1681e439f0f8SKowalski, Kamil             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1682e439f0f8SKowalski, Kamil             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1683e439f0f8SKowalski, Kamil             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1684e439f0f8SKowalski, Kamil             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1685e439f0f8SKowalski, Kamil     }
1686e439f0f8SKowalski, Kamil 
1687e439f0f8SKowalski, Kamil   private:
1688e439f0f8SKowalski, Kamil     /**
1689e439f0f8SKowalski, Kamil      * Functions triggers appropriate requests on DBus
1690e439f0f8SKowalski, Kamil      */
169155c7b7a2SEd Tanous     void doGet(crow::Response &res, const crow::Request &req,
16921abe55efSEd Tanous                const std::vector<std::string> &params) override
16931abe55efSEd Tanous     {
16944a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
16951abe55efSEd Tanous         if (params.size() != 1)
16961abe55efSEd Tanous         {
1697e439f0f8SKowalski, Kamil             // This means there is a problem with the router
1698f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1699e439f0f8SKowalski, Kamil             return;
1700e439f0f8SKowalski, Kamil         }
1701e439f0f8SKowalski, Kamil 
17024a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1703e439f0f8SKowalski, Kamil 
17044a0cb85cSEd Tanous         // Get eth interface list, and call the below callback for JSON
17051abe55efSEd Tanous         // preparation
1706f12894f8SJason M. Bills         getEthernetIfaceList(
1707f12894f8SJason M. Bills             [this, asyncResp,
1708f12894f8SJason M. Bills              rootInterfaceName{std::string(rootInterfaceName)}](
17091abe55efSEd Tanous                 const bool &success,
17101abe55efSEd Tanous                 const std::vector<std::string> &iface_list) {
17114a0cb85cSEd Tanous                 if (!success)
17121abe55efSEd Tanous                 {
1713f12894f8SJason M. Bills                     messages::internalError(asyncResp->res);
17144a0cb85cSEd Tanous                     return;
17151abe55efSEd Tanous                 }
17164a0cb85cSEd Tanous                 asyncResp->res.jsonValue = Node::json;
17174a0cb85cSEd Tanous 
17184a0cb85cSEd Tanous                 nlohmann::json iface_array = nlohmann::json::array();
17194a0cb85cSEd Tanous 
17204a0cb85cSEd Tanous                 for (const std::string &iface_item : iface_list)
17211abe55efSEd Tanous                 {
17224a0cb85cSEd Tanous                     if (boost::starts_with(iface_item, rootInterfaceName + "_"))
17234a0cb85cSEd Tanous                     {
17244a0cb85cSEd Tanous                         iface_array.push_back(
17254a0cb85cSEd Tanous                             {{"@odata.id",
17264a0cb85cSEd Tanous                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17274a0cb85cSEd Tanous                                   rootInterfaceName + "/VLANs/" + iface_item}});
1728e439f0f8SKowalski, Kamil                     }
1729e439f0f8SKowalski, Kamil                 }
1730e439f0f8SKowalski, Kamil 
17314a0cb85cSEd Tanous                 if (iface_array.empty())
17321abe55efSEd Tanous                 {
1733f12894f8SJason M. Bills                     messages::resourceNotFound(
1734f12894f8SJason M. Bills                         asyncResp->res, "EthernetInterface", rootInterfaceName);
17354a0cb85cSEd Tanous                     return;
1736e439f0f8SKowalski, Kamil                 }
17374a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members@odata.count"] =
17384a0cb85cSEd Tanous                     iface_array.size();
17394a0cb85cSEd Tanous                 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
17404a0cb85cSEd Tanous                 asyncResp->res.jsonValue["@odata.id"] =
17414a0cb85cSEd Tanous                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
17424a0cb85cSEd Tanous                     rootInterfaceName + "/VLANs";
1743e439f0f8SKowalski, Kamil             });
1744e439f0f8SKowalski, Kamil     }
1745e439f0f8SKowalski, Kamil 
174655c7b7a2SEd Tanous     void doPost(crow::Response &res, const crow::Request &req,
17471abe55efSEd Tanous                 const std::vector<std::string> &params) override
17481abe55efSEd Tanous     {
17494a0cb85cSEd Tanous         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
17501abe55efSEd Tanous         if (params.size() != 1)
17511abe55efSEd Tanous         {
1752f12894f8SJason M. Bills             messages::internalError(asyncResp->res);
1753e439f0f8SKowalski, Kamil             return;
1754e439f0f8SKowalski, Kamil         }
1755e439f0f8SKowalski, Kamil 
1756e439f0f8SKowalski, Kamil         nlohmann::json postReq;
17571abe55efSEd Tanous         if (!json_util::processJsonFromRequest(res, req, postReq))
17581abe55efSEd Tanous         {
1759e439f0f8SKowalski, Kamil             return;
1760e439f0f8SKowalski, Kamil         }
1761e439f0f8SKowalski, Kamil 
17624a0cb85cSEd Tanous         auto vlanIdJson = json.find("VLANId");
17634a0cb85cSEd Tanous         if (vlanIdJson == json.end())
17641abe55efSEd Tanous         {
1765*a08b46ccSJason M. Bills             messages::propertyMissing(asyncResp->res, "VLANId");
1766e439f0f8SKowalski, Kamil             return;
1767e439f0f8SKowalski, Kamil         }
1768e439f0f8SKowalski, Kamil 
17694a0cb85cSEd Tanous         const uint64_t *vlanId = vlanIdJson->get_ptr<const uint64_t *>();
17704a0cb85cSEd Tanous         if (vlanId == nullptr)
17711abe55efSEd Tanous         {
1772f12894f8SJason M. Bills             messages::propertyValueTypeError(asyncResp->res, vlanIdJson->dump(),
1773*a08b46ccSJason M. Bills                                              "VLANId");
17744a0cb85cSEd Tanous             return;
1775e439f0f8SKowalski, Kamil         }
17764a0cb85cSEd Tanous         const std::string &rootInterfaceName = params[0];
1777e439f0f8SKowalski, Kamil 
17784a0cb85cSEd Tanous         auto callback = [asyncResp](const boost::system::error_code ec) {
17791abe55efSEd Tanous             if (ec)
17801abe55efSEd Tanous             {
17814a0cb85cSEd Tanous                 // TODO(ed) make more consistent error messages based on
17824a0cb85cSEd Tanous                 // phosphor-network responses
1783f12894f8SJason M. Bills                 messages::internalError(asyncResp->res);
17844a0cb85cSEd Tanous                 return;
17851abe55efSEd Tanous             }
1786f12894f8SJason M. Bills             messages::created(asyncResp->res);
1787e439f0f8SKowalski, Kamil         };
17884a0cb85cSEd Tanous         crow::connections::systemBus->async_method_call(
17894a0cb85cSEd Tanous             std::move(callback), "xyz.openbmc_project.Network",
17904a0cb85cSEd Tanous             "/xyz/openbmc_project/network",
17914a0cb85cSEd Tanous             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
17924a0cb85cSEd Tanous             rootInterfaceName, static_cast<uint32_t>(*vlanId));
17934a0cb85cSEd Tanous     }
17944a0cb85cSEd Tanous };
17959391bb9cSRapkiewicz, Pawel } // namespace redfish
1796