blob: bbedd34952b7c690c1ca3d831ce740f7900c9cf7 [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;
77 if (!extractParameters(parameterComponent, parameters) || !parameters.hasName())
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 }
98 sendResponse(command, response);
99}
100
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700101void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600102StrategyChoiceManager::setStrategy(const ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700103 ControlResponse& response)
104{
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600105 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 DiBenedetto5330e0d2014-03-05 21:52:51 -0700113
114 if (!m_strategyChoice.hasStrategy(selectedStrategy))
115 {
116 NFD_LOG_INFO("strategy-choice result: FAIL reason: unknown-strategy: "
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600117 << parameters.getStrategy());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700118 setResponse(response, 504, "Unsupported strategy");
119 return;
120 }
121
122 if (m_strategyChoice.insert(prefix, selectedStrategy))
123 {
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600124 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700125 }
126 else
127 {
128 setResponse(response, 405, "Strategy not installed");
129 }
130}
131
132void
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600133StrategyChoiceManager::unsetStrategy(const ControlParameters& parameters,
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700134 ControlResponse& response)
135{
136 static const Name ROOT_PREFIX;
137
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600138 const Name& prefix = parameters.getName();
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700139 if (prefix == ROOT_PREFIX)
140 {
141 NFD_LOG_INFO("strategy-choice result: FAIL reason: unknown-prefix: "
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600142 << parameters.getName());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700143 setResponse(response, 403, "Cannot unset root prefix strategy");
144 return;
145 }
146
147 m_strategyChoice.erase(prefix);
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600148 setResponse(response, 200, "Success", parameters.wireEncode());
Steve DiBenedetto5330e0d2014-03-05 21:52:51 -0700149}
150
151
152
153} // namespace nfd
154
155