blob: cada99db88aa2e33228863eb1738fd6cd74b9777 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<string>
2#include<utility>
3#include "nlsr_lsdb.hpp"
4#include "nlsr.hpp"
5
akmhoque1fd8c1e2014-02-19 19:41:49 -06006namespace nlsr
akmhoque85d88332014-02-17 21:11:21 -06007{
akmhoque85d88332014-02-17 21:11:21 -06008
akmhoque1fd8c1e2014-02-19 19:41:49 -06009 using namespace std;
10
11 void
12 Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, EventId eid)
13 {
14 pnlsr.getScheduler().cancelEvent(eid);
15 }
16
17 static bool
18 nameLsaCompareByKey(NameLsa& nlsa1, string& key)
19 {
20 return nlsa1.getNameLsaKey()==key;
21 }
akmhoque298385a2014-02-13 14:13:09 -060022
23
akmhoque1fd8c1e2014-02-19 19:41:49 -060024 bool
25 Lsdb::buildAndInstallOwnNameLsa(Nlsr& pnlsr)
26 {
27 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
28 , 1
29 , pnlsr.getSm().getNameLsaSeq()+1
30 , pnlsr.getConfParameter().getRouterDeadInterval()
31 , pnlsr.getNpl() );
32 pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1);
33 return installNameLsa(pnlsr,nameLsa);
akmhoque1fd8c1e2014-02-19 19:41:49 -060034 }
akmhoque298385a2014-02-13 14:13:09 -060035
akmhoque1fd8c1e2014-02-19 19:41:49 -060036 std::pair<NameLsa&, bool>
37 Lsdb::getNameLsa(string key)
38 {
39 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
40 nameLsdb.end(),
41 bind(nameLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -060042 if( it != nameLsdb.end())
43 {
44 return std::make_pair(boost::ref((*it)),true);
45 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060046 NameLsa nlsa;
47 return std::make_pair(boost::ref(nlsa),false);
akmhoque1fd8c1e2014-02-19 19:41:49 -060048 }
akmhoque298385a2014-02-13 14:13:09 -060049
akmhoque2bb198e2014-02-28 11:46:27 -060050 bool
51 Lsdb::isNameLsaNew(string key, uint64_t seqNo)
52 {
53 std::pair<NameLsa& , bool> nameLsaCheck=getNameLsa(key);
54 if(nameLsaCheck.second)
55 {
56 if(nameLsaCheck.first.getLsSeqNo() < seqNo)
57 {
58 return true;
59 }
60 else
61 {
62 return false;
63 }
64 }
65 return true;
66 }
akmhoque298385a2014-02-13 14:13:09 -060067
akmhoque1fd8c1e2014-02-19 19:41:49 -060068 ndn::EventId
69 Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
70 {
71 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
72 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
73 this,boost::ref(pnlsr), key, seqNo));
74 }
akmhoque298385a2014-02-13 14:13:09 -060075
akmhoque1fd8c1e2014-02-19 19:41:49 -060076 bool
77 Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa &nlsa)
78 {
79 int timeToExpire=lsaRefreshTime;
80 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey());
81 if ( !chkNameLsa.second )
82 {
83 addNameLsa(nlsa);
84 printNameLsdb();
85 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
86 {
87 pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr);
88 std::list<string> nameList=nlsa.getNpl().getNameList();
89 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end(); it++)
90 {
91 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
92 {
93 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
94 }
95 }
96 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060097 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
98 {
99 timeToExpire=nlsa.getLifeTime();
100 }
101 nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
102 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
103 }
104 else
105 {
106 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
107 {
108 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
109 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600110 chkNameLsa.first.getNpl().sortNpl();
111 nlsa.getNpl().sortNpl();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600112 std::list<string> nameToAdd;
113 std::set_difference(nlsa.getNpl().getNameList().begin(),
114 nlsa.getNpl().getNameList().end(),
115 chkNameLsa.first.getNpl().getNameList().begin(),
116 chkNameLsa.first.getNpl().getNameList().end(),
117 std::inserter(nameToAdd, nameToAdd.begin()));
118 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
119 ++it)
120 {
121 chkNameLsa.first.addNameToLsa((*it));
122 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
123 {
124 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
125 {
126 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
127 }
128 }
129 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600130 std::list<string> nameToRemove;
131 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
132 chkNameLsa.first.getNpl().getNameList().end(),
133 nlsa.getNpl().getNameList().begin(),
134 nlsa.getNpl().getNameList().end(),
135 std::inserter(nameToRemove, nameToRemove.begin()));
136 for(std::list<string>::iterator it=nameToRemove.begin();
137 it!=nameToRemove.end(); ++it)
138 {
139 chkNameLsa.first.removeNameFromLsa((*it));
140 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
141 {
142 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
143 {
144 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
145 }
146 }
147 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600148 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
149 {
150 timeToExpire=nlsa.getLifeTime();
151 }
152 cancelScheduleLsaExpiringEvent(pnlsr,
153 chkNameLsa.first.getLsaExpiringEventId());
154 chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
155 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
156 }
157 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600158 return true;
159 }
akmhoque298385a2014-02-13 14:13:09 -0600160
akmhoque1fd8c1e2014-02-19 19:41:49 -0600161 bool
162 Lsdb::addNameLsa(NameLsa &nlsa)
163 {
164 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
165 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600166 if( it == nameLsdb.end())
167 {
168 nameLsdb.push_back(nlsa);
169 return true;
170 }
171 return false;
172 }
akmhoque298385a2014-02-13 14:13:09 -0600173
akmhoque1fd8c1e2014-02-19 19:41:49 -0600174 bool
175 Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
176 {
177 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
178 nameLsdb.end(),
179 bind(nameLsaCompareByKey, _1, key));
180 if ( it != nameLsdb.end() )
181 {
182 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
183 {
184 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
185 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
186 nit!=(*it).getNpl().getNameList().end(); ++nit)
187 {
188 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
189 {
190 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
191 }
192 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600193 }
194 nameLsdb.erase(it);
195 return true;
196 }
197 return false;
198 }
akmhoque298385a2014-02-13 14:13:09 -0600199
akmhoque1fd8c1e2014-02-19 19:41:49 -0600200 bool
201 Lsdb::doesNameLsaExist(string key)
202 {
203 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
204 nameLsdb.end(),
205 bind(nameLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600206 if( it == nameLsdb.end())
207 {
208 return false;
209 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600210 return true;
211 }
212
213 void
214 Lsdb::printNameLsdb()
215 {
216 cout<<"---------------Name LSDB-------------------"<<endl;
217 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
218 it!= nameLsdb.end() ; it++)
219 {
220 cout<< (*it) <<endl;
221 }
222 }
akmhoque298385a2014-02-13 14:13:09 -0600223
224// Cor LSA and LSDB related Functions start here
225
226
akmhoque1fd8c1e2014-02-19 19:41:49 -0600227 static bool
228 corLsaCompareByKey(CorLsa& clsa, string& key)
229 {
230 return clsa.getCorLsaKey()==key;
231 }
akmhoque298385a2014-02-13 14:13:09 -0600232
akmhoque1fd8c1e2014-02-19 19:41:49 -0600233 bool
234 Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr)
235 {
236 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
237 , 3
238 , pnlsr.getSm().getCorLsaSeq()+1
239 , pnlsr.getConfParameter().getRouterDeadInterval()
240 , pnlsr.getConfParameter().getCorR()
241 , pnlsr.getConfParameter().getCorTheta() );
242 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
243 installCorLsa(pnlsr, corLsa);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600244 return true;
245 }
akmhoque298385a2014-02-13 14:13:09 -0600246
akmhoque1fd8c1e2014-02-19 19:41:49 -0600247 std::pair<CorLsa&, bool>
248 Lsdb::getCorLsa(string key)
249 {
250 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
251 corLsdb.end(),
252 bind(corLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600253 if( it != corLsdb.end())
254 {
255 return std::make_pair(boost::ref((*it)), true);
256 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600257 CorLsa clsa;
258 return std::make_pair(boost::ref(clsa),false);
259 }
akmhoque298385a2014-02-13 14:13:09 -0600260
akmhoque2bb198e2014-02-28 11:46:27 -0600261 bool
262 Lsdb::isCorLsaNew(string key, uint64_t seqNo)
263 {
264 std::pair<CorLsa& , bool> corLsaCheck=getCorLsa(key);
265 if(corLsaCheck.second)
266 {
267 if(corLsaCheck.first.getLsSeqNo() < seqNo)
268 {
269 return true;
270 }
271 else
272 {
273 return false;
274 }
275 }
276 return true;
277 }
278
akmhoque1fd8c1e2014-02-19 19:41:49 -0600279 ndn::EventId
280 Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
281 {
282 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
283 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
284 this,boost::ref(pnlsr),key,seqNo));
285 }
akmhoque298385a2014-02-13 14:13:09 -0600286
akmhoque1fd8c1e2014-02-19 19:41:49 -0600287 bool
288 Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa)
289 {
290 int timeToExpire=lsaRefreshTime;
291 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
292 if ( !chkCorLsa.second )
293 {
294 addCorLsa(clsa);
295 printCorLsdb(); //debugging purpose
296 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
297 {
298 pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
299 }
300 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
301 {
302 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
303 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600304 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
305 {
306 timeToExpire=clsa.getLifeTime();
307 }
308 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
309 clsa.getLsSeqNo(), timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600310 }
311 else
312 {
313 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
314 {
315 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
316 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
317 if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
318 {
319 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
320 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600321 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
322 {
323 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
324 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600325 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600326 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
327 {
328 timeToExpire=clsa.getLifeTime();
329 }
330 cancelScheduleLsaExpiringEvent(pnlsr,
331 chkCorLsa.first.getLsaExpiringEventId());
332 chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr,
333 clsa.getCorLsaKey(),
334 clsa.getLsSeqNo(), timeToExpire));
335 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600336 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600337 return true;
338 }
akmhoque298385a2014-02-13 14:13:09 -0600339
akmhoque1fd8c1e2014-02-19 19:41:49 -0600340 bool
341 Lsdb::addCorLsa(CorLsa& clsa)
342 {
343 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
344 corLsdb.end(),
345 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600346 if( it == corLsdb.end())
347 {
348 corLsdb.push_back(clsa);
349 return true;
350 }
351 return false;
352 }
akmhoque298385a2014-02-13 14:13:09 -0600353
akmhoque1fd8c1e2014-02-19 19:41:49 -0600354 bool
355 Lsdb::removeCorLsa(Nlsr& pnlsr, string& key)
356 {
357 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
358 corLsdb.end(),
359 bind(corLsaCompareByKey, _1, key));
360 if ( it != corLsdb.end() )
361 {
362 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
363 {
364 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
365 }
366 corLsdb.erase(it);
367 return true;
368 }
369 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600370 }
371
372 bool
373 Lsdb::doesCorLsaExist(string key)
374 {
375 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
376 corLsdb.end(),
377 bind(corLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600378 if( it == corLsdb.end())
379 {
380 return false;
381 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600382 return true;
383 }
384
385 void
386 Lsdb::printCorLsdb() //debugging
387 {
388 cout<<"---------------Cor LSDB-------------------"<<endl;
389 for( std::list<CorLsa>::iterator it=corLsdb.begin();
390 it!= corLsdb.end() ; it++)
391 {
392 cout<< (*it) <<endl;
393 }
394 }
akmhoque298385a2014-02-13 14:13:09 -0600395
396
397// Adj LSA and LSDB related function starts here
398
akmhoque1fd8c1e2014-02-19 19:41:49 -0600399 static bool
400 adjLsaCompareByKey(AdjLsa& alsa, string& key)
401 {
402 return alsa.getAdjLsaKey()==key;
403 }
akmhoque298385a2014-02-13 14:13:09 -0600404
405
akmhoque1fd8c1e2014-02-19 19:41:49 -0600406 void
407 Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
408 {
409 cout<<"scheduledAdjLsaBuild Called"<<endl;
410 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600411 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
412 {
413 int adjBuildCount=pnlsr.getAdjBuildCount();
414 if(adjBuildCount>0 )
415 {
416 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
417 {
418 buildAndInstallOwnAdjLsa(pnlsr);
419 }
420 else
421 {
422 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
423 removeAdjLsa(pnlsr,key);
424 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
425 }
426 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
427 }
428 }
429 else
430 {
431 pnlsr.setIsBuildAdjLsaSheduled(1);
432 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
433 pnlsr.getConfParameter().getInterestResendTime();
434 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
435 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
436 boost::ref(pnlsr)));
437 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600438 }
akmhoque298385a2014-02-13 14:13:09 -0600439
440
akmhoque1fd8c1e2014-02-19 19:41:49 -0600441 bool
442 Lsdb::addAdjLsa(AdjLsa &alsa)
443 {
444 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
445 adjLsdb.end(),
446 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600447 if( it == adjLsdb.end())
448 {
449 adjLsdb.push_back(alsa);
450 return true;
451 }
452 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600453 }
akmhoque298385a2014-02-13 14:13:09 -0600454
akmhoque1fd8c1e2014-02-19 19:41:49 -0600455 std::pair<AdjLsa& , bool>
456 Lsdb::getAdjLsa(string key)
457 {
458 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
459 adjLsdb.end(),
460 bind(adjLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600461 if( it != adjLsdb.end())
462 {
463 return std::make_pair(boost::ref((*it)),true);
464 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600465 AdjLsa alsa;
466 return std::make_pair(boost::ref(alsa),false);
467 }
akmhoque298385a2014-02-13 14:13:09 -0600468
469
akmhoque2bb198e2014-02-28 11:46:27 -0600470 bool
471 Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
472 {
473 std::pair<AdjLsa& , bool> adjLsaCheck=getAdjLsa(key);
474 if(adjLsaCheck.second)
475 {
476 if(adjLsaCheck.first.getLsSeqNo() < seqNo)
477 {
478 return true;
479 }
480 else
481 {
482 return false;
483 }
484 }
485 return true;
486 }
487
akmhoque298385a2014-02-13 14:13:09 -0600488
akmhoque1fd8c1e2014-02-19 19:41:49 -0600489 ndn::EventId
490 Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
491 {
492 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
493 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
494 this,boost::ref(pnlsr),key,seqNo));
495 }
akmhoque298385a2014-02-13 14:13:09 -0600496
akmhoque1fd8c1e2014-02-19 19:41:49 -0600497 bool
498 Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa)
499 {
500 int timeToExpire=lsaRefreshTime;
501 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
502 if ( !chkAdjLsa.second )
503 {
504 addAdjLsa(alsa);
505 alsa.addNptEntriesForAdjLsa(pnlsr);
506 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600507 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
508 {
509 timeToExpire=alsa.getLifeTime();
510 }
511 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
512 alsa.getLsSeqNo(),timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600513 }
514 else
515 {
516 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
517 {
518 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
519 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600520 if ( ! chkAdjLsa.first.isLsaContentEqual(alsa))
521 {
522 chkAdjLsa.first.getAdl().resetAdl();
523 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
524 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
525 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600526 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
527 {
528 timeToExpire=alsa.getLifeTime();
529 }
530 cancelScheduleLsaExpiringEvent(pnlsr,
531 chkAdjLsa.first.getLsaExpiringEventId());
532 chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
533 alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire));
534 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600535 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600536 return true;
537 }
akmhoque298385a2014-02-13 14:13:09 -0600538
akmhoque1fd8c1e2014-02-19 19:41:49 -0600539 bool
540 Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
541 {
542 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
543 , 2
544 , pnlsr.getSm().getAdjLsaSeq()+1
545 , pnlsr.getConfParameter().getRouterDeadInterval()
546 , pnlsr.getAdl().getNumOfActiveNeighbor()
547 , pnlsr.getAdl() );
548 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
akmhoque2bb198e2014-02-28 11:46:27 -0600549 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
550 + pnlsr.getConfParameter().getRouterPrefix();
551 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
552 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600553 }
akmhoque298385a2014-02-13 14:13:09 -0600554
akmhoque1fd8c1e2014-02-19 19:41:49 -0600555 bool
556 Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
557 {
558 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
559 adjLsdb.end(),
560 bind(adjLsaCompareByKey, _1, key));
561 if ( it != adjLsdb.end() )
562 {
563 (*it).removeNptEntriesForAdjLsa(pnlsr);
564 adjLsdb.erase(it);
565 return true;
566 }
567 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600568 }
akmhoque298385a2014-02-13 14:13:09 -0600569
akmhoque1fd8c1e2014-02-19 19:41:49 -0600570 bool
571 Lsdb::doesAdjLsaExist(string key)
572 {
573 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
574 adjLsdb.end(),
575 bind(adjLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600576 if( it == adjLsdb.end())
577 {
578 return false;
579 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600580 return true;
581 }
akmhoque298385a2014-02-13 14:13:09 -0600582
akmhoque1fd8c1e2014-02-19 19:41:49 -0600583 std::list<AdjLsa>&
584 Lsdb::getAdjLsdb()
585 {
586 return adjLsdb;
587 }
akmhoque298385a2014-02-13 14:13:09 -0600588
akmhoque1fd8c1e2014-02-19 19:41:49 -0600589 void
590 Lsdb::setLsaRefreshTime(int lrt)
591 {
592 lsaRefreshTime=lrt;
593 }
akmhoque298385a2014-02-13 14:13:09 -0600594
akmhoque1fd8c1e2014-02-19 19:41:49 -0600595 void
596 Lsdb::setThisRouterPrefix(string trp)
597 {
598 thisRouterPrefix=trp;
599 }
600
601 void
602 Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
603 {
604 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
akmhoque2bb198e2014-02-28 11:46:27 -0600605 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600606 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
607 if( chkNameLsa.second )
608 {
609 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
610 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
611 {
612 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
613 {
614 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
615 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
616 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
617 // publish routing update
akmhoque2bb198e2014-02-28 11:46:27 -0600618 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
619 + pnlsr.getConfParameter().getRouterPrefix();
620 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600621 }
622 else
623 {
624 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
625 removeNameLsa(pnlsr, lsaKey);
626 }
627 }
628 }
629 }
630
631 void
632 Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
633 {
634 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
akmhoque2bb198e2014-02-28 11:46:27 -0600635 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600636 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
637 if( chkAdjLsa.second )
638 {
639 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
640 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
641 {
642 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
643 {
644 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
645 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
646 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
647 // publish routing update
akmhoque2bb198e2014-02-28 11:46:27 -0600648 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
649 + pnlsr.getConfParameter().getRouterPrefix();
650 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600651 }
652 else
653 {
654 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
655 removeAdjLsa(pnlsr, lsaKey);
656 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600657 // schedule Routing table calculaiton
658 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
659 }
660 }
661 }
662
663 void
664 Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
665 {
666 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
akmhoque2bb198e2014-02-28 11:46:27 -0600667 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600668 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
669 if( chkCorLsa.second )
670 {
671 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
672 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
673 {
674 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
675 {
676 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
677 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
678 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
679 // publish routing update
akmhoque2bb198e2014-02-28 11:46:27 -0600680 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
681 + pnlsr.getConfParameter().getRouterPrefix();
682 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600683 }
684 else
685 {
686 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
687 removeCorLsa(pnlsr, lsaKey);
688 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600689 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
690 {
691 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
692 }
693 }
694 }
695 }
akmhoque298385a2014-02-13 14:13:09 -0600696
697
akmhoque1fd8c1e2014-02-19 19:41:49 -0600698 void
699 Lsdb::printAdjLsdb()
700 {
701 cout<<"---------------Adj LSDB-------------------"<<endl;
702 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
703 it!= adjLsdb.end() ; it++)
704 {
705 cout<< (*it) <<endl;
706 }
707 }
akmhoque298385a2014-02-13 14:13:09 -0600708
709//-----utility function -----
akmhoque1fd8c1e2014-02-19 19:41:49 -0600710 bool
711 Lsdb::doesLsaExist(string key, int lsType)
712 {
713 if ( lsType == 1)
714 {
715 return doesNameLsaExist(key);
716 }
717 else if ( lsType == 2)
718 {
719 return doesAdjLsaExist(key);
720 }
721 else if ( lsType == 3)
722 {
723 return doesCorLsaExist(key);
724 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600725 return false;
726 }
akmhoque298385a2014-02-13 14:13:09 -0600727
akmhoqueb1710aa2014-02-19 17:13:36 -0600728}//namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -0600729