blob: dbb6ee51cc7ad704d0c810e57cddcef05bd42c60 [file] [log] [blame]
akmhoque53353462014-04-22 08:43:45 -05001#include <string>
2#include <iostream>
3#include <sstream>
4#include <algorithm>
5#include <cmath>
6#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -05007#include <boost/tokenizer.hpp>
8#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -05009
10#include "nlsr.hpp"
11#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050012#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050013#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050014#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050015
16
17namespace nlsr {
18
akmhoque674b0b12014-05-20 14:33:28 -050019INIT_LOGGER("Lsa");
20
akmhoque53353462014-04-22 08:43:45 -050021using namespace std;
22
akmhoque31d1d4b2014-05-05 22:08:14 -050023const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050024NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050025{
akmhoque31d1d4b2014-05-05 22:08:14 -050026 ndn::Name key = m_origRouter;
27 key.append("name");
akmhoque53353462014-04-22 08:43:45 -050028 return key;
29}
30
akmhoque31d1d4b2014-05-05 22:08:14 -050031NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
32 uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -050033 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050034{
35 m_origRouter = origR;
36 m_lsType = lst;
37 m_lsSeqNo = lsn;
38 m_lifeTime = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050039 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050040 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050041 addName((*it));
42 }
43}
44
45string
46NameLsa::getData()
47{
48 string nameLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -050049 nameLsaData = m_origRouter.toUri() + "|" + "name" + "|"
akmhoque53353462014-04-22 08:43:45 -050050 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
51 + boost::lexical_cast<std::string>(m_lifeTime);
52 nameLsaData += "|";
53 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050054 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050055 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050056 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050057 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050058 }
59 return nameLsaData + "|";
60}
61
62bool
akmhoque31d1d4b2014-05-05 22:08:14 -050063NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050064{
65 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050066 boost::char_separator<char> sep("|");
67 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
68 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
69 tokens.begin();
70 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050071 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050072 return false;
73 }
akmhoque31d1d4b2014-05-05 22:08:14 -050074 try {
75 m_lsType = *tok_iter++;
76 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
77 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
78 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -050079 }
akmhoque31d1d4b2014-05-05 22:08:14 -050080 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -050081 return false;
82 }
akmhoque157b0a42014-05-13 00:26:37 -050083 for (uint32_t i = 0; i < numName; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -050084 string name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -050085 addName(name);
86 }
87 return true;
88}
89
90void
91NameLsa::writeLog()
92{
akmhoque674b0b12014-05-20 14:33:28 -050093 _LOG_DEBUG("Name Lsa: ");
94 _LOG_DEBUG(" Origination Router: " << m_origRouter);
95 _LOG_DEBUG(" Ls Type: " << m_lsType);
96 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
97 _LOG_DEBUG(" Ls Lifetime: " << m_lifeTime);
98 _LOG_DEBUG(" Names: ");
99 int i = 1;
100 std::list<ndn::Name> nl = m_npl.getNameList();
101 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
102 {
103 _LOG_DEBUG(" Name " << i << ": " << (*it));
104 }
akmhoque53353462014-04-22 08:43:45 -0500105}
106
107std::ostream&
108operator<<(std::ostream& os, NameLsa& nLsa)
109{
110 os << "Name Lsa: " << endl;
111 os << " Origination Router: " << nLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500112 os << " Ls Type: " << nLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -0500113 os << " Ls Seq No: " << (unsigned int)nLsa.getLsSeqNo() << endl;
114 os << " Ls Lifetime: " << (unsigned int)nLsa.getLifeTime() << endl;
115 os << " Names: " << endl;
116 int i = 1;
akmhoque31d1d4b2014-05-05 22:08:14 -0500117 std::list<ndn::Name> nl = nLsa.getNpl().getNameList();
118 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
akmhoque53353462014-04-22 08:43:45 -0500119 {
120 os << " Name " << i << ": " << (*it) << endl;
121 }
122 return os;
123}
124
125
126
akmhoque31d1d4b2014-05-05 22:08:14 -0500127CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
128 uint32_t lsn,
akmhoqueb6450b12014-04-24 00:01:03 -0500129 uint32_t lt
130 , double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500131{
132 m_origRouter = origR;
133 m_lsType = lst;
134 m_lsSeqNo = lsn;
135 m_lifeTime = lt;
136 m_corRad = r;
137 m_corTheta = theta;
138}
139
akmhoque31d1d4b2014-05-05 22:08:14 -0500140const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500141CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500142{
akmhoque31d1d4b2014-05-05 22:08:14 -0500143 ndn::Name key = m_origRouter;
144 key.append("coordinate");
akmhoque53353462014-04-22 08:43:45 -0500145 return key;
146}
147
148bool
akmhoquefdbddb12014-05-02 18:35:19 -0500149CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500150{
151 return (std::abs(m_corRad - clsa.getCorRadius()) <
152 std::numeric_limits<double>::epsilon()) &&
153 (std::abs(m_corTheta - clsa.getCorTheta()) <
154 std::numeric_limits<double>::epsilon());
155}
156
157string
akmhoqueb6450b12014-04-24 00:01:03 -0500158CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500159{
160 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500161 corLsaData = m_origRouter.toUri() + "|";
162 corLsaData += "coordinate";
163 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500164 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
165 corLsaData += (boost::lexical_cast<std::string>(m_lifeTime) + "|");
166 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
167 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
168 return corLsaData;
169}
170
171bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500172CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500173{
akmhoque31d1d4b2014-05-05 22:08:14 -0500174 boost::char_separator<char> sep("|");
175 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
176 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
177 tokens.begin();
178 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500179 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500180 return false;
181 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500182 try {
183 m_lsType = *tok_iter++;
184 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
185 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
186 m_corRad = boost::lexical_cast<double>(*tok_iter++);
187 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500188 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500190 return false;
191 }
192 return true;
193}
194
akmhoque674b0b12014-05-20 14:33:28 -0500195void
196CoordinateLsa::writeLog()
197{
198 _LOG_DEBUG("Cor Lsa: ");
199 _LOG_DEBUG(" Origination Router: " << m_origRouter);
200 _LOG_DEBUG(" Ls Type: " << m_lsType);
201 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
202 _LOG_DEBUG(" Ls Lifetime: " << m_lifeTime);
203 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
204 _LOG_DEBUG(" Hyperbolic Theta: " << m_corRad);
205}
206
akmhoque53353462014-04-22 08:43:45 -0500207std::ostream&
akmhoqueb6450b12014-04-24 00:01:03 -0500208operator<<(std::ostream& os, const CoordinateLsa& cLsa)
akmhoque53353462014-04-22 08:43:45 -0500209{
210 os << "Cor Lsa: " << endl;
211 os << " Origination Router: " << cLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500212 os << " Ls Type: " << cLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -0500213 os << " Ls Seq No: " << (unsigned int)cLsa.getLsSeqNo() << endl;
214 os << " Ls Lifetime: " << (unsigned int)cLsa.getLifeTime() << endl;
215 os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
216 os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl;
217 return os;
218}
219
220
akmhoque31d1d4b2014-05-05 22:08:14 -0500221AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
222 uint32_t lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500223 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500224{
225 m_origRouter = origR;
226 m_lsType = lst;
227 m_lsSeqNo = lsn;
228 m_lifeTime = lt;
229 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500230 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500231 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
232 if ((*it).getStatus() == 1) {
akmhoque53353462014-04-22 08:43:45 -0500233 addAdjacent((*it));
234 }
235 }
236}
237
akmhoque31d1d4b2014-05-05 22:08:14 -0500238const ndn::Name
239AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500240{
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 ndn::Name key = m_origRouter;
242 key.append("adjacency");
akmhoque53353462014-04-22 08:43:45 -0500243 return key;
244}
245
246bool
akmhoquefdbddb12014-05-02 18:35:19 -0500247AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500248{
akmhoquefdbddb12014-05-02 18:35:19 -0500249 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500250}
251
252
253string
254AdjLsa::getData()
255{
256 string adjLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500257 adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|"
akmhoque53353462014-04-22 08:43:45 -0500258 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
259 + boost::lexical_cast<std::string>(m_lifeTime);
260 adjLsaData += "|";
261 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
262 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500263 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500264 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500265 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500266 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500267 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500268 adjLsaData += "|";
269 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
270 }
271 return adjLsaData + "|";
272}
273
274bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500275AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500276{
277 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500278 boost::char_separator<char> sep("|");
279 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
280 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
281 tokens.begin();
282 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500283 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500284 return false;
285 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500286 try {
287 m_lsType = *tok_iter++;
288 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
289 m_lifeTime = boost::lexical_cast<uint32_t>(*tok_iter++);
290 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500291 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500292 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500293 return false;
294 }
akmhoque157b0a42014-05-13 00:26:37 -0500295 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500296 try {
297 string adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500298 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500299 double linkCost = boost::lexical_cast<double>(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500300 Adjacent adjacent(adjName, connectingFaceUri, linkCost, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500301 addAdjacent(adjacent);
302 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500303 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500304 return false;
305 }
306 }
307 return true;
308}
309
310
311void
312AdjLsa::addNptEntries(Nlsr& pnlsr)
313{
akmhoque157b0a42014-05-13 00:26:37 -0500314 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500315 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500316 }
317}
318
319
320void
321AdjLsa::removeNptEntries(Nlsr& pnlsr)
322{
akmhoque157b0a42014-05-13 00:26:37 -0500323 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500324 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500325 }
326}
327
akmhoque674b0b12014-05-20 14:33:28 -0500328void
329AdjLsa::writeLog()
330{
331 _LOG_DEBUG("Adj Lsa: ");
332 _LOG_DEBUG(" Origination Router: " << m_origRouter);
333 _LOG_DEBUG(" Ls Type: " << m_lsType);
334 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
335 _LOG_DEBUG(" Ls Lifetime: " << m_lifeTime);
336 _LOG_DEBUG(" Adjacents: ");
337 int i = 1;
338 std::list<Adjacent> al = m_adl.getAdjList();
339 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
340 {
341 _LOG_DEBUG(" Adjacent " << i << ": ");;
342 _LOG_DEBUG(" Adjacent Name: " << (*it).getName());
343 _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri());
344 _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost());
345 }
346}
akmhoque53353462014-04-22 08:43:45 -0500347
348std::ostream&
349operator<<(std::ostream& os, AdjLsa& aLsa)
350{
351 os << "Adj Lsa: " << endl;
352 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500353 os << " Ls Type: " << aLsa.getLsType() << endl;
akmhoque53353462014-04-22 08:43:45 -0500354 os << " Ls Seq No: " << (unsigned int)aLsa.getLsSeqNo() << endl;
355 os << " Ls Lifetime: " << (unsigned int)aLsa.getLifeTime() << endl;
356 os << " No Link: " << (unsigned int)aLsa.getNoLink() << endl;
357 os << " Adjacents: " << endl;
358 int i = 1;
359 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
360 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
361 {
362 os << " Adjacent " << i << ": " << endl;
363 os << " Adjacent Name: " << (*it).getName() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500364 os << " Connecting FaceUri: " << (*it).getConnectingFaceUri() << endl;
akmhoque53353462014-04-22 08:43:45 -0500365 os << " Link Cost: " << (*it).getLinkCost() << endl;
366 }
367 return os;
368}
369
370}//namespace nlsr