dataset-interest-handler.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 #include "nlsr.hpp"
24 #include "tlv/lsdb-status.hpp"
25 #include "logger.hpp"
26 
27 #include <ndn-cxx/mgmt/nfd/control-response.hpp>
28 #include <ndn-cxx/util/regex.hpp>
29 
30 namespace nlsr {
31 
32 INIT_LOGGER(DatasetInterestHandler);
33 
34 const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies");
35 const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates");
36 const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names");
37 const ndn::PartialName RT_DATASET = ndn::PartialName("routing-table");
38 
40  const RoutingTable& rt,
41  ndn::mgmt::Dispatcher& dispatcher,
42  const ndn::Face& face,
43  const ndn::KeyChain& keyChain)
44  : m_lsdb(lsdb)
45  , m_dispatcher(dispatcher)
46  , m_routingTableEntries(rt.getRoutingTableEntry())
47  , m_dryRoutingTableEntries(rt.getDryRoutingTableEntry())
48 {
49  setDispatcher(m_dispatcher);
50 }
51 
52 void
53 DatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher)
54 {
55  dispatcher.addStatusDataset(ADJACENCIES_DATASET,
56  ndn::mgmt::makeAcceptAllAuthorization(),
57  std::bind(&DatasetInterestHandler::publishAdjStatus, this, _1, _2, _3));
58  dispatcher.addStatusDataset(COORDINATES_DATASET,
59  ndn::mgmt::makeAcceptAllAuthorization(),
60  std::bind(&DatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3));
61  dispatcher.addStatusDataset(NAMES_DATASET,
62  ndn::mgmt::makeAcceptAllAuthorization(),
63  std::bind(&DatasetInterestHandler::publishNameStatus, this, _1, _2, _3));
64  dispatcher.addStatusDataset(RT_DATASET,
65  ndn::mgmt::makeAcceptAllAuthorization(),
66  std::bind(&DatasetInterestHandler::publishRtStatus, this, _1, _2, _3));
67 }
68 
69 void
70 DatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
71  ndn::mgmt::StatusDatasetContext& context)
72 {
73  NLSR_LOG_DEBUG("Received interest: " << interest);
74 
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++) {
79  tlv::AdjacencyLsa tlvLsa;
80  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
81  tlvLsa.setLsaInfo(*tlvLsaInfo);
82 
83  for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
84  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();
94 }
95 
96 void
97 DatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
98  ndn::mgmt::StatusDatasetContext& context)
99 {
100  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 
104  NLSR_LOG_DEBUG("Received interest: " << interest);
105  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
106  tlv::CoordinateLsa tlvLsa;
107  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
108  tlvLsa.setLsaInfo(*tlvLsaInfo);
109 
110  tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
111  tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
112 
113  const ndn::Block& wire = tlvLsa.wireEncode();
114  context.append(wire);
115  }
116  context.end();
117 }
118 
119 void
120 DatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
121  ndn::mgmt::StatusDatasetContext& context)
122 {
123  auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>(
124  m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend());
125  NLSR_LOG_DEBUG("Received interest: " << interest);
126  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
127  tlv::NameLsa tlvLsa;
128 
129  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
130  tlvLsa.setLsaInfo(*tlvLsaInfo);
131 
132  for (const ndn::Name& name : lsa->getNpl().getNames()) {
133  tlvLsa.addName(name);
134  }
135 
136  const ndn::Block& wire = tlvLsa.wireEncode();
137  context.append(wire);
138  }
139  context.end();
140 }
141 
142 
143 std::vector<tlv::RoutingTable>
144 DatasetInterestHandler::getTlvRTEntries()
145 {
146  std::vector<tlv::RoutingTable> rtable;
147  for (const auto& rte : m_routingTableEntries) {
148  tlv::RoutingTable tlvRoutingTable;
149  std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(rte);
150  tlvRoutingTable.setDestination(*tlvDes);
151  for (const auto& nh : rte.getNexthopList().getNextHops()) {
152  tlv::NextHop tlvNexthop;
153  tlvNexthop.setUri(nh.getConnectingFaceUri());
154  tlvNexthop.setCost(nh.getRouteCost());
155  tlvRoutingTable.addNexthops(tlvNexthop);
156  }
157  rtable.push_back(tlvRoutingTable);
158  }
159  if (!m_dryRoutingTableEntries.empty()) {
160  for (const auto& dryRte : m_dryRoutingTableEntries) {
161  tlv::RoutingTable tlvRoutingTable;
162  std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(dryRte);
163  tlvRoutingTable.setDestination(*tlvDes);
164  for (const auto& nh : dryRte.getNexthopList().getNextHops()) {
165  tlv::NextHop tlvNexthop;
166  tlvNexthop.setUri(nh.getConnectingFaceUri());
167  tlvNexthop.setCost(nh.getRouteCost());
168  tlvRoutingTable.addNexthops(tlvNexthop);
169  }
170  rtable.push_back(tlvRoutingTable);
171  }
172  }
173  return rtable;
174 }
175 
176 void
177 DatasetInterestHandler::publishRtStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
178  ndn::mgmt::StatusDatasetContext& context)
179 {
180  NLSR_LOG_DEBUG("Received interest: " << interest);
181  tlv::RoutingTableStatus rtStatus;
182  for (const tlv::RoutingTable& rt : getTlvRTEntries()) {
183  rtStatus.addRoutingTable(rt);
184  }
185  const ndn::Block& wire = rtStatus.wireEncode();
186  context.append(wire);
187  context.end();
188 }
189 
190 template<> std::list<tlv::AdjacencyLsa>
191 getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb)
192 {
193  std::list<tlv::AdjacencyLsa> lsas;
194 
195  auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
196  std::list<AdjLsa>::const_iterator>(
197  lsdb.getAdjLsdb().cbegin(), lsdb.getAdjLsdb().cend());
198  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
199  tlv::AdjacencyLsa tlvLsa;
200 
201  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
202  tlvLsa.setLsaInfo(*tlvLsaInfo);
203 
204  for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
205  tlv::Adjacency tlvAdj;
206  tlvAdj.setName(adj.getName());
207  tlvAdj.setUri(adj.getFaceUri().toString());
208  tlvAdj.setCost(adj.getLinkCost());
209  tlvLsa.addAdjacency(tlvAdj);
210  }
211  lsas.push_back(tlvLsa);
212  }
213 
214  return lsas;
215 
216 }
217 
218 template<> std::list<tlv::CoordinateLsa>
219 getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb)
220 {
221  std::list<tlv::CoordinateLsa> lsas;
222 
223  auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
224  std::list<CoordinateLsa>::const_iterator>(
225  lsdb.getCoordinateLsdb().cbegin(), lsdb.getCoordinateLsdb().cend());
226 
227  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
228  tlv::CoordinateLsa tlvLsa;
229 
230  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
231  tlvLsa.setLsaInfo(*tlvLsaInfo);
232 
233  tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
234  tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
235 
236  lsas.push_back(tlvLsa);
237  }
238 
239  return lsas;
240 
241 }
242 
243 template<> std::list<tlv::NameLsa>
244 getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb)
245 {
246  std::list<tlv::NameLsa> lsas;
247 
248  auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator,
249  std::list<NameLsa>::const_iterator>(
250  lsdb.getNameLsdb().cbegin(), lsdb.getNameLsdb().cend());
251  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
252  tlv::NameLsa tlvLsa;
253 
254  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
255  tlvLsa.setLsaInfo(*tlvLsaInfo);
256 
257  for (const ndn::Name& name : lsa->getNpl().getNames()) {
258  tlvLsa.addName(name);
259  }
260 
261  lsas.push_back(tlvLsa);
262  }
263 
264  return lsas;
265 
266 }
267 
268 } // namespace nlsr
NextHop & setUri(const std::string &uri)
Definition: tlv/nexthop.hpp:68
Adjacency & setCost(uint64_t cost)
Definition: adjacency.hpp:97
const std::list< NameLsa > & getNameLsdb() const
Definition: lsdb.cpp:380
CoordinateLsa & setHyperbolicAngle(const std::vector< double > &hyperbolicAngle)
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes the Adjacent objects and some info using the method in TAG.
Data abstraction for Nexthop.
Definition: tlv/nexthop.hpp:43
RoutingTableStatus & addRoutingTable(const RoutingTable &routeTable)
AdjacencyLsa & addAdjacency(const Adjacency &adjacency)
NameLsa & addName(const ndn::Name &name)
Definition: name-lsa.hpp:95
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes the Name objects and some info using the method in TAG.
Definition: name-lsa.cpp:48
const ndn::PartialName COORDINATES_DATASET
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
std::shared_ptr< Destination > makeDes(const RoutingTableEntry &rte)
NameLsa & setLsaInfo(const LsaInfo &lsaInfo)
Definition: name-lsa.hpp:75
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
Data abstraction for routing table status.
Data abstraction for CoordinateLsa.
CoordinateLsa & setHyperbolicRadius(double hyperbolicRadius)
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:581
Data abstraction for RouteTableInfo.
Adjacency & setUri(const std::string &uri)
Definition: adjacency.hpp:83
const ndn::PartialName ADJACENCIES_DATASET
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
Encodes the hyperbolic coordinates and some info using the method in TAG.
size_t wireEncode(ndn::EncodingImpl< TAG > &block) const
std::shared_ptr< LsaInfo > makeLsaInfo(const Lsa &lsa)
Definition: lsa-info.cpp:174
RoutingTable & setDestination(const Destination &des)
AdjacencyLsa & setLsaInfo(const LsaInfo &lsaInfo)
Data abstraction for NameLsa.
Definition: name-lsa.hpp:47
A neighbor reachable over a Face.
Definition: adjacent.hpp:38
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
Data abstraction for AdjacencyLsa.
const ndn::PartialName NAMES_DATASET
const ndn::PartialName RT_DATASET
DatasetInterestHandler(const Lsdb &lsdb, const RoutingTable &rt, ndn::mgmt::Dispatcher &dispatcher, const ndn::Face &face, const ndn::KeyChain &keyChain)
NextHop & setCost(double cost)
Definition: tlv/nexthop.hpp:82
Data abstraction for Adjacency.
Definition: adjacency.hpp:44
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:819
Adjacency & setName(const ndn::Name &name)
Definition: adjacency.hpp:69
CoordinateLsa & setLsaInfo(const LsaInfo &lsaInfo)
RoutingTable & addNexthops(const NextHop &nexthop)