blob: e6a7ccf9321d4503411bd479f822f2526ee64238 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
2#include <utility>
3#include <algorithm>
4
akmhoque53353462014-04-22 08:43:45 -05005#include "nlsr.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -05006#include "name-prefix-table.hpp"
7#include "name-prefix-table-entry.hpp"
8
akmhoque53353462014-04-22 08:43:45 -05009
10
11namespace nlsr {
12
13using namespace std;
14
15static bool
akmhoquefdbddb12014-05-02 18:35:19 -050016npteCompare(NamePrefixTableEntry& npte, const string& name)
akmhoque53353462014-04-22 08:43:45 -050017{
18 return npte.getNamePrefix() == name;
19}
20
21
22
23void
akmhoquefdbddb12014-05-02 18:35:19 -050024NamePrefixTable::addEntry(const string& name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -050025{
akmhoquefdbddb12014-05-02 18:35:19 -050026 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
27 m_table.end(), bind(&npteCompare, _1, name));
28 if (it == m_table.end())
akmhoque53353462014-04-22 08:43:45 -050029 {
akmhoquec8a10f72014-04-25 18:42:55 -050030 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050031 newEntry.addRoutingTableEntry(rte);
32 newEntry.generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050033 newEntry.getNexthopList().sort();
34 m_table.push_back(newEntry);
35 if (rte.getNexthopList().getSize() > 0)
akmhoque53353462014-04-22 08:43:45 -050036 {
akmhoquefdbddb12014-05-02 18:35:19 -050037 pnlsr.getFib().update(pnlsr, name, newEntry.getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050038 }
39 }
40 else
41 {
akmhoquefdbddb12014-05-02 18:35:19 -050042 if (rte.getNexthopList().getSize() > 0)
akmhoque53353462014-04-22 08:43:45 -050043 {
44 (*it).addRoutingTableEntry(rte);
45 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050046 (*it).getNexthopList().sort();
47 pnlsr.getFib().update(pnlsr, name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050048 }
49 else
50 {
51 (*it).resetRteListNextHop();
akmhoquefdbddb12014-05-02 18:35:19 -050052 (*it).getNexthopList().reset();
akmhoque53353462014-04-22 08:43:45 -050053 pnlsr.getFib().remove(pnlsr, name);
54 }
55 }
56}
57
58void
akmhoquefdbddb12014-05-02 18:35:19 -050059NamePrefixTable::removeEntry(const string& name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -050060{
akmhoquefdbddb12014-05-02 18:35:19 -050061 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_table.begin(),
62 m_table.end(), bind(&npteCompare, _1, name));
63 if (it != m_table.end())
akmhoque53353462014-04-22 08:43:45 -050064 {
65 string destRouter = rte.getDestination();
66 (*it).removeRoutingTableEntry(rte);
67 if (((*it).getRteListSize() == 0) &&
68 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/1", 1)) &&
69 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/2", 2)) &&
70 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/3", 3)))
71 {
akmhoquefdbddb12014-05-02 18:35:19 -050072 m_table.erase(it);
akmhoque53353462014-04-22 08:43:45 -050073 pnlsr.getFib().remove(pnlsr, name);
74 }
75 else
76 {
77 (*it).generateNhlfromRteList();
akmhoquefdbddb12014-05-02 18:35:19 -050078 pnlsr.getFib().update(pnlsr, name, (*it).getNexthopList());
akmhoque53353462014-04-22 08:43:45 -050079 }
80 }
81}
82
83
84void
akmhoquefdbddb12014-05-02 18:35:19 -050085NamePrefixTable::addEntry(const string& name, const string& destRouter, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -050086{
akmhoqueb6450b12014-04-24 00:01:03 -050087 RoutingTableEntry* rteCheck =
akmhoque53353462014-04-22 08:43:45 -050088 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoqueb6450b12014-04-24 00:01:03 -050089 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -050090 {
akmhoquefdbddb12014-05-02 18:35:19 -050091 addEntry(name, *(rteCheck) , pnlsr);
akmhoque53353462014-04-22 08:43:45 -050092 }
93 else
94 {
95 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -050096 addEntry(name, rte, pnlsr);
akmhoque53353462014-04-22 08:43:45 -050097 }
98}
99
100void
akmhoquefdbddb12014-05-02 18:35:19 -0500101NamePrefixTable::removeEntry(const string& name, const string& destRouter, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500102{
akmhoqueb6450b12014-04-24 00:01:03 -0500103 RoutingTableEntry* rteCheck =
akmhoque53353462014-04-22 08:43:45 -0500104 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
akmhoqueb6450b12014-04-24 00:01:03 -0500105 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500106 {
akmhoquefdbddb12014-05-02 18:35:19 -0500107 removeEntry(name, *(rteCheck), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500108 }
109 else
110 {
111 RoutingTableEntry rte(destRouter);
akmhoquefdbddb12014-05-02 18:35:19 -0500112 removeEntry(name, rte, pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500113 }
114}
115
116void
akmhoquec8a10f72014-04-25 18:42:55 -0500117NamePrefixTable::updateWithNewRoute(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500118{
akmhoquefdbddb12014-05-02 18:35:19 -0500119 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
120 it != m_table.end(); ++it)
akmhoque53353462014-04-22 08:43:45 -0500121 {
122 std::list<RoutingTableEntry> rteList = (*it).getRteList();
123 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
124 rteit != rteList.end(); ++rteit)
125 {
akmhoqueb6450b12014-04-24 00:01:03 -0500126 RoutingTableEntry* rteCheck =
akmhoque53353462014-04-22 08:43:45 -0500127 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoqueb6450b12014-04-24 00:01:03 -0500128 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500129 {
akmhoquefdbddb12014-05-02 18:35:19 -0500130 addEntry((*it).getNamePrefix(), *(rteCheck), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500131 }
132 else
133 {
134 RoutingTableEntry rte((*rteit).getDestination());
akmhoquefdbddb12014-05-02 18:35:19 -0500135 addEntry((*it).getNamePrefix(), rte, pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500136 }
137 }
138 }
139}
140
141void
akmhoquec8a10f72014-04-25 18:42:55 -0500142NamePrefixTable::print()
akmhoque53353462014-04-22 08:43:45 -0500143{
144 std::cout << "----------------NPT----------------------" << std::endl;
akmhoquefdbddb12014-05-02 18:35:19 -0500145 for (std::list<NamePrefixTableEntry>::iterator it = m_table.begin();
146 it != m_table.end();
akmhoque53353462014-04-22 08:43:45 -0500147 ++it)
148 {
149 cout << (*it) << endl;
150 }
151}
152
153} //namespace nlsr