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