xref: /openbmc/bmcweb/features/redfish/lib/ethernet.hpp (revision 55c7b7a2e58779580f33046d2dd8649243776700)
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 
18179db1d7SKowalski, Kamil #include <dbus_singleton.hpp>
19588c3f0dSKowalski, Kamil #include <error_messages.hpp>
20179db1d7SKowalski, Kamil #include <node.hpp>
21588c3f0dSKowalski, Kamil #include <utils/json_utils.hpp>
229391bb9cSRapkiewicz, Pawel #include <boost/container/flat_map.hpp>
239391bb9cSRapkiewicz, Pawel 
249391bb9cSRapkiewicz, Pawel namespace redfish {
259391bb9cSRapkiewicz, Pawel 
269391bb9cSRapkiewicz, Pawel /**
279391bb9cSRapkiewicz, Pawel  * DBus types primitives for several generic DBus interfaces
289391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider move this to separate file into boost::dbus
299391bb9cSRapkiewicz, Pawel  */
30aa2e59c1SEd Tanous using PropertiesMapType = boost::container::flat_map<
31aa2e59c1SEd Tanous     std::string,
32aa2e59c1SEd Tanous     sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t,
33aa2e59c1SEd Tanous                                 int32_t, uint32_t, int64_t, uint64_t, double>>;
349391bb9cSRapkiewicz, Pawel 
359391bb9cSRapkiewicz, Pawel using GetManagedObjectsType = boost::container::flat_map<
36aa2e59c1SEd Tanous     sdbusplus::message::object_path,
37aa2e59c1SEd Tanous     boost::container::flat_map<
38aa2e59c1SEd Tanous         std::string,
39aa2e59c1SEd Tanous         boost::container::flat_map<
40aa2e59c1SEd Tanous             std::string, sdbusplus::message::variant<
41aa2e59c1SEd Tanous                              std::string, bool, uint8_t, int16_t, uint16_t,
42aa2e59c1SEd Tanous                              int32_t, uint32_t, int64_t, uint64_t, double>>>>;
439391bb9cSRapkiewicz, Pawel 
449391bb9cSRapkiewicz, Pawel /**
459391bb9cSRapkiewicz, Pawel  * Structure for keeping IPv4 data required by Redfish
469391bb9cSRapkiewicz, Pawel  * TODO(Pawel) consider change everything to ptr, or to non-ptr values.
479391bb9cSRapkiewicz, Pawel  */
489391bb9cSRapkiewicz, Pawel struct IPv4AddressData {
49179db1d7SKowalski, Kamil   std::string id;
509391bb9cSRapkiewicz, Pawel   const std::string *address;
519391bb9cSRapkiewicz, Pawel   const std::string *domain;
529391bb9cSRapkiewicz, Pawel   const std::string *gateway;
539391bb9cSRapkiewicz, Pawel   std::string netmask;
549391bb9cSRapkiewicz, Pawel   std::string origin;
559391bb9cSRapkiewicz, Pawel   bool global;
56179db1d7SKowalski, Kamil   /**
57179db1d7SKowalski, Kamil    * @brief Operator< to enable sorting
58179db1d7SKowalski, Kamil    *
59179db1d7SKowalski, Kamil    * @param[in] obj   Object to compare with
60179db1d7SKowalski, Kamil    *
61179db1d7SKowalski, Kamil    * @return This object id < supplied object id
62179db1d7SKowalski, Kamil    */
63179db1d7SKowalski, Kamil   bool operator<(const IPv4AddressData &obj) const { return (id < obj.id); }
649391bb9cSRapkiewicz, Pawel };
659391bb9cSRapkiewicz, Pawel 
669391bb9cSRapkiewicz, Pawel /**
679391bb9cSRapkiewicz, Pawel  * Structure for keeping basic single Ethernet Interface information
689391bb9cSRapkiewicz, Pawel  * available from DBus
699391bb9cSRapkiewicz, Pawel  */
709391bb9cSRapkiewicz, Pawel struct EthernetInterfaceData {
719391bb9cSRapkiewicz, Pawel   const unsigned int *speed;
72*55c7b7a2SEd Tanous   const bool *autoNeg;
739391bb9cSRapkiewicz, Pawel   const std::string *hostname;
74*55c7b7a2SEd Tanous   const std::string *defaultGateway;
75*55c7b7a2SEd Tanous   const std::string *macAddress;
76*55c7b7a2SEd Tanous   const unsigned int *vlanId;
779391bb9cSRapkiewicz, Pawel };
789391bb9cSRapkiewicz, Pawel 
799391bb9cSRapkiewicz, Pawel /**
809391bb9cSRapkiewicz, Pawel  * OnDemandEthernetProvider
81274fad5aSGunnar Mills  * Ethernet provider class that retrieves data directly from dbus, before
82274fad5aSGunnar Mills  * setting it into JSON output. This does not cache any data.
839391bb9cSRapkiewicz, Pawel  *
849391bb9cSRapkiewicz, Pawel  * TODO(Pawel)
859391bb9cSRapkiewicz, Pawel  * This perhaps shall be different file, which has to be chosen on compile time
869391bb9cSRapkiewicz, Pawel  * depending on OEM needs
879391bb9cSRapkiewicz, Pawel  */
889391bb9cSRapkiewicz, Pawel class OnDemandEthernetProvider {
899391bb9cSRapkiewicz, Pawel  private:
909391bb9cSRapkiewicz, Pawel   // Consts that may have influence on EthernetProvider performance/memory usage
91*55c7b7a2SEd Tanous   const size_t maxIpV4AddressesPerInterface = 10;
929391bb9cSRapkiewicz, Pawel 
939391bb9cSRapkiewicz, Pawel   // Helper function that allows to extract GetAllPropertiesType from
949391bb9cSRapkiewicz, Pawel   // GetManagedObjectsType, based on object path, and interface name
959391bb9cSRapkiewicz, Pawel   const PropertiesMapType *extractInterfaceProperties(
96aa2e59c1SEd Tanous       const sdbusplus::message::object_path &objpath,
97aa2e59c1SEd Tanous       const std::string &interface, const GetManagedObjectsType &dbus_data) {
98*55c7b7a2SEd Tanous     const auto &dbusObj = dbus_data.find(objpath);
99*55c7b7a2SEd Tanous     if (dbusObj != dbus_data.end()) {
100*55c7b7a2SEd Tanous       const auto &iface = dbusObj->second.find(interface);
101*55c7b7a2SEd Tanous       if (iface != dbusObj->second.end()) {
1029391bb9cSRapkiewicz, Pawel         return &iface->second;
1039391bb9cSRapkiewicz, Pawel       }
1049391bb9cSRapkiewicz, Pawel     }
1059391bb9cSRapkiewicz, Pawel     return nullptr;
1069391bb9cSRapkiewicz, Pawel   }
1079391bb9cSRapkiewicz, Pawel 
1089391bb9cSRapkiewicz, Pawel   // Helper Wrapper that does inline object_path conversion from string
109aa2e59c1SEd Tanous   // into sdbusplus::message::object_path type
1109391bb9cSRapkiewicz, Pawel   inline const PropertiesMapType *extractInterfaceProperties(
1119391bb9cSRapkiewicz, Pawel       const std::string &objpath, const std::string &interface,
1129391bb9cSRapkiewicz, Pawel       const GetManagedObjectsType &dbus_data) {
113*55c7b7a2SEd Tanous     const auto &dbusObj = sdbusplus::message::object_path{objpath};
114*55c7b7a2SEd Tanous     return extractInterfaceProperties(dbusObj, interface, dbus_data);
1159391bb9cSRapkiewicz, Pawel   }
1169391bb9cSRapkiewicz, Pawel 
1179391bb9cSRapkiewicz, Pawel   // Helper function that allows to get pointer to the property from
1189391bb9cSRapkiewicz, Pawel   // GetAllPropertiesType native, or extracted by GetAllPropertiesType
1199391bb9cSRapkiewicz, Pawel   template <typename T>
120aa2e59c1SEd Tanous   inline T const *const extractProperty(const PropertiesMapType &properties,
1219391bb9cSRapkiewicz, Pawel                                         const std::string &name) {
1229391bb9cSRapkiewicz, Pawel     const auto &property = properties.find(name);
1239391bb9cSRapkiewicz, Pawel     if (property != properties.end()) {
124*55c7b7a2SEd Tanous       return mapbox::getPtr<const T>(property->second);
1259391bb9cSRapkiewicz, Pawel     }
1269391bb9cSRapkiewicz, Pawel     return nullptr;
1279391bb9cSRapkiewicz, Pawel   }
1289391bb9cSRapkiewicz, Pawel   // TODO(Pawel) Consider to move the above functions to dbus
1299391bb9cSRapkiewicz, Pawel   // generic_interfaces.hpp
1309391bb9cSRapkiewicz, Pawel 
1319391bb9cSRapkiewicz, Pawel   // Helper function that extracts data from several dbus objects and several
1329391bb9cSRapkiewicz, Pawel   // interfaces required by single ethernet interface instance
133*55c7b7a2SEd Tanous   void extractEthernetInterfaceData(const std::string &ethifaceId,
1349391bb9cSRapkiewicz, Pawel                                     const GetManagedObjectsType &dbus_data,
1359391bb9cSRapkiewicz, Pawel                                     EthernetInterfaceData &eth_data) {
1369391bb9cSRapkiewicz, Pawel     // Extract data that contains MAC Address
137*55c7b7a2SEd Tanous     const PropertiesMapType *macProperties = extractInterfaceProperties(
138*55c7b7a2SEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId,
1399391bb9cSRapkiewicz, Pawel         "xyz.openbmc_project.Network.MACAddress", dbus_data);
1409391bb9cSRapkiewicz, Pawel 
141*55c7b7a2SEd Tanous     if (macProperties != nullptr) {
142*55c7b7a2SEd Tanous       eth_data.macAddress =
143*55c7b7a2SEd Tanous           extractProperty<std::string>(*macProperties, "MACAddress");
1449391bb9cSRapkiewicz, Pawel     }
1459391bb9cSRapkiewicz, Pawel 
146*55c7b7a2SEd Tanous     const PropertiesMapType *vlanProperties = extractInterfaceProperties(
147*55c7b7a2SEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId,
148c7070ac2SKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", dbus_data);
149c7070ac2SKowalski, Kamil 
150*55c7b7a2SEd Tanous     if (vlanProperties != nullptr) {
151*55c7b7a2SEd Tanous       eth_data.vlanId = extractProperty<unsigned int>(*vlanProperties, "Id");
152c7070ac2SKowalski, Kamil     }
153c7070ac2SKowalski, Kamil 
1549391bb9cSRapkiewicz, Pawel     // Extract data that contains link information (auto negotiation and speed)
155*55c7b7a2SEd Tanous     const PropertiesMapType *ethProperties = extractInterfaceProperties(
156*55c7b7a2SEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId,
1579391bb9cSRapkiewicz, Pawel         "xyz.openbmc_project.Network.EthernetInterface", dbus_data);
1589391bb9cSRapkiewicz, Pawel 
159*55c7b7a2SEd Tanous     if (ethProperties != nullptr) {
160*55c7b7a2SEd Tanous       eth_data.autoNeg = extractProperty<bool>(*ethProperties, "AutoNeg");
161*55c7b7a2SEd Tanous       eth_data.speed = extractProperty<unsigned int>(*ethProperties, "Speed");
1629391bb9cSRapkiewicz, Pawel     }
1639391bb9cSRapkiewicz, Pawel 
1649391bb9cSRapkiewicz, Pawel     // Extract data that contains network config (HostName and DefaultGW)
165*55c7b7a2SEd Tanous     const PropertiesMapType *configProperties = extractInterfaceProperties(
1669391bb9cSRapkiewicz, Pawel         "/xyz/openbmc_project/network/config",
1679391bb9cSRapkiewicz, Pawel         "xyz.openbmc_project.Network.SystemConfiguration", dbus_data);
1689391bb9cSRapkiewicz, Pawel 
169*55c7b7a2SEd Tanous     if (configProperties != nullptr) {
1709391bb9cSRapkiewicz, Pawel       eth_data.hostname =
171*55c7b7a2SEd Tanous           extractProperty<std::string>(*configProperties, "HostName");
172*55c7b7a2SEd Tanous       eth_data.defaultGateway =
173*55c7b7a2SEd Tanous           extractProperty<std::string>(*configProperties, "DefaultGateway");
1749391bb9cSRapkiewicz, Pawel     }
1759391bb9cSRapkiewicz, Pawel   }
1769391bb9cSRapkiewicz, Pawel 
1779391bb9cSRapkiewicz, Pawel   // Helper function that changes bits netmask notation (i.e. /24)
1789391bb9cSRapkiewicz, Pawel   // into full dot notation
1799391bb9cSRapkiewicz, Pawel   inline std::string getNetmask(unsigned int bits) {
1809391bb9cSRapkiewicz, Pawel     uint32_t value = 0xffffffff << (32 - bits);
1819391bb9cSRapkiewicz, Pawel     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
1829391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 16) & 0xff) + "." +
1839391bb9cSRapkiewicz, Pawel                           std::to_string((value >> 8) & 0xff) + "." +
1849391bb9cSRapkiewicz, Pawel                           std::to_string(value & 0xff);
1859391bb9cSRapkiewicz, Pawel     return netmask;
1869391bb9cSRapkiewicz, Pawel   }
1879391bb9cSRapkiewicz, Pawel 
1889391bb9cSRapkiewicz, Pawel   // Helper function that extracts data for single ethernet ipv4 address
189*55c7b7a2SEd Tanous   void extractIPv4Data(const std::string &ethifaceId,
1909391bb9cSRapkiewicz, Pawel                        const GetManagedObjectsType &dbus_data,
1919391bb9cSRapkiewicz, Pawel                        std::vector<IPv4AddressData> &ipv4_config) {
192179db1d7SKowalski, Kamil     const std::string pathStart =
193*55c7b7a2SEd Tanous         "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/";
194179db1d7SKowalski, Kamil 
1959391bb9cSRapkiewicz, Pawel     // Since there might be several IPv4 configurations aligned with
1969391bb9cSRapkiewicz, Pawel     // single ethernet interface, loop over all of them
1979391bb9cSRapkiewicz, Pawel     for (auto &objpath : dbus_data) {
198274fad5aSGunnar Mills       // Check if proper patter for object path appears
1999391bb9cSRapkiewicz, Pawel       if (boost::starts_with(
200daf36e2eSEd Tanous               static_cast<const std::string &>(objpath.first),
201*55c7b7a2SEd Tanous               "/xyz/openbmc_project/network/" + ethifaceId + "/ipv4/")) {
2029391bb9cSRapkiewicz, Pawel         // and get approrpiate interface
2039391bb9cSRapkiewicz, Pawel         const auto &interface =
2049391bb9cSRapkiewicz, Pawel             objpath.second.find("xyz.openbmc_project.Network.IP");
2059391bb9cSRapkiewicz, Pawel         if (interface != objpath.second.end()) {
2069391bb9cSRapkiewicz, Pawel           // Make a properties 'shortcut', to make everything more readable
2079391bb9cSRapkiewicz, Pawel           const PropertiesMapType &properties = interface->second;
2089391bb9cSRapkiewicz, Pawel           // Instance IPv4AddressData structure, and set as appropriate
209*55c7b7a2SEd Tanous           IPv4AddressData ipv4Address;
210179db1d7SKowalski, Kamil 
211*55c7b7a2SEd Tanous           ipv4Address.id = static_cast<const std::string &>(objpath.first)
212179db1d7SKowalski, Kamil                                .substr(pathStart.size());
213179db1d7SKowalski, Kamil 
2149391bb9cSRapkiewicz, Pawel           // IPv4 address
215*55c7b7a2SEd Tanous           ipv4Address.address =
2169391bb9cSRapkiewicz, Pawel               extractProperty<std::string>(properties, "Address");
2179391bb9cSRapkiewicz, Pawel           // IPv4 gateway
218*55c7b7a2SEd Tanous           ipv4Address.gateway =
2199391bb9cSRapkiewicz, Pawel               extractProperty<std::string>(properties, "Gateway");
2209391bb9cSRapkiewicz, Pawel 
2219391bb9cSRapkiewicz, Pawel           // Origin is kind of DBus object so fetch pointer...
2229391bb9cSRapkiewicz, Pawel           const std::string *origin =
2239391bb9cSRapkiewicz, Pawel               extractProperty<std::string>(properties, "Origin");
2249391bb9cSRapkiewicz, Pawel           if (origin != nullptr) {
225*55c7b7a2SEd Tanous             ipv4Address.origin =
226179db1d7SKowalski, Kamil                 translateAddressOriginBetweenDBusAndRedfish(origin, true, true);
2279391bb9cSRapkiewicz, Pawel           }
2289391bb9cSRapkiewicz, Pawel 
2299391bb9cSRapkiewicz, Pawel           // Netmask is presented as PrefixLength
2309391bb9cSRapkiewicz, Pawel           const auto *mask =
2319391bb9cSRapkiewicz, Pawel               extractProperty<uint8_t>(properties, "PrefixLength");
2329391bb9cSRapkiewicz, Pawel           if (mask != nullptr) {
2339391bb9cSRapkiewicz, Pawel             // convert it to the string
234*55c7b7a2SEd Tanous             ipv4Address.netmask = getNetmask(*mask);
2359391bb9cSRapkiewicz, Pawel           }
2369391bb9cSRapkiewicz, Pawel 
2379391bb9cSRapkiewicz, Pawel           // Attach IPv4 only if address is present
238*55c7b7a2SEd Tanous           if (ipv4Address.address != nullptr) {
239*55c7b7a2SEd Tanous             // Check if given addres is local, or global
240*55c7b7a2SEd Tanous             if (boost::starts_with(*ipv4Address.address, "169.254")) {
241*55c7b7a2SEd Tanous               ipv4Address.global = false;
2429391bb9cSRapkiewicz, Pawel             } else {
243*55c7b7a2SEd Tanous               ipv4Address.global = true;
2449391bb9cSRapkiewicz, Pawel             }
245*55c7b7a2SEd Tanous             ipv4_config.emplace_back(std::move(ipv4Address));
2469391bb9cSRapkiewicz, Pawel           }
2479391bb9cSRapkiewicz, Pawel         }
2489391bb9cSRapkiewicz, Pawel       }
2499391bb9cSRapkiewicz, Pawel     }
250179db1d7SKowalski, Kamil 
251179db1d7SKowalski, Kamil     /**
252179db1d7SKowalski, Kamil      * We have to sort this vector and ensure that order of IPv4 addresses
253179db1d7SKowalski, Kamil      * is consistent between GETs to allow modification and deletion in PATCHes
254179db1d7SKowalski, Kamil      */
255179db1d7SKowalski, Kamil     std::sort(ipv4_config.begin(), ipv4_config.end());
2569391bb9cSRapkiewicz, Pawel   }
2579391bb9cSRapkiewicz, Pawel 
258179db1d7SKowalski, Kamil   static const constexpr int ipV4AddressSectionsCount = 4;
259179db1d7SKowalski, Kamil 
2609391bb9cSRapkiewicz, Pawel  public:
2619391bb9cSRapkiewicz, Pawel   /**
262588c3f0dSKowalski, Kamil    * @brief Creates VLAN for given interface with given Id through D-Bus
263588c3f0dSKowalski, Kamil    *
264588c3f0dSKowalski, Kamil    * @param[in] ifaceId       Id of interface for which VLAN will be created
265588c3f0dSKowalski, Kamil    * @param[in] inputVlanId   ID of the new VLAN
266588c3f0dSKowalski, Kamil    * @param[in] callback      Function that will be called after the operation
267588c3f0dSKowalski, Kamil    *
268588c3f0dSKowalski, Kamil    * @return None.
269588c3f0dSKowalski, Kamil    */
270588c3f0dSKowalski, Kamil   template <typename CallbackFunc>
271588c3f0dSKowalski, Kamil   void createVlan(const std::string &ifaceId, const uint64_t &inputVlanId,
272588c3f0dSKowalski, Kamil                   CallbackFunc &&callback) {
273*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
274588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
275588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN.Create", "VLAN", ifaceId,
276588c3f0dSKowalski, Kamil         static_cast<uint32_t>(inputVlanId));
277588c3f0dSKowalski, Kamil   };
278588c3f0dSKowalski, Kamil 
279588c3f0dSKowalski, Kamil   /**
280588c3f0dSKowalski, Kamil    * @brief Sets given Id on the given VLAN interface through D-Bus
281588c3f0dSKowalski, Kamil    *
282588c3f0dSKowalski, Kamil    * @param[in] ifaceId       Id of VLAN interface that should be modified
283588c3f0dSKowalski, Kamil    * @param[in] inputVlanId   New ID of the VLAN
284588c3f0dSKowalski, Kamil    * @param[in] callback      Function that will be called after the operation
285588c3f0dSKowalski, Kamil    *
286588c3f0dSKowalski, Kamil    * @return None.
287588c3f0dSKowalski, Kamil    */
288588c3f0dSKowalski, Kamil   template <typename CallbackFunc>
289e439f0f8SKowalski, Kamil   static void changeVlanId(const std::string &ifaceId,
290e439f0f8SKowalski, Kamil                            const uint32_t &inputVlanId,
291588c3f0dSKowalski, Kamil                            CallbackFunc &&callback) {
292*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
293588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
294588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
295588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
296588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.VLAN", "Id",
297588c3f0dSKowalski, Kamil         sdbusplus::message::variant<uint32_t>(inputVlanId));
298588c3f0dSKowalski, Kamil   };
299588c3f0dSKowalski, Kamil 
300588c3f0dSKowalski, Kamil   /**
301179db1d7SKowalski, Kamil    * @brief Helper function that verifies IP address to check if it is in
302179db1d7SKowalski, Kamil    *        proper format. If bits pointer is provided, also calculates active
303179db1d7SKowalski, Kamil    *        bit count for Subnet Mask.
304179db1d7SKowalski, Kamil    *
305179db1d7SKowalski, Kamil    * @param[in]  ip     IP that will be verified
306179db1d7SKowalski, Kamil    * @param[out] bits   Calculated mask in bits notation
307179db1d7SKowalski, Kamil    *
308179db1d7SKowalski, Kamil    * @return true in case of success, false otherwise
309179db1d7SKowalski, Kamil    */
310179db1d7SKowalski, Kamil   bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
311179db1d7SKowalski, Kamil                                   uint8_t *bits = nullptr) {
312179db1d7SKowalski, Kamil     std::vector<std::string> bytesInMask;
313179db1d7SKowalski, Kamil 
314179db1d7SKowalski, Kamil     boost::split(bytesInMask, ip, boost::is_any_of("."));
315179db1d7SKowalski, Kamil 
316179db1d7SKowalski, Kamil     if (bytesInMask.size() != ipV4AddressSectionsCount) {
317179db1d7SKowalski, Kamil       return false;
318179db1d7SKowalski, Kamil     }
319179db1d7SKowalski, Kamil 
320179db1d7SKowalski, Kamil     if (bits != nullptr) {
321179db1d7SKowalski, Kamil       *bits = 0;
322179db1d7SKowalski, Kamil     }
323179db1d7SKowalski, Kamil 
324179db1d7SKowalski, Kamil     char *endPtr;
325179db1d7SKowalski, Kamil     long previousValue = 255;
326179db1d7SKowalski, Kamil     bool firstZeroInByteHit;
3271db9ca37SKowalski, Kamil     for (const std::string &byte : bytesInMask) {
3281db9ca37SKowalski, Kamil       if (byte.empty()) {
3291db9ca37SKowalski, Kamil         return false;
3301db9ca37SKowalski, Kamil       }
3311db9ca37SKowalski, Kamil 
332179db1d7SKowalski, Kamil       // Use strtol instead of stroi to avoid exceptions
3331db9ca37SKowalski, Kamil       long value = std::strtol(byte.c_str(), &endPtr, 10);
334179db1d7SKowalski, Kamil 
335179db1d7SKowalski, Kamil       // endPtr should point to the end of the string, otherwise given string
336179db1d7SKowalski, Kamil       // is not 100% number
337179db1d7SKowalski, Kamil       if (*endPtr != '\0') {
338179db1d7SKowalski, Kamil         return false;
339179db1d7SKowalski, Kamil       }
340179db1d7SKowalski, Kamil 
341179db1d7SKowalski, Kamil       // Value should be contained in byte
342179db1d7SKowalski, Kamil       if (value < 0 || value > 255) {
343179db1d7SKowalski, Kamil         return false;
344179db1d7SKowalski, Kamil       }
345179db1d7SKowalski, Kamil 
346179db1d7SKowalski, Kamil       if (bits != nullptr) {
347179db1d7SKowalski, Kamil         // Mask has to be continuous between bytes
348179db1d7SKowalski, Kamil         if (previousValue != 255 && value != 0) {
349179db1d7SKowalski, Kamil           return false;
350179db1d7SKowalski, Kamil         }
351179db1d7SKowalski, Kamil 
352179db1d7SKowalski, Kamil         // Mask has to be continuous inside bytes
353179db1d7SKowalski, Kamil         firstZeroInByteHit = false;
354179db1d7SKowalski, Kamil 
355179db1d7SKowalski, Kamil         // Count bits
356179db1d7SKowalski, Kamil         for (int bitIdx = 7; bitIdx >= 0; bitIdx--) {
357179db1d7SKowalski, Kamil           if (value & (1 << bitIdx)) {
358179db1d7SKowalski, Kamil             if (firstZeroInByteHit) {
359179db1d7SKowalski, Kamil               // Continuity not preserved
360179db1d7SKowalski, Kamil               return false;
361179db1d7SKowalski, Kamil             } else {
362179db1d7SKowalski, Kamil               (*bits)++;
363179db1d7SKowalski, Kamil             }
364179db1d7SKowalski, Kamil           } else {
365179db1d7SKowalski, Kamil             firstZeroInByteHit = true;
366179db1d7SKowalski, Kamil           }
367179db1d7SKowalski, Kamil         }
368179db1d7SKowalski, Kamil       }
369179db1d7SKowalski, Kamil 
370179db1d7SKowalski, Kamil       previousValue = value;
371179db1d7SKowalski, Kamil     }
372179db1d7SKowalski, Kamil 
373179db1d7SKowalski, Kamil     return true;
374179db1d7SKowalski, Kamil   }
375179db1d7SKowalski, Kamil 
376179db1d7SKowalski, Kamil   /**
377179db1d7SKowalski, Kamil    * @brief Changes IPv4 address type property (Address, Gateway)
378179db1d7SKowalski, Kamil    *
379179db1d7SKowalski, Kamil    * @param[in] ifaceId     Id of interface whose IP should be modified
380*55c7b7a2SEd Tanous    * @param[in] ipIdx       index of IP in input array that should be modified
381179db1d7SKowalski, Kamil    * @param[in] ipHash      DBus Hash id of modified IP
382179db1d7SKowalski, Kamil    * @param[in] name        Name of field in JSON representation
383179db1d7SKowalski, Kamil    * @param[in] newValue    New value that should be written
384179db1d7SKowalski, Kamil    * @param[io] asyncResp   Response object that will be returned to client
385179db1d7SKowalski, Kamil    *
386179db1d7SKowalski, Kamil    * @return true if give IP is valid and has been sent do D-Bus, false
387179db1d7SKowalski, Kamil    * otherwise
388179db1d7SKowalski, Kamil    */
389179db1d7SKowalski, Kamil   void changeIPv4AddressProperty(const std::string &ifaceId, int ipIdx,
390179db1d7SKowalski, Kamil                                  const std::string &ipHash,
391179db1d7SKowalski, Kamil                                  const std::string &name,
392179db1d7SKowalski, Kamil                                  const std::string &newValue,
393179db1d7SKowalski, Kamil                                  const std::shared_ptr<AsyncResp> &asyncResp) {
394179db1d7SKowalski, Kamil     auto callback = [
395179db1d7SKowalski, Kamil       asyncResp, ipIdx{std::move(ipIdx)}, name{std::move(name)},
396179db1d7SKowalski, Kamil       newValue{std::move(newValue)}
397179db1d7SKowalski, Kamil     ](const boost::system::error_code ec) {
398179db1d7SKowalski, Kamil       if (ec) {
399179db1d7SKowalski, Kamil         messages::addMessageToJson(
400*55c7b7a2SEd Tanous             asyncResp->res.jsonValue, messages::internalError(),
401179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(ipIdx) + "/" + name);
402179db1d7SKowalski, Kamil       } else {
403*55c7b7a2SEd Tanous         asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
404179db1d7SKowalski, Kamil       }
405179db1d7SKowalski, Kamil     };
406179db1d7SKowalski, Kamil 
407*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
408179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
409179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
410179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
411179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", name,
412179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValue));
413179db1d7SKowalski, Kamil   };
414179db1d7SKowalski, Kamil 
415179db1d7SKowalski, Kamil   /**
416179db1d7SKowalski, Kamil    * @brief Changes IPv4 address origin property
417179db1d7SKowalski, Kamil    *
418179db1d7SKowalski, Kamil    * @param[in] ifaceId       Id of interface whose IP should be modified
419*55c7b7a2SEd Tanous    * @param[in] ipIdx         index of IP in input array that should be modified
420179db1d7SKowalski, Kamil    * @param[in] ipHash        DBus Hash id of modified IP
421179db1d7SKowalski, Kamil    * @param[in] newValue      New value in Redfish format
422179db1d7SKowalski, Kamil    * @param[in] newValueDbus  New value in D-Bus format
423179db1d7SKowalski, Kamil    * @param[io] asyncResp     Response object that will be returned to client
424179db1d7SKowalski, Kamil    *
425179db1d7SKowalski, Kamil    * @return true if give IP is valid and has been sent do D-Bus, false
426179db1d7SKowalski, Kamil    * otherwise
427179db1d7SKowalski, Kamil    */
428179db1d7SKowalski, Kamil   void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
429179db1d7SKowalski, Kamil                         const std::string &ipHash, const std::string &newValue,
430179db1d7SKowalski, Kamil                         const std::string &newValueDbus,
431179db1d7SKowalski, Kamil                         const std::shared_ptr<AsyncResp> &asyncResp) {
432179db1d7SKowalski, Kamil     auto callback =
433179db1d7SKowalski, Kamil         [ asyncResp, ipIdx{std::move(ipIdx)},
434179db1d7SKowalski, Kamil           newValue{std::move(newValue)} ](const boost::system::error_code ec) {
435179db1d7SKowalski, Kamil       if (ec) {
436179db1d7SKowalski, Kamil         messages::addMessageToJson(
437*55c7b7a2SEd Tanous             asyncResp->res.jsonValue, messages::internalError(),
438179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(ipIdx) + "/AddressOrigin");
439179db1d7SKowalski, Kamil       } else {
440*55c7b7a2SEd Tanous         asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
441179db1d7SKowalski, Kamil             newValue;
442179db1d7SKowalski, Kamil       }
443179db1d7SKowalski, Kamil     };
444179db1d7SKowalski, Kamil 
445*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
446179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
447179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
448179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
449179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "Origin",
450179db1d7SKowalski, Kamil         sdbusplus::message::variant<std::string>(newValueDbus));
451179db1d7SKowalski, Kamil   };
452179db1d7SKowalski, Kamil 
453179db1d7SKowalski, Kamil   /**
454179db1d7SKowalski, Kamil    * @brief Modifies SubnetMask for given IP
455179db1d7SKowalski, Kamil    *
456179db1d7SKowalski, Kamil    * @param[in] ifaceId      Id of interface whose IP should be modified
457*55c7b7a2SEd Tanous    * @param[in] ipIdx        index of IP in input array that should be modified
458179db1d7SKowalski, Kamil    * @param[in] ipHash       DBus Hash id of modified IP
459179db1d7SKowalski, Kamil    * @param[in] newValueStr  Mask in dot notation as string
460179db1d7SKowalski, Kamil    * @param[in] newValue     Mask as PrefixLength in bitcount
461179db1d7SKowalski, Kamil    * @param[io] asyncResp   Response object that will be returned to client
462179db1d7SKowalski, Kamil    *
463179db1d7SKowalski, Kamil    * @return None
464179db1d7SKowalski, Kamil    */
465179db1d7SKowalski, Kamil   void changeIPv4SubnetMaskProperty(
466179db1d7SKowalski, Kamil       const std::string &ifaceId, int ipIdx, const std::string &ipHash,
467179db1d7SKowalski, Kamil       const std::string &newValueStr, uint8_t &newValue,
468179db1d7SKowalski, Kamil       const std::shared_ptr<AsyncResp> &asyncResp) {
469179db1d7SKowalski, Kamil     auto callback = [
470179db1d7SKowalski, Kamil       asyncResp, ipIdx{std::move(ipIdx)}, newValueStr{std::move(newValueStr)}
471179db1d7SKowalski, Kamil     ](const boost::system::error_code ec) {
472179db1d7SKowalski, Kamil       if (ec) {
473179db1d7SKowalski, Kamil         messages::addMessageToJson(
474*55c7b7a2SEd Tanous             asyncResp->res.jsonValue, messages::internalError(),
475179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(ipIdx) + "/SubnetMask");
476179db1d7SKowalski, Kamil       } else {
477*55c7b7a2SEd Tanous         asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
478179db1d7SKowalski, Kamil             newValueStr;
479179db1d7SKowalski, Kamil       }
480179db1d7SKowalski, Kamil     };
481179db1d7SKowalski, Kamil 
482*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
483179db1d7SKowalski, Kamil         std::move(callback), "xyz.openbmc_project.Network",
484179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
485179db1d7SKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
486179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP", "PrefixLength",
487179db1d7SKowalski, Kamil         sdbusplus::message::variant<uint8_t>(newValue));
488179db1d7SKowalski, Kamil   };
489179db1d7SKowalski, Kamil 
490179db1d7SKowalski, Kamil   /**
491588c3f0dSKowalski, Kamil    * @brief Disables VLAN with given ifaceId
492588c3f0dSKowalski, Kamil    *
493588c3f0dSKowalski, Kamil    * @param[in] ifaceId   Id of VLAN interface that should be disabled
494588c3f0dSKowalski, Kamil    * @param[in] callback  Function that will be called after the operation
495588c3f0dSKowalski, Kamil    *
496588c3f0dSKowalski, Kamil    * @return None.
497588c3f0dSKowalski, Kamil    */
498588c3f0dSKowalski, Kamil   template <typename CallbackFunc>
499e439f0f8SKowalski, Kamil   static void disableVlan(const std::string &ifaceId, CallbackFunc &&callback) {
500*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
501588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
502588c3f0dSKowalski, Kamil         std::string("/xyz/openbmc_project/network/") + ifaceId,
503588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
504588c3f0dSKowalski, Kamil   };
505588c3f0dSKowalski, Kamil 
506588c3f0dSKowalski, Kamil   /**
507588c3f0dSKowalski, Kamil    * @brief Sets given HostName of the machine through D-Bus
508588c3f0dSKowalski, Kamil    *
509588c3f0dSKowalski, Kamil    * @param[in] newHostname   New name that HostName will be changed to
510588c3f0dSKowalski, Kamil    * @param[in] callback      Function that will be called after the operation
511588c3f0dSKowalski, Kamil    *
512588c3f0dSKowalski, Kamil    * @return None.
513588c3f0dSKowalski, Kamil    */
514588c3f0dSKowalski, Kamil   template <typename CallbackFunc>
515588c3f0dSKowalski, Kamil   void setHostName(const std::string &newHostname, CallbackFunc &&callback) {
516*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
517588c3f0dSKowalski, Kamil         callback, "xyz.openbmc_project.Network",
518588c3f0dSKowalski, Kamil         "/xyz/openbmc_project/network/config",
519588c3f0dSKowalski, Kamil         "org.freedesktop.DBus.Properties", "Set",
520588c3f0dSKowalski, Kamil         "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
521588c3f0dSKowalski, Kamil         sdbusplus::message::variant<std::string>(newHostname));
522588c3f0dSKowalski, Kamil   };
523588c3f0dSKowalski, Kamil 
524588c3f0dSKowalski, Kamil   /**
525179db1d7SKowalski, Kamil    * @brief Deletes given IPv4
526179db1d7SKowalski, Kamil    *
527179db1d7SKowalski, Kamil    * @param[in] ifaceId     Id of interface whose IP should be deleted
528*55c7b7a2SEd Tanous    * @param[in] ipIdx       index of IP in input array that should be deleted
529179db1d7SKowalski, Kamil    * @param[in] ipHash      DBus Hash id of IP that should be deleted
530179db1d7SKowalski, Kamil    * @param[io] asyncResp   Response object that will be returned to client
531179db1d7SKowalski, Kamil    *
532179db1d7SKowalski, Kamil    * @return None
533179db1d7SKowalski, Kamil    */
534179db1d7SKowalski, Kamil   void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
535179db1d7SKowalski, Kamil                   unsigned int ipIdx,
536179db1d7SKowalski, Kamil                   const std::shared_ptr<AsyncResp> &asyncResp) {
537*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
538179db1d7SKowalski, Kamil         [ ipIdx{std::move(ipIdx)}, asyncResp{std::move(asyncResp)} ](
539179db1d7SKowalski, Kamil             const boost::system::error_code ec) {
540179db1d7SKowalski, Kamil           if (ec) {
541179db1d7SKowalski, Kamil             messages::addMessageToJson(
542*55c7b7a2SEd Tanous                 asyncResp->res.jsonValue, messages::internalError(),
543179db1d7SKowalski, Kamil                 "/IPv4Addresses/" + std::to_string(ipIdx) + "/");
544179db1d7SKowalski, Kamil           } else {
545*55c7b7a2SEd Tanous             asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
546179db1d7SKowalski, Kamil           }
547179db1d7SKowalski, Kamil         },
548179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network",
549179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
550179db1d7SKowalski, Kamil         "xyz.openbmc_project.Object.Delete", "Delete");
551179db1d7SKowalski, Kamil   }
552179db1d7SKowalski, Kamil 
553179db1d7SKowalski, Kamil   /**
554179db1d7SKowalski, Kamil    * @brief Creates IPv4 with given data
555179db1d7SKowalski, Kamil    *
556179db1d7SKowalski, Kamil    * @param[in] ifaceId     Id of interface whose IP should be deleted
557*55c7b7a2SEd Tanous    * @param[in] ipIdx       index of IP in input array that should be deleted
558179db1d7SKowalski, Kamil    * @param[in] ipHash      DBus Hash id of IP that should be deleted
559179db1d7SKowalski, Kamil    * @param[io] asyncResp   Response object that will be returned to client
560179db1d7SKowalski, Kamil    *
561179db1d7SKowalski, Kamil    * @return None
562179db1d7SKowalski, Kamil    */
563179db1d7SKowalski, Kamil   void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
564179db1d7SKowalski, Kamil                   uint8_t subnetMask, const std::string &gateway,
565179db1d7SKowalski, Kamil                   const std::string &address,
566179db1d7SKowalski, Kamil                   const std::shared_ptr<AsyncResp> &asyncResp) {
567179db1d7SKowalski, Kamil     auto createIpHandler = [
568179db1d7SKowalski, Kamil       ipIdx{std::move(ipIdx)}, asyncResp{std::move(asyncResp)}
569179db1d7SKowalski, Kamil     ](const boost::system::error_code ec) {
570179db1d7SKowalski, Kamil       if (ec) {
571179db1d7SKowalski, Kamil         messages::addMessageToJson(
572*55c7b7a2SEd Tanous             asyncResp->res.jsonValue, messages::internalError(),
573179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(ipIdx) + "/");
574179db1d7SKowalski, Kamil       }
575179db1d7SKowalski, Kamil     };
576179db1d7SKowalski, Kamil 
577*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
578179db1d7SKowalski, Kamil         std::move(createIpHandler), "xyz.openbmc_project.Network",
579179db1d7SKowalski, Kamil         "/xyz/openbmc_project/network/" + ifaceId,
580179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Create", "IP",
581179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
582179db1d7SKowalski, Kamil         gateway);
583179db1d7SKowalski, Kamil   }
584179db1d7SKowalski, Kamil 
585179db1d7SKowalski, Kamil   /**
586179db1d7SKowalski, Kamil    * @brief Translates Address Origin value from D-Bus to Redfish format and
587179db1d7SKowalski, Kamil    *        vice-versa
588179db1d7SKowalski, Kamil    *
589179db1d7SKowalski, Kamil    * @param[in] inputOrigin Input value that should be translated
590179db1d7SKowalski, Kamil    * @param[in] isIPv4      True for IPv4 origins, False for IPv6
591179db1d7SKowalski, Kamil    * @param[in] isFromDBus  True for DBus->Redfish conversion, false for reverse
592179db1d7SKowalski, Kamil    *
593179db1d7SKowalski, Kamil    * @return Empty string in case of failure, translated value otherwise
594179db1d7SKowalski, Kamil    */
595179db1d7SKowalski, Kamil   std::string translateAddressOriginBetweenDBusAndRedfish(
596179db1d7SKowalski, Kamil       const std::string *inputOrigin, bool isIPv4, bool isFromDBus) {
597179db1d7SKowalski, Kamil     // Invalid pointer
598179db1d7SKowalski, Kamil     if (inputOrigin == nullptr) {
599179db1d7SKowalski, Kamil       return "";
600179db1d7SKowalski, Kamil     }
601179db1d7SKowalski, Kamil 
602179db1d7SKowalski, Kamil     static const constexpr unsigned int firstIPv4OnlyIdx = 1;
603179db1d7SKowalski, Kamil     static const constexpr unsigned int firstIPv6OnlyIdx = 3;
604179db1d7SKowalski, Kamil 
605179db1d7SKowalski, Kamil     std::array<std::pair<const char *, const char *>, 6> translationTable{
606179db1d7SKowalski, Kamil         {{"xyz.openbmc_project.Network.IP.AddressOrigin.Static", "Static"},
607179db1d7SKowalski, Kamil          {"xyz.openbmc_project.Network.IP.AddressOrigin.DHCP", "DHCP"},
608179db1d7SKowalski, Kamil          {"xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal",
609179db1d7SKowalski, Kamil           "IPv4LinkLocal"},
610179db1d7SKowalski, Kamil          {"xyz.openbmc_project.Network.IP.AddressOrigin.DHCP", "DHCPv6"},
611179db1d7SKowalski, Kamil          {"xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal",
612179db1d7SKowalski, Kamil           "LinkLocal"},
613179db1d7SKowalski, Kamil          {"xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC", "SLAAC"}}};
614179db1d7SKowalski, Kamil 
615179db1d7SKowalski, Kamil     for (unsigned int i = 0; i < translationTable.size(); i++) {
616179db1d7SKowalski, Kamil       // Skip unrelated
617179db1d7SKowalski, Kamil       if (isIPv4 && i >= firstIPv6OnlyIdx) break;
618179db1d7SKowalski, Kamil       if (!isIPv4 && i >= firstIPv4OnlyIdx && i < firstIPv6OnlyIdx) continue;
619179db1d7SKowalski, Kamil 
620179db1d7SKowalski, Kamil       // When translating D-Bus to Redfish compare input to first element
621179db1d7SKowalski, Kamil       if (isFromDBus && translationTable[i].first == *inputOrigin)
622179db1d7SKowalski, Kamil         return translationTable[i].second;
623179db1d7SKowalski, Kamil 
624179db1d7SKowalski, Kamil       // When translating Redfish to D-Bus compare input to second element
625179db1d7SKowalski, Kamil       if (!isFromDBus && translationTable[i].second == *inputOrigin)
626179db1d7SKowalski, Kamil         return translationTable[i].first;
627179db1d7SKowalski, Kamil     }
628179db1d7SKowalski, Kamil 
629179db1d7SKowalski, Kamil     // If we are still here, that means that value has not been found
630179db1d7SKowalski, Kamil     return "";
631179db1d7SKowalski, Kamil   }
632179db1d7SKowalski, Kamil 
633179db1d7SKowalski, Kamil   /**
634179db1d7SKowalski, Kamil    * Function that retrieves all properties for given Ethernet Interface
635179db1d7SKowalski, Kamil    * Object
636179db1d7SKowalski, Kamil    * from EntityManager Network Manager
637*55c7b7a2SEd Tanous    * @param ethifaceId a eth interface id to query on DBus
638179db1d7SKowalski, Kamil    * @param callback a function that shall be called to convert Dbus output
639179db1d7SKowalski, Kamil    * into JSON
640179db1d7SKowalski, Kamil    */
641179db1d7SKowalski, Kamil   template <typename CallbackFunc>
642*55c7b7a2SEd Tanous   void getEthernetIfaceData(const std::string &ethifaceId,
643179db1d7SKowalski, Kamil                             CallbackFunc &&callback) {
644*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
645179db1d7SKowalski, Kamil         [
646*55c7b7a2SEd Tanous           this, ethifaceId{std::move(ethifaceId)},
647179db1d7SKowalski, Kamil           callback{std::move(callback)}
648179db1d7SKowalski, Kamil         ](const boost::system::error_code error_code,
649179db1d7SKowalski, Kamil           const GetManagedObjectsType &resp) {
650179db1d7SKowalski, Kamil 
651*55c7b7a2SEd Tanous           EthernetInterfaceData ethData{};
652*55c7b7a2SEd Tanous           std::vector<IPv4AddressData> ipv4Data;
653*55c7b7a2SEd Tanous           ipv4Data.reserve(maxIpV4AddressesPerInterface);
654179db1d7SKowalski, Kamil 
655179db1d7SKowalski, Kamil           if (error_code) {
656179db1d7SKowalski, Kamil             // Something wrong on DBus, the error_code is not important at
657179db1d7SKowalski, Kamil             // this moment, just return success=false, and empty output. Since
658179db1d7SKowalski, Kamil             // size of vector may vary depending on information from Network
659179db1d7SKowalski, Kamil             // Manager, and empty output could not be treated same way as
660179db1d7SKowalski, Kamil             // error.
661*55c7b7a2SEd Tanous             callback(false, ethData, ipv4Data);
662179db1d7SKowalski, Kamil             return;
663179db1d7SKowalski, Kamil           }
664179db1d7SKowalski, Kamil 
665927a505aSKowalski, Kamil           // Find interface
666*55c7b7a2SEd Tanous           if (resp.find("/xyz/openbmc_project/network/" + ethifaceId) ==
667927a505aSKowalski, Kamil               resp.end()) {
668927a505aSKowalski, Kamil             // Interface has not been found
669*55c7b7a2SEd Tanous             callback(false, ethData, ipv4Data);
670927a505aSKowalski, Kamil             return;
671927a505aSKowalski, Kamil           }
672927a505aSKowalski, Kamil 
673*55c7b7a2SEd Tanous           extractEthernetInterfaceData(ethifaceId, resp, ethData);
674*55c7b7a2SEd Tanous           extractIPv4Data(ethifaceId, resp, ipv4Data);
675179db1d7SKowalski, Kamil 
676179db1d7SKowalski, Kamil           // Fix global GW
677*55c7b7a2SEd Tanous           for (IPv4AddressData &ipv4 : ipv4Data) {
678179db1d7SKowalski, Kamil             if ((ipv4.global) &&
679179db1d7SKowalski, Kamil                 ((ipv4.gateway == nullptr) || (*ipv4.gateway == "0.0.0.0"))) {
680*55c7b7a2SEd Tanous               ipv4.gateway = ethData.defaultGateway;
681179db1d7SKowalski, Kamil             }
682179db1d7SKowalski, Kamil           }
683179db1d7SKowalski, Kamil 
684179db1d7SKowalski, Kamil           // Finally make a callback with usefull data
685*55c7b7a2SEd Tanous           callback(true, ethData, ipv4Data);
686179db1d7SKowalski, Kamil         },
687179db1d7SKowalski, Kamil         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
688179db1d7SKowalski, Kamil         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
689179db1d7SKowalski, Kamil   };
690179db1d7SKowalski, Kamil 
691179db1d7SKowalski, Kamil   /**
6929391bb9cSRapkiewicz, Pawel    * Function that retrieves all Ethernet Interfaces available through Network
6939391bb9cSRapkiewicz, Pawel    * Manager
6949391bb9cSRapkiewicz, Pawel    * @param callback a function that shall be called to convert Dbus output into
6959391bb9cSRapkiewicz, Pawel    * JSON.
6969391bb9cSRapkiewicz, Pawel    */
6979391bb9cSRapkiewicz, Pawel   template <typename CallbackFunc>
6989391bb9cSRapkiewicz, Pawel   void getEthernetIfaceList(CallbackFunc &&callback) {
699*55c7b7a2SEd Tanous     crow::connections::systemBus->async_method_call(
7009391bb9cSRapkiewicz, Pawel         [ this, callback{std::move(callback)} ](
7019391bb9cSRapkiewicz, Pawel             const boost::system::error_code error_code,
702aa2e59c1SEd Tanous             GetManagedObjectsType &resp) {
7039391bb9cSRapkiewicz, Pawel           // Callback requires vector<string> to retrieve all available ethernet
7049391bb9cSRapkiewicz, Pawel           // interfaces
705*55c7b7a2SEd Tanous           std::vector<std::string> ifaceList;
706*55c7b7a2SEd Tanous           ifaceList.reserve(resp.size());
7079391bb9cSRapkiewicz, Pawel           if (error_code) {
7089391bb9cSRapkiewicz, Pawel             // Something wrong on DBus, the error_code is not important at this
7099391bb9cSRapkiewicz, Pawel             // moment, just return success=false, and empty output. Since size
7109391bb9cSRapkiewicz, Pawel             // of vector may vary depending on information from Network Manager,
7119391bb9cSRapkiewicz, Pawel             // and empty output could not be treated same way as error.
712*55c7b7a2SEd Tanous             callback(false, ifaceList);
7139391bb9cSRapkiewicz, Pawel             return;
7149391bb9cSRapkiewicz, Pawel           }
7159391bb9cSRapkiewicz, Pawel 
7169391bb9cSRapkiewicz, Pawel           // Iterate over all retrieved ObjectPaths.
7179391bb9cSRapkiewicz, Pawel           for (auto &objpath : resp) {
7189391bb9cSRapkiewicz, Pawel             // And all interfaces available for certain ObjectPath.
7199391bb9cSRapkiewicz, Pawel             for (auto &interface : objpath.second) {
7209391bb9cSRapkiewicz, Pawel               // If interface is xyz.openbmc_project.Network.EthernetInterface,
7219391bb9cSRapkiewicz, Pawel               // this is what we're looking for.
7229391bb9cSRapkiewicz, Pawel               if (interface.first ==
7239391bb9cSRapkiewicz, Pawel                   "xyz.openbmc_project.Network.EthernetInterface") {
724aa2e59c1SEd Tanous                 // Cut out everyting until last "/", ...
725*55c7b7a2SEd Tanous                 const std::string &ifaceId =
726daf36e2eSEd Tanous                     static_cast<const std::string &>(objpath.first);
727*55c7b7a2SEd Tanous                 std::size_t lastPos = ifaceId.rfind("/");
728*55c7b7a2SEd Tanous                 if (lastPos != std::string::npos) {
7299391bb9cSRapkiewicz, Pawel                   // and put it into output vector.
730*55c7b7a2SEd Tanous                   ifaceList.emplace_back(ifaceId.substr(lastPos + 1));
7319391bb9cSRapkiewicz, Pawel                 }
7329391bb9cSRapkiewicz, Pawel               }
7339391bb9cSRapkiewicz, Pawel             }
7349391bb9cSRapkiewicz, Pawel           }
735*55c7b7a2SEd Tanous           // Finally make a callback with usefull data
736*55c7b7a2SEd Tanous           callback(true, ifaceList);
7379391bb9cSRapkiewicz, Pawel         },
738aa2e59c1SEd Tanous         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
739aa2e59c1SEd Tanous         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
7409391bb9cSRapkiewicz, Pawel   };
7419391bb9cSRapkiewicz, Pawel };
7429391bb9cSRapkiewicz, Pawel 
7439391bb9cSRapkiewicz, Pawel /**
7449391bb9cSRapkiewicz, Pawel  * EthernetCollection derived class for delivering Ethernet Collection Schema
7459391bb9cSRapkiewicz, Pawel  */
7469391bb9cSRapkiewicz, Pawel class EthernetCollection : public Node {
7479391bb9cSRapkiewicz, Pawel  public:
7489391bb9cSRapkiewicz, Pawel   // TODO(Pawel) Remove line from below, where we assume that there is only one
7499391bb9cSRapkiewicz, Pawel   // manager called openbmc This shall be generic, but requires to update
7509391bb9cSRapkiewicz, Pawel   // GetSubroutes method
7519391bb9cSRapkiewicz, Pawel   EthernetCollection(CrowApp &app)
7529391bb9cSRapkiewicz, Pawel       : Node(app, "/redfish/v1/Managers/openbmc/EthernetInterfaces/") {
7539391bb9cSRapkiewicz, Pawel     Node::json["@odata.type"] =
7549391bb9cSRapkiewicz, Pawel         "#EthernetInterfaceCollection.EthernetInterfaceCollection";
7559391bb9cSRapkiewicz, Pawel     Node::json["@odata.context"] =
7569391bb9cSRapkiewicz, Pawel         "/redfish/v1/"
7579391bb9cSRapkiewicz, Pawel         "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
7589391bb9cSRapkiewicz, Pawel     Node::json["@odata.id"] = "/redfish/v1/Managers/openbmc/EthernetInterfaces";
7599391bb9cSRapkiewicz, Pawel     Node::json["Name"] = "Ethernet Network Interface Collection";
7609391bb9cSRapkiewicz, Pawel     Node::json["Description"] =
7619391bb9cSRapkiewicz, Pawel         "Collection of EthernetInterfaces for this Manager";
7629391bb9cSRapkiewicz, Pawel 
763588c3f0dSKowalski, Kamil     entityPrivileges = {
764588c3f0dSKowalski, Kamil         {boost::beast::http::verb::get, {{"Login"}}},
765e0d918bcSEd Tanous         {boost::beast::http::verb::head, {{"Login"}}},
766e0d918bcSEd Tanous         {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
767e0d918bcSEd Tanous         {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
768e0d918bcSEd Tanous         {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
769e0d918bcSEd Tanous         {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
7709391bb9cSRapkiewicz, Pawel   }
7719391bb9cSRapkiewicz, Pawel 
7729391bb9cSRapkiewicz, Pawel  private:
7739391bb9cSRapkiewicz, Pawel   /**
7749391bb9cSRapkiewicz, Pawel    * Functions triggers appropriate requests on DBus
7759391bb9cSRapkiewicz, Pawel    */
776*55c7b7a2SEd Tanous   void doGet(crow::Response &res, const crow::Request &req,
7779391bb9cSRapkiewicz, Pawel              const std::vector<std::string> &params) override {
7789391bb9cSRapkiewicz, Pawel     // TODO(Pawel) this shall be parametrized call to get EthernetInterfaces for
7799391bb9cSRapkiewicz, Pawel     // any Manager, not only hardcoded 'openbmc'.
780*55c7b7a2SEd Tanous     std::string managerId = "openbmc";
7819391bb9cSRapkiewicz, Pawel 
782*55c7b7a2SEd Tanous     // get eth interface list, and call the below callback for JSON preparation
783*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceList([&, managerId{std::move(managerId)} ](
7849391bb9cSRapkiewicz, Pawel         const bool &success, const std::vector<std::string> &iface_list) {
7859391bb9cSRapkiewicz, Pawel       if (success) {
786*55c7b7a2SEd Tanous         nlohmann::json ifaceArray = nlohmann::json::array();
787*55c7b7a2SEd Tanous         for (const std::string &ifaceItem : iface_list) {
788*55c7b7a2SEd Tanous           ifaceArray.push_back(
789*55c7b7a2SEd Tanous               {{"@odata.id", "/redfish/v1/Managers/" + managerId +
790*55c7b7a2SEd Tanous                                  "/EthernetInterfaces/" + ifaceItem}});
7919391bb9cSRapkiewicz, Pawel         }
792*55c7b7a2SEd Tanous         Node::json["Members"] = ifaceArray;
793*55c7b7a2SEd Tanous         Node::json["Members@odata.count"] = ifaceArray.size();
7949391bb9cSRapkiewicz, Pawel         Node::json["@odata.id"] =
795*55c7b7a2SEd Tanous             "/redfish/v1/Managers/" + managerId + "/EthernetInterfaces";
796*55c7b7a2SEd Tanous         res.jsonValue = Node::json;
7979391bb9cSRapkiewicz, Pawel       } else {
7989391bb9cSRapkiewicz, Pawel         // No success, best what we can do is return INTERNALL ERROR
799e0d918bcSEd Tanous         res.result(boost::beast::http::status::internal_server_error);
8009391bb9cSRapkiewicz, Pawel       }
8019391bb9cSRapkiewicz, Pawel       res.end();
8029391bb9cSRapkiewicz, Pawel     });
8039391bb9cSRapkiewicz, Pawel   }
8049391bb9cSRapkiewicz, Pawel 
8059391bb9cSRapkiewicz, Pawel   // Ethernet Provider object
8069391bb9cSRapkiewicz, Pawel   // TODO(Pawel) consider move it to singleton
807*55c7b7a2SEd Tanous   OnDemandEthernetProvider ethernetProvider;
8089391bb9cSRapkiewicz, Pawel };
8099391bb9cSRapkiewicz, Pawel 
8109391bb9cSRapkiewicz, Pawel /**
8119391bb9cSRapkiewicz, Pawel  * EthernetInterface derived class for delivering Ethernet Schema
8129391bb9cSRapkiewicz, Pawel  */
8139391bb9cSRapkiewicz, Pawel class EthernetInterface : public Node {
8149391bb9cSRapkiewicz, Pawel  public:
8159391bb9cSRapkiewicz, Pawel   /*
8169391bb9cSRapkiewicz, Pawel    * Default Constructor
8179391bb9cSRapkiewicz, Pawel    */
8189391bb9cSRapkiewicz, Pawel   // TODO(Pawel) Remove line from below, where we assume that there is only one
8199391bb9cSRapkiewicz, Pawel   // manager called openbmc This shall be generic, but requires to update
8209391bb9cSRapkiewicz, Pawel   // GetSubroutes method
8219391bb9cSRapkiewicz, Pawel   EthernetInterface(CrowApp &app)
8229391bb9cSRapkiewicz, Pawel       : Node(app, "/redfish/v1/Managers/openbmc/EthernetInterfaces/<str>/",
8239391bb9cSRapkiewicz, Pawel              std::string()) {
8249391bb9cSRapkiewicz, Pawel     Node::json["@odata.type"] = "#EthernetInterface.v1_2_0.EthernetInterface";
8259391bb9cSRapkiewicz, Pawel     Node::json["@odata.context"] =
8269391bb9cSRapkiewicz, Pawel         "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
8279391bb9cSRapkiewicz, Pawel     Node::json["Name"] = "Manager Ethernet Interface";
8289391bb9cSRapkiewicz, Pawel     Node::json["Description"] = "Management Network Interface";
8299391bb9cSRapkiewicz, Pawel 
830588c3f0dSKowalski, Kamil     entityPrivileges = {
831588c3f0dSKowalski, Kamil         {boost::beast::http::verb::get, {{"Login"}}},
832e0d918bcSEd Tanous         {boost::beast::http::verb::head, {{"Login"}}},
833e0d918bcSEd Tanous         {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
834e0d918bcSEd Tanous         {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
835e0d918bcSEd Tanous         {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
836e0d918bcSEd Tanous         {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
8379391bb9cSRapkiewicz, Pawel   }
8389391bb9cSRapkiewicz, Pawel 
839e439f0f8SKowalski, Kamil   // TODO(kkowalsk) Find a suitable class/namespace for this
840e439f0f8SKowalski, Kamil   static void handleVlanPatch(const std::string &ifaceId,
841e439f0f8SKowalski, Kamil                               const nlohmann::json &input,
842588c3f0dSKowalski, Kamil                               const EthernetInterfaceData &eth_data,
843e439f0f8SKowalski, Kamil                               const std::string &pathPrefix,
844588c3f0dSKowalski, Kamil                               const std::shared_ptr<AsyncResp> &asyncResp) {
845588c3f0dSKowalski, Kamil     if (!input.is_object()) {
846588c3f0dSKowalski, Kamil       messages::addMessageToJson(
847*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
848e439f0f8SKowalski, Kamil           messages::propertyValueTypeError(input.dump(), "VLAN"), pathPrefix);
849588c3f0dSKowalski, Kamil       return;
850588c3f0dSKowalski, Kamil     }
851588c3f0dSKowalski, Kamil 
852e439f0f8SKowalski, Kamil     const std::string pathStart = (pathPrefix == "/") ? "" : pathPrefix;
853e439f0f8SKowalski, Kamil     nlohmann::json &paramsJson =
854e439f0f8SKowalski, Kamil         (pathPrefix == "/")
855*55c7b7a2SEd Tanous             ? asyncResp->res.jsonValue
856*55c7b7a2SEd Tanous             : asyncResp->res.jsonValue[nlohmann::json_pointer<nlohmann::json>(
857e439f0f8SKowalski, Kamil                   pathPrefix)];
858588c3f0dSKowalski, Kamil     bool inputVlanEnabled;
859588c3f0dSKowalski, Kamil     uint64_t inputVlanId;
860e439f0f8SKowalski, Kamil 
861588c3f0dSKowalski, Kamil     json_util::Result inputVlanEnabledState = json_util::getBool(
862588c3f0dSKowalski, Kamil         "VLANEnable", input, inputVlanEnabled,
863588c3f0dSKowalski, Kamil         static_cast<int>(json_util::MessageSetting::TYPE_ERROR),
864*55c7b7a2SEd Tanous         asyncResp->res.jsonValue, std::string(pathStart + "/VLANEnable"));
865588c3f0dSKowalski, Kamil     json_util::Result inputVlanIdState = json_util::getUnsigned(
866588c3f0dSKowalski, Kamil         "VLANId", input, inputVlanId,
867588c3f0dSKowalski, Kamil         static_cast<int>(json_util::MessageSetting::TYPE_ERROR),
868*55c7b7a2SEd Tanous         asyncResp->res.jsonValue, std::string(pathStart + "/VLANId"));
869588c3f0dSKowalski, Kamil     bool inputInvalid = false;
870588c3f0dSKowalski, Kamil 
871588c3f0dSKowalski, Kamil     // Do not proceed if fields in VLAN object were of wrong type
872588c3f0dSKowalski, Kamil     if (inputVlanEnabledState == json_util::Result::WRONG_TYPE ||
873588c3f0dSKowalski, Kamil         inputVlanIdState == json_util::Result::WRONG_TYPE) {
874588c3f0dSKowalski, Kamil       return;
875588c3f0dSKowalski, Kamil     }
876588c3f0dSKowalski, Kamil 
877588c3f0dSKowalski, Kamil     // Verify input
878*55c7b7a2SEd Tanous     if (eth_data.vlanId == nullptr) {
879e439f0f8SKowalski, Kamil       // This interface is not a VLAN. Cannot do anything with it
880e439f0f8SKowalski, Kamil       // TODO(kkowalsk) Change this message
881*55c7b7a2SEd Tanous       messages::addMessageToJson(asyncResp->res.jsonValue,
882588c3f0dSKowalski, Kamil                                  messages::propertyMissing("VLANEnable"),
883e439f0f8SKowalski, Kamil                                  pathPrefix);
884588c3f0dSKowalski, Kamil 
885588c3f0dSKowalski, Kamil       inputInvalid = true;
886588c3f0dSKowalski, Kamil     } else {
887588c3f0dSKowalski, Kamil       // Load actual data into field values if they were not provided
888588c3f0dSKowalski, Kamil       if (inputVlanEnabledState == json_util::Result::NOT_EXIST) {
889588c3f0dSKowalski, Kamil         inputVlanEnabled = true;
890588c3f0dSKowalski, Kamil       }
891588c3f0dSKowalski, Kamil 
892588c3f0dSKowalski, Kamil       if (inputVlanIdState == json_util::Result::NOT_EXIST) {
893*55c7b7a2SEd Tanous         inputVlanId = *eth_data.vlanId;
894588c3f0dSKowalski, Kamil       }
895588c3f0dSKowalski, Kamil     }
896588c3f0dSKowalski, Kamil 
897588c3f0dSKowalski, Kamil     // Do not proceed if input has not been valid
898588c3f0dSKowalski, Kamil     if (inputInvalid) {
899588c3f0dSKowalski, Kamil       return;
900588c3f0dSKowalski, Kamil     }
901588c3f0dSKowalski, Kamil 
902588c3f0dSKowalski, Kamil     // VLAN is configured on the interface
903*55c7b7a2SEd Tanous     if (inputVlanEnabled == true && inputVlanId != *eth_data.vlanId) {
904588c3f0dSKowalski, Kamil       // Change VLAN Id
905e439f0f8SKowalski, Kamil       paramsJson["VLANId"] = inputVlanId;
906e439f0f8SKowalski, Kamil       OnDemandEthernetProvider::changeVlanId(
907e439f0f8SKowalski, Kamil           ifaceId, static_cast<uint32_t>(inputVlanId),
908e439f0f8SKowalski, Kamil           [&, asyncResp, pathPrefx{std::move(pathPrefix)} ](
909e439f0f8SKowalski, Kamil               const boost::system::error_code ec) {
910588c3f0dSKowalski, Kamil             if (ec) {
911*55c7b7a2SEd Tanous               messages::addMessageToJson(asyncResp->res.jsonValue,
912e439f0f8SKowalski, Kamil                                          messages::internalError(), pathPrefix);
913588c3f0dSKowalski, Kamil             } else {
914e439f0f8SKowalski, Kamil               paramsJson["VLANEnable"] = true;
915e439f0f8SKowalski, Kamil             }
916e439f0f8SKowalski, Kamil           });
917e439f0f8SKowalski, Kamil     } else if (inputVlanEnabled == false) {
918e439f0f8SKowalski, Kamil       // Disable VLAN
919e439f0f8SKowalski, Kamil       OnDemandEthernetProvider::disableVlan(
920e439f0f8SKowalski, Kamil           ifaceId, [&, asyncResp, pathPrefx{std::move(pathPrefix)} ](
921e439f0f8SKowalski, Kamil                        const boost::system::error_code ec) {
922e439f0f8SKowalski, Kamil             if (ec) {
923*55c7b7a2SEd Tanous               messages::addMessageToJson(asyncResp->res.jsonValue,
924e439f0f8SKowalski, Kamil                                          messages::internalError(), pathPrefix);
925e439f0f8SKowalski, Kamil             } else {
926e439f0f8SKowalski, Kamil               paramsJson["VLANEnable"] = false;
927588c3f0dSKowalski, Kamil             }
928588c3f0dSKowalski, Kamil           });
929588c3f0dSKowalski, Kamil     }
930588c3f0dSKowalski, Kamil   }
931588c3f0dSKowalski, Kamil 
932e439f0f8SKowalski, Kamil  private:
933588c3f0dSKowalski, Kamil   void handleHostnamePatch(const nlohmann::json &input,
934588c3f0dSKowalski, Kamil                            const EthernetInterfaceData &eth_data,
935588c3f0dSKowalski, Kamil                            const std::shared_ptr<AsyncResp> &asyncResp) {
936588c3f0dSKowalski, Kamil     if (input.is_string()) {
937588c3f0dSKowalski, Kamil       std::string newHostname = input.get<std::string>();
938588c3f0dSKowalski, Kamil 
939588c3f0dSKowalski, Kamil       if (eth_data.hostname == nullptr || newHostname != *eth_data.hostname) {
940588c3f0dSKowalski, Kamil         // Change hostname
941*55c7b7a2SEd Tanous         ethernetProvider.setHostName(
942588c3f0dSKowalski, Kamil             newHostname,
943588c3f0dSKowalski, Kamil             [asyncResp, newHostname](const boost::system::error_code ec) {
944588c3f0dSKowalski, Kamil               if (ec) {
945*55c7b7a2SEd Tanous                 messages::addMessageToJson(asyncResp->res.jsonValue,
946588c3f0dSKowalski, Kamil                                            messages::internalError(),
947588c3f0dSKowalski, Kamil                                            "/HostName");
948588c3f0dSKowalski, Kamil               } else {
949*55c7b7a2SEd Tanous                 asyncResp->res.jsonValue["HostName"] = newHostname;
950588c3f0dSKowalski, Kamil               }
951588c3f0dSKowalski, Kamil             });
952588c3f0dSKowalski, Kamil       }
953588c3f0dSKowalski, Kamil     } else {
954588c3f0dSKowalski, Kamil       messages::addMessageToJson(
955*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
956588c3f0dSKowalski, Kamil           messages::propertyValueTypeError(input.dump(), "HostName"),
957588c3f0dSKowalski, Kamil           "/HostName");
958588c3f0dSKowalski, Kamil     }
959588c3f0dSKowalski, Kamil   }
960588c3f0dSKowalski, Kamil 
961179db1d7SKowalski, Kamil   void handleIPv4Patch(const std::string &ifaceId, const nlohmann::json &input,
962179db1d7SKowalski, Kamil                        const std::vector<IPv4AddressData> &ipv4_data,
963179db1d7SKowalski, Kamil                        const std::shared_ptr<AsyncResp> &asyncResp) {
964179db1d7SKowalski, Kamil     if (!input.is_array()) {
965179db1d7SKowalski, Kamil       messages::addMessageToJson(
966*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
967179db1d7SKowalski, Kamil           messages::propertyValueTypeError(input.dump(), "IPv4Addresses"),
968179db1d7SKowalski, Kamil           "/IPv4Addresses");
969179db1d7SKowalski, Kamil       return;
970179db1d7SKowalski, Kamil     }
971179db1d7SKowalski, Kamil 
972179db1d7SKowalski, Kamil     // According to Redfish PATCH definition, size must be at least equal
973179db1d7SKowalski, Kamil     if (input.size() < ipv4_data.size()) {
974179db1d7SKowalski, Kamil       // TODO(kkowalsk) This should be a message indicating that not enough
975179db1d7SKowalski, Kamil       // data has been provided
976*55c7b7a2SEd Tanous       messages::addMessageToJson(asyncResp->res.jsonValue,
977179db1d7SKowalski, Kamil                                  messages::internalError(), "/IPv4Addresses");
978179db1d7SKowalski, Kamil       return;
979179db1d7SKowalski, Kamil     }
980179db1d7SKowalski, Kamil 
981179db1d7SKowalski, Kamil     json_util::Result addressFieldState;
982179db1d7SKowalski, Kamil     json_util::Result subnetMaskFieldState;
983179db1d7SKowalski, Kamil     json_util::Result addressOriginFieldState;
984179db1d7SKowalski, Kamil     json_util::Result gatewayFieldState;
985179db1d7SKowalski, Kamil     const std::string *addressFieldValue;
986179db1d7SKowalski, Kamil     const std::string *subnetMaskFieldValue;
987179db1d7SKowalski, Kamil     const std::string *addressOriginFieldValue = nullptr;
988179db1d7SKowalski, Kamil     const std::string *gatewayFieldValue;
989179db1d7SKowalski, Kamil     uint8_t subnetMaskAsPrefixLength;
990179db1d7SKowalski, Kamil     std::string addressOriginInDBusFormat;
991179db1d7SKowalski, Kamil 
992179db1d7SKowalski, Kamil     bool errorDetected = false;
993179db1d7SKowalski, Kamil     for (unsigned int entryIdx = 0; entryIdx < input.size(); entryIdx++) {
994179db1d7SKowalski, Kamil       // Check that entry is not of some unexpected type
995179db1d7SKowalski, Kamil       if (!input[entryIdx].is_object() && !input[entryIdx].is_null()) {
996179db1d7SKowalski, Kamil         // Invalid object type
997179db1d7SKowalski, Kamil         messages::addMessageToJson(
998*55c7b7a2SEd Tanous             asyncResp->res.jsonValue,
999179db1d7SKowalski, Kamil             messages::propertyValueTypeError(input[entryIdx].dump(),
1000179db1d7SKowalski, Kamil                                              "IPv4Address"),
1001179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(entryIdx));
1002179db1d7SKowalski, Kamil 
1003179db1d7SKowalski, Kamil         continue;
1004179db1d7SKowalski, Kamil       }
1005179db1d7SKowalski, Kamil 
1006179db1d7SKowalski, Kamil       // Try to load fields
1007179db1d7SKowalski, Kamil       addressFieldState = json_util::getString(
1008179db1d7SKowalski, Kamil           "Address", input[entryIdx], addressFieldValue,
1009179db1d7SKowalski, Kamil           static_cast<uint8_t>(json_util::MessageSetting::TYPE_ERROR),
1010*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
1011179db1d7SKowalski, Kamil           "/IPv4Addresses/" + std::to_string(entryIdx) + "/Address");
1012179db1d7SKowalski, Kamil       subnetMaskFieldState = json_util::getString(
1013179db1d7SKowalski, Kamil           "SubnetMask", input[entryIdx], subnetMaskFieldValue,
1014179db1d7SKowalski, Kamil           static_cast<uint8_t>(json_util::MessageSetting::TYPE_ERROR),
1015*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
1016179db1d7SKowalski, Kamil           "/IPv4Addresses/" + std::to_string(entryIdx) + "/SubnetMask");
1017179db1d7SKowalski, Kamil       addressOriginFieldState = json_util::getString(
1018179db1d7SKowalski, Kamil           "AddressOrigin", input[entryIdx], addressOriginFieldValue,
1019179db1d7SKowalski, Kamil           static_cast<uint8_t>(json_util::MessageSetting::TYPE_ERROR),
1020*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
1021179db1d7SKowalski, Kamil           "/IPv4Addresses/" + std::to_string(entryIdx) + "/AddressOrigin");
1022179db1d7SKowalski, Kamil       gatewayFieldState = json_util::getString(
1023179db1d7SKowalski, Kamil           "Gateway", input[entryIdx], gatewayFieldValue,
1024179db1d7SKowalski, Kamil           static_cast<uint8_t>(json_util::MessageSetting::TYPE_ERROR),
1025*55c7b7a2SEd Tanous           asyncResp->res.jsonValue,
1026179db1d7SKowalski, Kamil           "/IPv4Addresses/" + std::to_string(entryIdx) + "/Gateway");
1027179db1d7SKowalski, Kamil 
1028179db1d7SKowalski, Kamil       if (addressFieldState == json_util::Result::WRONG_TYPE ||
1029179db1d7SKowalski, Kamil           subnetMaskFieldState == json_util::Result::WRONG_TYPE ||
1030179db1d7SKowalski, Kamil           addressOriginFieldState == json_util::Result::WRONG_TYPE ||
1031179db1d7SKowalski, Kamil           gatewayFieldState == json_util::Result::WRONG_TYPE) {
1032179db1d7SKowalski, Kamil         return;
1033179db1d7SKowalski, Kamil       }
1034179db1d7SKowalski, Kamil 
1035179db1d7SKowalski, Kamil       if (addressFieldState == json_util::Result::SUCCESS &&
1036*55c7b7a2SEd Tanous           !ethernetProvider.ipv4VerifyIpAndGetBitcount(*addressFieldValue)) {
1037179db1d7SKowalski, Kamil         errorDetected = true;
1038179db1d7SKowalski, Kamil         messages::addMessageToJson(
1039*55c7b7a2SEd Tanous             asyncResp->res.jsonValue,
1040179db1d7SKowalski, Kamil             messages::propertyValueFormatError(*addressFieldValue, "Address"),
1041179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(entryIdx) + "/Address");
1042179db1d7SKowalski, Kamil       }
1043179db1d7SKowalski, Kamil 
1044179db1d7SKowalski, Kamil       if (subnetMaskFieldState == json_util::Result::SUCCESS &&
1045*55c7b7a2SEd Tanous           !ethernetProvider.ipv4VerifyIpAndGetBitcount(
1046179db1d7SKowalski, Kamil               *subnetMaskFieldValue, &subnetMaskAsPrefixLength)) {
1047179db1d7SKowalski, Kamil         errorDetected = true;
1048179db1d7SKowalski, Kamil         messages::addMessageToJson(
1049*55c7b7a2SEd Tanous             asyncResp->res.jsonValue,
1050179db1d7SKowalski, Kamil             messages::propertyValueFormatError(*subnetMaskFieldValue,
1051179db1d7SKowalski, Kamil                                                "SubnetMask"),
1052179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(entryIdx) + "/SubnetMask");
1053179db1d7SKowalski, Kamil       }
1054179db1d7SKowalski, Kamil 
1055*55c7b7a2SEd Tanous       // get Address origin in proper format
1056179db1d7SKowalski, Kamil       addressOriginInDBusFormat =
1057*55c7b7a2SEd Tanous           ethernetProvider.translateAddressOriginBetweenDBusAndRedfish(
1058179db1d7SKowalski, Kamil               addressOriginFieldValue, true, false);
1059179db1d7SKowalski, Kamil 
1060179db1d7SKowalski, Kamil       if (addressOriginFieldState == json_util::Result::SUCCESS &&
1061179db1d7SKowalski, Kamil           addressOriginInDBusFormat.empty()) {
1062179db1d7SKowalski, Kamil         errorDetected = true;
1063179db1d7SKowalski, Kamil         messages::addMessageToJson(
1064*55c7b7a2SEd Tanous             asyncResp->res.jsonValue,
1065179db1d7SKowalski, Kamil             messages::propertyValueNotInList(*addressOriginFieldValue,
1066179db1d7SKowalski, Kamil                                              "AddressOrigin"),
1067179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(entryIdx) + "/AddressOrigin");
1068179db1d7SKowalski, Kamil       }
1069179db1d7SKowalski, Kamil 
1070179db1d7SKowalski, Kamil       if (gatewayFieldState == json_util::Result::SUCCESS &&
1071*55c7b7a2SEd Tanous           !ethernetProvider.ipv4VerifyIpAndGetBitcount(*gatewayFieldValue)) {
1072179db1d7SKowalski, Kamil         errorDetected = true;
1073179db1d7SKowalski, Kamil         messages::addMessageToJson(
1074*55c7b7a2SEd Tanous             asyncResp->res.jsonValue,
1075179db1d7SKowalski, Kamil             messages::propertyValueFormatError(*gatewayFieldValue, "Gateway"),
1076179db1d7SKowalski, Kamil             "/IPv4Addresses/" + std::to_string(entryIdx) + "/Gateway");
1077179db1d7SKowalski, Kamil       }
1078179db1d7SKowalski, Kamil 
1079179db1d7SKowalski, Kamil       // If any error occured do not proceed with current entry, but do not
1080179db1d7SKowalski, Kamil       // end loop
1081179db1d7SKowalski, Kamil       if (errorDetected) {
1082179db1d7SKowalski, Kamil         errorDetected = false;
1083179db1d7SKowalski, Kamil         continue;
1084179db1d7SKowalski, Kamil       }
1085179db1d7SKowalski, Kamil 
1086179db1d7SKowalski, Kamil       if (entryIdx >= ipv4_data.size()) {
1087*55c7b7a2SEd Tanous         asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = input[entryIdx];
1088179db1d7SKowalski, Kamil 
1089179db1d7SKowalski, Kamil         // Verify that all field were provided
1090179db1d7SKowalski, Kamil         if (addressFieldState == json_util::Result::NOT_EXIST) {
1091179db1d7SKowalski, Kamil           errorDetected = true;
1092179db1d7SKowalski, Kamil           messages::addMessageToJson(
1093*55c7b7a2SEd Tanous               asyncResp->res.jsonValue, messages::propertyMissing("Address"),
1094179db1d7SKowalski, Kamil               "/IPv4Addresses/" + std::to_string(entryIdx) + "/Address");
1095179db1d7SKowalski, Kamil         }
1096179db1d7SKowalski, Kamil 
1097179db1d7SKowalski, Kamil         if (subnetMaskFieldState == json_util::Result::NOT_EXIST) {
1098179db1d7SKowalski, Kamil           errorDetected = true;
1099179db1d7SKowalski, Kamil           messages::addMessageToJson(
1100*55c7b7a2SEd Tanous               asyncResp->res.jsonValue, messages::propertyMissing("SubnetMask"),
1101179db1d7SKowalski, Kamil               "/IPv4Addresses/" + std::to_string(entryIdx) + "/SubnetMask");
1102179db1d7SKowalski, Kamil         }
1103179db1d7SKowalski, Kamil 
1104179db1d7SKowalski, Kamil         if (addressOriginFieldState == json_util::Result::NOT_EXIST) {
1105179db1d7SKowalski, Kamil           errorDetected = true;
1106179db1d7SKowalski, Kamil           messages::addMessageToJson(
1107*55c7b7a2SEd Tanous               asyncResp->res.jsonValue,
1108179db1d7SKowalski, Kamil               messages::propertyMissing("AddressOrigin"),
1109179db1d7SKowalski, Kamil               "/IPv4Addresses/" + std::to_string(entryIdx) + "/AddressOrigin");
1110179db1d7SKowalski, Kamil         }
1111179db1d7SKowalski, Kamil 
1112179db1d7SKowalski, Kamil         if (gatewayFieldState == json_util::Result::NOT_EXIST) {
1113179db1d7SKowalski, Kamil           errorDetected = true;
1114179db1d7SKowalski, Kamil           messages::addMessageToJson(
1115*55c7b7a2SEd Tanous               asyncResp->res.jsonValue, messages::propertyMissing("Gateway"),
1116179db1d7SKowalski, Kamil               "/IPv4Addresses/" + std::to_string(entryIdx) + "/Gateway");
1117179db1d7SKowalski, Kamil         }
1118179db1d7SKowalski, Kamil 
1119179db1d7SKowalski, Kamil         // If any error occured do not proceed with current entry, but do not
1120179db1d7SKowalski, Kamil         // end loop
1121179db1d7SKowalski, Kamil         if (errorDetected) {
1122179db1d7SKowalski, Kamil           errorDetected = false;
1123179db1d7SKowalski, Kamil           continue;
1124179db1d7SKowalski, Kamil         }
1125179db1d7SKowalski, Kamil 
1126179db1d7SKowalski, Kamil         // Create IPv4 with provided data
1127*55c7b7a2SEd Tanous         ethernetProvider.createIPv4(ifaceId, entryIdx, subnetMaskAsPrefixLength,
1128*55c7b7a2SEd Tanous                                     *gatewayFieldValue, *addressFieldValue,
1129*55c7b7a2SEd Tanous                                     asyncResp);
1130179db1d7SKowalski, Kamil       } else {
1131179db1d7SKowalski, Kamil         // Existing object that should be modified/deleted/remain unchanged
1132179db1d7SKowalski, Kamil         if (input[entryIdx].is_null()) {
1133179db1d7SKowalski, Kamil           // Object should be deleted
1134*55c7b7a2SEd Tanous           ethernetProvider.deleteIPv4(ifaceId, ipv4_data[entryIdx].id, entryIdx,
1135*55c7b7a2SEd Tanous                                       asyncResp);
1136179db1d7SKowalski, Kamil         } else if (input[entryIdx].is_object()) {
1137179db1d7SKowalski, Kamil           if (input[entryIdx].size() == 0) {
1138179db1d7SKowalski, Kamil             // Object shall remain unchanged
1139179db1d7SKowalski, Kamil             continue;
1140179db1d7SKowalski, Kamil           }
1141179db1d7SKowalski, Kamil 
1142179db1d7SKowalski, Kamil           // Apply changes
1143179db1d7SKowalski, Kamil           if (addressFieldState == json_util::Result::SUCCESS &&
1144179db1d7SKowalski, Kamil               ipv4_data[entryIdx].address != nullptr &&
1145179db1d7SKowalski, Kamil               *ipv4_data[entryIdx].address != *addressFieldValue) {
1146*55c7b7a2SEd Tanous             ethernetProvider.changeIPv4AddressProperty(
1147179db1d7SKowalski, Kamil                 ifaceId, entryIdx, ipv4_data[entryIdx].id, "Address",
1148179db1d7SKowalski, Kamil                 *addressFieldValue, asyncResp);
1149179db1d7SKowalski, Kamil           }
1150179db1d7SKowalski, Kamil 
1151179db1d7SKowalski, Kamil           if (subnetMaskFieldState == json_util::Result::SUCCESS &&
1152179db1d7SKowalski, Kamil               ipv4_data[entryIdx].netmask != *subnetMaskFieldValue) {
1153*55c7b7a2SEd Tanous             ethernetProvider.changeIPv4SubnetMaskProperty(
1154179db1d7SKowalski, Kamil                 ifaceId, entryIdx, ipv4_data[entryIdx].id,
1155179db1d7SKowalski, Kamil                 *subnetMaskFieldValue, subnetMaskAsPrefixLength, asyncResp);
1156179db1d7SKowalski, Kamil           }
1157179db1d7SKowalski, Kamil 
1158179db1d7SKowalski, Kamil           if (addressOriginFieldState == json_util::Result::SUCCESS &&
1159179db1d7SKowalski, Kamil               ipv4_data[entryIdx].origin != *addressFieldValue) {
1160*55c7b7a2SEd Tanous             ethernetProvider.changeIPv4Origin(
1161179db1d7SKowalski, Kamil                 ifaceId, entryIdx, ipv4_data[entryIdx].id,
1162179db1d7SKowalski, Kamil                 *addressOriginFieldValue, addressOriginInDBusFormat, asyncResp);
1163179db1d7SKowalski, Kamil           }
1164179db1d7SKowalski, Kamil 
1165179db1d7SKowalski, Kamil           if (gatewayFieldState == json_util::Result::SUCCESS &&
1166179db1d7SKowalski, Kamil               ipv4_data[entryIdx].gateway != nullptr &&
1167179db1d7SKowalski, Kamil               *ipv4_data[entryIdx].gateway != *gatewayFieldValue) {
1168*55c7b7a2SEd Tanous             ethernetProvider.changeIPv4AddressProperty(
1169179db1d7SKowalski, Kamil                 ifaceId, entryIdx, ipv4_data[entryIdx].id, "Gateway",
1170179db1d7SKowalski, Kamil                 *gatewayFieldValue, asyncResp);
1171179db1d7SKowalski, Kamil           }
1172179db1d7SKowalski, Kamil         }
1173179db1d7SKowalski, Kamil       }
1174179db1d7SKowalski, Kamil     }
1175179db1d7SKowalski, Kamil   }
1176179db1d7SKowalski, Kamil 
1177588c3f0dSKowalski, Kamil   nlohmann::json parseInterfaceData(
1178*55c7b7a2SEd Tanous       const std::string &ifaceId, const EthernetInterfaceData &eth_data,
1179588c3f0dSKowalski, Kamil       const std::vector<IPv4AddressData> &ipv4_data) {
1180588c3f0dSKowalski, Kamil     // Copy JSON object to avoid race condition
1181*55c7b7a2SEd Tanous     nlohmann::json jsonResponse(Node::json);
1182588c3f0dSKowalski, Kamil 
1183588c3f0dSKowalski, Kamil     // Fill out obvious data...
1184*55c7b7a2SEd Tanous     jsonResponse["Id"] = ifaceId;
1185*55c7b7a2SEd Tanous     jsonResponse["@odata.id"] =
1186*55c7b7a2SEd Tanous         "/redfish/v1/Managers/openbmc/EthernetInterfaces/" + ifaceId;
1187588c3f0dSKowalski, Kamil 
1188588c3f0dSKowalski, Kamil     // ... then the one from DBus, regarding eth iface...
1189*55c7b7a2SEd Tanous     if (eth_data.speed != nullptr) jsonResponse["SpeedMbps"] = *eth_data.speed;
1190588c3f0dSKowalski, Kamil 
1191*55c7b7a2SEd Tanous     if (eth_data.macAddress != nullptr)
1192*55c7b7a2SEd Tanous       jsonResponse["MACAddress"] = *eth_data.macAddress;
1193588c3f0dSKowalski, Kamil 
1194588c3f0dSKowalski, Kamil     if (eth_data.hostname != nullptr)
1195*55c7b7a2SEd Tanous       jsonResponse["HostName"] = *eth_data.hostname;
1196588c3f0dSKowalski, Kamil 
1197*55c7b7a2SEd Tanous     if (eth_data.vlanId != nullptr) {
1198*55c7b7a2SEd Tanous       nlohmann::json &vlanObj = jsonResponse["VLAN"];
1199588c3f0dSKowalski, Kamil       vlanObj["VLANEnable"] = true;
1200*55c7b7a2SEd Tanous       vlanObj["VLANId"] = *eth_data.vlanId;
1201eb547730SKowalski, Kamil     } else {
1202*55c7b7a2SEd Tanous       nlohmann::json &vlanObj = jsonResponse["VLANs"];
1203eb547730SKowalski, Kamil       vlanObj["@odata.id"] =
1204*55c7b7a2SEd Tanous           "/redfish/v1/Managers/openbmc/EthernetInterfaces/" + ifaceId +
1205eb547730SKowalski, Kamil           "/VLANs";
1206588c3f0dSKowalski, Kamil     }
1207588c3f0dSKowalski, Kamil 
1208588c3f0dSKowalski, Kamil     // ... at last, check if there are IPv4 data and prepare appropriate
1209588c3f0dSKowalski, Kamil     // collection
1210588c3f0dSKowalski, Kamil     if (ipv4_data.size() > 0) {
1211*55c7b7a2SEd Tanous       nlohmann::json ipv4Array = nlohmann::json::array();
1212*55c7b7a2SEd Tanous       for (auto &ipv4Config : ipv4_data) {
1213*55c7b7a2SEd Tanous         nlohmann::json jsonIpv4;
1214*55c7b7a2SEd Tanous         if (ipv4Config.address != nullptr) {
1215*55c7b7a2SEd Tanous           jsonIpv4["Address"] = *ipv4Config.address;
1216*55c7b7a2SEd Tanous           if (ipv4Config.gateway != nullptr)
1217*55c7b7a2SEd Tanous             jsonIpv4["Gateway"] = *ipv4Config.gateway;
1218588c3f0dSKowalski, Kamil 
1219*55c7b7a2SEd Tanous           jsonIpv4["AddressOrigin"] = ipv4Config.origin;
1220*55c7b7a2SEd Tanous           jsonIpv4["SubnetMask"] = ipv4Config.netmask;
1221588c3f0dSKowalski, Kamil 
1222*55c7b7a2SEd Tanous           ipv4Array.push_back(std::move(jsonIpv4));
1223588c3f0dSKowalski, Kamil         }
1224588c3f0dSKowalski, Kamil       }
1225*55c7b7a2SEd Tanous       jsonResponse["IPv4Addresses"] = std::move(ipv4Array);
1226588c3f0dSKowalski, Kamil     }
1227588c3f0dSKowalski, Kamil 
1228*55c7b7a2SEd Tanous     return jsonResponse;
1229588c3f0dSKowalski, Kamil   }
1230588c3f0dSKowalski, Kamil 
12319391bb9cSRapkiewicz, Pawel   /**
12329391bb9cSRapkiewicz, Pawel    * Functions triggers appropriate requests on DBus
12339391bb9cSRapkiewicz, Pawel    */
1234*55c7b7a2SEd Tanous   void doGet(crow::Response &res, const crow::Request &req,
12359391bb9cSRapkiewicz, Pawel              const std::vector<std::string> &params) override {
12369391bb9cSRapkiewicz, Pawel     // TODO(Pawel) this shall be parametrized call (two params) to get
12379391bb9cSRapkiewicz, Pawel     // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
12389391bb9cSRapkiewicz, Pawel     // Check if there is required param, truly entering this shall be
12399391bb9cSRapkiewicz, Pawel     // impossible.
12409391bb9cSRapkiewicz, Pawel     if (params.size() != 1) {
1241e0d918bcSEd Tanous       res.result(boost::beast::http::status::internal_server_error);
12429391bb9cSRapkiewicz, Pawel       res.end();
12439391bb9cSRapkiewicz, Pawel       return;
12449391bb9cSRapkiewicz, Pawel     }
12459391bb9cSRapkiewicz, Pawel 
1246*55c7b7a2SEd Tanous     const std::string &ifaceId = params[0];
12479391bb9cSRapkiewicz, Pawel 
1248*55c7b7a2SEd Tanous     // get single eth interface data, and call the below callback for JSON
12499391bb9cSRapkiewicz, Pawel     // preparation
1250*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceData(
1251*55c7b7a2SEd Tanous         ifaceId,
1252*55c7b7a2SEd Tanous         [&, ifaceId](const bool &success, const EthernetInterfaceData &eth_data,
12539391bb9cSRapkiewicz, Pawel                      const std::vector<IPv4AddressData> &ipv4_data) {
12549391bb9cSRapkiewicz, Pawel           if (success) {
1255*55c7b7a2SEd Tanous             res.jsonValue = parseInterfaceData(ifaceId, eth_data, ipv4_data);
12569391bb9cSRapkiewicz, Pawel           } else {
12579391bb9cSRapkiewicz, Pawel             // ... otherwise return error
12589391bb9cSRapkiewicz, Pawel             // TODO(Pawel)consider distinguish between non existing object, and
12599391bb9cSRapkiewicz, Pawel             // other errors
1260e439f0f8SKowalski, Kamil             messages::addMessageToErrorJson(
1261*55c7b7a2SEd Tanous                 res.jsonValue,
1262*55c7b7a2SEd Tanous                 messages::resourceNotFound("EthernetInterface", ifaceId));
1263e0d918bcSEd Tanous             res.result(boost::beast::http::status::not_found);
12649391bb9cSRapkiewicz, Pawel           }
12659391bb9cSRapkiewicz, Pawel           res.end();
12669391bb9cSRapkiewicz, Pawel         });
12679391bb9cSRapkiewicz, Pawel   }
12689391bb9cSRapkiewicz, Pawel 
1269*55c7b7a2SEd Tanous   void doPatch(crow::Response &res, const crow::Request &req,
1270588c3f0dSKowalski, Kamil                const std::vector<std::string> &params) override {
1271588c3f0dSKowalski, Kamil     // TODO(Pawel) this shall be parametrized call (two params) to get
1272588c3f0dSKowalski, Kamil     // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1273588c3f0dSKowalski, Kamil     // Check if there is required param, truly entering this shall be
1274588c3f0dSKowalski, Kamil     // impossible.
1275588c3f0dSKowalski, Kamil     if (params.size() != 1) {
1276588c3f0dSKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1277588c3f0dSKowalski, Kamil       res.end();
1278588c3f0dSKowalski, Kamil       return;
1279588c3f0dSKowalski, Kamil     }
1280588c3f0dSKowalski, Kamil 
1281*55c7b7a2SEd Tanous     const std::string &ifaceId = params[0];
1282588c3f0dSKowalski, Kamil 
1283179db1d7SKowalski, Kamil     nlohmann::json patchReq;
1284588c3f0dSKowalski, Kamil 
1285179db1d7SKowalski, Kamil     if (!json_util::processJsonFromRequest(res, req, patchReq)) {
1286588c3f0dSKowalski, Kamil       return;
1287588c3f0dSKowalski, Kamil     }
1288588c3f0dSKowalski, Kamil 
1289*55c7b7a2SEd Tanous     // get single eth interface data, and call the below callback for JSON
1290588c3f0dSKowalski, Kamil     // preparation
1291*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceData(
1292*55c7b7a2SEd Tanous         ifaceId, [&, ifaceId, patchReq = std::move(patchReq) ](
1293588c3f0dSKowalski, Kamil                      const bool &success, const EthernetInterfaceData &eth_data,
1294588c3f0dSKowalski, Kamil                      const std::vector<IPv4AddressData> &ipv4_data) {
1295588c3f0dSKowalski, Kamil           if (!success) {
1296588c3f0dSKowalski, Kamil             // ... otherwise return error
1297588c3f0dSKowalski, Kamil             // TODO(Pawel)consider distinguish between non existing object, and
1298588c3f0dSKowalski, Kamil             // other errors
1299927a505aSKowalski, Kamil             messages::addMessageToErrorJson(
1300*55c7b7a2SEd Tanous                 res.jsonValue,
1301*55c7b7a2SEd Tanous                 messages::resourceNotFound("VLAN Network Interface", ifaceId));
1302588c3f0dSKowalski, Kamil             res.result(boost::beast::http::status::not_found);
1303588c3f0dSKowalski, Kamil             res.end();
1304588c3f0dSKowalski, Kamil 
1305588c3f0dSKowalski, Kamil             return;
1306588c3f0dSKowalski, Kamil           }
1307588c3f0dSKowalski, Kamil 
1308*55c7b7a2SEd Tanous           res.jsonValue = parseInterfaceData(ifaceId, eth_data, ipv4_data);
1309588c3f0dSKowalski, Kamil 
1310588c3f0dSKowalski, Kamil           std::shared_ptr<AsyncResp> asyncResp =
1311588c3f0dSKowalski, Kamil               std::make_shared<AsyncResp>(res);
1312588c3f0dSKowalski, Kamil 
1313588c3f0dSKowalski, Kamil           for (auto propertyIt = patchReq.begin(); propertyIt != patchReq.end();
1314588c3f0dSKowalski, Kamil                ++propertyIt) {
1315588c3f0dSKowalski, Kamil             if (propertyIt.key() == "VLAN") {
1316*55c7b7a2SEd Tanous               handleVlanPatch(ifaceId, propertyIt.value(), eth_data, "/VLAN",
1317588c3f0dSKowalski, Kamil                               asyncResp);
1318588c3f0dSKowalski, Kamil             } else if (propertyIt.key() == "HostName") {
1319588c3f0dSKowalski, Kamil               handleHostnamePatch(propertyIt.value(), eth_data, asyncResp);
1320179db1d7SKowalski, Kamil             } else if (propertyIt.key() == "IPv4Addresses") {
1321*55c7b7a2SEd Tanous               handleIPv4Patch(ifaceId, propertyIt.value(), ipv4_data,
1322179db1d7SKowalski, Kamil                               asyncResp);
1323179db1d7SKowalski, Kamil             } else if (propertyIt.key() == "IPv6Addresses") {
1324179db1d7SKowalski, Kamil               // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1325179db1d7SKowalski, Kamil               messages::addMessageToJsonRoot(
1326*55c7b7a2SEd Tanous                   res.jsonValue,
1327179db1d7SKowalski, Kamil                   messages::propertyNotWritable(propertyIt.key()));
1328588c3f0dSKowalski, Kamil             } else {
1329*55c7b7a2SEd Tanous               auto fieldInJsonIt = res.jsonValue.find(propertyIt.key());
1330588c3f0dSKowalski, Kamil 
1331*55c7b7a2SEd Tanous               if (fieldInJsonIt == res.jsonValue.end()) {
1332588c3f0dSKowalski, Kamil                 // Field not in scope of defined fields
1333588c3f0dSKowalski, Kamil                 messages::addMessageToJsonRoot(
1334*55c7b7a2SEd Tanous                     res.jsonValue, messages::propertyUnknown(propertyIt.key()));
1335588c3f0dSKowalski, Kamil               } else if (*fieldInJsonIt != *propertyIt) {
1336588c3f0dSKowalski, Kamil                 // User attempted to modify non-writable field
1337588c3f0dSKowalski, Kamil                 messages::addMessageToJsonRoot(
1338*55c7b7a2SEd Tanous                     res.jsonValue,
1339588c3f0dSKowalski, Kamil                     messages::propertyNotWritable(propertyIt.key()));
1340588c3f0dSKowalski, Kamil               }
1341588c3f0dSKowalski, Kamil             }
1342588c3f0dSKowalski, Kamil           }
1343588c3f0dSKowalski, Kamil         });
1344588c3f0dSKowalski, Kamil   }
1345588c3f0dSKowalski, Kamil 
13469391bb9cSRapkiewicz, Pawel   // Ethernet Provider object
13479391bb9cSRapkiewicz, Pawel   // TODO(Pawel) consider move it to singleton
1348*55c7b7a2SEd Tanous   OnDemandEthernetProvider ethernetProvider;
13499391bb9cSRapkiewicz, Pawel };
13509391bb9cSRapkiewicz, Pawel 
1351e439f0f8SKowalski, Kamil class VlanNetworkInterfaceCollection;
1352e439f0f8SKowalski, Kamil 
1353e439f0f8SKowalski, Kamil /**
1354e439f0f8SKowalski, Kamil  * VlanNetworkInterface derived class for delivering VLANNetworkInterface Schema
1355e439f0f8SKowalski, Kamil  */
1356e439f0f8SKowalski, Kamil class VlanNetworkInterface : public Node {
1357e439f0f8SKowalski, Kamil  public:
1358e439f0f8SKowalski, Kamil   /*
1359e439f0f8SKowalski, Kamil    * Default Constructor
1360e439f0f8SKowalski, Kamil    */
1361e439f0f8SKowalski, Kamil   template <typename CrowApp>
1362e439f0f8SKowalski, Kamil   // TODO(Pawel) Remove line from below, where we assume that there is only one
1363e439f0f8SKowalski, Kamil   // manager called openbmc This shall be generic, but requires to update
1364e439f0f8SKowalski, Kamil   // GetSubroutes method
1365e439f0f8SKowalski, Kamil   VlanNetworkInterface(CrowApp &app)
1366eb547730SKowalski, Kamil       : Node(
1367eb547730SKowalski, Kamil             app,
1368eb547730SKowalski, Kamil             "/redfish/v1/Managers/openbmc/EthernetInterfaces/<str>/VLANs/<str>",
1369e439f0f8SKowalski, Kamil             std::string(), std::string()) {
1370e439f0f8SKowalski, Kamil     Node::json["@odata.type"] =
1371e439f0f8SKowalski, Kamil         "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1372e439f0f8SKowalski, Kamil     Node::json["@odata.context"] =
1373e439f0f8SKowalski, Kamil         "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1374e439f0f8SKowalski, Kamil     Node::json["Name"] = "VLAN Network Interface";
1375e439f0f8SKowalski, Kamil 
1376e439f0f8SKowalski, Kamil     entityPrivileges = {
1377e439f0f8SKowalski, Kamil         {boost::beast::http::verb::get, {{"Login"}}},
1378e439f0f8SKowalski, Kamil         {boost::beast::http::verb::head, {{"Login"}}},
1379e439f0f8SKowalski, Kamil         {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1380e439f0f8SKowalski, Kamil         {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1381e439f0f8SKowalski, Kamil         {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1382e439f0f8SKowalski, Kamil         {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1383e439f0f8SKowalski, Kamil   }
1384e439f0f8SKowalski, Kamil 
1385e439f0f8SKowalski, Kamil  private:
1386e439f0f8SKowalski, Kamil   nlohmann::json parseInterfaceData(
1387*55c7b7a2SEd Tanous       const std::string &parent_ifaceId, const std::string &ifaceId,
1388e439f0f8SKowalski, Kamil       const EthernetInterfaceData &eth_data,
1389e439f0f8SKowalski, Kamil       const std::vector<IPv4AddressData> &ipv4_data) {
1390e439f0f8SKowalski, Kamil     // Copy JSON object to avoid race condition
1391*55c7b7a2SEd Tanous     nlohmann::json jsonResponse(Node::json);
1392e439f0f8SKowalski, Kamil 
1393*55c7b7a2SEd Tanous     if (eth_data.vlanId == nullptr) {
1394eb547730SKowalski, Kamil       // Interface not a VLAN - abort
1395*55c7b7a2SEd Tanous       messages::addMessageToErrorJson(jsonResponse, messages::internalError());
1396*55c7b7a2SEd Tanous       return jsonResponse;
1397eb547730SKowalski, Kamil     }
1398eb547730SKowalski, Kamil 
1399e439f0f8SKowalski, Kamil     // Fill out obvious data...
1400*55c7b7a2SEd Tanous     jsonResponse["Id"] = ifaceId;
1401*55c7b7a2SEd Tanous     jsonResponse["@odata.id"] =
1402*55c7b7a2SEd Tanous         "/redfish/v1/Managers/openbmc/EthernetInterfaces/" + parent_ifaceId +
1403*55c7b7a2SEd Tanous         "/VLANs/" + ifaceId;
1404e439f0f8SKowalski, Kamil 
1405*55c7b7a2SEd Tanous     jsonResponse["VLANEnable"] = true;
1406*55c7b7a2SEd Tanous     jsonResponse["VLANId"] = *eth_data.vlanId;
1407e439f0f8SKowalski, Kamil 
1408*55c7b7a2SEd Tanous     return jsonResponse;
1409e439f0f8SKowalski, Kamil   }
1410e439f0f8SKowalski, Kamil 
1411*55c7b7a2SEd Tanous   bool verifyNames(crow::Response &res, const std::string &parent,
1412927a505aSKowalski, Kamil                    const std::string &iface) {
1413927a505aSKowalski, Kamil     if (!boost::starts_with(iface, parent + "_")) {
1414927a505aSKowalski, Kamil       messages::addMessageToErrorJson(
1415*55c7b7a2SEd Tanous           res.jsonValue,
1416927a505aSKowalski, Kamil           messages::resourceNotFound("VLAN Network Interface", iface));
1417927a505aSKowalski, Kamil       res.result(boost::beast::http::status::not_found);
1418927a505aSKowalski, Kamil       res.end();
1419927a505aSKowalski, Kamil 
1420927a505aSKowalski, Kamil       return false;
1421927a505aSKowalski, Kamil     } else {
1422927a505aSKowalski, Kamil       return true;
1423927a505aSKowalski, Kamil     }
1424927a505aSKowalski, Kamil   }
1425927a505aSKowalski, Kamil 
1426e439f0f8SKowalski, Kamil   /**
1427e439f0f8SKowalski, Kamil    * Functions triggers appropriate requests on DBus
1428e439f0f8SKowalski, Kamil    */
1429*55c7b7a2SEd Tanous   void doGet(crow::Response &res, const crow::Request &req,
1430e439f0f8SKowalski, Kamil              const std::vector<std::string> &params) override {
1431e439f0f8SKowalski, Kamil     // TODO(Pawel) this shall be parametrized call (two params) to get
1432e439f0f8SKowalski, Kamil     // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1433e439f0f8SKowalski, Kamil     // Check if there is required param, truly entering this shall be
1434e439f0f8SKowalski, Kamil     // impossible.
1435e439f0f8SKowalski, Kamil     if (params.size() != 2) {
1436e439f0f8SKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1437e439f0f8SKowalski, Kamil       res.end();
1438e439f0f8SKowalski, Kamil       return;
1439e439f0f8SKowalski, Kamil     }
1440e439f0f8SKowalski, Kamil 
1441*55c7b7a2SEd Tanous     const std::string &parentIfaceId = params[0];
1442*55c7b7a2SEd Tanous     const std::string &ifaceId = params[1];
1443e439f0f8SKowalski, Kamil 
1444e439f0f8SKowalski, Kamil     // Get single eth interface data, and call the below callback for JSON
1445e439f0f8SKowalski, Kamil     // preparation
1446*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceData(
1447*55c7b7a2SEd Tanous         ifaceId,
1448*55c7b7a2SEd Tanous         [&, parentIfaceId, ifaceId](
1449e439f0f8SKowalski, Kamil             const bool &success, const EthernetInterfaceData &eth_data,
1450e439f0f8SKowalski, Kamil             const std::vector<IPv4AddressData> &ipv4_data) {
1451*55c7b7a2SEd Tanous           if (success && eth_data.vlanId != nullptr) {
1452*55c7b7a2SEd Tanous             res.jsonValue = parseInterfaceData(parentIfaceId, ifaceId,
1453e439f0f8SKowalski, Kamil                                                 eth_data, ipv4_data);
1454e439f0f8SKowalski, Kamil           } else {
1455e439f0f8SKowalski, Kamil             // ... otherwise return error
1456eb547730SKowalski, Kamil             // TODO(Pawel)consider distinguish between non existing object, and
1457e439f0f8SKowalski, Kamil             // other errors
1458927a505aSKowalski, Kamil             messages::addMessageToErrorJson(
1459*55c7b7a2SEd Tanous                 res.jsonValue,
1460*55c7b7a2SEd Tanous                 messages::resourceNotFound("VLAN Network Interface", ifaceId));
1461e439f0f8SKowalski, Kamil             res.result(boost::beast::http::status::not_found);
1462e439f0f8SKowalski, Kamil           }
1463e439f0f8SKowalski, Kamil           res.end();
1464e439f0f8SKowalski, Kamil         });
1465e439f0f8SKowalski, Kamil   }
1466e439f0f8SKowalski, Kamil 
1467*55c7b7a2SEd Tanous   void doPatch(crow::Response &res, const crow::Request &req,
1468e439f0f8SKowalski, Kamil                const std::vector<std::string> &params) override {
1469e439f0f8SKowalski, Kamil     if (params.size() != 2) {
1470e439f0f8SKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1471e439f0f8SKowalski, Kamil       res.end();
1472e439f0f8SKowalski, Kamil       return;
1473e439f0f8SKowalski, Kamil     }
1474e439f0f8SKowalski, Kamil 
1475*55c7b7a2SEd Tanous     const std::string &parent_ifaceId = params[0];
1476*55c7b7a2SEd Tanous     const std::string &ifaceId = params[1];
1477927a505aSKowalski, Kamil 
1478*55c7b7a2SEd Tanous     if (!verifyNames(res, parent_ifaceId, ifaceId)) {
1479927a505aSKowalski, Kamil       return;
1480927a505aSKowalski, Kamil     }
1481927a505aSKowalski, Kamil 
1482927a505aSKowalski, Kamil     nlohmann::json patchReq;
1483927a505aSKowalski, Kamil 
1484927a505aSKowalski, Kamil     if (!json_util::processJsonFromRequest(res, req, patchReq)) {
1485927a505aSKowalski, Kamil       return;
1486927a505aSKowalski, Kamil     }
1487927a505aSKowalski, Kamil 
1488927a505aSKowalski, Kamil     // Get single eth interface data, and call the below callback for JSON
1489927a505aSKowalski, Kamil     // preparation
1490*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceData(
1491*55c7b7a2SEd Tanous         ifaceId,
1492*55c7b7a2SEd Tanous         [&, parent_ifaceId, ifaceId, patchReq = std::move(patchReq) ](
1493927a505aSKowalski, Kamil             const bool &success, const EthernetInterfaceData &eth_data,
1494927a505aSKowalski, Kamil             const std::vector<IPv4AddressData> &ipv4_data) {
1495927a505aSKowalski, Kamil           if (!success) {
1496927a505aSKowalski, Kamil             // ... otherwise return error
1497927a505aSKowalski, Kamil             // TODO(Pawel)consider distinguish between non existing object,
1498927a505aSKowalski, Kamil             // and
1499927a505aSKowalski, Kamil             // other errors
1500927a505aSKowalski, Kamil             messages::addMessageToErrorJson(
1501*55c7b7a2SEd Tanous                 res.jsonValue,
1502*55c7b7a2SEd Tanous                 messages::resourceNotFound("VLAN Network Interface", ifaceId));
1503927a505aSKowalski, Kamil             res.result(boost::beast::http::status::not_found);
1504e439f0f8SKowalski, Kamil             res.end();
1505927a505aSKowalski, Kamil 
1506927a505aSKowalski, Kamil             return;
1507927a505aSKowalski, Kamil           }
1508927a505aSKowalski, Kamil 
1509*55c7b7a2SEd Tanous           res.jsonValue = parseInterfaceData(parent_ifaceId, ifaceId,
1510927a505aSKowalski, Kamil                                               eth_data, ipv4_data);
1511927a505aSKowalski, Kamil 
1512927a505aSKowalski, Kamil           std::shared_ptr<AsyncResp> asyncResp =
1513927a505aSKowalski, Kamil               std::make_shared<AsyncResp>(res);
1514927a505aSKowalski, Kamil 
1515927a505aSKowalski, Kamil           for (auto propertyIt = patchReq.begin(); propertyIt != patchReq.end();
1516927a505aSKowalski, Kamil                ++propertyIt) {
1517927a505aSKowalski, Kamil             if (propertyIt.key() != "VLANEnable" &&
1518927a505aSKowalski, Kamil                 propertyIt.key() != "VLANId") {
1519*55c7b7a2SEd Tanous               auto fieldInJsonIt = res.jsonValue.find(propertyIt.key());
1520927a505aSKowalski, Kamil 
1521*55c7b7a2SEd Tanous               if (fieldInJsonIt == res.jsonValue.end()) {
1522927a505aSKowalski, Kamil                 // Field not in scope of defined fields
1523927a505aSKowalski, Kamil                 messages::addMessageToJsonRoot(
1524*55c7b7a2SEd Tanous                     res.jsonValue,
1525927a505aSKowalski, Kamil                     messages::propertyUnknown(propertyIt.key()));
1526927a505aSKowalski, Kamil               } else if (*fieldInJsonIt != *propertyIt) {
1527927a505aSKowalski, Kamil                 // User attempted to modify non-writable field
1528927a505aSKowalski, Kamil                 messages::addMessageToJsonRoot(
1529*55c7b7a2SEd Tanous                     res.jsonValue,
1530927a505aSKowalski, Kamil                     messages::propertyNotWritable(propertyIt.key()));
1531927a505aSKowalski, Kamil               }
1532927a505aSKowalski, Kamil             }
1533927a505aSKowalski, Kamil           }
1534927a505aSKowalski, Kamil 
1535*55c7b7a2SEd Tanous           EthernetInterface::handleVlanPatch(ifaceId, patchReq, eth_data, "/",
1536927a505aSKowalski, Kamil                                              asyncResp);
1537927a505aSKowalski, Kamil         });
1538e439f0f8SKowalski, Kamil   }
1539e439f0f8SKowalski, Kamil 
1540*55c7b7a2SEd Tanous   void doDelete(crow::Response &res, const crow::Request &req,
1541e439f0f8SKowalski, Kamil                 const std::vector<std::string> &params) override {
1542e439f0f8SKowalski, Kamil     if (params.size() != 2) {
1543e439f0f8SKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1544e439f0f8SKowalski, Kamil       res.end();
1545e439f0f8SKowalski, Kamil       return;
1546e439f0f8SKowalski, Kamil     }
1547e439f0f8SKowalski, Kamil 
1548*55c7b7a2SEd Tanous     const std::string &parent_ifaceId = params[0];
1549*55c7b7a2SEd Tanous     const std::string &ifaceId = params[1];
1550927a505aSKowalski, Kamil 
1551*55c7b7a2SEd Tanous     if (!verifyNames(res, parent_ifaceId, ifaceId)) {
1552927a505aSKowalski, Kamil       return;
1553927a505aSKowalski, Kamil     }
1554927a505aSKowalski, Kamil 
1555927a505aSKowalski, Kamil     // Get single eth interface data, and call the below callback for JSON
1556927a505aSKowalski, Kamil     // preparation
1557*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceData(
1558*55c7b7a2SEd Tanous         ifaceId,
1559*55c7b7a2SEd Tanous         [&, parent_ifaceId, ifaceId](
1560927a505aSKowalski, Kamil             const bool &success, const EthernetInterfaceData &eth_data,
1561927a505aSKowalski, Kamil             const std::vector<IPv4AddressData> &ipv4_data) {
1562*55c7b7a2SEd Tanous           if (success && eth_data.vlanId != nullptr) {
1563*55c7b7a2SEd Tanous             res.jsonValue = parseInterfaceData(parent_ifaceId, ifaceId,
1564927a505aSKowalski, Kamil                                                 eth_data, ipv4_data);
1565927a505aSKowalski, Kamil 
1566927a505aSKowalski, Kamil             // Disable VLAN
1567927a505aSKowalski, Kamil             OnDemandEthernetProvider::disableVlan(
1568*55c7b7a2SEd Tanous                 ifaceId, [&](const boost::system::error_code ec) {
1569927a505aSKowalski, Kamil                   if (ec) {
1570*55c7b7a2SEd Tanous                     res.jsonValue = nlohmann::json::object();
1571*55c7b7a2SEd Tanous                     messages::addMessageToErrorJson(res.jsonValue,
1572927a505aSKowalski, Kamil                                                     messages::internalError());
1573927a505aSKowalski, Kamil                     res.result(
1574927a505aSKowalski, Kamil                         boost::beast::http::status::internal_server_error);
1575927a505aSKowalski, Kamil                   }
1576e439f0f8SKowalski, Kamil                   res.end();
1577927a505aSKowalski, Kamil                 });
1578927a505aSKowalski, Kamil           } else {
1579927a505aSKowalski, Kamil             // ... otherwise return error
1580927a505aSKowalski, Kamil             // TODO(Pawel)consider distinguish between non existing object,
1581927a505aSKowalski, Kamil             // and
1582927a505aSKowalski, Kamil             // other errors
1583927a505aSKowalski, Kamil             messages::addMessageToErrorJson(
1584*55c7b7a2SEd Tanous                 res.jsonValue,
1585*55c7b7a2SEd Tanous                 messages::resourceNotFound("VLAN Network Interface", ifaceId));
1586927a505aSKowalski, Kamil             res.result(boost::beast::http::status::not_found);
1587927a505aSKowalski, Kamil             res.end();
1588927a505aSKowalski, Kamil           }
1589927a505aSKowalski, Kamil         });
1590e439f0f8SKowalski, Kamil   }
1591e439f0f8SKowalski, Kamil 
1592e439f0f8SKowalski, Kamil   /**
1593e439f0f8SKowalski, Kamil    * This allows VlanNetworkInterfaceCollection to reuse this class' doGet
1594e439f0f8SKowalski, Kamil    * method, to maintain consistency of returned data, as Collection's doPost
1595e439f0f8SKowalski, Kamil    * should return data for created member which should match member's doGet
1596e439f0f8SKowalski, Kamil    * result in 100%.
1597e439f0f8SKowalski, Kamil    */
1598e439f0f8SKowalski, Kamil   friend VlanNetworkInterfaceCollection;
1599e439f0f8SKowalski, Kamil 
1600e439f0f8SKowalski, Kamil   // Ethernet Provider object
1601e439f0f8SKowalski, Kamil   // TODO(Pawel) consider move it to singleton
1602*55c7b7a2SEd Tanous   OnDemandEthernetProvider ethernetProvider;
1603e439f0f8SKowalski, Kamil };
1604e439f0f8SKowalski, Kamil 
1605e439f0f8SKowalski, Kamil /**
1606e439f0f8SKowalski, Kamil  * VlanNetworkInterfaceCollection derived class for delivering
1607e439f0f8SKowalski, Kamil  * VLANNetworkInterface Collection Schema
1608e439f0f8SKowalski, Kamil  */
1609e439f0f8SKowalski, Kamil class VlanNetworkInterfaceCollection : public Node {
1610e439f0f8SKowalski, Kamil  public:
1611e439f0f8SKowalski, Kamil   template <typename CrowApp>
1612e439f0f8SKowalski, Kamil   // TODO(Pawel) Remove line from below, where we assume that there is only one
1613e439f0f8SKowalski, Kamil   // manager called openbmc This shall be generic, but requires to update
1614e439f0f8SKowalski, Kamil   // GetSubroutes method
1615e439f0f8SKowalski, Kamil   VlanNetworkInterfaceCollection(CrowApp &app)
1616e439f0f8SKowalski, Kamil       : Node(app,
1617e439f0f8SKowalski, Kamil              "/redfish/v1/Managers/openbmc/EthernetInterfaces/<str>/VLANs/",
1618e439f0f8SKowalski, Kamil              std::string()),
1619e439f0f8SKowalski, Kamil         memberVlan(app) {
1620e439f0f8SKowalski, Kamil     Node::json["@odata.type"] =
1621e439f0f8SKowalski, Kamil         "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1622e439f0f8SKowalski, Kamil     Node::json["@odata.context"] =
1623e439f0f8SKowalski, Kamil         "/redfish/v1/$metadata"
1624e439f0f8SKowalski, Kamil         "#VLanNetworkInterfaceCollection.VLanNetworkInterfaceCollection";
1625e439f0f8SKowalski, Kamil     Node::json["Name"] = "VLAN Network Interface Collection";
1626e439f0f8SKowalski, Kamil 
1627e439f0f8SKowalski, Kamil     entityPrivileges = {
1628e439f0f8SKowalski, Kamil         {boost::beast::http::verb::get, {{"Login"}}},
1629e439f0f8SKowalski, Kamil         {boost::beast::http::verb::head, {{"Login"}}},
1630e439f0f8SKowalski, Kamil         {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1631e439f0f8SKowalski, Kamil         {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1632e439f0f8SKowalski, Kamil         {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1633e439f0f8SKowalski, Kamil         {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1634e439f0f8SKowalski, Kamil   }
1635e439f0f8SKowalski, Kamil 
1636e439f0f8SKowalski, Kamil  private:
1637e439f0f8SKowalski, Kamil   /**
1638e439f0f8SKowalski, Kamil    * Functions triggers appropriate requests on DBus
1639e439f0f8SKowalski, Kamil    */
1640*55c7b7a2SEd Tanous   void doGet(crow::Response &res, const crow::Request &req,
1641e439f0f8SKowalski, Kamil              const std::vector<std::string> &params) override {
1642e439f0f8SKowalski, Kamil     if (params.size() != 1) {
1643e439f0f8SKowalski, Kamil       // This means there is a problem with the router
1644e439f0f8SKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1645e439f0f8SKowalski, Kamil       res.end();
1646e439f0f8SKowalski, Kamil 
1647e439f0f8SKowalski, Kamil       return;
1648e439f0f8SKowalski, Kamil     }
1649e439f0f8SKowalski, Kamil 
1650e439f0f8SKowalski, Kamil     // TODO(Pawel) this shall be parametrized call to get EthernetInterfaces for
1651e439f0f8SKowalski, Kamil     // any Manager, not only hardcoded 'openbmc'.
1652*55c7b7a2SEd Tanous     std::string managerId = "openbmc";
1653e439f0f8SKowalski, Kamil     std::string rootInterfaceName = params[0];
1654e439f0f8SKowalski, Kamil 
1655*55c7b7a2SEd Tanous     // get eth interface list, and call the below callback for JSON preparation
1656*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceList([
1657*55c7b7a2SEd Tanous           &, managerId{std::move(managerId)},
1658e439f0f8SKowalski, Kamil           rootInterfaceName{std::move(rootInterfaceName)}
1659e439f0f8SKowalski, Kamil     ](const bool &success, const std::vector<std::string> &iface_list) {
1660e439f0f8SKowalski, Kamil       if (success) {
1661e439f0f8SKowalski, Kamil         bool rootInterfaceFound = false;
1662*55c7b7a2SEd Tanous         nlohmann::json ifaceArray = nlohmann::json::array();
1663e439f0f8SKowalski, Kamil 
1664*55c7b7a2SEd Tanous         for (const std::string &ifaceItem : iface_list) {
1665*55c7b7a2SEd Tanous           if (ifaceItem == rootInterfaceName) {
1666e439f0f8SKowalski, Kamil             rootInterfaceFound = true;
1667*55c7b7a2SEd Tanous           } else if (boost::starts_with(ifaceItem, rootInterfaceName + "_")) {
1668*55c7b7a2SEd Tanous             ifaceArray.push_back(
1669*55c7b7a2SEd Tanous                 {{"@odata.id", "/redfish/v1/Managers/" + managerId +
1670e439f0f8SKowalski, Kamil                                    "/EthernetInterfaces/" + rootInterfaceName +
1671*55c7b7a2SEd Tanous                                    "/VLANs/" + ifaceItem}});
1672e439f0f8SKowalski, Kamil           }
1673e439f0f8SKowalski, Kamil         }
1674e439f0f8SKowalski, Kamil 
1675e439f0f8SKowalski, Kamil         if (rootInterfaceFound) {
1676*55c7b7a2SEd Tanous           Node::json["Members"] = ifaceArray;
1677*55c7b7a2SEd Tanous           Node::json["Members@odata.count"] = ifaceArray.size();
1678*55c7b7a2SEd Tanous           Node::json["@odata.id"] = "/redfish/v1/Managers/" + managerId +
1679e439f0f8SKowalski, Kamil                                     "/EthernetInterfaces/" + rootInterfaceName +
1680e439f0f8SKowalski, Kamil                                     "/VLANs";
1681*55c7b7a2SEd Tanous           res.jsonValue = Node::json;
1682e439f0f8SKowalski, Kamil         } else {
1683e439f0f8SKowalski, Kamil           messages::addMessageToErrorJson(
1684*55c7b7a2SEd Tanous               res.jsonValue, messages::resourceNotFound("EthernetInterface",
1685e439f0f8SKowalski, Kamil                                                         rootInterfaceName));
1686e439f0f8SKowalski, Kamil           res.result(boost::beast::http::status::not_found);
1687e439f0f8SKowalski, Kamil           res.end();
1688e439f0f8SKowalski, Kamil         }
1689e439f0f8SKowalski, Kamil       } else {
1690e439f0f8SKowalski, Kamil         // No success, best what we can do is return INTERNALL ERROR
1691e439f0f8SKowalski, Kamil         res.result(boost::beast::http::status::internal_server_error);
1692e439f0f8SKowalski, Kamil       }
1693e439f0f8SKowalski, Kamil       res.end();
1694e439f0f8SKowalski, Kamil     });
1695e439f0f8SKowalski, Kamil   }
1696e439f0f8SKowalski, Kamil 
1697*55c7b7a2SEd Tanous   void doPost(crow::Response &res, const crow::Request &req,
1698e439f0f8SKowalski, Kamil               const std::vector<std::string> &params) override {
1699e439f0f8SKowalski, Kamil     if (params.size() != 1) {
1700e439f0f8SKowalski, Kamil       // This means there is a problem with the router
1701e439f0f8SKowalski, Kamil       res.result(boost::beast::http::status::internal_server_error);
1702e439f0f8SKowalski, Kamil       res.end();
1703e439f0f8SKowalski, Kamil       return;
1704e439f0f8SKowalski, Kamil     }
1705e439f0f8SKowalski, Kamil 
1706e439f0f8SKowalski, Kamil     nlohmann::json postReq;
1707e439f0f8SKowalski, Kamil 
1708e439f0f8SKowalski, Kamil     if (!json_util::processJsonFromRequest(res, req, postReq)) {
1709e439f0f8SKowalski, Kamil       return;
1710e439f0f8SKowalski, Kamil     }
1711e439f0f8SKowalski, Kamil 
1712e439f0f8SKowalski, Kamil     // TODO(Pawel) this shall be parametrized call to get EthernetInterfaces for
1713e439f0f8SKowalski, Kamil     // any Manager, not only hardcoded 'openbmc'.
1714*55c7b7a2SEd Tanous     std::string managerId = "openbmc";
1715e439f0f8SKowalski, Kamil     std::string rootInterfaceName = params[0];
1716e439f0f8SKowalski, Kamil     uint64_t vlanId;
1717e439f0f8SKowalski, Kamil     bool errorDetected;
1718e439f0f8SKowalski, Kamil 
1719e439f0f8SKowalski, Kamil     if (json_util::getUnsigned(
1720e439f0f8SKowalski, Kamil             "VLANId", postReq, vlanId,
1721e439f0f8SKowalski, Kamil             static_cast<uint8_t>(json_util::MessageSetting::MISSING) |
1722e439f0f8SKowalski, Kamil                 static_cast<uint8_t>(json_util::MessageSetting::TYPE_ERROR),
1723*55c7b7a2SEd Tanous             res.jsonValue, "/VLANId") != json_util::Result::SUCCESS) {
1724e439f0f8SKowalski, Kamil       res.end();
1725e439f0f8SKowalski, Kamil       return;
1726e439f0f8SKowalski, Kamil     }
1727e439f0f8SKowalski, Kamil 
1728*55c7b7a2SEd Tanous     // get eth interface list, and call the below callback for JSON preparation
1729*55c7b7a2SEd Tanous     ethernetProvider.getEthernetIfaceList([
1730*55c7b7a2SEd Tanous           &, managerId{std::move(managerId)},
1731e439f0f8SKowalski, Kamil           rootInterfaceName{std::move(rootInterfaceName)}
1732e439f0f8SKowalski, Kamil     ](const bool &success, const std::vector<std::string> &iface_list) {
1733e439f0f8SKowalski, Kamil       if (success) {
1734e439f0f8SKowalski, Kamil         bool rootInterfaceFound = false;
1735e439f0f8SKowalski, Kamil 
1736*55c7b7a2SEd Tanous         for (const std::string &ifaceItem : iface_list) {
1737*55c7b7a2SEd Tanous           if (ifaceItem == rootInterfaceName) {
1738e439f0f8SKowalski, Kamil             rootInterfaceFound = true;
1739e439f0f8SKowalski, Kamil             break;
1740e439f0f8SKowalski, Kamil           }
1741e439f0f8SKowalski, Kamil         }
1742e439f0f8SKowalski, Kamil 
1743e439f0f8SKowalski, Kamil         if (rootInterfaceFound) {
1744*55c7b7a2SEd Tanous           ethernetProvider.createVlan(
1745e439f0f8SKowalski, Kamil               rootInterfaceName, vlanId,
1746e439f0f8SKowalski, Kamil               [&, vlanId, rootInterfaceName,
1747e439f0f8SKowalski, Kamil                req{std::move(req)} ](const boost::system::error_code ec) {
1748e439f0f8SKowalski, Kamil                 if (ec) {
1749*55c7b7a2SEd Tanous                   messages::addMessageToErrorJson(res.jsonValue,
1750e439f0f8SKowalski, Kamil                                                   messages::internalError());
1751e439f0f8SKowalski, Kamil                   res.end();
1752e439f0f8SKowalski, Kamil                 } else {
1753e439f0f8SKowalski, Kamil                   memberVlan.doGet(
1754e439f0f8SKowalski, Kamil                       res, req,
1755e439f0f8SKowalski, Kamil                       {rootInterfaceName,
1756e439f0f8SKowalski, Kamil                        rootInterfaceName + "_" + std::to_string(vlanId)});
1757e439f0f8SKowalski, Kamil                 }
1758e439f0f8SKowalski, Kamil               });
1759e439f0f8SKowalski, Kamil         } else {
1760e439f0f8SKowalski, Kamil           messages::addMessageToErrorJson(
1761*55c7b7a2SEd Tanous               res.jsonValue, messages::resourceNotFound("EthernetInterface",
1762e439f0f8SKowalski, Kamil                                                         rootInterfaceName));
1763e439f0f8SKowalski, Kamil           res.result(boost::beast::http::status::not_found);
1764e439f0f8SKowalski, Kamil           res.end();
1765e439f0f8SKowalski, Kamil         }
1766e439f0f8SKowalski, Kamil       } else {
1767e439f0f8SKowalski, Kamil         // No success, best what we can do is return INTERNALL ERROR
1768e439f0f8SKowalski, Kamil         res.result(boost::beast::http::status::internal_server_error);
1769e439f0f8SKowalski, Kamil         res.end();
1770e439f0f8SKowalski, Kamil       }
1771e439f0f8SKowalski, Kamil     });
1772e439f0f8SKowalski, Kamil   }
1773e439f0f8SKowalski, Kamil 
1774e439f0f8SKowalski, Kamil   // Ethernet Provider object
1775e439f0f8SKowalski, Kamil   // TODO(Pawel) consider move it to singleton
1776*55c7b7a2SEd Tanous   OnDemandEthernetProvider ethernetProvider;
1777e439f0f8SKowalski, Kamil   VlanNetworkInterface memberVlan;
1778e439f0f8SKowalski, Kamil };
1779e439f0f8SKowalski, Kamil 
17809391bb9cSRapkiewicz, Pawel }  // namespace redfish
1781