blob: 1a15a9d5bcf2bba21b2ebc90a0a6d1aecb05e26a [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;
andrewsbrown39b0fc92015-05-11 13:44:38 -070023import com.intel.jndn.management.types.StrategyChoice;
andrewsbrown43b96302015-03-17 21:51:43 +010024import com.intel.jndn.utils.SimpleClient;
Andrew Brown07466442015-02-24 09:07:02 -080025import com.intel.jndn.utils.SegmentedClient;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080026import java.io.IOException;
27import java.util.List;
28import net.named_data.jndn.ControlParameters;
29import net.named_data.jndn.Data;
30import net.named_data.jndn.Face;
31import net.named_data.jndn.ForwardingFlags;
32import net.named_data.jndn.Interest;
33import net.named_data.jndn.Name;
34import net.named_data.jndn.encoding.EncodingException;
35import net.named_data.jndn.security.SecurityException;
Andrew Brownc46c1602015-02-18 10:45:56 -080036import java.util.logging.Logger;
Andrew Brownc5dda702015-04-13 09:36:04 -070037import net.named_data.jndn.KeyLocator;
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080038
39/**
40 * Helper class for interacting with an NDN forwarder daemon; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -070041 * <a href="http://redmine.named-data.net/projects/nfd/wiki/Management">http://redmine.named-data.net/projects/nfd/wiki/Management</a>
42 * for explanations of the various protocols used.
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080043 *
44 * @author Andrew Brown <andrew.brown@intel.com>
45 */
46public class NFD {
47
Andrew Brown211d2b62015-02-18 11:12:02 -080048 public final static long DEFAULT_TIMEOUT = 2000;
andrewsbrown43b96302015-03-17 21:51:43 +010049 public final static int OK_STATUS = 200;
Andrew Brown211d2b62015-02-18 11:12:02 -080050 static private final Logger logger = Logger.getLogger(NFD.class.getName());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080051
Andrew Brown211d2b62015-02-18 11:12:02 -080052 /**
53 * Ping a forwarder on an existing face to verify that the forwarder is
54 * working and responding to requests; this version sends a discovery packet
55 * to /localhost/nfd which should always respond if the requestor is on the
56 * same machine as the NDN forwarding daemon.
57 *
andrewsbrown8a32f302015-03-24 08:42:46 -070058 * @param face only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -080059 * @return true if successful, false otherwise
60 */
61 public static boolean pingLocal(Face face) {
62 return ping(face, new Name("/localhost/nfd"));
63 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080064
Andrew Brown211d2b62015-02-18 11:12:02 -080065 /**
66 * Request a name on an existing face to verify the forwarder is working and
67 * responding to requests. Note that the name must be served or cached on the
68 * forwarder for this to return true.
69 *
70 * @param face
71 * @param name
72 * @return true if successful, false otherwise
73 */
74 public static boolean ping(Face face, Name name) {
75 // build interest
76 Interest interest = new Interest(name);
77 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
78 interest.setMustBeFresh(true);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080079
Andrew Brown211d2b62015-02-18 11:12:02 -080080 // send packet
andrewsbrown43b96302015-03-17 21:51:43 +010081 try {
82 Data data = SimpleClient.getDefault().getSync(face, interest);
83 return data != null;
84 } catch (IOException e) {
85 return false;
86 }
Andrew Brown211d2b62015-02-18 11:12:02 -080087 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -080088
Andrew Brown211d2b62015-02-18 11:12:02 -080089 /**
andrewsbrown8a32f302015-03-24 08:42:46 -070090 * Retrieve the status of the given forwarder; calls /localhost/nfd/status
91 * which requires a local Face (all non-local packets are dropped)
92 *
93 * @param forwarder only a localhost Face
94 * @return the forwarder status object, see
Andrew Browndfa8fc12015-03-25 10:46:47 -070095 * <a href="http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus">
96 * http://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus</a>.
andrewsbrown8a32f302015-03-24 08:42:46 -070097 * @throws java.lang.Exception
98 */
99 public static ForwarderStatus getForwarderStatus(Face forwarder) throws Exception {
Andrew Brownc5dda702015-04-13 09:36:04 -0700100 Data data = retrieveStatus(forwarder);
andrewsbrown8a32f302015-03-24 08:42:46 -0700101 ForwarderStatus status = new ForwarderStatus();
102 status.wireDecode(data.getContent().buf());
103 return status;
104 }
105
106 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800107 * Retrieve a list of faces and their status from the given forwarder; calls
108 * /localhost/nfd/faces/list which requires a local Face (all non-local
109 * packets are dropped)
110 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700111 * @param forwarder only a localhost Face
112 * @return a list of face status objects, see
113 * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt.
andrewsbrown43b96302015-03-17 21:51:43 +0100114 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800115 */
116 public static List<FaceStatus> getFaceList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100117 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/faces/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800118 return StatusDataset.wireDecode(data.getContent(), FaceStatus.class);
119 }
Andrew Brownc46c1602015-02-18 10:45:56 -0800120
Andrew Brown211d2b62015-02-18 11:12:02 -0800121 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800122 * Retrieve a list of FIB entries and their NextHopRecords from the given
123 * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all
124 * non-local packets are dropped).
125 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700126 * @param forwarder only a localhost Face
127 * @return a list of FIB entries, see
128 * http://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100129 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800130 */
131 public static List<FibEntry> getFibList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100132 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/fib/list"));
Andrew Brown63bed362015-02-18 11:28:50 -0800133 return StatusDataset.wireDecode(data.getContent(), FibEntry.class);
134 }
135
136 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800137 * Retrieve a list of routing entries from the RIB; calls
138 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
andrewsbrown43b96302015-03-17 21:51:43 +0100139 * are dropped).
Andrew Brown211d2b62015-02-18 11:12:02 -0800140 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700141 * @param forwarder only a localhost Face
142 * @return a list of RIB entries, i.e. routes, see
143 * http://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset.
andrewsbrown43b96302015-03-17 21:51:43 +0100144 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800145 */
146 public static List<RibEntry> getRouteList(Face forwarder) throws Exception {
andrewsbrown43b96302015-03-17 21:51:43 +0100147 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/rib/list"));
Andrew Brown211d2b62015-02-18 11:12:02 -0800148 return StatusDataset.wireDecode(data.getContent(), RibEntry.class);
149 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800150
Andrew Brown211d2b62015-02-18 11:12:02 -0800151 /**
andrewsbrown39b0fc92015-05-11 13:44:38 -0700152 * Retrieve the list of strategy choice entries from the NFD; calls
153 * /localhost/nfd/rib/list which requires a local Face (all non-local packets
154 * are dropped).
155 *
156 * @param forwarder only a localhost Face
157 * @return a list of strategy choice entries, i.e. routes, see
158 * http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice.
159 * @throws java.lang.Exception
160 */
161 public static List<StrategyChoice> getStrategyList(Face forwarder) throws Exception {
162 Data data = retrieveDataSet(forwarder, new Name("/localhost/nfd/strategy-choice/list"));
163 return StatusDataset.wireDecode(data.getContent(), StrategyChoice.class);
164 }
165
166 /**
Andrew Brownc5dda702015-04-13 09:36:04 -0700167 * Retrieve the {@link KeyLocator} for an NFD.
168 *
169 * @param forwarder only a localhost {@link Face}
170 * @return the {@link KeyLocator} of the NFD's key
171 * @throws ManagementException if the key is not available
172 * @throws IOException if the request fails
173 */
174 public static KeyLocator getKeyLocator(Face forwarder) throws ManagementException, IOException {
175 Data data = retrieveStatus(forwarder);
176 if (!KeyLocator.canGetFromSignature(data.getSignature())) {
177 throw new ManagementException("No key locator available.");
178 }
179 return KeyLocator.getFromSignature(data.getSignature());
180 }
181
182 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800183 * Helper method to register a new face on the forwarder; as mentioned at
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700184 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>,
185 * this is more for debugging; use 'register' instead
Andrew Brown211d2b62015-02-18 11:12:02 -0800186 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700187 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800188 * @param faceId
189 * @param prefix
andrewsbrown43b96302015-03-17 21:51:43 +0100190 * @throws java.lang.Exception
Andrew Brown211d2b62015-02-18 11:12:02 -0800191 */
andrewsbrown43b96302015-03-17 21:51:43 +0100192 public static void addNextHop(Face forwarder, int faceId, Name prefix) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800193 // build command name
194 Name command = new Name("/localhost/nfd/fib/add-nexthop");
195 ControlParameters parameters = new ControlParameters();
196 parameters.setName(prefix);
197 parameters.setFaceId(faceId);
198 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800199
Andrew Brown211d2b62015-02-18 11:12:02 -0800200 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100201 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800202 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800203
Andrew Brown211d2b62015-02-18 11:12:02 -0800204 /**
205 * Create a new face on the given forwarder. Ensure the forwarding face is on
206 * the local machine (management requests are to /localhost/...) and that
207 * command signing has been set up (e.g. forwarder.setCommandSigningInfo()).
208 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700209 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800210 * @param uri
211 * @return
212 * @throws java.lang.Exception
213 */
214 public static int createFace(Face forwarder, String uri) throws Exception {
215 Name command = new Name("/localhost/nfd/faces/create");
216 ControlParameters parameters = new ControlParameters();
217 parameters.setUri(uri);
218 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800219
Andrew Brown211d2b62015-02-18 11:12:02 -0800220 // send the interest
221 ControlResponse response = sendCommand(forwarder, new Interest(command));
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800222
Andrew Brown211d2b62015-02-18 11:12:02 -0800223 // return
224 return response.getBody().get(0).getFaceId();
225 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800226
Andrew Brown211d2b62015-02-18 11:12:02 -0800227 /**
andrewsbrown43b96302015-03-17 21:51:43 +0100228 * Destroy a face on given forwarder. Ensure the forwarding face is on the
229 * local machine (management requests are to /localhost/...) and that command
230 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700231 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700232 * @param forwarder only a localhost Face
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700233 * @param faceId
234 * @throws java.lang.Exception
235 */
andrewsbrown43b96302015-03-17 21:51:43 +0100236 public static void destroyFace(Face forwarder, int faceId) throws Exception {
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700237 Name command = new Name("/localhost/nfd/faces/destroy");
238 ControlParameters parameters = new ControlParameters();
239 parameters.setFaceId(faceId);
240 command.append(parameters.wireEncode());
241
242 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100243 sendCommand(forwarder, new Interest(command));
Alexander Afanasyev75d8dfc2015-03-13 16:41:01 -0700244 }
245
246 /**
andrewsbrowne8e8e852015-03-09 13:48:31 -0700247 * Enable a local control feature on the given forwarder. See
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700248 * <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 -0700249 *
250 * @param forwarder
251 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700252 * @throws Exception
253 */
andrewsbrown43b96302015-03-17 21:51:43 +0100254 public static void enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700255 // build command name
256 Name command = new Name("/localhost/nfd/faces/enable-local-control");
257 ControlParameters parameters = new ControlParameters();
258 parameters.setLocalControlFeature(header.getNumericValue());
259 command.append(parameters.wireEncode());
260
261 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100262 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700263 }
264
265 /**
266 * Disable a local control feature on the given forwarder. See
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700267 * <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 -0700268 *
269 * @param forwarder
270 * @param header
andrewsbrowne8e8e852015-03-09 13:48:31 -0700271 * @throws Exception
272 */
andrewsbrown43b96302015-03-17 21:51:43 +0100273 public static void disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception {
andrewsbrowne8e8e852015-03-09 13:48:31 -0700274 // build command name
275 Name command = new Name("/localhost/nfd/faces/disable-local-control");
276 ControlParameters parameters = new ControlParameters();
277 parameters.setLocalControlFeature(header.getNumericValue());
278 command.append(parameters.wireEncode());
279
280 // send command and return
andrewsbrown43b96302015-03-17 21:51:43 +0100281 sendCommand(forwarder, new Interest(command));
andrewsbrowne8e8e852015-03-09 13:48:31 -0700282 }
283
284 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800285 * Register a route on the forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700286 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
287 * for command-line usage and
288 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
289 * for protocol documentation. Ensure the forwarding face is on the local
290 * machine (management requests are to /localhost/...) and that command
291 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
Andrew Brown211d2b62015-02-18 11:12:02 -0800292 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700293 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800294 * @param controlParameters
Andrew Brown211d2b62015-02-18 11:12:02 -0800295 * @throws Exception
296 */
andrewsbrown43b96302015-03-17 21:51:43 +0100297 public static void register(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800298 // build command name
299 Name command = new Name("/localhost/nfd/rib/register");
300 command.append(controlParameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800301
Andrew Brown211d2b62015-02-18 11:12:02 -0800302 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100303 sendCommand(forwarder, new Interest(command));
Andrew Brown211d2b62015-02-18 11:12:02 -0800304 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800305
Andrew Brown211d2b62015-02-18 11:12:02 -0800306 /**
307 * Register a route on a forwarder; this will create a new face on the
308 * forwarder to the given URI/route pair. See register(Face,
309 * ControlParameters) for more details documentation.
310 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700311 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800312 * @param uri
313 * @param cost
314 * @param route
Andrew Brown211d2b62015-02-18 11:12:02 -0800315 * @throws java.lang.Exception
316 */
andrewsbrown43b96302015-03-17 21:51:43 +0100317 public static void register(Face forwarder, String uri, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800318 // create the new face
319 int faceId = createFace(forwarder, uri);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800320
Andrew Brown211d2b62015-02-18 11:12:02 -0800321 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100322 register(forwarder, faceId, route, cost);
Andrew Brown211d2b62015-02-18 11:12:02 -0800323 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800324
Andrew Brown211d2b62015-02-18 11:12:02 -0800325 /**
326 * Register a route on a forwarder; this will not create a new face since it
327 * is provided a faceId. See register(Face, ControlParameters) for full
328 * documentation
329 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700330 * @param forwarder only a localhost Face
Andrew Brown211d2b62015-02-18 11:12:02 -0800331 * @param faceId
332 * @param route
333 * @param cost
Andrew Brown211d2b62015-02-18 11:12:02 -0800334 * @throws java.lang.Exception
335 */
andrewsbrown43b96302015-03-17 21:51:43 +0100336 public static void register(Face forwarder, int faceId, Name route, int cost) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800337 // build command name
338 ControlParameters parameters = new ControlParameters();
339 parameters.setName(route);
340 parameters.setFaceId(faceId);
341 parameters.setCost(cost);
342 ForwardingFlags flags = new ForwardingFlags();
343 flags.setCapture(true);
344 flags.setChildInherit(true);
345 parameters.setForwardingFlags(flags);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800346
Andrew Brown211d2b62015-02-18 11:12:02 -0800347 // run base method
andrewsbrown43b96302015-03-17 21:51:43 +0100348 register(forwarder, parameters);
Andrew Brown211d2b62015-02-18 11:12:02 -0800349 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800350
Andrew Brown211d2b62015-02-18 11:12:02 -0800351 /**
Andrew Brown63bed362015-02-18 11:28:50 -0800352 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700353 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
354 * for command-line usage and
355 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
356 * for protocol documentation. Ensure the forwarding face is on the local
357 * machine (management requests are to /localhost/...) and that command
358 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown63bed362015-02-18 11:28:50 -0800359 *
360 * @param forwarder
Andrew Brown07466442015-02-24 09:07:02 -0800361 * @param controlParameters
andrewsbrown43b96302015-03-17 21:51:43 +0100362 * @throws java.lang.Exception
Andrew Brown63bed362015-02-18 11:28:50 -0800363 */
andrewsbrown43b96302015-03-17 21:51:43 +0100364 public static void unregister(Face forwarder, ControlParameters controlParameters) throws Exception {
Andrew Brown63bed362015-02-18 11:28:50 -0800365 // build command name
366 Name command = new Name("/localhost/nfd/rib/unregister");
367 command.append(controlParameters.wireEncode());
368
369 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100370 sendCommand(forwarder, new Interest(command));
Andrew Brown63bed362015-02-18 11:28:50 -0800371 }
372
373 /**
Andrew Brown07466442015-02-24 09:07:02 -0800374 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700375 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
376 * for command-line usage and
377 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
378 * for protocol documentation. Ensure the forwarding face is on the local
379 * machine (management requests are to /localhost/...) and that command
380 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800381 *
382 * @param forwarder
383 * @param route
andrewsbrown43b96302015-03-17 21:51:43 +0100384 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800385 */
andrewsbrown43b96302015-03-17 21:51:43 +0100386 public static void unregister(Face forwarder, Name route) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800387 // build command name
388 ControlParameters controlParameters = new ControlParameters();
389 controlParameters.setName(route);
390
391 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100392 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800393 }
394
395 /**
396 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700397 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
398 * for command-line usage and
399 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
400 * for protocol documentation. Ensure the forwarding face is on the local
401 * machine (management requests are to /localhost/...) and that command
402 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800403 *
404 * @param forwarder
405 * @param route
406 * @param faceId
andrewsbrown43b96302015-03-17 21:51:43 +0100407 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800408 */
andrewsbrown43b96302015-03-17 21:51:43 +0100409 public static void unregister(Face forwarder, Name route, int faceId) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800410 // build command name
411 ControlParameters controlParameters = new ControlParameters();
412 controlParameters.setName(route);
413 controlParameters.setFaceId(faceId);
414
415 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100416 unregister(forwarder, controlParameters);
Andrew Brown07466442015-02-24 09:07:02 -0800417 }
418
419 /**
420 * Unregister a route on a forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700421 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
422 * for command-line usage and
423 * <a href="http://redmine.named-data.net/projects/nfd/wiki/RibMgmt">http://redmine.named-data.net/projects/nfd/wiki/RibMgmt</a>
424 * for protocol documentation. Ensure the forwarding face is on the local
425 * machine (management requests are to /localhost/...) and that command
426 * signing has been set up (e.g. forwarder.setCommandSigningInfo()
Andrew Brown07466442015-02-24 09:07:02 -0800427 *
428 * @param forwarder
429 * @param route
430 * @param uri
andrewsbrown43b96302015-03-17 21:51:43 +0100431 * @throws java.lang.Exception
Andrew Brown07466442015-02-24 09:07:02 -0800432 */
andrewsbrown43b96302015-03-17 21:51:43 +0100433 public static void unregister(Face forwarder, Name route, String uri) throws Exception {
Andrew Brown07466442015-02-24 09:07:02 -0800434 int faceId = -1;
andrewsbrown4c21ea22015-03-09 12:05:03 -0700435 for (FaceStatus face : getFaceList(forwarder)) {
436 if (face.getUri().matches(uri)) {
Andrew Brown07466442015-02-24 09:07:02 -0800437 faceId = face.getFaceId();
438 break;
439 }
440 }
andrewsbrown4c21ea22015-03-09 12:05:03 -0700441
442 if (faceId == -1) {
andrewsbrown43b96302015-03-17 21:51:43 +0100443 throw new ManagementException("Face not found: " + uri);
Andrew Brown07466442015-02-24 09:07:02 -0800444 }
445
446 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100447 unregister(forwarder, route, faceId);
Andrew Brown07466442015-02-24 09:07:02 -0800448 }
449
450 /**
Andrew Brown211d2b62015-02-18 11:12:02 -0800451 * Set a strategy on the forwarder; see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700452 * <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">http://named-data.net/doc/NFD/current/manpages/nfdc.html</a>
453 * for command-line usage and
454 * <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 -0800455 * for protocol documentation. Ensure the forwarding face is on the local
456 * machine (management requests are to /localhost/...) and that command
457 * signing has been set up (e.g. forwarder.setCommandSigningInfo()).
458 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700459 * @param forwarder only a localhost Face
andrewsbrowncad04eb2015-05-11 13:16:24 -0700460 * @param prefix the {@link Name} prefix
andrewsbrown39b0fc92015-05-11 13:44:38 -0700461 * @param strategy the {@link Name} of the strategy to set, e.g.
462 * /localhost/nfd/strategy/broadcast
andrewsbrowncad04eb2015-05-11 13:16:24 -0700463 * @throws Exception if the command fails
Andrew Brown211d2b62015-02-18 11:12:02 -0800464 */
andrewsbrown43b96302015-03-17 21:51:43 +0100465 public static void setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception {
Andrew Brown211d2b62015-02-18 11:12:02 -0800466 // build command name
467 Name command = new Name("/localhost/nfd/strategy-choice/set");
468 ControlParameters parameters = new ControlParameters();
469 parameters.setName(prefix);
470 parameters.setStrategy(strategy);
471 command.append(parameters.wireEncode());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800472
Andrew Brown211d2b62015-02-18 11:12:02 -0800473 // send the interest
andrewsbrown43b96302015-03-17 21:51:43 +0100474 sendCommand(forwarder, new Interest(command));
475 }
476
477 /**
andrewsbrowncad04eb2015-05-11 13:16:24 -0700478 * Set a strategy on the forwarder; see
479 * {@link #setStrategy(net.named_data.jndn.Face, net.named_data.jndn.Name, net.named_data.jndn.Name)}
480 * for more information. Ensure the forwarding face is on the local machine
481 * (management requests are to /localhost/...) and that command signing has
482 * been set up (e.g. forwarder.setCommandSigningInfo()).
483 *
484 * @param forwarder only a localhost {@link Face}
485 * @param prefix the {@link Name} prefix
486 * @throws Exception if the command fails
487 */
488 public static void unsetStrategy(Face forwarder, Name prefix) throws Exception {
489 // build command name
Andrew Brown37cec242015-05-11 14:24:13 -0700490 Name command = new Name("/localhost/nfd/strategy-choice/unset");
andrewsbrowncad04eb2015-05-11 13:16:24 -0700491 ControlParameters parameters = new ControlParameters();
492 parameters.setName(prefix);
493 command.append(parameters.wireEncode());
494
495 // send the interest
496 sendCommand(forwarder, new Interest(command));
497 }
498
499 /**
Andrew Brownc5dda702015-04-13 09:36:04 -0700500 * Build an interest to retrieve the NFD status.
501 *
502 * @param forwarder only a localhost {@link Face}
503 * @return the status {@link Data} packet
504 * @throws IOException if the retrieval fails
505 */
506 private static Data retrieveStatus(Face forwarder) throws IOException {
507 Interest interest = new Interest(new Name("/localhost/nfd/status"));
508 interest.setMustBeFresh(true);
509 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
510 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
511 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
512 return data;
513 }
514
515 /**
andrewsbrown43b96302015-03-17 21:51:43 +0100516 * Build an interest to retrieve a segmented data set from the NFD; for
517 * details on the DataSet, see
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700518 * <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 +0100519 *
Andrew Brownc5dda702015-04-13 09:36:04 -0700520 * @param forwarder the {@link Face} to an NFD
521 * @param datasetName the {@link Name} of the dataset to retrieve
522 * @return the re-assembled {@link Data} packet
523 * @throws IOException if the request fails
524 * @throws ManagementException if the returned TLV is not the expected type
andrewsbrown43b96302015-03-17 21:51:43 +0100525 */
526 public static Data retrieveDataSet(Face forwarder, Name datasetName) throws IOException, ManagementException {
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700527 // 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 -0700528 Interest interest = new Interest(datasetName);
andrewsbrown43b96302015-03-17 21:51:43 +0100529 interest.setMustBeFresh(true);
530 interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT);
531 interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT);
532
533 // send packet
534 Data data = SegmentedClient.getDefault().getSync(forwarder, interest);
535
536 // check for failed request
537 if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) {
538 throw ManagementException.fromResponse(data.getContent());
539 }
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700540
andrewsbrown43b96302015-03-17 21:51:43 +0100541 return data;
Andrew Brown211d2b62015-02-18 11:12:02 -0800542 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800543
Andrew Brown211d2b62015-02-18 11:12:02 -0800544 /**
545 * Send an interest as a command to the forwarder; this method will convert
546 * the interest to a command interest and block until a response is received
547 * from the forwarder. Ensure the forwarding face is on the local machine
548 * (management requests are to /localhost/...) and that command signing has
549 * been set up (e.g. forwarder.setCommandSigningInfo()).
550 *
andrewsbrown8a32f302015-03-24 08:42:46 -0700551 * @param forwarder only a localhost Face, command signing info must be set
Andrew Brown211d2b62015-02-18 11:12:02 -0800552 * @param interest As described at
andrewsbrown9a6d7ba2015-03-24 09:53:29 -0700553 * <a href="http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,">http://redmine.named-data.net/projects/nfd/wiki/ControlCommand,</a>
554 * the requested interest must have encoded ControlParameters appended to the
Andrew Brown211d2b62015-02-18 11:12:02 -0800555 * interest name
Andrew Brownc5dda702015-04-13 09:36:04 -0700556 * @return a {@link ControlResponse}
Andrew Brown211d2b62015-02-18 11:12:02 -0800557 * @throws java.io.IOException
558 * @throws net.named_data.jndn.encoding.EncodingException
andrewsbrown43b96302015-03-17 21:51:43 +0100559 * @throws com.intel.jndn.management.ManagementException
Andrew Brown211d2b62015-02-18 11:12:02 -0800560 */
andrewsbrown43b96302015-03-17 21:51:43 +0100561 public static ControlResponse sendCommand(Face forwarder, Interest interest) throws IOException, EncodingException, ManagementException {
562 // forwarder must have command signing info set
563 try {
564 forwarder.makeCommandInterest(interest);
565 } catch (SecurityException e) {
566 throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the face.", e);
567 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800568
Andrew Brown211d2b62015-02-18 11:12:02 -0800569 // send command packet
andrewsbrown43b96302015-03-17 21:51:43 +0100570 Data data = SimpleClient.getDefault().getSync(forwarder, interest);
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800571
andrewsbrown43b96302015-03-17 21:51:43 +0100572 // decode response
Andrew Brown211d2b62015-02-18 11:12:02 -0800573 ControlResponse response = new ControlResponse();
574 response.wireDecode(data.getContent().buf());
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800575
andrewsbrown43b96302015-03-17 21:51:43 +0100576 // check response for success
577 if (response.getStatusCode() != OK_STATUS) {
578 throw ManagementException.fromResponse(response);
Andrew Brown211d2b62015-02-18 11:12:02 -0800579 }
andrewsbrown43b96302015-03-17 21:51:43 +0100580
581 return response;
Andrew Brown211d2b62015-02-18 11:12:02 -0800582 }
Andrew Brown2f1fdbf2015-01-21 10:52:29 -0800583}