blob: 855f1c710a2d360ccc69d87edcf359d08d8ad38f [file] [log] [blame]
akmhoquebd7c8e62014-02-01 14:57:40 -06001#include<string>
2#include "nlsr_lsdb.hpp"
3#include "nlsr.hpp"
4
5using namespace std;
6
7
8
9
10
11bool
12Lsdb::doesLsaExist(string key, int lsType)
13{
14 if ( lsType == 1)
15 {
16 return doesNameLsaExist(key);
17 }
18 else if ( lsType == 2)
19 {
20 return doesAdjLsaExist(key);
21 }
22 else if ( lsType == 3)
23 {
24 return doesCorLsaExist(key);
25 }
26
27 return false;
28
29}
30
akmhoque3c6bd922014-02-01 17:10:17 -060031//Name LSA and LSDB related functions start here
32
akmhoquebd7c8e62014-02-01 14:57:40 -060033
34static bool
35nameLsaCompareByKey(NameLsa& nlsa1, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -060036 return nlsa1.getNameLsaKey()==key;
akmhoquebd7c8e62014-02-01 14:57:40 -060037}
38
39
40bool
41Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr)
42{
43 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
44 , 1
akmhoquedfa4a5b2014-02-03 20:12:29 -060045 , pnlsr.getSm().getNameLsaSeq()+1
akmhoquebd7c8e62014-02-01 14:57:40 -060046 , pnlsr.getConfParameter().getRouterDeadInterval()
47 , pnlsr.getNpl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -060048 pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1);
akmhoquebd7c8e62014-02-01 14:57:40 -060049 return installNameLsa(nameLsa);
50
51}
52
53NameLsa&
54Lsdb::getNameLsa(string key)
55{
56 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
57 nameLsdb.end(),
58 bind(nameLsaCompareByKey, _1, key));
59
akmhoquefcf765d2014-02-01 23:46:17 -060060 if( it != nameLsdb.end())
61 {
akmhoquebd7c8e62014-02-01 14:57:40 -060062 return (*it);
63 }
akmhoquedfa4a5b2014-02-03 20:12:29 -060064
akmhoquebd7c8e62014-02-01 14:57:40 -060065}
66
67
68
69bool
70Lsdb::installNameLsa(NameLsa &nlsa)
71{
akmhoque3fdf7612014-02-04 21:18:23 -060072 bool doesLsaExist_ = doesNameLsaExist(nlsa.getNameLsaKey());
akmhoquebd7c8e62014-02-01 14:57:40 -060073 if ( !doesLsaExist_ )
74 {
75 // add name LSA
76 addNameLsa(nlsa);
77 // update NPT and FIB
akmhoquefcf765d2014-02-01 23:46:17 -060078 // if its not own LSA
akmhoquedfa4a5b2014-02-03 20:12:29 -060079 printNameLsdb();
akmhoquebd7c8e62014-02-01 14:57:40 -060080 }
81 else
82 {
83 // check for newer name LSA
akmhoque3fdf7612014-02-04 21:18:23 -060084 NameLsa oldNameLsa=getNameLsa(nlsa.getNameLsaKey());
akmhoquebd7c8e62014-02-01 14:57:40 -060085 // Discard or Update Name lsa, NPT, FIB
akmhoquefcf765d2014-02-01 23:46:17 -060086 // if its not own LSA
akmhoquebd7c8e62014-02-01 14:57:40 -060087 }
88
89 return true;
90}
91
92bool
93Lsdb::addNameLsa(NameLsa &nlsa)
94{
95 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
96 nameLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -060097 bind(nameLsaCompareByKey, _1, nlsa.getNameLsaKey()));
akmhoquebd7c8e62014-02-01 14:57:40 -060098
akmhoquedfa4a5b2014-02-03 20:12:29 -060099 if( it == nameLsdb.end())
100 {
akmhoquebd7c8e62014-02-01 14:57:40 -0600101 nameLsdb.push_back(nlsa);
102 return true;
103 }
104 return false;
105}
106
107bool
108Lsdb::removeNameLsa(string& key)
109{
akmhoquefcf765d2014-02-01 23:46:17 -0600110 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
111 nameLsdb.end(),
112 bind(nameLsaCompareByKey, _1, key));
113 if ( it != nameLsdb.end() )
114 {
115 nameLsdb.erase(it);
116 return true;
117 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600118 return false;
119}
akmhoquebd7c8e62014-02-01 14:57:40 -0600120
121bool
122Lsdb::doesNameLsaExist(string key)
123{
124 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
125 nameLsdb.end(),
126 bind(nameLsaCompareByKey, _1, key));
127
128 if( it == nameLsdb.end()){
129 return false;
130 }
131
132 return true;
133}
134
akmhoque3c6bd922014-02-01 17:10:17 -0600135void
136Lsdb::printNameLsdb()
137{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600138 cout<<"---------------Name LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600139 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
140 it!= nameLsdb.end() ; it++)
141 {
142 cout<< (*it) <<endl;
143 }
144}
145
146// Cor LSA and LSDB related Functions start here
akmhoquedfa4a5b2014-02-03 20:12:29 -0600147/*
akmhoque3c6bd922014-02-01 17:10:17 -0600148static bool
149corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
150 return clsa1.getLsaKey()==clsa1.getLsaKey();
151}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600152*/
akmhoque3c6bd922014-02-01 17:10:17 -0600153static bool
154corLsaCompareByKey(CorLsa& clsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600155 return clsa.getCorLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600156}
akmhoquebd7c8e62014-02-01 14:57:40 -0600157
158bool
akmhoque3c6bd922014-02-01 17:10:17 -0600159Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
160 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
akmhoquecd552472014-02-01 21:22:16 -0600161 , 3
akmhoquedfa4a5b2014-02-03 20:12:29 -0600162 , pnlsr.getSm().getCorLsaSeq()+1
akmhoque3c6bd922014-02-01 17:10:17 -0600163 , pnlsr.getConfParameter().getRouterDeadInterval()
164 , pnlsr.getConfParameter().getCorR()
165 , pnlsr.getConfParameter().getCorTheta() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600166 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600167 installCorLsa(pnlsr, corLsa);
akmhoque3c6bd922014-02-01 17:10:17 -0600168
akmhoquefcf765d2014-02-01 23:46:17 -0600169 return true;
akmhoque3c6bd922014-02-01 17:10:17 -0600170}
171
172CorLsa&
173Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600174{
akmhoque3c6bd922014-02-01 17:10:17 -0600175 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
176 corLsdb.end(),
177 bind(corLsaCompareByKey, _1, key));
178
179 if( it != corLsdb.end()){
180 return (*it);
181 }
182}
183
184bool
akmhoquef7c2c7c2014-02-06 11:32:43 -0600185Lsdb::installCorLsa(nlsr& pnlsr, CorLsa &clsa)
akmhoque3c6bd922014-02-01 17:10:17 -0600186{
akmhoque3fdf7612014-02-04 21:18:23 -0600187 bool doesLsaExist_ = doesCorLsaExist(clsa.getCorLsaKey());
akmhoque3c6bd922014-02-01 17:10:17 -0600188 if ( !doesLsaExist_ )
189 {
190 // add cor LSA
191 addCorLsa(clsa);
akmhoquef7c2c7c2014-02-06 11:32:43 -0600192 printCorLsdb(); //debugging purpose
akmhoquefcf765d2014-02-01 23:46:17 -0600193 //schedule routing table calculation only if
194 //hyperbolic calculation is scheduled
akmhoquef7c2c7c2014-02-06 11:32:43 -0600195 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
196 {
197 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
198 {
199 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
200 ndn::bind(&RoutingTable::calculate,
201 &pnlsr.getRoutingTable(),boost::ref(pnlsr)));
202 pnlsr.setIsRouteCalculationScheduled(1);
203 }
204 }
205
akmhoque3c6bd922014-02-01 17:10:17 -0600206 }
207 else
208 {
209 // check for newer cor LSA
akmhoque3fdf7612014-02-04 21:18:23 -0600210 CorLsa oldCorLsa=getCorLsa(clsa.getCorLsaKey());
akmhoque3c6bd922014-02-01 17:10:17 -0600211
212 }
213
214 return true;
215}
216
217bool
218Lsdb::addCorLsa(CorLsa& clsa)
219{
220 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
221 corLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600222 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque3c6bd922014-02-01 17:10:17 -0600223
akmhoquedfa4a5b2014-02-03 20:12:29 -0600224 if( it == corLsdb.end())
225 {
akmhoque3c6bd922014-02-01 17:10:17 -0600226 corLsdb.push_back(clsa);
227 return true;
228 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600229 return false;
230}
231
232bool
akmhoque3c6bd922014-02-01 17:10:17 -0600233Lsdb::removeCorLsa(string& key)
234{
akmhoquefcf765d2014-02-01 23:46:17 -0600235 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
236 corLsdb.end(),
237 bind(corLsaCompareByKey, _1, key));
238 if ( it != corLsdb.end() )
239 {
240 corLsdb.erase(it);
241 return true;
242 }
243 return false;
akmhoque3c6bd922014-02-01 17:10:17 -0600244
245}
246
247bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600248Lsdb::doesCorLsaExist(string key)
249{
akmhoque3c6bd922014-02-01 17:10:17 -0600250 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
251 corLsdb.end(),
252 bind(corLsaCompareByKey, _1, key));
253
254 if( it == corLsdb.end()){
255 return false;
256 }
257
258 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600259}
akmhoque3c6bd922014-02-01 17:10:17 -0600260
261void
262Lsdb::printCorLsdb() //debugging
263{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600264 cout<<"---------------Cor LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600265 for( std::list<CorLsa>::iterator it=corLsdb.begin();
266 it!= corLsdb.end() ; it++)
267 {
268 cout<< (*it) <<endl;
269 }
270}
271
272
273// Adj LSA and LSDB related function starts here
akmhoquedfa4a5b2014-02-03 20:12:29 -0600274/*
akmhoque3c6bd922014-02-01 17:10:17 -0600275static bool
276adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
277 return alsa1.getLsaKey()==alsa1.getLsaKey();
278}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600279*/
akmhoque3c6bd922014-02-01 17:10:17 -0600280static bool
281adjLsaCompareByKey(AdjLsa& alsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600282 return alsa.getAdjLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600283}
284
285
akmhoquecd552472014-02-01 21:22:16 -0600286void
287Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr)
288{
289 cout<<"scheduledAdjLsaBuild Called"<<endl;
290 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque3c6bd922014-02-01 17:10:17 -0600291
akmhoquecd552472014-02-01 21:22:16 -0600292 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
293 {
294 int adjBuildCount=pnlsr.getAdjBuildCount();
295 if(adjBuildCount>0 )
296 {
297 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
298 {
299 buildAndInstallOwnAdjLsa(pnlsr);
300 }
301 else
302 {
303 //remove if there is any adj lsa in LSDB
akmhoquefcf765d2014-02-01 23:46:17 -0600304 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
305 removeAdjLsa(key);
306 // Remove alll fib entries as per NPT
akmhoquecd552472014-02-01 21:22:16 -0600307 }
308 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
309 }
310 }
311 else
312 {
313 pnlsr.setIsBuildAdjLsaSheduled(1);
314 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
315 pnlsr.getConfParameter().getInterestRetryNumber();
316 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
317 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
318 boost::ref(pnlsr)));
319 }
320
321}
322
323
324bool
325Lsdb::addAdjLsa(AdjLsa &alsa)
326{
327 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
328 adjLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600329 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoquecd552472014-02-01 21:22:16 -0600330
331 if( it == adjLsdb.end()){
332 adjLsdb.push_back(alsa);
333 return true;
334 }
335 return false;
336
337}
338
339AdjLsa&
340Lsdb::getAdjLsa(string key)
341{
342 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
343 adjLsdb.end(),
344 bind(adjLsaCompareByKey, _1, key));
345
346 if( it != adjLsdb.end()){
347 return (*it);
348 }
akmhoquecd552472014-02-01 21:22:16 -0600349}
350
351bool
akmhoquedfa4a5b2014-02-03 20:12:29 -0600352Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa)
akmhoquecd552472014-02-01 21:22:16 -0600353{
akmhoque3fdf7612014-02-04 21:18:23 -0600354 bool doesLsaExist_ = doesAdjLsaExist(alsa.getAdjLsaKey());
akmhoquecd552472014-02-01 21:22:16 -0600355 if ( !doesLsaExist_ )
356 {
357 // add Adj LSA
358 addAdjLsa(alsa);
359 // schedule routing table calculation
akmhoque79d355f2014-02-04 15:11:16 -0600360 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
361 {
362 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
363 ndn::bind(&RoutingTable::calculate,
364 &pnlsr.getRoutingTable(),boost::ref(pnlsr)));
365 pnlsr.setIsRouteCalculationScheduled(1);
366 }
akmhoquecd552472014-02-01 21:22:16 -0600367 }
368 else
369 {
370 // check for newer name LSA
akmhoque3fdf7612014-02-04 21:18:23 -0600371 AdjLsa oldAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
akmhoquecd552472014-02-01 21:22:16 -0600372
373 }
374
375 printAdjLsdb();
376
377 return true;
378}
379
380bool
381Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
382{
383 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
384 , 2
akmhoquedfa4a5b2014-02-03 20:12:29 -0600385 , pnlsr.getSm().getAdjLsaSeq()+1
akmhoquecd552472014-02-01 21:22:16 -0600386 , pnlsr.getConfParameter().getRouterDeadInterval()
387 , pnlsr.getAdl().getNumOfActiveNeighbor()
388 , pnlsr.getAdl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600389 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
390 return installAdjLsa(pnlsr, adjLsa);
akmhoquecd552472014-02-01 21:22:16 -0600391}
392
393bool
394Lsdb::removeAdjLsa(string& key)
395{
akmhoquefcf765d2014-02-01 23:46:17 -0600396 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
397 adjLsdb.end(),
398 bind(adjLsaCompareByKey, _1, key));
399 if ( it != adjLsdb.end() )
400 {
401 adjLsdb.erase(it);
402 return true;
403 }
404 return false;
akmhoquecd552472014-02-01 21:22:16 -0600405
406}
akmhoque3c6bd922014-02-01 17:10:17 -0600407
408bool
409Lsdb::doesAdjLsaExist(string key)
410{
411 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
412 adjLsdb.end(),
413 bind(adjLsaCompareByKey, _1, key));
414
415 if( it == adjLsdb.end()){
416 return false;
417 }
418
419 return true;
420}
421
akmhoque79d355f2014-02-04 15:11:16 -0600422std::list<AdjLsa>&
423Lsdb::getAdjLsdb()
424{
425 return adjLsdb;
426}
427
akmhoquecd552472014-02-01 21:22:16 -0600428void
429Lsdb::printAdjLsdb()
430{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600431 cout<<"---------------Adj LSDB-------------------"<<endl;
akmhoquecd552472014-02-01 21:22:16 -0600432 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
433 it!= adjLsdb.end() ; it++)
434 {
435 cout<< (*it) <<endl;
436 }
437}
akmhoque3c6bd922014-02-01 17:10:17 -0600438