blob: 498b2ca60e880aa9d75804f7d48f7b768ae1e88e [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
akmhoqueb1710aa2014-02-19 17:13:36 -06009namespace nlsr {
10
akmhoque298385a2014-02-13 14:13:09 -060011using namespace std;
12
13static bool
14npteCompare(Npte& npte, string& name){
15 return npte.getNamePrefix()==name;
16}
17
18// Following two methods will update FIB with response to change in NPT
19
20void
akmhoque1a481092014-02-19 16:34:22 -060021Npt::addNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060022{
23 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
24 npteList.end(), bind(&npteCompare, _1, name));
25
26 if ( it == npteList.end() )
27 {
28 Npte newEntry( name);
29 newEntry.addRoutingTableEntry(rte);
30 newEntry.generateNhlfromRteList();
31 npteList.push_back(newEntry);
32 // update FIB here with nhl list newEntry.getNhl()
akmhoque85d88332014-02-17 21:11:21 -060033 pnlsr.getFib().updateFib(pnlsr, name,newEntry.getNhl(),
akmhoque298385a2014-02-13 14:13:09 -060034 pnlsr.getConfParameter().getMaxFacesPerPrefix());
35 }
36 else
37 {
38 (*it).addRoutingTableEntry(rte);
39 (*it).generateNhlfromRteList();
40 // update FIB here with nhl list from (*it).getNhl()
akmhoque85d88332014-02-17 21:11:21 -060041 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl() ,
akmhoque298385a2014-02-13 14:13:09 -060042 pnlsr.getConfParameter().getMaxFacesPerPrefix());
43 }
44}
45
46void
akmhoque1a481092014-02-19 16:34:22 -060047Npt::removeNpte(string name, RoutingTableEntry& rte, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060048{
49 std::list<Npte >::iterator it = std::find_if( npteList.begin(),
50 npteList.end(), bind(&npteCompare, _1, name));
51 if ( it != npteList.end() )
52 {
53 string destRouter=rte.getDestination();
54
55 (*it).removeRoutingTableEntry(rte);
56 if ( ((*it).getRteListSize() == 0 ) &&
57 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/1",1) ) &&
58 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/2",2) ) &&
59 (!pnlsr.getLsdb().doesLsaExist(destRouter+"/3",3) ) )
60 {
61 npteList.erase(it); // remove entry from NPT
62 // remove FIB entry with this name
akmhoque85d88332014-02-17 21:11:21 -060063 pnlsr.getFib().removeFromFib(pnlsr,name);
akmhoque298385a2014-02-13 14:13:09 -060064
65 }
66 else
67 {
68 (*it).generateNhlfromRteList();
69 // update FIB entry with new NHL
akmhoque85d88332014-02-17 21:11:21 -060070 pnlsr.getFib().updateFib(pnlsr, name,(*it).getNhl(),
akmhoque298385a2014-02-13 14:13:09 -060071 pnlsr.getConfParameter().getMaxFacesPerPrefix());
72 }
73 }
74}
75
76
77void
akmhoque1a481092014-02-19 16:34:22 -060078Npt::addNpte(string name, string destRouter, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060079{
80 std::pair<RoutingTableEntry& , bool> rteCheck=
81 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
82 if(rteCheck.second)
83 {
84 addNpte(name,rteCheck.first,pnlsr);
85 }
86 else
87 {
88 RoutingTableEntry rte(destRouter);
89 addNpte(name, rte,pnlsr);
90 }
91
92}
93
94void
akmhoque1a481092014-02-19 16:34:22 -060095Npt::removeNpte(string name, string destRouter, Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -060096{
97 std::pair<RoutingTableEntry& , bool> rteCheck=
98 pnlsr.getRoutingTable().findRoutingTableEntry(destRouter);
99 if(rteCheck.second)
100 {
101 removeNpte(name,rteCheck.first,pnlsr);
102 }
103 else
104 {
105 RoutingTableEntry rte(destRouter);
106 removeNpte(name, rte,pnlsr);
107 }
108}
109
110void
akmhoque1a481092014-02-19 16:34:22 -0600111Npt::updateNptWithNewRoute(Nlsr& pnlsr)
akmhoque298385a2014-02-13 14:13:09 -0600112{
113 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
114 {
115 std::list<RoutingTableEntry> rteList=(*it).getRteList();
116 for(std::list<RoutingTableEntry >::iterator rteit=rteList.begin();
117 rteit !=rteList.end(); ++rteit)
118 {
119 std::pair<RoutingTableEntry& , bool> rteCheck=
120 pnlsr.getRoutingTable().findRoutingTableEntry((*rteit).getDestination());
121 if(rteCheck.second)
122 {
123 addNpte((*it).getNamePrefix(),rteCheck.first,pnlsr);
124 }
125 else
126 {
127 RoutingTableEntry rte((*rteit).getDestination());
128 addNpte((*it).getNamePrefix(), rte,pnlsr);
129 }
130 }
131 }
132}
133
134void
135Npt::printNpt()
136{
137 cout<<"----------------NPT----------------------"<<endl;
138 for(std::list<Npte >::iterator it=npteList.begin(); it!=npteList.end(); ++it)
139 {
140 cout <<(*it)<<endl;
141 }
142}
akmhoqueb1710aa2014-02-19 17:13:36 -0600143
144}