blob: dbe5b86219b0ff3779987b720186ea93b572b09c [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);
akmhoque298385a2014-02-13 14:13:09 -060034
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 }
akmhoque298385a2014-02-13 14:13:09 -060036
akmhoque1fd8c1e2014-02-19 19:41:49 -060037 std::pair<NameLsa&, bool>
38 Lsdb::getNameLsa(string key)
39 {
40 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
41 nameLsdb.end(),
42 bind(nameLsaCompareByKey, _1, key));
akmhoque298385a2014-02-13 14:13:09 -060043
akmhoque1fd8c1e2014-02-19 19:41:49 -060044 if( it != nameLsdb.end())
45 {
46 return std::make_pair(boost::ref((*it)),true);
47 }
akmhoque298385a2014-02-13 14:13:09 -060048
akmhoque1fd8c1e2014-02-19 19:41:49 -060049 NameLsa nlsa;
50 return std::make_pair(boost::ref(nlsa),false);
akmhoque298385a2014-02-13 14:13:09 -060051
akmhoque1fd8c1e2014-02-19 19:41:49 -060052 }
akmhoque298385a2014-02-13 14:13:09 -060053
54
akmhoque1fd8c1e2014-02-19 19:41:49 -060055 ndn::EventId
56 Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
57 {
58 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
59 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
60 this,boost::ref(pnlsr), key, seqNo));
61 }
akmhoque298385a2014-02-13 14:13:09 -060062
akmhoque1fd8c1e2014-02-19 19:41:49 -060063 bool
64 Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa &nlsa)
65 {
66 int timeToExpire=lsaRefreshTime;
67 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey());
68 if ( !chkNameLsa.second )
69 {
70 addNameLsa(nlsa);
71 printNameLsdb();
72 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
73 {
74 pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr);
75 std::list<string> nameList=nlsa.getNpl().getNameList();
76 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end(); it++)
77 {
78 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
79 {
80 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
81 }
82 }
83 }
akmhoque298385a2014-02-13 14:13:09 -060084
akmhoque1fd8c1e2014-02-19 19:41:49 -060085 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
86 {
87 timeToExpire=nlsa.getLifeTime();
88 }
89 nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
90 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
91 }
92 else
93 {
94 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
95 {
96 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
97 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
akmhoque298385a2014-02-13 14:13:09 -060098
akmhoque1fd8c1e2014-02-19 19:41:49 -060099 chkNameLsa.first.getNpl().sortNpl();
100 nlsa.getNpl().sortNpl();
akmhoque298385a2014-02-13 14:13:09 -0600101
akmhoque1fd8c1e2014-02-19 19:41:49 -0600102 std::list<string> nameToAdd;
103 std::set_difference(nlsa.getNpl().getNameList().begin(),
104 nlsa.getNpl().getNameList().end(),
105 chkNameLsa.first.getNpl().getNameList().begin(),
106 chkNameLsa.first.getNpl().getNameList().end(),
107 std::inserter(nameToAdd, nameToAdd.begin()));
108 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
109 ++it)
110 {
111 chkNameLsa.first.addNameToLsa((*it));
112 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
113 {
114 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
115 {
116 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
117 }
118 }
119 }
akmhoque298385a2014-02-13 14:13:09 -0600120
akmhoque1fd8c1e2014-02-19 19:41:49 -0600121 std::list<string> nameToRemove;
122 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
123 chkNameLsa.first.getNpl().getNameList().end(),
124 nlsa.getNpl().getNameList().begin(),
125 nlsa.getNpl().getNameList().end(),
126 std::inserter(nameToRemove, nameToRemove.begin()));
127 for(std::list<string>::iterator it=nameToRemove.begin();
128 it!=nameToRemove.end(); ++it)
129 {
130 chkNameLsa.first.removeNameFromLsa((*it));
131 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
132 {
133 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
134 {
135 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
136 }
137 }
138 }
akmhoque298385a2014-02-13 14:13:09 -0600139
akmhoque1fd8c1e2014-02-19 19:41:49 -0600140 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
141 {
142 timeToExpire=nlsa.getLifeTime();
143 }
144 cancelScheduleLsaExpiringEvent(pnlsr,
145 chkNameLsa.first.getLsaExpiringEventId());
146 chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
147 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
148 }
149 }
akmhoque298385a2014-02-13 14:13:09 -0600150
akmhoque1fd8c1e2014-02-19 19:41:49 -0600151 return true;
152 }
akmhoque298385a2014-02-13 14:13:09 -0600153
akmhoque1fd8c1e2014-02-19 19:41:49 -0600154 bool
155 Lsdb::addNameLsa(NameLsa &nlsa)
156 {
157 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
158 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoque298385a2014-02-13 14:13:09 -0600159
akmhoque1fd8c1e2014-02-19 19:41:49 -0600160 if( it == nameLsdb.end())
161 {
162 nameLsdb.push_back(nlsa);
163 return true;
164 }
165 return false;
166 }
akmhoque298385a2014-02-13 14:13:09 -0600167
akmhoque1fd8c1e2014-02-19 19:41:49 -0600168 bool
169 Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
170 {
171 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
172 nameLsdb.end(),
173 bind(nameLsaCompareByKey, _1, key));
174 if ( it != nameLsdb.end() )
175 {
176 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
177 {
178 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
179 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
180 nit!=(*it).getNpl().getNameList().end(); ++nit)
181 {
182 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
183 {
184 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
185 }
186 }
akmhoque298385a2014-02-13 14:13:09 -0600187
akmhoque1fd8c1e2014-02-19 19:41:49 -0600188 }
189 nameLsdb.erase(it);
190 return true;
191 }
192 return false;
193 }
akmhoque298385a2014-02-13 14:13:09 -0600194
akmhoque1fd8c1e2014-02-19 19:41:49 -0600195 bool
196 Lsdb::doesNameLsaExist(string key)
197 {
198 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
199 nameLsdb.end(),
200 bind(nameLsaCompareByKey, _1, key));
201
202 if( it == nameLsdb.end())
203 {
204 return false;
205 }
206
207 return true;
208 }
209
210 void
211 Lsdb::printNameLsdb()
212 {
213 cout<<"---------------Name LSDB-------------------"<<endl;
214 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
215 it!= nameLsdb.end() ; it++)
216 {
217 cout<< (*it) <<endl;
218 }
219 }
akmhoque298385a2014-02-13 14:13:09 -0600220
221// Cor LSA and LSDB related Functions start here
222
223
akmhoque1fd8c1e2014-02-19 19:41:49 -0600224 static bool
225 corLsaCompareByKey(CorLsa& clsa, string& key)
226 {
227 return clsa.getCorLsaKey()==key;
228 }
akmhoque298385a2014-02-13 14:13:09 -0600229
akmhoque1fd8c1e2014-02-19 19:41:49 -0600230 bool
231 Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr)
232 {
233 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
234 , 3
235 , pnlsr.getSm().getCorLsaSeq()+1
236 , pnlsr.getConfParameter().getRouterDeadInterval()
237 , pnlsr.getConfParameter().getCorR()
238 , pnlsr.getConfParameter().getCorTheta() );
239 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
240 installCorLsa(pnlsr, corLsa);
akmhoque298385a2014-02-13 14:13:09 -0600241
akmhoque1fd8c1e2014-02-19 19:41:49 -0600242 return true;
243 }
akmhoque298385a2014-02-13 14:13:09 -0600244
akmhoque1fd8c1e2014-02-19 19:41:49 -0600245 std::pair<CorLsa&, bool>
246 Lsdb::getCorLsa(string key)
247 {
248 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
249 corLsdb.end(),
250 bind(corLsaCompareByKey, _1, key));
akmhoque298385a2014-02-13 14:13:09 -0600251
akmhoque1fd8c1e2014-02-19 19:41:49 -0600252 if( it != corLsdb.end())
253 {
254 return std::make_pair(boost::ref((*it)), true);
255 }
akmhoque298385a2014-02-13 14:13:09 -0600256
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
akmhoque1fd8c1e2014-02-19 19:41:49 -0600261 ndn::EventId
262 Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
263 {
264 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
265 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
266 this,boost::ref(pnlsr),key,seqNo));
267 }
akmhoque298385a2014-02-13 14:13:09 -0600268
akmhoque1fd8c1e2014-02-19 19:41:49 -0600269 bool
270 Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa)
271 {
272 int timeToExpire=lsaRefreshTime;
273 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
274 if ( !chkCorLsa.second )
275 {
276 addCorLsa(clsa);
277 printCorLsdb(); //debugging purpose
278 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
279 {
280 pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
281 }
282 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
283 {
284 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
285 }
akmhoque298385a2014-02-13 14:13:09 -0600286
akmhoque1fd8c1e2014-02-19 19:41:49 -0600287 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
288 {
289 timeToExpire=clsa.getLifeTime();
290 }
291 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
292 clsa.getLsSeqNo(), timeToExpire);
akmhoque298385a2014-02-13 14:13:09 -0600293
akmhoque1fd8c1e2014-02-19 19:41:49 -0600294 }
295 else
296 {
297 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
298 {
299 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
300 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
301 if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
302 {
303 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
304 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
akmhoque298385a2014-02-13 14:13:09 -0600305
akmhoque1fd8c1e2014-02-19 19:41:49 -0600306 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
307 {
308 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
309 }
akmhoque298385a2014-02-13 14:13:09 -0600310
akmhoque1fd8c1e2014-02-19 19:41:49 -0600311 }
akmhoque298385a2014-02-13 14:13:09 -0600312
akmhoque1fd8c1e2014-02-19 19:41:49 -0600313 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
314 {
315 timeToExpire=clsa.getLifeTime();
316 }
317 cancelScheduleLsaExpiringEvent(pnlsr,
318 chkCorLsa.first.getLsaExpiringEventId());
319 chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr,
320 clsa.getCorLsaKey(),
321 clsa.getLsSeqNo(), timeToExpire));
322 }
akmhoque298385a2014-02-13 14:13:09 -0600323
akmhoque1fd8c1e2014-02-19 19:41:49 -0600324 }
akmhoque298385a2014-02-13 14:13:09 -0600325
akmhoque1fd8c1e2014-02-19 19:41:49 -0600326 return true;
327 }
akmhoque298385a2014-02-13 14:13:09 -0600328
akmhoque1fd8c1e2014-02-19 19:41:49 -0600329 bool
330 Lsdb::addCorLsa(CorLsa& clsa)
331 {
332 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
333 corLsdb.end(),
334 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque298385a2014-02-13 14:13:09 -0600335
akmhoque1fd8c1e2014-02-19 19:41:49 -0600336 if( it == corLsdb.end())
337 {
338 corLsdb.push_back(clsa);
339 return true;
340 }
341 return false;
342 }
akmhoque298385a2014-02-13 14:13:09 -0600343
akmhoque1fd8c1e2014-02-19 19:41:49 -0600344 bool
345 Lsdb::removeCorLsa(Nlsr& pnlsr, string& key)
346 {
347 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
348 corLsdb.end(),
349 bind(corLsaCompareByKey, _1, key));
350 if ( it != corLsdb.end() )
351 {
352 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
353 {
354 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
355 }
356 corLsdb.erase(it);
357 return true;
358 }
359 return false;
akmhoque298385a2014-02-13 14:13:09 -0600360
akmhoque1fd8c1e2014-02-19 19:41:49 -0600361 }
362
363 bool
364 Lsdb::doesCorLsaExist(string key)
365 {
366 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
367 corLsdb.end(),
368 bind(corLsaCompareByKey, _1, key));
369
370 if( it == corLsdb.end())
371 {
372 return false;
373 }
374
375 return true;
376 }
377
378 void
379 Lsdb::printCorLsdb() //debugging
380 {
381 cout<<"---------------Cor LSDB-------------------"<<endl;
382 for( std::list<CorLsa>::iterator it=corLsdb.begin();
383 it!= corLsdb.end() ; it++)
384 {
385 cout<< (*it) <<endl;
386 }
387 }
akmhoque298385a2014-02-13 14:13:09 -0600388
389
390// Adj LSA and LSDB related function starts here
391
akmhoque1fd8c1e2014-02-19 19:41:49 -0600392 static bool
393 adjLsaCompareByKey(AdjLsa& alsa, string& key)
394 {
395 return alsa.getAdjLsaKey()==key;
396 }
akmhoque298385a2014-02-13 14:13:09 -0600397
398
akmhoque1fd8c1e2014-02-19 19:41:49 -0600399 void
400 Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
401 {
402 cout<<"scheduledAdjLsaBuild Called"<<endl;
403 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque298385a2014-02-13 14:13:09 -0600404
akmhoque1fd8c1e2014-02-19 19:41:49 -0600405 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
406 {
407 int adjBuildCount=pnlsr.getAdjBuildCount();
408 if(adjBuildCount>0 )
409 {
410 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
411 {
412 buildAndInstallOwnAdjLsa(pnlsr);
413 }
414 else
415 {
416 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
417 removeAdjLsa(pnlsr,key);
418 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
419 }
420 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
421 }
422 }
423 else
424 {
425 pnlsr.setIsBuildAdjLsaSheduled(1);
426 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
427 pnlsr.getConfParameter().getInterestResendTime();
428 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
429 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
430 boost::ref(pnlsr)));
431 }
akmhoque298385a2014-02-13 14:13:09 -0600432
akmhoque1fd8c1e2014-02-19 19:41:49 -0600433 }
akmhoque298385a2014-02-13 14:13:09 -0600434
435
akmhoque1fd8c1e2014-02-19 19:41:49 -0600436 bool
437 Lsdb::addAdjLsa(AdjLsa &alsa)
438 {
439 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
440 adjLsdb.end(),
441 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoque298385a2014-02-13 14:13:09 -0600442
akmhoque1fd8c1e2014-02-19 19:41:49 -0600443 if( it == adjLsdb.end())
444 {
445 adjLsdb.push_back(alsa);
446 return true;
447 }
448 return false;
akmhoque298385a2014-02-13 14:13:09 -0600449
akmhoque1fd8c1e2014-02-19 19:41:49 -0600450 }
akmhoque298385a2014-02-13 14:13:09 -0600451
akmhoque1fd8c1e2014-02-19 19:41:49 -0600452 std::pair<AdjLsa& , bool>
453 Lsdb::getAdjLsa(string key)
454 {
455 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
456 adjLsdb.end(),
457 bind(adjLsaCompareByKey, _1, key));
akmhoque298385a2014-02-13 14:13:09 -0600458
akmhoque1fd8c1e2014-02-19 19:41:49 -0600459 if( it != adjLsdb.end())
460 {
461 return std::make_pair(boost::ref((*it)),true);
462 }
463
464 AdjLsa alsa;
465 return std::make_pair(boost::ref(alsa),false);
466 }
akmhoque298385a2014-02-13 14:13:09 -0600467
468
469
akmhoque1fd8c1e2014-02-19 19:41:49 -0600470 ndn::EventId
471 Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
472 {
473 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
474 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
475 this,boost::ref(pnlsr),key,seqNo));
476 }
akmhoque298385a2014-02-13 14:13:09 -0600477
akmhoque1fd8c1e2014-02-19 19:41:49 -0600478 bool
479 Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa)
480 {
481 int timeToExpire=lsaRefreshTime;
482 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
483 if ( !chkAdjLsa.second )
484 {
485 addAdjLsa(alsa);
486 alsa.addNptEntriesForAdjLsa(pnlsr);
487 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque298385a2014-02-13 14:13:09 -0600488
akmhoque1fd8c1e2014-02-19 19:41:49 -0600489 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
490 {
491 timeToExpire=alsa.getLifeTime();
492 }
493 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
494 alsa.getLsSeqNo(),timeToExpire);
akmhoque298385a2014-02-13 14:13:09 -0600495
akmhoque1fd8c1e2014-02-19 19:41:49 -0600496 }
497 else
498 {
499 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
500 {
501 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
502 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
akmhoque298385a2014-02-13 14:13:09 -0600503
akmhoque1fd8c1e2014-02-19 19:41:49 -0600504 if ( ! chkAdjLsa.first.isLsaContentEqual(alsa))
505 {
506 chkAdjLsa.first.getAdl().resetAdl();
507 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
508 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
509 }
akmhoque298385a2014-02-13 14:13:09 -0600510
akmhoque1fd8c1e2014-02-19 19:41:49 -0600511 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
512 {
513 timeToExpire=alsa.getLifeTime();
514 }
515 cancelScheduleLsaExpiringEvent(pnlsr,
516 chkAdjLsa.first.getLsaExpiringEventId());
517 chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
518 alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire));
519 }
akmhoque298385a2014-02-13 14:13:09 -0600520
akmhoque1fd8c1e2014-02-19 19:41:49 -0600521 }
akmhoque298385a2014-02-13 14:13:09 -0600522
akmhoque1fd8c1e2014-02-19 19:41:49 -0600523 printAdjLsdb();
akmhoque298385a2014-02-13 14:13:09 -0600524
akmhoque1fd8c1e2014-02-19 19:41:49 -0600525 return true;
526 }
akmhoque298385a2014-02-13 14:13:09 -0600527
akmhoque1fd8c1e2014-02-19 19:41:49 -0600528 bool
529 Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
530 {
531 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
532 , 2
533 , pnlsr.getSm().getAdjLsaSeq()+1
534 , pnlsr.getConfParameter().getRouterDeadInterval()
535 , pnlsr.getAdl().getNumOfActiveNeighbor()
536 , pnlsr.getAdl() );
537 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
538 return installAdjLsa(pnlsr, adjLsa);
539 }
akmhoque298385a2014-02-13 14:13:09 -0600540
akmhoque1fd8c1e2014-02-19 19:41:49 -0600541 bool
542 Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
543 {
544 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
545 adjLsdb.end(),
546 bind(adjLsaCompareByKey, _1, key));
547 if ( it != adjLsdb.end() )
548 {
549 (*it).removeNptEntriesForAdjLsa(pnlsr);
550 adjLsdb.erase(it);
551 return true;
552 }
553 return false;
akmhoque298385a2014-02-13 14:13:09 -0600554
akmhoque1fd8c1e2014-02-19 19:41:49 -0600555 }
akmhoque298385a2014-02-13 14:13:09 -0600556
akmhoque1fd8c1e2014-02-19 19:41:49 -0600557 bool
558 Lsdb::doesAdjLsaExist(string key)
559 {
560 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
561 adjLsdb.end(),
562 bind(adjLsaCompareByKey, _1, key));
akmhoque298385a2014-02-13 14:13:09 -0600563
akmhoque1fd8c1e2014-02-19 19:41:49 -0600564 if( it == adjLsdb.end())
565 {
566 return false;
567 }
akmhoque298385a2014-02-13 14:13:09 -0600568
akmhoque1fd8c1e2014-02-19 19:41:49 -0600569 return true;
570 }
akmhoque298385a2014-02-13 14:13:09 -0600571
akmhoque1fd8c1e2014-02-19 19:41:49 -0600572 std::list<AdjLsa>&
573 Lsdb::getAdjLsdb()
574 {
575 return adjLsdb;
576 }
akmhoque298385a2014-02-13 14:13:09 -0600577
akmhoque1fd8c1e2014-02-19 19:41:49 -0600578 void
579 Lsdb::setLsaRefreshTime(int lrt)
580 {
581 lsaRefreshTime=lrt;
582 }
akmhoque298385a2014-02-13 14:13:09 -0600583
akmhoque1fd8c1e2014-02-19 19:41:49 -0600584 void
585 Lsdb::setThisRouterPrefix(string trp)
586 {
587 thisRouterPrefix=trp;
588 }
589
590 void
591 Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
592 {
593 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
594 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
595 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
596 if( chkNameLsa.second )
597 {
598 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
599 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
600 {
601 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
602 {
603 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
604 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
605 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
606 // publish routing update
607 }
608 else
609 {
610 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
611 removeNameLsa(pnlsr, lsaKey);
612 }
613 }
614 }
615 }
616
617 void
618 Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
619 {
620 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
621 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
622 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
623 if( chkAdjLsa.second )
624 {
625 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
626 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
627 {
628 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
629 {
630 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
631 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
632 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
633 // publish routing update
634 }
635 else
636 {
637 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
638 removeAdjLsa(pnlsr, lsaKey);
639 }
640
641 // schedule Routing table calculaiton
642 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
643 }
644 }
645 }
646
647 void
648 Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
649 {
650 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
651 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
652 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
653 if( chkCorLsa.second )
654 {
655 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
656 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
657 {
658 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
659 {
660 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
661 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
662 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
663 // publish routing update
664 }
665 else
666 {
667 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
668 removeCorLsa(pnlsr, lsaKey);
669 }
670
671 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
672 {
673 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
674 }
675 }
676 }
677 }
akmhoque298385a2014-02-13 14:13:09 -0600678
679
akmhoque1fd8c1e2014-02-19 19:41:49 -0600680 void
681 Lsdb::printAdjLsdb()
682 {
683 cout<<"---------------Adj LSDB-------------------"<<endl;
684 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
685 it!= adjLsdb.end() ; it++)
686 {
687 cout<< (*it) <<endl;
688 }
689 }
akmhoque298385a2014-02-13 14:13:09 -0600690
691//-----utility function -----
akmhoque1fd8c1e2014-02-19 19:41:49 -0600692 bool
693 Lsdb::doesLsaExist(string key, int lsType)
694 {
695 if ( lsType == 1)
696 {
697 return doesNameLsaExist(key);
698 }
699 else if ( lsType == 2)
700 {
701 return doesAdjLsaExist(key);
702 }
703 else if ( lsType == 3)
704 {
705 return doesCorLsaExist(key);
706 }
707
708 return false;
709 }
akmhoque298385a2014-02-13 14:13:09 -0600710
akmhoqueb1710aa2014-02-19 17:13:36 -0600711}//namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -0600712