blob: 0d7e4c5eea28e44b622659ea1bff7d555ef822df [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>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <utils/json_utils.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080025#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace redfish
28{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010029
30/**
31 * DBus types primitives for several generic DBus interfaces
32 * TODO(Pawel) consider move this to separate file into boost::dbus
33 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070034using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080035 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
36 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
Ed Tanous4a0cb852018-10-15 07:55:04 -070038using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070039 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070040 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 std::string,
42 boost::container::flat_map<
Ed Tanous029573d2019-02-01 10:57:49 -080043 std::string, sdbusplus::message::variant<
44 std::string, bool, uint8_t, int16_t, uint16_t,
45 int32_t, uint32_t, int64_t, uint64_t, double,
46 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070047
48enum class LinkType
49{
50 Local,
51 Global
52};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010053
54/**
55 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010056 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070057struct IPv4AddressData
58{
59 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070060 std::string address;
61 std::string domain;
62 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 std::string netmask;
64 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070065 LinkType linktype;
66
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 bool operator<(const IPv4AddressData &obj) const
68 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070069 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070070 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010071};
72
73/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050074 * Structure for keeping IPv6 data required by Redfish
75 */
76struct IPv6AddressData
77{
78 std::string id;
79 std::string address;
80 std::string origin;
81 uint8_t prefixLength;
82
83 bool operator<(const IPv6AddressData &obj) const
84 {
85 return id < obj.id;
86 }
87};
88/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010089 * Structure for keeping basic single Ethernet Interface information
90 * available from DBus
91 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070092struct EthernetInterfaceData
93{
Ed Tanous4a0cb852018-10-15 07:55:04 -070094 uint32_t speed;
95 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070096 bool DNSEnabled;
97 bool NTPEnabled;
98 bool HostNameEnabled;
99 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800100 bool linkUp;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700101 std::string DHCPEnabled;
102 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700103 std::string hostname;
104 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500105 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700106 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500107 std::vector<std::uint32_t> vlan_id;
Ed Tanous029573d2019-02-01 10:57:49 -0800108 std::vector<std::string> nameservers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800109 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100110};
111
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700112struct DHCPParameters
113{
114 std::optional<bool> dhcpv4Enabled;
115 std::optional<bool> useDNSServers;
116 std::optional<bool> useNTPServers;
117 std::optional<bool> useUseDomainName;
118 std::optional<std::string> dhcpv6OperatingMode;
119};
120
Ed Tanous4a0cb852018-10-15 07:55:04 -0700121// Helper function that changes bits netmask notation (i.e. /24)
122// into full dot notation
123inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700124{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700125 uint32_t value = 0xffffffff << (32 - bits);
126 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
127 std::to_string((value >> 16) & 0xff) + "." +
128 std::to_string((value >> 8) & 0xff) + "." +
129 std::to_string(value & 0xff);
130 return netmask;
131}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100132
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700133inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
134 bool isIPv4)
135{
136 if (isIPv4)
137 {
138 return (
139 (inputDHCP ==
140 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
141 (inputDHCP ==
142 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
143 }
144 return ((inputDHCP ==
145 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
146 (inputDHCP ==
147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
148}
149
150inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
151{
152 if (isIPv4 && isIPv6)
153 {
154 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
155 }
156 else if (isIPv4)
157 {
158 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
159 }
160 else if (isIPv6)
161 {
162 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
163 }
164 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
165}
166
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167inline std::string
168 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
169 bool isIPv4)
170{
171 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 return "Static";
174 }
175 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
176 {
177 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700178 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700179 return "IPv4LinkLocal";
180 }
181 else
182 {
183 return "LinkLocal";
184 }
185 }
186 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
187 {
188 if (isIPv4)
189 {
190 return "DHCP";
191 }
192 else
193 {
194 return "DHCPv6";
195 }
196 }
197 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
198 {
199 return "SLAAC";
200 }
201 return "";
202}
203
Ed Tanous4c9afe42019-05-03 16:59:57 -0700204inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700205 const GetManagedObjects &dbus_data,
206 EthernetInterfaceData &ethData)
207{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700208 bool idFound = false;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 for (const auto &objpath : dbus_data)
210 {
Ed Tanous029573d2019-02-01 10:57:49 -0800211 for (const auto &ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700212 {
Ed Tanous029573d2019-02-01 10:57:49 -0800213 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700214 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700215 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700216 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700222 const std::string *mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800223 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 if (mac != nullptr)
225 {
226 ethData.mac_address = *mac;
227 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700229 }
230 }
231 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
232 {
233 for (const auto &propertyPair : ifacePair.second)
234 {
235 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800237 const uint32_t *id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800238 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700239 if (id != nullptr)
240 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500241 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700242 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700244 }
245 }
246 else if (ifacePair.first ==
247 "xyz.openbmc_project.Network.EthernetInterface")
248 {
249 for (const auto &propertyPair : ifacePair.second)
250 {
251 if (propertyPair.first == "AutoNeg")
252 {
253 const bool *auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800254 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700255 if (auto_neg != nullptr)
256 {
257 ethData.auto_neg = *auto_neg;
258 }
259 }
260 else if (propertyPair.first == "Speed")
261 {
262 const uint32_t *speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800263 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700264 if (speed != nullptr)
265 {
266 ethData.speed = *speed;
267 }
268 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800269 else if (propertyPair.first == "LinkUp")
270 {
271 const bool *linkUp =
272 std::get_if<bool>(&propertyPair.second);
273 if (linkUp != nullptr)
274 {
275 ethData.linkUp = *linkUp;
276 }
277 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500278 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700279 {
Ed Tanous029573d2019-02-01 10:57:49 -0800280 const std::vector<std::string> *nameservers =
281 sdbusplus::message::variant_ns::get_if<
282 std::vector<std::string>>(
283 &propertyPair.second);
284 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700285 {
Ed Tanous029573d2019-02-01 10:57:49 -0800286 ethData.nameservers = std::move(*nameservers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700287 }
288 }
manojkiraneda2a133282019-02-19 13:09:43 +0530289 else if (propertyPair.first == "DHCPEnabled")
290 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700291 const std::string *DHCPEnabled =
292 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530293 if (DHCPEnabled != nullptr)
294 {
295 ethData.DHCPEnabled = *DHCPEnabled;
296 }
297 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800298 else if (propertyPair.first == "DomainName")
299 {
300 const std::vector<std::string> *domainNames =
301 sdbusplus::message::variant_ns::get_if<
302 std::vector<std::string>>(
303 &propertyPair.second);
304 if (domainNames != nullptr)
305 {
306 ethData.domainnames = std::move(*domainNames);
307 }
308 }
Ed Tanous029573d2019-02-01 10:57:49 -0800309 }
310 }
311 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700312
313 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
314 {
315 if (ifacePair.first ==
316 "xyz.openbmc_project.Network.DHCPConfiguration")
317 {
318 for (const auto &propertyPair : ifacePair.second)
319 {
320 if (propertyPair.first == "DNSEnabled")
321 {
322 const bool *DNSEnabled =
323 std::get_if<bool>(&propertyPair.second);
324 if (DNSEnabled != nullptr)
325 {
326 ethData.DNSEnabled = *DNSEnabled;
327 }
328 }
329 else if (propertyPair.first == "NTPEnabled")
330 {
331 const bool *NTPEnabled =
332 std::get_if<bool>(&propertyPair.second);
333 if (NTPEnabled != nullptr)
334 {
335 ethData.NTPEnabled = *NTPEnabled;
336 }
337 }
338 else if (propertyPair.first == "HostNameEnabled")
339 {
340 const bool *HostNameEnabled =
341 std::get_if<bool>(&propertyPair.second);
342 if (HostNameEnabled != nullptr)
343 {
344 ethData.HostNameEnabled = *HostNameEnabled;
345 }
346 }
347 else if (propertyPair.first == "SendHostNameEnabled")
348 {
349 const bool *SendHostNameEnabled =
350 std::get_if<bool>(&propertyPair.second);
351 if (SendHostNameEnabled != nullptr)
352 {
353 ethData.SendHostNameEnabled =
354 *SendHostNameEnabled;
355 }
356 }
357 }
358 }
359 }
Ed Tanous029573d2019-02-01 10:57:49 -0800360 // System configuration shows up in the global namespace, so no need
361 // to check eth number
362 if (ifacePair.first ==
363 "xyz.openbmc_project.Network.SystemConfiguration")
364 {
365 for (const auto &propertyPair : ifacePair.second)
366 {
367 if (propertyPair.first == "HostName")
368 {
369 const std::string *hostname =
370 sdbusplus::message::variant_ns::get_if<std::string>(
371 &propertyPair.second);
372 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700373 {
Ed Tanous029573d2019-02-01 10:57:49 -0800374 ethData.hostname = *hostname;
375 }
376 }
377 else if (propertyPair.first == "DefaultGateway")
378 {
379 const std::string *defaultGateway =
380 sdbusplus::message::variant_ns::get_if<std::string>(
381 &propertyPair.second);
382 if (defaultGateway != nullptr)
383 {
384 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700385 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500387 else if (propertyPair.first == "DefaultGateway6")
388 {
389 const std::string *defaultGateway6 =
390 sdbusplus::message::variant_ns::get_if<std::string>(
391 &propertyPair.second);
392 if (defaultGateway6 != nullptr)
393 {
394 ethData.ipv6_default_gateway = *defaultGateway6;
395 }
396 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 }
398 }
399 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700400 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700401 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700402}
403
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500404// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700405inline void
406 extractIPV6Data(const std::string &ethiface_id,
407 const GetManagedObjects &dbus_data,
408 boost::container::flat_set<IPv6AddressData> &ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500409{
410 const std::string ipv6PathStart =
411 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
412
413 // Since there might be several IPv6 configurations aligned with
414 // single ethernet interface, loop over all of them
415 for (const auto &objpath : dbus_data)
416 {
417 // Check if proper pattern for object path appears
418 if (boost::starts_with(objpath.first.str, ipv6PathStart))
419 {
420 for (auto &interface : objpath.second)
421 {
422 if (interface.first == "xyz.openbmc_project.Network.IP")
423 {
424 // Instance IPv6AddressData structure, and set as
425 // appropriate
426 std::pair<
427 boost::container::flat_set<IPv6AddressData>::iterator,
428 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700429 it = ipv6_config.insert(IPv6AddressData{});
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500430 IPv6AddressData &ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700431 ipv6_address.id =
432 objpath.first.str.substr(ipv6PathStart.size());
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500433 for (auto &property : interface.second)
434 {
435 if (property.first == "Address")
436 {
437 const std::string *address =
438 std::get_if<std::string>(&property.second);
439 if (address != nullptr)
440 {
441 ipv6_address.address = *address;
442 }
443 }
444 else if (property.first == "Origin")
445 {
446 const std::string *origin =
447 std::get_if<std::string>(&property.second);
448 if (origin != nullptr)
449 {
450 ipv6_address.origin =
451 translateAddressOriginDbusToRedfish(*origin,
452 false);
453 }
454 }
455 else if (property.first == "PrefixLength")
456 {
457 const uint8_t *prefix =
458 std::get_if<uint8_t>(&property.second);
459 if (prefix != nullptr)
460 {
461 ipv6_address.prefixLength = *prefix;
462 }
463 }
464 else
465 {
466 BMCWEB_LOG_ERROR
467 << "Got extra property: " << property.first
468 << " on the " << objpath.first.str << " object";
469 }
470 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500471 }
472 }
473 }
474 }
475}
476
Ed Tanous4a0cb852018-10-15 07:55:04 -0700477// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700478inline void
479 extractIPData(const std::string &ethiface_id,
480 const GetManagedObjects &dbus_data,
481 boost::container::flat_set<IPv4AddressData> &ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700482{
483 const std::string ipv4PathStart =
484 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
485
486 // Since there might be several IPv4 configurations aligned with
487 // single ethernet interface, loop over all of them
488 for (const auto &objpath : dbus_data)
489 {
490 // Check if proper pattern for object path appears
491 if (boost::starts_with(objpath.first.str, ipv4PathStart))
492 {
493 for (auto &interface : objpath.second)
494 {
495 if (interface.first == "xyz.openbmc_project.Network.IP")
496 {
497 // Instance IPv4AddressData structure, and set as
498 // appropriate
499 std::pair<
500 boost::container::flat_set<IPv4AddressData>::iterator,
501 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700502 it = ipv4_config.insert(IPv4AddressData{});
Ed Tanous4a0cb852018-10-15 07:55:04 -0700503 IPv4AddressData &ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700504 ipv4_address.id =
505 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous4a0cb852018-10-15 07:55:04 -0700506 for (auto &property : interface.second)
507 {
508 if (property.first == "Address")
509 {
510 const std::string *address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800511 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700512 if (address != nullptr)
513 {
514 ipv4_address.address = *address;
515 }
516 }
517 else if (property.first == "Gateway")
518 {
519 const std::string *gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800520 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700521 if (gateway != nullptr)
522 {
523 ipv4_address.gateway = *gateway;
524 }
525 }
526 else if (property.first == "Origin")
527 {
528 const std::string *origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800529 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700530 if (origin != nullptr)
531 {
532 ipv4_address.origin =
533 translateAddressOriginDbusToRedfish(*origin,
534 true);
535 }
536 }
537 else if (property.first == "PrefixLength")
538 {
539 const uint8_t *mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800540 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 if (mask != nullptr)
542 {
543 // convert it to the string
544 ipv4_address.netmask = getNetmask(*mask);
545 }
546 }
547 else
548 {
549 BMCWEB_LOG_ERROR
550 << "Got extra property: " << property.first
551 << " on the " << objpath.first.str << " object";
552 }
553 }
554 // Check if given address is local, or global
555 ipv4_address.linktype =
556 boost::starts_with(ipv4_address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700557 ? LinkType::Local
558 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700559 }
560 }
561 }
562 }
563}
564
565/**
566 * @brief Sets given Id on the given VLAN interface through D-Bus
567 *
568 * @param[in] ifaceId Id of VLAN interface that should be modified
569 * @param[in] inputVlanId New ID of the VLAN
570 * @param[in] callback Function that will be called after the operation
571 *
572 * @return None.
573 */
574template <typename CallbackFunc>
575void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
576 CallbackFunc &&callback)
577{
578 crow::connections::systemBus->async_method_call(
579 callback, "xyz.openbmc_project.Network",
580 std::string("/xyz/openbmc_project/network/") + ifaceId,
581 "org.freedesktop.DBus.Properties", "Set",
582 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800583 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700584}
585
586/**
587 * @brief Helper function that verifies IP address to check if it is in
588 * proper format. If bits pointer is provided, also calculates active
589 * bit count for Subnet Mask.
590 *
591 * @param[in] ip IP that will be verified
592 * @param[out] bits Calculated mask in bits notation
593 *
594 * @return true in case of success, false otherwise
595 */
596inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
597 uint8_t *bits = nullptr)
598{
599 std::vector<std::string> bytesInMask;
600
601 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700602
603 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700606 return false;
607 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608
Ed Tanous4a0cb852018-10-15 07:55:04 -0700609 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700611 *bits = 0;
612 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613
Ed Tanous4a0cb852018-10-15 07:55:04 -0700614 char *endPtr;
615 long previousValue = 255;
616 bool firstZeroInByteHit;
617 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700619 if (byte.empty())
620 {
621 return false;
622 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623
Ed Tanous4a0cb852018-10-15 07:55:04 -0700624 // Use strtol instead of stroi to avoid exceptions
625 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626
Ed Tanous4a0cb852018-10-15 07:55:04 -0700627 // endPtr should point to the end of the string, otherwise given string
628 // is not 100% number
629 if (*endPtr != '\0')
630 {
631 return false;
632 }
633
634 // Value should be contained in byte
635 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 {
637 return false;
638 }
639
640 if (bits != nullptr)
641 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700642 // Mask has to be continuous between bytes
643 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 {
645 return false;
646 }
647
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Mask has to be continuous inside bytes
649 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650
Ed Tanous4a0cb852018-10-15 07:55:04 -0700651 // Count bits
652 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700654 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700656 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700658 // Continuity not preserved
659 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700660 }
661 else
662 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700663 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 }
665 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 else
667 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700668 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700669 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700670 }
671 }
672
673 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 }
675
Ed Tanous4a0cb852018-10-15 07:55:04 -0700676 return true;
677}
678
679/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700680 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 *
682 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700683 * @param[in] ipHash DBus Hash id of IP that should be deleted
684 * @param[io] asyncResp Response object that will be returned to client
685 *
686 * @return None
687 */
688inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700689 const std::shared_ptr<AsyncResp> asyncResp)
690{
691 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700692 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 if (ec)
694 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800695 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100696 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700697 },
698 "xyz.openbmc_project.Network",
699 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
700 "xyz.openbmc_project.Object.Delete", "Delete");
701}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702
Ed Tanous4a0cb852018-10-15 07:55:04 -0700703/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700704 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700705 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700706 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
707 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
708 * @param[in] gateway IPv4 address of this interfaces gateway
709 * @param[in] address IPv4 address to assign to this interface
710 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700711 *
712 * @return None
713 */
Ed Tanousb01bf292019-03-25 19:25:26 +0000714inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
Johnathan Mantey01784822019-06-18 12:44:21 -0700715 uint8_t prefixLength, const std::string &gateway,
Ed Tanousb01bf292019-03-25 19:25:26 +0000716 const std::string &address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 std::shared_ptr<AsyncResp> asyncResp)
718{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700719 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700720 [asyncResp](const boost::system::error_code ec) {
721 if (ec)
722 {
723 messages::internalError(asyncResp->res);
724 }
725 },
726 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700727 "/xyz/openbmc_project/network/" + ifaceId,
728 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700729 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700730 gateway);
731}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500732
733/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700734 * @brief Deletes the IPv4 entry for this interface and creates a replacement
735 * static IPv4 entry
736 *
737 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
738 * @param[in] id The unique hash entry identifying the DBus entry
739 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
740 * @param[in] gateway IPv4 address of this interfaces gateway
741 * @param[in] address IPv4 address to assign to this interface
742 * @param[io] asyncResp Response object that will be returned to client
743 *
744 * @return None
745 */
746inline void deleteAndCreateIPv4(const std::string &ifaceId,
747 const std::string &id, uint8_t prefixLength,
748 const std::string &gateway,
749 const std::string &address,
750 std::shared_ptr<AsyncResp> asyncResp)
751{
752 crow::connections::systemBus->async_method_call(
753 [asyncResp, ifaceId, address, prefixLength,
754 gateway](const boost::system::error_code ec) {
755 if (ec)
756 {
757 messages::internalError(asyncResp->res);
758 }
759 crow::connections::systemBus->async_method_call(
760 [asyncResp](const boost::system::error_code ec) {
761 if (ec)
762 {
763 messages::internalError(asyncResp->res);
764 }
765 },
766 "xyz.openbmc_project.Network",
767 "/xyz/openbmc_project/network/" + ifaceId,
768 "xyz.openbmc_project.Network.IP.Create", "IP",
769 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
770 prefixLength, gateway);
771 },
772 "xyz.openbmc_project.Network",
773 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
774 "xyz.openbmc_project.Object.Delete", "Delete");
775}
776
777/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500778 * @brief Deletes given IPv6
779 *
780 * @param[in] ifaceId Id of interface whose IP should be deleted
781 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500782 * @param[io] asyncResp Response object that will be returned to client
783 *
784 * @return None
785 */
786inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500787 const std::shared_ptr<AsyncResp> asyncResp)
788{
789 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700790 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500791 if (ec)
792 {
793 messages::internalError(asyncResp->res);
794 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500795 },
796 "xyz.openbmc_project.Network",
797 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
798 "xyz.openbmc_project.Object.Delete", "Delete");
799}
800
801/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700802 * @brief Deletes the IPv6 entry for this interface and creates a replacement
803 * static IPv6 entry
804 *
805 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
806 * @param[in] id The unique hash entry identifying the DBus entry
807 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
808 * @param[in] address IPv6 address to assign to this interface
809 * @param[io] asyncResp Response object that will be returned to client
810 *
811 * @return None
812 */
813inline void deleteAndCreateIPv6(const std::string &ifaceId,
814 const std::string &id, uint8_t prefixLength,
815 const std::string &address,
816 std::shared_ptr<AsyncResp> asyncResp)
817{
818 crow::connections::systemBus->async_method_call(
819 [asyncResp, ifaceId, address,
820 prefixLength](const boost::system::error_code ec) {
821 if (ec)
822 {
823 messages::internalError(asyncResp->res);
824 }
825 crow::connections::systemBus->async_method_call(
826 [asyncResp](const boost::system::error_code ec) {
827 if (ec)
828 {
829 messages::internalError(asyncResp->res);
830 }
831 },
832 "xyz.openbmc_project.Network",
833 "/xyz/openbmc_project/network/" + ifaceId,
834 "xyz.openbmc_project.Network.IP.Create", "IP",
835 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
836 prefixLength, "");
837 },
838 "xyz.openbmc_project.Network",
839 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
840 "xyz.openbmc_project.Object.Delete", "Delete");
841}
842
843/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500844 * @brief Creates IPv6 with given data
845 *
846 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500847 * @param[in] prefixLength Prefix length that needs to be added
848 * @param[in] address IP address that needs to be added
849 * @param[io] asyncResp Response object that will be returned to client
850 *
851 * @return None
852 */
Johnathan Mantey01784822019-06-18 12:44:21 -0700853inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
854 const std::string &address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500855 std::shared_ptr<AsyncResp> asyncResp)
856{
857 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
858 if (ec)
859 {
860 messages::internalError(asyncResp->res);
861 }
862 };
863 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
864 // does not have assosiated gateway property
865 crow::connections::systemBus->async_method_call(
866 std::move(createIpHandler), "xyz.openbmc_project.Network",
867 "/xyz/openbmc_project/network/" + ifaceId,
868 "xyz.openbmc_project.Network.IP.Create", "IP",
869 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
870 "");
871}
872
Ed Tanous4a0cb852018-10-15 07:55:04 -0700873/**
874 * Function that retrieves all properties for given Ethernet Interface
875 * Object
876 * from EntityManager Network Manager
877 * @param ethiface_id a eth interface id to query on DBus
878 * @param callback a function that shall be called to convert Dbus output
879 * into JSON
880 */
881template <typename CallbackFunc>
882void getEthernetIfaceData(const std::string &ethiface_id,
883 CallbackFunc &&callback)
884{
885 crow::connections::systemBus->async_method_call(
886 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
887 const boost::system::error_code error_code,
888 const GetManagedObjects &resp) {
889 EthernetInterfaceData ethData{};
890 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500891 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700892
893 if (error_code)
894 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700895 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700896 return;
897 }
898
Ed Tanous4c9afe42019-05-03 16:59:57 -0700899 bool found =
900 extractEthernetInterfaceData(ethiface_id, resp, ethData);
901 if (!found)
902 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700903 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700904 return;
905 }
906
Johnathan Mantey01784822019-06-18 12:44:21 -0700907 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700908 // Fix global GW
909 for (IPv4AddressData &ipv4 : ipv4Data)
910 {
Ravi Tejac6191412019-07-30 00:53:50 -0500911 if (((ipv4.linktype == LinkType::Global) &&
912 (ipv4.gateway == "0.0.0.0")) ||
913 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700914 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700915 ipv4.gateway = ethData.default_gateway;
916 }
917 }
918
Johnathan Mantey01784822019-06-18 12:44:21 -0700919 extractIPV6Data(ethiface_id, resp, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700920 // Finally make a callback with usefull data
Johnathan Mantey01784822019-06-18 12:44:21 -0700921 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700922 },
923 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
924 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700925}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700926
927/**
928 * Function that retrieves all Ethernet Interfaces available through Network
929 * Manager
930 * @param callback a function that shall be called to convert Dbus output
931 * into JSON.
932 */
933template <typename CallbackFunc>
934void getEthernetIfaceList(CallbackFunc &&callback)
935{
936 crow::connections::systemBus->async_method_call(
937 [callback{std::move(callback)}](
938 const boost::system::error_code error_code,
939 GetManagedObjects &resp) {
940 // Callback requires vector<string> to retrieve all available
941 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700942 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700943 iface_list.reserve(resp.size());
944 if (error_code)
945 {
946 callback(false, iface_list);
947 return;
948 }
949
950 // Iterate over all retrieved ObjectPaths.
951 for (const auto &objpath : resp)
952 {
953 // And all interfaces available for certain ObjectPath.
954 for (const auto &interface : objpath.second)
955 {
956 // If interface is
957 // xyz.openbmc_project.Network.EthernetInterface, this is
958 // what we're looking for.
959 if (interface.first ==
960 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 // Cut out everyting until last "/", ...
963 const std::string &iface_id = objpath.first.str;
964 std::size_t last_pos = iface_id.rfind("/");
965 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700966 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700967 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700968 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969 }
970 }
971 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700972 }
973 // Finally make a callback with useful data
974 callback(true, iface_list);
975 },
976 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
977 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700978}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100979
980/**
981 * EthernetCollection derived class for delivering Ethernet Collection Schema
982 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983class EthernetCollection : public Node
984{
985 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700987 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700988 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700989 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 entityPrivileges = {
991 {boost::beast::http::verb::get, {{"Login"}}},
992 {boost::beast::http::verb::head, {{"Login"}}},
993 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
994 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
995 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
996 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
997 }
998
999 private:
1000 /**
1001 * Functions triggers appropriate requests on DBus
1002 */
1003 void doGet(crow::Response &res, const crow::Request &req,
1004 const std::vector<std::string> &params) override
1005 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001006 res.jsonValue["@odata.type"] =
1007 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001008 res.jsonValue["@odata.id"] =
1009 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1010 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1011 res.jsonValue["Description"] =
1012 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001013 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001014 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001016 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001017 [asyncResp](
1018 const bool &success,
1019 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001020 if (!success)
1021 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001022 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001023 return;
1024 }
1025
Ed Tanous4c9afe42019-05-03 16:59:57 -07001026 nlohmann::json &iface_array =
1027 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001028 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001029 std::string tag = "_";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001030 for (const std::string &iface_item : iface_list)
1031 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001032 std::size_t found = iface_item.find(tag);
1033 if (found == std::string::npos)
1034 {
1035 iface_array.push_back(
1036 {{"@odata.id",
1037 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1038 iface_item}});
1039 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001040 }
1041
Ed Tanous4c9afe42019-05-03 16:59:57 -07001042 asyncResp->res.jsonValue["Members@odata.count"] =
1043 iface_array.size();
1044 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001045 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001046 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001047 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001048};
1049
1050/**
1051 * EthernetInterface derived class for delivering Ethernet Schema
1052 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053class EthernetInterface : public Node
1054{
1055 public:
1056 /*
1057 * Default Constructor
1058 */
Ed Tanous4a0cb852018-10-15 07:55:04 -07001059 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001061 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001062 std::string())
1063 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001064 entityPrivileges = {
1065 {boost::beast::http::verb::get, {{"Login"}}},
1066 {boost::beast::http::verb::head, {{"Login"}}},
1067 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1068 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1069 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1070 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001071 }
1072
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001074 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001075 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 {
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001077 asyncResp->res.jsonValue["HostName"] = hostname;
1078 crow::connections::systemBus->async_method_call(
1079 [asyncResp](const boost::system::error_code ec) {
1080 if (ec)
1081 {
1082 messages::internalError(asyncResp->res);
1083 }
1084 },
1085 "xyz.openbmc_project.Network",
1086 "/xyz/openbmc_project/network/config",
1087 "org.freedesktop.DBus.Properties", "Set",
1088 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001089 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001090 }
1091
Ratan Guptad5776652019-03-03 08:47:22 +05301092 void handleMACAddressPatch(const std::string &ifaceId,
1093 const std::string &macAddress,
1094 const std::shared_ptr<AsyncResp> &asyncResp)
1095 {
1096 crow::connections::systemBus->async_method_call(
1097 [asyncResp, macAddress](const boost::system::error_code ec) {
1098 if (ec)
1099 {
1100 messages::internalError(asyncResp->res);
1101 return;
1102 }
Ratan Guptad5776652019-03-03 08:47:22 +05301103 },
1104 "xyz.openbmc_project.Network",
1105 "/xyz/openbmc_project/network/" + ifaceId,
1106 "org.freedesktop.DBus.Properties", "Set",
1107 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1108 std::variant<std::string>(macAddress));
1109 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001110
Jennifer Leeda131a92019-04-24 15:13:55 -07001111 void setDHCPEnabled(const std::string &ifaceId,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001112 const std::string &propertyName, const bool v4Value,
1113 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001114 const std::shared_ptr<AsyncResp> asyncResp)
1115 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001116 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001117 crow::connections::systemBus->async_method_call(
1118 [asyncResp](const boost::system::error_code ec) {
1119 if (ec)
1120 {
1121 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1122 messages::internalError(asyncResp->res);
1123 return;
1124 }
1125 },
1126 "xyz.openbmc_project.Network",
1127 "/xyz/openbmc_project/network/" + ifaceId,
1128 "org.freedesktop.DBus.Properties", "Set",
1129 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001130 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001131 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001132
Jennifer Leeda131a92019-04-24 15:13:55 -07001133 void setDHCPv4Config(const std::string &propertyName, const bool &value,
1134 const std::shared_ptr<AsyncResp> asyncResp)
1135 {
1136 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1137 crow::connections::systemBus->async_method_call(
1138 [asyncResp](const boost::system::error_code ec) {
1139 if (ec)
1140 {
1141 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1142 messages::internalError(asyncResp->res);
1143 return;
1144 }
1145 },
1146 "xyz.openbmc_project.Network",
1147 "/xyz/openbmc_project/network/config/dhcp",
1148 "org.freedesktop.DBus.Properties", "Set",
1149 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1150 std::variant<bool>{value});
1151 }
Ratan Guptad5776652019-03-03 08:47:22 +05301152
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001153 void handleDHCPPatch(const std::string &ifaceId,
1154 const EthernetInterfaceData &ethData,
1155 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1156 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001157 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001158 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1159 bool ipv6Active =
1160 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001161
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001162 bool nextv4DHCPState =
1163 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1164
1165 bool nextv6DHCPState{};
1166 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001167 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001168 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1169 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1170 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1171 {
1172 messages::propertyValueFormatError(
1173 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1174 "OperatingMode");
1175 return;
1176 }
1177 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1178 }
1179 else
1180 {
1181 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001182 }
1183
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001184 bool nextDNS{};
1185 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001186 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001187 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1188 {
1189 messages::generalError(asyncResp->res);
1190 return;
1191 }
1192 nextDNS = *v4dhcpParms.useDNSServers;
1193 }
1194 else if (v4dhcpParms.useDNSServers)
1195 {
1196 nextDNS = *v4dhcpParms.useDNSServers;
1197 }
1198 else if (v6dhcpParms.useDNSServers)
1199 {
1200 nextDNS = *v6dhcpParms.useDNSServers;
1201 }
1202 else
1203 {
1204 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001205 }
1206
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001207 bool nextNTP{};
1208 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001209 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001210 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1211 {
1212 messages::generalError(asyncResp->res);
1213 return;
1214 }
1215 nextNTP = *v4dhcpParms.useNTPServers;
1216 }
1217 else if (v4dhcpParms.useNTPServers)
1218 {
1219 nextNTP = *v4dhcpParms.useNTPServers;
1220 }
1221 else if (v6dhcpParms.useNTPServers)
1222 {
1223 nextNTP = *v6dhcpParms.useNTPServers;
1224 }
1225 else
1226 {
1227 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001228 }
1229
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001230 bool nextUseDomain{};
1231 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001232 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001233 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1234 {
1235 messages::generalError(asyncResp->res);
1236 return;
1237 }
1238 nextUseDomain = *v4dhcpParms.useUseDomainName;
1239 }
1240 else if (v4dhcpParms.useUseDomainName)
1241 {
1242 nextUseDomain = *v4dhcpParms.useUseDomainName;
1243 }
1244 else if (v6dhcpParms.useUseDomainName)
1245 {
1246 nextUseDomain = *v6dhcpParms.useUseDomainName;
1247 }
1248 else
1249 {
1250 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001251 }
1252
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001253 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1254 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1255 asyncResp);
1256 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1257 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1258 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1259 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1260 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1261 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001262 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001263
1264 boost::container::flat_set<IPv4AddressData>::const_iterator
1265 GetNextStaticIPEntry(
1266 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1267 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1268 {
1269 for (; head != end; head++)
1270 {
1271 if (head->origin == "Static")
1272 {
1273 return head;
1274 }
1275 }
1276 return end;
1277 }
1278
1279 boost::container::flat_set<IPv6AddressData>::const_iterator
1280 GetNextStaticIPEntry(
1281 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1282 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1283 {
1284 for (; head != end; head++)
1285 {
1286 if (head->origin == "Static")
1287 {
1288 return head;
1289 }
1290 }
1291 return end;
1292 }
1293
Ravi Tejad1d50812019-06-23 16:20:27 -05001294 void handleIPv4StaticPatch(
Ratan Guptaf476acb2019-03-02 16:46:57 +05301295 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001296 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001297 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001298 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001299 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301300 {
1301 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001302 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301303 return;
1304 }
1305
Ed Tanous271584a2019-07-09 16:24:22 -07001306 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001307 // Find the first static IP address currently active on the NIC and
1308 // match it to the first JSON element in the IPv4StaticAddresses array.
1309 // Match each subsequent JSON element to the next static IP programmed
1310 // into the NIC.
1311 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1312 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1313
Ed Tanous537174c2018-12-10 15:09:31 -08001314 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001315 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001316 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001317 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001318
Johnathan Mantey01784822019-06-18 12:44:21 -07001319 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301320 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001321 std::optional<std::string> address;
1322 std::optional<std::string> subnetMask;
1323 std::optional<std::string> gateway;
1324
1325 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1326 address, "SubnetMask", subnetMask,
1327 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301328 {
1329 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001330 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301331 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301332 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301333
Johnathan Mantey01784822019-06-18 12:44:21 -07001334 // Find the address/subnet/gateway values. Any values that are
1335 // not explicitly provided are assumed to be unmodified from the
1336 // current state of the interface. Merge existing state into the
1337 // current request.
Ed Tanous271584a2019-07-09 16:24:22 -07001338 const std::string *addr = nullptr;
1339 const std::string *gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001340 uint8_t prefixLength = 0;
1341 bool errorInEntry = false;
1342 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301343 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001344 if (ipv4VerifyIpAndGetBitcount(*address))
1345 {
1346 addr = &(*address);
1347 }
1348 else
1349 {
1350 messages::propertyValueFormatError(
1351 asyncResp->res, *address, pathString + "/Address");
1352 errorInEntry = true;
1353 }
1354 }
1355 else if (NICIPentry != ipv4Data.cend())
1356 {
1357 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301358 }
1359 else
1360 {
1361 messages::propertyMissing(asyncResp->res,
1362 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001363 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001364 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301365
1366 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001367 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001368 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1369 {
1370 messages::propertyValueFormatError(
1371 asyncResp->res, *subnetMask,
1372 pathString + "/SubnetMask");
1373 errorInEntry = true;
1374 }
1375 }
1376 else if (NICIPentry != ipv4Data.cend())
1377 {
1378 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1379 &prefixLength))
1380 {
1381 messages::propertyValueFormatError(
1382 asyncResp->res, NICIPentry->netmask,
1383 pathString + "/SubnetMask");
1384 errorInEntry = true;
1385 }
1386 }
1387 else
1388 {
1389 messages::propertyMissing(asyncResp->res,
1390 pathString + "/SubnetMask");
1391 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001392 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301393
Ratan Guptaf476acb2019-03-02 16:46:57 +05301394 if (gateway)
1395 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001396 if (ipv4VerifyIpAndGetBitcount(*gateway))
1397 {
1398 gw = &(*gateway);
1399 }
1400 else
1401 {
1402 messages::propertyValueFormatError(
1403 asyncResp->res, *gateway, pathString + "/Gateway");
1404 errorInEntry = true;
1405 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301406 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001407 else if (NICIPentry != ipv4Data.cend())
1408 {
1409 gw = &NICIPentry->gateway;
1410 }
1411 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001412 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001413 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001414 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001415 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001416 }
1417
Johnathan Mantey01784822019-06-18 12:44:21 -07001418 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001419 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001420 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001421 }
1422
Johnathan Mantey01784822019-06-18 12:44:21 -07001423 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001424 {
Ed Tanous271584a2019-07-09 16:24:22 -07001425 if (gw != nullptr || addr != nullptr)
1426 {
1427 // Shouldn't be possible based on errorInEntry, but
1428 // it flags -wmaybe-uninitialized in the compiler,
1429 // so defend against that
1430 return;
1431 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001432 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1433 *gw, *addr, asyncResp);
1434 NICIPentry =
1435 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001436 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001437 else
1438 {
1439 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1440 *address, asyncResp);
1441 }
1442 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001443 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001444 else
1445 {
1446 if (NICIPentry == ipv4Data.cend())
1447 {
1448 // Requesting a DELETE/DO NOT MODIFY action for an item
1449 // that isn't present on the eth(n) interface. Input JSON is
1450 // in error, so bail out.
1451 if (thisJson.is_null())
1452 {
1453 messages::resourceCannotBeDeleted(asyncResp->res);
1454 return;
1455 }
1456 else
1457 {
1458 messages::propertyValueFormatError(
1459 asyncResp->res, thisJson.dump(), pathString);
1460 return;
1461 }
1462 }
1463
1464 if (thisJson.is_null())
1465 {
1466 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1467 }
1468 if (NICIPentry != ipv4Data.cend())
1469 {
1470 NICIPentry =
1471 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1472 }
1473 entryIdx++;
1474 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001475 }
1476 }
1477
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001478 void handleStaticNameServersPatch(
1479 const std::string &ifaceId,
1480 const std::vector<std::string> &updatedStaticNameServers,
1481 const std::shared_ptr<AsyncResp> &asyncResp)
1482 {
1483 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001484 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001485 if (ec)
1486 {
1487 messages::internalError(asyncResp->res);
1488 return;
1489 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001490 },
1491 "xyz.openbmc_project.Network",
1492 "/xyz/openbmc_project/network/" + ifaceId,
1493 "org.freedesktop.DBus.Properties", "Set",
1494 "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1495 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1496 }
1497
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001498 void handleIPv6StaticAddressesPatch(
1499 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001500 const boost::container::flat_set<IPv6AddressData> &ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001501 const std::shared_ptr<AsyncResp> asyncResp)
1502 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001503 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001504 {
1505 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1506 "IPv6StaticAddresses");
1507 return;
1508 }
Ed Tanous271584a2019-07-09 16:24:22 -07001509 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001510 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1511 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001512 for (nlohmann::json &thisJson : input)
1513 {
1514 std::string pathString =
1515 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1516
Johnathan Mantey01784822019-06-18 12:44:21 -07001517 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001518 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001519 std::optional<std::string> address;
1520 std::optional<uint8_t> prefixLength;
1521
1522 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1523 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001524 {
1525 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001526 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001527 return;
1528 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001529
Johnathan Mantey01784822019-06-18 12:44:21 -07001530 const std::string *addr;
1531 uint8_t prefix;
1532
1533 // Find the address and prefixLength values. Any values that are
1534 // not explicitly provided are assumed to be unmodified from the
1535 // current state of the interface. Merge existing state into the
1536 // current request.
1537 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001538 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001539 addr = &(*address);
1540 }
1541 else if (NICIPentry != ipv6Data.end())
1542 {
1543 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001544 }
1545 else
1546 {
1547 messages::propertyMissing(asyncResp->res,
1548 pathString + "/Address");
1549 return;
1550 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001551
1552 if (prefixLength)
1553 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001554 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001555 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001556 else if (NICIPentry != ipv6Data.end())
1557 {
1558 prefix = NICIPentry->prefixLength;
1559 }
1560 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001561 {
1562 messages::propertyMissing(asyncResp->res,
1563 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001564 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001565 }
1566
Johnathan Mantey01784822019-06-18 12:44:21 -07001567 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001568 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001569 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1570 asyncResp);
1571 NICIPentry =
1572 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1573 }
1574 else
1575 {
1576 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1577 }
1578 entryIdx++;
1579 }
1580 else
1581 {
1582 if (NICIPentry == ipv6Data.end())
1583 {
1584 // Requesting a DELETE/DO NOT MODIFY action for an item
1585 // that isn't present on the eth(n) interface. Input JSON is
1586 // in error, so bail out.
1587 if (thisJson.is_null())
1588 {
1589 messages::resourceCannotBeDeleted(asyncResp->res);
1590 return;
1591 }
1592 else
1593 {
1594 messages::propertyValueFormatError(
1595 asyncResp->res, thisJson.dump(), pathString);
1596 return;
1597 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001598 }
1599
Johnathan Mantey01784822019-06-18 12:44:21 -07001600 if (thisJson.is_null())
1601 {
1602 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1603 }
1604 if (NICIPentry != ipv6Data.cend())
1605 {
1606 NICIPentry =
1607 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1608 }
1609 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001610 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001611 }
1612 }
1613
Ed Tanous0f74e642018-11-12 15:17:05 -08001614 void parseInterfaceData(
1615 nlohmann::json &json_response, const std::string &iface_id,
1616 const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001617 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001618 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001619 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001620 json_response["Id"] = iface_id;
1621 json_response["@odata.id"] =
1622 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous029573d2019-02-01 10:57:49 -08001623 json_response["InterfaceEnabled"] = true;
1624 if (ethData.speed == 0)
1625 {
Ed Tanous029573d2019-02-01 10:57:49 -08001626 json_response["Status"] = {
1627 {"Health", "OK"},
1628 {"State", "Disabled"},
1629 };
1630 }
1631 else
1632 {
Ed Tanous029573d2019-02-01 10:57:49 -08001633 json_response["Status"] = {
1634 {"Health", "OK"},
1635 {"State", "Enabled"},
1636 };
1637 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001638
1639 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001640 json_response["SpeedMbps"] = ethData.speed;
1641 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001642 json_response["DHCPv4"]["DHCPEnabled"] =
1643 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1644 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1645 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1646 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1647
1648 json_response["DHCPv6"]["OperatingMode"] =
1649 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1650 : "Disabled";
1651 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1652 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1653 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301654
Ed Tanous4a0cb852018-10-15 07:55:04 -07001655 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001656 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001657 json_response["HostName"] = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001658 if (!ethData.domainnames.empty())
1659 {
1660 json_response["FQDN"] =
1661 ethData.hostname + "." + ethData.domainnames[0];
1662 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001663 }
1664
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001665 json_response["VLANs"] = {
1666 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1667 iface_id + "/VLANs"}};
1668
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001669 if (translateDHCPEnabledToBool(ethData.DHCPEnabled, true) &&
1670 ethData.DNSEnabled)
Manojkiran Eda95f86462019-08-07 15:07:54 +05301671 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001672 json_response["StaticNameServers"] = nlohmann::json::array();
Manojkiran Eda95f86462019-08-07 15:07:54 +05301673 }
1674 else
1675 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001676 json_response["StaticNameServers"] = ethData.nameservers;
Manojkiran Eda95f86462019-08-07 15:07:54 +05301677 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001678
Ravi Tejad1d50812019-06-23 16:20:27 -05001679 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001680 nlohmann::json &ipv4_static_array =
1681 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001682 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001683 ipv4_static_array = nlohmann::json::array();
Ravi Tejad1d50812019-06-23 16:20:27 -05001684 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001685 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001686
1687 std::string gatewayStr = ipv4_config.gateway;
1688 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001689 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001690 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001691 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001692
1693 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1694 {"SubnetMask", ipv4_config.netmask},
1695 {"Address", ipv4_config.address},
1696 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001697 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001698 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001699 ipv4_static_array.push_back(
1700 {{"AddressOrigin", ipv4_config.origin},
1701 {"SubnetMask", ipv4_config.netmask},
1702 {"Address", ipv4_config.address},
1703 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001704 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001705 }
1706
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001707 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001708
1709 nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001710 nlohmann::json &ipv6_static_array =
1711 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001712 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001713 ipv6_static_array = nlohmann::json::array();
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001714 for (auto &ipv6_config : ipv6Data)
1715 {
1716 ipv6_array.push_back({{"Address", ipv6_config.address},
1717 {"PrefixLength", ipv6_config.prefixLength},
1718 {"AddressOrigin", ipv6_config.origin}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001719 if (ipv6_config.origin == "Static")
1720 {
1721 ipv6_static_array.push_back(
1722 {{"Address", ipv6_config.address},
1723 {"PrefixLength", ipv6_config.prefixLength},
1724 {"AddressOrigin", ipv6_config.origin}});
1725 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001726 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001727 }
1728
1729 /**
1730 * Functions triggers appropriate requests on DBus
1731 */
1732 void doGet(crow::Response &res, const crow::Request &req,
1733 const std::vector<std::string> &params) override
1734 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001735 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001736 if (params.size() != 1)
1737 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001738 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001739 return;
1740 }
1741
Ed Tanous4a0cb852018-10-15 07:55:04 -07001742 getEthernetIfaceData(
1743 params[0],
1744 [this, asyncResp, iface_id{std::string(params[0])}](
1745 const bool &success, 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 Tanous4a0cb852018-10-15 07:55:04 -07001748 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001749 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001750 // TODO(Pawel)consider distinguish between non existing
1751 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001752 messages::resourceNotFound(asyncResp->res,
1753 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001754 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001755 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001756
Ed Tanous0f74e642018-11-12 15:17:05 -08001757 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001758 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001759 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1760 asyncResp->res.jsonValue["Description"] =
1761 "Management Network Interface";
1762
1763 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07001764 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001765 });
1766 }
1767
1768 void doPatch(crow::Response &res, const crow::Request &req,
1769 const std::vector<std::string> &params) override
1770 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001771 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001772 if (params.size() != 1)
1773 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001774 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001775 return;
1776 }
1777
Ed Tanous4a0cb852018-10-15 07:55:04 -07001778 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001779
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001780 std::optional<std::string> hostname;
Ratan Guptad5776652019-03-03 08:47:22 +05301781 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001782 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001783 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001784 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001785 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001786 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001787 std::optional<nlohmann::json> dhcpv6;
1788 DHCPParameters v4dhcpParms;
1789 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001790
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001791 if (!json_util::readJson(
1792 req, res, "HostName", hostname, "IPv4StaticAddresses",
1793 ipv4StaticAddresses, "MACAddress", macAddress,
1794 "StaticNameServers", staticNameServers, "IPv6DefaultGateway",
1795 ipv6DefaultGateway, "IPv6StaticAddresses", ipv6StaticAddresses,
1796 "DHCPv4", dhcpv4, "DHCPv6", dhcpv6))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001797 {
1798 return;
1799 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001800 if (dhcpv4)
1801 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001802 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1803 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1804 v4dhcpParms.useDNSServers, "UseNTPServers",
1805 v4dhcpParms.useNTPServers, "UseDomainName",
1806 v4dhcpParms.useUseDomainName))
1807 {
1808 return;
1809 }
1810 }
1811
1812 if (dhcpv6)
1813 {
1814 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1815 v6dhcpParms.dhcpv6OperatingMode,
1816 "UseDNSServers", v6dhcpParms.useDNSServers,
1817 "UseNTPServers", v6dhcpParms.useNTPServers,
1818 "UseDomainName",
1819 v6dhcpParms.useUseDomainName))
1820 {
1821 return;
1822 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001823 }
1824
Johnathan Mantey01784822019-06-18 12:44:21 -07001825 // Get single eth interface data, and call the below callback for
1826 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001827 getEthernetIfaceData(
1828 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001829 [this, asyncResp, iface_id, hostname = std::move(hostname),
1830 macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001831 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001832 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001833 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001834 staticNameServers = std::move(staticNameServers),
1835 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1836 v4dhcpParms = std::move(v4dhcpParms),
1837 v6dhcpParms = std::move(v6dhcpParms)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001838 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001839 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001840 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001841 if (!success)
1842 {
1843 // ... otherwise return error
1844 // TODO(Pawel)consider distinguish between non existing
1845 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001846 messages::resourceNotFound(asyncResp->res,
1847 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001848 return;
1849 }
1850
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001851 if (dhcpv4 || dhcpv6)
1852 {
1853 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1854 std::move(v6dhcpParms), asyncResp);
1855 }
1856
Ed Tanous0627a2c2018-11-29 17:09:23 -08001857 if (hostname)
1858 {
1859 handleHostnamePatch(*hostname, asyncResp);
1860 }
1861
Ratan Guptad5776652019-03-03 08:47:22 +05301862 if (macAddress)
1863 {
1864 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
1865 }
1866
Ravi Tejad1d50812019-06-23 16:20:27 -05001867 if (ipv4StaticAddresses)
1868 {
Ed Tanous537174c2018-12-10 15:09:31 -08001869 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07001870 // above is returning a const value, not a non-const
1871 // value. This doesn't really work for us, as we need to
1872 // be able to efficiently move out the intermedia
1873 // nlohmann::json objects. This makes a copy of the
1874 // structure, and operates on that, but could be done
1875 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05001876 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07001877 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05001878 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08001879 }
1880
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001881 if (staticNameServers)
1882 {
1883 handleStaticNameServersPatch(iface_id, *staticNameServers,
1884 asyncResp);
1885 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001886
1887 if (ipv6DefaultGateway)
1888 {
1889 messages::propertyNotWritable(asyncResp->res,
1890 "IPv6DefaultGateway");
1891 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001892
1893 if (ipv6StaticAddresses)
1894 {
1895 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
1896 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07001897 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001898 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001899 });
1900 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001901};
1902
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001903/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001904 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1905 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001906 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001907class VlanNetworkInterface : public Node
1908{
1909 public:
1910 /*
1911 * Default Constructor
1912 */
1913 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001915 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001916 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001917 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001918 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001919 entityPrivileges = {
1920 {boost::beast::http::verb::get, {{"Login"}}},
1921 {boost::beast::http::verb::head, {{"Login"}}},
1922 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1923 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1924 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1925 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001926 }
1927
Ed Tanous1abe55e2018-09-05 08:30:59 -07001928 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001929 void parseInterfaceData(
1930 nlohmann::json &json_response, const std::string &parent_iface_id,
1931 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001932 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001933 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001934 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001935 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001936 json_response["Id"] = iface_id;
1937 json_response["@odata.id"] =
1938 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1939 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940
Ed Tanous4a0cb852018-10-15 07:55:04 -07001941 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001942 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001943 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001944 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07001945 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001946 }
1947
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001948 bool verifyNames(const std::string &parent, const std::string &iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001949 {
1950 if (!boost::starts_with(iface, parent + "_"))
1951 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001952 return false;
1953 }
1954 else
1955 {
1956 return true;
1957 }
1958 }
1959
1960 /**
1961 * Functions triggers appropriate requests on DBus
1962 */
1963 void doGet(crow::Response &res, const crow::Request &req,
1964 const std::vector<std::string> &params) override
1965 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001966 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1967 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001968 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1969 // Check if there is required param, truly entering this shall be
1970 // impossible.
1971 if (params.size() != 2)
1972 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001973 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001974 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001975 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001976 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001977
Ed Tanous4a0cb852018-10-15 07:55:04 -07001978 const std::string &parent_iface_id = params[0];
1979 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001980 res.jsonValue["@odata.type"] =
1981 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001982 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001983
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001984 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001985 {
1986 return;
1987 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001988
Johnathan Mantey01784822019-06-18 12:44:21 -07001989 // Get single eth interface data, and call the below callback for
1990 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001991 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001992 params[1],
1993 [this, asyncResp, parent_iface_id{std::string(params[0])},
1994 iface_id{std::string(params[1])}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001995 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001996 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001997 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001998 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001999 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002000 parseInterfaceData(asyncResp->res.jsonValue,
2001 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002002 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002003 }
2004 else
2005 {
2006 // ... otherwise return error
2007 // TODO(Pawel)consider distinguish between non existing
2008 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002009 messages::resourceNotFound(
2010 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002011 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002012 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002013 }
2014
Ed Tanous1abe55e2018-09-05 08:30:59 -07002015 void doPatch(crow::Response &res, const crow::Request &req,
2016 const std::vector<std::string> &params) override
2017 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002018 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002019 if (params.size() != 2)
2020 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002021 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002022 return;
2023 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002024
Ed Tanous1abe55e2018-09-05 08:30:59 -07002025 const std::string &parentIfaceId = params[0];
2026 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002027
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002028 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002029 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002030 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2031 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002032 return;
2033 }
2034
Ed Tanous0627a2c2018-11-29 17:09:23 -08002035 bool vlanEnable = false;
2036 uint64_t vlanId = 0;
2037
2038 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2039 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002040 {
2041 return;
2042 }
2043
Johnathan Mantey01784822019-06-18 12:44:21 -07002044 // Get single eth interface data, and call the below callback for
2045 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002046 getEthernetIfaceData(
2047 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002048 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002049 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2050 const bool &success, const EthernetInterfaceData &ethData,
2051 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002052 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002053 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002054 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002055 auto callback =
2056 [asyncResp](const boost::system::error_code ec) {
2057 if (ec)
2058 {
2059 messages::internalError(asyncResp->res);
2060 }
2061 };
2062
2063 if (vlanEnable == true)
2064 {
2065 crow::connections::systemBus->async_method_call(
2066 std::move(callback), "xyz.openbmc_project.Network",
2067 "/xyz/openbmc_project/network/" + ifaceId,
2068 "org.freedesktop.DBus.Properties", "Set",
2069 "xyz.openbmc_project.Network.VLAN", "Id",
2070 std::variant<uint32_t>(vlanId));
2071 }
2072 else
2073 {
2074 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2075 "vlan interface";
2076 crow::connections::systemBus->async_method_call(
2077 std::move(callback), "xyz.openbmc_project.Network",
2078 std::string("/xyz/openbmc_project/network/") +
2079 ifaceId,
2080 "xyz.openbmc_project.Object.Delete", "Delete");
2081 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002082 }
2083 else
2084 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002085 // TODO(Pawel)consider distinguish between non existing
2086 // object, and other errors
2087 messages::resourceNotFound(
2088 asyncResp->res, "VLAN Network Interface", ifaceId);
2089 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002090 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002091 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002092 }
2093
2094 void doDelete(crow::Response &res, const crow::Request &req,
2095 const std::vector<std::string> &params) override
2096 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002097 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002098 if (params.size() != 2)
2099 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002100 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002101 return;
2102 }
2103
2104 const std::string &parentIfaceId = params[0];
2105 const std::string &ifaceId = params[1];
2106
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002107 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002108 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002109 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2110 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002111 return;
2112 }
2113
Johnathan Mantey01784822019-06-18 12:44:21 -07002114 // Get single eth interface data, and call the below callback for
2115 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002116 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002117 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002118 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002119 ifaceId{std::string(params[1])}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002120 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002121 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002122 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002123 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002124 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002125 auto callback =
2126 [asyncResp](const boost::system::error_code ec) {
2127 if (ec)
2128 {
2129 messages::internalError(asyncResp->res);
2130 }
2131 };
2132 crow::connections::systemBus->async_method_call(
2133 std::move(callback), "xyz.openbmc_project.Network",
2134 std::string("/xyz/openbmc_project/network/") + ifaceId,
2135 "xyz.openbmc_project.Object.Delete", "Delete");
2136 }
2137 else
2138 {
2139 // ... otherwise return error
2140 // TODO(Pawel)consider distinguish between non existing
2141 // object, and other errors
2142 messages::resourceNotFound(
2143 asyncResp->res, "VLAN Network Interface", ifaceId);
2144 }
2145 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002146 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002147};
2148
2149/**
2150 * VlanNetworkInterfaceCollection derived class for delivering
2151 * VLANNetworkInterface Collection Schema
2152 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002153class VlanNetworkInterfaceCollection : public Node
2154{
2155 public:
2156 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002157 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002158 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2159 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002160 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002161 entityPrivileges = {
2162 {boost::beast::http::verb::get, {{"Login"}}},
2163 {boost::beast::http::verb::head, {{"Login"}}},
2164 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2165 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2166 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2167 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002168 }
2169
Ed Tanous1abe55e2018-09-05 08:30:59 -07002170 private:
2171 /**
2172 * Functions triggers appropriate requests on DBus
2173 */
2174 void doGet(crow::Response &res, const crow::Request &req,
2175 const std::vector<std::string> &params) override
2176 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002177 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002178 if (params.size() != 1)
2179 {
2180 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002181 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002182 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002183 }
2184
Ed Tanous4a0cb852018-10-15 07:55:04 -07002185 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002186
Ed Tanous4a0cb852018-10-15 07:55:04 -07002187 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002188 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002189 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002190 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002191 const bool &success,
Ed Tanous4c9afe42019-05-03 16:59:57 -07002192 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002193 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002194 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002195 messages::internalError(asyncResp->res);
2196 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002197 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002198
2199 if (iface_list.find(rootInterfaceName) == iface_list.end())
2200 {
2201 messages::resourceNotFound(asyncResp->res,
2202 "VLanNetworkInterfaceCollection",
2203 rootInterfaceName);
2204 return;
2205 }
2206
Ed Tanous0f74e642018-11-12 15:17:05 -08002207 asyncResp->res.jsonValue["@odata.type"] =
2208 "#VLanNetworkInterfaceCollection."
2209 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002210 asyncResp->res.jsonValue["Name"] =
2211 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002212
Jason M. Billsf12894f2018-10-09 12:45:45 -07002213 nlohmann::json iface_array = nlohmann::json::array();
2214
2215 for (const std::string &iface_item : iface_list)
2216 {
2217 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2218 {
2219 iface_array.push_back(
2220 {{"@odata.id",
2221 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2222 rootInterfaceName + "/VLANs/" + iface_item}});
2223 }
2224 }
2225
Jason M. Billsf12894f2018-10-09 12:45:45 -07002226 asyncResp->res.jsonValue["Members@odata.count"] =
2227 iface_array.size();
2228 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2229 asyncResp->res.jsonValue["@odata.id"] =
2230 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2231 rootInterfaceName + "/VLANs";
2232 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002233 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002234
Ed Tanous1abe55e2018-09-05 08:30:59 -07002235 void doPost(crow::Response &res, const crow::Request &req,
2236 const std::vector<std::string> &params) override
2237 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002238 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002239 if (params.size() != 1)
2240 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002241 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002242 return;
2243 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002244 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002245 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002246 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2247 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002248 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002249 return;
2250 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002251 // Need both vlanId and vlanEnable to service this request
2252 if (!vlanId)
2253 {
2254 messages::propertyMissing(asyncResp->res, "VLANId");
2255 }
2256 if (!vlanEnable)
2257 {
2258 messages::propertyMissing(asyncResp->res, "VLANEnable");
2259 }
Ed Tanous271584a2019-07-09 16:24:22 -07002260 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002261 {
2262 return;
2263 }
2264
Ed Tanous4a0cb852018-10-15 07:55:04 -07002265 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002266 auto callback = [asyncResp](const boost::system::error_code ec) {
2267 if (ec)
2268 {
2269 // TODO(ed) make more consistent error messages based on
2270 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002271 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002272 return;
2273 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002274 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002275 };
2276 crow::connections::systemBus->async_method_call(
2277 std::move(callback), "xyz.openbmc_project.Network",
2278 "/xyz/openbmc_project/network",
2279 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002280 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002281 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002282};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002283} // namespace redfish