blob: 261358aff6ac327eb90e2f505f2e37980adc5564 [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070024
25#include "strategy-choice-manager.hpp"
26#include "table/strategy-choice.hpp"
Steve DiBenedettobf6a93d2014-03-21 14:03:02 -060027#include "core/logger.hpp"
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070028#include "mgmt/app-face.hpp"
29
30namespace nfd {
31
32NFD_LOG_INIT("StrategyChoiceManager");
33
34const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice";
35
36const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
37 StrategyChoiceManager::COMMAND_PREFIX.size() +
38 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060039 1; // verb parameters
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070040
41const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
42 StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070043 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070044
45StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070046 shared_ptr<InternalFace> face)
47 : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070048 , m_strategyChoice(strategyChoice)
49{
50 face->setInterestFilter("/localhost/nfd/strategy-choice",
51 bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
52}
53
54StrategyChoiceManager::~StrategyChoiceManager()
55{
56
57}
58
59void
60StrategyChoiceManager::onStrategyChoiceRequest(const Interest& request)
61{
62 const Name& command = request.getName();
63 const size_t commandNComps = command.size();
64
65 if (COMMAND_UNSIGNED_NCOMPS <= commandNComps &&
66 commandNComps < COMMAND_SIGNED_NCOMPS)
67 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070068 NFD_LOG_DEBUG("command result: unsigned verb: " << command);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070069 sendResponse(command, 401, "Signature required");
70
71 return;
72 }
73 else if (commandNComps < COMMAND_SIGNED_NCOMPS ||
74 !COMMAND_PREFIX.isPrefixOf(command))
75 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -070076 NFD_LOG_DEBUG("command result: malformed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070077 sendResponse(command, 400, "Malformed command");
78 return;
79 }
80
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070081 validate(request,
82 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
83 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070084}
85
86void
87StrategyChoiceManager::onValidatedStrategyChoiceRequest(const shared_ptr<const Interest>& request)
88{
89 static const Name::Component VERB_SET("set");
90 static const Name::Component VERB_UNSET("unset");
91
92 const Name& command = request->getName();
Steve DiBenedetto7564d972014-03-24 14:28:46 -060093 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070094
Steve DiBenedetto7564d972014-03-24 14:28:46 -060095 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060096 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070097 {
98 sendResponse(command, 400, "Malformed command");
99 return;
100 }
101
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600102 const Name::Component& verb = command[COMMAND_PREFIX.size()];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700103 ControlResponse response;
104 if (verb == VERB_SET)
105 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600106 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700107 }
108 else if (verb == VERB_UNSET)
109 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600110 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700111 }
112 else
113 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700114 NFD_LOG_DEBUG("command result: unsupported verb: " << verb);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700115 setResponse(response, 501, "Unsupported command");
116 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600117
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700118 sendResponse(command, response);
119}
120
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700121void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600122StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700123 ControlResponse& response)
124{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600125 ndn::nfd::StrategyChoiceSetCommand command;
126
127 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600128 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700129 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600130 setResponse(response, 400, "Malformed command");
131 return;
132 }
133
134 const Name& prefix = parameters.getName();
135 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700136
137 if (!m_strategyChoice.hasStrategy(selectedStrategy))
138 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700139 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unknown-strategy: "
140 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700141 setResponse(response, 504, "Unsupported strategy");
142 return;
143 }
144
145 if (m_strategyChoice.insert(prefix, selectedStrategy))
146 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700147 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600148 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700149 }
150 else
151 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700152 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700153 setResponse(response, 405, "Strategy not installed");
154 }
155}
156
157void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600158StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700159 ControlResponse& response)
160{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600161 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700162
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600163 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700164 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600165 static const Name ROOT_PREFIX;
166 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
167 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700168 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: unset-root");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600169 setResponse(response, 403, "Cannot unset root prefix strategy");
170 }
171 else
172 {
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700173 NFD_LOG_DEBUG("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600174 setResponse(response, 400, "Malformed command");
175 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700176 return;
177 }
178
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600179 m_strategyChoice.erase(parameters.getName());
180
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700181 NFD_LOG_DEBUG("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600182 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700183}
184
185
186
187} // namespace nfd