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