Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The University of Memphis, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 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/>. |
| 20 | **/ |
| 21 | |
Nick G | 97e3494 | 2016-07-11 14:46:27 -0500 | [diff] [blame] | 22 | /*! \file lsdb-dataset-interest-handler.cpp |
| 23 | |
| 24 | This file details a class that is used by NLSRC and other command-line |
| 25 | tools to examine the state of NLSR. This system is not designed to |
| 26 | be used by routers to publish data to each other. |
| 27 | */ |
| 28 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 29 | #include "lsdb-dataset-interest-handler.hpp" |
| 30 | |
| 31 | #include "logger.hpp" |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 32 | #include "nlsr.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 33 | |
| 34 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 35 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 36 | #include <ndn-cxx/util/regex.hpp> |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 37 | #include "tlv/lsdb-status.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 38 | |
| 39 | namespace nlsr { |
| 40 | |
| 41 | INIT_LOGGER("LsdbDatasetInterestHandler"); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 42 | const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies"); |
| 43 | const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates"); |
| 44 | const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names"); |
| 45 | const ndn::PartialName LISTS_DATASET = ndn::PartialName("lsdb/list"); |
| 46 | const ndn::PartialName LsdbDatasetInterestHandler::LOCALHOST_COMMAND_PREFIX = |
| 47 | ndn::Name(Nlsr::LOCALHOST_PREFIX).append(Lsdb::NAME_COMPONENT); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 48 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 49 | LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb, |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 50 | ndn::mgmt::Dispatcher& localHostDispatcher, |
| 51 | ndn::mgmt::Dispatcher& routerNameDispatcher, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 52 | ndn::Face& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 53 | ndn::KeyChain& keyChain) |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 54 | : m_localhostDispatcher(localHostDispatcher) |
| 55 | , m_routerNameDispatcher(routerNameDispatcher) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 56 | , m_adjacencyLsaPublisher(lsdb, face, keyChain) |
| 57 | , m_coordinateLsaPublisher(lsdb, face, keyChain) |
| 58 | , m_nameLsaPublisher(lsdb, face, keyChain) |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 59 | , m_adjacencyLsas(lsdb.getAdjLsdb()) |
| 60 | , m_coordinateLsas(lsdb.getCoordinateLsdb()) |
| 61 | , m_nameLsas(lsdb.getNameLsdb()) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 62 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 63 | _LOG_DEBUG("Setting dispatcher for lsdb status dataset:"); |
| 64 | setDispatcher(m_localhostDispatcher); |
| 65 | setDispatcher(m_routerNameDispatcher); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 69 | LsdbDatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 70 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 71 | dispatcher.addStatusDataset(ADJACENCIES_DATASET, |
| 72 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 73 | std::bind(&LsdbDatasetInterestHandler::publishAdjStatus, this, _1, _2, _3)); |
| 74 | dispatcher.addStatusDataset(COORDINATES_DATASET, |
| 75 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 76 | std::bind(&LsdbDatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3)); |
| 77 | dispatcher.addStatusDataset(NAMES_DATASET, |
| 78 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 79 | std::bind(&LsdbDatasetInterestHandler::publishNameStatus, this, _1, _2, _3)); |
| 80 | dispatcher.addStatusDataset(LISTS_DATASET, |
| 81 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 82 | std::bind(&LsdbDatasetInterestHandler::publishAllStatus, this, _1, _2, _3)); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 86 | LsdbDatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 87 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 88 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 89 | _LOG_DEBUG("Received interest: " << interest); |
| 90 | for (AdjLsa lsa : m_adjacencyLsas) { |
| 91 | tlv::AdjacencyLsa tlvLsa; |
| 92 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa); |
| 93 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 94 | |
| 95 | for (const Adjacent& adj : lsa.getAdl().getAdjList()) { |
| 96 | tlv::Adjacency tlvAdj; |
| 97 | tlvAdj.setName(adj.getName()); |
| 98 | tlvAdj.setUri(adj.getFaceUri().toString()); |
| 99 | tlvAdj.setCost(adj.getLinkCost()); |
| 100 | tlvLsa.addAdjacency(tlvAdj); |
| 101 | } |
| 102 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 103 | context.append(wire); |
| 104 | } |
| 105 | context.end(); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 109 | LsdbDatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 110 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 111 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 112 | _LOG_DEBUG("Received interest: " << interest); |
| 113 | for (const CoordinateLsa lsa : m_coordinateLsas) { |
| 114 | tlv::CoordinateLsa tlvLsa; |
| 115 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa); |
| 116 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 117 | |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 118 | tlvLsa.setHyperbolicRadius(lsa.getCorRadius()); |
| 119 | tlvLsa.setHyperbolicAngle(lsa.getCorTheta()); |
| 120 | |
| 121 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 122 | context.append(wire); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 123 | } |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 124 | context.end(); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 128 | LsdbDatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 129 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 130 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 131 | _LOG_DEBUG("Received interest: " << interest); |
| 132 | for (NameLsa lsa : m_nameLsas) { |
| 133 | tlv::NameLsa tlvLsa; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 134 | |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 135 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(lsa); |
| 136 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 137 | |
| 138 | for (const ndn::Name& name : lsa.getNpl().getNameList()) { |
| 139 | tlvLsa.addName(name); |
| 140 | } |
| 141 | |
| 142 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 143 | context.append(wire); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 144 | } |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 145 | context.end(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 149 | LsdbDatasetInterestHandler::publishAllStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 150 | ndn::mgmt::StatusDatasetContext& context) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 151 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 152 | _LOG_DEBUG("Received interest: " << interest); |
| 153 | tlv::LsdbStatus lsdbStatus; |
| 154 | for (const tlv::AdjacencyLsa& tlvLsa : m_adjacencyLsaPublisher.getTlvLsas()) { |
| 155 | lsdbStatus.addAdjacencyLsa(tlvLsa); |
| 156 | } |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 157 | |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 158 | for (const tlv::CoordinateLsa& tlvLsa : m_coordinateLsaPublisher.getTlvLsas()) { |
| 159 | lsdbStatus.addCoordinateLsa(tlvLsa); |
| 160 | } |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 161 | |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame^] | 162 | for (const tlv::NameLsa& tlvLsa : m_nameLsaPublisher.getTlvLsas()) { |
| 163 | lsdbStatus.addNameLsa(tlvLsa); |
| 164 | } |
| 165 | const ndn::Block& wire = lsdbStatus.wireEncode(); |
| 166 | context.append(wire); |
| 167 | context.end(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | } // namespace nlsr |