blob: 56e1a1508ba23da350e3a93bc8d49d8eea04f5fb [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous1abe55e2018-09-05 08:30:59 -070018#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020021#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <node.hpp>
Ed Tanousa24526d2018-12-10 15:17:59 -080023#include <optional>
Joshi-Mansiab6554f2020-03-10 18:33:36 +053024#include <regex>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020025#include <utils/json_utils.hpp>
Ed Tanousabf2add2019-01-22 16:40:12 -080026#include <variant>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace redfish
29{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010030
31/**
32 * DBus types primitives for several generic DBus interfaces
33 * TODO(Pawel) consider move this to separate file into boost::dbus
34 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070035using PropertiesMapType = boost::container::flat_map<
Ed Tanousabf2add2019-01-22 16:40:12 -080036 std::string, std::variant<std::string, bool, uint8_t, int16_t, uint16_t,
37 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010038
Ed Tanous4a0cb852018-10-15 07:55:04 -070039using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070040 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070041 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070042 std::string,
43 boost::container::flat_map<
Ed Tanous029573d2019-02-01 10:57:49 -080044 std::string, sdbusplus::message::variant<
45 std::string, bool, uint8_t, int16_t, uint16_t,
46 int32_t, uint32_t, int64_t, uint64_t, double,
47 std::vector<std::string>>>>>>>;
Ed Tanous4a0cb852018-10-15 07:55:04 -070048
49enum class LinkType
50{
51 Local,
52 Global
53};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010054
55/**
56 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010057 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070058struct IPv4AddressData
59{
60 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070061 std::string address;
62 std::string domain;
63 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070064 std::string netmask;
65 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070066 LinkType linktype;
67
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 bool operator<(const IPv4AddressData &obj) const
69 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070070 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070071 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010072};
73
74/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -050075 * Structure for keeping IPv6 data required by Redfish
76 */
77struct IPv6AddressData
78{
79 std::string id;
80 std::string address;
81 std::string origin;
82 uint8_t prefixLength;
83
84 bool operator<(const IPv6AddressData &obj) const
85 {
86 return id < obj.id;
87 }
88};
89/**
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010090 * Structure for keeping basic single Ethernet Interface information
91 * available from DBus
92 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070093struct EthernetInterfaceData
94{
Ed Tanous4a0cb852018-10-15 07:55:04 -070095 uint32_t speed;
96 bool auto_neg;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -070097 bool DNSEnabled;
98 bool NTPEnabled;
99 bool HostNameEnabled;
100 bool SendHostNameEnabled;
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800101 bool linkUp;
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700102 bool nicEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700103 std::string DHCPEnabled;
104 std::string operatingMode;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700105 std::string hostname;
106 std::string default_gateway;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500107 std::string ipv6_default_gateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700108 std::string mac_address;
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500109 std::vector<std::uint32_t> vlan_id;
Ed Tanous029573d2019-02-01 10:57:49 -0800110 std::vector<std::string> nameservers;
Jennifer Leed24bfc72019-03-05 13:03:37 -0800111 std::vector<std::string> domainnames;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100112};
113
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700114struct DHCPParameters
115{
116 std::optional<bool> dhcpv4Enabled;
117 std::optional<bool> useDNSServers;
118 std::optional<bool> useNTPServers;
119 std::optional<bool> useUseDomainName;
120 std::optional<std::string> dhcpv6OperatingMode;
121};
122
Ed Tanous4a0cb852018-10-15 07:55:04 -0700123// Helper function that changes bits netmask notation (i.e. /24)
124// into full dot notation
125inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700127 uint32_t value = 0xffffffff << (32 - bits);
128 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
129 std::to_string((value >> 16) & 0xff) + "." +
130 std::to_string((value >> 8) & 0xff) + "." +
131 std::to_string(value & 0xff);
132 return netmask;
133}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100134
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700135inline bool translateDHCPEnabledToBool(const std::string &inputDHCP,
136 bool isIPv4)
137{
138 if (isIPv4)
139 {
140 return (
141 (inputDHCP ==
142 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4") ||
143 (inputDHCP ==
144 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
145 }
146 return ((inputDHCP ==
147 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6") ||
148 (inputDHCP ==
149 "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both"));
150}
151
152inline std::string GetDHCPEnabledEnumeration(bool isIPv4, bool isIPv6)
153{
154 if (isIPv4 && isIPv6)
155 {
156 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.both";
157 }
158 else if (isIPv4)
159 {
160 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v4";
161 }
162 else if (isIPv6)
163 {
164 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.v6";
165 }
166 return "xyz.openbmc_project.Network.EthernetInterface.DHCPConf.none";
167}
168
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169inline std::string
170 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
171 bool isIPv4)
172{
173 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700175 return "Static";
176 }
177 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
178 {
179 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700181 return "IPv4LinkLocal";
182 }
183 else
184 {
185 return "LinkLocal";
186 }
187 }
188 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
189 {
190 if (isIPv4)
191 {
192 return "DHCP";
193 }
194 else
195 {
196 return "DHCPv6";
197 }
198 }
199 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
200 {
201 return "SLAAC";
202 }
203 return "";
204}
205
Ed Tanous4c9afe42019-05-03 16:59:57 -0700206inline bool extractEthernetInterfaceData(const std::string &ethiface_id,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700207 const GetManagedObjects &dbus_data,
208 EthernetInterfaceData &ethData)
209{
Ed Tanous4c9afe42019-05-03 16:59:57 -0700210 bool idFound = false;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700211 for (const auto &objpath : dbus_data)
212 {
Ed Tanous029573d2019-02-01 10:57:49 -0800213 for (const auto &ifacePair : objpath.second)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700214 {
Ed Tanous029573d2019-02-01 10:57:49 -0800215 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 {
Ed Tanous4c9afe42019-05-03 16:59:57 -0700217 idFound = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700218 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700220 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700221 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700222 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700224 const std::string *mac =
Ed Tanousabf2add2019-01-22 16:40:12 -0800225 std::get_if<std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700226 if (mac != nullptr)
227 {
228 ethData.mac_address = *mac;
229 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700231 }
232 }
233 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
234 {
235 for (const auto &propertyPair : ifacePair.second)
236 {
237 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700238 {
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800239 const uint32_t *id =
Ed Tanousabf2add2019-01-22 16:40:12 -0800240 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700241 if (id != nullptr)
242 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -0500243 ethData.vlan_id.push_back(*id);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700244 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 }
247 }
248 else if (ifacePair.first ==
249 "xyz.openbmc_project.Network.EthernetInterface")
250 {
251 for (const auto &propertyPair : ifacePair.second)
252 {
253 if (propertyPair.first == "AutoNeg")
254 {
255 const bool *auto_neg =
Ed Tanousabf2add2019-01-22 16:40:12 -0800256 std::get_if<bool>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700257 if (auto_neg != nullptr)
258 {
259 ethData.auto_neg = *auto_neg;
260 }
261 }
262 else if (propertyPair.first == "Speed")
263 {
264 const uint32_t *speed =
Ed Tanousabf2add2019-01-22 16:40:12 -0800265 std::get_if<uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700266 if (speed != nullptr)
267 {
268 ethData.speed = *speed;
269 }
270 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -0800271 else if (propertyPair.first == "LinkUp")
272 {
273 const bool *linkUp =
274 std::get_if<bool>(&propertyPair.second);
275 if (linkUp != nullptr)
276 {
277 ethData.linkUp = *linkUp;
278 }
279 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -0700280 else if (propertyPair.first == "NICEnabled")
281 {
282 const bool *nicEnabled =
283 std::get_if<bool>(&propertyPair.second);
284 if (nicEnabled != nullptr)
285 {
286 ethData.nicEnabled = *nicEnabled;
287 }
288 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -0500289 else if (propertyPair.first == "Nameservers")
Ed Tanous4a0cb852018-10-15 07:55:04 -0700290 {
Ed Tanous029573d2019-02-01 10:57:49 -0800291 const std::vector<std::string> *nameservers =
292 sdbusplus::message::variant_ns::get_if<
293 std::vector<std::string>>(
294 &propertyPair.second);
295 if (nameservers != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700296 {
Ed Tanous029573d2019-02-01 10:57:49 -0800297 ethData.nameservers = std::move(*nameservers);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700298 }
299 }
manojkiraneda2a133282019-02-19 13:09:43 +0530300 else if (propertyPair.first == "DHCPEnabled")
301 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700302 const std::string *DHCPEnabled =
303 std::get_if<std::string>(&propertyPair.second);
manojkiraneda2a133282019-02-19 13:09:43 +0530304 if (DHCPEnabled != nullptr)
305 {
306 ethData.DHCPEnabled = *DHCPEnabled;
307 }
308 }
Jennifer Leed24bfc72019-03-05 13:03:37 -0800309 else if (propertyPair.first == "DomainName")
310 {
311 const std::vector<std::string> *domainNames =
312 sdbusplus::message::variant_ns::get_if<
313 std::vector<std::string>>(
314 &propertyPair.second);
315 if (domainNames != nullptr)
316 {
317 ethData.domainnames = std::move(*domainNames);
318 }
319 }
Ed Tanous029573d2019-02-01 10:57:49 -0800320 }
321 }
322 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -0700323
324 if (objpath.first == "/xyz/openbmc_project/network/config/dhcp")
325 {
326 if (ifacePair.first ==
327 "xyz.openbmc_project.Network.DHCPConfiguration")
328 {
329 for (const auto &propertyPair : ifacePair.second)
330 {
331 if (propertyPair.first == "DNSEnabled")
332 {
333 const bool *DNSEnabled =
334 std::get_if<bool>(&propertyPair.second);
335 if (DNSEnabled != nullptr)
336 {
337 ethData.DNSEnabled = *DNSEnabled;
338 }
339 }
340 else if (propertyPair.first == "NTPEnabled")
341 {
342 const bool *NTPEnabled =
343 std::get_if<bool>(&propertyPair.second);
344 if (NTPEnabled != nullptr)
345 {
346 ethData.NTPEnabled = *NTPEnabled;
347 }
348 }
349 else if (propertyPair.first == "HostNameEnabled")
350 {
351 const bool *HostNameEnabled =
352 std::get_if<bool>(&propertyPair.second);
353 if (HostNameEnabled != nullptr)
354 {
355 ethData.HostNameEnabled = *HostNameEnabled;
356 }
357 }
358 else if (propertyPair.first == "SendHostNameEnabled")
359 {
360 const bool *SendHostNameEnabled =
361 std::get_if<bool>(&propertyPair.second);
362 if (SendHostNameEnabled != nullptr)
363 {
364 ethData.SendHostNameEnabled =
365 *SendHostNameEnabled;
366 }
367 }
368 }
369 }
370 }
Ed Tanous029573d2019-02-01 10:57:49 -0800371 // System configuration shows up in the global namespace, so no need
372 // to check eth number
373 if (ifacePair.first ==
374 "xyz.openbmc_project.Network.SystemConfiguration")
375 {
376 for (const auto &propertyPair : ifacePair.second)
377 {
378 if (propertyPair.first == "HostName")
379 {
380 const std::string *hostname =
381 sdbusplus::message::variant_ns::get_if<std::string>(
382 &propertyPair.second);
383 if (hostname != nullptr)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700384 {
Ed Tanous029573d2019-02-01 10:57:49 -0800385 ethData.hostname = *hostname;
386 }
387 }
388 else if (propertyPair.first == "DefaultGateway")
389 {
390 const std::string *defaultGateway =
391 sdbusplus::message::variant_ns::get_if<std::string>(
392 &propertyPair.second);
393 if (defaultGateway != nullptr)
394 {
395 ethData.default_gateway = *defaultGateway;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700396 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -0500398 else if (propertyPair.first == "DefaultGateway6")
399 {
400 const std::string *defaultGateway6 =
401 sdbusplus::message::variant_ns::get_if<std::string>(
402 &propertyPair.second);
403 if (defaultGateway6 != nullptr)
404 {
405 ethData.ipv6_default_gateway = *defaultGateway6;
406 }
407 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700408 }
409 }
410 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700411 }
Ed Tanous4c9afe42019-05-03 16:59:57 -0700412 return idFound;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700413}
414
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500415// Helper function that extracts data for single ethernet ipv6 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700416inline void
417 extractIPV6Data(const std::string &ethiface_id,
418 const GetManagedObjects &dbus_data,
419 boost::container::flat_set<IPv6AddressData> &ipv6_config)
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500420{
421 const std::string ipv6PathStart =
422 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv6/";
423
424 // Since there might be several IPv6 configurations aligned with
425 // single ethernet interface, loop over all of them
426 for (const auto &objpath : dbus_data)
427 {
428 // Check if proper pattern for object path appears
429 if (boost::starts_with(objpath.first.str, ipv6PathStart))
430 {
431 for (auto &interface : objpath.second)
432 {
433 if (interface.first == "xyz.openbmc_project.Network.IP")
434 {
435 // Instance IPv6AddressData structure, and set as
436 // appropriate
437 std::pair<
438 boost::container::flat_set<IPv6AddressData>::iterator,
439 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700440 it = ipv6_config.insert(IPv6AddressData{});
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500441 IPv6AddressData &ipv6_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700442 ipv6_address.id =
443 objpath.first.str.substr(ipv6PathStart.size());
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500444 for (auto &property : interface.second)
445 {
446 if (property.first == "Address")
447 {
448 const std::string *address =
449 std::get_if<std::string>(&property.second);
450 if (address != nullptr)
451 {
452 ipv6_address.address = *address;
453 }
454 }
455 else if (property.first == "Origin")
456 {
457 const std::string *origin =
458 std::get_if<std::string>(&property.second);
459 if (origin != nullptr)
460 {
461 ipv6_address.origin =
462 translateAddressOriginDbusToRedfish(*origin,
463 false);
464 }
465 }
466 else if (property.first == "PrefixLength")
467 {
468 const uint8_t *prefix =
469 std::get_if<uint8_t>(&property.second);
470 if (prefix != nullptr)
471 {
472 ipv6_address.prefixLength = *prefix;
473 }
474 }
475 else
476 {
477 BMCWEB_LOG_ERROR
478 << "Got extra property: " << property.first
479 << " on the " << objpath.first.str << " object";
480 }
481 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500482 }
483 }
484 }
485 }
486}
487
Ed Tanous4a0cb852018-10-15 07:55:04 -0700488// Helper function that extracts data for single ethernet ipv4 address
Johnathan Mantey01784822019-06-18 12:44:21 -0700489inline void
490 extractIPData(const std::string &ethiface_id,
491 const GetManagedObjects &dbus_data,
492 boost::container::flat_set<IPv4AddressData> &ipv4_config)
Ed Tanous4a0cb852018-10-15 07:55:04 -0700493{
494 const std::string ipv4PathStart =
495 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
496
497 // Since there might be several IPv4 configurations aligned with
498 // single ethernet interface, loop over all of them
499 for (const auto &objpath : dbus_data)
500 {
501 // Check if proper pattern for object path appears
502 if (boost::starts_with(objpath.first.str, ipv4PathStart))
503 {
504 for (auto &interface : objpath.second)
505 {
506 if (interface.first == "xyz.openbmc_project.Network.IP")
507 {
508 // Instance IPv4AddressData structure, and set as
509 // appropriate
510 std::pair<
511 boost::container::flat_set<IPv4AddressData>::iterator,
512 bool>
Ed Tanous271584a2019-07-09 16:24:22 -0700513 it = ipv4_config.insert(IPv4AddressData{});
Ed Tanous4a0cb852018-10-15 07:55:04 -0700514 IPv4AddressData &ipv4_address = *it.first;
Ed Tanous271584a2019-07-09 16:24:22 -0700515 ipv4_address.id =
516 objpath.first.str.substr(ipv4PathStart.size());
Ed Tanous4a0cb852018-10-15 07:55:04 -0700517 for (auto &property : interface.second)
518 {
519 if (property.first == "Address")
520 {
521 const std::string *address =
Ed Tanousabf2add2019-01-22 16:40:12 -0800522 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700523 if (address != nullptr)
524 {
525 ipv4_address.address = *address;
526 }
527 }
528 else if (property.first == "Gateway")
529 {
530 const std::string *gateway =
Ed Tanousabf2add2019-01-22 16:40:12 -0800531 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700532 if (gateway != nullptr)
533 {
534 ipv4_address.gateway = *gateway;
535 }
536 }
537 else if (property.first == "Origin")
538 {
539 const std::string *origin =
Ed Tanousabf2add2019-01-22 16:40:12 -0800540 std::get_if<std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700541 if (origin != nullptr)
542 {
543 ipv4_address.origin =
544 translateAddressOriginDbusToRedfish(*origin,
545 true);
546 }
547 }
548 else if (property.first == "PrefixLength")
549 {
550 const uint8_t *mask =
Ed Tanousabf2add2019-01-22 16:40:12 -0800551 std::get_if<uint8_t>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700552 if (mask != nullptr)
553 {
554 // convert it to the string
555 ipv4_address.netmask = getNetmask(*mask);
556 }
557 }
558 else
559 {
560 BMCWEB_LOG_ERROR
561 << "Got extra property: " << property.first
562 << " on the " << objpath.first.str << " object";
563 }
564 }
565 // Check if given address is local, or global
566 ipv4_address.linktype =
567 boost::starts_with(ipv4_address.address, "169.254.")
Johnathan Mantey18659d12019-06-07 10:26:29 -0700568 ? LinkType::Local
569 : LinkType::Global;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700570 }
571 }
572 }
573 }
574}
575
576/**
577 * @brief Sets given Id on the given VLAN interface through D-Bus
578 *
579 * @param[in] ifaceId Id of VLAN interface that should be modified
580 * @param[in] inputVlanId New ID of the VLAN
581 * @param[in] callback Function that will be called after the operation
582 *
583 * @return None.
584 */
585template <typename CallbackFunc>
586void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
587 CallbackFunc &&callback)
588{
589 crow::connections::systemBus->async_method_call(
590 callback, "xyz.openbmc_project.Network",
591 std::string("/xyz/openbmc_project/network/") + ifaceId,
592 "org.freedesktop.DBus.Properties", "Set",
593 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanousabf2add2019-01-22 16:40:12 -0800594 std::variant<uint32_t>(inputVlanId));
Ed Tanous4a0cb852018-10-15 07:55:04 -0700595}
596
597/**
598 * @brief Helper function that verifies IP address to check if it is in
599 * proper format. If bits pointer is provided, also calculates active
600 * bit count for Subnet Mask.
601 *
602 * @param[in] ip IP that will be verified
603 * @param[out] bits Calculated mask in bits notation
604 *
605 * @return true in case of success, false otherwise
606 */
607inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
608 uint8_t *bits = nullptr)
609{
610 std::vector<std::string> bytesInMask;
611
612 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613
614 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700615 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700616 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700617 return false;
618 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700619
Ed Tanous4a0cb852018-10-15 07:55:04 -0700620 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622 *bits = 0;
623 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700624
Ed Tanous4a0cb852018-10-15 07:55:04 -0700625 char *endPtr;
626 long previousValue = 255;
627 bool firstZeroInByteHit;
628 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700630 if (byte.empty())
631 {
632 return false;
633 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634
Ed Tanous4a0cb852018-10-15 07:55:04 -0700635 // Use strtol instead of stroi to avoid exceptions
636 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637
Ed Tanous4a0cb852018-10-15 07:55:04 -0700638 // endPtr should point to the end of the string, otherwise given string
639 // is not 100% number
640 if (*endPtr != '\0')
641 {
642 return false;
643 }
644
645 // Value should be contained in byte
646 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 {
648 return false;
649 }
650
651 if (bits != nullptr)
652 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700653 // Mask has to be continuous between bytes
654 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
656 return false;
657 }
658
Ed Tanous4a0cb852018-10-15 07:55:04 -0700659 // Mask has to be continuous inside bytes
660 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700661
Ed Tanous4a0cb852018-10-15 07:55:04 -0700662 // Count bits
663 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700665 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700667 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700668 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700669 // Continuity not preserved
670 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 }
672 else
673 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700674 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 }
676 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 else
678 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700679 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700680 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700681 }
682 }
683
684 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 }
686
Ed Tanous4a0cb852018-10-15 07:55:04 -0700687 return true;
688}
689
690/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700691 * @brief Deletes given IPv4 interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700692 *
693 * @param[in] ifaceId Id of interface whose IP should be deleted
Ed Tanous4a0cb852018-10-15 07:55:04 -0700694 * @param[in] ipHash DBus Hash id of IP that should be deleted
695 * @param[io] asyncResp Response object that will be returned to client
696 *
697 * @return None
698 */
699inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700700 const std::shared_ptr<AsyncResp> asyncResp)
701{
702 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700703 [asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700704 if (ec)
705 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800706 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100707 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700708 },
709 "xyz.openbmc_project.Network",
710 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
711 "xyz.openbmc_project.Object.Delete", "Delete");
712}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700713
Ed Tanous4a0cb852018-10-15 07:55:04 -0700714/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700715 * @brief Creates a static IPv4 entry
Ed Tanous4a0cb852018-10-15 07:55:04 -0700716 *
Johnathan Mantey01784822019-06-18 12:44:21 -0700717 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
718 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
719 * @param[in] gateway IPv4 address of this interfaces gateway
720 * @param[in] address IPv4 address to assign to this interface
721 * @param[io] asyncResp Response object that will be returned to client
Ed Tanous4a0cb852018-10-15 07:55:04 -0700722 *
723 * @return None
724 */
Ed Tanousb01bf292019-03-25 19:25:26 +0000725inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
Johnathan Mantey01784822019-06-18 12:44:21 -0700726 uint8_t prefixLength, const std::string &gateway,
Ed Tanousb01bf292019-03-25 19:25:26 +0000727 const std::string &address,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700728 std::shared_ptr<AsyncResp> asyncResp)
729{
Ed Tanous4a0cb852018-10-15 07:55:04 -0700730 crow::connections::systemBus->async_method_call(
Johnathan Mantey01784822019-06-18 12:44:21 -0700731 [asyncResp](const boost::system::error_code ec) {
732 if (ec)
733 {
734 messages::internalError(asyncResp->res);
735 }
736 },
737 "xyz.openbmc_project.Network",
Ed Tanous4a0cb852018-10-15 07:55:04 -0700738 "/xyz/openbmc_project/network/" + ifaceId,
739 "xyz.openbmc_project.Network.IP.Create", "IP",
Johnathan Mantey01784822019-06-18 12:44:21 -0700740 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, prefixLength,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700741 gateway);
742}
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500743
744/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700745 * @brief Deletes the IPv4 entry for this interface and creates a replacement
746 * static IPv4 entry
747 *
748 * @param[in] ifaceId Id of interface upon which to create the IPv4 entry
749 * @param[in] id The unique hash entry identifying the DBus entry
750 * @param[in] prefixLength IPv4 prefix syntax for the subnet mask
751 * @param[in] gateway IPv4 address of this interfaces gateway
752 * @param[in] address IPv4 address to assign to this interface
753 * @param[io] asyncResp Response object that will be returned to client
754 *
755 * @return None
756 */
757inline void deleteAndCreateIPv4(const std::string &ifaceId,
758 const std::string &id, uint8_t prefixLength,
759 const std::string &gateway,
760 const std::string &address,
761 std::shared_ptr<AsyncResp> asyncResp)
762{
763 crow::connections::systemBus->async_method_call(
764 [asyncResp, ifaceId, address, prefixLength,
765 gateway](const boost::system::error_code ec) {
766 if (ec)
767 {
768 messages::internalError(asyncResp->res);
769 }
770 crow::connections::systemBus->async_method_call(
771 [asyncResp](const boost::system::error_code ec) {
772 if (ec)
773 {
774 messages::internalError(asyncResp->res);
775 }
776 },
777 "xyz.openbmc_project.Network",
778 "/xyz/openbmc_project/network/" + ifaceId,
779 "xyz.openbmc_project.Network.IP.Create", "IP",
780 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address,
781 prefixLength, gateway);
782 },
783 "xyz.openbmc_project.Network",
784 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + id,
785 "xyz.openbmc_project.Object.Delete", "Delete");
786}
787
788/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500789 * @brief Deletes given IPv6
790 *
791 * @param[in] ifaceId Id of interface whose IP should be deleted
792 * @param[in] ipHash DBus Hash id of IP that should be deleted
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500793 * @param[io] asyncResp Response object that will be returned to client
794 *
795 * @return None
796 */
797inline void deleteIPv6(const std::string &ifaceId, const std::string &ipHash,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500798 const std::shared_ptr<AsyncResp> asyncResp)
799{
800 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -0700801 [asyncResp](const boost::system::error_code ec) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500802 if (ec)
803 {
804 messages::internalError(asyncResp->res);
805 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500806 },
807 "xyz.openbmc_project.Network",
808 "/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + ipHash,
809 "xyz.openbmc_project.Object.Delete", "Delete");
810}
811
812/**
Johnathan Mantey01784822019-06-18 12:44:21 -0700813 * @brief Deletes the IPv6 entry for this interface and creates a replacement
814 * static IPv6 entry
815 *
816 * @param[in] ifaceId Id of interface upon which to create the IPv6 entry
817 * @param[in] id The unique hash entry identifying the DBus entry
818 * @param[in] prefixLength IPv6 prefix syntax for the subnet mask
819 * @param[in] address IPv6 address to assign to this interface
820 * @param[io] asyncResp Response object that will be returned to client
821 *
822 * @return None
823 */
824inline void deleteAndCreateIPv6(const std::string &ifaceId,
825 const std::string &id, uint8_t prefixLength,
826 const std::string &address,
827 std::shared_ptr<AsyncResp> asyncResp)
828{
829 crow::connections::systemBus->async_method_call(
830 [asyncResp, ifaceId, address,
831 prefixLength](const boost::system::error_code ec) {
832 if (ec)
833 {
834 messages::internalError(asyncResp->res);
835 }
836 crow::connections::systemBus->async_method_call(
837 [asyncResp](const boost::system::error_code ec) {
838 if (ec)
839 {
840 messages::internalError(asyncResp->res);
841 }
842 },
843 "xyz.openbmc_project.Network",
844 "/xyz/openbmc_project/network/" + ifaceId,
845 "xyz.openbmc_project.Network.IP.Create", "IP",
846 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address,
847 prefixLength, "");
848 },
849 "xyz.openbmc_project.Network",
850 +"/xyz/openbmc_project/network/" + ifaceId + "/ipv6/" + id,
851 "xyz.openbmc_project.Object.Delete", "Delete");
852}
853
854/**
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500855 * @brief Creates IPv6 with given data
856 *
857 * @param[in] ifaceId Id of interface whose IP should be added
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500858 * @param[in] prefixLength Prefix length that needs to be added
859 * @param[in] address IP address that needs to be added
860 * @param[io] asyncResp Response object that will be returned to client
861 *
862 * @return None
863 */
Johnathan Mantey01784822019-06-18 12:44:21 -0700864inline void createIPv6(const std::string &ifaceId, uint8_t prefixLength,
865 const std::string &address,
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500866 std::shared_ptr<AsyncResp> asyncResp)
867{
868 auto createIpHandler = [asyncResp](const boost::system::error_code ec) {
869 if (ec)
870 {
871 messages::internalError(asyncResp->res);
872 }
873 };
874 // Passing null for gateway, as per redfish spec IPv6StaticAddresses object
875 // does not have assosiated gateway property
876 crow::connections::systemBus->async_method_call(
877 std::move(createIpHandler), "xyz.openbmc_project.Network",
878 "/xyz/openbmc_project/network/" + ifaceId,
879 "xyz.openbmc_project.Network.IP.Create", "IP",
880 "xyz.openbmc_project.Network.IP.Protocol.IPv6", address, prefixLength,
881 "");
882}
883
Ed Tanous4a0cb852018-10-15 07:55:04 -0700884/**
885 * Function that retrieves all properties for given Ethernet Interface
886 * Object
887 * from EntityManager Network Manager
888 * @param ethiface_id a eth interface id to query on DBus
889 * @param callback a function that shall be called to convert Dbus output
890 * into JSON
891 */
892template <typename CallbackFunc>
893void getEthernetIfaceData(const std::string &ethiface_id,
894 CallbackFunc &&callback)
895{
896 crow::connections::systemBus->async_method_call(
897 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
898 const boost::system::error_code error_code,
899 const GetManagedObjects &resp) {
900 EthernetInterfaceData ethData{};
901 boost::container::flat_set<IPv4AddressData> ipv4Data;
Ravi Tejae48c0fc2019-04-16 08:37:20 -0500902 boost::container::flat_set<IPv6AddressData> ipv6Data;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903
904 if (error_code)
905 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700906 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700907 return;
908 }
909
Ed Tanous4c9afe42019-05-03 16:59:57 -0700910 bool found =
911 extractEthernetInterfaceData(ethiface_id, resp, ethData);
912 if (!found)
913 {
Johnathan Mantey01784822019-06-18 12:44:21 -0700914 callback(false, ethData, ipv4Data, ipv6Data);
Ed Tanous4c9afe42019-05-03 16:59:57 -0700915 return;
916 }
917
Johnathan Mantey01784822019-06-18 12:44:21 -0700918 extractIPData(ethiface_id, resp, ipv4Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700919 // Fix global GW
920 for (IPv4AddressData &ipv4 : ipv4Data)
921 {
Ravi Tejac6191412019-07-30 00:53:50 -0500922 if (((ipv4.linktype == LinkType::Global) &&
923 (ipv4.gateway == "0.0.0.0")) ||
924 (ipv4.origin == "DHCP"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700925 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700926 ipv4.gateway = ethData.default_gateway;
927 }
928 }
929
Johnathan Mantey01784822019-06-18 12:44:21 -0700930 extractIPV6Data(ethiface_id, resp, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700931 // Finally make a callback with usefull data
Johnathan Mantey01784822019-06-18 12:44:21 -0700932 callback(true, ethData, ipv4Data, ipv6Data);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700933 },
934 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
935 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700936}
Ed Tanous4a0cb852018-10-15 07:55:04 -0700937
938/**
939 * Function that retrieves all Ethernet Interfaces available through Network
940 * Manager
941 * @param callback a function that shall be called to convert Dbus output
942 * into JSON.
943 */
944template <typename CallbackFunc>
945void getEthernetIfaceList(CallbackFunc &&callback)
946{
947 crow::connections::systemBus->async_method_call(
948 [callback{std::move(callback)}](
949 const boost::system::error_code error_code,
950 GetManagedObjects &resp) {
951 // Callback requires vector<string> to retrieve all available
952 // ethernet interfaces
Ed Tanous4c9afe42019-05-03 16:59:57 -0700953 boost::container::flat_set<std::string> iface_list;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954 iface_list.reserve(resp.size());
955 if (error_code)
956 {
957 callback(false, iface_list);
958 return;
959 }
960
961 // Iterate over all retrieved ObjectPaths.
962 for (const auto &objpath : resp)
963 {
964 // And all interfaces available for certain ObjectPath.
965 for (const auto &interface : objpath.second)
966 {
967 // If interface is
968 // xyz.openbmc_project.Network.EthernetInterface, this is
969 // what we're looking for.
970 if (interface.first ==
971 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700973 // Cut out everyting until last "/", ...
974 const std::string &iface_id = objpath.first.str;
975 std::size_t last_pos = iface_id.rfind("/");
976 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700977 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700978 // and put it into output vector.
Ed Tanous4c9afe42019-05-03 16:59:57 -0700979 iface_list.emplace(iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 }
981 }
982 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700983 }
984 // Finally make a callback with useful data
985 callback(true, iface_list);
986 },
987 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
988 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Ed Tanous271584a2019-07-09 16:24:22 -0700989}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100990
991/**
992 * EthernetCollection derived class for delivering Ethernet Collection Schema
993 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994class EthernetCollection : public Node
995{
996 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700997 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700999 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 entityPrivileges = {
1002 {boost::beast::http::verb::get, {{"Login"}}},
1003 {boost::beast::http::verb::head, {{"Login"}}},
1004 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1005 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1006 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1007 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
1008 }
1009
1010 private:
1011 /**
1012 * Functions triggers appropriate requests on DBus
1013 */
1014 void doGet(crow::Response &res, const crow::Request &req,
1015 const std::vector<std::string> &params) override
1016 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001017 res.jsonValue["@odata.type"] =
1018 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08001019 res.jsonValue["@odata.id"] =
1020 "/redfish/v1/Managers/bmc/EthernetInterfaces";
1021 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
1022 res.jsonValue["Description"] =
1023 "Collection of EthernetInterfaces for this Manager";
Ed Tanous4c9afe42019-05-03 16:59:57 -07001024 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001025 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001026 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001027 getEthernetIfaceList(
Ed Tanous4c9afe42019-05-03 16:59:57 -07001028 [asyncResp](
1029 const bool &success,
1030 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001031 if (!success)
1032 {
Ed Tanous4c9afe42019-05-03 16:59:57 -07001033 messages::internalError(asyncResp->res);
Jason M. Billsf12894f2018-10-09 12:45:45 -07001034 return;
1035 }
1036
Ed Tanous4c9afe42019-05-03 16:59:57 -07001037 nlohmann::json &iface_array =
1038 asyncResp->res.jsonValue["Members"];
Jason M. Billsf12894f2018-10-09 12:45:45 -07001039 iface_array = nlohmann::json::array();
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001040 std::string tag = "_";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001041 for (const std::string &iface_item : iface_list)
1042 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001043 std::size_t found = iface_item.find(tag);
1044 if (found == std::string::npos)
1045 {
1046 iface_array.push_back(
1047 {{"@odata.id",
1048 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1049 iface_item}});
1050 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001051 }
1052
Ed Tanous4c9afe42019-05-03 16:59:57 -07001053 asyncResp->res.jsonValue["Members@odata.count"] =
1054 iface_array.size();
1055 asyncResp->res.jsonValue["@odata.id"] =
Jason M. Billsf12894f2018-10-09 12:45:45 -07001056 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Jason M. Billsf12894f2018-10-09 12:45:45 -07001057 });
Ed Tanous4a0cb852018-10-15 07:55:04 -07001058 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001059};
1060
1061/**
1062 * EthernetInterface derived class for delivering Ethernet Schema
1063 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001064class EthernetInterface : public Node
1065{
1066 public:
1067 /*
1068 * Default Constructor
1069 */
Ed Tanous4a0cb852018-10-15 07:55:04 -07001070 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001071 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001072 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 std::string())
1074 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001075 entityPrivileges = {
1076 {boost::beast::http::verb::get, {{"Login"}}},
1077 {boost::beast::http::verb::head, {{"Login"}}},
1078 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1079 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1080 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1081 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +02001082 }
1083
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 private:
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001085 void handleHostnamePatch(const std::string &hostname,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001086 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001087 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301088 // SHOULD handle host names of up to 255 characters(RFC 1123)
1089 if (hostname.length() > 255)
1090 {
1091 messages::propertyValueFormatError(asyncResp->res, hostname,
1092 "HostName");
1093 return;
1094 }
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001095 crow::connections::systemBus->async_method_call(
1096 [asyncResp](const boost::system::error_code ec) {
1097 if (ec)
1098 {
1099 messages::internalError(asyncResp->res);
1100 }
1101 },
1102 "xyz.openbmc_project.Network",
1103 "/xyz/openbmc_project/network/config",
1104 "org.freedesktop.DBus.Properties", "Set",
1105 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
Ed Tanousabf2add2019-01-22 16:40:12 -08001106 std::variant<std::string>(hostname));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001107 }
1108
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301109 void handleDomainnamePatch(const std::string &ifaceId,
1110 const std::string &domainname,
1111 const std::shared_ptr<AsyncResp> asyncResp)
1112 {
1113 std::vector<std::string> vectorDomainname = {domainname};
1114 crow::connections::systemBus->async_method_call(
1115 [asyncResp](const boost::system::error_code ec) {
1116 if (ec)
1117 {
1118 messages::internalError(asyncResp->res);
1119 }
1120 },
1121 "xyz.openbmc_project.Network",
1122 "/xyz/openbmc_project/network/" + ifaceId,
1123 "org.freedesktop.DBus.Properties", "Set",
1124 "xyz.openbmc_project.Network.EthernetInterface", "DomainName",
1125 std::variant<std::vector<std::string>>(vectorDomainname));
1126 }
1127
1128 void handleFqdnPatch(const std::string &ifaceId, const std::string &fqdn,
1129 const std::shared_ptr<AsyncResp> asyncResp)
1130 {
1131 // Total length of FQDN must not exceed 255 characters(RFC 1035)
1132 if (fqdn.length() > 255)
1133 {
1134 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1135 return;
1136 }
1137
1138 size_t pos = fqdn.find('.');
1139 if (pos == std::string::npos)
1140 {
1141 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1142 return;
1143 }
1144
1145 std::string hostname;
1146 std::string domainname;
1147 domainname = (fqdn).substr(pos + 1);
1148 hostname = (fqdn).substr(0, pos);
1149
1150 if (!isHostnameValid(hostname) || !isDomainnameValid(domainname))
1151 {
1152 messages::propertyValueFormatError(asyncResp->res, fqdn, "FQDN");
1153 return;
1154 }
1155
1156 handleHostnamePatch(hostname, asyncResp);
1157 handleDomainnamePatch(ifaceId, domainname, asyncResp);
1158 }
1159
1160 bool isHostnameValid(const std::string &hostname)
1161 {
1162 // A valid host name can never have the dotted-decimal form (RFC 1123)
1163 if (std::all_of(hostname.begin(), hostname.end(), ::isdigit))
1164 {
1165 return false;
1166 }
1167 // Each label(hostname/subdomains) within a valid FQDN
1168 // MUST handle host names of up to 63 characters (RFC 1123)
1169 // labels cannot start or end with hyphens (RFC 952)
1170 // labels can start with numbers (RFC 1123)
1171 const std::regex pattern(
1172 "^[a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9]$");
1173
1174 return std::regex_match(hostname, pattern);
1175 }
1176
1177 bool isDomainnameValid(const std::string &domainname)
1178 {
1179 // Can have multiple subdomains
1180 // Top Level Domain's min length is 2 character
1181 const std::regex pattern("^([A-Za-z0-9][a-zA-Z0-9\\-]{1,61}|[a-zA-Z0-9]"
1182 "{1,30}\\.)*[a-zA-Z]{2,}$");
1183
1184 return std::regex_match(domainname, pattern);
1185 }
1186
Ratan Guptad5776652019-03-03 08:47:22 +05301187 void handleMACAddressPatch(const std::string &ifaceId,
1188 const std::string &macAddress,
1189 const std::shared_ptr<AsyncResp> &asyncResp)
1190 {
1191 crow::connections::systemBus->async_method_call(
1192 [asyncResp, macAddress](const boost::system::error_code ec) {
1193 if (ec)
1194 {
1195 messages::internalError(asyncResp->res);
1196 return;
1197 }
Ratan Guptad5776652019-03-03 08:47:22 +05301198 },
1199 "xyz.openbmc_project.Network",
1200 "/xyz/openbmc_project/network/" + ifaceId,
1201 "org.freedesktop.DBus.Properties", "Set",
1202 "xyz.openbmc_project.Network.MACAddress", "MACAddress",
1203 std::variant<std::string>(macAddress));
1204 }
Johnathan Mantey286b9112019-06-10 13:38:04 -07001205
Jennifer Leeda131a92019-04-24 15:13:55 -07001206 void setDHCPEnabled(const std::string &ifaceId,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001207 const std::string &propertyName, const bool v4Value,
1208 const bool v6Value,
Jennifer Leeda131a92019-04-24 15:13:55 -07001209 const std::shared_ptr<AsyncResp> asyncResp)
1210 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001211 const std::string dhcp = GetDHCPEnabledEnumeration(v4Value, v6Value);
Jennifer Leeda131a92019-04-24 15:13:55 -07001212 crow::connections::systemBus->async_method_call(
1213 [asyncResp](const boost::system::error_code ec) {
1214 if (ec)
1215 {
1216 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1217 messages::internalError(asyncResp->res);
1218 return;
1219 }
1220 },
1221 "xyz.openbmc_project.Network",
1222 "/xyz/openbmc_project/network/" + ifaceId,
1223 "org.freedesktop.DBus.Properties", "Set",
1224 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001225 std::variant<std::string>{dhcp});
Jennifer Leeda131a92019-04-24 15:13:55 -07001226 }
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001227
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001228 void setEthernetInterfaceBoolProperty(
1229 const std::string &ifaceId, const std::string &propertyName,
1230 const bool &value, const std::shared_ptr<AsyncResp> asyncResp)
1231 {
1232 crow::connections::systemBus->async_method_call(
1233 [asyncResp](const boost::system::error_code ec) {
1234 if (ec)
1235 {
1236 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1237 messages::internalError(asyncResp->res);
1238 return;
1239 }
1240 },
1241 "xyz.openbmc_project.Network",
1242 "/xyz/openbmc_project/network/" + ifaceId,
1243 "org.freedesktop.DBus.Properties", "Set",
1244 "xyz.openbmc_project.Network.EthernetInterface", propertyName,
1245 std::variant<bool>{value});
1246 }
1247
Jennifer Leeda131a92019-04-24 15:13:55 -07001248 void setDHCPv4Config(const std::string &propertyName, const bool &value,
1249 const std::shared_ptr<AsyncResp> asyncResp)
1250 {
1251 BMCWEB_LOG_DEBUG << propertyName << " = " << value;
1252 crow::connections::systemBus->async_method_call(
1253 [asyncResp](const boost::system::error_code ec) {
1254 if (ec)
1255 {
1256 BMCWEB_LOG_ERROR << "D-Bus responses error: " << ec;
1257 messages::internalError(asyncResp->res);
1258 return;
1259 }
1260 },
1261 "xyz.openbmc_project.Network",
1262 "/xyz/openbmc_project/network/config/dhcp",
1263 "org.freedesktop.DBus.Properties", "Set",
1264 "xyz.openbmc_project.Network.DHCPConfiguration", propertyName,
1265 std::variant<bool>{value});
1266 }
Ratan Guptad5776652019-03-03 08:47:22 +05301267
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001268 void handleDHCPPatch(const std::string &ifaceId,
1269 const EthernetInterfaceData &ethData,
1270 DHCPParameters v4dhcpParms, DHCPParameters v6dhcpParms,
1271 const std::shared_ptr<AsyncResp> asyncResp)
Jennifer Leeda131a92019-04-24 15:13:55 -07001272 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001273 bool ipv4Active = translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1274 bool ipv6Active =
1275 translateDHCPEnabledToBool(ethData.DHCPEnabled, false);
Jennifer Leeda131a92019-04-24 15:13:55 -07001276
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001277 bool nextv4DHCPState =
1278 v4dhcpParms.dhcpv4Enabled ? *v4dhcpParms.dhcpv4Enabled : ipv4Active;
1279
1280 bool nextv6DHCPState{};
1281 if (v6dhcpParms.dhcpv6OperatingMode)
Jennifer Leeda131a92019-04-24 15:13:55 -07001282 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001283 if ((*v6dhcpParms.dhcpv6OperatingMode != "Stateful") &&
1284 (*v6dhcpParms.dhcpv6OperatingMode != "Stateless") &&
1285 (*v6dhcpParms.dhcpv6OperatingMode != "Disabled"))
1286 {
1287 messages::propertyValueFormatError(
1288 asyncResp->res, *v6dhcpParms.dhcpv6OperatingMode,
1289 "OperatingMode");
1290 return;
1291 }
1292 nextv6DHCPState = (*v6dhcpParms.dhcpv6OperatingMode == "Stateful");
1293 }
1294 else
1295 {
1296 nextv6DHCPState = ipv6Active;
Jennifer Leeda131a92019-04-24 15:13:55 -07001297 }
1298
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001299 bool nextDNS{};
1300 if (v4dhcpParms.useDNSServers && v6dhcpParms.useDNSServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001301 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001302 if (*v4dhcpParms.useDNSServers != *v6dhcpParms.useDNSServers)
1303 {
1304 messages::generalError(asyncResp->res);
1305 return;
1306 }
1307 nextDNS = *v4dhcpParms.useDNSServers;
1308 }
1309 else if (v4dhcpParms.useDNSServers)
1310 {
1311 nextDNS = *v4dhcpParms.useDNSServers;
1312 }
1313 else if (v6dhcpParms.useDNSServers)
1314 {
1315 nextDNS = *v6dhcpParms.useDNSServers;
1316 }
1317 else
1318 {
1319 nextDNS = ethData.DNSEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001320 }
1321
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001322 bool nextNTP{};
1323 if (v4dhcpParms.useNTPServers && v6dhcpParms.useNTPServers)
Jennifer Leeda131a92019-04-24 15:13:55 -07001324 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001325 if (*v4dhcpParms.useNTPServers != *v6dhcpParms.useNTPServers)
1326 {
1327 messages::generalError(asyncResp->res);
1328 return;
1329 }
1330 nextNTP = *v4dhcpParms.useNTPServers;
1331 }
1332 else if (v4dhcpParms.useNTPServers)
1333 {
1334 nextNTP = *v4dhcpParms.useNTPServers;
1335 }
1336 else if (v6dhcpParms.useNTPServers)
1337 {
1338 nextNTP = *v6dhcpParms.useNTPServers;
1339 }
1340 else
1341 {
1342 nextNTP = ethData.NTPEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001343 }
1344
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001345 bool nextUseDomain{};
1346 if (v4dhcpParms.useUseDomainName && v6dhcpParms.useUseDomainName)
Jennifer Leeda131a92019-04-24 15:13:55 -07001347 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001348 if (*v4dhcpParms.useUseDomainName != *v6dhcpParms.useUseDomainName)
1349 {
1350 messages::generalError(asyncResp->res);
1351 return;
1352 }
1353 nextUseDomain = *v4dhcpParms.useUseDomainName;
1354 }
1355 else if (v4dhcpParms.useUseDomainName)
1356 {
1357 nextUseDomain = *v4dhcpParms.useUseDomainName;
1358 }
1359 else if (v6dhcpParms.useUseDomainName)
1360 {
1361 nextUseDomain = *v6dhcpParms.useUseDomainName;
1362 }
1363 else
1364 {
1365 nextUseDomain = ethData.HostNameEnabled;
Jennifer Leeda131a92019-04-24 15:13:55 -07001366 }
1367
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001368 BMCWEB_LOG_DEBUG << "set DHCPEnabled...";
1369 setDHCPEnabled(ifaceId, "DHCPEnabled", nextv4DHCPState, nextv6DHCPState,
1370 asyncResp);
1371 BMCWEB_LOG_DEBUG << "set DNSEnabled...";
1372 setDHCPv4Config("DNSEnabled", nextDNS, asyncResp);
1373 BMCWEB_LOG_DEBUG << "set NTPEnabled...";
1374 setDHCPv4Config("NTPEnabled", nextNTP, asyncResp);
1375 BMCWEB_LOG_DEBUG << "set HostNameEnabled...";
1376 setDHCPv4Config("HostNameEnabled", nextUseDomain, asyncResp);
Jennifer Leeda131a92019-04-24 15:13:55 -07001377 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001378
1379 boost::container::flat_set<IPv4AddressData>::const_iterator
1380 GetNextStaticIPEntry(
1381 boost::container::flat_set<IPv4AddressData>::const_iterator head,
1382 boost::container::flat_set<IPv4AddressData>::const_iterator end)
1383 {
1384 for (; head != end; head++)
1385 {
1386 if (head->origin == "Static")
1387 {
1388 return head;
1389 }
1390 }
1391 return end;
1392 }
1393
1394 boost::container::flat_set<IPv6AddressData>::const_iterator
1395 GetNextStaticIPEntry(
1396 boost::container::flat_set<IPv6AddressData>::const_iterator head,
1397 boost::container::flat_set<IPv6AddressData>::const_iterator end)
1398 {
1399 for (; head != end; head++)
1400 {
1401 if (head->origin == "Static")
1402 {
1403 return head;
1404 }
1405 }
1406 return end;
1407 }
1408
Ravi Tejad1d50812019-06-23 16:20:27 -05001409 void handleIPv4StaticPatch(
Ratan Guptaf476acb2019-03-02 16:46:57 +05301410 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001411 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001412 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001413 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001414 if ((!input.is_array()) || input.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301415 {
1416 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Ravi Tejad1d50812019-06-23 16:20:27 -05001417 "IPv4StaticAddresses");
Ratan Guptaf476acb2019-03-02 16:46:57 +05301418 return;
1419 }
1420
Ed Tanous271584a2019-07-09 16:24:22 -07001421 unsigned entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001422 // Find the first static IP address currently active on the NIC and
1423 // match it to the first JSON element in the IPv4StaticAddresses array.
1424 // Match each subsequent JSON element to the next static IP programmed
1425 // into the NIC.
1426 boost::container::flat_set<IPv4AddressData>::const_iterator NICIPentry =
1427 GetNextStaticIPEntry(ipv4Data.cbegin(), ipv4Data.cend());
1428
Ed Tanous537174c2018-12-10 15:09:31 -08001429 for (nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001431 std::string pathString =
Ravi Tejad1d50812019-06-23 16:20:27 -05001432 "IPv4StaticAddresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001433
Johnathan Mantey01784822019-06-18 12:44:21 -07001434 if (!thisJson.is_null() && !thisJson.empty())
Ratan Guptaf476acb2019-03-02 16:46:57 +05301435 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001436 std::optional<std::string> address;
1437 std::optional<std::string> subnetMask;
1438 std::optional<std::string> gateway;
1439
1440 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1441 address, "SubnetMask", subnetMask,
1442 "Gateway", gateway))
Ratan Guptaf476acb2019-03-02 16:46:57 +05301443 {
1444 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001445 asyncResp->res, thisJson.dump(), pathString);
Ratan Guptaf476acb2019-03-02 16:46:57 +05301446 return;
Ratan Guptaf476acb2019-03-02 16:46:57 +05301447 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301448
Johnathan Mantey01784822019-06-18 12:44:21 -07001449 // Find the address/subnet/gateway values. Any values that are
1450 // not explicitly provided are assumed to be unmodified from the
1451 // current state of the interface. Merge existing state into the
1452 // current request.
Ed Tanous271584a2019-07-09 16:24:22 -07001453 const std::string *addr = nullptr;
1454 const std::string *gw = nullptr;
Johnathan Mantey01784822019-06-18 12:44:21 -07001455 uint8_t prefixLength = 0;
1456 bool errorInEntry = false;
1457 if (address)
Ratan Gupta9474b372019-03-01 15:13:37 +05301458 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001459 if (ipv4VerifyIpAndGetBitcount(*address))
1460 {
1461 addr = &(*address);
1462 }
1463 else
1464 {
1465 messages::propertyValueFormatError(
1466 asyncResp->res, *address, pathString + "/Address");
1467 errorInEntry = true;
1468 }
1469 }
1470 else if (NICIPentry != ipv4Data.cend())
1471 {
1472 addr = &(NICIPentry->address);
Ratan Gupta9474b372019-03-01 15:13:37 +05301473 }
1474 else
1475 {
1476 messages::propertyMissing(asyncResp->res,
1477 pathString + "/Address");
Johnathan Mantey01784822019-06-18 12:44:21 -07001478 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001479 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301480
1481 if (subnetMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001482 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001483 if (!ipv4VerifyIpAndGetBitcount(*subnetMask, &prefixLength))
1484 {
1485 messages::propertyValueFormatError(
1486 asyncResp->res, *subnetMask,
1487 pathString + "/SubnetMask");
1488 errorInEntry = true;
1489 }
1490 }
1491 else if (NICIPentry != ipv4Data.cend())
1492 {
1493 if (!ipv4VerifyIpAndGetBitcount(NICIPentry->netmask,
1494 &prefixLength))
1495 {
1496 messages::propertyValueFormatError(
1497 asyncResp->res, NICIPentry->netmask,
1498 pathString + "/SubnetMask");
1499 errorInEntry = true;
1500 }
1501 }
1502 else
1503 {
1504 messages::propertyMissing(asyncResp->res,
1505 pathString + "/SubnetMask");
1506 errorInEntry = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001507 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301508
Ratan Guptaf476acb2019-03-02 16:46:57 +05301509 if (gateway)
1510 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001511 if (ipv4VerifyIpAndGetBitcount(*gateway))
1512 {
1513 gw = &(*gateway);
1514 }
1515 else
1516 {
1517 messages::propertyValueFormatError(
1518 asyncResp->res, *gateway, pathString + "/Gateway");
1519 errorInEntry = true;
1520 }
Ratan Guptaf476acb2019-03-02 16:46:57 +05301521 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001522 else if (NICIPentry != ipv4Data.cend())
1523 {
1524 gw = &NICIPentry->gateway;
1525 }
1526 else
Ed Tanous4a0cb852018-10-15 07:55:04 -07001527 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001528 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001529 pathString + "/Gateway");
Johnathan Mantey01784822019-06-18 12:44:21 -07001530 errorInEntry = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001531 }
1532
Johnathan Mantey01784822019-06-18 12:44:21 -07001533 if (errorInEntry)
Ed Tanous4a0cb852018-10-15 07:55:04 -07001534 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001535 return;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001536 }
1537
Johnathan Mantey01784822019-06-18 12:44:21 -07001538 if (NICIPentry != ipv4Data.cend())
Ed Tanous4a0cb852018-10-15 07:55:04 -07001539 {
Ed Tanous271584a2019-07-09 16:24:22 -07001540 if (gw != nullptr || addr != nullptr)
1541 {
1542 // Shouldn't be possible based on errorInEntry, but
1543 // it flags -wmaybe-uninitialized in the compiler,
1544 // so defend against that
1545 return;
1546 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001547 deleteAndCreateIPv4(ifaceId, NICIPentry->id, prefixLength,
1548 *gw, *addr, asyncResp);
1549 NICIPentry =
1550 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
Ed Tanous4a0cb852018-10-15 07:55:04 -07001551 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001552 else
1553 {
1554 createIPv4(ifaceId, entryIdx, prefixLength, *gateway,
1555 *address, asyncResp);
1556 }
1557 entryIdx++;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001558 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001559 else
1560 {
1561 if (NICIPentry == ipv4Data.cend())
1562 {
1563 // Requesting a DELETE/DO NOT MODIFY action for an item
1564 // that isn't present on the eth(n) interface. Input JSON is
1565 // in error, so bail out.
1566 if (thisJson.is_null())
1567 {
1568 messages::resourceCannotBeDeleted(asyncResp->res);
1569 return;
1570 }
1571 else
1572 {
1573 messages::propertyValueFormatError(
1574 asyncResp->res, thisJson.dump(), pathString);
1575 return;
1576 }
1577 }
1578
1579 if (thisJson.is_null())
1580 {
1581 deleteIPv4(ifaceId, NICIPentry->id, asyncResp);
1582 }
1583 if (NICIPentry != ipv4Data.cend())
1584 {
1585 NICIPentry =
1586 GetNextStaticIPEntry(++NICIPentry, ipv4Data.cend());
1587 }
1588 entryIdx++;
1589 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001590 }
1591 }
1592
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001593 void handleStaticNameServersPatch(
1594 const std::string &ifaceId,
1595 const std::vector<std::string> &updatedStaticNameServers,
1596 const std::shared_ptr<AsyncResp> &asyncResp)
1597 {
1598 crow::connections::systemBus->async_method_call(
Johnathan Mantey286b9112019-06-10 13:38:04 -07001599 [asyncResp](const boost::system::error_code ec) {
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001600 if (ec)
1601 {
1602 messages::internalError(asyncResp->res);
1603 return;
1604 }
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001605 },
1606 "xyz.openbmc_project.Network",
1607 "/xyz/openbmc_project/network/" + ifaceId,
1608 "org.freedesktop.DBus.Properties", "Set",
1609 "xyz.openbmc_project.Network.EthernetInterface", "Nameservers",
1610 std::variant<std::vector<std::string>>{updatedStaticNameServers});
1611 }
1612
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001613 void handleIPv6StaticAddressesPatch(
1614 const std::string &ifaceId, nlohmann::json &input,
Johnathan Mantey01784822019-06-18 12:44:21 -07001615 const boost::container::flat_set<IPv6AddressData> &ipv6Data,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001616 const std::shared_ptr<AsyncResp> asyncResp)
1617 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001618 if (!input.is_array() || input.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001619 {
1620 messages::propertyValueTypeError(asyncResp->res, input.dump(),
1621 "IPv6StaticAddresses");
1622 return;
1623 }
Ed Tanous271584a2019-07-09 16:24:22 -07001624 size_t entryIdx = 1;
Johnathan Mantey01784822019-06-18 12:44:21 -07001625 boost::container::flat_set<IPv6AddressData>::const_iterator NICIPentry =
1626 GetNextStaticIPEntry(ipv6Data.cbegin(), ipv6Data.cend());
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001627 for (nlohmann::json &thisJson : input)
1628 {
1629 std::string pathString =
1630 "IPv6StaticAddresses/" + std::to_string(entryIdx);
1631
Johnathan Mantey01784822019-06-18 12:44:21 -07001632 if (!thisJson.is_null() && !thisJson.empty())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001633 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001634 std::optional<std::string> address;
1635 std::optional<uint8_t> prefixLength;
1636
1637 if (!json_util::readJson(thisJson, asyncResp->res, "Address",
1638 address, "PrefixLength", prefixLength))
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001639 {
1640 messages::propertyValueFormatError(
Johnathan Mantey01784822019-06-18 12:44:21 -07001641 asyncResp->res, thisJson.dump(), pathString);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001642 return;
1643 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001644
Johnathan Mantey01784822019-06-18 12:44:21 -07001645 const std::string *addr;
1646 uint8_t prefix;
1647
1648 // Find the address and prefixLength values. Any values that are
1649 // not explicitly provided are assumed to be unmodified from the
1650 // current state of the interface. Merge existing state into the
1651 // current request.
1652 if (address)
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001653 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001654 addr = &(*address);
1655 }
1656 else if (NICIPentry != ipv6Data.end())
1657 {
1658 addr = &(NICIPentry->address);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001659 }
1660 else
1661 {
1662 messages::propertyMissing(asyncResp->res,
1663 pathString + "/Address");
1664 return;
1665 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001666
1667 if (prefixLength)
1668 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001669 prefix = *prefixLength;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001670 }
Johnathan Mantey01784822019-06-18 12:44:21 -07001671 else if (NICIPentry != ipv6Data.end())
1672 {
1673 prefix = NICIPentry->prefixLength;
1674 }
1675 else
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001676 {
1677 messages::propertyMissing(asyncResp->res,
1678 pathString + "/PrefixLength");
Johnathan Mantey01784822019-06-18 12:44:21 -07001679 return;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001680 }
1681
Johnathan Mantey01784822019-06-18 12:44:21 -07001682 if (NICIPentry != ipv6Data.end())
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001683 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001684 deleteAndCreateIPv6(ifaceId, NICIPentry->id, prefix, *addr,
1685 asyncResp);
1686 NICIPentry =
1687 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1688 }
1689 else
1690 {
1691 createIPv6(ifaceId, *prefixLength, *addr, asyncResp);
1692 }
1693 entryIdx++;
1694 }
1695 else
1696 {
1697 if (NICIPentry == ipv6Data.end())
1698 {
1699 // Requesting a DELETE/DO NOT MODIFY action for an item
1700 // that isn't present on the eth(n) interface. Input JSON is
1701 // in error, so bail out.
1702 if (thisJson.is_null())
1703 {
1704 messages::resourceCannotBeDeleted(asyncResp->res);
1705 return;
1706 }
1707 else
1708 {
1709 messages::propertyValueFormatError(
1710 asyncResp->res, thisJson.dump(), pathString);
1711 return;
1712 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001713 }
1714
Johnathan Mantey01784822019-06-18 12:44:21 -07001715 if (thisJson.is_null())
1716 {
1717 deleteIPv6(ifaceId, NICIPentry->id, asyncResp);
1718 }
1719 if (NICIPentry != ipv6Data.cend())
1720 {
1721 NICIPentry =
1722 GetNextStaticIPEntry(++NICIPentry, ipv6Data.cend());
1723 }
1724 entryIdx++;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001725 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001726 }
1727 }
1728
Ed Tanous0f74e642018-11-12 15:17:05 -08001729 void parseInterfaceData(
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001730 std::shared_ptr<AsyncResp> asyncResp, const std::string &iface_id,
Ed Tanous0f74e642018-11-12 15:17:05 -08001731 const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001732 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001733 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001734 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001735 constexpr const std::array<const char *, 1> inventoryForEthernet = {
1736 "xyz.openbmc_project.Inventory.Item.Ethernet"};
1737
1738 nlohmann::json &json_response = asyncResp->res.jsonValue;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001739 json_response["Id"] = iface_id;
1740 json_response["@odata.id"] =
1741 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001742 json_response["InterfaceEnabled"] = ethData.nicEnabled;
1743
1744 auto health = std::make_shared<HealthPopulate>(asyncResp);
1745
1746 crow::connections::systemBus->async_method_call(
1747 [health](const boost::system::error_code ec,
1748 std::vector<std::string> &resp) {
1749 if (ec)
1750 {
1751 return;
1752 }
1753
1754 health->inventory = std::move(resp);
1755 },
1756 "xyz.openbmc_project.ObjectMapper",
1757 "/xyz/openbmc_project/object_mapper",
1758 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", "/",
1759 int32_t(0), inventoryForEthernet);
1760
1761 health->populate();
1762
1763 if (ethData.nicEnabled)
Ed Tanous029573d2019-02-01 10:57:49 -08001764 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001765 json_response["LinkStatus"] = "LinkUp";
1766 json_response["Status"]["State"] = "Enabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001767 }
1768 else
1769 {
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001770 json_response["LinkStatus"] = "NoLink";
1771 json_response["Status"]["State"] = "Disabled";
Ed Tanous029573d2019-02-01 10:57:49 -08001772 }
Johnathan Manteyaa05fb22020-01-08 12:08:44 -08001773
1774 json_response["LinkStatus"] = ethData.linkUp ? "LinkUp" : "LinkDown";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001775 json_response["SpeedMbps"] = ethData.speed;
1776 json_response["MACAddress"] = ethData.mac_address;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001777 json_response["DHCPv4"]["DHCPEnabled"] =
1778 translateDHCPEnabledToBool(ethData.DHCPEnabled, true);
1779 json_response["DHCPv4"]["UseNTPServers"] = ethData.NTPEnabled;
1780 json_response["DHCPv4"]["UseDNSServers"] = ethData.DNSEnabled;
1781 json_response["DHCPv4"]["UseDomainName"] = ethData.HostNameEnabled;
1782
1783 json_response["DHCPv6"]["OperatingMode"] =
1784 translateDHCPEnabledToBool(ethData.DHCPEnabled, false) ? "Stateful"
1785 : "Disabled";
1786 json_response["DHCPv6"]["UseNTPServers"] = ethData.NTPEnabled;
1787 json_response["DHCPv6"]["UseDNSServers"] = ethData.DNSEnabled;
1788 json_response["DHCPv6"]["UseDomainName"] = ethData.HostNameEnabled;
manojkiraneda2a133282019-02-19 13:09:43 +05301789
Ed Tanous4a0cb852018-10-15 07:55:04 -07001790 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001791 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001792 json_response["HostName"] = ethData.hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301793
1794 // When domain name is empty then it means, that it is a network
1795 // without domain names, and the host name itself must be treated as
1796 // FQDN
1797 std::string FQDN = std::move(ethData.hostname);
Jennifer Leed24bfc72019-03-05 13:03:37 -08001798 if (!ethData.domainnames.empty())
1799 {
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301800 FQDN += "." + ethData.domainnames[0];
Jennifer Leed24bfc72019-03-05 13:03:37 -08001801 }
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301802 json_response["FQDN"] = FQDN;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001803 }
1804
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001805 json_response["VLANs"] = {
1806 {"@odata.id", "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1807 iface_id + "/VLANs"}};
1808
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001809 if (translateDHCPEnabledToBool(ethData.DHCPEnabled, true) &&
1810 ethData.DNSEnabled)
Manojkiran Eda95f86462019-08-07 15:07:54 +05301811 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001812 json_response["StaticNameServers"] = nlohmann::json::array();
Manojkiran Eda95f86462019-08-07 15:07:54 +05301813 }
1814 else
1815 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001816 json_response["StaticNameServers"] = ethData.nameservers;
Manojkiran Eda95f86462019-08-07 15:07:54 +05301817 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001818
Ravi Tejad1d50812019-06-23 16:20:27 -05001819 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001820 nlohmann::json &ipv4_static_array =
1821 json_response["IPv4StaticAddresses"];
Ravi Tejad1d50812019-06-23 16:20:27 -05001822 ipv4_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001823 ipv4_static_array = nlohmann::json::array();
Ravi Tejad1d50812019-06-23 16:20:27 -05001824 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001825 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001826
1827 std::string gatewayStr = ipv4_config.gateway;
1828 if (gatewayStr.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001829 {
Ravi Tejad1d50812019-06-23 16:20:27 -05001830 gatewayStr = "0.0.0.0";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001831 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001832
1833 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1834 {"SubnetMask", ipv4_config.netmask},
1835 {"Address", ipv4_config.address},
1836 {"Gateway", gatewayStr}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001837 if (ipv4_config.origin == "Static")
Ravi Tejad1d50812019-06-23 16:20:27 -05001838 {
Johnathan Mantey01784822019-06-18 12:44:21 -07001839 ipv4_static_array.push_back(
1840 {{"AddressOrigin", ipv4_config.origin},
1841 {"SubnetMask", ipv4_config.netmask},
1842 {"Address", ipv4_config.address},
1843 {"Gateway", gatewayStr}});
Ravi Tejad1d50812019-06-23 16:20:27 -05001844 }
Ravi Tejad1d50812019-06-23 16:20:27 -05001845 }
1846
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001847 json_response["IPv6DefaultGateway"] = ethData.ipv6_default_gateway;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001848
1849 nlohmann::json &ipv6_array = json_response["IPv6Addresses"];
Johnathan Mantey01784822019-06-18 12:44:21 -07001850 nlohmann::json &ipv6_static_array =
1851 json_response["IPv6StaticAddresses"];
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001852 ipv6_array = nlohmann::json::array();
Johnathan Mantey01784822019-06-18 12:44:21 -07001853 ipv6_static_array = nlohmann::json::array();
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001854 for (auto &ipv6_config : ipv6Data)
1855 {
1856 ipv6_array.push_back({{"Address", ipv6_config.address},
1857 {"PrefixLength", ipv6_config.prefixLength},
1858 {"AddressOrigin", ipv6_config.origin}});
Johnathan Mantey01784822019-06-18 12:44:21 -07001859 if (ipv6_config.origin == "Static")
1860 {
1861 ipv6_static_array.push_back(
1862 {{"Address", ipv6_config.address},
1863 {"PrefixLength", ipv6_config.prefixLength},
1864 {"AddressOrigin", ipv6_config.origin}});
1865 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001866 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001867 }
1868
1869 /**
1870 * Functions triggers appropriate requests on DBus
1871 */
1872 void doGet(crow::Response &res, const crow::Request &req,
1873 const std::vector<std::string> &params) override
1874 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001875 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001876 if (params.size() != 1)
1877 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001878 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001879 return;
1880 }
1881
Ed Tanous4a0cb852018-10-15 07:55:04 -07001882 getEthernetIfaceData(
1883 params[0],
1884 [this, asyncResp, iface_id{std::string(params[0])}](
1885 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001886 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001887 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001888 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001889 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001890 // TODO(Pawel)consider distinguish between non existing
1891 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001892 messages::resourceNotFound(asyncResp->res,
1893 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001894 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001895 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07001896
Ed Tanous0f74e642018-11-12 15:17:05 -08001897 asyncResp->res.jsonValue["@odata.type"] =
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001898 "#EthernetInterface.v1_4_1.EthernetInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08001899 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1900 asyncResp->res.jsonValue["Description"] =
1901 "Management Network Interface";
1902
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001903 parseInterfaceData(asyncResp, iface_id, ethData, ipv4Data,
1904 ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001905 });
1906 }
1907
1908 void doPatch(crow::Response &res, const crow::Request &req,
1909 const std::vector<std::string> &params) override
1910 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001911 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001912 if (params.size() != 1)
1913 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001914 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001915 return;
1916 }
1917
Ed Tanous4a0cb852018-10-15 07:55:04 -07001918 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001919
Ed Tanousbc0bd6e2018-12-10 14:07:55 -08001920 std::optional<std::string> hostname;
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301921 std::optional<std::string> fqdn;
Ratan Guptad5776652019-03-03 08:47:22 +05301922 std::optional<std::string> macAddress;
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001923 std::optional<std::string> ipv6DefaultGateway;
Ravi Tejad1d50812019-06-23 16:20:27 -05001924 std::optional<nlohmann::json> ipv4StaticAddresses;
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001925 std::optional<nlohmann::json> ipv6StaticAddresses;
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05001926 std::optional<std::vector<std::string>> staticNameServers;
Jennifer Leeda131a92019-04-24 15:13:55 -07001927 std::optional<nlohmann::json> dhcpv4;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001928 std::optional<nlohmann::json> dhcpv6;
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001929 std::optional<bool> interfaceEnabled;
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001930 DHCPParameters v4dhcpParms;
1931 DHCPParameters v6dhcpParms;
Ed Tanous0627a2c2018-11-29 17:09:23 -08001932
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001933 if (!json_util::readJson(
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301934 req, res, "HostName", hostname, "FQDN", fqdn,
1935 "IPv4StaticAddresses", ipv4StaticAddresses, "MACAddress",
1936 macAddress, "StaticNameServers", staticNameServers,
1937 "IPv6DefaultGateway", ipv6DefaultGateway, "IPv6StaticAddresses",
1938 ipv6StaticAddresses, "DHCPv4", dhcpv4, "DHCPv6", dhcpv6,
1939 "InterfaceEnabled", interfaceEnabled))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001940 {
1941 return;
1942 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001943 if (dhcpv4)
1944 {
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001945 if (!json_util::readJson(*dhcpv4, res, "DHCPEnabled",
1946 v4dhcpParms.dhcpv4Enabled, "UseDNSServers",
1947 v4dhcpParms.useDNSServers, "UseNTPServers",
1948 v4dhcpParms.useNTPServers, "UseDomainName",
1949 v4dhcpParms.useUseDomainName))
1950 {
1951 return;
1952 }
1953 }
1954
1955 if (dhcpv6)
1956 {
1957 if (!json_util::readJson(*dhcpv6, res, "OperatingMode",
1958 v6dhcpParms.dhcpv6OperatingMode,
1959 "UseDNSServers", v6dhcpParms.useDNSServers,
1960 "UseNTPServers", v6dhcpParms.useNTPServers,
1961 "UseDomainName",
1962 v6dhcpParms.useUseDomainName))
1963 {
1964 return;
1965 }
Jennifer Leeda131a92019-04-24 15:13:55 -07001966 }
1967
Johnathan Mantey01784822019-06-18 12:44:21 -07001968 // Get single eth interface data, and call the below callback for
1969 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001970 getEthernetIfaceData(
1971 iface_id,
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001972 [this, asyncResp, iface_id, hostname = std::move(hostname),
Joshi-Mansiab6554f2020-03-10 18:33:36 +05301973 fqdn = std::move(fqdn), macAddress = std::move(macAddress),
Ravi Tejad1d50812019-06-23 16:20:27 -05001974 ipv4StaticAddresses = std::move(ipv4StaticAddresses),
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05001975 ipv6DefaultGateway = std::move(ipv6DefaultGateway),
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001976 ipv6StaticAddresses = std::move(ipv6StaticAddresses),
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001977 staticNameServers = std::move(staticNameServers),
1978 dhcpv4 = std::move(dhcpv4), dhcpv6 = std::move(dhcpv6),
1979 v4dhcpParms = std::move(v4dhcpParms),
Johnathan Manteyeeedda22019-10-29 16:09:52 -07001980 v6dhcpParms = std::move(v6dhcpParms),
1981 interfaceEnabled = std::move(interfaceEnabled)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001982 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05001983 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07001984 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001985 if (!success)
1986 {
1987 // ... otherwise return error
1988 // TODO(Pawel)consider distinguish between non existing
1989 // object, and other errors
Sunitha Harishfda13ad2019-03-21 11:01:24 -05001990 messages::resourceNotFound(asyncResp->res,
1991 "Ethernet Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001992 return;
1993 }
1994
Johnathan Mantey1f8c7b52019-06-18 12:44:21 -07001995 if (dhcpv4 || dhcpv6)
1996 {
1997 handleDHCPPatch(iface_id, ethData, std::move(v4dhcpParms),
1998 std::move(v6dhcpParms), asyncResp);
1999 }
2000
Ed Tanous0627a2c2018-11-29 17:09:23 -08002001 if (hostname)
2002 {
2003 handleHostnamePatch(*hostname, asyncResp);
2004 }
2005
Joshi-Mansiab6554f2020-03-10 18:33:36 +05302006 if (fqdn)
2007 {
2008 handleFqdnPatch(iface_id, *fqdn, asyncResp);
2009 }
2010
Ratan Guptad5776652019-03-03 08:47:22 +05302011 if (macAddress)
2012 {
2013 handleMACAddressPatch(iface_id, *macAddress, asyncResp);
2014 }
2015
Ravi Tejad1d50812019-06-23 16:20:27 -05002016 if (ipv4StaticAddresses)
2017 {
Ed Tanous537174c2018-12-10 15:09:31 -08002018 // TODO(ed) for some reason the capture of ipv4Addresses
Johnathan Mantey01784822019-06-18 12:44:21 -07002019 // above is returning a const value, not a non-const
2020 // value. This doesn't really work for us, as we need to
2021 // be able to efficiently move out the intermedia
2022 // nlohmann::json objects. This makes a copy of the
2023 // structure, and operates on that, but could be done
2024 // more efficiently
Ravi Tejad1d50812019-06-23 16:20:27 -05002025 nlohmann::json ipv4Static = std::move(*ipv4StaticAddresses);
Johnathan Mantey01784822019-06-18 12:44:21 -07002026 handleIPv4StaticPatch(iface_id, ipv4Static, ipv4Data,
Ravi Tejad1d50812019-06-23 16:20:27 -05002027 asyncResp);
Ed Tanous0627a2c2018-11-29 17:09:23 -08002028 }
2029
RAJESWARAN THILLAIGOVINDANf85837b2019-04-04 05:18:53 -05002030 if (staticNameServers)
2031 {
2032 handleStaticNameServersPatch(iface_id, *staticNameServers,
2033 asyncResp);
2034 }
Ravi Teja9a6fc6f2019-04-16 02:43:13 -05002035
2036 if (ipv6DefaultGateway)
2037 {
2038 messages::propertyNotWritable(asyncResp->res,
2039 "IPv6DefaultGateway");
2040 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002041
2042 if (ipv6StaticAddresses)
2043 {
2044 nlohmann::json ipv6Static = std::move(*ipv6StaticAddresses);
2045 handleIPv6StaticAddressesPatch(iface_id, ipv6Static,
Johnathan Mantey01784822019-06-18 12:44:21 -07002046 ipv6Data, asyncResp);
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002047 }
Johnathan Manteyeeedda22019-10-29 16:09:52 -07002048
2049 if (interfaceEnabled)
2050 {
2051 setEthernetInterfaceBoolProperty(
2052 iface_id, "NICEnabled", *interfaceEnabled, asyncResp);
2053 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002054 });
2055 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01002056};
2057
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002058/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07002059 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
2060 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002061 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002062class VlanNetworkInterface : public Node
2063{
2064 public:
2065 /*
2066 * Default Constructor
2067 */
2068 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002069 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002070 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08002071 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07002072 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002073 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002074 entityPrivileges = {
2075 {boost::beast::http::verb::get, {{"Login"}}},
2076 {boost::beast::http::verb::head, {{"Login"}}},
2077 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2078 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2079 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2080 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002081 }
2082
Ed Tanous1abe55e2018-09-05 08:30:59 -07002083 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08002084 void parseInterfaceData(
2085 nlohmann::json &json_response, const std::string &parent_iface_id,
2086 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002087 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002088 const boost::container::flat_set<IPv6AddressData> &ipv6Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002089 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002090 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07002091 json_response["Id"] = iface_id;
2092 json_response["@odata.id"] =
2093 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
2094 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002095
Ed Tanous4a0cb852018-10-15 07:55:04 -07002096 json_response["VLANEnable"] = true;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002097 if (!ethData.vlan_id.empty())
Ed Tanous4a0cb852018-10-15 07:55:04 -07002098 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002099 json_response["VLANId"] = ethData.vlan_id.back();
Ed Tanous4a0cb852018-10-15 07:55:04 -07002100 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07002101 }
2102
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002103 bool verifyNames(const std::string &parent, const std::string &iface)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002104 {
2105 if (!boost::starts_with(iface, parent + "_"))
2106 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002107 return false;
2108 }
2109 else
2110 {
2111 return true;
2112 }
2113 }
2114
2115 /**
2116 * Functions triggers appropriate requests on DBus
2117 */
2118 void doGet(crow::Response &res, const crow::Request &req,
2119 const std::vector<std::string> &params) override
2120 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002121 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
2122 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07002123 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
2124 // Check if there is required param, truly entering this shall be
2125 // impossible.
2126 if (params.size() != 2)
2127 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002128 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002129 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002130 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002131 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002132
Ed Tanous4a0cb852018-10-15 07:55:04 -07002133 const std::string &parent_iface_id = params[0];
2134 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08002135 res.jsonValue["@odata.type"] =
2136 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
Ed Tanous0f74e642018-11-12 15:17:05 -08002137 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002138
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002139 if (!verifyNames(parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002140 {
2141 return;
2142 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02002143
Johnathan Mantey01784822019-06-18 12:44:21 -07002144 // Get single eth interface data, and call the below callback for
2145 // JSON preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07002146 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002147 params[1],
2148 [this, asyncResp, parent_iface_id{std::string(params[0])},
2149 iface_id{std::string(params[1])}](
Ed Tanous4a0cb852018-10-15 07:55:04 -07002150 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002151 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002152 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002153 if (success && ethData.vlan_id.size() != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002154 {
Ed Tanous0f74e642018-11-12 15:17:05 -08002155 parseInterfaceData(asyncResp->res.jsonValue,
2156 parent_iface_id, iface_id, ethData,
Johnathan Mantey01784822019-06-18 12:44:21 -07002157 ipv4Data, ipv6Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002158 }
2159 else
2160 {
2161 // ... otherwise return error
2162 // TODO(Pawel)consider distinguish between non existing
2163 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07002164 messages::resourceNotFound(
2165 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002166 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002167 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002168 }
2169
Ed Tanous1abe55e2018-09-05 08:30:59 -07002170 void doPatch(crow::Response &res, const crow::Request &req,
2171 const std::vector<std::string> &params) override
2172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002173 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002174 if (params.size() != 2)
2175 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002176 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002177 return;
2178 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002179
Ed Tanous1abe55e2018-09-05 08:30:59 -07002180 const std::string &parentIfaceId = params[0];
2181 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002182
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002183 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002184 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002185 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2186 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002187 return;
2188 }
2189
Ed Tanous0627a2c2018-11-29 17:09:23 -08002190 bool vlanEnable = false;
2191 uint64_t vlanId = 0;
2192
2193 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
2194 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002195 {
2196 return;
2197 }
2198
Johnathan Mantey01784822019-06-18 12:44:21 -07002199 // Get single eth interface data, and call the below callback for
2200 // JSON preparation
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002201 getEthernetIfaceData(
2202 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002203 [asyncResp, parentIfaceId{std::string(params[0])},
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002204 ifaceId{std::string(params[1])}, &vlanEnable, &vlanId](
2205 const bool &success, const EthernetInterfaceData &ethData,
2206 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002207 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002208 if (success && !ethData.vlan_id.empty())
Sunitha Harish08244d02019-04-01 03:57:25 -05002209 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002210 auto callback =
2211 [asyncResp](const boost::system::error_code ec) {
2212 if (ec)
2213 {
2214 messages::internalError(asyncResp->res);
2215 }
2216 };
2217
2218 if (vlanEnable == true)
2219 {
2220 crow::connections::systemBus->async_method_call(
2221 std::move(callback), "xyz.openbmc_project.Network",
2222 "/xyz/openbmc_project/network/" + ifaceId,
2223 "org.freedesktop.DBus.Properties", "Set",
2224 "xyz.openbmc_project.Network.VLAN", "Id",
2225 std::variant<uint32_t>(vlanId));
2226 }
2227 else
2228 {
2229 BMCWEB_LOG_DEBUG << "vlanEnable is false. Deleting the "
2230 "vlan interface";
2231 crow::connections::systemBus->async_method_call(
2232 std::move(callback), "xyz.openbmc_project.Network",
2233 std::string("/xyz/openbmc_project/network/") +
2234 ifaceId,
2235 "xyz.openbmc_project.Object.Delete", "Delete");
2236 }
Sunitha Harish08244d02019-04-01 03:57:25 -05002237 }
2238 else
2239 {
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002240 // TODO(Pawel)consider distinguish between non existing
2241 // object, and other errors
2242 messages::resourceNotFound(
2243 asyncResp->res, "VLAN Network Interface", ifaceId);
2244 return;
Sunitha Harish08244d02019-04-01 03:57:25 -05002245 }
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002246 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002247 }
2248
2249 void doDelete(crow::Response &res, const crow::Request &req,
2250 const std::vector<std::string> &params) override
2251 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002252 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002253 if (params.size() != 2)
2254 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002255 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002256 return;
2257 }
2258
2259 const std::string &parentIfaceId = params[0];
2260 const std::string &ifaceId = params[1];
2261
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002262 if (!verifyNames(parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002263 {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002264 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
2265 ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002266 return;
2267 }
2268
Johnathan Mantey01784822019-06-18 12:44:21 -07002269 // Get single eth interface data, and call the below callback for
2270 // JSON preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002271 getEthernetIfaceData(
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002272 params[1],
Ed Tanous271584a2019-07-09 16:24:22 -07002273 [asyncResp, parentIfaceId{std::string(params[0])},
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002274 ifaceId{std::string(params[1])}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002275 const bool &success, const EthernetInterfaceData &ethData,
Ravi Tejae48c0fc2019-04-16 08:37:20 -05002276 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
Johnathan Mantey01784822019-06-18 12:44:21 -07002277 const boost::container::flat_set<IPv6AddressData> &ipv6Data) {
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002278 if (success && !ethData.vlan_id.empty())
Jason M. Billsf12894f2018-10-09 12:45:45 -07002279 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002280 auto callback =
2281 [asyncResp](const boost::system::error_code ec) {
2282 if (ec)
2283 {
2284 messages::internalError(asyncResp->res);
2285 }
2286 };
2287 crow::connections::systemBus->async_method_call(
2288 std::move(callback), "xyz.openbmc_project.Network",
2289 std::string("/xyz/openbmc_project/network/") + ifaceId,
2290 "xyz.openbmc_project.Object.Delete", "Delete");
2291 }
2292 else
2293 {
2294 // ... otherwise return error
2295 // TODO(Pawel)consider distinguish between non existing
2296 // object, and other errors
2297 messages::resourceNotFound(
2298 asyncResp->res, "VLAN Network Interface", ifaceId);
2299 }
2300 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002301 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002302};
2303
2304/**
2305 * VlanNetworkInterfaceCollection derived class for delivering
2306 * VLANNetworkInterface Collection Schema
2307 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07002308class VlanNetworkInterfaceCollection : public Node
2309{
2310 public:
2311 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07002312 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07002313 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
2314 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002315 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07002316 entityPrivileges = {
2317 {boost::beast::http::verb::get, {{"Login"}}},
2318 {boost::beast::http::verb::head, {{"Login"}}},
2319 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
2320 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
2321 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
2322 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002323 }
2324
Ed Tanous1abe55e2018-09-05 08:30:59 -07002325 private:
2326 /**
2327 * Functions triggers appropriate requests on DBus
2328 */
2329 void doGet(crow::Response &res, const crow::Request &req,
2330 const std::vector<std::string> &params) override
2331 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002332 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002333 if (params.size() != 1)
2334 {
2335 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07002336 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002337 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07002338 }
2339
Ed Tanous4a0cb852018-10-15 07:55:04 -07002340 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002341
Ed Tanous4a0cb852018-10-15 07:55:04 -07002342 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07002343 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07002344 getEthernetIfaceList(
Ed Tanous43b761d2019-02-13 20:10:56 -08002345 [asyncResp, rootInterfaceName{std::string(rootInterfaceName)}](
Jason M. Billsf12894f2018-10-09 12:45:45 -07002346 const bool &success,
Ed Tanous4c9afe42019-05-03 16:59:57 -07002347 const boost::container::flat_set<std::string> &iface_list) {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002348 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002349 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002350 messages::internalError(asyncResp->res);
2351 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002352 }
Ed Tanous4c9afe42019-05-03 16:59:57 -07002353
2354 if (iface_list.find(rootInterfaceName) == iface_list.end())
2355 {
2356 messages::resourceNotFound(asyncResp->res,
2357 "VLanNetworkInterfaceCollection",
2358 rootInterfaceName);
2359 return;
2360 }
2361
Ed Tanous0f74e642018-11-12 15:17:05 -08002362 asyncResp->res.jsonValue["@odata.type"] =
2363 "#VLanNetworkInterfaceCollection."
2364 "VLanNetworkInterfaceCollection";
Ed Tanous0f74e642018-11-12 15:17:05 -08002365 asyncResp->res.jsonValue["Name"] =
2366 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07002367
Jason M. Billsf12894f2018-10-09 12:45:45 -07002368 nlohmann::json iface_array = nlohmann::json::array();
2369
2370 for (const std::string &iface_item : iface_list)
2371 {
2372 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
2373 {
2374 iface_array.push_back(
2375 {{"@odata.id",
2376 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2377 rootInterfaceName + "/VLANs/" + iface_item}});
2378 }
2379 }
2380
Jason M. Billsf12894f2018-10-09 12:45:45 -07002381 asyncResp->res.jsonValue["Members@odata.count"] =
2382 iface_array.size();
2383 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
2384 asyncResp->res.jsonValue["@odata.id"] =
2385 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
2386 rootInterfaceName + "/VLANs";
2387 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07002388 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002389
Ed Tanous1abe55e2018-09-05 08:30:59 -07002390 void doPost(crow::Response &res, const crow::Request &req,
2391 const std::vector<std::string> &params) override
2392 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002393 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002394 if (params.size() != 1)
2395 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07002396 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002397 return;
2398 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002399 bool vlanEnable = false;
Ed Tanous0627a2c2018-11-29 17:09:23 -08002400 uint32_t vlanId = 0;
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002401 if (!json_util::readJson(req, res, "VLANId", vlanId, "VLANEnable",
2402 vlanEnable))
Ed Tanous1abe55e2018-09-05 08:30:59 -07002403 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07002404 return;
2405 }
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002406 // Need both vlanId and vlanEnable to service this request
2407 if (!vlanId)
2408 {
2409 messages::propertyMissing(asyncResp->res, "VLANId");
2410 }
2411 if (!vlanEnable)
2412 {
2413 messages::propertyMissing(asyncResp->res, "VLANEnable");
2414 }
Ed Tanous271584a2019-07-09 16:24:22 -07002415 if (static_cast<bool>(vlanId) ^ vlanEnable)
Sunitha Harishfda13ad2019-03-21 11:01:24 -05002416 {
2417 return;
2418 }
2419
Ed Tanous4a0cb852018-10-15 07:55:04 -07002420 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07002421 auto callback = [asyncResp](const boost::system::error_code ec) {
2422 if (ec)
2423 {
2424 // TODO(ed) make more consistent error messages based on
2425 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07002426 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002427 return;
2428 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07002429 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07002430 };
2431 crow::connections::systemBus->async_method_call(
2432 std::move(callback), "xyz.openbmc_project.Network",
2433 "/xyz/openbmc_project/network",
2434 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08002435 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07002436 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02002437};
Ed Tanous1abe55e2018-09-05 08:30:59 -07002438} // namespace redfish