blob: 859a11ba33d42c01abdff0334c709f555156f681 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonc6a85222017-01-03 16:54:34 -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#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
Nick Gordon727d4832017-10-13 18:04:25 -050057 virtual Type
58 getType() const
akmhoque53353462014-04-22 08:43:45 -050059 {
Nick Gordon727d4832017-10-13 18:04:25 -050060 return Type::BASE;
akmhoque53353462014-04-22 08:43:45 -050061 }
62
63 void
64 setLsSeqNo(uint32_t lsn)
65 {
66 m_lsSeqNo = lsn;
67 }
68
69 uint32_t
70 getLsSeqNo() const
71 {
72 return m_lsSeqNo;
73 }
74
akmhoque31d1d4b2014-05-05 22:08:14 -050075 const ndn::Name&
akmhoque53353462014-04-22 08:43:45 -050076 getOrigRouter() const
77 {
78 return m_origRouter;
79 }
80
81 void
akmhoque31d1d4b2014-05-05 22:08:14 -050082 setOrigRouter(const ndn::Name& org)
akmhoque53353462014-04-22 08:43:45 -050083 {
84 m_origRouter = org;
85 }
86
akmhoquec7a79b22014-05-26 08:06:19 -050087 const ndn::time::system_clock::TimePoint&
88 getExpirationTimePoint() const
akmhoque53353462014-04-22 08:43:45 -050089 {
akmhoquec7a79b22014-05-26 08:06:19 -050090 return m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -050091 }
92
93 void
akmhoquec7a79b22014-05-26 08:06:19 -050094 setExpirationTimePoint(const ndn::time::system_clock::TimePoint& lt)
akmhoque53353462014-04-22 08:43:45 -050095 {
akmhoquec7a79b22014-05-26 08:06:19 -050096 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -050097 }
98
99 void
100 setExpiringEventId(const ndn::EventId leei)
101 {
102 m_expiringEventId = leei;
103 }
104
105 ndn::EventId
106 getExpiringEventId() const
107 {
108 return m_expiringEventId;
109 }
110
Nick Gordonfaf49f42017-10-23 12:36:28 -0500111 /*! \brief Return the data that this LSA represents.
112 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500113 virtual std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500114 serialize() const = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500115
Nick Gordon22cc1a82017-10-23 13:06:53 -0500116 /*! \brief Gets the key for this LSA.
117
118 Format is: \<router name\>/\<LSA type>\
119 */
120 const ndn::Name
121 getKey() const;
122
Nick Gordon9212bd42017-10-23 10:59:38 -0500123 virtual bool
Nick Gordon0fa4c772017-10-23 13:33:03 -0500124 deserialize(const std::string& content) = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500125
Nick Gordonae0a0472017-10-23 15:51:23 -0500126 virtual void
127 writeLog() const = 0;
128
akmhoque53353462014-04-22 08:43:45 -0500129protected:
Nick Gordonfaf49f42017-10-23 12:36:28 -0500130 /*! Get data common to all LSA types.
131
132 This method should be called by all LSA classes in their
133 serialize() method.
134 */
135 std::string
136 getData() const;
137
Nick Gordonae0a0472017-10-23 15:51:23 -0500138 /*! Print data common to all LSA types.
139 */
140 std::string
141 toString() const;
142
Nick Gordon0fa4c772017-10-23 13:33:03 -0500143 bool
144 deserializeCommon(boost::tokenizer<boost::char_separator<char>>::iterator& iterator);
145
Nick Gordonfaf49f42017-10-23 12:36:28 -0500146protected:
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 ndn::Name m_origRouter;
akmhoque53353462014-04-22 08:43:45 -0500148 uint32_t m_lsSeqNo;
akmhoquec7a79b22014-05-26 08:06:19 -0500149 ndn::time::system_clock::TimePoint m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -0500150 ndn::EventId m_expiringEventId;
151};
152
153class NameLsa: public Lsa
154{
155public:
156 NameLsa()
akmhoque53353462014-04-22 08:43:45 -0500157 {
akmhoque53353462014-04-22 08:43:45 -0500158 }
159
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600160 NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500161 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500162 NamePrefixList& npl);
akmhoque53353462014-04-22 08:43:45 -0500163
Nick Gordon727d4832017-10-13 18:04:25 -0500164 Lsa::Type
165 getType() const override
166 {
167 return Lsa::Type::NAME;
168 }
169
akmhoquec8a10f72014-04-25 18:42:55 -0500170 NamePrefixList&
akmhoque53353462014-04-22 08:43:45 -0500171 getNpl()
172 {
173 return m_npl;
174 }
175
Nick Gordon56d1fae2017-05-26 16:39:25 -0500176 const NamePrefixList&
177 getNpl() const
178 {
179 return m_npl;
180 }
181
akmhoque53353462014-04-22 08:43:45 -0500182 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500183 addName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500184 {
185 m_npl.insert(name);
186 }
187
188 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500189 removeName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500190 {
191 m_npl.remove(name);
192 }
193
Nick G97e34942016-07-11 14:46:27 -0500194 /*! \brief Initializes this LSA object with content's data.
195
196 \param content The data (e.g. name prefixes) to initialize this LSA with.
197
198 This function initializes this object to represent the data
199 contained in content. The format for this is the same as for
200 getData(); getData() returns data of this format, in other words.
201 */
akmhoque53353462014-04-22 08:43:45 -0500202 bool
Nick Gordon0fa4c772017-10-23 13:33:03 -0500203 deserialize(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500204
Nick Gordon56d1fae2017-05-26 16:39:25 -0500205 bool
206 isEqualContent(const NameLsa& other) const;
207
akmhoque53353462014-04-22 08:43:45 -0500208 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500209 writeLog() const override;
akmhoque53353462014-04-22 08:43:45 -0500210
Nick Gordonfaf49f42017-10-23 12:36:28 -0500211 /*! \brief Returns the data that this name LSA has.
212
213 Format is: \<original router
214 prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
215 2\>|...|\<prefix n\>|
216 */
217 std::string
218 serialize() const override;
219
akmhoque53353462014-04-22 08:43:45 -0500220private:
akmhoquec8a10f72014-04-25 18:42:55 -0500221 NamePrefixList m_npl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500222
223 friend std::ostream&
224 operator<<(std::ostream& os, const NameLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500225};
226
akmhoque53353462014-04-22 08:43:45 -0500227class AdjLsa: public Lsa
228{
229public:
alvydce3f182015-04-09 11:23:30 -0500230 typedef AdjacencyList::const_iterator const_iterator;
231
akmhoque53353462014-04-22 08:43:45 -0500232 AdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500233 {
akmhoque53353462014-04-22 08:43:45 -0500234 }
235
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600236 AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500237 const ndn::time::system_clock::TimePoint& lt,
238 uint32_t nl , AdjacencyList& adl);
akmhoque53353462014-04-22 08:43:45 -0500239
Nick Gordon727d4832017-10-13 18:04:25 -0500240 Lsa::Type
241 getType() const override
242 {
243 return Lsa::Type::ADJACENCY;
244 }
245
akmhoquec8a10f72014-04-25 18:42:55 -0500246 AdjacencyList&
akmhoque53353462014-04-22 08:43:45 -0500247 getAdl()
248 {
249 return m_adl;
250 }
251
Nick Gordon22b5c952017-08-10 17:48:15 -0500252 const AdjacencyList&
253 getAdl() const
254 {
255 return m_adl;
256 }
257
akmhoque53353462014-04-22 08:43:45 -0500258 void
259 addAdjacent(Adjacent adj)
260 {
261 m_adl.insert(adj);
262 }
263
Nick G97e34942016-07-11 14:46:27 -0500264 /*! \brief Initializes this adj. LSA from the supplied content.
265
266 \param content The content that this LSA is to have, formatted
267 according to getData().
268 */
akmhoque53353462014-04-22 08:43:45 -0500269 bool
Nick Gordon0fa4c772017-10-23 13:33:03 -0500270 deserialize(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500271
272 uint32_t
273 getNoLink()
274 {
275 return m_noLink;
276 }
277
278 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500279 isEqualContent(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500280
Nick G97e34942016-07-11 14:46:27 -0500281 /*! \brief Installs this LSA's name prefixes into the NPT.
282
283 \param pnlsr The NLSR top-level whose NPT you want to install the
284 entries into.
285 */
akmhoque53353462014-04-22 08:43:45 -0500286 void
287 addNptEntries(Nlsr& pnlsr);
288
289 void
290 removeNptEntries(Nlsr& pnlsr);
291
akmhoque674b0b12014-05-20 14:33:28 -0500292 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500293 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500294
alvydce3f182015-04-09 11:23:30 -0500295 const_iterator
296 begin() const
297 {
298 return m_adl.begin();
299 }
300
301 const_iterator
302 end() const
303 {
304 return m_adl.end();
305 }
306
Nick Gordonfaf49f42017-10-23 12:36:28 -0500307 /*! \brief Returns the data this adjacency LSA has.
308
309 The format is: \<original
310 router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
311 1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
312 n\>|\<cost n\>|
313 */
314 std::string
315 serialize() const override;
316
akmhoque53353462014-04-22 08:43:45 -0500317private:
318 uint32_t m_noLink;
akmhoquec8a10f72014-04-25 18:42:55 -0500319 AdjacencyList m_adl;
Nick Gordonae0a0472017-10-23 15:51:23 -0500320
321 friend std::ostream&
322 operator<<(std::ostream& os, const AdjLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500323};
324
akmhoqueb6450b12014-04-24 00:01:03 -0500325class CoordinateLsa: public Lsa
akmhoque53353462014-04-22 08:43:45 -0500326{
327public:
akmhoqueb6450b12014-04-24 00:01:03 -0500328 CoordinateLsa()
Nick Gordon727d4832017-10-13 18:04:25 -0500329 : m_corRad(0)
akmhoque53353462014-04-22 08:43:45 -0500330 {
akmhoque53353462014-04-22 08:43:45 -0500331 }
332
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600333 CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500334 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600335 double r, std::vector<double> theta);
akmhoque53353462014-04-22 08:43:45 -0500336
Nick Gordon727d4832017-10-13 18:04:25 -0500337 Lsa::Type
338 getType() const override
339 {
340 return Lsa::Type::COORDINATE;
341 }
342
Nick G97e34942016-07-11 14:46:27 -0500343 /*! \brief Initializes this coordinate LSA with the data in content.
344
345 \param content The string content that is used to build the LSA.
346
347 This function initializes this LSA object to represent the data
348 specified by the parameter. The format that it is expecting is the
349 same as for getData();
350 */
akmhoque53353462014-04-22 08:43:45 -0500351 bool
Nick Gordon0fa4c772017-10-23 13:33:03 -0500352 deserialize(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500353
354 double
akmhoqueb6450b12014-04-24 00:01:03 -0500355 getCorRadius() const
akmhoque53353462014-04-22 08:43:45 -0500356 {
akmhoque53353462014-04-22 08:43:45 -0500357 return m_corRad;
akmhoque53353462014-04-22 08:43:45 -0500358 }
359
360 void
361 setCorRadius(double cr)
362 {
akmhoque157b0a42014-05-13 00:26:37 -0500363 m_corRad = cr;
akmhoque53353462014-04-22 08:43:45 -0500364 }
365
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600366 const std::vector<double>
akmhoqueb6450b12014-04-24 00:01:03 -0500367 getCorTheta() const
akmhoque53353462014-04-22 08:43:45 -0500368 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600369 return m_angles;
akmhoque53353462014-04-22 08:43:45 -0500370 }
371
372 void
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600373 setCorTheta(std::vector<double> ct)
akmhoque53353462014-04-22 08:43:45 -0500374 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600375 m_angles = ct;
akmhoque53353462014-04-22 08:43:45 -0500376 }
377
378 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500379 isEqualContent(const CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500380
akmhoque674b0b12014-05-20 14:33:28 -0500381 void
Nick Gordonae0a0472017-10-23 15:51:23 -0500382 writeLog() const override;
akmhoque674b0b12014-05-20 14:33:28 -0500383
Nick Gordonfaf49f42017-10-23 12:36:28 -0500384 /*! \brief Returns the data that this coordinate LSA represents.
385
386 The format is: \<original
387 router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
388 */
389 std::string
390 serialize() const override;
391
akmhoque53353462014-04-22 08:43:45 -0500392private:
393 double m_corRad;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600394 std::vector<double> m_angles;
Nick Gordonae0a0472017-10-23 15:51:23 -0500395
396 friend std::ostream&
397 operator<<(std::ostream& os, const CoordinateLsa& lsa);
akmhoque53353462014-04-22 08:43:45 -0500398};
399
alvydce3f182015-04-09 11:23:30 -0500400std::ostream&
Nick Gordonae0a0472017-10-23 15:51:23 -0500401operator<<(std::ostream& os, const AdjLsa& lsa);
402
403std::ostream&
404operator<<(std::ostream& os, const CoordinateLsa& lsa);
405
406std::ostream&
407operator<<(std::ostream& os, const NameLsa& lsa);
alvydce3f182015-04-09 11:23:30 -0500408
Nick Gordon727d4832017-10-13 18:04:25 -0500409std::ostream&
410operator<<(std::ostream& os, const Lsa::Type& type);
411
412std::istream&
413operator>>(std::istream& is, Lsa::Type& type);
414
Nick Gordonfad8e252016-08-11 14:21:38 -0500415} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500416
Nick Gordon727d4832017-10-13 18:04:25 -0500417namespace std {
418 std::string
419 to_string(const nlsr::Lsa::Type& type);
420} // namespace std
421
Nick Gordon56d1fae2017-05-26 16:39:25 -0500422#endif // NLSR_LSA_HPP