routing-table.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2019, The University of Memphis,
4  * Regents of the University of California
5  *
6  * This file is part of NLSR (Named-data Link State Routing).
7  * See AUTHORS.md for complete list of NLSR authors and contributors.
8  *
9  * NLSR is free software: you can redistribute it and/or modify it under the terms
10  * of the GNU General Public License as published by the Free Software Foundation,
11  * either version 3 of the License, or (at your option) any later version.
12  *
13  * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19  **/
20 
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) {
164  NLSR_LOG_DEBUG("Scheduling routing table calculation in " << m_routingCalcInterval);
165  m_scheduler.schedule(m_routingCalcInterval, [this] { calculate(); });
166  m_isRouteCalculationScheduled = true;
167  }
168 }
169 
170 static bool
171 routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
172 {
173  return rte.getDestination() == destRouter;
174 }
175 
176 void
177 RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
178 {
179  NLSR_LOG_DEBUG("Adding " << nh << " for destination: " << destRouter);
180 
181  RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
182  if (rteChk == nullptr) {
183  RoutingTableEntry rte(destRouter);
184  rte.getNexthopList().addNextHop(nh);
185  m_rTable.push_back(rte);
186  }
187  else {
188  rteChk->getNexthopList().addNextHop(nh);
189  }
190 }
191 
193 RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
194 {
195  auto it = std::find_if(m_rTable.begin(), m_rTable.end(),
196  std::bind(&routingTableEntryCompare, _1, destRouter));
197  if (it != m_rTable.end()) {
198  return &(*it);
199  }
200  return nullptr;
201 }
202 
203 void
204 RoutingTable::writeLog()
205 {
206  NLSR_LOG_DEBUG("---------------Routing Table------------------");
207  for (const auto& rte : m_rTable) {
208  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
209  NLSR_LOG_DEBUG("Nexthops: ");
210  rte.getNexthopList().writeLog();
211  }
212 
213  if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
214  NLSR_LOG_DEBUG("--------Hyperbolic Routing Table(Dry)---------");
215  for (const auto& rte : m_dryTable) {
216  NLSR_LOG_DEBUG("Destination: " << rte.getDestination());
217  NLSR_LOG_DEBUG("Nexthops: ");
218  rte.getNexthopList().writeLog();
219  }
220  }
221 }
222 
223 void
224 RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
225 {
226  NLSR_LOG_DEBUG("Adding " << nh << " to dry table for destination: " << destRouter);
227 
228  auto it = std::find_if(m_dryTable.begin(), m_dryTable.end(),
229  std::bind(&routingTableEntryCompare, _1, destRouter));
230  if (it == m_dryTable.end()) {
231  RoutingTableEntry rte(destRouter);
232  rte.getNexthopList().addNextHop(nh);
233  m_dryTable.push_back(rte);
234  }
235  else {
236  it->getNexthopList().addNextHop(nh);
237  }
238 }
239 
240 void
241 RoutingTable::clearRoutingTable()
242 {
243  if (m_rTable.size() > 0) {
244  m_rTable.clear();
245  }
246 }
247 
248 void
249 RoutingTable::clearDryRoutingTable()
250 {
251  if (m_dryTable.size() > 0) {
252  m_dryTable.clear();
253  }
254 }
255 
256 } // namespace nlsr
void writeLog()
Definition: fib.cpp:390
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:335
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:1265
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:524
bool getIsBuildAdjLsaSheduled()
Definition: lsdb.hpp:205
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:533
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:1275
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:768
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37