blob: 665dbb2ee8826224cb6b6162015ce094a7ee03a1 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <list>
2#include <utility>
3#include <algorithm>
4
5#include "npt.hpp"
6#include "npte.hpp"
7#include "nlsr.hpp"
8
9
10namespace nlsr {
11
12using namespace std;
13
14static bool
15npteCompare(Npte& npte, string& name)
16{
17 return npte.getNamePrefix() == name;
18}
19
20
21
22void
23Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
24{
25 std::list<Npte>::iterator it = std::find_if(m_npteList.begin(),
26 m_npteList.end(), bind(&npteCompare, _1, name));
27 if (it == m_npteList.end())
28 {
29 Npte newEntry(name);
30 newEntry.addRoutingTableEntry(rte);
31 newEntry.generateNhlfromRteList();
32 newEntry.getNhl().sort();
33 m_npteList.push_back(newEntry);
34 if (rte.getNhl().getSize() > 0)
35 {
36 pnlsr.getFib().update(pnlsr, name, newEntry.getNhl());
37 }
38 }
39 else
40 {
41 if (rte.getNhl().getSize() > 0)
42 {
43 (*it).addRoutingTableEntry(rte);
44 (*it).generateNhlfromRteList();
45 (*it).getNhl().sort();
46 pnlsr.getFib().update(pnlsr, name, (*it).getNhl());
47 }
48 else
49 {
50 (*it).resetRteListNextHop();
51 (*it).getNhl().reset();
52 pnlsr.getFib().remove(pnlsr, name);
53 }
54 }
55}
56
57void
58Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
59{
60 std::list<Npte>::iterator it = std::find_if(m_npteList.begin(),
61 m_npteList.end(), bind(&npteCompare, _1, name));
62 if (it != m_npteList.end())
63 {
64 string destRouter = rte.getDestination();
65 (*it).removeRoutingTableEntry(rte);
66 if (((*it).getRteListSize() == 0) &&
67 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/1", 1)) &&
68 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/2", 2)) &&
69 (!pnlsr.getLsdb().doesLsaExist(destRouter + "/3", 3)))
70 {
71 m_npteList.erase(it);
72 pnlsr.getFib().remove(pnlsr, name);
73 }
74 else
75 {
76 (*it).generateNhlfromRteList();
77 pnlsr.getFib().update(pnlsr, name, (*it).getNhl());
78 }
79 }
80}
81
82
83void
84Npt::addNpteByDestName(string name, string destRouter, Nlsr& pnlsr)
85{
86 std::pair<RoutingTableEntry&, bool> rteCheck =
87 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
88 if (rteCheck.second)
89 {
90 addNpte(name, rteCheck.first, pnlsr);
91 }
92 else
93 {
94 RoutingTableEntry rte(destRouter);
95 addNpte(name, rte, pnlsr);
96 }
97}
98
99void
100Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
101{
102 std::pair<RoutingTableEntry&, bool> rteCheck =
103 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
104 if (rteCheck.second)
105 {
106 removeNpte(name, rteCheck.first, pnlsr);
107 }
108 else
109 {
110 RoutingTableEntry rte(destRouter);
111 removeNpte(name, rte, pnlsr);
112 }
113}
114
115void
116Npt::updateWithNewRoute(Nlsr& pnlsr)
117{
118 for (std::list<Npte>::iterator it = m_npteList.begin(); it != m_npteList.end();
119 ++it)
120 {
121 std::list<RoutingTableEntry> rteList = (*it).getRteList();
122 for (std::list<RoutingTableEntry>::iterator rteit = rteList.begin();
123 rteit != rteList.end(); ++rteit)
124 {
125 std::pair<RoutingTableEntry&, bool> rteCheck =
126 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
127 if (rteCheck.second)
128 {
129 addNpte((*it).getNamePrefix(), rteCheck.first, pnlsr);
130 }
131 else
132 {
133 RoutingTableEntry rte((*rteit).getDestination());
134 addNpte((*it).getNamePrefix(), rte, pnlsr);
135 }
136 }
137 }
138}
139
140void
141Npt::print()
142{
143 std::cout << "----------------NPT----------------------" << std::endl;
144 for (std::list<Npte>::iterator it = m_npteList.begin(); it != m_npteList.end();
145 ++it)
146 {
147 cout << (*it) << endl;
148 }
149}
150
151} //namespace nlsr