blob: 702136a09a1d6f797a1b78fba4fd7cfc19fcfec2 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
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
Ed Tanous1abe55e2018-09-05 08:30:59 -070018#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020021#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <node.hpp>
Ed Tanousa24526d2018-12-10 15:17:59 -080023#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053024#include <regex>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020025#include <utils/json_utils.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080026#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010030
31/**
32 * DBus types primitives for several generic DBus interfaces
33 * TODO(Pawel) consider move this to separate file into boost::dbus
34 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070035using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080036 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
37 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010038
Ed Tanous4a0cb852018-10-15 07:55:04 -070039using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070041 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 std::string,
43 boost::container::flat_map<
Ed Tanous029573d2019-02-01 10:57:49 -080044 std::string, sdbusplus::message::variant<
45 std::string, bool, uint8_t, int16_t, uint16_t,
46 int32_t, uint32_t, int64_t, uint64_t, double,
47 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070048
49enum class LinkType
50{
51 Local,
52 Global
53};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010054
55/**
56 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010057 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070058struct IPv4AddressData
59{
60 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070061 std::string address;
62 std::string domain;
63 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 std::string netmask;
65 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070066 LinkType linktype;
67
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 bool operator<(const IPv4AddressData &obj) const
69 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070070 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010072};
73
74/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050075 * Structure for keeping IPv6 data required by Redfish
76 */
77struct IPv6AddressData
78{
79 std::string id;
80 std::string address;
81 std::string origin;
82 uint8_t prefixLength;
83
84 bool operator<(const IPv6AddressData &obj) const
85 {
86 return id < obj.id;
87 }
88};
89/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010090 * Structure for keeping basic single Ethernet Interface information
91 * available from DBus
92 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070093struct EthernetInterfaceData
94{
Ed Tanous4a0cb852018-10-15 07:55:04 -070095 uint32_t speed;
96 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070097 bool DNSEnabled;
98 bool NTPEnabled;
99 bool HostNameEnabled;
100 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800101 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700102 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700103 std::string DHCPEnabled;
104 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700105 std::string hostname;
106 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500107 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700108 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500109 std::vector<std::uint32_t> vlan_id;
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500110 std::vector<std::string> nameServers;
111 std::vector<std::string> staticNameServers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800112 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100113};
114
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700115struct DHCPParameters
116{
117 std::optional<bool> dhcpv4Enabled;
118 std::optional<bool> useDNSServers;
119 std::optional<bool> useNTPServers;
120 std::optional<bool> useUseDomainName;
121 std::optional<std::string> dhcpv6OperatingMode;
122};
123
Ed Tanous4a0cb852018-10-15 07:55:04 -0700124// Helper function that changes bits netmask notation (i.e. /24)
125// into full dot notation
126inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700127{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700128 uint32_t value = 0xffffffff << (32 - bits);
129 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
130 std::to_string((value >> 16) & 0xff) + "." +
131 std::to_string((value >> 8) & 0xff) + "." +
132 std::to_string(value & 0xff);
133 return netmask;
134}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100135
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700136inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
137 bool isIPv4)
138{
139 if (isIPv4)
140 {
141 return (
142 (inputDHCP ==
143 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
144 (inputDHCP ==
145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
146 }
147 return ((inputDHCP ==
148 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
149 (inputDHCP ==
150 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
151}
152
153inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
154{
155 if (isIPv4 && isIPv6)
156 {
157 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
158 }
159 else if (isIPv4)
160 {
161 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
162 }
163 else if (isIPv6)
164 {
165 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
166 }
167 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
168}
169
Ed Tanous4a0cb852018-10-15 07:55:04 -0700170inline std::string
171 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
172 bool isIPv4)
173{
174 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700176 return "Static";
177 }
178 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
179 {
180 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700182 return "IPv4LinkLocal";
183 }
184 else
185 {
186 return "LinkLocal";
187 }
188 }
189 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
190 {
191 if (isIPv4)
192 {
193 return "DHCP";
194 }
195 else
196 {
197 return "DHCPv6";
198 }
199 }
200 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
201 {
202 return "SLAAC";
203 }
204 return "";
205}
206
Ed Tanous4c9afe42019-05-03 16:59:57 -0700207inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 const GetManagedObjects &dbus_data,
209 EthernetInterfaceData &ethData)
210{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700211 bool idFound = false;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 for (const auto &objpath : dbus_data)
213 {
Ed Tanous029573d2019-02-01 10:57:49 -0800214 for (const auto &ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 {
Ed Tanous029573d2019-02-01 10:57:49 -0800216 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700218 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700224 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700225 const std::string *mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800226 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700227 if (mac != nullptr)
228 {
229 ethData.mac_address = *mac;
230 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700232 }
233 }
234 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
235 {
236 for (const auto &propertyPair : ifacePair.second)
237 {
238 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800240 const uint32_t *id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800241 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700242 if (id != nullptr)
243 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500244 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700245 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700246 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700247 }
248 }
249 else if (ifacePair.first ==
250 "xyz.openbmc_project.Network.EthernetInterface")
251 {
252 for (const auto &propertyPair : ifacePair.second)
253 {
254 if (propertyPair.first == "AutoNeg")
255 {
256 const bool *auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800257 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700258 if (auto_neg != nullptr)
259 {
260 ethData.auto_neg = *auto_neg;
261 }
262 }
263 else if (propertyPair.first == "Speed")
264 {
265 const uint32_t *speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800266 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700267 if (speed != nullptr)
268 {
269 ethData.speed = *speed;
270 }
271 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800272 else if (propertyPair.first == "LinkUp")
273 {
274 const bool *linkUp =
275 std::get_if<bool>(&propertyPair.second);
276 if (linkUp != nullptr)
277 {
278 ethData.linkUp = *linkUp;
279 }
280 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700281 else if (propertyPair.first == "NICEnabled")
282 {
283 const bool *nicEnabled =
284 std::get_if<bool>(&propertyPair.second);
285 if (nicEnabled != nullptr)
286 {
287 ethData.nicEnabled = *nicEnabled;
288 }
289 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500290 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700291 {
Ed Tanous029573d2019-02-01 10:57:49 -0800292 const std::vector<std::string> *nameservers =
293 sdbusplus::message::variant_ns::get_if<
294 std::vector<std::string>>(
295 &propertyPair.second);
296 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700297 {
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -0500298 ethData.nameServers = std::move(*nameservers);
299 }
300 }
301 else if (propertyPair.first == "StaticNameServers")
302 {
303 const std::vector<std::string> *staticNameServers =
304 sdbusplus::message::variant_ns::get_if<
305 std::vector<std::string>>(
306 &propertyPair.second);
307 if (staticNameServers != nullptr)
308 {
309 ethData.staticNameServers =
310 std::move(*staticNameServers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700311 }
312 }
manojkiraneda2a133282019-02-19 13:09:43 +0530313 else if (propertyPair.first == "DHCPEnabled")
314 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700315 const std::string *DHCPEnabled =
316 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530317 if (DHCPEnabled != nullptr)
318 {
319 ethData.DHCPEnabled = *DHCPEnabled;
320 }
321 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800322 else if (propertyPair.first == "DomainName")
323 {
324 const std::vector<std::string> *domainNames =
325 sdbusplus::message::variant_ns::get_if<
326 std::vector<std::string>>(
327 &propertyPair.second);
328 if (domainNames != nullptr)
329 {
330 ethData.domainnames = std::move(*domainNames);
331 }
332 }
Ed Tanous029573d2019-02-01 10:57:49 -0800333 }
334 }
335 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700336
337 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
338 {
339 if (ifacePair.first ==
340 "xyz.openbmc_project.Network.DHCPConfiguration")
341 {
342 for (const auto &propertyPair : ifacePair.second)
343 {
344 if (propertyPair.first == "DNSEnabled")
345 {
346 const bool *DNSEnabled =
347 std::get_if<bool>(&propertyPair.second);
348 if (DNSEnabled != nullptr)
349 {
350 ethData.DNSEnabled = *DNSEnabled;
351 }
352 }
353 else if (propertyPair.first == "NTPEnabled")
354 {
355 const bool *NTPEnabled =
356 std::get_if<bool>(&propertyPair.second);
357 if (NTPEnabled != nullptr)
358 {
359 ethData.NTPEnabled = *NTPEnabled;
360 }
361 }
362 else if (propertyPair.first == "HostNameEnabled")
363 {
364 const bool *HostNameEnabled =
365 std::get_if<bool>(&propertyPair.second);
366 if (HostNameEnabled != nullptr)
367 {
368 ethData.HostNameEnabled = *HostNameEnabled;
369 }
370 }
371 else if (propertyPair.first == "SendHostNameEnabled")
372 {
373 const bool *SendHostNameEnabled =
374 std::get_if<bool>(&propertyPair.second);
375 if (SendHostNameEnabled != nullptr)
376 {
377 ethData.SendHostNameEnabled =
378 *SendHostNameEnabled;
379 }
380 }
381 }
382 }
383 }
Ed Tanous029573d2019-02-01 10:57:49 -0800384 // System configuration shows up in the global namespace, so no need
385 // to check eth number
386 if (ifacePair.first ==
387 "xyz.openbmc_project.Network.SystemConfiguration")
388 {
389 for (const auto &propertyPair : ifacePair.second)
390 {
391 if (propertyPair.first == "HostName")
392 {
393 const std::string *hostname =
394 sdbusplus::message::variant_ns::get_if<std::string>(
395 &propertyPair.second);
396 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700397 {
Ed Tanous029573d2019-02-01 10:57:49 -0800398 ethData.hostname = *hostname;
399 }
400 }
401 else if (propertyPair.first == "DefaultGateway")
402 {
403 const std::string *defaultGateway =
404 sdbusplus::message::variant_ns::get_if<std::string>(
405 &propertyPair.second);
406 if (defaultGateway != nullptr)
407 {
408 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700409 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500411 else if (propertyPair.first == "DefaultGateway6")
412 {
413 const std::string *defaultGateway6 =
414 sdbusplus::message::variant_ns::get_if<std::string>(
415 &propertyPair.second);
416 if (defaultGateway6 != nullptr)
417 {
418 ethData.ipv6_default_gateway = *defaultGateway6;
419 }
420 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700421 }
422 }
423 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700424 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700425 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700426}
427
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500428// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700429inline void
430 extractIPV6Data(const std::string &ethiface_id,
431 const GetManagedObjects &dbus_data,
432 boost::container::flat_set<IPv6AddressData> &ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500433{
434 const std::string ipv6PathStart =
435 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
436
437 // Since there might be several IPv6 configurations aligned with
438 // single ethernet interface, loop over all of them
439 for (const auto &objpath : dbus_data)
440 {
441 // Check if proper pattern for object path appears
442 if (boost::starts_with(objpath.first.str, ipv6PathStart))
443 {
444 for (auto &interface : objpath.second)
445 {
446 if (interface.first == "xyz.openbmc_project.Network.IP")
447 {
448 // Instance IPv6AddressData structure, and set as
449 // appropriate
450 std::pair<
451 boost::container::flat_set<IPv6AddressData>::iterator,
452 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700453 it = ipv6_config.insert(IPv6AddressData{});
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500454 IPv6AddressData &ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700455 ipv6_address.id =
456 objpath.first.str.substr(ipv6PathStart.size());
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500457 for (auto &property : interface.second)
458 {
459 if (property.first == "Address")
460 {
461 const std::string *address =
462 std::get_if<std::string>(&property.second);
463 if (address != nullptr)
464 {
465 ipv6_address.address = *address;
466 }
467 }
468 else if (property.first == "Origin")
469 {
470 const std::string *origin =
471 std::get_if<std::string>(&property.second);
472 if (origin != nullptr)
473 {
474 ipv6_address.origin =
475 translateAddressOriginDbusToRedfish(*origin,
476 false);
477 }
478 }
479 else if (property.first == "PrefixLength")
480 {
481 const uint8_t *prefix =
482 std::get_if<uint8_t>(&property.second);
483 if (prefix != nullptr)
484 {
485 ipv6_address.prefixLength = *prefix;
486 }
487 }
488 else
489 {
490 BMCWEB_LOG_ERROR
491 << "Got extra property: " << property.first
492 << " on the " << objpath.first.str << " object";
493 }
494 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500495 }
496 }
497 }
498 }
499}
500
Ed Tanous4a0cb852018-10-15 07:55:04 -0700501// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700502inline void
503 extractIPData(const std::string &ethiface_id,
504 const GetManagedObjects &dbus_data,
505 boost::container::flat_set<IPv4AddressData> &ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700506{
507 const std::string ipv4PathStart =
508 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
509
510 // Since there might be several IPv4 configurations aligned with
511 // single ethernet interface, loop over all of them
512 for (const auto &objpath : dbus_data)
513 {
514 // Check if proper pattern for object path appears
515 if (boost::starts_with(objpath.first.str, ipv4PathStart))
516 {
517 for (auto &interface : objpath.second)
518 {
519 if (interface.first == "xyz.openbmc_project.Network.IP")
520 {
521 // Instance IPv4AddressData structure, and set as
522 // appropriate
523 std::pair<
524 boost::container::flat_set<IPv4AddressData>::iterator,
525 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700526 it = ipv4_config.insert(IPv4AddressData{});
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 IPv4AddressData &ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700528 ipv4_address.id =
529 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous4a0cb852018-10-15 07:55:04 -0700530 for (auto &property : interface.second)
531 {
532 if (property.first == "Address")
533 {
534 const std::string *address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800535 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700536 if (address != nullptr)
537 {
538 ipv4_address.address = *address;
539 }
540 }
541 else if (property.first == "Gateway")
542 {
543 const std::string *gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800544 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700545 if (gateway != nullptr)
546 {
547 ipv4_address.gateway = *gateway;
548 }
549 }
550 else if (property.first == "Origin")
551 {
552 const std::string *origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800553 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700554 if (origin != nullptr)
555 {
556 ipv4_address.origin =
557 translateAddressOriginDbusToRedfish(*origin,
558 true);
559 }
560 }
561 else if (property.first == "PrefixLength")
562 {
563 const uint8_t *mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800564 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700565 if (mask != nullptr)
566 {
567 // convert it to the string
568 ipv4_address.netmask = getNetmask(*mask);
569 }
570 }
571 else
572 {
573 BMCWEB_LOG_ERROR
574 << "Got extra property: " << property.first
575 << " on the " << objpath.first.str << " object";
576 }
577 }
578 // Check if given address is local, or global
579 ipv4_address.linktype =
580 boost::starts_with(ipv4_address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700581 ? LinkType::Local
582 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700583 }
584 }
585 }
586 }
587}
588
589/**
590 * @brief Sets given Id on the given VLAN interface through D-Bus
591 *
592 * @param[in] ifaceId Id of VLAN interface that should be modified
593 * @param[in] inputVlanId New ID of the VLAN
594 * @param[in] callback Function that will be called after the operation
595 *
596 * @return None.
597 */
598template <typename CallbackFunc>
599void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
600 CallbackFunc &&callback)
601{
602 crow::connections::systemBus->async_method_call(
603 callback, "xyz.openbmc_project.Network",
604 std::string("/xyz/openbmc_project/network/") + ifaceId,
605 "org.freedesktop.DBus.Properties", "Set",
606 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800607 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700608}
609
610/**
611 * @brief Helper function that verifies IP address to check if it is in
612 * proper format. If bits pointer is provided, also calculates active
613 * bit count for Subnet Mask.
614 *
615 * @param[in] ip IP that will be verified
616 * @param[out] bits Calculated mask in bits notation
617 *
618 * @return true in case of success, false otherwise
619 */
620inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
621 uint8_t *bits = nullptr)
622{
623 std::vector<std::string> bytesInMask;
624
625 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626
627 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700628 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 return false;
631 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632
Ed Tanous4a0cb852018-10-15 07:55:04 -0700633 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 *bits = 0;
636 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 char *endPtr;
639 long previousValue = 255;
640 bool firstZeroInByteHit;
641 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700643 if (byte.empty())
644 {
645 return false;
646 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Use strtol instead of stroi to avoid exceptions
649 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 // endPtr should point to the end of the string, otherwise given string
652 // is not 100% number
653 if (*endPtr != '\0')
654 {
655 return false;
656 }
657
658 // Value should be contained in byte
659 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 {
661 return false;
662 }
663
664 if (bits != nullptr)
665 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700666 // Mask has to be continuous between bytes
667 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700668 {
669 return false;
670 }
671
Ed Tanous4a0cb852018-10-15 07:55:04 -0700672 // Mask has to be continuous inside bytes
673 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674
Ed Tanous4a0cb852018-10-15 07:55:04 -0700675 // Count bits
676 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700678 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700680 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700682 // Continuity not preserved
683 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 }
685 else
686 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700687 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 }
689 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 else
691 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700692 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700694 }
695 }
696
697 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 }
699
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700 return true;
701}
702
703/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700704 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700705 *
706 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 * @param[in] ipHash DBus Hash id of IP that should be deleted
708 * @param[io] asyncResp Response object that will be returned to client
709 *
710 * @return None
711 */
712inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700713 const std::shared_ptr<AsyncResp> asyncResp)
714{
715 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700716 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700717 if (ec)
718 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800719 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100720 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700721 },
722 "xyz.openbmc_project.Network",
723 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
724 "xyz.openbmc_project.Object.Delete", "Delete");
725}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700726
Ed Tanous4a0cb852018-10-15 07:55:04 -0700727/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700728 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700729 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700730 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
731 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
732 * @param[in] gateway IPv4 address of this interfaces gateway
733 * @param[in] address IPv4 address to assign to this interface
734 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700735 *
736 * @return None
737 */
Ed Tanousb01bf292019-03-25 19:25:26 +0000738inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
Johnathan Mantey01784822019-06-18 12:44:21 -0700739 uint8_t prefixLength, const std::string &gateway,
Ed Tanousb01bf292019-03-25 19:25:26 +0000740 const std::string &address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 std::shared_ptr<AsyncResp> asyncResp)
742{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700743 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700744 [asyncResp](const boost::system::error_code ec) {
745 if (ec)
746 {
747 messages::internalError(asyncResp->res);
748 }
749 },
750 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700751 "/xyz/openbmc_project/network/" + ifaceId,
752 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700753 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700754 gateway);
755}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500756
757/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700758 * @brief Deletes the IPv4 entry for this interface and creates a replacement
759 * static IPv4 entry
760 *
761 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
762 * @param[in] id The unique hash entry identifying the DBus entry
763 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
764 * @param[in] gateway IPv4 address of this interfaces gateway
765 * @param[in] address IPv4 address to assign to this interface
766 * @param[io] asyncResp Response object that will be returned to client
767 *
768 * @return None
769 */
770inline void deleteAndCreateIPv4(const std::string &ifaceId,
771 const std::string &id, uint8_t prefixLength,
772 const std::string &gateway,
773 const std::string &address,
774 std::shared_ptr<AsyncResp> asyncResp)
775{
776 crow::connections::systemBus->async_method_call(
777 [asyncResp, ifaceId, address, prefixLength,
778 gateway](const boost::system::error_code ec) {
779 if (ec)
780 {
781 messages::internalError(asyncResp->res);
782 }
783 crow::connections::systemBus->async_method_call(
784 [asyncResp](const boost::system::error_code ec) {
785 if (ec)
786 {
787 messages::internalError(asyncResp->res);
788 }
789 },
790 "xyz.openbmc_project.Network",
791 "/xyz/openbmc_project/network/" + ifaceId,
792 "xyz.openbmc_project.Network.IP.Create", "IP",
793 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
794 prefixLength, gateway);
795 },
796 "xyz.openbmc_project.Network",
797 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
798 "xyz.openbmc_project.Object.Delete", "Delete");
799}
800
801/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500802 * @brief Deletes given IPv6
803 *
804 * @param[in] ifaceId Id of interface whose IP should be deleted
805 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500806 * @param[io] asyncResp Response object that will be returned to client
807 *
808 * @return None
809 */
810inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500811 const std::shared_ptr<AsyncResp> asyncResp)
812{
813 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700814 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500815 if (ec)
816 {
817 messages::internalError(asyncResp->res);
818 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500819 },
820 "xyz.openbmc_project.Network",
821 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
822 "xyz.openbmc_project.Object.Delete", "Delete");
823}
824
825/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700826 * @brief Deletes the IPv6 entry for this interface and creates a replacement
827 * static IPv6 entry
828 *
829 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
830 * @param[in] id The unique hash entry identifying the DBus entry
831 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
832 * @param[in] address IPv6 address to assign to this interface
833 * @param[io] asyncResp Response object that will be returned to client
834 *
835 * @return None
836 */
837inline void deleteAndCreateIPv6(const std::string &ifaceId,
838 const std::string &id, uint8_t prefixLength,
839 const std::string &address,
840 std::shared_ptr<AsyncResp> asyncResp)
841{
842 crow::connections::systemBus->async_method_call(
843 [asyncResp, ifaceId, address,
844 prefixLength](const boost::system::error_code ec) {
845 if (ec)
846 {
847 messages::internalError(asyncResp->res);
848 }
849 crow::connections::systemBus->async_method_call(
850 [asyncResp](const boost::system::error_code ec) {
851 if (ec)
852 {
853 messages::internalError(asyncResp->res);
854 }
855 },
856 "xyz.openbmc_project.Network",
857 "/xyz/openbmc_project/network/" + ifaceId,
858 "xyz.openbmc_project.Network.IP.Create", "IP",
859 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
860 prefixLength, "");
861 },
862 "xyz.openbmc_project.Network",
863 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
864 "xyz.openbmc_project.Object.Delete", "Delete");
865}
866
867/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500868 * @brief Creates IPv6 with given data
869 *
870 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500871 * @param[in] prefixLength Prefix length that needs to be added
872 * @param[in] address IP address that needs to be added
873 * @param[io] asyncResp Response object that will be returned to client
874 *
875 * @return None
876 */
Johnathan Mantey01784822019-06-18 12:44:21 -0700877inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
878 const std::string &address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500879 std::shared_ptr<AsyncResp> asyncResp)
880{
881 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
882 if (ec)
883 {
884 messages::internalError(asyncResp->res);
885 }
886 };
887 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
888 // does not have assosiated gateway property
889 crow::connections::systemBus->async_method_call(
890 std::move(createIpHandler), "xyz.openbmc_project.Network",
891 "/xyz/openbmc_project/network/" + ifaceId,
892 "xyz.openbmc_project.Network.IP.Create", "IP",
893 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
894 "");
895}
896
Ed Tanous4a0cb852018-10-15 07:55:04 -0700897/**
898 * Function that retrieves all properties for given Ethernet Interface
899 * Object
900 * from EntityManager Network Manager
901 * @param ethiface_id a eth interface id to query on DBus
902 * @param callback a function that shall be called to convert Dbus output
903 * into JSON
904 */
905template <typename CallbackFunc>
906void getEthernetIfaceData(const std::string &ethiface_id,
907 CallbackFunc &&callback)
908{
909 crow::connections::systemBus->async_method_call(
910 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
911 const boost::system::error_code error_code,
912 const GetManagedObjects &resp) {
913 EthernetInterfaceData ethData{};
914 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500915 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700916
917 if (error_code)
918 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700919 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700920 return;
921 }
922
Ed Tanous4c9afe42019-05-03 16:59:57 -0700923 bool found =
924 extractEthernetInterfaceData(ethiface_id, resp, ethData);
925 if (!found)
926 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700927 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700928 return;
929 }
930
Johnathan Mantey01784822019-06-18 12:44:21 -0700931 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700932 // Fix global GW
933 for (IPv4AddressData &ipv4 : ipv4Data)
934 {
Ravi Tejac6191412019-07-30 00:53:50 -0500935 if (((ipv4.linktype == LinkType::Global) &&
936 (ipv4.gateway == "0.0.0.0")) ||
937 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700939 ipv4.gateway = ethData.default_gateway;
940 }
941 }
942
Johnathan Mantey01784822019-06-18 12:44:21 -0700943 extractIPV6Data(ethiface_id, resp, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700944 // Finally make a callback with usefull data
Johnathan Mantey01784822019-06-18 12:44:21 -0700945 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700946 },
947 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
948 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700949}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700950
951/**
952 * Function that retrieves all Ethernet Interfaces available through Network
953 * Manager
954 * @param callback a function that shall be called to convert Dbus output
955 * into JSON.
956 */
957template <typename CallbackFunc>
958void getEthernetIfaceList(CallbackFunc &&callback)
959{
960 crow::connections::systemBus->async_method_call(
961 [callback{std::move(callback)}](
962 const boost::system::error_code error_code,
963 GetManagedObjects &resp) {
964 // Callback requires vector<string> to retrieve all available
965 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700966 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 iface_list.reserve(resp.size());
968 if (error_code)
969 {
970 callback(false, iface_list);
971 return;
972 }
973
974 // Iterate over all retrieved ObjectPaths.
975 for (const auto &objpath : resp)
976 {
977 // And all interfaces available for certain ObjectPath.
978 for (const auto &interface : objpath.second)
979 {
980 // If interface is
981 // xyz.openbmc_project.Network.EthernetInterface, this is
982 // what we're looking for.
983 if (interface.first ==
984 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700985 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986 // Cut out everyting until last "/", ...
987 const std::string &iface_id = objpath.first.str;
988 std::size_t last_pos = iface_id.rfind("/");
989 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700992 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 }
994 }
995 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700996 }
997 // Finally make a callback with useful data
998 callback(true, iface_list);
999 },
1000 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
1001 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -07001002}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001003
1004/**
1005 * EthernetCollection derived class for delivering Ethernet Collection Schema
1006 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007class EthernetCollection : public Node
1008{
1009 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -07001010 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001012 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001013 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001014 entityPrivileges = {
1015 {boost::beast::http::verb::get, {{"Login"}}},
1016 {boost::beast::http::verb::head, {{"Login"}}},
1017 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1018 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1019 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1020 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1021 }
1022
1023 private:
1024 /**
1025 * Functions triggers appropriate requests on DBus
1026 */
1027 void doGet(crow::Response &res, const crow::Request &req,
1028 const std::vector<std::string> &params) override
1029 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001030 res.jsonValue["@odata.type"] =
1031 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001032 res.jsonValue["@odata.id"] =
1033 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1034 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1035 res.jsonValue["Description"] =
1036 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001037 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001038 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001039 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001040 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001041 [asyncResp](
1042 const bool &success,
1043 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001044 if (!success)
1045 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001046 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001047 return;
1048 }
1049
Ed Tanous4c9afe42019-05-03 16:59:57 -07001050 nlohmann::json &iface_array =
1051 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001052 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001053 std::string tag = "_";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001054 for (const std::string &iface_item : iface_list)
1055 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001056 std::size_t found = iface_item.find(tag);
1057 if (found == std::string::npos)
1058 {
1059 iface_array.push_back(
1060 {{"@odata.id",
1061 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1062 iface_item}});
1063 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001064 }
1065
Ed Tanous4c9afe42019-05-03 16:59:57 -07001066 asyncResp->res.jsonValue["Members@odata.count"] =
1067 iface_array.size();
1068 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001069 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001070 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001071 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001072};
1073
1074/**
1075 * EthernetInterface derived class for delivering Ethernet Schema
1076 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077class EthernetInterface : public Node
1078{
1079 public:
1080 /*
1081 * Default Constructor
1082 */
Ed Tanous4a0cb852018-10-15 07:55:04 -07001083 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001085 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001086 std::string())
1087 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001088 entityPrivileges = {
1089 {boost::beast::http::verb::get, {{"Login"}}},
1090 {boost::beast::http::verb::head, {{"Login"}}},
1091 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1092 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1093 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1094 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001095 }
1096
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001098 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001099 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001100 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301101 // SHOULD handle host names of up to 255 characters(RFC 1123)
1102 if (hostname.length() > 255)
1103 {
1104 messages::propertyValueFormatError(asyncResp->res, hostname,
1105 "HostName");
1106 return;
1107 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001108 crow::connections::systemBus->async_method_call(
1109 [asyncResp](const boost::system::error_code ec) {
1110 if (ec)
1111 {
1112 messages::internalError(asyncResp->res);
1113 }
1114 },
1115 "xyz.openbmc_project.Network",
1116 "/xyz/openbmc_project/network/config",
1117 "org.freedesktop.DBus.Properties", "Set",
1118 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001119 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001120 }
1121
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301122 void handleDomainnamePatch(const std::string &ifaceId,
1123 const std::string &domainname,
1124 const std::shared_ptr<AsyncResp> asyncResp)
1125 {
1126 std::vector<std::string> vectorDomainname = {domainname};
1127 crow::connections::systemBus->async_method_call(
1128 [asyncResp](const boost::system::error_code ec) {
1129 if (ec)
1130 {
1131 messages::internalError(asyncResp->res);
1132 }
1133 },
1134 "xyz.openbmc_project.Network",
1135 "/xyz/openbmc_project/network/" + ifaceId,
1136 "org.freedesktop.DBus.Properties", "Set",
1137 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1138 std::variant<std::vector<std::string>>(vectorDomainname));
1139 }
1140
1141 void handleFqdnPatch(const std::string &ifaceId, const std::string &fqdn,
1142 const std::shared_ptr<AsyncResp> asyncResp)
1143 {
1144 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1145 if (fqdn.length() > 255)
1146 {
1147 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1148 return;
1149 }
1150
1151 size_t pos = fqdn.find('.');
1152 if (pos == std::string::npos)
1153 {
1154 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1155 return;
1156 }
1157
1158 std::string hostname;
1159 std::string domainname;
1160 domainname = (fqdn).substr(pos + 1);
1161 hostname = (fqdn).substr(0, pos);
1162
1163 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1164 {
1165 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1166 return;
1167 }
1168
1169 handleHostnamePatch(hostname, asyncResp);
1170 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1171 }
1172
1173 bool isHostnameValid(const std::string &hostname)
1174 {
1175 // A valid host name can never have the dotted-decimal form (RFC 1123)
1176 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1177 {
1178 return false;
1179 }
1180 // Each label(hostname/subdomains) within a valid FQDN
1181 // MUST handle host names of up to 63 characters (RFC 1123)
1182 // labels cannot start or end with hyphens (RFC 952)
1183 // labels can start with numbers (RFC 1123)
1184 const std::regex pattern(
1185 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1186
1187 return std::regex_match(hostname, pattern);
1188 }
1189
1190 bool isDomainnameValid(const std::string &domainname)
1191 {
1192 // Can have multiple subdomains
1193 // Top Level Domain's min length is 2 character
1194 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1195 "{1,30}\\.)*[a-zA-Z]{2,}$");
1196
1197 return std::regex_match(domainname, pattern);
1198 }
1199
Ratan Guptad5776652019-03-03 08:47:22 +05301200 void handleMACAddressPatch(const std::string &ifaceId,
1201 const std::string &macAddress,
1202 const std::shared_ptr<AsyncResp> &asyncResp)
1203 {
1204 crow::connections::systemBus->async_method_call(
1205 [asyncResp, macAddress](const boost::system::error_code ec) {
1206 if (ec)
1207 {
1208 messages::internalError(asyncResp->res);
1209 return;
1210 }
Ratan Guptad5776652019-03-03 08:47:22 +05301211 },
1212 "xyz.openbmc_project.Network",
1213 "/xyz/openbmc_project/network/" + ifaceId,
1214 "org.freedesktop.DBus.Properties", "Set",
1215 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1216 std::variant<std::string>(macAddress));
1217 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001218
Jennifer Leeda131a92019-04-24 15:13:55 -07001219 void setDHCPEnabled(const std::string &ifaceId,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001220 const std::string &propertyName, const bool v4Value,
1221 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001222 const std::shared_ptr<AsyncResp> asyncResp)
1223 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001224 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001225 crow::connections::systemBus->async_method_call(
1226 [asyncResp](const boost::system::error_code ec) {
1227 if (ec)
1228 {
1229 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1230 messages::internalError(asyncResp->res);
1231 return;
1232 }
1233 },
1234 "xyz.openbmc_project.Network",
1235 "/xyz/openbmc_project/network/" + ifaceId,
1236 "org.freedesktop.DBus.Properties", "Set",
1237 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001238 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001239 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001240
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001241 void setEthernetInterfaceBoolProperty(
1242 const std::string &ifaceId, const std::string &propertyName,
1243 const bool &value, const std::shared_ptr<AsyncResp> asyncResp)
1244 {
1245 crow::connections::systemBus->async_method_call(
1246 [asyncResp](const boost::system::error_code ec) {
1247 if (ec)
1248 {
1249 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1250 messages::internalError(asyncResp->res);
1251 return;
1252 }
1253 },
1254 "xyz.openbmc_project.Network",
1255 "/xyz/openbmc_project/network/" + ifaceId,
1256 "org.freedesktop.DBus.Properties", "Set",
1257 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1258 std::variant<bool>{value});
1259 }
1260
Jennifer Leeda131a92019-04-24 15:13:55 -07001261 void setDHCPv4Config(const std::string &propertyName, const bool &value,
1262 const std::shared_ptr<AsyncResp> asyncResp)
1263 {
1264 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1265 crow::connections::systemBus->async_method_call(
1266 [asyncResp](const boost::system::error_code ec) {
1267 if (ec)
1268 {
1269 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1270 messages::internalError(asyncResp->res);
1271 return;
1272 }
1273 },
1274 "xyz.openbmc_project.Network",
1275 "/xyz/openbmc_project/network/config/dhcp",
1276 "org.freedesktop.DBus.Properties", "Set",
1277 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1278 std::variant<bool>{value});
1279 }
Ratan Guptad5776652019-03-03 08:47:22 +05301280
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001281 void handleDHCPPatch(const std::string &ifaceId,
1282 const EthernetInterfaceData &ethData,
1283 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1284 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001285 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001286 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1287 bool ipv6Active =
1288 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001289
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001290 bool nextv4DHCPState =
1291 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1292
1293 bool nextv6DHCPState{};
1294 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001295 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001296 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1297 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1298 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1299 {
1300 messages::propertyValueFormatError(
1301 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1302 "OperatingMode");
1303 return;
1304 }
1305 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1306 }
1307 else
1308 {
1309 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001310 }
1311
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001312 bool nextDNS{};
1313 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001314 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001315 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1316 {
1317 messages::generalError(asyncResp->res);
1318 return;
1319 }
1320 nextDNS = *v4dhcpParms.useDNSServers;
1321 }
1322 else if (v4dhcpParms.useDNSServers)
1323 {
1324 nextDNS = *v4dhcpParms.useDNSServers;
1325 }
1326 else if (v6dhcpParms.useDNSServers)
1327 {
1328 nextDNS = *v6dhcpParms.useDNSServers;
1329 }
1330 else
1331 {
1332 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001333 }
1334
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001335 bool nextNTP{};
1336 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001337 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001338 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1339 {
1340 messages::generalError(asyncResp->res);
1341 return;
1342 }
1343 nextNTP = *v4dhcpParms.useNTPServers;
1344 }
1345 else if (v4dhcpParms.useNTPServers)
1346 {
1347 nextNTP = *v4dhcpParms.useNTPServers;
1348 }
1349 else if (v6dhcpParms.useNTPServers)
1350 {
1351 nextNTP = *v6dhcpParms.useNTPServers;
1352 }
1353 else
1354 {
1355 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001356 }
1357
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001358 bool nextUseDomain{};
1359 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001360 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001361 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1362 {
1363 messages::generalError(asyncResp->res);
1364 return;
1365 }
1366 nextUseDomain = *v4dhcpParms.useUseDomainName;
1367 }
1368 else if (v4dhcpParms.useUseDomainName)
1369 {
1370 nextUseDomain = *v4dhcpParms.useUseDomainName;
1371 }
1372 else if (v6dhcpParms.useUseDomainName)
1373 {
1374 nextUseDomain = *v6dhcpParms.useUseDomainName;
1375 }
1376 else
1377 {
1378 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001379 }
1380
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001381 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1382 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1383 asyncResp);
1384 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1385 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1386 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1387 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1388 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1389 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001390 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001391
1392 boost::container::flat_set<IPv4AddressData>::const_iterator
1393 GetNextStaticIPEntry(
1394 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1395 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1396 {
1397 for (; head != end; head++)
1398 {
1399 if (head->origin == "Static")
1400 {
1401 return head;
1402 }
1403 }
1404 return end;
1405 }
1406
1407 boost::container::flat_set<IPv6AddressData>::const_iterator
1408 GetNextStaticIPEntry(
1409 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1410 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1411 {
1412 for (; head != end; head++)
1413 {
1414 if (head->origin == "Static")
1415 {
1416 return head;
1417 }
1418 }
1419 return end;
1420 }
1421
Ravi Tejad1d50812019-06-23 16:20:27 -05001422 void handleIPv4StaticPatch(
Ratan Guptaf476acb2019-03-02 16:46:57 +05301423 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001424 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001425 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001427 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301428 {
1429 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001430 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301431 return;
1432 }
1433
Ed Tanous271584a2019-07-09 16:24:22 -07001434 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001435 // Find the first static IP address currently active on the NIC and
1436 // match it to the first JSON element in the IPv4StaticAddresses array.
1437 // Match each subsequent JSON element to the next static IP programmed
1438 // into the NIC.
1439 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1440 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1441
Ed Tanous537174c2018-12-10 15:09:31 -08001442 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001443 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001444 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001445 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001446
Johnathan Mantey01784822019-06-18 12:44:21 -07001447 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301448 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001449 std::optional<std::string> address;
1450 std::optional<std::string> subnetMask;
1451 std::optional<std::string> gateway;
1452
1453 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1454 address, "SubnetMask", subnetMask,
1455 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301456 {
1457 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001458 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301459 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301460 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301461
Johnathan Mantey01784822019-06-18 12:44:21 -07001462 // Find the address/subnet/gateway values. Any values that are
1463 // not explicitly provided are assumed to be unmodified from the
1464 // current state of the interface. Merge existing state into the
1465 // current request.
Ed Tanous271584a2019-07-09 16:24:22 -07001466 const std::string *addr = nullptr;
1467 const std::string *gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001468 uint8_t prefixLength = 0;
1469 bool errorInEntry = false;
1470 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301471 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001472 if (ipv4VerifyIpAndGetBitcount(*address))
1473 {
1474 addr = &(*address);
1475 }
1476 else
1477 {
1478 messages::propertyValueFormatError(
1479 asyncResp->res, *address, pathString + "/Address");
1480 errorInEntry = true;
1481 }
1482 }
1483 else if (NICIPentry != ipv4Data.cend())
1484 {
1485 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301486 }
1487 else
1488 {
1489 messages::propertyMissing(asyncResp->res,
1490 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001491 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001492 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301493
1494 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001495 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001496 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1497 {
1498 messages::propertyValueFormatError(
1499 asyncResp->res, *subnetMask,
1500 pathString + "/SubnetMask");
1501 errorInEntry = true;
1502 }
1503 }
1504 else if (NICIPentry != ipv4Data.cend())
1505 {
1506 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1507 &prefixLength))
1508 {
1509 messages::propertyValueFormatError(
1510 asyncResp->res, NICIPentry->netmask,
1511 pathString + "/SubnetMask");
1512 errorInEntry = true;
1513 }
1514 }
1515 else
1516 {
1517 messages::propertyMissing(asyncResp->res,
1518 pathString + "/SubnetMask");
1519 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001520 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301521
Ratan Guptaf476acb2019-03-02 16:46:57 +05301522 if (gateway)
1523 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001524 if (ipv4VerifyIpAndGetBitcount(*gateway))
1525 {
1526 gw = &(*gateway);
1527 }
1528 else
1529 {
1530 messages::propertyValueFormatError(
1531 asyncResp->res, *gateway, pathString + "/Gateway");
1532 errorInEntry = true;
1533 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301534 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001535 else if (NICIPentry != ipv4Data.cend())
1536 {
1537 gw = &NICIPentry->gateway;
1538 }
1539 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001540 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001541 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001542 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001543 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001544 }
1545
Johnathan Mantey01784822019-06-18 12:44:21 -07001546 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001547 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001548 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001549 }
1550
Johnathan Mantey01784822019-06-18 12:44:21 -07001551 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001552 {
Ed Tanous271584a2019-07-09 16:24:22 -07001553 if (gw != nullptr || addr != nullptr)
1554 {
1555 // Shouldn't be possible based on errorInEntry, but
1556 // it flags -wmaybe-uninitialized in the compiler,
1557 // so defend against that
1558 return;
1559 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001560 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1561 *gw, *addr, asyncResp);
1562 NICIPentry =
1563 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001564 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001565 else
1566 {
1567 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1568 *address, asyncResp);
1569 }
1570 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001571 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001572 else
1573 {
1574 if (NICIPentry == ipv4Data.cend())
1575 {
1576 // Requesting a DELETE/DO NOT MODIFY action for an item
1577 // that isn't present on the eth(n) interface. Input JSON is
1578 // in error, so bail out.
1579 if (thisJson.is_null())
1580 {
1581 messages::resourceCannotBeDeleted(asyncResp->res);
1582 return;
1583 }
1584 else
1585 {
1586 messages::propertyValueFormatError(
1587 asyncResp->res, thisJson.dump(), pathString);
1588 return;
1589 }
1590 }
1591
1592 if (thisJson.is_null())
1593 {
1594 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1595 }
1596 if (NICIPentry != ipv4Data.cend())
1597 {
1598 NICIPentry =
1599 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1600 }
1601 entryIdx++;
1602 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001603 }
1604 }
1605
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001606 void handleStaticNameServersPatch(
1607 const std::string &ifaceId,
1608 const std::vector<std::string> &updatedStaticNameServers,
1609 const std::shared_ptr<AsyncResp> &asyncResp)
1610 {
1611 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001612 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001613 if (ec)
1614 {
1615 messages::internalError(asyncResp->res);
1616 return;
1617 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001618 },
1619 "xyz.openbmc_project.Network",
1620 "/xyz/openbmc_project/network/" + ifaceId,
1621 "org.freedesktop.DBus.Properties", "Set",
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001622 "xyz.openbmc_project.Network.EthernetInterface",
1623 "StaticNameServers",
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001624 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1625 }
1626
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001627 void handleIPv6StaticAddressesPatch(
1628 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001629 const boost::container::flat_set<IPv6AddressData> &ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001630 const std::shared_ptr<AsyncResp> asyncResp)
1631 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001632 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001633 {
1634 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1635 "IPv6StaticAddresses");
1636 return;
1637 }
Ed Tanous271584a2019-07-09 16:24:22 -07001638 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001639 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1640 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001641 for (nlohmann::json &thisJson : input)
1642 {
1643 std::string pathString =
1644 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1645
Johnathan Mantey01784822019-06-18 12:44:21 -07001646 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001647 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001648 std::optional<std::string> address;
1649 std::optional<uint8_t> prefixLength;
1650
1651 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1652 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001653 {
1654 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001655 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001656 return;
1657 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001658
Johnathan Mantey01784822019-06-18 12:44:21 -07001659 const std::string *addr;
1660 uint8_t prefix;
1661
1662 // Find the address and prefixLength values. Any values that are
1663 // not explicitly provided are assumed to be unmodified from the
1664 // current state of the interface. Merge existing state into the
1665 // current request.
1666 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001667 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001668 addr = &(*address);
1669 }
1670 else if (NICIPentry != ipv6Data.end())
1671 {
1672 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001673 }
1674 else
1675 {
1676 messages::propertyMissing(asyncResp->res,
1677 pathString + "/Address");
1678 return;
1679 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001680
1681 if (prefixLength)
1682 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001683 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001684 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001685 else if (NICIPentry != ipv6Data.end())
1686 {
1687 prefix = NICIPentry->prefixLength;
1688 }
1689 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001690 {
1691 messages::propertyMissing(asyncResp->res,
1692 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001693 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001694 }
1695
Johnathan Mantey01784822019-06-18 12:44:21 -07001696 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001697 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001698 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1699 asyncResp);
1700 NICIPentry =
1701 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1702 }
1703 else
1704 {
1705 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1706 }
1707 entryIdx++;
1708 }
1709 else
1710 {
1711 if (NICIPentry == ipv6Data.end())
1712 {
1713 // Requesting a DELETE/DO NOT MODIFY action for an item
1714 // that isn't present on the eth(n) interface. Input JSON is
1715 // in error, so bail out.
1716 if (thisJson.is_null())
1717 {
1718 messages::resourceCannotBeDeleted(asyncResp->res);
1719 return;
1720 }
1721 else
1722 {
1723 messages::propertyValueFormatError(
1724 asyncResp->res, thisJson.dump(), pathString);
1725 return;
1726 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001727 }
1728
Johnathan Mantey01784822019-06-18 12:44:21 -07001729 if (thisJson.is_null())
1730 {
1731 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1732 }
1733 if (NICIPentry != ipv6Data.cend())
1734 {
1735 NICIPentry =
1736 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1737 }
1738 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001739 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001740 }
1741 }
1742
Ed Tanous0f74e642018-11-12 15:17:05 -08001743 void parseInterfaceData(
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001744 std::shared_ptr<AsyncResp> asyncResp, const std::string &iface_id,
Ed Tanous0f74e642018-11-12 15:17:05 -08001745 const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001746 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001747 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001748 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001749 constexpr const std::array<const char *, 1> inventoryForEthernet = {
1750 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1751
1752 nlohmann::json &json_response = asyncResp->res.jsonValue;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001753 json_response["Id"] = iface_id;
1754 json_response["@odata.id"] =
1755 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001756 json_response["InterfaceEnabled"] = ethData.nicEnabled;
1757
1758 auto health = std::make_shared<HealthPopulate>(asyncResp);
1759
1760 crow::connections::systemBus->async_method_call(
1761 [health](const boost::system::error_code ec,
1762 std::vector<std::string> &resp) {
1763 if (ec)
1764 {
1765 return;
1766 }
1767
1768 health->inventory = std::move(resp);
1769 },
1770 "xyz.openbmc_project.ObjectMapper",
1771 "/xyz/openbmc_project/object_mapper",
1772 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1773 int32_t(0), inventoryForEthernet);
1774
1775 health->populate();
1776
1777 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001778 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001779 json_response["LinkStatus"] = "LinkUp";
1780 json_response["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001781 }
1782 else
1783 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001784 json_response["LinkStatus"] = "NoLink";
1785 json_response["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001786 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001787
1788 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001789 json_response["SpeedMbps"] = ethData.speed;
1790 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001791 json_response["DHCPv4"]["DHCPEnabled"] =
1792 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1793 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1794 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1795 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1796
1797 json_response["DHCPv6"]["OperatingMode"] =
1798 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1799 : "Disabled";
1800 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1801 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1802 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301803
Ed Tanous4a0cb852018-10-15 07:55:04 -07001804 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001805 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001806 json_response["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301807
1808 // When domain name is empty then it means, that it is a network
1809 // without domain names, and the host name itself must be treated as
1810 // FQDN
1811 std::string FQDN = std::move(ethData.hostname);
Jennifer Leed24bfc72019-03-05 13:03:37 -08001812 if (!ethData.domainnames.empty())
1813 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301814 FQDN += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001815 }
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301816 json_response["FQDN"] = FQDN;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001817 }
1818
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001819 json_response["VLANs"] = {
1820 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1821 iface_id + "/VLANs"}};
1822
manojkiran.eda@gmail.com0f6efdc2019-10-03 04:53:44 -05001823 json_response["NameServers"] = ethData.nameServers;
1824 json_response["StaticNameServers"] = ethData.staticNameServers;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001825
Ravi Tejad1d50812019-06-23 16:20:27 -05001826 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001827 nlohmann::json &ipv4_static_array =
1828 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001829 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001830 ipv4_static_array = nlohmann::json::array();
Ravi Tejad1d50812019-06-23 16:20:27 -05001831 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001832 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001833
1834 std::string gatewayStr = ipv4_config.gateway;
1835 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001836 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001837 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001838 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001839
1840 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1841 {"SubnetMask", ipv4_config.netmask},
1842 {"Address", ipv4_config.address},
1843 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001844 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001845 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001846 ipv4_static_array.push_back(
1847 {{"AddressOrigin", ipv4_config.origin},
1848 {"SubnetMask", ipv4_config.netmask},
1849 {"Address", ipv4_config.address},
1850 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001851 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001852 }
1853
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001854 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001855
1856 nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001857 nlohmann::json &ipv6_static_array =
1858 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001859 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001860 ipv6_static_array = nlohmann::json::array();
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001861 for (auto &ipv6_config : ipv6Data)
1862 {
1863 ipv6_array.push_back({{"Address", ipv6_config.address},
1864 {"PrefixLength", ipv6_config.prefixLength},
1865 {"AddressOrigin", ipv6_config.origin}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001866 if (ipv6_config.origin == "Static")
1867 {
1868 ipv6_static_array.push_back(
1869 {{"Address", ipv6_config.address},
1870 {"PrefixLength", ipv6_config.prefixLength},
1871 {"AddressOrigin", ipv6_config.origin}});
1872 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001873 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001874 }
1875
1876 /**
1877 * Functions triggers appropriate requests on DBus
1878 */
1879 void doGet(crow::Response &res, const crow::Request &req,
1880 const std::vector<std::string> &params) override
1881 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001882 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001883 if (params.size() != 1)
1884 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001885 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001886 return;
1887 }
1888
Ed Tanous4a0cb852018-10-15 07:55:04 -07001889 getEthernetIfaceData(
1890 params[0],
1891 [this, asyncResp, iface_id{std::string(params[0])}](
1892 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001893 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001894 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001895 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001896 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001897 // TODO(Pawel)consider distinguish between non existing
1898 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001899 messages::resourceNotFound(asyncResp->res,
1900 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001901 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001902 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001903
Ed Tanous0f74e642018-11-12 15:17:05 -08001904 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001905 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001906 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1907 asyncResp->res.jsonValue["Description"] =
1908 "Management Network Interface";
1909
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001910 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1911 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001912 });
1913 }
1914
1915 void doPatch(crow::Response &res, const crow::Request &req,
1916 const std::vector<std::string> &params) override
1917 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001918 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001919 if (params.size() != 1)
1920 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001921 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001922 return;
1923 }
1924
Ed Tanous4a0cb852018-10-15 07:55:04 -07001925 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001926
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001927 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301928 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301929 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001930 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001931 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001932 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001933 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001934 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001935 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001936 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001937 DHCPParameters v4dhcpParms;
1938 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001939
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001940 if (!json_util::readJson(
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301941 req, res, "HostName", hostname, "FQDN", fqdn,
1942 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1943 macAddress, "StaticNameServers", staticNameServers,
1944 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1945 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1946 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001947 {
1948 return;
1949 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001950 if (dhcpv4)
1951 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001952 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1953 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1954 v4dhcpParms.useDNSServers, "UseNTPServers",
1955 v4dhcpParms.useNTPServers, "UseDomainName",
1956 v4dhcpParms.useUseDomainName))
1957 {
1958 return;
1959 }
1960 }
1961
1962 if (dhcpv6)
1963 {
1964 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1965 v6dhcpParms.dhcpv6OperatingMode,
1966 "UseDNSServers", v6dhcpParms.useDNSServers,
1967 "UseNTPServers", v6dhcpParms.useNTPServers,
1968 "UseDomainName",
1969 v6dhcpParms.useUseDomainName))
1970 {
1971 return;
1972 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001973 }
1974
Johnathan Mantey01784822019-06-18 12:44:21 -07001975 // Get single eth interface data, and call the below callback for
1976 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001977 getEthernetIfaceData(
1978 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001979 [this, asyncResp, iface_id, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301980 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001981 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001982 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001983 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001984 staticNameServers = std::move(staticNameServers),
1985 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1986 v4dhcpParms = std::move(v4dhcpParms),
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001987 v6dhcpParms = std::move(v6dhcpParms),
1988 interfaceEnabled = std::move(interfaceEnabled)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001989 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001990 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001991 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001992 if (!success)
1993 {
1994 // ... otherwise return error
1995 // TODO(Pawel)consider distinguish between non existing
1996 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001997 messages::resourceNotFound(asyncResp->res,
1998 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001999 return;
2000 }
2001
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07002002 if (dhcpv4 || dhcpv6)
2003 {
2004 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
2005 std::move(v6dhcpParms), asyncResp);
2006 }
2007
Ed Tanous0627a2c2018-11-29 17:09:23 -08002008 if (hostname)
2009 {
2010 handleHostnamePatch(*hostname, asyncResp);
2011 }
2012
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302013 if (fqdn)
2014 {
2015 handleFqdnPatch(iface_id, *fqdn, asyncResp);
2016 }
2017
Ratan Guptad5776652019-03-03 08:47:22 +05302018 if (macAddress)
2019 {
2020 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2021 }
2022
Ravi Tejad1d50812019-06-23 16:20:27 -05002023 if (ipv4StaticAddresses)
2024 {
Ed Tanous537174c2018-12-10 15:09:31 -08002025 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002026 // above is returning a const value, not a non-const
2027 // value. This doesn't really work for us, as we need to
2028 // be able to efficiently move out the intermedia
2029 // nlohmann::json objects. This makes a copy of the
2030 // structure, and operates on that, but could be done
2031 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05002032 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07002033 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002034 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002035 }
2036
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002037 if (staticNameServers)
2038 {
2039 handleStaticNameServersPatch(iface_id, *staticNameServers,
2040 asyncResp);
2041 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002042
2043 if (ipv6DefaultGateway)
2044 {
2045 messages::propertyNotWritable(asyncResp->res,
2046 "IPv6DefaultGateway");
2047 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002048
2049 if (ipv6StaticAddresses)
2050 {
2051 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2052 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002053 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002054 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002055
2056 if (interfaceEnabled)
2057 {
2058 setEthernetInterfaceBoolProperty(
2059 iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2060 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002061 });
2062 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002063};
2064
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002065/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002066 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2067 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002068 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002069class VlanNetworkInterface : public Node
2070{
2071 public:
2072 /*
2073 * Default Constructor
2074 */
2075 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002076 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002077 Node(app,
Gunnar Mills7af91512020-04-14 22:16:57 -05002078 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>/",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002079 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002080 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002081 entityPrivileges = {
2082 {boost::beast::http::verb::get, {{"Login"}}},
2083 {boost::beast::http::verb::head, {{"Login"}}},
2084 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2085 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2086 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2087 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002088 }
2089
Ed Tanous1abe55e2018-09-05 08:30:59 -07002090 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08002091 void parseInterfaceData(
2092 nlohmann::json &json_response, const std::string &parent_iface_id,
2093 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002094 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002095 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002096 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002097 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07002098 json_response["Id"] = iface_id;
2099 json_response["@odata.id"] =
2100 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2101 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002102
Ed Tanous4a0cb852018-10-15 07:55:04 -07002103 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002104 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002105 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002106 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002107 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002108 }
2109
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002110 bool verifyNames(const std::string &parent, const std::string &iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002111 {
2112 if (!boost::starts_with(iface, parent + "_"))
2113 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002114 return false;
2115 }
2116 else
2117 {
2118 return true;
2119 }
2120 }
2121
2122 /**
2123 * Functions triggers appropriate requests on DBus
2124 */
2125 void doGet(crow::Response &res, const crow::Request &req,
2126 const std::vector<std::string> &params) override
2127 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002128 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2129 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002130 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2131 // Check if there is required param, truly entering this shall be
2132 // impossible.
2133 if (params.size() != 2)
2134 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002135 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002136 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002137 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002138 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002139
Ed Tanous4a0cb852018-10-15 07:55:04 -07002140 const std::string &parent_iface_id = params[0];
2141 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08002142 res.jsonValue["@odata.type"] =
2143 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002144 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002145
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002146 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002147 {
2148 return;
2149 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002150
Johnathan Mantey01784822019-06-18 12:44:21 -07002151 // Get single eth interface data, and call the below callback for
2152 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002153 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002154 params[1],
2155 [this, asyncResp, parent_iface_id{std::string(params[0])},
2156 iface_id{std::string(params[1])}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07002157 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002158 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002159 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002160 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002161 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002162 parseInterfaceData(asyncResp->res.jsonValue,
2163 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002164 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002165 }
2166 else
2167 {
2168 // ... otherwise return error
2169 // TODO(Pawel)consider distinguish between non existing
2170 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002171 messages::resourceNotFound(
2172 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002173 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002174 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002175 }
2176
Ed Tanous1abe55e2018-09-05 08:30:59 -07002177 void doPatch(crow::Response &res, const crow::Request &req,
2178 const std::vector<std::string> &params) override
2179 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002180 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002181 if (params.size() != 2)
2182 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002183 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002184 return;
2185 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002186
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 const std::string &parentIfaceId = params[0];
2188 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002189
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002190 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002191 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002192 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2193 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002194 return;
2195 }
2196
Ed Tanous0627a2c2018-11-29 17:09:23 -08002197 bool vlanEnable = false;
2198 uint64_t vlanId = 0;
2199
2200 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2201 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002202 {
2203 return;
2204 }
2205
Johnathan Mantey01784822019-06-18 12:44:21 -07002206 // Get single eth interface data, and call the below callback for
2207 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002208 getEthernetIfaceData(
2209 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002210 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002211 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2212 const bool &success, const EthernetInterfaceData &ethData,
2213 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002214 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002215 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002216 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002217 auto callback =
2218 [asyncResp](const boost::system::error_code ec) {
2219 if (ec)
2220 {
2221 messages::internalError(asyncResp->res);
2222 }
2223 };
2224
2225 if (vlanEnable == true)
2226 {
2227 crow::connections::systemBus->async_method_call(
2228 std::move(callback), "xyz.openbmc_project.Network",
2229 "/xyz/openbmc_project/network/" + ifaceId,
2230 "org.freedesktop.DBus.Properties", "Set",
2231 "xyz.openbmc_project.Network.VLAN", "Id",
2232 std::variant<uint32_t>(vlanId));
2233 }
2234 else
2235 {
2236 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2237 "vlan interface";
2238 crow::connections::systemBus->async_method_call(
2239 std::move(callback), "xyz.openbmc_project.Network",
2240 std::string("/xyz/openbmc_project/network/") +
2241 ifaceId,
2242 "xyz.openbmc_project.Object.Delete", "Delete");
2243 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002244 }
2245 else
2246 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002247 // TODO(Pawel)consider distinguish between non existing
2248 // object, and other errors
2249 messages::resourceNotFound(
2250 asyncResp->res, "VLAN Network Interface", ifaceId);
2251 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002252 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002253 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002254 }
2255
2256 void doDelete(crow::Response &res, const crow::Request &req,
2257 const std::vector<std::string> &params) override
2258 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002259 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002260 if (params.size() != 2)
2261 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002262 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002263 return;
2264 }
2265
2266 const std::string &parentIfaceId = params[0];
2267 const std::string &ifaceId = params[1];
2268
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002269 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002270 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002271 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2272 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002273 return;
2274 }
2275
Johnathan Mantey01784822019-06-18 12:44:21 -07002276 // Get single eth interface data, and call the below callback for
2277 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002278 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002279 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002280 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002281 ifaceId{std::string(params[1])}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002282 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002283 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002284 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002285 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002286 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002287 auto callback =
2288 [asyncResp](const boost::system::error_code ec) {
2289 if (ec)
2290 {
2291 messages::internalError(asyncResp->res);
2292 }
2293 };
2294 crow::connections::systemBus->async_method_call(
2295 std::move(callback), "xyz.openbmc_project.Network",
2296 std::string("/xyz/openbmc_project/network/") + ifaceId,
2297 "xyz.openbmc_project.Object.Delete", "Delete");
2298 }
2299 else
2300 {
2301 // ... otherwise return error
2302 // TODO(Pawel)consider distinguish between non existing
2303 // object, and other errors
2304 messages::resourceNotFound(
2305 asyncResp->res, "VLAN Network Interface", ifaceId);
2306 }
2307 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002308 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002309};
2310
2311/**
2312 * VlanNetworkInterfaceCollection derived class for delivering
2313 * VLANNetworkInterface Collection Schema
2314 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002315class VlanNetworkInterfaceCollection : public Node
2316{
2317 public:
2318 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002319 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002320 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2321 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002322 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002323 entityPrivileges = {
2324 {boost::beast::http::verb::get, {{"Login"}}},
2325 {boost::beast::http::verb::head, {{"Login"}}},
2326 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2327 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2328 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2329 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002330 }
2331
Ed Tanous1abe55e2018-09-05 08:30:59 -07002332 private:
2333 /**
2334 * Functions triggers appropriate requests on DBus
2335 */
2336 void doGet(crow::Response &res, const crow::Request &req,
2337 const std::vector<std::string> &params) override
2338 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002339 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002340 if (params.size() != 1)
2341 {
2342 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002343 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002344 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002345 }
2346
Ed Tanous4a0cb852018-10-15 07:55:04 -07002347 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002348
Ed Tanous4a0cb852018-10-15 07:55:04 -07002349 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002350 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002351 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002352 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002353 const bool &success,
Ed Tanous4c9afe42019-05-03 16:59:57 -07002354 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002355 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002356 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002357 messages::internalError(asyncResp->res);
2358 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002359 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002360
2361 if (iface_list.find(rootInterfaceName) == iface_list.end())
2362 {
2363 messages::resourceNotFound(asyncResp->res,
2364 "VLanNetworkInterfaceCollection",
2365 rootInterfaceName);
2366 return;
2367 }
2368
Ed Tanous0f74e642018-11-12 15:17:05 -08002369 asyncResp->res.jsonValue["@odata.type"] =
2370 "#VLanNetworkInterfaceCollection."
2371 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002372 asyncResp->res.jsonValue["Name"] =
2373 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002374
Jason M. Billsf12894f2018-10-09 12:45:45 -07002375 nlohmann::json iface_array = nlohmann::json::array();
2376
2377 for (const std::string &iface_item : iface_list)
2378 {
2379 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2380 {
2381 iface_array.push_back(
2382 {{"@odata.id",
2383 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2384 rootInterfaceName + "/VLANs/" + iface_item}});
2385 }
2386 }
2387
Jason M. Billsf12894f2018-10-09 12:45:45 -07002388 asyncResp->res.jsonValue["Members@odata.count"] =
2389 iface_array.size();
2390 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2391 asyncResp->res.jsonValue["@odata.id"] =
2392 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2393 rootInterfaceName + "/VLANs";
2394 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002395 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002396
Ed Tanous1abe55e2018-09-05 08:30:59 -07002397 void doPost(crow::Response &res, const crow::Request &req,
2398 const std::vector<std::string> &params) override
2399 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002400 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002401 if (params.size() != 1)
2402 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002403 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002404 return;
2405 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002406 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002407 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002408 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2409 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002410 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002411 return;
2412 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002413 // Need both vlanId and vlanEnable to service this request
2414 if (!vlanId)
2415 {
2416 messages::propertyMissing(asyncResp->res, "VLANId");
2417 }
2418 if (!vlanEnable)
2419 {
2420 messages::propertyMissing(asyncResp->res, "VLANEnable");
2421 }
Ed Tanous271584a2019-07-09 16:24:22 -07002422 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002423 {
2424 return;
2425 }
2426
Ed Tanous4a0cb852018-10-15 07:55:04 -07002427 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002428 auto callback = [asyncResp](const boost::system::error_code ec) {
2429 if (ec)
2430 {
2431 // TODO(ed) make more consistent error messages based on
2432 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002433 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002434 return;
2435 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002436 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002437 };
2438 crow::connections::systemBus->async_method_call(
2439 std::move(callback), "xyz.openbmc_project.Network",
2440 "/xyz/openbmc_project/network",
2441 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002442 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002443 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002444};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002445} // namespace redfish