blob: f62d12bd8c1fbce0d90d0e8463c10b947f78cb96 [file] [log] [blame]
akmhoquebd7c8e62014-02-01 14:57:40 -06001#include<string>
2#include "nlsr_lsdb.hpp"
3#include "nlsr.hpp"
4
5using namespace std;
6
7
8
9
10
11bool
12Lsdb::doesLsaExist(string key, int lsType)
13{
14 if ( lsType == 1)
15 {
16 return doesNameLsaExist(key);
17 }
18 else if ( lsType == 2)
19 {
20 return doesAdjLsaExist(key);
21 }
22 else if ( lsType == 3)
23 {
24 return doesCorLsaExist(key);
25 }
26
27 return false;
28
29}
30
akmhoque3c6bd922014-02-01 17:10:17 -060031//Name LSA and LSDB related functions start here
32
akmhoquebd7c8e62014-02-01 14:57:40 -060033static bool
34nameLsaCompare(NameLsa& nlsa1, NameLsa& nlsa2){
35 return nlsa1.getLsaKey()==nlsa1.getLsaKey();
36}
37
38static bool
39nameLsaCompareByKey(NameLsa& nlsa1, string& key){
40 return nlsa1.getLsaKey()==key;
41}
42
43
44bool
45Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr)
46{
47 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
48 , 1
49 , pnlsr.getNameLsaSeq()+1
50 , pnlsr.getConfParameter().getRouterDeadInterval()
51 , pnlsr.getNpl() );
52 pnlsr.setNameLsaSeq(pnlsr.getNameLsaSeq()+1);
akmhoque3c6bd922014-02-01 17:10:17 -060053 //cout<<nameLsa;
akmhoquebd7c8e62014-02-01 14:57:40 -060054 return installNameLsa(nameLsa);
55
56}
57
58NameLsa&
59Lsdb::getNameLsa(string key)
60{
61 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
62 nameLsdb.end(),
63 bind(nameLsaCompareByKey, _1, key));
64
65 if( it != nameLsdb.end()){
66 return (*it);
67 }
68}
69
70
71
72bool
73Lsdb::installNameLsa(NameLsa &nlsa)
74{
75 bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey());
76 if ( !doesLsaExist_ )
77 {
78 // add name LSA
79 addNameLsa(nlsa);
80 // update NPT and FIB
81 }
82 else
83 {
84 // check for newer name LSA
85 NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey());
86 // Discard or Update Name lsa, NPT, FIB
87 }
88
89 return true;
90}
91
92bool
93Lsdb::addNameLsa(NameLsa &nlsa)
94{
95 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
96 nameLsdb.end(),
97 bind(nameLsaCompare, _1, nlsa));
98
99 if( it == nameLsdb.end()){
100 nameLsdb.push_back(nlsa);
101 return true;
102 }
103 return false;
104}
105
106bool
107Lsdb::removeNameLsa(string& key)
108{
109 return false;
110}
akmhoquebd7c8e62014-02-01 14:57:40 -0600111
112bool
113Lsdb::doesNameLsaExist(string key)
114{
115 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
116 nameLsdb.end(),
117 bind(nameLsaCompareByKey, _1, key));
118
119 if( it == nameLsdb.end()){
120 return false;
121 }
122
123 return true;
124}
125
akmhoque3c6bd922014-02-01 17:10:17 -0600126void
127Lsdb::printNameLsdb()
128{
129 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
130 it!= nameLsdb.end() ; it++)
131 {
132 cout<< (*it) <<endl;
133 }
134}
135
136// Cor LSA and LSDB related Functions start here
137
138static bool
139corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
140 return clsa1.getLsaKey()==clsa1.getLsaKey();
141}
142
143static bool
144corLsaCompareByKey(CorLsa& clsa, string& key){
145 return clsa.getLsaKey()==key;
146}
akmhoquebd7c8e62014-02-01 14:57:40 -0600147
148bool
akmhoque3c6bd922014-02-01 17:10:17 -0600149Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
150 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
akmhoquecd552472014-02-01 21:22:16 -0600151 , 3
akmhoque3c6bd922014-02-01 17:10:17 -0600152 , pnlsr.getCorLsaSeq()+1
153 , pnlsr.getConfParameter().getRouterDeadInterval()
154 , pnlsr.getConfParameter().getCorR()
155 , pnlsr.getConfParameter().getCorTheta() );
156 pnlsr.setCorLsaSeq(pnlsr.getCorLsaSeq()+1);
157 //cout<<corLsa;
158 installCorLsa(corLsa);
159
160}
161
162CorLsa&
163Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600164{
akmhoque3c6bd922014-02-01 17:10:17 -0600165 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
166 corLsdb.end(),
167 bind(corLsaCompareByKey, _1, key));
168
169 if( it != corLsdb.end()){
170 return (*it);
171 }
172}
173
174bool
175Lsdb::installCorLsa(CorLsa &clsa)
176{
177 bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey());
178 if ( !doesLsaExist_ )
179 {
180 // add cor LSA
181 addCorLsa(clsa);
182 }
183 else
184 {
185 // check for newer cor LSA
186 CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey());
187
188 }
189
190 return true;
191}
192
193bool
194Lsdb::addCorLsa(CorLsa& clsa)
195{
196 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
197 corLsdb.end(),
198 bind(corLsaCompare, _1, clsa));
199
200 if( it == corLsdb.end()){
201 corLsdb.push_back(clsa);
202 return true;
203 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600204 return false;
205}
206
207bool
akmhoque3c6bd922014-02-01 17:10:17 -0600208Lsdb::removeCorLsa(string& key)
209{
210
211}
212
213bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600214Lsdb::doesCorLsaExist(string key)
215{
akmhoque3c6bd922014-02-01 17:10:17 -0600216 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
217 corLsdb.end(),
218 bind(corLsaCompareByKey, _1, key));
219
220 if( it == corLsdb.end()){
221 return false;
222 }
223
224 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600225}
akmhoque3c6bd922014-02-01 17:10:17 -0600226
227void
228Lsdb::printCorLsdb() //debugging
229{
230 for( std::list<CorLsa>::iterator it=corLsdb.begin();
231 it!= corLsdb.end() ; it++)
232 {
233 cout<< (*it) <<endl;
234 }
235}
236
237
238// Adj LSA and LSDB related function starts here
239
240static bool
241adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
242 return alsa1.getLsaKey()==alsa1.getLsaKey();
243}
244
245static bool
246adjLsaCompareByKey(AdjLsa& alsa, string& key){
247 return alsa.getLsaKey()==key;
248}
249
250
akmhoquecd552472014-02-01 21:22:16 -0600251void
252Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr)
253{
254 cout<<"scheduledAdjLsaBuild Called"<<endl;
255 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque3c6bd922014-02-01 17:10:17 -0600256
akmhoquecd552472014-02-01 21:22:16 -0600257 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
258 {
259 int adjBuildCount=pnlsr.getAdjBuildCount();
260 if(adjBuildCount>0 )
261 {
262 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
263 {
264 buildAndInstallOwnAdjLsa(pnlsr);
265 }
266 else
267 {
268 //remove if there is any adj lsa in LSDB
269 }
270 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
271 }
272 }
273 else
274 {
275 pnlsr.setIsBuildAdjLsaSheduled(1);
276 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
277 pnlsr.getConfParameter().getInterestRetryNumber();
278 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
279 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
280 boost::ref(pnlsr)));
281 }
282
283}
284
285
286bool
287Lsdb::addAdjLsa(AdjLsa &alsa)
288{
289 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
290 adjLsdb.end(),
291 bind(adjLsaCompare, _1, alsa));
292
293 if( it == adjLsdb.end()){
294 adjLsdb.push_back(alsa);
295 return true;
296 }
297 return false;
298
299}
300
301AdjLsa&
302Lsdb::getAdjLsa(string key)
303{
304 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
305 adjLsdb.end(),
306 bind(adjLsaCompareByKey, _1, key));
307
308 if( it != adjLsdb.end()){
309 return (*it);
310 }
311
312}
313
314bool
315Lsdb::installAdjLsa(AdjLsa &alsa)
316{
317 bool doesLsaExist_ = doesAdjLsaExist(alsa.getLsaKey());
318 if ( !doesLsaExist_ )
319 {
320 // add Adj LSA
321 addAdjLsa(alsa);
322 // schedule routing table calculation
323 }
324 else
325 {
326 // check for newer name LSA
327 AdjLsa oldAdjLsa=getAdjLsa(alsa.getLsaKey());
328
329 }
330
331 printAdjLsdb();
332
333 return true;
334}
335
336bool
337Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
338{
339 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
340 , 2
341 , pnlsr.getAdjLsaSeq()+1
342 , pnlsr.getConfParameter().getRouterDeadInterval()
343 , pnlsr.getAdl().getNumOfActiveNeighbor()
344 , pnlsr.getAdl() );
345 pnlsr.setAdjLsaSeq(pnlsr.getAdjLsaSeq()+1);
346 return installAdjLsa(adjLsa);
347}
348
349bool
350Lsdb::removeAdjLsa(string& key)
351{
352
353}
akmhoque3c6bd922014-02-01 17:10:17 -0600354
355bool
356Lsdb::doesAdjLsaExist(string key)
357{
358 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
359 adjLsdb.end(),
360 bind(adjLsaCompareByKey, _1, key));
361
362 if( it == adjLsdb.end()){
363 return false;
364 }
365
366 return true;
367}
368
akmhoquecd552472014-02-01 21:22:16 -0600369void
370Lsdb::printAdjLsdb()
371{
372 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
373 it!= adjLsdb.end() ; it++)
374 {
375 cout<< (*it) <<endl;
376 }
377}
akmhoque3c6bd922014-02-01 17:10:17 -0600378