blob: db516b7c53b707980ee3d974d69e87f227e01d4e [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince Lehmanc2e51f62015-01-20 15:03:11 -06003 * Copyright (c) 2014-2015, The University of Memphis,
4 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include <string>
23#include <iostream>
24#include <sstream>
25#include <algorithm>
26#include <cmath>
27#include <limits>
akmhoque31d1d4b2014-05-05 22:08:14 -050028#include <boost/tokenizer.hpp>
29#include <boost/algorithm/string.hpp>
akmhoque53353462014-04-22 08:43:45 -050030
31#include "nlsr.hpp"
32#include "lsa.hpp"
akmhoquec8a10f72014-04-25 18:42:55 -050033#include "name-prefix-list.hpp"
akmhoque53353462014-04-22 08:43:45 -050034#include "adjacent.hpp"
akmhoque674b0b12014-05-20 14:33:28 -050035#include "logger.hpp"
akmhoque53353462014-04-22 08:43:45 -050036
37
38namespace nlsr {
39
akmhoque674b0b12014-05-20 14:33:28 -050040INIT_LOGGER("Lsa");
41
akmhoque53353462014-04-22 08:43:45 -050042using namespace std;
43
alvy49b1c0c2014-12-19 13:57:46 -060044const std::string NameLsa::TYPE_STRING = "name";
45const std::string AdjLsa::TYPE_STRING = "adjacency";
46const std::string CoordinateLsa::TYPE_STRING = "coordinate";
47
akmhoque31d1d4b2014-05-05 22:08:14 -050048const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050049NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050050{
akmhoque31d1d4b2014-05-05 22:08:14 -050051 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -060052 key.append(NameLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -050053 return key;
54}
55
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060056NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050057 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050058 NamePrefixList& npl)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060059 : Lsa(NameLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -050060{
61 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -050062 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050063 m_expirationTimePoint = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050064 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050065 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050066 addName((*it));
67 }
68}
69
70string
71NameLsa::getData()
72{
73 string nameLsaData;
alvy49b1c0c2014-12-19 13:57:46 -060074 nameLsaData = m_origRouter.toUri() + "|" + NameLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -050075 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -050076 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -050077 nameLsaData += "|";
78 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050079 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050080 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050081 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050082 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050083 }
84 return nameLsaData + "|";
85}
86
87bool
akmhoque31d1d4b2014-05-05 22:08:14 -050088NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050089{
90 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050091 boost::char_separator<char> sep("|");
92 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
93 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
94 tokens.begin();
95 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050096 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050097 return false;
98 }
akmhoque31d1d4b2014-05-05 22:08:14 -050099 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600100 if (*tok_iter++ != NameLsa::TYPE_STRING) {
101 return false;
102 }
103
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500105 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500107 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500108 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600109 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500110 return false;
111 }
akmhoque157b0a42014-05-13 00:26:37 -0500112 for (uint32_t i = 0; i < numName; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500113 ndn::Name name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500114 addName(name);
115 }
116 return true;
117}
118
119void
120NameLsa::writeLog()
121{
akmhoque674b0b12014-05-20 14:33:28 -0500122 _LOG_DEBUG("Name Lsa: ");
123 _LOG_DEBUG(" Origination Router: " << m_origRouter);
124 _LOG_DEBUG(" Ls Type: " << m_lsType);
125 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500126 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500127 _LOG_DEBUG(" Names: ");
128 int i = 1;
129 std::list<ndn::Name> nl = m_npl.getNameList();
130 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
131 {
132 _LOG_DEBUG(" Name " << i << ": " << (*it));
133 }
akmhoque2f423352014-06-03 11:49:35 -0500134 _LOG_DEBUG("name_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500135}
136
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600137CoordinateLsa::CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500138 const ndn::time::system_clock::TimePoint& lt,
139 double r, double theta)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600140 : Lsa(CoordinateLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -0500141{
142 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500143 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500144 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500145 m_corRad = r;
146 m_corTheta = theta;
147}
148
akmhoque31d1d4b2014-05-05 22:08:14 -0500149const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500150CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500151{
akmhoque31d1d4b2014-05-05 22:08:14 -0500152 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600153 key.append(CoordinateLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500154 return key;
155}
156
157bool
akmhoquefdbddb12014-05-02 18:35:19 -0500158CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500159{
160 return (std::abs(m_corRad - clsa.getCorRadius()) <
161 std::numeric_limits<double>::epsilon()) &&
162 (std::abs(m_corTheta - clsa.getCorTheta()) <
163 std::numeric_limits<double>::epsilon());
164}
165
166string
akmhoqueb6450b12014-04-24 00:01:03 -0500167CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500168{
169 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500170 corLsaData = m_origRouter.toUri() + "|";
alvy49b1c0c2014-12-19 13:57:46 -0600171 corLsaData += CoordinateLsa::TYPE_STRING;
akmhoque31d1d4b2014-05-05 22:08:14 -0500172 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500173 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
akmhoquec7a79b22014-05-26 08:06:19 -0500174 corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|");
akmhoque53353462014-04-22 08:43:45 -0500175 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
176 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
177 return corLsaData;
178}
179
180bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500181CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500182{
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 boost::char_separator<char> sep("|");
184 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
185 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
186 tokens.begin();
187 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500188 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500189 return false;
190 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600192 if (*tok_iter++ != CoordinateLsa::TYPE_STRING) {
193 return false;
194 }
195
akmhoque31d1d4b2014-05-05 22:08:14 -0500196 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500197 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500198 m_corRad = boost::lexical_cast<double>(*tok_iter++);
199 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500200 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500201 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600202 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500203 return false;
204 }
205 return true;
206}
207
akmhoque674b0b12014-05-20 14:33:28 -0500208void
209CoordinateLsa::writeLog()
210{
211 _LOG_DEBUG("Cor Lsa: ");
212 _LOG_DEBUG(" Origination Router: " << m_origRouter);
213 _LOG_DEBUG(" Ls Type: " << m_lsType);
214 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500215 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500216 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
Vince Lehman9a709032014-09-13 16:28:07 -0500217 _LOG_DEBUG(" Hyperbolic Theta: " << m_corTheta);
akmhoque674b0b12014-05-20 14:33:28 -0500218}
219
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600220AdjLsa::AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500221 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500222 uint32_t nl , AdjacencyList& adl)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600223 : Lsa(AdjLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -0500224{
225 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500226 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500227 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500228 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500229 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500230 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500231 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500232 addAdjacent((*it));
233 }
234 }
235}
236
akmhoque31d1d4b2014-05-05 22:08:14 -0500237const ndn::Name
238AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500239{
akmhoque31d1d4b2014-05-05 22:08:14 -0500240 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600241 key.append(AdjLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500242 return key;
243}
244
245bool
akmhoquefdbddb12014-05-02 18:35:19 -0500246AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500247{
akmhoquefdbddb12014-05-02 18:35:19 -0500248 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500249}
250
251
252string
253AdjLsa::getData()
254{
255 string adjLsaData;
alvy49b1c0c2014-12-19 13:57:46 -0600256 adjLsaData = m_origRouter.toUri() + "|" + AdjLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -0500257 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500258 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500259 adjLsaData += "|";
260 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
261 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500262 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500263 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500264 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500265 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500266 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500267 adjLsaData += "|";
268 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
269 }
270 return adjLsaData + "|";
271}
272
273bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500274AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500275{
276 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500277 boost::char_separator<char> sep("|");
278 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
279 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
280 tokens.begin();
281 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500282 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500283 return false;
284 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500285 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600286 if (*tok_iter++ != AdjLsa::TYPE_STRING) {
287 return false;
288 }
289
akmhoque31d1d4b2014-05-05 22:08:14 -0500290 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500291 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500292 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500293 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500294 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600295 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500296 return false;
297 }
akmhoque157b0a42014-05-13 00:26:37 -0500298 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500299 try {
akmhoque778f81b2014-06-27 10:07:56 -0500300 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500301 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500302 double linkCost = boost::lexical_cast<double>(*tok_iter++);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500303 Adjacent adjacent(adjName, connectingFaceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500304 addAdjacent(adjacent);
305 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500306 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600307 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500308 return false;
309 }
310 }
311 return true;
312}
313
314
315void
316AdjLsa::addNptEntries(Nlsr& pnlsr)
317{
akmhoque157b0a42014-05-13 00:26:37 -0500318 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500319 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500320 }
321}
322
323
324void
325AdjLsa::removeNptEntries(Nlsr& pnlsr)
326{
akmhoque157b0a42014-05-13 00:26:37 -0500327 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500328 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500329 }
330}
331
akmhoque674b0b12014-05-20 14:33:28 -0500332void
333AdjLsa::writeLog()
334{
alvydce3f182015-04-09 11:23:30 -0500335 _LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500336}
337
alvydce3f182015-04-09 11:23:30 -0500338std::ostream&
339operator<<(std::ostream& os, const AdjLsa& adjLsa)
340{
341 os << "Adj Lsa:\n"
342 << " Origination Router: " << adjLsa.getOrigRouter() << "\n"
343 << " Ls Type: " << adjLsa.getLsType() << "\n"
344 << " Ls Seq No: " << adjLsa.getLsSeqNo() << "\n"
345 << " Ls Lifetime: " << adjLsa.getExpirationTimePoint() << "\n"
346 << " Adjacents: \n";
347
348 int adjacencyIndex = 1;
349
350 for (const Adjacent& adjacency : adjLsa) {
351 os << " Adjacent " << adjacencyIndex++ << ":\n"
352 << " Adjacent Name: " << adjacency.getName() << "\n"
353 << " Connecting FaceUri: " << adjacency.getConnectingFaceUri() << "\n"
354 << " Link Cost: " << adjacency.getLinkCost() << "\n";
355 }
356 os << "adj_lsa_end";
357
358 return os;
359}
360
361} // namespace nlsr