blob: 8b15741effce0679db3772aaf9f5de6c2cd200ce [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
alvy49b1c0c2014-12-19 13:57:46 -060045const std::string NameLsa::TYPE_STRING = "name";
46const std::string AdjLsa::TYPE_STRING = "adjacency";
47const std::string CoordinateLsa::TYPE_STRING = "coordinate";
48
akmhoque31d1d4b2014-05-05 22:08:14 -050049const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -050050NameLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -050051{
akmhoque31d1d4b2014-05-05 22:08:14 -050052 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -060053 key.append(NameLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -050054 return key;
55}
56
akmhoque31d1d4b2014-05-05 22:08:14 -050057NameLsa::NameLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -050058 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -050059 NamePrefixList& npl)
akmhoque53353462014-04-22 08:43:45 -050060{
61 m_origRouter = origR;
62 m_lsType = lst;
63 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -050064 m_expirationTimePoint = lt;
akmhoque31d1d4b2014-05-05 22:08:14 -050065 std::list<ndn::Name>& nl = npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050066 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050067 addName((*it));
68 }
69}
70
71string
72NameLsa::getData()
73{
74 string nameLsaData;
alvy49b1c0c2014-12-19 13:57:46 -060075 nameLsaData = m_origRouter.toUri() + "|" + NameLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -050076 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -050077 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -050078 nameLsaData += "|";
79 nameLsaData += boost::lexical_cast<std::string>(m_npl.getSize());
akmhoque31d1d4b2014-05-05 22:08:14 -050080 std::list<ndn::Name> nl = m_npl.getNameList();
akmhoque157b0a42014-05-13 00:26:37 -050081 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -050082 nameLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -050083 nameLsaData += (*it).toUri();
akmhoque53353462014-04-22 08:43:45 -050084 }
85 return nameLsaData + "|";
86}
87
88bool
akmhoque31d1d4b2014-05-05 22:08:14 -050089NameLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -050090{
91 uint32_t numName = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -050092 boost::char_separator<char> sep("|");
93 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
94 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
95 tokens.begin();
96 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -050097 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -050098 return false;
99 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500100 try {
101 m_lsType = *tok_iter++;
102 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500103 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500104 numName = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500105 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500106 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500107 return false;
108 }
akmhoque157b0a42014-05-13 00:26:37 -0500109 for (uint32_t i = 0; i < numName; i++) {
akmhoque778f81b2014-06-27 10:07:56 -0500110 ndn::Name name(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500111 addName(name);
112 }
113 return true;
114}
115
116void
117NameLsa::writeLog()
118{
akmhoque674b0b12014-05-20 14:33:28 -0500119 _LOG_DEBUG("Name Lsa: ");
120 _LOG_DEBUG(" Origination Router: " << m_origRouter);
121 _LOG_DEBUG(" Ls Type: " << m_lsType);
122 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500123 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500124 _LOG_DEBUG(" Names: ");
125 int i = 1;
126 std::list<ndn::Name> nl = m_npl.getNameList();
127 for (std::list<ndn::Name>::iterator it = nl.begin(); it != nl.end(); it++)
128 {
129 _LOG_DEBUG(" Name " << i << ": " << (*it));
130 }
akmhoque2f423352014-06-03 11:49:35 -0500131 _LOG_DEBUG("name_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500132}
133
akmhoque31d1d4b2014-05-05 22:08:14 -0500134CoordinateLsa::CoordinateLsa(const ndn::Name& origR, const string lst,
135 uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500136 const ndn::time::system_clock::TimePoint& lt,
137 double r, double theta)
akmhoque53353462014-04-22 08:43:45 -0500138{
139 m_origRouter = origR;
140 m_lsType = lst;
141 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500142 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500143 m_corRad = r;
144 m_corTheta = theta;
145}
146
akmhoque31d1d4b2014-05-05 22:08:14 -0500147const ndn::Name
akmhoqueb6450b12014-04-24 00:01:03 -0500148CoordinateLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500149{
akmhoque31d1d4b2014-05-05 22:08:14 -0500150 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600151 key.append(CoordinateLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500152 return key;
153}
154
155bool
akmhoquefdbddb12014-05-02 18:35:19 -0500156CoordinateLsa::isEqualContent(const CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500157{
158 return (std::abs(m_corRad - clsa.getCorRadius()) <
159 std::numeric_limits<double>::epsilon()) &&
160 (std::abs(m_corTheta - clsa.getCorTheta()) <
161 std::numeric_limits<double>::epsilon());
162}
163
164string
akmhoqueb6450b12014-04-24 00:01:03 -0500165CoordinateLsa::getData()
akmhoque53353462014-04-22 08:43:45 -0500166{
167 string corLsaData;
akmhoque31d1d4b2014-05-05 22:08:14 -0500168 corLsaData = m_origRouter.toUri() + "|";
alvy49b1c0c2014-12-19 13:57:46 -0600169 corLsaData += CoordinateLsa::TYPE_STRING;
akmhoque31d1d4b2014-05-05 22:08:14 -0500170 corLsaData += "|";
akmhoque53353462014-04-22 08:43:45 -0500171 corLsaData += (boost::lexical_cast<std::string>(m_lsSeqNo) + "|");
akmhoquec7a79b22014-05-26 08:06:19 -0500172 corLsaData += (ndn::time::toIsoString(m_expirationTimePoint) + "|");
akmhoque53353462014-04-22 08:43:45 -0500173 corLsaData += (boost::lexical_cast<std::string>(m_corRad) + "|");
174 corLsaData += (boost::lexical_cast<std::string>(m_corTheta) + "|");
175 return corLsaData;
176}
177
178bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500179CoordinateLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500180{
akmhoque31d1d4b2014-05-05 22:08:14 -0500181 boost::char_separator<char> sep("|");
182 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
183 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
184 tokens.begin();
185 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500186 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500187 return false;
188 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 try {
190 m_lsType = *tok_iter++;
191 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500192 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500193 m_corRad = boost::lexical_cast<double>(*tok_iter++);
194 m_corTheta = boost::lexical_cast<double>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500195 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500196 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500197 return false;
198 }
199 return true;
200}
201
akmhoque674b0b12014-05-20 14:33:28 -0500202void
203CoordinateLsa::writeLog()
204{
205 _LOG_DEBUG("Cor Lsa: ");
206 _LOG_DEBUG(" Origination Router: " << m_origRouter);
207 _LOG_DEBUG(" Ls Type: " << m_lsType);
208 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500209 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500210 _LOG_DEBUG(" Hyperbolic Radius: " << m_corRad);
Vince Lehman9a709032014-09-13 16:28:07 -0500211 _LOG_DEBUG(" Hyperbolic Theta: " << m_corTheta);
akmhoque674b0b12014-05-20 14:33:28 -0500212}
213
akmhoque31d1d4b2014-05-05 22:08:14 -0500214AdjLsa::AdjLsa(const ndn::Name& origR, const string& lst, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500215 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500216 uint32_t nl , AdjacencyList& adl)
akmhoque53353462014-04-22 08:43:45 -0500217{
218 m_origRouter = origR;
219 m_lsType = lst;
220 m_lsSeqNo = lsn;
akmhoquec7a79b22014-05-26 08:06:19 -0500221 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500222 m_noLink = nl;
akmhoquec8a10f72014-04-25 18:42:55 -0500223 std::list<Adjacent> al = adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500224 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
Vince Lehmancb76ade2014-08-28 21:24:41 -0500225 if (it->getStatus() == Adjacent::STATUS_ACTIVE) {
akmhoque53353462014-04-22 08:43:45 -0500226 addAdjacent((*it));
227 }
228 }
229}
230
akmhoque31d1d4b2014-05-05 22:08:14 -0500231const ndn::Name
232AdjLsa::getKey() const
akmhoque53353462014-04-22 08:43:45 -0500233{
akmhoque31d1d4b2014-05-05 22:08:14 -0500234 ndn::Name key = m_origRouter;
alvy49b1c0c2014-12-19 13:57:46 -0600235 key.append(AdjLsa::TYPE_STRING);
akmhoque53353462014-04-22 08:43:45 -0500236 return key;
237}
238
239bool
akmhoquefdbddb12014-05-02 18:35:19 -0500240AdjLsa::isEqualContent(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500241{
akmhoquefdbddb12014-05-02 18:35:19 -0500242 return m_adl == alsa.getAdl();
akmhoque53353462014-04-22 08:43:45 -0500243}
244
245
246string
247AdjLsa::getData()
248{
249 string adjLsaData;
alvy49b1c0c2014-12-19 13:57:46 -0600250 adjLsaData = m_origRouter.toUri() + "|" + AdjLsa::TYPE_STRING + "|"
akmhoque53353462014-04-22 08:43:45 -0500251 + boost::lexical_cast<std::string>(m_lsSeqNo) + "|"
akmhoquec7a79b22014-05-26 08:06:19 -0500252 + ndn::time::toIsoString(m_expirationTimePoint);
akmhoque53353462014-04-22 08:43:45 -0500253 adjLsaData += "|";
254 adjLsaData += boost::lexical_cast<std::string>(m_adl.getSize());
255 std::list<Adjacent> al = m_adl.getAdjList();
akmhoque157b0a42014-05-13 00:26:37 -0500256 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++) {
akmhoque53353462014-04-22 08:43:45 -0500257 adjLsaData += "|";
akmhoque31d1d4b2014-05-05 22:08:14 -0500258 adjLsaData += (*it).getName().toUri();
akmhoque53353462014-04-22 08:43:45 -0500259 adjLsaData += "|";
akmhoque157b0a42014-05-13 00:26:37 -0500260 adjLsaData += (*it).getConnectingFaceUri();
akmhoque53353462014-04-22 08:43:45 -0500261 adjLsaData += "|";
262 adjLsaData += boost::lexical_cast<std::string>((*it).getLinkCost());
263 }
264 return adjLsaData + "|";
265}
266
267bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500268AdjLsa::initializeFromContent(const std::string& content)
akmhoque53353462014-04-22 08:43:45 -0500269{
270 uint32_t numLink = 0;
akmhoque31d1d4b2014-05-05 22:08:14 -0500271 boost::char_separator<char> sep("|");
272 boost::tokenizer<boost::char_separator<char> >tokens(content, sep);
273 boost::tokenizer<boost::char_separator<char> >::iterator tok_iter =
274 tokens.begin();
275 m_origRouter = ndn::Name(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500276 if (!(m_origRouter.size() > 0)) {
akmhoque53353462014-04-22 08:43:45 -0500277 return false;
278 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500279 try {
280 m_lsType = *tok_iter++;
281 m_lsSeqNo = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoquec7a79b22014-05-26 08:06:19 -0500282 m_expirationTimePoint = ndn::time::fromIsoString(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500283 numLink = boost::lexical_cast<uint32_t>(*tok_iter++);
akmhoque53353462014-04-22 08:43:45 -0500284 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500285 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500286 return false;
287 }
akmhoque157b0a42014-05-13 00:26:37 -0500288 for (uint32_t i = 0; i < numLink; i++) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500289 try {
akmhoque778f81b2014-06-27 10:07:56 -0500290 ndn::Name adjName(*tok_iter++);
akmhoque157b0a42014-05-13 00:26:37 -0500291 std::string connectingFaceUri(*tok_iter++);
akmhoque31d1d4b2014-05-05 22:08:14 -0500292 double linkCost = boost::lexical_cast<double>(*tok_iter++);
Vince Lehmancb76ade2014-08-28 21:24:41 -0500293 Adjacent adjacent(adjName, connectingFaceUri, linkCost, Adjacent::STATUS_INACTIVE, 0, 0);
akmhoque53353462014-04-22 08:43:45 -0500294 addAdjacent(adjacent);
295 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500296 catch (std::exception& e) {
akmhoque53353462014-04-22 08:43:45 -0500297 return false;
298 }
299 }
300 return true;
301}
302
303
304void
305AdjLsa::addNptEntries(Nlsr& pnlsr)
306{
akmhoque157b0a42014-05-13 00:26:37 -0500307 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500308 pnlsr.getNamePrefixTable().addEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500309 }
310}
311
312
313void
314AdjLsa::removeNptEntries(Nlsr& pnlsr)
315{
akmhoque157b0a42014-05-13 00:26:37 -0500316 if (getOrigRouter() != pnlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500317 pnlsr.getNamePrefixTable().removeEntry(getOrigRouter(), getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500318 }
319}
320
akmhoque674b0b12014-05-20 14:33:28 -0500321void
322AdjLsa::writeLog()
323{
324 _LOG_DEBUG("Adj Lsa: ");
325 _LOG_DEBUG(" Origination Router: " << m_origRouter);
326 _LOG_DEBUG(" Ls Type: " << m_lsType);
327 _LOG_DEBUG(" Ls Seq No: " << m_lsSeqNo);
akmhoquec7a79b22014-05-26 08:06:19 -0500328 _LOG_DEBUG(" Ls Lifetime: " << m_expirationTimePoint);
akmhoque674b0b12014-05-20 14:33:28 -0500329 _LOG_DEBUG(" Adjacents: ");
330 int i = 1;
331 std::list<Adjacent> al = m_adl.getAdjList();
332 for (std::list<Adjacent>::iterator it = al.begin(); it != al.end(); it++)
333 {
334 _LOG_DEBUG(" Adjacent " << i << ": ");;
335 _LOG_DEBUG(" Adjacent Name: " << (*it).getName());
336 _LOG_DEBUG(" Connecting FaceUri: " << (*it).getConnectingFaceUri());
337 _LOG_DEBUG(" Link Cost: " << (*it).getLinkCost());
338 }
akmhoque2f423352014-06-03 11:49:35 -0500339 _LOG_DEBUG("adj_lsa_end");
akmhoque53353462014-04-22 08:43:45 -0500340}
341
342}//namespace nlsr