xref: /openbmc/bmcweb/redfish-core/lib/ethernet.hpp (revision 3174e4dfd3185c131a07371b4b5a5b40cf0e0bdb)
1 /*
2 // Copyright (c) 2018 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #pragma once
17 
18 #include <boost/container/flat_map.hpp>
19 #include <boost/container/flat_set.hpp>
20 #include <dbus_singleton.hpp>
21 #include <error_messages.hpp>
22 #include <node.hpp>
23 #include <utils/json_utils.hpp>
24 
25 #include <optional>
26 #include <regex>
27 #include <variant>
28 
29 namespace redfish
30 {
31 
32 /**
33  * DBus types primitives for several generic DBus interfaces
34  * TODO(Pawel) consider move this to separate file into boost::dbus
35  */
36 using PropertiesMapType = boost::container::flat_map<
37     std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
38                               int32_t, uint32_t, int64_t, uint64_t, double>>;
39 
40 using GetManagedObjects = std::vector<std::pair<
41     sdbusplus::message::object_path,
42     std::vector<std::pair<
43         std::string,
44         boost::container::flat_map<
45             std::string,
46             std::variant<std::string, bool, uint8_t, int16_t, uint16_t, int32_t,
47                          uint32_t, int64_t, uint64_t, double,
48                          std::vector<std::string>>>>>>>;
49 
50 enum class LinkType
51 {
52     Local,
53     Global
54 };
55 
56 /**
57  * Structure for keeping IPv4 data required by Redfish
58  */
59 struct IPv4AddressData
60 {
61     std::string id;
62     std::string address;
63     std::string domain;
64     std::string gateway;
65     std::string netmask;
66     std::string origin;
67     LinkType linktype;
68     bool isActive;
69 
70     bool operator<(const IPv4AddressData& obj) const
71     {
72         return id < obj.id;
73     }
74 };
75 
76 /**
77  * Structure for keeping IPv6 data required by Redfish
78  */
79 struct IPv6AddressData
80 {
81     std::string id;
82     std::string address;
83     std::string origin;
84     uint8_t prefixLength;
85 
86     bool operator<(const IPv6AddressData& obj) const
87     {
88         return id < obj.id;
89     }
90 };
91 /**
92  * Structure for keeping basic single Ethernet Interface information
93  * available from DBus
94  */
95 struct EthernetInterfaceData
96 {
97     uint32_t speed;
98     bool auto_neg;
99     bool DNSEnabled;
100     bool NTPEnabled;
101     bool HostNameEnabled;
102     bool SendHostNameEnabled;
103     bool linkUp;
104     bool nicEnabled;
105     std::string DHCPEnabled;
106     std::string operatingMode;
107     std::string hostname;
108     std::string default_gateway;
109     std::string ipv6_default_gateway;
110     std::string mac_address;
111     std::vector<std::uint32_t> vlan_id;
112     std::vector<std::string> nameServers;
113     std::vector<std::string> staticNameServers;
114     std::vector<std::string> domainnames;
115 };
116 
117 struct DHCPParameters
118 {
119     std::optional<bool> dhcpv4Enabled;
120     std::optional<bool> useDNSServers;
121     std::optional<bool> useNTPServers;
122     std::optional<bool> useUseDomainName;
123     std::optional<std::string> dhcpv6OperatingMode;
124 };
125 
126 // Helper function that changes bits netmask notation (i.e. /24)
127 // into full dot notation
128 inline std::string getNetmask(unsigned int bits)
129 {
130     uint32_t value = 0xffffffff << (32 - bits);
131     std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
132                           std::to_string((value >> 16) & 0xff) + "." +
133                           std::to_string((value >> 8) & 0xff) + "." +
134                           std::to_string(value & 0xff);
135     return netmask;
136 }
137 
138 inline bool translateDHCPEnabledToBool(const std::string& inputDHCP,
139                                        bool isIPv4)
140 {
141     if (isIPv4)
142     {
143         return (
144             (inputDHCP ==
145              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
146             (inputDHCP ==
147              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
148     }
149     return ((inputDHCP ==
150              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
151             (inputDHCP ==
152              "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
153 }
154 
155 inline std::string getDhcpEnabledEnumeration(bool isIPv4, bool isIPv6)
156 {
157     if (isIPv4 && isIPv6)
158     {
159         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
160     }
161     if (isIPv4)
162     {
163         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
164     }
165     if (isIPv6)
166     {
167         return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
168     }
169     return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
170 }
171 
172 inline std::string
173     translateAddressOriginDbusToRedfish(const std::string& inputOrigin,
174                                         bool isIPv4)
175 {
176     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
177     {
178         return "Static";
179     }
180     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
181     {
182         if (isIPv4)
183         {
184             return "IPv4LinkLocal";
185         }
186         return "LinkLocal";
187     }
188     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
189     {
190         if (isIPv4)
191         {
192             return "DHCP";
193         }
194         return "DHCPv6";
195     }
196     if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
197     {
198         return "SLAAC";
199     }
200     return "";
201 }
202 
203 inline bool extractEthernetInterfaceData(const std::string& ethiface_id,
204                                          const GetManagedObjects& dbus_data,
205                                          EthernetInterfaceData& ethData)
206 {
207     bool idFound = false;
208     for (const auto& objpath : dbus_data)
209     {
210         for (const auto& ifacePair : objpath.second)
211         {
212             if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
213             {
214                 idFound = true;
215                 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
216                 {
217                     for (const auto& propertyPair : ifacePair.second)
218                     {
219                         if (propertyPair.first == "MACAddress")
220                         {
221                             const std::string* mac =
222                                 std::get_if<std::string>(&propertyPair.second);
223                             if (mac != nullptr)
224                             {
225                                 ethData.mac_address = *mac;
226                             }
227                         }
228                     }
229                 }
230                 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
231                 {
232                     for (const auto& propertyPair : ifacePair.second)
233                     {
234                         if (propertyPair.first == "Id")
235                         {
236                             const uint32_t* id =
237                                 std::get_if<uint32_t>(&propertyPair.second);
238                             if (id != nullptr)
239                             {
240                                 ethData.vlan_id.push_back(*id);
241                             }
242                         }
243                     }
244                 }
245                 else if (ifacePair.first ==
246                          "xyz.openbmc_project.Network.EthernetInterface")
247                 {
248                     for (const auto& propertyPair : ifacePair.second)
249                     {
250                         if (propertyPair.first == "AutoNeg")
251                         {
252                             const bool* autoNeg =
253                                 std::get_if<bool>(&propertyPair.second);
254                             if (autoNeg != nullptr)
255                             {
256                                 ethData.auto_neg = *autoNeg;
257                             }
258                         }
259                         else if (propertyPair.first == "Speed")
260                         {
261                             const uint32_t* speed =
262                                 std::get_if<uint32_t>(&propertyPair.second);
263                             if (speed != nullptr)
264                             {
265                                 ethData.speed = *speed;
266                             }
267                         }
268                         else if (propertyPair.first == "LinkUp")
269                         {
270                             const bool* linkUp =
271                                 std::get_if<bool>(&propertyPair.second);
272                             if (linkUp != nullptr)
273                             {
274                                 ethData.linkUp = *linkUp;
275                             }
276                         }
277                         else if (propertyPair.first == "NICEnabled")
278                         {
279                             const bool* nicEnabled =
280                                 std::get_if<bool>(&propertyPair.second);
281                             if (nicEnabled != nullptr)
282                             {
283                                 ethData.nicEnabled = *nicEnabled;
284                             }
285                         }
286                         else if (propertyPair.first == "Nameservers")
287                         {
288                             const std::vector<std::string>* nameservers =
289                                 std::get_if<std::vector<std::string>>(
290                                     &propertyPair.second);
291                             if (nameservers != nullptr)
292                             {
293                                 ethData.nameServers = std::move(*nameservers);
294                             }
295                         }
296                         else if (propertyPair.first == "StaticNameServers")
297                         {
298                             const std::vector<std::string>* staticNameServers =
299                                 std::get_if<std::vector<std::string>>(
300                                     &propertyPair.second);
301                             if (staticNameServers != nullptr)
302                             {
303                                 ethData.staticNameServers =
304                                     std::move(*staticNameServers);
305                             }
306                         }
307                         else if (propertyPair.first == "DHCPEnabled")
308                         {
309                             const std::string* dhcpEnabled =
310                                 std::get_if<std::string>(&propertyPair.second);
311                             if (dhcpEnabled != nullptr)
312                             {
313                                 ethData.DHCPEnabled = *dhcpEnabled;
314                             }
315                         }
316                         else if (propertyPair.first == "DomainName")
317                         {
318                             const std::vector<std::string>* domainNames =
319                                 std::get_if<std::vector<std::string>>(
320                                     &propertyPair.second);
321                             if (domainNames != nullptr)
322                             {
323                                 ethData.domainnames = std::move(*domainNames);
324                             }
325                         }
326                     }
327                 }
328             }
329 
330             if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
331             {
332                 if (ifacePair.first ==
333                     "xyz.openbmc_project.Network.DHCPConfiguration")
334                 {
335                     for (const auto& propertyPair : ifacePair.second)
336                     {
337                         if (propertyPair.first == "DNSEnabled")
338                         {
339                             const bool* dnsEnabled =
340                                 std::get_if<bool>(&propertyPair.second);
341                             if (dnsEnabled != nullptr)
342                             {
343                                 ethData.DNSEnabled = *dnsEnabled;
344                             }
345                         }
346                         else if (propertyPair.first == "NTPEnabled")
347                         {
348                             const bool* ntpEnabled =
349                                 std::get_if<bool>(&propertyPair.second);
350                             if (ntpEnabled != nullptr)
351                             {
352                                 ethData.NTPEnabled = *ntpEnabled;
353                             }
354                         }
355                         else if (propertyPair.first == "HostNameEnabled")
356                         {
357                             const bool* hostNameEnabled =
358                                 std::get_if<bool>(&propertyPair.second);
359                             if (hostNameEnabled != nullptr)
360                             {
361                                 ethData.HostNameEnabled = *hostNameEnabled;
362                             }
363                         }
364                         else if (propertyPair.first == "SendHostNameEnabled")
365                         {
366                             const bool* sendHostNameEnabled =
367                                 std::get_if<bool>(&propertyPair.second);
368                             if (sendHostNameEnabled != nullptr)
369                             {
370                                 ethData.SendHostNameEnabled =
371                                     *sendHostNameEnabled;
372                             }
373                         }
374                     }
375                 }
376             }
377             // System configuration shows up in the global namespace, so no need
378             // to check eth number
379             if (ifacePair.first ==
380                 "xyz.openbmc_project.Network.SystemConfiguration")
381             {
382                 for (const auto& propertyPair : ifacePair.second)
383                 {
384                     if (propertyPair.first == "HostName")
385                     {
386                         const std::string* hostname =
387                             std::get_if<std::string>(&propertyPair.second);
388                         if (hostname != nullptr)
389                         {
390                             ethData.hostname = *hostname;
391                         }
392                     }
393                     else if (propertyPair.first == "DefaultGateway")
394                     {
395                         const std::string* defaultGateway =
396                             std::get_if<std::string>(&propertyPair.second);
397                         if (defaultGateway != nullptr)
398                         {
399                             ethData.default_gateway = *defaultGateway;
400                         }
401                     }
402                     else if (propertyPair.first == "DefaultGateway6")
403                     {
404                         const std::string* defaultGateway6 =
405                             std::get_if<std::string>(&propertyPair.second);
406                         if (defaultGateway6 != nullptr)
407                         {
408                             ethData.ipv6_default_gateway = *defaultGateway6;
409                         }
410                     }
411                 }
412             }
413         }
414     }
415     return idFound;
416 }
417 
418 // Helper function that extracts data for single ethernet ipv6 address
419 inline void
420     extractIPV6Data(const std::string& ethiface_id,
421                     const GetManagedObjects& dbus_data,
422                     boost::container::flat_set<IPv6AddressData>& ipv6_config)
423 {
424     const std::string ipv6PathStart =
425         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
426 
427     // Since there might be several IPv6 configurations aligned with
428     // single ethernet interface, loop over all of them
429     for (const auto& objpath : dbus_data)
430     {
431         // Check if proper pattern for object path appears
432         if (boost::starts_with(objpath.first.str, ipv6PathStart))
433         {
434             for (auto& interface : objpath.second)
435             {
436                 if (interface.first == "xyz.openbmc_project.Network.IP")
437                 {
438                     // Instance IPv6AddressData structure, and set as
439                     // appropriate
440                     std::pair<
441                         boost::container::flat_set<IPv6AddressData>::iterator,
442                         bool>
443                         it = ipv6_config.insert(IPv6AddressData{});
444                     IPv6AddressData& ipv6Address = *it.first;
445                     ipv6Address.id =
446                         objpath.first.str.substr(ipv6PathStart.size());
447                     for (auto& property : interface.second)
448                     {
449                         if (property.first == "Address")
450                         {
451                             const std::string* address =
452                                 std::get_if<std::string>(&property.second);
453                             if (address != nullptr)
454                             {
455                                 ipv6Address.address = *address;
456                             }
457                         }
458                         else if (property.first == "Origin")
459                         {
460                             const std::string* origin =
461                                 std::get_if<std::string>(&property.second);
462                             if (origin != nullptr)
463                             {
464                                 ipv6Address.origin =
465                                     translateAddressOriginDbusToRedfish(*origin,
466                                                                         false);
467                             }
468                         }
469                         else if (property.first == "PrefixLength")
470                         {
471                             const uint8_t* prefix =
472                                 std::get_if<uint8_t>(&property.second);
473                             if (prefix != nullptr)
474                             {
475                                 ipv6Address.prefixLength = *prefix;
476                             }
477                         }
478                         else
479                         {
480                             BMCWEB_LOG_ERROR
481                                 << "Got extra property: " << property.first
482                                 << " on the " << objpath.first.str << " object";
483                         }
484                     }
485                 }
486             }
487         }
488     }
489 }
490 
491 // Helper function that extracts data for single ethernet ipv4 address
492 inline void
493     extractIPData(const std::string& ethiface_id,
494                   const GetManagedObjects& dbus_data,
495                   boost::container::flat_set<IPv4AddressData>& ipv4_config)
496 {
497     const std::string ipv4PathStart =
498         "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
499 
500     // Since there might be several IPv4 configurations aligned with
501     // single ethernet interface, loop over all of them
502     for (const auto& objpath : dbus_data)
503     {
504         // Check if proper pattern for object path appears
505         if (boost::starts_with(objpath.first.str, ipv4PathStart))
506         {
507             for (auto& interface : objpath.second)
508             {
509                 if (interface.first == "xyz.openbmc_project.Network.IP")
510                 {
511                     // Instance IPv4AddressData structure, and set as
512                     // appropriate
513                     std::pair<
514                         boost::container::flat_set<IPv4AddressData>::iterator,
515                         bool>
516                         it = ipv4_config.insert(IPv4AddressData{});
517                     IPv4AddressData& ipv4Address = *it.first;
518                     ipv4Address.id =
519                         objpath.first.str.substr(ipv4PathStart.size());
520                     for (auto& property : interface.second)
521                     {
522                         if (property.first == "Address")
523                         {
524                             const std::string* address =
525                                 std::get_if<std::string>(&property.second);
526                             if (address != nullptr)
527                             {
528                                 ipv4Address.address = *address;
529                             }
530                         }
531                         else if (property.first == "Gateway")
532                         {
533                             const std::string* gateway =
534                                 std::get_if<std::string>(&property.second);
535                             if (gateway != nullptr)
536                             {
537                                 ipv4Address.gateway = *gateway;
538                             }
539                         }
540                         else if (property.first == "Origin")
541                         {
542                             const std::string* origin =
543                                 std::get_if<std::string>(&property.second);
544                             if (origin != nullptr)
545                             {
546                                 ipv4Address.origin =
547                                     translateAddressOriginDbusToRedfish(*origin,
548                                                                         true);
549                             }
550                         }
551                         else if (property.first == "PrefixLength")
552                         {
553                             const uint8_t* mask =
554                                 std::get_if<uint8_t>(&property.second);
555                             if (mask != nullptr)
556                             {
557                                 // convert it to the string
558                                 ipv4Address.netmask = getNetmask(*mask);
559                             }
560                         }
561                         else
562                         {
563                             BMCWEB_LOG_ERROR
564                                 << "Got extra property: " << property.first
565                                 << " on the " << objpath.first.str << " object";
566                         }
567                     }
568                     // Check if given address is local, or global
569                     ipv4Address.linktype =
570                         boost::starts_with(ipv4Address.address, "169.254.")
571                             ? LinkType::Local
572                             : LinkType::Global;
573                 }
574             }
575         }
576     }
577 }
578 
579 /**
580  * @brief Sets given Id on the given VLAN interface through D-Bus
581  *
582  * @param[in] ifaceId       Id of VLAN interface that should be modified
583  * @param[in] inputVlanId   New ID of the VLAN
584  * @param[in] callback      Function that will be called after the operation
585  *
586  * @return None.
587  */
588 template <typename CallbackFunc>
589 void changeVlanId(const std::string& ifaceId, const uint32_t& inputVlanId,
590                   CallbackFunc&& callback)
591 {
592     crow::connections::systemBus->async_method_call(
593         callback, "xyz.openbmc_project.Network",
594         std::string("/xyz/openbmc_project/network/") + ifaceId,
595         "org.freedesktop.DBus.Properties", "Set",
596         "xyz.openbmc_project.Network.VLAN", "Id",
597         std::variant<uint32_t>(inputVlanId));
598 }
599 
600 /**
601  * @brief Helper function that verifies IP address to check if it is in
602  *        proper format. If bits pointer is provided, also calculates active
603  *        bit count for Subnet Mask.
604  *
605  * @param[in]  ip     IP that will be verified
606  * @param[out] bits   Calculated mask in bits notation
607  *
608  * @return true in case of success, false otherwise
609  */
610 inline bool ipv4VerifyIpAndGetBitcount(const std::string& ip,
611                                        uint8_t* bits = nullptr)
612 {
613     std::vector<std::string> bytesInMask;
614 
615     boost::split(bytesInMask, ip, boost::is_any_of("."));
616 
617     static const constexpr int ipV4AddressSectionsCount = 4;
618     if (bytesInMask.size() != ipV4AddressSectionsCount)
619     {
620         return false;
621     }
622 
623     if (bits != nullptr)
624     {
625         *bits = 0;
626     }
627 
628     char* endPtr;
629     long previousValue = 255;
630     bool firstZeroInByteHit;
631     for (const std::string& byte : bytesInMask)
632     {
633         if (byte.empty())
634         {
635             return false;
636         }
637 
638         // Use strtol instead of stroi to avoid exceptions
639         long value = std::strtol(byte.c_str(), &endPtr, 10);
640 
641         // endPtr should point to the end of the string, otherwise given string
642         // is not 100% number
643         if (*endPtr != '\0')
644         {
645             return false;
646         }
647 
648         // Value should be contained in byte
649         if (value < 0 || value > 255)
650         {
651             return false;
652         }
653 
654         if (bits != nullptr)
655         {
656             // Mask has to be continuous between bytes
657             if (previousValue != 255 && value != 0)
658             {
659                 return false;
660             }
661 
662             // Mask has to be continuous inside bytes
663             firstZeroInByteHit = false;
664 
665             // Count bits
666             for (long bitIdx = 7; bitIdx >= 0; bitIdx--)
667             {
668                 if (value & (1L << bitIdx))
669                 {
670                     if (firstZeroInByteHit)
671                     {
672                         // Continuity not preserved
673                         return false;
674                     }
675                     (*bits)++;
676                 }
677                 else
678                 {
679                     firstZeroInByteHit = true;
680                 }
681             }
682         }
683 
684         previousValue = value;
685     }
686 
687     return true;
688 }
689 
690 /**
691  * @brief Deletes given IPv4 interface
692  *
693  * @param[in] ifaceId     Id of interface whose IP should be deleted
694  * @param[in] ipHash      DBus Hash id of IP that should be deleted
695  * @param[io] asyncResp   Response object that will be returned to client
696  *
697  * @return None
698  */
699 inline void deleteIPv4(const std::string& ifaceId, const std::string& ipHash,
700                        const std::shared_ptr<AsyncResp> asyncResp)
701 {
702     crow::connections::systemBus->async_method_call(
703         [asyncResp](const boost::system::error_code ec) {
704             if (ec)
705             {
706                 messages::internalError(asyncResp->res);
707             }
708         },
709         "xyz.openbmc_project.Network",
710         "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
711         "xyz.openbmc_project.Object.Delete", "Delete");
712 }
713 
714 /**
715  * @brief Creates a static IPv4 entry
716  *
717  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
718  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
719  * @param[in] gateway      IPv4 address of this interfaces gateway
720  * @param[in] address      IPv4 address to assign to this interface
721  * @param[io] asyncResp    Response object that will be returned to client
722  *
723  * @return None
724  */
725 inline void createIPv4(const std::string& ifaceId, uint8_t prefixLength,
726                        const std::string& gateway, const std::string& address,
727                        std::shared_ptr<AsyncResp> asyncResp)
728 {
729     crow::connections::systemBus->async_method_call(
730         [asyncResp](const boost::system::error_code ec) {
731             if (ec)
732             {
733                 messages::internalError(asyncResp->res);
734             }
735         },
736         "xyz.openbmc_project.Network",
737         "/xyz/openbmc_project/network/" + ifaceId,
738         "xyz.openbmc_project.Network.IP.Create", "IP",
739         "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
740         gateway);
741 }
742 
743 /**
744  * @brief Deletes the IPv4 entry for this interface and creates a replacement
745  * static IPv4 entry
746  *
747  * @param[in] ifaceId      Id of interface upon which to create the IPv4 entry
748  * @param[in] id           The unique hash entry identifying the DBus entry
749  * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
750  * @param[in] gateway      IPv4 address of this interfaces gateway
751  * @param[in] address      IPv4 address to assign to this interface
752  * @param[io] asyncResp    Response object that will be returned to client
753  *
754  * @return None
755  */
756 inline void deleteAndCreateIPv4(const std::string& ifaceId,
757                                 const std::string& id, uint8_t prefixLength,
758                                 const std::string& gateway,
759                                 const std::string& address,
760                                 std::shared_ptr<AsyncResp> asyncResp)
761 {
762     crow::connections::systemBus->async_method_call(
763         [asyncResp, ifaceId, address, prefixLength,
764          gateway](const boost::system::error_code ec) {
765             if (ec)
766             {
767                 messages::internalError(asyncResp->res);
768             }
769             crow::connections::systemBus->async_method_call(
770                 [asyncResp](const boost::system::error_code ec2) {
771                     if (ec2)
772                     {
773                         messages::internalError(asyncResp->res);
774                     }
775                 },
776                 "xyz.openbmc_project.Network",
777                 "/xyz/openbmc_project/network/" + ifaceId,
778                 "xyz.openbmc_project.Network.IP.Create", "IP",
779                 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
780                 prefixLength, gateway);
781         },
782         "xyz.openbmc_project.Network",
783         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
784         "xyz.openbmc_project.Object.Delete", "Delete");
785 }
786 
787 /**
788  * @brief Deletes given IPv6
789  *
790  * @param[in] ifaceId     Id of interface whose IP should be deleted
791  * @param[in] ipHash      DBus Hash id of IP that should be deleted
792  * @param[io] asyncResp   Response object that will be returned to client
793  *
794  * @return None
795  */
796 inline void deleteIPv6(const std::string& ifaceId, const std::string& ipHash,
797                        const std::shared_ptr<AsyncResp> asyncResp)
798 {
799     crow::connections::systemBus->async_method_call(
800         [asyncResp](const boost::system::error_code ec) {
801             if (ec)
802             {
803                 messages::internalError(asyncResp->res);
804             }
805         },
806         "xyz.openbmc_project.Network",
807         "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
808         "xyz.openbmc_project.Object.Delete", "Delete");
809 }
810 
811 /**
812  * @brief Deletes the IPv6 entry for this interface and creates a replacement
813  * static IPv6 entry
814  *
815  * @param[in] ifaceId      Id of interface upon which to create the IPv6 entry
816  * @param[in] id           The unique hash entry identifying the DBus entry
817  * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
818  * @param[in] address      IPv6 address to assign to this interface
819  * @param[io] asyncResp    Response object that will be returned to client
820  *
821  * @return None
822  */
823 inline void deleteAndCreateIPv6(const std::string& ifaceId,
824                                 const std::string& id, uint8_t prefixLength,
825                                 const std::string& address,
826                                 std::shared_ptr<AsyncResp> asyncResp)
827 {
828     crow::connections::systemBus->async_method_call(
829         [asyncResp, ifaceId, address,
830          prefixLength](const boost::system::error_code ec) {
831             if (ec)
832             {
833                 messages::internalError(asyncResp->res);
834             }
835             crow::connections::systemBus->async_method_call(
836                 [asyncResp](const boost::system::error_code ec2) {
837                     if (ec2)
838                     {
839                         messages::internalError(asyncResp->res);
840                     }
841                 },
842                 "xyz.openbmc_project.Network",
843                 "/xyz/openbmc_project/network/" + ifaceId,
844                 "xyz.openbmc_project.Network.IP.Create", "IP",
845                 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
846                 prefixLength, "");
847         },
848         "xyz.openbmc_project.Network",
849         +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
850         "xyz.openbmc_project.Object.Delete", "Delete");
851 }
852 
853 /**
854  * @brief Creates IPv6 with given data
855  *
856  * @param[in] ifaceId      Id of interface whose IP should be added
857  * @param[in] prefixLength Prefix length that needs to be added
858  * @param[in] address      IP address that needs to be added
859  * @param[io] asyncResp    Response object that will be returned to client
860  *
861  * @return None
862  */
863 inline void createIPv6(const std::string& ifaceId, uint8_t prefixLength,
864                        const std::string& address,
865                        std::shared_ptr<AsyncResp> asyncResp)
866 {
867     auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
868         if (ec)
869         {
870             messages::internalError(asyncResp->res);
871         }
872     };
873     // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
874     // does not have associated gateway property
875     crow::connections::systemBus->async_method_call(
876         std::move(createIpHandler), "xyz.openbmc_project.Network",
877         "/xyz/openbmc_project/network/" + ifaceId,
878         "xyz.openbmc_project.Network.IP.Create", "IP",
879         "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
880         "");
881 }
882 
883 /**
884  * Function that retrieves all properties for given Ethernet Interface
885  * Object
886  * from EntityManager Network Manager
887  * @param ethiface_id a eth interface id to query on DBus
888  * @param callback a function that shall be called to convert Dbus output
889  * into JSON
890  */
891 template <typename CallbackFunc>
892 void getEthernetIfaceData(const std::string& ethiface_id,
893                           CallbackFunc&& callback)
894 {
895     crow::connections::systemBus->async_method_call(
896         [ethifaceId{std::string{ethiface_id}}, callback{std::move(callback)}](
897             const boost::system::error_code error_code,
898             const GetManagedObjects& resp) {
899             EthernetInterfaceData ethData{};
900             boost::container::flat_set<IPv4AddressData> ipv4Data;
901             boost::container::flat_set<IPv6AddressData> ipv6Data;
902 
903             if (error_code)
904             {
905                 callback(false, ethData, ipv4Data, ipv6Data);
906                 return;
907             }
908 
909             bool found =
910                 extractEthernetInterfaceData(ethifaceId, resp, ethData);
911             if (!found)
912             {
913                 callback(false, ethData, ipv4Data, ipv6Data);
914                 return;
915             }
916 
917             extractIPData(ethifaceId, resp, ipv4Data);
918             // Fix global GW
919             for (IPv4AddressData& ipv4 : ipv4Data)
920             {
921                 if (((ipv4.linktype == LinkType::Global) &&
922                      (ipv4.gateway == "0.0.0.0")) ||
923                     (ipv4.origin == "DHCP"))
924                 {
925                     ipv4.gateway = ethData.default_gateway;
926                 }
927             }
928 
929             extractIPV6Data(ethifaceId, resp, ipv6Data);
930             // Finally make a callback with useful data
931             callback(true, ethData, ipv4Data, ipv6Data);
932         },
933         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
934         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
935 }
936 
937 /**
938  * Function that retrieves all Ethernet Interfaces available through Network
939  * Manager
940  * @param callback a function that shall be called to convert Dbus output
941  * into JSON.
942  */
943 template <typename CallbackFunc>
944 void getEthernetIfaceList(CallbackFunc&& callback)
945 {
946     crow::connections::systemBus->async_method_call(
947         [callback{std::move(callback)}](
948             const boost::system::error_code error_code,
949             GetManagedObjects& resp) {
950             // Callback requires vector<string> to retrieve all available
951             // ethernet interfaces
952             boost::container::flat_set<std::string> ifaceList;
953             ifaceList.reserve(resp.size());
954             if (error_code)
955             {
956                 callback(false, ifaceList);
957                 return;
958             }
959 
960             // Iterate over all retrieved ObjectPaths.
961             for (const auto& objpath : resp)
962             {
963                 // And all interfaces available for certain ObjectPath.
964                 for (const auto& interface : objpath.second)
965                 {
966                     // If interface is
967                     // xyz.openbmc_project.Network.EthernetInterface, this is
968                     // what we're looking for.
969                     if (interface.first ==
970                         "xyz.openbmc_project.Network.EthernetInterface")
971                     {
972                         // Cut out everything until last "/", ...
973                         const std::string& ifaceId = objpath.first.str;
974                         std::size_t lastPos = ifaceId.rfind("/");
975                         if (lastPos != std::string::npos)
976                         {
977                             // and put it into output vector.
978                             ifaceList.emplace(ifaceId.substr(lastPos + 1));
979                         }
980                     }
981                 }
982             }
983             // Finally make a callback with useful data
984             callback(true, ifaceList);
985         },
986         "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
987         "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
988 }
989 
990 /**
991  * EthernetCollection derived class for delivering Ethernet Collection Schema
992  */
993 class EthernetCollection : public Node
994 {
995   public:
996     EthernetCollection(App& app) :
997         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
998     {
999         entityPrivileges = {
1000             {boost::beast::http::verb::get, {{"Login"}}},
1001             {boost::beast::http::verb::head, {{"Login"}}},
1002             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1003             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1004             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1005             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1006     }
1007 
1008   private:
1009     /**
1010      * Functions triggers appropriate requests on DBus
1011      */
1012     void doGet(crow::Response& res, const crow::Request&,
1013                const std::vector<std::string>&) override
1014     {
1015         res.jsonValue["@odata.type"] =
1016             "#EthernetInterfaceCollection.EthernetInterfaceCollection";
1017         res.jsonValue["@odata.id"] =
1018             "/redfish/v1/Managers/bmc/EthernetInterfaces";
1019         res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1020         res.jsonValue["Description"] =
1021             "Collection of EthernetInterfaces for this Manager";
1022         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1023         // Get eth interface list, and call the below callback for JSON
1024         // preparation
1025         getEthernetIfaceList(
1026             [asyncResp](
1027                 const bool& success,
1028                 const boost::container::flat_set<std::string>& iface_list) {
1029                 if (!success)
1030                 {
1031                     messages::internalError(asyncResp->res);
1032                     return;
1033                 }
1034 
1035                 nlohmann::json& ifaceArray =
1036                     asyncResp->res.jsonValue["Members"];
1037                 ifaceArray = nlohmann::json::array();
1038                 std::string tag = "_";
1039                 for (const std::string& ifaceItem : iface_list)
1040                 {
1041                     std::size_t found = ifaceItem.find(tag);
1042                     if (found == std::string::npos)
1043                     {
1044                         ifaceArray.push_back(
1045                             {{"@odata.id",
1046                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1047                                   ifaceItem}});
1048                     }
1049                 }
1050 
1051                 asyncResp->res.jsonValue["Members@odata.count"] =
1052                     ifaceArray.size();
1053                 asyncResp->res.jsonValue["@odata.id"] =
1054                     "/redfish/v1/Managers/bmc/EthernetInterfaces";
1055             });
1056     }
1057 };
1058 
1059 /**
1060  * EthernetInterface derived class for delivering Ethernet Schema
1061  */
1062 class EthernetInterface : public Node
1063 {
1064   public:
1065     /*
1066      * Default Constructor
1067      */
1068     EthernetInterface(App& app) :
1069         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
1070              std::string())
1071     {
1072         entityPrivileges = {
1073             {boost::beast::http::verb::get, {{"Login"}}},
1074             {boost::beast::http::verb::head, {{"Login"}}},
1075             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1076             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1077             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1078             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1079     }
1080 
1081   private:
1082     void handleHostnamePatch(const std::string& hostname,
1083                              const std::shared_ptr<AsyncResp> asyncResp)
1084     {
1085         // SHOULD handle host names of up to 255 characters(RFC 1123)
1086         if (hostname.length() > 255)
1087         {
1088             messages::propertyValueFormatError(asyncResp->res, hostname,
1089                                                "HostName");
1090             return;
1091         }
1092         crow::connections::systemBus->async_method_call(
1093             [asyncResp](const boost::system::error_code ec) {
1094                 if (ec)
1095                 {
1096                     messages::internalError(asyncResp->res);
1097                 }
1098             },
1099             "xyz.openbmc_project.Network",
1100             "/xyz/openbmc_project/network/config",
1101             "org.freedesktop.DBus.Properties", "Set",
1102             "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
1103             std::variant<std::string>(hostname));
1104     }
1105 
1106     void handleDomainnamePatch(const std::string& ifaceId,
1107                                const std::string& domainname,
1108                                const std::shared_ptr<AsyncResp> asyncResp)
1109     {
1110         std::vector<std::string> vectorDomainname = {domainname};
1111         crow::connections::systemBus->async_method_call(
1112             [asyncResp](const boost::system::error_code ec) {
1113                 if (ec)
1114                 {
1115                     messages::internalError(asyncResp->res);
1116                 }
1117             },
1118             "xyz.openbmc_project.Network",
1119             "/xyz/openbmc_project/network/" + ifaceId,
1120             "org.freedesktop.DBus.Properties", "Set",
1121             "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1122             std::variant<std::vector<std::string>>(vectorDomainname));
1123     }
1124 
1125     void handleFqdnPatch(const std::string& ifaceId, const std::string& fqdn,
1126                          const std::shared_ptr<AsyncResp> asyncResp)
1127     {
1128         // Total length of FQDN must not exceed 255 characters(RFC 1035)
1129         if (fqdn.length() > 255)
1130         {
1131             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1132             return;
1133         }
1134 
1135         size_t pos = fqdn.find('.');
1136         if (pos == std::string::npos)
1137         {
1138             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1139             return;
1140         }
1141 
1142         std::string hostname;
1143         std::string domainname;
1144         domainname = (fqdn).substr(pos + 1);
1145         hostname = (fqdn).substr(0, pos);
1146 
1147         if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1148         {
1149             messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1150             return;
1151         }
1152 
1153         handleHostnamePatch(hostname, asyncResp);
1154         handleDomainnamePatch(ifaceId, domainname, asyncResp);
1155     }
1156 
1157     bool isHostnameValid(const std::string& hostname)
1158     {
1159         // A valid host name can never have the dotted-decimal form (RFC 1123)
1160         if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1161         {
1162             return false;
1163         }
1164         // Each label(hostname/subdomains) within a valid FQDN
1165         // MUST handle host names of up to 63 characters (RFC 1123)
1166         // labels cannot start or end with hyphens (RFC 952)
1167         // labels can start with numbers (RFC 1123)
1168         const std::regex pattern(
1169             "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1170 
1171         return std::regex_match(hostname, pattern);
1172     }
1173 
1174     bool isDomainnameValid(const std::string& domainname)
1175     {
1176         // Can have multiple subdomains
1177         // Top Level Domain's min length is 2 character
1178         const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1179                                  "{1,30}\\.)*[a-zA-Z]{2,}$");
1180 
1181         return std::regex_match(domainname, pattern);
1182     }
1183 
1184     void handleMACAddressPatch(const std::string& ifaceId,
1185                                const std::string& macAddress,
1186                                const std::shared_ptr<AsyncResp>& asyncResp)
1187     {
1188         crow::connections::systemBus->async_method_call(
1189             [asyncResp, macAddress](const boost::system::error_code ec) {
1190                 if (ec)
1191                 {
1192                     messages::internalError(asyncResp->res);
1193                     return;
1194                 }
1195             },
1196             "xyz.openbmc_project.Network",
1197             "/xyz/openbmc_project/network/" + ifaceId,
1198             "org.freedesktop.DBus.Properties", "Set",
1199             "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1200             std::variant<std::string>(macAddress));
1201     }
1202 
1203     void setDHCPEnabled(const std::string& ifaceId,
1204                         const std::string& propertyName, const bool v4Value,
1205                         const bool v6Value,
1206                         const std::shared_ptr<AsyncResp> asyncResp)
1207     {
1208         const std::string dhcp = getDhcpEnabledEnumeration(v4Value, v6Value);
1209         crow::connections::systemBus->async_method_call(
1210             [asyncResp](const boost::system::error_code ec) {
1211                 if (ec)
1212                 {
1213                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1214                     messages::internalError(asyncResp->res);
1215                     return;
1216                 }
1217             },
1218             "xyz.openbmc_project.Network",
1219             "/xyz/openbmc_project/network/" + ifaceId,
1220             "org.freedesktop.DBus.Properties", "Set",
1221             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1222             std::variant<std::string>{dhcp});
1223     }
1224 
1225     void setEthernetInterfaceBoolProperty(
1226         const std::string& ifaceId, const std::string& propertyName,
1227         const bool& value, const std::shared_ptr<AsyncResp> asyncResp)
1228     {
1229         crow::connections::systemBus->async_method_call(
1230             [asyncResp](const boost::system::error_code ec) {
1231                 if (ec)
1232                 {
1233                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1234                     messages::internalError(asyncResp->res);
1235                     return;
1236                 }
1237             },
1238             "xyz.openbmc_project.Network",
1239             "/xyz/openbmc_project/network/" + ifaceId,
1240             "org.freedesktop.DBus.Properties", "Set",
1241             "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1242             std::variant<bool>{value});
1243     }
1244 
1245     void setDHCPv4Config(const std::string& propertyName, const bool& value,
1246                          const std::shared_ptr<AsyncResp> asyncResp)
1247     {
1248         BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1249         crow::connections::systemBus->async_method_call(
1250             [asyncResp](const boost::system::error_code ec) {
1251                 if (ec)
1252                 {
1253                     BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1254                     messages::internalError(asyncResp->res);
1255                     return;
1256                 }
1257             },
1258             "xyz.openbmc_project.Network",
1259             "/xyz/openbmc_project/network/config/dhcp",
1260             "org.freedesktop.DBus.Properties", "Set",
1261             "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1262             std::variant<bool>{value});
1263     }
1264 
1265     void handleDHCPPatch(const std::string& ifaceId,
1266                          const EthernetInterfaceData& ethData,
1267                          DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1268                          const std::shared_ptr<AsyncResp> asyncResp)
1269     {
1270         bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1271         bool ipv6Active =
1272             translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
1273 
1274         bool nextv4DHCPState =
1275             v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1276 
1277         bool nextv6DHCPState{};
1278         if (v6dhcpParms.dhcpv6OperatingMode)
1279         {
1280             if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1281                 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1282                 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1283             {
1284                 messages::propertyValueFormatError(
1285                     asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1286                     "OperatingMode");
1287                 return;
1288             }
1289             nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1290         }
1291         else
1292         {
1293             nextv6DHCPState = ipv6Active;
1294         }
1295 
1296         bool nextDNS{};
1297         if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
1298         {
1299             if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1300             {
1301                 messages::generalError(asyncResp->res);
1302                 return;
1303             }
1304             nextDNS = *v4dhcpParms.useDNSServers;
1305         }
1306         else if (v4dhcpParms.useDNSServers)
1307         {
1308             nextDNS = *v4dhcpParms.useDNSServers;
1309         }
1310         else if (v6dhcpParms.useDNSServers)
1311         {
1312             nextDNS = *v6dhcpParms.useDNSServers;
1313         }
1314         else
1315         {
1316             nextDNS = ethData.DNSEnabled;
1317         }
1318 
1319         bool nextNTP{};
1320         if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
1321         {
1322             if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1323             {
1324                 messages::generalError(asyncResp->res);
1325                 return;
1326             }
1327             nextNTP = *v4dhcpParms.useNTPServers;
1328         }
1329         else if (v4dhcpParms.useNTPServers)
1330         {
1331             nextNTP = *v4dhcpParms.useNTPServers;
1332         }
1333         else if (v6dhcpParms.useNTPServers)
1334         {
1335             nextNTP = *v6dhcpParms.useNTPServers;
1336         }
1337         else
1338         {
1339             nextNTP = ethData.NTPEnabled;
1340         }
1341 
1342         bool nextUseDomain{};
1343         if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
1344         {
1345             if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1346             {
1347                 messages::generalError(asyncResp->res);
1348                 return;
1349             }
1350             nextUseDomain = *v4dhcpParms.useUseDomainName;
1351         }
1352         else if (v4dhcpParms.useUseDomainName)
1353         {
1354             nextUseDomain = *v4dhcpParms.useUseDomainName;
1355         }
1356         else if (v6dhcpParms.useUseDomainName)
1357         {
1358             nextUseDomain = *v6dhcpParms.useUseDomainName;
1359         }
1360         else
1361         {
1362             nextUseDomain = ethData.HostNameEnabled;
1363         }
1364 
1365         BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1366         setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1367                        asyncResp);
1368         BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1369         setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1370         BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1371         setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1372         BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1373         setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
1374     }
1375 
1376     boost::container::flat_set<IPv4AddressData>::const_iterator
1377         getNextStaticIpEntry(
1378             boost::container::flat_set<IPv4AddressData>::const_iterator head,
1379             boost::container::flat_set<IPv4AddressData>::const_iterator end)
1380     {
1381         for (; head != end; head++)
1382         {
1383             if (head->origin == "Static")
1384             {
1385                 return head;
1386             }
1387         }
1388         return end;
1389     }
1390 
1391     boost::container::flat_set<IPv6AddressData>::const_iterator
1392         getNextStaticIpEntry(
1393             boost::container::flat_set<IPv6AddressData>::const_iterator head,
1394             boost::container::flat_set<IPv6AddressData>::const_iterator end)
1395     {
1396         for (; head != end; head++)
1397         {
1398             if (head->origin == "Static")
1399             {
1400                 return head;
1401             }
1402         }
1403         return end;
1404     }
1405 
1406     void handleIPv4StaticPatch(
1407         const std::string& ifaceId, nlohmann::json& input,
1408         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1409         const std::shared_ptr<AsyncResp> asyncResp)
1410     {
1411         if ((!input.is_array()) || input.empty())
1412         {
1413             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1414                                              "IPv4StaticAddresses");
1415             return;
1416         }
1417 
1418         unsigned entryIdx = 1;
1419         // Find the first static IP address currently active on the NIC and
1420         // match it to the first JSON element in the IPv4StaticAddresses array.
1421         // Match each subsequent JSON element to the next static IP programmed
1422         // into the NIC.
1423         boost::container::flat_set<IPv4AddressData>::const_iterator niciPentry =
1424             getNextStaticIpEntry(ipv4Data.cbegin(), ipv4Data.cend());
1425 
1426         for (nlohmann::json& thisJson : input)
1427         {
1428             std::string pathString =
1429                 "IPv4StaticAddresses/" + std::to_string(entryIdx);
1430 
1431             if (!thisJson.is_null() && !thisJson.empty())
1432             {
1433                 std::optional<std::string> address;
1434                 std::optional<std::string> subnetMask;
1435                 std::optional<std::string> gateway;
1436 
1437                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1438                                          address, "SubnetMask", subnetMask,
1439                                          "Gateway", gateway))
1440                 {
1441                     messages::propertyValueFormatError(
1442                         asyncResp->res, thisJson.dump(), pathString);
1443                     return;
1444                 }
1445 
1446                 // Find the address/subnet/gateway values. Any values that are
1447                 // not explicitly provided are assumed to be unmodified from the
1448                 // current state of the interface. Merge existing state into the
1449                 // current request.
1450                 const std::string* addr = nullptr;
1451                 const std::string* gw = nullptr;
1452                 uint8_t prefixLength = 0;
1453                 bool errorInEntry = false;
1454                 if (address)
1455                 {
1456                     if (ipv4VerifyIpAndGetBitcount(*address))
1457                     {
1458                         addr = &(*address);
1459                     }
1460                     else
1461                     {
1462                         messages::propertyValueFormatError(
1463                             asyncResp->res, *address, pathString + "/Address");
1464                         errorInEntry = true;
1465                     }
1466                 }
1467                 else if (niciPentry != ipv4Data.cend())
1468                 {
1469                     addr = &(niciPentry->address);
1470                 }
1471                 else
1472                 {
1473                     messages::propertyMissing(asyncResp->res,
1474                                               pathString + "/Address");
1475                     errorInEntry = true;
1476                 }
1477 
1478                 if (subnetMask)
1479                 {
1480                     if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1481                     {
1482                         messages::propertyValueFormatError(
1483                             asyncResp->res, *subnetMask,
1484                             pathString + "/SubnetMask");
1485                         errorInEntry = true;
1486                     }
1487                 }
1488                 else if (niciPentry != ipv4Data.cend())
1489                 {
1490                     if (!ipv4VerifyIpAndGetBitcount(niciPentry->netmask,
1491                                                     &prefixLength))
1492                     {
1493                         messages::propertyValueFormatError(
1494                             asyncResp->res, niciPentry->netmask,
1495                             pathString + "/SubnetMask");
1496                         errorInEntry = true;
1497                     }
1498                 }
1499                 else
1500                 {
1501                     messages::propertyMissing(asyncResp->res,
1502                                               pathString + "/SubnetMask");
1503                     errorInEntry = true;
1504                 }
1505 
1506                 if (gateway)
1507                 {
1508                     if (ipv4VerifyIpAndGetBitcount(*gateway))
1509                     {
1510                         gw = &(*gateway);
1511                     }
1512                     else
1513                     {
1514                         messages::propertyValueFormatError(
1515                             asyncResp->res, *gateway, pathString + "/Gateway");
1516                         errorInEntry = true;
1517                     }
1518                 }
1519                 else if (niciPentry != ipv4Data.cend())
1520                 {
1521                     gw = &niciPentry->gateway;
1522                 }
1523                 else
1524                 {
1525                     messages::propertyMissing(asyncResp->res,
1526                                               pathString + "/Gateway");
1527                     errorInEntry = true;
1528                 }
1529 
1530                 if (errorInEntry)
1531                 {
1532                     return;
1533                 }
1534 
1535                 if (niciPentry != ipv4Data.cend())
1536                 {
1537                     deleteAndCreateIPv4(ifaceId, niciPentry->id, prefixLength,
1538                                         *gw, *addr, asyncResp);
1539                     niciPentry =
1540                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1541                 }
1542                 else
1543                 {
1544                     createIPv4(ifaceId, prefixLength, *gateway, *address,
1545                                asyncResp);
1546                 }
1547                 entryIdx++;
1548             }
1549             else
1550             {
1551                 if (niciPentry == ipv4Data.cend())
1552                 {
1553                     // Requesting a DELETE/DO NOT MODIFY action for an item
1554                     // that isn't present on the eth(n) interface. Input JSON is
1555                     // in error, so bail out.
1556                     if (thisJson.is_null())
1557                     {
1558                         messages::resourceCannotBeDeleted(asyncResp->res);
1559                         return;
1560                     }
1561                     messages::propertyValueFormatError(
1562                         asyncResp->res, thisJson.dump(), pathString);
1563                     return;
1564                 }
1565 
1566                 if (thisJson.is_null())
1567                 {
1568                     deleteIPv4(ifaceId, niciPentry->id, asyncResp);
1569                 }
1570                 if (niciPentry != ipv4Data.cend())
1571                 {
1572                     niciPentry =
1573                         getNextStaticIpEntry(++niciPentry, ipv4Data.cend());
1574                 }
1575                 entryIdx++;
1576             }
1577         }
1578     }
1579 
1580     void handleStaticNameServersPatch(
1581         const std::string& ifaceId,
1582         const std::vector<std::string>& updatedStaticNameServers,
1583         const std::shared_ptr<AsyncResp>& asyncResp)
1584     {
1585         crow::connections::systemBus->async_method_call(
1586             [asyncResp](const boost::system::error_code ec) {
1587                 if (ec)
1588                 {
1589                     messages::internalError(asyncResp->res);
1590                     return;
1591                 }
1592             },
1593             "xyz.openbmc_project.Network",
1594             "/xyz/openbmc_project/network/" + ifaceId,
1595             "org.freedesktop.DBus.Properties", "Set",
1596             "xyz.openbmc_project.Network.EthernetInterface",
1597             "StaticNameServers",
1598             std::variant<std::vector<std::string>>{updatedStaticNameServers});
1599     }
1600 
1601     void handleIPv6StaticAddressesPatch(
1602         const std::string& ifaceId, nlohmann::json& input,
1603         const boost::container::flat_set<IPv6AddressData>& ipv6Data,
1604         const std::shared_ptr<AsyncResp> asyncResp)
1605     {
1606         if (!input.is_array() || input.empty())
1607         {
1608             messages::propertyValueTypeError(asyncResp->res, input.dump(),
1609                                              "IPv6StaticAddresses");
1610             return;
1611         }
1612         size_t entryIdx = 1;
1613         boost::container::flat_set<IPv6AddressData>::const_iterator niciPentry =
1614             getNextStaticIpEntry(ipv6Data.cbegin(), ipv6Data.cend());
1615         for (nlohmann::json& thisJson : input)
1616         {
1617             std::string pathString =
1618                 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1619 
1620             if (!thisJson.is_null() && !thisJson.empty())
1621             {
1622                 std::optional<std::string> address;
1623                 std::optional<uint8_t> prefixLength;
1624 
1625                 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1626                                          address, "PrefixLength", prefixLength))
1627                 {
1628                     messages::propertyValueFormatError(
1629                         asyncResp->res, thisJson.dump(), pathString);
1630                     return;
1631                 }
1632 
1633                 const std::string* addr;
1634                 uint8_t prefix;
1635 
1636                 // Find the address and prefixLength values. Any values that are
1637                 // not explicitly provided are assumed to be unmodified from the
1638                 // current state of the interface. Merge existing state into the
1639                 // current request.
1640                 if (address)
1641                 {
1642                     addr = &(*address);
1643                 }
1644                 else if (niciPentry != ipv6Data.end())
1645                 {
1646                     addr = &(niciPentry->address);
1647                 }
1648                 else
1649                 {
1650                     messages::propertyMissing(asyncResp->res,
1651                                               pathString + "/Address");
1652                     return;
1653                 }
1654 
1655                 if (prefixLength)
1656                 {
1657                     prefix = *prefixLength;
1658                 }
1659                 else if (niciPentry != ipv6Data.end())
1660                 {
1661                     prefix = niciPentry->prefixLength;
1662                 }
1663                 else
1664                 {
1665                     messages::propertyMissing(asyncResp->res,
1666                                               pathString + "/PrefixLength");
1667                     return;
1668                 }
1669 
1670                 if (niciPentry != ipv6Data.end())
1671                 {
1672                     deleteAndCreateIPv6(ifaceId, niciPentry->id, prefix, *addr,
1673                                         asyncResp);
1674                     niciPentry =
1675                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1676                 }
1677                 else
1678                 {
1679                     createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1680                 }
1681                 entryIdx++;
1682             }
1683             else
1684             {
1685                 if (niciPentry == ipv6Data.end())
1686                 {
1687                     // Requesting a DELETE/DO NOT MODIFY action for an item
1688                     // that isn't present on the eth(n) interface. Input JSON is
1689                     // in error, so bail out.
1690                     if (thisJson.is_null())
1691                     {
1692                         messages::resourceCannotBeDeleted(asyncResp->res);
1693                         return;
1694                     }
1695                     messages::propertyValueFormatError(
1696                         asyncResp->res, thisJson.dump(), pathString);
1697                     return;
1698                 }
1699 
1700                 if (thisJson.is_null())
1701                 {
1702                     deleteIPv6(ifaceId, niciPentry->id, asyncResp);
1703                 }
1704                 if (niciPentry != ipv6Data.cend())
1705                 {
1706                     niciPentry =
1707                         getNextStaticIpEntry(++niciPentry, ipv6Data.cend());
1708                 }
1709                 entryIdx++;
1710             }
1711         }
1712     }
1713 
1714     void parseInterfaceData(
1715         std::shared_ptr<AsyncResp> asyncResp, const std::string& iface_id,
1716         const EthernetInterfaceData& ethData,
1717         const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1718         const boost::container::flat_set<IPv6AddressData>& ipv6Data)
1719     {
1720         constexpr const std::array<const char*, 1> inventoryForEthernet = {
1721             "xyz.openbmc_project.Inventory.Item.Ethernet"};
1722 
1723         nlohmann::json& jsonResponse = asyncResp->res.jsonValue;
1724         jsonResponse["Id"] = iface_id;
1725         jsonResponse["@odata.id"] =
1726             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
1727         jsonResponse["InterfaceEnabled"] = ethData.nicEnabled;
1728 
1729         auto health = std::make_shared<HealthPopulate>(asyncResp);
1730 
1731         crow::connections::systemBus->async_method_call(
1732             [health](const boost::system::error_code ec,
1733                      std::vector<std::string>& resp) {
1734                 if (ec)
1735                 {
1736                     return;
1737                 }
1738 
1739                 health->inventory = std::move(resp);
1740             },
1741             "xyz.openbmc_project.ObjectMapper",
1742             "/xyz/openbmc_project/object_mapper",
1743             "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1744             int32_t(0), inventoryForEthernet);
1745 
1746         health->populate();
1747 
1748         if (ethData.nicEnabled)
1749         {
1750             jsonResponse["LinkStatus"] = "LinkUp";
1751             jsonResponse["Status"]["State"] = "Enabled";
1752         }
1753         else
1754         {
1755             jsonResponse["LinkStatus"] = "NoLink";
1756             jsonResponse["Status"]["State"] = "Disabled";
1757         }
1758 
1759         jsonResponse["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
1760         jsonResponse["SpeedMbps"] = ethData.speed;
1761         jsonResponse["MACAddress"] = ethData.mac_address;
1762         jsonResponse["DHCPv4"]["DHCPEnabled"] =
1763             translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1764         jsonResponse["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1765         jsonResponse["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1766         jsonResponse["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1767 
1768         jsonResponse["DHCPv6"]["OperatingMode"] =
1769             translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1770                                                                    : "Disabled";
1771         jsonResponse["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1772         jsonResponse["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1773         jsonResponse["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
1774 
1775         if (!ethData.hostname.empty())
1776         {
1777             jsonResponse["HostName"] = ethData.hostname;
1778 
1779             // When domain name is empty then it means, that it is a network
1780             // without domain names, and the host name itself must be treated as
1781             // FQDN
1782             std::string fqdn = std::move(ethData.hostname);
1783             if (!ethData.domainnames.empty())
1784             {
1785                 fqdn += "." + ethData.domainnames[0];
1786             }
1787             jsonResponse["FQDN"] = fqdn;
1788         }
1789 
1790         jsonResponse["VLANs"] = {
1791             {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1792                               iface_id + "/VLANs"}};
1793 
1794         jsonResponse["NameServers"] = ethData.nameServers;
1795         jsonResponse["StaticNameServers"] = ethData.staticNameServers;
1796 
1797         nlohmann::json& ipv4Array = jsonResponse["IPv4Addresses"];
1798         nlohmann::json& ipv4StaticArray = jsonResponse["IPv4StaticAddresses"];
1799         ipv4Array = nlohmann::json::array();
1800         ipv4StaticArray = nlohmann::json::array();
1801         for (auto& ipv4Config : ipv4Data)
1802         {
1803 
1804             std::string gatewayStr = ipv4Config.gateway;
1805             if (gatewayStr.empty())
1806             {
1807                 gatewayStr = "0.0.0.0";
1808             }
1809 
1810             ipv4Array.push_back({{"AddressOrigin", ipv4Config.origin},
1811                                  {"SubnetMask", ipv4Config.netmask},
1812                                  {"Address", ipv4Config.address},
1813                                  {"Gateway", gatewayStr}});
1814             if (ipv4Config.origin == "Static")
1815             {
1816                 ipv4StaticArray.push_back({{"AddressOrigin", ipv4Config.origin},
1817                                            {"SubnetMask", ipv4Config.netmask},
1818                                            {"Address", ipv4Config.address},
1819                                            {"Gateway", gatewayStr}});
1820             }
1821         }
1822 
1823         jsonResponse["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
1824 
1825         nlohmann::json& ipv6Array = jsonResponse["IPv6Addresses"];
1826         nlohmann::json& ipv6StaticArray = jsonResponse["IPv6StaticAddresses"];
1827         ipv6Array = nlohmann::json::array();
1828         ipv6StaticArray = nlohmann::json::array();
1829         nlohmann::json& ipv6AddrPolicyTable =
1830             jsonResponse["IPv6AddressPolicyTable"];
1831         ipv6AddrPolicyTable = nlohmann::json::array();
1832         for (auto& ipv6Config : ipv6Data)
1833         {
1834             ipv6Array.push_back({{"Address", ipv6Config.address},
1835                                  {"PrefixLength", ipv6Config.prefixLength},
1836                                  {"AddressOrigin", ipv6Config.origin},
1837                                  {"AddressState", nullptr}});
1838             if (ipv6Config.origin == "Static")
1839             {
1840                 ipv6StaticArray.push_back(
1841                     {{"Address", ipv6Config.address},
1842                      {"PrefixLength", ipv6Config.prefixLength},
1843                      {"AddressOrigin", ipv6Config.origin},
1844                      {"AddressState", nullptr}});
1845             }
1846         }
1847     }
1848 
1849     /**
1850      * Functions triggers appropriate requests on DBus
1851      */
1852     void doGet(crow::Response& res, const crow::Request&,
1853                const std::vector<std::string>& params) override
1854     {
1855         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1856         if (params.size() != 1)
1857         {
1858             messages::internalError(asyncResp->res);
1859             return;
1860         }
1861 
1862         getEthernetIfaceData(
1863             params[0],
1864             [this, asyncResp, ifaceId{std::string(params[0])}](
1865                 const bool& success, const EthernetInterfaceData& ethData,
1866                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1867                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1868                 if (!success)
1869                 {
1870                     // TODO(Pawel)consider distinguish between non existing
1871                     // object, and other errors
1872                     messages::resourceNotFound(asyncResp->res,
1873                                                "EthernetInterface", ifaceId);
1874                     return;
1875                 }
1876 
1877                 asyncResp->res.jsonValue["@odata.type"] =
1878                     "#EthernetInterface.v1_4_1.EthernetInterface";
1879                 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1880                 asyncResp->res.jsonValue["Description"] =
1881                     "Management Network Interface";
1882 
1883                 parseInterfaceData(asyncResp, ifaceId, ethData, ipv4Data,
1884                                    ipv6Data);
1885             });
1886     }
1887 
1888     void doPatch(crow::Response& res, const crow::Request& req,
1889                  const std::vector<std::string>& params) override
1890     {
1891         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1892         if (params.size() != 1)
1893         {
1894             messages::internalError(asyncResp->res);
1895             return;
1896         }
1897 
1898         const std::string& ifaceId = params[0];
1899 
1900         std::optional<std::string> hostname;
1901         std::optional<std::string> fqdn;
1902         std::optional<std::string> macAddress;
1903         std::optional<std::string> ipv6DefaultGateway;
1904         std::optional<nlohmann::json> ipv4StaticAddresses;
1905         std::optional<nlohmann::json> ipv6StaticAddresses;
1906         std::optional<std::vector<std::string>> staticNameServers;
1907         std::optional<nlohmann::json> dhcpv4;
1908         std::optional<nlohmann::json> dhcpv6;
1909         std::optional<bool> interfaceEnabled;
1910         DHCPParameters v4dhcpParms;
1911         DHCPParameters v6dhcpParms;
1912 
1913         if (!json_util::readJson(
1914                 req, res, "HostName", hostname, "FQDN", fqdn,
1915                 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1916                 macAddress, "StaticNameServers", staticNameServers,
1917                 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1918                 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1919                 "InterfaceEnabled", interfaceEnabled))
1920         {
1921             return;
1922         }
1923         if (dhcpv4)
1924         {
1925             if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1926                                      v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1927                                      v4dhcpParms.useDNSServers, "UseNTPServers",
1928                                      v4dhcpParms.useNTPServers, "UseDomainName",
1929                                      v4dhcpParms.useUseDomainName))
1930             {
1931                 return;
1932             }
1933         }
1934 
1935         if (dhcpv6)
1936         {
1937             if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1938                                      v6dhcpParms.dhcpv6OperatingMode,
1939                                      "UseDNSServers", v6dhcpParms.useDNSServers,
1940                                      "UseNTPServers", v6dhcpParms.useNTPServers,
1941                                      "UseDomainName",
1942                                      v6dhcpParms.useUseDomainName))
1943             {
1944                 return;
1945             }
1946         }
1947 
1948         // Get single eth interface data, and call the below callback for
1949         // JSON preparation
1950         getEthernetIfaceData(
1951             ifaceId,
1952             [this, asyncResp, ifaceId, hostname = std::move(hostname),
1953              fqdn = std::move(fqdn), macAddress = std::move(macAddress),
1954              ipv4StaticAddresses = std::move(ipv4StaticAddresses),
1955              ipv6DefaultGateway = std::move(ipv6DefaultGateway),
1956              ipv6StaticAddresses = std::move(ipv6StaticAddresses),
1957              staticNameServers = std::move(staticNameServers),
1958              dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1959              v4dhcpParms = std::move(v4dhcpParms),
1960              v6dhcpParms = std::move(v6dhcpParms),
1961              interfaceEnabled = std::move(interfaceEnabled)](
1962                 const bool& success, const EthernetInterfaceData& ethData,
1963                 const boost::container::flat_set<IPv4AddressData>& ipv4Data,
1964                 const boost::container::flat_set<IPv6AddressData>& ipv6Data) {
1965                 if (!success)
1966                 {
1967                     // ... otherwise return error
1968                     // TODO(Pawel)consider distinguish between non existing
1969                     // object, and other errors
1970                     messages::resourceNotFound(asyncResp->res,
1971                                                "Ethernet Interface", ifaceId);
1972                     return;
1973                 }
1974 
1975                 if (dhcpv4 || dhcpv6)
1976                 {
1977                     handleDHCPPatch(ifaceId, ethData, std::move(v4dhcpParms),
1978                                     std::move(v6dhcpParms), asyncResp);
1979                 }
1980 
1981                 if (hostname)
1982                 {
1983                     handleHostnamePatch(*hostname, asyncResp);
1984                 }
1985 
1986                 if (fqdn)
1987                 {
1988                     handleFqdnPatch(ifaceId, *fqdn, asyncResp);
1989                 }
1990 
1991                 if (macAddress)
1992                 {
1993                     handleMACAddressPatch(ifaceId, *macAddress, asyncResp);
1994                 }
1995 
1996                 if (ipv4StaticAddresses)
1997                 {
1998                     // TODO(ed) for some reason the capture of ipv4Addresses
1999                     // above is returning a const value, not a non-const
2000                     // value. This doesn't really work for us, as we need to
2001                     // be able to efficiently move out the intermedia
2002                     // nlohmann::json objects. This makes a copy of the
2003                     // structure, and operates on that, but could be done
2004                     // more efficiently
2005                     nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
2006                     handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data,
2007                                           asyncResp);
2008                 }
2009 
2010                 if (staticNameServers)
2011                 {
2012                     handleStaticNameServersPatch(ifaceId, *staticNameServers,
2013                                                  asyncResp);
2014                 }
2015 
2016                 if (ipv6DefaultGateway)
2017                 {
2018                     messages::propertyNotWritable(asyncResp->res,
2019                                                   "IPv6DefaultGateway");
2020                 }
2021 
2022                 if (ipv6StaticAddresses)
2023                 {
2024                     nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2025                     handleIPv6StaticAddressesPatch(ifaceId, ipv6Static,
2026                                                    ipv6Data, asyncResp);
2027                 }
2028 
2029                 if (interfaceEnabled)
2030                 {
2031                     setEthernetInterfaceBoolProperty(
2032                         ifaceId, "NICEnabled", *interfaceEnabled, asyncResp);
2033                 }
2034             });
2035     }
2036 };
2037 
2038 /**
2039  * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2040  * Schema
2041  */
2042 class VlanNetworkInterface : public Node
2043 {
2044   public:
2045     /*
2046      * Default Constructor
2047      */
2048     VlanNetworkInterface(App& app) :
2049         Node(app,
2050              "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
2051              std::string(), std::string())
2052     {
2053         entityPrivileges = {
2054             {boost::beast::http::verb::get, {{"Login"}}},
2055             {boost::beast::http::verb::head, {{"Login"}}},
2056             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2057             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2058             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2059             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2060     }
2061 
2062   private:
2063     void parseInterfaceData(nlohmann::json& json_response,
2064                             const std::string& parent_iface_id,
2065                             const std::string& iface_id,
2066                             const EthernetInterfaceData& ethData)
2067     {
2068         // Fill out obvious data...
2069         json_response["Id"] = iface_id;
2070         json_response["@odata.id"] =
2071             "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2072             "/VLANs/" + iface_id;
2073 
2074         json_response["VLANEnable"] = true;
2075         if (!ethData.vlan_id.empty())
2076         {
2077             json_response["VLANId"] = ethData.vlan_id.back();
2078         }
2079     }
2080 
2081     bool verifyNames(const std::string& parent, const std::string& iface)
2082     {
2083         if (!boost::starts_with(iface, parent + "_"))
2084         {
2085             return false;
2086         }
2087         return true;
2088     }
2089 
2090     /**
2091      * Functions triggers appropriate requests on DBus
2092      */
2093     void doGet(crow::Response& res, const crow::Request&,
2094                const std::vector<std::string>& params) override
2095     {
2096         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2097         // TODO(Pawel) this shall be parameterized call (two params) to get
2098         // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2099         // Check if there is required param, truly entering this shall be
2100         // impossible.
2101         if (params.size() != 2)
2102         {
2103             messages::internalError(res);
2104             res.end();
2105             return;
2106         }
2107 
2108         const std::string& parentIfaceId = params[0];
2109         const std::string& ifaceId = params[1];
2110         res.jsonValue["@odata.type"] =
2111             "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
2112         res.jsonValue["Name"] = "VLAN Network Interface";
2113 
2114         if (!verifyNames(parentIfaceId, ifaceId))
2115         {
2116             return;
2117         }
2118 
2119         // Get single eth interface data, and call the below callback for
2120         // JSON preparation
2121         getEthernetIfaceData(
2122             params[1],
2123             [this, asyncResp, parentIfaceId{std::string(params[0])},
2124              ifaceId{std::string(params[1])}](
2125                 const bool& success, const EthernetInterfaceData& ethData,
2126                 const boost::container::flat_set<IPv4AddressData>&,
2127                 const boost::container::flat_set<IPv6AddressData>&) {
2128                 if (success && ethData.vlan_id.size() != 0)
2129                 {
2130                     parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
2131                                        ifaceId, ethData);
2132                 }
2133                 else
2134                 {
2135                     // ... otherwise return error
2136                     // TODO(Pawel)consider distinguish between non existing
2137                     // object, and other errors
2138                     messages::resourceNotFound(
2139                         asyncResp->res, "VLAN Network Interface", ifaceId);
2140                 }
2141             });
2142     }
2143 
2144     void doPatch(crow::Response& res, const crow::Request& req,
2145                  const std::vector<std::string>& params) override
2146     {
2147         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2148         if (params.size() != 2)
2149         {
2150             messages::internalError(asyncResp->res);
2151             return;
2152         }
2153 
2154         const std::string& parentIfaceId = params[0];
2155         const std::string& ifaceId = params[1];
2156 
2157         if (!verifyNames(parentIfaceId, ifaceId))
2158         {
2159             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2160                                        ifaceId);
2161             return;
2162         }
2163 
2164         bool vlanEnable = false;
2165         uint32_t vlanId = 0;
2166 
2167         if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2168                                  vlanId))
2169         {
2170             return;
2171         }
2172 
2173         // Get single eth interface data, and call the below callback for
2174         // JSON preparation
2175         getEthernetIfaceData(
2176             params[1],
2177             [asyncResp, parentIfaceId{std::string(params[0])},
2178              ifaceId{std::string(params[1])}, &vlanEnable,
2179              &vlanId](const bool& success, const EthernetInterfaceData& ethData,
2180                       const boost::container::flat_set<IPv4AddressData>&,
2181                       const boost::container::flat_set<IPv6AddressData>&) {
2182                 if (success && !ethData.vlan_id.empty())
2183                 {
2184                     auto callback =
2185                         [asyncResp](const boost::system::error_code ec) {
2186                             if (ec)
2187                             {
2188                                 messages::internalError(asyncResp->res);
2189                             }
2190                         };
2191 
2192                     if (vlanEnable == true)
2193                     {
2194                         crow::connections::systemBus->async_method_call(
2195                             std::move(callback), "xyz.openbmc_project.Network",
2196                             "/xyz/openbmc_project/network/" + ifaceId,
2197                             "org.freedesktop.DBus.Properties", "Set",
2198                             "xyz.openbmc_project.Network.VLAN", "Id",
2199                             std::variant<uint32_t>(vlanId));
2200                     }
2201                     else
2202                     {
2203                         BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2204                                             "vlan interface";
2205                         crow::connections::systemBus->async_method_call(
2206                             std::move(callback), "xyz.openbmc_project.Network",
2207                             std::string("/xyz/openbmc_project/network/") +
2208                                 ifaceId,
2209                             "xyz.openbmc_project.Object.Delete", "Delete");
2210                     }
2211                 }
2212                 else
2213                 {
2214                     // TODO(Pawel)consider distinguish between non existing
2215                     // object, and other errors
2216                     messages::resourceNotFound(
2217                         asyncResp->res, "VLAN Network Interface", ifaceId);
2218                     return;
2219                 }
2220             });
2221     }
2222 
2223     void doDelete(crow::Response& res, const crow::Request&,
2224                   const std::vector<std::string>& params) override
2225     {
2226         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2227         if (params.size() != 2)
2228         {
2229             messages::internalError(asyncResp->res);
2230             return;
2231         }
2232 
2233         const std::string& parentIfaceId = params[0];
2234         const std::string& ifaceId = params[1];
2235 
2236         if (!verifyNames(parentIfaceId, ifaceId))
2237         {
2238             messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2239                                        ifaceId);
2240             return;
2241         }
2242 
2243         // Get single eth interface data, and call the below callback for
2244         // JSON preparation
2245         getEthernetIfaceData(
2246             params[1],
2247             [asyncResp, parentIfaceId{std::string(params[0])},
2248              ifaceId{std::string(params[1])}](
2249                 const bool& success, const EthernetInterfaceData& ethData,
2250                 const boost::container::flat_set<IPv4AddressData>&,
2251                 const boost::container::flat_set<IPv6AddressData>&) {
2252                 if (success && !ethData.vlan_id.empty())
2253                 {
2254                     auto callback =
2255                         [asyncResp](const boost::system::error_code ec) {
2256                             if (ec)
2257                             {
2258                                 messages::internalError(asyncResp->res);
2259                             }
2260                         };
2261                     crow::connections::systemBus->async_method_call(
2262                         std::move(callback), "xyz.openbmc_project.Network",
2263                         std::string("/xyz/openbmc_project/network/") + ifaceId,
2264                         "xyz.openbmc_project.Object.Delete", "Delete");
2265                 }
2266                 else
2267                 {
2268                     // ... otherwise return error
2269                     // TODO(Pawel)consider distinguish between non existing
2270                     // object, and other errors
2271                     messages::resourceNotFound(
2272                         asyncResp->res, "VLAN Network Interface", ifaceId);
2273                 }
2274             });
2275     }
2276 };
2277 
2278 /**
2279  * VlanNetworkInterfaceCollection derived class for delivering
2280  * VLANNetworkInterface Collection Schema
2281  */
2282 class VlanNetworkInterfaceCollection : public Node
2283 {
2284   public:
2285     VlanNetworkInterfaceCollection(App& app) :
2286         Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2287              std::string())
2288     {
2289         entityPrivileges = {
2290             {boost::beast::http::verb::get, {{"Login"}}},
2291             {boost::beast::http::verb::head, {{"Login"}}},
2292             {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2293             {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2294             {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2295             {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
2296     }
2297 
2298   private:
2299     /**
2300      * Functions triggers appropriate requests on DBus
2301      */
2302     void doGet(crow::Response& res, const crow::Request&,
2303                const std::vector<std::string>& params) override
2304     {
2305         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2306         if (params.size() != 1)
2307         {
2308             // This means there is a problem with the router
2309             messages::internalError(asyncResp->res);
2310             return;
2311         }
2312 
2313         const std::string& rootInterfaceName = params[0];
2314 
2315         // Get eth interface list, and call the below callback for JSON
2316         // preparation
2317         getEthernetIfaceList(
2318             [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
2319                 const bool& success,
2320                 const boost::container::flat_set<std::string>& iface_list) {
2321                 if (!success)
2322                 {
2323                     messages::internalError(asyncResp->res);
2324                     return;
2325                 }
2326 
2327                 if (iface_list.find(rootInterfaceName) == iface_list.end())
2328                 {
2329                     messages::resourceNotFound(asyncResp->res,
2330                                                "VLanNetworkInterfaceCollection",
2331                                                rootInterfaceName);
2332                     return;
2333                 }
2334 
2335                 asyncResp->res.jsonValue["@odata.type"] =
2336                     "#VLanNetworkInterfaceCollection."
2337                     "VLanNetworkInterfaceCollection";
2338                 asyncResp->res.jsonValue["Name"] =
2339                     "VLAN Network Interface Collection";
2340 
2341                 nlohmann::json ifaceArray = nlohmann::json::array();
2342 
2343                 for (const std::string& ifaceItem : iface_list)
2344                 {
2345                     if (boost::starts_with(ifaceItem, rootInterfaceName + "_"))
2346                     {
2347                         ifaceArray.push_back(
2348                             {{"@odata.id",
2349                               "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2350                                   rootInterfaceName + "/VLANs/" + ifaceItem}});
2351                     }
2352                 }
2353 
2354                 asyncResp->res.jsonValue["Members@odata.count"] =
2355                     ifaceArray.size();
2356                 asyncResp->res.jsonValue["Members"] = std::move(ifaceArray);
2357                 asyncResp->res.jsonValue["@odata.id"] =
2358                     "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2359                     rootInterfaceName + "/VLANs";
2360             });
2361     }
2362 
2363     void doPost(crow::Response& res, const crow::Request& req,
2364                 const std::vector<std::string>& params) override
2365     {
2366         std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2367         if (params.size() != 1)
2368         {
2369             messages::internalError(asyncResp->res);
2370             return;
2371         }
2372         bool vlanEnable = false;
2373         uint32_t vlanId = 0;
2374         if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2375                                  vlanEnable))
2376         {
2377             return;
2378         }
2379         // Need both vlanId and vlanEnable to service this request
2380         if (!vlanId)
2381         {
2382             messages::propertyMissing(asyncResp->res, "VLANId");
2383         }
2384         if (!vlanEnable)
2385         {
2386             messages::propertyMissing(asyncResp->res, "VLANEnable");
2387         }
2388         if (static_cast<bool>(vlanId) ^ vlanEnable)
2389         {
2390             return;
2391         }
2392 
2393         const std::string& rootInterfaceName = params[0];
2394         auto callback = [asyncResp](const boost::system::error_code ec) {
2395             if (ec)
2396             {
2397                 // TODO(ed) make more consistent error messages based on
2398                 // phosphor-network responses
2399                 messages::internalError(asyncResp->res);
2400                 return;
2401             }
2402             messages::created(asyncResp->res);
2403         };
2404         crow::connections::systemBus->async_method_call(
2405             std::move(callback), "xyz.openbmc_project.Network",
2406             "/xyz/openbmc_project/network",
2407             "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
2408             rootInterfaceName, vlanId);
2409     }
2410 };
2411 } // namespace redfish
2412