blob: efd76f844d8e634b677b4ac34e03955883915ed4 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
laqinfan35731852017-08-08 06:17:39 -05003 * Copyright (c) 2014-2018, 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
laqinfan35731852017-08-08 06:17:39 -050022#include "dataset-interest-handler.hpp"
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050023#include "nlsr.hpp"
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050024#include "tlv/lsdb-status.hpp"
Nick Gordon114537f2017-08-09 14:51:37 -050025#include "logger.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080026
Junxiao Shi3e5120c2016-09-10 16:58:34 +000027#include <ndn-cxx/mgmt/nfd/control-response.hpp>
Jiewen Tana0497d82015-02-02 21:59:18 -080028#include <ndn-cxx/util/regex.hpp>
29
30namespace nlsr {
31
laqinfan35731852017-08-08 06:17:39 -050032INIT_LOGGER("DatasetInterestHandler");
Ashlesh Gawande90173ad2017-08-09 15:19:50 -050033
laqinfand22da512017-05-25 17:29:53 -050034const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies");
35const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates");
36const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names");
laqinfan35731852017-08-08 06:17:39 -050037const ndn::PartialName RT_DATASET = ndn::PartialName("routing-table");
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050038
laqinfan35731852017-08-08 06:17:39 -050039DatasetInterestHandler::DatasetInterestHandler(const Lsdb& lsdb,
40 const RoutingTable& rt,
41 ndn::mgmt::Dispatcher& dispatcher,
42 const ndn::Face& face,
43 const ndn::KeyChain& keyChain)
Nick Gordon114537f2017-08-09 14:51:37 -050044 : m_lsdb(lsdb)
laqinfan35731852017-08-08 06:17:39 -050045 , m_dispatcher(dispatcher)
46 , m_routingTableEntries(rt.getRoutingTableEntry())
47 , m_dryRoutingTableEntries(rt.getDryRoutingTableEntry())
Jiewen Tana0497d82015-02-02 21:59:18 -080048{
laqinfan35731852017-08-08 06:17:39 -050049 setDispatcher(m_dispatcher);
Jiewen Tana0497d82015-02-02 21:59:18 -080050}
51
52void
laqinfan35731852017-08-08 06:17:39 -050053DatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher)
Jiewen Tana0497d82015-02-02 21:59:18 -080054{
laqinfand22da512017-05-25 17:29:53 -050055 dispatcher.addStatusDataset(ADJACENCIES_DATASET,
56 ndn::mgmt::makeAcceptAllAuthorization(),
laqinfan35731852017-08-08 06:17:39 -050057 std::bind(&DatasetInterestHandler::publishAdjStatus, this, _1, _2, _3));
laqinfand22da512017-05-25 17:29:53 -050058 dispatcher.addStatusDataset(COORDINATES_DATASET,
59 ndn::mgmt::makeAcceptAllAuthorization(),
laqinfan35731852017-08-08 06:17:39 -050060 std::bind(&DatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3));
laqinfand22da512017-05-25 17:29:53 -050061 dispatcher.addStatusDataset(NAMES_DATASET,
62 ndn::mgmt::makeAcceptAllAuthorization(),
laqinfan35731852017-08-08 06:17:39 -050063 std::bind(&DatasetInterestHandler::publishNameStatus, this, _1, _2, _3));
64 dispatcher.addStatusDataset(RT_DATASET,
laqinfand22da512017-05-25 17:29:53 -050065 ndn::mgmt::makeAcceptAllAuthorization(),
laqinfan35731852017-08-08 06:17:39 -050066 std::bind(&DatasetInterestHandler::publishRtStatus, this, _1, _2, _3));
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050067}
68
69void
laqinfan35731852017-08-08 06:17:39 -050070DatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
71 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050072{
dmcoomes5bcb39e2017-10-31 15:07:55 -050073 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -050074
75 auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
76 std::list<AdjLsa>::const_iterator>(
77 m_lsdb.getAdjLsdb().cbegin(), m_lsdb.getAdjLsdb().cend());
78 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -050079 tlv::AdjacencyLsa tlvLsa;
Nick Gordon114537f2017-08-09 14:51:37 -050080 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -050081 tlvLsa.setLsaInfo(*tlvLsaInfo);
82
Nick Gordon114537f2017-08-09 14:51:37 -050083 for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
laqinfand22da512017-05-25 17:29:53 -050084 tlv::Adjacency tlvAdj;
85 tlvAdj.setName(adj.getName());
86 tlvAdj.setUri(adj.getFaceUri().toString());
87 tlvAdj.setCost(adj.getLinkCost());
88 tlvLsa.addAdjacency(tlvAdj);
89 }
90 const ndn::Block& wire = tlvLsa.wireEncode();
91 context.append(wire);
92 }
93 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050094}
95
96void
laqinfan35731852017-08-08 06:17:39 -050097DatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
98 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050099{
Nick Gordon114537f2017-08-09 14:51:37 -0500100 auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
101 std::list<CoordinateLsa>::const_iterator>(
102 m_lsdb.getCoordinateLsdb().cbegin(), m_lsdb.getCoordinateLsdb().cend());
103
dmcoomes5bcb39e2017-10-31 15:07:55 -0500104 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -0500105 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -0500106 tlv::CoordinateLsa tlvLsa;
Nick Gordon114537f2017-08-09 14:51:37 -0500107 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -0500108 tlvLsa.setLsaInfo(*tlvLsaInfo);
Jiewen Tana0497d82015-02-02 21:59:18 -0800109
Nick Gordon114537f2017-08-09 14:51:37 -0500110 tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
111 tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
laqinfand22da512017-05-25 17:29:53 -0500112
113 const ndn::Block& wire = tlvLsa.wireEncode();
114 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800115 }
laqinfand22da512017-05-25 17:29:53 -0500116 context.end();
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500117}
118
119void
laqinfan35731852017-08-08 06:17:39 -0500120DatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
121 ndn::mgmt::StatusDatasetContext& context)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500122{
Nick Gordon114537f2017-08-09 14:51:37 -0500123 auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>(
124 m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend());
dmcoomes5bcb39e2017-10-31 15:07:55 -0500125 NLSR_LOG_DEBUG("Received interest: " << interest);
Nick Gordon114537f2017-08-09 14:51:37 -0500126 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
laqinfand22da512017-05-25 17:29:53 -0500127 tlv::NameLsa tlvLsa;
Jiewen Tana0497d82015-02-02 21:59:18 -0800128
Nick Gordon114537f2017-08-09 14:51:37 -0500129 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
laqinfand22da512017-05-25 17:29:53 -0500130 tlvLsa.setLsaInfo(*tlvLsaInfo);
131
Nick Gordon114537f2017-08-09 14:51:37 -0500132 for (const ndn::Name& name : lsa->getNpl().getNames()) {
laqinfand22da512017-05-25 17:29:53 -0500133 tlvLsa.addName(name);
134 }
135
136 const ndn::Block& wire = tlvLsa.wireEncode();
137 context.append(wire);
Jiewen Tana0497d82015-02-02 21:59:18 -0800138 }
laqinfand22da512017-05-25 17:29:53 -0500139 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800140}
141
142void
laqinfan35731852017-08-08 06:17:39 -0500143DatasetInterestHandler::publishRtStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
144 ndn::mgmt::StatusDatasetContext& context)
Jiewen Tana0497d82015-02-02 21:59:18 -0800145{
laqinfan35731852017-08-08 06:17:39 -0500146 NLSR_LOG_DEBUG("Received interest for routing table: " << interest);
147 tlv::RoutingTable tlvRoutingTable;
148
149 for (const auto& rte : m_routingTableEntries) {
150 std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(rte);
151 tlvRoutingTable.setDestination(*tlvDes);
152
153 for (const auto& nh : rte.getNexthopList().getNextHops()) {
154 tlv::NextHop tlvNexthop;
155 tlvNexthop.setUri(nh.getConnectingFaceUri());
156 tlvNexthop.setCost(nh.getRouteCost());
157 tlvRoutingTable.addNexthops(tlvNexthop);
158 }
159
160 const ndn::Block& wire = tlvRoutingTable.wireEncode();
161 context.append(wire);
162 }
163 if (!m_dryRoutingTableEntries.empty()) {
164 for (const auto& dry_rte : m_dryRoutingTableEntries ) {
165 std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(dry_rte);
166 tlvRoutingTable.setDestination(*tlvDes);
167
168 for (const auto& nh : dry_rte.getNexthopList().getNextHops()) {
169 tlv::NextHop tlvNexthop;
170 tlvNexthop.setUri(nh.getConnectingFaceUri());
171 tlvNexthop.setCost(nh.getRouteCost());
172 tlvRoutingTable.addNexthops(tlvNexthop);
173 }
174 const ndn::Block& wire = tlvRoutingTable.wireEncode();
175 context.append(wire);
176 }
laqinfand22da512017-05-25 17:29:53 -0500177 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800178
laqinfand22da512017-05-25 17:29:53 -0500179 context.end();
Jiewen Tana0497d82015-02-02 21:59:18 -0800180}
181
Nick Gordon114537f2017-08-09 14:51:37 -0500182template<> std::list<tlv::AdjacencyLsa>
183getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb)
184{
185 std::list<tlv::AdjacencyLsa> lsas;
186
187 auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
188 std::list<AdjLsa>::const_iterator>(
189 lsdb.getAdjLsdb().cbegin(), lsdb.getAdjLsdb().cend());
190 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
191 tlv::AdjacencyLsa tlvLsa;
192
193 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
194 tlvLsa.setLsaInfo(*tlvLsaInfo);
195
196 for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
197 tlv::Adjacency tlvAdj;
198 tlvAdj.setName(adj.getName());
199 tlvAdj.setUri(adj.getFaceUri().toString());
200 tlvAdj.setCost(adj.getLinkCost());
201 tlvLsa.addAdjacency(tlvAdj);
202 }
203
204 lsas.push_back(tlvLsa);
205 }
206
207 return lsas;
208
209}
210
211template<> std::list<tlv::CoordinateLsa>
212getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb)
213{
214 std::list<tlv::CoordinateLsa> lsas;
215
216 auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
217 std::list<CoordinateLsa>::const_iterator>(
218 lsdb.getCoordinateLsdb().cbegin(), lsdb.getCoordinateLsdb().cend());
219
220 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
221 tlv::CoordinateLsa tlvLsa;
222
223 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
224 tlvLsa.setLsaInfo(*tlvLsaInfo);
225
226 tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
227 tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
228
229 lsas.push_back(tlvLsa);
230 }
231
232 return lsas;
233
234}
235
236template<> std::list<tlv::NameLsa>
237getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb)
238{
239 std::list<tlv::NameLsa> lsas;
240
241 auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator,
242 std::list<NameLsa>::const_iterator>(
243 lsdb.getNameLsdb().cbegin(), lsdb.getNameLsdb().cend());
244 for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
245 tlv::NameLsa tlvLsa;
246
247 std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
248 tlvLsa.setLsaInfo(*tlvLsaInfo);
249
250 for (const ndn::Name& name : lsa->getNpl().getNames()) {
251 tlvLsa.addName(name);
252 }
253
254 lsas.push_back(tlvLsa);
255 }
256
257 return lsas;
258
259}
260
Jiewen Tana0497d82015-02-02 21:59:18 -0800261} // namespace nlsr