routing-table.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
21 #include "routing-table.hpp"
22 #include "nlsr.hpp"
23 #include "map.hpp"
24 #include "conf-parameter.hpp"
26 #include "routing-table-entry.hpp"
27 #include "name-prefix-table.hpp"
28 #include "logger.hpp"
29 
30 #include <iostream>
31 #include <list>
32 #include <string>
33 
34 namespace nlsr {
35 
36 INIT_LOGGER(route.RoutingTable);
37 
38 RoutingTable::RoutingTable(ndn::Scheduler& scheduler, Fib& fib, Lsdb& lsdb,
39  NamePrefixTable& namePrefixTable, ConfParameter& confParam)
40  : afterRoutingChange{std::make_unique<AfterRoutingChange>()}
41  , m_scheduler(scheduler)
42  , m_fib(fib)
43  , m_lsdb(lsdb)
44  , m_namePrefixTable(namePrefixTable)
45  , m_NO_NEXT_HOP{-12345}
46  , m_routingCalcInterval{confParam.getRoutingCalcInterval()}
47  , m_isRoutingTableCalculating(false)
48  , m_isRouteCalculationScheduled(false)
49  , m_confParam(confParam)
50 {
51 }
52 
53 void
55 {
56  m_lsdb.writeCorLsdbLog();
57  m_lsdb.writeNameLsdbLog();
58  m_lsdb.writeAdjLsdbLog();
59  m_namePrefixTable.writeLog();
60  if (m_isRoutingTableCalculating == false) {
61  // setting routing table calculation
62  m_isRoutingTableCalculating = true;
63 
64  bool isHrEnabled = m_confParam.getHyperbolicState() != HYPERBOLIC_STATE_OFF;
65 
66  if ((!isHrEnabled &&
67  m_lsdb
68  .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
69  .append(std::to_string(Lsa::Type::ADJACENCY)), Lsa::Type::ADJACENCY))
70  ||
71  (isHrEnabled &&
72  m_lsdb
73  .doesLsaExist(ndn::Name{m_confParam.getRouterPrefix()}
74  .append(std::to_string(Lsa::Type::COORDINATE)), Lsa::Type::COORDINATE))) {
75  if (m_lsdb.getIsBuildAdjLsaSheduled() != 1) {
76  NLSR_LOG_TRACE("Clearing old routing table");
77  clearRoutingTable();
78  // for dry run options
79  clearDryRoutingTable();
80 
81  NLSR_LOG_DEBUG("Calculating routing table");
82 
83  // calculate Link State routing
84  if ((m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF)
85  || (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
86  calculateLsRoutingTable();
87  }
88  // calculate hyperbolic routing
89  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
90  calculateHypRoutingTable(false);
91  }
92  // calculate dry hyperbolic routing
93  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
94  calculateHypRoutingTable(true);
95  }
96  // Inform the NPT that updates have been made
97  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
98  (*afterRoutingChange)(m_rTable);
99  writeLog();
100  m_namePrefixTable.writeLog();
101  m_fib.writeLog();
102  }
103  else {
104  NLSR_LOG_DEBUG("Adjacency building is scheduled, so"
105  " routing table can not be calculated :(");
106  }
107  }
108  else {
109  NLSR_LOG_DEBUG("No Adj LSA of router itself,"
110  " so Routing table can not be calculated :(");
111  clearRoutingTable();
112  clearDryRoutingTable(); // for dry run options
113  // need to update NPT here
114  NLSR_LOG_DEBUG("Calling Update NPT With new Route");
115  (*afterRoutingChange)(m_rTable);
116  writeLog();
117  m_namePrefixTable.writeLog();
118  m_fib.writeLog();
119  // debugging purpose end
120  }
121  m_isRouteCalculationScheduled = false; // clear scheduled flag
122  m_isRoutingTableCalculating = false; // unsetting routing table calculation
123  }
124  else {
126  }
127 }
128 
129 void
130 RoutingTable::calculateLsRoutingTable()
131 {
132  NLSR_LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
133 
134  Map map;
135  map.createFromAdjLsdb(m_lsdb.getAdjLsdb().begin(), m_lsdb.getAdjLsdb().end());
136  map.writeLog();
137 
138  size_t nRouters = map.getMapSize();
139 
140  LinkStateRoutingTableCalculator calculator(nRouters);
141 
142  calculator.calculatePath(map, *this, m_confParam, m_lsdb.getAdjLsdb());
143 }
144 
145 void
146 RoutingTable::calculateHypRoutingTable(bool isDryRun)
147 {
148  Map map;
149  map.createFromCoordinateLsdb(m_lsdb.getCoordinateLsdb().begin(),
150  m_lsdb.getCoordinateLsdb().end());
151  map.writeLog();
152 
153  size_t nRouters = map.getMapSize();
154 
155  HyperbolicRoutingCalculator calculator(nRouters, isDryRun, m_confParam.getRouterPrefix());
156 
157  calculator.calculatePath(map, *this, m_lsdb, m_confParam.getAdjacencyList());
158 }
159 
160 void
162 {
163  if (m_isRouteCalculationScheduled != true) {
164  NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
165 
166  m_scheduler.scheduleEvent(m_routingCalcInterval,
167  std::bind(&RoutingTable::calculate, this));
168 
169  m_isRouteCalculationScheduled = true;
170  }
171 }
172 
173 static bool
174 routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
175 {
176  return rte.getDestination() == destRouter;
177 }
178 
179 void
180 RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
181 {
182  NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
183 
184  RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
185  if (rteChk == 0) {
186  RoutingTableEntry rte(destRouter);
187  rte.getNexthopList().addNextHop(nh);
188  m_rTable.push_back(rte);
189  }
190  else {
191  rteChk->getNexthopList().addNextHop(nh);
192  }
193 }
194 
196 RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
197 {
198  std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
199  m_rTable.end(),
200  std::bind(&routingTableEntryCompare,
201  _1, destRouter));
202  if (it != m_rTable.end()) {
203  return &(*it);
204  }
205  return 0;
206 }
207 
208 void
209 RoutingTable::writeLog()
210 {
211  NLSR_LOG_DEBUG("---------------Routing Table------------------");
212  for (const auto& rte : m_rTable) {
213  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
214  NLSR_LOG_DEBUG("Nexthops: ");
215  rte.getNexthopList().writeLog();
216  }
217 
218  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
219  NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
220  for (const auto& rte : m_dryTable) {
221  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
222  NLSR_LOG_DEBUG("Nexthops: ");
223  rte.getNexthopList().writeLog();
224  }
225  }
226 }
227 
228 void
229 RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
230 {
231  NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
232 
233  std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
234  m_dryTable.end(),
235  std::bind(&routingTableEntryCompare,
236  _1, destRouter));
237  if (it == m_dryTable.end()) {
238  RoutingTableEntry rte(destRouter);
239  rte.getNexthopList().addNextHop(nh);
240  m_dryTable.push_back(rte);
241  }
242  else {
243  (*it).getNexthopList().addNextHop(nh);
244  }
245 }
246 
247 void
248 RoutingTable::clearRoutingTable()
249 {
250  if (m_rTable.size() > 0) {
251  m_rTable.clear();
252  }
253 }
254 
255 void
256 RoutingTable::clearDryRoutingTable()
257 {
258  if (m_dryTable.size() > 0) {
259  m_dryTable.clear();
260  }
261 }
262 
263 } // namespace nlsr
void writeLog()
Definition: fib.cpp:397
void calculate()
Calculates a list of next hops for each router in the network.
A class to house all the configuration parameters for NLSR.
void writeNameLsdbLog()
Definition: lsdb.cpp:354
void calculatePath(Map &map, RoutingTable &rt, Lsdb &lsdb, AdjacencyList &adjacencies)
size_t getMapSize() const
Definition: map.hpp:115
void createFromCoordinateLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:97
void writeAdjLsdbLog()
Definition: lsdb.cpp:1299
void scheduleRoutingTableCalculation()
Schedules a calculation event in the event scheduler only if one isn&#39;t already scheduled.
AdjacencyList & getAdjacencyList()
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
const ndn::Name & getRouterPrefix() const
RoutingTable(ndn::Scheduler &scheduler, Fib &fib, Lsdb &lsdb, NamePrefixTable &namePrefixTable, ConfParameter &confParam)
void writeCorLsdbLog()
Definition: lsdb.cpp:554
bool getIsBuildAdjLsaSheduled()
Definition: lsdb.hpp:223
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
Maps names to lists of next hops, and exports this information to NFD.
Definition: fib.hpp:53
#define INIT_LOGGER(name)
Definition: logger.hpp:35
const std::list< CoordinateLsa > & getCoordinateLsdb() const
Definition: lsdb.cpp:563
static bool routingTableEntryCompare(RoutingTableEntry &rte, ndn::Name &destRouter)
const ndn::Name & getDestination() const
void writeLog()
Definition: map.cpp:81
bool doesLsaExist(const ndn::Name &key, const Lsa::Type &lsType)
Definition: lsdb.cpp:1309
void createFromAdjLsdb(IteratorType begin, IteratorType end)
Definition: map.hpp:80
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California, Arizona Board of Regents.
void calculatePath(Map &pMap, RoutingTable &rt, ConfParameter &confParam, const std::list< AdjLsa > &adjLsaList)
void addNextHop(const NextHop &nh)
Adds a next hop to the list.
void addNextHopToDryTable(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry in a dry run scenario.
RoutingTableEntry * findRoutingTableEntry(const ndn::Name &destRouter)
int32_t getHyperbolicState() const
void addNextHop(const ndn::Name &destRouter, NextHop &nh)
Adds a next hop to a routing table entry.
const std::list< AdjLsa > & getAdjLsdb() const
Definition: lsdb.cpp:809
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37