blob: 9aa4f7738781da2f7040199f2e24cbdd5b3c36b5 [file] [log] [blame]
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16#pragma once
17
Ed Tanous1abe55e2018-09-05 08:30:59 -070018#include <boost/container/flat_map.hpp>
Ed Tanous4a0cb852018-10-15 07:55:04 -070019#include <boost/container/flat_set.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020020#include <dbus_singleton.hpp>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020021#include <error_messages.hpp>
Kowalski, Kamil179db1d2018-04-23 11:12:41 +020022#include <node.hpp>
Ed Tanousa24526d2018-12-10 15:17:59 -080023#include <optional>
Kowalski, Kamil588c3f02018-04-03 14:55:27 +020024#include <utils/json_utils.hpp>
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;
Ed Tanousa24526d2018-12-10 15:17:59 -080083 std::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 =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800174 sdbusplus::message::variant_ns::get_if<
175 std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700176 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 Tanous1b6b96c2018-11-30 11:35:41 -0800189 const uint32_t *id =
190 sdbusplus::message::variant_ns::get_if<
191 uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700192 if (id != nullptr)
193 {
194 ethData.vlan_id = *id;
195 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700197 }
198 }
199 else if (ifacePair.first ==
200 "xyz.openbmc_project.Network.EthernetInterface")
201 {
202 for (const auto &propertyPair : ifacePair.second)
203 {
204 if (propertyPair.first == "AutoNeg")
205 {
206 const bool *auto_neg =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800207 sdbusplus::message::variant_ns::get_if<bool>(
208 &propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700209 if (auto_neg != nullptr)
210 {
211 ethData.auto_neg = *auto_neg;
212 }
213 }
214 else if (propertyPair.first == "Speed")
215 {
216 const uint32_t *speed =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800217 sdbusplus::message::variant_ns::get_if<
218 uint32_t>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700219 if (speed != nullptr)
220 {
221 ethData.speed = *speed;
222 }
223 }
224 }
225 }
226 else if (ifacePair.first ==
227 "xyz.openbmc_project.Network.SystemConfiguration")
228 {
229 for (const auto &propertyPair : ifacePair.second)
230 {
231 if (propertyPair.first == "HostName")
232 {
233 const std::string *hostname =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800234 sdbusplus::message::variant_ns::get_if<
235 std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700236 if (hostname != nullptr)
237 {
238 ethData.hostname = *hostname;
239 }
240 }
241 else if (propertyPair.first == "DefaultGateway")
242 {
243 const std::string *defaultGateway =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800244 sdbusplus::message::variant_ns::get_if<
245 std::string>(&propertyPair.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700246 if (defaultGateway != nullptr)
247 {
248 ethData.default_gateway = *defaultGateway;
249 }
250 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 }
252 }
253 }
254 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700256}
257
258// Helper function that extracts data for single ethernet ipv4 address
259inline void
260 extractIPData(const std::string &ethiface_id,
261 const GetManagedObjects &dbus_data,
262 boost::container::flat_set<IPv4AddressData> &ipv4_config)
263{
264 const std::string ipv4PathStart =
265 "/xyz/openbmc_project/network/" + ethiface_id + "/ipv4/";
266
267 // Since there might be several IPv4 configurations aligned with
268 // single ethernet interface, loop over all of them
269 for (const auto &objpath : dbus_data)
270 {
271 // Check if proper pattern for object path appears
272 if (boost::starts_with(objpath.first.str, ipv4PathStart))
273 {
274 for (auto &interface : objpath.second)
275 {
276 if (interface.first == "xyz.openbmc_project.Network.IP")
277 {
278 // Instance IPv4AddressData structure, and set as
279 // appropriate
280 std::pair<
281 boost::container::flat_set<IPv4AddressData>::iterator,
282 bool>
283 it = ipv4_config.insert(
284 {objpath.first.str.substr(ipv4PathStart.size())});
285 IPv4AddressData &ipv4_address = *it.first;
286 for (auto &property : interface.second)
287 {
288 if (property.first == "Address")
289 {
290 const std::string *address =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800291 sdbusplus::message::variant_ns::get_if<
292 std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700293 if (address != nullptr)
294 {
295 ipv4_address.address = *address;
296 }
297 }
298 else if (property.first == "Gateway")
299 {
300 const std::string *gateway =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800301 sdbusplus::message::variant_ns::get_if<
302 std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700303 if (gateway != nullptr)
304 {
305 ipv4_address.gateway = *gateway;
306 }
307 }
308 else if (property.first == "Origin")
309 {
310 const std::string *origin =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800311 sdbusplus::message::variant_ns::get_if<
312 std::string>(&property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700313 if (origin != nullptr)
314 {
315 ipv4_address.origin =
316 translateAddressOriginDbusToRedfish(*origin,
317 true);
318 }
319 }
320 else if (property.first == "PrefixLength")
321 {
322 const uint8_t *mask =
Ed Tanous1b6b96c2018-11-30 11:35:41 -0800323 sdbusplus::message::variant_ns::get_if<uint8_t>(
324 &property.second);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700325 if (mask != nullptr)
326 {
327 // convert it to the string
328 ipv4_address.netmask = getNetmask(*mask);
329 }
330 }
331 else
332 {
333 BMCWEB_LOG_ERROR
334 << "Got extra property: " << property.first
335 << " on the " << objpath.first.str << " object";
336 }
337 }
338 // Check if given address is local, or global
339 ipv4_address.linktype =
340 boost::starts_with(ipv4_address.address, "169.254.")
341 ? LinkType::Global
342 : LinkType::Local;
343 }
344 }
345 }
346 }
347}
348
349/**
350 * @brief Sets given Id on the given VLAN interface through D-Bus
351 *
352 * @param[in] ifaceId Id of VLAN interface that should be modified
353 * @param[in] inputVlanId New ID of the VLAN
354 * @param[in] callback Function that will be called after the operation
355 *
356 * @return None.
357 */
358template <typename CallbackFunc>
359void changeVlanId(const std::string &ifaceId, const uint32_t &inputVlanId,
360 CallbackFunc &&callback)
361{
362 crow::connections::systemBus->async_method_call(
363 callback, "xyz.openbmc_project.Network",
364 std::string("/xyz/openbmc_project/network/") + ifaceId,
365 "org.freedesktop.DBus.Properties", "Set",
366 "xyz.openbmc_project.Network.VLAN", "Id",
367 sdbusplus::message::variant<uint32_t>(inputVlanId));
368}
369
370/**
371 * @brief Helper function that verifies IP address to check if it is in
372 * proper format. If bits pointer is provided, also calculates active
373 * bit count for Subnet Mask.
374 *
375 * @param[in] ip IP that will be verified
376 * @param[out] bits Calculated mask in bits notation
377 *
378 * @return true in case of success, false otherwise
379 */
380inline bool ipv4VerifyIpAndGetBitcount(const std::string &ip,
381 uint8_t *bits = nullptr)
382{
383 std::vector<std::string> bytesInMask;
384
385 boost::split(bytesInMask, ip, boost::is_any_of("."));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386
387 static const constexpr int ipV4AddressSectionsCount = 4;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700388 if (bytesInMask.size() != ipV4AddressSectionsCount)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700389 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700390 return false;
391 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392
Ed Tanous4a0cb852018-10-15 07:55:04 -0700393 if (bits != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700395 *bits = 0;
396 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397
Ed Tanous4a0cb852018-10-15 07:55:04 -0700398 char *endPtr;
399 long previousValue = 255;
400 bool firstZeroInByteHit;
401 for (const std::string &byte : bytesInMask)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700403 if (byte.empty())
404 {
405 return false;
406 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700407
Ed Tanous4a0cb852018-10-15 07:55:04 -0700408 // Use strtol instead of stroi to avoid exceptions
409 long value = std::strtol(byte.c_str(), &endPtr, 10);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410
Ed Tanous4a0cb852018-10-15 07:55:04 -0700411 // endPtr should point to the end of the string, otherwise given string
412 // is not 100% number
413 if (*endPtr != '\0')
414 {
415 return false;
416 }
417
418 // Value should be contained in byte
419 if (value < 0 || value > 255)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 {
421 return false;
422 }
423
424 if (bits != nullptr)
425 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700426 // Mask has to be continuous between bytes
427 if (previousValue != 255 && value != 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700428 {
429 return false;
430 }
431
Ed Tanous4a0cb852018-10-15 07:55:04 -0700432 // Mask has to be continuous inside bytes
433 firstZeroInByteHit = false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434
Ed Tanous4a0cb852018-10-15 07:55:04 -0700435 // Count bits
436 for (int bitIdx = 7; bitIdx >= 0; bitIdx--)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700437 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700438 if (value & (1 << bitIdx))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700439 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700440 if (firstZeroInByteHit)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700441 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700442 // Continuity not preserved
443 return false;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700444 }
445 else
446 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700447 (*bits)++;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 }
449 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 else
451 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700452 firstZeroInByteHit = true;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700453 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700454 }
455 }
456
457 previousValue = value;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700458 }
459
Ed Tanous4a0cb852018-10-15 07:55:04 -0700460 return true;
461}
462
463/**
464 * @brief Changes IPv4 address type property (Address, Gateway)
465 *
466 * @param[in] ifaceId Id of interface whose IP should be modified
467 * @param[in] ipIdx Index of IP in input array that should be modified
468 * @param[in] ipHash DBus Hash id of modified IP
469 * @param[in] name Name of field in JSON representation
470 * @param[in] newValue New value that should be written
471 * @param[io] asyncResp Response object that will be returned to client
472 *
473 * @return true if give IP is valid and has been sent do D-Bus, false
474 * otherwise
475 */
476inline void changeIPv4AddressProperty(
477 const std::string &ifaceId, int ipIdx, const std::string &ipHash,
478 const std::string &name, const std::string &newValue,
479 const std::shared_ptr<AsyncResp> asyncResp)
480{
481 auto callback = [asyncResp, ipIdx, name{std::string(name)},
482 newValue{std::move(newValue)}](
483 const boost::system::error_code ec) {
484 if (ec)
485 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800486 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700487 }
488 else
489 {
490 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx][name] = newValue;
491 }
492 };
493
494 crow::connections::systemBus->async_method_call(
495 std::move(callback), "xyz.openbmc_project.Network",
496 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
497 "org.freedesktop.DBus.Properties", "Set",
498 "xyz.openbmc_project.Network.IP", name,
499 sdbusplus::message::variant<std::string>(newValue));
500}
501
502/**
503 * @brief Changes IPv4 address origin property
504 *
505 * @param[in] ifaceId Id of interface whose IP should be modified
506 * @param[in] ipIdx Index of IP in input array that should be
507 * modified
508 * @param[in] ipHash DBus Hash id of modified IP
509 * @param[in] newValue New value in Redfish format
510 * @param[in] newValueDbus New value in D-Bus format
511 * @param[io] asyncResp Response object that will be returned to client
512 *
513 * @return true if give IP is valid and has been sent do D-Bus, false
514 * otherwise
515 */
516inline void changeIPv4Origin(const std::string &ifaceId, int ipIdx,
517 const std::string &ipHash,
518 const std::string &newValue,
519 const std::string &newValueDbus,
520 const std::shared_ptr<AsyncResp> asyncResp)
521{
522 auto callback = [asyncResp, ipIdx, newValue{std::move(newValue)}](
523 const boost::system::error_code ec) {
524 if (ec)
525 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800526 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700527 }
528 else
529 {
530 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["AddressOrigin"] =
531 newValue;
532 }
533 };
534
535 crow::connections::systemBus->async_method_call(
536 std::move(callback), "xyz.openbmc_project.Network",
537 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
538 "org.freedesktop.DBus.Properties", "Set",
539 "xyz.openbmc_project.Network.IP", "Origin",
540 sdbusplus::message::variant<std::string>(newValueDbus));
541}
542
543/**
544 * @brief Modifies SubnetMask for given IP
545 *
546 * @param[in] ifaceId Id of interface whose IP should be modified
547 * @param[in] ipIdx Index of IP in input array that should be
548 * modified
549 * @param[in] ipHash DBus Hash id of modified IP
550 * @param[in] newValueStr Mask in dot notation as string
551 * @param[in] newValue Mask as PrefixLength in bitcount
552 * @param[io] asyncResp Response object that will be returned to client
553 *
554 * @return None
555 */
556inline void changeIPv4SubnetMaskProperty(const std::string &ifaceId, int ipIdx,
557 const std::string &ipHash,
558 const std::string &newValueStr,
559 uint8_t &newValue,
560 std::shared_ptr<AsyncResp> asyncResp)
561{
562 auto callback = [asyncResp, ipIdx, newValueStr{std::move(newValueStr)}](
563 const boost::system::error_code ec) {
564 if (ec)
565 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800566 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700567 }
568 else
569 {
570 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx]["SubnetMask"] =
571 newValueStr;
572 }
573 };
574
575 crow::connections::systemBus->async_method_call(
576 std::move(callback), "xyz.openbmc_project.Network",
577 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
578 "org.freedesktop.DBus.Properties", "Set",
579 "xyz.openbmc_project.Network.IP", "PrefixLength",
580 sdbusplus::message::variant<uint8_t>(newValue));
581}
582
583/**
584 * @brief Sets given HostName of the machine through D-Bus
585 *
586 * @param[in] newHostname New name that HostName will be changed to
587 * @param[in] callback Function that will be called after the operation
588 *
589 * @return None.
590 */
591template <typename CallbackFunc>
592void setHostName(const std::string &newHostname, CallbackFunc &&callback)
593{
594 crow::connections::systemBus->async_method_call(
595 callback, "xyz.openbmc_project.Network",
596 "/xyz/openbmc_project/network/config",
597 "org.freedesktop.DBus.Properties", "Set",
598 "xyz.openbmc_project.Network.SystemConfiguration", "HostName",
599 sdbusplus::message::variant<std::string>(newHostname));
600}
601
602/**
603 * @brief Deletes given IPv4
604 *
605 * @param[in] ifaceId Id of interface whose IP should be deleted
606 * @param[in] ipIdx Index of IP in input array that should be deleted
607 * @param[in] ipHash DBus Hash id of IP that should be deleted
608 * @param[io] asyncResp Response object that will be returned to client
609 *
610 * @return None
611 */
612inline void deleteIPv4(const std::string &ifaceId, const std::string &ipHash,
613 unsigned int ipIdx,
614 const std::shared_ptr<AsyncResp> asyncResp)
615{
616 crow::connections::systemBus->async_method_call(
617 [ipIdx, asyncResp](const boost::system::error_code ec) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 if (ec)
619 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800620 messages::internalError(asyncResp->res);
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100621 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700622 else
623 {
624 asyncResp->res.jsonValue["IPv4Addresses"][ipIdx] = nullptr;
625 }
626 },
627 "xyz.openbmc_project.Network",
628 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" + ipHash,
629 "xyz.openbmc_project.Object.Delete", "Delete");
630}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631
Ed Tanous4a0cb852018-10-15 07:55:04 -0700632/**
633 * @brief Creates IPv4 with given data
634 *
635 * @param[in] ifaceId Id of interface whose IP should be deleted
636 * @param[in] ipIdx Index of IP in input array that should be deleted
637 * @param[in] ipHash DBus Hash id of IP that should be deleted
638 * @param[io] asyncResp Response object that will be returned to client
639 *
640 * @return None
641 */
642inline void createIPv4(const std::string &ifaceId, unsigned int ipIdx,
643 uint8_t subnetMask, const std::string &gateway,
644 const std::string &address,
645 std::shared_ptr<AsyncResp> asyncResp)
646{
647 auto createIpHandler = [ipIdx,
648 asyncResp](const boost::system::error_code ec) {
649 if (ec)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700650 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800651 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700652 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 };
654
Ed Tanous4a0cb852018-10-15 07:55:04 -0700655 crow::connections::systemBus->async_method_call(
656 std::move(createIpHandler), "xyz.openbmc_project.Network",
657 "/xyz/openbmc_project/network/" + ifaceId,
658 "xyz.openbmc_project.Network.IP.Create", "IP",
659 "xyz.openbmc_project.Network.IP.Protocol.IPv4", address, subnetMask,
660 gateway);
661}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662
Ed Tanous4a0cb852018-10-15 07:55:04 -0700663/**
664 * Function that retrieves all properties for given Ethernet Interface
665 * Object
666 * from EntityManager Network Manager
667 * @param ethiface_id a eth interface id to query on DBus
668 * @param callback a function that shall be called to convert Dbus output
669 * into JSON
670 */
671template <typename CallbackFunc>
672void getEthernetIfaceData(const std::string &ethiface_id,
673 CallbackFunc &&callback)
674{
675 crow::connections::systemBus->async_method_call(
676 [ethiface_id{std::string{ethiface_id}}, callback{std::move(callback)}](
677 const boost::system::error_code error_code,
678 const GetManagedObjects &resp) {
679 EthernetInterfaceData ethData{};
680 boost::container::flat_set<IPv4AddressData> ipv4Data;
681
682 if (error_code)
683 {
684 callback(false, ethData, ipv4Data);
685 return;
686 }
687
688 extractEthernetInterfaceData(ethiface_id, resp, ethData);
689 extractIPData(ethiface_id, resp, ipv4Data);
690
691 // Fix global GW
692 for (IPv4AddressData &ipv4 : ipv4Data)
693 {
694 if ((ipv4.linktype == LinkType::Global) &&
695 (ipv4.gateway == "0.0.0.0"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700697 ipv4.gateway = ethData.default_gateway;
698 }
699 }
700
701 // Finally make a callback with usefull data
702 callback(true, ethData, ipv4Data);
703 },
704 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
705 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
706};
707
708/**
709 * Function that retrieves all Ethernet Interfaces available through Network
710 * Manager
711 * @param callback a function that shall be called to convert Dbus output
712 * into JSON.
713 */
714template <typename CallbackFunc>
715void getEthernetIfaceList(CallbackFunc &&callback)
716{
717 crow::connections::systemBus->async_method_call(
718 [callback{std::move(callback)}](
719 const boost::system::error_code error_code,
720 GetManagedObjects &resp) {
721 // Callback requires vector<string> to retrieve all available
722 // ethernet interfaces
723 std::vector<std::string> iface_list;
724 iface_list.reserve(resp.size());
725 if (error_code)
726 {
727 callback(false, iface_list);
728 return;
729 }
730
731 // Iterate over all retrieved ObjectPaths.
732 for (const auto &objpath : resp)
733 {
734 // And all interfaces available for certain ObjectPath.
735 for (const auto &interface : objpath.second)
736 {
737 // If interface is
738 // xyz.openbmc_project.Network.EthernetInterface, this is
739 // what we're looking for.
740 if (interface.first ==
741 "xyz.openbmc_project.Network.EthernetInterface")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700742 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700743 // Cut out everyting until last "/", ...
744 const std::string &iface_id = objpath.first.str;
745 std::size_t last_pos = iface_id.rfind("/");
746 if (last_pos != std::string::npos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700747 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700748 // and put it into output vector.
749 iface_list.emplace_back(
750 iface_id.substr(last_pos + 1));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700751 }
752 }
753 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700754 }
755 // Finally make a callback with useful data
756 callback(true, iface_list);
757 },
758 "xyz.openbmc_project.Network", "/xyz/openbmc_project/network",
759 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100760};
761
762/**
763 * EthernetCollection derived class for delivering Ethernet Collection Schema
764 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765class EthernetCollection : public Node
766{
767 public:
Ed Tanous4a0cb852018-10-15 07:55:04 -0700768 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 EthernetCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700770 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700771 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700772 entityPrivileges = {
773 {boost::beast::http::verb::get, {{"Login"}}},
774 {boost::beast::http::verb::head, {{"Login"}}},
775 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
776 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
777 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
778 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
779 }
780
781 private:
782 /**
783 * Functions triggers appropriate requests on DBus
784 */
785 void doGet(crow::Response &res, const crow::Request &req,
786 const std::vector<std::string> &params) override
787 {
Ed Tanous0f74e642018-11-12 15:17:05 -0800788 res.jsonValue["@odata.type"] =
789 "#EthernetInterfaceCollection.EthernetInterfaceCollection";
790 res.jsonValue["@odata.context"] =
791 "/redfish/v1/"
792 "$metadata#EthernetInterfaceCollection.EthernetInterfaceCollection";
793 res.jsonValue["@odata.id"] =
794 "/redfish/v1/Managers/bmc/EthernetInterfaces";
795 res.jsonValue["Name"] = "Ethernet Network Interface Collection";
796 res.jsonValue["Description"] =
797 "Collection of EthernetInterfaces for this Manager";
798
Ed Tanous4a0cb852018-10-15 07:55:04 -0700799 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -0700801 getEthernetIfaceList(
802 [&res](const bool &success,
803 const std::vector<std::string> &iface_list) {
804 if (!success)
805 {
806 messages::internalError(res);
807 res.end();
808 return;
809 }
810
811 nlohmann::json &iface_array = res.jsonValue["Members"];
812 iface_array = nlohmann::json::array();
813 for (const std::string &iface_item : iface_list)
814 {
815 iface_array.push_back(
816 {{"@odata.id",
817 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
818 iface_item}});
819 }
820
821 res.jsonValue["Members@odata.count"] = iface_array.size();
822 res.jsonValue["@odata.id"] =
823 "/redfish/v1/Managers/bmc/EthernetInterfaces";
Ed Tanous1abe55e2018-09-05 08:30:59 -0700824 res.end();
Jason M. Billsf12894f2018-10-09 12:45:45 -0700825 });
Ed Tanous4a0cb852018-10-15 07:55:04 -0700826 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +0100827};
828
829/**
830 * EthernetInterface derived class for delivering Ethernet Schema
831 */
Ed Tanous1abe55e2018-09-05 08:30:59 -0700832class EthernetInterface : public Node
833{
834 public:
835 /*
836 * Default Constructor
837 */
Ed Tanous4a0cb852018-10-15 07:55:04 -0700838 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839 EthernetInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -0700840 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700841 std::string())
842 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700843 entityPrivileges = {
844 {boost::beast::http::verb::get, {{"Login"}}},
845 {boost::beast::http::verb::head, {{"Login"}}},
846 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
847 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
848 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
849 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamil588c3f02018-04-03 14:55:27 +0200850 }
851
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 // TODO(kkowalsk) Find a suitable class/namespace for this
Ed Tanous0627a2c2018-11-29 17:09:23 -0800853 static void handleVlanPatch(const std::string &ifaceId, bool vlanEnable,
854 uint64_t vlanId,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700855 const EthernetInterfaceData &ethData,
856 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700858 if (!ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700859 {
860 // This interface is not a VLAN. Cannot do anything with it
861 // TODO(kkowalsk) Change this message
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800862 messages::propertyNotWritable(asyncResp->res, "VLANEnable");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 return;
865 }
866
867 // VLAN is configured on the interface
Ed Tanous0627a2c2018-11-29 17:09:23 -0800868 if (vlanEnable == true)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700869 {
870 // Change VLAN Id
Ed Tanous0627a2c2018-11-29 17:09:23 -0800871 asyncResp->res.jsonValue["VLANId"] = vlanId;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700872 auto callback = [asyncResp](const boost::system::error_code ec) {
873 if (ec)
874 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700875 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700876 }
877 else
878 {
879 asyncResp->res.jsonValue["VLANEnable"] = true;
880 }
881 };
882 crow::connections::systemBus->async_method_call(
883 std::move(callback), "xyz.openbmc_project.Network",
884 "/xyz/openbmc_project/network/" + ifaceId,
885 "org.freedesktop.DBus.Properties", "Set",
886 "xyz.openbmc_project.Network.VLAN", "Id",
Ed Tanous0627a2c2018-11-29 17:09:23 -0800887 sdbusplus::message::variant<uint32_t>(vlanId));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700889 else
Ed Tanous1abe55e2018-09-05 08:30:59 -0700890 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700891 auto callback = [asyncResp](const boost::system::error_code ec) {
892 if (ec)
893 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700894 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -0700895 return;
896 }
897 asyncResp->res.jsonValue["VLANEnable"] = false;
898 };
899
900 crow::connections::systemBus->async_method_call(
901 std::move(callback), "xyz.openbmc_project.Network",
902 "/xyz/openbmc_project/network/" + ifaceId,
903 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700904 }
905 }
906
907 private:
908 void handleHostnamePatch(const nlohmann::json &input,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700909 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700911 const std::string *newHostname = input.get_ptr<const std::string *>();
912 if (newHostname == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700914 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800915 "HostName");
Ed Tanous4a0cb852018-10-15 07:55:04 -0700916 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700917 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700918
919 // Change hostname
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800920 setHostName(*newHostname,
921 [asyncResp, newHostname{std::string(*newHostname)}](
922 const boost::system::error_code ec) {
923 if (ec)
924 {
925 messages::internalError(asyncResp->res);
926 }
927 else
928 {
929 asyncResp->res.jsonValue["HostName"] = newHostname;
930 }
931 });
Ed Tanous1abe55e2018-09-05 08:30:59 -0700932 }
933
Ed Tanous4a0cb852018-10-15 07:55:04 -0700934 void handleIPv4Patch(
935 const std::string &ifaceId, const nlohmann::json &input,
936 const boost::container::flat_set<IPv4AddressData> &ipv4Data,
937 const std::shared_ptr<AsyncResp> asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938 {
939 if (!input.is_array())
940 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700941 messages::propertyValueTypeError(asyncResp->res, input.dump(),
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800942 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 return;
944 }
945
946 // According to Redfish PATCH definition, size must be at least equal
Ed Tanous4a0cb852018-10-15 07:55:04 -0700947 if (input.size() < ipv4Data.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700948 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800949 messages::propertyValueFormatError(asyncResp->res, input.dump(),
950 "IPv4Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 return;
952 }
953
Ed Tanous4a0cb852018-10-15 07:55:04 -0700954 int entryIdx = 0;
955 boost::container::flat_set<IPv4AddressData>::const_iterator thisData =
956 ipv4Data.begin();
957 for (const nlohmann::json &thisJson : input)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700959 std::string pathString =
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800960 "IPv4Addresses/" + std::to_string(entryIdx);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 // Check that entry is not of some unexpected type
Ed Tanous4a0cb852018-10-15 07:55:04 -0700962 if (!thisJson.is_object() && !thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700963 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800964 messages::propertyValueTypeError(asyncResp->res,
965 thisJson.dump(),
966 pathString + "/IPv4Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700967
968 continue;
969 }
970
Ed Tanous4a0cb852018-10-15 07:55:04 -0700971 nlohmann::json::const_iterator addressFieldIt =
972 thisJson.find("Address");
973 const std::string *addressField = nullptr;
974 if (addressFieldIt != thisJson.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700975 {
Ed Tanous4a0cb852018-10-15 07:55:04 -0700976 addressField = addressFieldIt->get_ptr<const std::string *>();
977 if (addressField == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800979 messages::propertyValueFormatError(asyncResp->res,
980 addressFieldIt->dump(),
981 pathString + "/Address");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700982 continue;
983 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700984 else
985 {
986 if (!ipv4VerifyIpAndGetBitcount(*addressField))
987 {
Jason M. Billsf12894f2018-10-09 12:45:45 -0700988 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -0800989 asyncResp->res, *addressField,
Ed Tanous4a0cb852018-10-15 07:55:04 -0700990 pathString + "/Address");
991 continue;
992 }
993 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994 }
Ed Tanous4a0cb852018-10-15 07:55:04 -0700995
Ed Tanousa24526d2018-12-10 15:17:59 -0800996 std::optional<uint8_t> prefixLength;
Ed Tanous4a0cb852018-10-15 07:55:04 -0700997 const std::string *subnetField = nullptr;
998 nlohmann::json::const_iterator subnetFieldIt =
999 thisJson.find("SubnetMask");
1000 if (subnetFieldIt != thisJson.end())
1001 {
1002 subnetField = subnetFieldIt->get_ptr<const std::string *>();
1003 if (subnetField == nullptr)
1004 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001005 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001006 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001007 pathString + "/SubnetMask");
1008 continue;
1009 }
1010 else
1011 {
1012 prefixLength = 0;
1013 if (!ipv4VerifyIpAndGetBitcount(*subnetField,
1014 &*prefixLength))
1015 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001016 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001017 asyncResp->res, *subnetField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001018 pathString + "/SubnetMask");
1019 continue;
1020 }
1021 }
1022 }
1023
1024 std::string addressOriginInDBusFormat;
1025 const std::string *addressOriginField = nullptr;
1026 nlohmann::json::const_iterator addressOriginFieldIt =
1027 thisJson.find("AddressOrigin");
1028 if (addressOriginFieldIt != thisJson.end())
1029 {
1030 const std::string *addressOriginField =
1031 addressOriginFieldIt->get_ptr<const std::string *>();
1032 if (addressOriginField == nullptr)
1033 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001034 messages::propertyValueFormatError(
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001035 asyncResp->res, *addressOriginField,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001036 pathString + "/AddressOrigin");
1037 continue;
1038 }
1039 else
1040 {
1041 // Get Address origin in proper format
1042 addressOriginInDBusFormat =
1043 translateAddressOriginRedfishToDbus(
1044 *addressOriginField);
1045 if (addressOriginInDBusFormat.empty())
1046 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001047 messages::propertyValueNotInList(
1048 asyncResp->res, *addressOriginField,
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001049 pathString + "/AddressOrigin");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001050 continue;
1051 }
1052 }
1053 }
1054
1055 nlohmann::json::const_iterator gatewayFieldIt =
1056 thisJson.find("Gateway");
1057 const std::string *gatewayField = nullptr;
1058 if (gatewayFieldIt != thisJson.end())
1059 {
1060 const std::string *gatewayField =
1061 gatewayFieldIt->get_ptr<const std::string *>();
1062 if (gatewayField == nullptr ||
1063 !ipv4VerifyIpAndGetBitcount(*gatewayField))
1064 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001065 messages::propertyValueFormatError(
1066 asyncResp->res, *gatewayField, pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001067 continue;
1068 }
1069 }
1070
1071 // if a vlan already exists, modify the existing
1072 if (thisData != ipv4Data.end())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 {
1074 // Existing object that should be modified/deleted/remain
1075 // unchanged
Ed Tanous4a0cb852018-10-15 07:55:04 -07001076 if (thisJson.is_null())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001078 auto callback = [entryIdx{std::to_string(entryIdx)},
1079 asyncResp](
1080 const boost::system::error_code ec) {
1081 if (ec)
1082 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001083 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001084 return;
1085 }
1086 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] =
1087 nullptr;
1088 };
1089 crow::connections::systemBus->async_method_call(
1090 std::move(callback), "xyz.openbmc_project.Network",
1091 "/xyz/openbmc_project/network/" + ifaceId + "/ipv4/" +
1092 thisData->id,
1093 "xyz.openbmc_project.Object.Delete", "Delete");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001095 else if (thisJson.is_object())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001096 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 // Apply changes
Ed Tanous4a0cb852018-10-15 07:55:04 -07001098 if (addressField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001100 auto callback =
1101 [asyncResp, entryIdx,
1102 addressField{std::string(*addressField)}](
1103 const boost::system::error_code ec) {
1104 if (ec)
1105 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001106 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001107 return;
1108 }
1109 asyncResp->res
1110 .jsonValue["IPv4Addresses"][std::to_string(
1111 entryIdx)]["Address"] = addressField;
1112 };
1113
1114 crow::connections::systemBus->async_method_call(
1115 std::move(callback), "xyz.openbmc_project.Network",
1116 "/xyz/openbmc_project/network/" + ifaceId +
1117 "/ipv4/" + thisData->id,
1118 "org.freedesktop.DBus.Properties", "Set",
1119 "xyz.openbmc_project.Network.IP", "Address",
1120 sdbusplus::message::variant<std::string>(
1121 *addressField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001122 }
1123
Ed Tanous4a0cb852018-10-15 07:55:04 -07001124 if (prefixLength && subnetField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001125 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001126 changeIPv4SubnetMaskProperty(ifaceId, entryIdx,
1127 thisData->id, *subnetField,
1128 *prefixLength, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001129 }
1130
Ed Tanous4a0cb852018-10-15 07:55:04 -07001131 if (!addressOriginInDBusFormat.empty() &&
1132 addressOriginField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001134 changeIPv4Origin(ifaceId, entryIdx, thisData->id,
1135 *addressOriginField,
1136 addressOriginInDBusFormat, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001137 }
1138
Ed Tanous4a0cb852018-10-15 07:55:04 -07001139 if (gatewayField != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001140 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001141 auto callback =
1142 [asyncResp, entryIdx,
1143 gatewayField{std::string(*gatewayField)}](
1144 const boost::system::error_code ec) {
1145 if (ec)
1146 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001147 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001148 return;
1149 }
1150 asyncResp->res
1151 .jsonValue["IPv4Addresses"][std::to_string(
1152 entryIdx)]["Gateway"] =
1153 std::move(gatewayField);
1154 };
1155
1156 crow::connections::systemBus->async_method_call(
1157 std::move(callback), "xyz.openbmc_project.Network",
1158 "/xyz/openbmc_project/network/" + ifaceId +
1159 "/ipv4/" + thisData->id,
1160 "org.freedesktop.DBus.Properties", "Set",
1161 "xyz.openbmc_project.Network.IP", "Gateway",
1162 sdbusplus::message::variant<std::string>(
1163 *gatewayField));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164 }
1165 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001166 thisData++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001167 }
Ed Tanous4a0cb852018-10-15 07:55:04 -07001168 else
1169 {
1170 // Create IPv4 with provided data
1171 if (gatewayField == nullptr)
1172 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001173 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001174 pathString + "/Gateway");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001175 continue;
1176 }
1177
1178 if (addressField == nullptr)
1179 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001180 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001181 pathString + "/Address");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001182 continue;
1183 }
1184
1185 if (!prefixLength)
1186 {
Jason M. Billsa08b46c2018-11-06 15:01:08 -08001187 messages::propertyMissing(asyncResp->res,
Jason M. Billsf12894f2018-10-09 12:45:45 -07001188 pathString + "/SubnetMask");
Ed Tanous4a0cb852018-10-15 07:55:04 -07001189 continue;
1190 }
1191
1192 createIPv4(ifaceId, entryIdx, *prefixLength, *gatewayField,
1193 *addressField, asyncResp);
1194 asyncResp->res.jsonValue["IPv4Addresses"][entryIdx] = thisJson;
1195 }
1196 entryIdx++;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001197 }
1198 }
1199
Ed Tanous0f74e642018-11-12 15:17:05 -08001200 void parseInterfaceData(
1201 nlohmann::json &json_response, const std::string &iface_id,
1202 const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001203 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001204 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001205 json_response["Id"] = iface_id;
1206 json_response["@odata.id"] =
1207 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208
Ed Tanous4a0cb852018-10-15 07:55:04 -07001209 json_response["SpeedMbps"] = ethData.speed;
1210 json_response["MACAddress"] = ethData.mac_address;
1211 if (!ethData.hostname.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001212 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001213 json_response["HostName"] = ethData.hostname;
1214 }
1215
1216 nlohmann::json &vlanObj = json_response["VLAN"];
1217 if (ethData.vlan_id)
1218 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001219 vlanObj["VLANEnable"] = true;
Ed Tanous4a0cb852018-10-15 07:55:04 -07001220 vlanObj["VLANId"] = *ethData.vlan_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001221 }
1222 else
1223 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001224 vlanObj["VLANEnable"] = false;
1225 vlanObj["VLANId"] = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001226 }
1227
Ed Tanous4a0cb852018-10-15 07:55:04 -07001228 if (ipv4Data.size() > 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001229 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001230 nlohmann::json &ipv4_array = json_response["IPv4Addresses"];
1231 ipv4_array = nlohmann::json::array();
1232 for (auto &ipv4_config : ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001233 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001234 if (!ipv4_config.address.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001235 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001236 ipv4_array.push_back({{"AddressOrigin", ipv4_config.origin},
1237 {"SubnetMask", ipv4_config.netmask},
1238 {"Address", ipv4_config.address}});
Ed Tanous1abe55e2018-09-05 08:30:59 -07001239
Ed Tanous4a0cb852018-10-15 07:55:04 -07001240 if (!ipv4_config.gateway.empty())
1241 {
1242 ipv4_array.back()["Gateway"] = ipv4_config.gateway;
1243 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001244 }
1245 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001246 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001247 }
1248
1249 /**
1250 * Functions triggers appropriate requests on DBus
1251 */
1252 void doGet(crow::Response &res, const crow::Request &req,
1253 const std::vector<std::string> &params) override
1254 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001255 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001256 if (params.size() != 1)
1257 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001258 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001259 return;
1260 }
1261
Ed Tanous4a0cb852018-10-15 07:55:04 -07001262 getEthernetIfaceData(
1263 params[0],
1264 [this, asyncResp, iface_id{std::string(params[0])}](
1265 const bool &success, const EthernetInterfaceData &ethData,
1266 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1267 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001268 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001269 // TODO(Pawel)consider distinguish between non existing
1270 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001271 messages::resourceNotFound(asyncResp->res,
1272 "EthernetInterface", iface_id);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001273 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001275 asyncResp->res.jsonValue["@odata.type"] =
1276 "#EthernetInterface.v1_2_0.EthernetInterface";
1277 asyncResp->res.jsonValue["@odata.context"] =
1278 "/redfish/v1/$metadata#EthernetInterface.EthernetInterface";
1279 asyncResp->res.jsonValue["Name"] = "Manager Ethernet Interface";
1280 asyncResp->res.jsonValue["Description"] =
1281 "Management Network Interface";
1282
1283 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1284 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001285 });
1286 }
1287
1288 void doPatch(crow::Response &res, const crow::Request &req,
1289 const std::vector<std::string> &params) override
1290 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001291 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001292 if (params.size() != 1)
1293 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001294 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001295 return;
1296 }
1297
Ed Tanous4a0cb852018-10-15 07:55:04 -07001298 const std::string &iface_id = params[0];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001299
Ed Tanous0627a2c2018-11-29 17:09:23 -08001300 std::optional<nlohmann::json> vlan;
1301 std::optional<nlohmann::json> hostname;
1302 std::optional<nlohmann::json> ipv4Addresses;
1303 std::optional<nlohmann::json> ipv6Addresses;
1304
1305 if (!json_util::readJson(req, res, "VLAN", vlan, "HostName", hostname,
1306 "IPv4Addresses", ipv4Addresses,
1307 "IPv6Addresses", ipv6Addresses))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001308 {
1309 return;
1310 }
Ed Tanous0627a2c2018-11-29 17:09:23 -08001311 std::optional<uint64_t> vlanId = 0;
1312 std::optional<bool> vlanEnable = false;
1313 if (vlan)
1314 {
1315 if (!json_util::readJson(*vlan, res, "VLANEnable", vlanEnable,
1316 "VLANId", vlanId))
1317 {
1318 return;
1319 }
1320 // Need both vlanId and vlanEnable to service this request
1321 if (static_cast<bool>(vlanId) ^ static_cast<bool>(vlanEnable))
1322 {
1323 if (vlanId)
1324 {
1325 messages::propertyMissing(asyncResp->res, "VLANEnable");
1326 }
1327 else
1328 {
1329 messages::propertyMissing(asyncResp->res, "VLANId");
1330 }
1331
1332 return;
1333 }
1334 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001335
Ed Tanous4a0cb852018-10-15 07:55:04 -07001336 // Get single eth interface data, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001337 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001338 getEthernetIfaceData(
1339 iface_id,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001340 [this, asyncResp, iface_id, vlanId, vlanEnable,
1341 hostname = std::move(hostname),
1342 ipv4Addresses = std::move(ipv4Addresses),
1343 ipv6Addresses = std::move(ipv6Addresses)](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001344 const bool &success, const EthernetInterfaceData &ethData,
1345 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001346 if (!success)
1347 {
1348 // ... otherwise return error
1349 // TODO(Pawel)consider distinguish between non existing
1350 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001351 messages::resourceNotFound(
1352 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001353 return;
1354 }
1355
Ed Tanous0f74e642018-11-12 15:17:05 -08001356 parseInterfaceData(asyncResp->res.jsonValue, iface_id, ethData,
1357 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001358
Ed Tanous0627a2c2018-11-29 17:09:23 -08001359 if (vlanId && vlanEnable)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001360 {
Ed Tanous0627a2c2018-11-29 17:09:23 -08001361 handleVlanPatch(iface_id, *vlanId, *vlanEnable, ethData,
1362 asyncResp);
1363 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001364
Ed Tanous0627a2c2018-11-29 17:09:23 -08001365 if (hostname)
1366 {
1367 handleHostnamePatch(*hostname, asyncResp);
1368 }
1369
1370 if (ipv4Addresses)
1371 {
1372 handleIPv4Patch(iface_id, *ipv4Addresses, ipv4Data,
1373 asyncResp);
1374 }
1375
1376 if (ipv6Addresses)
1377 {
1378 // TODO(kkowalsk) IPv6 Not supported on D-Bus yet
1379 messages::propertyNotWritable(asyncResp->res,
1380 "IPv6Addresses");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001381 }
1382 });
1383 }
Rapkiewicz, Pawel9391bb92018-03-20 03:12:18 +01001384};
1385
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001386/**
Ed Tanous4a0cb852018-10-15 07:55:04 -07001387 * VlanNetworkInterface derived class for delivering VLANNetworkInterface
1388 * Schema
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001389 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001390class VlanNetworkInterface : public Node
1391{
1392 public:
1393 /*
1394 * Default Constructor
1395 */
1396 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001397 VlanNetworkInterface(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001398 Node(app,
Ed Tanous0f74e642018-11-12 15:17:05 -08001399 "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/<str>",
Ed Tanous4a0cb852018-10-15 07:55:04 -07001400 std::string(), std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001401 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001402 entityPrivileges = {
1403 {boost::beast::http::verb::get, {{"Login"}}},
1404 {boost::beast::http::verb::head, {{"Login"}}},
1405 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1406 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1407 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1408 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001409 }
1410
Ed Tanous1abe55e2018-09-05 08:30:59 -07001411 private:
Ed Tanous0f74e642018-11-12 15:17:05 -08001412 void parseInterfaceData(
1413 nlohmann::json &json_response, const std::string &parent_iface_id,
1414 const std::string &iface_id, const EthernetInterfaceData &ethData,
Ed Tanous4a0cb852018-10-15 07:55:04 -07001415 const boost::container::flat_set<IPv4AddressData> &ipv4Data)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001416 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001417 // Fill out obvious data...
Ed Tanous4a0cb852018-10-15 07:55:04 -07001418 json_response["Id"] = iface_id;
1419 json_response["@odata.id"] =
1420 "/redfish/v1/Managers/bmc/EthernetInterfaces/" + parent_iface_id +
1421 "/VLANs/" + iface_id;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001422
Ed Tanous4a0cb852018-10-15 07:55:04 -07001423 json_response["VLANEnable"] = true;
1424 if (ethData.vlan_id)
1425 {
1426 json_response["VLANId"] = *ethData.vlan_id;
1427 }
Ed Tanousa434f2b2018-07-27 13:04:22 -07001428 }
1429
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430 bool verifyNames(crow::Response &res, const std::string &parent,
1431 const std::string &iface)
1432 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001433 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001434 if (!boost::starts_with(iface, parent + "_"))
1435 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001436 messages::resourceNotFound(asyncResp->res, "VLAN Network Interface",
1437 iface);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001438 return false;
1439 }
1440 else
1441 {
1442 return true;
1443 }
1444 }
1445
1446 /**
1447 * Functions triggers appropriate requests on DBus
1448 */
1449 void doGet(crow::Response &res, const crow::Request &req,
1450 const std::vector<std::string> &params) override
1451 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001452 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
1453 // TODO(Pawel) this shall be parameterized call (two params) to get
Ed Tanous1abe55e2018-09-05 08:30:59 -07001454 // EthernetInterfaces for any Manager, not only hardcoded 'openbmc'.
1455 // Check if there is required param, truly entering this shall be
1456 // impossible.
1457 if (params.size() != 2)
1458 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001459 messages::internalError(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001460 res.end();
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001461 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001462 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001463
Ed Tanous4a0cb852018-10-15 07:55:04 -07001464 const std::string &parent_iface_id = params[0];
1465 const std::string &iface_id = params[1];
Ed Tanous0f74e642018-11-12 15:17:05 -08001466 res.jsonValue["@odata.type"] =
1467 "#VLanNetworkInterface.v1_1_0.VLanNetworkInterface";
1468 res.jsonValue["@odata.context"] =
1469 "/redfish/v1/$metadata#VLanNetworkInterface.VLanNetworkInterface";
1470 res.jsonValue["Name"] = "VLAN Network Interface";
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001471
Ed Tanous4a0cb852018-10-15 07:55:04 -07001472 if (!verifyNames(res, parent_iface_id, iface_id))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001473 {
1474 return;
1475 }
Kowalski, Kamil927a5052018-07-03 14:16:46 +02001476
Ed Tanous1abe55e2018-09-05 08:30:59 -07001477 // Get single eth interface data, and call the below callback for JSON
1478 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001479 getEthernetIfaceData(
1480 iface_id,
1481 [this, asyncResp, parent_iface_id, iface_id](
1482 const bool &success, const EthernetInterfaceData &ethData,
1483 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1484 if (success && ethData.vlan_id)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001485 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001486 parseInterfaceData(asyncResp->res.jsonValue,
1487 parent_iface_id, iface_id, ethData,
1488 ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001489 }
1490 else
1491 {
1492 // ... otherwise return error
1493 // TODO(Pawel)consider distinguish between non existing
1494 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001495 messages::resourceNotFound(
1496 asyncResp->res, "VLAN Network Interface", iface_id);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001497 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001498 });
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001499 }
1500
Ed Tanous1abe55e2018-09-05 08:30:59 -07001501 void doPatch(crow::Response &res, const crow::Request &req,
1502 const std::vector<std::string> &params) override
1503 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001504 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001505 if (params.size() != 2)
1506 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001507 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001508 return;
1509 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001510
Ed Tanous1abe55e2018-09-05 08:30:59 -07001511 const std::string &parentIfaceId = params[0];
1512 const std::string &ifaceId = params[1];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001513
Ed Tanous1abe55e2018-09-05 08:30:59 -07001514 if (!verifyNames(res, parentIfaceId, ifaceId))
1515 {
1516 return;
1517 }
1518
Ed Tanous0627a2c2018-11-29 17:09:23 -08001519 bool vlanEnable = false;
1520 uint64_t vlanId = 0;
1521
1522 if (!json_util::readJson(req, res, "VLANEnable", vlanEnable, "VLANId",
1523 vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001524 {
1525 return;
1526 }
1527
1528 // Get single eth interface data, and call the below callback for JSON
1529 // preparation
Ed Tanous4a0cb852018-10-15 07:55:04 -07001530 getEthernetIfaceData(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001531 ifaceId,
Ed Tanous0627a2c2018-11-29 17:09:23 -08001532 [this, asyncResp, parentIfaceId, ifaceId, vlanEnable, vlanId](
Ed Tanous4a0cb852018-10-15 07:55:04 -07001533 const bool &success, const EthernetInterfaceData &ethData,
1534 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001535 if (!success)
1536 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001537 // TODO(Pawel)consider distinguish between non existing
1538 // object, and other errors
Jason M. Billsf12894f2018-10-09 12:45:45 -07001539 messages::resourceNotFound(
1540 asyncResp->res, "VLAN Network Interface", ifaceId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001541
1542 return;
1543 }
1544
Ed Tanous0f74e642018-11-12 15:17:05 -08001545 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1546 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001547
Ed Tanous0627a2c2018-11-29 17:09:23 -08001548 EthernetInterface::handleVlanPatch(ifaceId, vlanId, vlanEnable,
1549 ethData, asyncResp);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001550 });
1551 }
1552
1553 void doDelete(crow::Response &res, const crow::Request &req,
1554 const std::vector<std::string> &params) override
1555 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001556 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001557 if (params.size() != 2)
1558 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001559 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001560 return;
1561 }
1562
1563 const std::string &parentIfaceId = params[0];
1564 const std::string &ifaceId = params[1];
1565
Ed Tanous4a0cb852018-10-15 07:55:04 -07001566 if (!verifyNames(asyncResp->res, parentIfaceId, ifaceId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001567 {
1568 return;
1569 }
1570
1571 // Get single eth interface data, and call the below callback for JSON
1572 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001573 getEthernetIfaceData(
1574 ifaceId,
1575 [this, asyncResp, parentIfaceId{std::string(parentIfaceId)},
1576 ifaceId{std::string(ifaceId)}](
1577 const bool &success, const EthernetInterfaceData &ethData,
1578 const boost::container::flat_set<IPv4AddressData> &ipv4Data) {
1579 if (success && ethData.vlan_id)
1580 {
Ed Tanous0f74e642018-11-12 15:17:05 -08001581 parseInterfaceData(asyncResp->res.jsonValue, parentIfaceId,
1582 ifaceId, ethData, ipv4Data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001583
Jason M. Billsf12894f2018-10-09 12:45:45 -07001584 auto callback =
1585 [asyncResp](const boost::system::error_code ec) {
1586 if (ec)
1587 {
1588 messages::internalError(asyncResp->res);
1589 }
1590 };
1591 crow::connections::systemBus->async_method_call(
1592 std::move(callback), "xyz.openbmc_project.Network",
1593 std::string("/xyz/openbmc_project/network/") + ifaceId,
1594 "xyz.openbmc_project.Object.Delete", "Delete");
1595 }
1596 else
1597 {
1598 // ... otherwise return error
1599 // TODO(Pawel)consider distinguish between non existing
1600 // object, and other errors
1601 messages::resourceNotFound(
1602 asyncResp->res, "VLAN Network Interface", ifaceId);
1603 }
1604 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001605 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001606};
1607
1608/**
1609 * VlanNetworkInterfaceCollection derived class for delivering
1610 * VLANNetworkInterface Collection Schema
1611 */
Ed Tanous1abe55e2018-09-05 08:30:59 -07001612class VlanNetworkInterfaceCollection : public Node
1613{
1614 public:
1615 template <typename CrowApp>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001616 VlanNetworkInterfaceCollection(CrowApp &app) :
Ed Tanous4a0cb852018-10-15 07:55:04 -07001617 Node(app, "/redfish/v1/Managers/bmc/EthernetInterfaces/<str>/VLANs/",
1618 std::string())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001619 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001620 entityPrivileges = {
1621 {boost::beast::http::verb::get, {{"Login"}}},
1622 {boost::beast::http::verb::head, {{"Login"}}},
1623 {boost::beast::http::verb::patch, {{"ConfigureComponents"}}},
1624 {boost::beast::http::verb::put, {{"ConfigureComponents"}}},
1625 {boost::beast::http::verb::delete_, {{"ConfigureComponents"}}},
1626 {boost::beast::http::verb::post, {{"ConfigureComponents"}}}};
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001627 }
1628
Ed Tanous1abe55e2018-09-05 08:30:59 -07001629 private:
1630 /**
1631 * Functions triggers appropriate requests on DBus
1632 */
1633 void doGet(crow::Response &res, const crow::Request &req,
1634 const std::vector<std::string> &params) override
1635 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001636 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001637 if (params.size() != 1)
1638 {
1639 // This means there is a problem with the router
Jason M. Billsf12894f2018-10-09 12:45:45 -07001640 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001641 return;
Ed Tanous8ceb2ec2018-08-13 11:11:56 -07001642 }
1643
Ed Tanous4a0cb852018-10-15 07:55:04 -07001644 const std::string &rootInterfaceName = params[0];
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001645
Ed Tanous4a0cb852018-10-15 07:55:04 -07001646 // Get eth interface list, and call the below callback for JSON
Ed Tanous1abe55e2018-09-05 08:30:59 -07001647 // preparation
Jason M. Billsf12894f2018-10-09 12:45:45 -07001648 getEthernetIfaceList(
1649 [this, asyncResp,
1650 rootInterfaceName{std::string(rootInterfaceName)}](
1651 const bool &success,
1652 const std::vector<std::string> &iface_list) {
1653 if (!success)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001654 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001655 messages::internalError(asyncResp->res);
1656 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001657 }
Ed Tanous0f74e642018-11-12 15:17:05 -08001658 asyncResp->res.jsonValue["@odata.type"] =
1659 "#VLanNetworkInterfaceCollection."
1660 "VLanNetworkInterfaceCollection";
1661 asyncResp->res.jsonValue["@odata.context"] =
1662 "/redfish/v1/$metadata"
1663 "#VLanNetworkInterfaceCollection."
1664 "VLanNetworkInterfaceCollection";
1665 asyncResp->res.jsonValue["Name"] =
1666 "VLAN Network Interface Collection";
Ed Tanous4a0cb852018-10-15 07:55:04 -07001667
Jason M. Billsf12894f2018-10-09 12:45:45 -07001668 nlohmann::json iface_array = nlohmann::json::array();
1669
1670 for (const std::string &iface_item : iface_list)
1671 {
1672 if (boost::starts_with(iface_item, rootInterfaceName + "_"))
1673 {
1674 iface_array.push_back(
1675 {{"@odata.id",
1676 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1677 rootInterfaceName + "/VLANs/" + iface_item}});
1678 }
1679 }
1680
1681 if (iface_array.empty())
1682 {
1683 messages::resourceNotFound(
1684 asyncResp->res, "EthernetInterface", rootInterfaceName);
1685 return;
1686 }
1687 asyncResp->res.jsonValue["Members@odata.count"] =
1688 iface_array.size();
1689 asyncResp->res.jsonValue["Members"] = std::move(iface_array);
1690 asyncResp->res.jsonValue["@odata.id"] =
1691 "/redfish/v1/Managers/bmc/EthernetInterfaces/" +
1692 rootInterfaceName + "/VLANs";
1693 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001694 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001695
Ed Tanous1abe55e2018-09-05 08:30:59 -07001696 void doPost(crow::Response &res, const crow::Request &req,
1697 const std::vector<std::string> &params) override
1698 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001699 std::shared_ptr<AsyncResp> asyncResp = std::make_shared<AsyncResp>(res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001700 if (params.size() != 1)
1701 {
Jason M. Billsf12894f2018-10-09 12:45:45 -07001702 messages::internalError(asyncResp->res);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001703 return;
1704 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001705
Ed Tanous0627a2c2018-11-29 17:09:23 -08001706 uint32_t vlanId = 0;
1707 if (!json_util::readJson(req, res, "VLANId", vlanId))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001708 {
Ed Tanous4a0cb852018-10-15 07:55:04 -07001709 return;
1710 }
1711 const std::string &rootInterfaceName = params[0];
Ed Tanous4a0cb852018-10-15 07:55:04 -07001712 auto callback = [asyncResp](const boost::system::error_code ec) {
1713 if (ec)
1714 {
1715 // TODO(ed) make more consistent error messages based on
1716 // phosphor-network responses
Jason M. Billsf12894f2018-10-09 12:45:45 -07001717 messages::internalError(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001718 return;
1719 }
Jason M. Billsf12894f2018-10-09 12:45:45 -07001720 messages::created(asyncResp->res);
Ed Tanous4a0cb852018-10-15 07:55:04 -07001721 };
1722 crow::connections::systemBus->async_method_call(
1723 std::move(callback), "xyz.openbmc_project.Network",
1724 "/xyz/openbmc_project/network",
1725 "xyz.openbmc_project.Network.VLAN.Create", "VLAN",
Ed Tanous0627a2c2018-11-29 17:09:23 -08001726 rootInterfaceName, vlanId);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001727 }
Kowalski, Kamile439f0f2018-05-21 08:13:57 +02001728};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001729} // namespace redfish