Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "strategy-choice-manager.hpp" |
| 8 | #include "table/strategy-choice.hpp" |
Steve DiBenedetto | bf6a93d | 2014-03-21 14:03:02 -0600 | [diff] [blame] | 9 | #include "core/logger.hpp" |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 10 | #include "mgmt/app-face.hpp" |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | NFD_LOG_INIT("StrategyChoiceManager"); |
| 15 | |
| 16 | const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice"; |
| 17 | |
| 18 | const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS = |
| 19 | StrategyChoiceManager::COMMAND_PREFIX.size() + |
| 20 | 1 + // verb |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 21 | 1; // verb parameters |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 22 | |
| 23 | const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS = |
| 24 | StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS + |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 25 | 4; // (timestamp, nonce, signed info tlv, signature tlv) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 26 | |
| 27 | StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice, |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 28 | shared_ptr<InternalFace> face) |
| 29 | : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 30 | , m_strategyChoice(strategyChoice) |
| 31 | { |
| 32 | face->setInterestFilter("/localhost/nfd/strategy-choice", |
| 33 | bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2)); |
| 34 | } |
| 35 | |
| 36 | StrategyChoiceManager::~StrategyChoiceManager() |
| 37 | { |
| 38 | |
| 39 | } |
| 40 | |
| 41 | void |
| 42 | StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request) |
| 43 | { |
| 44 | const Name& command = request.getName(); |
| 45 | const size_t commandNComps = command.size(); |
| 46 | |
| 47 | if (COMMAND_UNSIGNED_NCOMPS <= commandNComps && |
| 48 | commandNComps < COMMAND_SIGNED_NCOMPS) |
| 49 | { |
| 50 | NFD_LOG_INFO("command result: unsigned verb: " << command); |
| 51 | sendResponse(command, 401, "Signature required"); |
| 52 | |
| 53 | return; |
| 54 | } |
| 55 | else if (commandNComps < COMMAND_SIGNED_NCOMPS || |
| 56 | !COMMAND_PREFIX.isPrefixOf(command)) |
| 57 | { |
| 58 | NFD_LOG_INFO("command result: malformed"); |
| 59 | sendResponse(command, 400, "Malformed command"); |
| 60 | return; |
| 61 | } |
| 62 | |
Steve DiBenedetto | 2c2b889 | 2014-02-27 11:46:48 -0700 | [diff] [blame] | 63 | validate(request, |
| 64 | bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1), |
| 65 | bind(&ManagerBase::onCommandValidationFailed, this, _1, _2)); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void |
| 69 | StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request) |
| 70 | { |
| 71 | static const Name::Component VERB_SET("set"); |
| 72 | static const Name::Component VERB_UNSET("unset"); |
| 73 | |
| 74 | const Name& command = request->getName(); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 75 | const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1]; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 76 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 77 | ControlParameters parameters; |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 78 | if (!extractParameters(parameterComponent, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 79 | { |
| 80 | sendResponse(command, 400, "Malformed command"); |
| 81 | return; |
| 82 | } |
| 83 | |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 84 | const Name::Component& verb = command[COMMAND_PREFIX.size()]; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 85 | ControlResponse response; |
| 86 | if (verb == VERB_SET) |
| 87 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 88 | setStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 89 | } |
| 90 | else if (verb == VERB_UNSET) |
| 91 | { |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 92 | unsetStrategy(parameters, response); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 93 | } |
| 94 | else |
| 95 | { |
| 96 | NFD_LOG_INFO("command result: unsupported verb: " << verb); |
| 97 | setResponse(response, 501, "Unsupported command"); |
| 98 | } |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 99 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 100 | sendResponse(command, response); |
| 101 | } |
| 102 | |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 103 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 104 | StrategyChoiceManager::setStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 105 | ControlResponse& response) |
| 106 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 107 | ndn::nfd::StrategyChoiceSetCommand command; |
| 108 | |
| 109 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 110 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 111 | NFD_LOG_INFO("strategy-choice result: FAIL reason: malformed"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 112 | setResponse(response, 400, "Malformed command"); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | const Name& prefix = parameters.getName(); |
| 117 | const Name& selectedStrategy = parameters.getStrategy(); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 118 | |
| 119 | if (!m_strategyChoice.hasStrategy(selectedStrategy)) |
| 120 | { |
| 121 | NFD_LOG_INFO("strategy-choice result: FAIL reason: unknown-strategy: " |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 122 | << parameters.getStrategy()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 123 | setResponse(response, 504, "Unsupported strategy"); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | if (m_strategyChoice.insert(prefix, selectedStrategy)) |
| 128 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 129 | NFD_LOG_INFO("strategy-choice result: SUCCESS"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 130 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 131 | } |
| 132 | else |
| 133 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 134 | NFD_LOG_INFO("strategy-choice result: FAIL reason: not-installed"); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 135 | setResponse(response, 405, "Strategy not installed"); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | void |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 140 | StrategyChoiceManager::unsetStrategy(ControlParameters& parameters, |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 141 | ControlResponse& response) |
| 142 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 143 | ndn::nfd::StrategyChoiceUnsetCommand command; |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 144 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 145 | if (!validateParameters(command, parameters)) |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 146 | { |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 147 | static const Name ROOT_PREFIX; |
| 148 | if (parameters.hasName() && parameters.getName() == ROOT_PREFIX) |
| 149 | { |
| 150 | NFD_LOG_INFO("strategy-choice result: FAIL reason: unset-root"); |
| 151 | setResponse(response, 403, "Cannot unset root prefix strategy"); |
| 152 | } |
| 153 | else |
| 154 | { |
| 155 | NFD_LOG_INFO("strategy-choice result: FAIL reason: malformed"); |
| 156 | setResponse(response, 400, "Malformed command"); |
| 157 | } |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 158 | return; |
| 159 | } |
| 160 | |
Steve DiBenedetto | 51d242a | 2014-03-31 13:46:43 -0600 | [diff] [blame] | 161 | m_strategyChoice.erase(parameters.getName()); |
| 162 | |
| 163 | NFD_LOG_INFO("strategy-choice result: SUCCESS"); |
Steve DiBenedetto | 7564d97 | 2014-03-24 14:28:46 -0600 | [diff] [blame] | 164 | setResponse(response, 200, "Success", parameters.wireEncode()); |
Steve DiBenedetto | 5330e0d | 2014-03-05 21:52:51 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | |
| 169 | } // namespace nfd |