Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 24 | */ |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 25 | |
| 26 | #include "strategy-choice-manager.hpp" |
| 27 | #include "table/strategy-choice.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 28 | #include "core/logger.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 29 | #include "mgmt/app-face.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | |
| 33 | NFD_LOG_INIT("StrategyChoiceManager"); |
| 34 | |
| 35 | const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice"; |
| 36 | |
| 37 | const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS = |
| 38 | StrategyChoiceManager::COMMAND_PREFIX.size() + |
| 39 | 1 + // verb |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 40 | 1; // verb parameters |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 41 | |
| 42 | const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS = |
| 43 | StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 44 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 45 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 46 | const Name StrategyChoiceManager::LIST_DATASET_PREFIX("/localhost/nfd/strategy-choice/list"); |
| 47 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 48 | StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice, |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 49 | shared_ptr<InternalFace> face, |
| 50 | ndn::KeyChain& keyChain) |
| 51 | : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE, keyChain) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 52 | , m_strategyChoice(strategyChoice) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 53 | , m_listPublisher(strategyChoice, *m_face, LIST_DATASET_PREFIX, keyChain) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 54 | { |
| 55 | face->setInterestFilter("/localhost/nfd/strategy-choice", |
| 56 | bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2)); |
| 57 | } |
| 58 | |
| 59 | StrategyChoiceManager::~StrategyChoiceManager() |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request) |
| 66 | { |
| 67 | const Name& command = request.getName(); |
| 68 | const size_t commandNComps = command.size(); |
| 69 | |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 70 | if (command == LIST_DATASET_PREFIX) |
| 71 | { |
| 72 | listStrategies(request); |
| 73 | return; |
| 74 | } |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 75 | else if (commandNComps <= COMMAND_PREFIX.size()) |
| 76 | { |
| 77 | // command is too short to have a verb |
| 78 | NFD_LOG_DEBUG("command result: malformed"); |
| 79 | sendResponse(command, 400, "Malformed command"); |
| 80 | return; |
| 81 | } |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 82 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 83 | if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 84 | commandNComps < COMMAND_SIGNED_NCOMPS) |
| 85 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 86 | NFD_LOG_DEBUG("command result: unsigned verb: " << command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 87 | sendResponse(command, 401, "Signature required"); |
| 88 | |
| 89 | return; |
| 90 | } |
| 91 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 92 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 93 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 94 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 95 | sendResponse(command, 400, "Malformed command"); |
| 96 | return; |
| 97 | } |
| 98 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 99 | validate(request, |
| 100 | bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1), |
| 101 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 105 | StrategyChoiceManager::listStrategies(const Interest& request) |
| 106 | { |
| 107 | m_listPublisher.publish(); |
| 108 | } |
| 109 | |
| 110 | void |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 111 | StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request) |
| 112 | { |
| 113 | static const Name::Component VERB_SET("set"); |
| 114 | static const Name::Component VERB_UNSET("unset"); |
| 115 | |
| 116 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 117 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 118 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 119 | ControlParameters parameters; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 120 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 121 | { |
| 122 | sendResponse(command, 400, "Malformed command"); |
| 123 | return; |
| 124 | } |
| 125 | |
Steve DiBenedetto | cd4ee5f | 2014-12-08 16:09:11 -0700 | [diff] [blame] | 126 | const Name::Component& verb = command.at(COMMAND_PREFIX.size()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 127 | ControlResponse response; |
| 128 | if (verb == VERB_SET) |
| 129 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 130 | setStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 131 | } |
| 132 | else if (verb == VERB_UNSET) |
| 133 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 134 | unsetStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 135 | } |
| 136 | else |
| 137 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 138 | NFD_LOG_DEBUG("command result: unsupported verb: " << verb); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 139 | setResponse(response, 501, "Unsupported command"); |
| 140 | } |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 141 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 142 | sendResponse(command, response); |
| 143 | } |
| 144 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 145 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 146 | StrategyChoiceManager::setStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 147 | ControlResponse& response) |
| 148 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 149 | ndn::nfd::StrategyChoiceSetCommand command; |
| 150 | |
| 151 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 152 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 153 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 154 | setResponse(response, 400, "Malformed command"); |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | const Name& prefix = parameters.getName(); |
| 159 | const Name& selectedStrategy = parameters.getStrategy(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 160 | |
| 161 | if (!m_strategyChoice.hasStrategy(selectedStrategy)) |
| 162 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 163 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: " |
| 164 | << parameters.getStrategy()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 165 | setResponse(response, 504, "Unsupported strategy"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | if (m_strategyChoice.insert(prefix, selectedStrategy)) |
| 170 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 171 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
Junxiao Shi | 838c4f1 | 2014-11-03 18:55:24 -0700 | [diff] [blame] | 172 | auto currentStrategyChoice = m_strategyChoice.get(prefix); |
| 173 | BOOST_ASSERT(currentStrategyChoice.first); |
| 174 | parameters.setStrategy(currentStrategyChoice.second); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 175 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 176 | } |
| 177 | else |
| 178 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 179 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 180 | setResponse(response, 405, "Strategy not installed"); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 185 | StrategyChoiceManager::unsetStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 186 | ControlResponse& response) |
| 187 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 188 | ndn::nfd::StrategyChoiceUnsetCommand command; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 189 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 190 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 191 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 192 | static const Name ROOT_PREFIX; |
| 193 | if (parameters.hasName() && parameters.getName() == ROOT_PREFIX) |
| 194 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 195 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 196 | setResponse(response, 403, "Cannot unset root prefix strategy"); |
| 197 | } |
| 198 | else |
| 199 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 200 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 201 | setResponse(response, 400, "Malformed command"); |
| 202 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 206 | m_strategyChoice.erase(parameters.getName()); |
| 207 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 208 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 209 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | } // namespace nfd |