blob: 308457e6f617d1cf082feb9bc927fe8db6379179 [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
Nick Gordon563611e2018-01-23 13:44:36 -060036SyncLogicHandler::SyncLogicHandler(ndn::Face& face, const IsLsaNew& isLsaNew,
37 const ConfParameter& conf)
Davide Pesaventoa08dc3f2018-05-24 00:40:28 -040038 : onNewLsa(std::make_unique<OnNewLsa>())
Nick Gordon9eac4d92017-08-29 17:31:29 -050039 , m_syncFace(face)
40 , m_isLsaNew(isLsaNew)
Vince Lehman0bcf9a32014-12-10 11:24:45 -060041 , m_confParam(conf)
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050042 , m_nameLsaUserPrefix(ndn::Name(m_confParam.getSyncUserPrefix()).append(std::to_string(Lsa::Type::NAME)))
43 , m_syncLogic(m_syncFace, m_confParam.getSyncProtocol(), m_confParam.getSyncPrefix(),
44 m_nameLsaUserPrefix, m_confParam.getSyncInterestLifetime(),
45 std::bind(&SyncLogicHandler::processUpdate, this, _1, _2))
Vince Lehman0bcf9a32014-12-10 11:24:45 -060046{
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050047 m_adjLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
48 .append(std::to_string(Lsa::Type::ADJACENCY));
49 m_coorLsaUserPrefix = ndn::Name(m_confParam.getSyncUserPrefix())
50 .append(std::to_string(Lsa::Type::COORDINATE));
Vince Lehman0bcf9a32014-12-10 11:24:45 -060051
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050052 if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF ||
53 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
54 m_syncLogic.addUserNode(m_adjLsaUserPrefix);
Vince Lehman9d097802015-03-16 17:55:59 -050055 }
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -050056 else if (m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON ||
57 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_DRY_RUN) {
58 m_syncLogic.addUserNode(m_coorLsaUserPrefix);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050059 }
akmhoque53353462014-04-22 08:43:45 -050060}
61
62void
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050063SyncLogicHandler::processUpdate(const ndn::Name& updateName, uint64_t highSeq)
akmhoque53353462014-04-22 08:43:45 -050064{
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050065 NLSR_LOG_DEBUG("Update Name: " << updateName << " Seq no: " << highSeq);
Vince Lehman904c2412014-09-23 19:36:11 -050066
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050067 int32_t nlsrPosition = util::getNameComponentPosition(updateName, nlsr::NLSR_COMPONENT);
68 int32_t lsaPosition = util::getNameComponentPosition(updateName, nlsr::LSA_COMPONENT);
Vince Lehman904c2412014-09-23 19:36:11 -050069
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050070 if (nlsrPosition < 0 || lsaPosition < 0) {
71 NLSR_LOG_WARN("Received malformed sync update");
72 return;
akmhoque53353462014-04-22 08:43:45 -050073 }
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050074
75 ndn::Name networkName = updateName.getSubName(1, nlsrPosition-1);
76 ndn::Name routerName = updateName.getSubName(lsaPosition + 1).getPrefix(-1);
77
78 ndn::Name originRouter = networkName;
79 originRouter.append(routerName);
80
81 processUpdateFromSync(originRouter, updateName, highSeq);
akmhoque53353462014-04-22 08:43:45 -050082}
83
84void
Ashlesh Gawande3e105a02017-05-16 17:36:56 -050085SyncLogicHandler::processUpdateFromSync(const ndn::Name& originRouter,
Ashlesh Gawande32ec3fd2018-07-18 13:42:32 -050086 const ndn::Name& updateName, uint64_t seqNo)
akmhoque53353462014-04-22 08:43:45 -050087{
dmcoomes5bcb39e2017-10-31 15:07:55 -050088 NLSR_LOG_DEBUG("Origin Router of update: " << originRouter);
akmhoque53353462014-04-22 08:43:45 -050089
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050090 // A router should not try to fetch its own LSA
91 if (originRouter != m_confParam.getRouterPrefix()) {
Vince Lehman904c2412014-09-23 19:36:11 -050092
Nick Gordon727d4832017-10-13 18:04:25 -050093 Lsa::Type lsaType;
94 std::istringstream(updateName.get(updateName.size()-1).toUri()) >> lsaType;
Vince Lehman904c2412014-09-23 19:36:11 -050095
Ashlesh Gawande85998a12017-12-07 22:22:13 -060096 NLSR_LOG_DEBUG("Received sync update with higher " << lsaType <<
97 " sequence number than entry in LSDB");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -050098
Nick Gordon9eac4d92017-08-29 17:31:29 -050099 if (m_isLsaNew(originRouter, lsaType, seqNo)) {
Nick Gordon727d4832017-10-13 18:04:25 -0500100 if (lsaType == Lsa::Type::ADJACENCY && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500101 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_ON) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600102 NLSR_LOG_ERROR("Got an update for adjacency LSA when hyperbolic routing " <<
103 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500104 return;
105 }
106
Nick Gordon727d4832017-10-13 18:04:25 -0500107 if (lsaType == Lsa::Type::COORDINATE && seqNo != 0 &&
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500108 m_confParam.getHyperbolicState() == HYPERBOLIC_STATE_OFF) {
Ashlesh Gawande85998a12017-12-07 22:22:13 -0600109 NLSR_LOG_ERROR("Got an update for coordinate LSA when link-state " <<
110 "is enabled. Not going to fetch.");
Nick Gordon0f1bf1d2017-06-22 15:40:27 -0500111 return;
112 }
Ashlesh Gawande08bce9c2019-04-05 11:08:07 -0500113 (*onNewLsa)(updateName, seqNo, originRouter);
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500114 }
akmhoque53353462014-04-22 08:43:45 -0500115 }
116}
117
Vince Lehman904c2412014-09-23 19:36:11 -0500118void
Nick Gordon727d4832017-10-13 18:04:25 -0500119SyncLogicHandler::publishRoutingUpdate(const Lsa::Type& type, const uint64_t& seqNo)
Vince Lehman904c2412014-09-23 19:36:11 -0500120{
Nick Gordon727d4832017-10-13 18:04:25 -0500121 switch (type) {
122 case Lsa::Type::ADJACENCY:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500123 m_syncLogic.publishUpdate(m_adjLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500124 break;
125 case Lsa::Type::COORDINATE:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500126 m_syncLogic.publishUpdate(m_coorLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500127 break;
128 case Lsa::Type::NAME:
Ashlesh Gawande6b388fc2019-09-30 10:14:41 -0500129 m_syncLogic.publishUpdate(m_nameLsaUserPrefix, seqNo);
Nick Gordon727d4832017-10-13 18:04:25 -0500130 break;
131 default:
132 break;
Ashlesh Gawande3e105a02017-05-16 17:36:56 -0500133 }
Vince Lehman904c2412014-09-23 19:36:11 -0500134}
135
Nick Gordonfad8e252016-08-11 14:21:38 -0500136} // namespace nlsr