blob: 5db64d3c3f76fc2804bb19a9cca76f4e9435ee8f [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()) {
akmhoque31d1d4b2014-05-05 22:08:14 -050084 ndn::Name destRouter = rte.getDestination();
akmhoque53353462014-04-22 08:43:45 -050085 (*it).removeRoutingTableEntry(rte);
86 if (((*it).getRteListSize() == 0) &&
alvy49b1c0c2014-12-19 13:57:46 -060087 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + NameLsa::TYPE_STRING),
88 (NameLsa::TYPE_STRING))) &&
89 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + AdjLsa::TYPE_STRING),
90 (AdjLsa::TYPE_STRING))) &&
91 (!m_nlsr.getLsdb().doesLsaExist(destRouter.append("/" + CoordinateLsa::TYPE_STRING),
92 (CoordinateLsa::TYPE_STRING)))) {
akmhoquefdbddb12014-05-02 18:35:19 -050093 m_table.erase(it);
akmhoque31d1d4b2014-05-05 22:08:14 -050094 m_nlsr.getFib().remove(name);
akmhoque53353462014-04-22 08:43:45 -050095 }
akmhoque157b0a42014-05-13 00:26:37 -050096 else {
akmhoque53353462014-04-22 08:43:45 -050097 (*it).generateNhlfromRteList();
akmhoque31d1d4b2014-05-05 22:08:14 -050098 m_nlsr.getFib().update(name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050099 }
100 }
101}
102
103
104void
akmhoque31d1d4b2014-05-05 22:08:14 -0500105NamePrefixTable::addEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500106{
akmhoque31d1d4b2014-05-05 22:08:14 -0500107 //
akmhoqueb6450b12014-04-24 00:01:03 -0500108 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500109 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500110 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500111 addEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500112 }
akmhoque157b0a42014-05-13 00:26:37 -0500113 else {
akmhoque53353462014-04-22 08:43:45 -0500114 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500115 addEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500116 }
117}
118
119void
akmhoque31d1d4b2014-05-05 22:08:14 -0500120NamePrefixTable::removeEntry(const ndn::Name& name, const ndn::Name& destRouter)
akmhoque53353462014-04-22 08:43:45 -0500121{
akmhoque31d1d4b2014-05-05 22:08:14 -0500122 //
akmhoqueb6450b12014-04-24 00:01:03 -0500123 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500124 m_nlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoque157b0a42014-05-13 00:26:37 -0500125 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500126 removeEntry(name, *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500127 }
akmhoque157b0a42014-05-13 00:26:37 -0500128 else {
akmhoque53353462014-04-22 08:43:45 -0500129 RoutingTableEntry rte(destRouter);
akmhoque31d1d4b2014-05-05 22:08:14 -0500130 removeEntry(name, rte);
akmhoque53353462014-04-22 08:43:45 -0500131 }
132}
133
134void
akmhoque31d1d4b2014-05-05 22:08:14 -0500135NamePrefixTable::updateWithNewRoute()
akmhoque53353462014-04-22 08:43:45 -0500136{
akmhoquefdbddb12014-05-02 18:35:19 -0500137 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500138 it != m_table.end(); ++it) {
akmhoque53353462014-04-22 08:43:45 -0500139 std::list<RoutingTableEntry> rteList = (*it).getRteList();
140 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500141 rteit != rteList.end(); ++rteit) {
akmhoqueb6450b12014-04-24 00:01:03 -0500142 RoutingTableEntry* rteCheck =
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 m_nlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoque157b0a42014-05-13 00:26:37 -0500144 if (rteCheck != 0) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500145 addEntry((*it).getNamePrefix(), *(rteCheck));
akmhoque53353462014-04-22 08:43:45 -0500146 }
akmhoque157b0a42014-05-13 00:26:37 -0500147 else {
akmhoque53353462014-04-22 08:43:45 -0500148 RoutingTableEntry rte((*rteit).getDestination());
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 addEntry((*it).getNamePrefix(), rte);
akmhoque53353462014-04-22 08:43:45 -0500150 }
151 }
152 }
153}
154
155void
akmhoque674b0b12014-05-20 14:33:28 -0500156NamePrefixTable::writeLog()
157{
158 _LOG_DEBUG("----------------NPT----------------------");
159 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
160 it != m_table.end();
161 ++it) {
162 (*it).writeLog();
163 }
164}
165
akmhoque53353462014-04-22 08:43:45 -0500166} //namespace nlsr