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 | /** |
andrewsbrown | e8e8e85 | 2015-03-09 13:48:31 -0700 | [diff] [blame^] | 236 | * Enable a local control feature on the given forwarder. See |
| 237 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Enable-a-LocalControlHeader-feature |
| 238 | * |
| 239 | * @param forwarder |
| 240 | * @param header |
| 241 | * @return |
| 242 | * @throws Exception |
| 243 | */ |
| 244 | public static boolean enableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
| 245 | // build command name |
| 246 | Name command = new Name("/localhost/nfd/faces/enable-local-control"); |
| 247 | ControlParameters parameters = new ControlParameters(); |
| 248 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 249 | command.append(parameters.wireEncode()); |
| 250 | |
| 251 | // send command and return |
| 252 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Disable a local control feature on the given forwarder. See |
| 257 | * http://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Disable-a-LocalControlHeader-feature |
| 258 | * |
| 259 | * @param forwarder |
| 260 | * @param header |
| 261 | * @return |
| 262 | * @throws Exception |
| 263 | */ |
| 264 | public static boolean disableLocalControlHeader(Face forwarder, LocalControlHeader header) throws Exception { |
| 265 | // build command name |
| 266 | Name command = new Name("/localhost/nfd/faces/disable-local-control"); |
| 267 | ControlParameters parameters = new ControlParameters(); |
| 268 | parameters.setLocalControlFeature(header.getNumericValue()); |
| 269 | command.append(parameters.wireEncode()); |
| 270 | |
| 271 | // send command and return |
| 272 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 273 | } |
| 274 | |
| 275 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 276 | * Register a route on the forwarder; see |
| 277 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 278 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 279 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 280 | * (management requests are to /localhost/...) and that command signing has |
| 281 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 282 | * |
| 283 | * @param forwarder Only a localhost Face |
| 284 | * @param controlParameters |
| 285 | * @return |
| 286 | * @throws Exception |
| 287 | */ |
| 288 | public static boolean register(Face forwarder, ControlParameters controlParameters) throws Exception { |
| 289 | // build command name |
| 290 | Name command = new Name("/localhost/nfd/rib/register"); |
| 291 | command.append(controlParameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 292 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 293 | // send the interest |
| 294 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 295 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 296 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 297 | /** |
| 298 | * Register a route on a forwarder; this will create a new face on the |
| 299 | * forwarder to the given URI/route pair. See register(Face, |
| 300 | * ControlParameters) for more details documentation. |
| 301 | * |
| 302 | * @param forwarder Only a localhost Face |
| 303 | * @param uri |
| 304 | * @param cost |
| 305 | * @param route |
| 306 | * @return true for successful registration |
| 307 | * @throws java.lang.Exception |
| 308 | */ |
| 309 | public static boolean register(Face forwarder, String uri, Name route, int cost) throws Exception { |
| 310 | // create the new face |
| 311 | int faceId = createFace(forwarder, uri); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 312 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 313 | // run base method |
| 314 | return register(forwarder, faceId, route, cost); |
| 315 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 316 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 317 | /** |
| 318 | * Register a route on a forwarder; this will not create a new face since it |
| 319 | * is provided a faceId. See register(Face, ControlParameters) for full |
| 320 | * documentation |
| 321 | * |
| 322 | * @param forwarder Only a localhost Face |
| 323 | * @param faceId |
| 324 | * @param route |
| 325 | * @param cost |
| 326 | * @return true for successful registration |
| 327 | * @throws java.lang.Exception |
| 328 | */ |
| 329 | public static boolean register(Face forwarder, int faceId, Name route, int cost) throws Exception { |
| 330 | // build command name |
| 331 | ControlParameters parameters = new ControlParameters(); |
| 332 | parameters.setName(route); |
| 333 | parameters.setFaceId(faceId); |
| 334 | parameters.setCost(cost); |
| 335 | ForwardingFlags flags = new ForwardingFlags(); |
| 336 | flags.setCapture(true); |
| 337 | flags.setChildInherit(true); |
| 338 | parameters.setForwardingFlags(flags); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 339 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 340 | // run base method |
| 341 | return register(forwarder, parameters); |
| 342 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 343 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 344 | /** |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 345 | * Unregister a route on a forwarder; see |
| 346 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 347 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 348 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 349 | * (management requests are to /localhost/...) and that command signing has |
| 350 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 351 | * |
| 352 | * @param forwarder |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 353 | * @param controlParameters |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 354 | * @return |
| 355 | */ |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 356 | public static boolean unregister(Face forwarder, ControlParameters controlParameters) throws Exception { |
Andrew Brown | 63bed36 | 2015-02-18 11:28:50 -0800 | [diff] [blame] | 357 | // build command name |
| 358 | Name command = new Name("/localhost/nfd/rib/unregister"); |
| 359 | command.append(controlParameters.wireEncode()); |
| 360 | |
| 361 | // send the interest |
| 362 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 363 | } |
| 364 | |
| 365 | /** |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 366 | * Unregister a route on a forwarder; see |
| 367 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 368 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 369 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 370 | * (management requests are to /localhost/...) and that command signing has |
| 371 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 372 | * |
| 373 | * @param forwarder |
| 374 | * @param route |
| 375 | * @return |
| 376 | */ |
| 377 | public static boolean unregister(Face forwarder, Name route) throws Exception { |
| 378 | // build command name |
| 379 | ControlParameters controlParameters = new ControlParameters(); |
| 380 | controlParameters.setName(route); |
| 381 | |
| 382 | // send the interest |
| 383 | return unregister(forwarder, controlParameters); |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * Unregister a route on a forwarder; see |
| 388 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 389 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 390 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 391 | * (management requests are to /localhost/...) and that command signing has |
| 392 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 393 | * |
| 394 | * @param forwarder |
| 395 | * @param route |
| 396 | * @param faceId |
| 397 | * @return |
| 398 | */ |
| 399 | public static boolean unregister(Face forwarder, Name route, int faceId) throws Exception { |
| 400 | // build command name |
| 401 | ControlParameters controlParameters = new ControlParameters(); |
| 402 | controlParameters.setName(route); |
| 403 | controlParameters.setFaceId(faceId); |
| 404 | |
| 405 | // send the interest |
| 406 | return unregister(forwarder, controlParameters); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * Unregister a route on a forwarder; see |
| 411 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 412 | * usage and http://redmine.named-data.net/projects/nfd/wiki/RibMgmt for |
| 413 | * protocol documentation. Ensure the forwarding face is on the local machine |
| 414 | * (management requests are to /localhost/...) and that command signing has |
| 415 | * been set up (e.g. forwarder.setCommandSigningInfo() |
| 416 | * |
| 417 | * @param forwarder |
| 418 | * @param route |
| 419 | * @param uri |
| 420 | * @return |
| 421 | */ |
| 422 | public static boolean unregister(Face forwarder, Name route, String uri) throws Exception { |
| 423 | int faceId = -1; |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 424 | for (FaceStatus face : getFaceList(forwarder)) { |
| 425 | if (face.getUri().matches(uri)) { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 426 | faceId = face.getFaceId(); |
| 427 | break; |
| 428 | } |
| 429 | } |
andrewsbrown | 4c21ea2 | 2015-03-09 12:05:03 -0700 | [diff] [blame] | 430 | |
| 431 | if (faceId == -1) { |
Andrew Brown | 0746644 | 2015-02-24 09:07:02 -0800 | [diff] [blame] | 432 | throw new Exception("Face not found: " + uri); |
| 433 | } |
| 434 | |
| 435 | // send the interest |
| 436 | return unregister(forwarder, route, faceId); |
| 437 | } |
| 438 | |
| 439 | /** |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 440 | * Set a strategy on the forwarder; see |
| 441 | * http://named-data.net/doc/NFD/current/manpages/nfdc.html for command-line |
| 442 | * usage and http://redmine.named-data.net/projects/nfd/wiki/StrategyChoice |
| 443 | * for protocol documentation. Ensure the forwarding face is on the local |
| 444 | * machine (management requests are to /localhost/...) and that command |
| 445 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 446 | * |
| 447 | * @param forwarder Only a localhost Face |
| 448 | * @param prefix |
| 449 | * @param strategy |
| 450 | * @return true for successful command |
| 451 | * @throws Exception |
| 452 | */ |
| 453 | public static boolean setStrategy(Face forwarder, Name prefix, Name strategy) throws Exception { |
| 454 | // build command name |
| 455 | Name command = new Name("/localhost/nfd/strategy-choice/set"); |
| 456 | ControlParameters parameters = new ControlParameters(); |
| 457 | parameters.setName(prefix); |
| 458 | parameters.setStrategy(strategy); |
| 459 | command.append(parameters.wireEncode()); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 460 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 461 | // send the interest |
| 462 | return sendCommandAndErrorCheck(forwarder, new Interest(command)); |
| 463 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 464 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 465 | /** |
| 466 | * Send an interest as a command to the forwarder; this method will convert |
| 467 | * the interest to a command interest and block until a response is received |
| 468 | * from the forwarder. Ensure the forwarding face is on the local machine |
| 469 | * (management requests are to /localhost/...) and that command signing has |
| 470 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 471 | * |
| 472 | * @param forwarder Only a localhost Face |
| 473 | * @param interest As described at |
| 474 | * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the |
| 475 | * requested interest must have encoded ControlParameters appended to the |
| 476 | * interest name |
| 477 | * @return |
| 478 | * @throws net.named_data.jndn.security.SecurityException |
| 479 | * @throws java.io.IOException |
| 480 | * @throws net.named_data.jndn.encoding.EncodingException |
| 481 | */ |
| 482 | public static ControlResponse sendCommand(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException { |
| 483 | forwarder.makeCommandInterest(interest); |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 484 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 485 | // send command packet |
| 486 | Data data = Client.getDefault().getSync(forwarder, interest); |
| 487 | if (data == null) { |
| 488 | throw new IOException("Failed to receive command response."); |
| 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 | // return response |
| 492 | ControlResponse response = new ControlResponse(); |
| 493 | response.wireDecode(data.getContent().buf()); |
| 494 | return response; |
| 495 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 496 | |
Andrew Brown | 211d2b6 | 2015-02-18 11:12:02 -0800 | [diff] [blame] | 497 | /** |
| 498 | * Send an interest as a command to the forwarder; this method will convert |
| 499 | * the interest to a command interest and block until a response is received |
| 500 | * from the forwarder. |
| 501 | * |
| 502 | * @param forwarder Only a localhost Face |
| 503 | * @param interest As described at |
| 504 | * http://redmine.named-data.net/projects/nfd/wiki/ControlCommand, the |
| 505 | * requested interest must have encoded ControlParameters appended to the |
| 506 | * interest name |
| 507 | * @return |
| 508 | * @throws net.named_data.jndn.security.SecurityException |
| 509 | * @throws java.io.IOException |
| 510 | * @throws net.named_data.jndn.encoding.EncodingException |
| 511 | */ |
| 512 | public static boolean sendCommandAndErrorCheck(Face forwarder, Interest interest) throws SecurityException, IOException, EncodingException { |
| 513 | ControlResponse response = sendCommand(forwarder, interest); |
| 514 | if (response.getStatusCode() < 400) { |
| 515 | return true; |
| 516 | } else { |
| 517 | logger.warning("Command sent but failed: " + response.getStatusCode() + " " + response.getStatusText()); |
| 518 | return false; |
| 519 | } |
| 520 | } |
Andrew Brown | 2f1fdbf | 2015-01-21 10:52:29 -0800 | [diff] [blame] | 521 | } |