blob: e3cc825d6c934398a73a3fa0b048ace13b2b46a9 [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
Andrew Browndfa8fc12015-03-25 10:46:47 -070093 * <a href="http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus">
94 * http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus</a>.
andrewsbrown8a32f302015-03-24 08:42:46 -070095 * @throws java.lang.Exception
96 */
97 public static ForwarderStatus getForwarderStatus(Face forwarder) throws Exception {
Andrew Browndfa8fc12015-03-25 10:46:47 -070098 Interest interest = new Interest(new Name("/localhost/nfd/status"));
99 interest.setMustBeFresh(true);
100 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
101 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
102
103 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
andrewsbrown8a32f302015-03-24 08:42:46 -0700104 ForwarderStatus status = new ForwarderStatus();
105 status.wireDecode(data.getContent().buf());
106 return status;
107 }
108
109 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800110 * Retrieve a list of faces and their status from the given forwarder; calls
111 * /localhost/nfd/faces/list which requires a local Face (all non-local
112 * packets are dropped)
113 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700114 * @param forwarder only a localhost Face
115 * @return a list of face status objects, see
116 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt.
andrewsbrown43b96302015-03-17 21:51:43 +0100117 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800118 */
119 public static List<FaceStatus> getFaceList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100120 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/faces/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800121 return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
122 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800123
Andrew Brown211d2b62015-02-18 11:12:02 -0800124 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800125 * Retrieve a list of FIB entries and their NextHopRecords from the given
126 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
127 * non-local packets are dropped).
128 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700129 * @param forwarder only a localhost Face
130 * @return a list of FIB entries, see
131 * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100132 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800133 */
134 public static List<FibEntry> getFibList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100135 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/fib/list"));
Andrew Brown63bed362015-02-18 11:28:50 -0800136 return StatusDataset.wireDecode(data.getContent(), FibEntry.class);
137 }
138
139 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800140 * Retrieve a list of routing entries from the RIB; calls
141 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
andrewsbrown43b96302015-03-17 21:51:43 +0100142 * are dropped).
Andrew Brown211d2b62015-02-18 11:12:02 -0800143 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700144 * @param forwarder only a localhost Face
145 * @return a list of RIB entries, i.e. routes, see
146 * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100147 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800148 */
149 public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100150 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/rib/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800151 return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
152 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800153
Andrew Brown211d2b62015-02-18 11:12:02 -0800154 /**
155 * Helper method to register a new face on the forwarder; as mentioned at
156 * http://named-data.net/doc/NFD/current/manpages/nfdc.html, this is more for
157 * debugging; use 'register' instead
158 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700159 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800160 * @param faceId
161 * @param prefix
andrewsbrown43b96302015-03-17 21:51:43 +0100162 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800163 */
andrewsbrown43b96302015-03-17 21:51:43 +0100164 public static void addNextHop(Face forwarder, int faceId, Name prefix) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800165 // build command name
166 Name command = new Name("/localhost/nfd/fib/add-nexthop");
167 ControlParameters parameters = new ControlParameters();
168 parameters.setName(prefix);
169 parameters.setFaceId(faceId);
170 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800171
Andrew Brown211d2b62015-02-18 11:12:02 -0800172 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100173 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800174 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800175
Andrew Brown211d2b62015-02-18 11:12:02 -0800176 /**
177 * Create a new face on the given forwarder. Ensure the forwarding face is on
178 * the local machine (management requests are to /localhost/...) and that
179 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
180 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700181 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800182 * @param uri
183 * @return
184 * @throws java.lang.Exception
185 */
186 public static int createFace(Face forwarder, String uri) throws Exception {
187 Name command = new Name("/localhost/nfd/faces/create");
188 ControlParameters parameters = new ControlParameters();
189 parameters.setUri(uri);
190 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800191
Andrew Brown211d2b62015-02-18 11:12:02 -0800192 // send the interest
193 ControlResponse response = sendCommand(forwarder, new Interest(command));
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800194
Andrew Brown211d2b62015-02-18 11:12:02 -0800195 // return
196 return response.getBody().get(0).getFaceId();
197 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800198
Andrew Brown211d2b62015-02-18 11:12:02 -0800199 /**
andrewsbrown43b96302015-03-17 21:51:43 +0100200 * Destroy a face on given forwarder. Ensure the forwarding face is on the
201 * local machine (management requests are to /localhost/...) and that command
202 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700203 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700204 * @param forwarder only a localhost Face
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700205 * @param faceId
206 * @throws java.lang.Exception
207 */
andrewsbrown43b96302015-03-17 21:51:43 +0100208 public static void destroyFace(Face forwarder, int faceId) throws Exception {
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700209 Name command = new Name("/localhost/nfd/faces/destroy");
210 ControlParameters parameters = new ControlParameters();
211 parameters.setFaceId(faceId);
212 command.append(parameters.wireEncode());
213
214 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100215 sendCommand(forwarder, new Interest(command));
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700216 }
217
218 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700219 * Enable a local control feature on the given forwarder. See
220 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature
221 *
222 * @param forwarder
223 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700224 * @throws Exception
225 */
andrewsbrown43b96302015-03-17 21:51:43 +0100226 public static void enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700227 // build command name
228 Name command = new Name("/localhost/nfd/faces/enable-local-control");
229 ControlParameters parameters = new ControlParameters();
230 parameters.setLocalControlFeature(header.getNumericValue());
231 command.append(parameters.wireEncode());
232
233 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100234 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700235 }
236
237 /**
238 * Disable a local control feature on the given forwarder. See
239 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature
240 *
241 * @param forwarder
242 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700243 * @throws Exception
244 */
andrewsbrown43b96302015-03-17 21:51:43 +0100245 public static void disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700246 // build command name
247 Name command = new Name("/localhost/nfd/faces/disable-local-control");
248 ControlParameters parameters = new ControlParameters();
249 parameters.setLocalControlFeature(header.getNumericValue());
250 command.append(parameters.wireEncode());
251
252 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100253 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700254 }
255
256 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800257 * Register a route on the forwarder; see
258 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
259 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
260 * protocol documentation. Ensure the forwarding face is on the local machine
261 * (management requests are to /localhost/...) and that command signing has
262 * been set up (e.g. forwarder.setCommandSigningInfo()).
263 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700264 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800265 * @param controlParameters
Andrew Brown211d2b62015-02-18 11:12:02 -0800266 * @throws Exception
267 */
andrewsbrown43b96302015-03-17 21:51:43 +0100268 public static void register(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800269 // build command name
270 Name command = new Name("/localhost/nfd/rib/register");
271 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800272
Andrew Brown211d2b62015-02-18 11:12:02 -0800273 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100274 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800275 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800276
Andrew Brown211d2b62015-02-18 11:12:02 -0800277 /**
278 * Register a route on a forwarder; this will create a new face on the
279 * forwarder to the given URI/route pair. See register(Face,
280 * ControlParameters) for more details documentation.
281 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700282 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800283 * @param uri
284 * @param cost
285 * @param route
Andrew Brown211d2b62015-02-18 11:12:02 -0800286 * @throws java.lang.Exception
287 */
andrewsbrown43b96302015-03-17 21:51:43 +0100288 public static void register(Face forwarder, String uri, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800289 // create the new face
290 int faceId = createFace(forwarder, uri);
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, faceId, route, cost);
Andrew Brown211d2b62015-02-18 11:12:02 -0800294 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800295
Andrew Brown211d2b62015-02-18 11:12:02 -0800296 /**
297 * Register a route on a forwarder; this will not create a new face since it
298 * is provided a faceId. See register(Face, ControlParameters) for full
299 * documentation
300 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700301 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800302 * @param faceId
303 * @param route
304 * @param cost
Andrew Brown211d2b62015-02-18 11:12:02 -0800305 * @throws java.lang.Exception
306 */
andrewsbrown43b96302015-03-17 21:51:43 +0100307 public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800308 // build command name
309 ControlParameters parameters = new ControlParameters();
310 parameters.setName(route);
311 parameters.setFaceId(faceId);
312 parameters.setCost(cost);
313 ForwardingFlags flags = new ForwardingFlags();
314 flags.setCapture(true);
315 flags.setChildInherit(true);
316 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800317
Andrew Brown211d2b62015-02-18 11:12:02 -0800318 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100319 register(forwarder, parameters);
Andrew Brown211d2b62015-02-18 11:12:02 -0800320 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800321
Andrew Brown211d2b62015-02-18 11:12:02 -0800322 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800323 * Unregister a route on a forwarder; see
324 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
325 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
326 * protocol documentation. Ensure the forwarding face is on the local machine
327 * (management requests are to /localhost/...) and that command signing has
328 * been set up (e.g. forwarder.setCommandSigningInfo()
329 *
330 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800331 * @param controlParameters
andrewsbrown43b96302015-03-17 21:51:43 +0100332 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800333 */
andrewsbrown43b96302015-03-17 21:51:43 +0100334 public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800335 // build command name
336 Name command = new Name("/localhost/nfd/rib/unregister");
337 command.append(controlParameters.wireEncode());
338
339 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100340 sendCommand(forwarder, new Interest(command));
Andrew Brown63bed362015-02-18 11:28:50 -0800341 }
342
343 /**
Andrew Brown07466442015-02-24 09:07:02 -0800344 * Unregister a route on a forwarder; see
345 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
346 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
347 * protocol documentation. Ensure the forwarding face is on the local machine
348 * (management requests are to /localhost/...) and that command signing has
349 * been set up (e.g. forwarder.setCommandSigningInfo()
350 *
351 * @param forwarder
352 * @param route
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) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800356 // build command name
357 ControlParameters controlParameters = new ControlParameters();
358 controlParameters.setName(route);
359
360 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100361 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800362 }
363
364 /**
365 * Unregister a route on a forwarder; see
366 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
367 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
368 * protocol documentation. Ensure the forwarding face is on the local machine
369 * (management requests are to /localhost/...) and that command signing has
370 * been set up (e.g. forwarder.setCommandSigningInfo()
371 *
372 * @param forwarder
373 * @param route
374 * @param faceId
andrewsbrown43b96302015-03-17 21:51:43 +0100375 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800376 */
andrewsbrown43b96302015-03-17 21:51:43 +0100377 public static void unregister(Face forwarder, Name route, int faceId) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800378 // build command name
379 ControlParameters controlParameters = new ControlParameters();
380 controlParameters.setName(route);
381 controlParameters.setFaceId(faceId);
382
383 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100384 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800385 }
386
387 /**
388 * Unregister a route on a forwarder; see
389 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
390 * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for
391 * protocol documentation. Ensure the forwarding face is on the local machine
392 * (management requests are to /localhost/...) and that command signing has
393 * been set up (e.g. forwarder.setCommandSigningInfo()
394 *
395 * @param forwarder
396 * @param route
397 * @param uri
andrewsbrown43b96302015-03-17 21:51:43 +0100398 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800399 */
andrewsbrown43b96302015-03-17 21:51:43 +0100400 public static void unregister(Face forwarder, Name route, String uri) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800401 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700402 for (FaceStatus face : getFaceList(forwarder)) {
403 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800404 faceId = face.getFaceId();
405 break;
406 }
407 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700408
409 if (faceId == -1) {
andrewsbrown43b96302015-03-17 21:51:43 +0100410 throw new ManagementException("Face not found: " + uri);
Andrew Brown07466442015-02-24 09:07:02 -0800411 }
412
413 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100414 unregister(forwarder, route, faceId);
Andrew Brown07466442015-02-24 09:07:02 -0800415 }
416
417 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800418 * Set a strategy on the forwarder; see
419 * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line
420 * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice
421 * for protocol documentation. Ensure the forwarding face is on the local
422 * machine (management requests are to /localhost/...) and that command
423 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
424 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700425 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800426 * @param prefix
427 * @param strategy
Andrew Brown211d2b62015-02-18 11:12:02 -0800428 * @throws Exception
429 */
andrewsbrown43b96302015-03-17 21:51:43 +0100430 public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800431 // build command name
432 Name command = new Name("/localhost/nfd/strategy-choice/set");
433 ControlParameters parameters = new ControlParameters();
434 parameters.setName(prefix);
435 parameters.setStrategy(strategy);
436 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800437
Andrew Brown211d2b62015-02-18 11:12:02 -0800438 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100439 sendCommand(forwarder, new Interest(command));
440 }
441
442 /**
443 * Build an interest to retrieve a segmented data set from the NFD; for
444 * details on the DataSet, see
445 * http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
446 *
447 * @param forwarder
448 * @param datasetName
449 * @return
450 * @throws IOException
451 * @throws ManagementException
452 */
453 public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException {
454 // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset
Andrew Brown0e904e12015-03-17 14:10:29 -0700455 Interest interest = new Interest(datasetName);
andrewsbrown43b96302015-03-17 21:51:43 +0100456 interest.setMustBeFresh(true);
457 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
458 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
459
460 // send packet
461 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
462
463 // check for failed request
464 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
465 throw ManagementException.fromResponse(data.getContent());
466 }
andrewsbrown8a32f302015-03-24 08:42:46 -0700467
andrewsbrown43b96302015-03-17 21:51:43 +0100468 return data;
Andrew Brown211d2b62015-02-18 11:12:02 -0800469 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800470
Andrew Brown211d2b62015-02-18 11:12:02 -0800471 /**
472 * Send an interest as a command to the forwarder; this method will convert
473 * the interest to a command interest and block until a response is received
474 * from the forwarder. Ensure the forwarding face is on the local machine
475 * (management requests are to /localhost/...) and that command signing has
476 * been set up (e.g. forwarder.setCommandSigningInfo()).
477 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700478 * @param forwarder only a localhost Face, command signing info must be set
Andrew Brown211d2b62015-02-18 11:12:02 -0800479 * @param interest As described at
480 * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the
481 * requested interest must have encoded ControlParameters appended to the
482 * interest name
483 * @return
Andrew Brown211d2b62015-02-18 11:12:02 -0800484 * @throws java.io.IOException
485 * @throws net.named_data.jndn.encoding.EncodingException
andrewsbrown43b96302015-03-17 21:51:43 +0100486 * @throws com.intel.jndn.management.ManagementException
Andrew Brown211d2b62015-02-18 11:12:02 -0800487 */
andrewsbrown43b96302015-03-17 21:51:43 +0100488 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException {
489 // forwarder must have command signing info set
490 try {
491 forwarder.makeCommandInterest(interest);
492 } catch (SecurityException e) {
493 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e);
494 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800495
Andrew Brown211d2b62015-02-18 11:12:02 -0800496 // send command packet
andrewsbrown43b96302015-03-17 21:51:43 +0100497 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800498
andrewsbrown43b96302015-03-17 21:51:43 +0100499 // decode response
Andrew Brown211d2b62015-02-18 11:12:02 -0800500 ControlResponse response = new ControlResponse();
501 response.wireDecode(data.getContent().buf());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800502
andrewsbrown43b96302015-03-17 21:51:43 +0100503 // check response for success
504 if (response.getStatusCode() != OK_STATUS) {
505 throw ManagementException.fromResponse(response);
Andrew Brown211d2b62015-02-18 11:12:02 -0800506 }
andrewsbrown43b96302015-03-17 21:51:43 +0100507
508 return response;
Andrew Brown211d2b62015-02-18 11:12:02 -0800509 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800510}