Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 1 | /* |
| 2 | * jndn-management |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2015-2018, Intel Corporation. |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 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. |
| 13 | */ |
| 14 | package com.intel.jndn.management; |
| 15 | |
| 16 | import com.intel.jndn.management.enums.RouteOrigin; |
| 17 | import com.intel.jndn.management.helpers.FetchHelper; |
| 18 | import com.intel.jndn.management.helpers.StatusDatasetHelper; |
Alexander Afanasyev | 7ac3e39 | 2016-02-19 23:21:01 -0800 | [diff] [blame] | 19 | import com.intel.jndn.management.types.ChannelStatus; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 20 | import com.intel.jndn.management.types.FaceStatus; |
| 21 | import com.intel.jndn.management.types.FibEntry; |
| 22 | import com.intel.jndn.management.types.ForwarderStatus; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 23 | import com.intel.jndn.management.types.RibEntry; |
| 24 | import com.intel.jndn.management.types.StrategyChoice; |
Alexander Afanasyev | 3c5ae7c | 2016-02-19 19:33:21 -0800 | [diff] [blame] | 25 | import net.named_data.jndn.ControlParameters; |
| 26 | import net.named_data.jndn.ControlResponse; |
| 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.KeyLocator; |
| 32 | import net.named_data.jndn.Name; |
| 33 | import net.named_data.jndn.encoding.EncodingException; |
| 34 | import net.named_data.jndn.security.SecurityException; |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 36 | import java.io.IOException; |
| 37 | import java.util.List; |
| 38 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Helper class for interacting with an NDN forwarder daemon; see |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 41 | * <a href="https://redmine.named-data.net/projects/nfd/wiki/Management">NFD Management</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 42 | * for explanations of the various protocols used. |
| 43 | * |
| 44 | * @author Andrew Brown <andrew.brown@intel.com> |
| 45 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 46 | public final class Nfdc { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 47 | private static final int OK_STATUS = 200; |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 48 | private static final int FACE_ALREADY_EXISTS = 409; |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 49 | |
| 50 | ///////////////////////////////////////////////////////////////////////////// |
| 51 | |
| 52 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 53 | * Prevent creation of Nfdc instances. |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 54 | */ |
| 55 | private Nfdc() { |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Retrieve the status of the given forwarder; calls /localhost/nfd/status/general |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 60 | * which requires a local Face (all non-local packets are dropped). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 61 | * |
| 62 | * @param face only a localhost Face |
| 63 | * @return the forwarder status object |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 64 | * @throws ManagementException if the network request failed or the returned status could not be decoded |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 65 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/ForwarderStatus">ForwarderStatus</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 66 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 67 | public static ForwarderStatus getForwarderStatus(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 68 | try { |
| 69 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/status/general")); |
| 70 | return new ForwarderStatus(StatusDatasetHelper.combine(segments)); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 71 | } catch (IOException | EncodingException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 72 | throw new ManagementException(e.getMessage(), e); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Retrieve a list of faces and their status from the given forwarder; calls |
| 78 | * /localhost/nfd/faces/list which requires a local Face (all non-local |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 79 | * packets are dropped). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 80 | * |
| 81 | * @param face only a localhost Face |
| 82 | * @return a list of face status objects |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 83 | * @throws ManagementException if the network request failed or if the NFD rejected the request |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 84 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt">FaceManagement</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 86 | public static List<FaceStatus> getFaceList(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 87 | try { |
| 88 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/faces/list")); |
| 89 | return StatusDatasetHelper.wireDecode(segments, FaceStatus.class); |
| 90 | } catch (IOException e) { |
| 91 | throw new ManagementException(e.getMessage(), e); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Retrieve a list of FIB entries and their NextHopRecords from the given |
| 97 | * forwarder; calls /localhost/nfd/fib/list which requires a local Face (all |
| 98 | * non-local packets are dropped). |
| 99 | * |
| 100 | * @param face only a localhost Face |
| 101 | * @return a list of FIB entries |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 102 | * @throws ManagementException if the network request failed or if the NFD rejected the request |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 103 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FibMgmt#FIB-Dataset">FIB Dataset</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 104 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 105 | public static List<FibEntry> getFibList(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 106 | try { |
| 107 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/fib/list")); |
| 108 | return StatusDatasetHelper.wireDecode(segments, FibEntry.class); |
| 109 | } catch (IOException e) { |
| 110 | throw new ManagementException(e.getMessage(), e); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 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 |
| 117 | * are dropped). |
| 118 | * |
| 119 | * @param face only a localhost Face |
| 120 | * @return a list of RIB entries, i.e. routes |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 121 | * @throws ManagementException if the network request failed or if the NFD rejected the request |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 122 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt#RIB-Dataset">RIB Dataset</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 124 | public static List<RibEntry> getRouteList(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 125 | try { |
| 126 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/rib/list")); |
| 127 | return StatusDatasetHelper.wireDecode(segments, RibEntry.class); |
Andrew Brown | 862fe42 | 2016-10-07 10:02:26 -0700 | [diff] [blame] | 128 | } catch (IOException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 129 | throw new ManagementException(e.getMessage(), e); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Retrieve the list of strategy choice entries from the NFD; calls |
| 135 | * /localhost/nfd/rib/list which requires a local Face (all non-local packets |
| 136 | * are dropped). |
| 137 | * |
| 138 | * @param face only a localhost Face |
| 139 | * @return a list of strategy choice entries, i.e. routes |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 140 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 141 | * the NFD rejected the request |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 142 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">StrategyChoice</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 143 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 144 | public static List<StrategyChoice> getStrategyList(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 145 | try { |
| 146 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/strategy-choice/list")); |
| 147 | return StatusDatasetHelper.wireDecode(segments, StrategyChoice.class); |
| 148 | } catch (IOException e) { |
| 149 | throw new ManagementException(e.getMessage(), e); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** |
Alexander Afanasyev | 7ac3e39 | 2016-02-19 23:21:01 -0800 | [diff] [blame] | 154 | * Retrieve the list of channel status entries from the NFD; calls |
| 155 | * /localhost/nfd/faces/channels which requires a local Face (all non-local packets |
| 156 | * are dropped). |
| 157 | * |
| 158 | * @param face only a localhost Face |
| 159 | * @return a list of channel status entries |
| 160 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 161 | * the NFD rejected the request |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 162 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#Channel-Dataset">Face Management</a> |
Alexander Afanasyev | 7ac3e39 | 2016-02-19 23:21:01 -0800 | [diff] [blame] | 163 | */ |
| 164 | public static List<ChannelStatus> getChannelStatusList(final Face face) throws ManagementException { |
| 165 | try { |
| 166 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/faces/channels")); |
| 167 | return StatusDatasetHelper.wireDecode(segments, ChannelStatus.class); |
| 168 | } catch (IOException e) { |
| 169 | throw new ManagementException(e.getMessage(), e); |
| 170 | } |
| 171 | } |
| 172 | /** |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 173 | * Retrieve the {@link KeyLocator} for an NFD. |
| 174 | * |
| 175 | * @param face only a localhost {@link Face} |
| 176 | * @return the {@link KeyLocator} of the NFD's key |
| 177 | * @throws ManagementException if the network request failed, if the NFD rejected the request, or no |
| 178 | * KeyLocator was found |
| 179 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 180 | public static KeyLocator getKeyLocator(final Face face) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 181 | try { |
| 182 | List<Data> segments = FetchHelper.getSegmentedData(face, new Name("/localhost/nfd/status/general")); |
| 183 | if (segments.isEmpty() || !KeyLocator.canGetFromSignature(segments.get(0).getSignature())) { |
| 184 | throw new ManagementException("No key locator available."); |
| 185 | } |
| 186 | return KeyLocator.getFromSignature(segments.get(0).getSignature()); |
| 187 | } catch (IOException e) { |
| 188 | throw new ManagementException(e.getMessage(), e); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Create a new face on the given forwarder. Ensure the forwarding face is on |
| 194 | * the local machine (management requests are to /localhost/...) and that |
| 195 | * command signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 196 | * |
| 197 | * @param face only a localhost {@link Face} |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 198 | * @param uri a string like "tcp4://host.name.com" (see nfd-status channels |
| 199 | * for more protocol options) |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 200 | * @return the newly created face ID |
| 201 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 202 | * the NFD rejected the request |
| 203 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 204 | public static int createFace(final Face face, final String uri) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 205 | Name command = new Name("/localhost/nfd/faces/create"); |
| 206 | ControlParameters parameters = new ControlParameters(); |
| 207 | parameters.setUri(uri); |
| 208 | command.append(parameters.wireEncode()); |
| 209 | |
| 210 | try { |
| 211 | // send the interest |
| 212 | ControlResponse response = sendCommand(face, command); |
| 213 | |
| 214 | // return |
Alexander Afanasyev | 499aced | 2016-02-17 19:32:37 -0800 | [diff] [blame] | 215 | return response.getBodyAsControlParameters().getFaceId(); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 216 | } catch (IOException | EncodingException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 217 | throw new ManagementException(e.getMessage(), e); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Destroy a face on given forwarder. Ensure the forwarding face is on the |
| 223 | * local machine (management requests are to /localhost/...) and that command |
| 224 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 225 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 226 | * @param face only a localhost {@link Face} |
| 227 | * @param faceId the ID of the face to destroy |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 228 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 229 | * the NFD rejected the request |
| 230 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 231 | public static void destroyFace(final Face face, final int faceId) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 232 | Name command = new Name("/localhost/nfd/faces/destroy"); |
| 233 | ControlParameters parameters = new ControlParameters(); |
| 234 | parameters.setFaceId(faceId); |
| 235 | command.append(parameters.wireEncode()); |
| 236 | |
| 237 | try { |
| 238 | sendCommand(face, command); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 239 | } catch (IOException | EncodingException | ManagementException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 240 | throw new ManagementException(e.getMessage(), e); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 245 | * Register a route on the forwarder. |
| 246 | * <p/> |
| 247 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 248 | * signing has been set up (e.g. forwarder.setCommandSigningInfo()). |
| 249 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 250 | * @param face only a localhost {@link Face} |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 251 | * @param controlParameters the {@link ControlParameters} command options |
| 252 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 253 | * the NFD rejected the request |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 254 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 255 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 256 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 257 | public static void register(final Face face, final ControlParameters controlParameters) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 258 | // build command name |
| 259 | Name command = new Name("/localhost/nfd/rib/register"); |
| 260 | command.append(controlParameters.wireEncode()); |
| 261 | |
| 262 | try { |
| 263 | sendCommand(face, command); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 264 | } catch (IOException | EncodingException | ManagementException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 265 | throw new ManagementException(e.getMessage(), e); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Register a route on a forwarder; this will create a new face on the |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 271 | * forwarder towards the face (e.g., self registration). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 272 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 273 | * @param face only a localhost {@link Face} |
| 274 | * @param route the {@link Name} prefix of the route |
| 275 | * @param cost the numeric cost of forwarding along the route |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 276 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 277 | * the NFD rejected the request |
| 278 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 279 | public static void register(final Face face, final Name route, final int cost) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 280 | ForwardingFlags flags = new ForwardingFlags(); |
| 281 | flags.setCapture(false); |
| 282 | flags.setChildInherit(true); |
| 283 | |
| 284 | register(face, new ControlParameters() |
| 285 | .setName(route) |
| 286 | .setCost(cost) |
| 287 | .setOrigin(RouteOrigin.APP.toInteger()) |
| 288 | .setForwardingFlags(flags)); |
| 289 | } |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 290 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 291 | /** |
| 292 | * Register a route on a forwarder; this will create a new face on the |
| 293 | * forwarder to the given URI/route pair. See register(Face, |
| 294 | * ControlParameters) for more detailed documentation. |
| 295 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 296 | * @param face only a localhost {@link Face} |
| 297 | * @param uri the URI (e.g. "tcp4://10.10.2.2:6363") of the remote node; note |
| 298 | * that this must be one of the canonical forms described in the wiki |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 299 | * (https://redmine.named-data.net/projects/nfd/wiki/FaceMgmt#TCP) for NFD to |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 300 | * accept the registration--otherwise you will see 400 errors |
| 301 | * @param route the {@link Name} prefix of the route |
| 302 | * @param cost the numeric cost of forwarding along the route |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 303 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 304 | * the NFD rejected the request |
| 305 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 306 | public static void register(final Face face, final String uri, final Name route, final int cost) throws |
| 307 | ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 308 | // create the new face |
| 309 | int faceId = createFace(face, uri); |
| 310 | |
| 311 | // run base method |
| 312 | register(face, faceId, route, cost); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Register a route on a forwarder; this will not create a new face since it |
| 317 | * is provided a faceId. See register(Face, ControlParameters) for full |
| 318 | * documentation. |
| 319 | * |
| 320 | * @param forwarder only a localhost {@link Face} |
| 321 | * @param faceId the ID of the {@link Face} to assign to the route |
| 322 | * @param route the {@link Name} prefix of the route |
| 323 | * @param cost the numeric cost of forwarding along the route |
| 324 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 325 | * the NFD rejected the request |
| 326 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 327 | public static void register(final Face forwarder, final int faceId, final Name route, final int cost) throws |
| 328 | ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 329 | // build command name |
| 330 | ControlParameters parameters = new ControlParameters(); |
| 331 | parameters.setName(route); |
| 332 | parameters.setFaceId(faceId); |
| 333 | parameters.setCost(cost); |
| 334 | parameters.setOrigin(RouteOrigin.STATIC.toInteger()); |
| 335 | ForwardingFlags flags = new ForwardingFlags(); |
| 336 | flags.setCapture(false); |
| 337 | flags.setChildInherit(true); |
| 338 | parameters.setForwardingFlags(flags); |
| 339 | |
| 340 | // run base method |
| 341 | register(forwarder, parameters); |
| 342 | } |
| 343 | |
| 344 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 345 | * Unregister a route on a forwarder |
| 346 | * <p/> |
| 347 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
| 348 | * signing has been set up (e.g. forwarder.setCommandSigningInfo(). |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 349 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 350 | * @param face only a localhost {@link Face} |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 351 | * @param controlParameters the {@link ControlParameters} command options |
| 352 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 353 | * the NFD rejected the request |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 354 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 355 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 356 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 357 | public static void unregister(final Face face, final ControlParameters controlParameters) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 358 | // build command name |
| 359 | Name command = new Name("/localhost/nfd/rib/unregister"); |
| 360 | command.append(controlParameters.wireEncode()); |
| 361 | |
| 362 | try { |
| 363 | sendCommand(face, command); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 364 | } catch (IOException | EncodingException | ManagementException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 365 | throw new ManagementException(e.getMessage(), e); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /** |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 370 | * Unregister a route on a forwarder. |
| 371 | * <p/> |
| 372 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 373 | * signing has been set up (e.g. forwarder.setCommandSigningInfo(). |
| 374 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 375 | * @param face only a localhost {@link Face} |
| 376 | * @param route the {@link Name} prefix of the route |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 377 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 378 | * the NFD rejected the request |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 379 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 380 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 381 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 382 | public static void unregister(final Face face, final Name route) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 383 | // build command name |
| 384 | ControlParameters controlParameters = new ControlParameters(); |
| 385 | controlParameters.setName(route); |
| 386 | |
| 387 | // send the interest |
| 388 | unregister(face, controlParameters); |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Unregister a route on a forwarder; see |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 393 | * <p/> |
| 394 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 395 | * signing has been set up (e.g. forwarder.setCommandSigningInfo(). |
| 396 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 397 | * @param face only a localhost {@link Face} |
| 398 | * @param route the {@link Name} prefix of the route |
| 399 | * @param faceId the specific ID of the face to remove (more than one face can |
| 400 | * be registered to a route) |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 401 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 402 | * the NFD rejected the request |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 403 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 404 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RIB Management</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 405 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 406 | public static void unregister(final Face face, final Name route, final int faceId) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 407 | // build command name |
| 408 | ControlParameters controlParameters = new ControlParameters(); |
| 409 | controlParameters.setName(route); |
| 410 | controlParameters.setFaceId(faceId); |
| 411 | |
| 412 | // send the interest |
| 413 | unregister(face, controlParameters); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * Unregister a route on a forwarder |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 418 | * <p/> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 419 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
| 420 | * signing has been set up using forwarder.setCommandSigningInfo(). |
| 421 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 422 | * @param face only a localhost {@link Face} |
| 423 | * @param route the {@link Name} prefix of the route |
| 424 | * @param uri the URI (e.g. "tcp4://some.host.com") of the remote node (more |
| 425 | * than one face can be registered to a route) |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 426 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 427 | * the NFD rejected the request |
| 428 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc command-line usage</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 429 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/RibMgmt">RibMgmt</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 430 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 431 | public static void unregister(final Face face, final Name route, final String uri) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 432 | int faceId = -1; |
| 433 | for (FaceStatus faceStatus : getFaceList(face)) { |
| 434 | if (faceStatus.getRemoteUri().matches(uri)) { |
| 435 | faceId = faceStatus.getFaceId(); |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | if (faceId == -1) { |
| 441 | throw new ManagementException("Face not found: " + uri); |
| 442 | } |
| 443 | |
| 444 | // send the interest |
| 445 | unregister(face, route, faceId); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Set a strategy on the forwarder |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 450 | * <p/> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 451 | * Ensure the forwarding face is on the local machine (management requests are to /localhost/...) and that command |
| 452 | * signing has been set up using forwarder.setCommandSigningInfo(). |
| 453 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 454 | * @param face only a localhost {@link Face} |
| 455 | * @param prefix the {@link Name} prefix |
| 456 | * @param strategy the {@link Name} of the strategy to set, e.g. |
| 457 | * /localhost/nfd/strategy/broadcast |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 458 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 459 | * the NFD rejected the request |
| 460 | * @see <a href="http://named-data.net/doc/NFD/current/manpages/nfdc.html">nfdc command-line usage</a> |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 461 | * @see <a href="https://redmine.named-data.net/projects/nfd/wiki/StrategyChoice">StrategyChoice</a> |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 462 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 463 | public static void setStrategy(final Face face, final Name prefix, final Name strategy) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 464 | // build command name |
| 465 | Name command = new Name("/localhost/nfd/strategy-choice/set"); |
| 466 | ControlParameters parameters = new ControlParameters(); |
| 467 | parameters.setName(prefix); |
| 468 | parameters.setStrategy(strategy); |
| 469 | command.append(parameters.wireEncode()); |
| 470 | |
| 471 | try { |
| 472 | sendCommand(face, command); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 473 | } catch (IOException | EncodingException | ManagementException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 474 | throw new ManagementException(e.getMessage(), e); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Set a strategy on the forwarder; see |
| 480 | * {@link #setStrategy(net.named_data.jndn.Face, net.named_data.jndn.Name, net.named_data.jndn.Name)} |
| 481 | * for more information. Ensure the forwarding face is on the local machine |
| 482 | * (management requests are to /localhost/...) and that command signing has |
| 483 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 484 | * |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 485 | * @param face only a localhost {@link Face} |
| 486 | * @param prefix the {@link Name} prefix |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 487 | * @throws ManagementException if the network request failed, the NFD response could not be decoded, or |
| 488 | * the NFD rejected the request |
| 489 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 490 | public static void unsetStrategy(final Face face, final Name prefix) throws ManagementException { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 491 | // build command name |
| 492 | Name command = new Name("/localhost/nfd/strategy-choice/unset"); |
| 493 | ControlParameters parameters = new ControlParameters(); |
| 494 | parameters.setName(prefix); |
| 495 | command.append(parameters.wireEncode()); |
| 496 | |
| 497 | try { |
| 498 | sendCommand(face, command); |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 499 | } catch (IOException | EncodingException | ManagementException e) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 500 | throw new ManagementException(e.getMessage(), e); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | /** |
| 505 | * Send an interest as a command to the forwarder; this method will convert |
| 506 | * the interest to a command interest and block until a response is received |
| 507 | * from the forwarder. Ensure the forwarding face is on the local machine |
| 508 | * (management requests are to /localhost/...) and that command signing has |
| 509 | * been set up (e.g. forwarder.setCommandSigningInfo()). |
| 510 | * |
| 511 | * @param face only a localhost Face, command signing info must be set |
| 512 | * @param name As described at |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 513 | * <a href="https://redmine.named-data.net/projects/nfd/wiki/ControlCommand">ControlCommand</a>, |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 514 | * the requested interest must have encoded ControlParameters appended to the |
| 515 | * interest name |
| 516 | * @return a {@link ControlResponse} |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 517 | * @throws IOException if the network request failed |
| 518 | * @throws EncodingException if the NFD response could not be decoded |
| 519 | * @throws ManagementException if the NFD rejected the request |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 520 | */ |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 521 | private static ControlResponse sendCommand(final Face face, final Name name) throws IOException, EncodingException, |
| 522 | ManagementException { |
Andrew Brown | 5a8b7c4 | 2016-10-06 09:54:36 -0700 | [diff] [blame] | 523 | if (face == null) { |
| 524 | throw new IllegalArgumentException("Face parameter is null."); |
| 525 | } |
| 526 | |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 527 | Interest interest = new Interest(name); |
| 528 | |
| 529 | // forwarder must have command signing info set |
| 530 | try { |
| 531 | face.makeCommandInterest(interest); |
Andrew Brown | 5a8b7c4 | 2016-10-06 09:54:36 -0700 | [diff] [blame] | 532 | } catch (SecurityException e) { |
Alexander Afanasyev | e36e1af | 2016-02-19 18:06:05 -0800 | [diff] [blame] | 533 | throw new IllegalArgumentException("Failed to make command interest; ensure command signing info is set on the " + |
| 534 | "face.", e); |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | // send command packet |
| 538 | Data data = FetchHelper.getData(face, interest.getName()); |
| 539 | |
| 540 | // decode response |
| 541 | ControlResponse response = new ControlResponse(); |
| 542 | response.wireDecode(data.getContent().buf()); |
| 543 | |
| 544 | // check response for success |
Alexander Afanasyev | 60f8f8e | 2018-07-25 13:24:19 -0400 | [diff] [blame^] | 545 | if (response.getStatusCode() != OK_STATUS && response.getStatusCode() != FACE_ALREADY_EXISTS) { |
Alexander Afanasyev | a8bc0d8 | 2016-01-25 17:25:30 -0800 | [diff] [blame] | 546 | throw ManagementException.fromResponse(response); |
| 547 | } |
| 548 | |
| 549 | return response; |
| 550 | } |
| 551 | } |