blob: c6ae523ea021521a81519d420b8db613d253f6e0 [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
65 if( it != nameLsdb.end()){
66 return (*it);
67 }
68}
69
70
71
72bool
73Lsdb::installNameLsa(NameLsa &nlsa)
74{
75 bool doesLsaExist_ = doesNameLsaExist(nlsa.getLsaKey());
76 if ( !doesLsaExist_ )
77 {
78 // add name LSA
79 addNameLsa(nlsa);
80 // update NPT and FIB
81 }
82 else
83 {
84 // check for newer name LSA
85 NameLsa oldNameLsa=getNameLsa(nlsa.getLsaKey());
86 // Discard or Update Name lsa, NPT, FIB
87 }
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(),
97 bind(nameLsaCompare, _1, nlsa));
98
99 if( it == nameLsdb.end()){
100 nameLsdb.push_back(nlsa);
101 return true;
102 }
103 return false;
104}
105
106bool
107Lsdb::removeNameLsa(string& key)
108{
109 return false;
110}
akmhoquebd7c8e62014-02-01 14:57:40 -0600111
112bool
113Lsdb::doesNameLsaExist(string key)
114{
115 std::list<NameLsa >::iterator it = std::find_if( nameLsdb.begin(),
116 nameLsdb.end(),
117 bind(nameLsaCompareByKey, _1, key));
118
119 if( it == nameLsdb.end()){
120 return false;
121 }
122
123 return true;
124}
125
akmhoque3c6bd922014-02-01 17:10:17 -0600126void
127Lsdb::printNameLsdb()
128{
129 for( std::list<NameLsa>::iterator it=nameLsdb.begin();
130 it!= nameLsdb.end() ; it++)
131 {
132 cout<< (*it) <<endl;
133 }
134}
135
136// Cor LSA and LSDB related Functions start here
137
138static bool
139corLsaCompare(CorLsa& clsa1, CorLsa& clsa2){
140 return clsa1.getLsaKey()==clsa1.getLsaKey();
141}
142
143static bool
144corLsaCompareByKey(CorLsa& clsa, string& key){
145 return clsa.getLsaKey()==key;
146}
akmhoquebd7c8e62014-02-01 14:57:40 -0600147
148bool
akmhoque3c6bd922014-02-01 17:10:17 -0600149Lsdb::buildAndInstallOwnCorLsa(nlsr& pnlsr){
150 CorLsa corLsa(pnlsr.getConfParameter().getRouterPrefix()
151 , 1
152 , pnlsr.getCorLsaSeq()+1
153 , pnlsr.getConfParameter().getRouterDeadInterval()
154 , pnlsr.getConfParameter().getCorR()
155 , pnlsr.getConfParameter().getCorTheta() );
156 pnlsr.setCorLsaSeq(pnlsr.getCorLsaSeq()+1);
157 //cout<<corLsa;
158 installCorLsa(corLsa);
159
160}
161
162CorLsa&
163Lsdb::getCorLsa(string key)
akmhoquebd7c8e62014-02-01 14:57:40 -0600164{
akmhoque3c6bd922014-02-01 17:10:17 -0600165 std::list< CorLsa >::iterator it = std::find_if( corLsdb.begin(),
166 corLsdb.end(),
167 bind(corLsaCompareByKey, _1, key));
168
169 if( it != corLsdb.end()){
170 return (*it);
171 }
172}
173
174bool
175Lsdb::installCorLsa(CorLsa &clsa)
176{
177 bool doesLsaExist_ = doesCorLsaExist(clsa.getLsaKey());
178 if ( !doesLsaExist_ )
179 {
180 // add cor LSA
181 addCorLsa(clsa);
182 }
183 else
184 {
185 // check for newer cor LSA
186 CorLsa oldCorLsa=getCorLsa(clsa.getLsaKey());
187
188 }
189
190 return true;
191}
192
193bool
194Lsdb::addCorLsa(CorLsa& clsa)
195{
196 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
197 corLsdb.end(),
198 bind(corLsaCompare, _1, clsa));
199
200 if( it == corLsdb.end()){
201 corLsdb.push_back(clsa);
202 return true;
203 }
akmhoquebd7c8e62014-02-01 14:57:40 -0600204 return false;
205}
206
207bool
akmhoque3c6bd922014-02-01 17:10:17 -0600208Lsdb::removeCorLsa(string& key)
209{
210
211}
212
213bool
akmhoquebd7c8e62014-02-01 14:57:40 -0600214Lsdb::doesCorLsaExist(string key)
215{
akmhoque3c6bd922014-02-01 17:10:17 -0600216 std::list<CorLsa >::iterator it = std::find_if( corLsdb.begin(),
217 corLsdb.end(),
218 bind(corLsaCompareByKey, _1, key));
219
220 if( it == corLsdb.end()){
221 return false;
222 }
223
224 return true;
akmhoquebd7c8e62014-02-01 14:57:40 -0600225}
akmhoque3c6bd922014-02-01 17:10:17 -0600226
227void
228Lsdb::printCorLsdb() //debugging
229{
230 for( std::list<CorLsa>::iterator it=corLsdb.begin();
231 it!= corLsdb.end() ; it++)
232 {
233 cout<< (*it) <<endl;
234 }
235}
236
237
238// Adj LSA and LSDB related function starts here
239
240static bool
241adjLsaCompare(AdjLsa& alsa1, AdjLsa& alsa2){
242 return alsa1.getLsaKey()==alsa1.getLsaKey();
243}
244
245static bool
246adjLsaCompareByKey(AdjLsa& alsa, string& key){
247 return alsa.getLsaKey()==key;
248}
249
250
251
252
253bool
254Lsdb::doesAdjLsaExist(string key)
255{
256 std::list< AdjLsa >::iterator it = std::find_if( adjLsdb.begin(),
257 adjLsdb.end(),
258 bind(adjLsaCompareByKey, _1, key));
259
260 if( it == adjLsdb.end()){
261 return false;
262 }
263
264 return true;
265}
266
267