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