blob: ad35a81a724a625b0cae198955eb8f23e4ec51de [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, 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
akmhoque53353462014-04-22 08:43:45 -050037namespace nlsr {
38
akmhoque674b0b12014-05-20 14:33:28 -050039INIT_LOGGER("Lsa");
40
akmhoque53353462014-04-22 08:43:45 -050041using namespace std;
42
alvy49b1c0c2014-12-19 13:57:46 -060043const std::string NameLsa::TYPE_STRING = "name";
44const std::string AdjLsa::TYPE_STRING = "adjacency";
45const std::string CoordinateLsa::TYPE_STRING = "coordinate";
46
akmhoque31d1d4b2014-05-05 22:08:14 -050047const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050048NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050049{
akmhoque31d1d4b2014-05-05 22:08:14 -050050 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -060051 key.append(NameLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -050052 return key;
53}
54
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060055NameLsa::NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050056 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050057 NamePrefixList& npl)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060058 : Lsa(NameLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -050059{
60 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -050061 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050062 m_expirationTimePoint = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050063 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050064 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050065 addName((*it));
66 }
67}
68
69string
70NameLsa::getData()
71{
Nick Gordonadad2492017-05-25 10:53:07 -050072 std::ostringstream os;
73 os << m_origRouter << "|" << NameLsa::TYPE_STRING << "|" << m_lsSeqNo << "|"
74 << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_npl.getSize();
75 for (const auto& name : m_npl.getNameList()) {
76 os << "|" << name;
akmhoque53353462014-04-22 08:43:45 -050077 }
Nick Gordonadad2492017-05-25 10:53:07 -050078 os << "|";
79 return os.str();
akmhoque53353462014-04-22 08:43:45 -050080}
81
82bool
akmhoque31d1d4b2014-05-05 22:08:14 -050083NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050084{
85 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050086 boost::char_separator<char> sep("|");
87 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
88 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
89 tokens.begin();
90 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050091 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050092 return false;
93 }
akmhoque31d1d4b2014-05-05 22:08:14 -050094 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -060095 if (*tok_iter++ != NameLsa::TYPE_STRING) {
96 return false;
97 }
98
akmhoque31d1d4b2014-05-05 22:08:14 -050099 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500100 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500101 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500102 }
Nick Gordonadad2492017-05-25 10:53:07 -0500103 catch (const std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600104 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500105 return false;
106 }
akmhoque157b0a42014-05-13 00:26:37 -0500107 for (uint32_t i = 0; i < numName; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500108 ndn::Name name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500109 addName(name);
110 }
111 return true;
112}
113
Nick Gordon56d1fae2017-05-26 16:39:25 -0500114bool
115NameLsa::isEqualContent(const NameLsa& other) const
116{
117 return m_npl == other.getNpl();
118}
119
akmhoque53353462014-04-22 08:43:45 -0500120void
121NameLsa::writeLog()
122{
akmhoque674b0b12014-05-20 14:33:28 -0500123 _LOG_DEBUG("Name Lsa: ");
124 _LOG_DEBUG(" Origination Router: " << m_origRouter);
125 _LOG_DEBUG(" Ls Type: " << m_lsType);
126 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500127 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500128 _LOG_DEBUG(" Names: ");
129 int i = 1;
130 std::list<ndn::Name> nl = m_npl.getNameList();
131 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
132 {
133 _LOG_DEBUG(" Name " << i << ": " << (*it));
134 }
akmhoque2f423352014-06-03 11:49:35 -0500135 _LOG_DEBUG("name_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500136}
137
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600138CoordinateLsa::CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500139 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600140 double r, std::vector<double> theta)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600141 : Lsa(CoordinateLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -0500142{
143 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500144 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500145 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500146 m_corRad = r;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600147 m_angles = theta;
akmhoque53353462014-04-22 08:43:45 -0500148}
149
akmhoque31d1d4b2014-05-05 22:08:14 -0500150const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500151CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500152{
akmhoque31d1d4b2014-05-05 22:08:14 -0500153 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600154 key.append(CoordinateLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500155 return key;
156}
157
158bool
akmhoquefdbddb12014-05-02 18:35:19 -0500159CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500160{
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600161 if (clsa.getCorTheta().size() != m_angles.size()) {
162 return false;
163 }
164
165 std::vector<double> m_angles2 = clsa.getCorTheta();
166 for (unsigned int i = 0; i < clsa.getCorTheta().size(); i++) {
167 if (std::abs(m_angles[i] - m_angles2[i]) > std::numeric_limits<double>::epsilon()) {
168 return false;
169 }
170 }
171
akmhoque53353462014-04-22 08:43:45 -0500172 return (std::abs(m_corRad - clsa.getCorRadius()) <
akmhoque53353462014-04-22 08:43:45 -0500173 std::numeric_limits<double>::epsilon());
174}
175
176string
akmhoqueb6450b12014-04-24 00:01:03 -0500177CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500178{
Nick Gordonadad2492017-05-25 10:53:07 -0500179 std::ostringstream os;
180 os << m_origRouter << "|" << CoordinateLsa::TYPE_STRING << "|" << m_lsSeqNo << "|"
181 << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_corRad << "|"
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600182 << m_angles.size() << "|";
183
184 for (const auto& angle: m_angles) {
185 os << angle << "|";
186 }
187
Nick Gordonadad2492017-05-25 10:53:07 -0500188 return os.str();
akmhoque53353462014-04-22 08:43:45 -0500189}
190
191bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500192CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500193{
akmhoque31d1d4b2014-05-05 22:08:14 -0500194 boost::char_separator<char> sep("|");
195 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
196 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
197 tokens.begin();
198 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500199 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500200 return false;
201 }
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600202
akmhoque31d1d4b2014-05-05 22:08:14 -0500203 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600204 if (*tok_iter++ != CoordinateLsa::TYPE_STRING) {
205 return false;
206 }
207
akmhoque31d1d4b2014-05-05 22:08:14 -0500208 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500209 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500210 m_corRad = boost::lexical_cast<double>(*tok_iter++);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600211 int numAngles = boost::lexical_cast<uint32_t>(*tok_iter++);
212
213 for (int i = 0; i < numAngles; i++) {
214 m_angles.push_back(boost::lexical_cast<double>(*tok_iter++));
215 }
akmhoque53353462014-04-22 08:43:45 -0500216 }
Nick Gordonadad2492017-05-25 10:53:07 -0500217 catch (const std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600218 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500219 return false;
220 }
221 return true;
222}
223
akmhoque674b0b12014-05-20 14:33:28 -0500224void
225CoordinateLsa::writeLog()
226{
227 _LOG_DEBUG("Cor Lsa: ");
228 _LOG_DEBUG(" Origination Router: " << m_origRouter);
229 _LOG_DEBUG(" Ls Type: " << m_lsType);
230 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500231 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500232 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600233 int i = 0;
234 for(auto const& value: m_angles) {
235 _LOG_DEBUG(" Hyperbolic Theta " << i++ << ": "<< value);
236 }
akmhoque674b0b12014-05-20 14:33:28 -0500237}
238
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600239AdjLsa::AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500240 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500241 uint32_t nl , AdjacencyList& adl)
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600242 : Lsa(AdjLsa::TYPE_STRING)
akmhoque53353462014-04-22 08:43:45 -0500243{
244 m_origRouter = origR;
akmhoque53353462014-04-22 08:43:45 -0500245 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500246 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500247 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500248 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500249 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500250 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500251 addAdjacent((*it));
252 }
253 }
254}
255
akmhoque31d1d4b2014-05-05 22:08:14 -0500256const ndn::Name
257AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500258{
akmhoque31d1d4b2014-05-05 22:08:14 -0500259 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600260 key.append(AdjLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500261 return key;
262}
263
264bool
akmhoquefdbddb12014-05-02 18:35:19 -0500265AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500266{
akmhoquefdbddb12014-05-02 18:35:19 -0500267 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500268}
269
akmhoque53353462014-04-22 08:43:45 -0500270string
271AdjLsa::getData()
272{
Nick Gordonadad2492017-05-25 10:53:07 -0500273 std::ostringstream os;
274 os << m_origRouter << "|" << AdjLsa::TYPE_STRING << "|" << m_lsSeqNo << "|"
275 << ndn::time::toIsoString(m_expirationTimePoint) << "|" << m_adl.getSize();
276 for (const auto& adjacent : m_adl.getAdjList()) {
Nick Gordone9733ed2017-04-26 10:48:39 -0500277 os << "|" << adjacent.getName() << "|" << adjacent.getFaceUri()
Nick Gordonadad2492017-05-25 10:53:07 -0500278 << "|" << adjacent.getLinkCost();
akmhoque53353462014-04-22 08:43:45 -0500279 }
Nick Gordonadad2492017-05-25 10:53:07 -0500280 os << "|";
281 return os.str();
akmhoque53353462014-04-22 08:43:45 -0500282}
283
284bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500285AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500286{
287 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500288 boost::char_separator<char> sep("|");
289 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
290 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
291 tokens.begin();
292 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500293 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500294 return false;
295 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500296 try {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600297 if (*tok_iter++ != AdjLsa::TYPE_STRING) {
298 return false;
299 }
300
akmhoque31d1d4b2014-05-05 22:08:14 -0500301 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500302 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500303 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500304 }
Nick Gordonadad2492017-05-25 10:53:07 -0500305 catch (const 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 }
akmhoque157b0a42014-05-13 00:26:37 -0500309 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500310 try {
akmhoque778f81b2014-06-27 10:07:56 -0500311 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500312 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500313 double linkCost = boost::lexical_cast<double>(*tok_iter++);
Nick Gordone9733ed2017-04-26 10:48:39 -0500314 Adjacent adjacent(adjName, ndn::util::FaceUri(connectingFaceUri), linkCost,
315 Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500316 addAdjacent(adjacent);
317 }
Nick Gordonadad2492017-05-25 10:53:07 -0500318 catch (const std::exception& e) {
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600319 _LOG_ERROR(e.what());
akmhoque53353462014-04-22 08:43:45 -0500320 return false;
321 }
322 }
323 return true;
324}
325
akmhoque53353462014-04-22 08:43:45 -0500326void
327AdjLsa::addNptEntries(Nlsr& pnlsr)
328{
Nick G97e34942016-07-11 14:46:27 -0500329 // Only add NPT entries if this is an adj LSA from another router.
akmhoque157b0a42014-05-13 00:26:37 -0500330 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500331 // Pass the originating router as both the name to register and
332 // where it came from.
akmhoque31d1d4b2014-05-05 22:08:14 -0500333 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500334 }
335}
336
337
338void
339AdjLsa::removeNptEntries(Nlsr& pnlsr)
340{
akmhoque157b0a42014-05-13 00:26:37 -0500341 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500342 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500343 }
344}
345
akmhoque674b0b12014-05-20 14:33:28 -0500346void
347AdjLsa::writeLog()
348{
alvydce3f182015-04-09 11:23:30 -0500349 _LOG_DEBUG(*this);
akmhoque53353462014-04-22 08:43:45 -0500350}
351
alvydce3f182015-04-09 11:23:30 -0500352std::ostream&
353operator<<(std::ostream& os, const AdjLsa& adjLsa)
354{
355 os << "Adj Lsa:\n"
356 << " Origination Router: " << adjLsa.getOrigRouter() << "\n"
357 << " Ls Type: " << adjLsa.getLsType() << "\n"
358 << " Ls Seq No: " << adjLsa.getLsSeqNo() << "\n"
359 << " Ls Lifetime: " << adjLsa.getExpirationTimePoint() << "\n"
360 << " Adjacents: \n";
361
362 int adjacencyIndex = 1;
363
364 for (const Adjacent& adjacency : adjLsa) {
365 os << " Adjacent " << adjacencyIndex++ << ":\n"
366 << " Adjacent Name: " << adjacency.getName() << "\n"
Nick Gordone9733ed2017-04-26 10:48:39 -0500367 << " Connecting FaceUri: " << adjacency.getFaceUri() << "\n"
alvydce3f182015-04-09 11:23:30 -0500368 << " Link Cost: " << adjacency.getLinkCost() << "\n";
369 }
370 os << "adj_lsa_end";
371
372 return os;
373}
374
375} // namespace nlsr