blob: 1486c2c70998a2c35df3781f7b56a1a751636c8f [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05003 * Copyright (c) 2014-2016, 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
32#include <string>
akmhoque53353462014-04-22 08:43:45 -050033
34namespace nlsr {
35
akmhoque674b0b12014-05-20 14:33:28 -050036INIT_LOGGER("Lsdb");
37
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050038class LsaContentPublisher : public SegmentPublisher<ndn::Face>
39{
40public:
41 LsaContentPublisher(ndn::Face& face,
42 ndn::KeyChain& keyChain,
43 const ndn::time::milliseconds& freshnessPeriod,
44 const std::string& content)
45 : SegmentPublisher(face, keyChain, freshnessPeriod)
46 , m_content(content)
47 {
48 }
49
50 virtual size_t
51 generate(ndn::EncodingBuffer& outBuffer) {
52 size_t totalLength = 0;
53 totalLength += outBuffer.prependByteArray(reinterpret_cast<const uint8_t*>(m_content.c_str()),
54 m_content.size());
55 return totalLength;
56 }
57
58private:
59 const std::string m_content;
60};
61
Jiewen Tana0497d82015-02-02 21:59:18 -080062const ndn::Name::Component Lsdb::NAME_COMPONENT = ndn::Name::Component("lsdb");
Vince Lehman18841082014-08-19 17:15:24 -050063const ndn::time::seconds Lsdb::GRACE_PERIOD = ndn::time::seconds(10);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -050064const steady_clock::TimePoint Lsdb::DEFAULT_LSA_RETRIEVAL_DEADLINE = steady_clock::TimePoint::min();
Vince Lehman18841082014-08-19 17:15:24 -050065
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -050066Lsdb::Lsdb(Nlsr& nlsr, ndn::Scheduler& scheduler, SyncLogicHandler& sync)
67 : m_nlsr(nlsr)
68 , m_scheduler(scheduler)
69 , m_sync(sync)
70 , m_lsaRefreshTime(0)
71 , m_adjLsaBuildInterval(ADJ_LSA_BUILD_INTERVAL_DEFAULT)
72{
73}
74
75void
76Lsdb::onFetchLsaError(uint32_t errorCode,
77 const std::string& msg,
78 ndn::Name& interestName,
79 uint32_t retransmitNo,
80 const ndn::time::steady_clock::TimePoint& deadline,
81 ndn::Name lsaName,
82 uint64_t seqNo)
83{
84 _LOG_DEBUG("Failed to fetch LSA: " << lsaName << ", Error code: " << errorCode
85 << ", Message: " << msg);
86
87 if (ndn::time::steady_clock::now() < deadline) {
88 SequenceNumberMap::const_iterator it = m_highestSeqNo.find(lsaName);
89
90 if (it != m_highestSeqNo.end() && it->second == seqNo) {
91 // If the SegmentFetcher failed due to an Interest timeout, it is safe to re-express
92 // immediately since at the least the LSA Interest lifetime has elapsed.
93 // Otherwise, it is necessary to delay the Interest re-expression to prevent
94 // the potential for constant Interest flooding.
95 ndn::time::seconds delay = m_nlsr.getConfParameter().getLsaInterestLifetime();
96
97 if (errorCode == ndn::util::SegmentFetcher::ErrorCode::INTEREST_TIMEOUT) {
98 delay = ndn::time::seconds(0);
99 }
100
101 m_scheduler.scheduleEvent(delay, std::bind(&Lsdb::expressInterest, this,
102 interestName, retransmitNo + 1, deadline));
103 }
104 }
105}
106
107void
108Lsdb::afterFetchLsa(const ndn::ConstBufferPtr& bufferPtr, ndn::Name& interestName)
109{
110 shared_ptr<ndn::Data> data = make_shared<ndn::Data>(ndn::Name(interestName));
111 data->setContent(bufferPtr);
112
113 _LOG_DEBUG("Received data for LSA(name): " << data->getName());
114
115 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
116 uint64_t seqNo = interestName[-1].toNumber();
117
118 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
119 m_highestSeqNo[lsaName] = seqNo;
120 }
121 else if (seqNo > m_highestSeqNo[lsaName]) {
122 m_highestSeqNo[lsaName] = seqNo;
123 }
124 else if (seqNo < m_highestSeqNo[lsaName]) {
125 return;
126 }
127
128 onContentValidated(data);
129}
akmhoque53353462014-04-22 08:43:45 -0500130
131void
akmhoque31d1d4b2014-05-05 22:08:14 -0500132Lsdb::cancelScheduleLsaExpiringEvent(ndn::EventId eid)
akmhoque53353462014-04-22 08:43:45 -0500133{
Vince Lehman7c603292014-09-11 17:48:16 -0500134 m_scheduler.cancelEvent(eid);
akmhoque53353462014-04-22 08:43:45 -0500135}
136
137static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500138nameLsaCompareByKey(const NameLsa& nlsa1, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500139{
140 return nlsa1.getKey() == key;
141}
142
akmhoque53353462014-04-22 08:43:45 -0500143bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500144Lsdb::buildAndInstallOwnNameLsa()
akmhoque53353462014-04-22 08:43:45 -0500145{
akmhoque31d1d4b2014-05-05 22:08:14 -0500146 NameLsa nameLsa(m_nlsr.getConfParameter().getRouterPrefix(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500147 m_nlsr.getSequencingManager().getNameLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500148 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500149 m_nlsr.getNamePrefixList());
150 m_nlsr.getSequencingManager().increaseNameLsaSeq();
151 return installNameLsa(nameLsa);
akmhoque53353462014-04-22 08:43:45 -0500152}
153
akmhoqueb6450b12014-04-24 00:01:03 -0500154NameLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500155Lsdb::findNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500156{
157 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
158 m_nameLsdb.end(),
159 bind(nameLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500160 if (it != m_nameLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500161 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500162 }
akmhoqueb6450b12014-04-24 00:01:03 -0500163 return 0;
akmhoque53353462014-04-22 08:43:45 -0500164}
165
166bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500167Lsdb::isNameLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500168{
akmhoqueb6450b12014-04-24 00:01:03 -0500169 NameLsa* nameLsaCheck = findNameLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500170 if (nameLsaCheck != 0) {
171 if (nameLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500172 return true;
173 }
akmhoque157b0a42014-05-13 00:26:37 -0500174 else {
akmhoque53353462014-04-22 08:43:45 -0500175 return false;
176 }
177 }
178 return true;
179}
180
181ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500182Lsdb::scheduleNameLsaExpiration(const ndn::Name& key, int seqNo,
183 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500184{
Vince Lehman7c603292014-09-11 17:48:16 -0500185 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
186 ndn::bind(&Lsdb::exprireOrRefreshNameLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500187}
188
189bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500190Lsdb::installNameLsa(NameLsa& nlsa)
akmhoque53353462014-04-22 08:43:45 -0500191{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700192 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500193 NameLsa* chkNameLsa = findNameLsa(nlsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500194 if (chkNameLsa == 0) {
akmhoque53353462014-04-22 08:43:45 -0500195 addNameLsa(nlsa);
akmhoque2f423352014-06-03 11:49:35 -0500196 _LOG_DEBUG("New Name LSA");
197 _LOG_DEBUG("Adding Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500198 nlsa.writeLog();
akmhoque674b0b12014-05-20 14:33:28 -0500199
akmhoque157b0a42014-05-13 00:26:37 -0500200 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500201 m_nlsr.getNamePrefixTable().addEntry(nlsa.getOrigRouter(),
202 nlsa.getOrigRouter());
203 std::list<ndn::Name> nameList = nlsa.getNpl().getNameList();
204 for (std::list<ndn::Name>::iterator it = nameList.begin(); it != nameList.end();
akmhoque157b0a42014-05-13 00:26:37 -0500205 it++) {
206 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500207 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500208 }
209 }
210 }
akmhoque157b0a42014-05-13 00:26:37 -0500211 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500212 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
213 ndn::time::system_clock::now();
214 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500215 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500216 nlsa.setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500217 nlsa.getLsSeqNo(),
218 timeToExpire));
219 }
akmhoque157b0a42014-05-13 00:26:37 -0500220 else {
221 if (chkNameLsa->getLsSeqNo() < nlsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500222 _LOG_DEBUG("Updated Name LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500223 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500224 chkNameLsa->writeLog();
225 chkNameLsa->setLsSeqNo(nlsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500226 chkNameLsa->setExpirationTimePoint(nlsa.getExpirationTimePoint());
akmhoqueb6450b12014-04-24 00:01:03 -0500227 chkNameLsa->getNpl().sort();
akmhoque53353462014-04-22 08:43:45 -0500228 nlsa.getNpl().sort();
akmhoque31d1d4b2014-05-05 22:08:14 -0500229 std::list<ndn::Name> nameToAdd;
akmhoque53353462014-04-22 08:43:45 -0500230 std::set_difference(nlsa.getNpl().getNameList().begin(),
231 nlsa.getNpl().getNameList().end(),
akmhoqueb6450b12014-04-24 00:01:03 -0500232 chkNameLsa->getNpl().getNameList().begin(),
233 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500234 std::inserter(nameToAdd, nameToAdd.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500235 for (std::list<ndn::Name>::iterator it = nameToAdd.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500236 it != nameToAdd.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500237 chkNameLsa->addName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500238 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
239 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500240 m_nlsr.getNamePrefixTable().addEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500241 }
242 }
243 }
Vince Lehmanf1aa5232014-10-06 17:57:35 -0500244
245 chkNameLsa->getNpl().sort();
246
akmhoque31d1d4b2014-05-05 22:08:14 -0500247 std::list<ndn::Name> nameToRemove;
akmhoqueb6450b12014-04-24 00:01:03 -0500248 std::set_difference(chkNameLsa->getNpl().getNameList().begin(),
249 chkNameLsa->getNpl().getNameList().end(),
akmhoque53353462014-04-22 08:43:45 -0500250 nlsa.getNpl().getNameList().begin(),
251 nlsa.getNpl().getNameList().end(),
252 std::inserter(nameToRemove, nameToRemove.begin()));
akmhoque31d1d4b2014-05-05 22:08:14 -0500253 for (std::list<ndn::Name>::iterator it = nameToRemove.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500254 it != nameToRemove.end(); ++it) {
akmhoqueb6450b12014-04-24 00:01:03 -0500255 chkNameLsa->removeName((*it));
akmhoque157b0a42014-05-13 00:26:37 -0500256 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
257 if ((*it) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500258 m_nlsr.getNamePrefixTable().removeEntry((*it), nlsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500259 }
260 }
261 }
akmhoque157b0a42014-05-13 00:26:37 -0500262 if (nlsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500263 ndn::time::system_clock::Duration duration = nlsa.getExpirationTimePoint() -
264 ndn::time::system_clock::now();
265 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500266 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500267 cancelScheduleLsaExpiringEvent(chkNameLsa->getExpiringEventId());
268 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(nlsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500269 nlsa.getLsSeqNo(),
270 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500271 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500272 chkNameLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500273 }
274 }
275 return true;
276}
277
278bool
279Lsdb::addNameLsa(NameLsa& nlsa)
280{
281 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
282 m_nameLsdb.end(),
283 bind(nameLsaCompareByKey, _1,
284 nlsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500285 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500286 m_nameLsdb.push_back(nlsa);
287 return true;
288 }
289 return false;
290}
291
292bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500293Lsdb::removeNameLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500294{
295 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
296 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500297 ndn::bind(nameLsaCompareByKey, _1, key));
298 if (it != m_nameLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500299 _LOG_DEBUG("Deleting Name Lsa");
akmhoque53353462014-04-22 08:43:45 -0500300 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500301 if ((*it).getOrigRouter() !=
akmhoque157b0a42014-05-13 00:26:37 -0500302 m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500303 m_nlsr.getNamePrefixTable().removeEntry((*it).getOrigRouter(),
304 (*it).getOrigRouter());
305 for (std::list<ndn::Name>::iterator nit = (*it).getNpl().getNameList().begin();
akmhoque157b0a42014-05-13 00:26:37 -0500306 nit != (*it).getNpl().getNameList().end(); ++nit) {
307 if ((*nit) != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500308 m_nlsr.getNamePrefixTable().removeEntry((*nit), (*it).getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500309 }
310 }
311 }
312 m_nameLsdb.erase(it);
313 return true;
314 }
315 return false;
316}
317
318bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500319Lsdb::doesNameLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500320{
321 std::list<NameLsa>::iterator it = std::find_if(m_nameLsdb.begin(),
322 m_nameLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500323 ndn::bind(nameLsaCompareByKey, _1, key));
324 if (it == m_nameLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500325 return false;
326 }
327 return true;
328}
329
330void
akmhoque2f423352014-06-03 11:49:35 -0500331Lsdb::writeNameLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500332{
akmhoque2f423352014-06-03 11:49:35 -0500333 _LOG_DEBUG("---------------Name LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -0500334 for (std::list<NameLsa>::iterator it = m_nameLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500335 it != m_nameLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500336 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500337 }
338}
339
Jiewen Tana0497d82015-02-02 21:59:18 -0800340const std::list<NameLsa>&
341Lsdb::getNameLsdb()
342{
343 return m_nameLsdb;
344}
345
akmhoque53353462014-04-22 08:43:45 -0500346// Cor LSA and LSDB related Functions start here
347
akmhoque53353462014-04-22 08:43:45 -0500348static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500349corLsaCompareByKey(const CoordinateLsa& clsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500350{
351 return clsa.getKey() == key;
352}
353
354bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500355Lsdb::buildAndInstallOwnCoordinateLsa()
akmhoque53353462014-04-22 08:43:45 -0500356{
akmhoque31d1d4b2014-05-05 22:08:14 -0500357 CoordinateLsa corLsa(m_nlsr.getConfParameter().getRouterPrefix(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500358 m_nlsr.getSequencingManager().getCorLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500359 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500360 m_nlsr.getConfParameter().getCorR(),
361 m_nlsr.getConfParameter().getCorTheta());
Nick Gordon5c467f02016-07-13 13:40:10 -0500362
363 // Sync coordinate LSAs if using HR or HR dry run.
364 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
365 m_nlsr.getSequencingManager().increaseCorLsaSeq();
366 m_sync.publishRoutingUpdate();
367 }
368
akmhoque31d1d4b2014-05-05 22:08:14 -0500369 installCoordinateLsa(corLsa);
Nick Gordon5c467f02016-07-13 13:40:10 -0500370
akmhoque53353462014-04-22 08:43:45 -0500371 return true;
372}
373
akmhoqueb6450b12014-04-24 00:01:03 -0500374CoordinateLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500375Lsdb::findCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500376{
akmhoqueb6450b12014-04-24 00:01:03 -0500377 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
378 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500379 ndn::bind(corLsaCompareByKey, _1, key));
380 if (it != m_corLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500381 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500382 }
akmhoqueb6450b12014-04-24 00:01:03 -0500383 return 0;
akmhoque53353462014-04-22 08:43:45 -0500384}
385
386bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500387Lsdb::isCoordinateLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500388{
akmhoqueb6450b12014-04-24 00:01:03 -0500389 CoordinateLsa* clsa = findCoordinateLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500390 if (clsa != 0) {
391 if (clsa->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500392 return true;
393 }
akmhoque157b0a42014-05-13 00:26:37 -0500394 else {
akmhoque53353462014-04-22 08:43:45 -0500395 return false;
396 }
397 }
398 return true;
399}
400
401ndn::EventId
akmhoque31d1d4b2014-05-05 22:08:14 -0500402Lsdb::scheduleCoordinateLsaExpiration(const ndn::Name& key, int seqNo,
akmhoquec7a79b22014-05-26 08:06:19 -0500403 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500404{
Vince Lehman7c603292014-09-11 17:48:16 -0500405 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
406 ndn::bind(&Lsdb::exprireOrRefreshCoordinateLsa,
407 this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500408}
409
410bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500411Lsdb::installCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500412{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700413 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500414 CoordinateLsa* chkCorLsa = findCoordinateLsa(clsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500415 if (chkCorLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500416 _LOG_DEBUG("New Coordinate LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500417 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500418 clsa.writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500419 addCoordinateLsa(clsa);
akmhoque2f423352014-06-03 11:49:35 -0500420
Nick Gordon5c467f02016-07-13 13:40:10 -0500421 // Register the LSA's origin router prefix
akmhoque157b0a42014-05-13 00:26:37 -0500422 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500423 m_nlsr.getNamePrefixTable().addEntry(clsa.getOrigRouter(),
424 clsa.getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500425 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500426 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500427 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500428 }
akmhoque157b0a42014-05-13 00:26:37 -0500429 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500430 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
431 ndn::time::system_clock::now();
432 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500433 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500434 scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500435 clsa.getLsSeqNo(), timeToExpire);
akmhoque53353462014-04-22 08:43:45 -0500436 }
akmhoque157b0a42014-05-13 00:26:37 -0500437 else {
438 if (chkCorLsa->getLsSeqNo() < clsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500439 _LOG_DEBUG("Updated Coordinate LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500440 _LOG_DEBUG("Deleting Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500441 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500442 chkCorLsa->setLsSeqNo(clsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500443 chkCorLsa->setExpirationTimePoint(clsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500444 if (!chkCorLsa->isEqualContent(clsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500445 chkCorLsa->setCorRadius(clsa.getCorRadius());
446 chkCorLsa->setCorTheta(clsa.getCorTheta());
akmhoque157b0a42014-05-13 00:26:37 -0500447 if (m_nlsr.getConfParameter().getHyperbolicState() >= HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500448 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500449 }
450 }
akmhoque157b0a42014-05-13 00:26:37 -0500451 if (clsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500452 ndn::time::system_clock::Duration duration = clsa.getExpirationTimePoint() -
453 ndn::time::system_clock::now();
454 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500455 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500456 cancelScheduleLsaExpiringEvent(chkCorLsa->getExpiringEventId());
457 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(clsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500458 clsa.getLsSeqNo(),
459 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500460 _LOG_DEBUG("Adding Coordinate Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500461 chkCorLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500462 }
463 }
464 return true;
465}
466
467bool
akmhoqueb6450b12014-04-24 00:01:03 -0500468Lsdb::addCoordinateLsa(CoordinateLsa& clsa)
akmhoque53353462014-04-22 08:43:45 -0500469{
akmhoqueb6450b12014-04-24 00:01:03 -0500470 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
471 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500472 ndn::bind(corLsaCompareByKey, _1,
473 clsa.getKey()));
474 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500475 m_corLsdb.push_back(clsa);
476 return true;
477 }
478 return false;
479}
480
481bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500482Lsdb::removeCoordinateLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500483{
akmhoqueb6450b12014-04-24 00:01:03 -0500484 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
485 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500486 ndn::bind(corLsaCompareByKey,
487 _1, key));
488 if (it != m_corLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500489 _LOG_DEBUG("Deleting Coordinate Lsa");
Nick Gordon5c467f02016-07-13 13:40:10 -0500490 it->writeLog();
491
492 if (it->getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
493 m_nlsr.getNamePrefixTable().removeEntry(it->getOrigRouter(), it->getOrigRouter());
akmhoque53353462014-04-22 08:43:45 -0500494 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500495
akmhoque53353462014-04-22 08:43:45 -0500496 m_corLsdb.erase(it);
497 return true;
498 }
499 return false;
500}
501
502bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500503Lsdb::doesCoordinateLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500504{
akmhoqueb6450b12014-04-24 00:01:03 -0500505 std::list<CoordinateLsa>::iterator it = std::find_if(m_corLsdb.begin(),
506 m_corLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500507 ndn::bind(corLsaCompareByKey,
508 _1, key));
509 if (it == m_corLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500510 return false;
511 }
512 return true;
513}
514
515void
akmhoque2f423352014-06-03 11:49:35 -0500516Lsdb::writeCorLsdbLog()
akmhoque53353462014-04-22 08:43:45 -0500517{
akmhoque2f423352014-06-03 11:49:35 -0500518 _LOG_DEBUG("---------------Cor LSDB-------------------");
akmhoqueb6450b12014-04-24 00:01:03 -0500519 for (std::list<CoordinateLsa>::iterator it = m_corLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -0500520 it != m_corLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -0500521 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -0500522 }
523}
524
Jiewen Tana0497d82015-02-02 21:59:18 -0800525const std::list<CoordinateLsa>&
526Lsdb::getCoordinateLsdb()
527{
528 return m_corLsdb;
529}
530
akmhoque53353462014-04-22 08:43:45 -0500531// Adj LSA and LSDB related function starts here
532
533static bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500534adjLsaCompareByKey(AdjLsa& alsa, const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500535{
536 return alsa.getKey() == key;
537}
538
akmhoque53353462014-04-22 08:43:45 -0500539void
Vince Lehman50df6b72015-03-03 12:06:40 -0600540Lsdb::scheduleAdjLsaBuild()
akmhoque53353462014-04-22 08:43:45 -0500541{
Vince Lehman50df6b72015-03-03 12:06:40 -0600542 m_nlsr.incrementAdjBuildCount();
543
Nick Gordon5c467f02016-07-13 13:40:10 -0500544 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
545 // Don't build adjacency LSAs in hyperbolic routing
546 return;
547 }
548
Vince Lehman50df6b72015-03-03 12:06:40 -0600549 if (m_nlsr.getIsBuildAdjLsaSheduled() == false) {
550 _LOG_DEBUG("Scheduling Adjacency LSA build in " << m_adjLsaBuildInterval);
551
552 m_scheduler.scheduleEvent(m_adjLsaBuildInterval, ndn::bind(&Lsdb::buildAdjLsa, this));
553 m_nlsr.setIsBuildAdjLsaSheduled(true);
554 }
555}
556
557void
558Lsdb::buildAdjLsa()
559{
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500560 _LOG_TRACE("Lsdb::buildAdjLsa called");
Vince Lehman50df6b72015-03-03 12:06:40 -0600561
akmhoque674b0b12014-05-20 14:33:28 -0500562 m_nlsr.setIsBuildAdjLsaSheduled(false);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500563
564 if (m_nlsr.getAdjacencyList().isAdjLsaBuildable(m_nlsr.getConfParameter().getInterestRetryNumber())) {
565
akmhoque31d1d4b2014-05-05 22:08:14 -0500566 int adjBuildCount = m_nlsr.getAdjBuildCount();
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500567
568 // Is the adjacency LSA build still necessary, or has another build
569 // fulfilled this request?
akmhoque157b0a42014-05-13 00:26:37 -0500570 if (adjBuildCount > 0) {
571 if (m_nlsr.getAdjacencyList().getNumOfActiveNeighbor() > 0) {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500572 _LOG_DEBUG("Building and installing own Adj LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -0500573 buildAndInstallOwnAdjLsa();
akmhoque53353462014-04-22 08:43:45 -0500574 }
akmhoque157b0a42014-05-13 00:26:37 -0500575 else {
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500576 _LOG_DEBUG("Removing own Adj LSA; no ACTIVE neighbors")
akmhoque31d1d4b2014-05-05 22:08:14 -0500577 ndn::Name key = m_nlsr.getConfParameter().getRouterPrefix();
alvy49b1c0c2014-12-19 13:57:46 -0600578 key.append(AdjLsa::TYPE_STRING);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500579
akmhoque31d1d4b2014-05-05 22:08:14 -0500580 removeAdjLsa(key);
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500581
akmhoque31d1d4b2014-05-05 22:08:14 -0500582 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500583 }
Vince Lehmanf7eec4f2015-05-08 19:02:31 -0500584
585 // Since more adjacency LSA builds may have been scheduled while this build
586 // was in progress, decrease the build count by the number of scheduled
587 // builds at the beginning of this build.
akmhoque31d1d4b2014-05-05 22:08:14 -0500588 m_nlsr.setAdjBuildCount(m_nlsr.getAdjBuildCount() - adjBuildCount);
akmhoque53353462014-04-22 08:43:45 -0500589 }
590 }
akmhoque53353462014-04-22 08:43:45 -0500591}
592
593
594bool
595Lsdb::addAdjLsa(AdjLsa& alsa)
596{
597 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
598 m_adjLsdb.end(),
599 bind(adjLsaCompareByKey, _1,
600 alsa.getKey()));
akmhoque157b0a42014-05-13 00:26:37 -0500601 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500602 m_adjLsdb.push_back(alsa);
603 return true;
604 }
605 return false;
606}
607
akmhoqueb6450b12014-04-24 00:01:03 -0500608AdjLsa*
akmhoque31d1d4b2014-05-05 22:08:14 -0500609Lsdb::findAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500610{
611 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
612 m_adjLsdb.end(),
613 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500614 if (it != m_adjLsdb.end()) {
akmhoqueb6450b12014-04-24 00:01:03 -0500615 return &(*it);
akmhoque53353462014-04-22 08:43:45 -0500616 }
akmhoqueb6450b12014-04-24 00:01:03 -0500617 return 0;
akmhoque53353462014-04-22 08:43:45 -0500618}
619
620
621bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500622Lsdb::isAdjLsaNew(const ndn::Name& key, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500623{
akmhoqueb6450b12014-04-24 00:01:03 -0500624 AdjLsa* adjLsaCheck = findAdjLsa(key);
akmhoque157b0a42014-05-13 00:26:37 -0500625 if (adjLsaCheck != 0) {
626 if (adjLsaCheck->getLsSeqNo() < seqNo) {
akmhoque53353462014-04-22 08:43:45 -0500627 return true;
628 }
akmhoque157b0a42014-05-13 00:26:37 -0500629 else {
akmhoque53353462014-04-22 08:43:45 -0500630 return false;
631 }
632 }
633 return true;
634}
635
636
637ndn::EventId
akmhoquec7a79b22014-05-26 08:06:19 -0500638Lsdb::scheduleAdjLsaExpiration(const ndn::Name& key, int seqNo,
639 const ndn::time::seconds& expTime)
akmhoque53353462014-04-22 08:43:45 -0500640{
Vince Lehman7c603292014-09-11 17:48:16 -0500641 return m_scheduler.scheduleEvent(expTime + GRACE_PERIOD,
642 ndn::bind(&Lsdb::exprireOrRefreshAdjLsa, this, key, seqNo));
akmhoque53353462014-04-22 08:43:45 -0500643}
644
645bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500646Lsdb::installAdjLsa(AdjLsa& alsa)
akmhoque53353462014-04-22 08:43:45 -0500647{
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700648 ndn::time::seconds timeToExpire = m_lsaRefreshTime;
akmhoqueb6450b12014-04-24 00:01:03 -0500649 AdjLsa* chkAdjLsa = findAdjLsa(alsa.getKey());
akmhoque157b0a42014-05-13 00:26:37 -0500650 if (chkAdjLsa == 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500651 _LOG_DEBUG("New Adj LSA. Adding to LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500652 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500653 alsa.writeLog();
akmhoque53353462014-04-22 08:43:45 -0500654 addAdjLsa(alsa);
akmhoque31d1d4b2014-05-05 22:08:14 -0500655 alsa.addNptEntries(m_nlsr);
656 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque157b0a42014-05-13 00:26:37 -0500657 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500658 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
659 ndn::time::system_clock::now();
660 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500661 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500662 scheduleAdjLsaExpiration(alsa.getKey(),
akmhoque53353462014-04-22 08:43:45 -0500663 alsa.getLsSeqNo(), timeToExpire);
664 }
akmhoque157b0a42014-05-13 00:26:37 -0500665 else {
666 if (chkAdjLsa->getLsSeqNo() < alsa.getLsSeqNo()) {
akmhoque674b0b12014-05-20 14:33:28 -0500667 _LOG_DEBUG("Updated Adj LSA. Updating LSDB");
akmhoque2f423352014-06-03 11:49:35 -0500668 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500669 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500670 chkAdjLsa->setLsSeqNo(alsa.getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500671 chkAdjLsa->setExpirationTimePoint(alsa.getExpirationTimePoint());
akmhoque157b0a42014-05-13 00:26:37 -0500672 if (!chkAdjLsa->isEqualContent(alsa)) {
akmhoqueb6450b12014-04-24 00:01:03 -0500673 chkAdjLsa->getAdl().reset();
akmhoquefdbddb12014-05-02 18:35:19 -0500674 chkAdjLsa->getAdl().addAdjacents(alsa.getAdl());
akmhoque31d1d4b2014-05-05 22:08:14 -0500675 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500676 }
akmhoque157b0a42014-05-13 00:26:37 -0500677 if (alsa.getOrigRouter() != m_nlsr.getConfParameter().getRouterPrefix()) {
akmhoquec7a79b22014-05-26 08:06:19 -0500678 ndn::time::system_clock::Duration duration = alsa.getExpirationTimePoint() -
679 ndn::time::system_clock::now();
680 timeToExpire = ndn::time::duration_cast<ndn::time::seconds>(duration);
akmhoque53353462014-04-22 08:43:45 -0500681 }
akmhoque31d1d4b2014-05-05 22:08:14 -0500682 cancelScheduleLsaExpiringEvent(chkAdjLsa->getExpiringEventId());
683 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(alsa.getKey(),
akmhoqueb6450b12014-04-24 00:01:03 -0500684 alsa.getLsSeqNo(),
685 timeToExpire));
akmhoque2f423352014-06-03 11:49:35 -0500686 _LOG_DEBUG("Adding Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500687 chkAdjLsa->writeLog();
akmhoque53353462014-04-22 08:43:45 -0500688 }
689 }
690 return true;
691}
692
693bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500694Lsdb::buildAndInstallOwnAdjLsa()
akmhoque53353462014-04-22 08:43:45 -0500695{
akmhoque31d1d4b2014-05-05 22:08:14 -0500696 AdjLsa adjLsa(m_nlsr.getConfParameter().getRouterPrefix(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500697 m_nlsr.getSequencingManager().getAdjLsaSeq() + 1,
akmhoquec7a79b22014-05-26 08:06:19 -0500698 getLsaExpirationTimePoint(),
akmhoque31d1d4b2014-05-05 22:08:14 -0500699 m_nlsr.getAdjacencyList().getNumOfActiveNeighbor(),
700 m_nlsr.getAdjacencyList());
Vince Lehman904c2412014-09-23 19:36:11 -0500701
Nick Gordon5c467f02016-07-13 13:40:10 -0500702 //Sync adjacency LSAs if link-state or dry-run HR is enabled.
703 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_ON) {
704 m_nlsr.getSequencingManager().increaseAdjLsaSeq();
705 m_sync.publishRoutingUpdate();
706 }
Vince Lehman904c2412014-09-23 19:36:11 -0500707
Vince Lehman9d097802015-03-16 17:55:59 -0500708 return installAdjLsa(adjLsa);
akmhoque53353462014-04-22 08:43:45 -0500709}
710
711bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500712Lsdb::removeAdjLsa(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500713{
714 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
715 m_adjLsdb.end(),
akmhoque157b0a42014-05-13 00:26:37 -0500716 ndn::bind(adjLsaCompareByKey, _1, key));
717 if (it != m_adjLsdb.end()) {
akmhoque2f423352014-06-03 11:49:35 -0500718 _LOG_DEBUG("Deleting Adj Lsa");
akmhoque674b0b12014-05-20 14:33:28 -0500719 (*it).writeLog();
akmhoque31d1d4b2014-05-05 22:08:14 -0500720 (*it).removeNptEntries(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500721 m_adjLsdb.erase(it);
722 return true;
723 }
724 return false;
725}
726
727bool
akmhoque31d1d4b2014-05-05 22:08:14 -0500728Lsdb::doesAdjLsaExist(const ndn::Name& key)
akmhoque53353462014-04-22 08:43:45 -0500729{
730 std::list<AdjLsa>::iterator it = std::find_if(m_adjLsdb.begin(),
731 m_adjLsdb.end(),
732 bind(adjLsaCompareByKey, _1, key));
akmhoque157b0a42014-05-13 00:26:37 -0500733 if (it == m_adjLsdb.end()) {
akmhoque53353462014-04-22 08:43:45 -0500734 return false;
735 }
736 return true;
737}
738
Jiewen Tana0497d82015-02-02 21:59:18 -0800739const std::list<AdjLsa>&
akmhoque53353462014-04-22 08:43:45 -0500740Lsdb::getAdjLsdb()
741{
742 return m_adjLsdb;
743}
744
745void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700746Lsdb::setLsaRefreshTime(const seconds& lsaRefreshTime)
akmhoque53353462014-04-22 08:43:45 -0500747{
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700748 m_lsaRefreshTime = lsaRefreshTime;
akmhoque53353462014-04-22 08:43:45 -0500749}
750
751void
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500752Lsdb::setThisRouterPrefix(std::string trp)
akmhoque53353462014-04-22 08:43:45 -0500753{
754 m_thisRouterPrefix = trp;
755}
756
757void
akmhoque31d1d4b2014-05-05 22:08:14 -0500758Lsdb::exprireOrRefreshNameLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500759{
akmhoque674b0b12014-05-20 14:33:28 -0500760 _LOG_DEBUG("Lsdb::exprireOrRefreshNameLsa Called");
761 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500762 NameLsa* chkNameLsa = findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500763 if (chkNameLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500764 _LOG_DEBUG("LSA Exists with seq no: " << chkNameLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500765 if (chkNameLsa->getLsSeqNo() == seqNo) {
766 if (chkNameLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500767 _LOG_DEBUG("Own Name LSA, so refreshing it");
768 _LOG_DEBUG("Deleting Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500769 chkNameLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500770 chkNameLsa->setLsSeqNo(chkNameLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500771 m_nlsr.getSequencingManager().setNameLsaSeq(chkNameLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500772 chkNameLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500773 _LOG_DEBUG("Adding Name Lsa");
akmhoqueb6450b12014-04-24 00:01:03 -0500774 chkNameLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500775 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500776 chkNameLsa->setExpiringEventId(scheduleNameLsaExpiration(chkNameLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500777 chkNameLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700778 m_lsaRefreshTime));
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600779 m_sync.publishRoutingUpdate();
akmhoque53353462014-04-22 08:43:45 -0500780 }
akmhoque157b0a42014-05-13 00:26:37 -0500781 else {
akmhoque674b0b12014-05-20 14:33:28 -0500782 _LOG_DEBUG("Other's Name LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500783 removeNameLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500784 }
785 }
786 }
787}
788
789void
akmhoque31d1d4b2014-05-05 22:08:14 -0500790Lsdb::exprireOrRefreshAdjLsa(const ndn::Name& lsaKey, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500791{
akmhoque674b0b12014-05-20 14:33:28 -0500792 _LOG_DEBUG("Lsdb::exprireOrRefreshAdjLsa Called");
793 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500794 AdjLsa* chkAdjLsa = findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500795 if (chkAdjLsa != 0) {
akmhoque2f423352014-06-03 11:49:35 -0500796 _LOG_DEBUG("LSA Exists with seq no: " << chkAdjLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500797 if (chkAdjLsa->getLsSeqNo() == seqNo) {
798 if (chkAdjLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500799 _LOG_DEBUG("Own Adj LSA, so refreshing it");
800 _LOG_DEBUG("Deleting Adj Lsa");
801 chkAdjLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500802 chkAdjLsa->setLsSeqNo(chkAdjLsa->getLsSeqNo() + 1);
akmhoque31d1d4b2014-05-05 22:08:14 -0500803 m_nlsr.getSequencingManager().setAdjLsaSeq(chkAdjLsa->getLsSeqNo());
akmhoquec7a79b22014-05-26 08:06:19 -0500804 chkAdjLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500805 _LOG_DEBUG("Adding Adj Lsa");
806 chkAdjLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500807 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500808 chkAdjLsa->setExpiringEventId(scheduleAdjLsaExpiration(chkAdjLsa->getKey(),
akmhoquefdbddb12014-05-02 18:35:19 -0500809 chkAdjLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700810 m_lsaRefreshTime));
Vince Lehman0bcf9a32014-12-10 11:24:45 -0600811 m_sync.publishRoutingUpdate();
akmhoque53353462014-04-22 08:43:45 -0500812 }
akmhoque157b0a42014-05-13 00:26:37 -0500813 else {
akmhoque674b0b12014-05-20 14:33:28 -0500814 _LOG_DEBUG("Other's Adj LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500815 removeAdjLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500816 }
817 // schedule Routing table calculaiton
akmhoque31d1d4b2014-05-05 22:08:14 -0500818 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500819 }
820 }
821}
822
823void
akmhoque31d1d4b2014-05-05 22:08:14 -0500824Lsdb::exprireOrRefreshCoordinateLsa(const ndn::Name& lsaKey,
akmhoqueb6450b12014-04-24 00:01:03 -0500825 uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500826{
akmhoque674b0b12014-05-20 14:33:28 -0500827 _LOG_DEBUG("Lsdb::exprireOrRefreshCorLsa Called ");
828 _LOG_DEBUG("LSA Key : " << lsaKey << " Seq No: " << seqNo);
akmhoqueb6450b12014-04-24 00:01:03 -0500829 CoordinateLsa* chkCorLsa = findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500830 if (chkCorLsa != 0) {
akmhoque674b0b12014-05-20 14:33:28 -0500831 _LOG_DEBUG("LSA Exists with seq no: " << chkCorLsa->getLsSeqNo());
akmhoque157b0a42014-05-13 00:26:37 -0500832 if (chkCorLsa->getLsSeqNo() == seqNo) {
833 if (chkCorLsa->getOrigRouter() == m_thisRouterPrefix) {
akmhoque2f423352014-06-03 11:49:35 -0500834 _LOG_DEBUG("Own Cor LSA, so refreshing it");
835 _LOG_DEBUG("Deleting Coordinate Lsa");
836 chkCorLsa->writeLog();
akmhoqueb6450b12014-04-24 00:01:03 -0500837 chkCorLsa->setLsSeqNo(chkCorLsa->getLsSeqNo() + 1);
Nick Gordon5c467f02016-07-13 13:40:10 -0500838 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
839 m_nlsr.getSequencingManager().setCorLsaSeq(chkCorLsa->getLsSeqNo());
840 }
841
akmhoquec7a79b22014-05-26 08:06:19 -0500842 chkCorLsa->setExpirationTimePoint(getLsaExpirationTimePoint());
akmhoque2f423352014-06-03 11:49:35 -0500843 _LOG_DEBUG("Adding Coordinate Lsa");
844 chkCorLsa->writeLog();
akmhoquefdbddb12014-05-02 18:35:19 -0500845 // schedule refreshing event again
akmhoque31d1d4b2014-05-05 22:08:14 -0500846 chkCorLsa->setExpiringEventId(scheduleCoordinateLsaExpiration(
847 chkCorLsa->getKey(),
848 chkCorLsa->getLsSeqNo(),
Alexander Afanasyev8388ec62014-08-16 18:38:57 -0700849 m_lsaRefreshTime));
Nick Gordon5c467f02016-07-13 13:40:10 -0500850 // Only sync coordinate LSAs if link-state routing is disabled
851 if (m_nlsr.getConfParameter().getHyperbolicState() != HYPERBOLIC_STATE_OFF) {
852 m_sync.publishRoutingUpdate();
853 }
akmhoque53353462014-04-22 08:43:45 -0500854 }
akmhoque157b0a42014-05-13 00:26:37 -0500855 else {
akmhoque674b0b12014-05-20 14:33:28 -0500856 _LOG_DEBUG("Other's Cor LSA, so removing form LSDB");
akmhoque31d1d4b2014-05-05 22:08:14 -0500857 removeCoordinateLsa(lsaKey);
akmhoque53353462014-04-22 08:43:45 -0500858 }
Nick Gordon5c467f02016-07-13 13:40:10 -0500859 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500860 m_nlsr.getRoutingTable().scheduleRoutingTableCalculation(m_nlsr);
akmhoque53353462014-04-22 08:43:45 -0500861 }
862 }
863 }
864}
865
akmhoque53353462014-04-22 08:43:45 -0500866void
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700867Lsdb::expressInterest(const ndn::Name& interestName, uint32_t timeoutCount,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500868 steady_clock::TimePoint deadline)
akmhoque31d1d4b2014-05-05 22:08:14 -0500869{
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500870 if (deadline == DEFAULT_LSA_RETRIEVAL_DEADLINE) {
871 deadline = steady_clock::now() + ndn::time::seconds(static_cast<int>(LSA_REFRESH_TIME_MAX));
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700872 }
873
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500874 ndn::Name lsaName = interestName.getSubName(0, interestName.size()-1);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500875 uint64_t seqNo = interestName[-1].toNumber();
876
877 if (m_highestSeqNo.find(lsaName) == m_highestSeqNo.end()) {
878 m_highestSeqNo[lsaName] = seqNo;
879 }
880 else if (seqNo > m_highestSeqNo[lsaName]) {
881 m_highestSeqNo[lsaName] = seqNo;
882 }
883 else if (seqNo < m_highestSeqNo[lsaName]) {
884 return;
885 }
886
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500887 ndn::Interest interest(interestName);
Alexander Afanasyev411ee4b2014-08-16 23:17:03 -0700888 interest.setInterestLifetime(m_nlsr.getConfParameter().getLsaInterestLifetime());
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500889
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500890 _LOG_DEBUG("Fetching Data for LSA: " << interestName << " Seq number: " << seqNo);
891 ndn::util::SegmentFetcher::fetch(m_nlsr.getNlsrFace(), interest,
892 m_nlsr.getValidator(),
893 ndn::bind(&Lsdb::afterFetchLsa, this, _1, interestName),
894 ndn::bind(&Lsdb::onFetchLsaError, this, _1, _2, interestName,
895 timeoutCount, deadline, lsaName, seqNo));
akmhoque31d1d4b2014-05-05 22:08:14 -0500896}
897
898void
899Lsdb::processInterest(const ndn::Name& name, const ndn::Interest& interest)
900{
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500901 const ndn::Name& interestName(interest.getName());
902 _LOG_DEBUG("Interest received for LSA: " << interestName);
903
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500904 std::string chkString("LSA");
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500905 int32_t lsaPosition = util::getNameComponentPosition(interest.getName(), chkString);
906
akmhoque157b0a42014-05-13 00:26:37 -0500907 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500908
909 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
910 originRouter.append(interestName.getSubName(lsaPosition + 1,
911 interest.getName().size() - lsaPosition - 3));
912
913 uint64_t seqNo = interestName[-1].toNumber();
914 _LOG_DEBUG("LSA sequence number from interest: " << seqNo);
915
916 std::string interestedLsType = interestName[-2].toUri();
917
alvy49b1c0c2014-12-19 13:57:46 -0600918 if (interestedLsType == NameLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500919 processInterestForNameLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500920 }
alvy49b1c0c2014-12-19 13:57:46 -0600921 else if (interestedLsType == AdjLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500922 processInterestForAdjacencyLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500923 }
alvy49b1c0c2014-12-19 13:57:46 -0600924 else if (interestedLsType == CoordinateLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500925 processInterestForCoordinateLsa(interest, originRouter.append(interestedLsType), seqNo);
akmhoque31d1d4b2014-05-05 22:08:14 -0500926 }
akmhoque157b0a42014-05-13 00:26:37 -0500927 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500928 _LOG_WARN("Received unrecognized LSA type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -0500929 }
930 }
931}
932
933void
akmhoque69c9aa92014-07-23 15:15:05 -0500934Lsdb::putLsaData(const ndn::Interest& interest, const std::string& content)
935{
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -0500936 LsaContentPublisher publisher(m_nlsr.getNlsrFace(),
937 m_nlsr.getKeyChain(),
938 m_lsaRefreshTime,
939 content);
940 publisher.publish(interest.getName(),
941 ndn::security::signingByCertificate(m_nlsr.getDefaultCertName()));
akmhoque69c9aa92014-07-23 15:15:05 -0500942}
943
944void
akmhoque31d1d4b2014-05-05 22:08:14 -0500945Lsdb::processInterestForNameLsa(const ndn::Interest& interest,
946 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500947 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500948{
949 NameLsa* nameLsa = m_nlsr.getLsdb().findNameLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500950 if (nameLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500951 if (nameLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500952 std::string content = nameLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500953 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500954 }
955 }
956}
957
958void
959Lsdb::processInterestForAdjacencyLsa(const ndn::Interest& interest,
960 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500961 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500962{
Nick Gordon5c467f02016-07-13 13:40:10 -0500963 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_ON) {
964 _LOG_ERROR("Received interest for an adjacency LSA when hyperbolic routing is enabled.");
965 }
966
akmhoque31d1d4b2014-05-05 22:08:14 -0500967 AdjLsa* adjLsa = m_nlsr.getLsdb().findAdjLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500968 if (adjLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500969 if (adjLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500970 std::string content = adjLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500971 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500972 }
973 }
974}
975
976void
977Lsdb::processInterestForCoordinateLsa(const ndn::Interest& interest,
978 const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500979 uint64_t seqNo)
akmhoque31d1d4b2014-05-05 22:08:14 -0500980{
Nick Gordon5c467f02016-07-13 13:40:10 -0500981 if (m_nlsr.getConfParameter().getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
982 _LOG_ERROR("Received Interest for a coordinate LSA when link-state routing is enabled.");
983 }
984
akmhoque31d1d4b2014-05-05 22:08:14 -0500985 CoordinateLsa* corLsa = m_nlsr.getLsdb().findCoordinateLsa(lsaKey);
akmhoque157b0a42014-05-13 00:26:37 -0500986 if (corLsa != 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500987 if (corLsa->getLsSeqNo() == seqNo) {
akmhoque31d1d4b2014-05-05 22:08:14 -0500988 std::string content = corLsa->getData();
akmhoque69c9aa92014-07-23 15:15:05 -0500989 putLsaData(interest,content);
akmhoque31d1d4b2014-05-05 22:08:14 -0500990 }
991 }
992}
993
994void
Yingdi Yu20e3a6e2014-05-26 23:16:10 -0700995Lsdb::onContentValidated(const ndn::shared_ptr<const ndn::Data>& data)
996{
997 const ndn::Name& dataName = data->getName();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -0500998 _LOG_DEBUG("Data validation successful for LSA: " << dataName);
999
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001000 std::string chkString("LSA");
akmhoque31d1d4b2014-05-05 22:08:14 -05001001 int32_t lsaPosition = util::getNameComponentPosition(dataName, chkString);
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001002
akmhoque157b0a42014-05-13 00:26:37 -05001003 if (lsaPosition >= 0) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001004
1005 ndn::Name originRouter = m_nlsr.getConfParameter().getNetwork();
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001006 originRouter.append(dataName.getSubName(lsaPosition + 1, dataName.size() - lsaPosition - 3));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001007
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001008 uint64_t seqNo = dataName[-1].toNumber();
1009 std::string dataContent(reinterpret_cast<const char*>(data->getContent().value()));
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001010
Muktadir R Chowdhuryaa3b0852015-08-06 13:08:56 -05001011 std::string interestedLsType = dataName[-2].toUri();
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001012
alvy49b1c0c2014-12-19 13:57:46 -06001013 if (interestedLsType == NameLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001014 processContentNameLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001015 }
alvy49b1c0c2014-12-19 13:57:46 -06001016 else if (interestedLsType == AdjLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001017 processContentAdjacencyLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001018 }
alvy49b1c0c2014-12-19 13:57:46 -06001019 else if (interestedLsType == CoordinateLsa::TYPE_STRING) {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001020 processContentCoordinateLsa(originRouter.append(interestedLsType), seqNo, dataContent);
akmhoque31d1d4b2014-05-05 22:08:14 -05001021 }
akmhoque157b0a42014-05-13 00:26:37 -05001022 else {
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001023 _LOG_WARN("Received unrecognized LSA Type: " << interestedLsType);
akmhoque31d1d4b2014-05-05 22:08:14 -05001024 }
1025 }
1026}
1027
1028void
1029Lsdb::processContentNameLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001030 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001031{
akmhoque157b0a42014-05-13 00:26:37 -05001032 if (isNameLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001033 NameLsa nameLsa;
akmhoque157b0a42014-05-13 00:26:37 -05001034 if (nameLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001035 installNameLsa(nameLsa);
1036 }
akmhoque157b0a42014-05-13 00:26:37 -05001037 else {
akmhoque2f423352014-06-03 11:49:35 -05001038 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001039 }
1040 }
1041}
1042
1043void
1044Lsdb::processContentAdjacencyLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001045 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001046{
akmhoque157b0a42014-05-13 00:26:37 -05001047 if (isAdjLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001048 AdjLsa adjLsa;
akmhoque157b0a42014-05-13 00:26:37 -05001049 if (adjLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001050 installAdjLsa(adjLsa);
1051 }
akmhoque157b0a42014-05-13 00:26:37 -05001052 else {
akmhoque2f423352014-06-03 11:49:35 -05001053 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001054 }
1055 }
1056}
1057
1058void
1059Lsdb::processContentCoordinateLsa(const ndn::Name& lsaKey,
Ashlesh Gawande5bf83172014-09-19 12:38:17 -05001060 uint64_t lsSeqNo, std::string& dataContent)
akmhoque31d1d4b2014-05-05 22:08:14 -05001061{
akmhoque157b0a42014-05-13 00:26:37 -05001062 if (isCoordinateLsaNew(lsaKey, lsSeqNo)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001063 CoordinateLsa corLsa;
akmhoque157b0a42014-05-13 00:26:37 -05001064 if (corLsa.initializeFromContent(dataContent)) {
akmhoque31d1d4b2014-05-05 22:08:14 -05001065 installCoordinateLsa(corLsa);
1066 }
akmhoque157b0a42014-05-13 00:26:37 -05001067 else {
akmhoque2f423352014-06-03 11:49:35 -05001068 _LOG_DEBUG("LSA data decoding error :(");
akmhoque31d1d4b2014-05-05 22:08:14 -05001069 }
1070 }
1071}
1072
akmhoquec7a79b22014-05-26 08:06:19 -05001073ndn::time::system_clock::TimePoint
1074Lsdb::getLsaExpirationTimePoint()
1075{
1076 ndn::time::system_clock::TimePoint expirationTimePoint = ndn::time::system_clock::now();
1077 expirationTimePoint = expirationTimePoint +
1078 ndn::time::seconds(m_nlsr.getConfParameter().getRouterDeadInterval());
1079 return expirationTimePoint;
1080}
akmhoque31d1d4b2014-05-05 22:08:14 -05001081
1082void
akmhoque2f423352014-06-03 11:49:35 -05001083Lsdb::writeAdjLsdbLog()
akmhoque53353462014-04-22 08:43:45 -05001084{
akmhoque2f423352014-06-03 11:49:35 -05001085 _LOG_DEBUG("---------------Adj LSDB-------------------");
akmhoque53353462014-04-22 08:43:45 -05001086 for (std::list<AdjLsa>::iterator it = m_adjLsdb.begin();
akmhoque157b0a42014-05-13 00:26:37 -05001087 it != m_adjLsdb.end() ; it++) {
akmhoque2f423352014-06-03 11:49:35 -05001088 (*it).writeLog();
akmhoque53353462014-04-22 08:43:45 -05001089 }
1090}
1091
1092//-----utility function -----
1093bool
akmhoque31d1d4b2014-05-05 22:08:14 -05001094Lsdb::doesLsaExist(const ndn::Name& key, const std::string& lsType)
akmhoque53353462014-04-22 08:43:45 -05001095{
alvy49b1c0c2014-12-19 13:57:46 -06001096 if (lsType == NameLsa::TYPE_STRING) {
akmhoque53353462014-04-22 08:43:45 -05001097 return doesNameLsaExist(key);
1098 }
alvy49b1c0c2014-12-19 13:57:46 -06001099 else if (lsType == AdjLsa::TYPE_STRING) {
akmhoque53353462014-04-22 08:43:45 -05001100 return doesAdjLsaExist(key);
1101 }
alvy49b1c0c2014-12-19 13:57:46 -06001102 else if (lsType == CoordinateLsa::TYPE_STRING) {
akmhoqueb6450b12014-04-24 00:01:03 -05001103 return doesCoordinateLsaExist(key);
akmhoque53353462014-04-22 08:43:45 -05001104 }
1105 return false;
1106}
1107
Alexander Afanasyev8388ec62014-08-16 18:38:57 -07001108} // namespace nlsr