blob: 87127106247a7c8b3f97df53cad0861131429669 [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>
20#include <boost/optional.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020021#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020022#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020023#include <node.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <utils/json_utils.hpp>
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace redfish
27{
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010028
29/**
30 * DBus types primitives for several generic DBus interfaces
31 * TODO(Pawel) consider move this to separate file into boost::dbus
32 */
Ed Tanousaa2e59c2018-04-12 12:17:20 -070033using PropertiesMapType = boost::container::flat_map<
34 std::string,
35 sdbusplus::message::variant<std::string, bool, uint8_t, int16_t, uint16_t,
36 int32_t, uint32_t, int64_t, uint64_t, double>>;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010037
Ed Tanous4a0cb852018-10-15 07:55:04 -070038using GetManagedObjects = std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070039 sdbusplus::message::object_path,
Ed Tanous4a0cb852018-10-15 07:55:04 -070040 std::vector<std::pair<
Ed Tanousaa2e59c2018-04-12 12:17:20 -070041 std::string,
42 boost::container::flat_map<
43 std::string, sdbusplus::message::variant<
44 std::string, bool, uint8_t, int16_t, uint16_t,
Ed Tanous4a0cb852018-10-15 07:55:04 -070045 int32_t, uint32_t, int64_t, uint64_t, double>>>>>>;
46
47enum class LinkType
48{
49 Local,
50 Global
51};
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010052
53/**
54 * Structure for keeping IPv4 data required by Redfish
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010055 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070056struct IPv4AddressData
57{
58 std::string id;
Ed Tanous4a0cb852018-10-15 07:55:04 -070059 std::string address;
60 std::string domain;
61 std::string gateway;
Ed Tanous1abe55e2018-09-05 08:30:59 -070062 std::string netmask;
63 std::string origin;
Ed Tanous4a0cb852018-10-15 07:55:04 -070064 LinkType linktype;
65
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 bool operator<(const IPv4AddressData &obj) const
67 {
Ed Tanous4a0cb852018-10-15 07:55:04 -070068 return id < obj.id;
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010070};
71
72/**
73 * Structure for keeping basic single Ethernet Interface information
74 * available from DBus
75 */
Ed Tanous1abe55e2018-09-05 08:30:59 -070076struct EthernetInterfaceData
77{
Ed Tanous4a0cb852018-10-15 07:55:04 -070078 uint32_t speed;
79 bool auto_neg;
80 std::string hostname;
81 std::string default_gateway;
82 std::string mac_address;
83 boost::optional<uint32_t> vlan_id;
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010084};
85
Ed Tanous4a0cb852018-10-15 07:55:04 -070086// Helper function that changes bits netmask notation (i.e. /24)
87// into full dot notation
88inline std::string getNetmask(unsigned int bits)
Ed Tanous1abe55e2018-09-05 08:30:59 -070089{
Ed Tanous4a0cb852018-10-15 07:55:04 -070090 uint32_t value = 0xffffffff << (32 - bits);
91 std::string netmask = std::to_string((value >> 24) & 0xff) + "." +
92 std::to_string((value >> 16) & 0xff) + "." +
93 std::to_string((value >> 8) & 0xff) + "." +
94 std::to_string(value & 0xff);
95 return netmask;
96}
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +010097
Ed Tanous4a0cb852018-10-15 07:55:04 -070098inline std::string
99 translateAddressOriginDbusToRedfish(const std::string &inputOrigin,
100 bool isIPv4)
101{
102 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.Static")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700104 return "Static";
105 }
106 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal")
107 {
108 if (isIPv4)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700110 return "IPv4LinkLocal";
111 }
112 else
113 {
114 return "LinkLocal";
115 }
116 }
117 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP")
118 {
119 if (isIPv4)
120 {
121 return "DHCP";
122 }
123 else
124 {
125 return "DHCPv6";
126 }
127 }
128 if (inputOrigin == "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC")
129 {
130 return "SLAAC";
131 }
132 return "";
133}
134
135inline std::string
136 translateAddressOriginRedfishToDbus(const std::string &inputOrigin)
137{
138 if (inputOrigin == "Static")
139 {
140 return "xyz.openbmc_project.Network.IP.AddressOrigin.Static";
141 }
142 if (inputOrigin == "DHCP" || inputOrigin == "DHCPv6")
143 {
144 return "xyz.openbmc_project.Network.IP.AddressOrigin.DHCP";
145 }
146 if (inputOrigin == "IPv4LinkLocal" || inputOrigin == "LinkLocal")
147 {
148 return "xyz.openbmc_project.Network.IP.AddressOrigin.LinkLocal";
149 }
150 if (inputOrigin == "SLAAC")
151 {
152 return "xyz.openbmc_project.Network.IP.AddressOrigin.SLAAC";
153 }
154 return "";
155}
156
157inline void extractEthernetInterfaceData(const std::string &ethiface_id,
158 const GetManagedObjects &dbus_data,
159 EthernetInterfaceData &ethData)
160{
161 for (const auto &objpath : dbus_data)
162 {
163 if (objpath.first == "/xyz/openbmc_project/network/" + ethiface_id)
164 {
165 for (const auto &ifacePair : objpath.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700167 if (ifacePair.first == "xyz.openbmc_project.Network.MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700168 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700169 for (const auto &propertyPair : ifacePair.second)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700170 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700171 if (propertyPair.first == "MACAddress")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700173 const std::string *mac =
174 mapbox::getPtr<const std::string>(
175 propertyPair.second);
176 if (mac != nullptr)
177 {
178 ethData.mac_address = *mac;
179 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700181 }
182 }
183 else if (ifacePair.first == "xyz.openbmc_project.Network.VLAN")
184 {
185 for (const auto &propertyPair : ifacePair.second)
186 {
187 if (propertyPair.first == "Id")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700189 const uint32_t *id = mapbox::getPtr<const uint32_t>(
190 propertyPair.second);
191 if (id != nullptr)
192 {
193 ethData.vlan_id = *id;
194 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700195 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700196 }
197 }
198 else if (ifacePair.first ==
199 "xyz.openbmc_project.Network.EthernetInterface")
200 {
201 for (const auto &propertyPair : ifacePair.second)
202 {
203 if (propertyPair.first == "AutoNeg")
204 {
205 const bool *auto_neg =
206 mapbox::getPtr<const bool>(propertyPair.second);
207 if (auto_neg != nullptr)
208 {
209 ethData.auto_neg = *auto_neg;
210 }
211 }
212 else if (propertyPair.first == "Speed")
213 {
214 const uint32_t *speed =
215 mapbox::getPtr<const uint32_t>(
216 propertyPair.second);
217 if (speed != nullptr)
218 {
219 ethData.speed = *speed;
220 }
221 }
222 }
223 }
224 else if (ifacePair.first ==
225 "xyz.openbmc_project.Network.SystemConfiguration")
226 {
227 for (const auto &propertyPair : ifacePair.second)
228 {
229 if (propertyPair.first == "HostName")
230 {
231 const std::string *hostname =
232 mapbox::getPtr<const std::string>(
233 propertyPair.second);
234 if (hostname != nullptr)
235 {
236 ethData.hostname = *hostname;
237 }
238 }
239 else if (propertyPair.first == "DefaultGateway")
240 {
241 const std::string *defaultGateway =
242 mapbox::getPtr<const std::string>(
243 propertyPair.second);
244 if (defaultGateway != nullptr)
245 {
246 ethData.default_gateway = *defaultGateway;
247 }
248 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 }
250 }
251 }
252 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700254}
255
256// Helper function that extracts data for single ethernet ipv4 address
257inline void
258 extractIPData(const std::string &ethiface_id,
259 const GetManagedObjects &dbus_data,
260 boost::container::flat_set<IPv4AddressData> &ipv4_config)
261{
262 const std::string ipv4PathStart =
263 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
264
265 // Since there might be several IPv4 configurations aligned with
266 // single ethernet interface, loop over all of them
267 for (const auto &objpath : dbus_data)
268 {
269 // Check if proper pattern for object path appears
270 if (boost::starts_with(objpath.first.str, ipv4PathStart))
271 {
272 for (auto &interface : objpath.second)
273 {
274 if (interface.first == "xyz.openbmc_project.Network.IP")
275 {
276 // Instance IPv4AddressData structure, and set as
277 // appropriate
278 std::pair<
279 boost::container::flat_set<IPv4AddressData>::iterator,
280 bool>
281 it = ipv4_config.insert(
282 {objpath.first.str.substr(ipv4PathStart.size())});
283 IPv4AddressData &ipv4_address = *it.first;
284 for (auto &property : interface.second)
285 {
286 if (property.first == "Address")
287 {
288 const std::string *address =
289 mapbox::getPtr<const std::string>(
290 property.second);
291 if (address != nullptr)
292 {
293 ipv4_address.address = *address;
294 }
295 }
296 else if (property.first == "Gateway")
297 {
298 const std::string *gateway =
299 mapbox::getPtr<const std::string>(
300 property.second);
301 if (gateway != nullptr)
302 {
303 ipv4_address.gateway = *gateway;
304 }
305 }
306 else if (property.first == "Origin")
307 {
308 const std::string *origin =
309 mapbox::getPtr<const std::string>(
310 property.second);
311 if (origin != nullptr)
312 {
313 ipv4_address.origin =
314 translateAddressOriginDbusToRedfish(*origin,
315 true);
316 }
317 }
318 else if (property.first == "PrefixLength")
319 {
320 const uint8_t *mask =
321 mapbox::getPtr<uint8_t>(property.second);
322 if (mask != nullptr)
323 {
324 // convert it to the string
325 ipv4_address.netmask = getNetmask(*mask);
326 }
327 }
328 else
329 {
330 BMCWEB_LOG_ERROR
331 << "Got extra property: " << property.first
332 << " on the " << objpath.first.str << " object";
333 }
334 }
335 // Check if given address is local, or global
336 ipv4_address.linktype =
337 boost::starts_with(ipv4_address.address, "169.254.")
338 ? LinkType::Global
339 : LinkType::Local;
340 }
341 }
342 }
343 }
344}
345
346/**
347 * @brief Sets given Id on the given VLAN interface through D-Bus
348 *
349 * @param[in] ifaceId Id of VLAN interface that should be modified
350 * @param[in] inputVlanId New ID of the VLAN
351 * @param[in] callback Function that will be called after the operation
352 *
353 * @return None.
354 */
355template <typename CallbackFunc>
356void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
357 CallbackFunc &&callback)
358{
359 crow::connections::systemBus->async_method_call(
360 callback, "xyz.openbmc_project.Network",
361 std::string("/xyz/openbmc_project/network/") + ifaceId,
362 "org.freedesktop.DBus.Properties", "Set",
363 "xyz.openbmc_project.Network.VLAN", "Id",
364 sdbusplus::message::variant<uint32_t>(inputVlanId));
365}
366
367/**
368 * @brief Helper function that verifies IP address to check if it is in
369 * proper format. If bits pointer is provided, also calculates active
370 * bit count for Subnet Mask.
371 *
372 * @param[in] ip IP that will be verified
373 * @param[out] bits Calculated mask in bits notation
374 *
375 * @return true in case of success, false otherwise
376 */
377inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
378 uint8_t *bits = nullptr)
379{
380 std::vector<std::string> bytesInMask;
381
382 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700383
384 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700385 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700387 return false;
388 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389
Ed Tanous4a0cb852018-10-15 07:55:04 -0700390 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700391 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700392 *bits = 0;
393 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394
Ed Tanous4a0cb852018-10-15 07:55:04 -0700395 char *endPtr;
396 long previousValue = 255;
397 bool firstZeroInByteHit;
398 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700400 if (byte.empty())
401 {
402 return false;
403 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700404
Ed Tanous4a0cb852018-10-15 07:55:04 -0700405 // Use strtol instead of stroi to avoid exceptions
406 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407
Ed Tanous4a0cb852018-10-15 07:55:04 -0700408 // endPtr should point to the end of the string, otherwise given string
409 // is not 100% number
410 if (*endPtr != '\0')
411 {
412 return false;
413 }
414
415 // Value should be contained in byte
416 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 {
418 return false;
419 }
420
421 if (bits != nullptr)
422 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700423 // Mask has to be continuous between bytes
424 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700425 {
426 return false;
427 }
428
Ed Tanous4a0cb852018-10-15 07:55:04 -0700429 // Mask has to be continuous inside bytes
430 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700431
Ed Tanous4a0cb852018-10-15 07:55:04 -0700432 // Count bits
433 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700435 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700436 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700437 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700439 // Continuity not preserved
440 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700441 }
442 else
443 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700444 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700445 }
446 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700447 else
448 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700449 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700451 }
452 }
453
454 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700455 }
456
Ed Tanous4a0cb852018-10-15 07:55:04 -0700457 return true;
458}
459
460/**
461 * @brief Changes IPv4 address type property (Address, Gateway)
462 *
463 * @param[in] ifaceId Id of interface whose IP should be modified
464 * @param[in] ipIdx Index of IP in input array that should be modified
465 * @param[in] ipHash DBus Hash id of modified IP
466 * @param[in] name Name of field in JSON representation
467 * @param[in] newValue New value that should be written
468 * @param[io] asyncResp Response object that will be returned to client
469 *
470 * @return true if give IP is valid and has been sent do D-Bus, false
471 * otherwise
472 */
473inline void changeIPv4AddressProperty(
474 const std::string &ifaceId, int ipIdx, const std::string &ipHash,
475 const std::string &name, const std::string &newValue,
476 const std::shared_ptr<AsyncResp> asyncResp)
477{
478 auto callback = [asyncResp, ipIdx, name{std::string(name)},
479 newValue{std::move(newValue)}](
480 const boost::system::error_code ec) {
481 if (ec)
482 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800483 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700484 }
485 else
486 {
487 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
488 }
489 };
490
491 crow::connections::systemBus->async_method_call(
492 std::move(callback), "xyz.openbmc_project.Network",
493 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
494 "org.freedesktop.DBus.Properties", "Set",
495 "xyz.openbmc_project.Network.IP", name,
496 sdbusplus::message::variant<std::string>(newValue));
497}
498
499/**
500 * @brief Changes IPv4 address origin property
501 *
502 * @param[in] ifaceId Id of interface whose IP should be modified
503 * @param[in] ipIdx Index of IP in input array that should be
504 * modified
505 * @param[in] ipHash DBus Hash id of modified IP
506 * @param[in] newValue New value in Redfish format
507 * @param[in] newValueDbus New value in D-Bus format
508 * @param[io] asyncResp Response object that will be returned to client
509 *
510 * @return true if give IP is valid and has been sent do D-Bus, false
511 * otherwise
512 */
513inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
514 const std::string &ipHash,
515 const std::string &newValue,
516 const std::string &newValueDbus,
517 const std::shared_ptr<AsyncResp> asyncResp)
518{
519 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
520 const boost::system::error_code ec) {
521 if (ec)
522 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800523 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700524 }
525 else
526 {
527 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
528 newValue;
529 }
530 };
531
532 crow::connections::systemBus->async_method_call(
533 std::move(callback), "xyz.openbmc_project.Network",
534 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
535 "org.freedesktop.DBus.Properties", "Set",
536 "xyz.openbmc_project.Network.IP", "Origin",
537 sdbusplus::message::variant<std::string>(newValueDbus));
538}
539
540/**
541 * @brief Modifies SubnetMask for given IP
542 *
543 * @param[in] ifaceId Id of interface whose IP should be modified
544 * @param[in] ipIdx Index of IP in input array that should be
545 * modified
546 * @param[in] ipHash DBus Hash id of modified IP
547 * @param[in] newValueStr Mask in dot notation as string
548 * @param[in] newValue Mask as PrefixLength in bitcount
549 * @param[io] asyncResp Response object that will be returned to client
550 *
551 * @return None
552 */
553inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
554 const std::string &ipHash,
555 const std::string &newValueStr,
556 uint8_t &newValue,
557 std::shared_ptr<AsyncResp> asyncResp)
558{
559 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
560 const boost::system::error_code ec) {
561 if (ec)
562 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800563 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700564 }
565 else
566 {
567 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
568 newValueStr;
569 }
570 };
571
572 crow::connections::systemBus->async_method_call(
573 std::move(callback), "xyz.openbmc_project.Network",
574 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
575 "org.freedesktop.DBus.Properties", "Set",
576 "xyz.openbmc_project.Network.IP", "PrefixLength",
577 sdbusplus::message::variant<uint8_t>(newValue));
578}
579
580/**
581 * @brief Sets given HostName of the machine through D-Bus
582 *
583 * @param[in] newHostname New name that HostName will be changed to
584 * @param[in] callback Function that will be called after the operation
585 *
586 * @return None.
587 */
588template <typename CallbackFunc>
589void setHostName(const std::string &newHostname, CallbackFunc &&callback)
590{
591 crow::connections::systemBus->async_method_call(
592 callback, "xyz.openbmc_project.Network",
593 "/xyz/openbmc_project/network/config",
594 "org.freedesktop.DBus.Properties", "Set",
595 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
596 sdbusplus::message::variant<std::string>(newHostname));
597}
598
599/**
600 * @brief Deletes given IPv4
601 *
602 * @param[in] ifaceId Id of interface whose IP should be deleted
603 * @param[in] ipIdx Index of IP in input array that should be deleted
604 * @param[in] ipHash DBus Hash id of IP that should be deleted
605 * @param[io] asyncResp Response object that will be returned to client
606 *
607 * @return None
608 */
609inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
610 unsigned int ipIdx,
611 const std::shared_ptr<AsyncResp> asyncResp)
612{
613 crow::connections::systemBus->async_method_call(
614 [ipIdx, asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 if (ec)
616 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800617 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100618 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700619 else
620 {
621 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
622 }
623 },
624 "xyz.openbmc_project.Network",
625 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
626 "xyz.openbmc_project.Object.Delete", "Delete");
627}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628
Ed Tanous4a0cb852018-10-15 07:55:04 -0700629/**
630 * @brief Creates IPv4 with given data
631 *
632 * @param[in] ifaceId Id of interface whose IP should be deleted
633 * @param[in] ipIdx Index of IP in input array that should be deleted
634 * @param[in] ipHash DBus Hash id of IP that should be deleted
635 * @param[io] asyncResp Response object that will be returned to client
636 *
637 * @return None
638 */
639inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
640 uint8_t subnetMask, const std::string &gateway,
641 const std::string &address,
642 std::shared_ptr<AsyncResp> asyncResp)
643{
644 auto createIpHandler = [ipIdx,
645 asyncResp](const boost::system::error_code ec) {
646 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800648 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 };
651
Ed Tanous4a0cb852018-10-15 07:55:04 -0700652 crow::connections::systemBus->async_method_call(
653 std::move(createIpHandler), "xyz.openbmc_project.Network",
654 "/xyz/openbmc_project/network/" + ifaceId,
655 "xyz.openbmc_project.Network.IP.Create", "IP",
656 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
657 gateway);
658}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700659
Ed Tanous4a0cb852018-10-15 07:55:04 -0700660/**
661 * Function that retrieves all properties for given Ethernet Interface
662 * Object
663 * from EntityManager Network Manager
664 * @param ethiface_id a eth interface id to query on DBus
665 * @param callback a function that shall be called to convert Dbus output
666 * into JSON
667 */
668template <typename CallbackFunc>
669void getEthernetIfaceData(const std::string &ethiface_id,
670 CallbackFunc &&callback)
671{
672 crow::connections::systemBus->async_method_call(
673 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
674 const boost::system::error_code error_code,
675 const GetManagedObjects &resp) {
676 EthernetInterfaceData ethData{};
677 boost::container::flat_set<IPv4AddressData> ipv4Data;
678
679 if (error_code)
680 {
681 callback(false, ethData, ipv4Data);
682 return;
683 }
684
685 extractEthernetInterfaceData(ethiface_id, resp, ethData);
686 extractIPData(ethiface_id, resp, ipv4Data);
687
688 // Fix global GW
689 for (IPv4AddressData &ipv4 : ipv4Data)
690 {
691 if ((ipv4.linktype == LinkType::Global) &&
692 (ipv4.gateway == "0.0.0.0"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700694 ipv4.gateway = ethData.default_gateway;
695 }
696 }
697
698 // Finally make a callback with usefull data
699 callback(true, ethData, ipv4Data);
700 },
701 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
702 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
703};
704
705/**
706 * Function that retrieves all Ethernet Interfaces available through Network
707 * Manager
708 * @param callback a function that shall be called to convert Dbus output
709 * into JSON.
710 */
711template <typename CallbackFunc>
712void getEthernetIfaceList(CallbackFunc &&callback)
713{
714 crow::connections::systemBus->async_method_call(
715 [callback{std::move(callback)}](
716 const boost::system::error_code error_code,
717 GetManagedObjects &resp) {
718 // Callback requires vector<string> to retrieve all available
719 // ethernet interfaces
720 std::vector<std::string> iface_list;
721 iface_list.reserve(resp.size());
722 if (error_code)
723 {
724 callback(false, iface_list);
725 return;
726 }
727
728 // Iterate over all retrieved ObjectPaths.
729 for (const auto &objpath : resp)
730 {
731 // And all interfaces available for certain ObjectPath.
732 for (const auto &interface : objpath.second)
733 {
734 // If interface is
735 // xyz.openbmc_project.Network.EthernetInterface, this is
736 // what we're looking for.
737 if (interface.first ==
738 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700739 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700740 // Cut out everyting until last "/", ...
741 const std::string &iface_id = objpath.first.str;
742 std::size_t last_pos = iface_id.rfind("/");
743 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700744 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700745 // and put it into output vector.
746 iface_list.emplace_back(
747 iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700748 }
749 }
750 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700751 }
752 // Finally make a callback with useful data
753 callback(true, iface_list);
754 },
755 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
756 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100757};
758
759/**
760 * EthernetCollection derived class for delivering Ethernet Collection Schema
761 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700762class EthernetCollection : public Node
763{
764 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700765 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700766 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700767 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700768 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 entityPrivileges = {
770 {boost::beast::http::verb::get, {{"Login"}}},
771 {boost::beast::http::verb::head, {{"Login"}}},
772 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
773 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
774 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
775 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
776 }
777
778 private:
779 /**
780 * Functions triggers appropriate requests on DBus
781 */
782 void doGet(crow::Response &res, const crow::Request &req,
783 const std::vector<std::string> &params) override
784 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800785 res.jsonValue["@odata.type"] =
786 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
787 res.jsonValue["@odata.context"] =
788 "/redfish/v1/"
789 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
790 res.jsonValue["@odata.id"] =
791 "/redfish/v1/Managers/bmc/EthernetInterfaces";
792 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
793 res.jsonValue["Description"] =
794 "Collection of EthernetInterfaces for this Manager";
795
Ed Tanous4a0cb852018-10-15 07:55:04 -0700796 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -0700798 getEthernetIfaceList(
799 [&res](const bool &success,
800 const std::vector<std::string> &iface_list) {
801 if (!success)
802 {
803 messages::internalError(res);
804 res.end();
805 return;
806 }
807
808 nlohmann::json &iface_array = res.jsonValue["Members"];
809 iface_array = nlohmann::json::array();
810 for (const std::string &iface_item : iface_list)
811 {
812 iface_array.push_back(
813 {{"@odata.id",
814 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
815 iface_item}});
816 }
817
818 res.jsonValue["Members@odata.count"] = iface_array.size();
819 res.jsonValue["@odata.id"] =
820 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 res.end();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700822 });
Ed Tanous4a0cb852018-10-15 07:55:04 -0700823 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100824};
825
826/**
827 * EthernetInterface derived class for delivering Ethernet Schema
828 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829class EthernetInterface : public Node
830{
831 public:
832 /*
833 * Default Constructor
834 */
Ed Tanous4a0cb852018-10-15 07:55:04 -0700835 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700836 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700837 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700838 std::string())
839 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 entityPrivileges = {
841 {boost::beast::http::verb::get, {{"Login"}}},
842 {boost::beast::http::verb::head, {{"Login"}}},
843 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
844 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
845 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
846 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200847 }
848
Ed Tanous1abe55e2018-09-05 08:30:59 -0700849 // TODO(kkowalsk) Find a suitable class/namespace for this
850 static void handleVlanPatch(const std::string &ifaceId,
851 const nlohmann::json &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700852 const EthernetInterfaceData &ethData,
853 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700854 {
855 if (!input.is_object())
856 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700857 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800858 "VLAN");
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200859 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200861
Ed Tanous4a0cb852018-10-15 07:55:04 -0700862 nlohmann::json::const_iterator vlanEnable = input.find("VLANEnable");
863 if (vlanEnable == input.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800865 messages::propertyMissing(asyncResp->res, "VLANEnable");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700866 return;
867 }
868 const bool *vlanEnableBool = vlanEnable->get_ptr<const bool *>();
869 if (vlanEnableBool == nullptr)
870 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700871 messages::propertyValueTypeError(asyncResp->res, vlanEnable->dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800872 "VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 return;
874 }
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200875
Ed Tanous4a0cb852018-10-15 07:55:04 -0700876 nlohmann::json::const_iterator vlanId = input.find("VLANId");
877 if (vlanId == input.end())
878 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800879 messages::propertyMissing(asyncResp->res, "VLANId");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700880 return;
881 }
882 const uint64_t *vlanIdUint = vlanId->get_ptr<const uint64_t *>();
883 if (vlanIdUint == nullptr)
884 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700885 messages::propertyValueTypeError(asyncResp->res, vlanId->dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800886 "VLANId");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700887 return;
888 }
889
890 if (!ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700891 {
892 // This interface is not a VLAN. Cannot do anything with it
893 // TODO(kkowalsk) Change this message
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800894 messages::propertyNotWritable(asyncResp->res, "VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896 return;
897 }
898
899 // VLAN is configured on the interface
Ed Tanous4a0cb852018-10-15 07:55:04 -0700900 if (*vlanEnableBool == true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 {
902 // Change VLAN Id
Ed Tanous4a0cb852018-10-15 07:55:04 -0700903 asyncResp->res.jsonValue["VLANId"] = *vlanIdUint;
904 auto callback = [asyncResp](const boost::system::error_code ec) {
905 if (ec)
906 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700907 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700908 }
909 else
910 {
911 asyncResp->res.jsonValue["VLANEnable"] = true;
912 }
913 };
914 crow::connections::systemBus->async_method_call(
915 std::move(callback), "xyz.openbmc_project.Network",
916 "/xyz/openbmc_project/network/" + ifaceId,
917 "org.freedesktop.DBus.Properties", "Set",
918 "xyz.openbmc_project.Network.VLAN", "Id",
919 sdbusplus::message::variant<uint32_t>(*vlanIdUint));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700920 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700921 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700923 auto callback = [asyncResp](const boost::system::error_code ec) {
924 if (ec)
925 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700926 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700927 return;
928 }
929 asyncResp->res.jsonValue["VLANEnable"] = false;
930 };
931
932 crow::connections::systemBus->async_method_call(
933 std::move(callback), "xyz.openbmc_project.Network",
934 "/xyz/openbmc_project/network/" + ifaceId,
935 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700936 }
937 }
938
939 private:
940 void handleHostnamePatch(const nlohmann::json &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700941 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700943 const std::string *newHostname = input.get_ptr<const std::string *>();
944 if (newHostname == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700946 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800947 "HostName");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700948 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700950
951 // Change hostname
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800952 setHostName(*newHostname,
953 [asyncResp, newHostname{std::string(*newHostname)}](
954 const boost::system::error_code ec) {
955 if (ec)
956 {
957 messages::internalError(asyncResp->res);
958 }
959 else
960 {
961 asyncResp->res.jsonValue["HostName"] = newHostname;
962 }
963 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964 }
965
Ed Tanous4a0cb852018-10-15 07:55:04 -0700966 void handleIPv4Patch(
967 const std::string &ifaceId, const nlohmann::json &input,
968 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
969 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700970 {
971 if (!input.is_array())
972 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700973 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800974 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 return;
976 }
977
978 // According to Redfish PATCH definition, size must be at least equal
Ed Tanous4a0cb852018-10-15 07:55:04 -0700979 if (input.size() < ipv4Data.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800981 messages::propertyValueFormatError(asyncResp->res, input.dump(),
982 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 return;
984 }
985
Ed Tanous4a0cb852018-10-15 07:55:04 -0700986 int entryIdx = 0;
987 boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
988 ipv4Data.begin();
989 for (const nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700991 std::string pathString =
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800992 "IPv4Addresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 // Check that entry is not of some unexpected type
Ed Tanous4a0cb852018-10-15 07:55:04 -0700994 if (!thisJson.is_object() && !thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800996 messages::propertyValueTypeError(asyncResp->res,
997 thisJson.dump(),
998 pathString + "/IPv4Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999
1000 continue;
1001 }
1002
Ed Tanous4a0cb852018-10-15 07:55:04 -07001003 nlohmann::json::const_iterator addressFieldIt =
1004 thisJson.find("Address");
1005 const std::string *addressField = nullptr;
1006 if (addressFieldIt != thisJson.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001008 addressField = addressFieldIt->get_ptr<const std::string *>();
1009 if (addressField == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001010 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001011 messages::propertyValueFormatError(asyncResp->res,
1012 addressFieldIt->dump(),
1013 pathString + "/Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001014 continue;
1015 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001016 else
1017 {
1018 if (!ipv4VerifyIpAndGetBitcount(*addressField))
1019 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001020 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001021 asyncResp->res, *addressField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001022 pathString + "/Address");
1023 continue;
1024 }
1025 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001026 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001027
1028 boost::optional<uint8_t> prefixLength;
1029 const std::string *subnetField = nullptr;
1030 nlohmann::json::const_iterator subnetFieldIt =
1031 thisJson.find("SubnetMask");
1032 if (subnetFieldIt != thisJson.end())
1033 {
1034 subnetField = subnetFieldIt->get_ptr<const std::string *>();
1035 if (subnetField == nullptr)
1036 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001037 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001038 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001039 pathString + "/SubnetMask");
1040 continue;
1041 }
1042 else
1043 {
1044 prefixLength = 0;
1045 if (!ipv4VerifyIpAndGetBitcount(*subnetField,
1046 &*prefixLength))
1047 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001048 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001049 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001050 pathString + "/SubnetMask");
1051 continue;
1052 }
1053 }
1054 }
1055
1056 std::string addressOriginInDBusFormat;
1057 const std::string *addressOriginField = nullptr;
1058 nlohmann::json::const_iterator addressOriginFieldIt =
1059 thisJson.find("AddressOrigin");
1060 if (addressOriginFieldIt != thisJson.end())
1061 {
1062 const std::string *addressOriginField =
1063 addressOriginFieldIt->get_ptr<const std::string *>();
1064 if (addressOriginField == nullptr)
1065 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001066 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001067 asyncResp->res, *addressOriginField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001068 pathString + "/AddressOrigin");
1069 continue;
1070 }
1071 else
1072 {
1073 // Get Address origin in proper format
1074 addressOriginInDBusFormat =
1075 translateAddressOriginRedfishToDbus(
1076 *addressOriginField);
1077 if (addressOriginInDBusFormat.empty())
1078 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001079 messages::propertyValueNotInList(
1080 asyncResp->res, *addressOriginField,
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001081 pathString + "/AddressOrigin");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001082 continue;
1083 }
1084 }
1085 }
1086
1087 nlohmann::json::const_iterator gatewayFieldIt =
1088 thisJson.find("Gateway");
1089 const std::string *gatewayField = nullptr;
1090 if (gatewayFieldIt != thisJson.end())
1091 {
1092 const std::string *gatewayField =
1093 gatewayFieldIt->get_ptr<const std::string *>();
1094 if (gatewayField == nullptr ||
1095 !ipv4VerifyIpAndGetBitcount(*gatewayField))
1096 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001097 messages::propertyValueFormatError(
1098 asyncResp->res, *gatewayField, pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001099 continue;
1100 }
1101 }
1102
1103 // if a vlan already exists, modify the existing
1104 if (thisData != ipv4Data.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001105 {
1106 // Existing object that should be modified/deleted/remain
1107 // unchanged
Ed Tanous4a0cb852018-10-15 07:55:04 -07001108 if (thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001109 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001110 auto callback = [entryIdx{std::to_string(entryIdx)},
1111 asyncResp](
1112 const boost::system::error_code ec) {
1113 if (ec)
1114 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001115 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001116 return;
1117 }
1118 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
1119 nullptr;
1120 };
1121 crow::connections::systemBus->async_method_call(
1122 std::move(callback), "xyz.openbmc_project.Network",
1123 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1124 thisData->id,
1125 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001126 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001127 else if (thisJson.is_object())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001129 // Apply changes
Ed Tanous4a0cb852018-10-15 07:55:04 -07001130 if (addressField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001131 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001132 auto callback =
1133 [asyncResp, entryIdx,
1134 addressField{std::string(*addressField)}](
1135 const boost::system::error_code ec) {
1136 if (ec)
1137 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001138 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001139 return;
1140 }
1141 asyncResp->res
1142 .jsonValue["IPv4Addresses"][std::to_string(
1143 entryIdx)]["Address"] = addressField;
1144 };
1145
1146 crow::connections::systemBus->async_method_call(
1147 std::move(callback), "xyz.openbmc_project.Network",
1148 "/xyz/openbmc_project/network/" + ifaceId +
1149 "/ipv4/" + thisData->id,
1150 "org.freedesktop.DBus.Properties", "Set",
1151 "xyz.openbmc_project.Network.IP", "Address",
1152 sdbusplus::message::variant<std::string>(
1153 *addressField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001154 }
1155
Ed Tanous4a0cb852018-10-15 07:55:04 -07001156 if (prefixLength && subnetField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001157 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001158 changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
1159 thisData->id, *subnetField,
1160 *prefixLength, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001161 }
1162
Ed Tanous4a0cb852018-10-15 07:55:04 -07001163 if (!addressOriginInDBusFormat.empty() &&
1164 addressOriginField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001165 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001166 changeIPv4Origin(ifaceId, entryIdx, thisData->id,
1167 *addressOriginField,
1168 addressOriginInDBusFormat, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001169 }
1170
Ed Tanous4a0cb852018-10-15 07:55:04 -07001171 if (gatewayField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001172 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001173 auto callback =
1174 [asyncResp, entryIdx,
1175 gatewayField{std::string(*gatewayField)}](
1176 const boost::system::error_code ec) {
1177 if (ec)
1178 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001179 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001180 return;
1181 }
1182 asyncResp->res
1183 .jsonValue["IPv4Addresses"][std::to_string(
1184 entryIdx)]["Gateway"] =
1185 std::move(gatewayField);
1186 };
1187
1188 crow::connections::systemBus->async_method_call(
1189 std::move(callback), "xyz.openbmc_project.Network",
1190 "/xyz/openbmc_project/network/" + ifaceId +
1191 "/ipv4/" + thisData->id,
1192 "org.freedesktop.DBus.Properties", "Set",
1193 "xyz.openbmc_project.Network.IP", "Gateway",
1194 sdbusplus::message::variant<std::string>(
1195 *gatewayField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 }
1197 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001198 thisData++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001199 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001200 else
1201 {
1202 // Create IPv4 with provided data
1203 if (gatewayField == nullptr)
1204 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001205 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001206 pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001207 continue;
1208 }
1209
1210 if (addressField == nullptr)
1211 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001212 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001213 pathString + "/Address");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001214 continue;
1215 }
1216
1217 if (!prefixLength)
1218 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001219 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001220 pathString + "/SubnetMask");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001221 continue;
1222 }
1223
1224 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
1225 *addressField, asyncResp);
1226 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
1227 }
1228 entryIdx++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001229 }
1230 }
1231
Ed Tanous0f74e642018-11-12 15:17:05 -08001232 void parseInterfaceData(
1233 nlohmann::json &json_response, const std::string &iface_id,
1234 const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001235 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001236 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001237 json_response["Id"] = iface_id;
1238 json_response["@odata.id"] =
1239 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001240
Ed Tanous4a0cb852018-10-15 07:55:04 -07001241 json_response["SpeedMbps"] = ethData.speed;
1242 json_response["MACAddress"] = ethData.mac_address;
1243 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001244 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001245 json_response["HostName"] = ethData.hostname;
1246 }
1247
1248 nlohmann::json &vlanObj = json_response["VLAN"];
1249 if (ethData.vlan_id)
1250 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001251 vlanObj["VLANEnable"] = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001252 vlanObj["VLANId"] = *ethData.vlan_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001253 }
1254 else
1255 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001256 vlanObj["VLANEnable"] = false;
1257 vlanObj["VLANId"] = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001258 }
1259
Ed Tanous4a0cb852018-10-15 07:55:04 -07001260 if (ipv4Data.size() > 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001261 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001262 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
1263 ipv4_array = nlohmann::json::array();
1264 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001265 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001266 if (!ipv4_config.address.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001267 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001268 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1269 {"SubnetMask", ipv4_config.netmask},
1270 {"Address", ipv4_config.address}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001271
Ed Tanous4a0cb852018-10-15 07:55:04 -07001272 if (!ipv4_config.gateway.empty())
1273 {
1274 ipv4_array.back()["Gateway"] = ipv4_config.gateway;
1275 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001276 }
1277 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001278 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001279 }
1280
1281 /**
1282 * Functions triggers appropriate requests on DBus
1283 */
1284 void doGet(crow::Response &res, const crow::Request &req,
1285 const std::vector<std::string> &params) override
1286 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001287 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001288 if (params.size() != 1)
1289 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001290 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001291 return;
1292 }
1293
Ed Tanous4a0cb852018-10-15 07:55:04 -07001294 getEthernetIfaceData(
1295 params[0],
1296 [this, asyncResp, iface_id{std::string(params[0])}](
1297 const bool &success, const EthernetInterfaceData &ethData,
1298 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1299 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001300 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001301 // TODO(Pawel)consider distinguish between non existing
1302 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001303 messages::resourceNotFound(asyncResp->res,
1304 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001305 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001306 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001307 asyncResp->res.jsonValue["@odata.type"] =
1308 "#EthernetInterface.v1_2_0.EthernetInterface";
1309 asyncResp->res.jsonValue["@odata.context"] =
1310 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
1311 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1312 asyncResp->res.jsonValue["Description"] =
1313 "Management Network Interface";
1314
1315 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1316 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001317 });
1318 }
1319
1320 void doPatch(crow::Response &res, const crow::Request &req,
1321 const std::vector<std::string> &params) override
1322 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001323 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001324 if (params.size() != 1)
1325 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001326 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001327 return;
1328 }
1329
Ed Tanous4a0cb852018-10-15 07:55:04 -07001330 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001331
1332 nlohmann::json patchReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001333 if (!json_util::processJsonFromRequest(res, req, patchReq))
1334 {
1335 return;
1336 }
1337
Ed Tanous4a0cb852018-10-15 07:55:04 -07001338 // Get single eth interface data, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001339 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001340 getEthernetIfaceData(
1341 iface_id,
1342 [this, asyncResp, iface_id, patchReq = std::move(patchReq)](
1343 const bool &success, const EthernetInterfaceData &ethData,
1344 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001345 if (!success)
1346 {
1347 // ... otherwise return error
1348 // TODO(Pawel)consider distinguish between non existing
1349 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001350 messages::resourceNotFound(
1351 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001352 return;
1353 }
1354
Ed Tanous0f74e642018-11-12 15:17:05 -08001355 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1356 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001357
Ed Tanous4a0cb852018-10-15 07:55:04 -07001358 for (auto propertyIt : patchReq.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001359 {
1360 if (propertyIt.key() == "VLAN")
1361 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001362 handleVlanPatch(iface_id, propertyIt.value(), ethData,
1363 asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001364 }
1365 else if (propertyIt.key() == "HostName")
1366 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001367 handleHostnamePatch(propertyIt.value(), asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001368 }
1369 else if (propertyIt.key() == "IPv4Addresses")
1370 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001371 handleIPv4Patch(iface_id, propertyIt.value(), ipv4Data,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001372 asyncResp);
1373 }
1374 else if (propertyIt.key() == "IPv6Addresses")
1375 {
1376 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001377 messages::propertyNotWritable(asyncResp->res,
1378 propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001379 }
1380 else
1381 {
1382 auto fieldInJsonIt =
Ed Tanous4a0cb852018-10-15 07:55:04 -07001383 asyncResp->res.jsonValue.find(propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001384
Ed Tanous4a0cb852018-10-15 07:55:04 -07001385 if (fieldInJsonIt == asyncResp->res.jsonValue.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001386 {
1387 // Field not in scope of defined fields
Jason M. Billsf12894f2018-10-09 12:45:45 -07001388 messages::propertyUnknown(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001389 propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001390 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001391 else
Ed Tanous1abe55e2018-09-05 08:30:59 -07001392 {
1393 // User attempted to modify non-writable field
Jason M. Billsf12894f2018-10-09 12:45:45 -07001394 messages::propertyNotWritable(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001395 propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001396 }
1397 }
1398 }
1399 });
1400 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001401};
1402
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001403/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001404 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1405 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001406 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001407class VlanNetworkInterface : public Node
1408{
1409 public:
1410 /*
1411 * Default Constructor
1412 */
1413 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001414 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001415 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001416 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001417 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001418 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001419 entityPrivileges = {
1420 {boost::beast::http::verb::get, {{"Login"}}},
1421 {boost::beast::http::verb::head, {{"Login"}}},
1422 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1423 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1424 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1425 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001426 }
1427
Ed Tanous1abe55e2018-09-05 08:30:59 -07001428 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001429 void parseInterfaceData(
1430 nlohmann::json &json_response, const std::string &parent_iface_id,
1431 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001432 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001433 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001434 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001435 json_response["Id"] = iface_id;
1436 json_response["@odata.id"] =
1437 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1438 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001439
Ed Tanous4a0cb852018-10-15 07:55:04 -07001440 json_response["VLANEnable"] = true;
1441 if (ethData.vlan_id)
1442 {
1443 json_response["VLANId"] = *ethData.vlan_id;
1444 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001445 }
1446
Ed Tanous1abe55e2018-09-05 08:30:59 -07001447 bool verifyNames(crow::Response &res, const std::string &parent,
1448 const std::string &iface)
1449 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001450 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001451 if (!boost::starts_with(iface, parent + "_"))
1452 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001453 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1454 iface);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001455 return false;
1456 }
1457 else
1458 {
1459 return true;
1460 }
1461 }
1462
1463 /**
1464 * Functions triggers appropriate requests on DBus
1465 */
1466 void doGet(crow::Response &res, const crow::Request &req,
1467 const std::vector<std::string> &params) override
1468 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001469 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1470 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001471 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1472 // Check if there is required param, truly entering this shall be
1473 // impossible.
1474 if (params.size() != 2)
1475 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001476 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001477 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001478 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001479 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001480
Ed Tanous4a0cb852018-10-15 07:55:04 -07001481 const std::string &parent_iface_id = params[0];
1482 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001483 res.jsonValue["@odata.type"] =
1484 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1485 res.jsonValue["@odata.context"] =
1486 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1487 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001488
Ed Tanous4a0cb852018-10-15 07:55:04 -07001489 if (!verifyNames(res, parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001490 {
1491 return;
1492 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001493
Ed Tanous1abe55e2018-09-05 08:30:59 -07001494 // Get single eth interface data, and call the below callback for JSON
1495 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001496 getEthernetIfaceData(
1497 iface_id,
1498 [this, asyncResp, parent_iface_id, iface_id](
1499 const bool &success, const EthernetInterfaceData &ethData,
1500 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1501 if (success && ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001502 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001503 parseInterfaceData(asyncResp->res.jsonValue,
1504 parent_iface_id, iface_id, ethData,
1505 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001506 }
1507 else
1508 {
1509 // ... otherwise return error
1510 // TODO(Pawel)consider distinguish between non existing
1511 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001512 messages::resourceNotFound(
1513 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001514 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001515 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001516 }
1517
Ed Tanous1abe55e2018-09-05 08:30:59 -07001518 void doPatch(crow::Response &res, const crow::Request &req,
1519 const std::vector<std::string> &params) override
1520 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001521 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001522 if (params.size() != 2)
1523 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001524 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001525 return;
1526 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001527
Ed Tanous1abe55e2018-09-05 08:30:59 -07001528 const std::string &parentIfaceId = params[0];
1529 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001530
Ed Tanous1abe55e2018-09-05 08:30:59 -07001531 if (!verifyNames(res, parentIfaceId, ifaceId))
1532 {
1533 return;
1534 }
1535
1536 nlohmann::json patchReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001537 if (!json_util::processJsonFromRequest(res, req, patchReq))
1538 {
1539 return;
1540 }
1541
1542 // Get single eth interface data, and call the below callback for JSON
1543 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001544 getEthernetIfaceData(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001545 ifaceId,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001546 [this, asyncResp, parentIfaceId, ifaceId,
1547 patchReq{std::move(patchReq)}](
1548 const bool &success, const EthernetInterfaceData &ethData,
1549 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001550 if (!success)
1551 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001552 // TODO(Pawel)consider distinguish between non existing
1553 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001554 messages::resourceNotFound(
1555 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001556
1557 return;
1558 }
1559
Ed Tanous0f74e642018-11-12 15:17:05 -08001560 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1561 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001562
Ed Tanous4a0cb852018-10-15 07:55:04 -07001563 for (auto propertyIt : patchReq.items())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001564 {
1565 if (propertyIt.key() != "VLANEnable" &&
1566 propertyIt.key() != "VLANId")
1567 {
1568 auto fieldInJsonIt =
Ed Tanous4a0cb852018-10-15 07:55:04 -07001569 asyncResp->res.jsonValue.find(propertyIt.key());
1570 if (fieldInJsonIt == asyncResp->res.jsonValue.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001571 {
1572 // Field not in scope of defined fields
Jason M. Billsf12894f2018-10-09 12:45:45 -07001573 messages::propertyUnknown(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001574 propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001575 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001576 else
Ed Tanous1abe55e2018-09-05 08:30:59 -07001577 {
1578 // User attempted to modify non-writable field
Jason M. Billsf12894f2018-10-09 12:45:45 -07001579 messages::propertyNotWritable(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001580 propertyIt.key());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001581 }
1582 }
1583 }
1584
Ed Tanous4a0cb852018-10-15 07:55:04 -07001585 EthernetInterface::handleVlanPatch(ifaceId, patchReq, ethData,
1586 asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001587 });
1588 }
1589
1590 void doDelete(crow::Response &res, const crow::Request &req,
1591 const std::vector<std::string> &params) override
1592 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001593 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001594 if (params.size() != 2)
1595 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001596 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001597 return;
1598 }
1599
1600 const std::string &parentIfaceId = params[0];
1601 const std::string &ifaceId = params[1];
1602
Ed Tanous4a0cb852018-10-15 07:55:04 -07001603 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001604 {
1605 return;
1606 }
1607
1608 // Get single eth interface data, and call the below callback for JSON
1609 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001610 getEthernetIfaceData(
1611 ifaceId,
1612 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
1613 ifaceId{std::string(ifaceId)}](
1614 const bool &success, const EthernetInterfaceData &ethData,
1615 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1616 if (success && ethData.vlan_id)
1617 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001618 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1619 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001620
Jason M. Billsf12894f2018-10-09 12:45:45 -07001621 auto callback =
1622 [asyncResp](const boost::system::error_code ec) {
1623 if (ec)
1624 {
1625 messages::internalError(asyncResp->res);
1626 }
1627 };
1628 crow::connections::systemBus->async_method_call(
1629 std::move(callback), "xyz.openbmc_project.Network",
1630 std::string("/xyz/openbmc_project/network/") + ifaceId,
1631 "xyz.openbmc_project.Object.Delete", "Delete");
1632 }
1633 else
1634 {
1635 // ... otherwise return error
1636 // TODO(Pawel)consider distinguish between non existing
1637 // object, and other errors
1638 messages::resourceNotFound(
1639 asyncResp->res, "VLAN Network Interface", ifaceId);
1640 }
1641 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001642 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001643};
1644
1645/**
1646 * VlanNetworkInterfaceCollection derived class for delivering
1647 * VLANNetworkInterface Collection Schema
1648 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001649class VlanNetworkInterfaceCollection : public Node
1650{
1651 public:
1652 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001653 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001654 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
1655 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001656 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001657 entityPrivileges = {
1658 {boost::beast::http::verb::get, {{"Login"}}},
1659 {boost::beast::http::verb::head, {{"Login"}}},
1660 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1661 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1662 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1663 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001664 }
1665
Ed Tanous1abe55e2018-09-05 08:30:59 -07001666 private:
1667 /**
1668 * Functions triggers appropriate requests on DBus
1669 */
1670 void doGet(crow::Response &res, const crow::Request &req,
1671 const std::vector<std::string> &params) override
1672 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001673 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001674 if (params.size() != 1)
1675 {
1676 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07001677 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001678 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001679 }
1680
Ed Tanous4a0cb852018-10-15 07:55:04 -07001681 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001682
Ed Tanous4a0cb852018-10-15 07:55:04 -07001683 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001684 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001685 getEthernetIfaceList(
1686 [this, asyncResp,
1687 rootInterfaceName{std::string(rootInterfaceName)}](
1688 const bool &success,
1689 const std::vector<std::string> &iface_list) {
1690 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001691 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001692 messages::internalError(asyncResp->res);
1693 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001694 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001695 asyncResp->res.jsonValue["@odata.type"] =
1696 "#VLanNetworkInterfaceCollection."
1697 "VLanNetworkInterfaceCollection";
1698 asyncResp->res.jsonValue["@odata.context"] =
1699 "/redfish/v1/$metadata"
1700 "#VLanNetworkInterfaceCollection."
1701 "VLanNetworkInterfaceCollection";
1702 asyncResp->res.jsonValue["Name"] =
1703 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001704
Jason M. Billsf12894f2018-10-09 12:45:45 -07001705 nlohmann::json iface_array = nlohmann::json::array();
1706
1707 for (const std::string &iface_item : iface_list)
1708 {
1709 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
1710 {
1711 iface_array.push_back(
1712 {{"@odata.id",
1713 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1714 rootInterfaceName + "/VLANs/" + iface_item}});
1715 }
1716 }
1717
1718 if (iface_array.empty())
1719 {
1720 messages::resourceNotFound(
1721 asyncResp->res, "EthernetInterface", rootInterfaceName);
1722 return;
1723 }
1724 asyncResp->res.jsonValue["Members@odata.count"] =
1725 iface_array.size();
1726 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
1727 asyncResp->res.jsonValue["@odata.id"] =
1728 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1729 rootInterfaceName + "/VLANs";
1730 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001731 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001732
Ed Tanous1abe55e2018-09-05 08:30:59 -07001733 void doPost(crow::Response &res, const crow::Request &req,
1734 const std::vector<std::string> &params) override
1735 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001736 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001737 if (params.size() != 1)
1738 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001739 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001740 return;
1741 }
1742
1743 nlohmann::json postReq;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001744 if (!json_util::processJsonFromRequest(res, req, postReq))
1745 {
1746 return;
1747 }
1748
Ed Tanous0f74e642018-11-12 15:17:05 -08001749 auto vlanIdJson = postReq.find("VLANId");
1750 if (vlanIdJson == postReq.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001751 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001752 messages::propertyMissing(asyncResp->res, "VLANId");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001753 return;
1754 }
1755
Ed Tanous4a0cb852018-10-15 07:55:04 -07001756 const uint64_t *vlanId = vlanIdJson->get_ptr<const uint64_t *>();
1757 if (vlanId == nullptr)
1758 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001759 messages::propertyValueTypeError(asyncResp->res, vlanIdJson->dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001760 "VLANId");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001761 return;
1762 }
1763 const std::string &rootInterfaceName = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001764
Ed Tanous4a0cb852018-10-15 07:55:04 -07001765 auto callback = [asyncResp](const boost::system::error_code ec) {
1766 if (ec)
1767 {
1768 // TODO(ed) make more consistent error messages based on
1769 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07001770 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001771 return;
1772 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001773 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001774 };
1775 crow::connections::systemBus->async_method_call(
1776 std::move(callback), "xyz.openbmc_project.Network",
1777 "/xyz/openbmc_project/network",
1778 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
1779 rootInterfaceName, static_cast<uint32_t>(*vlanId));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001780 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001781};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001782} // namespace redfish