blob: cd80809503fbad164d4c6c4b26f53dcc713e8e80 [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
50
akmhoque1fd8c1e2014-02-19 19:41:49 -060051 ndn::EventId
52 Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
53 {
54 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
55 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
56 this,boost::ref(pnlsr), key, seqNo));
57 }
akmhoque298385a2014-02-13 14:13:09 -060058
akmhoque1fd8c1e2014-02-19 19:41:49 -060059 bool
60 Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa &nlsa)
61 {
62 int timeToExpire=lsaRefreshTime;
63 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey());
64 if ( !chkNameLsa.second )
65 {
66 addNameLsa(nlsa);
67 printNameLsdb();
68 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
69 {
70 pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr);
71 std::list<string> nameList=nlsa.getNpl().getNameList();
72 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end(); it++)
73 {
74 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
75 {
76 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
77 }
78 }
79 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060080 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
81 {
82 timeToExpire=nlsa.getLifeTime();
83 }
84 nlsa.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
85 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
86 }
87 else
88 {
89 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
90 {
91 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
92 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
akmhoque1fd8c1e2014-02-19 19:41:49 -060093 chkNameLsa.first.getNpl().sortNpl();
94 nlsa.getNpl().sortNpl();
akmhoque1fd8c1e2014-02-19 19:41:49 -060095 std::list<string> nameToAdd;
96 std::set_difference(nlsa.getNpl().getNameList().begin(),
97 nlsa.getNpl().getNameList().end(),
98 chkNameLsa.first.getNpl().getNameList().begin(),
99 chkNameLsa.first.getNpl().getNameList().end(),
100 std::inserter(nameToAdd, nameToAdd.begin()));
101 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
102 ++it)
103 {
104 chkNameLsa.first.addNameToLsa((*it));
105 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
106 {
107 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
108 {
109 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
110 }
111 }
112 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600113 std::list<string> nameToRemove;
114 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
115 chkNameLsa.first.getNpl().getNameList().end(),
116 nlsa.getNpl().getNameList().begin(),
117 nlsa.getNpl().getNameList().end(),
118 std::inserter(nameToRemove, nameToRemove.begin()));
119 for(std::list<string>::iterator it=nameToRemove.begin();
120 it!=nameToRemove.end(); ++it)
121 {
122 chkNameLsa.first.removeNameFromLsa((*it));
123 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
124 {
125 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
126 {
127 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
128 }
129 }
130 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600131 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
132 {
133 timeToExpire=nlsa.getLifeTime();
134 }
135 cancelScheduleLsaExpiringEvent(pnlsr,
136 chkNameLsa.first.getLsaExpiringEventId());
137 chkNameLsa.first.setLsaExpiringEventId(scheduleNameLsaExpiration( pnlsr,
138 nlsa.getNameLsaKey(), nlsa.getLsSeqNo(), timeToExpire));
139 }
140 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600141 return true;
142 }
akmhoque298385a2014-02-13 14:13:09 -0600143
akmhoque1fd8c1e2014-02-19 19:41:49 -0600144 bool
145 Lsdb::addNameLsa(NameLsa &nlsa)
146 {
147 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
148 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600149 if( it == nameLsdb.end())
150 {
151 nameLsdb.push_back(nlsa);
152 return true;
153 }
154 return false;
155 }
akmhoque298385a2014-02-13 14:13:09 -0600156
akmhoque1fd8c1e2014-02-19 19:41:49 -0600157 bool
158 Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
159 {
160 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
161 nameLsdb.end(),
162 bind(nameLsaCompareByKey, _1, key));
163 if ( it != nameLsdb.end() )
164 {
165 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
166 {
167 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
168 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
169 nit!=(*it).getNpl().getNameList().end(); ++nit)
170 {
171 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
172 {
173 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
174 }
175 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600176 }
177 nameLsdb.erase(it);
178 return true;
179 }
180 return false;
181 }
akmhoque298385a2014-02-13 14:13:09 -0600182
akmhoque1fd8c1e2014-02-19 19:41:49 -0600183 bool
184 Lsdb::doesNameLsaExist(string key)
185 {
186 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
187 nameLsdb.end(),
188 bind(nameLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600189 if( it == nameLsdb.end())
190 {
191 return false;
192 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600193 return true;
194 }
195
196 void
197 Lsdb::printNameLsdb()
198 {
199 cout<<"---------------Name LSDB-------------------"<<endl;
200 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
201 it!= nameLsdb.end() ; it++)
202 {
203 cout<< (*it) <<endl;
204 }
205 }
akmhoque298385a2014-02-13 14:13:09 -0600206
207// Cor LSA and LSDB related Functions start here
208
209
akmhoque1fd8c1e2014-02-19 19:41:49 -0600210 static bool
211 corLsaCompareByKey(CorLsa& clsa, string& key)
212 {
213 return clsa.getCorLsaKey()==key;
214 }
akmhoque298385a2014-02-13 14:13:09 -0600215
akmhoque1fd8c1e2014-02-19 19:41:49 -0600216 bool
217 Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr)
218 {
219 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
220 , 3
221 , pnlsr.getSm().getCorLsaSeq()+1
222 , pnlsr.getConfParameter().getRouterDeadInterval()
223 , pnlsr.getConfParameter().getCorR()
224 , pnlsr.getConfParameter().getCorTheta() );
225 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
226 installCorLsa(pnlsr, corLsa);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600227 return true;
228 }
akmhoque298385a2014-02-13 14:13:09 -0600229
akmhoque1fd8c1e2014-02-19 19:41:49 -0600230 std::pair<CorLsa&, bool>
231 Lsdb::getCorLsa(string key)
232 {
233 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
234 corLsdb.end(),
235 bind(corLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600236 if( it != corLsdb.end())
237 {
238 return std::make_pair(boost::ref((*it)), true);
239 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600240 CorLsa clsa;
241 return std::make_pair(boost::ref(clsa),false);
242 }
akmhoque298385a2014-02-13 14:13:09 -0600243
akmhoque1fd8c1e2014-02-19 19:41:49 -0600244 ndn::EventId
245 Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
246 {
247 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
248 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
249 this,boost::ref(pnlsr),key,seqNo));
250 }
akmhoque298385a2014-02-13 14:13:09 -0600251
akmhoque1fd8c1e2014-02-19 19:41:49 -0600252 bool
253 Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa)
254 {
255 int timeToExpire=lsaRefreshTime;
256 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
257 if ( !chkCorLsa.second )
258 {
259 addCorLsa(clsa);
260 printCorLsdb(); //debugging purpose
261 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
262 {
263 pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
264 }
265 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
266 {
267 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
268 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600269 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
270 {
271 timeToExpire=clsa.getLifeTime();
272 }
273 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
274 clsa.getLsSeqNo(), timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600275 }
276 else
277 {
278 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
279 {
280 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
281 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
282 if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
283 {
284 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
285 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600286 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
287 {
288 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
289 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600290 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600291 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
292 {
293 timeToExpire=clsa.getLifeTime();
294 }
295 cancelScheduleLsaExpiringEvent(pnlsr,
296 chkCorLsa.first.getLsaExpiringEventId());
297 chkCorLsa.first.setLsaExpiringEventId(scheduleCorLsaExpiration(pnlsr,
298 clsa.getCorLsaKey(),
299 clsa.getLsSeqNo(), timeToExpire));
300 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600301 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600302 return true;
303 }
akmhoque298385a2014-02-13 14:13:09 -0600304
akmhoque1fd8c1e2014-02-19 19:41:49 -0600305 bool
306 Lsdb::addCorLsa(CorLsa& clsa)
307 {
308 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
309 corLsdb.end(),
310 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600311 if( it == corLsdb.end())
312 {
313 corLsdb.push_back(clsa);
314 return true;
315 }
316 return false;
317 }
akmhoque298385a2014-02-13 14:13:09 -0600318
akmhoque1fd8c1e2014-02-19 19:41:49 -0600319 bool
320 Lsdb::removeCorLsa(Nlsr& pnlsr, string& key)
321 {
322 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
323 corLsdb.end(),
324 bind(corLsaCompareByKey, _1, key));
325 if ( it != corLsdb.end() )
326 {
327 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
328 {
329 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
330 }
331 corLsdb.erase(it);
332 return true;
333 }
334 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600335 }
336
337 bool
338 Lsdb::doesCorLsaExist(string key)
339 {
340 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
341 corLsdb.end(),
342 bind(corLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600343 if( it == corLsdb.end())
344 {
345 return false;
346 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600347 return true;
348 }
349
350 void
351 Lsdb::printCorLsdb() //debugging
352 {
353 cout<<"---------------Cor LSDB-------------------"<<endl;
354 for( std::list<CorLsa>::iterator it=corLsdb.begin();
355 it!= corLsdb.end() ; it++)
356 {
357 cout<< (*it) <<endl;
358 }
359 }
akmhoque298385a2014-02-13 14:13:09 -0600360
361
362// Adj LSA and LSDB related function starts here
363
akmhoque1fd8c1e2014-02-19 19:41:49 -0600364 static bool
365 adjLsaCompareByKey(AdjLsa& alsa, string& key)
366 {
367 return alsa.getAdjLsaKey()==key;
368 }
akmhoque298385a2014-02-13 14:13:09 -0600369
370
akmhoque1fd8c1e2014-02-19 19:41:49 -0600371 void
372 Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
373 {
374 cout<<"scheduledAdjLsaBuild Called"<<endl;
375 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600376 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
377 {
378 int adjBuildCount=pnlsr.getAdjBuildCount();
379 if(adjBuildCount>0 )
380 {
381 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
382 {
383 buildAndInstallOwnAdjLsa(pnlsr);
384 }
385 else
386 {
387 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
388 removeAdjLsa(pnlsr,key);
389 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
390 }
391 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
392 }
393 }
394 else
395 {
396 pnlsr.setIsBuildAdjLsaSheduled(1);
397 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
398 pnlsr.getConfParameter().getInterestResendTime();
399 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
400 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
401 boost::ref(pnlsr)));
402 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600403 }
akmhoque298385a2014-02-13 14:13:09 -0600404
405
akmhoque1fd8c1e2014-02-19 19:41:49 -0600406 bool
407 Lsdb::addAdjLsa(AdjLsa &alsa)
408 {
409 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
410 adjLsdb.end(),
411 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600412 if( it == adjLsdb.end())
413 {
414 adjLsdb.push_back(alsa);
415 return true;
416 }
417 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600418 }
akmhoque298385a2014-02-13 14:13:09 -0600419
akmhoque1fd8c1e2014-02-19 19:41:49 -0600420 std::pair<AdjLsa& , bool>
421 Lsdb::getAdjLsa(string key)
422 {
423 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
424 adjLsdb.end(),
425 bind(adjLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600426 if( it != adjLsdb.end())
427 {
428 return std::make_pair(boost::ref((*it)),true);
429 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600430 AdjLsa alsa;
431 return std::make_pair(boost::ref(alsa),false);
432 }
akmhoque298385a2014-02-13 14:13:09 -0600433
434
435
akmhoque1fd8c1e2014-02-19 19:41:49 -0600436 ndn::EventId
437 Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
438 {
439 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
440 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
441 this,boost::ref(pnlsr),key,seqNo));
442 }
akmhoque298385a2014-02-13 14:13:09 -0600443
akmhoque1fd8c1e2014-02-19 19:41:49 -0600444 bool
445 Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa)
446 {
447 int timeToExpire=lsaRefreshTime;
448 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
449 if ( !chkAdjLsa.second )
450 {
451 addAdjLsa(alsa);
452 alsa.addNptEntriesForAdjLsa(pnlsr);
453 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600454 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
455 {
456 timeToExpire=alsa.getLifeTime();
457 }
458 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
459 alsa.getLsSeqNo(),timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600460 }
461 else
462 {
463 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
464 {
465 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
466 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600467 if ( ! chkAdjLsa.first.isLsaContentEqual(alsa))
468 {
469 chkAdjLsa.first.getAdl().resetAdl();
470 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
471 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
472 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600473 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
474 {
475 timeToExpire=alsa.getLifeTime();
476 }
477 cancelScheduleLsaExpiringEvent(pnlsr,
478 chkAdjLsa.first.getLsaExpiringEventId());
479 chkAdjLsa.first.setLsaExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
480 alsa.getAdjLsaKey(), alsa.getLsSeqNo(),timeToExpire));
481 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600482 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600483 printAdjLsdb();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600484 return true;
485 }
akmhoque298385a2014-02-13 14:13:09 -0600486
akmhoque1fd8c1e2014-02-19 19:41:49 -0600487 bool
488 Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
489 {
490 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
491 , 2
492 , pnlsr.getSm().getAdjLsaSeq()+1
493 , pnlsr.getConfParameter().getRouterDeadInterval()
494 , pnlsr.getAdl().getNumOfActiveNeighbor()
495 , pnlsr.getAdl() );
496 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
497 return installAdjLsa(pnlsr, adjLsa);
498 }
akmhoque298385a2014-02-13 14:13:09 -0600499
akmhoque1fd8c1e2014-02-19 19:41:49 -0600500 bool
501 Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
502 {
503 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
504 adjLsdb.end(),
505 bind(adjLsaCompareByKey, _1, key));
506 if ( it != adjLsdb.end() )
507 {
508 (*it).removeNptEntriesForAdjLsa(pnlsr);
509 adjLsdb.erase(it);
510 return true;
511 }
512 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600513 }
akmhoque298385a2014-02-13 14:13:09 -0600514
akmhoque1fd8c1e2014-02-19 19:41:49 -0600515 bool
516 Lsdb::doesAdjLsaExist(string key)
517 {
518 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
519 adjLsdb.end(),
520 bind(adjLsaCompareByKey, _1, key));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600521 if( it == adjLsdb.end())
522 {
523 return false;
524 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600525 return true;
526 }
akmhoque298385a2014-02-13 14:13:09 -0600527
akmhoque1fd8c1e2014-02-19 19:41:49 -0600528 std::list<AdjLsa>&
529 Lsdb::getAdjLsdb()
530 {
531 return adjLsdb;
532 }
akmhoque298385a2014-02-13 14:13:09 -0600533
akmhoque1fd8c1e2014-02-19 19:41:49 -0600534 void
535 Lsdb::setLsaRefreshTime(int lrt)
536 {
537 lsaRefreshTime=lrt;
538 }
akmhoque298385a2014-02-13 14:13:09 -0600539
akmhoque1fd8c1e2014-02-19 19:41:49 -0600540 void
541 Lsdb::setThisRouterPrefix(string trp)
542 {
543 thisRouterPrefix=trp;
544 }
545
546 void
547 Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
548 {
549 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
550 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
551 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
552 if( chkNameLsa.second )
553 {
554 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
555 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
556 {
557 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
558 {
559 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
560 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
561 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
562 // publish routing update
563 }
564 else
565 {
566 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
567 removeNameLsa(pnlsr, lsaKey);
568 }
569 }
570 }
571 }
572
573 void
574 Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
575 {
576 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
577 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
578 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
579 if( chkAdjLsa.second )
580 {
581 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
582 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
583 {
584 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
585 {
586 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
587 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
588 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
589 // publish routing update
590 }
591 else
592 {
593 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
594 removeAdjLsa(pnlsr, lsaKey);
595 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600596 // schedule Routing table calculaiton
597 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
598 }
599 }
600 }
601
602 void
603 Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
604 {
605 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
606 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
607 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
608 if( chkCorLsa.second )
609 {
610 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
611 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
612 {
613 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
614 {
615 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
616 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
617 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
618 // publish routing update
619 }
620 else
621 {
622 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
623 removeCorLsa(pnlsr, lsaKey);
624 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600625 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
626 {
627 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
628 }
629 }
630 }
631 }
akmhoque298385a2014-02-13 14:13:09 -0600632
633
akmhoque1fd8c1e2014-02-19 19:41:49 -0600634 void
635 Lsdb::printAdjLsdb()
636 {
637 cout<<"---------------Adj LSDB-------------------"<<endl;
638 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
639 it!= adjLsdb.end() ; it++)
640 {
641 cout<< (*it) <<endl;
642 }
643 }
akmhoque298385a2014-02-13 14:13:09 -0600644
645//-----utility function -----
akmhoque1fd8c1e2014-02-19 19:41:49 -0600646 bool
647 Lsdb::doesLsaExist(string key, int lsType)
648 {
649 if ( lsType == 1)
650 {
651 return doesNameLsaExist(key);
652 }
653 else if ( lsType == 2)
654 {
655 return doesAdjLsaExist(key);
656 }
657 else if ( lsType == 3)
658 {
659 return doesCorLsaExist(key);
660 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600661 return false;
662 }
akmhoque298385a2014-02-13 14:13:09 -0600663
akmhoqueb1710aa2014-02-19 17:13:36 -0600664}//namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -0600665