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