Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <numeric> |
| 9 | #include "LLDPCallback.h" |
| 10 | #include "utils/log.h" |
| 11 | |
| 12 | namespace velia::system { |
| 13 | |
| 14 | LLDPCallback::LLDPCallback(std::shared_ptr<LLDPDataProvider> lldp) |
| 15 | : m_log(spdlog::get("system")) |
| 16 | , m_lldp(std::move(lldp)) |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 17 | { |
| 18 | } |
| 19 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 20 | sysrepo::ErrorCode LLDPCallback::operator()(sysrepo::Session session, uint32_t, std::string_view, std::optional<std::string_view> subXPath, std::optional<std::string_view> requestXPath, uint32_t, std::optional<libyang::DataNode>& output) |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 21 | { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 22 | m_log->trace("operational data callback: subXPath {} request-XPath {}", |
| 23 | subXPath ? *subXPath : "(none)", requestXPath ? *requestXPath : "(none)"); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 24 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 25 | output = session.getContext().newPath("/czechlight-lldp:nbr-list"); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 26 | |
| 27 | for (const auto& n : m_lldp->getNeighbors()) { |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 28 | auto ifc = output->newPath("neighbors"); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 29 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 30 | auto ifName = ifc->newPath("ifName", n.m_portId.c_str()); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 31 | |
| 32 | for (const auto& [key, val] : n.m_properties) { // garbage properties in, garbage out |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 33 | ifc->newPath(key.c_str(), val.c_str()); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 34 | } |
| 35 | } |
| 36 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 37 | m_log->trace("Pushing to sysrepo (JSON): {}", *output->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings)); |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 38 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 39 | return sysrepo::ErrorCode::Ok; |
Tomáš Pecka | 7acf392 | 2021-08-10 11:16:57 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | } |