blob: 7272aed93759204bac21cf62fde945dc82c81920 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev67758b12018-03-06 18:36:44 -05002/*
3 * Copyright (c) 2014-2018, 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/>.
Alexander Afanasyev67758b12018-03-06 18:36:44 -050020 */
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#ifndef NLSR_LSA_HPP
23#define NLSR_LSA_HPP
24
Nick Gordon22b5c952017-08-10 17:48:15 -050025#include "name-prefix-list.hpp"
26#include "adjacent.hpp"
27#include "adjacency-list.hpp"
28
akmhoquefdbddb12014-05-02 18:35:19 -050029#include <boost/cstdint.hpp>
akmhoquec8a10f72014-04-25 18:42:55 -050030#include <ndn-cxx/util/scheduler.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050031#include <ndn-cxx/util/time.hpp>
Nick Gordon0fa4c772017-10-23 13:33:03 -050032#include <boost/tokenizer.hpp>
akmhoquec7a79b22014-05-26 08:06:19 -050033
akmhoque53353462014-04-22 08:43:45 -050034namespace nlsr {
akmhoque31d1d4b2014-05-05 22:08:14 -050035
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050036class Nlsr;
37
akmhoque53353462014-04-22 08:43:45 -050038class Lsa
39{
40public:
Nick Gordon727d4832017-10-13 18:04:25 -050041 enum class Type {
42 ADJACENCY,
43 COORDINATE,
44 NAME,
Nick Gordon9212bd42017-10-23 10:59:38 -050045 BASE,
46 MOCK
Nick Gordon727d4832017-10-13 18:04:25 -050047 };
48
49 Lsa()
akmhoque53353462014-04-22 08:43:45 -050050 : m_origRouter()
51 , m_lsSeqNo()
akmhoquec7a79b22014-05-26 08:06:19 -050052 , m_expirationTimePoint()
akmhoque53353462014-04-22 08:43:45 -050053 , m_expiringEventId()
54 {
55 }
56
Alexander Afanasyev67758b12018-03-06 18:36:44 -050057 virtual
58 ~Lsa() = default;
59
Nick Gordon727d4832017-10-13 18:04:25 -050060 virtual Type
61 getType() const
akmhoque53353462014-04-22 08:43:45 -050062 {
Nick Gordon727d4832017-10-13 18:04:25 -050063 return Type::BASE;
akmhoque53353462014-04-22 08:43:45 -050064 }
65
66 void
67 setLsSeqNo(uint32_t lsn)
68 {
69 m_lsSeqNo = lsn;
70 }
71
72 uint32_t
73 getLsSeqNo() const
74 {
75 return m_lsSeqNo;
76 }
77
akmhoque31d1d4b2014-05-05 22:08:14 -050078 const ndn::Name&
akmhoque53353462014-04-22 08:43:45 -050079 getOrigRouter() const
80 {
81 return m_origRouter;
82 }
83
84 void
akmhoque31d1d4b2014-05-05 22:08:14 -050085 setOrigRouter(const ndn::Name& org)
akmhoque53353462014-04-22 08:43:45 -050086 {
87 m_origRouter = org;
88 }
89
akmhoquec7a79b22014-05-26 08:06:19 -050090 const ndn::time::system_clock::TimePoint&
91 getExpirationTimePoint() const
akmhoque53353462014-04-22 08:43:45 -050092 {
akmhoquec7a79b22014-05-26 08:06:19 -050093 return m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -050094 }
95
96 void
akmhoquec7a79b22014-05-26 08:06:19 -050097 setExpirationTimePoint(const ndn::time::system_clock::TimePoint& lt)
akmhoque53353462014-04-22 08:43:45 -050098 {
akmhoquec7a79b22014-05-26 08:06:19 -050099 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -0500100 }
101
102 void
103 setExpiringEventId(const ndn::EventId leei)
104 {
105 m_expiringEventId = leei;
106 }
107
108 ndn::EventId
109 getExpiringEventId() const
110 {
111 return m_expiringEventId;
112 }
113
Nick Gordonfaf49f42017-10-23 12:36:28 -0500114 /*! \brief Return the data that this LSA represents.
115 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500116 virtual std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500117 serialize() const = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500118
Nick Gordon22cc1a82017-10-23 13:06:53 -0500119 /*! \brief Gets the key for this LSA.
120
121 Format is: \<router name\>/\<LSA type>\
122 */
123 const ndn::Name
124 getKey() const;
125
Nick Gordon2f623382017-11-03 13:49:31 -0500126 /*! \brief Populate this LSA with content from the string "content".
127 \param content The string containing a valid serialization of LSA content.
128
129 This method populates "this" LSA with data from the string.
130 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500131 virtual bool
Nick Gordon2f623382017-11-03 13:49:31 -0500132 deserialize(const std::string& content) noexcept = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500133
Nick Gordonae0a0472017-10-23 15:51:23 -0500134 virtual void
135 writeLog() const = 0;
136
akmhoque53353462014-04-22 08:43:45 -0500137protected:
Nick Gordonfaf49f42017-10-23 12:36:28 -0500138 /*! Get data common to all LSA types.
139
140 This method should be called by all LSA classes in their
141 serialize() method.
142 */
143 std::string
144 getData() const;
145
Nick Gordonae0a0472017-10-23 15:51:23 -0500146 /*! Print data common to all LSA types.
147 */
148 std::string
149 toString() const;
150
Nick Gordon0fa4c772017-10-23 13:33:03 -0500151 bool
152 deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator);
153
Nick Gordonfaf49f42017-10-23 12:36:28 -0500154protected:
akmhoque31d1d4b2014-05-05 22:08:14 -0500155 ndn::Name m_origRouter;
akmhoque53353462014-04-22 08:43:45 -0500156 uint32_t m_lsSeqNo;
akmhoquec7a79b22014-05-26 08:06:19 -0500157 ndn::time::system_clock::TimePoint m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -0500158 ndn::EventId m_expiringEventId;
159};
160
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500161class NameLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500162{
163public:
164 NameLsa()
akmhoque53353462014-04-22 08:43:45 -0500165 {
akmhoque53353462014-04-22 08:43:45 -0500166 }
167
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600168 NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500169 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500170 NamePrefixList& npl);
akmhoque53353462014-04-22 08:43:45 -0500171
Nick Gordon727d4832017-10-13 18:04:25 -0500172 Lsa::Type
173 getType() const override
174 {
175 return Lsa::Type::NAME;
176 }
177
akmhoquec8a10f72014-04-25 18:42:55 -0500178 NamePrefixList&
akmhoque53353462014-04-22 08:43:45 -0500179 getNpl()
180 {
181 return m_npl;
182 }
183
Nick Gordon56d1fae2017-05-26 16:39:25 -0500184 const NamePrefixList&
185 getNpl() const
186 {
187 return m_npl;
188 }
189
akmhoque53353462014-04-22 08:43:45 -0500190 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500191 addName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500192 {
193 m_npl.insert(name);
194 }
195
196 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500197 removeName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500198 {
199 m_npl.remove(name);
200 }
201
Nick G97e34942016-07-11 14:46:27 -0500202 /*! \brief Initializes this LSA object with content's data.
203
204 \param content The data (e.g. name prefixes) to initialize this LSA with.
205
206 This function initializes this object to represent the data
207 contained in content. The format for this is the same as for
208 getData(); getData() returns data of this format, in other words.
209 */
akmhoque53353462014-04-22 08:43:45 -0500210 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500211 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500212
Nick Gordon56d1fae2017-05-26 16:39:25 -0500213 bool
214 isEqualContent(const NameLsa& other) const;
215
akmhoque53353462014-04-22 08:43:45 -0500216 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500217 writeLog() const override;
akmhoque53353462014-04-22 08:43:45 -0500218
Nick Gordonfaf49f42017-10-23 12:36:28 -0500219 /*! \brief Returns the data that this name LSA has.
220
221 Format is: \<original router
222 prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
223 2\>|...|\<prefix n\>|
224 */
225 std::string
226 serialize() const override;
227
akmhoque53353462014-04-22 08:43:45 -0500228private:
akmhoquec8a10f72014-04-25 18:42:55 -0500229 NamePrefixList m_npl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500230
231 friend std::ostream&
232 operator<<(std::ostream& os, const NameLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500233};
234
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500235class AdjLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500236{
237public:
alvydce3f182015-04-09 11:23:30 -0500238 typedef AdjacencyList::const_iterator const_iterator;
239
akmhoque53353462014-04-22 08:43:45 -0500240 AdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500241 {
akmhoque53353462014-04-22 08:43:45 -0500242 }
243
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600244 AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500245 const ndn::time::system_clock::TimePoint& lt,
246 uint32_t nl , AdjacencyList& adl);
akmhoque53353462014-04-22 08:43:45 -0500247
Nick Gordon727d4832017-10-13 18:04:25 -0500248 Lsa::Type
249 getType() const override
250 {
251 return Lsa::Type::ADJACENCY;
252 }
253
akmhoquec8a10f72014-04-25 18:42:55 -0500254 AdjacencyList&
akmhoque53353462014-04-22 08:43:45 -0500255 getAdl()
256 {
257 return m_adl;
258 }
259
Nick Gordon22b5c952017-08-10 17:48:15 -0500260 const AdjacencyList&
261 getAdl() const
262 {
263 return m_adl;
264 }
265
akmhoque53353462014-04-22 08:43:45 -0500266 void
267 addAdjacent(Adjacent adj)
268 {
269 m_adl.insert(adj);
270 }
271
Nick G97e34942016-07-11 14:46:27 -0500272 /*! \brief Initializes this adj. LSA from the supplied content.
273
274 \param content The content that this LSA is to have, formatted
275 according to getData().
276 */
akmhoque53353462014-04-22 08:43:45 -0500277 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500278 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500279
280 uint32_t
281 getNoLink()
282 {
283 return m_noLink;
284 }
285
286 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500287 isEqualContent(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500288
Nick G97e34942016-07-11 14:46:27 -0500289 /*! \brief Installs this LSA's name prefixes into the NPT.
290
291 \param pnlsr The NLSR top-level whose NPT you want to install the
292 entries into.
293 */
akmhoque53353462014-04-22 08:43:45 -0500294 void
295 addNptEntries(Nlsr& pnlsr);
296
297 void
298 removeNptEntries(Nlsr& pnlsr);
299
akmhoque674b0b12014-05-20 14:33:28 -0500300 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500301 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500302
alvydce3f182015-04-09 11:23:30 -0500303 const_iterator
304 begin() const
305 {
306 return m_adl.begin();
307 }
308
309 const_iterator
310 end() const
311 {
312 return m_adl.end();
313 }
314
Nick Gordonfaf49f42017-10-23 12:36:28 -0500315 /*! \brief Returns the data this adjacency LSA has.
316
317 The format is: \<original
318 router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
319 1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
320 n\>|\<cost n\>|
321 */
322 std::string
323 serialize() const override;
324
akmhoque53353462014-04-22 08:43:45 -0500325private:
326 uint32_t m_noLink;
akmhoquec8a10f72014-04-25 18:42:55 -0500327 AdjacencyList m_adl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500328
329 friend std::ostream&
330 operator<<(std::ostream& os, const AdjLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500331};
332
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500333class CoordinateLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500334{
335public:
akmhoqueb6450b12014-04-24 00:01:03 -0500336 CoordinateLsa()
Nick Gordon727d4832017-10-13 18:04:25 -0500337 : m_corRad(0)
akmhoque53353462014-04-22 08:43:45 -0500338 {
akmhoque53353462014-04-22 08:43:45 -0500339 }
340
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600341 CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500342 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600343 double r, std::vector<double> theta);
akmhoque53353462014-04-22 08:43:45 -0500344
Nick Gordon727d4832017-10-13 18:04:25 -0500345 Lsa::Type
346 getType() const override
347 {
348 return Lsa::Type::COORDINATE;
349 }
350
Nick G97e34942016-07-11 14:46:27 -0500351 /*! \brief Initializes this coordinate LSA with the data in content.
352
353 \param content The string content that is used to build the LSA.
354
355 This function initializes this LSA object to represent the data
356 specified by the parameter. The format that it is expecting is the
357 same as for getData();
358 */
akmhoque53353462014-04-22 08:43:45 -0500359 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500360 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500361
362 double
akmhoqueb6450b12014-04-24 00:01:03 -0500363 getCorRadius() const
akmhoque53353462014-04-22 08:43:45 -0500364 {
akmhoque53353462014-04-22 08:43:45 -0500365 return m_corRad;
akmhoque53353462014-04-22 08:43:45 -0500366 }
367
368 void
369 setCorRadius(double cr)
370 {
akmhoque157b0a42014-05-13 00:26:37 -0500371 m_corRad = cr;
akmhoque53353462014-04-22 08:43:45 -0500372 }
373
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600374 const std::vector<double>
akmhoqueb6450b12014-04-24 00:01:03 -0500375 getCorTheta() const
akmhoque53353462014-04-22 08:43:45 -0500376 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600377 return m_angles;
akmhoque53353462014-04-22 08:43:45 -0500378 }
379
380 void
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600381 setCorTheta(std::vector<double> ct)
akmhoque53353462014-04-22 08:43:45 -0500382 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600383 m_angles = ct;
akmhoque53353462014-04-22 08:43:45 -0500384 }
385
386 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500387 isEqualContent(const CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500388
akmhoque674b0b12014-05-20 14:33:28 -0500389 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500390 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500391
Nick Gordonfaf49f42017-10-23 12:36:28 -0500392 /*! \brief Returns the data that this coordinate LSA represents.
393
394 The format is: \<original
395 router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
396 */
397 std::string
398 serialize() const override;
399
akmhoque53353462014-04-22 08:43:45 -0500400private:
401 double m_corRad;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600402 std::vector<double> m_angles;
Nick Gordonae0a0472017-10-23 15:51:23 -0500403
404 friend std::ostream&
405 operator<<(std::ostream& os, const CoordinateLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500406};
407
alvydce3f182015-04-09 11:23:30 -0500408std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500409operator<<(std::ostream& os, const AdjLsa& lsa);
410
411std::ostream&
412operator<<(std::ostream& os, const CoordinateLsa& lsa);
413
414std::ostream&
415operator<<(std::ostream& os, const NameLsa& lsa);
alvydce3f182015-04-09 11:23:30 -0500416
Nick Gordon727d4832017-10-13 18:04:25 -0500417std::ostream&
418operator<<(std::ostream& os, const Lsa::Type& type);
419
420std::istream&
421operator>>(std::istream& is, Lsa::Type& type);
422
Nick Gordonfad8e252016-08-11 14:21:38 -0500423} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500424
Nick Gordon727d4832017-10-13 18:04:25 -0500425namespace std {
426 std::string
427 to_string(const nlsr::Lsa::Type& type);
428} // namespace std
429
Nick Gordon56d1fae2017-05-26 16:39:25 -0500430#endif // NLSR_LSA_HPP