blob: 06c26e893a0ad3efaed62993d34e553aa097e0be [file] [log] [blame]
Tom Josephbe703f72017-03-09 12:34:35 +05301#include "utils.hpp"
2
Ratan Guptab8e99552017-07-27 07:07:48 +05303#include <arpa/inet.h>
4#include <dirent.h>
5#include <net/if.h>
6
Patrick Venture2e633522018-10-13 13:51:06 -07007#include <algorithm>
Patrick Venture0b02be92018-08-31 11:55:55 -07008#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/log.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
11
Tom Josephbe703f72017-03-09 12:34:35 +053012namespace ipmi
13{
14
Ratan Guptacc8feb42017-07-25 21:52:10 +053015using namespace phosphor::logging;
16using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -050018namespace network
19{
20
21/** @brief checks if the given ip is Link Local Ip or not.
Patrick Venture0b02be92018-08-31 11:55:55 -070022 * @param[in] ipaddress - IPAddress.
23 */
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -050024bool isLinkLocalIP(const std::string& ipaddress);
25
Patrick Venture0b02be92018-08-31 11:55:55 -070026} // namespace network
Ratan Guptacc8feb42017-07-25 21:52:10 +053027
Patrick Venture0b02be92018-08-31 11:55:55 -070028// TODO There may be cases where an interface is implemented by multiple
Ratan Guptacc8feb42017-07-25 21:52:10 +053029// objects,to handle such cases we are interested on that object
30// which are on interested busname.
31// Currently mapper doesn't give the readable busname(gives busid) so we can't
32// use busname to find the object,will do later once the support is there.
33
Ratan Gupta01d4bd12017-08-07 15:53:25 +053034DbusObjectInfo getDbusObject(sdbusplus::bus::bus& bus,
35 const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +053036 const std::string& serviceRoot,
37 const std::string& match)
38{
39 std::vector<DbusInterface> interfaces;
40 interfaces.emplace_back(interface);
41
Ratan Guptacc8feb42017-07-25 21:52:10 +053042 auto depth = 0;
43
Patrick Venture0b02be92018-08-31 11:55:55 -070044 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
45 MAPPER_INTF, "GetSubTree");
Ratan Guptacc8feb42017-07-25 21:52:10 +053046
47 mapperCall.append(serviceRoot, depth, interfaces);
48
49 auto mapperReply = bus.call(mapperCall);
50 if (mapperReply.is_method_error())
51 {
52 log<level::ERR>("Error in mapper call");
53 elog<InternalFailure>();
54 }
55
56 ObjectTree objectTree;
57 mapperReply.read(objectTree);
58
59 if (objectTree.empty())
60 {
Patrick Venturef0c48782017-09-21 18:48:51 -070061 log<level::ERR>("No Object has implemented the interface",
Ratan Guptacc8feb42017-07-25 21:52:10 +053062 entry("INTERFACE=%s", interface.c_str()));
63 elog<InternalFailure>();
64 }
65
66 DbusObjectInfo objectInfo;
67
68 // if match is empty then return the first object
Patrick Venture0b02be92018-08-31 11:55:55 -070069 if (match == "")
Ratan Guptacc8feb42017-07-25 21:52:10 +053070 {
Patrick Venture0b02be92018-08-31 11:55:55 -070071 objectInfo = std::make_pair(
72 objectTree.begin()->first,
Ratan Guptacc8feb42017-07-25 21:52:10 +053073 std::move(objectTree.begin()->second.begin()->first));
74 return objectInfo;
75 }
76
77 // else search the match string in the object path
Patrick Venture2e633522018-10-13 13:51:06 -070078 auto found = std::find_if(
79 objectTree.begin(), objectTree.end(), [&match](const auto& object) {
80 return (object.first.find(match) != std::string::npos);
81 });
Ratan Guptacc8feb42017-07-25 21:52:10 +053082
Patrick Venture2e633522018-10-13 13:51:06 -070083 if (found == objectTree.end())
Ratan Guptacc8feb42017-07-25 21:52:10 +053084 {
85 log<level::ERR>("Failed to find object which matches",
86 entry("MATCH=%s", match.c_str()));
87 elog<InternalFailure>();
Patrick Venture2e633522018-10-13 13:51:06 -070088 // elog<> throws an exception.
Ratan Guptacc8feb42017-07-25 21:52:10 +053089 }
Patrick Venture2e633522018-10-13 13:51:06 -070090
91 return make_pair(found->first, std::move(found->second.begin()->first));
Ratan Guptacc8feb42017-07-25 21:52:10 +053092}
93
Ratan Guptadd646202017-11-21 17:46:59 +053094DbusObjectInfo getIPObject(sdbusplus::bus::bus& bus,
95 const std::string& interface,
96 const std::string& serviceRoot,
97 const std::string& match)
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -050098{
99 auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match);
100
101 if (objectTree.empty())
102 {
103 log<level::ERR>("No Object has implemented the IP interface",
104 entry("INTERFACE=%s", interface.c_str()));
105 elog<InternalFailure>();
106 }
107
Ratan Guptadd646202017-11-21 17:46:59 +0530108 DbusObjectInfo objectInfo;
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500109
110 for (auto& object : objectTree)
111 {
112 auto variant = ipmi::getDbusProperty(
Patrick Venture0b02be92018-08-31 11:55:55 -0700113 bus, object.second.begin()->first, object.first,
114 ipmi::network::IP_INTERFACE, "Address");
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500115
Patrick Venture0b02be92018-08-31 11:55:55 -0700116 objectInfo = std::make_pair(object.first, object.second.begin()->first);
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500117
118 // if LinkLocalIP found look for Non-LinkLocalIP
Ratan Guptadd646202017-11-21 17:46:59 +0530119 if (ipmi::network::isLinkLocalIP(variant.get<std::string>()))
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500120 {
121 continue;
122 }
123 else
124 {
125 break;
126 }
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500127 }
Ratan Guptadd646202017-11-21 17:46:59 +0530128 return objectInfo;
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500129}
130
Patrick Venture0b02be92018-08-31 11:55:55 -0700131Value getDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
132 const std::string& objPath, const std::string& interface,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530133 const std::string& property)
134{
135
136 Value value;
137
Patrick Venture0b02be92018-08-31 11:55:55 -0700138 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
139 PROP_INTF, METHOD_GET);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530140
141 method.append(interface, property);
142
143 auto reply = bus.call(method);
144
145 if (reply.is_method_error())
146 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700147 log<level::ERR>("Failed to get property",
148 entry("PROPERTY=%s", property.c_str()),
149 entry("PATH=%s", objPath.c_str()),
150 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptacc8feb42017-07-25 21:52:10 +0530151 elog<InternalFailure>();
152 }
153
154 reply.read(value);
155
156 return value;
157}
158
Ratan Gupta01d4bd12017-08-07 15:53:25 +0530159PropertyMap getAllDbusProperties(sdbusplus::bus::bus& bus,
160 const std::string& service,
Ratan Guptacc8feb42017-07-25 21:52:10 +0530161 const std::string& objPath,
162 const std::string& interface)
163{
164 PropertyMap properties;
Ratan Guptacc8feb42017-07-25 21:52:10 +0530165
Patrick Venture0b02be92018-08-31 11:55:55 -0700166 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
167 PROP_INTF, METHOD_GET_ALL);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530168
169 method.append(interface);
170
171 auto reply = bus.call(method);
172
173 if (reply.is_method_error())
174 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700175 log<level::ERR>("Failed to get all properties",
176 entry("PATH=%s", objPath.c_str()),
177 entry("INTERFACE=%s", interface.c_str()));
178 elog<InternalFailure>();
Ratan Guptacc8feb42017-07-25 21:52:10 +0530179 }
180
181 reply.read(properties);
182 return properties;
183}
184
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600185ObjectValueTree getManagedObjects(sdbusplus::bus::bus& bus,
Patrick Venture0b02be92018-08-31 11:55:55 -0700186 const std::string& service,
187 const std::string& objPath)
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600188{
189 ipmi::ObjectValueTree interfaces;
190
Patrick Venture0b02be92018-08-31 11:55:55 -0700191 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
192 "org.freedesktop.DBus.ObjectManager",
193 "GetManagedObjects");
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600194
195 auto reply = bus.call(method);
196
197 if (reply.is_method_error())
198 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700199 log<level::ERR>("Failed to get managed objects",
200 entry("PATH=%s", objPath.c_str()));
201 elog<InternalFailure>();
Dhruvaraj Subhashchandran5c0beec2018-01-23 04:47:06 -0600202 }
203
204 reply.read(interfaces);
205 return interfaces;
206}
207
Patrick Venture0b02be92018-08-31 11:55:55 -0700208void setDbusProperty(sdbusplus::bus::bus& bus, const std::string& service,
209 const std::string& objPath, const std::string& interface,
210 const std::string& property, const Value& value)
Ratan Guptacc8feb42017-07-25 21:52:10 +0530211{
Patrick Venture0b02be92018-08-31 11:55:55 -0700212 auto method = bus.new_method_call(service.c_str(), objPath.c_str(),
213 PROP_INTF, METHOD_SET);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530214
215 method.append(interface, property, value);
216
217 if (!bus.call(method))
218 {
219 log<level::ERR>("Failed to set property",
220 entry("PROPERTY=%s", property.c_str()),
Patrick Venture0b02be92018-08-31 11:55:55 -0700221 entry("PATH=%s", objPath.c_str()),
222 entry("INTERFACE=%s", interface.c_str()));
Ratan Guptacc8feb42017-07-25 21:52:10 +0530223 elog<InternalFailure>();
224 }
Ratan Guptacc8feb42017-07-25 21:52:10 +0530225}
226
Patrick Venture0b02be92018-08-31 11:55:55 -0700227ServiceCache::ServiceCache(const std::string& intf, const std::string& path) :
228 intf(intf), path(path), cachedService(std::experimental::nullopt),
229 cachedBusName(std::experimental::nullopt)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700230{
231}
232
Patrick Venture0b02be92018-08-31 11:55:55 -0700233ServiceCache::ServiceCache(std::string&& intf, std::string&& path) :
234 intf(std::move(intf)), path(std::move(path)),
235 cachedService(std::experimental::nullopt),
236 cachedBusName(std::experimental::nullopt)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700237{
238}
239
240const std::string& ServiceCache::getService(sdbusplus::bus::bus& bus)
241{
242 if (!isValid(bus))
243 {
244 cachedBusName = bus.get_unique_name();
245 cachedService = ::ipmi::getService(bus, intf, path);
246 }
247 return cachedService.value();
248}
249
250void ServiceCache::invalidate()
251{
252 cachedBusName = std::experimental::nullopt;
253 cachedService = std::experimental::nullopt;
254}
255
Patrick Venture0b02be92018-08-31 11:55:55 -0700256sdbusplus::message::message
257 ServiceCache::newMethodCall(sdbusplus::bus::bus& bus, const char* intf,
258 const char* method)
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700259{
Patrick Venture0b02be92018-08-31 11:55:55 -0700260 return bus.new_method_call(getService(bus).c_str(), path.c_str(), intf,
261 method);
William A. Kennington IIIe47fdfb2018-03-15 17:09:28 -0700262}
263
264bool ServiceCache::isValid(sdbusplus::bus::bus& bus) const
265{
266 return cachedService && cachedBusName == bus.get_unique_name();
267}
268
Patrick Venture0b02be92018-08-31 11:55:55 -0700269std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Tom Josephbe703f72017-03-09 12:34:35 +0530270 const std::string& path)
271{
Patrick Venture0b02be92018-08-31 11:55:55 -0700272 auto mapperCall =
273 bus.new_method_call("xyz.openbmc_project.ObjectMapper",
274 "/xyz/openbmc_project/object_mapper",
275 "xyz.openbmc_project.ObjectMapper", "GetObject");
Tom Josephbe703f72017-03-09 12:34:35 +0530276
277 mapperCall.append(path);
278 mapperCall.append(std::vector<std::string>({intf}));
279
280 auto mapperResponseMsg = bus.call(mapperCall);
281
282 if (mapperResponseMsg.is_method_error())
283 {
284 throw std::runtime_error("ERROR in mapper call");
285 }
286
287 std::map<std::string, std::vector<std::string>> mapperResponse;
288 mapperResponseMsg.read(mapperResponse);
289
290 if (mapperResponse.begin() == mapperResponse.end())
291 {
292 throw std::runtime_error("ERROR in reading the mapper response");
293 }
294
295 return mapperResponse.begin()->first;
296}
297
Ratan Guptab8e99552017-07-27 07:07:48 +0530298ipmi::ObjectTree getAllDbusObjects(sdbusplus::bus::bus& bus,
299 const std::string& serviceRoot,
300 const std::string& interface,
301 const std::string& match)
302{
303 std::vector<std::string> interfaces;
304 interfaces.emplace_back(interface);
Ratan Guptacc8feb42017-07-25 21:52:10 +0530305
Ratan Guptab8e99552017-07-27 07:07:48 +0530306 auto depth = 0;
307
Patrick Venture0b02be92018-08-31 11:55:55 -0700308 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
309 MAPPER_INTF, "GetSubTree");
Ratan Guptab8e99552017-07-27 07:07:48 +0530310
311 mapperCall.append(serviceRoot, depth, interfaces);
312
313 auto mapperReply = bus.call(mapperCall);
314 if (mapperReply.is_method_error())
315 {
316 log<level::ERR>("Error in mapper call",
Patrick Venture0b02be92018-08-31 11:55:55 -0700317 entry("SERVICEROOT=%s", serviceRoot.c_str()),
Ratan Guptab8e99552017-07-27 07:07:48 +0530318 entry("INTERFACE=%s", interface.c_str()));
319
320 elog<InternalFailure>();
321 }
322
323 ObjectTree objectTree;
324 mapperReply.read(objectTree);
325
326 for (auto it = objectTree.begin(); it != objectTree.end();)
327 {
328 if (it->first.find(match) == std::string::npos)
329 {
330 it = objectTree.erase(it);
331 }
332 else
333 {
334 ++it;
335 }
336 }
337
338 return objectTree;
339}
340
341void deleteAllDbusObjects(sdbusplus::bus::bus& bus,
342 const std::string& serviceRoot,
343 const std::string& interface,
344 const std::string& match)
345{
346 try
347 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700348 auto objectTree = getAllDbusObjects(bus, serviceRoot, interface, match);
Ratan Guptab8e99552017-07-27 07:07:48 +0530349
350 for (auto& object : objectTree)
351 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700352 method_no_args::callDbusMethod(bus, object.second.begin()->first,
353 object.first, DELETE_INTERFACE,
354 "Delete");
Ratan Guptab8e99552017-07-27 07:07:48 +0530355 }
356 }
357 catch (InternalFailure& e)
358 {
359 log<level::INFO>("Unable to delete the objects having",
360 entry("INTERFACE=%s", interface.c_str()),
361 entry("SERVICE=%s", serviceRoot.c_str()));
362 }
363}
364
Patrick Venture0b02be92018-08-31 11:55:55 -0700365ObjectTree getAllAncestors(sdbusplus::bus::bus& bus, const std::string& path,
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530366 InterfaceList&& interfaces)
367{
Patrick Venture0b02be92018-08-31 11:55:55 -0700368 auto convertToString = [](InterfaceList& interfaces) -> std::string {
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530369 std::string intfStr;
370 for (const auto& intf : interfaces)
371 {
372 intfStr += "," + intf;
373 }
374 return intfStr;
375 };
376
Patrick Venture0b02be92018-08-31 11:55:55 -0700377 auto mapperCall = bus.new_method_call(MAPPER_BUS_NAME, MAPPER_OBJ,
378 MAPPER_INTF, "GetAncestors");
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530379 mapperCall.append(path, interfaces);
380
381 auto mapperReply = bus.call(mapperCall);
382 if (mapperReply.is_method_error())
383 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700384 log<level::ERR>(
385 "Error in mapper call", entry("PATH=%s", path.c_str()),
386 entry("INTERFACES=%s", convertToString(interfaces).c_str()));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530387
388 elog<InternalFailure>();
389 }
390
391 ObjectTree objectTree;
392 mapperReply.read(objectTree);
393
394 if (objectTree.empty())
395 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700396 log<level::ERR>(
397 "No Object has implemented the interface",
398 entry("PATH=%s", path.c_str()),
399 entry("INTERFACES=%s", convertToString(interfaces).c_str()));
Ratan Guptacc6cdbf2017-09-01 23:06:25 +0530400 elog<InternalFailure>();
401 }
402
403 return objectTree;
404}
Ratan Guptab8e99552017-07-27 07:07:48 +0530405
406namespace method_no_args
407{
408
Patrick Venture0b02be92018-08-31 11:55:55 -0700409void callDbusMethod(sdbusplus::bus::bus& bus, const std::string& service,
410 const std::string& objPath, const std::string& interface,
Ratan Guptab8e99552017-07-27 07:07:48 +0530411 const std::string& method)
412
413{
Patrick Venture0b02be92018-08-31 11:55:55 -0700414 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
415 interface.c_str(), method.c_str());
Ratan Guptab8e99552017-07-27 07:07:48 +0530416
417 auto reply = bus.call(busMethod);
418
419 if (reply.is_method_error())
420 {
421 log<level::ERR>("Failed to execute method",
422 entry("METHOD=%s", method.c_str()),
423 entry("PATH=%s", objPath.c_str()),
424 entry("INTERFACE=%s", interface.c_str()));
425 elog<InternalFailure>();
426 }
427}
428
Patrick Venture0b02be92018-08-31 11:55:55 -0700429} // namespace method_no_args
Ratan Guptab8e99552017-07-27 07:07:48 +0530430
431namespace network
432{
433
Nagaraju Goruganti1fe5c832017-09-21 07:44:17 -0500434bool isLinkLocalIP(const std::string& address)
435{
436 return address.find(IPV4_PREFIX) == 0 || address.find(IPV6_PREFIX) == 0;
437}
438
Patrick Venture0b02be92018-08-31 11:55:55 -0700439void createIP(sdbusplus::bus::bus& bus, const std::string& service,
440 const std::string& objPath, const std::string& protocolType,
441 const std::string& ipaddress, uint8_t prefix)
Ratan Guptab8e99552017-07-27 07:07:48 +0530442{
443 std::string gateway = "";
444
Patrick Venture0b02be92018-08-31 11:55:55 -0700445 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
446 IP_CREATE_INTERFACE, "IP");
Ratan Guptab8e99552017-07-27 07:07:48 +0530447
448 busMethod.append(protocolType, ipaddress, prefix, gateway);
449
450 auto reply = bus.call(busMethod);
451
452 if (reply.is_method_error())
453 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700454 log<level::ERR>("Failed to execute method", entry("METHOD=%s", "IP"),
Ratan Guptab8e99552017-07-27 07:07:48 +0530455 entry("PATH=%s", objPath.c_str()));
456 elog<InternalFailure>();
457 }
Ratan Guptab8e99552017-07-27 07:07:48 +0530458}
459
Patrick Venture0b02be92018-08-31 11:55:55 -0700460void createVLAN(sdbusplus::bus::bus& bus, const std::string& service,
461 const std::string& objPath, const std::string& interfaceName,
Ratan Gupta533d03b2017-07-30 10:39:22 +0530462 uint32_t vlanID)
463{
Patrick Venture0b02be92018-08-31 11:55:55 -0700464 auto busMethod = bus.new_method_call(service.c_str(), objPath.c_str(),
465 VLAN_CREATE_INTERFACE, "VLAN");
Ratan Gupta533d03b2017-07-30 10:39:22 +0530466
467 busMethod.append(interfaceName, vlanID);
468
469 auto reply = bus.call(busMethod);
470
471 if (reply.is_method_error())
472 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700473 log<level::ERR>("Failed to execute method", entry("METHOD=%s", "VLAN"),
Ratan Gupta533d03b2017-07-30 10:39:22 +0530474 entry("PATH=%s", objPath.c_str()));
475 elog<InternalFailure>();
476 }
Ratan Gupta533d03b2017-07-30 10:39:22 +0530477}
478
Ratan Guptab8e99552017-07-27 07:07:48 +0530479uint8_t toPrefix(int addressFamily, const std::string& subnetMask)
480{
481 if (addressFamily == AF_INET6)
482 {
483 return 0;
484 }
485
Patrick Venture0b02be92018-08-31 11:55:55 -0700486 uint32_t buff{};
Ratan Guptab8e99552017-07-27 07:07:48 +0530487
488 auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff);
489 if (rc <= 0)
490 {
491 log<level::ERR>("inet_pton failed:",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -0500492 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Guptab8e99552017-07-27 07:07:48 +0530493 return 0;
494 }
495
496 buff = be32toh(buff);
497 // total no of bits - total no of leading zero == total no of ones
Patrick Venture0b02be92018-08-31 11:55:55 -0700498 if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) ==
499 __builtin_popcount(buff))
Ratan Guptab8e99552017-07-27 07:07:48 +0530500 {
501 return __builtin_popcount(buff);
502 }
503 else
504 {
505 log<level::ERR>("Invalid Mask",
Joseph Reynolds510eb9c2018-05-30 11:51:28 -0500506 entry("SUBNETMASK=%s", subnetMask.c_str()));
Ratan Guptab8e99552017-07-27 07:07:48 +0530507 return 0;
508 }
509}
510
Ratan Gupta533d03b2017-07-30 10:39:22 +0530511uint32_t getVLAN(const std::string& path)
512{
513 // Path would be look like
514 // /xyz/openbmc_project/network/eth0_443/ipv4
515
516 uint32_t vlanID = 0;
517 try
518 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700519 auto intfObjectPath = path.substr(0, path.find(IP_TYPE) - 1);
Ratan Gupta533d03b2017-07-30 10:39:22 +0530520
521 auto intfName = intfObjectPath.substr(intfObjectPath.rfind("/") + 1);
522
523 auto index = intfName.find("_");
524 if (index != std::string::npos)
525 {
526 auto str = intfName.substr(index + 1);
527 vlanID = std::stoul(str);
528 }
529 }
Patrick Venture0b02be92018-08-31 11:55:55 -0700530 catch (std::exception& e)
Ratan Gupta533d03b2017-07-30 10:39:22 +0530531 {
Gunnar Mills8991dd62017-10-25 17:11:29 -0500532 log<level::ERR>("Exception occurred during getVLAN",
Patrick Venture0b02be92018-08-31 11:55:55 -0700533 entry("PATH=%s", path.c_str()),
Gunnar Mills5b801e42017-10-19 17:11:42 -0500534 entry("EXCEPTION=%s", e.what()));
Ratan Gupta533d03b2017-07-30 10:39:22 +0530535 }
536 return vlanID;
537}
538
Ratan Guptab8e99552017-07-27 07:07:48 +0530539} // namespace network
Tom Josephbe703f72017-03-09 12:34:35 +0530540} // namespace ipmi