blob: 01b98e5a7044e2397e79504700aa666ceb2c4cc4 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include <list>
2#include <utility>
3#include <algorithm>
4
5#include "nlsr_npt.hpp"
6#include "nlsr_npte.hpp"
7#include "nlsr.hpp"
8
9using namespace std;
10
11static bool
12npteCompare(Npte& npte, string& name){
13 return npte.getNamePrefix()==name;
14}
15
16// Following two methods will update FIB with response to change in NPT
17
18void
akmhoque1a481092014-02-19 16:34:22 -060019Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060020{
21 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
22 npteList.end(), bind(&npteCompare, _1, name));
23
24 if ( it == npteList.end() )
25 {
26 Npte newEntry( name);
27 newEntry.addRoutingTableEntry(rte);
28 newEntry.generateNhlfromRteList();
29 npteList.push_back(newEntry);
30 // update FIB here with nhl list newEntry.getNhl()
akmhoque85d88332014-02-17 21:11:21 -060031 pnlsr.getFib().updateFib(pnlsr, name,newEntry.getNhl(),
akmhoque298385a2014-02-13 14:13:09 -060032 pnlsr.getConfParameter().getMaxFacesPerPrefix());
33 }
34 else
35 {
36 (*it).addRoutingTableEntry(rte);
37 (*it).generateNhlfromRteList();
38 // update FIB here with nhl list from (*it).getNhl()
akmhoque85d88332014-02-17 21:11:21 -060039 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl() ,
akmhoque298385a2014-02-13 14:13:09 -060040 pnlsr.getConfParameter().getMaxFacesPerPrefix());
41 }
42}
43
44void
akmhoque1a481092014-02-19 16:34:22 -060045Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060046{
47 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
48 npteList.end(), bind(&npteCompare, _1, name));
49 if ( it != npteList.end() )
50 {
51 string destRouter=rte.getDestination();
52
53 (*it).removeRoutingTableEntry(rte);
54 if ( ((*it).getRteListSize() == 0 ) &&
55 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) &&
56 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) &&
57 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) )
58 {
59 npteList.erase(it); // remove entry from NPT
60 // remove FIB entry with this name
akmhoque85d88332014-02-17 21:11:21 -060061 pnlsr.getFib().removeFromFib(pnlsr,name);
akmhoque298385a2014-02-13 14:13:09 -060062
63 }
64 else
65 {
66 (*it).generateNhlfromRteList();
67 // update FIB entry with new NHL
akmhoque85d88332014-02-17 21:11:21 -060068 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl(),
akmhoque298385a2014-02-13 14:13:09 -060069 pnlsr.getConfParameter().getMaxFacesPerPrefix());
70 }
71 }
72}
73
74
75void
akmhoque1a481092014-02-19 16:34:22 -060076Npt::addNpte(string name, string destRouter, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060077{
78 std::pair<RoutingTableEntry& , bool> rteCheck=
79 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
80 if(rteCheck.second)
81 {
82 addNpte(name,rteCheck.first,pnlsr);
83 }
84 else
85 {
86 RoutingTableEntry rte(destRouter);
87 addNpte(name, rte,pnlsr);
88 }
89
90}
91
92void
akmhoque1a481092014-02-19 16:34:22 -060093Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060094{
95 std::pair<RoutingTableEntry& , bool> rteCheck=
96 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
97 if(rteCheck.second)
98 {
99 removeNpte(name,rteCheck.first,pnlsr);
100 }
101 else
102 {
103 RoutingTableEntry rte(destRouter);
104 removeNpte(name, rte,pnlsr);
105 }
106}
107
108void
akmhoque1a481092014-02-19 16:34:22 -0600109Npt::updateNptWithNewRoute(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600110{
111 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
112 {
113 std::list<RoutingTableEntry> rteList=(*it).getRteList();
114 for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin();
115 rteit !=rteList.end(); ++rteit)
116 {
117 std::pair<RoutingTableEntry& , bool> rteCheck=
118 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
119 if(rteCheck.second)
120 {
121 addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr);
122 }
123 else
124 {
125 RoutingTableEntry rte((*rteit).getDestination());
126 addNpte((*it).getNamePrefix(), rte,pnlsr);
127 }
128 }
129 }
130}
131
132void
133Npt::printNpt()
134{
135 cout<<"----------------NPT----------------------"<<endl;
136 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
137 {
138 cout <<(*it)<<endl;
139 }
140}