blob: b6f3baa9e1ce3d5a9be70a51dff8c093021c1811 [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;
100 std::string DHCPEnabled;
101 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700102 std::string hostname;
103 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500104 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700105 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500106 std::vector<std::uint32_t> vlan_id;
Ed Tanous029573d2019-02-01 10:57:49 -0800107 std::vector<std::string> nameservers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800108 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100109};
110
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700111struct DHCPParameters
112{
113 std::optional<bool> dhcpv4Enabled;
114 std::optional<bool> useDNSServers;
115 std::optional<bool> useNTPServers;
116 std::optional<bool> useUseDomainName;
117 std::optional<std::string> dhcpv6OperatingMode;
118};
119
Ed Tanous4a0cb852018-10-15 07:55:04 -0700120// Helper function that changes bits netmask notation (i.e. /24)
121// into full dot notation
122inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700124 uint32_t value = 0xffffffff << (32 - bits);
125 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
126 std::to_string((value >> 16) & 0xff) + "." +
127 std::to_string((value >> 8) & 0xff) + "." +
128 std::to_string(value & 0xff);
129 return netmask;
130}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100131
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700132inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
133 bool isIPv4)
134{
135 if (isIPv4)
136 {
137 return (
138 (inputDHCP ==
139 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
140 (inputDHCP ==
141 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
142 }
143 return ((inputDHCP ==
144 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
145 (inputDHCP ==
146 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
147}
148
149inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
150{
151 if (isIPv4 && isIPv6)
152 {
153 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
154 }
155 else if (isIPv4)
156 {
157 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
158 }
159 else if (isIPv6)
160 {
161 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
162 }
163 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
164}
165
Ed Tanous4a0cb852018-10-15 07:55:04 -0700166inline std::string
167 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
168 bool isIPv4)
169{
170 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700171 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700172 return "Static";
173 }
174 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
175 {
176 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700177 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700178 return "IPv4LinkLocal";
179 }
180 else
181 {
182 return "LinkLocal";
183 }
184 }
185 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
186 {
187 if (isIPv4)
188 {
189 return "DHCP";
190 }
191 else
192 {
193 return "DHCPv6";
194 }
195 }
196 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
197 {
198 return "SLAAC";
199 }
200 return "";
201}
202
Ed Tanous4c9afe42019-05-03 16:59:57 -0700203inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700204 const GetManagedObjects &dbus_data,
205 EthernetInterfaceData &ethData)
206{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700207 bool idFound = false;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700208 for (const auto &objpath : dbus_data)
209 {
Ed Tanous029573d2019-02-01 10:57:49 -0800210 for (const auto &ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 {
Ed Tanous029573d2019-02-01 10:57:49 -0800212 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700214 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700215 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700217 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700218 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700221 const std::string *mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800222 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700223 if (mac != nullptr)
224 {
225 ethData.mac_address = *mac;
226 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700228 }
229 }
230 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
231 {
232 for (const auto &propertyPair : ifacePair.second)
233 {
234 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700235 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800236 const uint32_t *id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800237 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700238 if (id != nullptr)
239 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500240 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700243 }
244 }
245 else if (ifacePair.first ==
246 "xyz.openbmc_project.Network.EthernetInterface")
247 {
248 for (const auto &propertyPair : ifacePair.second)
249 {
250 if (propertyPair.first == "AutoNeg")
251 {
252 const bool *auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800253 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254 if (auto_neg != nullptr)
255 {
256 ethData.auto_neg = *auto_neg;
257 }
258 }
259 else if (propertyPair.first == "Speed")
260 {
261 const uint32_t *speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800262 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700263 if (speed != nullptr)
264 {
265 ethData.speed = *speed;
266 }
267 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500268 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700269 {
Ed Tanous029573d2019-02-01 10:57:49 -0800270 const std::vector<std::string> *nameservers =
271 sdbusplus::message::variant_ns::get_if<
272 std::vector<std::string>>(
273 &propertyPair.second);
274 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700275 {
Ed Tanous029573d2019-02-01 10:57:49 -0800276 ethData.nameservers = std::move(*nameservers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700277 }
278 }
manojkiraneda2a133282019-02-19 13:09:43 +0530279 else if (propertyPair.first == "DHCPEnabled")
280 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700281 const std::string *DHCPEnabled =
282 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530283 if (DHCPEnabled != nullptr)
284 {
285 ethData.DHCPEnabled = *DHCPEnabled;
286 }
287 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800288 else if (propertyPair.first == "DomainName")
289 {
290 const std::vector<std::string> *domainNames =
291 sdbusplus::message::variant_ns::get_if<
292 std::vector<std::string>>(
293 &propertyPair.second);
294 if (domainNames != nullptr)
295 {
296 ethData.domainnames = std::move(*domainNames);
297 }
298 }
Ed Tanous029573d2019-02-01 10:57:49 -0800299 }
300 }
301 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700302
303 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
304 {
305 if (ifacePair.first ==
306 "xyz.openbmc_project.Network.DHCPConfiguration")
307 {
308 for (const auto &propertyPair : ifacePair.second)
309 {
310 if (propertyPair.first == "DNSEnabled")
311 {
312 const bool *DNSEnabled =
313 std::get_if<bool>(&propertyPair.second);
314 if (DNSEnabled != nullptr)
315 {
316 ethData.DNSEnabled = *DNSEnabled;
317 }
318 }
319 else if (propertyPair.first == "NTPEnabled")
320 {
321 const bool *NTPEnabled =
322 std::get_if<bool>(&propertyPair.second);
323 if (NTPEnabled != nullptr)
324 {
325 ethData.NTPEnabled = *NTPEnabled;
326 }
327 }
328 else if (propertyPair.first == "HostNameEnabled")
329 {
330 const bool *HostNameEnabled =
331 std::get_if<bool>(&propertyPair.second);
332 if (HostNameEnabled != nullptr)
333 {
334 ethData.HostNameEnabled = *HostNameEnabled;
335 }
336 }
337 else if (propertyPair.first == "SendHostNameEnabled")
338 {
339 const bool *SendHostNameEnabled =
340 std::get_if<bool>(&propertyPair.second);
341 if (SendHostNameEnabled != nullptr)
342 {
343 ethData.SendHostNameEnabled =
344 *SendHostNameEnabled;
345 }
346 }
347 }
348 }
349 }
Ed Tanous029573d2019-02-01 10:57:49 -0800350 // System configuration shows up in the global namespace, so no need
351 // to check eth number
352 if (ifacePair.first ==
353 "xyz.openbmc_project.Network.SystemConfiguration")
354 {
355 for (const auto &propertyPair : ifacePair.second)
356 {
357 if (propertyPair.first == "HostName")
358 {
359 const std::string *hostname =
360 sdbusplus::message::variant_ns::get_if<std::string>(
361 &propertyPair.second);
362 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700363 {
Ed Tanous029573d2019-02-01 10:57:49 -0800364 ethData.hostname = *hostname;
365 }
366 }
367 else if (propertyPair.first == "DefaultGateway")
368 {
369 const std::string *defaultGateway =
370 sdbusplus::message::variant_ns::get_if<std::string>(
371 &propertyPair.second);
372 if (defaultGateway != nullptr)
373 {
374 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700375 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700376 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500377 else if (propertyPair.first == "DefaultGateway6")
378 {
379 const std::string *defaultGateway6 =
380 sdbusplus::message::variant_ns::get_if<std::string>(
381 &propertyPair.second);
382 if (defaultGateway6 != nullptr)
383 {
384 ethData.ipv6_default_gateway = *defaultGateway6;
385 }
386 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 }
388 }
389 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700391 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700392}
393
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500394// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700395inline void
396 extractIPV6Data(const std::string &ethiface_id,
397 const GetManagedObjects &dbus_data,
398 boost::container::flat_set<IPv6AddressData> &ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500399{
400 const std::string ipv6PathStart =
401 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
402
403 // Since there might be several IPv6 configurations aligned with
404 // single ethernet interface, loop over all of them
405 for (const auto &objpath : dbus_data)
406 {
407 // Check if proper pattern for object path appears
408 if (boost::starts_with(objpath.first.str, ipv6PathStart))
409 {
410 for (auto &interface : objpath.second)
411 {
412 if (interface.first == "xyz.openbmc_project.Network.IP")
413 {
414 // Instance IPv6AddressData structure, and set as
415 // appropriate
416 std::pair<
417 boost::container::flat_set<IPv6AddressData>::iterator,
418 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700419 it = ipv6_config.insert(IPv6AddressData{});
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500420 IPv6AddressData &ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700421 ipv6_address.id =
422 objpath.first.str.substr(ipv6PathStart.size());
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500423 for (auto &property : interface.second)
424 {
425 if (property.first == "Address")
426 {
427 const std::string *address =
428 std::get_if<std::string>(&property.second);
429 if (address != nullptr)
430 {
431 ipv6_address.address = *address;
432 }
433 }
434 else if (property.first == "Origin")
435 {
436 const std::string *origin =
437 std::get_if<std::string>(&property.second);
438 if (origin != nullptr)
439 {
440 ipv6_address.origin =
441 translateAddressOriginDbusToRedfish(*origin,
442 false);
443 }
444 }
445 else if (property.first == "PrefixLength")
446 {
447 const uint8_t *prefix =
448 std::get_if<uint8_t>(&property.second);
449 if (prefix != nullptr)
450 {
451 ipv6_address.prefixLength = *prefix;
452 }
453 }
454 else
455 {
456 BMCWEB_LOG_ERROR
457 << "Got extra property: " << property.first
458 << " on the " << objpath.first.str << " object";
459 }
460 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500461 }
462 }
463 }
464 }
465}
466
Ed Tanous4a0cb852018-10-15 07:55:04 -0700467// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700468inline void
469 extractIPData(const std::string &ethiface_id,
470 const GetManagedObjects &dbus_data,
471 boost::container::flat_set<IPv4AddressData> &ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700472{
473 const std::string ipv4PathStart =
474 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
475
476 // Since there might be several IPv4 configurations aligned with
477 // single ethernet interface, loop over all of them
478 for (const auto &objpath : dbus_data)
479 {
480 // Check if proper pattern for object path appears
481 if (boost::starts_with(objpath.first.str, ipv4PathStart))
482 {
483 for (auto &interface : objpath.second)
484 {
485 if (interface.first == "xyz.openbmc_project.Network.IP")
486 {
487 // Instance IPv4AddressData structure, and set as
488 // appropriate
489 std::pair<
490 boost::container::flat_set<IPv4AddressData>::iterator,
491 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700492 it = ipv4_config.insert(IPv4AddressData{});
Ed Tanous4a0cb852018-10-15 07:55:04 -0700493 IPv4AddressData &ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700494 ipv4_address.id =
495 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous4a0cb852018-10-15 07:55:04 -0700496 for (auto &property : interface.second)
497 {
498 if (property.first == "Address")
499 {
500 const std::string *address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800501 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700502 if (address != nullptr)
503 {
504 ipv4_address.address = *address;
505 }
506 }
507 else if (property.first == "Gateway")
508 {
509 const std::string *gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800510 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700511 if (gateway != nullptr)
512 {
513 ipv4_address.gateway = *gateway;
514 }
515 }
516 else if (property.first == "Origin")
517 {
518 const std::string *origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800519 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700520 if (origin != nullptr)
521 {
522 ipv4_address.origin =
523 translateAddressOriginDbusToRedfish(*origin,
524 true);
525 }
526 }
527 else if (property.first == "PrefixLength")
528 {
529 const uint8_t *mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800530 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700531 if (mask != nullptr)
532 {
533 // convert it to the string
534 ipv4_address.netmask = getNetmask(*mask);
535 }
536 }
537 else
538 {
539 BMCWEB_LOG_ERROR
540 << "Got extra property: " << property.first
541 << " on the " << objpath.first.str << " object";
542 }
543 }
544 // Check if given address is local, or global
545 ipv4_address.linktype =
546 boost::starts_with(ipv4_address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700547 ? LinkType::Local
548 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700549 }
550 }
551 }
552 }
553}
554
555/**
556 * @brief Sets given Id on the given VLAN interface through D-Bus
557 *
558 * @param[in] ifaceId Id of VLAN interface that should be modified
559 * @param[in] inputVlanId New ID of the VLAN
560 * @param[in] callback Function that will be called after the operation
561 *
562 * @return None.
563 */
564template <typename CallbackFunc>
565void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
566 CallbackFunc &&callback)
567{
568 crow::connections::systemBus->async_method_call(
569 callback, "xyz.openbmc_project.Network",
570 std::string("/xyz/openbmc_project/network/") + ifaceId,
571 "org.freedesktop.DBus.Properties", "Set",
572 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800573 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700574}
575
576/**
577 * @brief Helper function that verifies IP address to check if it is in
578 * proper format. If bits pointer is provided, also calculates active
579 * bit count for Subnet Mask.
580 *
581 * @param[in] ip IP that will be verified
582 * @param[out] bits Calculated mask in bits notation
583 *
584 * @return true in case of success, false otherwise
585 */
586inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
587 uint8_t *bits = nullptr)
588{
589 std::vector<std::string> bytesInMask;
590
591 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592
593 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700594 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700596 return false;
597 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700598
Ed Tanous4a0cb852018-10-15 07:55:04 -0700599 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700600 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700601 *bits = 0;
602 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603
Ed Tanous4a0cb852018-10-15 07:55:04 -0700604 char *endPtr;
605 long previousValue = 255;
606 bool firstZeroInByteHit;
607 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700609 if (byte.empty())
610 {
611 return false;
612 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613
Ed Tanous4a0cb852018-10-15 07:55:04 -0700614 // Use strtol instead of stroi to avoid exceptions
615 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700616
Ed Tanous4a0cb852018-10-15 07:55:04 -0700617 // endPtr should point to the end of the string, otherwise given string
618 // is not 100% number
619 if (*endPtr != '\0')
620 {
621 return false;
622 }
623
624 // Value should be contained in byte
625 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700626 {
627 return false;
628 }
629
630 if (bits != nullptr)
631 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632 // Mask has to be continuous between bytes
633 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
635 return false;
636 }
637
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 // Mask has to be continuous inside bytes
639 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640
Ed Tanous4a0cb852018-10-15 07:55:04 -0700641 // Count bits
642 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700643 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700644 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700646 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700648 // Continuity not preserved
649 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 }
651 else
652 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 }
655 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 else
657 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700658 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700660 }
661 }
662
663 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 }
665
Ed Tanous4a0cb852018-10-15 07:55:04 -0700666 return true;
667}
668
669/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700670 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700671 *
672 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700673 * @param[in] ipHash DBus Hash id of IP that should be deleted
674 * @param[io] asyncResp Response object that will be returned to client
675 *
676 * @return None
677 */
678inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 const std::shared_ptr<AsyncResp> asyncResp)
680{
681 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700682 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 if (ec)
684 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800685 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100686 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700687 },
688 "xyz.openbmc_project.Network",
689 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
690 "xyz.openbmc_project.Object.Delete", "Delete");
691}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692
Ed Tanous4a0cb852018-10-15 07:55:04 -0700693/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700694 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700695 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700696 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
697 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
698 * @param[in] gateway IPv4 address of this interfaces gateway
699 * @param[in] address IPv4 address to assign to this interface
700 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700701 *
702 * @return None
703 */
Ed Tanousb01bf292019-03-25 19:25:26 +0000704inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
Johnathan Mantey01784822019-06-18 12:44:21 -0700705 uint8_t prefixLength, const std::string &gateway,
Ed Tanousb01bf292019-03-25 19:25:26 +0000706 const std::string &address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700707 std::shared_ptr<AsyncResp> asyncResp)
708{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700709 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700710 [asyncResp](const boost::system::error_code ec) {
711 if (ec)
712 {
713 messages::internalError(asyncResp->res);
714 }
715 },
716 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700717 "/xyz/openbmc_project/network/" + ifaceId,
718 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700719 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700720 gateway);
721}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500722
723/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700724 * @brief Deletes the IPv4 entry for this interface and creates a replacement
725 * static IPv4 entry
726 *
727 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
728 * @param[in] id The unique hash entry identifying the DBus entry
729 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
730 * @param[in] gateway IPv4 address of this interfaces gateway
731 * @param[in] address IPv4 address to assign to this interface
732 * @param[io] asyncResp Response object that will be returned to client
733 *
734 * @return None
735 */
736inline void deleteAndCreateIPv4(const std::string &ifaceId,
737 const std::string &id, uint8_t prefixLength,
738 const std::string &gateway,
739 const std::string &address,
740 std::shared_ptr<AsyncResp> asyncResp)
741{
742 crow::connections::systemBus->async_method_call(
743 [asyncResp, ifaceId, address, prefixLength,
744 gateway](const boost::system::error_code ec) {
745 if (ec)
746 {
747 messages::internalError(asyncResp->res);
748 }
749 crow::connections::systemBus->async_method_call(
750 [asyncResp](const boost::system::error_code ec) {
751 if (ec)
752 {
753 messages::internalError(asyncResp->res);
754 }
755 },
756 "xyz.openbmc_project.Network",
757 "/xyz/openbmc_project/network/" + ifaceId,
758 "xyz.openbmc_project.Network.IP.Create", "IP",
759 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
760 prefixLength, gateway);
761 },
762 "xyz.openbmc_project.Network",
763 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
764 "xyz.openbmc_project.Object.Delete", "Delete");
765}
766
767/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500768 * @brief Deletes given IPv6
769 *
770 * @param[in] ifaceId Id of interface whose IP should be deleted
771 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500772 * @param[io] asyncResp Response object that will be returned to client
773 *
774 * @return None
775 */
776inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500777 const std::shared_ptr<AsyncResp> asyncResp)
778{
779 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700780 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500781 if (ec)
782 {
783 messages::internalError(asyncResp->res);
784 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500785 },
786 "xyz.openbmc_project.Network",
787 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
788 "xyz.openbmc_project.Object.Delete", "Delete");
789}
790
791/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700792 * @brief Deletes the IPv6 entry for this interface and creates a replacement
793 * static IPv6 entry
794 *
795 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
796 * @param[in] id The unique hash entry identifying the DBus entry
797 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
798 * @param[in] address IPv6 address to assign to this interface
799 * @param[io] asyncResp Response object that will be returned to client
800 *
801 * @return None
802 */
803inline void deleteAndCreateIPv6(const std::string &ifaceId,
804 const std::string &id, uint8_t prefixLength,
805 const std::string &address,
806 std::shared_ptr<AsyncResp> asyncResp)
807{
808 crow::connections::systemBus->async_method_call(
809 [asyncResp, ifaceId, address,
810 prefixLength](const boost::system::error_code ec) {
811 if (ec)
812 {
813 messages::internalError(asyncResp->res);
814 }
815 crow::connections::systemBus->async_method_call(
816 [asyncResp](const boost::system::error_code ec) {
817 if (ec)
818 {
819 messages::internalError(asyncResp->res);
820 }
821 },
822 "xyz.openbmc_project.Network",
823 "/xyz/openbmc_project/network/" + ifaceId,
824 "xyz.openbmc_project.Network.IP.Create", "IP",
825 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
826 prefixLength, "");
827 },
828 "xyz.openbmc_project.Network",
829 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
830 "xyz.openbmc_project.Object.Delete", "Delete");
831}
832
833/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500834 * @brief Creates IPv6 with given data
835 *
836 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500837 * @param[in] prefixLength Prefix length that needs to be added
838 * @param[in] address IP address that needs to be added
839 * @param[io] asyncResp Response object that will be returned to client
840 *
841 * @return None
842 */
Johnathan Mantey01784822019-06-18 12:44:21 -0700843inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
844 const std::string &address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500845 std::shared_ptr<AsyncResp> asyncResp)
846{
847 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
848 if (ec)
849 {
850 messages::internalError(asyncResp->res);
851 }
852 };
853 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
854 // does not have assosiated gateway property
855 crow::connections::systemBus->async_method_call(
856 std::move(createIpHandler), "xyz.openbmc_project.Network",
857 "/xyz/openbmc_project/network/" + ifaceId,
858 "xyz.openbmc_project.Network.IP.Create", "IP",
859 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
860 "");
861}
862
Ed Tanous4a0cb852018-10-15 07:55:04 -0700863/**
864 * Function that retrieves all properties for given Ethernet Interface
865 * Object
866 * from EntityManager Network Manager
867 * @param ethiface_id a eth interface id to query on DBus
868 * @param callback a function that shall be called to convert Dbus output
869 * into JSON
870 */
871template <typename CallbackFunc>
872void getEthernetIfaceData(const std::string &ethiface_id,
873 CallbackFunc &&callback)
874{
875 crow::connections::systemBus->async_method_call(
876 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
877 const boost::system::error_code error_code,
878 const GetManagedObjects &resp) {
879 EthernetInterfaceData ethData{};
880 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500881 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700882
883 if (error_code)
884 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700885 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700886 return;
887 }
888
Ed Tanous4c9afe42019-05-03 16:59:57 -0700889 bool found =
890 extractEthernetInterfaceData(ethiface_id, resp, ethData);
891 if (!found)
892 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700893 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700894 return;
895 }
896
Johnathan Mantey01784822019-06-18 12:44:21 -0700897 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700898 // Fix global GW
899 for (IPv4AddressData &ipv4 : ipv4Data)
900 {
Ravi Tejac6191412019-07-30 00:53:50 -0500901 if (((ipv4.linktype == LinkType::Global) &&
902 (ipv4.gateway == "0.0.0.0")) ||
903 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700905 ipv4.gateway = ethData.default_gateway;
906 }
907 }
908
Johnathan Mantey01784822019-06-18 12:44:21 -0700909 extractIPV6Data(ethiface_id, resp, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700910 // Finally make a callback with usefull data
Johnathan Mantey01784822019-06-18 12:44:21 -0700911 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700912 },
913 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
914 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700915}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700916
917/**
918 * Function that retrieves all Ethernet Interfaces available through Network
919 * Manager
920 * @param callback a function that shall be called to convert Dbus output
921 * into JSON.
922 */
923template <typename CallbackFunc>
924void getEthernetIfaceList(CallbackFunc &&callback)
925{
926 crow::connections::systemBus->async_method_call(
927 [callback{std::move(callback)}](
928 const boost::system::error_code error_code,
929 GetManagedObjects &resp) {
930 // Callback requires vector<string> to retrieve all available
931 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700932 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700933 iface_list.reserve(resp.size());
934 if (error_code)
935 {
936 callback(false, iface_list);
937 return;
938 }
939
940 // Iterate over all retrieved ObjectPaths.
941 for (const auto &objpath : resp)
942 {
943 // And all interfaces available for certain ObjectPath.
944 for (const auto &interface : objpath.second)
945 {
946 // If interface is
947 // xyz.openbmc_project.Network.EthernetInterface, this is
948 // what we're looking for.
949 if (interface.first ==
950 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700952 // Cut out everyting until last "/", ...
953 const std::string &iface_id = objpath.first.str;
954 std::size_t last_pos = iface_id.rfind("/");
955 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700956 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700957 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700958 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700959 }
960 }
961 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 }
963 // Finally make a callback with useful data
964 callback(true, iface_list);
965 },
966 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
967 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700968}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100969
970/**
971 * EthernetCollection derived class for delivering Ethernet Collection Schema
972 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973class EthernetCollection : public Node
974{
975 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700976 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700977 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700978 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700979 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 entityPrivileges = {
981 {boost::beast::http::verb::get, {{"Login"}}},
982 {boost::beast::http::verb::head, {{"Login"}}},
983 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
984 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
985 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
986 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
987 }
988
989 private:
990 /**
991 * Functions triggers appropriate requests on DBus
992 */
993 void doGet(crow::Response &res, const crow::Request &req,
994 const std::vector<std::string> &params) override
995 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800996 res.jsonValue["@odata.type"] =
997 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
998 res.jsonValue["@odata.context"] =
999 "/redfish/v1/"
1000 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
1001 res.jsonValue["@odata.id"] =
1002 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1003 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1004 res.jsonValue["Description"] =
1005 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001006 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001007 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001009 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001010 [asyncResp](
1011 const bool &success,
1012 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001013 if (!success)
1014 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001015 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001016 return;
1017 }
1018
Ed Tanous4c9afe42019-05-03 16:59:57 -07001019 nlohmann::json &iface_array =
1020 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001021 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001022 std::string tag = "_";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001023 for (const std::string &iface_item : iface_list)
1024 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001025 std::size_t found = iface_item.find(tag);
1026 if (found == std::string::npos)
1027 {
1028 iface_array.push_back(
1029 {{"@odata.id",
1030 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1031 iface_item}});
1032 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001033 }
1034
Ed Tanous4c9afe42019-05-03 16:59:57 -07001035 asyncResp->res.jsonValue["Members@odata.count"] =
1036 iface_array.size();
1037 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001038 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001039 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001040 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001041};
1042
1043/**
1044 * EthernetInterface derived class for delivering Ethernet Schema
1045 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001046class EthernetInterface : public Node
1047{
1048 public:
1049 /*
1050 * Default Constructor
1051 */
Ed Tanous4a0cb852018-10-15 07:55:04 -07001052 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001054 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001055 std::string())
1056 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057 entityPrivileges = {
1058 {boost::beast::http::verb::get, {{"Login"}}},
1059 {boost::beast::http::verb::head, {{"Login"}}},
1060 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1061 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1062 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1063 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001064 }
1065
Ed Tanous1abe55e2018-09-05 08:30:59 -07001066 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001067 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001068 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001069 {
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001070 asyncResp->res.jsonValue["HostName"] = hostname;
1071 crow::connections::systemBus->async_method_call(
1072 [asyncResp](const boost::system::error_code ec) {
1073 if (ec)
1074 {
1075 messages::internalError(asyncResp->res);
1076 }
1077 },
1078 "xyz.openbmc_project.Network",
1079 "/xyz/openbmc_project/network/config",
1080 "org.freedesktop.DBus.Properties", "Set",
1081 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001082 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001083 }
1084
Ratan Guptad5776652019-03-03 08:47:22 +05301085 void handleMACAddressPatch(const std::string &ifaceId,
1086 const std::string &macAddress,
1087 const std::shared_ptr<AsyncResp> &asyncResp)
1088 {
1089 crow::connections::systemBus->async_method_call(
1090 [asyncResp, macAddress](const boost::system::error_code ec) {
1091 if (ec)
1092 {
1093 messages::internalError(asyncResp->res);
1094 return;
1095 }
Ratan Guptad5776652019-03-03 08:47:22 +05301096 },
1097 "xyz.openbmc_project.Network",
1098 "/xyz/openbmc_project/network/" + ifaceId,
1099 "org.freedesktop.DBus.Properties", "Set",
1100 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1101 std::variant<std::string>(macAddress));
1102 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001103
Jennifer Leeda131a92019-04-24 15:13:55 -07001104 void setDHCPEnabled(const std::string &ifaceId,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001105 const std::string &propertyName, const bool v4Value,
1106 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001107 const std::shared_ptr<AsyncResp> asyncResp)
1108 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001109 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001110 crow::connections::systemBus->async_method_call(
1111 [asyncResp](const boost::system::error_code ec) {
1112 if (ec)
1113 {
1114 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1115 messages::internalError(asyncResp->res);
1116 return;
1117 }
1118 },
1119 "xyz.openbmc_project.Network",
1120 "/xyz/openbmc_project/network/" + ifaceId,
1121 "org.freedesktop.DBus.Properties", "Set",
1122 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001123 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001124 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001125
Jennifer Leeda131a92019-04-24 15:13:55 -07001126 void setDHCPv4Config(const std::string &propertyName, const bool &value,
1127 const std::shared_ptr<AsyncResp> asyncResp)
1128 {
1129 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1130 crow::connections::systemBus->async_method_call(
1131 [asyncResp](const boost::system::error_code ec) {
1132 if (ec)
1133 {
1134 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1135 messages::internalError(asyncResp->res);
1136 return;
1137 }
1138 },
1139 "xyz.openbmc_project.Network",
1140 "/xyz/openbmc_project/network/config/dhcp",
1141 "org.freedesktop.DBus.Properties", "Set",
1142 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1143 std::variant<bool>{value});
1144 }
Ratan Guptad5776652019-03-03 08:47:22 +05301145
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001146 void handleDHCPPatch(const std::string &ifaceId,
1147 const EthernetInterfaceData &ethData,
1148 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1149 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001150 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001151 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1152 bool ipv6Active =
1153 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001154
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001155 bool nextv4DHCPState =
1156 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1157
1158 bool nextv6DHCPState{};
1159 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001160 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001161 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1162 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1163 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1164 {
1165 messages::propertyValueFormatError(
1166 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1167 "OperatingMode");
1168 return;
1169 }
1170 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1171 }
1172 else
1173 {
1174 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001175 }
1176
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001177 bool nextDNS{};
1178 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001179 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001180 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1181 {
1182 messages::generalError(asyncResp->res);
1183 return;
1184 }
1185 nextDNS = *v4dhcpParms.useDNSServers;
1186 }
1187 else if (v4dhcpParms.useDNSServers)
1188 {
1189 nextDNS = *v4dhcpParms.useDNSServers;
1190 }
1191 else if (v6dhcpParms.useDNSServers)
1192 {
1193 nextDNS = *v6dhcpParms.useDNSServers;
1194 }
1195 else
1196 {
1197 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001198 }
1199
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001200 bool nextNTP{};
1201 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001202 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001203 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1204 {
1205 messages::generalError(asyncResp->res);
1206 return;
1207 }
1208 nextNTP = *v4dhcpParms.useNTPServers;
1209 }
1210 else if (v4dhcpParms.useNTPServers)
1211 {
1212 nextNTP = *v4dhcpParms.useNTPServers;
1213 }
1214 else if (v6dhcpParms.useNTPServers)
1215 {
1216 nextNTP = *v6dhcpParms.useNTPServers;
1217 }
1218 else
1219 {
1220 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001221 }
1222
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001223 bool nextUseDomain{};
1224 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001225 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001226 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1227 {
1228 messages::generalError(asyncResp->res);
1229 return;
1230 }
1231 nextUseDomain = *v4dhcpParms.useUseDomainName;
1232 }
1233 else if (v4dhcpParms.useUseDomainName)
1234 {
1235 nextUseDomain = *v4dhcpParms.useUseDomainName;
1236 }
1237 else if (v6dhcpParms.useUseDomainName)
1238 {
1239 nextUseDomain = *v6dhcpParms.useUseDomainName;
1240 }
1241 else
1242 {
1243 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001244 }
1245
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001246 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1247 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1248 asyncResp);
1249 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1250 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1251 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1252 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1253 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1254 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001255 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001256
1257 boost::container::flat_set<IPv4AddressData>::const_iterator
1258 GetNextStaticIPEntry(
1259 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1260 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1261 {
1262 for (; head != end; head++)
1263 {
1264 if (head->origin == "Static")
1265 {
1266 return head;
1267 }
1268 }
1269 return end;
1270 }
1271
1272 boost::container::flat_set<IPv6AddressData>::const_iterator
1273 GetNextStaticIPEntry(
1274 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1275 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1276 {
1277 for (; head != end; head++)
1278 {
1279 if (head->origin == "Static")
1280 {
1281 return head;
1282 }
1283 }
1284 return end;
1285 }
1286
Ravi Tejad1d50812019-06-23 16:20:27 -05001287 void handleIPv4StaticPatch(
Ratan Guptaf476acb2019-03-02 16:46:57 +05301288 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001289 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001290 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001291 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001292 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301293 {
1294 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001295 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301296 return;
1297 }
1298
Ed Tanous271584a2019-07-09 16:24:22 -07001299 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001300 // Find the first static IP address currently active on the NIC and
1301 // match it to the first JSON element in the IPv4StaticAddresses array.
1302 // Match each subsequent JSON element to the next static IP programmed
1303 // into the NIC.
1304 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1305 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1306
Ed Tanous537174c2018-12-10 15:09:31 -08001307 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001308 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001309 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001310 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001311
Johnathan Mantey01784822019-06-18 12:44:21 -07001312 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301313 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001314 std::optional<std::string> address;
1315 std::optional<std::string> subnetMask;
1316 std::optional<std::string> gateway;
1317
1318 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1319 address, "SubnetMask", subnetMask,
1320 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301321 {
1322 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001323 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301324 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301325 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301326
Johnathan Mantey01784822019-06-18 12:44:21 -07001327 // Find the address/subnet/gateway values. Any values that are
1328 // not explicitly provided are assumed to be unmodified from the
1329 // current state of the interface. Merge existing state into the
1330 // current request.
Ed Tanous271584a2019-07-09 16:24:22 -07001331 const std::string *addr = nullptr;
1332 const std::string *gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001333 uint8_t prefixLength = 0;
1334 bool errorInEntry = false;
1335 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301336 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001337 if (ipv4VerifyIpAndGetBitcount(*address))
1338 {
1339 addr = &(*address);
1340 }
1341 else
1342 {
1343 messages::propertyValueFormatError(
1344 asyncResp->res, *address, pathString + "/Address");
1345 errorInEntry = true;
1346 }
1347 }
1348 else if (NICIPentry != ipv4Data.cend())
1349 {
1350 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301351 }
1352 else
1353 {
1354 messages::propertyMissing(asyncResp->res,
1355 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001356 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001357 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301358
1359 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001360 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001361 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1362 {
1363 messages::propertyValueFormatError(
1364 asyncResp->res, *subnetMask,
1365 pathString + "/SubnetMask");
1366 errorInEntry = true;
1367 }
1368 }
1369 else if (NICIPentry != ipv4Data.cend())
1370 {
1371 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1372 &prefixLength))
1373 {
1374 messages::propertyValueFormatError(
1375 asyncResp->res, NICIPentry->netmask,
1376 pathString + "/SubnetMask");
1377 errorInEntry = true;
1378 }
1379 }
1380 else
1381 {
1382 messages::propertyMissing(asyncResp->res,
1383 pathString + "/SubnetMask");
1384 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001385 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301386
Ratan Guptaf476acb2019-03-02 16:46:57 +05301387 if (gateway)
1388 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001389 if (ipv4VerifyIpAndGetBitcount(*gateway))
1390 {
1391 gw = &(*gateway);
1392 }
1393 else
1394 {
1395 messages::propertyValueFormatError(
1396 asyncResp->res, *gateway, pathString + "/Gateway");
1397 errorInEntry = true;
1398 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301399 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001400 else if (NICIPentry != ipv4Data.cend())
1401 {
1402 gw = &NICIPentry->gateway;
1403 }
1404 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001405 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001406 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001407 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001408 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001409 }
1410
Johnathan Mantey01784822019-06-18 12:44:21 -07001411 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001412 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001413 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001414 }
1415
Johnathan Mantey01784822019-06-18 12:44:21 -07001416 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001417 {
Ed Tanous271584a2019-07-09 16:24:22 -07001418 if (gw != nullptr || addr != nullptr)
1419 {
1420 // Shouldn't be possible based on errorInEntry, but
1421 // it flags -wmaybe-uninitialized in the compiler,
1422 // so defend against that
1423 return;
1424 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001425 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1426 *gw, *addr, asyncResp);
1427 NICIPentry =
1428 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001429 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001430 else
1431 {
1432 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1433 *address, asyncResp);
1434 }
1435 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001436 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001437 else
1438 {
1439 if (NICIPentry == ipv4Data.cend())
1440 {
1441 // Requesting a DELETE/DO NOT MODIFY action for an item
1442 // that isn't present on the eth(n) interface. Input JSON is
1443 // in error, so bail out.
1444 if (thisJson.is_null())
1445 {
1446 messages::resourceCannotBeDeleted(asyncResp->res);
1447 return;
1448 }
1449 else
1450 {
1451 messages::propertyValueFormatError(
1452 asyncResp->res, thisJson.dump(), pathString);
1453 return;
1454 }
1455 }
1456
1457 if (thisJson.is_null())
1458 {
1459 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1460 }
1461 if (NICIPentry != ipv4Data.cend())
1462 {
1463 NICIPentry =
1464 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1465 }
1466 entryIdx++;
1467 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001468 }
1469 }
1470
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001471 void handleStaticNameServersPatch(
1472 const std::string &ifaceId,
1473 const std::vector<std::string> &updatedStaticNameServers,
1474 const std::shared_ptr<AsyncResp> &asyncResp)
1475 {
1476 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001477 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001478 if (ec)
1479 {
1480 messages::internalError(asyncResp->res);
1481 return;
1482 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001483 },
1484 "xyz.openbmc_project.Network",
1485 "/xyz/openbmc_project/network/" + ifaceId,
1486 "org.freedesktop.DBus.Properties", "Set",
1487 "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1488 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1489 }
1490
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001491 void handleIPv6StaticAddressesPatch(
1492 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001493 const boost::container::flat_set<IPv6AddressData> &ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001494 const std::shared_ptr<AsyncResp> asyncResp)
1495 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001496 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001497 {
1498 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1499 "IPv6StaticAddresses");
1500 return;
1501 }
Ed Tanous271584a2019-07-09 16:24:22 -07001502 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001503 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1504 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001505 for (nlohmann::json &thisJson : input)
1506 {
1507 std::string pathString =
1508 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1509
Johnathan Mantey01784822019-06-18 12:44:21 -07001510 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001511 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001512 std::optional<std::string> address;
1513 std::optional<uint8_t> prefixLength;
1514
1515 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1516 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001517 {
1518 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001519 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001520 return;
1521 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001522
Johnathan Mantey01784822019-06-18 12:44:21 -07001523 const std::string *addr;
1524 uint8_t prefix;
1525
1526 // Find the address and prefixLength values. Any values that are
1527 // not explicitly provided are assumed to be unmodified from the
1528 // current state of the interface. Merge existing state into the
1529 // current request.
1530 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001531 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001532 addr = &(*address);
1533 }
1534 else if (NICIPentry != ipv6Data.end())
1535 {
1536 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001537 }
1538 else
1539 {
1540 messages::propertyMissing(asyncResp->res,
1541 pathString + "/Address");
1542 return;
1543 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001544
1545 if (prefixLength)
1546 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001547 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001548 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001549 else if (NICIPentry != ipv6Data.end())
1550 {
1551 prefix = NICIPentry->prefixLength;
1552 }
1553 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001554 {
1555 messages::propertyMissing(asyncResp->res,
1556 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001557 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001558 }
1559
Johnathan Mantey01784822019-06-18 12:44:21 -07001560 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001561 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001562 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1563 asyncResp);
1564 NICIPentry =
1565 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1566 }
1567 else
1568 {
1569 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1570 }
1571 entryIdx++;
1572 }
1573 else
1574 {
1575 if (NICIPentry == ipv6Data.end())
1576 {
1577 // Requesting a DELETE/DO NOT MODIFY action for an item
1578 // that isn't present on the eth(n) interface. Input JSON is
1579 // in error, so bail out.
1580 if (thisJson.is_null())
1581 {
1582 messages::resourceCannotBeDeleted(asyncResp->res);
1583 return;
1584 }
1585 else
1586 {
1587 messages::propertyValueFormatError(
1588 asyncResp->res, thisJson.dump(), pathString);
1589 return;
1590 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001591 }
1592
Johnathan Mantey01784822019-06-18 12:44:21 -07001593 if (thisJson.is_null())
1594 {
1595 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1596 }
1597 if (NICIPentry != ipv6Data.cend())
1598 {
1599 NICIPentry =
1600 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1601 }
1602 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001603 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001604 }
1605 }
1606
Ed Tanous0f74e642018-11-12 15:17:05 -08001607 void parseInterfaceData(
1608 nlohmann::json &json_response, const std::string &iface_id,
1609 const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001610 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001611 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001612 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001613 json_response["Id"] = iface_id;
1614 json_response["@odata.id"] =
1615 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous029573d2019-02-01 10:57:49 -08001616 json_response["InterfaceEnabled"] = true;
1617 if (ethData.speed == 0)
1618 {
1619 json_response["LinkStatus"] = "NoLink";
1620 json_response["Status"] = {
1621 {"Health", "OK"},
1622 {"State", "Disabled"},
1623 };
1624 }
1625 else
1626 {
1627 json_response["LinkStatus"] = "LinkUp";
1628 json_response["Status"] = {
1629 {"Health", "OK"},
1630 {"State", "Enabled"},
1631 };
1632 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001633 json_response["SpeedMbps"] = ethData.speed;
1634 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001635 json_response["DHCPv4"]["DHCPEnabled"] =
1636 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1637 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1638 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1639 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1640
1641 json_response["DHCPv6"]["OperatingMode"] =
1642 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1643 : "Disabled";
1644 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1645 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1646 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301647
Ed Tanous4a0cb852018-10-15 07:55:04 -07001648 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001649 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001650 json_response["HostName"] = ethData.hostname;
Jennifer Leed24bfc72019-03-05 13:03:37 -08001651 if (!ethData.domainnames.empty())
1652 {
1653 json_response["FQDN"] =
1654 ethData.hostname + "." + ethData.domainnames[0];
1655 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001656 }
1657
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001658 json_response["VLANs"] = {
1659 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1660 iface_id + "/VLANs"}};
1661
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001662 if (translateDHCPEnabledToBool(ethData.DHCPEnabled, true) &&
1663 ethData.DNSEnabled)
Manojkiran Eda95f86462019-08-07 15:07:54 +05301664 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001665 json_response["StaticNameServers"] = nlohmann::json::array();
Manojkiran Eda95f86462019-08-07 15:07:54 +05301666 }
1667 else
1668 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001669 json_response["StaticNameServers"] = ethData.nameservers;
Manojkiran Eda95f86462019-08-07 15:07:54 +05301670 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001671
Ravi Tejad1d50812019-06-23 16:20:27 -05001672 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001673 nlohmann::json &ipv4_static_array =
1674 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001675 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001676 ipv4_static_array = nlohmann::json::array();
Ravi Tejad1d50812019-06-23 16:20:27 -05001677 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001678 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001679
1680 std::string gatewayStr = ipv4_config.gateway;
1681 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001682 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001683 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001684 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001685
1686 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1687 {"SubnetMask", ipv4_config.netmask},
1688 {"Address", ipv4_config.address},
1689 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001690 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001691 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001692 ipv4_static_array.push_back(
1693 {{"AddressOrigin", ipv4_config.origin},
1694 {"SubnetMask", ipv4_config.netmask},
1695 {"Address", ipv4_config.address},
1696 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001697 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001698 }
1699
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001700 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001701
1702 nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001703 nlohmann::json &ipv6_static_array =
1704 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001705 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001706 ipv6_static_array = nlohmann::json::array();
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001707 for (auto &ipv6_config : ipv6Data)
1708 {
1709 ipv6_array.push_back({{"Address", ipv6_config.address},
1710 {"PrefixLength", ipv6_config.prefixLength},
1711 {"AddressOrigin", ipv6_config.origin}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001712 if (ipv6_config.origin == "Static")
1713 {
1714 ipv6_static_array.push_back(
1715 {{"Address", ipv6_config.address},
1716 {"PrefixLength", ipv6_config.prefixLength},
1717 {"AddressOrigin", ipv6_config.origin}});
1718 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001719 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001720 }
1721
1722 /**
1723 * Functions triggers appropriate requests on DBus
1724 */
1725 void doGet(crow::Response &res, const crow::Request &req,
1726 const std::vector<std::string> &params) override
1727 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001728 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001729 if (params.size() != 1)
1730 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001731 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001732 return;
1733 }
1734
Ed Tanous4a0cb852018-10-15 07:55:04 -07001735 getEthernetIfaceData(
1736 params[0],
1737 [this, asyncResp, iface_id{std::string(params[0])}](
1738 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001739 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001740 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001741 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001742 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001743 // TODO(Pawel)consider distinguish between non existing
1744 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001745 messages::resourceNotFound(asyncResp->res,
1746 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001747 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001748 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001749
Ed Tanous0f74e642018-11-12 15:17:05 -08001750 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001751 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001752 asyncResp->res.jsonValue["@odata.context"] =
Johnathan Mantey01784822019-06-18 12:44:21 -07001753 "/redfish/v1/"
1754 "$metadata#EthernetInterface.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001755 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1756 asyncResp->res.jsonValue["Description"] =
1757 "Management Network Interface";
1758
1759 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07001760 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001761 });
1762 }
1763
1764 void doPatch(crow::Response &res, const crow::Request &req,
1765 const std::vector<std::string> &params) override
1766 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001767 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001768 if (params.size() != 1)
1769 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001770 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001771 return;
1772 }
1773
Ed Tanous4a0cb852018-10-15 07:55:04 -07001774 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001775
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001776 std::optional<std::string> hostname;
Ratan Guptad5776652019-03-03 08:47:22 +05301777 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001778 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001779 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001780 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001781 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001782 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001783 std::optional<nlohmann::json> dhcpv6;
1784 DHCPParameters v4dhcpParms;
1785 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001786
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001787 if (!json_util::readJson(
1788 req, res, "HostName", hostname, "IPv4StaticAddresses",
1789 ipv4StaticAddresses, "MACAddress", macAddress,
1790 "StaticNameServers", staticNameServers, "IPv6DefaultGateway",
1791 ipv6DefaultGateway, "IPv6StaticAddresses", ipv6StaticAddresses,
1792 "DHCPv4", dhcpv4, "DHCPv6", dhcpv6))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001793 {
1794 return;
1795 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001796 if (dhcpv4)
1797 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001798 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1799 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1800 v4dhcpParms.useDNSServers, "UseNTPServers",
1801 v4dhcpParms.useNTPServers, "UseDomainName",
1802 v4dhcpParms.useUseDomainName))
1803 {
1804 return;
1805 }
1806 }
1807
1808 if (dhcpv6)
1809 {
1810 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1811 v6dhcpParms.dhcpv6OperatingMode,
1812 "UseDNSServers", v6dhcpParms.useDNSServers,
1813 "UseNTPServers", v6dhcpParms.useNTPServers,
1814 "UseDomainName",
1815 v6dhcpParms.useUseDomainName))
1816 {
1817 return;
1818 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001819 }
1820
Johnathan Mantey01784822019-06-18 12:44:21 -07001821 // Get single eth interface data, and call the below callback for
1822 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001823 getEthernetIfaceData(
1824 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001825 [this, asyncResp, iface_id, hostname = std::move(hostname),
1826 macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001827 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001828 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001829 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001830 staticNameServers = std::move(staticNameServers),
1831 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1832 v4dhcpParms = std::move(v4dhcpParms),
1833 v6dhcpParms = std::move(v6dhcpParms)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001834 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001835 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001836 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001837 if (!success)
1838 {
1839 // ... otherwise return error
1840 // TODO(Pawel)consider distinguish between non existing
1841 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001842 messages::resourceNotFound(asyncResp->res,
1843 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001844 return;
1845 }
1846
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001847 if (dhcpv4 || dhcpv6)
1848 {
1849 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1850 std::move(v6dhcpParms), asyncResp);
1851 }
1852
Ed Tanous0627a2c2018-11-29 17:09:23 -08001853 if (hostname)
1854 {
1855 handleHostnamePatch(*hostname, asyncResp);
1856 }
1857
Ratan Guptad5776652019-03-03 08:47:22 +05301858 if (macAddress)
1859 {
1860 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
1861 }
1862
Ravi Tejad1d50812019-06-23 16:20:27 -05001863 if (ipv4StaticAddresses)
1864 {
Ed Tanous537174c2018-12-10 15:09:31 -08001865 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07001866 // above is returning a const value, not a non-const
1867 // value. This doesn't really work for us, as we need to
1868 // be able to efficiently move out the intermedia
1869 // nlohmann::json objects. This makes a copy of the
1870 // structure, and operates on that, but could be done
1871 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05001872 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07001873 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05001874 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08001875 }
1876
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001877 if (staticNameServers)
1878 {
1879 handleStaticNameServersPatch(iface_id, *staticNameServers,
1880 asyncResp);
1881 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001882
1883 if (ipv6DefaultGateway)
1884 {
1885 messages::propertyNotWritable(asyncResp->res,
1886 "IPv6DefaultGateway");
1887 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001888
1889 if (ipv6StaticAddresses)
1890 {
1891 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
1892 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07001893 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001894 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001895 });
1896 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001897};
1898
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001899/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001900 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1901 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001902 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001903class VlanNetworkInterface : public Node
1904{
1905 public:
1906 /*
1907 * Default Constructor
1908 */
1909 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001910 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001911 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001912 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001913 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001914 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001915 entityPrivileges = {
1916 {boost::beast::http::verb::get, {{"Login"}}},
1917 {boost::beast::http::verb::head, {{"Login"}}},
1918 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1919 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1920 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1921 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001922 }
1923
Ed Tanous1abe55e2018-09-05 08:30:59 -07001924 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001925 void parseInterfaceData(
1926 nlohmann::json &json_response, const std::string &parent_iface_id,
1927 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001928 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001929 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001930 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001931 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001932 json_response["Id"] = iface_id;
1933 json_response["@odata.id"] =
1934 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1935 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001936
Ed Tanous4a0cb852018-10-15 07:55:04 -07001937 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001938 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001939 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001940 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07001941 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001942 }
1943
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001944 bool verifyNames(const std::string &parent, const std::string &iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001945 {
1946 if (!boost::starts_with(iface, parent + "_"))
1947 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001948 return false;
1949 }
1950 else
1951 {
1952 return true;
1953 }
1954 }
1955
1956 /**
1957 * Functions triggers appropriate requests on DBus
1958 */
1959 void doGet(crow::Response &res, const crow::Request &req,
1960 const std::vector<std::string> &params) override
1961 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001962 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1963 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001964 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1965 // Check if there is required param, truly entering this shall be
1966 // impossible.
1967 if (params.size() != 2)
1968 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001969 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001970 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001971 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001972 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001973
Ed Tanous4a0cb852018-10-15 07:55:04 -07001974 const std::string &parent_iface_id = params[0];
1975 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001976 res.jsonValue["@odata.type"] =
1977 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1978 res.jsonValue["@odata.context"] =
Johnathan Mantey01784822019-06-18 12:44:21 -07001979 "/redfish/v1/"
1980 "$metadata#VLanNetworkInterface.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001981 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001982
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001983 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001984 {
1985 return;
1986 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001987
Johnathan Mantey01784822019-06-18 12:44:21 -07001988 // Get single eth interface data, and call the below callback for
1989 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001990 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001991 params[1],
1992 [this, asyncResp, parent_iface_id{std::string(params[0])},
1993 iface_id{std::string(params[1])}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001994 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001995 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001996 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001997 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001998 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001999 parseInterfaceData(asyncResp->res.jsonValue,
2000 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002001 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002002 }
2003 else
2004 {
2005 // ... otherwise return error
2006 // TODO(Pawel)consider distinguish between non existing
2007 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002008 messages::resourceNotFound(
2009 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002010 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002011 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002012 }
2013
Ed Tanous1abe55e2018-09-05 08:30:59 -07002014 void doPatch(crow::Response &res, const crow::Request &req,
2015 const std::vector<std::string> &params) override
2016 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002017 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002018 if (params.size() != 2)
2019 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002020 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002021 return;
2022 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002023
Ed Tanous1abe55e2018-09-05 08:30:59 -07002024 const std::string &parentIfaceId = params[0];
2025 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002026
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002027 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002028 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002029 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2030 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002031 return;
2032 }
2033
Ed Tanous0627a2c2018-11-29 17:09:23 -08002034 bool vlanEnable = false;
2035 uint64_t vlanId = 0;
2036
2037 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2038 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002039 {
2040 return;
2041 }
2042
Johnathan Mantey01784822019-06-18 12:44:21 -07002043 // Get single eth interface data, and call the below callback for
2044 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002045 getEthernetIfaceData(
2046 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002047 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002048 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2049 const bool &success, const EthernetInterfaceData &ethData,
2050 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002051 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002052 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002053 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002054 auto callback =
2055 [asyncResp](const boost::system::error_code ec) {
2056 if (ec)
2057 {
2058 messages::internalError(asyncResp->res);
2059 }
2060 };
2061
2062 if (vlanEnable == true)
2063 {
2064 crow::connections::systemBus->async_method_call(
2065 std::move(callback), "xyz.openbmc_project.Network",
2066 "/xyz/openbmc_project/network/" + ifaceId,
2067 "org.freedesktop.DBus.Properties", "Set",
2068 "xyz.openbmc_project.Network.VLAN", "Id",
2069 std::variant<uint32_t>(vlanId));
2070 }
2071 else
2072 {
2073 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2074 "vlan interface";
2075 crow::connections::systemBus->async_method_call(
2076 std::move(callback), "xyz.openbmc_project.Network",
2077 std::string("/xyz/openbmc_project/network/") +
2078 ifaceId,
2079 "xyz.openbmc_project.Object.Delete", "Delete");
2080 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002081 }
2082 else
2083 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002084 // TODO(Pawel)consider distinguish between non existing
2085 // object, and other errors
2086 messages::resourceNotFound(
2087 asyncResp->res, "VLAN Network Interface", ifaceId);
2088 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002089 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002090 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002091 }
2092
2093 void doDelete(crow::Response &res, const crow::Request &req,
2094 const std::vector<std::string> &params) override
2095 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002096 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002097 if (params.size() != 2)
2098 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002099 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002100 return;
2101 }
2102
2103 const std::string &parentIfaceId = params[0];
2104 const std::string &ifaceId = params[1];
2105
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002106 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002107 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002108 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2109 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002110 return;
2111 }
2112
Johnathan Mantey01784822019-06-18 12:44:21 -07002113 // Get single eth interface data, and call the below callback for
2114 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002115 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002116 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002117 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002118 ifaceId{std::string(params[1])}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002119 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002120 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002121 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002122 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002123 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002124 auto callback =
2125 [asyncResp](const boost::system::error_code ec) {
2126 if (ec)
2127 {
2128 messages::internalError(asyncResp->res);
2129 }
2130 };
2131 crow::connections::systemBus->async_method_call(
2132 std::move(callback), "xyz.openbmc_project.Network",
2133 std::string("/xyz/openbmc_project/network/") + ifaceId,
2134 "xyz.openbmc_project.Object.Delete", "Delete");
2135 }
2136 else
2137 {
2138 // ... otherwise return error
2139 // TODO(Pawel)consider distinguish between non existing
2140 // object, and other errors
2141 messages::resourceNotFound(
2142 asyncResp->res, "VLAN Network Interface", ifaceId);
2143 }
2144 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002145 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002146};
2147
2148/**
2149 * VlanNetworkInterfaceCollection derived class for delivering
2150 * VLANNetworkInterface Collection Schema
2151 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002152class VlanNetworkInterfaceCollection : public Node
2153{
2154 public:
2155 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002156 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002157 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2158 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002159 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002160 entityPrivileges = {
2161 {boost::beast::http::verb::get, {{"Login"}}},
2162 {boost::beast::http::verb::head, {{"Login"}}},
2163 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2164 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2165 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2166 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002167 }
2168
Ed Tanous1abe55e2018-09-05 08:30:59 -07002169 private:
2170 /**
2171 * Functions triggers appropriate requests on DBus
2172 */
2173 void doGet(crow::Response &res, const crow::Request &req,
2174 const std::vector<std::string> &params) override
2175 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002176 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002177 if (params.size() != 1)
2178 {
2179 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002180 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002181 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002182 }
2183
Ed Tanous4a0cb852018-10-15 07:55:04 -07002184 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002185
Ed Tanous4a0cb852018-10-15 07:55:04 -07002186 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002188 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002189 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002190 const bool &success,
Ed Tanous4c9afe42019-05-03 16:59:57 -07002191 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002192 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002193 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002194 messages::internalError(asyncResp->res);
2195 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002196 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002197
2198 if (iface_list.find(rootInterfaceName) == iface_list.end())
2199 {
2200 messages::resourceNotFound(asyncResp->res,
2201 "VLanNetworkInterfaceCollection",
2202 rootInterfaceName);
2203 return;
2204 }
2205
Ed Tanous0f74e642018-11-12 15:17:05 -08002206 asyncResp->res.jsonValue["@odata.type"] =
2207 "#VLanNetworkInterfaceCollection."
2208 "VLanNetworkInterfaceCollection";
2209 asyncResp->res.jsonValue["@odata.context"] =
2210 "/redfish/v1/$metadata"
2211 "#VLanNetworkInterfaceCollection."
2212 "VLanNetworkInterfaceCollection";
2213 asyncResp->res.jsonValue["Name"] =
2214 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002215
Jason M. Billsf12894f2018-10-09 12:45:45 -07002216 nlohmann::json iface_array = nlohmann::json::array();
2217
2218 for (const std::string &iface_item : iface_list)
2219 {
2220 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2221 {
2222 iface_array.push_back(
2223 {{"@odata.id",
2224 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2225 rootInterfaceName + "/VLANs/" + iface_item}});
2226 }
2227 }
2228
Jason M. Billsf12894f2018-10-09 12:45:45 -07002229 asyncResp->res.jsonValue["Members@odata.count"] =
2230 iface_array.size();
2231 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2232 asyncResp->res.jsonValue["@odata.id"] =
2233 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2234 rootInterfaceName + "/VLANs";
2235 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002236 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002237
Ed Tanous1abe55e2018-09-05 08:30:59 -07002238 void doPost(crow::Response &res, const crow::Request &req,
2239 const std::vector<std::string> &params) override
2240 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002241 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002242 if (params.size() != 1)
2243 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002244 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002245 return;
2246 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002247 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002248 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002249 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2250 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002251 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002252 return;
2253 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002254 // Need both vlanId and vlanEnable to service this request
2255 if (!vlanId)
2256 {
2257 messages::propertyMissing(asyncResp->res, "VLANId");
2258 }
2259 if (!vlanEnable)
2260 {
2261 messages::propertyMissing(asyncResp->res, "VLANEnable");
2262 }
Ed Tanous271584a2019-07-09 16:24:22 -07002263 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002264 {
2265 return;
2266 }
2267
Ed Tanous4a0cb852018-10-15 07:55:04 -07002268 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002269 auto callback = [asyncResp](const boost::system::error_code ec) {
2270 if (ec)
2271 {
2272 // TODO(ed) make more consistent error messages based on
2273 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002274 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002275 return;
2276 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002277 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002278 };
2279 crow::connections::systemBus->async_method_call(
2280 std::move(callback), "xyz.openbmc_project.Network",
2281 "/xyz/openbmc_project/network",
2282 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002283 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002284 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002285};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002286} // namespace redfish