blob: e37751bd8aba5ef0b53e2f1a7f8f64015eaad79c [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
akmhoquec8a10f72014-04-25 18:42:55 -050016npteCompare(NamePrefixTableEntry& npte, string& name)
akmhoque53353462014-04-22 08:43:45 -050017{
18 return npte.getNamePrefix() == name;
19}
20
21
22
23void
akmhoquec8a10f72014-04-25 18:42:55 -050024NamePrefixTable::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -050025{
akmhoquec8a10f72014-04-25 18:42:55 -050026 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_npteList.begin(),
27 m_npteList.end(), bind(&npteCompare, _1, name));
akmhoque53353462014-04-22 08:43:45 -050028 if (it == m_npteList.end())
29 {
akmhoquec8a10f72014-04-25 18:42:55 -050030 NamePrefixTableEntry newEntry(name);
akmhoque53353462014-04-22 08:43:45 -050031 newEntry.addRoutingTableEntry(rte);
32 newEntry.generateNhlfromRteList();
33 newEntry.getNhl().sort();
34 m_npteList.push_back(newEntry);
35 if (rte.getNhl().getSize() > 0)
36 {
37 pnlsr.getFib().update(pnlsr, name, newEntry.getNhl());
38 }
39 }
40 else
41 {
42 if (rte.getNhl().getSize() > 0)
43 {
44 (*it).addRoutingTableEntry(rte);
45 (*it).generateNhlfromRteList();
46 (*it).getNhl().sort();
47 pnlsr.getFib().update(pnlsr, name, (*it).getNhl());
48 }
49 else
50 {
51 (*it).resetRteListNextHop();
52 (*it).getNhl().reset();
53 pnlsr.getFib().remove(pnlsr, name);
54 }
55 }
56}
57
58void
akmhoquec8a10f72014-04-25 18:42:55 -050059NamePrefixTable::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -050060{
akmhoquec8a10f72014-04-25 18:42:55 -050061 std::list<NamePrefixTableEntry>::iterator it = std::find_if(m_npteList.begin(),
62 m_npteList.end(), bind(&npteCompare, _1, name));
akmhoque53353462014-04-22 08:43:45 -050063 if (it != m_npteList.end())
64 {
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 {
72 m_npteList.erase(it);
73 pnlsr.getFib().remove(pnlsr, name);
74 }
75 else
76 {
77 (*it).generateNhlfromRteList();
78 pnlsr.getFib().update(pnlsr, name, (*it).getNhl());
79 }
80 }
81}
82
83
84void
akmhoquec8a10f72014-04-25 18:42:55 -050085NamePrefixTable::addNpteByDestName(string name, 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 {
akmhoqueb6450b12014-04-24 00:01:03 -050091 addNpte(name, *(rteCheck) , pnlsr);
akmhoque53353462014-04-22 08:43:45 -050092 }
93 else
94 {
95 RoutingTableEntry rte(destRouter);
96 addNpte(name, rte, pnlsr);
97 }
98}
99
100void
akmhoquec8a10f72014-04-25 18:42:55 -0500101NamePrefixTable::removeNpte(string name, 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 {
akmhoqueb6450b12014-04-24 00:01:03 -0500107 removeNpte(name, *(rteCheck), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500108 }
109 else
110 {
111 RoutingTableEntry rte(destRouter);
112 removeNpte(name, rte, pnlsr);
113 }
114}
115
116void
akmhoquec8a10f72014-04-25 18:42:55 -0500117NamePrefixTable::updateWithNewRoute(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500118{
akmhoquec8a10f72014-04-25 18:42:55 -0500119 for (std::list<NamePrefixTableEntry>::iterator it = m_npteList.begin();
120 it != m_npteList.end();
akmhoque53353462014-04-22 08:43:45 -0500121 ++it)
122 {
123 std::list<RoutingTableEntry> rteList = (*it).getRteList();
124 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
125 rteit != rteList.end(); ++rteit)
126 {
akmhoqueb6450b12014-04-24 00:01:03 -0500127 RoutingTableEntry* rteCheck =
akmhoque53353462014-04-22 08:43:45 -0500128 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
akmhoqueb6450b12014-04-24 00:01:03 -0500129 if (rteCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500130 {
akmhoqueb6450b12014-04-24 00:01:03 -0500131 addNpte((*it).getNamePrefix(), *(rteCheck), pnlsr);
akmhoque53353462014-04-22 08:43:45 -0500132 }
133 else
134 {
135 RoutingTableEntry rte((*rteit).getDestination());
136 addNpte((*it).getNamePrefix(), rte, pnlsr);
137 }
138 }
139 }
140}
141
142void
akmhoquec8a10f72014-04-25 18:42:55 -0500143NamePrefixTable::print()
akmhoque53353462014-04-22 08:43:45 -0500144{
145 std::cout << "----------------NPT----------------------" << std::endl;
akmhoquec8a10f72014-04-25 18:42:55 -0500146 for (std::list<NamePrefixTableEntry>::iterator it = m_npteList.begin();
147 it != m_npteList.end();
akmhoque53353462014-04-22 08:43:45 -0500148 ++it)
149 {
150 cout << (*it) << endl;
151 }
152}
153
154} //namespace nlsr