blob: 3177fc8e10ec67d3de4c589fa826c945f6c06419 [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
45
46bool
akmhoque62a8e402014-02-08 12:00:27 -060047Lsdb::installNameLsa(nlsr& pnlsr, NameLsa &nlsa)
akmhoquebd7c8e62014-02-01 14:57:40 -060048{
akmhoque62a8e402014-02-08 12:00:27 -060049 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getNameLsaKey());
50 if ( !chkNameLsa.second )
akmhoquebd7c8e62014-02-01 14:57:40 -060051 {
akmhoquebd7c8e62014-02-01 14:57:40 -060052 addNameLsa(nlsa);
akmhoquedfa4a5b2014-02-03 20:12:29 -060053 printNameLsdb();
akmhoque62a8e402014-02-08 12:00:27 -060054 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
55 {
56 pnlsr.getNpt().addNpte(nlsa.getOrigRouter(),nlsa.getOrigRouter(),pnlsr);
57 std::list<string> nameList=nlsa.getNpl().getNameList();
58 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end();it++)
59 {
60 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
61 }
62 }
akmhoquebd7c8e62014-02-01 14:57:40 -060063 }
64 else
65 {
akmhoque62a8e402014-02-08 12:00:27 -060066 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
67 {
68 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
69 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
70
71 chkNameLsa.first.getNpl().sortNpl();
72 nlsa.getNpl().sortNpl();
73
74 std::list<string> nameToAdd;
75 std::set_difference(nlsa.getNpl().getNameList().begin(),
76 nlsa.getNpl().getNameList().end(),
77 chkNameLsa.first.getNpl().getNameList().begin(),
78 chkNameLsa.first.getNpl().getNameList().end(),
79 std::inserter(nameToAdd, nameToAdd.begin()));
80 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
81 ++it)
82 {
83 chkNameLsa.first.addNameToLsa((*it));
84 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
85 {
86 pnlsr.getNpt().addNpte((*it),nlsa.getOrigRouter(),pnlsr);
87 }
88 }
89
90 std::list<string> nameToRemove;
91 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
92 chkNameLsa.first.getNpl().getNameList().end(),
93 nlsa.getNpl().getNameList().begin(),
94 nlsa.getNpl().getNameList().end(),
95 std::inserter(nameToRemove, nameToRemove.begin()));
96 for(std::list<string>::iterator it=nameToRemove.begin();
97 it!=nameToRemove.end(); ++it)
98 {
99 chkNameLsa.first.removeNameFromLsa((*it));
100 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
101 {
102 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
103 }
104 }
105
106 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600107 }
108
109 return true;
110}
111
112bool
113Lsdb::addNameLsa(NameLsa &nlsa)
114{
115 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
116 nameLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600117 bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoquebd7c8e62014-02-01 14:57:40 -0600118
akmhoquedfa4a5b2014-02-03 20:12:29 -0600119 if( it == nameLsdb.end())
120 {
akmhoquebd7c8e62014-02-01 14:57:40 -0600121 nameLsdb.push_back(nlsa);
122 return true;
123 }
124 return false;
125}
126
127bool
128Lsdb::removeNameLsa(string& key)
129{
akmhoquefcf765d2014-02-01 23:46:17 -0600130 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
131 nameLsdb.end(),
132 bind(nameLsaCompareByKey, _1, key));
133 if ( it != nameLsdb.end() )
134 {
135 nameLsdb.erase(it);
136 return true;
137 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600138 return false;
139}
akmhoquebd7c8e62014-02-01 14:57:40 -0600140
141bool
142Lsdb::doesNameLsaExist(string key)
143{
144 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
145 nameLsdb.end(),
146 bind(nameLsaCompareByKey, _1, key));
147
148 if( it == nameLsdb.end()){
149 return false;
150 }
151
152 return true;
153}
154
akmhoque3c6bd922014-02-01 17:10:17 -0600155void
156Lsdb::printNameLsdb()
157{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600158 cout<<"---------------Name LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600159 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
160 it!= nameLsdb.end() ; it++)
161 {
162 cout<< (*it) <<endl;
163 }
164}
165
166// Cor LSA and LSDB related Functions start here
akmhoquedfa4a5b2014-02-03 20:12:29 -0600167/*
akmhoque3c6bd922014-02-01 17:10:17 -0600168static bool
169corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
170 return clsa1.getLsaKey()==clsa1.getLsaKey();
171}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600172*/
akmhoque3c6bd922014-02-01 17:10:17 -0600173static bool
174corLsaCompareByKey(CorLsa& clsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600175 return clsa.getCorLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600176}
akmhoquebd7c8e62014-02-01 14:57:40 -0600177
178bool
akmhoque3c6bd922014-02-01 17:10:17 -0600179Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
180 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
akmhoquecd552472014-02-01 21:22:16 -0600181 , 3
akmhoquedfa4a5b2014-02-03 20:12:29 -0600182 , pnlsr.getSm().getCorLsaSeq()+1
akmhoque3c6bd922014-02-01 17:10:17 -0600183 , pnlsr.getConfParameter().getRouterDeadInterval()
184 , pnlsr.getConfParameter().getCorR()
185 , pnlsr.getConfParameter().getCorTheta() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600186 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600187 installCorLsa(pnlsr, corLsa);
akmhoque3c6bd922014-02-01 17:10:17 -0600188
akmhoquefcf765d2014-02-01 23:46:17 -0600189 return true;
akmhoque3c6bd922014-02-01 17:10:17 -0600190}
191
akmhoque62a8e402014-02-08 12:00:27 -0600192std::pair<CorLsa&, bool>
akmhoque3c6bd922014-02-01 17:10:17 -0600193Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600194{
akmhoque3c6bd922014-02-01 17:10:17 -0600195 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
196 corLsdb.end(),
197 bind(corLsaCompareByKey, _1, key));
198
199 if( it != corLsdb.end()){
akmhoque62a8e402014-02-08 12:00:27 -0600200 return std::make_pair(boost::ref((*it)), true);
akmhoque3c6bd922014-02-01 17:10:17 -0600201 }
akmhoque62a8e402014-02-08 12:00:27 -0600202
203 CorLsa clsa;
204 return std::make_pair(boost::ref(clsa),false);
akmhoque3c6bd922014-02-01 17:10:17 -0600205}
206
207bool
akmhoquef7c2c7c2014-02-06 11:32:43 -0600208Lsdb::installCorLsa(nlsr& pnlsr, CorLsa &clsa)
akmhoque3c6bd922014-02-01 17:10:17 -0600209{
akmhoque62a8e402014-02-08 12:00:27 -0600210 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getCorLsaKey());
211 if ( !chkCorLsa.second )
akmhoque3c6bd922014-02-01 17:10:17 -0600212 {
213 // add cor LSA
214 addCorLsa(clsa);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600215 printCorLsdb(); //debugging purpose
akmhoque62a8e402014-02-08 12:00:27 -0600216 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
217 {
218 pnlsr.getNpt().addNpte(clsa.getOrigRouter(),clsa.getOrigRouter(),pnlsr);
219 }
akmhoquefcf765d2014-02-01 23:46:17 -0600220 //schedule routing table calculation only if
221 //hyperbolic calculation is scheduled
akmhoquef7c2c7c2014-02-06 11:32:43 -0600222 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
223 {
224 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
225 {
226 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
227 ndn::bind(&RoutingTable::calculate,
228 &pnlsr.getRoutingTable(),boost::ref(pnlsr)));
229 pnlsr.setIsRouteCalculationScheduled(1);
230 }
231 }
232
akmhoque3c6bd922014-02-01 17:10:17 -0600233 }
234 else
235 {
236 // check for newer cor LSA
akmhoque62a8e402014-02-08 12:00:27 -0600237 //CorLsa oldCorLsa=getCorLsa(clsa.getCorLsaKey());
akmhoque3c6bd922014-02-01 17:10:17 -0600238
239 }
240
241 return true;
242}
243
244bool
245Lsdb::addCorLsa(CorLsa& clsa)
246{
247 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
248 corLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600249 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque3c6bd922014-02-01 17:10:17 -0600250
akmhoquedfa4a5b2014-02-03 20:12:29 -0600251 if( it == corLsdb.end())
252 {
akmhoque3c6bd922014-02-01 17:10:17 -0600253 corLsdb.push_back(clsa);
254 return true;
255 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600256 return false;
257}
258
259bool
akmhoque3c6bd922014-02-01 17:10:17 -0600260Lsdb::removeCorLsa(string& key)
261{
akmhoquefcf765d2014-02-01 23:46:17 -0600262 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
263 corLsdb.end(),
264 bind(corLsaCompareByKey, _1, key));
265 if ( it != corLsdb.end() )
266 {
267 corLsdb.erase(it);
268 return true;
269 }
270 return false;
akmhoque3c6bd922014-02-01 17:10:17 -0600271
272}
273
274bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600275Lsdb::doesCorLsaExist(string key)
276{
akmhoque3c6bd922014-02-01 17:10:17 -0600277 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
278 corLsdb.end(),
279 bind(corLsaCompareByKey, _1, key));
280
281 if( it == corLsdb.end()){
282 return false;
283 }
284
285 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600286}
akmhoque3c6bd922014-02-01 17:10:17 -0600287
288void
289Lsdb::printCorLsdb() //debugging
290{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600291 cout<<"---------------Cor LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600292 for( std::list<CorLsa>::iterator it=corLsdb.begin();
293 it!= corLsdb.end() ; it++)
294 {
295 cout<< (*it) <<endl;
296 }
297}
298
299
300// Adj LSA and LSDB related function starts here
akmhoquedfa4a5b2014-02-03 20:12:29 -0600301/*
akmhoque3c6bd922014-02-01 17:10:17 -0600302static bool
303adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
304 return alsa1.getLsaKey()==alsa1.getLsaKey();
305}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600306*/
akmhoque3c6bd922014-02-01 17:10:17 -0600307static bool
308adjLsaCompareByKey(AdjLsa& alsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600309 return alsa.getAdjLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600310}
311
312
akmhoquecd552472014-02-01 21:22:16 -0600313void
314Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr)
315{
316 cout<<"scheduledAdjLsaBuild Called"<<endl;
317 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque3c6bd922014-02-01 17:10:17 -0600318
akmhoquecd552472014-02-01 21:22:16 -0600319 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
320 {
321 int adjBuildCount=pnlsr.getAdjBuildCount();
322 if(adjBuildCount>0 )
323 {
324 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
325 {
326 buildAndInstallOwnAdjLsa(pnlsr);
327 }
328 else
329 {
330 //remove if there is any adj lsa in LSDB
akmhoquefcf765d2014-02-01 23:46:17 -0600331 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
332 removeAdjLsa(key);
333 // Remove alll fib entries as per NPT
akmhoquecd552472014-02-01 21:22:16 -0600334 }
335 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
336 }
337 }
338 else
339 {
340 pnlsr.setIsBuildAdjLsaSheduled(1);
341 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
342 pnlsr.getConfParameter().getInterestRetryNumber();
343 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
344 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
345 boost::ref(pnlsr)));
346 }
347
348}
349
350
351bool
352Lsdb::addAdjLsa(AdjLsa &alsa)
353{
354 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
355 adjLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600356 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoquecd552472014-02-01 21:22:16 -0600357
358 if( it == adjLsdb.end()){
359 adjLsdb.push_back(alsa);
360 return true;
361 }
362 return false;
363
364}
365
akmhoque62a8e402014-02-08 12:00:27 -0600366std::pair<AdjLsa& , bool>
akmhoquecd552472014-02-01 21:22:16 -0600367Lsdb::getAdjLsa(string key)
368{
369 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
370 adjLsdb.end(),
371 bind(adjLsaCompareByKey, _1, key));
372
373 if( it != adjLsdb.end()){
akmhoque62a8e402014-02-08 12:00:27 -0600374 return std::make_pair(boost::ref((*it)),true);
akmhoquecd552472014-02-01 21:22:16 -0600375 }
akmhoque62a8e402014-02-08 12:00:27 -0600376
377 AdjLsa alsa;
378 return std::make_pair(boost::ref(alsa),false);
akmhoquecd552472014-02-01 21:22:16 -0600379}
380
381bool
akmhoquedfa4a5b2014-02-03 20:12:29 -0600382Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa)
akmhoquecd552472014-02-01 21:22:16 -0600383{
akmhoque62a8e402014-02-08 12:00:27 -0600384 //bool doesLsaExist_ = doesAdjLsaExist(alsa.getAdjLsaKey());
385 //if ( !doesLsaExist_ )
386 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
387 if ( !chkAdjLsa.second )
akmhoquecd552472014-02-01 21:22:16 -0600388 {
389 // add Adj LSA
390 addAdjLsa(alsa);
akmhoque62a8e402014-02-08 12:00:27 -0600391 // adding a NPT entry for router itself
392 if ( alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
393 {
394 pnlsr.getNpt().addNpte(alsa.getOrigRouter(),alsa.getOrigRouter(),pnlsr);
395 }
akmhoquecd552472014-02-01 21:22:16 -0600396 // schedule routing table calculation
akmhoque79d355f2014-02-04 15:11:16 -0600397 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
398 {
399 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
400 ndn::bind(&RoutingTable::calculate,
401 &pnlsr.getRoutingTable(),boost::ref(pnlsr)));
402 pnlsr.setIsRouteCalculationScheduled(1);
403 }
akmhoquecd552472014-02-01 21:22:16 -0600404 }
405 else
406 {
407 // check for newer name LSA
akmhoque62a8e402014-02-08 12:00:27 -0600408 //AdjLsa oldAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
akmhoquecd552472014-02-01 21:22:16 -0600409
410 }
411
412 printAdjLsdb();
413
414 return true;
415}
416
417bool
418Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
419{
420 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
421 , 2
akmhoquedfa4a5b2014-02-03 20:12:29 -0600422 , pnlsr.getSm().getAdjLsaSeq()+1
akmhoquecd552472014-02-01 21:22:16 -0600423 , pnlsr.getConfParameter().getRouterDeadInterval()
424 , pnlsr.getAdl().getNumOfActiveNeighbor()
425 , pnlsr.getAdl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600426 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
427 return installAdjLsa(pnlsr, adjLsa);
akmhoquecd552472014-02-01 21:22:16 -0600428}
429
430bool
431Lsdb::removeAdjLsa(string& key)
432{
akmhoquefcf765d2014-02-01 23:46:17 -0600433 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
434 adjLsdb.end(),
435 bind(adjLsaCompareByKey, _1, key));
436 if ( it != adjLsdb.end() )
437 {
438 adjLsdb.erase(it);
439 return true;
440 }
441 return false;
akmhoquecd552472014-02-01 21:22:16 -0600442
443}
akmhoque3c6bd922014-02-01 17:10:17 -0600444
445bool
446Lsdb::doesAdjLsaExist(string key)
447{
448 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
449 adjLsdb.end(),
450 bind(adjLsaCompareByKey, _1, key));
451
452 if( it == adjLsdb.end()){
453 return false;
454 }
455
456 return true;
457}
458
akmhoque79d355f2014-02-04 15:11:16 -0600459std::list<AdjLsa>&
460Lsdb::getAdjLsdb()
461{
462 return adjLsdb;
463}
464
akmhoquecd552472014-02-01 21:22:16 -0600465void
466Lsdb::printAdjLsdb()
467{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600468 cout<<"---------------Adj LSDB-------------------"<<endl;
akmhoquecd552472014-02-01 21:22:16 -0600469 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
470 it!= adjLsdb.end() ; it++)
471 {
472 cout<< (*it) <<endl;
473 }
474}
akmhoque3c6bd922014-02-01 17:10:17 -0600475
akmhoque62a8e402014-02-08 12:00:27 -0600476//-----utility function -----
477bool
478Lsdb::doesLsaExist(string key, int lsType)
479{
480 if ( lsType == 1)
481 {
482 return doesNameLsaExist(key);
483 }
484 else if ( lsType == 2)
485 {
486 return doesAdjLsaExist(key);
487 }
488 else if ( lsType == 3)
489 {
490 return doesCorLsaExist(key);
491 }
492
493 return false;
494}
495
496