blob: e3cfa1f9c5c808f4b3889ec6bdc440ab6b731934 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, The University of Memphis,
Jiewen Tana0497d82015-02-02 21:59:18 -08004 * Regents of the University of California,
5 * Arizona Board of Regents.
6 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
20 **/
21
Nick G97e34942016-07-11 14:46:27 -050022/*! \file lsdb-dataset-interest-handler.cpp
23
24 This file details a class that is used by NLSRC and other command-line
25 tools to examine the state of NLSR. This system is not designed to
26 be used by routers to publish data to each other.
27 */
28
Jiewen Tana0497d82015-02-02 21:59:18 -080029#include "lsdb-dataset-interest-handler.hpp"
30
31#include "logger.hpp"
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050032#include "nlsr.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080033
34#include <ndn-cxx/face.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000035#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Jiewen Tana0497d82015-02-02 21:59:18 -080036#include <ndn-cxx/util/regex.hpp>
laqinfand22da512017-05-25 17:29:53 -050037#include "tlv/lsdb-status.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080038
39namespace nlsr {
40
41INIT_LOGGER("LsdbDatasetInterestHandler");
laqinfand22da512017-05-25 17:29:53 -050042const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies");
43const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates");
44const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names");
45const ndn::PartialName LISTS_DATASET = ndn::PartialName("lsdb/list");
46const ndn::PartialName LsdbDatasetInterestHandler::LOCALHOST_COMMAND_PREFIX =
47 ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT);
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050048
Jiewen Tana0497d82015-02-02 21:59:18 -080049LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb,
laqinfand22da512017-05-25 17:29:53 -050050 ndn::mgmt::Dispatcher& localHostDispatcher,
51 ndn::mgmt::Dispatcher& routerNameDispatcher,
Jiewen Tana0497d82015-02-02 21:59:18 -080052 ndn::Face& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080053 ndn::KeyChain& keyChain)
laqinfand22da512017-05-25 17:29:53 -050054 : m_localhostDispatcher(localHostDispatcher)
55 , m_routerNameDispatcher(routerNameDispatcher)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050056 , m_adjacencyLsaPublisher(lsdb, face, keyChain)
57 , m_coordinateLsaPublisher(lsdb, face, keyChain)
58 , m_nameLsaPublisher(lsdb, face, keyChain)
laqinfand22da512017-05-25 17:29:53 -050059 , m_adjacencyLsas(lsdb.getAdjLsdb())
60 , m_coordinateLsas(lsdb.getCoordinateLsdb())
61 , m_nameLsas(lsdb.getNameLsdb())
Jiewen Tana0497d82015-02-02 21:59:18 -080062{
laqinfand22da512017-05-25 17:29:53 -050063 _LOG_DEBUG("Setting dispatcher for lsdb status dataset:");
64 setDispatcher(m_localhostDispatcher);
65 setDispatcher(m_routerNameDispatcher);
Jiewen Tana0497d82015-02-02 21:59:18 -080066}
67
68void
laqinfand22da512017-05-25 17:29:53 -050069LsdbDatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher)
Jiewen Tana0497d82015-02-02 21:59:18 -080070{
laqinfand22da512017-05-25 17:29:53 -050071 dispatcher.addStatusDataset(ADJACENCIES_DATASET,
72 ndn::mgmt::makeAcceptAllAuthorization(),
73 std::bind(&LsdbDatasetInterestHandler::publishAdjStatus, this, _1, _2, _3));
74 dispatcher.addStatusDataset(COORDINATES_DATASET,
75 ndn::mgmt::makeAcceptAllAuthorization(),
76 std::bind(&LsdbDatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3));
77 dispatcher.addStatusDataset(NAMES_DATASET,
78 ndn::mgmt::makeAcceptAllAuthorization(),
79 std::bind(&LsdbDatasetInterestHandler::publishNameStatus, this, _1, _2, _3));
80 dispatcher.addStatusDataset(LISTS_DATASET,
81 ndn::mgmt::makeAcceptAllAuthorization(),
82 std::bind(&LsdbDatasetInterestHandler::publishAllStatus, this, _1, _2, _3));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050083}
84
85void
laqinfand22da512017-05-25 17:29:53 -050086LsdbDatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
87 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050088{
laqinfand22da512017-05-25 17:29:53 -050089 _LOG_DEBUG("Received interest: " << interest);
90 for (AdjLsa lsa : m_adjacencyLsas) {
91 tlv::AdjacencyLsa tlvLsa;
92 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
93 tlvLsa.setLsaInfo(*tlvLsaInfo);
94
95 for (const Adjacent& adj : lsa.getAdl().getAdjList()) {
96 tlv::Adjacency tlvAdj;
97 tlvAdj.setName(adj.getName());
98 tlvAdj.setUri(adj.getFaceUri().toString());
99 tlvAdj.setCost(adj.getLinkCost());
100 tlvLsa.addAdjacency(tlvAdj);
101 }
102 const ndn::Block& wire = tlvLsa.wireEncode();
103 context.append(wire);
104 }
105 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500106}
107
108void
laqinfand22da512017-05-25 17:29:53 -0500109LsdbDatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
110 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500111{
laqinfand22da512017-05-25 17:29:53 -0500112 _LOG_DEBUG("Received interest: " << interest);
113 for (const CoordinateLsa lsa : m_coordinateLsas) {
114 tlv::CoordinateLsa tlvLsa;
115 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
116 tlvLsa.setLsaInfo(*tlvLsaInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800117
laqinfand22da512017-05-25 17:29:53 -0500118 tlvLsa.setHyperbolicRadius(lsa.getCorRadius());
119 tlvLsa.setHyperbolicAngle(lsa.getCorTheta());
120
121 const ndn::Block& wire = tlvLsa.wireEncode();
122 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800123 }
laqinfand22da512017-05-25 17:29:53 -0500124 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500125}
126
127void
laqinfand22da512017-05-25 17:29:53 -0500128LsdbDatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
129 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500130{
laqinfand22da512017-05-25 17:29:53 -0500131 _LOG_DEBUG("Received interest: " << interest);
132 for (NameLsa lsa : m_nameLsas) {
133 tlv::NameLsa tlvLsa;
Jiewen Tana0497d82015-02-02 21:59:18 -0800134
laqinfand22da512017-05-25 17:29:53 -0500135 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa);
136 tlvLsa.setLsaInfo(*tlvLsaInfo);
137
138 for (const ndn::Name& name : lsa.getNpl().getNameList()) {
139 tlvLsa.addName(name);
140 }
141
142 const ndn::Block& wire = tlvLsa.wireEncode();
143 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800144 }
laqinfand22da512017-05-25 17:29:53 -0500145 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800146}
147
148void
laqinfand22da512017-05-25 17:29:53 -0500149LsdbDatasetInterestHandler::publishAllStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
150 ndn::mgmt::StatusDatasetContext& context)
Jiewen Tana0497d82015-02-02 21:59:18 -0800151{
laqinfand22da512017-05-25 17:29:53 -0500152 _LOG_DEBUG("Received interest: " << interest);
153 tlv::LsdbStatus lsdbStatus;
154 for (const tlv::AdjacencyLsa& tlvLsa : m_adjacencyLsaPublisher.getTlvLsas()) {
155 lsdbStatus.addAdjacencyLsa(tlvLsa);
156 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800157
laqinfand22da512017-05-25 17:29:53 -0500158 for (const tlv::CoordinateLsa& tlvLsa : m_coordinateLsaPublisher.getTlvLsas()) {
159 lsdbStatus.addCoordinateLsa(tlvLsa);
160 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800161
laqinfand22da512017-05-25 17:29:53 -0500162 for (const tlv::NameLsa& tlvLsa : m_nameLsaPublisher.getTlvLsas()) {
163 lsdbStatus.addNameLsa(tlvLsa);
164 }
165 const ndn::Block& wire = lsdbStatus.wireEncode();
166 context.append(wire);
167 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800168}
169
170} // namespace nlsr