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