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