blob: b4cbe9121f5a8bdfe144ec8cde3cc41f09a11b4f [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 -060033static bool
34nameLsaCompare(NameLsa& nlsa1, NameLsa& nlsa2){
35 return nlsa1.getLsaKey()==nlsa1.getLsaKey();
36}
37
38static bool
39nameLsaCompareByKey(NameLsa& nlsa1, string& key){
40 return nlsa1.getLsaKey()==key;
41}
42
43
44bool
45Lsdb::buildAndInstallOwnNameLsa(nlsr& pnlsr)
46{
47 NameLsa nameLsa(pnlsr.getConfParameter().getRouterPrefix()
48 , 1
49 , pnlsr.getNameLsaSeq()+1
50 , pnlsr.getConfParameter().getRouterDeadInterval()
51 , pnlsr.getNpl() );
52 pnlsr.setNameLsaSeq(pnlsr.getNameLsaSeq()+1);
akmhoque3c6bd922014-02-01 17:10:17 -060053 //cout<<nameLsa;
akmhoquebd7c8e62014-02-01 14:57:40 -060054 return installNameLsa(nameLsa);
55
56}
57
58NameLsa&
59Lsdb::getNameLsa(string key)
60{
61 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
62 nameLsdb.end(),
63 bind(nameLsaCompareByKey, _1, key));
64
akmhoquefcf765d2014-02-01 23:46:17 -060065 if( it != nameLsdb.end())
66 {
akmhoquebd7c8e62014-02-01 14:57:40 -060067 return (*it);
68 }
akmhoquefcf765d2014-02-01 23:46:17 -060069
akmhoquebd7c8e62014-02-01 14:57:40 -060070}
71
72
73
74bool
75Lsdb::installNameLsa(NameLsa &nlsa)
76{
77 bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey());
78 if ( !doesLsaExist_ )
79 {
80 // add name LSA
81 addNameLsa(nlsa);
82 // update NPT and FIB
akmhoquefcf765d2014-02-01 23:46:17 -060083 // if its not own LSA
akmhoquebd7c8e62014-02-01 14:57:40 -060084 }
85 else
86 {
87 // check for newer name LSA
88 NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey());
89 // Discard or Update Name lsa, NPT, FIB
akmhoquefcf765d2014-02-01 23:46:17 -060090 // if its not own LSA
akmhoquebd7c8e62014-02-01 14:57:40 -060091 }
92
93 return true;
94}
95
96bool
97Lsdb::addNameLsa(NameLsa &nlsa)
98{
99 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
100 nameLsdb.end(),
101 bind(nameLsaCompare, _1, nlsa));
102
103 if( it == nameLsdb.end()){
104 nameLsdb.push_back(nlsa);
105 return true;
106 }
107 return false;
108}
109
110bool
111Lsdb::removeNameLsa(string& key)
112{
akmhoquefcf765d2014-02-01 23:46:17 -0600113 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
114 nameLsdb.end(),
115 bind(nameLsaCompareByKey, _1, key));
116 if ( it != nameLsdb.end() )
117 {
118 nameLsdb.erase(it);
119 return true;
120 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600121 return false;
122}
akmhoquebd7c8e62014-02-01 14:57:40 -0600123
124bool
125Lsdb::doesNameLsaExist(string key)
126{
127 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
128 nameLsdb.end(),
129 bind(nameLsaCompareByKey, _1, key));
130
131 if( it == nameLsdb.end()){
132 return false;
133 }
134
135 return true;
136}
137
akmhoque3c6bd922014-02-01 17:10:17 -0600138void
139Lsdb::printNameLsdb()
140{
141 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
142 it!= nameLsdb.end() ; it++)
143 {
144 cout<< (*it) <<endl;
145 }
146}
147
148// Cor LSA and LSDB related Functions start here
149
150static bool
151corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
152 return clsa1.getLsaKey()==clsa1.getLsaKey();
153}
154
155static bool
156corLsaCompareByKey(CorLsa& clsa, string& key){
157 return clsa.getLsaKey()==key;
158}
akmhoquebd7c8e62014-02-01 14:57:40 -0600159
160bool
akmhoque3c6bd922014-02-01 17:10:17 -0600161Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
162 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
akmhoquecd552472014-02-01 21:22:16 -0600163 , 3
akmhoque3c6bd922014-02-01 17:10:17 -0600164 , pnlsr.getCorLsaSeq()+1
165 , pnlsr.getConfParameter().getRouterDeadInterval()
166 , pnlsr.getConfParameter().getCorR()
167 , pnlsr.getConfParameter().getCorTheta() );
168 pnlsr.setCorLsaSeq(pnlsr.getCorLsaSeq()+1);
169 //cout<<corLsa;
170 installCorLsa(corLsa);
171
akmhoquefcf765d2014-02-01 23:46:17 -0600172 return true;
akmhoque3c6bd922014-02-01 17:10:17 -0600173}
174
175CorLsa&
176Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600177{
akmhoque3c6bd922014-02-01 17:10:17 -0600178 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
179 corLsdb.end(),
180 bind(corLsaCompareByKey, _1, key));
181
182 if( it != corLsdb.end()){
183 return (*it);
184 }
185}
186
187bool
188Lsdb::installCorLsa(CorLsa &clsa)
189{
190 bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey());
191 if ( !doesLsaExist_ )
192 {
193 // add cor LSA
194 addCorLsa(clsa);
akmhoquefcf765d2014-02-01 23:46:17 -0600195 //schedule routing table calculation only if
196 //hyperbolic calculation is scheduled
akmhoque3c6bd922014-02-01 17:10:17 -0600197 }
198 else
199 {
200 // check for newer cor LSA
201 CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey());
202
203 }
204
205 return true;
206}
207
208bool
209Lsdb::addCorLsa(CorLsa& clsa)
210{
211 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
212 corLsdb.end(),
213 bind(corLsaCompare, _1, clsa));
214
215 if( it == corLsdb.end()){
216 corLsdb.push_back(clsa);
217 return true;
218 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600219 return false;
220}
221
222bool
akmhoque3c6bd922014-02-01 17:10:17 -0600223Lsdb::removeCorLsa(string& key)
224{
akmhoquefcf765d2014-02-01 23:46:17 -0600225 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
226 corLsdb.end(),
227 bind(corLsaCompareByKey, _1, key));
228 if ( it != corLsdb.end() )
229 {
230 corLsdb.erase(it);
231 return true;
232 }
233 return false;
akmhoque3c6bd922014-02-01 17:10:17 -0600234
235}
236
237bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600238Lsdb::doesCorLsaExist(string key)
239{
akmhoque3c6bd922014-02-01 17:10:17 -0600240 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
241 corLsdb.end(),
242 bind(corLsaCompareByKey, _1, key));
243
244 if( it == corLsdb.end()){
245 return false;
246 }
247
248 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600249}
akmhoque3c6bd922014-02-01 17:10:17 -0600250
251void
252Lsdb::printCorLsdb() //debugging
253{
254 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
263
264static bool
265adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
266 return alsa1.getLsaKey()==alsa1.getLsaKey();
267}
268
269static 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(),
318 bind(adjLsaCompare, _1, alsa));
319
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
341Lsdb::installAdjLsa(AdjLsa &alsa)
342{
343 bool doesLsaExist_ = doesAdjLsaExist(alsa.getLsaKey());
344 if ( !doesLsaExist_ )
345 {
346 // add Adj LSA
347 addAdjLsa(alsa);
348 // schedule routing table calculation
349 }
350 else
351 {
352 // check for newer name LSA
353 AdjLsa oldAdjLsa=getAdjLsa(alsa.getLsaKey());
354
355 }
356
357 printAdjLsdb();
358
359 return true;
360}
361
362bool
363Lsdb::buildAndInstallOwnAdjLsa(nlsr& pnlsr)
364{
365 AdjLsa adjLsa(pnlsr.getConfParameter().getRouterPrefix()
366 , 2
367 , pnlsr.getAdjLsaSeq()+1
368 , pnlsr.getConfParameter().getRouterDeadInterval()
369 , pnlsr.getAdl().getNumOfActiveNeighbor()
370 , pnlsr.getAdl() );
371 pnlsr.setAdjLsaSeq(pnlsr.getAdjLsaSeq()+1);
372 return installAdjLsa(adjLsa);
373}
374
375bool
376Lsdb::removeAdjLsa(string& key)
377{
akmhoquefcf765d2014-02-01 23:46:17 -0600378 std::list<AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
379 adjLsdb.end(),
380 bind(adjLsaCompareByKey, _1, key));
381 if ( it != adjLsdb.end() )
382 {
383 adjLsdb.erase(it);
384 return true;
385 }
386 return false;
akmhoquecd552472014-02-01 21:22:16 -0600387
388}
akmhoque3c6bd922014-02-01 17:10:17 -0600389
390bool
391Lsdb::doesAdjLsaExist(string key)
392{
393 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
394 adjLsdb.end(),
395 bind(adjLsaCompareByKey, _1, key));
396
397 if( it == adjLsdb.end()){
398 return false;
399 }
400
401 return true;
402}
403
akmhoquecd552472014-02-01 21:22:16 -0600404void
405Lsdb::printAdjLsdb()
406{
407 for( std::list<AdjLsa>::iterator it=adjLsdb.begin();
408 it!= adjLsdb.end() ; it++)
409 {
410 cout<< (*it) <<endl;
411 }
412}
akmhoque3c6bd922014-02-01 17:10:17 -0600413