blob: ba7dd7543ac6d0aa8427c165b1ce3e72d0194f7b [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include <list>
23#include <utility>
24#include <algorithm>
25
akmhoque53353462014-04-22 08:43:45 -050026#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050027#include "name-prefix-table.hpp"
28#include "name-prefix-table-entry.hpp"
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include "routing-table.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050030#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050031
32namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("NamePrefixTable");
35
akmhoque53353462014-04-22 08:43:45 -050036using namespace std;
37
38static bool
akmhoque31d1d4b2014-05-05 22:08:14 -050039npteCompare(NamePrefixTableEntry& npte, const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -050040{
41 return npte.getNamePrefix() == name;
42}
43
44
45
46void
akmhoque31d1d4b2014-05-05 22:08:14 -050047NamePrefixTable::addEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050048{
akmhoquefdbddb12014-05-02 18:35:19 -050049 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050050 m_table.end(),
51 ndn::bind(&npteCompare, _1, name));
52 if (it == m_table.end()) {
akmhoquec8a10f72014-04-25 18:42:55 -050053 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050054 newEntry.addRoutingTableEntry(rte);
55 newEntry.generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050056 newEntry.getNexthopList().sort();
57 m_table.push_back(newEntry);
akmhoque157b0a42014-05-13 00:26:37 -050058 if (rte.getNexthopList().getSize() > 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -050059 m_nlsr.getFib().update(name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050060 }
61 }
akmhoque157b0a42014-05-13 00:26:37 -050062 else {
63 if (rte.getNexthopList().getSize() > 0) {
akmhoque53353462014-04-22 08:43:45 -050064 (*it).addRoutingTableEntry(rte);
65 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050066 (*it).getNexthopList().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -050067 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050068 }
akmhoque157b0a42014-05-13 00:26:37 -050069 else {
akmhoque53353462014-04-22 08:43:45 -050070 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050071 (*it).getNexthopList().reset();
akmhoque31d1d4b2014-05-05 22:08:14 -050072 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050073 }
74 }
75}
76
77void
akmhoque31d1d4b2014-05-05 22:08:14 -050078NamePrefixTable::removeEntry(const ndn::Name& name, RoutingTableEntry& rte)
akmhoque53353462014-04-22 08:43:45 -050079{
akmhoquefdbddb12014-05-02 18:35:19 -050080 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
akmhoque157b0a42014-05-13 00:26:37 -050081 m_table.end(),
82 ndn::bind(&npteCompare, _1, name));
83 if (it != m_table.end()) {
Vince Lehman9630fbf2015-05-05 12:33:44 -050084 const ndn::Name destRouter = rte.getDestination();
85 it->removeRoutingTableEntry(rte);
86
87 if (it->getRteListSize() == 0 &&
88 !m_nlsr.getLsdb().doesLsaExist(ndn::Name(destRouter).append(NameLsa::TYPE_STRING),
89 NameLsa::TYPE_STRING) &&
90 !m_nlsr.getLsdb().doesLsaExist(ndn::Name(destRouter).append(AdjLsa::TYPE_STRING),
91 AdjLsa::TYPE_STRING) &&
92 !m_nlsr.getLsdb().doesLsaExist(ndn::Name(destRouter).append(CoordinateLsa::TYPE_STRING),
93 CoordinateLsa::TYPE_STRING))
94 {
akmhoquefdbddb12014-05-02 18:35:19 -050095 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050096 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050097 }
akmhoque157b0a42014-05-13 00:26:37 -050098 else {
akmhoque53353462014-04-22 08:43:45 -050099 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -0500101 }
102 }
103}
104
105
106void
akmhoque31d1d4b2014-05-05 22:08:14 -0500107NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500108{
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 //
akmhoqueb6450b12014-04-24 00:01:03 -0500110 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500112 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500113 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500114 }
akmhoque157b0a42014-05-13 00:26:37 -0500115 else {
akmhoque53353462014-04-22 08:43:45 -0500116 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500118 }
119}
120
121void
akmhoque31d1d4b2014-05-05 22:08:14 -0500122NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500123{
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 //
akmhoqueb6450b12014-04-24 00:01:03 -0500125 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500127 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500128 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500129 }
akmhoque157b0a42014-05-13 00:26:37 -0500130 else {
akmhoque53353462014-04-22 08:43:45 -0500131 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500132 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500133 }
134}
135
136void
akmhoque31d1d4b2014-05-05 22:08:14 -0500137NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500138{
akmhoquefdbddb12014-05-02 18:35:19 -0500139 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500140 it != m_table.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500141 std::list<RoutingTableEntry> rteList = (*it).getRteList();
142 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500143 rteit != rteList.end(); ++rteit) {
akmhoqueb6450b12014-04-24 00:01:03 -0500144 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoque157b0a42014-05-13 00:26:37 -0500146 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500148 }
akmhoque157b0a42014-05-13 00:26:37 -0500149 else {
akmhoque53353462014-04-22 08:43:45 -0500150 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500151 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500152 }
153 }
154 }
155}
156
157void
akmhoque674b0b12014-05-20 14:33:28 -0500158NamePrefixTable::writeLog()
159{
160 _LOG_DEBUG("----------------NPT----------------------");
161 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
162 it != m_table.end();
163 ++it) {
164 (*it).writeLog();
165 }
166}
167
akmhoque53353462014-04-22 08:43:45 -0500168} //namespace nlsr