blob: 9e3ffe7d4e6af62369780e6cefc6178470952a76 [file] [log] [blame]
akmhoquebd7c8e62014-02-01 14:57:40 -06001#include<string>
akmhoque62a8e402014-02-08 12:00:27 -06002#include<utility>
akmhoquebd7c8e62014-02-01 14:57:40 -06003#include "nlsr_lsdb.hpp"
4#include "nlsr.hpp"
5
6using namespace std;
7
akmhoquebd7c8e62014-02-01 14:57:40 -06008static bool
9nameLsaCompareByKey(NameLsa& nlsa1, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -060010 return nlsa1.getNameLsaKey()==key;
akmhoquebd7c8e62014-02-01 14:57:40 -060011}
12
13
14bool
15Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr)
16{
17 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
18 , 1
akmhoquedfa4a5b2014-02-03 20:12:29 -060019 , pnlsr.getSm().getNameLsaSeq()+1
akmhoquebd7c8e62014-02-01 14:57:40 -060020 , pnlsr.getConfParameter().getRouterDeadInterval()
21 , pnlsr.getNpl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -060022 pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1);
akmhoque62a8e402014-02-08 12:00:27 -060023 return installNameLsa(pnlsr,nameLsa);
akmhoquebd7c8e62014-02-01 14:57:40 -060024
25}
26
akmhoque62a8e402014-02-08 12:00:27 -060027std::pair<NameLsa&, bool>
akmhoquebd7c8e62014-02-01 14:57:40 -060028Lsdb::getNameLsa(string key)
29{
30 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
31 nameLsdb.end(),
32 bind(nameLsaCompareByKey, _1, key));
33
akmhoquefcf765d2014-02-01 23:46:17 -060034 if( it != nameLsdb.end())
35 {
akmhoque62a8e402014-02-08 12:00:27 -060036 return std::make_pair(boost::ref((*it)),true);
akmhoquebd7c8e62014-02-01 14:57:40 -060037 }
akmhoquedfa4a5b2014-02-03 20:12:29 -060038
akmhoque62a8e402014-02-08 12:00:27 -060039 NameLsa nlsa;
40 return std::make_pair(boost::ref(nlsa),false);
41
akmhoquebd7c8e62014-02-01 14:57:40 -060042}
43
44
akmhoquee77d8142014-02-11 11:59:57 -060045void
46Lsdb::scheduleNameLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
47{
48 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
49 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
50 this,boost::ref(pnlsr), key, seqNo));
51}
akmhoquebd7c8e62014-02-01 14:57:40 -060052
53bool
akmhoque62a8e402014-02-08 12:00:27 -060054Lsdb::installNameLsa(nlsr& pnlsr, NameLsa &nlsa)
akmhoquebd7c8e62014-02-01 14:57:40 -060055{
akmhoquee77d8142014-02-11 11:59:57 -060056 int timeToExpire=lsaRefreshTime;
akmhoque62a8e402014-02-08 12:00:27 -060057 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey());
58 if ( !chkNameLsa.second )
akmhoquebd7c8e62014-02-01 14:57:40 -060059 {
akmhoquebd7c8e62014-02-01 14:57:40 -060060 addNameLsa(nlsa);
akmhoquedfa4a5b2014-02-03 20:12:29 -060061 printNameLsdb();
akmhoque62a8e402014-02-08 12:00:27 -060062 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
63 {
64 pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr);
65 std::list<string> nameList=nlsa.getNpl().getNameList();
66 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end();it++)
67 {
akmhoque4768f892014-02-08 23:58:07 -060068 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
69 {
70 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
71 }
akmhoque62a8e402014-02-08 12:00:27 -060072 }
akmhoquee77d8142014-02-11 11:59:57 -060073 }
74
75 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
76 {
77 timeToExpire=nlsa.getLifeTime();
78 }
79 scheduleNameLsaExpiration( pnlsr, nlsa.getNameLsaKey(),
80 nlsa.getLsSeqNo(), timeToExpire);
akmhoquebd7c8e62014-02-01 14:57:40 -060081 }
82 else
83 {
akmhoque62a8e402014-02-08 12:00:27 -060084 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
85 {
86 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
87 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
88
89 chkNameLsa.first.getNpl().sortNpl();
90 nlsa.getNpl().sortNpl();
91
92 std::list<string> nameToAdd;
93 std::set_difference(nlsa.getNpl().getNameList().begin(),
94 nlsa.getNpl().getNameList().end(),
95 chkNameLsa.first.getNpl().getNameList().begin(),
96 chkNameLsa.first.getNpl().getNameList().end(),
97 std::inserter(nameToAdd, nameToAdd.begin()));
98 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
99 ++it)
100 {
101 chkNameLsa.first.addNameToLsa((*it));
102 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
103 {
akmhoque4768f892014-02-08 23:58:07 -0600104 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
105 {
106 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
107 }
akmhoque62a8e402014-02-08 12:00:27 -0600108 }
109 }
110
111 std::list<string> nameToRemove;
112 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
113 chkNameLsa.first.getNpl().getNameList().end(),
114 nlsa.getNpl().getNameList().begin(),
115 nlsa.getNpl().getNameList().end(),
116 std::inserter(nameToRemove, nameToRemove.begin()));
117 for(std::list<string>::iterator it=nameToRemove.begin();
118 it!=nameToRemove.end(); ++it)
119 {
120 chkNameLsa.first.removeNameFromLsa((*it));
121 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
122 {
akmhoque4768f892014-02-08 23:58:07 -0600123 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
124 {
125 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
126 }
akmhoque62a8e402014-02-08 12:00:27 -0600127 }
akmhoquee77d8142014-02-11 11:59:57 -0600128 }
129
130 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
131 {
132 timeToExpire=nlsa.getLifeTime();
133 }
134 scheduleNameLsaExpiration( pnlsr, nlsa.getNameLsaKey(),
135 nlsa.getLsSeqNo(), timeToExpire);
akmhoque62a8e402014-02-08 12:00:27 -0600136 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600137 }
138
139 return true;
140}
141
142bool
143Lsdb::addNameLsa(NameLsa &nlsa)
144{
145 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
akmhoquee77d8142014-02-11 11:59:57 -0600146 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoquebd7c8e62014-02-01 14:57:40 -0600147
akmhoquedfa4a5b2014-02-03 20:12:29 -0600148 if( it == nameLsdb.end())
149 {
akmhoquebd7c8e62014-02-01 14:57:40 -0600150 nameLsdb.push_back(nlsa);
151 return true;
152 }
153 return false;
154}
155
156bool
akmhoque4768f892014-02-08 23:58:07 -0600157Lsdb::removeNameLsa(nlsr& pnlsr, string& key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600158{
akmhoquefcf765d2014-02-01 23:46:17 -0600159 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
160 nameLsdb.end(),
161 bind(nameLsaCompareByKey, _1, key));
162 if ( it != nameLsdb.end() )
163 {
akmhoque4768f892014-02-08 23:58:07 -0600164 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
165 {
166 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
167 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
168 nit!=(*it).getNpl().getNameList().end(); ++nit)
169 {
170 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
171 {
172 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
173 }
174 }
175
176 }
akmhoquefcf765d2014-02-01 23:46:17 -0600177 nameLsdb.erase(it);
178 return true;
179 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600180 return false;
181}
akmhoquebd7c8e62014-02-01 14:57:40 -0600182
183bool
184Lsdb::doesNameLsaExist(string key)
185{
186 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
187 nameLsdb.end(),
188 bind(nameLsaCompareByKey, _1, key));
189
190 if( it == nameLsdb.end()){
191 return false;
192 }
193
194 return true;
195}
196
akmhoque3c6bd922014-02-01 17:10:17 -0600197void
198Lsdb::printNameLsdb()
199{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600200 cout<<"---------------Name LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600201 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
202 it!= nameLsdb.end() ; it++)
203 {
204 cout<< (*it) <<endl;
205 }
206}
207
208// Cor LSA and LSDB related Functions start here
akmhoque4768f892014-02-08 23:58:07 -0600209
210
akmhoque3c6bd922014-02-01 17:10:17 -0600211static bool
212corLsaCompareByKey(CorLsa& clsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600213 return clsa.getCorLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600214}
akmhoquebd7c8e62014-02-01 14:57:40 -0600215
216bool
akmhoque3c6bd922014-02-01 17:10:17 -0600217Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
218 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
akmhoquecd552472014-02-01 21:22:16 -0600219 , 3
akmhoquedfa4a5b2014-02-03 20:12:29 -0600220 , pnlsr.getSm().getCorLsaSeq()+1
akmhoque3c6bd922014-02-01 17:10:17 -0600221 , pnlsr.getConfParameter().getRouterDeadInterval()
222 , pnlsr.getConfParameter().getCorR()
223 , pnlsr.getConfParameter().getCorTheta() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600224 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600225 installCorLsa(pnlsr, corLsa);
akmhoque3c6bd922014-02-01 17:10:17 -0600226
akmhoquefcf765d2014-02-01 23:46:17 -0600227 return true;
akmhoque3c6bd922014-02-01 17:10:17 -0600228}
229
akmhoque62a8e402014-02-08 12:00:27 -0600230std::pair<CorLsa&, bool>
akmhoque3c6bd922014-02-01 17:10:17 -0600231Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600232{
akmhoque3c6bd922014-02-01 17:10:17 -0600233 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
234 corLsdb.end(),
235 bind(corLsaCompareByKey, _1, key));
236
237 if( it != corLsdb.end()){
akmhoque62a8e402014-02-08 12:00:27 -0600238 return std::make_pair(boost::ref((*it)), true);
akmhoque3c6bd922014-02-01 17:10:17 -0600239 }
akmhoque62a8e402014-02-08 12:00:27 -0600240
241 CorLsa clsa;
242 return std::make_pair(boost::ref(clsa),false);
akmhoque3c6bd922014-02-01 17:10:17 -0600243}
244
akmhoquee77d8142014-02-11 11:59:57 -0600245void
246Lsdb::scheduleCorLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
247{
248 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
249 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
250 this,boost::ref(pnlsr),key,seqNo));
251}
252
akmhoque3c6bd922014-02-01 17:10:17 -0600253bool
akmhoquef7c2c7c2014-02-06 11:32:43 -0600254Lsdb::installCorLsa(nlsr& pnlsr, CorLsa &clsa)
akmhoque3c6bd922014-02-01 17:10:17 -0600255{
akmhoquee77d8142014-02-11 11:59:57 -0600256 int timeToExpire=lsaRefreshTime;
akmhoque62a8e402014-02-08 12:00:27 -0600257 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
258 if ( !chkCorLsa.second )
akmhoque3c6bd922014-02-01 17:10:17 -0600259 {
akmhoque3c6bd922014-02-01 17:10:17 -0600260 addCorLsa(clsa);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600261 printCorLsdb(); //debugging purpose
akmhoque62a8e402014-02-08 12:00:27 -0600262 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
263 {
264 pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
265 }
akmhoquef7c2c7c2014-02-06 11:32:43 -0600266 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
267 {
akmhoquee77d8142014-02-11 11:59:57 -0600268 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600269 }
akmhoquee77d8142014-02-11 11:59:57 -0600270
271 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
272 {
273 timeToExpire=clsa.getLifeTime();
274 }
275 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
276 clsa.getLsSeqNo(), timeToExpire);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600277
akmhoque3c6bd922014-02-01 17:10:17 -0600278 }
279 else
280 {
akmhoque4768f892014-02-08 23:58:07 -0600281 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
282 {
283 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
284 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
285 if ( !chkCorLsa.first.isLsaContentEqual(clsa) )
286 {
287 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
288 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
289
290 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
291 {
akmhoquee77d8142014-02-11 11:59:57 -0600292 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque4768f892014-02-08 23:58:07 -0600293 }
294
295 }
akmhoquee77d8142014-02-11 11:59:57 -0600296
297 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
298 {
299 timeToExpire=clsa.getLifeTime();
300 }
301 scheduleCorLsaExpiration(pnlsr,clsa.getCorLsaKey(),
302 clsa.getLsSeqNo(), timeToExpire);
akmhoque4768f892014-02-08 23:58:07 -0600303 }
akmhoque3c6bd922014-02-01 17:10:17 -0600304
305 }
306
307 return true;
308}
309
310bool
311Lsdb::addCorLsa(CorLsa& clsa)
312{
313 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
314 corLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600315 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque3c6bd922014-02-01 17:10:17 -0600316
akmhoquedfa4a5b2014-02-03 20:12:29 -0600317 if( it == corLsdb.end())
318 {
akmhoque3c6bd922014-02-01 17:10:17 -0600319 corLsdb.push_back(clsa);
320 return true;
321 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600322 return false;
323}
324
325bool
akmhoque4768f892014-02-08 23:58:07 -0600326Lsdb::removeCorLsa(nlsr& pnlsr, string& key)
akmhoque3c6bd922014-02-01 17:10:17 -0600327{
akmhoquefcf765d2014-02-01 23:46:17 -0600328 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
329 corLsdb.end(),
330 bind(corLsaCompareByKey, _1, key));
331 if ( it != corLsdb.end() )
332 {
akmhoque4768f892014-02-08 23:58:07 -0600333 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
334 {
335 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
336 }
akmhoquefcf765d2014-02-01 23:46:17 -0600337 corLsdb.erase(it);
338 return true;
339 }
340 return false;
akmhoque3c6bd922014-02-01 17:10:17 -0600341
342}
343
344bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600345Lsdb::doesCorLsaExist(string key)
346{
akmhoque3c6bd922014-02-01 17:10:17 -0600347 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
348 corLsdb.end(),
349 bind(corLsaCompareByKey, _1, key));
350
351 if( it == corLsdb.end()){
352 return false;
353 }
354
355 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600356}
akmhoque3c6bd922014-02-01 17:10:17 -0600357
358void
359Lsdb::printCorLsdb() //debugging
360{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600361 cout<<"---------------Cor LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600362 for( std::list<CorLsa>::iterator it=corLsdb.begin();
363 it!= corLsdb.end() ; it++)
364 {
365 cout<< (*it) <<endl;
366 }
367}
368
369
370// Adj LSA and LSDB related function starts here
akmhoquee77d8142014-02-11 11:59:57 -0600371
akmhoque3c6bd922014-02-01 17:10:17 -0600372static bool
373adjLsaCompareByKey(AdjLsa& alsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600374 return alsa.getAdjLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600375}
376
377
akmhoquecd552472014-02-01 21:22:16 -0600378void
379Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr)
380{
381 cout<<"scheduledAdjLsaBuild Called"<<endl;
382 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque3c6bd922014-02-01 17:10:17 -0600383
akmhoquecd552472014-02-01 21:22:16 -0600384 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
385 {
386 int adjBuildCount=pnlsr.getAdjBuildCount();
387 if(adjBuildCount>0 )
388 {
389 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
390 {
391 buildAndInstallOwnAdjLsa(pnlsr);
392 }
393 else
394 {
akmhoquefcf765d2014-02-01 23:46:17 -0600395 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
akmhoque4768f892014-02-08 23:58:07 -0600396 removeAdjLsa(pnlsr,key);
akmhoquee77d8142014-02-11 11:59:57 -0600397 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoquecd552472014-02-01 21:22:16 -0600398 }
399 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
400 }
401 }
402 else
403 {
404 pnlsr.setIsBuildAdjLsaSheduled(1);
405 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
akmhoquee77d8142014-02-11 11:59:57 -0600406 pnlsr.getConfParameter().getInterestResendTime();
akmhoquecd552472014-02-01 21:22:16 -0600407 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
408 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
409 boost::ref(pnlsr)));
410 }
411
412}
413
414
415bool
416Lsdb::addAdjLsa(AdjLsa &alsa)
417{
418 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
419 adjLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600420 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoquecd552472014-02-01 21:22:16 -0600421
422 if( it == adjLsdb.end()){
423 adjLsdb.push_back(alsa);
424 return true;
425 }
426 return false;
427
428}
429
akmhoque62a8e402014-02-08 12:00:27 -0600430std::pair<AdjLsa& , bool>
akmhoquecd552472014-02-01 21:22:16 -0600431Lsdb::getAdjLsa(string key)
432{
433 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
434 adjLsdb.end(),
435 bind(adjLsaCompareByKey, _1, key));
436
437 if( it != adjLsdb.end()){
akmhoque62a8e402014-02-08 12:00:27 -0600438 return std::make_pair(boost::ref((*it)),true);
akmhoquecd552472014-02-01 21:22:16 -0600439 }
akmhoque62a8e402014-02-08 12:00:27 -0600440
441 AdjLsa alsa;
442 return std::make_pair(boost::ref(alsa),false);
akmhoquecd552472014-02-01 21:22:16 -0600443}
444
akmhoquee77d8142014-02-11 11:59:57 -0600445
446
447void
448Lsdb::scheduleAdjLsaExpiration(nlsr& pnlsr, string key, int seqNo, int expTime)
449{
450 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
451 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
452 this,boost::ref(pnlsr),key,seqNo));
453}
454
akmhoquecd552472014-02-01 21:22:16 -0600455bool
akmhoquedfa4a5b2014-02-03 20:12:29 -0600456Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa)
akmhoquecd552472014-02-01 21:22:16 -0600457{
akmhoquee77d8142014-02-11 11:59:57 -0600458 int timeToExpire=lsaRefreshTime;
akmhoque62a8e402014-02-08 12:00:27 -0600459 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
460 if ( !chkAdjLsa.second )
akmhoquecd552472014-02-01 21:22:16 -0600461 {
akmhoquecd552472014-02-01 21:22:16 -0600462 addAdjLsa(alsa);
akmhoque4768f892014-02-08 23:58:07 -0600463 alsa.addNptEntriesForAdjLsa(pnlsr);
akmhoquee77d8142014-02-11 11:59:57 -0600464 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
465
466 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
akmhoque79d355f2014-02-04 15:11:16 -0600467 {
akmhoquee77d8142014-02-11 11:59:57 -0600468 timeToExpire=alsa.getLifeTime();
akmhoque79d355f2014-02-04 15:11:16 -0600469 }
akmhoquee77d8142014-02-11 11:59:57 -0600470 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
471 alsa.getLsSeqNo(),timeToExpire);
472
akmhoquecd552472014-02-01 21:22:16 -0600473 }
474 else
475 {
akmhoque4768f892014-02-08 23:58:07 -0600476 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
477 {
478 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
479 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
480
481 if ( ! chkAdjLsa.first.isLsaContentEqual(alsa))
482 {
483 chkAdjLsa.first.getAdl().resetAdl();
484 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
akmhoquee77d8142014-02-11 11:59:57 -0600485 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque4768f892014-02-08 23:58:07 -0600486 }
akmhoquee77d8142014-02-11 11:59:57 -0600487
488 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
489 {
490 timeToExpire=alsa.getLifeTime();
491 }
492 scheduleAdjLsaExpiration(pnlsr,alsa.getAdjLsaKey(),
493 alsa.getLsSeqNo(),timeToExpire);
akmhoque4768f892014-02-08 23:58:07 -0600494 }
akmhoquecd552472014-02-01 21:22:16 -0600495
496 }
497
498 printAdjLsdb();
499
500 return true;
501}
502
503bool
504Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
505{
506 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
507 , 2
akmhoquedfa4a5b2014-02-03 20:12:29 -0600508 , pnlsr.getSm().getAdjLsaSeq()+1
akmhoquecd552472014-02-01 21:22:16 -0600509 , pnlsr.getConfParameter().getRouterDeadInterval()
510 , pnlsr.getAdl().getNumOfActiveNeighbor()
511 , pnlsr.getAdl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600512 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
513 return installAdjLsa(pnlsr, adjLsa);
akmhoquecd552472014-02-01 21:22:16 -0600514}
515
516bool
akmhoque4768f892014-02-08 23:58:07 -0600517Lsdb::removeAdjLsa(nlsr& pnlsr, string& key)
akmhoquecd552472014-02-01 21:22:16 -0600518{
akmhoquefcf765d2014-02-01 23:46:17 -0600519 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
520 adjLsdb.end(),
521 bind(adjLsaCompareByKey, _1, key));
522 if ( it != adjLsdb.end() )
523 {
akmhoque4768f892014-02-08 23:58:07 -0600524 (*it).removeNptEntriesForAdjLsa(pnlsr);
akmhoquefcf765d2014-02-01 23:46:17 -0600525 adjLsdb.erase(it);
526 return true;
527 }
528 return false;
akmhoquecd552472014-02-01 21:22:16 -0600529
530}
akmhoque3c6bd922014-02-01 17:10:17 -0600531
532bool
533Lsdb::doesAdjLsaExist(string key)
534{
535 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
536 adjLsdb.end(),
537 bind(adjLsaCompareByKey, _1, key));
538
539 if( it == adjLsdb.end()){
540 return false;
541 }
542
543 return true;
544}
545
akmhoque79d355f2014-02-04 15:11:16 -0600546std::list<AdjLsa>&
547Lsdb::getAdjLsdb()
548{
549 return adjLsdb;
550}
551
akmhoquee77d8142014-02-11 11:59:57 -0600552void
553Lsdb::setLsaRefreshTime(int lrt)
554{
555 lsaRefreshTime=lrt;
556}
557
558void
559Lsdb::setThisRouterPrefix(string trp)
560{
561 thisRouterPrefix=trp;
562}
563
564void
565Lsdb::exprireOrRefreshNameLsa(nlsr& pnlsr, string lsaKey, int seqNo)
566{
567 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
568 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
569 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
570 if( chkNameLsa.second )
571 {
572 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
573 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
574 {
575 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
576 {
577 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
578 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
579 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
580 // publish routing update
581 }
582 else
583 {
584 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
585 removeNameLsa(pnlsr, lsaKey);
586 }
587 }
588 }
589}
590
591void
592Lsdb::exprireOrRefreshAdjLsa(nlsr& pnlsr, string lsaKey, int seqNo)
593{
594 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
595 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
596 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
597 if( chkAdjLsa.second )
598 {
599 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
600 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
601 {
602 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
603 {
604 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
605 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
606 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
607 // publish routing update
608 }
609 else
610 {
611 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
612 removeAdjLsa(pnlsr, lsaKey);
613 }
614
615 // schedule Routing table calculaiton
616 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
617 }
618 }
619}
620
621void
622Lsdb::exprireOrRefreshCorLsa(nlsr& pnlsr, string lsaKey, int seqNo)
623{
624 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
625 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<endl;
626 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
627 if( chkCorLsa.second )
628 {
629 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
630 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
631 {
632 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
633 {
634 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
635 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
636 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
637 // publish routing update
638 }
639 else
640 {
641 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
642 removeCorLsa(pnlsr, lsaKey);
643 }
644
645 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
646 {
647 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
648 }
649 }
650 }
651}
652
akmhoque4768f892014-02-08 23:58:07 -0600653
akmhoquecd552472014-02-01 21:22:16 -0600654void
655Lsdb::printAdjLsdb()
656{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600657 cout<<"---------------Adj LSDB-------------------"<<endl;
akmhoquecd552472014-02-01 21:22:16 -0600658 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
659 it!= adjLsdb.end() ; it++)
660 {
661 cout<< (*it) <<endl;
662 }
663}
akmhoque3c6bd922014-02-01 17:10:17 -0600664
akmhoque62a8e402014-02-08 12:00:27 -0600665//-----utility function -----
666bool
667Lsdb::doesLsaExist(string key, int lsType)
668{
669 if ( lsType == 1)
670 {
671 return doesNameLsaExist(key);
672 }
673 else if ( lsType == 2)
674 {
675 return doesAdjLsaExist(key);
676 }
677 else if ( lsType == 3)
678 {
679 return doesCorLsaExist(key);
680 }
681
682 return false;
683}
684
685