blob: 56d98ff2199b1dc50b42b23d9b1e49f576443317 [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;
andrewsbrown8a32f302015-03-24 08:42:46 -070020import com.intel.jndn.management.types.ForwarderStatus;
andrewsbrowne8e8e852015-03-09 13:48:31 -070021import com.intel.jndn.management.types.LocalControlHeader;
Andrew Brownc46c1602015-02-18 10:45:56 -080022import com.intel.jndn.management.types.RibEntry;
andrewsbrown43b96302015-03-17 21:51:43 +010023import com.intel.jndn.utils.SimpleClient;
Andrew Brown07466442015-02-24 09:07:02 -080024import com.intel.jndn.utils.SegmentedClient;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080025import java.io.IOException;
26import java.util.List;
27import net.named_data.jndn.ControlParameters;
28import net.named_data.jndn.Data;
29import net.named_data.jndn.Face;
30import net.named_data.jndn.ForwardingFlags;
31import net.named_data.jndn.Interest;
32import net.named_data.jndn.Name;
33import net.named_data.jndn.encoding.EncodingException;
34import net.named_data.jndn.security.SecurityException;
Andrew Brownc46c1602015-02-18 10:45:56 -080035import java.util.logging.Logger;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080036
37/**
38 * Helper class for interacting with an NDN forwarder daemon; see
39 * http://redmine.named-data.net/projects/nfd/wiki/Management for explanations
40 * of the various protocols used.
41 *
42 * @author Andrew Brown <andrew.brown@intel.com>
43 */
44public class NFD {
45
Andrew Brown211d2b62015-02-18 11:12:02 -080046 public final static long DEFAULT_TIMEOUT = 2000;
andrewsbrown43b96302015-03-17 21:51:43 +010047 public final static int OK_STATUS = 200;
Andrew Brown211d2b62015-02-18 11:12:02 -080048 static private final Logger logger = Logger.getLogger(NFD.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080049
Andrew Brown211d2b62015-02-18 11:12:02 -080050 /**
51 * Ping a forwarder on an existing face to verify that the forwarder is
52 * working and responding to requests; this version sends a discovery packet
53 * to /localhost/nfd which should always respond if the requestor is on the
54 * same machine as the NDN forwarding daemon.
55 *
andrewsbrown8a32f302015-03-24 08:42:46 -070056 * @param face only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -080057 * @return true if successful, false otherwise
58 */
59 public static boolean pingLocal(Face face) {
60 return ping(face, new Name("/localhost/nfd"));
61 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080062
Andrew Brown211d2b62015-02-18 11:12:02 -080063 /**
64 * Request a name on an existing face to verify the forwarder is working and
65 * responding to requests. Note that the name must be served or cached on the
66 * forwarder for this to return true.
67 *
68 * @param face
69 * @param name
70 * @return true if successful, false otherwise
71 */
72 public static boolean ping(Face face, Name name) {
73 // build interest
74 Interest interest = new Interest(name);
75 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
76 interest.setMustBeFresh(true);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080077
Andrew Brown211d2b62015-02-18 11:12:02 -080078 // send packet
andrewsbrown43b96302015-03-17 21:51:43 +010079 try {
80 Data data = SimpleClient.getDefault().getSync(face, interest);
81 return data != null;
82 } catch (IOException e) {
83 return false;
84 }
Andrew Brown211d2b62015-02-18 11:12:02 -080085 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080086
Andrew Brown211d2b62015-02-18 11:12:02 -080087 /**
andrewsbrown8a32f302015-03-24 08:42:46 -070088 * Retrieve the status of the given forwarder; calls /localhost/nfd/status
89 * which requires a local Face (all non-local packets are dropped)
90 *
91 * @param forwarder only a localhost Face
92 * @return the forwarder status object, see
93 * http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus.
94 * @throws java.lang.Exception
95 */
96 public static ForwarderStatus getForwarderStatus(Face forwarder) throws Exception {
97 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/status"));
98 ForwarderStatus status = new ForwarderStatus();
99 status.wireDecode(data.getContent().buf());
100 return status;
101 }
102
103 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800104 * Retrieve a list of faces and their status from the given forwarder; calls
105 * /localhost/nfd/faces/list which requires a local Face (all non-local
106 * packets are dropped)
107 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700108 * @param forwarder only a localhost Face
109 * @return a list of face status objects, see
110 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt.
andrewsbrown43b96302015-03-17 21:51:43 +0100111 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800112 */
113 public static List<FaceStatus> getFaceList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100114 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/faces/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800115 return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
116 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800117
Andrew Brown211d2b62015-02-18 11:12:02 -0800118 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800119 * Retrieve a list of FIB entries and their NextHopRecords from the given
120 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
121 * non-local packets are dropped).
122 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700123 * @param forwarder only a localhost Face
124 * @return a list of FIB entries, see
125 * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100126 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800127 */
128 public static List<FibEntry> getFibList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100129 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/fib/list"));
Andrew Brown63bed362015-02-18 11:28:50 -0800130 return StatusDataset.wireDecode(data.getContent(), FibEntry.class);
131 }
132
133 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800134 * Retrieve a list of routing entries from the RIB; calls
135 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
andrewsbrown43b96302015-03-17 21:51:43 +0100136 * are dropped).
Andrew Brown211d2b62015-02-18 11:12:02 -0800137 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700138 * @param forwarder only a localhost Face
139 * @return a list of RIB entries, i.e. routes, see
140 * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100141 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800142 */
143 public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100144 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/rib/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800145 return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
146 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800147
Andrew Brown211d2b62015-02-18 11:12:02 -0800148 /**
149 * Helper method to register a new face on the forwarder; as mentioned at
150 * http://named-data.net/doc/NFD/current/manpages/nfdc.html, this is more for
151 * debugging; use 'register' instead
152 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700153 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800154 * @param faceId
155 * @param prefix
andrewsbrown43b96302015-03-17 21:51:43 +0100156 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800157 */
andrewsbrown43b96302015-03-17 21:51:43 +0100158 public static void addNextHop(Face forwarder, int faceId, Name prefix) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800159 // build command name
160 Name command = new Name("/localhost/nfd/fib/add-nexthop");
161 ControlParameters parameters = new ControlParameters();
162 parameters.setName(prefix);
163 parameters.setFaceId(faceId);
164 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800165
Andrew Brown211d2b62015-02-18 11:12:02 -0800166 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100167 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800168 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800169
Andrew Brown211d2b62015-02-18 11:12:02 -0800170 /**
171 * Create a new face on the given forwarder. Ensure the forwarding face is on
172 * the local machine (management requests are to /localhost/...) and that
173 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
174 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700175 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800176 * @param uri
177 * @return
178 * @throws java.lang.Exception
179 */
180 public static int createFace(Face forwarder, String uri) throws Exception {
181 Name command = new Name("/localhost/nfd/faces/create");
182 ControlParameters parameters = new ControlParameters();
183 parameters.setUri(uri);
184 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800185
Andrew Brown211d2b62015-02-18 11:12:02 -0800186 // send the interest
187 ControlResponse response = sendCommand(forwarder, new Interest(command));
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800188
Andrew Brown211d2b62015-02-18 11:12:02 -0800189 // return
190 return response.getBody().get(0).getFaceId();
191 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800192
Andrew Brown211d2b62015-02-18 11:12:02 -0800193 /**
andrewsbrown43b96302015-03-17 21:51:43 +0100194 * Destroy a face on given forwarder. Ensure the forwarding face is on the
195 * local machine (management requests are to /localhost/...) and that command
196 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700197 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700198 * @param forwarder only a localhost Face
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700199 * @param faceId
200 * @throws java.lang.Exception
201 */
andrewsbrown43b96302015-03-17 21:51:43 +0100202 public static void destroyFace(Face forwarder, int faceId) throws Exception {
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700203 Name command = new Name("/localhost/nfd/faces/destroy");
204 ControlParameters parameters = new ControlParameters();
205 parameters.setFaceId(faceId);
206 command.append(parameters.wireEncode());
207
208 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100209 sendCommand(forwarder, new Interest(command));
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700210 }
211
212 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700213 * Enable a local control feature on the given forwarder. See
214 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature
215 *
216 * @param forwarder
217 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700218 * @throws Exception
219 */
andrewsbrown43b96302015-03-17 21:51:43 +0100220 public static void enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700221 // build command name
222 Name command = new Name("/localhost/nfd/faces/enable-local-control");
223 ControlParameters parameters = new ControlParameters();
224 parameters.setLocalControlFeature(header.getNumericValue());
225 command.append(parameters.wireEncode());
226
227 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100228 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700229 }
230
231 /**
232 * Disable a local control feature on the given forwarder. See
233 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature
234 *
235 * @param forwarder
236 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700237 * @throws Exception
238 */
andrewsbrown43b96302015-03-17 21:51:43 +0100239 public static void disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700240 // build command name
241 Name command = new Name("/localhost/nfd/faces/disable-local-control");
242 ControlParameters parameters = new ControlParameters();
243 parameters.setLocalControlFeature(header.getNumericValue());
244 command.append(parameters.wireEncode());
245
246 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100247 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700248 }
249
250 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800251 * Register a route on the forwarder; see
252 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
253 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
254 * protocol documentation. Ensure the forwarding face is on the local machine
255 * (management requests are to /localhost/...) and that command signing has
256 * been set up (e.g. forwarder.setCommandSigningInfo()).
257 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700258 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800259 * @param controlParameters
Andrew Brown211d2b62015-02-18 11:12:02 -0800260 * @throws Exception
261 */
andrewsbrown43b96302015-03-17 21:51:43 +0100262 public static void register(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800263 // build command name
264 Name command = new Name("/localhost/nfd/rib/register");
265 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800266
Andrew Brown211d2b62015-02-18 11:12:02 -0800267 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100268 sendCommand(forwarder, new Interest(command));
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 create a new face on the
273 * forwarder to the given URI/route pair. See register(Face,
274 * ControlParameters) for more details documentation.
275 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700276 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800277 * @param uri
278 * @param cost
279 * @param route
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, String uri, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800283 // create the new face
284 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800285
Andrew Brown211d2b62015-02-18 11:12:02 -0800286 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100287 register(forwarder, faceId, route, cost);
Andrew Brown211d2b62015-02-18 11:12:02 -0800288 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800289
Andrew Brown211d2b62015-02-18 11:12:02 -0800290 /**
291 * Register a route on a forwarder; this will not create a new face since it
292 * is provided a faceId. See register(Face, ControlParameters) for full
293 * documentation
294 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700295 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800296 * @param faceId
297 * @param route
298 * @param cost
Andrew Brown211d2b62015-02-18 11:12:02 -0800299 * @throws java.lang.Exception
300 */
andrewsbrown43b96302015-03-17 21:51:43 +0100301 public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800302 // build command name
303 ControlParameters parameters = new ControlParameters();
304 parameters.setName(route);
305 parameters.setFaceId(faceId);
306 parameters.setCost(cost);
307 ForwardingFlags flags = new ForwardingFlags();
308 flags.setCapture(true);
309 flags.setChildInherit(true);
310 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800311
Andrew Brown211d2b62015-02-18 11:12:02 -0800312 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100313 register(forwarder, parameters);
Andrew Brown211d2b62015-02-18 11:12:02 -0800314 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800315
Andrew Brown211d2b62015-02-18 11:12:02 -0800316 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800317 * Unregister a route on a forwarder; see
318 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
319 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
320 * protocol documentation. Ensure the forwarding face is on the local machine
321 * (management requests are to /localhost/...) and that command signing has
322 * been set up (e.g. forwarder.setCommandSigningInfo()
323 *
324 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800325 * @param controlParameters
andrewsbrown43b96302015-03-17 21:51:43 +0100326 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800327 */
andrewsbrown43b96302015-03-17 21:51:43 +0100328 public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800329 // build command name
330 Name command = new Name("/localhost/nfd/rib/unregister");
331 command.append(controlParameters.wireEncode());
332
333 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100334 sendCommand(forwarder, new Interest(command));
Andrew Brown63bed362015-02-18 11:28:50 -0800335 }
336
337 /**
Andrew Brown07466442015-02-24 09:07:02 -0800338 * Unregister a route on a forwarder; see
339 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
340 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
341 * protocol documentation. Ensure the forwarding face is on the local machine
342 * (management requests are to /localhost/...) and that command signing has
343 * been set up (e.g. forwarder.setCommandSigningInfo()
344 *
345 * @param forwarder
346 * @param route
andrewsbrown43b96302015-03-17 21:51:43 +0100347 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800348 */
andrewsbrown43b96302015-03-17 21:51:43 +0100349 public static void unregister(Face forwarder, Name route) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800350 // build command name
351 ControlParameters controlParameters = new ControlParameters();
352 controlParameters.setName(route);
353
354 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100355 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800356 }
357
358 /**
359 * Unregister a route on a forwarder; see
360 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
361 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
362 * protocol documentation. Ensure the forwarding face is on the local machine
363 * (management requests are to /localhost/...) and that command signing has
364 * been set up (e.g. forwarder.setCommandSigningInfo()
365 *
366 * @param forwarder
367 * @param route
368 * @param faceId
andrewsbrown43b96302015-03-17 21:51:43 +0100369 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800370 */
andrewsbrown43b96302015-03-17 21:51:43 +0100371 public static void unregister(Face forwarder, Name route, int faceId) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800372 // build command name
373 ControlParameters controlParameters = new ControlParameters();
374 controlParameters.setName(route);
375 controlParameters.setFaceId(faceId);
376
377 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100378 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800379 }
380
381 /**
382 * Unregister a route on a forwarder; see
383 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
384 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
385 * protocol documentation. Ensure the forwarding face is on the local machine
386 * (management requests are to /localhost/...) and that command signing has
387 * been set up (e.g. forwarder.setCommandSigningInfo()
388 *
389 * @param forwarder
390 * @param route
391 * @param uri
andrewsbrown43b96302015-03-17 21:51:43 +0100392 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800393 */
andrewsbrown43b96302015-03-17 21:51:43 +0100394 public static void unregister(Face forwarder, Name route, String uri) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800395 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700396 for (FaceStatus face : getFaceList(forwarder)) {
397 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800398 faceId = face.getFaceId();
399 break;
400 }
401 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700402
403 if (faceId == -1) {
andrewsbrown43b96302015-03-17 21:51:43 +0100404 throw new ManagementException("Face not found: " + uri);
Andrew Brown07466442015-02-24 09:07:02 -0800405 }
406
407 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100408 unregister(forwarder, route, faceId);
Andrew Brown07466442015-02-24 09:07:02 -0800409 }
410
411 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800412 * Set a strategy on the forwarder; see
413 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
414 * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
415 * for protocol documentation. Ensure the forwarding face is on the local
416 * machine (management requests are to /localhost/...) and that command
417 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
418 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700419 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800420 * @param prefix
421 * @param strategy
Andrew Brown211d2b62015-02-18 11:12:02 -0800422 * @throws Exception
423 */
andrewsbrown43b96302015-03-17 21:51:43 +0100424 public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800425 // build command name
426 Name command = new Name("/localhost/nfd/strategy-choice/set");
427 ControlParameters parameters = new ControlParameters();
428 parameters.setName(prefix);
429 parameters.setStrategy(strategy);
430 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800431
Andrew Brown211d2b62015-02-18 11:12:02 -0800432 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100433 sendCommand(forwarder, new Interest(command));
434 }
435
436 /**
437 * Build an interest to retrieve a segmented data set from the NFD; for
438 * details on the DataSet, see
439 * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
440 *
441 * @param forwarder
442 * @param datasetName
443 * @return
444 * @throws IOException
445 * @throws ManagementException
446 */
447 public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException {
448 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
Andrew Brown0e904e12015-03-17 14:10:29 -0700449 Interest interest = new Interest(datasetName);
andrewsbrown43b96302015-03-17 21:51:43 +0100450 interest.setMustBeFresh(true);
451 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
452 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
453
454 // send packet
455 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
456
457 // check for failed request
458 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
459 throw ManagementException.fromResponse(data.getContent());
460 }
andrewsbrown8a32f302015-03-24 08:42:46 -0700461
andrewsbrown43b96302015-03-17 21:51:43 +0100462 return data;
Andrew Brown211d2b62015-02-18 11:12:02 -0800463 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800464
Andrew Brown211d2b62015-02-18 11:12:02 -0800465 /**
466 * Send an interest as a command to the forwarder; this method will convert
467 * the interest to a command interest and block until a response is received
468 * from the forwarder. Ensure the forwarding face is on the local machine
469 * (management requests are to /localhost/...) and that command signing has
470 * been set up (e.g. forwarder.setCommandSigningInfo()).
471 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700472 * @param forwarder only a localhost Face, command signing info must be set
Andrew Brown211d2b62015-02-18 11:12:02 -0800473 * @param interest As described at
474 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
475 * requested interest must have encoded ControlParameters appended to the
476 * interest name
477 * @return
Andrew Brown211d2b62015-02-18 11:12:02 -0800478 * @throws java.io.IOException
479 * @throws net.named_data.jndn.encoding.EncodingException
andrewsbrown43b96302015-03-17 21:51:43 +0100480 * @throws com.intel.jndn.management.ManagementException
Andrew Brown211d2b62015-02-18 11:12:02 -0800481 */
andrewsbrown43b96302015-03-17 21:51:43 +0100482 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException {
483 // forwarder must have command signing info set
484 try {
485 forwarder.makeCommandInterest(interest);
486 } catch (SecurityException e) {
487 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e);
488 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800489
Andrew Brown211d2b62015-02-18 11:12:02 -0800490 // send command packet
andrewsbrown43b96302015-03-17 21:51:43 +0100491 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800492
andrewsbrown43b96302015-03-17 21:51:43 +0100493 // decode response
Andrew Brown211d2b62015-02-18 11:12:02 -0800494 ControlResponse response = new ControlResponse();
495 response.wireDecode(data.getContent().buf());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800496
andrewsbrown43b96302015-03-17 21:51:43 +0100497 // check response for success
498 if (response.getStatusCode() != OK_STATUS) {
499 throw ManagementException.fromResponse(response);
Andrew Brown211d2b62015-02-18 11:12:02 -0800500 }
andrewsbrown43b96302015-03-17 21:51:43 +0100501
502 return response;
Andrew Brown211d2b62015-02-18 11:12:02 -0800503 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800504}