blob: 7b96db29415d41b47355efb6eae5363d31ffea1d [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
akmhoque31d1d4b2014-05-05 22:08:14 -050056NameLsa::NameLsa(const ndn::Name& origR, const string& lst, 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)
akmhoque53353462014-04-22 08:43:45 -050059{
60 m_origRouter = origR;
61 m_lsType = lst;
62 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 {
100 m_lsType = *tok_iter++;
101 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500102 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500103 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500104 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500105 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500106 return false;
107 }
akmhoque157b0a42014-05-13 00:26:37 -0500108 for (uint32_t i = 0; i < numName; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500109 ndn::Name name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500110 addName(name);
111 }
112 return true;
113}
114
115void
116NameLsa::writeLog()
117{
akmhoque674b0b12014-05-20 14:33:28 -0500118 _LOG_DEBUG("Name Lsa: ");
119 _LOG_DEBUG(" Origination Router: " << m_origRouter);
120 _LOG_DEBUG(" Ls Type: " << m_lsType);
121 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500122 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500123 _LOG_DEBUG(" Names: ");
124 int i = 1;
125 std::list<ndn::Name> nl = m_npl.getNameList();
126 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
127 {
128 _LOG_DEBUG(" Name " << i << ": " << (*it));
129 }
akmhoque2f423352014-06-03 11:49:35 -0500130 _LOG_DEBUG("name_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500131}
132
akmhoque31d1d4b2014-05-05 22:08:14 -0500133CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
134 uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500135 const ndn::time::system_clock::TimePoint& lt,
136 double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500137{
138 m_origRouter = origR;
139 m_lsType = lst;
140 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500141 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500142 m_corRad = r;
143 m_corTheta = theta;
144}
145
akmhoque31d1d4b2014-05-05 22:08:14 -0500146const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500147CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500148{
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600150 key.append(CoordinateLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500151 return key;
152}
153
154bool
akmhoquefdbddb12014-05-02 18:35:19 -0500155CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500156{
157 return (std::abs(m_corRad - clsa.getCorRadius()) <
158 std::numeric_limits<double>::epsilon()) &&
159 (std::abs(m_corTheta - clsa.getCorTheta()) <
160 std::numeric_limits<double>::epsilon());
161}
162
163string
akmhoqueb6450b12014-04-24 00:01:03 -0500164CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500165{
166 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500167 corLsaData = m_origRouter.toUri() + "|";
alvy49b1c0c2014-12-19 13:57:46 -0600168 corLsaData += CoordinateLsa::TYPE_STRING;
akmhoque31d1d4b2014-05-05 22:08:14 -0500169 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500170 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
akmhoquec7a79b22014-05-26 08:06:19 -0500171 corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|");
akmhoque53353462014-04-22 08:43:45 -0500172 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
173 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
174 return corLsaData;
175}
176
177bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500178CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500179{
akmhoque31d1d4b2014-05-05 22:08:14 -0500180 boost::char_separator<char> sep("|");
181 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
182 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
183 tokens.begin();
184 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500185 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500186 return false;
187 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500188 try {
189 m_lsType = *tok_iter++;
190 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500191 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500192 m_corRad = boost::lexical_cast<double>(*tok_iter++);
193 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500194 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500196 return false;
197 }
198 return true;
199}
200
akmhoque674b0b12014-05-20 14:33:28 -0500201void
202CoordinateLsa::writeLog()
203{
204 _LOG_DEBUG("Cor Lsa: ");
205 _LOG_DEBUG(" Origination Router: " << m_origRouter);
206 _LOG_DEBUG(" Ls Type: " << m_lsType);
207 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500208 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500209 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
Vince Lehman9a709032014-09-13 16:28:07 -0500210 _LOG_DEBUG(" Hyperbolic Theta: " << m_corTheta);
akmhoque674b0b12014-05-20 14:33:28 -0500211}
212
akmhoque31d1d4b2014-05-05 22:08:14 -0500213AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500214 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500215 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500216{
217 m_origRouter = origR;
218 m_lsType = lst;
219 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500220 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500221 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500222 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500223 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500224 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500225 addAdjacent((*it));
226 }
227 }
228}
229
akmhoque31d1d4b2014-05-05 22:08:14 -0500230const ndn::Name
231AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500232{
akmhoque31d1d4b2014-05-05 22:08:14 -0500233 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600234 key.append(AdjLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500235 return key;
236}
237
238bool
akmhoquefdbddb12014-05-02 18:35:19 -0500239AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500240{
akmhoquefdbddb12014-05-02 18:35:19 -0500241 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500242}
243
244
245string
246AdjLsa::getData()
247{
248 string adjLsaData;
alvy49b1c0c2014-12-19 13:57:46 -0600249 adjLsaData = m_origRouter.toUri() + "|" + AdjLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -0500250 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500251 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500252 adjLsaData += "|";
253 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
254 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500255 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500256 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500257 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500258 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500259 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500260 adjLsaData += "|";
261 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
262 }
263 return adjLsaData + "|";
264}
265
266bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500267AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500268{
269 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500270 boost::char_separator<char> sep("|");
271 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
272 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
273 tokens.begin();
274 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500275 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500276 return false;
277 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500278 try {
279 m_lsType = *tok_iter++;
280 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500281 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500282 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500283 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500284 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500285 return false;
286 }
akmhoque157b0a42014-05-13 00:26:37 -0500287 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500288 try {
akmhoque778f81b2014-06-27 10:07:56 -0500289 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500290 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500291 double linkCost = boost::lexical_cast<double>(*tok_iter++);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500292 Adjacent adjacent(adjName, connectingFaceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500293 addAdjacent(adjacent);
294 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500295 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500296 return false;
297 }
298 }
299 return true;
300}
301
302
303void
304AdjLsa::addNptEntries(Nlsr& pnlsr)
305{
akmhoque157b0a42014-05-13 00:26:37 -0500306 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500307 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500308 }
309}
310
311
312void
313AdjLsa::removeNptEntries(Nlsr& pnlsr)
314{
akmhoque157b0a42014-05-13 00:26:37 -0500315 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500316 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500317 }
318}
319
akmhoque674b0b12014-05-20 14:33:28 -0500320void
321AdjLsa::writeLog()
322{
323 _LOG_DEBUG("Adj Lsa: ");
324 _LOG_DEBUG(" Origination Router: " << m_origRouter);
325 _LOG_DEBUG(" Ls Type: " << m_lsType);
326 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500327 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500328 _LOG_DEBUG(" Adjacents: ");
329 int i = 1;
330 std::list<Adjacent> al = m_adl.getAdjList();
331 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
332 {
333 _LOG_DEBUG(" Adjacent " << i << ": ");;
334 _LOG_DEBUG(" Adjacent Name: " << (*it).getName());
335 _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri());
336 _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost());
337 }
akmhoque2f423352014-06-03 11:49:35 -0500338 _LOG_DEBUG("adj_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500339}
340
341}//namespace nlsr