blob: 4354393968d3e5256c47b6162a10c28a87478cf5 [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
akmhoque5a44dd42014-03-12 18:11:32 -050018 using namespace std;
akmhoque298385a2014-02-13 14:13:09 -060019
akmhoque298385a2014-02-13 14:13:09 -060020
akmhoque5a44dd42014-03-12 18:11:32 -050021 string
22 NameLsa::getNameLsaKey()
23 {
24 string key;
25 key=origRouter + "/" + boost::lexical_cast<std::string>(1);
26 return key;
27 }
28
29 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;
35 std::list<string> nl=npl.getNameList();
36 for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -060037 {
akmhoque5a44dd42014-03-12 18:11:32 -050038 addNameToLsa((*it));
akmhoque1fd8c1e2014-02-19 19:41:49 -060039 }
akmhoque5a44dd42014-03-12 18:11:32 -050040 }
akmhoque298385a2014-02-13 14:13:09 -060041
akmhoque5a44dd42014-03-12 18:11:32 -050042 string
43 NameLsa::getNameLsaData()
44 {
45 string nameLsaData;
46 nameLsaData=origRouter + "|" + boost::lexical_cast<std::string>(1) + "|"
47 + boost::lexical_cast<std::string>(lsSeqNo) + "|"
48 + boost::lexical_cast<std::string>(lifeTime);
49 nameLsaData+="|";
50 nameLsaData+=boost::lexical_cast<std::string>(npl.getNplSize());
51 std::list<string> nl=npl.getNameList();
52 for( std::list<string>::iterator it=nl.begin(); it != nl.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -060053 {
akmhoque5a44dd42014-03-12 18:11:32 -050054 nameLsaData+="|";
55 nameLsaData+=(*it);
akmhoque1fd8c1e2014-02-19 19:41:49 -060056 }
akmhoque5a44dd42014-03-12 18:11:32 -050057 return nameLsaData+"|";
58 }
akmhoque1fd8c1e2014-02-19 19:41:49 -060059
akmhoque5a44dd42014-03-12 18:11:32 -050060 bool
61 NameLsa::initNameLsaFromContent(string content)
62 {
63 uint32_t numName=0;
64 nlsrTokenizer nt(content, "|");
65 origRouter=nt.getNextToken();
66 if(origRouter.empty())
akmhoque1fd8c1e2014-02-19 19:41:49 -060067 {
akmhoque5a44dd42014-03-12 18:11:32 -050068 return false;
akmhoque2bb198e2014-02-28 11:46:27 -060069 }
akmhoque5a44dd42014-03-12 18:11:32 -050070 try
akmhoque2bb198e2014-02-28 11:46:27 -060071 {
akmhoque5a44dd42014-03-12 18:11:32 -050072 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());
akmhoque1fd8c1e2014-02-19 19:41:49 -060076 }
akmhoque5a44dd42014-03-12 18:11:32 -050077 catch(std::exception &e)
akmhoque1fd8c1e2014-02-19 19:41:49 -060078 {
akmhoque5a44dd42014-03-12 18:11:32 -050079 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -060080 }
akmhoque5a44dd42014-03-12 18:11:32 -050081 for(int i=0; i<numName; i++)
akmhoque1fd8c1e2014-02-19 19:41:49 -060082 {
akmhoque5a44dd42014-03-12 18:11:32 -050083 string name=nt.getNextToken();
84 addNameToLsa(name);
akmhoque1fd8c1e2014-02-19 19:41:49 -060085 }
akmhoque5a44dd42014-03-12 18:11:32 -050086 return true;
87 }
akmhoque298385a2014-02-13 14:13:09 -060088
akmhoque5a44dd42014-03-12 18:11:32 -050089 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++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600101 {
akmhoque5a44dd42014-03-12 18:11:32 -0500102 os<<" Name "<<i<<": "<<(*it)<<endl;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600103 }
akmhoque5a44dd42014-03-12 18:11:32 -0500104 return os;
105 }
akmhoque298385a2014-02-13 14:13:09 -0600106
akmhoque5a44dd42014-03-12 18:11:32 -0500107
108
109 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 }
119
120 string
121 CorLsa::getCorLsaKey()
122 {
123 string key;
124 key=origRouter + "/" + boost::lexical_cast<std::string>(3);
125 return key;
126 }
127
128 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 }
136
137 string
138 CorLsa::getCorLsaData()
139 {
140 string corLsaData;
141 corLsaData=origRouter + "|";
142 corLsaData+=(boost::lexical_cast<std::string>(3) + "|");
143 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) + "|");
147 return corLsaData;
148 }
149
150 bool
151 CorLsa::initCorLsaFromContent(string content)
152 {
153 nlsrTokenizer nt(content, "|");
154 origRouter=nt.getNextToken();
155 if(origRouter.empty())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600156 {
akmhoque5a44dd42014-03-12 18:11:32 -0500157 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600158 }
akmhoque5a44dd42014-03-12 18:11:32 -0500159 try
akmhoque1fd8c1e2014-02-19 19:41:49 -0600160 {
akmhoque5a44dd42014-03-12 18:11:32 -0500161 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());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600166 }
akmhoque5a44dd42014-03-12 18:11:32 -0500167 catch(std::exception &e)
akmhoque2bb198e2014-02-28 11:46:27 -0600168 {
akmhoque5a44dd42014-03-12 18:11:32 -0500169 return false;
akmhoque2bb198e2014-02-28 11:46:27 -0600170 }
akmhoque5a44dd42014-03-12 18:11:32 -0500171 return true;
172 }
akmhoque2bb198e2014-02-28 11:46:27 -0600173
akmhoque5a44dd42014-03-12 18:11:32 -0500174 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;
184 return os;
185 }
186
187
188 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;
196 std::list<Adjacent> al=padl.getAdjList();
197 for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600198 {
akmhoque5a44dd42014-03-12 18:11:32 -0500199 if((*it).getStatus()==1)
200 {
201 addAdjacentToLsa((*it));
202 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600203 }
akmhoque5a44dd42014-03-12 18:11:32 -0500204 }
205
206 string
207 AdjLsa::getAdjLsaKey()
208 {
209 string key;
210 key=origRouter + "/" + boost::lexical_cast<std::string>(2);
211 return key;
212 }
213
214 bool
215 AdjLsa::isLsaContentEqual(AdjLsa& alsa)
216 {
217 return adl.isAdlEqual(alsa.getAdl());
218 }
akmhoque298385a2014-02-13 14:13:09 -0600219
220
akmhoque5a44dd42014-03-12 18:11:32 -0500221 string
222 AdjLsa::getAdjLsaData()
223 {
224 string adjLsaData;
225 adjLsaData=origRouter + "|" + boost::lexical_cast<std::string>(2) + "|"
226 + boost::lexical_cast<std::string>(lsSeqNo) + "|"
227 + boost::lexical_cast<std::string>(lifeTime);
228 adjLsaData+="|";
229 adjLsaData+=boost::lexical_cast<std::string>(adl.getAdlSize());
230 std::list<Adjacent> al=adl.getAdjList();
231 for( std::list<Adjacent>::iterator it=al.begin(); it != al.end(); it++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600232 {
akmhoque5a44dd42014-03-12 18:11:32 -0500233 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 }
akmhoque5a44dd42014-03-12 18:11:32 -0500240 return adjLsaData+"|";
241 }
akmhoque298385a2014-02-13 14:13:09 -0600242
akmhoque5a44dd42014-03-12 18:11:32 -0500243 bool
244 AdjLsa::initAdjLsaFromContent(string content)
245 {
246 uint32_t numLink=0;
247 nlsrTokenizer nt(content, "|");
248 origRouter=nt.getNextToken();
249 if(origRouter.empty())
akmhoque1fd8c1e2014-02-19 19:41:49 -0600250 {
akmhoque5a44dd42014-03-12 18:11:32 -0500251 return false;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600252 }
akmhoque5a44dd42014-03-12 18:11:32 -0500253 try
akmhoque1fd8c1e2014-02-19 19:41:49 -0600254 {
akmhoque5a44dd42014-03-12 18:11:32 -0500255 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());
akmhoque1fd8c1e2014-02-19 19:41:49 -0600259 }
akmhoque5a44dd42014-03-12 18:11:32 -0500260 catch(std::exception &e)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600261 {
akmhoque5a44dd42014-03-12 18:11:32 -0500262 return false;
akmhoque2bb198e2014-02-28 11:46:27 -0600263 }
akmhoque5a44dd42014-03-12 18:11:32 -0500264 for(int i=0; i< numLink; i++)
akmhoque2bb198e2014-02-28 11:46:27 -0600265 {
akmhoque5a44dd42014-03-12 18:11:32 -0500266 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 }
akmhoque1fd8c1e2014-02-19 19:41:49 -0600278 }
akmhoque5a44dd42014-03-12 18:11:32 -0500279 return true;
280 }
akmhoque298385a2014-02-13 14:13:09 -0600281
282
akmhoque5a44dd42014-03-12 18:11:32 -0500283 void
284 AdjLsa::addNptEntriesForAdjLsa(Nlsr& pnlsr)
285 {
286 if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600287 {
akmhoque5a44dd42014-03-12 18:11:32 -0500288 pnlsr.getNpt().addNpteByDestName(getOrigRouter(), getOrigRouter(),pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600289 }
akmhoque5a44dd42014-03-12 18:11:32 -0500290 }
akmhoque298385a2014-02-13 14:13:09 -0600291
292
akmhoque5a44dd42014-03-12 18:11:32 -0500293 void
294 AdjLsa::removeNptEntriesForAdjLsa(Nlsr& pnlsr)
295 {
296 if ( getOrigRouter() !=pnlsr.getConfParameter().getRouterPrefix() )
akmhoque1fd8c1e2014-02-19 19:41:49 -0600297 {
akmhoque5a44dd42014-03-12 18:11:32 -0500298 pnlsr.getNpt().removeNpte(getOrigRouter(), getOrigRouter(),pnlsr);
akmhoque1fd8c1e2014-02-19 19:41:49 -0600299 }
akmhoque5a44dd42014-03-12 18:11:32 -0500300 }
akmhoque298385a2014-02-13 14:13:09 -0600301
302
303
akmhoque5a44dd42014-03-12 18:11:32 -0500304 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++)
akmhoque1fd8c1e2014-02-19 19:41:49 -0600317 {
akmhoque5a44dd42014-03-12 18:11:32 -0500318 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;
akmhoque1fd8c1e2014-02-19 19:41:49 -0600322 }
akmhoque5a44dd42014-03-12 18:11:32 -0500323 return os;
324 }
akmhoqueb1710aa2014-02-19 17:13:36 -0600325
326}//namespace nlsr