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" |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 30 | |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 31 | #include "nlsr.hpp" |
Ashlesh Gawande | 90173ad | 2017-08-09 15:19:50 -0500 | [diff] [blame] | 32 | #include "tlv/lsdb-status.hpp" |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 33 | #include "logger.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 34 | |
| 35 | #include <ndn-cxx/face.hpp> |
Junxiao Shi | 3e5120c | 2016-09-10 16:58:34 +0000 | [diff] [blame] | 36 | #include <ndn-cxx/mgmt/nfd/control-response.hpp> |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 37 | #include <ndn-cxx/util/regex.hpp> |
| 38 | |
| 39 | namespace nlsr { |
| 40 | |
| 41 | INIT_LOGGER("LsdbDatasetInterestHandler"); |
Ashlesh Gawande | 90173ad | 2017-08-09 15:19:50 -0500 | [diff] [blame] | 42 | |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 43 | const ndn::PartialName ADJACENCIES_DATASET = ndn::PartialName("lsdb/adjacencies"); |
| 44 | const ndn::PartialName COORDINATES_DATASET = ndn::PartialName("lsdb/coordinates"); |
| 45 | const ndn::PartialName NAMES_DATASET = ndn::PartialName("lsdb/names"); |
| 46 | const ndn::PartialName LISTS_DATASET = ndn::PartialName("lsdb/list"); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 47 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 48 | LsdbDatasetInterestHandler::LsdbDatasetInterestHandler(Lsdb& lsdb, |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 49 | ndn::mgmt::Dispatcher& localHostDispatcher, |
| 50 | ndn::mgmt::Dispatcher& routerNameDispatcher, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 51 | ndn::Face& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 52 | ndn::KeyChain& keyChain) |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 53 | : m_lsdb(lsdb) |
| 54 | , m_localhostDispatcher(localHostDispatcher) |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 55 | , m_routerNameDispatcher(routerNameDispatcher) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 56 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame^] | 57 | NLSR_LOG_DEBUG("Setting dispatcher for lsdb status dataset:"); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 58 | setDispatcher(m_localhostDispatcher); |
| 59 | setDispatcher(m_routerNameDispatcher); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 63 | LsdbDatasetInterestHandler::setDispatcher(ndn::mgmt::Dispatcher& dispatcher) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 64 | { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 65 | dispatcher.addStatusDataset(ADJACENCIES_DATASET, |
| 66 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 67 | std::bind(&LsdbDatasetInterestHandler::publishAdjStatus, this, _1, _2, _3)); |
| 68 | dispatcher.addStatusDataset(COORDINATES_DATASET, |
| 69 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 70 | std::bind(&LsdbDatasetInterestHandler::publishCoordinateStatus, this, _1, _2, _3)); |
| 71 | dispatcher.addStatusDataset(NAMES_DATASET, |
| 72 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 73 | std::bind(&LsdbDatasetInterestHandler::publishNameStatus, this, _1, _2, _3)); |
| 74 | dispatcher.addStatusDataset(LISTS_DATASET, |
| 75 | ndn::mgmt::makeAcceptAllAuthorization(), |
| 76 | std::bind(&LsdbDatasetInterestHandler::publishAllStatus, this, _1, _2, _3)); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 80 | LsdbDatasetInterestHandler::publishAdjStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 81 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 82 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame^] | 83 | NLSR_LOG_DEBUG("Received interest: " << interest); |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 84 | |
| 85 | auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator, |
| 86 | std::list<AdjLsa>::const_iterator>( |
| 87 | m_lsdb.getAdjLsdb().cbegin(), m_lsdb.getAdjLsdb().cend()); |
| 88 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 89 | tlv::AdjacencyLsa tlvLsa; |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 90 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 91 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 92 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 93 | for (const Adjacent& adj : lsa->getAdl().getAdjList()) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 94 | tlv::Adjacency tlvAdj; |
| 95 | tlvAdj.setName(adj.getName()); |
| 96 | tlvAdj.setUri(adj.getFaceUri().toString()); |
| 97 | tlvAdj.setCost(adj.getLinkCost()); |
| 98 | tlvLsa.addAdjacency(tlvAdj); |
| 99 | } |
| 100 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 101 | context.append(wire); |
| 102 | } |
| 103 | context.end(); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 107 | LsdbDatasetInterestHandler::publishCoordinateStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 108 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 109 | { |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 110 | auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator, |
| 111 | std::list<CoordinateLsa>::const_iterator>( |
| 112 | m_lsdb.getCoordinateLsdb().cbegin(), m_lsdb.getCoordinateLsdb().cend()); |
| 113 | |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame^] | 114 | NLSR_LOG_DEBUG("Received interest: " << interest); |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 115 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 116 | tlv::CoordinateLsa tlvLsa; |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 117 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 118 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 119 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 120 | tlvLsa.setHyperbolicRadius(lsa->getCorRadius()); |
| 121 | tlvLsa.setHyperbolicAngle(lsa->getCorTheta()); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 122 | |
| 123 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 124 | context.append(wire); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 125 | } |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 126 | context.end(); |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 130 | LsdbDatasetInterestHandler::publishNameStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 131 | ndn::mgmt::StatusDatasetContext& context) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 132 | { |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 133 | auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, std::list<NameLsa>::const_iterator>( |
| 134 | m_lsdb.getNameLsdb().cbegin(), m_lsdb.getNameLsdb().cend()); |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame^] | 135 | NLSR_LOG_DEBUG("Received interest: " << interest); |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 136 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 137 | tlv::NameLsa tlvLsa; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 138 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 139 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 140 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 141 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 142 | for (const ndn::Name& name : lsa->getNpl().getNames()) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 143 | tlvLsa.addName(name); |
| 144 | } |
| 145 | |
| 146 | const ndn::Block& wire = tlvLsa.wireEncode(); |
| 147 | context.append(wire); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 148 | } |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 149 | context.end(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | void |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 153 | LsdbDatasetInterestHandler::publishAllStatus(const ndn::Name& topPrefix, const ndn::Interest& interest, |
| 154 | ndn::mgmt::StatusDatasetContext& context) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 155 | { |
dmcoomes | 5bcb39e | 2017-10-31 15:07:55 -0500 | [diff] [blame^] | 156 | NLSR_LOG_DEBUG("Received interest: " << interest); |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 157 | tlv::LsdbStatus lsdbStatus; |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 158 | for (const tlv::AdjacencyLsa& tlvLsa : getTlvLsas<tlv::AdjacencyLsa>(m_lsdb)) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 159 | lsdbStatus.addAdjacencyLsa(tlvLsa); |
| 160 | } |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 161 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 162 | for (const tlv::CoordinateLsa& tlvLsa : getTlvLsas<tlv::CoordinateLsa>(m_lsdb)) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 163 | lsdbStatus.addCoordinateLsa(tlvLsa); |
| 164 | } |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 165 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 166 | for (const tlv::NameLsa& tlvLsa : getTlvLsas<tlv::NameLsa>(m_lsdb)) { |
laqinfan | d22da51 | 2017-05-25 17:29:53 -0500 | [diff] [blame] | 167 | lsdbStatus.addNameLsa(tlvLsa); |
| 168 | } |
| 169 | const ndn::Block& wire = lsdbStatus.wireEncode(); |
| 170 | context.append(wire); |
| 171 | context.end(); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Nick Gordon | 114537f | 2017-08-09 14:51:37 -0500 | [diff] [blame] | 174 | template<> std::list<tlv::AdjacencyLsa> |
| 175 | getTlvLsas<tlv::AdjacencyLsa>(const Lsdb& lsdb) |
| 176 | { |
| 177 | std::list<tlv::AdjacencyLsa> lsas; |
| 178 | |
| 179 | auto lsaRange = std::make_pair<std::list<AdjLsa>::const_iterator, |
| 180 | std::list<AdjLsa>::const_iterator>( |
| 181 | lsdb.getAdjLsdb().cbegin(), lsdb.getAdjLsdb().cend()); |
| 182 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
| 183 | tlv::AdjacencyLsa tlvLsa; |
| 184 | |
| 185 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
| 186 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 187 | |
| 188 | for (const Adjacent& adj : lsa->getAdl().getAdjList()) { |
| 189 | tlv::Adjacency tlvAdj; |
| 190 | tlvAdj.setName(adj.getName()); |
| 191 | tlvAdj.setUri(adj.getFaceUri().toString()); |
| 192 | tlvAdj.setCost(adj.getLinkCost()); |
| 193 | tlvLsa.addAdjacency(tlvAdj); |
| 194 | } |
| 195 | |
| 196 | lsas.push_back(tlvLsa); |
| 197 | } |
| 198 | |
| 199 | return lsas; |
| 200 | |
| 201 | } |
| 202 | |
| 203 | template<> std::list<tlv::CoordinateLsa> |
| 204 | getTlvLsas<tlv::CoordinateLsa>(const Lsdb& lsdb) |
| 205 | { |
| 206 | std::list<tlv::CoordinateLsa> lsas; |
| 207 | |
| 208 | auto lsaRange = std::make_pair<std::list<CoordinateLsa>::const_iterator, |
| 209 | std::list<CoordinateLsa>::const_iterator>( |
| 210 | lsdb.getCoordinateLsdb().cbegin(), lsdb.getCoordinateLsdb().cend()); |
| 211 | |
| 212 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
| 213 | tlv::CoordinateLsa tlvLsa; |
| 214 | |
| 215 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
| 216 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 217 | |
| 218 | tlvLsa.setHyperbolicRadius(lsa->getCorRadius()); |
| 219 | tlvLsa.setHyperbolicAngle(lsa->getCorTheta()); |
| 220 | |
| 221 | lsas.push_back(tlvLsa); |
| 222 | } |
| 223 | |
| 224 | return lsas; |
| 225 | |
| 226 | } |
| 227 | |
| 228 | template<> std::list<tlv::NameLsa> |
| 229 | getTlvLsas<tlv::NameLsa>(const Lsdb& lsdb) |
| 230 | { |
| 231 | std::list<tlv::NameLsa> lsas; |
| 232 | |
| 233 | auto lsaRange = std::make_pair<std::list<NameLsa>::const_iterator, |
| 234 | std::list<NameLsa>::const_iterator>( |
| 235 | lsdb.getNameLsdb().cbegin(), lsdb.getNameLsdb().cend()); |
| 236 | for (auto lsa = lsaRange.first; lsa != lsaRange.second; lsa++) { |
| 237 | tlv::NameLsa tlvLsa; |
| 238 | |
| 239 | std::shared_ptr<tlv::LsaInfo> tlvLsaInfo = tlv::makeLsaInfo(*lsa); |
| 240 | tlvLsa.setLsaInfo(*tlvLsaInfo); |
| 241 | |
| 242 | for (const ndn::Name& name : lsa->getNpl().getNames()) { |
| 243 | tlvLsa.addName(name); |
| 244 | } |
| 245 | |
| 246 | lsas.push_back(tlvLsa); |
| 247 | } |
| 248 | |
| 249 | return lsas; |
| 250 | |
| 251 | } |
| 252 | |
| 253 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 254 | } // namespace nlsr |