blob: e4bbb4fcc5cccd24a140f9f9bc0625e7aa20ff6c [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <utility>
3#include "lsdb.hpp"
4#include "nlsr.hpp"
5
6namespace nlsr {
7
8using namespace std;
9
10void
11Lsdb::cancelScheduleLsaExpiringEvent(Nlsr& pnlsr, EventId eid)
12{
13 pnlsr.getScheduler().cancelEvent(eid);
14}
15
16static bool
akmhoqueb6450b12014-04-24 00:01:03 -050017nameLsaCompareByKey(const NameLsa& nlsa1, const string& key)
akmhoque53353462014-04-22 08:43:45 -050018{
19 return nlsa1.getKey() == key;
20}
21
22
23bool
24Lsdb::buildAndInstallOwnNameLsa(Nlsr& pnlsr)
25{
26 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
27 , 1
28 , pnlsr.getSm().getNameLsaSeq() + 1
29 , pnlsr.getConfParameter().getRouterDeadInterval()
30 , pnlsr.getNpl());
31 pnlsr.getSm().setNameLsaSeq(pnlsr.getSm().getNameLsaSeq() + 1);
32 return installNameLsa(pnlsr, nameLsa);
33}
34
akmhoqueb6450b12014-04-24 00:01:03 -050035NameLsa*
36Lsdb::findNameLsa(const string key)
akmhoque53353462014-04-22 08:43:45 -050037{
38 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
39 m_nameLsdb.end(),
40 bind(nameLsaCompareByKey, _1, key));
41 if (it != m_nameLsdb.end())
42 {
akmhoqueb6450b12014-04-24 00:01:03 -050043 return &(*it);
akmhoque53353462014-04-22 08:43:45 -050044 }
akmhoqueb6450b12014-04-24 00:01:03 -050045 return 0;
akmhoque53353462014-04-22 08:43:45 -050046}
47
48bool
49Lsdb::isNameLsaNew(string key, uint64_t seqNo)
50{
akmhoqueb6450b12014-04-24 00:01:03 -050051 NameLsa* nameLsaCheck = findNameLsa(key);
52 if (nameLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -050053 {
akmhoqueb6450b12014-04-24 00:01:03 -050054 if (nameLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -050055 {
56 return true;
57 }
58 else
59 {
60 return false;
61 }
62 }
63 return true;
64}
65
66ndn::EventId
67Lsdb::scheduleNameLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
68{
69 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
70 ndn::bind(&Lsdb::exprireOrRefreshNameLsa,
71 this, boost::ref(pnlsr), key, seqNo));
72}
73
74bool
75Lsdb::installNameLsa(Nlsr& pnlsr, NameLsa& nlsa)
76{
77 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -050078 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
79 if (chkNameLsa == 0)
akmhoque53353462014-04-22 08:43:45 -050080 {
81 addNameLsa(nlsa);
82 nlsa.writeLog();
83 printNameLsdb();
84 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
85 {
86 pnlsr.getNpt().addNpteByDestName(nlsa.getOrigRouter(), nlsa.getOrigRouter(),
87 pnlsr);
88 std::list<string> nameList = nlsa.getNpl().getNameList();
89 for (std::list<string>::iterator it = nameList.begin(); it != nameList.end();
90 it++)
91 {
92 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
93 {
94 pnlsr.getNpt().addNpteByDestName((*it), nlsa.getOrigRouter(), pnlsr);
95 }
96 }
97 }
98 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
99 {
100 timeToExpire = nlsa.getLifeTime();
101 }
102 nlsa.setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
103 nlsa.getKey(),
104 nlsa.getLsSeqNo(),
105 timeToExpire));
106 }
107 else
108 {
akmhoqueb6450b12014-04-24 00:01:03 -0500109 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500110 {
akmhoqueb6450b12014-04-24 00:01:03 -0500111 chkNameLsa->writeLog();
112 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
113 chkNameLsa->setLifeTime(nlsa.getLifeTime());
114 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500115 nlsa.getNpl().sort();
116 std::list<string> nameToAdd;
117 std::set_difference(nlsa.getNpl().getNameList().begin(),
118 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500119 chkNameLsa->getNpl().getNameList().begin(),
120 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500121 std::inserter(nameToAdd, nameToAdd.begin()));
122 for (std::list<string>::iterator it = nameToAdd.begin(); it != nameToAdd.end();
123 ++it)
124 {
akmhoqueb6450b12014-04-24 00:01:03 -0500125 chkNameLsa->addName((*it));
akmhoque53353462014-04-22 08:43:45 -0500126 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
127 {
128 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
129 {
130 pnlsr.getNpt().addNpteByDestName((*it), nlsa.getOrigRouter(), pnlsr);
131 }
132 }
133 }
134 std::list<string> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500135 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
136 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500137 nlsa.getNpl().getNameList().begin(),
138 nlsa.getNpl().getNameList().end(),
139 std::inserter(nameToRemove, nameToRemove.begin()));
140 for (std::list<string>::iterator it = nameToRemove.begin();
141 it != nameToRemove.end(); ++it)
142 {
akmhoqueb6450b12014-04-24 00:01:03 -0500143 chkNameLsa->removeName((*it));
akmhoque53353462014-04-22 08:43:45 -0500144 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
145 {
146 if ((*it) != pnlsr.getConfParameter().getRouterPrefix())
147 {
148 pnlsr.getNpt().removeNpte((*it), nlsa.getOrigRouter(), pnlsr);
149 }
150 }
151 }
152 if (nlsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
153 {
154 timeToExpire = nlsa.getLifeTime();
155 }
156 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500157 chkNameLsa->getExpiringEventId());
158 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(pnlsr,
159 nlsa.getKey(),
160 nlsa.getLsSeqNo(),
161 timeToExpire));
162 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500163 }
164 }
165 return true;
166}
167
168bool
169Lsdb::addNameLsa(NameLsa& nlsa)
170{
171 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
172 m_nameLsdb.end(),
173 bind(nameLsaCompareByKey, _1,
174 nlsa.getKey()));
175 if (it == m_nameLsdb.end())
176 {
177 m_nameLsdb.push_back(nlsa);
178 return true;
179 }
180 return false;
181}
182
183bool
184Lsdb::removeNameLsa(Nlsr& pnlsr, string& key)
185{
186 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
187 m_nameLsdb.end(),
188 bind(nameLsaCompareByKey, _1, key));
189 if (it != m_nameLsdb.end())
190 {
191 (*it).writeLog();
192 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
193 {
194 pnlsr.getNpt().removeNpte((*it).getOrigRouter(), (*it).getOrigRouter(), pnlsr);
195 for (std::list<string>::iterator nit = (*it).getNpl().getNameList().begin();
196 nit != (*it).getNpl().getNameList().end(); ++nit)
197 {
198 if ((*nit) != pnlsr.getConfParameter().getRouterPrefix())
199 {
200 pnlsr.getNpt().removeNpte((*nit), (*it).getOrigRouter(), pnlsr);
201 }
202 }
203 }
204 m_nameLsdb.erase(it);
205 return true;
206 }
207 return false;
208}
209
210bool
211Lsdb::doesNameLsaExist(string key)
212{
213 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
214 m_nameLsdb.end(),
215 bind(nameLsaCompareByKey, _1, key));
216 if (it == m_nameLsdb.end())
217 {
218 return false;
219 }
220 return true;
221}
222
223void
224Lsdb::printNameLsdb()
225{
226 cout << "---------------Name LSDB-------------------" << endl;
227 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
228 it != m_nameLsdb.end() ; it++)
229 {
230 cout << (*it) << endl;
231 }
232}
233
234// Cor LSA and LSDB related Functions start here
235
236
237static bool
akmhoqueb6450b12014-04-24 00:01:03 -0500238corLsaCompareByKey(const CoordinateLsa& clsa, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500239{
240 return clsa.getKey() == key;
241}
242
243bool
akmhoqueb6450b12014-04-24 00:01:03 -0500244Lsdb::buildAndInstallOwnCoordinateLsa(Nlsr& pnlsr)
akmhoque53353462014-04-22 08:43:45 -0500245{
akmhoqueb6450b12014-04-24 00:01:03 -0500246 CoordinateLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
247 , 3
248 , pnlsr.getSm().getCorLsaSeq() + 1
249 , pnlsr.getConfParameter().getRouterDeadInterval()
250 , pnlsr.getConfParameter().getCorR()
251 , pnlsr.getConfParameter().getCorTheta());
akmhoque53353462014-04-22 08:43:45 -0500252 pnlsr.getSm().setCorLsaSeq(pnlsr.getSm().getCorLsaSeq() + 1);
akmhoqueb6450b12014-04-24 00:01:03 -0500253 installCoordinateLsa(pnlsr, corLsa);
akmhoque53353462014-04-22 08:43:45 -0500254 return true;
255}
256
akmhoqueb6450b12014-04-24 00:01:03 -0500257CoordinateLsa*
258Lsdb::findCoordinateLsa(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500259{
akmhoqueb6450b12014-04-24 00:01:03 -0500260 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
261 m_corLsdb.end(),
262 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500263 if (it != m_corLsdb.end())
264 {
akmhoqueb6450b12014-04-24 00:01:03 -0500265 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500266 }
akmhoqueb6450b12014-04-24 00:01:03 -0500267 return 0;
akmhoque53353462014-04-22 08:43:45 -0500268}
269
270bool
akmhoqueb6450b12014-04-24 00:01:03 -0500271Lsdb::isCoordinateLsaNew(const string& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500272{
akmhoqueb6450b12014-04-24 00:01:03 -0500273 CoordinateLsa* clsa = findCoordinateLsa(key);
274 if (clsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500275 {
akmhoqueb6450b12014-04-24 00:01:03 -0500276 if (clsa->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500277 {
278 return true;
279 }
280 else
281 {
282 return false;
283 }
284 }
285 return true;
286}
287
288ndn::EventId
akmhoqueb6450b12014-04-24 00:01:03 -0500289Lsdb::scheduleCoordinateLsaExpiration(Nlsr& pnlsr, const string& key, int seqNo,
290 int expTime)
akmhoque53353462014-04-22 08:43:45 -0500291{
292 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
akmhoqueb6450b12014-04-24 00:01:03 -0500293 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
akmhoque53353462014-04-22 08:43:45 -0500294 this, boost::ref(pnlsr),
295 key, seqNo));
296}
297
298bool
akmhoqueb6450b12014-04-24 00:01:03 -0500299Lsdb::installCoordinateLsa(Nlsr& pnlsr, CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500300{
301 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500302 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
303 if (chkCorLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500304 {
akmhoqueb6450b12014-04-24 00:01:03 -0500305 addCoordinateLsa(clsa);
akmhoque53353462014-04-22 08:43:45 -0500306 printCorLsdb(); //debugging purpose
307 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
308 {
309 pnlsr.getNpt().addNpteByDestName(clsa.getOrigRouter(), clsa.getOrigRouter(),
310 pnlsr);
311 }
312 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
313 {
314 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
315 }
316 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
317 {
318 timeToExpire = clsa.getLifeTime();
319 }
akmhoqueb6450b12014-04-24 00:01:03 -0500320 scheduleCoordinateLsaExpiration(pnlsr, clsa.getKey(),
321 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500322 }
323 else
324 {
akmhoqueb6450b12014-04-24 00:01:03 -0500325 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500326 {
akmhoqueb6450b12014-04-24 00:01:03 -0500327 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
328 chkCorLsa->setLifeTime(clsa.getLifeTime());
329 if (!chkCorLsa->isEqual(clsa))
akmhoque53353462014-04-22 08:43:45 -0500330 {
akmhoqueb6450b12014-04-24 00:01:03 -0500331 chkCorLsa->setCorRadius(clsa.getCorRadius());
332 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque53353462014-04-22 08:43:45 -0500333 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
334 {
335 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
336 }
337 }
338 if (clsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
339 {
340 timeToExpire = clsa.getLifeTime();
341 }
342 cancelScheduleLsaExpiringEvent(pnlsr,
akmhoqueb6450b12014-04-24 00:01:03 -0500343 chkCorLsa->getExpiringEventId());
344 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(pnlsr,
345 clsa.getKey(),
346 clsa.getLsSeqNo(),
347 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500348 }
349 }
350 return true;
351}
352
353bool
akmhoqueb6450b12014-04-24 00:01:03 -0500354Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500355{
akmhoqueb6450b12014-04-24 00:01:03 -0500356 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
357 m_corLsdb.end(),
358 bind(corLsaCompareByKey, _1,
359 clsa.getKey()));
akmhoque53353462014-04-22 08:43:45 -0500360 if (it == m_corLsdb.end())
361 {
362 m_corLsdb.push_back(clsa);
363 return true;
364 }
365 return false;
366}
367
368bool
akmhoqueb6450b12014-04-24 00:01:03 -0500369Lsdb::removeCoordinateLsa(Nlsr& pnlsr, const string& key)
akmhoque53353462014-04-22 08:43:45 -0500370{
akmhoqueb6450b12014-04-24 00:01:03 -0500371 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
372 m_corLsdb.end(),
373 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500374 if (it != m_corLsdb.end())
375 {
376 if ((*it).getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
377 {
378 pnlsr.getNpt().removeNpte((*it).getOrigRouter(), (*it).getOrigRouter(), pnlsr);
379 }
380 m_corLsdb.erase(it);
381 return true;
382 }
383 return false;
384}
385
386bool
akmhoqueb6450b12014-04-24 00:01:03 -0500387Lsdb::doesCoordinateLsaExist(const string& key)
akmhoque53353462014-04-22 08:43:45 -0500388{
akmhoqueb6450b12014-04-24 00:01:03 -0500389 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
390 m_corLsdb.end(),
391 bind(corLsaCompareByKey, _1, key));
akmhoque53353462014-04-22 08:43:45 -0500392 if (it == m_corLsdb.end())
393 {
394 return false;
395 }
396 return true;
397}
398
399void
400Lsdb::printCorLsdb() //debugging
401{
402 cout << "---------------Cor LSDB-------------------" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500403 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque53353462014-04-22 08:43:45 -0500404 it != m_corLsdb.end() ; it++)
405 {
406 cout << (*it) << endl;
407 }
408}
409
410
411// Adj LSA and LSDB related function starts here
412
413static bool
414adjLsaCompareByKey(AdjLsa& alsa, string& key)
415{
416 return alsa.getKey() == key;
417}
418
419
420void
421Lsdb::scheduledAdjLsaBuild(Nlsr& pnlsr)
422{
423 cout << "scheduledAdjLsaBuild Called" << endl;
424 pnlsr.setIsBuildAdjLsaSheduled(0);
425 if (pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
426 {
427 int adjBuildCount = pnlsr.getAdjBuildCount();
428 if (adjBuildCount > 0)
429 {
430 if (pnlsr.getAdl().getNumOfActiveNeighbor() > 0)
431 {
432 buildAndInstallOwnAdjLsa(pnlsr);
433 }
434 else
435 {
436 string key = pnlsr.getConfParameter().getRouterPrefix() + "/2";
437 removeAdjLsa(pnlsr, key);
438 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
439 }
440 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount() - adjBuildCount);
441 }
442 }
443 else
444 {
445 pnlsr.setIsBuildAdjLsaSheduled(1);
446 int schedulingTime = pnlsr.getConfParameter().getInterestRetryNumber() *
447 pnlsr.getConfParameter().getInterestResendTime();
448 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
449 ndn::bind(&Lsdb::scheduledAdjLsaBuild,
450 pnlsr.getLsdb(), boost::ref(pnlsr)));
451 }
452}
453
454
455bool
456Lsdb::addAdjLsa(AdjLsa& alsa)
457{
458 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
459 m_adjLsdb.end(),
460 bind(adjLsaCompareByKey, _1,
461 alsa.getKey()));
462 if (it == m_adjLsdb.end())
463 {
464 m_adjLsdb.push_back(alsa);
465 return true;
466 }
467 return false;
468}
469
akmhoqueb6450b12014-04-24 00:01:03 -0500470AdjLsa*
471Lsdb::findAdjLsa(const string key)
akmhoque53353462014-04-22 08:43:45 -0500472{
473 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
474 m_adjLsdb.end(),
475 bind(adjLsaCompareByKey, _1, key));
476 if (it != m_adjLsdb.end())
477 {
akmhoqueb6450b12014-04-24 00:01:03 -0500478 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500479 }
akmhoqueb6450b12014-04-24 00:01:03 -0500480 return 0;
akmhoque53353462014-04-22 08:43:45 -0500481}
482
483
484bool
485Lsdb::isAdjLsaNew(string key, uint64_t seqNo)
486{
akmhoqueb6450b12014-04-24 00:01:03 -0500487 AdjLsa* adjLsaCheck = findAdjLsa(key);
488 if (adjLsaCheck != 0)
akmhoque53353462014-04-22 08:43:45 -0500489 {
akmhoqueb6450b12014-04-24 00:01:03 -0500490 if (adjLsaCheck->getLsSeqNo() < seqNo)
akmhoque53353462014-04-22 08:43:45 -0500491 {
492 return true;
493 }
494 else
495 {
496 return false;
497 }
498 }
499 return true;
500}
501
502
503ndn::EventId
504Lsdb::scheduleAdjLsaExpiration(Nlsr& pnlsr, string key, int seqNo, int expTime)
505{
506 return pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(expTime),
507 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa,
508 this, boost::ref(pnlsr),
509 key, seqNo));
510}
511
512bool
513Lsdb::installAdjLsa(Nlsr& pnlsr, AdjLsa& alsa)
514{
515 int timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500516 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
517 if (chkAdjLsa == 0)
akmhoque53353462014-04-22 08:43:45 -0500518 {
519 addAdjLsa(alsa);
520 alsa.addNptEntries(pnlsr);
521 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
522 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
523 {
524 timeToExpire = alsa.getLifeTime();
525 }
526 scheduleAdjLsaExpiration(pnlsr, alsa.getKey(),
527 alsa.getLsSeqNo(), timeToExpire);
528 }
529 else
530 {
akmhoqueb6450b12014-04-24 00:01:03 -0500531 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo())
akmhoque53353462014-04-22 08:43:45 -0500532 {
akmhoqueb6450b12014-04-24 00:01:03 -0500533 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
534 chkAdjLsa->setLifeTime(alsa.getLifeTime());
535 if (!chkAdjLsa->isEqual(alsa))
akmhoque53353462014-04-22 08:43:45 -0500536 {
akmhoqueb6450b12014-04-24 00:01:03 -0500537 chkAdjLsa->getAdl().reset();
538 chkAdjLsa->getAdl().addAdjacentsFromAdl(alsa.getAdl());
akmhoque53353462014-04-22 08:43:45 -0500539 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
540 }
541 if (alsa.getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix())
542 {
543 timeToExpire = alsa.getLifeTime();
544 }
akmhoqueb6450b12014-04-24 00:01:03 -0500545 cancelScheduleLsaExpiringEvent(pnlsr, chkAdjLsa->getExpiringEventId());
546 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(pnlsr,
547 alsa.getKey(),
548 alsa.getLsSeqNo(),
549 timeToExpire));
akmhoque53353462014-04-22 08:43:45 -0500550 }
551 }
552 return true;
553}
554
555bool
556Lsdb::buildAndInstallOwnAdjLsa(Nlsr& pnlsr)
557{
558 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
559 , 2
560 , pnlsr.getSm().getAdjLsaSeq() + 1
561 , pnlsr.getConfParameter().getRouterDeadInterval()
562 , pnlsr.getAdl().getNumOfActiveNeighbor()
563 , pnlsr.getAdl());
564 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq() + 1);
565 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
566 + pnlsr.getConfParameter().getRouterPrefix();
567 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(), lsaPrefix);
568 return pnlsr.getLsdb().installAdjLsa(pnlsr, adjLsa);
569}
570
571bool
572Lsdb::removeAdjLsa(Nlsr& pnlsr, string& key)
573{
574 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
575 m_adjLsdb.end(),
576 bind(adjLsaCompareByKey, _1, key));
577 if (it != m_adjLsdb.end())
578 {
579 (*it).removeNptEntries(pnlsr);
580 m_adjLsdb.erase(it);
581 return true;
582 }
583 return false;
584}
585
586bool
587Lsdb::doesAdjLsaExist(string key)
588{
589 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
590 m_adjLsdb.end(),
591 bind(adjLsaCompareByKey, _1, key));
592 if (it == m_adjLsdb.end())
593 {
594 return false;
595 }
596 return true;
597}
598
599std::list<AdjLsa>&
600Lsdb::getAdjLsdb()
601{
602 return m_adjLsdb;
603}
604
605void
606Lsdb::setLsaRefreshTime(int lrt)
607{
608 m_lsaRefreshTime = lrt;
609}
610
611void
612Lsdb::setThisRouterPrefix(string trp)
613{
614 m_thisRouterPrefix = trp;
615}
616
617void
618Lsdb::exprireOrRefreshNameLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
619{
620 cout << "Lsdb::exprireOrRefreshNameLsa Called " << endl;
621 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500622 NameLsa* chkNameLsa = findNameLsa(lsaKey);
623 if (chkNameLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500624 {
akmhoqueb6450b12014-04-24 00:01:03 -0500625 cout << " LSA Exists with seq no: " << chkNameLsa->getLsSeqNo() << endl;
626 if (chkNameLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500627 {
akmhoqueb6450b12014-04-24 00:01:03 -0500628 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500629 {
akmhoqueb6450b12014-04-24 00:01:03 -0500630 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500631 cout << "Own Name LSA, so refreshing name LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500632 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
633 pnlsr.getSm().setNameLsaSeq(chkNameLsa->getLsSeqNo());
634 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500635 // publish routing update
636 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
637 + pnlsr.getConfParameter().getRouterPrefix();
638 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(), lsaPrefix);
639 }
640 else
641 {
642 cout << "Other's Name LSA, so removing form LSDB" << endl;
643 removeNameLsa(pnlsr, lsaKey);
644 }
645 }
646 }
647}
648
649void
650Lsdb::exprireOrRefreshAdjLsa(Nlsr& pnlsr, string lsaKey, uint64_t seqNo)
651{
652 cout << "Lsdb::exprireOrRefreshAdjLsa Called " << endl;
653 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500654 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
655 if (chkAdjLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500656 {
akmhoqueb6450b12014-04-24 00:01:03 -0500657 cout << " LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo() << endl;
658 if (chkAdjLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500659 {
akmhoqueb6450b12014-04-24 00:01:03 -0500660 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500661 {
662 cout << "Own Adj LSA, so refreshing Adj LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500663 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
664 pnlsr.getSm().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoque53353462014-04-22 08:43:45 -0500665 // publish routing update
666 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
667 + pnlsr.getConfParameter().getRouterPrefix();
668 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(), lsaPrefix);
669 }
670 else
671 {
672 cout << "Other's Adj LSA, so removing form LSDB" << endl;
673 removeAdjLsa(pnlsr, lsaKey);
674 }
675 // schedule Routing table calculaiton
676 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
677 }
678 }
679}
680
681void
akmhoqueb6450b12014-04-24 00:01:03 -0500682Lsdb::exprireOrRefreshCoordinateLsa(Nlsr& pnlsr, const string& lsaKey,
683 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500684{
685 cout << "Lsdb::exprireOrRefreshCorLsa Called " << endl;
686 cout << "LSA Key : " << lsaKey << " Seq No: " << seqNo << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500687 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
688 if (chkCorLsa != 0)
akmhoque53353462014-04-22 08:43:45 -0500689 {
akmhoqueb6450b12014-04-24 00:01:03 -0500690 cout << " LSA Exists with seq no: " << chkCorLsa->getLsSeqNo() << endl;
691 if (chkCorLsa->getLsSeqNo() == seqNo)
akmhoque53353462014-04-22 08:43:45 -0500692 {
akmhoqueb6450b12014-04-24 00:01:03 -0500693 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix)
akmhoque53353462014-04-22 08:43:45 -0500694 {
695 cout << "Own Cor LSA, so refreshing Cor LSA" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500696 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
697 pnlsr.getSm().setCorLsaSeq(chkCorLsa->getLsSeqNo());
akmhoque53353462014-04-22 08:43:45 -0500698 // publish routing update
699 string lsaPrefix = pnlsr.getConfParameter().getChronosyncLsaPrefix()
700 + pnlsr.getConfParameter().getRouterPrefix();
701 pnlsr.getSlh().publishRoutingUpdate(pnlsr.getSm(), lsaPrefix);
702 }
703 else
704 {
705 cout << "Other's Cor LSA, so removing form LSDB" << endl;
akmhoqueb6450b12014-04-24 00:01:03 -0500706 removeCoordinateLsa(pnlsr, lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500707 }
708 if (pnlsr.getConfParameter().getIsHyperbolicCalc() >= 1)
709 {
710 pnlsr.getRoutingTable().scheduleRoutingTableCalculation(pnlsr);
711 }
712 }
713 }
714}
715
716
717void
718Lsdb::printAdjLsdb()
719{
720 cout << "---------------Adj LSDB-------------------" << endl;
721 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
722 it != m_adjLsdb.end() ; it++)
723 {
724 cout << (*it) << endl;
725 }
726}
727
728//-----utility function -----
729bool
730Lsdb::doesLsaExist(string key, int lsType)
731{
732 if (lsType == 1)
733 {
734 return doesNameLsaExist(key);
735 }
736 else if (lsType == 2)
737 {
738 return doesAdjLsaExist(key);
739 }
740 else if (lsType == 3)
741 {
akmhoqueb6450b12014-04-24 00:01:03 -0500742 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -0500743 }
744 return false;
745}
746
747}//namespace nlsr
748