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