blob: 2a11e41fbf49cfd0d5b4f273c1571615de28ec0b [file] [log] [blame]
akmhoque298385a2014-02-13 14:13:09 -06001#include<string>
2#include<iostream>
akmhoque2bb198e2014-02-28 11:46:27 -06003#include<sstream>
akmhoque298385a2014-02-13 14:13:09 -06004#include<algorithm>
5#include<cmath>
6#include<limits>
7
akmhoque2bb198e2014-02-28 11:46:27 -06008#include "nlsr.hpp"
akmhoque298385a2014-02-13 14:13:09 -06009#include "nlsr_lsa.hpp"
10#include "nlsr_npl.hpp"
11#include "nlsr_adjacent.hpp"
akmhoque2bb198e2014-02-28 11:46:27 -060012#include "utility/nlsr_tokenizer.hpp"
13
akmhoque298385a2014-02-13 14:13:09 -060014
akmhoque1fd8c1e2014-02-19 19:41:49 -060015namespace nlsr
akmhoque298385a2014-02-13 14:13:09 -060016{
akmhoque298385a2014-02-13 14:13:09 -060017
akmhoque1fd8c1e2014-02-19 19:41:49 -060018 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060019
akmhoque298385a2014-02-13 14:13:09 -060020
akmhoque1fd8c1e2014-02-19 19:41:49 -060021 string
22 NameLsa::getNameLsaKey()
23 {
24 string key;
25 key=origRouter + "/" + boost::lexical_cast<std::string>(1);
26 return key;
27 }
akmhoque298385a2014-02-13 14:13:09 -060028
akmhoque1fd8c1e2014-02-19 19:41:49 -060029 NameLsa::NameLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt, Npl npl)
30 {
31 origRouter=origR;
32 lsType=lst;
33 lsSeqNo=lsn;
34 lifeTime=lt;
akmhoque1fd8c1e2014-02-19 19:41:49 -060035 std::list<string> nl=npl.getNameList();
36 for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++)
37 {
38 addNameToLsa((*it));
39 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060040 }
41
42 string
43 NameLsa::getNameLsaData()
44 {
45 string nameLsaData;
akmhoque2bb198e2014-02-28 11:46:27 -060046 nameLsaData=origRouter + "|" + boost::lexical_cast<std::string>(1) + "|"
akmhoque1fd8c1e2014-02-19 19:41:49 -060047 + boost::lexical_cast<std::string>(lsSeqNo) + "|"
48 + boost::lexical_cast<std::string>(lifeTime);
49 nameLsaData+="|";
50 nameLsaData+=boost::lexical_cast<std::string>(npl.getNplSize());
akmhoque1fd8c1e2014-02-19 19:41:49 -060051 std::list<string> nl=npl.getNameList();
52 for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++)
53 {
54 nameLsaData+="|";
55 nameLsaData+=(*it);
56 }
akmhoque2bb198e2014-02-28 11:46:27 -060057 return nameLsaData+"|";
58 }
59
60 bool
61 NameLsa::initNameLsaFromContent(string content)
62 {
63 uint32_t numName=0;
64 nlsrTokenizer nt(content, "|");
65 origRouter=nt.getNextToken();
66 if(origRouter.empty())
67 {
68 return false;
69 }
70 try
71 {
72 lsType=boost::lexical_cast<uint8_t>(nt.getNextToken());
73 lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken());
74 lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken());
75 numName=boost::lexical_cast<uint32_t>(nt.getNextToken());
76 }
77 catch(std::exception &e)
78 {
79 return false;
80 }
81 for(int i=0; i<numName; i++)
82 {
83 string name=nt.getNextToken();
84 addNameToLsa(name);
85 }
86 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -060087 }
88
89 std::ostream&
90 operator<<(std::ostream& os, NameLsa& nLsa)
91 {
92 os<<"Name Lsa: "<<endl;
93 os<<" Origination Router: "<<nLsa.getOrigRouter()<<endl;
94 os<<" Ls Type: "<<(unsigned short)nLsa.getLsType()<<endl;
95 os<<" Ls Seq No: "<<(unsigned int)nLsa.getLsSeqNo()<<endl;
96 os<<" Ls Lifetime: "<<(unsigned int)nLsa.getLifeTime()<<endl;
97 os<<" Names: "<<endl;
98 int i=1;
99 std::list<string> nl=nLsa.getNpl().getNameList();
100 for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++)
101 {
102 os<<" Name "<<i<<": "<<(*it)<<endl;
103 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600104 return os;
105 }
akmhoque298385a2014-02-13 14:13:09 -0600106
107
108
akmhoque1fd8c1e2014-02-19 19:41:49 -0600109 CorLsa::CorLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt
110 , double r, double theta)
111 {
112 origRouter=origR;
113 lsType=lst;
114 lsSeqNo=lsn;
115 lifeTime=lt;
116 corRad=r;
117 corTheta=theta;
118 }
akmhoque298385a2014-02-13 14:13:09 -0600119
akmhoque1fd8c1e2014-02-19 19:41:49 -0600120 string
121 CorLsa::getCorLsaKey()
122 {
123 string key;
124 key=origRouter + "/" + boost::lexical_cast<std::string>(3);
125 return key;
126 }
akmhoque298385a2014-02-13 14:13:09 -0600127
akmhoque1fd8c1e2014-02-19 19:41:49 -0600128 bool
129 CorLsa::isLsaContentEqual(CorLsa& clsa)
130 {
131 return (std::abs(corRad - clsa.getCorRadius()) <
132 std::numeric_limits<double>::epsilon()) &&
133 (std::abs(corTheta - clsa.getCorTheta()) <
134 std::numeric_limits<double>::epsilon());
135 }
akmhoque298385a2014-02-13 14:13:09 -0600136
akmhoque1fd8c1e2014-02-19 19:41:49 -0600137 string
138 CorLsa::getCorLsaData()
139 {
140 string corLsaData;
141 corLsaData=origRouter + "|";
akmhoque2bb198e2014-02-28 11:46:27 -0600142 corLsaData+=(boost::lexical_cast<std::string>(3) + "|");
akmhoque1fd8c1e2014-02-19 19:41:49 -0600143 corLsaData+=(boost::lexical_cast<std::string>(lsSeqNo) + "|");
144 corLsaData+=(boost::lexical_cast<std::string>(lifeTime) + "|");
145 corLsaData+=(boost::lexical_cast<std::string>(corRad) + "|");
146 corLsaData+=(boost::lexical_cast<std::string>(corTheta) + "|");
akmhoque1fd8c1e2014-02-19 19:41:49 -0600147 return corLsaData;
148 }
akmhoque298385a2014-02-13 14:13:09 -0600149
akmhoque2bb198e2014-02-28 11:46:27 -0600150 bool
151 CorLsa::initCorLsaFromContent(string content)
152 {
153 nlsrTokenizer nt(content, "|");
154 origRouter=nt.getNextToken();
155 if(origRouter.empty())
156 {
157 return false;
158 }
159 try
160 {
161 lsType=boost::lexical_cast<uint8_t>(nt.getNextToken());
162 lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken());
163 lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken());
164 corRad=boost::lexical_cast<double>(nt.getNextToken());
165 corTheta=boost::lexical_cast<double>(nt.getNextToken());
166 }
167 catch(std::exception &e)
168 {
169 return false;
170 }
171 return true;
172 }
173
akmhoque1fd8c1e2014-02-19 19:41:49 -0600174 std::ostream&
175 operator<<(std::ostream& os, CorLsa& cLsa)
176 {
177 os<<"Cor Lsa: "<<endl;
178 os<<" Origination Router: "<<cLsa.getOrigRouter()<<endl;
179 os<<" Ls Type: "<<(unsigned short)cLsa.getLsType()<<endl;
180 os<<" Ls Seq No: "<<(unsigned int)cLsa.getLsSeqNo()<<endl;
181 os<<" Ls Lifetime: "<<(unsigned int)cLsa.getLifeTime()<<endl;
182 os<<" Hyperbolic Radius: "<<cLsa.getCorRadius()<<endl;
183 os<<" Hyperbolic Theta: "<<cLsa.getCorTheta()<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600184 return os;
185 }
akmhoque298385a2014-02-13 14:13:09 -0600186
187
akmhoque1fd8c1e2014-02-19 19:41:49 -0600188 AdjLsa::AdjLsa(string origR, uint8_t lst, uint32_t lsn, uint32_t lt,
189 uint32_t nl ,Adl padl)
190 {
191 origRouter=origR;
192 lsType=lst;
193 lsSeqNo=lsn;
194 lifeTime=lt;
195 noLink=nl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600196 std::list<Adjacent> al=padl.getAdjList();
197 for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++)
198 {
199 if((*it).getStatus()==1)
200 {
201 addAdjacentToLsa((*it));
202 }
203 }
204 }
akmhoque298385a2014-02-13 14:13:09 -0600205
akmhoque1fd8c1e2014-02-19 19:41:49 -0600206 string
207 AdjLsa::getAdjLsaKey()
208 {
209 string key;
210 key=origRouter + "/" + boost::lexical_cast<std::string>(2);
211 return key;
212 }
akmhoque298385a2014-02-13 14:13:09 -0600213
akmhoque1fd8c1e2014-02-19 19:41:49 -0600214 bool
215 AdjLsa::isLsaContentEqual(AdjLsa& alsa)
216 {
217 return adl.isAdlEqual(alsa.getAdl());
218 }
akmhoque298385a2014-02-13 14:13:09 -0600219
220
akmhoque1fd8c1e2014-02-19 19:41:49 -0600221 string
222 AdjLsa::getAdjLsaData()
223 {
224 string adjLsaData;
akmhoque2bb198e2014-02-28 11:46:27 -0600225 adjLsaData=origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
akmhoque1fd8c1e2014-02-19 19:41:49 -0600226 + boost::lexical_cast<std::string>(lsSeqNo) + "|"
227 + boost::lexical_cast<std::string>(lifeTime);
228 adjLsaData+="|";
229 adjLsaData+=boost::lexical_cast<std::string>(adl.getAdlSize());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600230 std::list<Adjacent> al=adl.getAdjList();
231 for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++)
232 {
233 adjLsaData+="|";
234 adjLsaData+=(*it).getAdjacentName();
235 adjLsaData+="|";
236 adjLsaData+=boost::lexical_cast<std::string>((*it).getConnectingFace());
237 adjLsaData+="|";
238 adjLsaData+=boost::lexical_cast<std::string>((*it).getLinkCost());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600239 }
akmhoque2bb198e2014-02-28 11:46:27 -0600240 return adjLsaData+"|";
241 }
242
243 bool
244 AdjLsa::initAdjLsaFromContent(string content)
245 {
246 uint32_t numLink=0;
247 nlsrTokenizer nt(content, "|");
248 origRouter=nt.getNextToken();
249 if(origRouter.empty())
250 {
251 return false;
252 }
253 try
254 {
255 lsType=boost::lexical_cast<uint8_t>(nt.getNextToken());
256 lsSeqNo=boost::lexical_cast<uint32_t>(nt.getNextToken());
257 lifeTime=boost::lexical_cast<uint32_t>(nt.getNextToken());
258 numLink=boost::lexical_cast<uint32_t>(nt.getNextToken());
259 }
260 catch(std::exception &e)
261 {
262 return false;
263 }
264 for(int i=0; i< numLink; i++)
265 {
266 try
267 {
268 string adjName=nt.getNextToken();
269 int connectingFace=boost::lexical_cast<int>(nt.getNextToken());
270 double linkCost=boost::lexical_cast<double>(nt.getNextToken());
271 Adjacent adjacent(adjName, connectingFace, linkCost, 0, 0);
272 addAdjacentToLsa(adjacent);
273 }
274 catch( std::exception &e )
275 {
276 return false;
277 }
278 }
279 return true;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600280 }
akmhoque298385a2014-02-13 14:13:09 -0600281
282
akmhoque1fd8c1e2014-02-19 19:41:49 -0600283 void
284 AdjLsa::addNptEntriesForAdjLsa(Nlsr& pnlsr)
285 {
286 if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
287 {
288 pnlsr.getNpt().addNpte(getOrigRouter(), getOrigRouter(),pnlsr);
289 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600290 }
akmhoque298385a2014-02-13 14:13:09 -0600291
292
akmhoque1fd8c1e2014-02-19 19:41:49 -0600293 void
294 AdjLsa::removeNptEntriesForAdjLsa(Nlsr& pnlsr)
295 {
296 if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
297 {
298 pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(),pnlsr);
299 }
300 }
akmhoque298385a2014-02-13 14:13:09 -0600301
302
303
akmhoque1fd8c1e2014-02-19 19:41:49 -0600304 std::ostream&
305 operator<<(std::ostream& os, AdjLsa& aLsa)
306 {
307 os<<"Adj Lsa: "<<endl;
308 os<<" Origination Router: "<<aLsa.getOrigRouter()<<endl;
309 os<<" Ls Type: "<<(unsigned short)aLsa.getLsType()<<endl;
310 os<<" Ls Seq No: "<<(unsigned int)aLsa.getLsSeqNo()<<endl;
311 os<<" Ls Lifetime: "<<(unsigned int)aLsa.getLifeTime()<<endl;
312 os<<" No Link: "<<(unsigned int)aLsa.getNoLink()<<endl;
313 os<<" Adjacents: "<<endl;
314 int i=1;
315 std::list<Adjacent> al=aLsa.getAdl().getAdjList();
316 for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++)
317 {
318 os<<" Adjacent "<<i<<": "<<endl;
319 os<<" Adjacent Name: "<<(*it).getAdjacentName()<<endl;
320 os<<" Connecting Face: "<<(*it).getConnectingFace()<<endl;
321 os<<" Link Cost: "<<(*it).getLinkCost()<<endl;
322 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600323 return os;
324 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600325
326}//namespace nlsr