blob: 613371ff0c307cc30f8a7f5fe06c65bcfdb1b62e [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{
akmhoque2f423352014-06-03 11:49:35 -050045 pnlsr.getLsdb().writeCorLsdbLog();
46 pnlsr.getLsdb().writeNameLsdbLog();
47 pnlsr.getLsdb().writeAdjLsdbLog();
akmhoque674b0b12014-05-20 14:33:28 -050048 pnlsr.getNamePrefixTable().writeLog();
akmhoque157b0a42014-05-13 00:26:37 -050049 if (pnlsr.getIsRoutingTableCalculating() == false) {
50 //setting routing table calculation
51 pnlsr.setIsRoutingTableCalculating(true);
akmhoque53353462014-04-22 08:43:45 -050052 if (pnlsr.getLsdb().doesLsaExist(
akmhoque31d1d4b2014-05-05 22:08:14 -050053 pnlsr.getConfParameter().getRouterPrefix().toUri() + "/" + "adjacency",
akmhoque157b0a42014-05-13 00:26:37 -050054 std::string("adjacency"))) {
55 if (pnlsr.getIsBuildAdjLsaSheduled() != 1) {
akmhoque674b0b12014-05-20 14:33:28 -050056 _LOG_DEBUG("CLearing old routing table .....");
akmhoque53353462014-04-22 08:43:45 -050057 clearRoutingTable();
akmhoque157b0a42014-05-13 00:26:37 -050058 // for dry run options
59 clearDryRoutingTable();
akmhoque53353462014-04-22 08:43:45 -050060 // calculate Link State routing
akmhoque157b0a42014-05-13 00:26:37 -050061 if ((pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF)
62 || (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN)) {
akmhoque53353462014-04-22 08:43:45 -050063 calculateLsRoutingTable(pnlsr);
64 }
65 //calculate hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050066 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque53353462014-04-22 08:43:45 -050067 calculateHypRoutingTable(pnlsr);
68 }
69 //calculate dry hyperbolic routing
akmhoque157b0a42014-05-13 00:26:37 -050070 if (pnlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
akmhoque53353462014-04-22 08:43:45 -050071 calculateHypDryRoutingTable(pnlsr);
72 }
73 //need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -050074 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050075 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque674b0b12014-05-20 14:33:28 -050076 writeLog();
77 pnlsr.getNamePrefixTable().writeLog();
78 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050079 }
akmhoque157b0a42014-05-13 00:26:37 -050080 else {
akmhoque674b0b12014-05-20 14:33:28 -050081 _LOG_DEBUG("Adjacency building is scheduled, so"
82 " routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050083 }
84 }
akmhoque157b0a42014-05-13 00:26:37 -050085 else {
akmhoque674b0b12014-05-20 14:33:28 -050086 _LOG_DEBUG("No Adj LSA of router itself,"
87 " so Routing table can not be calculated :(");
akmhoque53353462014-04-22 08:43:45 -050088 clearRoutingTable();
89 clearDryRoutingTable(); // for dry run options
90 // need to update NPT here
akmhoque2f423352014-06-03 11:49:35 -050091 _LOG_DEBUG("Calling Update NPT With new Route");
akmhoque31d1d4b2014-05-05 22:08:14 -050092 pnlsr.getNamePrefixTable().updateWithNewRoute();
akmhoque674b0b12014-05-20 14:33:28 -050093 writeLog();
94 pnlsr.getNamePrefixTable().writeLog();
95 pnlsr.getFib().writeLog();
akmhoque53353462014-04-22 08:43:45 -050096 //debugging purpose end
97 }
akmhoque157b0a42014-05-13 00:26:37 -050098 pnlsr.setIsRouteCalculationScheduled(false); //clear scheduled flag
99 pnlsr.setIsRoutingTableCalculating(false); //unsetting routing table calculation
akmhoque53353462014-04-22 08:43:45 -0500100 }
akmhoque157b0a42014-05-13 00:26:37 -0500101 else {
akmhoque53353462014-04-22 08:43:45 -0500102 scheduleRoutingTableCalculation(pnlsr);
103 }
104}
105
106
107void
108RoutingTable::calculateLsRoutingTable(Nlsr& pnlsr)
109{
akmhoque2f423352014-06-03 11:49:35 -0500110 _LOG_DEBUG("RoutingTable::calculateLsRoutingTable Called");
akmhoque53353462014-04-22 08:43:45 -0500111 Map vMap;
112 vMap.createFromAdjLsdb(pnlsr);
akmhoque2f423352014-06-03 11:49:35 -0500113 vMap.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500114 int numOfRouter = vMap.getMapSize();
115 LinkStateRoutingTableCalculator lsrtc(numOfRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500116 lsrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500117}
118
119void
120RoutingTable::calculateHypRoutingTable(Nlsr& pnlsr)
121{
122 Map vMap;
123 vMap.createFromAdjLsdb(pnlsr);
akmhoque2f423352014-06-03 11:49:35 -0500124 vMap.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500125 int numOfRouter = vMap.getMapSize();
126 HypRoutingTableCalculator hrtc(numOfRouter, 0);
akmhoque157b0a42014-05-13 00:26:37 -0500127 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500128}
129
130void
131RoutingTable::calculateHypDryRoutingTable(Nlsr& pnlsr)
132{
133 Map vMap;
134 vMap.createFromAdjLsdb(pnlsr);
akmhoque2f423352014-06-03 11:49:35 -0500135 vMap.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500136 int numOfRouter = vMap.getMapSize();
137 HypRoutingTableCalculator hrtc(numOfRouter, 1);
akmhoque157b0a42014-05-13 00:26:37 -0500138 hrtc.calculatePath(vMap, ndn::ref(*this), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500139}
140
141void
142RoutingTable::scheduleRoutingTableCalculation(Nlsr& pnlsr)
143{
akmhoque157b0a42014-05-13 00:26:37 -0500144 if (pnlsr.getIsRouteCalculationScheduled() != true) {
akmhoque53353462014-04-22 08:43:45 -0500145 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
akmhoque157b0a42014-05-13 00:26:37 -0500146 ndn::bind(&RoutingTable::calculate, this,
147 ndn::ref(pnlsr)));
148 pnlsr.setIsRouteCalculationScheduled(true);
akmhoque53353462014-04-22 08:43:45 -0500149 }
150}
151
152static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500153routingTableEntryCompare(RoutingTableEntry& rte, ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500154{
155 return rte.getDestination() == destRouter;
156}
157
158// function related to manipulation of routing table
159void
akmhoque31d1d4b2014-05-05 22:08:14 -0500160RoutingTable::addNextHop(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500161{
akmhoqueb6450b12014-04-24 00:01:03 -0500162 RoutingTableEntry* rteChk = findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500163 if (rteChk == 0) {
akmhoque53353462014-04-22 08:43:45 -0500164 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500165 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500166 m_rTable.push_back(rte);
167 }
akmhoque157b0a42014-05-13 00:26:37 -0500168 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500169 rteChk->getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500170 }
171}
172
akmhoqueb6450b12014-04-24 00:01:03 -0500173RoutingTableEntry*
akmhoque31d1d4b2014-05-05 22:08:14 -0500174RoutingTable::findRoutingTableEntry(const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500175{
176 std::list<RoutingTableEntry>::iterator it = std::find_if(m_rTable.begin(),
177 m_rTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500178 ndn::bind(&routingTableEntryCompare,
179 _1, destRouter));
180 if (it != m_rTable.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500181 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500182 }
akmhoqueb6450b12014-04-24 00:01:03 -0500183 return 0;
akmhoque53353462014-04-22 08:43:45 -0500184}
185
186void
akmhoque674b0b12014-05-20 14:33:28 -0500187RoutingTable::writeLog()
188{
189 _LOG_DEBUG("---------------Routing Table------------------");
190 for (std::list<RoutingTableEntry>::iterator it = m_rTable.begin() ;
191 it != m_rTable.end(); ++it) {
192 _LOG_DEBUG("Destination: " << (*it).getDestination());
193 _LOG_DEBUG("Nexthops: ");
194 (*it).getNexthopList().writeLog();
195 }
196}
197
akmhoque53353462014-04-22 08:43:45 -0500198//function related to manipulation of dry routing table
199void
akmhoque31d1d4b2014-05-05 22:08:14 -0500200RoutingTable::addNextHopToDryTable(const ndn::Name& destRouter, NextHop& nh)
akmhoque53353462014-04-22 08:43:45 -0500201{
202 std::list<RoutingTableEntry>::iterator it = std::find_if(m_dryTable.begin(),
203 m_dryTable.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500204 ndn::bind(&routingTableEntryCompare,
205 _1, destRouter));
206 if (it == m_dryTable.end()) {
akmhoque53353462014-04-22 08:43:45 -0500207 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500208 rte.getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500209 m_dryTable.push_back(rte);
210 }
akmhoque157b0a42014-05-13 00:26:37 -0500211 else {
akmhoquefdbddb12014-05-02 18:35:19 -0500212 (*it).getNexthopList().addNextHop(nh);
akmhoque53353462014-04-22 08:43:45 -0500213 }
214}
215
216void
akmhoque53353462014-04-22 08:43:45 -0500217RoutingTable::clearRoutingTable()
218{
akmhoque157b0a42014-05-13 00:26:37 -0500219 if (m_rTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500220 m_rTable.clear();
221 }
222}
223
224void
225RoutingTable::clearDryRoutingTable()
226{
akmhoque157b0a42014-05-13 00:26:37 -0500227 if (m_dryTable.size() > 0) {
akmhoque53353462014-04-22 08:43:45 -0500228 m_dryTable.clear();
229 }
230}
231
232}//namespace nlsr
233