blob: 062a2b15b148174340df24b0334ad1a15372b668 [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){
36 return nlsa1.getLsaKey()==key;
37}
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{
72 bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey());
73 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
84 NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey());
85 // 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(),
akmhoquedfa4a5b2014-02-03 20:12:29 -060097 bind(nameLsaCompareByKey, _1, nlsa.getLsaKey()));
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){
155 return clsa.getLsaKey()==key;
156}
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{
187 bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey());
188 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
199 CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey());
200
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(),
akmhoquedfa4a5b2014-02-03 20:12:29 -0600211 bind(corLsaCompareByKey, _1, clsa.getLsaKey()));
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){
271 return alsa.getLsaKey()==key;
272}
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(),
akmhoquedfa4a5b2014-02-03 20:12:29 -0600318 bind(adjLsaCompareByKey, _1, alsa.getLsaKey()));
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{
343 bool doesLsaExist_ = doesAdjLsaExist(alsa.getLsaKey());
344 if ( !doesLsaExist_ )
345 {
346 // add Adj LSA
347 addAdjLsa(alsa);
348 // schedule routing table calculation
akmhoquedfa4a5b2014-02-03 20:12:29 -0600349 pnlsr.getScheduler().scheduleEvent(ndn::time::seconds(15),
350 ndn::bind(&RoutingTable::calculate, &pnlsr.getRoutingTable()));
akmhoquecd552472014-02-01 21:22:16 -0600351 }
352 else
353 {
354 // check for newer name LSA
355 AdjLsa oldAdjLsa=getAdjLsa(alsa.getLsaKey());
356
357 }
358
359 printAdjLsdb();
360
361 return true;
362}
363
364bool
365Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
366{
367 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
368 , 2
akmhoquedfa4a5b2014-02-03 20:12:29 -0600369 , pnlsr.getSm().getAdjLsaSeq()+1
akmhoquecd552472014-02-01 21:22:16 -0600370 , pnlsr.getConfParameter().getRouterDeadInterval()
371 , pnlsr.getAdl().getNumOfActiveNeighbor()
372 , pnlsr.getAdl() );
akmhoquedfa4a5b2014-02-03 20:12:29 -0600373 pnlsr.getSm().setAdjLsaSeq(pnlsr.getSm().getAdjLsaSeq()+1);
374 return installAdjLsa(pnlsr, adjLsa);
akmhoquecd552472014-02-01 21:22:16 -0600375}
376
377bool
378Lsdb::removeAdjLsa(string& key)
379{
akmhoquefcf765d2014-02-01 23:46:17 -0600380 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
381 adjLsdb.end(),
382 bind(adjLsaCompareByKey, _1, key));
383 if ( it != adjLsdb.end() )
384 {
385 adjLsdb.erase(it);
386 return true;
387 }
388 return false;
akmhoquecd552472014-02-01 21:22:16 -0600389
390}
akmhoque3c6bd922014-02-01 17:10:17 -0600391
392bool
393Lsdb::doesAdjLsaExist(string key)
394{
395 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
396 adjLsdb.end(),
397 bind(adjLsaCompareByKey, _1, key));
398
399 if( it == adjLsdb.end()){
400 return false;
401 }
402
403 return true;
404}
405
akmhoquecd552472014-02-01 21:22:16 -0600406void
407Lsdb::printAdjLsdb()
408{
akmhoquedfa4a5b2014-02-03 20:12:29 -0600409 cout<<"---------------Adj LSDB-------------------"<<endl;
akmhoquecd552472014-02-01 21:22:16 -0600410 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
411 it!= adjLsdb.end() ; it++)
412 {
413 cout<< (*it) <<endl;
414 }
415}
akmhoque3c6bd922014-02-01 17:10:17 -0600416