blob: e0b4c6ce930f2e8dbea67f5b3788cd5ab1e1d63c [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -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 DiBenedettoabe9e972014-02-20 15:37:04 -070024
25#ifndef NFD_MGMT_FACE_MANAGER_HPP
26#define NFD_MGMT_FACE_MANAGER_HPP
27
28#include "common.hpp"
29#include "face/face.hpp"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060030#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070031#include "mgmt/app-face.hpp"
32#include "mgmt/manager-base.hpp"
33#include "mgmt/config-file.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060034#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060035#include "mgmt/notification-stream.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060036#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070037
Steve DiBenedetto7564d972014-03-24 14:28:46 -060038#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070039#include <ndn-cpp-dev/management/nfd-control-response.hpp>
40
41namespace nfd {
42
43const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060044
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070045class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060046class NetworkInterfaceInfo;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060047class LocalFace;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070048
49class FaceManager : public ManagerBase
50{
51public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060052 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070053 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060054 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070055 Error(const std::string& what) : ManagerBase::Error(what) {}
56 };
57
58 /**
59 * \throws FaceManager::Error if localPort is an invalid port number
60 */
61
62 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070063 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070064
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060065 virtual
66 ~FaceManager();
67
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070068 /** \brief Subscribe to a face management section(s) for the config file
69 */
70 void
71 setConfigFile(ConfigFile& configFile);
72
73 void
74 onFaceRequest(const Interest& request);
75
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060076PUBLIC_WITH_TESTS_ELSE_PRIVATE:
77 void
78 listFaces(const Interest& request);
79
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070080PROTECTED_WITH_TESTS_ELSE_PRIVATE:
81
82 void
83 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
84
85 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060086 createFace(const Interest& request,
87 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070088
89 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060090 destroyFace(const Interest& request,
91 ControlParameters& parameters);
92
93 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060094 extractLocalControlParameters(const Interest& request,
95 ControlParameters& parameters,
96 ControlCommand& command,
97 shared_ptr<LocalFace>& outFace,
98 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -060099
100 VIRTUAL_WITH_TESTS void
101 enableLocalControl(const Interest& request,
102 ControlParameters& parambeters);
103
104 VIRTUAL_WITH_TESTS void
105 disableLocalControl(const Interest& request,
106 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700107
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600108 void
109 ignoreUnsignedVerb(const Interest& request);
110
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700111 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -0700112 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
113
114 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700115 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600116 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700117 const shared_ptr<Face>& newFace);
118
119 void
120 onConnectFailed(const Name& requestName, const std::string& reason);
121
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600122 void
123 onAddFace(shared_ptr<Face> face);
124
125 void
126 onRemoveFace(shared_ptr<Face> face);
127
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700128private:
129 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600130 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700131
132 void
133 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
134
135 void
136 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
137
138 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600139 processSectionUdp(const ConfigSection& configSection,
140 bool isDryRun,
141 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700142
143 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600144 processSectionEther(const ConfigSection& configSection,
145 bool isDryRun,
146 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700147
148 /** \brief parse a config option that can be either "yes" or "no"
149 * \throw ConfigFile::Error value is neither "yes" nor "no"
150 * \return true if "yes", false if "no"
151 */
152 bool
153 parseYesNo(const ConfigSection::const_iterator& i,
154 const std::string& optionName,
155 const std::string& sectionName);
156
157private:
158 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
159 FactoryMap m_factories;
160 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600161 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600162 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700163
164 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600165 const Interest&,
166 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700167
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600168 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
169 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700170
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600171 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700172
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600173 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
174 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700175
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700176
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600177 const SignedVerbDispatchTable m_signedVerbDispatch;
178 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
179
180 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
181
182 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600183 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700184 static const size_t COMMAND_UNSIGNED_NCOMPS;
185
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600186 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700187 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
188 static const size_t COMMAND_SIGNED_NCOMPS;
189
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600190 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
191 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
192
193 static const Name LIST_COMMAND_PREFIX;
194 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600195
196 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700197};
198
199inline bool
200FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
201 const std::string& optionName,
202 const std::string& sectionName)
203{
204 const std::string value = i->second.get_value<std::string>();
205 if (value == "yes")
206 {
207 return true;
208 }
209 else if (value == "no")
210 {
211 return false;
212 }
213
214 throw ConfigFile::Error("Invalid value for option \"" +
215 optionName + "\" in \"" +
216 sectionName + "\" section");
217
218}
219
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600220inline void
221FaceManager::ignoreUnsignedVerb(const Interest& request)
222{
223 // do nothing
224}
225
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700226} // namespace nfd
227
228#endif // NFD_MGMT_FACE_MANAGER_HPP