blob: 4998a9500dffd36a3885aad6bdaddfa5f0e2d6c3 [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";
1008 res.jsonValue["@odata.context"] =
1009 "/redfish/v1/"
1010 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
1011 res.jsonValue["@odata.id"] =
1012 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1013 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1014 res.jsonValue["Description"] =
1015 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001016 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001017 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001019 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001020 [asyncResp](
1021 const bool &success,
1022 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001023 if (!success)
1024 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001025 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001026 return;
1027 }
1028
Ed Tanous4c9afe42019-05-03 16:59:57 -07001029 nlohmann::json &iface_array =
1030 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001031 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001032 std::string tag = "_";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001033 for (const std::string &iface_item : iface_list)
1034 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001035 std::size_t found = iface_item.find(tag);
1036 if (found == std::string::npos)
1037 {
1038 iface_array.push_back(
1039 {{"@odata.id",
1040 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1041 iface_item}});
1042 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001043 }
1044
Ed Tanous4c9afe42019-05-03 16:59:57 -07001045 asyncResp->res.jsonValue["Members@odata.count"] =
1046 iface_array.size();
1047 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001048 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001049 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001050 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001051};
1052
1053/**
1054 * EthernetInterface derived class for delivering Ethernet Schema
1055 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001056class EthernetInterface : public Node
1057{
1058 public:
1059 /*
1060 * Default Constructor
1061 */
Ed Tanous4a0cb852018-10-15 07:55:04 -07001062 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001063 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001064 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 std::string())
1066 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001067 entityPrivileges = {
1068 {boost::beast::http::verb::get, {{"Login"}}},
1069 {boost::beast::http::verb::head, {{"Login"}}},
1070 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1071 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1072 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1073 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001074 }
1075
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001077 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001078 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 {
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001080 asyncResp->res.jsonValue["HostName"] = hostname;
1081 crow::connections::systemBus->async_method_call(
1082 [asyncResp](const boost::system::error_code ec) {
1083 if (ec)
1084 {
1085 messages::internalError(asyncResp->res);
1086 }
1087 },
1088 "xyz.openbmc_project.Network",
1089 "/xyz/openbmc_project/network/config",
1090 "org.freedesktop.DBus.Properties", "Set",
1091 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001092 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001093 }
1094
Ratan Guptad5776652019-03-03 08:47:22 +05301095 void handleMACAddressPatch(const std::string &ifaceId,
1096 const std::string &macAddress,
1097 const std::shared_ptr<AsyncResp> &asyncResp)
1098 {
1099 crow::connections::systemBus->async_method_call(
1100 [asyncResp, macAddress](const boost::system::error_code ec) {
1101 if (ec)
1102 {
1103 messages::internalError(asyncResp->res);
1104 return;
1105 }
Ratan Guptad5776652019-03-03 08:47:22 +05301106 },
1107 "xyz.openbmc_project.Network",
1108 "/xyz/openbmc_project/network/" + ifaceId,
1109 "org.freedesktop.DBus.Properties", "Set",
1110 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1111 std::variant<std::string>(macAddress));
1112 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001113
Jennifer Leeda131a92019-04-24 15:13:55 -07001114 void setDHCPEnabled(const std::string &ifaceId,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001115 const std::string &propertyName, const bool v4Value,
1116 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001117 const std::shared_ptr<AsyncResp> asyncResp)
1118 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001119 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001120 crow::connections::systemBus->async_method_call(
1121 [asyncResp](const boost::system::error_code ec) {
1122 if (ec)
1123 {
1124 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1125 messages::internalError(asyncResp->res);
1126 return;
1127 }
1128 },
1129 "xyz.openbmc_project.Network",
1130 "/xyz/openbmc_project/network/" + ifaceId,
1131 "org.freedesktop.DBus.Properties", "Set",
1132 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001133 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001134 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001135
Jennifer Leeda131a92019-04-24 15:13:55 -07001136 void setDHCPv4Config(const std::string &propertyName, const bool &value,
1137 const std::shared_ptr<AsyncResp> asyncResp)
1138 {
1139 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1140 crow::connections::systemBus->async_method_call(
1141 [asyncResp](const boost::system::error_code ec) {
1142 if (ec)
1143 {
1144 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1145 messages::internalError(asyncResp->res);
1146 return;
1147 }
1148 },
1149 "xyz.openbmc_project.Network",
1150 "/xyz/openbmc_project/network/config/dhcp",
1151 "org.freedesktop.DBus.Properties", "Set",
1152 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1153 std::variant<bool>{value});
1154 }
Ratan Guptad5776652019-03-03 08:47:22 +05301155
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001156 void handleDHCPPatch(const std::string &ifaceId,
1157 const EthernetInterfaceData &ethData,
1158 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1159 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001160 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001161 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1162 bool ipv6Active =
1163 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001164
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001165 bool nextv4DHCPState =
1166 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1167
1168 bool nextv6DHCPState{};
1169 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001170 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001171 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1172 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1173 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1174 {
1175 messages::propertyValueFormatError(
1176 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1177 "OperatingMode");
1178 return;
1179 }
1180 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1181 }
1182 else
1183 {
1184 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001185 }
1186
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001187 bool nextDNS{};
1188 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001189 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001190 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1191 {
1192 messages::generalError(asyncResp->res);
1193 return;
1194 }
1195 nextDNS = *v4dhcpParms.useDNSServers;
1196 }
1197 else if (v4dhcpParms.useDNSServers)
1198 {
1199 nextDNS = *v4dhcpParms.useDNSServers;
1200 }
1201 else if (v6dhcpParms.useDNSServers)
1202 {
1203 nextDNS = *v6dhcpParms.useDNSServers;
1204 }
1205 else
1206 {
1207 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001208 }
1209
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001210 bool nextNTP{};
1211 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001212 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001213 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1214 {
1215 messages::generalError(asyncResp->res);
1216 return;
1217 }
1218 nextNTP = *v4dhcpParms.useNTPServers;
1219 }
1220 else if (v4dhcpParms.useNTPServers)
1221 {
1222 nextNTP = *v4dhcpParms.useNTPServers;
1223 }
1224 else if (v6dhcpParms.useNTPServers)
1225 {
1226 nextNTP = *v6dhcpParms.useNTPServers;
1227 }
1228 else
1229 {
1230 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001231 }
1232
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001233 bool nextUseDomain{};
1234 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001235 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001236 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1237 {
1238 messages::generalError(asyncResp->res);
1239 return;
1240 }
1241 nextUseDomain = *v4dhcpParms.useUseDomainName;
1242 }
1243 else if (v4dhcpParms.useUseDomainName)
1244 {
1245 nextUseDomain = *v4dhcpParms.useUseDomainName;
1246 }
1247 else if (v6dhcpParms.useUseDomainName)
1248 {
1249 nextUseDomain = *v6dhcpParms.useUseDomainName;
1250 }
1251 else
1252 {
1253 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001254 }
1255
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001256 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1257 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1258 asyncResp);
1259 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1260 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1261 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1262 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1263 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1264 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001265 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001266
1267 boost::container::flat_set<IPv4AddressData>::const_iterator
1268 GetNextStaticIPEntry(
1269 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1270 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1271 {
1272 for (; head != end; head++)
1273 {
1274 if (head->origin == "Static")
1275 {
1276 return head;
1277 }
1278 }
1279 return end;
1280 }
1281
1282 boost::container::flat_set<IPv6AddressData>::const_iterator
1283 GetNextStaticIPEntry(
1284 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1285 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1286 {
1287 for (; head != end; head++)
1288 {
1289 if (head->origin == "Static")
1290 {
1291 return head;
1292 }
1293 }
1294 return end;
1295 }
1296
Ravi Tejad1d50812019-06-23 16:20:27 -05001297 void handleIPv4StaticPatch(
Ratan Guptaf476acb2019-03-02 16:46:57 +05301298 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001299 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001300 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001301 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001302 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301303 {
1304 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001305 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301306 return;
1307 }
1308
Ed Tanous271584a2019-07-09 16:24:22 -07001309 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001310 // Find the first static IP address currently active on the NIC and
1311 // match it to the first JSON element in the IPv4StaticAddresses array.
1312 // Match each subsequent JSON element to the next static IP programmed
1313 // into the NIC.
1314 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1315 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1316
Ed Tanous537174c2018-12-10 15:09:31 -08001317 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001318 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001319 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001320 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001321
Johnathan Mantey01784822019-06-18 12:44:21 -07001322 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301323 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001324 std::optional<std::string> address;
1325 std::optional<std::string> subnetMask;
1326 std::optional<std::string> gateway;
1327
1328 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1329 address, "SubnetMask", subnetMask,
1330 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301331 {
1332 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001333 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301334 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301335 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301336
Johnathan Mantey01784822019-06-18 12:44:21 -07001337 // Find the address/subnet/gateway values. Any values that are
1338 // not explicitly provided are assumed to be unmodified from the
1339 // current state of the interface. Merge existing state into the
1340 // current request.
Ed Tanous271584a2019-07-09 16:24:22 -07001341 const std::string *addr = nullptr;
1342 const std::string *gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001343 uint8_t prefixLength = 0;
1344 bool errorInEntry = false;
1345 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301346 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001347 if (ipv4VerifyIpAndGetBitcount(*address))
1348 {
1349 addr = &(*address);
1350 }
1351 else
1352 {
1353 messages::propertyValueFormatError(
1354 asyncResp->res, *address, pathString + "/Address");
1355 errorInEntry = true;
1356 }
1357 }
1358 else if (NICIPentry != ipv4Data.cend())
1359 {
1360 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301361 }
1362 else
1363 {
1364 messages::propertyMissing(asyncResp->res,
1365 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001366 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001367 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301368
1369 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001370 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001371 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1372 {
1373 messages::propertyValueFormatError(
1374 asyncResp->res, *subnetMask,
1375 pathString + "/SubnetMask");
1376 errorInEntry = true;
1377 }
1378 }
1379 else if (NICIPentry != ipv4Data.cend())
1380 {
1381 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1382 &prefixLength))
1383 {
1384 messages::propertyValueFormatError(
1385 asyncResp->res, NICIPentry->netmask,
1386 pathString + "/SubnetMask");
1387 errorInEntry = true;
1388 }
1389 }
1390 else
1391 {
1392 messages::propertyMissing(asyncResp->res,
1393 pathString + "/SubnetMask");
1394 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001395 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301396
Ratan Guptaf476acb2019-03-02 16:46:57 +05301397 if (gateway)
1398 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001399 if (ipv4VerifyIpAndGetBitcount(*gateway))
1400 {
1401 gw = &(*gateway);
1402 }
1403 else
1404 {
1405 messages::propertyValueFormatError(
1406 asyncResp->res, *gateway, pathString + "/Gateway");
1407 errorInEntry = true;
1408 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301409 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001410 else if (NICIPentry != ipv4Data.cend())
1411 {
1412 gw = &NICIPentry->gateway;
1413 }
1414 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001415 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001416 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001417 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001418 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001419 }
1420
Johnathan Mantey01784822019-06-18 12:44:21 -07001421 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001422 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001423 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001424 }
1425
Johnathan Mantey01784822019-06-18 12:44:21 -07001426 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001427 {
Ed Tanous271584a2019-07-09 16:24:22 -07001428 if (gw != nullptr || addr != nullptr)
1429 {
1430 // Shouldn't be possible based on errorInEntry, but
1431 // it flags -wmaybe-uninitialized in the compiler,
1432 // so defend against that
1433 return;
1434 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001435 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1436 *gw, *addr, asyncResp);
1437 NICIPentry =
1438 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001439 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001440 else
1441 {
1442 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1443 *address, asyncResp);
1444 }
1445 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001446 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001447 else
1448 {
1449 if (NICIPentry == ipv4Data.cend())
1450 {
1451 // Requesting a DELETE/DO NOT MODIFY action for an item
1452 // that isn't present on the eth(n) interface. Input JSON is
1453 // in error, so bail out.
1454 if (thisJson.is_null())
1455 {
1456 messages::resourceCannotBeDeleted(asyncResp->res);
1457 return;
1458 }
1459 else
1460 {
1461 messages::propertyValueFormatError(
1462 asyncResp->res, thisJson.dump(), pathString);
1463 return;
1464 }
1465 }
1466
1467 if (thisJson.is_null())
1468 {
1469 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1470 }
1471 if (NICIPentry != ipv4Data.cend())
1472 {
1473 NICIPentry =
1474 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1475 }
1476 entryIdx++;
1477 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001478 }
1479 }
1480
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001481 void handleStaticNameServersPatch(
1482 const std::string &ifaceId,
1483 const std::vector<std::string> &updatedStaticNameServers,
1484 const std::shared_ptr<AsyncResp> &asyncResp)
1485 {
1486 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001487 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001488 if (ec)
1489 {
1490 messages::internalError(asyncResp->res);
1491 return;
1492 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001493 },
1494 "xyz.openbmc_project.Network",
1495 "/xyz/openbmc_project/network/" + ifaceId,
1496 "org.freedesktop.DBus.Properties", "Set",
1497 "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1498 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1499 }
1500
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001501 void handleIPv6StaticAddressesPatch(
1502 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001503 const boost::container::flat_set<IPv6AddressData> &ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001504 const std::shared_ptr<AsyncResp> asyncResp)
1505 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001506 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001507 {
1508 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1509 "IPv6StaticAddresses");
1510 return;
1511 }
Ed Tanous271584a2019-07-09 16:24:22 -07001512 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001513 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1514 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001515 for (nlohmann::json &thisJson : input)
1516 {
1517 std::string pathString =
1518 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1519
Johnathan Mantey01784822019-06-18 12:44:21 -07001520 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001521 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001522 std::optional<std::string> address;
1523 std::optional<uint8_t> prefixLength;
1524
1525 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1526 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001527 {
1528 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001529 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001530 return;
1531 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001532
Johnathan Mantey01784822019-06-18 12:44:21 -07001533 const std::string *addr;
1534 uint8_t prefix;
1535
1536 // Find the address and prefixLength values. Any values that are
1537 // not explicitly provided are assumed to be unmodified from the
1538 // current state of the interface. Merge existing state into the
1539 // current request.
1540 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001541 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001542 addr = &(*address);
1543 }
1544 else if (NICIPentry != ipv6Data.end())
1545 {
1546 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001547 }
1548 else
1549 {
1550 messages::propertyMissing(asyncResp->res,
1551 pathString + "/Address");
1552 return;
1553 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001554
1555 if (prefixLength)
1556 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001557 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001558 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001559 else if (NICIPentry != ipv6Data.end())
1560 {
1561 prefix = NICIPentry->prefixLength;
1562 }
1563 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001564 {
1565 messages::propertyMissing(asyncResp->res,
1566 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001567 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001568 }
1569
Johnathan Mantey01784822019-06-18 12:44:21 -07001570 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001571 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001572 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1573 asyncResp);
1574 NICIPentry =
1575 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1576 }
1577 else
1578 {
1579 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1580 }
1581 entryIdx++;
1582 }
1583 else
1584 {
1585 if (NICIPentry == ipv6Data.end())
1586 {
1587 // Requesting a DELETE/DO NOT MODIFY action for an item
1588 // that isn't present on the eth(n) interface. Input JSON is
1589 // in error, so bail out.
1590 if (thisJson.is_null())
1591 {
1592 messages::resourceCannotBeDeleted(asyncResp->res);
1593 return;
1594 }
1595 else
1596 {
1597 messages::propertyValueFormatError(
1598 asyncResp->res, thisJson.dump(), pathString);
1599 return;
1600 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001601 }
1602
Johnathan Mantey01784822019-06-18 12:44:21 -07001603 if (thisJson.is_null())
1604 {
1605 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1606 }
1607 if (NICIPentry != ipv6Data.cend())
1608 {
1609 NICIPentry =
1610 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1611 }
1612 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001613 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001614 }
1615 }
1616
Ed Tanous0f74e642018-11-12 15:17:05 -08001617 void parseInterfaceData(
1618 nlohmann::json &json_response, const std::string &iface_id,
1619 const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001620 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001621 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001622 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001623 json_response["Id"] = iface_id;
1624 json_response["@odata.id"] =
1625 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous029573d2019-02-01 10:57:49 -08001626 json_response["InterfaceEnabled"] = true;
1627 if (ethData.speed == 0)
1628 {
Ed Tanous029573d2019-02-01 10:57:49 -08001629 json_response["Status"] = {
1630 {"Health", "OK"},
1631 {"State", "Disabled"},
1632 };
1633 }
1634 else
1635 {
Ed Tanous029573d2019-02-01 10:57:49 -08001636 json_response["Status"] = {
1637 {"Health", "OK"},
1638 {"State", "Enabled"},
1639 };
1640 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001641
1642 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001643 json_response["SpeedMbps"] = ethData.speed;
1644 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001645 json_response["DHCPv4"]["DHCPEnabled"] =
1646 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1647 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1648 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1649 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1650
1651 json_response["DHCPv6"]["OperatingMode"] =
1652 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1653 : "Disabled";
1654 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1655 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1656 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301657
Ed Tanous4a0cb852018-10-15 07:55:04 -07001658 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001659 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001660 json_response["HostName"] = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001661 if (!ethData.domainnames.empty())
1662 {
1663 json_response["FQDN"] =
1664 ethData.hostname + "." + ethData.domainnames[0];
1665 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001666 }
1667
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001668 json_response["VLANs"] = {
1669 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1670 iface_id + "/VLANs"}};
1671
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001672 if (translateDHCPEnabledToBool(ethData.DHCPEnabled, true) &&
1673 ethData.DNSEnabled)
Manojkiran Eda95f86462019-08-07 15:07:54 +05301674 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001675 json_response["StaticNameServers"] = nlohmann::json::array();
Manojkiran Eda95f86462019-08-07 15:07:54 +05301676 }
1677 else
1678 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001679 json_response["StaticNameServers"] = ethData.nameservers;
Manojkiran Eda95f86462019-08-07 15:07:54 +05301680 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001681
Ravi Tejad1d50812019-06-23 16:20:27 -05001682 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001683 nlohmann::json &ipv4_static_array =
1684 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001685 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001686 ipv4_static_array = nlohmann::json::array();
Ravi Tejad1d50812019-06-23 16:20:27 -05001687 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001688 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001689
1690 std::string gatewayStr = ipv4_config.gateway;
1691 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001692 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001693 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001694 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001695
1696 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1697 {"SubnetMask", ipv4_config.netmask},
1698 {"Address", ipv4_config.address},
1699 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001700 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001701 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001702 ipv4_static_array.push_back(
1703 {{"AddressOrigin", ipv4_config.origin},
1704 {"SubnetMask", ipv4_config.netmask},
1705 {"Address", ipv4_config.address},
1706 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001707 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001708 }
1709
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001710 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001711
1712 nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001713 nlohmann::json &ipv6_static_array =
1714 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001715 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001716 ipv6_static_array = nlohmann::json::array();
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001717 for (auto &ipv6_config : ipv6Data)
1718 {
1719 ipv6_array.push_back({{"Address", ipv6_config.address},
1720 {"PrefixLength", ipv6_config.prefixLength},
1721 {"AddressOrigin", ipv6_config.origin}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001722 if (ipv6_config.origin == "Static")
1723 {
1724 ipv6_static_array.push_back(
1725 {{"Address", ipv6_config.address},
1726 {"PrefixLength", ipv6_config.prefixLength},
1727 {"AddressOrigin", ipv6_config.origin}});
1728 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001729 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001730 }
1731
1732 /**
1733 * Functions triggers appropriate requests on DBus
1734 */
1735 void doGet(crow::Response &res, const crow::Request &req,
1736 const std::vector<std::string> &params) override
1737 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001738 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001739 if (params.size() != 1)
1740 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001741 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742 return;
1743 }
1744
Ed Tanous4a0cb852018-10-15 07:55:04 -07001745 getEthernetIfaceData(
1746 params[0],
1747 [this, asyncResp, iface_id{std::string(params[0])}](
1748 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001749 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001750 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001751 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001752 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001753 // TODO(Pawel)consider distinguish between non existing
1754 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001755 messages::resourceNotFound(asyncResp->res,
1756 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001757 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001758 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001759
Ed Tanous0f74e642018-11-12 15:17:05 -08001760 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001761 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001762 asyncResp->res.jsonValue["@odata.context"] =
Johnathan Mantey01784822019-06-18 12:44:21 -07001763 "/redfish/v1/"
1764 "$metadata#EthernetInterface.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001765 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1766 asyncResp->res.jsonValue["Description"] =
1767 "Management Network Interface";
1768
1769 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07001770 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001771 });
1772 }
1773
1774 void doPatch(crow::Response &res, const crow::Request &req,
1775 const std::vector<std::string> &params) override
1776 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001777 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001778 if (params.size() != 1)
1779 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001780 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001781 return;
1782 }
1783
Ed Tanous4a0cb852018-10-15 07:55:04 -07001784 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001785
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001786 std::optional<std::string> hostname;
Ratan Guptad5776652019-03-03 08:47:22 +05301787 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001788 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001789 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001790 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001791 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001792 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001793 std::optional<nlohmann::json> dhcpv6;
1794 DHCPParameters v4dhcpParms;
1795 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001796
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001797 if (!json_util::readJson(
1798 req, res, "HostName", hostname, "IPv4StaticAddresses",
1799 ipv4StaticAddresses, "MACAddress", macAddress,
1800 "StaticNameServers", staticNameServers, "IPv6DefaultGateway",
1801 ipv6DefaultGateway, "IPv6StaticAddresses", ipv6StaticAddresses,
1802 "DHCPv4", dhcpv4, "DHCPv6", dhcpv6))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001803 {
1804 return;
1805 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001806 if (dhcpv4)
1807 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001808 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1809 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1810 v4dhcpParms.useDNSServers, "UseNTPServers",
1811 v4dhcpParms.useNTPServers, "UseDomainName",
1812 v4dhcpParms.useUseDomainName))
1813 {
1814 return;
1815 }
1816 }
1817
1818 if (dhcpv6)
1819 {
1820 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1821 v6dhcpParms.dhcpv6OperatingMode,
1822 "UseDNSServers", v6dhcpParms.useDNSServers,
1823 "UseNTPServers", v6dhcpParms.useNTPServers,
1824 "UseDomainName",
1825 v6dhcpParms.useUseDomainName))
1826 {
1827 return;
1828 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001829 }
1830
Johnathan Mantey01784822019-06-18 12:44:21 -07001831 // Get single eth interface data, and call the below callback for
1832 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001833 getEthernetIfaceData(
1834 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001835 [this, asyncResp, iface_id, hostname = std::move(hostname),
1836 macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001837 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001838 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001839 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001840 staticNameServers = std::move(staticNameServers),
1841 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1842 v4dhcpParms = std::move(v4dhcpParms),
1843 v6dhcpParms = std::move(v6dhcpParms)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001844 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001845 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001846 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001847 if (!success)
1848 {
1849 // ... otherwise return error
1850 // TODO(Pawel)consider distinguish between non existing
1851 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001852 messages::resourceNotFound(asyncResp->res,
1853 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001854 return;
1855 }
1856
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001857 if (dhcpv4 || dhcpv6)
1858 {
1859 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1860 std::move(v6dhcpParms), asyncResp);
1861 }
1862
Ed Tanous0627a2c2018-11-29 17:09:23 -08001863 if (hostname)
1864 {
1865 handleHostnamePatch(*hostname, asyncResp);
1866 }
1867
Ratan Guptad5776652019-03-03 08:47:22 +05301868 if (macAddress)
1869 {
1870 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
1871 }
1872
Ravi Tejad1d50812019-06-23 16:20:27 -05001873 if (ipv4StaticAddresses)
1874 {
Ed Tanous537174c2018-12-10 15:09:31 -08001875 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07001876 // above is returning a const value, not a non-const
1877 // value. This doesn't really work for us, as we need to
1878 // be able to efficiently move out the intermedia
1879 // nlohmann::json objects. This makes a copy of the
1880 // structure, and operates on that, but could be done
1881 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05001882 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07001883 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05001884 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08001885 }
1886
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001887 if (staticNameServers)
1888 {
1889 handleStaticNameServersPatch(iface_id, *staticNameServers,
1890 asyncResp);
1891 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001892
1893 if (ipv6DefaultGateway)
1894 {
1895 messages::propertyNotWritable(asyncResp->res,
1896 "IPv6DefaultGateway");
1897 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001898
1899 if (ipv6StaticAddresses)
1900 {
1901 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
1902 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07001903 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001904 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001905 });
1906 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001907};
1908
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001909/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001910 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1911 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001912 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001913class VlanNetworkInterface : public Node
1914{
1915 public:
1916 /*
1917 * Default Constructor
1918 */
1919 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001920 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001921 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001922 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001923 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001924 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001925 entityPrivileges = {
1926 {boost::beast::http::verb::get, {{"Login"}}},
1927 {boost::beast::http::verb::head, {{"Login"}}},
1928 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1929 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1930 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1931 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001932 }
1933
Ed Tanous1abe55e2018-09-05 08:30:59 -07001934 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001935 void parseInterfaceData(
1936 nlohmann::json &json_response, const std::string &parent_iface_id,
1937 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001938 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001939 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001941 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001942 json_response["Id"] = iface_id;
1943 json_response["@odata.id"] =
1944 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1945 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001946
Ed Tanous4a0cb852018-10-15 07:55:04 -07001947 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001948 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001949 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001950 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07001951 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001952 }
1953
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001954 bool verifyNames(const std::string &parent, const std::string &iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001955 {
1956 if (!boost::starts_with(iface, parent + "_"))
1957 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001958 return false;
1959 }
1960 else
1961 {
1962 return true;
1963 }
1964 }
1965
1966 /**
1967 * Functions triggers appropriate requests on DBus
1968 */
1969 void doGet(crow::Response &res, const crow::Request &req,
1970 const std::vector<std::string> &params) override
1971 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001972 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1973 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001974 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1975 // Check if there is required param, truly entering this shall be
1976 // impossible.
1977 if (params.size() != 2)
1978 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001979 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001980 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001981 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001982 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001983
Ed Tanous4a0cb852018-10-15 07:55:04 -07001984 const std::string &parent_iface_id = params[0];
1985 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001986 res.jsonValue["@odata.type"] =
1987 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1988 res.jsonValue["@odata.context"] =
Johnathan Mantey01784822019-06-18 12:44:21 -07001989 "/redfish/v1/"
1990 "$metadata#VLanNetworkInterface.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001991 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001992
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001993 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001994 {
1995 return;
1996 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001997
Johnathan Mantey01784822019-06-18 12:44:21 -07001998 // Get single eth interface data, and call the below callback for
1999 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002000 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002001 params[1],
2002 [this, asyncResp, parent_iface_id{std::string(params[0])},
2003 iface_id{std::string(params[1])}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07002004 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002005 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002006 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002007 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002008 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002009 parseInterfaceData(asyncResp->res.jsonValue,
2010 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002011 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002012 }
2013 else
2014 {
2015 // ... otherwise return error
2016 // TODO(Pawel)consider distinguish between non existing
2017 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002018 messages::resourceNotFound(
2019 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002020 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002021 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002022 }
2023
Ed Tanous1abe55e2018-09-05 08:30:59 -07002024 void doPatch(crow::Response &res, const crow::Request &req,
2025 const std::vector<std::string> &params) override
2026 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002027 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002028 if (params.size() != 2)
2029 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002030 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002031 return;
2032 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002033
Ed Tanous1abe55e2018-09-05 08:30:59 -07002034 const std::string &parentIfaceId = params[0];
2035 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002036
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002037 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002038 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002039 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2040 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002041 return;
2042 }
2043
Ed Tanous0627a2c2018-11-29 17:09:23 -08002044 bool vlanEnable = false;
2045 uint64_t vlanId = 0;
2046
2047 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2048 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002049 {
2050 return;
2051 }
2052
Johnathan Mantey01784822019-06-18 12:44:21 -07002053 // Get single eth interface data, and call the below callback for
2054 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002055 getEthernetIfaceData(
2056 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002057 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002058 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2059 const bool &success, const EthernetInterfaceData &ethData,
2060 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002061 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002062 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002063 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002064 auto callback =
2065 [asyncResp](const boost::system::error_code ec) {
2066 if (ec)
2067 {
2068 messages::internalError(asyncResp->res);
2069 }
2070 };
2071
2072 if (vlanEnable == true)
2073 {
2074 crow::connections::systemBus->async_method_call(
2075 std::move(callback), "xyz.openbmc_project.Network",
2076 "/xyz/openbmc_project/network/" + ifaceId,
2077 "org.freedesktop.DBus.Properties", "Set",
2078 "xyz.openbmc_project.Network.VLAN", "Id",
2079 std::variant<uint32_t>(vlanId));
2080 }
2081 else
2082 {
2083 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2084 "vlan interface";
2085 crow::connections::systemBus->async_method_call(
2086 std::move(callback), "xyz.openbmc_project.Network",
2087 std::string("/xyz/openbmc_project/network/") +
2088 ifaceId,
2089 "xyz.openbmc_project.Object.Delete", "Delete");
2090 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002091 }
2092 else
2093 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002094 // TODO(Pawel)consider distinguish between non existing
2095 // object, and other errors
2096 messages::resourceNotFound(
2097 asyncResp->res, "VLAN Network Interface", ifaceId);
2098 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002099 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002100 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002101 }
2102
2103 void doDelete(crow::Response &res, const crow::Request &req,
2104 const std::vector<std::string> &params) override
2105 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002106 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002107 if (params.size() != 2)
2108 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002109 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002110 return;
2111 }
2112
2113 const std::string &parentIfaceId = params[0];
2114 const std::string &ifaceId = params[1];
2115
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002116 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002117 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002118 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2119 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002120 return;
2121 }
2122
Johnathan Mantey01784822019-06-18 12:44:21 -07002123 // Get single eth interface data, and call the below callback for
2124 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002125 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002126 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002127 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002128 ifaceId{std::string(params[1])}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002129 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002130 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002131 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002132 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002133 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002134 auto callback =
2135 [asyncResp](const boost::system::error_code ec) {
2136 if (ec)
2137 {
2138 messages::internalError(asyncResp->res);
2139 }
2140 };
2141 crow::connections::systemBus->async_method_call(
2142 std::move(callback), "xyz.openbmc_project.Network",
2143 std::string("/xyz/openbmc_project/network/") + ifaceId,
2144 "xyz.openbmc_project.Object.Delete", "Delete");
2145 }
2146 else
2147 {
2148 // ... otherwise return error
2149 // TODO(Pawel)consider distinguish between non existing
2150 // object, and other errors
2151 messages::resourceNotFound(
2152 asyncResp->res, "VLAN Network Interface", ifaceId);
2153 }
2154 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002155 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002156};
2157
2158/**
2159 * VlanNetworkInterfaceCollection derived class for delivering
2160 * VLANNetworkInterface Collection Schema
2161 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002162class VlanNetworkInterfaceCollection : public Node
2163{
2164 public:
2165 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002166 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002167 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2168 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002169 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002170 entityPrivileges = {
2171 {boost::beast::http::verb::get, {{"Login"}}},
2172 {boost::beast::http::verb::head, {{"Login"}}},
2173 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2174 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2175 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2176 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002177 }
2178
Ed Tanous1abe55e2018-09-05 08:30:59 -07002179 private:
2180 /**
2181 * Functions triggers appropriate requests on DBus
2182 */
2183 void doGet(crow::Response &res, const crow::Request &req,
2184 const std::vector<std::string> &params) override
2185 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002186 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 if (params.size() != 1)
2188 {
2189 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002190 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002191 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002192 }
2193
Ed Tanous4a0cb852018-10-15 07:55:04 -07002194 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002195
Ed Tanous4a0cb852018-10-15 07:55:04 -07002196 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002197 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002198 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002199 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002200 const bool &success,
Ed Tanous4c9afe42019-05-03 16:59:57 -07002201 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002202 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002203 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002204 messages::internalError(asyncResp->res);
2205 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002206 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002207
2208 if (iface_list.find(rootInterfaceName) == iface_list.end())
2209 {
2210 messages::resourceNotFound(asyncResp->res,
2211 "VLanNetworkInterfaceCollection",
2212 rootInterfaceName);
2213 return;
2214 }
2215
Ed Tanous0f74e642018-11-12 15:17:05 -08002216 asyncResp->res.jsonValue["@odata.type"] =
2217 "#VLanNetworkInterfaceCollection."
2218 "VLanNetworkInterfaceCollection";
2219 asyncResp->res.jsonValue["@odata.context"] =
2220 "/redfish/v1/$metadata"
2221 "#VLanNetworkInterfaceCollection."
2222 "VLanNetworkInterfaceCollection";
2223 asyncResp->res.jsonValue["Name"] =
2224 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002225
Jason M. Billsf12894f2018-10-09 12:45:45 -07002226 nlohmann::json iface_array = nlohmann::json::array();
2227
2228 for (const std::string &iface_item : iface_list)
2229 {
2230 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2231 {
2232 iface_array.push_back(
2233 {{"@odata.id",
2234 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2235 rootInterfaceName + "/VLANs/" + iface_item}});
2236 }
2237 }
2238
Jason M. Billsf12894f2018-10-09 12:45:45 -07002239 asyncResp->res.jsonValue["Members@odata.count"] =
2240 iface_array.size();
2241 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2242 asyncResp->res.jsonValue["@odata.id"] =
2243 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2244 rootInterfaceName + "/VLANs";
2245 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002246 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002247
Ed Tanous1abe55e2018-09-05 08:30:59 -07002248 void doPost(crow::Response &res, const crow::Request &req,
2249 const std::vector<std::string> &params) override
2250 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002251 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002252 if (params.size() != 1)
2253 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002254 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002255 return;
2256 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002257 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002258 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002259 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2260 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002261 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002262 return;
2263 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002264 // Need both vlanId and vlanEnable to service this request
2265 if (!vlanId)
2266 {
2267 messages::propertyMissing(asyncResp->res, "VLANId");
2268 }
2269 if (!vlanEnable)
2270 {
2271 messages::propertyMissing(asyncResp->res, "VLANEnable");
2272 }
Ed Tanous271584a2019-07-09 16:24:22 -07002273 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002274 {
2275 return;
2276 }
2277
Ed Tanous4a0cb852018-10-15 07:55:04 -07002278 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002279 auto callback = [asyncResp](const boost::system::error_code ec) {
2280 if (ec)
2281 {
2282 // TODO(ed) make more consistent error messages based on
2283 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002284 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002285 return;
2286 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002287 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002288 };
2289 crow::connections::systemBus->async_method_call(
2290 std::move(callback), "xyz.openbmc_project.Network",
2291 "/xyz/openbmc_project/network",
2292 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002293 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002294 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002295};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002296} // namespace redfish