blob: 73535b9536ed65d6c400785753b3ef20c6c55ecd [file] [log] [blame]
Andrew Brown2f1fdbf2015-01-21 10:52:29 -08001/*
andrewsbrown7e6b9e82015-03-03 16:11:11 -08002 * jndn-management
3 * Copyright (c) 2015, Intel Corporation.
4 *
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.
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080013 */
14package com.intel.jndn.management;
15
Andrew Brownc46c1602015-02-18 10:45:56 -080016import com.intel.jndn.management.types.StatusDataset;
17import com.intel.jndn.management.types.ControlResponse;
18import com.intel.jndn.management.types.FaceStatus;
Andrew Brown63bed362015-02-18 11:28:50 -080019import com.intel.jndn.management.types.FibEntry;
andrewsbrowne8e8e852015-03-09 13:48:31 -070020import com.intel.jndn.management.types.LocalControlHeader;
Andrew Brownc46c1602015-02-18 10:45:56 -080021import com.intel.jndn.management.types.RibEntry;
andrewsbrown43b96302015-03-17 21:51:43 +010022import com.intel.jndn.utils.SimpleClient;
Andrew Brown07466442015-02-24 09:07:02 -080023import com.intel.jndn.utils.SegmentedClient;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080024import java.io.IOException;
25import java.util.List;
26import net.named_data.jndn.ControlParameters;
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.Name;
32import net.named_data.jndn.encoding.EncodingException;
33import net.named_data.jndn.security.SecurityException;
Andrew Brownc46c1602015-02-18 10:45:56 -080034import java.util.logging.Logger;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080035
36/**
37 * Helper class for interacting with an NDN forwarder daemon; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -070038 * <a href="http://redmine.named-data.net/projects/nfd/wiki/Management">http://redmine.named-data.net/projects/nfd/wiki/Management</a>
39 * for explanations of the various protocols used.
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080040 *
41 * @author Andrew Brown <andrew.brown@intel.com>
42 */
43public class NFD {
44
Andrew Brown211d2b62015-02-18 11:12:02 -080045 public final static long DEFAULT_TIMEOUT = 2000;
andrewsbrown43b96302015-03-17 21:51:43 +010046 public final static int OK_STATUS = 200;
Andrew Brown211d2b62015-02-18 11:12:02 -080047 static private final Logger logger = Logger.getLogger(NFD.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080048
Andrew Brown211d2b62015-02-18 11:12:02 -080049 /**
50 * Ping a forwarder on an existing face to verify that the forwarder is
51 * working and responding to requests; this version sends a discovery packet
52 * to /localhost/nfd which should always respond if the requestor is on the
53 * same machine as the NDN forwarding daemon.
54 *
55 * @param face
56 * @return true if successful, false otherwise
57 */
58 public static boolean pingLocal(Face face) {
59 return ping(face, new Name("/localhost/nfd"));
60 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080061
Andrew Brown211d2b62015-02-18 11:12:02 -080062 /**
63 * Request a name on an existing face to verify the forwarder is working and
64 * responding to requests. Note that the name must be served or cached on the
65 * forwarder for this to return true.
66 *
67 * @param face
68 * @param name
69 * @return true if successful, false otherwise
70 */
71 public static boolean ping(Face face, Name name) {
72 // build interest
73 Interest interest = new Interest(name);
74 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
75 interest.setMustBeFresh(true);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080076
Andrew Brown211d2b62015-02-18 11:12:02 -080077 // send packet
andrewsbrown43b96302015-03-17 21:51:43 +010078 try {
79 Data data = SimpleClient.getDefault().getSync(face, interest);
80 return data != null;
81 } catch (IOException e) {
82 return false;
83 }
Andrew Brown211d2b62015-02-18 11:12:02 -080084 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080085
Andrew Brown211d2b62015-02-18 11:12:02 -080086 /**
87 * Retrieve a list of faces and their status from the given forwarder; calls
88 * /localhost/nfd/faces/list which requires a local Face (all non-local
89 * packets are dropped)
90 *
91 * @param forwarder Only a localhost Face
92 * @return
andrewsbrown43b96302015-03-17 21:51:43 +010093 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -080094 */
95 public static List<FaceStatus> getFaceList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +010096 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/faces/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -080097 return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
98 }
Andrew Brownc46c1602015-02-18 10:45:56 -080099
Andrew Brown211d2b62015-02-18 11:12:02 -0800100 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800101 * Retrieve a list of FIB entries and their NextHopRecords from the given
102 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
103 * non-local packets are dropped).
104 *
105 * @param forwarder Only a localhost Face
106 * @return
andrewsbrown43b96302015-03-17 21:51:43 +0100107 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800108 */
109 public static List<FibEntry> getFibList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100110 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/fib/list"));
Andrew Brown63bed362015-02-18 11:28:50 -0800111 return StatusDataset.wireDecode(data.getContent(), FibEntry.class);
112 }
113
114 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800115 * Retrieve a list of routing entries from the RIB; calls
116 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
andrewsbrown43b96302015-03-17 21:51:43 +0100117 * are dropped).
Andrew Brown211d2b62015-02-18 11:12:02 -0800118 *
119 * @param forwarder Only a localhost Face
120 * @return
andrewsbrown43b96302015-03-17 21:51:43 +0100121 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800122 */
123 public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100124 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/rib/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800125 return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
126 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800127
Andrew Brown211d2b62015-02-18 11:12:02 -0800128 /**
129 * Helper method to register a new face on the forwarder; as mentioned at
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700130 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>,
131 * this is more for debugging; use 'register' instead
Andrew Brown211d2b62015-02-18 11:12:02 -0800132 *
133 * @param forwarder Only a localhost Face
134 * @param faceId
135 * @param prefix
andrewsbrown43b96302015-03-17 21:51:43 +0100136 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800137 */
andrewsbrown43b96302015-03-17 21:51:43 +0100138 public static void addNextHop(Face forwarder, int faceId, Name prefix) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800139 // build command name
140 Name command = new Name("/localhost/nfd/fib/add-nexthop");
141 ControlParameters parameters = new ControlParameters();
142 parameters.setName(prefix);
143 parameters.setFaceId(faceId);
144 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800145
Andrew Brown211d2b62015-02-18 11:12:02 -0800146 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100147 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800148 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800149
Andrew Brown211d2b62015-02-18 11:12:02 -0800150 /**
151 * Create a new face on the given forwarder. Ensure the forwarding face is on
152 * the local machine (management requests are to /localhost/...) and that
153 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
154 *
155 * @param forwarder Only a localhost Face
156 * @param uri
157 * @return
158 * @throws java.lang.Exception
159 */
160 public static int createFace(Face forwarder, String uri) throws Exception {
161 Name command = new Name("/localhost/nfd/faces/create");
162 ControlParameters parameters = new ControlParameters();
163 parameters.setUri(uri);
164 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800165
Andrew Brown211d2b62015-02-18 11:12:02 -0800166 // send the interest
167 ControlResponse response = sendCommand(forwarder, new Interest(command));
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800168
Andrew Brown211d2b62015-02-18 11:12:02 -0800169 // return
170 return response.getBody().get(0).getFaceId();
171 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800172
Andrew Brown211d2b62015-02-18 11:12:02 -0800173 /**
andrewsbrown43b96302015-03-17 21:51:43 +0100174 * Destroy a face on given forwarder. Ensure the forwarding face is on the
175 * local machine (management requests are to /localhost/...) and that command
176 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700177 *
178 * @param forwarder Only a localhost Face
179 * @param faceId
180 * @throws java.lang.Exception
181 */
andrewsbrown43b96302015-03-17 21:51:43 +0100182 public static void destroyFace(Face forwarder, int faceId) throws Exception {
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700183 Name command = new Name("/localhost/nfd/faces/destroy");
184 ControlParameters parameters = new ControlParameters();
185 parameters.setFaceId(faceId);
186 command.append(parameters.wireEncode());
187
188 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100189 sendCommand(forwarder, new Interest(command));
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700190 }
191
192 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700193 * Enable a local control feature on the given forwarder. See
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700194 * <a href="http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature">http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature</a>
andrewsbrowne8e8e852015-03-09 13:48:31 -0700195 *
196 * @param forwarder
197 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700198 * @throws Exception
199 */
andrewsbrown43b96302015-03-17 21:51:43 +0100200 public static void enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700201 // build command name
202 Name command = new Name("/localhost/nfd/faces/enable-local-control");
203 ControlParameters parameters = new ControlParameters();
204 parameters.setLocalControlFeature(header.getNumericValue());
205 command.append(parameters.wireEncode());
206
207 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100208 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700209 }
210
211 /**
212 * Disable a local control feature on the given forwarder. See
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700213 * <a href="http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature">http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature</a>
andrewsbrowne8e8e852015-03-09 13:48:31 -0700214 *
215 * @param forwarder
216 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700217 * @throws Exception
218 */
andrewsbrown43b96302015-03-17 21:51:43 +0100219 public static void disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700220 // build command name
221 Name command = new Name("/localhost/nfd/faces/disable-local-control");
222 ControlParameters parameters = new ControlParameters();
223 parameters.setLocalControlFeature(header.getNumericValue());
224 command.append(parameters.wireEncode());
225
226 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100227 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700228 }
229
230 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800231 * Register a route on the forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700232 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
233 * for command-line usage and
234 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
235 * for protocol documentation. Ensure the forwarding face is on the local
236 * machine (management requests are to /localhost/...) and that command
237 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Andrew Brown211d2b62015-02-18 11:12:02 -0800238 *
239 * @param forwarder Only a localhost Face
240 * @param controlParameters
Andrew Brown211d2b62015-02-18 11:12:02 -0800241 * @throws Exception
242 */
andrewsbrown43b96302015-03-17 21:51:43 +0100243 public static void register(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800244 // build command name
245 Name command = new Name("/localhost/nfd/rib/register");
246 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800247
Andrew Brown211d2b62015-02-18 11:12:02 -0800248 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100249 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800250 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800251
Andrew Brown211d2b62015-02-18 11:12:02 -0800252 /**
253 * Register a route on a forwarder; this will create a new face on the
254 * forwarder to the given URI/route pair. See register(Face,
255 * ControlParameters) for more details documentation.
256 *
257 * @param forwarder Only a localhost Face
258 * @param uri
259 * @param cost
260 * @param route
Andrew Brown211d2b62015-02-18 11:12:02 -0800261 * @throws java.lang.Exception
262 */
andrewsbrown43b96302015-03-17 21:51:43 +0100263 public static void register(Face forwarder, String uri, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800264 // create the new face
265 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800266
Andrew Brown211d2b62015-02-18 11:12:02 -0800267 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100268 register(forwarder, faceId, route, cost);
Andrew Brown211d2b62015-02-18 11:12:02 -0800269 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800270
Andrew Brown211d2b62015-02-18 11:12:02 -0800271 /**
272 * Register a route on a forwarder; this will not create a new face since it
273 * is provided a faceId. See register(Face, ControlParameters) for full
274 * documentation
275 *
276 * @param forwarder Only a localhost Face
277 * @param faceId
278 * @param route
279 * @param cost
Andrew Brown211d2b62015-02-18 11:12:02 -0800280 * @throws java.lang.Exception
281 */
andrewsbrown43b96302015-03-17 21:51:43 +0100282 public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800283 // build command name
284 ControlParameters parameters = new ControlParameters();
285 parameters.setName(route);
286 parameters.setFaceId(faceId);
287 parameters.setCost(cost);
288 ForwardingFlags flags = new ForwardingFlags();
289 flags.setCapture(true);
290 flags.setChildInherit(true);
291 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800292
Andrew Brown211d2b62015-02-18 11:12:02 -0800293 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100294 register(forwarder, parameters);
Andrew Brown211d2b62015-02-18 11:12:02 -0800295 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800296
Andrew Brown211d2b62015-02-18 11:12:02 -0800297 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800298 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700299 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
300 * for command-line usage and
301 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
302 * for protocol documentation. Ensure the forwarding face is on the local
303 * machine (management requests are to /localhost/...) and that command
304 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown63bed362015-02-18 11:28:50 -0800305 *
306 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800307 * @param controlParameters
andrewsbrown43b96302015-03-17 21:51:43 +0100308 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800309 */
andrewsbrown43b96302015-03-17 21:51:43 +0100310 public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800311 // build command name
312 Name command = new Name("/localhost/nfd/rib/unregister");
313 command.append(controlParameters.wireEncode());
314
315 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100316 sendCommand(forwarder, new Interest(command));
Andrew Brown63bed362015-02-18 11:28:50 -0800317 }
318
319 /**
Andrew Brown07466442015-02-24 09:07:02 -0800320 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700321 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
322 * for command-line usage and
323 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
324 * for protocol documentation. Ensure the forwarding face is on the local
325 * machine (management requests are to /localhost/...) and that command
326 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800327 *
328 * @param forwarder
329 * @param route
andrewsbrown43b96302015-03-17 21:51:43 +0100330 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800331 */
andrewsbrown43b96302015-03-17 21:51:43 +0100332 public static void unregister(Face forwarder, Name route) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800333 // build command name
334 ControlParameters controlParameters = new ControlParameters();
335 controlParameters.setName(route);
336
337 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100338 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800339 }
340
341 /**
342 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700343 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
344 * for command-line usage and
345 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
346 * for protocol documentation. Ensure the forwarding face is on the local
347 * machine (management requests are to /localhost/...) and that command
348 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800349 *
350 * @param forwarder
351 * @param route
352 * @param faceId
andrewsbrown43b96302015-03-17 21:51:43 +0100353 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800354 */
andrewsbrown43b96302015-03-17 21:51:43 +0100355 public static void unregister(Face forwarder, Name route, int faceId) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800356 // build command name
357 ControlParameters controlParameters = new ControlParameters();
358 controlParameters.setName(route);
359 controlParameters.setFaceId(faceId);
360
361 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100362 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800363 }
364
365 /**
366 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700367 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
368 * for command-line usage and
369 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
370 * for protocol documentation. Ensure the forwarding face is on the local
371 * machine (management requests are to /localhost/...) and that command
372 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800373 *
374 * @param forwarder
375 * @param route
376 * @param uri
andrewsbrown43b96302015-03-17 21:51:43 +0100377 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800378 */
andrewsbrown43b96302015-03-17 21:51:43 +0100379 public static void unregister(Face forwarder, Name route, String uri) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800380 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700381 for (FaceStatus face : getFaceList(forwarder)) {
382 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800383 faceId = face.getFaceId();
384 break;
385 }
386 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700387
388 if (faceId == -1) {
andrewsbrown43b96302015-03-17 21:51:43 +0100389 throw new ManagementException("Face not found: " + uri);
Andrew Brown07466442015-02-24 09:07:02 -0800390 }
391
392 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100393 unregister(forwarder, route, faceId);
Andrew Brown07466442015-02-24 09:07:02 -0800394 }
395
396 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800397 * Set a strategy on the forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700398 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
399 * for command-line usage and
400 * <a href="http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice</a>
Andrew Brown211d2b62015-02-18 11:12:02 -0800401 * for protocol documentation. Ensure the forwarding face is on the local
402 * machine (management requests are to /localhost/...) and that command
403 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
404 *
405 * @param forwarder Only a localhost Face
406 * @param prefix
407 * @param strategy
Andrew Brown211d2b62015-02-18 11:12:02 -0800408 * @throws Exception
409 */
andrewsbrown43b96302015-03-17 21:51:43 +0100410 public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800411 // build command name
412 Name command = new Name("/localhost/nfd/strategy-choice/set");
413 ControlParameters parameters = new ControlParameters();
414 parameters.setName(prefix);
415 parameters.setStrategy(strategy);
416 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800417
Andrew Brown211d2b62015-02-18 11:12:02 -0800418 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100419 sendCommand(forwarder, new Interest(command));
420 }
421
422 /**
423 * Build an interest to retrieve a segmented data set from the NFD; for
424 * details on the DataSet, see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700425 * <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a>
andrewsbrown43b96302015-03-17 21:51:43 +0100426 *
427 * @param forwarder
428 * @param datasetName
429 * @return
430 * @throws IOException
431 * @throws ManagementException
432 */
433 public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException {
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700434 // build management Interest packet; see <a href="http://redmine.named-data.net/projects/nfd/wiki/StatusDataset">http://redmine.named-data.net/projects/nfd/wiki/StatusDataset</a>
Andrew Brown0e904e12015-03-17 14:10:29 -0700435 Interest interest = new Interest(datasetName);
andrewsbrown43b96302015-03-17 21:51:43 +0100436 interest.setMustBeFresh(true);
437 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
438 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
439
440 // send packet
441 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
442
443 // check for failed request
444 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
445 throw ManagementException.fromResponse(data.getContent());
446 }
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700447
andrewsbrown43b96302015-03-17 21:51:43 +0100448 return data;
Andrew Brown211d2b62015-02-18 11:12:02 -0800449 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800450
Andrew Brown211d2b62015-02-18 11:12:02 -0800451 /**
452 * Send an interest as a command to the forwarder; this method will convert
453 * the interest to a command interest and block until a response is received
454 * from the forwarder. Ensure the forwarding face is on the local machine
455 * (management requests are to /localhost/...) and that command signing has
456 * been set up (e.g. forwarder.setCommandSigningInfo()).
457 *
andrewsbrown43b96302015-03-17 21:51:43 +0100458 * @param forwarder Only a localhost Face, command signing info must be set
Andrew Brown211d2b62015-02-18 11:12:02 -0800459 * @param interest As described at
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700460 * <a href="http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,">http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,</a>
461 * the requested interest must have encoded ControlParameters appended to the
Andrew Brown211d2b62015-02-18 11:12:02 -0800462 * interest name
463 * @return
Andrew Brown211d2b62015-02-18 11:12:02 -0800464 * @throws java.io.IOException
465 * @throws net.named_data.jndn.encoding.EncodingException
andrewsbrown43b96302015-03-17 21:51:43 +0100466 * @throws com.intel.jndn.management.ManagementException
Andrew Brown211d2b62015-02-18 11:12:02 -0800467 */
andrewsbrown43b96302015-03-17 21:51:43 +0100468 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException {
469 // forwarder must have command signing info set
470 try {
471 forwarder.makeCommandInterest(interest);
472 } catch (SecurityException e) {
473 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e);
474 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800475
Andrew Brown211d2b62015-02-18 11:12:02 -0800476 // send command packet
andrewsbrown43b96302015-03-17 21:51:43 +0100477 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800478
andrewsbrown43b96302015-03-17 21:51:43 +0100479 // decode response
Andrew Brown211d2b62015-02-18 11:12:02 -0800480 ControlResponse response = new ControlResponse();
481 response.wireDecode(data.getContent().buf());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800482
andrewsbrown43b96302015-03-17 21:51:43 +0100483 // check response for success
484 if (response.getStatusCode() != OK_STATUS) {
485 throw ManagementException.fromResponse(response);
Andrew Brown211d2b62015-02-18 11:12:02 -0800486 }
andrewsbrown43b96302015-03-17 21:51:43 +0100487
488 return response;
Andrew Brown211d2b62015-02-18 11:12:02 -0800489 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800490}