blob: 9c3bf4472ad0303e0daf5fc533d3fa1e6f17719b [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014 University of Memphis,
4 * Regents of the University of California
5 *
6 * This file is part of NLSR (Named-data Link State Routing).
7 * See AUTHORS.md for complete list of NLSR authors and contributors.
8 *
9 * NLSR is free software: you can redistribute it and/or modify it under the terms
10 * of the GNU General Public License as published by the Free Software Foundation,
11 * either version 3 of the License, or (at your option) any later version.
12 *
13 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
14 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * \author A K M Mahmudul Hoque <ahoque1@memphis.edu>
21 *
22 **/
akmhoque53353462014-04-22 08:43:45 -050023#include <string>
24#include <iostream>
25#include <sstream>
26#include <algorithm>
27#include <cmath>
28#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -050029#include <boost/tokenizer.hpp>
30#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050031
32#include "nlsr.hpp"
33#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050034#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050035#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050036#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050037
38
39namespace nlsr {
40
akmhoque674b0b12014-05-20 14:33:28 -050041INIT_LOGGER("Lsa");
42
akmhoque53353462014-04-22 08:43:45 -050043using namespace std;
44
akmhoque31d1d4b2014-05-05 22:08:14 -050045const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050046NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050047{
akmhoque31d1d4b2014-05-05 22:08:14 -050048 ndn::Name key = m_origRouter;
49 key.append("name");
akmhoque53353462014-04-22 08:43:45 -050050 return key;
51}
52
akmhoque31d1d4b2014-05-05 22:08:14 -050053NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050054 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050055 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050056{
57 m_origRouter = origR;
58 m_lsType = lst;
59 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050060 m_expirationTimePoint = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050061 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050062 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050063 addName((*it));
64 }
65}
66
67string
68NameLsa::getData()
69{
70 string nameLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -050071 nameLsaData = m_origRouter.toUri() + "|" + "name" + "|"
akmhoque53353462014-04-22 08:43:45 -050072 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -050073 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -050074 nameLsaData += "|";
75 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050076 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050077 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050078 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050079 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050080 }
81 return nameLsaData + "|";
82}
83
84bool
akmhoque31d1d4b2014-05-05 22:08:14 -050085NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050086{
87 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050088 boost::char_separator<char> sep("|");
89 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
90 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
91 tokens.begin();
92 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050093 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050094 return false;
95 }
akmhoque31d1d4b2014-05-05 22:08:14 -050096 try {
97 m_lsType = *tok_iter++;
98 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -050099 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500101 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500102 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500103 return false;
104 }
akmhoque157b0a42014-05-13 00:26:37 -0500105 for (uint32_t i = 0; i < numName; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 string name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500107 addName(name);
108 }
109 return true;
110}
111
112void
113NameLsa::writeLog()
114{
akmhoque674b0b12014-05-20 14:33:28 -0500115 _LOG_DEBUG("Name Lsa: ");
116 _LOG_DEBUG(" Origination Router: " << m_origRouter);
117 _LOG_DEBUG(" Ls Type: " << m_lsType);
118 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500119 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500120 _LOG_DEBUG(" Names: ");
121 int i = 1;
122 std::list<ndn::Name> nl = m_npl.getNameList();
123 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
124 {
125 _LOG_DEBUG(" Name " << i << ": " << (*it));
126 }
akmhoque53353462014-04-22 08:43:45 -0500127}
128
129std::ostream&
130operator<<(std::ostream& os, NameLsa& nLsa)
131{
132 os << "Name Lsa: " << endl;
133 os << " Origination Router: " << nLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500134 os << " Ls Type: " << nLsa.getLsType() << endl;
akmhoquec7a79b22014-05-26 08:06:19 -0500135 os << " Ls Seq No: " << nLsa.getLsSeqNo() << endl;
136 os << " Ls Lifetime: " << nLsa.getExpirationTimePoint() << endl;
akmhoque53353462014-04-22 08:43:45 -0500137 os << " Names: " << endl;
138 int i = 1;
akmhoque31d1d4b2014-05-05 22:08:14 -0500139 std::list<ndn::Name> nl = nLsa.getNpl().getNameList();
140 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
akmhoque53353462014-04-22 08:43:45 -0500141 {
142 os << " Name " << i << ": " << (*it) << endl;
143 }
144 return os;
145}
146
147
148
akmhoque31d1d4b2014-05-05 22:08:14 -0500149CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
150 uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500151 const ndn::time::system_clock::TimePoint& lt,
152 double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500153{
154 m_origRouter = origR;
155 m_lsType = lst;
156 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500157 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500158 m_corRad = r;
159 m_corTheta = theta;
160}
161
akmhoque31d1d4b2014-05-05 22:08:14 -0500162const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500163CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500164{
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 ndn::Name key = m_origRouter;
166 key.append("coordinate");
akmhoque53353462014-04-22 08:43:45 -0500167 return key;
168}
169
170bool
akmhoquefdbddb12014-05-02 18:35:19 -0500171CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500172{
173 return (std::abs(m_corRad - clsa.getCorRadius()) <
174 std::numeric_limits<double>::epsilon()) &&
175 (std::abs(m_corTheta - clsa.getCorTheta()) <
176 std::numeric_limits<double>::epsilon());
177}
178
179string
akmhoqueb6450b12014-04-24 00:01:03 -0500180CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500181{
182 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 corLsaData = m_origRouter.toUri() + "|";
184 corLsaData += "coordinate";
185 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500186 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
akmhoquec7a79b22014-05-26 08:06:19 -0500187 corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|");
akmhoque53353462014-04-22 08:43:45 -0500188 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
189 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
190 return corLsaData;
191}
192
193bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500194CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500195{
akmhoque31d1d4b2014-05-05 22:08:14 -0500196 boost::char_separator<char> sep("|");
197 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
198 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
199 tokens.begin();
200 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500201 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500202 return false;
203 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500204 try {
205 m_lsType = *tok_iter++;
206 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500207 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 m_corRad = boost::lexical_cast<double>(*tok_iter++);
209 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500210 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500211 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500212 return false;
213 }
214 return true;
215}
216
akmhoque674b0b12014-05-20 14:33:28 -0500217void
218CoordinateLsa::writeLog()
219{
220 _LOG_DEBUG("Cor Lsa: ");
221 _LOG_DEBUG(" Origination Router: " << m_origRouter);
222 _LOG_DEBUG(" Ls Type: " << m_lsType);
223 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500224 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500225 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
226 _LOG_DEBUG(" Hyperbolic Theta: " << m_corRad);
227}
228
akmhoque53353462014-04-22 08:43:45 -0500229std::ostream&
akmhoqueb6450b12014-04-24 00:01:03 -0500230operator<<(std::ostream& os, const CoordinateLsa& cLsa)
akmhoque53353462014-04-22 08:43:45 -0500231{
232 os << "Cor Lsa: " << endl;
233 os << " Origination Router: " << cLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500234 os << " Ls Type: " << cLsa.getLsType() << endl;
akmhoquec7a79b22014-05-26 08:06:19 -0500235 os << " Ls Seq No: " << cLsa.getLsSeqNo() << endl;
236 os << " Ls Lifetime: " << cLsa.getExpirationTimePoint() << endl;
akmhoque53353462014-04-22 08:43:45 -0500237 os << " Hyperbolic Radius: " << cLsa.getCorRadius() << endl;
238 os << " Hyperbolic Theta: " << cLsa.getCorTheta() << endl;
239 return os;
240}
241
242
akmhoque31d1d4b2014-05-05 22:08:14 -0500243AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500244 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500245 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500246{
247 m_origRouter = origR;
248 m_lsType = lst;
249 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500250 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500251 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500252 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500253 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
254 if ((*it).getStatus() == 1) {
akmhoque53353462014-04-22 08:43:45 -0500255 addAdjacent((*it));
256 }
257 }
258}
259
akmhoque31d1d4b2014-05-05 22:08:14 -0500260const ndn::Name
261AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500262{
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 ndn::Name key = m_origRouter;
264 key.append("adjacency");
akmhoque53353462014-04-22 08:43:45 -0500265 return key;
266}
267
268bool
akmhoquefdbddb12014-05-02 18:35:19 -0500269AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500270{
akmhoquefdbddb12014-05-02 18:35:19 -0500271 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500272}
273
274
275string
276AdjLsa::getData()
277{
278 string adjLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 adjLsaData = m_origRouter.toUri() + "|" + "adjacency" + "|"
akmhoque53353462014-04-22 08:43:45 -0500280 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500281 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500282 adjLsaData += "|";
283 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
284 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500285 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500286 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500287 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500288 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500289 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500290 adjLsaData += "|";
291 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
292 }
293 return adjLsaData + "|";
294}
295
296bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500297AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500298{
299 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500300 boost::char_separator<char> sep("|");
301 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
302 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
303 tokens.begin();
304 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500305 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500306 return false;
307 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500308 try {
309 m_lsType = *tok_iter++;
310 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500311 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500312 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500313 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500314 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500315 return false;
316 }
akmhoque157b0a42014-05-13 00:26:37 -0500317 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500318 try {
319 string adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500320 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500321 double linkCost = boost::lexical_cast<double>(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500322 Adjacent adjacent(adjName, connectingFaceUri, linkCost, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500323 addAdjacent(adjacent);
324 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500325 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500326 return false;
327 }
328 }
329 return true;
330}
331
332
333void
334AdjLsa::addNptEntries(Nlsr& pnlsr)
335{
akmhoque157b0a42014-05-13 00:26:37 -0500336 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500337 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500338 }
339}
340
341
342void
343AdjLsa::removeNptEntries(Nlsr& pnlsr)
344{
akmhoque157b0a42014-05-13 00:26:37 -0500345 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500346 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500347 }
348}
349
akmhoque674b0b12014-05-20 14:33:28 -0500350void
351AdjLsa::writeLog()
352{
353 _LOG_DEBUG("Adj Lsa: ");
354 _LOG_DEBUG(" Origination Router: " << m_origRouter);
355 _LOG_DEBUG(" Ls Type: " << m_lsType);
356 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500357 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500358 _LOG_DEBUG(" Adjacents: ");
359 int i = 1;
360 std::list<Adjacent> al = m_adl.getAdjList();
361 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
362 {
363 _LOG_DEBUG(" Adjacent " << i << ": ");;
364 _LOG_DEBUG(" Adjacent Name: " << (*it).getName());
365 _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri());
366 _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost());
367 }
368}
akmhoque53353462014-04-22 08:43:45 -0500369
370std::ostream&
371operator<<(std::ostream& os, AdjLsa& aLsa)
372{
373 os << "Adj Lsa: " << endl;
374 os << " Origination Router: " << aLsa.getOrigRouter() << endl;
akmhoque31d1d4b2014-05-05 22:08:14 -0500375 os << " Ls Type: " << aLsa.getLsType() << endl;
akmhoquec7a79b22014-05-26 08:06:19 -0500376 os << " Ls Seq No: " << aLsa.getLsSeqNo() << endl;
377 os << " Ls Lifetime: " << aLsa.getExpirationTimePoint() << endl;
378 os << " No Link: " << aLsa.getNoLink() << endl;
akmhoque53353462014-04-22 08:43:45 -0500379 os << " Adjacents: " << endl;
380 int i = 1;
381 std::list<Adjacent> al = aLsa.getAdl().getAdjList();
382 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
383 {
384 os << " Adjacent " << i << ": " << endl;
385 os << " Adjacent Name: " << (*it).getName() << endl;
akmhoque157b0a42014-05-13 00:26:37 -0500386 os << " Connecting FaceUri: " << (*it).getConnectingFaceUri() << endl;
akmhoque53353462014-04-22 08:43:45 -0500387 os << " Link Cost: " << (*it).getLinkCost() << endl;
388 }
389 return os;
390}
391
392}//namespace nlsr