blob: 6d632f4b3aa2784b724f36e594d79e70bbbdbd6f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 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 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <iostream>
24#include <string>
25#include <list>
26
27#include "routing-table.hpp"
28#include "nlsr.hpp"
29#include "map.hpp"
akmhoque157b0a42014-05-13 00:26:37 -050030#include "conf-parameter.hpp"
akmhoque53353462014-04-22 08:43:45 -050031#include "routing-table-calculator.hpp"
32#include "routing-table-entry.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050033#include "name-prefix-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050034#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050035
36namespace nlsr {
37
akmhoque674b0b12014-05-20 14:33:28 -050038INIT_LOGGER("RoutingTable");
39
akmhoque53353462014-04-22 08:43:45 -050040using namespace std;
41
42void
43RoutingTable::calculate(Nlsr& pnlsr)
44{
45 //debugging purpose
akmhoquec8a10f72014-04-25 18:42:55 -050046 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050047 pnlsr.getLsdb().printAdjLsdb();
48 pnlsr.getLsdb().printCorLsdb();
49 pnlsr.getLsdb().printNameLsdb();
akmhoque674b0b12014-05-20 14:33:28 -050050 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050051 if (pnlsr.getIsRoutingTableCalculating() == false) {
52 //setting routing table calculation
53 pnlsr.setIsRoutingTableCalculating(true);
akmhoque53353462014-04-22 08:43:45 -050054 if (pnlsr.getLsdb().doesLsaExist(
akmhoque31d1d4b2014-05-05 22:08:14 -050055 pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
akmhoque157b0a42014-05-13 00:26:37 -050056 std::string("adjacency"))) {
57 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
akmhoque53353462014-04-22 08:43:45 -050058 std::cout << "CLearing old routing table ....." << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050059 _LOG_DEBUG("CLearing old routing table .....");
akmhoque53353462014-04-22 08:43:45 -050060 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050061 // for dry run options
62 clearDryRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050063 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050064 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
65 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050066 calculateLsRoutingTable(pnlsr);
67 }
68 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050069 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050070 calculateHypRoutingTable(pnlsr);
71 }
72 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050073 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050074 calculateHypDryRoutingTable(pnlsr);
75 }
76 //need to update NPT here
akmhoque31d1d4b2014-05-05 22:08:14 -050077 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -050078 //debugging purpose
79 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -050080 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -050081 pnlsr.getFib().print();
akmhoque674b0b12014-05-20 14:33:28 -050082 writeLog();
83 pnlsr.getNamePrefixTable().writeLog();
84 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050085 //debugging purpose end
86 }
akmhoque157b0a42014-05-13 00:26:37 -050087 else {
akmhoque53353462014-04-22 08:43:45 -050088 std::cout << "Adjacency building is scheduled, so ";
89 std::cout << "routing table can not be calculated :(" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050090 _LOG_DEBUG("Adjacency building is scheduled, so"
91 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050092 }
93 }
akmhoque157b0a42014-05-13 00:26:37 -050094 else {
akmhoque53353462014-04-22 08:43:45 -050095 std::cout << "No Adj LSA of router itself,";
96 std::cout << " so Routing table can not be calculated :(" << std::endl;
akmhoque674b0b12014-05-20 14:33:28 -050097 _LOG_DEBUG("No Adj LSA of router itself,"
98 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050099 clearRoutingTable();
100 clearDryRoutingTable(); // for dry run options
101 // need to update NPT here
102 std::cout << "Calling Update NPT With new Route" << std::endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque53353462014-04-22 08:43:45 -0500104 //debugging purpose
105 printRoutingTable();
akmhoquec8a10f72014-04-25 18:42:55 -0500106 pnlsr.getNamePrefixTable().print();
akmhoque53353462014-04-22 08:43:45 -0500107 pnlsr.getFib().print();
akmhoque674b0b12014-05-20 14:33:28 -0500108 writeLog();
109 pnlsr.getNamePrefixTable().writeLog();
110 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -0500111 //debugging purpose end
112 }
akmhoque157b0a42014-05-13 00:26:37 -0500113 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
114 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500115 }
akmhoque157b0a42014-05-13 00:26:37 -0500116 else {
akmhoque53353462014-04-22 08:43:45 -0500117 scheduleRoutingTableCalculation(pnlsr);
118 }
119}
120
121
122void
123RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
124{
125 std::cout << "RoutingTable::calculateLsRoutingTable Called" << std::endl;
126 Map vMap;
127 vMap.createFromAdjLsdb(pnlsr);
128 int numOfRouter = vMap.getMapSize();
129 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500130 lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500131}
132
133void
134RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
135{
136 Map vMap;
137 vMap.createFromAdjLsdb(pnlsr);
138 int numOfRouter = vMap.getMapSize();
139 HypRoutingTableCalculator hrtc(numOfRouter, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500140 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500141}
142
143void
144RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
145{
146 Map vMap;
147 vMap.createFromAdjLsdb(pnlsr);
148 int numOfRouter = vMap.getMapSize();
149 HypRoutingTableCalculator hrtc(numOfRouter, 1);
akmhoque157b0a42014-05-13 00:26:37 -0500150 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500151}
152
153void
154RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
155{
akmhoque157b0a42014-05-13 00:26:37 -0500156 if (pnlsr.getIsRouteCalculationScheduled() != true) {
akmhoque53353462014-04-22 08:43:45 -0500157 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
akmhoque157b0a42014-05-13 00:26:37 -0500158 ndn::bind(&RoutingTable::calculate, this,
159 ndn::ref(pnlsr)));
160 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500161 }
162}
163
164static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500165routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500166{
167 return rte.getDestination() == destRouter;
168}
169
170// function related to manipulation of routing table
171void
akmhoque31d1d4b2014-05-05 22:08:14 -0500172RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500173{
akmhoqueb6450b12014-04-24 00:01:03 -0500174 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500175 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500176 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500177 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500178 m_rTable.push_back(rte);
179 }
akmhoque157b0a42014-05-13 00:26:37 -0500180 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500181 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500182 }
183}
184
akmhoqueb6450b12014-04-24 00:01:03 -0500185RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500186RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500187{
188 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
189 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500190 ndn::bind(&routingTableEntryCompare,
191 _1, destRouter));
192 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500193 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500194 }
akmhoqueb6450b12014-04-24 00:01:03 -0500195 return 0;
akmhoque53353462014-04-22 08:43:45 -0500196}
197
198void
akmhoque674b0b12014-05-20 14:33:28 -0500199RoutingTable::writeLog()
200{
201 _LOG_DEBUG("---------------Routing Table------------------");
202 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
203 it != m_rTable.end(); ++it) {
204 _LOG_DEBUG("Destination: " << (*it).getDestination());
205 _LOG_DEBUG("Nexthops: ");
206 (*it).getNexthopList().writeLog();
207 }
208}
209
210void
akmhoque53353462014-04-22 08:43:45 -0500211RoutingTable::printRoutingTable()
212{
213 std::cout << "---------------Routing Table------------------" << std::endl;
214 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500215 it != m_rTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500216 std::cout << (*it) << std::endl;
217 }
218}
219
220
221//function related to manipulation of dry routing table
222void
akmhoque31d1d4b2014-05-05 22:08:14 -0500223RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500224{
225 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
226 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500227 ndn::bind(&routingTableEntryCompare,
228 _1, destRouter));
229 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500230 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500231 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500232 m_dryTable.push_back(rte);
233 }
akmhoque157b0a42014-05-13 00:26:37 -0500234 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500235 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500236 }
237}
238
239void
240RoutingTable::printDryRoutingTable()
241{
242 std::cout << "--------Dry Run's Routing Table--------------" << std::endl;
243 for (std::list<RoutingTableEntry>::iterator it = m_dryTable.begin() ;
akmhoque157b0a42014-05-13 00:26:37 -0500244 it != m_dryTable.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500245 cout << (*it) << endl;
246 }
247}
248
249
250void
251RoutingTable::clearRoutingTable()
252{
akmhoque157b0a42014-05-13 00:26:37 -0500253 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500254 m_rTable.clear();
255 }
256}
257
258void
259RoutingTable::clearDryRoutingTable()
260{
akmhoque157b0a42014-05-13 00:26:37 -0500261 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500262 m_dryTable.clear();
263 }
264}
265
266}//namespace nlsr
267