blob: 8d7e14162162f7bc5ccf279d5cde4e6aa7e561f8 [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);
akmhoque3c6bd922014-02-01 17:10:17 -0600167 installCorLsa(corLsa);
168
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
185Lsdb::installCorLsa(CorLsa &clsa)
186{
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);
akmhoquefcf765d2014-02-01 23:46:17 -0600192 //schedule routing table calculation only if
193 //hyperbolic calculation is scheduled
akmhoquedfa4a5b2014-02-03 20:12:29 -0600194 printCorLsdb();
akmhoque3c6bd922014-02-01 17:10:17 -0600195 }
196 else
197 {
198 // check for newer cor LSA
akmhoque3fdf7612014-02-04 21:18:23 -0600199 CorLsa oldCorLsa=getCorLsa(clsa.getCorLsaKey());
akmhoque3c6bd922014-02-01 17:10:17 -0600200
201 }
202
203 return true;
204}
205
206bool
207Lsdb::addCorLsa(CorLsa& clsa)
208{
209 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
210 corLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600211 bind(corLsaCompareByKey, _1, clsa.getCorLsaKey()));
akmhoque3c6bd922014-02-01 17:10:17 -0600212
akmhoquedfa4a5b2014-02-03 20:12:29 -0600213 if( it == corLsdb.end())
214 {
akmhoque3c6bd922014-02-01 17:10:17 -0600215 corLsdb.push_back(clsa);
216 return true;
217 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600218 return false;
219}
220
221bool
akmhoque3c6bd922014-02-01 17:10:17 -0600222Lsdb::removeCorLsa(string& key)
223{
akmhoquefcf765d2014-02-01 23:46:17 -0600224 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
225 corLsdb.end(),
226 bind(corLsaCompareByKey, _1, key));
227 if ( it != corLsdb.end() )
228 {
229 corLsdb.erase(it);
230 return true;
231 }
232 return false;
akmhoque3c6bd922014-02-01 17:10:17 -0600233
234}
235
236bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600237Lsdb::doesCorLsaExist(string key)
238{
akmhoque3c6bd922014-02-01 17:10:17 -0600239 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
240 corLsdb.end(),
241 bind(corLsaCompareByKey, _1, key));
242
243 if( it == corLsdb.end()){
244 return false;
245 }
246
247 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600248}
akmhoque3c6bd922014-02-01 17:10:17 -0600249
250void
251Lsdb::printCorLsdb() //debugging
252{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600253 cout<<"---------------Cor LSDB-------------------"<<endl;
akmhoque3c6bd922014-02-01 17:10:17 -0600254 for( std::list<CorLsa>::iterator it=corLsdb.begin();
255 it!= corLsdb.end() ; it++)
256 {
257 cout<< (*it) <<endl;
258 }
259}
260
261
262// Adj LSA and LSDB related function starts here
akmhoquedfa4a5b2014-02-03 20:12:29 -0600263/*
akmhoque3c6bd922014-02-01 17:10:17 -0600264static bool
265adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
266 return alsa1.getLsaKey()==alsa1.getLsaKey();
267}
akmhoquedfa4a5b2014-02-03 20:12:29 -0600268*/
akmhoque3c6bd922014-02-01 17:10:17 -0600269static bool
270adjLsaCompareByKey(AdjLsa& alsa, string& key){
akmhoque3fdf7612014-02-04 21:18:23 -0600271 return alsa.getAdjLsaKey()==key;
akmhoque3c6bd922014-02-01 17:10:17 -0600272}
273
274
akmhoquecd552472014-02-01 21:22:16 -0600275void
276Lsdb::scheduledAdjLsaBuild(nlsr& pnlsr)
277{
278 cout<<"scheduledAdjLsaBuild Called"<<endl;
279 pnlsr.setIsBuildAdjLsaSheduled(0);
akmhoque3c6bd922014-02-01 17:10:17 -0600280
akmhoquecd552472014-02-01 21:22:16 -0600281 if( pnlsr.getAdl().isAdjLsaBuildable(pnlsr))
282 {
283 int adjBuildCount=pnlsr.getAdjBuildCount();
284 if(adjBuildCount>0 )
285 {
286 if (pnlsr.getAdl().getNumOfActiveNeighbor()>0)
287 {
288 buildAndInstallOwnAdjLsa(pnlsr);
289 }
290 else
291 {
292 //remove if there is any adj lsa in LSDB
akmhoquefcf765d2014-02-01 23:46:17 -0600293 string key=pnlsr.getConfParameter().getRouterPrefix()+"/2";
294 removeAdjLsa(key);
295 // Remove alll fib entries as per NPT
akmhoquecd552472014-02-01 21:22:16 -0600296 }
297 pnlsr.setAdjBuildCount(pnlsr.getAdjBuildCount()-adjBuildCount);
298 }
299 }
300 else
301 {
302 pnlsr.setIsBuildAdjLsaSheduled(1);
303 int schedulingTime=pnlsr.getConfParameter().getInterestRetryNumber()*
304 pnlsr.getConfParameter().getInterestRetryNumber();
305 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(schedulingTime),
306 ndn::bind(&Lsdb::scheduledAdjLsaBuild, pnlsr.getLsdb(),
307 boost::ref(pnlsr)));
308 }
309
310}
311
312
313bool
314Lsdb::addAdjLsa(AdjLsa &alsa)
315{
316 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
317 adjLsdb.end(),
akmhoque3fdf7612014-02-04 21:18:23 -0600318 bind(adjLsaCompareByKey, _1, alsa.getAdjLsaKey()));
akmhoquecd552472014-02-01 21:22:16 -0600319
320 if( it == adjLsdb.end()){
321 adjLsdb.push_back(alsa);
322 return true;
323 }
324 return false;
325
326}
327
328AdjLsa&
329Lsdb::getAdjLsa(string key)
330{
331 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
332 adjLsdb.end(),
333 bind(adjLsaCompareByKey, _1, key));
334
335 if( it != adjLsdb.end()){
336 return (*it);
337 }
akmhoquecd552472014-02-01 21:22:16 -0600338}
339
340bool
akmhoquedfa4a5b2014-02-03 20:12:29 -0600341Lsdb::installAdjLsa(nlsr& pnlsr, AdjLsa &alsa)
akmhoquecd552472014-02-01 21:22:16 -0600342{
akmhoque3fdf7612014-02-04 21:18:23 -0600343 bool doesLsaExist_ = doesAdjLsaExist(alsa.getAdjLsaKey());
akmhoquecd552472014-02-01 21:22:16 -0600344 if ( !doesLsaExist_ )
345 {
346 // add Adj LSA
347 addAdjLsa(alsa);
348 // schedule routing table calculation
akmhoque79d355f2014-02-04 15:11:16 -0600349 if ( pnlsr.getIsRouteCalculationScheduled() != 1 )
350 {
351 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
352 ndn::bind(&RoutingTable::calculate,
353 &pnlsr.getRoutingTable(),boost::ref(pnlsr)));
354 pnlsr.setIsRouteCalculationScheduled(1);
355 }
akmhoquecd552472014-02-01 21:22:16 -0600356 }
357 else
358 {
359 // check for newer name LSA
akmhoque3fdf7612014-02-04 21:18:23 -0600360 AdjLsa oldAdjLsa=getAdjLsa(alsa.getAdjLsaKey());
akmhoquecd552472014-02-01 21:22:16 -0600361
362 }
363
364 printAdjLsdb();
365
366 return true;
367}
368
369bool
370Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
371{
372 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
373 , 2
akmhoquedfa4a5b2014-02-03 20:12:29 -0600374 , pnlsr.getSm().getAdjLsaSeq()+1
akmhoquecd552472014-02-01 21:22:16 -0600375 , pnlsr.getConfParameter().getRouterDeadInterval()
376 , pnlsr.getAdl().getNumOfActiveNeighbor()
377 , pnlsr.getAdl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600378 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
379 return installAdjLsa(pnlsr, adjLsa);
akmhoquecd552472014-02-01 21:22:16 -0600380}
381
382bool
383Lsdb::removeAdjLsa(string& key)
384{
akmhoquefcf765d2014-02-01 23:46:17 -0600385 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
386 adjLsdb.end(),
387 bind(adjLsaCompareByKey, _1, key));
388 if ( it != adjLsdb.end() )
389 {
390 adjLsdb.erase(it);
391 return true;
392 }
393 return false;
akmhoquecd552472014-02-01 21:22:16 -0600394
395}
akmhoque3c6bd922014-02-01 17:10:17 -0600396
397bool
398Lsdb::doesAdjLsaExist(string key)
399{
400 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
401 adjLsdb.end(),
402 bind(adjLsaCompareByKey, _1, key));
403
404 if( it == adjLsdb.end()){
405 return false;
406 }
407
408 return true;
409}
410
akmhoque79d355f2014-02-04 15:11:16 -0600411std::list<AdjLsa>&
412Lsdb::getAdjLsdb()
413{
414 return adjLsdb;
415}
416
akmhoquecd552472014-02-01 21:22:16 -0600417void
418Lsdb::printAdjLsdb()
419{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600420 cout<<"---------------Adj LSDB-------------------"<<endl;
akmhoquecd552472014-02-01 21:22:16 -0600421 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
422 it!= adjLsdb.end() ; it++)
423 {
424 cout<< (*it) <<endl;
425 }
426}
akmhoque3c6bd922014-02-01 17:10:17 -0600427