blob: 74e56b1bdc7496248595ac22537e0b72921b8f04 [file] [log] [blame]
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -08001/*
2 * jndn-management
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -04003 * Copyright (c) 2015-2018, Intel Corporation.
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -08004 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU Lesser General Public License,
7 * version 3, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
12 * more details.
13 */
14package com.intel.jndn.management;
15
16import com.intel.jndn.management.enums.RouteOrigin;
17import com.intel.jndn.management.helpers.FetchHelper;
18import com.intel.jndn.management.helpers.StatusDatasetHelper;
Alexander Afanasyev7ac3e392016-02-19 23:21:01 -080019import com.intel.jndn.management.types.ChannelStatus;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080020import com.intel.jndn.management.types.FaceStatus;
21import com.intel.jndn.management.types.FibEntry;
22import com.intel.jndn.management.types.ForwarderStatus;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080023import com.intel.jndn.management.types.RibEntry;
24import com.intel.jndn.management.types.StrategyChoice;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080025import net.named_data.jndn.ControlParameters;
26import net.named_data.jndn.ControlResponse;
27import net.named_data.jndn.Data;
28import net.named_data.jndn.Face;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080029import net.named_data.jndn.Interest;
30import net.named_data.jndn.KeyLocator;
31import net.named_data.jndn.Name;
Alexander Afanasyeve6037802019-06-24 16:36:12 -040032import net.named_data.jndn.RegistrationOptions;
Alexander Afanasyev3c5ae7c2016-02-19 19:33:21 -080033import net.named_data.jndn.encoding.EncodingException;
34import net.named_data.jndn.security.SecurityException;
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080035
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080036import java.io.IOException;
37import java.util.List;
38
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080039/**
40 * Helper class for interacting with an NDN forwarder daemon; see
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -040041 * <a href="https://redmine.named-data.net/projects/nfd/wiki/Management">NFD Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080042 * for explanations of the various protocols used.
43 *
44 * @author Andrew Brown <andrew.brown@intel.com>
45 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080046public final class Nfdc {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080047 private static final int OK_STATUS = 200;
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -040048 private static final int FACE_ALREADY_EXISTS = 409;
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080049
50 /////////////////////////////////////////////////////////////////////////////
51
52 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080053 * Prevent creation of Nfdc instances.
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080054 */
55 private Nfdc() {
56 }
57
58 /**
59 * Retrieve the status of the given forwarder; calls /localhost/nfd/status/general
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080060 * which requires a local Face (all non-local packets are dropped).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080061 *
62 * @param face only a localhost Face
63 * @return the forwarder status object
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080064 * @throws ManagementException if the network request failed or the returned status could not be decoded
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -040065 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus">ForwarderStatus</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080066 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080067 public static ForwarderStatus getForwarderStatus(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080068 try {
69 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/status/general"));
70 return new ForwarderStatus(StatusDatasetHelper.combine(segments));
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080071 } catch (IOException | EncodingException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080072 throw new ManagementException(e.getMessage(), e);
73 }
74 }
75
76 /**
77 * Retrieve a list of faces and their status from the given forwarder; calls
78 * /localhost/nfd/faces/list which requires a local Face (all non-local
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080079 * packets are dropped).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080080 *
81 * @param face only a localhost Face
82 * @return a list of face status objects
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080083 * @throws ManagementException if the network request failed or if the NFD rejected the request
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -040084 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt">FaceManagement</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080085 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -080086 public static List<FaceStatus> getFaceList(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -080087 try {
88 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/faces/list"));
89 return StatusDatasetHelper.wireDecode(segments, FaceStatus.class);
90 } catch (IOException e) {
91 throw new ManagementException(e.getMessage(), e);
92 }
93 }
94
95 /**
96 * Retrieve a list of FIB entries and their NextHopRecords from the given
97 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
98 * non-local packets are dropped).
99 *
100 * @param face only a localhost Face
101 * @return a list of FIB entries
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800102 * @throws ManagementException if the network request failed or if the NFD rejected the request
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400103 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset">FIB Dataset</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800104 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800105 public static List<FibEntry> getFibList(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800106 try {
107 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/fib/list"));
108 return StatusDatasetHelper.wireDecode(segments, FibEntry.class);
109 } catch (IOException e) {
110 throw new ManagementException(e.getMessage(), e);
111 }
112 }
113
114 /**
115 * Retrieve a list of routing entries from the RIB; calls
116 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
117 * are dropped).
118 *
119 * @param face only a localhost Face
120 * @return a list of RIB entries, i.e. routes
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800121 * @throws ManagementException if the network request failed or if the NFD rejected the request
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400122 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset">RIB Dataset</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800123 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800124 public static List<RibEntry> getRouteList(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800125 try {
126 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/rib/list"));
127 return StatusDatasetHelper.wireDecode(segments, RibEntry.class);
Andrew Brown862fe422016-10-07 10:02:26 -0700128 } catch (IOException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800129 throw new ManagementException(e.getMessage(), e);
130 }
131 }
132
133 /**
134 * Retrieve the list of strategy choice entries from the NFD; calls
135 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
136 * are dropped).
137 *
138 * @param face only a localhost Face
139 * @return a list of strategy choice entries, i.e. routes
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800140 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
141 * the NFD rejected the request
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400142 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">StrategyChoice</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800143 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800144 public static List<StrategyChoice> getStrategyList(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800145 try {
146 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/strategy-choice/list"));
147 return StatusDatasetHelper.wireDecode(segments, StrategyChoice.class);
148 } catch (IOException e) {
149 throw new ManagementException(e.getMessage(), e);
150 }
151 }
152
153 /**
Alexander Afanasyev7ac3e392016-02-19 23:21:01 -0800154 * Retrieve the list of channel status entries from the NFD; calls
155 * /localhost/nfd/faces/channels which requires a local Face (all non-local packets
156 * are dropped).
157 *
158 * @param face only a localhost Face
159 * @return a list of channel status entries
160 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
161 * the NFD rejected the request
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400162 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Channel-Dataset">Face Management</a>
Alexander Afanasyev7ac3e392016-02-19 23:21:01 -0800163 */
164 public static List<ChannelStatus> getChannelStatusList(final Face face) throws ManagementException {
165 try {
166 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/faces/channels"));
167 return StatusDatasetHelper.wireDecode(segments, ChannelStatus.class);
168 } catch (IOException e) {
169 throw new ManagementException(e.getMessage(), e);
170 }
171 }
Davide Pesavento63463132020-11-03 20:37:23 -0500172
Alexander Afanasyev7ac3e392016-02-19 23:21:01 -0800173 /**
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800174 * Retrieve the {@link KeyLocator} for an NFD.
175 *
176 * @param face only a localhost {@link Face}
177 * @return the {@link KeyLocator} of the NFD's key
178 * @throws ManagementException if the network request failed, if the NFD rejected the request, or no
179 * KeyLocator was found
180 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800181 public static KeyLocator getKeyLocator(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800182 try {
183 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/status/general"));
184 if (segments.isEmpty() || !KeyLocator.canGetFromSignature(segments.get(0).getSignature())) {
185 throw new ManagementException("No key locator available.");
186 }
187 return KeyLocator.getFromSignature(segments.get(0).getSignature());
188 } catch (IOException e) {
189 throw new ManagementException(e.getMessage(), e);
190 }
191 }
192
193 /**
194 * Create a new face on the given forwarder. Ensure the forwarding face is on
195 * the local machine (management requests are to /localhost/...) and that
196 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
197 *
198 * @param face only a localhost {@link Face}
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800199 * @param uri a string like "tcp4://host.name.com" (see nfd-status channels
200 * for more protocol options)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800201 * @return the newly created face ID
202 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
203 * the NFD rejected the request
204 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800205 public static int createFace(final Face face, final String uri) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800206 Name command = new Name("/localhost/nfd/faces/create");
207 ControlParameters parameters = new ControlParameters();
208 parameters.setUri(uri);
209 command.append(parameters.wireEncode());
210
211 try {
212 // send the interest
213 ControlResponse response = sendCommand(face, command);
214
215 // return
Alexander Afanasyev499aced2016-02-17 19:32:37 -0800216 return response.getBodyAsControlParameters().getFaceId();
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800217 } catch (IOException | EncodingException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800218 throw new ManagementException(e.getMessage(), e);
219 }
220 }
221
222 /**
223 * Destroy a face on given forwarder. Ensure the forwarding face is on the
224 * local machine (management requests are to /localhost/...) and that command
225 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
226 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800227 * @param face only a localhost {@link Face}
228 * @param faceId the ID of the face to destroy
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800229 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
230 * the NFD rejected the request
231 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800232 public static void destroyFace(final Face face, final int faceId) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800233 Name command = new Name("/localhost/nfd/faces/destroy");
234 ControlParameters parameters = new ControlParameters();
235 parameters.setFaceId(faceId);
236 command.append(parameters.wireEncode());
237
238 try {
239 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800240 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800241 throw new ManagementException(e.getMessage(), e);
242 }
243 }
244
245 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800246 * Register a route on the forwarder.
247 * <p/>
248 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800249 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
250 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800251 * @param face only a localhost {@link Face}
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800252 * @param controlParameters the {@link ControlParameters} command options
253 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
254 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800255 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400256 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800257 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800258 public static void register(final Face face, final ControlParameters controlParameters) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800259 // build command name
260 Name command = new Name("/localhost/nfd/rib/register");
261 command.append(controlParameters.wireEncode());
262
263 try {
264 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800265 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800266 throw new ManagementException(e.getMessage(), e);
267 }
268 }
269
270 /**
271 * Register a route on a forwarder; this will create a new face on the
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800272 * forwarder towards the face (e.g., self registration).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800273 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800274 * @param face only a localhost {@link Face}
275 * @param route the {@link Name} prefix of the route
276 * @param cost the numeric cost of forwarding along the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800277 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
278 * the NFD rejected the request
279 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800280 public static void register(final Face face, final Name route, final int cost) throws ManagementException {
Alexander Afanasyeve6037802019-06-24 16:36:12 -0400281 RegistrationOptions options = new RegistrationOptions();
282 options.setCapture(false);
283 options.setChildInherit(true);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800284
285 register(face, new ControlParameters()
286 .setName(route)
287 .setCost(cost)
288 .setOrigin(RouteOrigin.APP.toInteger())
Alexander Afanasyeve6037802019-06-24 16:36:12 -0400289 .setForwardingFlags(options));
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800290 }
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800291
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800292 /**
293 * Register a route on a forwarder; this will create a new face on the
294 * forwarder to the given URI/route pair. See register(Face,
295 * ControlParameters) for more detailed documentation.
296 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800297 * @param face only a localhost {@link Face}
298 * @param uri the URI (e.g. "tcp4://10.10.2.2:6363") of the remote node; note
299 * that this must be one of the canonical forms described in the wiki
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400300 * (https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#TCP) for NFD to
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800301 * accept the registration--otherwise you will see 400 errors
302 * @param route the {@link Name} prefix of the route
303 * @param cost the numeric cost of forwarding along the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800304 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
305 * the NFD rejected the request
306 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800307 public static void register(final Face face, final String uri, final Name route, final int cost) throws
308 ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800309 // create the new face
310 int faceId = createFace(face, uri);
311
312 // run base method
313 register(face, faceId, route, cost);
314 }
315
316 /**
317 * Register a route on a forwarder; this will not create a new face since it
318 * is provided a faceId. See register(Face, ControlParameters) for full
319 * documentation.
320 *
321 * @param forwarder only a localhost {@link Face}
322 * @param faceId the ID of the {@link Face} to assign to the route
323 * @param route the {@link Name} prefix of the route
324 * @param cost the numeric cost of forwarding along the route
325 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
326 * the NFD rejected the request
327 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800328 public static void register(final Face forwarder, final int faceId, final Name route, final int cost) throws
329 ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800330 // build command name
331 ControlParameters parameters = new ControlParameters();
332 parameters.setName(route);
333 parameters.setFaceId(faceId);
334 parameters.setCost(cost);
335 parameters.setOrigin(RouteOrigin.STATIC.toInteger());
Alexander Afanasyeve6037802019-06-24 16:36:12 -0400336 RegistrationOptions options = new RegistrationOptions();
337 options.setCapture(false);
338 options.setChildInherit(true);
339 parameters.setForwardingFlags(options);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800340
341 // run base method
342 register(forwarder, parameters);
343 }
344
345 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800346 * Unregister a route on a forwarder
347 * <p/>
348 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
349 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800350 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800351 * @param face only a localhost {@link Face}
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800352 * @param controlParameters the {@link ControlParameters} command options
353 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
354 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800355 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400356 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800357 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800358 public static void unregister(final Face face, final ControlParameters controlParameters) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800359 // build command name
360 Name command = new Name("/localhost/nfd/rib/unregister");
361 command.append(controlParameters.wireEncode());
362
363 try {
364 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800365 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800366 throw new ManagementException(e.getMessage(), e);
367 }
368 }
369
370 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800371 * Unregister a route on a forwarder.
372 * <p/>
373 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800374 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
375 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800376 * @param face only a localhost {@link Face}
377 * @param route the {@link Name} prefix of the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800378 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
379 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800380 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400381 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800382 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800383 public static void unregister(final Face face, final Name route) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800384 // build command name
385 ControlParameters controlParameters = new ControlParameters();
386 controlParameters.setName(route);
387
388 // send the interest
389 unregister(face, controlParameters);
390 }
391
392 /**
393 * Unregister a route on a forwarder; see
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800394 * <p/>
395 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800396 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
397 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800398 * @param face only a localhost {@link Face}
399 * @param route the {@link Name} prefix of the route
400 * @param faceId the specific ID of the face to remove (more than one face can
401 * be registered to a route)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800402 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
403 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800404 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400405 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800406 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800407 public static void unregister(final Face face, final Name route, final int faceId) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800408 // build command name
409 ControlParameters controlParameters = new ControlParameters();
410 controlParameters.setName(route);
411 controlParameters.setFaceId(faceId);
412
413 // send the interest
414 unregister(face, controlParameters);
415 }
416
417 /**
418 * Unregister a route on a forwarder
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800419 * <p/>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800420 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
421 * signing has been set up using forwarder.setCommandSigningInfo().
422 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800423 * @param face only a localhost {@link Face}
424 * @param route the {@link Name} prefix of the route
425 * @param uri the URI (e.g. "tcp4://some.host.com") of the remote node (more
426 * than one face can be registered to a route)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800427 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
428 * the NFD rejected the request
429 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc command-line usage</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400430 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RibMgmt</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800431 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800432 public static void unregister(final Face face, final Name route, final String uri) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800433 int faceId = -1;
434 for (FaceStatus faceStatus : getFaceList(face)) {
435 if (faceStatus.getRemoteUri().matches(uri)) {
436 faceId = faceStatus.getFaceId();
437 break;
438 }
439 }
440
441 if (faceId == -1) {
442 throw new ManagementException("Face not found: " + uri);
443 }
444
445 // send the interest
446 unregister(face, route, faceId);
447 }
448
449 /**
450 * Set a strategy on the forwarder
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800451 * <p/>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800452 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
453 * signing has been set up using forwarder.setCommandSigningInfo().
454 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800455 * @param face only a localhost {@link Face}
456 * @param prefix the {@link Name} prefix
457 * @param strategy the {@link Name} of the strategy to set, e.g.
458 * /localhost/nfd/strategy/broadcast
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800459 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
460 * the NFD rejected the request
461 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc command-line usage</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400462 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">StrategyChoice</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800463 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800464 public static void setStrategy(final Face face, final Name prefix, final Name strategy) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800465 // build command name
466 Name command = new Name("/localhost/nfd/strategy-choice/set");
467 ControlParameters parameters = new ControlParameters();
468 parameters.setName(prefix);
469 parameters.setStrategy(strategy);
470 command.append(parameters.wireEncode());
471
472 try {
473 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800474 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800475 throw new ManagementException(e.getMessage(), e);
476 }
477 }
478
479 /**
480 * Set a strategy on the forwarder; see
481 * {@link #setStrategy(net.named_data.jndn.Face, net.named_data.jndn.Name, net.named_data.jndn.Name)}
482 * for more information. Ensure the forwarding face is on the local machine
483 * (management requests are to /localhost/...) and that command signing has
484 * been set up (e.g. forwarder.setCommandSigningInfo()).
485 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800486 * @param face only a localhost {@link Face}
487 * @param prefix the {@link Name} prefix
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800488 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
489 * the NFD rejected the request
490 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800491 public static void unsetStrategy(final Face face, final Name prefix) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800492 // build command name
493 Name command = new Name("/localhost/nfd/strategy-choice/unset");
494 ControlParameters parameters = new ControlParameters();
495 parameters.setName(prefix);
496 command.append(parameters.wireEncode());
497
498 try {
499 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800500 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800501 throw new ManagementException(e.getMessage(), e);
502 }
503 }
504
505 /**
506 * Send an interest as a command to the forwarder; this method will convert
507 * the interest to a command interest and block until a response is received
508 * from the forwarder. Ensure the forwarding face is on the local machine
509 * (management requests are to /localhost/...) and that command signing has
510 * been set up (e.g. forwarder.setCommandSigningInfo()).
511 *
512 * @param face only a localhost Face, command signing info must be set
513 * @param name As described at
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400514 * <a href="https://redmine.named-data.net/projects/nfd/wiki/ControlCommand">ControlCommand</a>,
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800515 * the requested interest must have encoded ControlParameters appended to the
516 * interest name
517 * @return a {@link ControlResponse}
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800518 * @throws IOException if the network request failed
519 * @throws EncodingException if the NFD response could not be decoded
520 * @throws ManagementException if the NFD rejected the request
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800521 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800522 private static ControlResponse sendCommand(final Face face, final Name name) throws IOException, EncodingException,
523 ManagementException {
Andrew Brown5a8b7c42016-10-06 09:54:36 -0700524 if (face == null) {
525 throw new IllegalArgumentException("Face parameter is null.");
526 }
527
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800528 Interest interest = new Interest(name);
529
530 // forwarder must have command signing info set
531 try {
532 face.makeCommandInterest(interest);
Andrew Brown5a8b7c42016-10-06 09:54:36 -0700533 } catch (SecurityException e) {
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800534 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the " +
535 "face.", e);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800536 }
537
538 // send command packet
539 Data data = FetchHelper.getData(face, interest.getName());
540
541 // decode response
542 ControlResponse response = new ControlResponse();
543 response.wireDecode(data.getContent().buf());
544
545 // check response for success
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400546 if (response.getStatusCode() != OK_STATUS && response.getStatusCode() != FACE_ALREADY_EXISTS) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800547 throw ManagementException.fromResponse(response);
548 }
549
550 return response;
551 }
552}