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 
39 DatasetInterestHandler::DatasetInterestHandler(ndn::mgmt::Dispatcher& dispatcher,
40  const Lsdb& lsdb,
41  const RoutingTable& rt)
42  : m_dispatcher(dispatcher)
43  , m_lsdb(lsdb)
44  , m_routingTableEntries(rt.getRoutingTableEntry())
45  , m_dryRoutingTableEntries(rt.getDryRoutingTableEntry())
46 {
47  setDispatcher(m_dispatcher);
48 }
49 
50 void
51 DatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher)
52 {
53  dispatcher.addStatusDataset(ADJACENCIES_DATASET,
54  ndn::mgmt::makeAcceptAllAuthorization(),
55  std::bind(&DatasetInterestHandler::publishAdjStatus, this, _1, _2, _3));
56  dispatcher.addStatusDataset(COORDINATES_DATASET,
57  ndn::mgmt::makeAcceptAllAuthorization(),
58  std::bind(&DatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3));
59  dispatcher.addStatusDataset(NAMES_DATASET,
60  ndn::mgmt::makeAcceptAllAuthorization(),
61  std::bind(&DatasetInterestHandler::publishNameStatus, this, _1, _2, _3));
62  dispatcher.addStatusDataset(RT_DATASET,
63  ndn::mgmt::makeAcceptAllAuthorization(),
64  std::bind(&DatasetInterestHandler::publishRtStatus, this, _1, _2, _3));
65 }
66 
67 void
68 DatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
69  ndn::mgmt::StatusDatasetContext& context)
70 {
71  NLSR_LOG_DEBUG("Received interest: " << interest);
72 
73  auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator,
74  std::list<AdjLsa>::const_iterator>(
75  m_lsdb.getAdjLsdb().cbegin(), m_lsdb.getAdjLsdb().cend());
76  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
77  tlv::AdjacencyLsa tlvLsa;
78  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
79  tlvLsa.setLsaInfo(*tlvLsaInfo);
80 
81  for (const Adjacent& adj : lsa->getAdl().getAdjList()) {
82  tlv::Adjacency tlvAdj;
83  tlvAdj.setName(adj.getName());
84  tlvAdj.setUri(adj.getFaceUri().toString());
85  tlvAdj.setCost(adj.getLinkCost());
86  tlvLsa.addAdjacency(tlvAdj);
87  }
88  const ndn::Block& wire = tlvLsa.wireEncode();
89  context.append(wire);
90  }
91  context.end();
92 }
93 
94 void
95 DatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
96  ndn::mgmt::StatusDatasetContext& context)
97 {
98  auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator,
99  std::list<CoordinateLsa>::const_iterator>(
100  m_lsdb.getCoordinateLsdb().cbegin(), m_lsdb.getCoordinateLsdb().cend());
101 
102  NLSR_LOG_DEBUG("Received interest: " << interest);
103  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
104  tlv::CoordinateLsa tlvLsa;
105  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
106  tlvLsa.setLsaInfo(*tlvLsaInfo);
107 
108  tlvLsa.setHyperbolicRadius(lsa->getCorRadius());
109  tlvLsa.setHyperbolicAngle(lsa->getCorTheta());
110 
111  const ndn::Block& wire = tlvLsa.wireEncode();
112  context.append(wire);
113  }
114  context.end();
115 }
116 
117 void
118 DatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
119  ndn::mgmt::StatusDatasetContext& context)
120 {
121  auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>(
122  m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend());
123  NLSR_LOG_DEBUG("Received interest: " << interest);
124  for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) {
125  tlv::NameLsa tlvLsa;
126 
127  std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa);
128  tlvLsa.setLsaInfo(*tlvLsaInfo);
129 
130  for (const ndn::Name& name : lsa->getNpl().getNames()) {
131  tlvLsa.addName(name);
132  }
133 
134  const ndn::Block& wire = tlvLsa.wireEncode();
135  context.append(wire);
136  }
137  context.end();
138 }
139 
140 
141 std::vector<tlv::RoutingTable>
142 DatasetInterestHandler::getTlvRTEntries()
143 {
144  std::vector<tlv::RoutingTable> rtable;
145  for (const auto& rte : m_routingTableEntries) {
146  tlv::RoutingTable tlvRoutingTable;
147  std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(rte);
148  tlvRoutingTable.setDestination(*tlvDes);
149  for (const auto& nh : rte.getNexthopList().getNextHops()) {
150  tlv::NextHop tlvNexthop;
151  tlvNexthop.setUri(nh.getConnectingFaceUri());
152  tlvNexthop.setCost(nh.getRouteCost());
153  tlvRoutingTable.addNexthops(tlvNexthop);
154  }
155  rtable.push_back(tlvRoutingTable);
156  }
157  if (!m_dryRoutingTableEntries.empty()) {
158  for (const auto& dryRte : m_dryRoutingTableEntries) {
159  tlv::RoutingTable tlvRoutingTable;
160  std::shared_ptr<tlv::Destination> tlvDes = tlv::makeDes(dryRte);
161  tlvRoutingTable.setDestination(*tlvDes);
162  for (const auto& nh : dryRte.getNexthopList().getNextHops()) {
163  tlv::NextHop tlvNexthop;
164  tlvNexthop.setUri(nh.getConnectingFaceUri());
165  tlvNexthop.setCost(nh.getRouteCost());
166  tlvRoutingTable.addNexthops(tlvNexthop);
167  }
168  rtable.push_back(tlvRoutingTable);
169  }
170  }
171  return rtable;
172 }
173 
174 void
175 DatasetInterestHandler::publishRtStatus(const ndn::Name& topPrefix, const ndn::Interest& interest,
176  ndn::mgmt::StatusDatasetContext& context)
177 {
178  NLSR_LOG_DEBUG("Received interest: " << interest);
179  tlv::RoutingTableStatus rtStatus;
180  for (const tlv::RoutingTable& rt : getTlvRTEntries()) {
181  rtStatus.addRoutingTable(rt);
182  }
183  const ndn::Block& wire = rtStatus.wireEncode();
184  context.append(wire);
185  context.end();
186 }
187 
188 } // 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:344
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:533
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:170
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-2019, 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
DatasetInterestHandler(ndn::mgmt::Dispatcher &dispatcher, const Lsdb &lsdb, const RoutingTable &rt)
const ndn::PartialName RT_DATASET
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:768
Adjacency & setName(const ndn::Name &name)
Definition: adjacency.hpp:69
CoordinateLsa & setLsaInfo(const LsaInfo &lsaInfo)
RoutingTable & addNexthops(const NextHop &nexthop)