blob: 0cbfab2818efbbad51f070022763ecc22bdde2c1 [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
38 * http://redmine.named-data.net/projects/nfd/wiki/Management for explanations
39 * of the various protocols used.
40 *
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
130 * http://named-data.net/doc/NFD/current/manpages/nfdc.html, this is more for
131 * debugging; use 'register' instead
132 *
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
194 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature
195 *
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
213 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature
214 *
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
232 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
233 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
234 * protocol documentation. Ensure the forwarding face is on the local machine
235 * (management requests are to /localhost/...) and that command signing has
236 * been set up (e.g. forwarder.setCommandSigningInfo()).
237 *
238 * @param forwarder Only a localhost Face
239 * @param controlParameters
Andrew Brown211d2b62015-02-18 11:12:02 -0800240 * @throws Exception
241 */
andrewsbrown43b96302015-03-17 21:51:43 +0100242 public static void register(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800243 // build command name
244 Name command = new Name("/localhost/nfd/rib/register");
245 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800246
Andrew Brown211d2b62015-02-18 11:12:02 -0800247 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100248 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800249 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800250
Andrew Brown211d2b62015-02-18 11:12:02 -0800251 /**
252 * Register a route on a forwarder; this will create a new face on the
253 * forwarder to the given URI/route pair. See register(Face,
254 * ControlParameters) for more details documentation.
255 *
256 * @param forwarder Only a localhost Face
257 * @param uri
258 * @param cost
259 * @param route
Andrew Brown211d2b62015-02-18 11:12:02 -0800260 * @throws java.lang.Exception
261 */
andrewsbrown43b96302015-03-17 21:51:43 +0100262 public static void register(Face forwarder, String uri, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800263 // create the new face
264 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800265
Andrew Brown211d2b62015-02-18 11:12:02 -0800266 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100267 register(forwarder, faceId, route, cost);
Andrew Brown211d2b62015-02-18 11:12:02 -0800268 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800269
Andrew Brown211d2b62015-02-18 11:12:02 -0800270 /**
271 * Register a route on a forwarder; this will not create a new face since it
272 * is provided a faceId. See register(Face, ControlParameters) for full
273 * documentation
274 *
275 * @param forwarder Only a localhost Face
276 * @param faceId
277 * @param route
278 * @param cost
Andrew Brown211d2b62015-02-18 11:12:02 -0800279 * @throws java.lang.Exception
280 */
andrewsbrown43b96302015-03-17 21:51:43 +0100281 public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800282 // build command name
283 ControlParameters parameters = new ControlParameters();
284 parameters.setName(route);
285 parameters.setFaceId(faceId);
286 parameters.setCost(cost);
287 ForwardingFlags flags = new ForwardingFlags();
288 flags.setCapture(true);
289 flags.setChildInherit(true);
290 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800291
Andrew Brown211d2b62015-02-18 11:12:02 -0800292 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100293 register(forwarder, parameters);
Andrew Brown211d2b62015-02-18 11:12:02 -0800294 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800295
Andrew Brown211d2b62015-02-18 11:12:02 -0800296 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800297 * Unregister a route on a forwarder; see
298 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
299 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
300 * protocol documentation. Ensure the forwarding face is on the local machine
301 * (management requests are to /localhost/...) and that command signing has
302 * been set up (e.g. forwarder.setCommandSigningInfo()
303 *
304 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800305 * @param controlParameters
andrewsbrown43b96302015-03-17 21:51:43 +0100306 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800307 */
andrewsbrown43b96302015-03-17 21:51:43 +0100308 public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800309 // build command name
310 Name command = new Name("/localhost/nfd/rib/unregister");
311 command.append(controlParameters.wireEncode());
312
313 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100314 sendCommand(forwarder, new Interest(command));
Andrew Brown63bed362015-02-18 11:28:50 -0800315 }
316
317 /**
Andrew Brown07466442015-02-24 09:07:02 -0800318 * Unregister a route on a forwarder; see
319 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
320 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
321 * protocol documentation. Ensure the forwarding face is on the local machine
322 * (management requests are to /localhost/...) and that command signing has
323 * been set up (e.g. forwarder.setCommandSigningInfo()
324 *
325 * @param forwarder
326 * @param route
andrewsbrown43b96302015-03-17 21:51:43 +0100327 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800328 */
andrewsbrown43b96302015-03-17 21:51:43 +0100329 public static void unregister(Face forwarder, Name route) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800330 // build command name
331 ControlParameters controlParameters = new ControlParameters();
332 controlParameters.setName(route);
333
334 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100335 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800336 }
337
338 /**
339 * Unregister a route on a forwarder; see
340 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
341 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
342 * protocol documentation. Ensure the forwarding face is on the local machine
343 * (management requests are to /localhost/...) and that command signing has
344 * been set up (e.g. forwarder.setCommandSigningInfo()
345 *
346 * @param forwarder
347 * @param route
348 * @param faceId
andrewsbrown43b96302015-03-17 21:51:43 +0100349 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800350 */
andrewsbrown43b96302015-03-17 21:51:43 +0100351 public static void unregister(Face forwarder, Name route, int faceId) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800352 // build command name
353 ControlParameters controlParameters = new ControlParameters();
354 controlParameters.setName(route);
355 controlParameters.setFaceId(faceId);
356
357 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100358 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800359 }
360
361 /**
362 * Unregister a route on a forwarder; see
363 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
364 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
365 * protocol documentation. Ensure the forwarding face is on the local machine
366 * (management requests are to /localhost/...) and that command signing has
367 * been set up (e.g. forwarder.setCommandSigningInfo()
368 *
369 * @param forwarder
370 * @param route
371 * @param uri
andrewsbrown43b96302015-03-17 21:51:43 +0100372 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800373 */
andrewsbrown43b96302015-03-17 21:51:43 +0100374 public static void unregister(Face forwarder, Name route, String uri) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800375 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700376 for (FaceStatus face : getFaceList(forwarder)) {
377 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800378 faceId = face.getFaceId();
379 break;
380 }
381 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700382
383 if (faceId == -1) {
andrewsbrown43b96302015-03-17 21:51:43 +0100384 throw new ManagementException("Face not found: " + uri);
Andrew Brown07466442015-02-24 09:07:02 -0800385 }
386
387 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100388 unregister(forwarder, route, faceId);
Andrew Brown07466442015-02-24 09:07:02 -0800389 }
390
391 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800392 * Set a strategy on the forwarder; see
393 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
394 * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
395 * for protocol documentation. Ensure the forwarding face is on the local
396 * machine (management requests are to /localhost/...) and that command
397 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
398 *
399 * @param forwarder Only a localhost Face
400 * @param prefix
401 * @param strategy
Andrew Brown211d2b62015-02-18 11:12:02 -0800402 * @throws Exception
403 */
andrewsbrown43b96302015-03-17 21:51:43 +0100404 public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800405 // build command name
406 Name command = new Name("/localhost/nfd/strategy-choice/set");
407 ControlParameters parameters = new ControlParameters();
408 parameters.setName(prefix);
409 parameters.setStrategy(strategy);
410 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800411
Andrew Brown211d2b62015-02-18 11:12:02 -0800412 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100413 sendCommand(forwarder, new Interest(command));
414 }
415
416 /**
417 * Build an interest to retrieve a segmented data set from the NFD; for
418 * details on the DataSet, see
419 * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
420 *
421 * @param forwarder
422 * @param datasetName
423 * @return
424 * @throws IOException
425 * @throws ManagementException
426 */
427 public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException {
428 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
429 Interest interest = new Interest(new Name());
430 interest.setMustBeFresh(true);
431 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
432 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
433
434 // send packet
435 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
436
437 // check for failed request
438 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
439 throw ManagementException.fromResponse(data.getContent());
440 }
441
442 return data;
Andrew Brown211d2b62015-02-18 11:12:02 -0800443 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800444
Andrew Brown211d2b62015-02-18 11:12:02 -0800445 /**
446 * Send an interest as a command to the forwarder; this method will convert
447 * the interest to a command interest and block until a response is received
448 * from the forwarder. Ensure the forwarding face is on the local machine
449 * (management requests are to /localhost/...) and that command signing has
450 * been set up (e.g. forwarder.setCommandSigningInfo()).
451 *
andrewsbrown43b96302015-03-17 21:51:43 +0100452 * @param forwarder Only a localhost Face, command signing info must be set
Andrew Brown211d2b62015-02-18 11:12:02 -0800453 * @param interest As described at
454 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
455 * requested interest must have encoded ControlParameters appended to the
456 * interest name
457 * @return
Andrew Brown211d2b62015-02-18 11:12:02 -0800458 * @throws java.io.IOException
459 * @throws net.named_data.jndn.encoding.EncodingException
andrewsbrown43b96302015-03-17 21:51:43 +0100460 * @throws com.intel.jndn.management.ManagementException
Andrew Brown211d2b62015-02-18 11:12:02 -0800461 */
andrewsbrown43b96302015-03-17 21:51:43 +0100462 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException {
463 // forwarder must have command signing info set
464 try {
465 forwarder.makeCommandInterest(interest);
466 } catch (SecurityException e) {
467 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e);
468 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800469
Andrew Brown211d2b62015-02-18 11:12:02 -0800470 // send command packet
andrewsbrown43b96302015-03-17 21:51:43 +0100471 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800472
andrewsbrown43b96302015-03-17 21:51:43 +0100473 // decode response
Andrew Brown211d2b62015-02-18 11:12:02 -0800474 ControlResponse response = new ControlResponse();
475 response.wireDecode(data.getContent().buf());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800476
andrewsbrown43b96302015-03-17 21:51:43 +0100477 // check response for success
478 if (response.getStatusCode() != OK_STATUS) {
479 throw ManagementException.fromResponse(response);
Andrew Brown211d2b62015-02-18 11:12:02 -0800480 }
andrewsbrown43b96302015-03-17 21:51:43 +0100481
482 return response;
Andrew Brown211d2b62015-02-18 11:12:02 -0800483 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800484}