blob: 4033b670790d3e2a809eb5bee6cbdf341ddfc8bd [file] [log] [blame]
Steve DiBenedettoabe9e972014-02-20 15:37:04 -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#ifndef NFD_MGMT_FACE_MANAGER_HPP
8#define NFD_MGMT_FACE_MANAGER_HPP
9
10#include "common.hpp"
11#include "face/face.hpp"
Steve DiBenedetto7564d972014-03-24 14:28:46 -060012#include "face/local-face.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070013#include "mgmt/app-face.hpp"
14#include "mgmt/manager-base.hpp"
15#include "mgmt/config-file.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060016#include "mgmt/face-status-publisher.hpp"
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060017#include "mgmt/notification-stream.hpp"
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060018#include "fw/face-table.hpp"
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070019
Steve DiBenedetto7564d972014-03-24 14:28:46 -060020#include <ndn-cpp-dev/management/nfd-control-parameters.hpp>
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070021#include <ndn-cpp-dev/management/nfd-control-response.hpp>
22
23namespace nfd {
24
25const std::string FACE_MANAGER_PRIVILEGE = "faces";
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060026
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070027class ProtocolFactory;
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060028class NetworkInterfaceInfo;
Steve DiBenedetto7564d972014-03-24 14:28:46 -060029class LocalFace;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070030
31class FaceManager : public ManagerBase
32{
33public:
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060034 class Error : public ManagerBase::Error
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070035 {
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060036 public:
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070037 Error(const std::string& what) : ManagerBase::Error(what) {}
38 };
39
40 /**
41 * \throws FaceManager::Error if localPort is an invalid port number
42 */
43
44 FaceManager(FaceTable& faceTable,
Steve DiBenedetto2c2b8892014-02-27 11:46:48 -070045 shared_ptr<InternalFace> face);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070046
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -060047 virtual
48 ~FaceManager();
49
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070050 /** \brief Subscribe to a face management section(s) for the config file
51 */
52 void
53 setConfigFile(ConfigFile& configFile);
54
55 void
56 onFaceRequest(const Interest& request);
57
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060058PUBLIC_WITH_TESTS_ELSE_PRIVATE:
59 void
60 listFaces(const Interest& request);
61
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070062PROTECTED_WITH_TESTS_ELSE_PRIVATE:
63
64 void
65 onValidatedFaceRequest(const shared_ptr<const Interest>& request);
66
67 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060068 createFace(const Interest& request,
69 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070070
71 VIRTUAL_WITH_TESTS void
Steve DiBenedetto7564d972014-03-24 14:28:46 -060072 destroyFace(const Interest& request,
73 ControlParameters& parameters);
74
75 VIRTUAL_WITH_TESTS bool
Steve DiBenedetto51d242a2014-03-31 13:46:43 -060076 extractLocalControlParameters(const Interest& request,
77 ControlParameters& parameters,
78 ControlCommand& command,
79 shared_ptr<LocalFace>& outFace,
80 LocalControlFeature& outFeature);
Steve DiBenedetto7564d972014-03-24 14:28:46 -060081
82 VIRTUAL_WITH_TESTS void
83 enableLocalControl(const Interest& request,
84 ControlParameters& parambeters);
85
86 VIRTUAL_WITH_TESTS void
87 disableLocalControl(const Interest& request,
88 ControlParameters& parameters);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070089
Steve DiBenedettofbb40a82014-03-11 19:40:15 -060090 void
91 ignoreUnsignedVerb(const Interest& request);
92
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070093 void
Alexander Afanasyevbbe3f0c2014-03-23 11:44:01 -070094 addCreatedFaceToForwarder(const shared_ptr<Face>& newFace);
95
96 void
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070097 onCreated(const Name& requestName,
Steve DiBenedetto7564d972014-03-24 14:28:46 -060098 ControlParameters& parameters,
Steve DiBenedettoabe9e972014-02-20 15:37:04 -070099 const shared_ptr<Face>& newFace);
100
101 void
102 onConnectFailed(const Name& requestName, const std::string& reason);
103
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600104 void
105 onAddFace(shared_ptr<Face> face);
106
107 void
108 onRemoveFace(shared_ptr<Face> face);
109
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700110private:
111 void
Steve DiBenedetto1a3c6732014-03-13 06:44:05 -0600112 onConfig(const ConfigSection& configSection, bool isDryRun, const std::string& filename);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700113
114 void
115 processSectionUnix(const ConfigSection& configSection, bool isDryRun);
116
117 void
118 processSectionTcp(const ConfigSection& configSection, bool isDryRun);
119
120 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600121 processSectionUdp(const ConfigSection& configSection,
122 bool isDryRun,
123 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700124
125 void
Steve DiBenedetto4aca99c2014-03-11 11:27:54 -0600126 processSectionEther(const ConfigSection& configSection,
127 bool isDryRun,
128 const std::list<shared_ptr<NetworkInterfaceInfo> >& nicList);
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700129
130 /** \brief parse a config option that can be either "yes" or "no"
131 * \throw ConfigFile::Error value is neither "yes" nor "no"
132 * \return true if "yes", false if "no"
133 */
134 bool
135 parseYesNo(const ConfigSection::const_iterator& i,
136 const std::string& optionName,
137 const std::string& sectionName);
138
139private:
140 typedef std::map< std::string/*protocol*/, shared_ptr<ProtocolFactory> > FactoryMap;
141 FactoryMap m_factories;
142 FaceTable& m_faceTable;
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600143 FaceStatusPublisher m_statusPublisher;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600144 NotificationStream m_notificationStream;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700145
146 typedef function<void(FaceManager*,
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600147 const Interest&,
148 ControlParameters&)> SignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700149
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600150 typedef std::map<Name::Component, SignedVerbProcessor> SignedVerbDispatchTable;
151 typedef std::pair<Name::Component, SignedVerbProcessor> SignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700152
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600153 typedef function<void(FaceManager*, const Interest&)> UnsignedVerbProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700154
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600155 typedef std::map<Name::Component, UnsignedVerbProcessor> UnsignedVerbDispatchTable;
156 typedef std::pair<Name::Component, UnsignedVerbProcessor> UnsignedVerbAndProcessor;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700157
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700158
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600159 const SignedVerbDispatchTable m_signedVerbDispatch;
160 const UnsignedVerbDispatchTable m_unsignedVerbDispatch;
161
162 static const Name COMMAND_PREFIX; // /localhost/nfd/faces
163
164 // number of components in an invalid signed command (i.e. should be signed, but isn't)
Steve DiBenedetto7564d972014-03-24 14:28:46 -0600165 // (/localhost/nfd/faces + verb + parameters) = 5
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700166 static const size_t COMMAND_UNSIGNED_NCOMPS;
167
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600168 // number of components in a valid signed command.
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700169 // (see UNSIGNED_NCOMPS), 9 with signed Interest support.
170 static const size_t COMMAND_SIGNED_NCOMPS;
171
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -0600172 static const SignedVerbAndProcessor SIGNED_COMMAND_VERBS[];
173 static const UnsignedVerbAndProcessor UNSIGNED_COMMAND_VERBS[];
174
175 static const Name LIST_COMMAND_PREFIX;
176 static const size_t LIST_COMMAND_NCOMPS;
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600177
178 static const Name EVENTS_COMMAND_PREFIX;
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700179};
180
181inline bool
182FaceManager::parseYesNo(const ConfigSection::const_iterator& i,
183 const std::string& optionName,
184 const std::string& sectionName)
185{
186 const std::string value = i->second.get_value<std::string>();
187 if (value == "yes")
188 {
189 return true;
190 }
191 else if (value == "no")
192 {
193 return false;
194 }
195
196 throw ConfigFile::Error("Invalid value for option \"" +
197 optionName + "\" in \"" +
198 sectionName + "\" section");
199
200}
201
Steve DiBenedettofbb40a82014-03-11 19:40:15 -0600202inline void
203FaceManager::ignoreUnsignedVerb(const Interest& request)
204{
205 // do nothing
206}
207
Steve DiBenedettoabe9e972014-02-20 15:37:04 -0700208} // namespace nfd
209
210#endif // NFD_MGMT_FACE_MANAGER_HPP