blob: 8bc9e30bb76f383bc8a46bf53fbd15cd36be6056 [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>
32
akmhoque53353462014-04-22 08:43:45 -050033namespace nlsr {
akmhoque31d1d4b2014-05-05 22:08:14 -050034
Vince Lehmanf7eec4f2015-05-08 19:02:31 -050035class Nlsr;
36
akmhoque53353462014-04-22 08:43:45 -050037class Lsa
38{
39public:
Nick Gordon727d4832017-10-13 18:04:25 -050040 enum class Type {
41 ADJACENCY,
42 COORDINATE,
43 NAME,
Nick Gordon9212bd42017-10-23 10:59:38 -050044 BASE,
45 MOCK
Nick Gordon727d4832017-10-13 18:04:25 -050046 };
47
48 Lsa()
akmhoque53353462014-04-22 08:43:45 -050049 : m_origRouter()
50 , m_lsSeqNo()
akmhoquec7a79b22014-05-26 08:06:19 -050051 , m_expirationTimePoint()
akmhoque53353462014-04-22 08:43:45 -050052 , m_expiringEventId()
53 {
54 }
55
Nick Gordon727d4832017-10-13 18:04:25 -050056 virtual Type
57 getType() const
akmhoque53353462014-04-22 08:43:45 -050058 {
Nick Gordon727d4832017-10-13 18:04:25 -050059 return Type::BASE;
akmhoque53353462014-04-22 08:43:45 -050060 }
61
62 void
63 setLsSeqNo(uint32_t lsn)
64 {
65 m_lsSeqNo = lsn;
66 }
67
68 uint32_t
69 getLsSeqNo() const
70 {
71 return m_lsSeqNo;
72 }
73
akmhoque31d1d4b2014-05-05 22:08:14 -050074 const ndn::Name&
akmhoque53353462014-04-22 08:43:45 -050075 getOrigRouter() const
76 {
77 return m_origRouter;
78 }
79
80 void
akmhoque31d1d4b2014-05-05 22:08:14 -050081 setOrigRouter(const ndn::Name& org)
akmhoque53353462014-04-22 08:43:45 -050082 {
83 m_origRouter = org;
84 }
85
akmhoquec7a79b22014-05-26 08:06:19 -050086 const ndn::time::system_clock::TimePoint&
87 getExpirationTimePoint() const
akmhoque53353462014-04-22 08:43:45 -050088 {
akmhoquec7a79b22014-05-26 08:06:19 -050089 return m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -050090 }
91
92 void
akmhoquec7a79b22014-05-26 08:06:19 -050093 setExpirationTimePoint(const ndn::time::system_clock::TimePoint& lt)
akmhoque53353462014-04-22 08:43:45 -050094 {
akmhoquec7a79b22014-05-26 08:06:19 -050095 m_expirationTimePoint = lt;
akmhoque53353462014-04-22 08:43:45 -050096 }
97
98 void
99 setExpiringEventId(const ndn::EventId leei)
100 {
101 m_expiringEventId = leei;
102 }
103
104 ndn::EventId
105 getExpiringEventId() const
106 {
107 return m_expiringEventId;
108 }
109
Nick Gordonfaf49f42017-10-23 12:36:28 -0500110 /*! \brief Return the data that this LSA represents.
111 */
Nick Gordon9212bd42017-10-23 10:59:38 -0500112 virtual std::string
Nick Gordonfaf49f42017-10-23 12:36:28 -0500113 serialize() const = 0;
Nick Gordon9212bd42017-10-23 10:59:38 -0500114
Nick Gordon22cc1a82017-10-23 13:06:53 -0500115 /*! \brief Gets the key for this LSA.
116
117 Format is: \<router name\>/\<LSA type>\
118 */
119 const ndn::Name
120 getKey() const;
121
Nick Gordon9212bd42017-10-23 10:59:38 -0500122 virtual bool
123 initializeFromContent(const std::string& content) = 0;
124
akmhoque53353462014-04-22 08:43:45 -0500125protected:
Nick Gordonfaf49f42017-10-23 12:36:28 -0500126 /*! Get data common to all LSA types.
127
128 This method should be called by all LSA classes in their
129 serialize() method.
130 */
131 std::string
132 getData() const;
133
134protected:
akmhoque31d1d4b2014-05-05 22:08:14 -0500135 ndn::Name m_origRouter;
akmhoque53353462014-04-22 08:43:45 -0500136 uint32_t m_lsSeqNo;
akmhoquec7a79b22014-05-26 08:06:19 -0500137 ndn::time::system_clock::TimePoint m_expirationTimePoint;
akmhoque53353462014-04-22 08:43:45 -0500138 ndn::EventId m_expiringEventId;
139};
140
141class NameLsa: public Lsa
142{
143public:
144 NameLsa()
akmhoque53353462014-04-22 08:43:45 -0500145 {
akmhoque53353462014-04-22 08:43:45 -0500146 }
147
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600148 NameLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500149 const ndn::time::system_clock::TimePoint& lt,
akmhoquefdbddb12014-05-02 18:35:19 -0500150 NamePrefixList& npl);
akmhoque53353462014-04-22 08:43:45 -0500151
Nick Gordon727d4832017-10-13 18:04:25 -0500152 Lsa::Type
153 getType() const override
154 {
155 return Lsa::Type::NAME;
156 }
157
akmhoquec8a10f72014-04-25 18:42:55 -0500158 NamePrefixList&
akmhoque53353462014-04-22 08:43:45 -0500159 getNpl()
160 {
161 return m_npl;
162 }
163
Nick Gordon56d1fae2017-05-26 16:39:25 -0500164 const NamePrefixList&
165 getNpl() const
166 {
167 return m_npl;
168 }
169
akmhoque53353462014-04-22 08:43:45 -0500170 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 addName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500172 {
173 m_npl.insert(name);
174 }
175
176 void
akmhoque31d1d4b2014-05-05 22:08:14 -0500177 removeName(const ndn::Name& name)
akmhoque53353462014-04-22 08:43:45 -0500178 {
179 m_npl.remove(name);
180 }
181
Nick G97e34942016-07-11 14:46:27 -0500182 /*! \brief Initializes this LSA object with content's data.
183
184 \param content The data (e.g. name prefixes) to initialize this LSA with.
185
186 This function initializes this object to represent the data
187 contained in content. The format for this is the same as for
188 getData(); getData() returns data of this format, in other words.
189 */
akmhoque53353462014-04-22 08:43:45 -0500190 bool
Nick Gordon9212bd42017-10-23 10:59:38 -0500191 initializeFromContent(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500192
Nick Gordon56d1fae2017-05-26 16:39:25 -0500193 bool
194 isEqualContent(const NameLsa& other) const;
195
akmhoque53353462014-04-22 08:43:45 -0500196 void
197 writeLog();
198
Nick Gordonfaf49f42017-10-23 12:36:28 -0500199 /*! \brief Returns the data that this name LSA has.
200
201 Format is: \<original router
202 prefix\>|name|\<seq. no.\>|\<exp. time\>|\<prefix 1\>|\<prefix
203 2\>|...|\<prefix n\>|
204 */
205 std::string
206 serialize() const override;
207
akmhoque53353462014-04-22 08:43:45 -0500208private:
akmhoquec8a10f72014-04-25 18:42:55 -0500209 NamePrefixList m_npl;
akmhoque53353462014-04-22 08:43:45 -0500210};
211
akmhoque53353462014-04-22 08:43:45 -0500212class AdjLsa: public Lsa
213{
214public:
alvydce3f182015-04-09 11:23:30 -0500215 typedef AdjacencyList::const_iterator const_iterator;
216
akmhoque53353462014-04-22 08:43:45 -0500217 AdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500218 {
akmhoque53353462014-04-22 08:43:45 -0500219 }
220
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600221 AdjLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500222 const ndn::time::system_clock::TimePoint& lt,
223 uint32_t nl , AdjacencyList& adl);
akmhoque53353462014-04-22 08:43:45 -0500224
Nick Gordon727d4832017-10-13 18:04:25 -0500225 Lsa::Type
226 getType() const override
227 {
228 return Lsa::Type::ADJACENCY;
229 }
230
akmhoquec8a10f72014-04-25 18:42:55 -0500231 AdjacencyList&
akmhoque53353462014-04-22 08:43:45 -0500232 getAdl()
233 {
234 return m_adl;
235 }
236
Nick Gordon22b5c952017-08-10 17:48:15 -0500237 const AdjacencyList&
238 getAdl() const
239 {
240 return m_adl;
241 }
242
akmhoque53353462014-04-22 08:43:45 -0500243 void
244 addAdjacent(Adjacent adj)
245 {
246 m_adl.insert(adj);
247 }
248
Nick G97e34942016-07-11 14:46:27 -0500249 /*! \brief Initializes this adj. LSA from the supplied content.
250
251 \param content The content that this LSA is to have, formatted
252 according to getData().
253 */
akmhoque53353462014-04-22 08:43:45 -0500254 bool
Nick Gordon9212bd42017-10-23 10:59:38 -0500255 initializeFromContent(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500256
257 uint32_t
258 getNoLink()
259 {
260 return m_noLink;
261 }
262
263 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500264 isEqualContent(AdjLsa& alsa);
akmhoque53353462014-04-22 08:43:45 -0500265
Nick G97e34942016-07-11 14:46:27 -0500266 /*! \brief Installs this LSA's name prefixes into the NPT.
267
268 \param pnlsr The NLSR top-level whose NPT you want to install the
269 entries into.
270 */
akmhoque53353462014-04-22 08:43:45 -0500271 void
272 addNptEntries(Nlsr& pnlsr);
273
274 void
275 removeNptEntries(Nlsr& pnlsr);
276
akmhoque674b0b12014-05-20 14:33:28 -0500277 void
278 writeLog();
279
alvydce3f182015-04-09 11:23:30 -0500280 const_iterator
281 begin() const
282 {
283 return m_adl.begin();
284 }
285
286 const_iterator
287 end() const
288 {
289 return m_adl.end();
290 }
291
Nick Gordonfaf49f42017-10-23 12:36:28 -0500292 /*! \brief Returns the data this adjacency LSA has.
293
294 The format is: \<original
295 router\>|adjacency|\<seq. no.\>|\<exp. time\>|\<size\>|\<adjacency prefix
296 1\>|\<face uri 1\>|\<cost 1\>|...|\<adjacency prefix n\>|\<face uri
297 n\>|\<cost n\>|
298 */
299 std::string
300 serialize() const override;
301
akmhoque53353462014-04-22 08:43:45 -0500302private:
303 uint32_t m_noLink;
akmhoquec8a10f72014-04-25 18:42:55 -0500304 AdjacencyList m_adl;
akmhoque53353462014-04-22 08:43:45 -0500305};
306
akmhoqueb6450b12014-04-24 00:01:03 -0500307class CoordinateLsa: public Lsa
akmhoque53353462014-04-22 08:43:45 -0500308{
309public:
akmhoqueb6450b12014-04-24 00:01:03 -0500310 CoordinateLsa()
Nick Gordon727d4832017-10-13 18:04:25 -0500311 : m_corRad(0)
akmhoque53353462014-04-22 08:43:45 -0500312 {
akmhoque53353462014-04-22 08:43:45 -0500313 }
314
Ashlesh Gawanded02c3882015-12-29 16:02:51 -0600315 CoordinateLsa(const ndn::Name& origR, uint32_t lsn,
akmhoquec7a79b22014-05-26 08:06:19 -0500316 const ndn::time::system_clock::TimePoint& lt,
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600317 double r, std::vector<double> theta);
akmhoque53353462014-04-22 08:43:45 -0500318
Nick Gordon727d4832017-10-13 18:04:25 -0500319 Lsa::Type
320 getType() const override
321 {
322 return Lsa::Type::COORDINATE;
323 }
324
Nick G97e34942016-07-11 14:46:27 -0500325 /*! \brief Initializes this coordinate LSA with the data in content.
326
327 \param content The string content that is used to build the LSA.
328
329 This function initializes this LSA object to represent the data
330 specified by the parameter. The format that it is expecting is the
331 same as for getData();
332 */
akmhoque53353462014-04-22 08:43:45 -0500333 bool
Nick Gordon9212bd42017-10-23 10:59:38 -0500334 initializeFromContent(const std::string& content) override;
akmhoque53353462014-04-22 08:43:45 -0500335
336 double
akmhoqueb6450b12014-04-24 00:01:03 -0500337 getCorRadius() const
akmhoque53353462014-04-22 08:43:45 -0500338 {
akmhoque53353462014-04-22 08:43:45 -0500339 return m_corRad;
akmhoque53353462014-04-22 08:43:45 -0500340 }
341
342 void
343 setCorRadius(double cr)
344 {
akmhoque157b0a42014-05-13 00:26:37 -0500345 m_corRad = cr;
akmhoque53353462014-04-22 08:43:45 -0500346 }
347
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600348 const std::vector<double>
akmhoqueb6450b12014-04-24 00:01:03 -0500349 getCorTheta() const
akmhoque53353462014-04-22 08:43:45 -0500350 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600351 return m_angles;
akmhoque53353462014-04-22 08:43:45 -0500352 }
353
354 void
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600355 setCorTheta(std::vector<double> ct)
akmhoque53353462014-04-22 08:43:45 -0500356 {
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600357 m_angles = ct;
akmhoque53353462014-04-22 08:43:45 -0500358 }
359
360 bool
akmhoquefdbddb12014-05-02 18:35:19 -0500361 isEqualContent(const CoordinateLsa& clsa);
akmhoque53353462014-04-22 08:43:45 -0500362
akmhoque674b0b12014-05-20 14:33:28 -0500363 void
364 writeLog();
365
Nick Gordonfaf49f42017-10-23 12:36:28 -0500366 /*! \brief Returns the data that this coordinate LSA represents.
367
368 The format is: \<original
369 router\>|coordinate|\<seq. no.\>|\<exp. time\>|\<radians\>|\<theta\>|
370 */
371 std::string
372 serialize() const override;
373
akmhoque53353462014-04-22 08:43:45 -0500374private:
375 double m_corRad;
Muktadir R Chowdhuryb00dc2a2016-11-05 10:48:58 -0600376 std::vector<double> m_angles;
akmhoque53353462014-04-22 08:43:45 -0500377};
378
alvydce3f182015-04-09 11:23:30 -0500379std::ostream&
380operator<<(std::ostream& os, const AdjLsa& adjLsa);
381
Nick Gordon727d4832017-10-13 18:04:25 -0500382std::ostream&
383operator<<(std::ostream& os, const Lsa::Type& type);
384
385std::istream&
386operator>>(std::istream& is, Lsa::Type& type);
387
Nick Gordonfad8e252016-08-11 14:21:38 -0500388} // namespace nlsr
akmhoque53353462014-04-22 08:43:45 -0500389
Nick Gordon727d4832017-10-13 18:04:25 -0500390namespace std {
391 std::string
392 to_string(const nlsr::Lsa::Type& type);
393} // namespace std
394
Nick Gordon56d1fae2017-05-26 16:39:25 -0500395#endif // NLSR_LSA_HPP