blob: 7b662e26ba38d5e438fdaffca9a9220468532243 [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<string>
2#include<utility>
3#include "nlsr_lsdb.hpp"
4#include "nlsr.hpp"
akmhoque05d5fcf2014-04-15 14:58:45 -05005#include "utility/nlsr_logger.hpp"
6
7#define THIS_FILE "nlsr_lsdb.cpp"
akmhoque298385a2014-02-13 14:13:09 -06008
akmhoque1fd8c1e2014-02-19 19:41:49 -06009namespace nlsr
akmhoque85d88332014-02-17 21:11:21 -060010{
akmhoque85d88332014-02-17 21:11:21 -060011
akmhoque5a44dd42014-03-12 18:11:32 -050012 using namespace std;
akmhoque1fd8c1e2014-02-19 19:41:49 -060013
akmhoque5a44dd42014-03-12 18:11:32 -050014 void
15 Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, EventId eid)
16 {
17 pnlsr.getScheduler().cancelEvent(eid);
18 }
19
20 static bool
21 nameLsaCompareByKey(NameLsa& nlsa1, string& key)
22 {
akmhoque05d5fcf2014-04-15 14:58:45 -050023 return nlsa1.getKey()==key;
akmhoque5a44dd42014-03-12 18:11:32 -050024 }
25
26
27 bool
28 Lsdb::buildAndInstallOwnNameLsa(Nlsr& pnlsr)
29 {
30 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
31 , 1
32 , pnlsr.getSm().getNameLsaSeq()+1
33 , pnlsr.getConfParameter().getRouterDeadInterval()
34 , pnlsr.getNpl() );
35 pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq()+1);
36 return installNameLsa(pnlsr,nameLsa);
37 }
38
39 std::pair<NameLsa&, bool>
40 Lsdb::getNameLsa(string key)
41 {
42 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
43 nameLsdb.end(),
44 bind(nameLsaCompareByKey, _1, key));
45 if( it != nameLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -060046 {
akmhoque5a44dd42014-03-12 18:11:32 -050047 return std::make_pair(boost::ref((*it)),true);
akmhoque1fd8c1e2014-02-19 19:41:49 -060048 }
akmhoque5a44dd42014-03-12 18:11:32 -050049 NameLsa nlsa;
50 return std::make_pair(boost::ref(nlsa),false);
51 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060052
akmhoque5a44dd42014-03-12 18:11:32 -050053 bool
54 Lsdb::isNameLsaNew(string key, uint64_t seqNo)
55 {
56 std::pair<NameLsa& , bool> nameLsaCheck=getNameLsa(key);
57 if(nameLsaCheck.second)
akmhoque1fd8c1e2014-02-19 19:41:49 -060058 {
akmhoque5a44dd42014-03-12 18:11:32 -050059 if(nameLsaCheck.first.getLsSeqNo() < seqNo)
60 {
akmhoque2bb198e2014-02-28 11:46:27 -060061 return true;
akmhoque5a44dd42014-03-12 18:11:32 -050062 }
63 else
64 {
akmhoque1fd8c1e2014-02-19 19:41:49 -060065 return false;
akmhoque5a44dd42014-03-12 18:11:32 -050066 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060067 }
akmhoque5a44dd42014-03-12 18:11:32 -050068 return true;
69 }
akmhoque298385a2014-02-13 14:13:09 -060070
akmhoque5a44dd42014-03-12 18:11:32 -050071 ndn::EventId
72 Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
73 {
74 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
75 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
76 this,boost::ref(pnlsr), key, seqNo));
77 }
78
79 bool
80 Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa &nlsa)
81 {
akmhoque05d5fcf2014-04-15 14:58:45 -050082 src::logger lg;
akmhoque5a44dd42014-03-12 18:11:32 -050083 int timeToExpire=lsaRefreshTime;
akmhoque05d5fcf2014-04-15 14:58:45 -050084 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(nlsa.getKey());
akmhoque5a44dd42014-03-12 18:11:32 -050085 if ( !chkNameLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -060086 {
akmhoque05d5fcf2014-04-15 14:58:45 -050087 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Adding name lsa";
akmhoque5a44dd42014-03-12 18:11:32 -050088 addNameLsa(nlsa);
akmhoque05d5fcf2014-04-15 14:58:45 -050089 nlsa.writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -050090 printNameLsdb();
91 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
92 {
93 pnlsr.getNpt().addNpteByDestName(nlsa.getOrigRouter(),nlsa.getOrigRouter(),
94 pnlsr);
95 std::list<string> nameList=nlsa.getNpl().getNameList();
96 for(std::list<string>::iterator it=nameList.begin(); it!=nameList.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -060097 {
akmhoque5a44dd42014-03-12 18:11:32 -050098 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
99 {
100 pnlsr.getNpt().addNpteByDestName((*it),nlsa.getOrigRouter(),pnlsr);
101 }
102 }
103 }
104 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
105 {
106 timeToExpire=nlsa.getLifeTime();
107 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500108 nlsa.setExpiringEventId(scheduleNameLsaExpiration( pnlsr,
109 nlsa.getKey(), nlsa.getLsSeqNo(), timeToExpire));
akmhoque5a44dd42014-03-12 18:11:32 -0500110 }
111 else
112 {
113 if ( chkNameLsa.first.getLsSeqNo() < nlsa.getLsSeqNo() )
114 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500115 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Deleting name lsa";
116 chkNameLsa.first.writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -0500117 chkNameLsa.first.setLsSeqNo(nlsa.getLsSeqNo());
118 chkNameLsa.first.setLifeTime(nlsa.getLifeTime());
akmhoque05d5fcf2014-04-15 14:58:45 -0500119 chkNameLsa.first.getNpl().sort();
120 nlsa.getNpl().sort();
akmhoque5a44dd42014-03-12 18:11:32 -0500121 std::list<string> nameToAdd;
122 std::set_difference(nlsa.getNpl().getNameList().begin(),
123 nlsa.getNpl().getNameList().end(),
124 chkNameLsa.first.getNpl().getNameList().begin(),
125 chkNameLsa.first.getNpl().getNameList().end(),
126 std::inserter(nameToAdd, nameToAdd.begin()));
127 for(std::list<string>::iterator it=nameToAdd.begin(); it!=nameToAdd.end();
128 ++it)
129 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500130 chkNameLsa.first.addName((*it));
akmhoque5a44dd42014-03-12 18:11:32 -0500131 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
132 {
133 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600134 {
akmhoque5a44dd42014-03-12 18:11:32 -0500135 pnlsr.getNpt().addNpteByDestName((*it),nlsa.getOrigRouter(),pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600136 }
akmhoque5a44dd42014-03-12 18:11:32 -0500137 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600138 }
akmhoque5a44dd42014-03-12 18:11:32 -0500139 std::list<string> nameToRemove;
140 std::set_difference(chkNameLsa.first.getNpl().getNameList().begin(),
141 chkNameLsa.first.getNpl().getNameList().end(),
142 nlsa.getNpl().getNameList().begin(),
143 nlsa.getNpl().getNameList().end(),
144 std::inserter(nameToRemove, nameToRemove.begin()));
145 for(std::list<string>::iterator it=nameToRemove.begin();
146 it!=nameToRemove.end(); ++it)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600147 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500148 chkNameLsa.first.removeName((*it));
akmhoque5a44dd42014-03-12 18:11:32 -0500149 if ( nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
150 {
151 if ( (*it) !=pnlsr.getConfParameter().getRouterPrefix())
152 {
153 pnlsr.getNpt().removeNpte((*it),nlsa.getOrigRouter(),pnlsr);
154 }
155 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600156 }
akmhoque5a44dd42014-03-12 18:11:32 -0500157 if(nlsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600158 {
akmhoque5a44dd42014-03-12 18:11:32 -0500159 timeToExpire=nlsa.getLifeTime();
akmhoque1fd8c1e2014-02-19 19:41:49 -0600160 }
akmhoque5a44dd42014-03-12 18:11:32 -0500161 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoque05d5fcf2014-04-15 14:58:45 -0500162 chkNameLsa.first.getExpiringEventId());
163 chkNameLsa.first.setExpiringEventId(scheduleNameLsaExpiration( pnlsr,
164 nlsa.getKey(), nlsa.getLsSeqNo(), timeToExpire));
165 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Adding name lsa";
166 chkNameLsa.first.writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -0500167 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600168 }
akmhoque5a44dd42014-03-12 18:11:32 -0500169 return true;
170 }
171
172 bool
173 Lsdb::addNameLsa(NameLsa &nlsa)
174 {
175 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
akmhoque05d5fcf2014-04-15 14:58:45 -0500176 nameLsdb.end(), bind(nameLsaCompareByKey, _1, nlsa.getKey()));
akmhoque5a44dd42014-03-12 18:11:32 -0500177 if( it == nameLsdb.end())
178 {
179 nameLsdb.push_back(nlsa);
180 return true;
181 }
182 return false;
183 }
184
185 bool
186 Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
187 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500188 src::logger lg;
akmhoque5a44dd42014-03-12 18:11:32 -0500189 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
190 nameLsdb.end(),
191 bind(nameLsaCompareByKey, _1, key));
192 if ( it != nameLsdb.end() )
193 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500194 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Deleting name lsa";
195 (*it).writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -0500196 if ( (*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix() )
197 {
198 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
199 for( std::list<string>::iterator nit=(*it).getNpl().getNameList().begin();
200 nit!=(*it).getNpl().getNameList().end(); ++nit)
201 {
202 if ( (*nit) !=pnlsr.getConfParameter().getRouterPrefix())
203 {
204 pnlsr.getNpt().removeNpte((*nit),(*it).getOrigRouter(),pnlsr);
205 }
206 }
207 }
208 nameLsdb.erase(it);
209 return true;
210 }
211 return false;
212 }
213
214 bool
215 Lsdb::doesNameLsaExist(string key)
216 {
217 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
218 nameLsdb.end(),
219 bind(nameLsaCompareByKey, _1, key));
220 if( it == nameLsdb.end())
221 {
222 return false;
223 }
224 return true;
225 }
226
227 void
228 Lsdb::printNameLsdb()
229 {
230 cout<<"---------------Name LSDB-------------------"<<endl;
231 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
232 it!= nameLsdb.end() ; it++)
233 {
234 cout<< (*it) <<endl;
235 }
236 }
akmhoque298385a2014-02-13 14:13:09 -0600237
238// Cor LSA and LSDB related Functions start here
239
240
akmhoque5a44dd42014-03-12 18:11:32 -0500241 static bool
242 corLsaCompareByKey(CorLsa& clsa, string& key)
243 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500244 return clsa.getKey()==key;
akmhoque5a44dd42014-03-12 18:11:32 -0500245 }
akmhoque298385a2014-02-13 14:13:09 -0600246
akmhoque5a44dd42014-03-12 18:11:32 -0500247 bool
248 Lsdb::buildAndInstallOwnCorLsa(Nlsr& pnlsr)
249 {
250 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
251 , 3
252 , pnlsr.getSm().getCorLsaSeq()+1
253 , pnlsr.getConfParameter().getRouterDeadInterval()
254 , pnlsr.getConfParameter().getCorR()
255 , pnlsr.getConfParameter().getCorTheta() );
256 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq()+1);
257 installCorLsa(pnlsr, corLsa);
258 return true;
259 }
260
261 std::pair<CorLsa&, bool>
262 Lsdb::getCorLsa(string key)
263 {
264 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
265 corLsdb.end(),
266 bind(corLsaCompareByKey, _1, key));
267 if( it != corLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600268 {
akmhoque5a44dd42014-03-12 18:11:32 -0500269 return std::make_pair(boost::ref((*it)), true);
270 }
271 CorLsa clsa;
272 return std::make_pair(boost::ref(clsa),false);
273 }
274
275 bool
276 Lsdb::isCorLsaNew(string key, uint64_t seqNo)
277 {
278 std::pair<CorLsa& , bool> corLsaCheck=getCorLsa(key);
279 if(corLsaCheck.second)
280 {
281 if(corLsaCheck.first.getLsSeqNo() < seqNo)
282 {
akmhoque1fd8c1e2014-02-19 19:41:49 -0600283 return true;
akmhoque5a44dd42014-03-12 18:11:32 -0500284 }
285 else
286 {
akmhoque1fd8c1e2014-02-19 19:41:49 -0600287 return false;
akmhoque5a44dd42014-03-12 18:11:32 -0500288 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600289 }
akmhoque5a44dd42014-03-12 18:11:32 -0500290 return true;
291 }
akmhoque298385a2014-02-13 14:13:09 -0600292
akmhoque5a44dd42014-03-12 18:11:32 -0500293 ndn::EventId
294 Lsdb::scheduleCorLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
295 {
296 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
297 ndn::bind(&Lsdb::exprireOrRefreshCorLsa,
298 this,boost::ref(pnlsr),key,seqNo));
299 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600300
akmhoque5a44dd42014-03-12 18:11:32 -0500301 bool
302 Lsdb::installCorLsa(Nlsr& pnlsr, CorLsa &clsa)
303 {
304 int timeToExpire=lsaRefreshTime;
akmhoque05d5fcf2014-04-15 14:58:45 -0500305 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(clsa.getKey());
akmhoque5a44dd42014-03-12 18:11:32 -0500306 if ( !chkCorLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600307 {
akmhoque5a44dd42014-03-12 18:11:32 -0500308 addCorLsa(clsa);
309 printCorLsdb(); //debugging purpose
310 if ( clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
311 {
312 pnlsr.getNpt().addNpteByDestName(clsa.getOrigRouter(),clsa.getOrigRouter(),
313 pnlsr);
314 }
315 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
316 {
317 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
318 }
319 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
320 {
321 timeToExpire=clsa.getLifeTime();
322 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500323 scheduleCorLsaExpiration(pnlsr,clsa.getKey(),
akmhoque5a44dd42014-03-12 18:11:32 -0500324 clsa.getLsSeqNo(), timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600325 }
akmhoque5a44dd42014-03-12 18:11:32 -0500326 else
327 {
328 if ( chkCorLsa.first.getLsSeqNo() < clsa.getLsSeqNo() )
329 {
330 chkCorLsa.first.setLsSeqNo(clsa.getLsSeqNo());
331 chkCorLsa.first.setLifeTime(clsa.getLifeTime());
akmhoque05d5fcf2014-04-15 14:58:45 -0500332 if ( !chkCorLsa.first.isEqual(clsa) )
akmhoque5a44dd42014-03-12 18:11:32 -0500333 {
334 chkCorLsa.first.setCorRadius(clsa.getCorRadius());
335 chkCorLsa.first.setCorTheta(clsa.getCorTheta());
336 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
337 {
338 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
339 }
340 }
341 if(clsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
342 {
343 timeToExpire=clsa.getLifeTime();
344 }
345 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoque05d5fcf2014-04-15 14:58:45 -0500346 chkCorLsa.first.getExpiringEventId());
347 chkCorLsa.first.setExpiringEventId(scheduleCorLsaExpiration(pnlsr,
348 clsa.getKey(),
akmhoque5a44dd42014-03-12 18:11:32 -0500349 clsa.getLsSeqNo(), timeToExpire));
350 }
351 }
352 return true;
353 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600354
akmhoque5a44dd42014-03-12 18:11:32 -0500355 bool
356 Lsdb::addCorLsa(CorLsa& clsa)
357 {
358 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
359 corLsdb.end(),
akmhoque05d5fcf2014-04-15 14:58:45 -0500360 bind(corLsaCompareByKey, _1, clsa.getKey()));
akmhoque5a44dd42014-03-12 18:11:32 -0500361 if( it == corLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600362 {
akmhoque5a44dd42014-03-12 18:11:32 -0500363 corLsdb.push_back(clsa);
364 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600365 }
akmhoque5a44dd42014-03-12 18:11:32 -0500366 return false;
367 }
368
369 bool
370 Lsdb::removeCorLsa(Nlsr& pnlsr, string& key)
371 {
372 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
373 corLsdb.end(),
374 bind(corLsaCompareByKey, _1, key));
375 if ( it != corLsdb.end() )
376 {
377 if ( (*it).getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
378 {
379 pnlsr.getNpt().removeNpte((*it).getOrigRouter(),(*it).getOrigRouter(),pnlsr);
380 }
381 corLsdb.erase(it);
382 return true;
383 }
384 return false;
385 }
386
387 bool
388 Lsdb::doesCorLsaExist(string key)
389 {
390 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
391 corLsdb.end(),
392 bind(corLsaCompareByKey, _1, key));
393 if( it == corLsdb.end())
394 {
395 return false;
396 }
397 return true;
398 }
399
400 void
401 Lsdb::printCorLsdb() //debugging
402 {
403 cout<<"---------------Cor LSDB-------------------"<<endl;
404 for( std::list<CorLsa>::iterator it=corLsdb.begin();
405 it!= corLsdb.end() ; it++)
406 {
407 cout<< (*it) <<endl;
408 }
409 }
akmhoque298385a2014-02-13 14:13:09 -0600410
411
412// Adj LSA and LSDB related function starts here
413
akmhoque5a44dd42014-03-12 18:11:32 -0500414 static bool
415 adjLsaCompareByKey(AdjLsa& alsa, string& key)
416 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500417 return alsa.getKey()==key;
akmhoque5a44dd42014-03-12 18:11:32 -0500418 }
akmhoque298385a2014-02-13 14:13:09 -0600419
420
akmhoque5a44dd42014-03-12 18:11:32 -0500421 void
422 Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
423 {
424 cout<<"scheduledAdjLsaBuild Called"<<endl;
425 pnlsr.setIsBuildAdjLsaSheduled(0);
426 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
akmhoque1fd8c1e2014-02-19 19:41:49 -0600427 {
akmhoque5a44dd42014-03-12 18:11:32 -0500428 int adjBuildCount=pnlsr.getAdjBuildCount();
429 if(adjBuildCount>0 )
430 {
431 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600432 {
akmhoque5a44dd42014-03-12 18:11:32 -0500433 buildAndInstallOwnAdjLsa(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600434 }
435 else
436 {
akmhoque5a44dd42014-03-12 18:11:32 -0500437 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
438 removeAdjLsa(pnlsr,key);
439 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600440 }
akmhoque5a44dd42014-03-12 18:11:32 -0500441 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
442 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600443 }
akmhoque5a44dd42014-03-12 18:11:32 -0500444 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600445 {
akmhoque5a44dd42014-03-12 18:11:32 -0500446 pnlsr.setIsBuildAdjLsaSheduled(1);
447 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
448 pnlsr.getConfParameter().getInterestResendTime();
449 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
450 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
451 boost::ref(pnlsr)));
akmhoque1fd8c1e2014-02-19 19:41:49 -0600452 }
akmhoque5a44dd42014-03-12 18:11:32 -0500453 }
akmhoque298385a2014-02-13 14:13:09 -0600454
akmhoque5a44dd42014-03-12 18:11:32 -0500455
456 bool
457 Lsdb::addAdjLsa(AdjLsa &alsa)
458 {
459 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
460 adjLsdb.end(),
akmhoque05d5fcf2014-04-15 14:58:45 -0500461 bind(adjLsaCompareByKey, _1, alsa.getKey()));
akmhoque5a44dd42014-03-12 18:11:32 -0500462 if( it == adjLsdb.end())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600463 {
akmhoque5a44dd42014-03-12 18:11:32 -0500464 adjLsdb.push_back(alsa);
465 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600466 }
akmhoque5a44dd42014-03-12 18:11:32 -0500467 return false;
468 }
akmhoque298385a2014-02-13 14:13:09 -0600469
akmhoque5a44dd42014-03-12 18:11:32 -0500470 std::pair<AdjLsa& , bool>
471 Lsdb::getAdjLsa(string key)
472 {
473 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
474 adjLsdb.end(),
475 bind(adjLsaCompareByKey, _1, key));
476 if( it != adjLsdb.end())
akmhoque2bb198e2014-02-28 11:46:27 -0600477 {
akmhoque5a44dd42014-03-12 18:11:32 -0500478 return std::make_pair(boost::ref((*it)),true);
479 }
480 AdjLsa alsa;
481 return std::make_pair(boost::ref(alsa),false);
482 }
483
484
485 bool
486 Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
487 {
488 std::pair<AdjLsa& , bool> adjLsaCheck=getAdjLsa(key);
489 if(adjLsaCheck.second)
490 {
491 if(adjLsaCheck.first.getLsSeqNo() < seqNo)
492 {
akmhoque2bb198e2014-02-28 11:46:27 -0600493 return true;
akmhoque5a44dd42014-03-12 18:11:32 -0500494 }
495 else
496 {
497 return false;
498 }
akmhoque2bb198e2014-02-28 11:46:27 -0600499 }
akmhoque5a44dd42014-03-12 18:11:32 -0500500 return true;
501 }
akmhoque2bb198e2014-02-28 11:46:27 -0600502
akmhoque298385a2014-02-13 14:13:09 -0600503
akmhoque5a44dd42014-03-12 18:11:32 -0500504 ndn::EventId
505 Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
506 {
507 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
508 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
509 this,boost::ref(pnlsr),key,seqNo));
510 }
511
512 bool
513 Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa &alsa)
514 {
515 int timeToExpire=lsaRefreshTime;
akmhoque05d5fcf2014-04-15 14:58:45 -0500516 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(alsa.getKey());
akmhoque5a44dd42014-03-12 18:11:32 -0500517 if ( !chkAdjLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600518 {
akmhoque5a44dd42014-03-12 18:11:32 -0500519 addAdjLsa(alsa);
akmhoque05d5fcf2014-04-15 14:58:45 -0500520 alsa.addNptEntries(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -0500521 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
522 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
523 {
524 timeToExpire=alsa.getLifeTime();
525 }
akmhoque05d5fcf2014-04-15 14:58:45 -0500526 scheduleAdjLsaExpiration(pnlsr,alsa.getKey(),
akmhoque5a44dd42014-03-12 18:11:32 -0500527 alsa.getLsSeqNo(),timeToExpire);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600528 }
akmhoque5a44dd42014-03-12 18:11:32 -0500529 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600530 {
akmhoque5a44dd42014-03-12 18:11:32 -0500531 if ( chkAdjLsa.first.getLsSeqNo() < alsa.getLsSeqNo() )
532 {
533 chkAdjLsa.first.setLsSeqNo(alsa.getLsSeqNo());
534 chkAdjLsa.first.setLifeTime(alsa.getLifeTime());
akmhoque05d5fcf2014-04-15 14:58:45 -0500535 if ( ! chkAdjLsa.first.isEqual(alsa))
akmhoque1fd8c1e2014-02-19 19:41:49 -0600536 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500537 chkAdjLsa.first.getAdl().reset();
akmhoque5a44dd42014-03-12 18:11:32 -0500538 chkAdjLsa.first.getAdl().addAdjacentsFromAdl(alsa.getAdl());
539 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
540 }
541 if(alsa.getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
542 {
543 timeToExpire=alsa.getLifeTime();
544 }
545 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoque05d5fcf2014-04-15 14:58:45 -0500546 chkAdjLsa.first.getExpiringEventId());
547 chkAdjLsa.first.setExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
548 alsa.getKey(), alsa.getLsSeqNo(),timeToExpire));
akmhoque5a44dd42014-03-12 18:11:32 -0500549 }
550 }
551 return true;
552 }
553
554 bool
555 Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
556 {
557 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
558 , 2
559 , pnlsr.getSm().getAdjLsaSeq()+1
560 , pnlsr.getConfParameter().getRouterDeadInterval()
561 , pnlsr.getAdl().getNumOfActiveNeighbor()
562 , pnlsr.getAdl() );
563 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
564 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
565 + pnlsr.getConfParameter().getRouterPrefix();
566 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
567 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
568 }
569
570 bool
571 Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
572 {
573 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
574 adjLsdb.end(),
575 bind(adjLsaCompareByKey, _1, key));
576 if ( it != adjLsdb.end() )
577 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500578 (*it).removeNptEntries(pnlsr);
akmhoque5a44dd42014-03-12 18:11:32 -0500579 adjLsdb.erase(it);
580 return true;
581 }
582 return false;
583 }
584
585 bool
586 Lsdb::doesAdjLsaExist(string key)
587 {
588 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
589 adjLsdb.end(),
590 bind(adjLsaCompareByKey, _1, key));
591 if( it == adjLsdb.end())
592 {
593 return false;
594 }
595 return true;
596 }
597
598 std::list<AdjLsa>&
599 Lsdb::getAdjLsdb()
600 {
601 return adjLsdb;
602 }
603
604 void
605 Lsdb::setLsaRefreshTime(int lrt)
606 {
607 lsaRefreshTime=lrt;
608 }
609
610 void
611 Lsdb::setThisRouterPrefix(string trp)
612 {
613 thisRouterPrefix=trp;
614 }
615
616 void
617 Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
618 {
619 cout<<"Lsdb::exprireOrRefreshNameLsa Called "<<endl;
620 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
621 std::pair<NameLsa& , bool> chkNameLsa=getNameLsa(lsaKey);
622 if( chkNameLsa.second )
623 {
624 cout<<" LSA Exists with seq no: "<<chkNameLsa.first.getLsSeqNo()<<endl;
625 if ( chkNameLsa.first.getLsSeqNo() == seqNo )
626 {
627 if(chkNameLsa.first.getOrigRouter() == thisRouterPrefix )
628 {
akmhoque05d5fcf2014-04-15 14:58:45 -0500629 src::logger lg;
630 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Deleting name lsa";
631 chkNameLsa.first.writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -0500632 cout<<"Own Name LSA, so refreshing name LSA"<<endl;
633 chkNameLsa.first.setLsSeqNo(chkNameLsa.first.getLsSeqNo()+1);
634 pnlsr.getSm().setNameLsaSeq(chkNameLsa.first.getLsSeqNo());
akmhoque05d5fcf2014-04-15 14:58:45 -0500635 BOOST_LOG(lg)<<" "<<THIS_FILE<<" "<<__LINE__<<": "<<"Adding name lsa";
636 chkNameLsa.first.writeLog();
akmhoque5a44dd42014-03-12 18:11:32 -0500637 // publish routing update
638 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
639 + pnlsr.getConfParameter().getRouterPrefix();
640 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600641 }
642 else
643 {
akmhoque5a44dd42014-03-12 18:11:32 -0500644 cout<<"Other's Name LSA, so removing form LSDB"<<endl;
645 removeNameLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600646 }
akmhoque5a44dd42014-03-12 18:11:32 -0500647 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600648 }
akmhoque5a44dd42014-03-12 18:11:32 -0500649 }
akmhoque298385a2014-02-13 14:13:09 -0600650
akmhoque5a44dd42014-03-12 18:11:32 -0500651 void
652 Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
653 {
654 cout<<"Lsdb::exprireOrRefreshAdjLsa Called "<<endl;
655 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
656 std::pair<AdjLsa& , bool> chkAdjLsa=getAdjLsa(lsaKey);
657 if( chkAdjLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600658 {
akmhoque5a44dd42014-03-12 18:11:32 -0500659 cout<<" LSA Exists with seq no: "<<chkAdjLsa.first.getLsSeqNo()<<endl;
660 if ( chkAdjLsa.first.getLsSeqNo() == seqNo )
661 {
662 if(chkAdjLsa.first.getOrigRouter() == thisRouterPrefix )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600663 {
akmhoque5a44dd42014-03-12 18:11:32 -0500664 cout<<"Own Adj LSA, so refreshing Adj LSA"<<endl;
665 chkAdjLsa.first.setLsSeqNo(chkAdjLsa.first.getLsSeqNo()+1);
666 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa.first.getLsSeqNo());
667 // publish routing update
668 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
669 + pnlsr.getConfParameter().getRouterPrefix();
670 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600671 }
akmhoque5a44dd42014-03-12 18:11:32 -0500672 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600673 {
akmhoque5a44dd42014-03-12 18:11:32 -0500674 cout<<"Other's Adj LSA, so removing form LSDB"<<endl;
675 removeAdjLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600676 }
akmhoque5a44dd42014-03-12 18:11:32 -0500677 // schedule Routing table calculaiton
678 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
679 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600680 }
akmhoque5a44dd42014-03-12 18:11:32 -0500681 }
akmhoque298385a2014-02-13 14:13:09 -0600682
akmhoque5a44dd42014-03-12 18:11:32 -0500683 void
684 Lsdb::exprireOrRefreshCorLsa(Nlsr& pnlsr, string lsaKey, int seqNo)
685 {
686 cout<<"Lsdb::exprireOrRefreshCorLsa Called "<<endl;
687 cout<<"LSA Key : "<<lsaKey<<" Seq No: "<<seqNo<<endl;
688 std::pair<CorLsa& , bool> chkCorLsa=getCorLsa(lsaKey);
689 if( chkCorLsa.second )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600690 {
akmhoque5a44dd42014-03-12 18:11:32 -0500691 cout<<" LSA Exists with seq no: "<<chkCorLsa.first.getLsSeqNo()<<endl;
692 if ( chkCorLsa.first.getLsSeqNo() == seqNo )
693 {
694 if(chkCorLsa.first.getOrigRouter() == thisRouterPrefix )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600695 {
akmhoque5a44dd42014-03-12 18:11:32 -0500696 cout<<"Own Cor LSA, so refreshing Cor LSA"<<endl;
697 chkCorLsa.first.setLsSeqNo(chkCorLsa.first.getLsSeqNo()+1);
698 pnlsr.getSm().setCorLsaSeq(chkCorLsa.first.getLsSeqNo());
699 // publish routing update
700 string lsaPrefix=pnlsr.getConfParameter().getChronosyncLsaPrefix()
701 + pnlsr.getConfParameter().getRouterPrefix();
702 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(),lsaPrefix);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600703 }
akmhoque5a44dd42014-03-12 18:11:32 -0500704 else
akmhoque1fd8c1e2014-02-19 19:41:49 -0600705 {
akmhoque5a44dd42014-03-12 18:11:32 -0500706 cout<<"Other's Cor LSA, so removing form LSDB"<<endl;
707 removeCorLsa(pnlsr, lsaKey);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600708 }
akmhoque5a44dd42014-03-12 18:11:32 -0500709 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >=1 )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600710 {
akmhoque5a44dd42014-03-12 18:11:32 -0500711 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600712 }
akmhoque5a44dd42014-03-12 18:11:32 -0500713 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600714 }
akmhoque5a44dd42014-03-12 18:11:32 -0500715 }
akmhoque298385a2014-02-13 14:13:09 -0600716
717
akmhoque5a44dd42014-03-12 18:11:32 -0500718 void
719 Lsdb::printAdjLsdb()
720 {
721 cout<<"---------------Adj LSDB-------------------"<<endl;
722 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
723 it!= adjLsdb.end() ; it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600724 {
akmhoque5a44dd42014-03-12 18:11:32 -0500725 cout<< (*it) <<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600726 }
akmhoque5a44dd42014-03-12 18:11:32 -0500727 }
akmhoque298385a2014-02-13 14:13:09 -0600728
729//-----utility function -----
akmhoque5a44dd42014-03-12 18:11:32 -0500730 bool
731 Lsdb::doesLsaExist(string key, int lsType)
732 {
733 if ( lsType == 1)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600734 {
akmhoque5a44dd42014-03-12 18:11:32 -0500735 return doesNameLsaExist(key);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600736 }
akmhoque5a44dd42014-03-12 18:11:32 -0500737 else if ( lsType == 2)
738 {
739 return doesAdjLsaExist(key);
740 }
741 else if ( lsType == 3)
742 {
743 return doesCorLsaExist(key);
744 }
745 return false;
746 }
akmhoque298385a2014-02-13 14:13:09 -0600747
akmhoqueb1710aa2014-02-19 17:13:36 -0600748}//namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -0600749