blob: 1784267066fa90ed9795f2ecfe3972bb3ac3703a [file] [log] [blame]
akmhoque3d06e792014-05-27 16:23:20 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Ashlesh Gawande85998a12017-12-07 22:22:13 -06003 * Copyright (c) 2014-2019, The University of Memphis,
Vince Lehmanc2e51f62015-01-20 15:03:11 -06004 * Regents of the University of California,
5 * Arizona Board of Regents.
akmhoque3d06e792014-05-27 16:23:20 -05006 *
7 * This file is part of NLSR (Named-data Link State Routing).
8 * See AUTHORS.md for complete list of NLSR authors and contributors.
9 *
10 * NLSR is free software: you can redistribute it and/or modify it under the terms
11 * of the GNU General Public License as published by the Free Software Foundation,
12 * either version 3 of the License, or (at your option) any later version.
13 *
14 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 * PURPOSE. See the GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
akmhoque3d06e792014-05-27 16:23:20 -050020 **/
Vince Lehmanc2e51f62015-01-20 15:03:11 -060021
akmhoque53353462014-04-22 08:43:45 -050022#include "sync-logic-handler.hpp"
Vince Lehman0a7da612014-10-29 14:39:29 -050023#include "common.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050024#include "conf-parameter.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050025#include "lsa.hpp"
Nick Gordon9eac4d92017-08-29 17:31:29 -050026#include "logger.hpp"
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050027#include "utility/name-helper.hpp"
Vince Lehman904c2412014-09-23 19:36:11 -050028
akmhoque53353462014-04-22 08:43:45 -050029namespace nlsr {
30
Ashlesh Gawandecba0ae22018-03-27 17:57:56 -050031const std::string NLSR_COMPONENT = "nlsr";
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050032const std::string LSA_COMPONENT = "LSA";
Vince Lehman904c2412014-09-23 19:36:11 -050033
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050034INIT_LOGGER(SyncLogicHandler);
35
Alexander Afanasyev7c8882f2014-10-28 12:12:15 -070036template<class T>
37class NullDeleter
38{
39public:
40 void
41 operator()(T*)
42 {
43 }
44};
45
Nick Gordon563611e2018-01-23 13:44:36 -060046SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
47 const ConfParameter& conf)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040048 : onNewLsa(std::make_unique<OnNewLsa>())
Nick Gordon9eac4d92017-08-29 17:31:29 -050049 , m_syncFace(face)
50 , m_isLsaNew(isLsaNew)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060051 , m_confParam(conf)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060052{
Ashlesh Gawande85998a12017-12-07 22:22:13 -060053 createSyncLogic(conf.getSyncPrefix());
Vince Lehman0bcf9a32014-12-10 11:24:45 -060054}
55
akmhoque53353462014-04-22 08:43:45 -050056void
Ashlesh Gawande48101072018-05-30 17:53:06 -050057SyncLogicHandler::createSyncLogic(const ndn::Name& syncPrefix, const ndn::time::milliseconds& syncInterestLifetime)
akmhoque53353462014-04-22 08:43:45 -050058{
Ashlesh Gawande48101072018-05-30 17:53:06 -050059 if (m_syncLogic != nullptr) {
60 NLSR_LOG_WARN("Trying to create Sync Logic object, but Sync Logic object already exists");
Vince Lehman9d097802015-03-16 17:55:59 -050061 return;
62 }
63
Vince Lehman9d097802015-03-16 17:55:59 -050064 // Build LSA sync update prefix
65 buildUpdatePrefix();
66
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050067 NLSR_LOG_DEBUG("Creating Sync Logic object. Sync Prefix: " << syncPrefix);
Vince Lehman904c2412014-09-23 19:36:11 -050068
Ashlesh Gawande48101072018-05-30 17:53:06 -050069 // The face's lifetime is managed in main.cpp; Logic should not manage the memory
Vince Lehman904c2412014-09-23 19:36:11 -050070 // of the object
dmcoomes9f936662017-03-02 10:33:09 -060071 std::shared_ptr<ndn::Face> facePtr(&m_syncFace, NullDeleter<ndn::Face>());
Vince Lehman904c2412014-09-23 19:36:11 -050072
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050073 m_syncLogic = std::make_shared<SyncProtocolAdapter>(*facePtr,
74 m_confParam.getSyncProtocol(),
75 syncPrefix,
76 m_nameLsaUserPrefix,
77 syncInterestLifetime,
78 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050079
80 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050081 m_syncLogic->addUserNode(m_adjLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050082 }
laqinfanac4b6562017-12-08 11:24:21 +000083 else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050084 m_syncLogic->addUserNode(m_coorLsaUserPrefix);
laqinfanac4b6562017-12-08 11:24:21 +000085 }
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050086 else {
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050087 m_syncLogic->addUserNode(m_adjLsaUserPrefix);
88 m_syncLogic->addUserNode(m_coorLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050089 }
akmhoque53353462014-04-22 08:43:45 -050090}
91
92void
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050093SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq)
akmhoque53353462014-04-22 08:43:45 -050094{
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050095 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
Vince Lehman904c2412014-09-23 19:36:11 -050096
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050097 int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT);
98 int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT);
Vince Lehman904c2412014-09-23 19:36:11 -050099
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500100 if (nlsrPosition < 0 || lsaPosition < 0) {
101 NLSR_LOG_WARN("Received malformed sync update");
102 return;
akmhoque53353462014-04-22 08:43:45 -0500103 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500104
105 ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
106 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
107
108 ndn::Name originRouter = networkName;
109 originRouter.append(routerName);
110
111 processUpdateFromSync(originRouter, updateName, highSeq);
akmhoque53353462014-04-22 08:43:45 -0500112}
113
114void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500115SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500116 const ndn::Name& updateName, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -0500117{
dmcoomes5bcb39e2017-10-31 15:07:55 -0500118 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -0500119
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500120 // A router should not try to fetch its own LSA
121 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -0500122
Nick Gordon727d4832017-10-13 18:04:25 -0500123 Lsa::Type lsaType;
124 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -0500125
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600126 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
127 " sequence number than entry in LSDB");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500128
Nick Gordon9eac4d92017-08-29 17:31:29 -0500129 if (m_isLsaNew(originRouter, lsaType, seqNo)) {
Nick Gordon727d4832017-10-13 18:04:25 -0500130 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500131 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600132 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
133 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500134 return;
135 }
136
Nick Gordon727d4832017-10-13 18:04:25 -0500137 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500138 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600139 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
140 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500141 return;
142 }
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500143 (*onNewLsa)(updateName, seqNo, originRouter);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500144 }
akmhoque53353462014-04-22 08:43:45 -0500145 }
146}
147
Vince Lehman904c2412014-09-23 19:36:11 -0500148void
Nick Gordon727d4832017-10-13 18:04:25 -0500149SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500150{
Ashlesh Gawande48101072018-05-30 17:53:06 -0500151 if (m_syncLogic == nullptr) {
152 NLSR_LOG_FATAL("Cannot publish routing update; SyncLogic does not exist");
Vince Lehman9d097802015-03-16 17:55:59 -0500153
Ashlesh Gawande48101072018-05-30 17:53:06 -0500154 BOOST_THROW_EXCEPTION(SyncLogicHandler::Error("Cannot publish routing update; SyncLogic does not exist"));
Vince Lehman9d097802015-03-16 17:55:59 -0500155 }
156
Nick Gordon727d4832017-10-13 18:04:25 -0500157 switch (type) {
158 case Lsa::Type::ADJACENCY:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500159 m_syncLogic->publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500160 break;
161 case Lsa::Type::COORDINATE:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500162 m_syncLogic->publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500163 break;
164 case Lsa::Type::NAME:
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -0500165 m_syncLogic->publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500166 break;
167 default:
168 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500169 }
Vince Lehman904c2412014-09-23 19:36:11 -0500170}
171
172void
Vince Lehmanc11cc202015-01-20 11:41:33 -0600173SyncLogicHandler::buildUpdatePrefix()
174{
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500175 ndn::Name updatePrefix = m_confParam.getLsaPrefix();
176 updatePrefix.append(m_confParam.getSiteName());
177 updatePrefix.append(m_confParam.getRouterName());
178
179 m_nameLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500180 m_nameLsaUserPrefix.append(std::to_string(Lsa::Type::NAME));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500181
182 m_adjLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500183 m_adjLsaUserPrefix.append(std::to_string(Lsa::Type::ADJACENCY));
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500184
185 m_coorLsaUserPrefix = updatePrefix;
Nick Gordon727d4832017-10-13 18:04:25 -0500186 m_coorLsaUserPrefix.append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehmanc11cc202015-01-20 11:41:33 -0600187}
188
Nick Gordonfad8e252016-08-11 14:21:38 -0500189} // namespace nlsr