blob: a4c4e3836a5136122fb4d381f399c08ad29c1563 [file] [log] [blame]
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -07001/* -*- 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
11namespace nfd {
12
13NFD_LOG_INIT("StrategyChoiceManager");
14
15const Name StrategyChoiceManager::COMMAND_PREFIX = "/localhost/nfd/strategy-choice";
16
17const size_t StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS =
18 StrategyChoiceManager::COMMAND_PREFIX.size() +
19 1 + // verb
Steve DiBenedetto7564d972014-03-24 14:28:46 -060020 1; // verb parameters
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070021
22const size_t StrategyChoiceManager::COMMAND_SIGNED_NCOMPS =
23 StrategyChoiceManager::COMMAND_UNSIGNED_NCOMPS +
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070024 4; // (timestamp, nonce, signed info tlv, signature tlv)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070025
26StrategyChoiceManager::StrategyChoiceManager(StrategyChoice& strategyChoice,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070027 shared_ptr<InternalFace> face)
28 : ManagerBase(face, STRATEGY_CHOICE_PRIVILEGE)
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070029 , m_strategyChoice(strategyChoice)
30{
31 face->setInterestFilter("/localhost/nfd/strategy-choice",
32 bind(&StrategyChoiceManager::onStrategyChoiceRequest, this, _2));
33}
34
35StrategyChoiceManager::~StrategyChoiceManager()
36{
37
38}
39
40void
41StrategyChoiceManager::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 DiBenedetto2c2b8892014-02-27 11:46:48 -070062 validate(request,
63 bind(&StrategyChoiceManager::onValidatedStrategyChoiceRequest, this, _1),
64 bind(&ManagerBase::onCommandValidationFailed, this, _1, _2));
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070065}
66
67void
68StrategyChoiceManager::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 DiBenedetto7564d972014-03-24 14:28:46 -060074 const Name::Component& parameterComponent = command[COMMAND_PREFIX.size() + 1];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070075
Steve DiBenedetto7564d972014-03-24 14:28:46 -060076 ControlParameters parameters;
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060077 if (!extractParameters(parameterComponent, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070078 {
79 sendResponse(command, 400, "Malformed command");
80 return;
81 }
82
Steve DiBenedetto7564d972014-03-24 14:28:46 -060083 const Name::Component& verb = command[COMMAND_PREFIX.size()];
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070084 ControlResponse response;
85 if (verb == VERB_SET)
86 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -060087 setStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070088 }
89 else if (verb == VERB_UNSET)
90 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -060091 unsetStrategy(parameters, response);
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070092 }
93 else
94 {
95 NFD_LOG_INFO("command result: unsupported verb: " << verb);
96 setResponse(response, 501, "Unsupported command");
97 }
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060098
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -070099 sendResponse(command, response);
100}
101
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700102void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600103StrategyChoiceManager::setStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700104 ControlResponse& response)
105{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600106 ndn::nfd::StrategyChoiceSetCommand command;
107
108 if (!validateParameters(command, parameters))
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600109 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600110 NFD_LOG_INFO("strategy-choice result: FAIL reason: malformed");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600111 setResponse(response, 400, "Malformed command");
112 return;
113 }
114
115 const Name& prefix = parameters.getName();
116 const Name& selectedStrategy = parameters.getStrategy();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700117
118 if (!m_strategyChoice.hasStrategy(selectedStrategy))
119 {
120 NFD_LOG_INFO("strategy-choice result: FAIL reason: unknown-strategy: "
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600121 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700122 setResponse(response, 504, "Unsupported strategy");
123 return;
124 }
125
126 if (m_strategyChoice.insert(prefix, selectedStrategy))
127 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600128 NFD_LOG_INFO("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600129 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700130 }
131 else
132 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600133 NFD_LOG_INFO("strategy-choice result: FAIL reason: not-installed");
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700134 setResponse(response, 405, "Strategy not installed");
135 }
136}
137
138void
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600139StrategyChoiceManager::unsetStrategy(ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700140 ControlResponse& response)
141{
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600142 ndn::nfd::StrategyChoiceUnsetCommand command;
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700143
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600144 if (!validateParameters(command, parameters))
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700145 {
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600146 static const Name ROOT_PREFIX;
147 if (parameters.hasName() && parameters.getName() == ROOT_PREFIX)
148 {
149 NFD_LOG_INFO("strategy-choice result: FAIL reason: unset-root");
150 setResponse(response, 403, "Cannot unset root prefix strategy");
151 }
152 else
153 {
154 NFD_LOG_INFO("strategy-choice result: FAIL reason: malformed");
155 setResponse(response, 400, "Malformed command");
156 }
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700157 return;
158 }
159
Steve DiBenedetto51d242a2014-03-31 13:46:43 -0600160 m_strategyChoice.erase(parameters.getName());
161
162 NFD_LOG_INFO("strategy-choice result: SUCCESS");
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600163 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700164}
165
166
167
168} // namespace nfd
169
170