blob: 54a2c759f50b0ec762fea262d42dddcc4ca2a706 [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/*
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, 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
akmhoque53353462014-04-22 08:43:45 -050036class Lsa
37{
38public:
Nick Gordon727d4832017-10-13 18:04:25 -050039 enum class Type {
40 ADJACENCY,
41 COORDINATE,
42 NAME,
Nick Gordon9212bd42017-10-23 10:59:38 -050043 BASE,
44 MOCK
Nick Gordon727d4832017-10-13 18:04:25 -050045 };
46
47 Lsa()
akmhoque53353462014-04-22 08:43:45 -050048 : m_origRouter()
49 , m_lsSeqNo()
akmhoquec7a79b22014-05-26 08:06:19 -050050 , m_expirationTimePoint()
akmhoque53353462014-04-22 08:43:45 -050051 , m_expiringEventId()
52 {
53 }
54
Alexander Afanasyev67758b12018-03-06 18:36:44 -050055 virtual
56 ~Lsa() = default;
57
Nick Gordon727d4832017-10-13 18:04:25 -050058 virtual Type
59 getType() const
akmhoque53353462014-04-22 08:43:45 -050060 {
Nick Gordon727d4832017-10-13 18:04:25 -050061 return Type::BASE;
akmhoque53353462014-04-22 08:43:45 -050062 }
63
64 void
65 setLsSeqNo(uint32_t lsn)
66 {
67 m_lsSeqNo = lsn;
68 }
69
70 uint32_t
71 getLsSeqNo() const
72 {
73 return m_lsSeqNo;
74 }
75
akmhoque31d1d4b2014-05-05 22:08:14 -050076 const ndn::Name&
akmhoque53353462014-04-22 08:43:45 -050077 getOrigRouter() const
78 {
79 return m_origRouter;
80 }
81
82 void
akmhoque31d1d4b2014-05-05 22:08:14 -050083 setOrigRouter(const ndn::Name& org)
akmhoque53353462014-04-22 08:43:45 -050084 {
85 m_origRouter = org;
86 }
87
akmhoquec7a79b22014-05-26 08:06:19 -050088 const ndn::time::system_clock::TimePoint&
89 getExpirationTimePoint() const
akmhoque53353462014-04-22 08:43:45 -050090 {
akmhoquec7a79b22014-05-26 08:06:19 -050091 return m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -050092 }
93
94 void
akmhoquec7a79b22014-05-26 08:06:19 -050095 setExpirationTimePoint(const ndn::time::system_clock::TimePoint& lt)
akmhoque53353462014-04-22 08:43:45 -050096 {
akmhoquec7a79b22014-05-26 08:06:19 -050097 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -050098 }
99
100 void
101 setExpiringEventId(const ndn::EventId leei)
102 {
103 m_expiringEventId = leei;
104 }
105
106 ndn::EventId
107 getExpiringEventId() const
108 {
109 return m_expiringEventId;
110 }
111
Nick Gordonfaf49f42017-10-23 12:36:28 -0500112 /*! \brief Return the data that this LSA represents.
113 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500114 virtual std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500115 serialize() const = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500116
Nick Gordon22cc1a82017-10-23 13:06:53 -0500117 /*! \brief Gets the key for this LSA.
118
119 Format is: \<router name\>/\<LSA type>\
120 */
121 const ndn::Name
122 getKey() const;
123
Nick Gordon2f623382017-11-03 13:49:31 -0500124 /*! \brief Populate this LSA with content from the string "content".
125 \param content The string containing a valid serialization of LSA content.
126
127 This method populates "this" LSA with data from the string.
128 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500129 virtual bool
Nick Gordon2f623382017-11-03 13:49:31 -0500130 deserialize(const std::string& content) noexcept = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500131
Nick Gordonae0a0472017-10-23 15:51:23 -0500132 virtual void
133 writeLog() const = 0;
134
akmhoque53353462014-04-22 08:43:45 -0500135protected:
Nick Gordonfaf49f42017-10-23 12:36:28 -0500136 /*! Get data common to all LSA types.
137
138 This method should be called by all LSA classes in their
139 serialize() method.
140 */
141 std::string
142 getData() const;
143
Nick Gordonae0a0472017-10-23 15:51:23 -0500144 /*! Print data common to all LSA types.
145 */
146 std::string
147 toString() const;
148
Nick Gordon0fa4c772017-10-23 13:33:03 -0500149 bool
150 deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator);
151
Nick Gordonfaf49f42017-10-23 12:36:28 -0500152protected:
akmhoque31d1d4b2014-05-05 22:08:14 -0500153 ndn::Name m_origRouter;
akmhoque53353462014-04-22 08:43:45 -0500154 uint32_t m_lsSeqNo;
akmhoquec7a79b22014-05-26 08:06:19 -0500155 ndn::time::system_clock::TimePoint m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -0500156 ndn::EventId m_expiringEventId;
157};
158
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500159class NameLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500160{
161public:
162 NameLsa()
akmhoque53353462014-04-22 08:43:45 -0500163 {
akmhoque53353462014-04-22 08:43:45 -0500164 }
165
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600166 NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500167 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500168 NamePrefixList& npl);
akmhoque53353462014-04-22 08:43:45 -0500169
Nick Gordon727d4832017-10-13 18:04:25 -0500170 Lsa::Type
171 getType() const override
172 {
173 return Lsa::Type::NAME;
174 }
175
akmhoquec8a10f72014-04-25 18:42:55 -0500176 NamePrefixList&
akmhoque53353462014-04-22 08:43:45 -0500177 getNpl()
178 {
179 return m_npl;
180 }
181
Nick Gordon56d1fae2017-05-26 16:39:25 -0500182 const NamePrefixList&
183 getNpl() const
184 {
185 return m_npl;
186 }
187
akmhoque53353462014-04-22 08:43:45 -0500188 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 addName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500190 {
191 m_npl.insert(name);
192 }
193
194 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500195 removeName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500196 {
197 m_npl.remove(name);
198 }
199
Nick G97e34942016-07-11 14:46:27 -0500200 /*! \brief Initializes this LSA object with content's data.
201
202 \param content The data (e.g. name prefixes) to initialize this LSA with.
203
204 This function initializes this object to represent the data
205 contained in content. The format for this is the same as for
206 getData(); getData() returns data of this format, in other words.
207 */
akmhoque53353462014-04-22 08:43:45 -0500208 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500209 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500210
Nick Gordon56d1fae2017-05-26 16:39:25 -0500211 bool
212 isEqualContent(const NameLsa& other) const;
213
akmhoque53353462014-04-22 08:43:45 -0500214 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500215 writeLog() const override;
akmhoque53353462014-04-22 08:43:45 -0500216
Nick Gordonfaf49f42017-10-23 12:36:28 -0500217 /*! \brief Returns the data that this name LSA has.
218
219 Format is: \<original router
220 prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
221 2\>|...|\<prefix n\>|
222 */
223 std::string
224 serialize() const override;
225
akmhoque53353462014-04-22 08:43:45 -0500226private:
akmhoquec8a10f72014-04-25 18:42:55 -0500227 NamePrefixList m_npl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500228
229 friend std::ostream&
230 operator<<(std::ostream& os, const NameLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500231};
232
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500233class AdjLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500234{
235public:
alvydce3f182015-04-09 11:23:30 -0500236 typedef AdjacencyList::const_iterator const_iterator;
237
akmhoque53353462014-04-22 08:43:45 -0500238 AdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500239 {
akmhoque53353462014-04-22 08:43:45 -0500240 }
241
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600242 AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500243 const ndn::time::system_clock::TimePoint& lt,
244 uint32_t nl , AdjacencyList& adl);
akmhoque53353462014-04-22 08:43:45 -0500245
Nick Gordon727d4832017-10-13 18:04:25 -0500246 Lsa::Type
247 getType() const override
248 {
249 return Lsa::Type::ADJACENCY;
250 }
251
akmhoquec8a10f72014-04-25 18:42:55 -0500252 AdjacencyList&
akmhoque53353462014-04-22 08:43:45 -0500253 getAdl()
254 {
255 return m_adl;
256 }
257
Nick Gordon22b5c952017-08-10 17:48:15 -0500258 const AdjacencyList&
259 getAdl() const
260 {
261 return m_adl;
262 }
263
akmhoque53353462014-04-22 08:43:45 -0500264 void
265 addAdjacent(Adjacent adj)
266 {
267 m_adl.insert(adj);
268 }
269
Nick G97e34942016-07-11 14:46:27 -0500270 /*! \brief Initializes this adj. LSA from the supplied content.
271
272 \param content The content that this LSA is to have, formatted
273 according to getData().
274 */
akmhoque53353462014-04-22 08:43:45 -0500275 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500276 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500277
278 uint32_t
279 getNoLink()
280 {
281 return m_noLink;
282 }
283
284 bool
Ryan Wickmanf3c79a62018-05-10 19:32:32 -0500285 isEqualContent(const AdjLsa& alsa) const;
akmhoque53353462014-04-22 08:43:45 -0500286
akmhoque674b0b12014-05-20 14:33:28 -0500287 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500288 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500289
alvydce3f182015-04-09 11:23:30 -0500290 const_iterator
291 begin() const
292 {
293 return m_adl.begin();
294 }
295
296 const_iterator
297 end() const
298 {
299 return m_adl.end();
300 }
301
Nick Gordonfaf49f42017-10-23 12:36:28 -0500302 /*! \brief Returns the data this adjacency LSA has.
303
304 The format is: \<original
305 router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
306 1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
307 n\>|\<cost n\>|
308 */
309 std::string
310 serialize() const override;
311
akmhoque53353462014-04-22 08:43:45 -0500312private:
313 uint32_t m_noLink;
akmhoquec8a10f72014-04-25 18:42:55 -0500314 AdjacencyList m_adl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500315
316 friend std::ostream&
317 operator<<(std::ostream& os, const AdjLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500318};
319
Alexander Afanasyev67758b12018-03-06 18:36:44 -0500320class CoordinateLsa : public Lsa
akmhoque53353462014-04-22 08:43:45 -0500321{
322public:
akmhoqueb6450b12014-04-24 00:01:03 -0500323 CoordinateLsa()
Nick Gordon727d4832017-10-13 18:04:25 -0500324 : m_corRad(0)
akmhoque53353462014-04-22 08:43:45 -0500325 {
akmhoque53353462014-04-22 08:43:45 -0500326 }
327
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600328 CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500329 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600330 double r, std::vector<double> theta);
akmhoque53353462014-04-22 08:43:45 -0500331
Nick Gordon727d4832017-10-13 18:04:25 -0500332 Lsa::Type
333 getType() const override
334 {
335 return Lsa::Type::COORDINATE;
336 }
337
Nick G97e34942016-07-11 14:46:27 -0500338 /*! \brief Initializes this coordinate LSA with the data in content.
339
340 \param content The string content that is used to build the LSA.
341
342 This function initializes this LSA object to represent the data
343 specified by the parameter. The format that it is expecting is the
344 same as for getData();
345 */
akmhoque53353462014-04-22 08:43:45 -0500346 bool
Nick Gordon2f623382017-11-03 13:49:31 -0500347 deserialize(const std::string& content) noexcept override;
akmhoque53353462014-04-22 08:43:45 -0500348
349 double
akmhoqueb6450b12014-04-24 00:01:03 -0500350 getCorRadius() const
akmhoque53353462014-04-22 08:43:45 -0500351 {
akmhoque53353462014-04-22 08:43:45 -0500352 return m_corRad;
akmhoque53353462014-04-22 08:43:45 -0500353 }
354
355 void
356 setCorRadius(double cr)
357 {
akmhoque157b0a42014-05-13 00:26:37 -0500358 m_corRad = cr;
akmhoque53353462014-04-22 08:43:45 -0500359 }
360
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600361 const std::vector<double>
akmhoqueb6450b12014-04-24 00:01:03 -0500362 getCorTheta() const
akmhoque53353462014-04-22 08:43:45 -0500363 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600364 return m_angles;
akmhoque53353462014-04-22 08:43:45 -0500365 }
366
367 void
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600368 setCorTheta(std::vector<double> ct)
akmhoque53353462014-04-22 08:43:45 -0500369 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600370 m_angles = ct;
akmhoque53353462014-04-22 08:43:45 -0500371 }
372
373 bool
Ryan Wickmanf3c79a62018-05-10 19:32:32 -0500374 isEqualContent(const CoordinateLsa& clsa) const;
akmhoque53353462014-04-22 08:43:45 -0500375
akmhoque674b0b12014-05-20 14:33:28 -0500376 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500377 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500378
Nick Gordonfaf49f42017-10-23 12:36:28 -0500379 /*! \brief Returns the data that this coordinate LSA represents.
380
381 The format is: \<original
382 router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
383 */
384 std::string
385 serialize() const override;
386
akmhoque53353462014-04-22 08:43:45 -0500387private:
388 double m_corRad;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600389 std::vector<double> m_angles;
Nick Gordonae0a0472017-10-23 15:51:23 -0500390
391 friend std::ostream&
392 operator<<(std::ostream& os, const CoordinateLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500393};
394
alvydce3f182015-04-09 11:23:30 -0500395std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500396operator<<(std::ostream& os, const AdjLsa& lsa);
397
398std::ostream&
399operator<<(std::ostream& os, const CoordinateLsa& lsa);
400
401std::ostream&
402operator<<(std::ostream& os, const NameLsa& lsa);
alvydce3f182015-04-09 11:23:30 -0500403
Nick Gordon727d4832017-10-13 18:04:25 -0500404std::ostream&
405operator<<(std::ostream& os, const Lsa::Type& type);
406
407std::istream&
408operator>>(std::istream& is, Lsa::Type& type);
409
Nick Gordonfad8e252016-08-11 14:21:38 -0500410} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500411
Nick Gordon727d4832017-10-13 18:04:25 -0500412namespace std {
413 std::string
414 to_string(const nlsr::Lsa::Type& type);
415} // namespace std
416
Nick Gordon56d1fae2017-05-26 16:39:25 -0500417#endif // NLSR_LSA_HPP