blob: 53cdfbce58f710611c46ba4dd34fb4d94b6e262f [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"
Nick Gordon114537f2017-08-09 14:51:37 -050030
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050031#include "nlsr.hpp"
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050032#include "tlv/lsdb-status.hpp"
Nick Gordon114537f2017-08-09 14:51:37 -050033#include "logger.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080034
35#include <ndn-cxx/face.hpp>
Junxiao Shi3e5120c2016-09-10 16:58:34 +000036#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Jiewen Tana0497d82015-02-02 21:59:18 -080037#include <ndn-cxx/util/regex.hpp>
38
39namespace nlsr {
40
41INIT_LOGGER("LsdbDatasetInterestHandler");
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050042
laqinfand22da512017-05-25 17:29:53 -050043const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies");
44const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates");
45const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names");
46const ndn::PartialName LISTS_DATASET = ndn::PartialName("lsdb/list");
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050047
Jiewen Tana0497d82015-02-02 21:59:18 -080048LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb,
laqinfand22da512017-05-25 17:29:53 -050049 ndn::mgmt::Dispatcher& localHostDispatcher,
50 ndn::mgmt::Dispatcher& routerNameDispatcher,
Jiewen Tana0497d82015-02-02 21:59:18 -080051 ndn::Face& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080052 ndn::KeyChain& keyChain)
Nick Gordon114537f2017-08-09 14:51:37 -050053 : m_lsdb(lsdb)
54 , m_localhostDispatcher(localHostDispatcher)
laqinfand22da512017-05-25 17:29:53 -050055 , m_routerNameDispatcher(routerNameDispatcher)
Jiewen Tana0497d82015-02-02 21:59:18 -080056{
dmcoomes5bcb39e2017-10-31 15:07:55 -050057 NLSR_LOG_DEBUG("Setting dispatcher for lsdb status dataset:");
laqinfand22da512017-05-25 17:29:53 -050058 setDispatcher(m_localhostDispatcher);
59 setDispatcher(m_routerNameDispatcher);
Jiewen Tana0497d82015-02-02 21:59:18 -080060}
61
62void
laqinfand22da512017-05-25 17:29:53 -050063LsdbDatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher)
Jiewen Tana0497d82015-02-02 21:59:18 -080064{
laqinfand22da512017-05-25 17:29:53 -050065 dispatcher.addStatusDataset(ADJACENCIES_DATASET,
66 ndn::mgmt::makeAcceptAllAuthorization(),
67 std::bind(&LsdbDatasetInterestHandler::publishAdjStatus, this, _1, _2, _3));
68 dispatcher.addStatusDataset(COORDINATES_DATASET,
69 ndn::mgmt::makeAcceptAllAuthorization(),
70 std::bind(&LsdbDatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3));
71 dispatcher.addStatusDataset(NAMES_DATASET,
72 ndn::mgmt::makeAcceptAllAuthorization(),
73 std::bind(&LsdbDatasetInterestHandler::publishNameStatus, this, _1, _2, _3));
74 dispatcher.addStatusDataset(LISTS_DATASET,
75 ndn::mgmt::makeAcceptAllAuthorization(),
76 std::bind(&LsdbDatasetInterestHandler::publishAllStatus, this, _1, _2, _3));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050077}
78
79void
laqinfand22da512017-05-25 17:29:53 -050080LsdbDatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
81 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050082{
dmcoomes5bcb39e2017-10-31 15:07:55 -050083 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -050084
85 auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
86 std::list<AdjLsa>::const_iterator>(
87 m_lsdb.getAdjLsdb().cbegin(), m_lsdb.getAdjLsdb().cend());
88 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -050089 tlv::AdjacencyLsa tlvLsa;
Nick Gordon114537f2017-08-09 14:51:37 -050090 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -050091 tlvLsa.setLsaInfo(*tlvLsaInfo);
92
Nick Gordon114537f2017-08-09 14:51:37 -050093 for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
laqinfand22da512017-05-25 17:29:53 -050094 tlv::Adjacency tlvAdj;
95 tlvAdj.setName(adj.getName());
96 tlvAdj.setUri(adj.getFaceUri().toString());
97 tlvAdj.setCost(adj.getLinkCost());
98 tlvLsa.addAdjacency(tlvAdj);
99 }
100 const ndn::Block& wire = tlvLsa.wireEncode();
101 context.append(wire);
102 }
103 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500104}
105
106void
laqinfand22da512017-05-25 17:29:53 -0500107LsdbDatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
108 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500109{
Nick Gordon114537f2017-08-09 14:51:37 -0500110 auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
111 std::list<CoordinateLsa>::const_iterator>(
112 m_lsdb.getCoordinateLsdb().cbegin(), m_lsdb.getCoordinateLsdb().cend());
113
dmcoomes5bcb39e2017-10-31 15:07:55 -0500114 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -0500115 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -0500116 tlv::CoordinateLsa tlvLsa;
Nick Gordon114537f2017-08-09 14:51:37 -0500117 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -0500118 tlvLsa.setLsaInfo(*tlvLsaInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800119
Nick Gordon114537f2017-08-09 14:51:37 -0500120 tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
121 tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
laqinfand22da512017-05-25 17:29:53 -0500122
123 const ndn::Block& wire = tlvLsa.wireEncode();
124 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800125 }
laqinfand22da512017-05-25 17:29:53 -0500126 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500127}
128
129void
laqinfand22da512017-05-25 17:29:53 -0500130LsdbDatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
131 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500132{
Nick Gordon114537f2017-08-09 14:51:37 -0500133 auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>(
134 m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500135 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -0500136 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -0500137 tlv::NameLsa tlvLsa;
Jiewen Tana0497d82015-02-02 21:59:18 -0800138
Nick Gordon114537f2017-08-09 14:51:37 -0500139 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -0500140 tlvLsa.setLsaInfo(*tlvLsaInfo);
141
Nick Gordon114537f2017-08-09 14:51:37 -0500142 for (const ndn::Name& name : lsa->getNpl().getNames()) {
laqinfand22da512017-05-25 17:29:53 -0500143 tlvLsa.addName(name);
144 }
145
146 const ndn::Block& wire = tlvLsa.wireEncode();
147 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800148 }
laqinfand22da512017-05-25 17:29:53 -0500149 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800150}
151
152void
laqinfand22da512017-05-25 17:29:53 -0500153LsdbDatasetInterestHandler::publishAllStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
154 ndn::mgmt::StatusDatasetContext& context)
Jiewen Tana0497d82015-02-02 21:59:18 -0800155{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500156 NLSR_LOG_DEBUG("Received interest: " << interest);
laqinfand22da512017-05-25 17:29:53 -0500157 tlv::LsdbStatus lsdbStatus;
Nick Gordon114537f2017-08-09 14:51:37 -0500158 for (const tlv::AdjacencyLsa& tlvLsa : getTlvLsas<tlv::AdjacencyLsa>(m_lsdb)) {
laqinfand22da512017-05-25 17:29:53 -0500159 lsdbStatus.addAdjacencyLsa(tlvLsa);
160 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800161
Nick Gordon114537f2017-08-09 14:51:37 -0500162 for (const tlv::CoordinateLsa& tlvLsa : getTlvLsas<tlv::CoordinateLsa>(m_lsdb)) {
laqinfand22da512017-05-25 17:29:53 -0500163 lsdbStatus.addCoordinateLsa(tlvLsa);
164 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800165
Nick Gordon114537f2017-08-09 14:51:37 -0500166 for (const tlv::NameLsa& tlvLsa : getTlvLsas<tlv::NameLsa>(m_lsdb)) {
laqinfand22da512017-05-25 17:29:53 -0500167 lsdbStatus.addNameLsa(tlvLsa);
168 }
169 const ndn::Block& wire = lsdbStatus.wireEncode();
170 context.append(wire);
171 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800172}
173
Nick Gordon114537f2017-08-09 14:51:37 -0500174template<> std::list<tlv::AdjacencyLsa>
175getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb)
176{
177 std::list<tlv::AdjacencyLsa> lsas;
178
179 auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
180 std::list<AdjLsa>::const_iterator>(
181 lsdb.getAdjLsdb().cbegin(), lsdb.getAdjLsdb().cend());
182 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
183 tlv::AdjacencyLsa tlvLsa;
184
185 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
186 tlvLsa.setLsaInfo(*tlvLsaInfo);
187
188 for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
189 tlv::Adjacency tlvAdj;
190 tlvAdj.setName(adj.getName());
191 tlvAdj.setUri(adj.getFaceUri().toString());
192 tlvAdj.setCost(adj.getLinkCost());
193 tlvLsa.addAdjacency(tlvAdj);
194 }
195
196 lsas.push_back(tlvLsa);
197 }
198
199 return lsas;
200
201}
202
203template<> std::list<tlv::CoordinateLsa>
204getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb)
205{
206 std::list<tlv::CoordinateLsa> lsas;
207
208 auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
209 std::list<CoordinateLsa>::const_iterator>(
210 lsdb.getCoordinateLsdb().cbegin(), lsdb.getCoordinateLsdb().cend());
211
212 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
213 tlv::CoordinateLsa tlvLsa;
214
215 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
216 tlvLsa.setLsaInfo(*tlvLsaInfo);
217
218 tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
219 tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
220
221 lsas.push_back(tlvLsa);
222 }
223
224 return lsas;
225
226}
227
228template<> std::list<tlv::NameLsa>
229getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb)
230{
231 std::list<tlv::NameLsa> lsas;
232
233 auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator,
234 std::list<NameLsa>::const_iterator>(
235 lsdb.getNameLsdb().cbegin(), lsdb.getNameLsdb().cend());
236 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
237 tlv::NameLsa tlvLsa;
238
239 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
240 tlvLsa.setLsaInfo(*tlvLsaInfo);
241
242 for (const ndn::Name& name : lsa->getNpl().getNames()) {
243 tlvLsa.addName(name);
244 }
245
246 lsas.push_back(tlvLsa);
247 }
248
249 return lsas;
250
251}
252
253
Jiewen Tana0497d82015-02-02 21:59:18 -0800254} // namespace nlsr