blob: 3da115a866e5aefef41511e5f4e2b37463d6eace [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
akmhoque5a44dd42014-03-12 18:11:32 -05009 using namespace std;
akmhoque1fd8c1e2014-02-19 19:41:49 -060010
akmhoque5a44dd42014-03-12 18:11:32 -050011 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 }
22
23
24 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);
34 }
35
36 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));
42 if( it != nameLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -060043 {
akmhoque5a44dd42014-03-12 18:11:32 -050044 return std::make_pair(boost::ref((*it)),true);
akmhoque1fd8c1e2014-02-19 19:41:49 -060045 }
akmhoque5a44dd42014-03-12 18:11:32 -050046 NameLsa nlsa;
47 return std::make_pair(boost::ref(nlsa),false);
48 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060049
akmhoque5a44dd42014-03-12 18:11:32 -050050 bool
51 Lsdb::isNameLsaNew(string key, uint64_t seqNo)
52 {
53 std::pair<NameLsa& , bool> nameLsaCheck=getNameLsa(key);
54 if(nameLsaCheck.second)
akmhoque1fd8c1e2014-02-19 19:41:49 -060055 {
akmhoque5a44dd42014-03-12 18:11:32 -050056 if(nameLsaCheck.first.getLsSeqNo() < seqNo)
57 {
akmhoque2bb198e2014-02-28 11:46:27 -060058 return true;
akmhoque5a44dd42014-03-12 18:11:32 -050059 }
60 else
61 {
akmhoque1fd8c1e2014-02-19 19:41:49 -060062 return false;
akmhoque5a44dd42014-03-12 18:11:32 -050063 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060064 }
akmhoque5a44dd42014-03-12 18:11:32 -050065 return true;
66 }
akmhoque298385a2014-02-13 14:13:09 -060067
akmhoque5a44dd42014-03-12 18:11:32 -050068 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 }
75
76 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 )
akmhoque1fd8c1e2014-02-19 19:41:49 -060082 {
akmhoque5a44dd42014-03-12 18:11:32 -050083 addNameLsa(nlsa);
84 printNameLsdb();
85 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
86 {
87 pnlsr.getNpt().addNpteByDestName(nlsa.getOrigRouter(),nlsa.getOrigRouter(),
88 pnlsr);
89 std::list<string> nameList=nlsa.getNpl().getNameList();
90 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -060091 {
akmhoque5a44dd42014-03-12 18:11:32 -050092 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
93 {
94 pnlsr.getNpt().addNpteByDestName((*it),nlsa.getOrigRouter(),pnlsr);
95 }
96 }
97 }
98 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
99 {
100 timeToExpire=nlsa.getLifeTime();
101 }
102 nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
103 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
104 }
105 else
106 {
107 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
108 {
109 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
110 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
111 chkNameLsa.first.getNpl().sortNpl();
112 nlsa.getNpl().sortNpl();
113 std::list<string> nameToAdd;
114 std::set_difference(nlsa.getNpl().getNameList().begin(),
115 nlsa.getNpl().getNameList().end(),
116 chkNameLsa.first.getNpl().getNameList().begin(),
117 chkNameLsa.first.getNpl().getNameList().end(),
118 std::inserter(nameToAdd, nameToAdd.begin()));
119 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
120 ++it)
121 {
122 chkNameLsa.first.addNameToLsa((*it));
123 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
124 {
125 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600126 {
akmhoque5a44dd42014-03-12 18:11:32 -0500127 pnlsr.getNpt().addNpteByDestName((*it),nlsa.getOrigRouter(),pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600128 }
akmhoque5a44dd42014-03-12 18:11:32 -0500129 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600130 }
akmhoque5a44dd42014-03-12 18:11:32 -0500131 std::list<string> nameToRemove;
132 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
133 chkNameLsa.first.getNpl().getNameList().end(),
134 nlsa.getNpl().getNameList().begin(),
135 nlsa.getNpl().getNameList().end(),
136 std::inserter(nameToRemove, nameToRemove.begin()));
137 for(std::list<string>::iterator it=nameToRemove.begin();
138 it!=nameToRemove.end(); ++it)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600139 {
akmhoque5a44dd42014-03-12 18:11:32 -0500140 chkNameLsa.first.removeNameFromLsa((*it));
141 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
142 {
143 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
144 {
145 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
146 }
147 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600148 }
akmhoque5a44dd42014-03-12 18:11:32 -0500149 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600150 {
akmhoque5a44dd42014-03-12 18:11:32 -0500151 timeToExpire=nlsa.getLifeTime();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600152 }
akmhoque5a44dd42014-03-12 18:11:32 -0500153 cancelScheduleLsaExpiringEvent(pnlsr,
154 chkNameLsa.first.getLsaExpiringEventId());
155 chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
156 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
157 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600158 }
akmhoque5a44dd42014-03-12 18:11:32 -0500159 return true;
160 }
161
162 bool
163 Lsdb::addNameLsa(NameLsa &nlsa)
164 {
165 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
166 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
167 if( it == nameLsdb.end())
168 {
169 nameLsdb.push_back(nlsa);
170 return true;
171 }
172 return false;
173 }
174
175 bool
176 Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
177 {
178 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
179 nameLsdb.end(),
180 bind(nameLsaCompareByKey, _1, key));
181 if ( it != nameLsdb.end() )
182 {
183 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
184 {
185 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
186 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
187 nit!=(*it).getNpl().getNameList().end(); ++nit)
188 {
189 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
190 {
191 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
192 }
193 }
194 }
195 nameLsdb.erase(it);
196 return true;
197 }
198 return false;
199 }
200
201 bool
202 Lsdb::doesNameLsaExist(string key)
203 {
204 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
205 nameLsdb.end(),
206 bind(nameLsaCompareByKey, _1, key));
207 if( it == nameLsdb.end())
208 {
209 return false;
210 }
211 return true;
212 }
213
214 void
215 Lsdb::printNameLsdb()
216 {
217 cout<<"---------------Name LSDB-------------------"<<endl;
218 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
219 it!= nameLsdb.end() ; it++)
220 {
221 cout<< (*it) <<endl;
222 }
223 }
akmhoque298385a2014-02-13 14:13:09 -0600224
225// Cor LSA and LSDB related Functions start here
226
227
akmhoque5a44dd42014-03-12 18:11:32 -0500228 static bool
229 corLsaCompareByKey(CorLsa& clsa, string& key)
230 {
231 return clsa.getCorLsaKey()==key;
232 }
akmhoque298385a2014-02-13 14:13:09 -0600233
akmhoque5a44dd42014-03-12 18:11:32 -0500234 bool
235 Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr)
236 {
237 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
238 , 3
239 , pnlsr.getSm().getCorLsaSeq()+1
240 , pnlsr.getConfParameter().getRouterDeadInterval()
241 , pnlsr.getConfParameter().getCorR()
242 , pnlsr.getConfParameter().getCorTheta() );
243 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
244 installCorLsa(pnlsr, corLsa);
245 return true;
246 }
247
248 std::pair<CorLsa&, bool>
249 Lsdb::getCorLsa(string key)
250 {
251 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
252 corLsdb.end(),
253 bind(corLsaCompareByKey, _1, key));
254 if( it != corLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600255 {
akmhoque5a44dd42014-03-12 18:11:32 -0500256 return std::make_pair(boost::ref((*it)), true);
257 }
258 CorLsa clsa;
259 return std::make_pair(boost::ref(clsa),false);
260 }
261
262 bool
263 Lsdb::isCorLsaNew(string key, uint64_t seqNo)
264 {
265 std::pair<CorLsa& , bool> corLsaCheck=getCorLsa(key);
266 if(corLsaCheck.second)
267 {
268 if(corLsaCheck.first.getLsSeqNo() < seqNo)
269 {
akmhoque1fd8c1e2014-02-19 19:41:49 -0600270 return true;
akmhoque5a44dd42014-03-12 18:11:32 -0500271 }
272 else
273 {
akmhoque1fd8c1e2014-02-19 19:41:49 -0600274 return false;
akmhoque5a44dd42014-03-12 18:11:32 -0500275 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600276 }
akmhoque5a44dd42014-03-12 18:11:32 -0500277 return true;
278 }
akmhoque298385a2014-02-13 14:13:09 -0600279
akmhoque5a44dd42014-03-12 18:11:32 -0500280 ndn::EventId
281 Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
282 {
283 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
284 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
285 this,boost::ref(pnlsr),key,seqNo));
286 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600287
akmhoque5a44dd42014-03-12 18:11:32 -0500288 bool
289 Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa)
290 {
291 int timeToExpire=lsaRefreshTime;
292 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
293 if ( !chkCorLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600294 {
akmhoque5a44dd42014-03-12 18:11:32 -0500295 addCorLsa(clsa);
296 printCorLsdb(); //debugging purpose
297 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
298 {
299 pnlsr.getNpt().addNpteByDestName(clsa.getOrigRouter(),clsa.getOrigRouter(),
300 pnlsr);
301 }
302 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
303 {
304 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
305 }
306 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
307 {
308 timeToExpire=clsa.getLifeTime();
309 }
310 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
311 clsa.getLsSeqNo(), timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600312 }
akmhoque5a44dd42014-03-12 18:11:32 -0500313 else
314 {
315 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
316 {
317 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
318 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
319 if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
320 {
321 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
322 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
323 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
324 {
325 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
326 }
327 }
328 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
329 {
330 timeToExpire=clsa.getLifeTime();
331 }
332 cancelScheduleLsaExpiringEvent(pnlsr,
333 chkCorLsa.first.getLsaExpiringEventId());
334 chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr,
335 clsa.getCorLsaKey(),
336 clsa.getLsSeqNo(), timeToExpire));
337 }
338 }
339 return true;
340 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600341
akmhoque5a44dd42014-03-12 18:11:32 -0500342 bool
343 Lsdb::addCorLsa(CorLsa& clsa)
344 {
345 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
346 corLsdb.end(),
347 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
348 if( it == corLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600349 {
akmhoque5a44dd42014-03-12 18:11:32 -0500350 corLsdb.push_back(clsa);
351 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600352 }
akmhoque5a44dd42014-03-12 18:11:32 -0500353 return false;
354 }
355
356 bool
357 Lsdb::removeCorLsa(Nlsr& pnlsr, string& key)
358 {
359 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
360 corLsdb.end(),
361 bind(corLsaCompareByKey, _1, key));
362 if ( it != corLsdb.end() )
363 {
364 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
365 {
366 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
367 }
368 corLsdb.erase(it);
369 return true;
370 }
371 return false;
372 }
373
374 bool
375 Lsdb::doesCorLsaExist(string key)
376 {
377 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
378 corLsdb.end(),
379 bind(corLsaCompareByKey, _1, key));
380 if( it == corLsdb.end())
381 {
382 return false;
383 }
384 return true;
385 }
386
387 void
388 Lsdb::printCorLsdb() //debugging
389 {
390 cout<<"---------------Cor LSDB-------------------"<<endl;
391 for( std::list<CorLsa>::iterator it=corLsdb.begin();
392 it!= corLsdb.end() ; it++)
393 {
394 cout<< (*it) <<endl;
395 }
396 }
akmhoque298385a2014-02-13 14:13:09 -0600397
398
399// Adj LSA and LSDB related function starts here
400
akmhoque5a44dd42014-03-12 18:11:32 -0500401 static bool
402 adjLsaCompareByKey(AdjLsa& alsa, string& key)
403 {
404 return alsa.getAdjLsaKey()==key;
405 }
akmhoque298385a2014-02-13 14:13:09 -0600406
407
akmhoque5a44dd42014-03-12 18:11:32 -0500408 void
409 Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
410 {
411 cout<<"scheduledAdjLsaBuild Called"<<endl;
412 pnlsr.setIsBuildAdjLsaSheduled(0);
413 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
akmhoque1fd8c1e2014-02-19 19:41:49 -0600414 {
akmhoque5a44dd42014-03-12 18:11:32 -0500415 int adjBuildCount=pnlsr.getAdjBuildCount();
416 if(adjBuildCount>0 )
417 {
418 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600419 {
akmhoque5a44dd42014-03-12 18:11:32 -0500420 buildAndInstallOwnAdjLsa(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600421 }
422 else
423 {
akmhoque5a44dd42014-03-12 18:11:32 -0500424 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
425 removeAdjLsa(pnlsr,key);
426 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600427 }
akmhoque5a44dd42014-03-12 18:11:32 -0500428 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
429 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600430 }
akmhoque5a44dd42014-03-12 18:11:32 -0500431 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600432 {
akmhoque5a44dd42014-03-12 18:11:32 -0500433 pnlsr.setIsBuildAdjLsaSheduled(1);
434 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
435 pnlsr.getConfParameter().getInterestResendTime();
436 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
437 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
438 boost::ref(pnlsr)));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600439 }
akmhoque5a44dd42014-03-12 18:11:32 -0500440 }
akmhoque298385a2014-02-13 14:13:09 -0600441
akmhoque5a44dd42014-03-12 18:11:32 -0500442
443 bool
444 Lsdb::addAdjLsa(AdjLsa &alsa)
445 {
446 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
447 adjLsdb.end(),
448 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
449 if( it == adjLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600450 {
akmhoque5a44dd42014-03-12 18:11:32 -0500451 adjLsdb.push_back(alsa);
452 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600453 }
akmhoque5a44dd42014-03-12 18:11:32 -0500454 return false;
455 }
akmhoque298385a2014-02-13 14:13:09 -0600456
akmhoque5a44dd42014-03-12 18:11:32 -0500457 std::pair<AdjLsa& , bool>
458 Lsdb::getAdjLsa(string key)
459 {
460 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
461 adjLsdb.end(),
462 bind(adjLsaCompareByKey, _1, key));
463 if( it != adjLsdb.end())
akmhoque2bb198e2014-02-28 11:46:27 -0600464 {
akmhoque5a44dd42014-03-12 18:11:32 -0500465 return std::make_pair(boost::ref((*it)),true);
466 }
467 AdjLsa alsa;
468 return std::make_pair(boost::ref(alsa),false);
469 }
470
471
472 bool
473 Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
474 {
475 std::pair<AdjLsa& , bool> adjLsaCheck=getAdjLsa(key);
476 if(adjLsaCheck.second)
477 {
478 if(adjLsaCheck.first.getLsSeqNo() < seqNo)
479 {
akmhoque2bb198e2014-02-28 11:46:27 -0600480 return true;
akmhoque5a44dd42014-03-12 18:11:32 -0500481 }
482 else
483 {
484 return false;
485 }
akmhoque2bb198e2014-02-28 11:46:27 -0600486 }
akmhoque5a44dd42014-03-12 18:11:32 -0500487 return true;
488 }
akmhoque2bb198e2014-02-28 11:46:27 -0600489
akmhoque298385a2014-02-13 14:13:09 -0600490
akmhoque5a44dd42014-03-12 18:11:32 -0500491 ndn::EventId
492 Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
493 {
494 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
495 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
496 this,boost::ref(pnlsr),key,seqNo));
497 }
498
499 bool
500 Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa)
501 {
502 int timeToExpire=lsaRefreshTime;
503 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
504 if ( !chkAdjLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600505 {
akmhoque5a44dd42014-03-12 18:11:32 -0500506 addAdjLsa(alsa);
507 alsa.addNptEntriesForAdjLsa(pnlsr);
508 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
509 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
510 {
511 timeToExpire=alsa.getLifeTime();
512 }
513 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
514 alsa.getLsSeqNo(),timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600515 }
akmhoque5a44dd42014-03-12 18:11:32 -0500516 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600517 {
akmhoque5a44dd42014-03-12 18:11:32 -0500518 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
519 {
520 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
521 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
522 if ( ! chkAdjLsa.first.isLsaContentEqual(alsa))
akmhoque1fd8c1e2014-02-19 19:41:49 -0600523 {
akmhoque5a44dd42014-03-12 18:11:32 -0500524 chkAdjLsa.first.getAdl().resetAdl();
525 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
526 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
527 }
528 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
529 {
530 timeToExpire=alsa.getLifeTime();
531 }
532 cancelScheduleLsaExpiringEvent(pnlsr,
533 chkAdjLsa.first.getLsaExpiringEventId());
534 chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
535 alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire));
536 }
537 }
538 return true;
539 }
540
541 bool
542 Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
543 {
544 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
545 , 2
546 , pnlsr.getSm().getAdjLsaSeq()+1
547 , pnlsr.getConfParameter().getRouterDeadInterval()
548 , pnlsr.getAdl().getNumOfActiveNeighbor()
549 , pnlsr.getAdl() );
550 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
551 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
552 + pnlsr.getConfParameter().getRouterPrefix();
553 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
554 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
555 }
556
557 bool
558 Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
559 {
560 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
561 adjLsdb.end(),
562 bind(adjLsaCompareByKey, _1, key));
563 if ( it != adjLsdb.end() )
564 {
565 (*it).removeNptEntriesForAdjLsa(pnlsr);
566 adjLsdb.erase(it);
567 return true;
568 }
569 return false;
570 }
571
572 bool
573 Lsdb::doesAdjLsaExist(string key)
574 {
575 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
576 adjLsdb.end(),
577 bind(adjLsaCompareByKey, _1, key));
578 if( it == adjLsdb.end())
579 {
580 return false;
581 }
582 return true;
583 }
584
585 std::list<AdjLsa>&
586 Lsdb::getAdjLsdb()
587 {
588 return adjLsdb;
589 }
590
591 void
592 Lsdb::setLsaRefreshTime(int lrt)
593 {
594 lsaRefreshTime=lrt;
595 }
596
597 void
598 Lsdb::setThisRouterPrefix(string trp)
599 {
600 thisRouterPrefix=trp;
601 }
602
603 void
604 Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
605 {
606 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
607 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
608 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
609 if( chkNameLsa.second )
610 {
611 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
612 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
613 {
614 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
615 {
616 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
617 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
618 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
619 // publish routing update
620 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
621 + pnlsr.getConfParameter().getRouterPrefix();
622 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600623 }
624 else
625 {
akmhoque5a44dd42014-03-12 18:11:32 -0500626 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
627 removeNameLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600628 }
akmhoque5a44dd42014-03-12 18:11:32 -0500629 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600630 }
akmhoque5a44dd42014-03-12 18:11:32 -0500631 }
akmhoque298385a2014-02-13 14:13:09 -0600632
akmhoque5a44dd42014-03-12 18:11:32 -0500633 void
634 Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
635 {
636 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
637 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
638 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
639 if( chkAdjLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600640 {
akmhoque5a44dd42014-03-12 18:11:32 -0500641 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
642 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
643 {
644 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600645 {
akmhoque5a44dd42014-03-12 18:11:32 -0500646 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
647 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
648 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
649 // publish routing update
650 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
651 + pnlsr.getConfParameter().getRouterPrefix();
652 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600653 }
akmhoque5a44dd42014-03-12 18:11:32 -0500654 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600655 {
akmhoque5a44dd42014-03-12 18:11:32 -0500656 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
657 removeAdjLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600658 }
akmhoque5a44dd42014-03-12 18:11:32 -0500659 // schedule Routing table calculaiton
660 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
661 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600662 }
akmhoque5a44dd42014-03-12 18:11:32 -0500663 }
akmhoque298385a2014-02-13 14:13:09 -0600664
akmhoque5a44dd42014-03-12 18:11:32 -0500665 void
666 Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
667 {
668 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
669 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
670 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
671 if( chkCorLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600672 {
akmhoque5a44dd42014-03-12 18:11:32 -0500673 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
674 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
675 {
676 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600677 {
akmhoque5a44dd42014-03-12 18:11:32 -0500678 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
679 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
680 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
681 // publish routing update
682 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
683 + pnlsr.getConfParameter().getRouterPrefix();
684 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600685 }
akmhoque5a44dd42014-03-12 18:11:32 -0500686 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600687 {
akmhoque5a44dd42014-03-12 18:11:32 -0500688 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
689 removeCorLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600690 }
akmhoque5a44dd42014-03-12 18:11:32 -0500691 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600692 {
akmhoque5a44dd42014-03-12 18:11:32 -0500693 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600694 }
akmhoque5a44dd42014-03-12 18:11:32 -0500695 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600696 }
akmhoque5a44dd42014-03-12 18:11:32 -0500697 }
akmhoque298385a2014-02-13 14:13:09 -0600698
699
akmhoque5a44dd42014-03-12 18:11:32 -0500700 void
701 Lsdb::printAdjLsdb()
702 {
703 cout<<"---------------Adj LSDB-------------------"<<endl;
704 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
705 it!= adjLsdb.end() ; it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600706 {
akmhoque5a44dd42014-03-12 18:11:32 -0500707 cout<< (*it) <<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600708 }
akmhoque5a44dd42014-03-12 18:11:32 -0500709 }
akmhoque298385a2014-02-13 14:13:09 -0600710
711//-----utility function -----
akmhoque5a44dd42014-03-12 18:11:32 -0500712 bool
713 Lsdb::doesLsaExist(string key, int lsType)
714 {
715 if ( lsType == 1)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600716 {
akmhoque5a44dd42014-03-12 18:11:32 -0500717 return doesNameLsaExist(key);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600718 }
akmhoque5a44dd42014-03-12 18:11:32 -0500719 else if ( lsType == 2)
720 {
721 return doesAdjLsaExist(key);
722 }
723 else if ( lsType == 3)
724 {
725 return doesCorLsaExist(key);
726 }
727 return false;
728 }
akmhoque298385a2014-02-13 14:13:09 -0600729
akmhoqueb1710aa2014-02-19 17:13:36 -0600730}//namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -0600731