blob: 2d7861042040502f9fea5abf4bb5d446f69c0635 [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -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#include "lsdb.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050023
akmhoque674b0b12014-05-20 14:33:28 -050024#include "logger.hpp"
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050025#include "nlsr.hpp"
26#include "publisher/segment-publisher.hpp"
27#include "utility/name-helper.hpp"
28
29#include <ndn-cxx/security/signing-helpers.hpp>
30#include <ndn-cxx/util/segment-fetcher.hpp>
31
akmhoque53353462014-04-22 08:43:45 -050032namespace nlsr {
33
akmhoque674b0b12014-05-20 14:33:28 -050034INIT_LOGGER("Lsdb");
35
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050036class LsaContentPublisher : public SegmentPublisher<ndn::Face>
37{
38public:
39 LsaContentPublisher(ndn::Face& face,
40 ndn::KeyChain& keyChain,
41 const ndn::time::milliseconds& freshnessPeriod,
42 const std::string& content)
43 : SegmentPublisher(face, keyChain, freshnessPeriod)
44 , m_content(content)
45 {
46 }
47
48 virtual size_t
49 generate(ndn::EncodingBuffer& outBuffer) {
50 size_t totalLength = 0;
51 totalLength += outBuffer.prependByteArray(reinterpret_cast<const uint8_t*>(m_content.c_str()),
52 m_content.size());
53 return totalLength;
54 }
55
56private:
57 const std::string m_content;
58};
59
Jiewen Tana0497d82015-02-02 21:59:18 -080060const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050061const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Nick Gordone98480b2017-05-24 11:23:03 -050062const ndn::time::steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE =
63 ndn::time::steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050064
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050065Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler)
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050066 : m_nlsr(nlsr)
67 , m_scheduler(scheduler)
Nick Gordon9eac4d92017-08-29 17:31:29 -050068 , m_sync(m_nlsr.getNlsrFace(),
Nick Gordon727d4832017-10-13 18:04:25 -050069 [this] (const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon9eac4d92017-08-29 17:31:29 -050070 const uint64_t& sequenceNumber) {
Nick Gordon8f23b5d2017-08-31 17:53:07 -050071 return isLsaNew(routerName, lsaType, sequenceNumber);
72 }, m_nlsr.getConfParameter())
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050073 , m_lsaRefreshTime(0)
74 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050075 , m_sequencingManager()
Nick Gordon9eac4d92017-08-29 17:31:29 -050076 , m_onNewLsaConnection(m_sync.onNewLsa->connect(
77 [this] (const ndn::Name& updateName, const uint64_t& sequenceNumber) {
78 ndn::Name lsaInterest{updateName};
79 lsaInterest.appendNumber(sequenceNumber);
80 expressInterest(lsaInterest, 0);
81 }))
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050082{
83}
84
85void
86Lsdb::onFetchLsaError(uint32_t errorCode,
87 const std::string& msg,
88 ndn::Name& interestName,
89 uint32_t retransmitNo,
90 const ndn::time::steady_clock::TimePoint& deadline,
91 ndn::Name lsaName,
92 uint64_t seqNo)
93{
94 _LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
95 << ", Message: " << msg);
96
97 if (ndn::time::steady_clock::now() < deadline) {
98 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
99
100 if (it != m_highestSeqNo.end() && it->second == seqNo) {
101 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
102 // immediately since at the least the LSA Interest lifetime has elapsed.
103 // Otherwise, it is necessary to delay the Interest re-expression to prevent
104 // the potential for constant Interest flooding.
105 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
106
107 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
108 delay = ndn::time::seconds(0);
109 }
110
111 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
112 interestName, retransmitNo + 1, deadline));
113 }
114 }
115}
116
117void
118Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
119{
dmcoomes9f936662017-03-02 10:33:09 -0600120 std::shared_ptr<ndn::Data> data = std::make_shared<ndn::Data>(ndn::Name(interestName));
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500121 data->setContent(bufferPtr);
122
123 _LOG_DEBUG("Received data for LSA(name): " << data->getName());
124
125 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
126 uint64_t seqNo = interestName[-1].toNumber();
127
128 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
129 m_highestSeqNo[lsaName] = seqNo;
130 }
131 else if (seqNo > m_highestSeqNo[lsaName]) {
132 m_highestSeqNo[lsaName] = seqNo;
dmcoomes9eaf3f42017-02-21 11:39:01 -0600133 _LOG_TRACE("SeqNo for LSA(name): " << data->getName() << " updated");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500134 }
135 else if (seqNo < m_highestSeqNo[lsaName]) {
136 return;
137 }
138
139 onContentValidated(data);
140}
akmhoque53353462014-04-22 08:43:45 -0500141
142void
akmhoque31d1d4b2014-05-05 22:08:14 -0500143Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500144{
Vince Lehman7c603292014-09-11 17:48:16 -0500145 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500146}
147
Nick G97e34942016-07-11 14:46:27 -0500148 /*! \brief Compares if a name LSA is the same as the one specified by key
149
150 \param nlsa1 A name LSA object
151 \param key A key of an originating router to compare to nlsa1
152 */
akmhoque53353462014-04-22 08:43:45 -0500153static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500154nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500155{
156 return nlsa1.getKey() == key;
157}
158
akmhoque53353462014-04-22 08:43:45 -0500159bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500160Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500161{
akmhoque31d1d4b2014-05-05 22:08:14 -0500162 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500163 m_sequencingManager.getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500164 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500165 m_nlsr.getNamePrefixList());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500166 m_sequencingManager.increaseNameLsaSeq();
167
168 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500169 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500170
akmhoque31d1d4b2014-05-05 22:08:14 -0500171 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500172}
173
akmhoqueb6450b12014-04-24 00:01:03 -0500174NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500175Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500176{
177 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
178 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600179 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500180 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500181 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500182 }
akmhoqueb6450b12014-04-24 00:01:03 -0500183 return 0;
akmhoque53353462014-04-22 08:43:45 -0500184}
185
186bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500187Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500188{
akmhoqueb6450b12014-04-24 00:01:03 -0500189 NameLsa* nameLsaCheck = findNameLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500190 // Is the name in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500191 if (nameLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500192 // And the supplied seq no is the highest so far
akmhoque157b0a42014-05-13 00:26:37 -0500193 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500194 return true;
195 }
akmhoque157b0a42014-05-13 00:26:37 -0500196 else {
akmhoque53353462014-04-22 08:43:45 -0500197 return false;
198 }
199 }
200 return true;
201}
202
203ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500204Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
205 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500206{
Vince Lehman7c603292014-09-11 17:48:16 -0500207 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500208 std::bind(&Lsdb::expireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500209}
210
211bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500212Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500213{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700214 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500215 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500216 // Determines if the name LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500217 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500218 addNameLsa(nlsa);
akmhoque2f423352014-06-03 11:49:35 -0500219 _LOG_DEBUG("New Name LSA");
220 _LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500221 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500222
akmhoque157b0a42014-05-13 00:26:37 -0500223 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
Nick G97e34942016-07-11 14:46:27 -0500224 // If this name LSA is from another router, add the advertised
225 // prefixes to the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500226 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
227 nlsa.getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500228 std::list<ndn::Name> nameList = nlsa.getNpl().getNames();
akmhoque31d1d4b2014-05-05 22:08:14 -0500229 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500230 it++) {
231 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500232 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500233 }
234 }
235 }
akmhoque157b0a42014-05-13 00:26:37 -0500236 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500237 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
238 ndn::time::system_clock::now();
239 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500240 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500241 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500242 nlsa.getLsSeqNo(),
243 timeToExpire));
244 }
Nick G97e34942016-07-11 14:46:27 -0500245 // Else this is a known name LSA, so we are updating it.
akmhoque157b0a42014-05-13 00:26:37 -0500246 else {
247 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500248 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500249 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500250 chkNameLsa->writeLog();
251 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500252 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500253 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500254 nlsa.getNpl().sort();
Nick G97e34942016-07-11 14:46:27 -0500255 // Obtain the set difference of the current and the incoming
256 // name prefix sets, and add those.
Nick Gordonf14ec352017-07-24 16:09:58 -0500257 std::list<ndn::Name> newNames = nlsa.getNpl().getNames();
258 std::list<ndn::Name> oldNames = chkNameLsa->getNpl().getNames();
259 std::list<ndn::Name> namesToAdd;
260 std::set_difference(newNames.begin(), newNames.end(), oldNames.begin(), oldNames.end(),
261 std::inserter(namesToAdd, namesToAdd.begin()));
262 for (std::list<ndn::Name>::iterator it = namesToAdd.begin();
263 it != namesToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500264 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500265 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
266 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500267 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500268 }
269 }
270 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500271
272 chkNameLsa->getNpl().sort();
273
Nick G97e34942016-07-11 14:46:27 -0500274 // Also remove any names that are no longer being advertised.
Nick Gordonf14ec352017-07-24 16:09:58 -0500275 std::list<ndn::Name> namesToRemove;
276 std::set_difference(oldNames.begin(), oldNames.end(), newNames.begin(), newNames.end(),
277 std::inserter(namesToRemove, namesToRemove.begin()));
278 for (std::list<ndn::Name>::iterator it = namesToRemove.begin();
279 it != namesToRemove.end(); ++it) {
dmcoomes9eaf3f42017-02-21 11:39:01 -0600280 _LOG_DEBUG("Removing name LSA no longer advertised: " << (*it).toUri());
akmhoqueb6450b12014-04-24 00:01:03 -0500281 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500282 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
283 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500284 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500285 }
286 }
287 }
dmcoomes9eaf3f42017-02-21 11:39:01 -0600288
akmhoque157b0a42014-05-13 00:26:37 -0500289 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500290 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
291 ndn::time::system_clock::now();
292 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500293 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500294 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
295 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500296 nlsa.getLsSeqNo(),
297 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500298 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500299 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500300 }
301 }
302 return true;
303}
304
305bool
306Lsdb::addNameLsa(NameLsa& nlsa)
307{
308 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
309 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600310 std::bind(nameLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500311 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500312 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500313 m_nameLsdb.push_back(nlsa);
314 return true;
315 }
316 return false;
317}
318
319bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500320Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500321{
322 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
323 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600324 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500325 if (it != m_nameLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500326 _LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500327 (*it).writeLog();
Nick G97e34942016-07-11 14:46:27 -0500328 // If the requested name LSA is not ours, we also need to remove
329 // its entries from the NPT.
akmhoque31d1d4b2014-05-05 22:08:14 -0500330 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500331 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500332 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
333 (*it).getOrigRouter());
Nick Gordonf14ec352017-07-24 16:09:58 -0500334 for (const auto& name : it->getNpl().getNames()) {
335 if (name != m_nlsr.getConfParameter().getRouterPrefix()) {
336 m_nlsr.getNamePrefixTable().removeEntry(name, it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500337 }
338 }
339 }
340 m_nameLsdb.erase(it);
341 return true;
342 }
343 return false;
344}
345
346bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500347Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500348{
349 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
350 m_nameLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600351 std::bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500352 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500353 return false;
354 }
355 return true;
356}
357
358void
akmhoque2f423352014-06-03 11:49:35 -0500359Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500360{
akmhoque2f423352014-06-03 11:49:35 -0500361 _LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500362 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500363 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500364 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500365 }
366}
367
Jiewen Tana0497d82015-02-02 21:59:18 -0800368const std::list<NameLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500369Lsdb::getNameLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800370{
371 return m_nameLsdb;
372}
373
akmhoque53353462014-04-22 08:43:45 -0500374// Cor LSA and LSDB related Functions start here
375
Nick G97e34942016-07-11 14:46:27 -0500376/*! \brief Compares whether an LSA object is the same as a key.
377 \param clsa The cor. LSA to check the identity of.
378 \param key The key of the publishing router to check against.
379*/
akmhoque53353462014-04-22 08:43:45 -0500380static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500381corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500382{
383 return clsa.getKey() == key;
384}
385
386bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500387Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500388{
akmhoque31d1d4b2014-05-05 22:08:14 -0500389 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500390 m_sequencingManager.getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500391 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500392 m_nlsr.getConfParameter().getCorR(),
393 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500394
395 // Sync coordinate LSAs if using HR or HR dry run.
396 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500397 m_sequencingManager.increaseCorLsaSeq();
398 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500399 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500400 }
401
akmhoque31d1d4b2014-05-05 22:08:14 -0500402 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500403
akmhoque53353462014-04-22 08:43:45 -0500404 return true;
405}
406
akmhoqueb6450b12014-04-24 00:01:03 -0500407CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500408Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500409{
akmhoqueb6450b12014-04-24 00:01:03 -0500410 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
411 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600412 std::bind(corLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500413 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500414 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500415 }
akmhoqueb6450b12014-04-24 00:01:03 -0500416 return 0;
akmhoque53353462014-04-22 08:43:45 -0500417}
418
419bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500420Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500421{
akmhoqueb6450b12014-04-24 00:01:03 -0500422 CoordinateLsa* clsa = findCoordinateLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500423 // Is the coordinate LSA in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500424 if (clsa != 0) {
Nick G97e34942016-07-11 14:46:27 -0500425 // And the seq no is newer (higher) than the current one
akmhoque157b0a42014-05-13 00:26:37 -0500426 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500427 return true;
428 }
akmhoque157b0a42014-05-13 00:26:37 -0500429 else {
akmhoque53353462014-04-22 08:43:45 -0500430 return false;
431 }
432 }
433 return true;
434}
435
Nick G97e34942016-07-11 14:46:27 -0500436 // Schedules a refresh/expire event in the scheduler.
437 // \param key The name of the router that published the LSA.
438 // \param seqNo the seq. no. associated with the LSA to check.
439 // \param expTime How long to wait before triggering the event.
akmhoque53353462014-04-22 08:43:45 -0500440ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500441Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500442 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500443{
Vince Lehman7c603292014-09-11 17:48:16 -0500444 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500445 std::bind(&Lsdb::expireOrRefreshCoordinateLsa,
Vince Lehman7c603292014-09-11 17:48:16 -0500446 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500447}
448
449bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500450Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500451{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700452 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500453 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500454 // Checking whether the LSA is new or not.
akmhoque157b0a42014-05-13 00:26:37 -0500455 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500456 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500457 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500458 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500459 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500460
Nick Gordon5c467f02016-07-13 13:40:10 -0500461 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500462 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500463 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
464 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500465 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500466 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500467 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500468 }
Nick G97e34942016-07-11 14:46:27 -0500469 // Set the expiration time for the new LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500470 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500471 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
472 ndn::time::system_clock::now();
473 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500474 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500475 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500476 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500477 }
Nick G97e34942016-07-11 14:46:27 -0500478 // We are just updating this LSA.
akmhoque157b0a42014-05-13 00:26:37 -0500479 else {
480 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500481 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500482 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500483 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500484 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500485 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500486 // If the new LSA contains new routing information, update the LSDB with it.
akmhoque157b0a42014-05-13 00:26:37 -0500487 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500488 chkCorLsa->setCorRadius(clsa.getCorRadius());
489 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500490 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500491 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500492 }
493 }
Nick G97e34942016-07-11 14:46:27 -0500494 // If this is an LSA from another router, refresh its expiration time.
akmhoque157b0a42014-05-13 00:26:37 -0500495 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500496 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
497 ndn::time::system_clock::now();
498 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500499 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500500 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
501 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500502 clsa.getLsSeqNo(),
503 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500504 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500505 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500506 }
507 }
508 return true;
509}
510
511bool
akmhoqueb6450b12014-04-24 00:01:03 -0500512Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500513{
akmhoqueb6450b12014-04-24 00:01:03 -0500514 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
515 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600516 std::bind(corLsaCompareByKey, _1,
akmhoque157b0a42014-05-13 00:26:37 -0500517 clsa.getKey()));
518 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500519 m_corLsdb.push_back(clsa);
520 return true;
521 }
522 return false;
523}
524
525bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500526Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500527{
akmhoqueb6450b12014-04-24 00:01:03 -0500528 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
529 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600530 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500531 _1, key));
532 if (it != m_corLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500533 _LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500534 it->writeLog();
535
536 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
537 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500538 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500539
akmhoque53353462014-04-22 08:43:45 -0500540 m_corLsdb.erase(it);
541 return true;
542 }
543 return false;
544}
545
546bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500547Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500548{
akmhoqueb6450b12014-04-24 00:01:03 -0500549 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
550 m_corLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600551 std::bind(corLsaCompareByKey,
akmhoque157b0a42014-05-13 00:26:37 -0500552 _1, key));
553 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500554 return false;
555 }
556 return true;
557}
558
559void
akmhoque2f423352014-06-03 11:49:35 -0500560Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500561{
akmhoque2f423352014-06-03 11:49:35 -0500562 _LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500563 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500564 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500565 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500566 }
567}
568
Jiewen Tana0497d82015-02-02 21:59:18 -0800569const std::list<CoordinateLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500570Lsdb::getCoordinateLsdb() const
Jiewen Tana0497d82015-02-02 21:59:18 -0800571{
572 return m_corLsdb;
573}
574
akmhoque53353462014-04-22 08:43:45 -0500575// Adj LSA and LSDB related function starts here
576
Nick G97e34942016-07-11 14:46:27 -0500577 /*! \brief Returns whether an adj. LSA object is from some router.
578 \param alsa The adj. LSA object.
579 \param key The router name that you want to compare the LSA with.
580 */
akmhoque53353462014-04-22 08:43:45 -0500581static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500582adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500583{
584 return alsa.getKey() == key;
585}
586
akmhoque53353462014-04-22 08:43:45 -0500587void
Vince Lehman50df6b72015-03-03 12:06:40 -0600588Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500589{
Vince Lehman50df6b72015-03-03 12:06:40 -0600590 m_nlsr.incrementAdjBuildCount();
591
Nick Gordon5c467f02016-07-13 13:40:10 -0500592 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
593 // Don't build adjacency LSAs in hyperbolic routing
dmcoomes9eaf3f42017-02-21 11:39:01 -0600594 _LOG_DEBUG("Adjacency LSA not built. Currently in hyperbolic routing state.");
Nick Gordon5c467f02016-07-13 13:40:10 -0500595 return;
596 }
597
Vince Lehman50df6b72015-03-03 12:06:40 -0600598 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
599 _LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
600
dmcoomes9f936662017-03-02 10:33:09 -0600601 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, std::bind(&Lsdb::buildAdjLsa, this));
Vince Lehman50df6b72015-03-03 12:06:40 -0600602 m_nlsr.setIsBuildAdjLsaSheduled(true);
603 }
604}
605
606void
607Lsdb::buildAdjLsa()
608{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500609 _LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600610
akmhoque674b0b12014-05-20 14:33:28 -0500611 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500612
613 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
614
akmhoque31d1d4b2014-05-05 22:08:14 -0500615 int adjBuildCount = m_nlsr.getAdjBuildCount();
Nick G97e34942016-07-11 14:46:27 -0500616 // Only do the adjLsa build if there's one scheduled
akmhoque157b0a42014-05-13 00:26:37 -0500617 if (adjBuildCount > 0) {
Nick G97e34942016-07-11 14:46:27 -0500618 // It only makes sense to do the adjLsa build if we have neighbors
akmhoque157b0a42014-05-13 00:26:37 -0500619 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500620 _LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500621 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500622 }
Nick G97e34942016-07-11 14:46:27 -0500623 // We have no active neighbors, meaning no one can route through
624 // us. So delete our entry in the LSDB. This prevents this
625 // router from refreshing the LSA, eventually causing other
626 // routers to delete it, too.
akmhoque157b0a42014-05-13 00:26:37 -0500627 else {
dmcoomes9f936662017-03-02 10:33:09 -0600628 _LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors");
Nick G97e34942016-07-11 14:46:27 -0500629 // Get this router's key
akmhoque31d1d4b2014-05-05 22:08:14 -0500630 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
Nick Gordon727d4832017-10-13 18:04:25 -0500631 key.append(std::to_string(Lsa::Type::ADJACENCY));
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500632
akmhoque31d1d4b2014-05-05 22:08:14 -0500633 removeAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500634 // Recompute routing table after removal
akmhoque31d1d4b2014-05-05 22:08:14 -0500635 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500636 }
Nick G97e34942016-07-11 14:46:27 -0500637 // In the case that during building the adj LSA, the FIB has to
638 // wait on an Interest response, the number of scheduled adj LSA
639 // builds could change, so we shouldn't just set it to 0.
akmhoque31d1d4b2014-05-05 22:08:14 -0500640 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500641 }
642 }
Nick G97e34942016-07-11 14:46:27 -0500643 // We are still waiting to know the adjacency status of some
644 // neighbor, so schedule a build for later (when all that has
645 // hopefully finished)
646 else {
647 m_nlsr.setIsBuildAdjLsaSheduled(true);
648 int schedulingTime = m_nlsr.getConfParameter().getInterestRetryNumber() *
649 m_nlsr.getConfParameter().getInterestResendTime();
650 m_scheduler.scheduleEvent(ndn::time::seconds(schedulingTime),
dmcoomes9f936662017-03-02 10:33:09 -0600651 std::bind(&Lsdb::buildAdjLsa, this));
Nick G97e34942016-07-11 14:46:27 -0500652 }
akmhoque53353462014-04-22 08:43:45 -0500653}
654
akmhoque53353462014-04-22 08:43:45 -0500655bool
656Lsdb::addAdjLsa(AdjLsa& alsa)
657{
658 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
659 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600660 std::bind(adjLsaCompareByKey, _1,
akmhoque53353462014-04-22 08:43:45 -0500661 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500662 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500663 m_adjLsdb.push_back(alsa);
664 return true;
665 }
666 return false;
667}
668
akmhoqueb6450b12014-04-24 00:01:03 -0500669AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500670Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500671{
672 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
673 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600674 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500675 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500676 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500677 }
akmhoqueb6450b12014-04-24 00:01:03 -0500678 return 0;
akmhoque53353462014-04-22 08:43:45 -0500679}
680
akmhoque53353462014-04-22 08:43:45 -0500681bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500682Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500683{
akmhoqueb6450b12014-04-24 00:01:03 -0500684 AdjLsa* adjLsaCheck = findAdjLsa(key);
Nick G97e34942016-07-11 14:46:27 -0500685 // If it is in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500686 if (adjLsaCheck != 0) {
Nick G97e34942016-07-11 14:46:27 -0500687 // And the supplied seq no is newer (higher) than the current one.
akmhoque157b0a42014-05-13 00:26:37 -0500688 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500689 return true;
690 }
akmhoque157b0a42014-05-13 00:26:37 -0500691 else {
akmhoque53353462014-04-22 08:43:45 -0500692 return false;
693 }
694 }
695 return true;
696}
697
akmhoque53353462014-04-22 08:43:45 -0500698ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500699Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
700 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500701{
Vince Lehman7c603292014-09-11 17:48:16 -0500702 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500703 std::bind(&Lsdb::expireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500704}
705
706bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500707Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500708{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700709 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500710 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
Nick G97e34942016-07-11 14:46:27 -0500711 // If this adj. LSA is not in the LSDB already
akmhoque157b0a42014-05-13 00:26:37 -0500712 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500713 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500714 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500715 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500716 addAdjLsa(alsa);
Nick G97e34942016-07-11 14:46:27 -0500717 // Add any new name prefixes to the NPT
akmhoque31d1d4b2014-05-05 22:08:14 -0500718 alsa.addNptEntries(m_nlsr);
719 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500720 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500721 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
722 ndn::time::system_clock::now();
723 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500724 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500725 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500726 alsa.getLsSeqNo(), timeToExpire);
727 }
akmhoque157b0a42014-05-13 00:26:37 -0500728 else {
729 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500730 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500731 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500732 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500733 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500734 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
Nick G97e34942016-07-11 14:46:27 -0500735 // If the new adj LSA has new content, update the contents of
736 // the LSDB entry. Additionally, since we've changed the
737 // contents of the LSDB, we have to schedule a routing
738 // calculation.
akmhoque157b0a42014-05-13 00:26:37 -0500739 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500740 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500741 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500742 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500743 }
akmhoque157b0a42014-05-13 00:26:37 -0500744 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500745 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
746 ndn::time::system_clock::now();
747 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500748 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500749 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
750 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500751 alsa.getLsSeqNo(),
752 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500753 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500754 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500755 }
756 }
757 return true;
758}
759
760bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500761Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500762{
akmhoque31d1d4b2014-05-05 22:08:14 -0500763 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500764 m_sequencingManager.getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500765 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500766 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
767 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500768
Nick Gordon5c467f02016-07-13 13:40:10 -0500769 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
770 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500771 m_sequencingManager.increaseAdjLsaSeq();
772 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500773 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500774 }
Vince Lehman904c2412014-09-23 19:36:11 -0500775
Vince Lehman9d097802015-03-16 17:55:59 -0500776 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500777}
778
779bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500780Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500781{
782 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
783 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600784 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500785 if (it != m_adjLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500786 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500787 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500788 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500789 m_adjLsdb.erase(it);
790 return true;
791 }
792 return false;
793}
794
795bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500796Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500797{
798 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
799 m_adjLsdb.end(),
dmcoomes9f936662017-03-02 10:33:09 -0600800 std::bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500801 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500802 return false;
803 }
804 return true;
805}
806
Jiewen Tana0497d82015-02-02 21:59:18 -0800807const std::list<AdjLsa>&
Nick Gordon114537f2017-08-09 14:51:37 -0500808Lsdb::getAdjLsdb() const
akmhoque53353462014-04-22 08:43:45 -0500809{
810 return m_adjLsdb;
811}
812
813void
Nick Gordone98480b2017-05-24 11:23:03 -0500814Lsdb::setLsaRefreshTime(const ndn::time::seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500815{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700816 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500817}
818
819void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500820Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500821{
822 m_thisRouterPrefix = trp;
823}
824
Nick G97e34942016-07-11 14:46:27 -0500825 // This function determines whether a name LSA should be refreshed
826 // or expired. The conditions for getting refreshed are: it is still
827 // in the LSDB, it hasn't been updated by something else already (as
828 // evidenced by its seq. no.), and this is the originating router for
829 // the LSA. Is it let expire in all other cases.
830 // lsaKey is the key of the LSA's publishing router.
831 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500832void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500833Lsdb::expireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500834{
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500835 _LOG_DEBUG("Lsdb::expireOrRefreshNameLsa Called");
akmhoque674b0b12014-05-20 14:33:28 -0500836 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500837 NameLsa* chkNameLsa = findNameLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500838 // If this name LSA exists in the LSDB
akmhoque157b0a42014-05-13 00:26:37 -0500839 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500840 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500841 // If its seq no is the one we are expecting.
akmhoque157b0a42014-05-13 00:26:37 -0500842 if (chkNameLsa->getLsSeqNo() == seqNo) {
843 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500844 _LOG_DEBUG("Own Name LSA, so refreshing it");
845 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500846 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500847 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500848 m_sequencingManager.setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500849 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500850 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500851 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500852 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500853 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500854 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700855 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500856 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500857 m_sync.publishRoutingUpdate(Lsa::Type::NAME, m_sequencingManager.getNameLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500858 }
Nick G97e34942016-07-11 14:46:27 -0500859 // Since we cannot refresh other router's LSAs, our only choice is to expire.
akmhoque157b0a42014-05-13 00:26:37 -0500860 else {
dmcoomes9eaf3f42017-02-21 11:39:01 -0600861 _LOG_DEBUG("Other's Name LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500862 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500863 }
864 }
865 }
866}
867
Nick G97e34942016-07-11 14:46:27 -0500868 // This function determines whether an adj. LSA should be refreshed
869 // or expired. The conditions for getting refreshed are: it is still
870 // in the LSDB, it hasn't been updated by something else already (as
871 // evidenced by its seq. no.), and this is the originating router for
872 // the LSA. Is it let expire in all other cases.
873 // lsaKey is the key of the LSA's publishing router.
874 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500875void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500876Lsdb::expireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500877{
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500878 _LOG_DEBUG("Lsdb::expireOrRefreshAdjLsa Called");
dmcoomes9eaf3f42017-02-21 11:39:01 -0600879 _LOG_DEBUG("LSA Key: " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500880 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500881 // If this is a valid LSA
akmhoque157b0a42014-05-13 00:26:37 -0500882 if (chkAdjLsa != 0) {
akmhoque2f423352014-06-03 11:49:35 -0500883 _LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500884 // And if it hasn't been updated for some other reason
akmhoque157b0a42014-05-13 00:26:37 -0500885 if (chkAdjLsa->getLsSeqNo() == seqNo) {
Nick G97e34942016-07-11 14:46:27 -0500886 // If it is our own LSA
akmhoque157b0a42014-05-13 00:26:37 -0500887 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500888 _LOG_DEBUG("Own Adj LSA, so refreshing it");
889 _LOG_DEBUG("Deleting Adj Lsa");
890 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500891 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500892 m_sequencingManager.setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500893 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500894 _LOG_DEBUG("Adding Adj Lsa");
895 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500896 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500897 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500898 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700899 m_lsaRefreshTime));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500900 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500901 m_sync.publishRoutingUpdate(Lsa::Type::ADJACENCY, m_sequencingManager.getAdjLsaSeq());
akmhoque53353462014-04-22 08:43:45 -0500902 }
Nick G97e34942016-07-11 14:46:27 -0500903 // An LSA from another router is expiring
akmhoque157b0a42014-05-13 00:26:37 -0500904 else {
dmcoomes9eaf3f42017-02-21 11:39:01 -0600905 _LOG_DEBUG("Other's Adj LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500906 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500907 }
Nick G97e34942016-07-11 14:46:27 -0500908 // We have changed the contents of the LSDB, so we have to
909 // schedule a routing calculation
akmhoque31d1d4b2014-05-05 22:08:14 -0500910 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500911 }
912 }
913}
914
Nick G97e34942016-07-11 14:46:27 -0500915 // This function determines whether an adj. LSA should be refreshed
916 // or expired. The conditions for getting refreshed are: it is still
917 // in the LSDB, it hasn't been updated by something else already (as
918 // evidenced by its seq. no.), and this is the originating router for
919 // the LSA. It is let expire in all other cases.
920 // lsaKey is the key of the LSA's publishing router.
921 // seqNo is the seq. no. of the candidate LSA.
akmhoque53353462014-04-22 08:43:45 -0500922void
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500923Lsdb::expireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500924 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500925{
Ashlesh Gawande90173ad2017-08-09 15:19:50 -0500926 _LOG_DEBUG("Lsdb::expireOrRefreshCorLsa Called ");
akmhoque674b0b12014-05-20 14:33:28 -0500927 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500928 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
Nick G97e34942016-07-11 14:46:27 -0500929 // Whether the LSA is in the LSDB or not.
akmhoque157b0a42014-05-13 00:26:37 -0500930 if (chkCorLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500931 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
Nick G97e34942016-07-11 14:46:27 -0500932 // Whether the LSA has been updated without our knowledge.
akmhoque157b0a42014-05-13 00:26:37 -0500933 if (chkCorLsa->getLsSeqNo() == seqNo) {
934 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500935 _LOG_DEBUG("Own Cor LSA, so refreshing it");
936 _LOG_DEBUG("Deleting Coordinate Lsa");
937 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500938 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500939 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500940 m_sequencingManager.setCorLsaSeq(chkCorLsa->getLsSeqNo());
Nick Gordon5c467f02016-07-13 13:40:10 -0500941 }
942
akmhoquec7a79b22014-05-26 08:06:19 -0500943 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500944 _LOG_DEBUG("Adding Coordinate Lsa");
945 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500946 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500947 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
948 chkCorLsa->getKey(),
949 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700950 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500951 // Only sync coordinate LSAs if link-state routing is disabled
952 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500953 m_sequencingManager.writeSeqNoToFile();
Nick Gordon727d4832017-10-13 18:04:25 -0500954 m_sync.publishRoutingUpdate(Lsa::Type::COORDINATE, m_sequencingManager.getCorLsaSeq());
Nick Gordon5c467f02016-07-13 13:40:10 -0500955 }
akmhoque53353462014-04-22 08:43:45 -0500956 }
Nick G97e34942016-07-11 14:46:27 -0500957 // We can't refresh other router's LSAs, so we remove it.
akmhoque157b0a42014-05-13 00:26:37 -0500958 else {
dmcoomes9eaf3f42017-02-21 11:39:01 -0600959 _LOG_DEBUG("Other's Cor LSA, so removing from LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500960 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500961 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500962 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500963 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500964 }
965 }
966 }
967}
968
akmhoque53353462014-04-22 08:43:45 -0500969void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700970Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Nick Gordone98480b2017-05-24 11:23:03 -0500971 ndn::time::steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500972{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -0600973 // increment SENT_LSA_INTEREST
974 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_INTEREST);
975
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500976 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
Nick Gordone98480b2017-05-24 11:23:03 -0500977 deadline = ndn::time::steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700978 }
Nick G97e34942016-07-11 14:46:27 -0500979 // The first component of the interest is the name.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500980 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Nick G97e34942016-07-11 14:46:27 -0500981 // The seq no is the last
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500982 uint64_t seqNo = interestName[-1].toNumber();
983
Nick G97e34942016-07-11 14:46:27 -0500984 // If the LSA is not found in the list currently.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500985 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
986 m_highestSeqNo[lsaName] = seqNo;
987 }
Nick G97e34942016-07-11 14:46:27 -0500988 // If the new seq no is higher, that means the LSA is valid
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500989 else if (seqNo > m_highestSeqNo[lsaName]) {
990 m_highestSeqNo[lsaName] = seqNo;
991 }
Nick G97e34942016-07-11 14:46:27 -0500992 // Otherwise, its an old/invalid LSA
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500993 else if (seqNo < m_highestSeqNo[lsaName]) {
994 return;
995 }
996
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500997 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700998 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500999
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001000 _LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
1001 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
1002 m_nlsr.getValidator(),
dmcoomes9f936662017-03-02 10:33:09 -06001003 std::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
1004 std::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001005 timeoutCount, deadline, lsaName, seqNo));
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001006 // increment a specific SENT_LSA_INTEREST
Nick Gordon727d4832017-10-13 18:04:25 -05001007 Lsa::Type lsaType;
1008 std::istringstream(interestName[-2].toUri()) >> lsaType;
1009 switch (lsaType) {
1010 case Lsa::Type::ADJACENCY:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001011 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001012 break;
1013 case Lsa::Type::COORDINATE:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001014 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001015 break;
1016 case Lsa::Type::NAME:
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001017 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_INTEREST);
Nick Gordon727d4832017-10-13 18:04:25 -05001018 break;
1019 default:
1020 _LOG_ERROR("lsaType " << lsaType << " not recognized; failed Statistics::PacketType conversion");
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001021 }
akmhoque31d1d4b2014-05-05 22:08:14 -05001022}
1023
1024void
1025Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
1026{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001027 // increment RCV_LSA_INTEREST
1028 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_INTEREST);
1029
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001030 const ndn::Name& interestName(interest.getName());
1031 _LOG_DEBUG("Interest received for LSA: " << interestName);
1032
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001033 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001034 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
1035
akmhoque157b0a42014-05-13 00:26:37 -05001036 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001037
Nick G97e34942016-07-11 14:46:27 -05001038 // Forms the name of the router that the Interest packet came from.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001039 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
1040 originRouter.append(interestName.getSubName(lsaPosition + 1,
1041 interest.getName().size() - lsaPosition - 3));
1042
1043 uint64_t seqNo = interestName[-1].toNumber();
1044 _LOG_DEBUG("LSA sequence number from interest: " << seqNo);
1045
Nick Gordon727d4832017-10-13 18:04:25 -05001046 Lsa::Type interestedLsType;
1047 std::istringstream(interestName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001048
Nick Gordon727d4832017-10-13 18:04:25 -05001049 if (interestedLsType == Lsa::Type::NAME) {
1050 processInterestForNameLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1051 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001052 }
Nick Gordon727d4832017-10-13 18:04:25 -05001053 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1054 processInterestForAdjacencyLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1055 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001056 }
Nick Gordon727d4832017-10-13 18:04:25 -05001057 else if (interestedLsType == Lsa::Type::COORDINATE) {
1058 processInterestForCoordinateLsa(interest, originRouter.append(std::to_string(interestedLsType)),
1059 seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -05001060 }
akmhoque157b0a42014-05-13 00:26:37 -05001061 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001062 _LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001063 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001064 lsaIncrementSignal(Statistics::PacketType::SENT_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001065 }
1066}
1067
Nick G97e34942016-07-11 14:46:27 -05001068 // \brief Sends LSA data.
1069 // \param interest The Interest that warranted the data.
1070 // \param content The data that the Interest was seeking.
akmhoque31d1d4b2014-05-05 22:08:14 -05001071void
akmhoque69c9aa92014-07-23 15:15:05 -05001072Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
1073{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001074 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
1075 m_nlsr.getKeyChain(),
1076 m_lsaRefreshTime,
1077 content);
dmcoomes9eaf3f42017-02-21 11:39:01 -06001078 _LOG_DEBUG("Sending requested data ( " << content << ") for interest (" << interest
1079 << ") to be published and added to face.");
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001080 publisher.publish(interest.getName(),
1081 ndn::security::signingByCertificate(m_nlsr.getDefaultCertName()));
akmhoque69c9aa92014-07-23 15:15:05 -05001082}
1083
Nick G97e34942016-07-11 14:46:27 -05001084 // \brief Finds and sends a requested name LSA.
1085 // \param interest The interest that seeks the name LSA.
1086 // \param lsaKey The LSA that the Interest is seeking.
1087 // \param seqNo A sequence number to ensure that we are sending the
1088 // version that was requested.
akmhoque69c9aa92014-07-23 15:15:05 -05001089void
akmhoque31d1d4b2014-05-05 22:08:14 -05001090Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
1091 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001092 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001093{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001094 // increment RCV_NAME_LSA_INTEREST
1095 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_INTEREST);
dmcoomes9eaf3f42017-02-21 11:39:01 -06001096 _LOG_DEBUG("nameLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001097 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -05001098 if (nameLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001099 if (nameLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001100 std::string content = nameLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001101 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001102 // increment SENT_NAME_LSA_DATA
1103 lsaIncrementSignal(Statistics::PacketType::SENT_NAME_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001104 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001105 else {
1106 _LOG_TRACE("SeqNo for nameLsa does not match");
1107 }
1108 }
1109 else {
1110 _LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001111 }
1112}
1113
Nick G97e34942016-07-11 14:46:27 -05001114 // \brief Finds and sends a requested adj. LSA.
1115 // \param interest The interest that seeks the adj. LSA.
1116 // \param lsaKey The LSA that the Interest is seeking.
1117 // \param seqNo A sequence number to ensure that we are sending the
1118 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001119void
1120Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
1121 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001122 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001123{
Nick Gordon5c467f02016-07-13 13:40:10 -05001124 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
dmcoomes9eaf3f42017-02-21 11:39:01 -06001125 _LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001126 }
1127
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001128 // increment RCV_ADJ_LSA_INTEREST
1129 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_INTEREST);
dmcoomes9eaf3f42017-02-21 11:39:01 -06001130 _LOG_DEBUG("AdjLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001131 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -05001132 if (adjLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001133 if (adjLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001134 std::string content = adjLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001135 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001136 // increment SENT_ADJ_LSA_DATA
1137 lsaIncrementSignal(Statistics::PacketType::SENT_ADJ_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001138 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001139 else {
1140 _LOG_TRACE("SeqNo for AdjLsa does not match");
1141 }
1142 }
1143 else {
1144 _LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001145 }
1146}
1147
Nick G97e34942016-07-11 14:46:27 -05001148 // \brief Finds and sends a requested cor. LSA.
1149 // \param interest The interest that seeks the cor. LSA.
1150 // \param lsaKey The LSA that the Interest is seeking.
1151 // \param seqNo A sequence number to ensure that we are sending the
1152 // version that was requested.
akmhoque31d1d4b2014-05-05 22:08:14 -05001153void
1154Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
1155 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001156 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -05001157{
Nick Gordon5c467f02016-07-13 13:40:10 -05001158 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
dmcoomes9eaf3f42017-02-21 11:39:01 -06001159 _LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled");
Nick Gordon5c467f02016-07-13 13:40:10 -05001160 }
1161
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001162 // increment RCV_COORD_LSA_INTEREST
1163 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_INTEREST);
dmcoomes9eaf3f42017-02-21 11:39:01 -06001164 _LOG_DEBUG("CoordinateLsa interest " << interest << " received");
akmhoque31d1d4b2014-05-05 22:08:14 -05001165 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -05001166 if (corLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001167 if (corLsa->getLsSeqNo() == seqNo) {
Nick Gordonfaf49f42017-10-23 12:36:28 -05001168 std::string content = corLsa->serialize();
akmhoque69c9aa92014-07-23 15:15:05 -05001169 putLsaData(interest,content);
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001170 // increment SENT_COORD_LSA_DATA
1171 lsaIncrementSignal(Statistics::PacketType::SENT_COORD_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001172 }
dmcoomes9eaf3f42017-02-21 11:39:01 -06001173 else {
1174 _LOG_TRACE("SeqNo for CoordinateLsa does not match");
1175 }
1176 }
1177 else {
1178 _LOG_TRACE(interest << " was not found in this lsdb");
akmhoque31d1d4b2014-05-05 22:08:14 -05001179 }
1180}
1181
1182void
dmcoomes9f936662017-03-02 10:33:09 -06001183Lsdb::onContentValidated(const std::shared_ptr<const ndn::Data>& data)
Yingdi Yu20e3a6e2014-05-26 23:16:10 -07001184{
1185 const ndn::Name& dataName = data->getName();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001186 _LOG_DEBUG("Data validation successful for LSA: " << dataName);
1187
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001188 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001189 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001190
akmhoque157b0a42014-05-13 00:26:37 -05001191 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001192
Nick G97e34942016-07-11 14:46:27 -05001193 // Extracts the prefix of the originating router from the data.
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001194 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001195 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001196
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001197 uint64_t seqNo = dataName[-1].toNumber();
Ashlesh Gawande820bb662017-08-03 16:12:07 -05001198 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()),
1199 data->getContent().value_size());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001200
Nick Gordon727d4832017-10-13 18:04:25 -05001201 Lsa::Type interestedLsType;
1202 std::istringstream(dataName[-2].toUri()) >> interestedLsType;
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001203
Nick Gordon727d4832017-10-13 18:04:25 -05001204 if (interestedLsType == Lsa::Type::NAME) {
1205 processContentNameLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1206 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001207 }
Nick Gordon727d4832017-10-13 18:04:25 -05001208 else if (interestedLsType == Lsa::Type::ADJACENCY) {
1209 processContentAdjacencyLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1210 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001211 }
Nick Gordon727d4832017-10-13 18:04:25 -05001212 else if (interestedLsType == Lsa::Type::COORDINATE) {
1213 processContentCoordinateLsa(originRouter.append(std::to_string(interestedLsType)), seqNo,
1214 dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001215 }
akmhoque157b0a42014-05-13 00:26:37 -05001216 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001217 _LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001218 }
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001219
1220 // increment RCV_LSA_DATA
1221 lsaIncrementSignal(Statistics::PacketType::RCV_LSA_DATA);
akmhoque31d1d4b2014-05-05 22:08:14 -05001222 }
1223}
1224
1225void
1226Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001227 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001228{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001229 // increment RCV_NAME_LSA_DATA
1230 lsaIncrementSignal(Statistics::PacketType::RCV_NAME_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001231 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001232 NameLsa nameLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001233 if (nameLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001234 installNameLsa(nameLsa);
1235 }
akmhoque157b0a42014-05-13 00:26:37 -05001236 else {
akmhoque2f423352014-06-03 11:49:35 -05001237 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001238 }
1239 }
1240}
1241
1242void
1243Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001244 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001245{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001246 // increment RCV_ADJ_LSA_DATA
1247 lsaIncrementSignal(Statistics::PacketType::RCV_ADJ_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001248 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001249 AdjLsa adjLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001250 if (adjLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001251 installAdjLsa(adjLsa);
1252 }
akmhoque157b0a42014-05-13 00:26:37 -05001253 else {
akmhoque2f423352014-06-03 11:49:35 -05001254 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001255 }
1256 }
1257}
1258
1259void
1260Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001261 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001262{
Alejandro Gil Torrese0d20482016-03-06 23:56:19 -06001263 // increment RCV_COORD_LSA_DATA
1264 lsaIncrementSignal(Statistics::PacketType::RCV_COORD_LSA_DATA);
akmhoque157b0a42014-05-13 00:26:37 -05001265 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001266 CoordinateLsa corLsa;
Nick Gordon0fa4c772017-10-23 13:33:03 -05001267 if (corLsa.deserialize(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001268 installCoordinateLsa(corLsa);
1269 }
akmhoque157b0a42014-05-13 00:26:37 -05001270 else {
akmhoque2f423352014-06-03 11:49:35 -05001271 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001272 }
1273 }
1274}
1275
akmhoquec7a79b22014-05-26 08:06:19 -05001276ndn::time::system_clock::TimePoint
1277Lsdb::getLsaExpirationTimePoint()
1278{
1279 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1280 expirationTimePoint = expirationTimePoint +
1281 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1282 return expirationTimePoint;
1283}
akmhoque31d1d4b2014-05-05 22:08:14 -05001284
1285void
akmhoque2f423352014-06-03 11:49:35 -05001286Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001287{
akmhoque2f423352014-06-03 11:49:35 -05001288 _LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001289 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001290 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001291 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001292 }
1293}
1294
1295//-----utility function -----
1296bool
Nick Gordon727d4832017-10-13 18:04:25 -05001297Lsdb::doesLsaExist(const ndn::Name& key, const Lsa::Type& lsType)
akmhoque53353462014-04-22 08:43:45 -05001298{
Nick Gordon727d4832017-10-13 18:04:25 -05001299 switch (lsType) {
1300 case Lsa::Type::ADJACENCY:
akmhoque53353462014-04-22 08:43:45 -05001301 return doesAdjLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001302 case Lsa::Type::COORDINATE:
akmhoqueb6450b12014-04-24 00:01:03 -05001303 return doesCoordinateLsaExist(key);
Nick Gordon727d4832017-10-13 18:04:25 -05001304 case Lsa::Type::NAME:
1305 return doesNameLsaExist(key);
1306 default:
1307 return false;
akmhoque53353462014-04-22 08:43:45 -05001308 }
akmhoque53353462014-04-22 08:43:45 -05001309}
1310
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001311bool
Nick Gordon727d4832017-10-13 18:04:25 -05001312Lsdb::isLsaNew(const ndn::Name& routerName, const Lsa::Type& lsaType,
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001313 const uint64_t& sequenceNumber) {
1314 ndn::Name lsaKey = routerName;
Nick Gordon727d4832017-10-13 18:04:25 -05001315 lsaKey.append(std::to_string(lsaType));
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001316
Nick Gordon727d4832017-10-13 18:04:25 -05001317 switch (lsaType) {
1318 case Lsa::Type::ADJACENCY:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001319 return isAdjLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001320 case Lsa::Type::COORDINATE:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001321 return isCoordinateLsaNew(lsaKey, sequenceNumber);
Nick Gordon727d4832017-10-13 18:04:25 -05001322 case Lsa::Type::NAME:
1323 return isNameLsaNew(lsaKey, sequenceNumber);
1324 default:
Nick Gordon8f23b5d2017-08-31 17:53:07 -05001325 return false;
1326 }
1327}
1328
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001329} // namespace nlsr