Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 1 | /* |
andrewsbrown | 7e6b9e8 | 2015-03-03 16:11:11 -0800 | [diff] [blame] | 2 | * 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 Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 16 | import com.intel.jndn.management.types.StatusDataset; |
| 17 | import com.intel.jndn.management.types.ControlResponse; |
| 18 | import com.intel.jndn.management.types.FaceStatus; |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 19 | import com.intel.jndn.management.types.FibEntry; |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 20 | import com.intel.jndn.management.types.LocalControlHeader; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 21 | import com.intel.jndn.management.types.RibEntry; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 22 | import com.intel.jndn.utils.Client; |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 23 | import com.intel.jndn.utils.SegmentedClient; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 24 | import java.io.IOException; |
| 25 | import java.util.List; |
| 26 | import net.named_data.jndn.ControlParameters; |
| 27 | import net.named_data.jndn.Data; |
| 28 | import net.named_data.jndn.Face; |
| 29 | import net.named_data.jndn.ForwardingFlags; |
| 30 | import net.named_data.jndn.Interest; |
| 31 | import net.named_data.jndn.Name; |
| 32 | import net.named_data.jndn.encoding.EncodingException; |
| 33 | import net.named_data.jndn.security.SecurityException; |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 34 | import java.util.logging.Logger; |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 35 | |
| 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 | */ |
| 43 | public class NFD { |
| 44 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 45 | public final static long DEFAULT_TIMEOUT = 2000; |
| 46 | static private final Logger logger = Logger.getLogger(NFD.class.getName()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 47 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 48 | /** |
| 49 | * Ping a forwarder on an existing face to verify that the forwarder is |
| 50 | * working and responding to requests; this version sends a discovery packet |
| 51 | * to /localhost/nfd which should always respond if the requestor is on the |
| 52 | * same machine as the NDN forwarding daemon. |
| 53 | * |
| 54 | * @param face |
| 55 | * @return true if successful, false otherwise |
| 56 | */ |
| 57 | public static boolean pingLocal(Face face) { |
| 58 | return ping(face, new Name("/localhost/nfd")); |
| 59 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 60 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Request a name on an existing face to verify the forwarder is working and |
| 63 | * responding to requests. Note that the name must be served or cached on the |
| 64 | * forwarder for this to return true. |
| 65 | * |
| 66 | * @param face |
| 67 | * @param name |
| 68 | * @return true if successful, false otherwise |
| 69 | */ |
| 70 | public static boolean ping(Face face, Name name) { |
| 71 | // build interest |
| 72 | Interest interest = new Interest(name); |
| 73 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 74 | interest.setMustBeFresh(true); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 75 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 76 | // send packet |
| 77 | Data data = Client.getDefault().getSync(face, interest); |
| 78 | return data != null; |
| 79 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 80 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 81 | /** |
| 82 | * Retrieve a list of faces and their status from the given forwarder; calls |
| 83 | * /localhost/nfd/faces/list which requires a local Face (all non-local |
| 84 | * packets are dropped) |
| 85 | * |
| 86 | * @param forwarder Only a localhost Face |
| 87 | * @return |
| 88 | * @throws Exception |
| 89 | */ |
| 90 | public static List<FaceStatus> getFaceList(Face forwarder) throws Exception { |
| 91 | // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 92 | Interest interest = new Interest(new Name("/localhost/nfd/faces/list")); |
| 93 | interest.setMustBeFresh(true); |
| 94 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 95 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 96 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 97 | // send packet |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 98 | Data data = SegmentedClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 99 | if (data == null) { |
| 100 | throw new Exception("Failed to retrieve list of faces from the forwarder."); |
| 101 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 102 | |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 103 | // check for failed request |
| 104 | if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) { |
| 105 | ControlResponse response = new ControlResponse(); |
| 106 | response.wireDecode(data.getContent().buf()); |
| 107 | throw new Exception("Failed to retrieve list of faces, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText()); |
| 108 | } |
| 109 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 110 | // parse packet |
| 111 | return StatusDataset.wireDecode(data.getContent(), FaceStatus.class); |
| 112 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 113 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 114 | /** |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 115 | * Retrieve a list of FIB entries and their NextHopRecords from the given |
| 116 | * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all |
| 117 | * non-local packets are dropped). |
| 118 | * |
| 119 | * @param forwarder Only a localhost Face |
| 120 | * @return |
| 121 | * @throws Exception |
| 122 | */ |
| 123 | public static List<FibEntry> getFibList(Face forwarder) throws Exception { |
| 124 | // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 125 | Interest interest = new Interest(new Name("/localhost/nfd/fib/list")); |
| 126 | interest.setMustBeFresh(true); |
| 127 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 128 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
| 129 | |
| 130 | // TODO verify that all faces are being returned; right now they don't |
| 131 | // match up with the results from nfd-status-http-server but no |
| 132 | // additional segments are present; |
| 133 | // see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 134 | // send packet |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 135 | Data data = SegmentedClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 136 | if (data == null) { |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 137 | throw new Exception("Failed to retrieve list of FIB entries from the forwarder."); |
| 138 | } |
| 139 | |
| 140 | // check for failed request |
| 141 | if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) { |
| 142 | ControlResponse response = new ControlResponse(); |
| 143 | response.wireDecode(data.getContent().buf()); |
| 144 | throw new Exception("Failed to retrieve list of FIB entries, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText()); |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // parse packet |
| 148 | return StatusDataset.wireDecode(data.getContent(), FibEntry.class); |
| 149 | } |
| 150 | |
| 151 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 152 | * Retrieve a list of routing entries from the RIB; 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 |
| 158 | * @throws Exception |
| 159 | */ |
| 160 | public static List<RibEntry> getRouteList(Face forwarder) throws Exception { |
| 161 | // build management Interest packet; see http://redmine.named-data.net/projects/nfd/wiki/StatusDataset |
| 162 | Interest interest = new Interest(new Name("/localhost/nfd/rib/list")); |
| 163 | interest.setMustBeFresh(true); |
| 164 | interest.setChildSelector(Interest.CHILD_SELECTOR_RIGHT); |
| 165 | interest.setInterestLifetimeMilliseconds(DEFAULT_TIMEOUT); |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 166 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 167 | // send packet |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 168 | Data data = SegmentedClient.getDefault().getSync(forwarder, interest); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 169 | if (data == null) { |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 170 | throw new Exception("Failed to retrieve list of routes from the forwarder."); |
| 171 | } |
| 172 | |
| 173 | // check for failed request |
| 174 | if (data.getContent().buf().get(0) == ControlResponse.TLV_CONTROL_RESPONSE) { |
| 175 | ControlResponse response = new ControlResponse(); |
| 176 | response.wireDecode(data.getContent().buf()); |
| 177 | throw new Exception("Failed to retrieve list of routes, forwarder returned: " + response.getStatusCode() + " " + response.getStatusText()); |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 178 | } |
Andrew Brown | c46c160 | 2015-02-18 10:45:56 -0800 | [diff] [blame] | 179 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 180 | // parse packet |
| 181 | return StatusDataset.wireDecode(data.getContent(), RibEntry.class); |
| 182 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 183 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 184 | /** |
| 185 | * Helper method to register a new face on the forwarder; as mentioned at |
| 186 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html, this is more for |
| 187 | * debugging; use 'register' instead |
| 188 | * |
| 189 | * @param forwarder Only a localhost Face |
| 190 | * @param faceId |
| 191 | * @param prefix |
| 192 | * @return |
| 193 | * @throws Exception |
| 194 | */ |
| 195 | public static boolean addNextHop(Face forwarder, int faceId, Name prefix) throws Exception { |
| 196 | // build command name |
| 197 | Name command = new Name("/localhost/nfd/fib/add-nexthop"); |
| 198 | ControlParameters parameters = new ControlParameters(); |
| 199 | parameters.setName(prefix); |
| 200 | parameters.setFaceId(faceId); |
| 201 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 202 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 203 | // send the interest |
| 204 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 205 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 206 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 207 | /** |
| 208 | * Create a new face on the given forwarder. Ensure the forwarding face is on |
| 209 | * the local machine (management requests are to /localhost/...) and that |
| 210 | * command signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 211 | * |
| 212 | * @param forwarder Only a localhost Face |
| 213 | * @param uri |
| 214 | * @return |
| 215 | * @throws java.lang.Exception |
| 216 | */ |
| 217 | public static int createFace(Face forwarder, String uri) throws Exception { |
| 218 | Name command = new Name("/localhost/nfd/faces/create"); |
| 219 | ControlParameters parameters = new ControlParameters(); |
| 220 | parameters.setUri(uri); |
| 221 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 222 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 223 | // send the interest |
| 224 | ControlResponse response = sendCommand(forwarder, new Interest(command)); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 225 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 226 | // check for body and that status code is OK (TODO: 200 should be replaced with a CONSTANT like ControlResponse.STATUS_OK) |
| 227 | if (response.getBody().isEmpty() || response.getStatusCode() != 200) { |
| 228 | throw new Exception("Failed to create face: " + uri + " " + response.getStatusText()); |
| 229 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 230 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 231 | // return |
| 232 | return response.getBody().get(0).getFaceId(); |
| 233 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 234 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 235 | /** |
Alexander Afanasyev | 75d8dfc | 2015-03-13 16:41:01 -0700 | [diff] [blame^] | 236 | * Create a new face on the given forwarder. Ensure the forwarding face is on |
| 237 | * the local machine (management requests are to /localhost/...) and that |
| 238 | * command signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 239 | * |
| 240 | * @param forwarder Only a localhost Face |
| 241 | * @param faceId |
| 242 | * @throws java.lang.Exception |
| 243 | */ |
| 244 | public static void destroyFace(Face forwarder, int faceId) throws Exception { |
| 245 | Name command = new Name("/localhost/nfd/faces/destroy"); |
| 246 | ControlParameters parameters = new ControlParameters(); |
| 247 | parameters.setFaceId(faceId); |
| 248 | command.append(parameters.wireEncode()); |
| 249 | |
| 250 | // send the interest |
| 251 | ControlResponse response = sendCommand(forwarder, new Interest(command)); |
| 252 | |
| 253 | // check for body and that status code is OK (TODO: 200 should be replaced with a CONSTANT like ControlResponse.STATUS_OK) |
| 254 | if (response.getBody().isEmpty() || response.getStatusCode() != 200) { |
| 255 | throw new Exception("Failed to destroy face: " + String.valueOf(faceId) + " " + response.getStatusText()); |
| 256 | } |
| 257 | |
| 258 | return; |
| 259 | } |
| 260 | |
| 261 | /** |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame] | 262 | * Enable a local control feature on the given forwarder. See |
| 263 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature |
| 264 | * |
| 265 | * @param forwarder |
| 266 | * @param header |
| 267 | * @return |
| 268 | * @throws Exception |
| 269 | */ |
| 270 | public static boolean enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
| 271 | // build command name |
| 272 | Name command = new Name("/localhost/nfd/faces/enable-local-control"); |
| 273 | ControlParameters parameters = new ControlParameters(); |
| 274 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 275 | command.append(parameters.wireEncode()); |
| 276 | |
| 277 | // send command and return |
| 278 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Disable a local control feature on the given forwarder. See |
| 283 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature |
| 284 | * |
| 285 | * @param forwarder |
| 286 | * @param header |
| 287 | * @return |
| 288 | * @throws Exception |
| 289 | */ |
| 290 | public static boolean disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
| 291 | // build command name |
| 292 | Name command = new Name("/localhost/nfd/faces/disable-local-control"); |
| 293 | ControlParameters parameters = new ControlParameters(); |
| 294 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 295 | command.append(parameters.wireEncode()); |
| 296 | |
| 297 | // send command and return |
| 298 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 299 | } |
| 300 | |
| 301 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 302 | * Register a route on the forwarder; see |
| 303 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 304 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 305 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 306 | * (management requests are to /localhost/...) and that command signing has |
| 307 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 308 | * |
| 309 | * @param forwarder Only a localhost Face |
| 310 | * @param controlParameters |
| 311 | * @return |
| 312 | * @throws Exception |
| 313 | */ |
| 314 | public static boolean register(Face forwarder, ControlParameters controlParameters) throws Exception { |
| 315 | // build command name |
| 316 | Name command = new Name("/localhost/nfd/rib/register"); |
| 317 | command.append(controlParameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 318 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 319 | // send the interest |
| 320 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 321 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 322 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 323 | /** |
| 324 | * Register a route on a forwarder; this will create a new face on the |
| 325 | * forwarder to the given URI/route pair. See register(Face, |
| 326 | * ControlParameters) for more details documentation. |
| 327 | * |
| 328 | * @param forwarder Only a localhost Face |
| 329 | * @param uri |
| 330 | * @param cost |
| 331 | * @param route |
| 332 | * @return true for successful registration |
| 333 | * @throws java.lang.Exception |
| 334 | */ |
| 335 | public static boolean register(Face forwarder, String uri, Name route, int cost) throws Exception { |
| 336 | // create the new face |
| 337 | int faceId = createFace(forwarder, uri); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 338 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 339 | // run base method |
| 340 | return register(forwarder, faceId, route, cost); |
| 341 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 342 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 343 | /** |
| 344 | * Register a route on a forwarder; this will not create a new face since it |
| 345 | * is provided a faceId. See register(Face, ControlParameters) for full |
| 346 | * documentation |
| 347 | * |
| 348 | * @param forwarder Only a localhost Face |
| 349 | * @param faceId |
| 350 | * @param route |
| 351 | * @param cost |
| 352 | * @return true for successful registration |
| 353 | * @throws java.lang.Exception |
| 354 | */ |
| 355 | public static boolean register(Face forwarder, int faceId, Name route, int cost) throws Exception { |
| 356 | // build command name |
| 357 | ControlParameters parameters = new ControlParameters(); |
| 358 | parameters.setName(route); |
| 359 | parameters.setFaceId(faceId); |
| 360 | parameters.setCost(cost); |
| 361 | ForwardingFlags flags = new ForwardingFlags(); |
| 362 | flags.setCapture(true); |
| 363 | flags.setChildInherit(true); |
| 364 | parameters.setForwardingFlags(flags); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 365 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 366 | // run base method |
| 367 | return register(forwarder, parameters); |
| 368 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 369 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 370 | /** |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 371 | * Unregister a route on a forwarder; see |
| 372 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 373 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 374 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 375 | * (management requests are to /localhost/...) and that command signing has |
| 376 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 377 | * |
| 378 | * @param forwarder |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 379 | * @param controlParameters |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 380 | * @return |
| 381 | */ |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 382 | public static boolean unregister(Face forwarder, ControlParameters controlParameters) throws Exception { |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 383 | // build command name |
| 384 | Name command = new Name("/localhost/nfd/rib/unregister"); |
| 385 | command.append(controlParameters.wireEncode()); |
| 386 | |
| 387 | // send the interest |
| 388 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 389 | } |
| 390 | |
| 391 | /** |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 392 | * Unregister a route on a 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/RibMgmt for |
| 395 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 396 | * (management requests are to /localhost/...) and that command signing has |
| 397 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 398 | * |
| 399 | * @param forwarder |
| 400 | * @param route |
| 401 | * @return |
| 402 | */ |
| 403 | public static boolean unregister(Face forwarder, Name route) throws Exception { |
| 404 | // build command name |
| 405 | ControlParameters controlParameters = new ControlParameters(); |
| 406 | controlParameters.setName(route); |
| 407 | |
| 408 | // send the interest |
| 409 | return unregister(forwarder, controlParameters); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Unregister a route on a forwarder; see |
| 414 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 415 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 416 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 417 | * (management requests are to /localhost/...) and that command signing has |
| 418 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 419 | * |
| 420 | * @param forwarder |
| 421 | * @param route |
| 422 | * @param faceId |
| 423 | * @return |
| 424 | */ |
| 425 | public static boolean unregister(Face forwarder, Name route, int faceId) throws Exception { |
| 426 | // build command name |
| 427 | ControlParameters controlParameters = new ControlParameters(); |
| 428 | controlParameters.setName(route); |
| 429 | controlParameters.setFaceId(faceId); |
| 430 | |
| 431 | // send the interest |
| 432 | return unregister(forwarder, controlParameters); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Unregister a route on a forwarder; see |
| 437 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 438 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 439 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 440 | * (management requests are to /localhost/...) and that command signing has |
| 441 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 442 | * |
| 443 | * @param forwarder |
| 444 | * @param route |
| 445 | * @param uri |
| 446 | * @return |
| 447 | */ |
| 448 | public static boolean unregister(Face forwarder, Name route, String uri) throws Exception { |
| 449 | int faceId = -1; |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 450 | for (FaceStatus face : getFaceList(forwarder)) { |
| 451 | if (face.getUri().matches(uri)) { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 452 | faceId = face.getFaceId(); |
| 453 | break; |
| 454 | } |
| 455 | } |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 456 | |
| 457 | if (faceId == -1) { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 458 | throw new Exception("Face not found: " + uri); |
| 459 | } |
| 460 | |
| 461 | // send the interest |
| 462 | return unregister(forwarder, route, faceId); |
| 463 | } |
| 464 | |
| 465 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 466 | * Set a strategy on the forwarder; see |
| 467 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 468 | * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice |
| 469 | * for protocol documentation. Ensure the forwarding face is on the local |
| 470 | * machine (management requests are to /localhost/...) and that command |
| 471 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 472 | * |
| 473 | * @param forwarder Only a localhost Face |
| 474 | * @param prefix |
| 475 | * @param strategy |
| 476 | * @return true for successful command |
| 477 | * @throws Exception |
| 478 | */ |
| 479 | public static boolean setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception { |
| 480 | // build command name |
| 481 | Name command = new Name("/localhost/nfd/strategy-choice/set"); |
| 482 | ControlParameters parameters = new ControlParameters(); |
| 483 | parameters.setName(prefix); |
| 484 | parameters.setStrategy(strategy); |
| 485 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 486 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 487 | // send the interest |
| 488 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 489 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 490 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Send an interest as a command to the forwarder; this method will convert |
| 493 | * the interest to a command interest and block until a response is received |
| 494 | * from the forwarder. Ensure the forwarding face is on the local machine |
| 495 | * (management requests are to /localhost/...) and that command signing has |
| 496 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 497 | * |
| 498 | * @param forwarder Only a localhost Face |
| 499 | * @param interest As described at |
| 500 | * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the |
| 501 | * requested interest must have encoded ControlParameters appended to the |
| 502 | * interest name |
| 503 | * @return |
| 504 | * @throws net.named_data.jndn.security.SecurityException |
| 505 | * @throws java.io.IOException |
| 506 | * @throws net.named_data.jndn.encoding.EncodingException |
| 507 | */ |
| 508 | public static ControlResponse sendCommand(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException { |
| 509 | forwarder.makeCommandInterest(interest); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 510 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 511 | // send command packet |
| 512 | Data data = Client.getDefault().getSync(forwarder, interest); |
| 513 | if (data == null) { |
| 514 | throw new IOException("Failed to receive command response."); |
| 515 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 516 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 517 | // return response |
| 518 | ControlResponse response = new ControlResponse(); |
| 519 | response.wireDecode(data.getContent().buf()); |
| 520 | return response; |
| 521 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 522 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 523 | /** |
| 524 | * Send an interest as a command to the forwarder; this method will convert |
| 525 | * the interest to a command interest and block until a response is received |
| 526 | * from the forwarder. |
| 527 | * |
| 528 | * @param forwarder Only a localhost Face |
| 529 | * @param interest As described at |
| 530 | * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the |
| 531 | * requested interest must have encoded ControlParameters appended to the |
| 532 | * interest name |
| 533 | * @return |
| 534 | * @throws net.named_data.jndn.security.SecurityException |
| 535 | * @throws java.io.IOException |
| 536 | * @throws net.named_data.jndn.encoding.EncodingException |
| 537 | */ |
| 538 | public static boolean sendCommandAndErrorCheck(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException { |
| 539 | ControlResponse response = sendCommand(forwarder, interest); |
| 540 | if (response.getStatusCode() < 400) { |
| 541 | return true; |
| 542 | } else { |
| 543 | logger.warning("Command sent but failed: " + response.getStatusCode() + " " + response.getStatusText()); |
| 544 | return false; |
| 545 | } |
| 546 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 547 | } |