blob: c25651ed0629a05d58a4cee869944ed9174f477d [file] [log] [blame]
Tomáš Pecka7acf3922021-08-10 11:16:57 +02001/*
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
12namespace velia::system {
13
14LLDPCallback::LLDPCallback(std::shared_ptr<LLDPDataProvider> lldp)
15 : m_log(spdlog::get("system"))
16 , m_lldp(std::move(lldp))
Tomáš Pecka7acf3922021-08-10 11:16:57 +020017{
18}
19
Václav Kubernát7efd6d52021-11-09 01:31:11 +010020sysrepo::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áš Pecka7acf3922021-08-10 11:16:57 +020021{
Václav Kubernát7efd6d52021-11-09 01:31:11 +010022 m_log->trace("operational data callback: subXPath {} request-XPath {}",
23 subXPath ? *subXPath : "(none)", requestXPath ? *requestXPath : "(none)");
Tomáš Pecka7acf3922021-08-10 11:16:57 +020024
Václav Kubernát7efd6d52021-11-09 01:31:11 +010025 output = session.getContext().newPath("/czechlight-lldp:nbr-list");
Tomáš Pecka7acf3922021-08-10 11:16:57 +020026
27 for (const auto& n : m_lldp->getNeighbors()) {
Václav Kubernát7efd6d52021-11-09 01:31:11 +010028 auto ifc = output->newPath("neighbors");
Tomáš Pecka7acf3922021-08-10 11:16:57 +020029
Václav Kubernát7efd6d52021-11-09 01:31:11 +010030 auto ifName = ifc->newPath("ifName", n.m_portId.c_str());
Tomáš Pecka7acf3922021-08-10 11:16:57 +020031
32 for (const auto& [key, val] : n.m_properties) { // garbage properties in, garbage out
Václav Kubernát7efd6d52021-11-09 01:31:11 +010033 ifc->newPath(key.c_str(), val.c_str());
Tomáš Pecka7acf3922021-08-10 11:16:57 +020034 }
35 }
36
Václav Kubernát7efd6d52021-11-09 01:31:11 +010037 m_log->trace("Pushing to sysrepo (JSON): {}", *output->printStr(libyang::DataFormat::JSON, libyang::PrintFlags::WithSiblings));
Tomáš Pecka7acf3922021-08-10 11:16:57 +020038
Václav Kubernát7efd6d52021-11-09 01:31:11 +010039 return sysrepo::ErrorCode::Ok;
Tomáš Pecka7acf3922021-08-10 11:16:57 +020040}
41
42}