blob: 1ab7ae2a2346441ac1ce8f1b8c013b6704b8c82e [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;
29import net.named_data.jndn.ForwardingFlags;
30import net.named_data.jndn.Interest;
31import net.named_data.jndn.KeyLocator;
32import net.named_data.jndn.Name;
33import 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 }
172 /**
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800173 * Retrieve the {@link KeyLocator} for an NFD.
174 *
175 * @param face only a localhost {@link Face}
176 * @return the {@link KeyLocator} of the NFD's key
177 * @throws ManagementException if the network request failed, if the NFD rejected the request, or no
178 * KeyLocator was found
179 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800180 public static KeyLocator getKeyLocator(final Face face) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800181 try {
182 List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/status/general"));
183 if (segments.isEmpty() || !KeyLocator.canGetFromSignature(segments.get(0).getSignature())) {
184 throw new ManagementException("No key locator available.");
185 }
186 return KeyLocator.getFromSignature(segments.get(0).getSignature());
187 } catch (IOException e) {
188 throw new ManagementException(e.getMessage(), e);
189 }
190 }
191
192 /**
193 * Create a new face on the given forwarder. Ensure the forwarding face is on
194 * the local machine (management requests are to /localhost/...) and that
195 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
196 *
197 * @param face only a localhost {@link Face}
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800198 * @param uri a string like "tcp4://host.name.com" (see nfd-status channels
199 * for more protocol options)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800200 * @return the newly created face ID
201 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
202 * the NFD rejected the request
203 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800204 public static int createFace(final Face face, final String uri) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800205 Name command = new Name("/localhost/nfd/faces/create");
206 ControlParameters parameters = new ControlParameters();
207 parameters.setUri(uri);
208 command.append(parameters.wireEncode());
209
210 try {
211 // send the interest
212 ControlResponse response = sendCommand(face, command);
213
214 // return
Alexander Afanasyev499aced2016-02-17 19:32:37 -0800215 return response.getBodyAsControlParameters().getFaceId();
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800216 } catch (IOException | EncodingException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800217 throw new ManagementException(e.getMessage(), e);
218 }
219 }
220
221 /**
222 * Destroy a face on given forwarder. Ensure the forwarding face is on the
223 * local machine (management requests are to /localhost/...) and that command
224 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
225 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800226 * @param face only a localhost {@link Face}
227 * @param faceId the ID of the face to destroy
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800228 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
229 * the NFD rejected the request
230 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800231 public static void destroyFace(final Face face, final int faceId) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800232 Name command = new Name("/localhost/nfd/faces/destroy");
233 ControlParameters parameters = new ControlParameters();
234 parameters.setFaceId(faceId);
235 command.append(parameters.wireEncode());
236
237 try {
238 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800239 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800240 throw new ManagementException(e.getMessage(), e);
241 }
242 }
243
244 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800245 * Register a route on the forwarder.
246 * <p/>
247 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800248 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
249 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800250 * @param face only a localhost {@link Face}
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800251 * @param controlParameters the {@link ControlParameters} command options
252 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
253 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800254 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400255 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800256 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800257 public static void register(final Face face, final ControlParameters controlParameters) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800258 // build command name
259 Name command = new Name("/localhost/nfd/rib/register");
260 command.append(controlParameters.wireEncode());
261
262 try {
263 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800264 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800265 throw new ManagementException(e.getMessage(), e);
266 }
267 }
268
269 /**
270 * Register a route on a forwarder; this will create a new face on the
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800271 * forwarder towards the face (e.g., self registration).
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800272 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800273 * @param face only a localhost {@link Face}
274 * @param route the {@link Name} prefix of the route
275 * @param cost the numeric cost of forwarding along the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800276 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
277 * the NFD rejected the request
278 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800279 public static void register(final Face face, final Name route, final int cost) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800280 ForwardingFlags flags = new ForwardingFlags();
281 flags.setCapture(false);
282 flags.setChildInherit(true);
283
284 register(face, new ControlParameters()
285 .setName(route)
286 .setCost(cost)
287 .setOrigin(RouteOrigin.APP.toInteger())
288 .setForwardingFlags(flags));
289 }
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800290
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800291 /**
292 * Register a route on a forwarder; this will create a new face on the
293 * forwarder to the given URI/route pair. See register(Face,
294 * ControlParameters) for more detailed documentation.
295 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800296 * @param face only a localhost {@link Face}
297 * @param uri the URI (e.g. "tcp4://10.10.2.2:6363") of the remote node; note
298 * that this must be one of the canonical forms described in the wiki
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400299 * (https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#TCP) for NFD to
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800300 * accept the registration--otherwise you will see 400 errors
301 * @param route the {@link Name} prefix of the route
302 * @param cost the numeric cost of forwarding along the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800303 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
304 * the NFD rejected the request
305 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800306 public static void register(final Face face, final String uri, final Name route, final int cost) throws
307 ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800308 // create the new face
309 int faceId = createFace(face, uri);
310
311 // run base method
312 register(face, faceId, route, cost);
313 }
314
315 /**
316 * Register a route on a forwarder; this will not create a new face since it
317 * is provided a faceId. See register(Face, ControlParameters) for full
318 * documentation.
319 *
320 * @param forwarder only a localhost {@link Face}
321 * @param faceId the ID of the {@link Face} to assign to the route
322 * @param route the {@link Name} prefix of the route
323 * @param cost the numeric cost of forwarding along the route
324 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
325 * the NFD rejected the request
326 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800327 public static void register(final Face forwarder, final int faceId, final Name route, final int cost) throws
328 ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800329 // build command name
330 ControlParameters parameters = new ControlParameters();
331 parameters.setName(route);
332 parameters.setFaceId(faceId);
333 parameters.setCost(cost);
334 parameters.setOrigin(RouteOrigin.STATIC.toInteger());
335 ForwardingFlags flags = new ForwardingFlags();
336 flags.setCapture(false);
337 flags.setChildInherit(true);
338 parameters.setForwardingFlags(flags);
339
340 // run base method
341 register(forwarder, parameters);
342 }
343
344 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800345 * Unregister a route on a forwarder
346 * <p/>
347 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
348 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800349 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800350 * @param face only a localhost {@link Face}
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800351 * @param controlParameters the {@link ControlParameters} command options
352 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
353 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800354 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400355 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800356 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800357 public static void unregister(final Face face, final ControlParameters controlParameters) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800358 // build command name
359 Name command = new Name("/localhost/nfd/rib/unregister");
360 command.append(controlParameters.wireEncode());
361
362 try {
363 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800364 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800365 throw new ManagementException(e.getMessage(), e);
366 }
367 }
368
369 /**
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800370 * Unregister a route on a forwarder.
371 * <p/>
372 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800373 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
374 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800375 * @param face only a localhost {@link Face}
376 * @param route the {@link Name} prefix of the route
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800377 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
378 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800379 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400380 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800381 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800382 public static void unregister(final Face face, final Name route) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800383 // build command name
384 ControlParameters controlParameters = new ControlParameters();
385 controlParameters.setName(route);
386
387 // send the interest
388 unregister(face, controlParameters);
389 }
390
391 /**
392 * Unregister a route on a forwarder; see
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800393 * <p/>
394 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800395 * signing has been set up (e.g. forwarder.setCommandSigningInfo().
396 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800397 * @param face only a localhost {@link Face}
398 * @param route the {@link Name} prefix of the route
399 * @param faceId the specific ID of the face to remove (more than one face can
400 * be registered to a route)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800401 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
402 * the NFD rejected the request
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800403 * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a>
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400404 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800405 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800406 public static void unregister(final Face face, final Name route, final int faceId) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800407 // build command name
408 ControlParameters controlParameters = new ControlParameters();
409 controlParameters.setName(route);
410 controlParameters.setFaceId(faceId);
411
412 // send the interest
413 unregister(face, controlParameters);
414 }
415
416 /**
417 * Unregister a route on a forwarder
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800418 * <p/>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800419 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
420 * signing has been set up using forwarder.setCommandSigningInfo().
421 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800422 * @param face only a localhost {@link Face}
423 * @param route the {@link Name} prefix of the route
424 * @param uri the URI (e.g. "tcp4://some.host.com") of the remote node (more
425 * than one face can be registered to a route)
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800426 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
427 * the NFD rejected the request
428 * @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 -0400429 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RibMgmt</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800430 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800431 public static void unregister(final Face face, final Name route, final String uri) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800432 int faceId = -1;
433 for (FaceStatus faceStatus : getFaceList(face)) {
434 if (faceStatus.getRemoteUri().matches(uri)) {
435 faceId = faceStatus.getFaceId();
436 break;
437 }
438 }
439
440 if (faceId == -1) {
441 throw new ManagementException("Face not found: " + uri);
442 }
443
444 // send the interest
445 unregister(face, route, faceId);
446 }
447
448 /**
449 * Set a strategy on the forwarder
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800450 * <p/>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800451 * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command
452 * signing has been set up using forwarder.setCommandSigningInfo().
453 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800454 * @param face only a localhost {@link Face}
455 * @param prefix the {@link Name} prefix
456 * @param strategy the {@link Name} of the strategy to set, e.g.
457 * /localhost/nfd/strategy/broadcast
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800458 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
459 * the NFD rejected the request
460 * @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 -0400461 * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">StrategyChoice</a>
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800462 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800463 public static void setStrategy(final Face face, final Name prefix, final Name strategy) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800464 // build command name
465 Name command = new Name("/localhost/nfd/strategy-choice/set");
466 ControlParameters parameters = new ControlParameters();
467 parameters.setName(prefix);
468 parameters.setStrategy(strategy);
469 command.append(parameters.wireEncode());
470
471 try {
472 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800473 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800474 throw new ManagementException(e.getMessage(), e);
475 }
476 }
477
478 /**
479 * Set a strategy on the forwarder; see
480 * {@link #setStrategy(net.named_data.jndn.Face, net.named_data.jndn.Name, net.named_data.jndn.Name)}
481 * for more information. Ensure the forwarding face is on the local machine
482 * (management requests are to /localhost/...) and that command signing has
483 * been set up (e.g. forwarder.setCommandSigningInfo()).
484 *
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800485 * @param face only a localhost {@link Face}
486 * @param prefix the {@link Name} prefix
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800487 * @throws ManagementException if the network request failed, the NFD response could not be decoded, or
488 * the NFD rejected the request
489 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800490 public static void unsetStrategy(final Face face, final Name prefix) throws ManagementException {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800491 // build command name
492 Name command = new Name("/localhost/nfd/strategy-choice/unset");
493 ControlParameters parameters = new ControlParameters();
494 parameters.setName(prefix);
495 command.append(parameters.wireEncode());
496
497 try {
498 sendCommand(face, command);
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800499 } catch (IOException | EncodingException | ManagementException e) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800500 throw new ManagementException(e.getMessage(), e);
501 }
502 }
503
504 /**
505 * Send an interest as a command to the forwarder; this method will convert
506 * the interest to a command interest and block until a response is received
507 * from the forwarder. Ensure the forwarding face is on the local machine
508 * (management requests are to /localhost/...) and that command signing has
509 * been set up (e.g. forwarder.setCommandSigningInfo()).
510 *
511 * @param face only a localhost Face, command signing info must be set
512 * @param name As described at
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400513 * <a href="https://redmine.named-data.net/projects/nfd/wiki/ControlCommand">ControlCommand</a>,
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800514 * the requested interest must have encoded ControlParameters appended to the
515 * interest name
516 * @return a {@link ControlResponse}
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800517 * @throws IOException if the network request failed
518 * @throws EncodingException if the NFD response could not be decoded
519 * @throws ManagementException if the NFD rejected the request
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800520 */
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800521 private static ControlResponse sendCommand(final Face face, final Name name) throws IOException, EncodingException,
522 ManagementException {
Andrew Brown5a8b7c42016-10-06 09:54:36 -0700523 if (face == null) {
524 throw new IllegalArgumentException("Face parameter is null.");
525 }
526
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800527 Interest interest = new Interest(name);
528
529 // forwarder must have command signing info set
530 try {
531 face.makeCommandInterest(interest);
Andrew Brown5a8b7c42016-10-06 09:54:36 -0700532 } catch (SecurityException e) {
Alexander Afanasyeve36e1af2016-02-19 18:06:05 -0800533 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the " +
534 "face.", e);
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800535 }
536
537 // send command packet
538 Data data = FetchHelper.getData(face, interest.getName());
539
540 // decode response
541 ControlResponse response = new ControlResponse();
542 response.wireDecode(data.getContent().buf());
543
544 // check response for success
Alexander Afanasyev60f8f8e2018-07-25 13:24:19 -0400545 if (response.getStatusCode() != OK_STATUS && response.getStatusCode() != FACE_ALREADY_EXISTS) {
Alexander Afanasyeva8bc0d82016-01-25 17:25:30 -0800546 throw ManagementException.fromResponse(response);
547 }
548
549 return response;
550 }
551}