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 | } |
| 75 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 76 | if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 77 | commandNComps < COMMAND_SIGNED_NCOMPS) |
| 78 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 79 | NFD_LOG_DEBUG("command result: unsigned verb: " << command); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 80 | sendResponse(command, 401, "Signature required"); |
| 81 | |
| 82 | return; |
| 83 | } |
| 84 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 85 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 86 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 87 | NFD_LOG_DEBUG("command result: malformed"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 88 | sendResponse(command, 400, "Malformed command"); |
| 89 | return; |
| 90 | } |
| 91 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 92 | validate(request, |
| 93 | bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1), |
| 94 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
Steve DiBenedetto | 3fff561 | 2014-05-30 15:52:56 -0600 | [diff] [blame] | 98 | StrategyChoiceManager::listStrategies(const Interest& request) |
| 99 | { |
| 100 | m_listPublisher.publish(); |
| 101 | } |
| 102 | |
| 103 | void |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 104 | StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request) |
| 105 | { |
| 106 | static const Name::Component VERB_SET("set"); |
| 107 | static const Name::Component VERB_UNSET("unset"); |
| 108 | |
| 109 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 110 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 111 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 112 | ControlParameters parameters; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 113 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 114 | { |
| 115 | sendResponse(command, 400, "Malformed command"); |
| 116 | return; |
| 117 | } |
| 118 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 119 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 120 | ControlResponse response; |
| 121 | if (verb == VERB_SET) |
| 122 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 123 | setStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 124 | } |
| 125 | else if (verb == VERB_UNSET) |
| 126 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 127 | unsetStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 128 | } |
| 129 | else |
| 130 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 131 | NFD_LOG_DEBUG("command result: unsupported verb: " << verb); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 132 | setResponse(response, 501, "Unsupported command"); |
| 133 | } |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 134 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 135 | sendResponse(command, response); |
| 136 | } |
| 137 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 138 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 139 | StrategyChoiceManager::setStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 140 | ControlResponse& response) |
| 141 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 142 | ndn::nfd::StrategyChoiceSetCommand command; |
| 143 | |
| 144 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 145 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 146 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 147 | setResponse(response, 400, "Malformed command"); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | const Name& prefix = parameters.getName(); |
| 152 | const Name& selectedStrategy = parameters.getStrategy(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 153 | |
| 154 | if (!m_strategyChoice.hasStrategy(selectedStrategy)) |
| 155 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 156 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: " |
| 157 | << parameters.getStrategy()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 158 | setResponse(response, 504, "Unsupported strategy"); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | if (m_strategyChoice.insert(prefix, selectedStrategy)) |
| 163 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 164 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 165 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 166 | } |
| 167 | else |
| 168 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 169 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 170 | setResponse(response, 405, "Strategy not installed"); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 175 | StrategyChoiceManager::unsetStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 176 | ControlResponse& response) |
| 177 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 178 | ndn::nfd::StrategyChoiceUnsetCommand command; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 179 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 180 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 181 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 182 | static const Name ROOT_PREFIX; |
| 183 | if (parameters.hasName() && parameters.getName() == ROOT_PREFIX) |
| 184 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 185 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 186 | setResponse(response, 403, "Cannot unset root prefix strategy"); |
| 187 | } |
| 188 | else |
| 189 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 190 | NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed"); |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 191 | setResponse(response, 400, "Malformed command"); |
| 192 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 193 | return; |
| 194 | } |
| 195 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 196 | m_strategyChoice.erase(parameters.getName()); |
| 197 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 198 | NFD_LOG_DEBUG("strategy-choice result: SUCCESS"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 199 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | |
| 203 | |
| 204 | } // namespace nfd |