blob: 410d56ad210e772ad196e54527e8943238d1eab8 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Muktadir R Chowdhury800833b2016-07-29 13:43:59 -05003 * Copyright (c) 2014-2016, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * 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
akmhoque53353462014-04-22 08:43:45 -0500251string
252AdjLsa::getData()
253{
254 string adjLsaData;
alvy49b1c0c2014-12-19 13:57:46 -0600255 adjLsaData = m_origRouter.toUri() + "|" + AdjLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -0500256 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500257 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500258 adjLsaData += "|";
259 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
260 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500261 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500262 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500263 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500264 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500265 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500266 adjLsaData += "|";
267 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
268 }
269 return adjLsaData + "|";
270}
271
272bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500273AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500274{
275 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500276 boost::char_separator<char> sep("|");
277 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
278 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
279 tokens.begin();
280 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500281 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500282 return false;
283 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500284 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600285 if (*tok_iter++ != AdjLsa::TYPE_STRING) {
286 return false;
287 }
288
akmhoque31d1d4b2014-05-05 22:08:14 -0500289 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500290 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500291 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500292 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500293 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600294 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500295 return false;
296 }
akmhoque157b0a42014-05-13 00:26:37 -0500297 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500298 try {
akmhoque778f81b2014-06-27 10:07:56 -0500299 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500300 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500301 double linkCost = boost::lexical_cast<double>(*tok_iter++);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500302 Adjacent adjacent(adjName, connectingFaceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500303 addAdjacent(adjacent);
304 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500305 catch (std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600306 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500307 return false;
308 }
309 }
310 return true;
311}
312
akmhoque53353462014-04-22 08:43:45 -0500313void
314AdjLsa::addNptEntries(Nlsr& pnlsr)
315{
Nick G97e34942016-07-11 14:46:27 -0500316 // Only add NPT entries if this is an adj LSA from another router.
akmhoque157b0a42014-05-13 00:26:37 -0500317 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500318 // Pass the originating router as both the name to register and
319 // where it came from.
akmhoque31d1d4b2014-05-05 22:08:14 -0500320 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500321 }
322}
323
324
325void
326AdjLsa::removeNptEntries(Nlsr& pnlsr)
327{
akmhoque157b0a42014-05-13 00:26:37 -0500328 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500329 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500330 }
331}
332
akmhoque674b0b12014-05-20 14:33:28 -0500333void
334AdjLsa::writeLog()
335{
alvydce3f182015-04-09 11:23:30 -0500336 _LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500337}
338
alvydce3f182015-04-09 11:23:30 -0500339std::ostream&
340operator<<(std::ostream& os, const AdjLsa& adjLsa)
341{
342 os << "Adj Lsa:\n"
343 << " Origination Router: " << adjLsa.getOrigRouter() << "\n"
344 << " Ls Type: " << adjLsa.getLsType() << "\n"
345 << " Ls Seq No: " << adjLsa.getLsSeqNo() << "\n"
346 << " Ls Lifetime: " << adjLsa.getExpirationTimePoint() << "\n"
347 << " Adjacents: \n";
348
349 int adjacencyIndex = 1;
350
351 for (const Adjacent& adjacency : adjLsa) {
352 os << " Adjacent " << adjacencyIndex++ << ":\n"
353 << " Adjacent Name: " << adjacency.getName() << "\n"
354 << " Connecting FaceUri: " << adjacency.getConnectingFaceUri() << "\n"
355 << " Link Cost: " << adjacency.getLinkCost() << "\n";
356 }
357 os << "adj_lsa_end";
358
359 return os;
360}
361
362} // namespace nlsr