Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 3 | * * Copyright 2016-present Open Networking Laboratory |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 4 | * * |
| 5 | * * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * * you may not use this file except in compliance with the License. |
| 7 | * * You may obtain a copy of the License at |
| 8 | * * |
| 9 | * * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * * |
| 11 | * * Unless required by applicable law or agreed to in writing, software |
| 12 | * * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * * See the License for the specific language governing permissions and |
| 15 | * * limitations under the License. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | package org.onosproject.drivers.cisco; |
| 20 | |
| 21 | import org.onlab.packet.VlanId; |
| 22 | import org.onosproject.drivers.utilities.XmlConfigParser; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.behaviour.InterfaceConfig; |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 25 | import org.onosproject.net.device.DeviceInterfaceDescription; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 26 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 27 | import org.onosproject.netconf.NetconfController; |
| 28 | import org.onosproject.netconf.NetconfException; |
| 29 | import org.onosproject.netconf.NetconfSession; |
| 30 | import org.slf4j.Logger; |
| 31 | |
| 32 | import java.io.ByteArrayInputStream; |
| 33 | import java.nio.charset.StandardCharsets; |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 34 | import java.util.List; |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 35 | |
| 36 | import static com.google.common.base.Preconditions.checkNotNull; |
| 37 | import static org.slf4j.LoggerFactory.getLogger; |
| 38 | |
| 39 | /** |
| 40 | * Configures interfaces on Cisco IOS devices. |
| 41 | */ |
| 42 | public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour |
| 43 | implements InterfaceConfig { |
| 44 | |
| 45 | private final Logger log = getLogger(getClass()); |
| 46 | |
| 47 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 48 | * Adds an access interface to a VLAN. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 49 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 50 | * @param deviceId the device ID |
| 51 | * @param intf the name of the interface |
| 52 | * @param vlanId the VLAN ID |
| 53 | * @return the result of operation |
| 54 | */ |
| 55 | @Override |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 56 | public boolean addAccessInterface(DeviceId deviceId, String intf, VlanId vlanId) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 57 | return addAccessMode(intf, vlanId); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Adds an access interface to a VLAN. |
| 62 | * |
| 63 | * @param intf the name of the interface |
| 64 | * @param vlanId the VLAN ID |
| 65 | * @return the result of operation |
| 66 | */ |
| 67 | @Override |
| 68 | public boolean addAccessMode(String intf, VlanId vlanId) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 69 | NetconfController controller = checkNotNull(handler() |
| 70 | .get(NetconfController.class)); |
| 71 | |
| 72 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 73 | .data().deviceId()).getSession(); |
| 74 | String reply; |
| 75 | try { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 76 | reply = session.requestSync(addAccessModeBuilder(intf, vlanId)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 77 | } catch (NetconfException e) { |
| 78 | log.error("Failed to configure VLAN ID {} on device {} interface {}.", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 79 | vlanId, handler().data().deviceId(), intf, e); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 84 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 85 | } |
| 86 | |
| 87 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 88 | * Builds a request to add an access interface to a VLAN. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 89 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 90 | * @param intf the name of the interface |
| 91 | * @param vlanId the VLAN ID |
| 92 | * @return the request string. |
| 93 | */ |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 94 | private String addAccessModeBuilder(String intf, VlanId vlanId) { |
| 95 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 96 | rpc.append("<switchport><access><vlan><VLANIDVLANPortAccessMode>"); |
| 97 | rpc.append(vlanId); |
| 98 | rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>"); |
| 99 | rpc.append("<switchport><mode><access/></mode></switchport>"); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 100 | rpc.append(getClosingString()); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 101 | |
| 102 | return rpc.toString(); |
| 103 | } |
| 104 | |
| 105 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 106 | * Removes an access interface to a VLAN. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 107 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 108 | * @param deviceId the device ID |
| 109 | * @param intf the name of the interface |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 110 | * @return the result of operation |
| 111 | */ |
| 112 | @Override |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 113 | public boolean removeAccessInterface(DeviceId deviceId, String intf) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 114 | return removeAccessMode(intf); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Removes an access interface to a VLAN. |
| 119 | * |
| 120 | * @param intf the name of the interface |
| 121 | * @return the result of operation |
| 122 | */ |
| 123 | @Override |
| 124 | public boolean removeAccessMode(String intf) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 125 | NetconfController controller = checkNotNull(handler() |
| 126 | .get(NetconfController.class)); |
| 127 | |
| 128 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 129 | .data().deviceId()).getSession(); |
| 130 | String reply; |
| 131 | try { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 132 | reply = session.requestSync(removeAccessModeBuilder(intf)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 133 | } catch (NetconfException e) { |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 134 | log.error("Failed to remove access mode from device {} interface {}.", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 135 | handler().data().deviceId(), intf, e); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | |
| 139 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 140 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 141 | } |
| 142 | |
| 143 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 144 | * Builds a request to remove an access interface from a VLAN. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 145 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 146 | * @param intf the name of the interface |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 147 | * @return the request string. |
| 148 | */ |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 149 | private String removeAccessModeBuilder(String intf) { |
| 150 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 151 | rpc.append("<switchport operation=\"delete\"><access><vlan><VLANIDVLANPortAccessMode>"); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 152 | rpc.append("</VLANIDVLANPortAccessMode></vlan></access></switchport>"); |
| 153 | rpc.append("<switchport operation=\"delete\"><mode><access/></mode></switchport>"); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 154 | rpc.append(getClosingString()); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 155 | |
| 156 | return rpc.toString(); |
| 157 | } |
| 158 | |
| 159 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 160 | * Adds a trunk interface for VLANs. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 161 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 162 | * @param deviceId the device ID |
| 163 | * @param intf the name of the interface |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 164 | * @param vlanIds the VLAN IDs |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 165 | * @return the result of operation |
| 166 | */ |
| 167 | @Override |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 168 | public boolean addTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 169 | return addTrunkMode(intf, vlanIds); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Adds a trunk interface for VLANs. |
| 174 | * |
| 175 | * @param intf the name of the interface |
| 176 | * @param vlanIds the VLAN IDs |
| 177 | * @return the result of operation |
| 178 | */ |
| 179 | @Override |
| 180 | public boolean addTrunkMode(String intf, List<VlanId> vlanIds) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 181 | NetconfController controller = checkNotNull(handler() |
| 182 | .get(NetconfController.class)); |
| 183 | |
| 184 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 185 | .data().deviceId()).getSession(); |
| 186 | String reply; |
| 187 | try { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 188 | reply = session.requestSync(addTrunkModeBuilder(intf, vlanIds)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 189 | } catch (NetconfException e) { |
| 190 | log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 191 | vlanIds, handler().data().deviceId(), intf, e); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | |
| 195 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 196 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 197 | } |
| 198 | |
| 199 | /** |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 200 | * Builds a request to configure an interface as trunk for VLANs. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 201 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 202 | * @param intf the name of the interface |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 203 | * @param vlanIds the VLAN IDs |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 204 | * @return the request string. |
| 205 | */ |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 206 | private String addTrunkModeBuilder(String intf, List<VlanId> vlanIds) { |
| 207 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 208 | rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>"); |
| 209 | rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>"); |
| 210 | rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>"); |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 211 | rpc.append(getVlansString(vlanIds)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 212 | rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>"); |
| 213 | rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>"); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 214 | rpc.append(getClosingString()); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 215 | |
| 216 | return rpc.toString(); |
| 217 | } |
| 218 | |
| 219 | /** |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 220 | * Removes trunk mode configuration from an interface. |
| 221 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 222 | * @param deviceId the device ID |
| 223 | * @param intf the name of the interface |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 224 | * @return the result of operation |
| 225 | */ |
| 226 | @Override |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 227 | public boolean removeTrunkInterface(DeviceId deviceId, String intf) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 228 | return removeTrunkMode(intf); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Removes trunk mode configuration from an interface. |
| 233 | * |
| 234 | * @param intf the name of the interface |
| 235 | * @return the result of operation |
| 236 | */ |
| 237 | @Override |
| 238 | public boolean removeTrunkMode(String intf) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 239 | NetconfController controller = checkNotNull(handler() |
| 240 | .get(NetconfController.class)); |
| 241 | |
| 242 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 243 | .data().deviceId()).getSession(); |
| 244 | String reply; |
| 245 | try { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 246 | reply = session.requestSync(removeTrunkModeBuilder(intf)); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 247 | } catch (NetconfException e) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 248 | log.error("Failed to remove trunk mode from device {} interface {}.", |
| 249 | handler().data().deviceId(), intf, e); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
| 253 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 254 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 255 | } |
| 256 | |
| 257 | /** |
Andreas Papazois | 827d8d0 | 2016-04-15 11:53:16 +0300 | [diff] [blame] | 258 | * Builds a request to remove trunk mode configuration from an interface. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 259 | * |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 260 | * @param intf the name of the interface |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 261 | * @return the request string. |
| 262 | */ |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 263 | private String removeTrunkModeBuilder(String intf) { |
| 264 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
| 265 | rpc.append("<switchport><mode operation=\"delete\"><trunk/></mode></switchport>"); |
| 266 | rpc.append("<switchport><trunk operation=\"delete\"><encapsulation>"); |
| 267 | rpc.append("<dot1q/></encapsulation></trunk></switchport>"); |
| 268 | rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>"); |
| 269 | rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>"); |
| 270 | rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>"); |
| 271 | rpc.append("</trunk></switchport>"); |
| 272 | rpc.append(getClosingString()); |
| 273 | |
| 274 | return rpc.toString(); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Adds a rate limit on an interface. |
| 279 | * |
| 280 | * @param intf the name of the interface |
| 281 | * @param limit the limit as a percentage |
| 282 | * @return the result of operation |
| 283 | */ |
| 284 | @Override |
| 285 | public boolean addRateLimit(String intf, short limit) { |
| 286 | NetconfController controller = checkNotNull(handler() |
| 287 | .get(NetconfController.class)); |
| 288 | |
| 289 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 290 | .data().deviceId()).getSession(); |
| 291 | String reply; |
| 292 | try { |
| 293 | reply = session.requestSync(addRateLimitBuilder(intf, limit)); |
| 294 | } catch (NetconfException e) { |
| 295 | log.error("Failed to configure rate limit {}%% on device {} interface {}.", |
| 296 | limit, handler().data().deviceId(), intf, e); |
| 297 | return false; |
| 298 | } |
| 299 | |
| 300 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 301 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Builds a request to configure an interface with rate limit. |
| 306 | * |
| 307 | * @param intf the name of the interface |
| 308 | * @param limit the limit as a percentage |
| 309 | * @return the request string. |
| 310 | */ |
| 311 | private String addRateLimitBuilder(String intf, short limit) { |
| 312 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
| 313 | rpc.append("<srr-queue><bandwidth><limit>"); |
| 314 | rpc.append("<EnterBandwidthLimitInterfaceAsPercentage>"); |
| 315 | rpc.append(limit); |
| 316 | rpc.append("</EnterBandwidthLimitInterfaceAsPercentage>"); |
| 317 | rpc.append("</limit></bandwidth></srr-queue>"); |
| 318 | rpc.append(getClosingString()); |
| 319 | |
| 320 | return rpc.toString(); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Removes rate limit from an interface. |
| 325 | * |
| 326 | * @param intf the name of the interface |
| 327 | * @return the result of operation |
| 328 | */ |
| 329 | @Override |
| 330 | public boolean removeRateLimit(String intf) { |
| 331 | NetconfController controller = checkNotNull(handler() |
| 332 | .get(NetconfController.class)); |
| 333 | |
| 334 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 335 | .data().deviceId()).getSession(); |
| 336 | String reply; |
| 337 | try { |
| 338 | reply = session.requestSync(removeRateLimitBuilder(intf)); |
| 339 | } catch (NetconfException e) { |
| 340 | log.error("Failed to remove rate limit from device {} interface {}.", |
| 341 | handler().data().deviceId(), intf, e); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( |
| 346 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Builds a request to remove a rate limit from an interface. |
| 351 | * |
| 352 | * @param intf the name of the interface |
| 353 | * @return the request string. |
| 354 | */ |
| 355 | private String removeRateLimitBuilder(String intf) { |
| 356 | StringBuilder rpc = new StringBuilder(getOpeningString(intf)); |
| 357 | rpc.append("<srr-queue operation=\"delete\"><bandwidth><limit>"); |
| 358 | rpc.append("</limit></bandwidth></srr-queue>"); |
| 359 | rpc.append(getClosingString()); |
| 360 | |
| 361 | return rpc.toString(); |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Builds the opening of a request for the configuration of an interface. |
| 366 | * |
| 367 | * @param intf the interface to be configured |
| 368 | * @return the opening string |
| 369 | */ |
| 370 | private String getOpeningString(String intf) { |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 371 | StringBuilder rpc = |
| 372 | new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "); |
| 373 | rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"); |
| 374 | rpc.append("<edit-config>"); |
| 375 | rpc.append("<target>"); |
| 376 | rpc.append("<running/>"); |
| 377 | rpc.append("</target>"); |
| 378 | rpc.append("<config>"); |
| 379 | rpc.append("<xml-config-data>"); |
| 380 | rpc.append("<Device-Configuration><interface><Param>"); |
| 381 | rpc.append(intf); |
| 382 | rpc.append("</Param>"); |
| 383 | rpc.append("<ConfigIf-Configuration>"); |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 384 | |
| 385 | return rpc.toString(); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Builds the closing of a request for the configuration of an interface. |
| 390 | * |
| 391 | * @return the closing string |
| 392 | */ |
| 393 | private String getClosingString() { |
| 394 | StringBuilder rpc = new StringBuilder("</ConfigIf-Configuration>"); |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 395 | rpc.append("</interface>"); |
| 396 | rpc.append("</Device-Configuration>"); |
| 397 | rpc.append("</xml-config-data>"); |
| 398 | rpc.append("</config>"); |
| 399 | rpc.append("</edit-config>"); |
| 400 | rpc.append("</rpc>"); |
| 401 | |
| 402 | return rpc.toString(); |
| 403 | } |
| 404 | |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 405 | /** |
| 406 | * Builds a string with comma separated VLAN-IDs. |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 407 | * |
Andreas Papazois | 59e19bb | 2016-04-12 13:59:58 +0300 | [diff] [blame] | 408 | * @param vlanIds the VLAN IDs |
| 409 | * @return the string including the VLAN-IDs |
| 410 | */ |
| 411 | private String getVlansString(List<VlanId> vlanIds) { |
| 412 | StringBuilder vlansStringBuilder = new StringBuilder(); |
| 413 | |
| 414 | for (int i = 0; i < vlanIds.size(); i++) { |
| 415 | vlansStringBuilder.append(vlanIds.get(i)); |
| 416 | |
| 417 | if (i != vlanIds.size() - 1) { |
| 418 | vlansStringBuilder.append(","); |
| 419 | } |
| 420 | } |
| 421 | return vlansStringBuilder.toString(); |
| 422 | } |
| 423 | |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 424 | /** |
| 425 | * Provides the interfaces configured on a device. |
| 426 | * |
| 427 | * @param deviceId the device ID |
| 428 | * @return the list of the configured interfaces |
| 429 | */ |
| 430 | @Override |
| 431 | public List<DeviceInterfaceDescription> getInterfaces(DeviceId deviceId) { |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 432 | return getInterfaces(); |
| 433 | } |
| 434 | |
| 435 | /** |
| 436 | * Provides the interfaces configured on a device. |
| 437 | * |
| 438 | * @return the list of the configured interfaces |
| 439 | */ |
| 440 | @Override |
| 441 | public List<DeviceInterfaceDescription> getInterfaces() { |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 442 | NetconfController controller = |
| 443 | checkNotNull(handler().get(NetconfController.class)); |
| 444 | |
| 445 | NetconfSession session = controller.getDevicesMap().get(handler() |
| 446 | .data().deviceId()).getSession(); |
| 447 | String reply; |
| 448 | try { |
| 449 | reply = session.requestSync(getConfigBuilder()); |
| 450 | } catch (NetconfException e) { |
| 451 | log.error("Failed to retrieve configuration from device {}.", |
Andreas Papazois | 34a82cf | 2016-04-27 09:09:13 +0300 | [diff] [blame^] | 452 | handler().data().deviceId(), e); |
Andreas Papazois | 1ed54cf | 2016-05-04 16:22:40 +0300 | [diff] [blame] | 453 | return null; |
| 454 | } |
| 455 | |
| 456 | return XmlParserCisco.getInterfacesFromConfig(XmlConfigParser.loadXml( |
| 457 | new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Builds a request for getting configuration from device. |
| 462 | * |
| 463 | * @return the request string. |
| 464 | */ |
| 465 | private String getConfigBuilder() { |
| 466 | StringBuilder rpc = |
| 467 | new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "); |
| 468 | rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"); |
| 469 | rpc.append("<get-config>"); |
| 470 | rpc.append("<source>"); |
| 471 | rpc.append("<running/>"); |
| 472 | rpc.append("</source>"); |
| 473 | rpc.append("<filter>"); |
| 474 | rpc.append("<config-format-xml>"); |
| 475 | rpc.append("</config-format-xml>"); |
| 476 | rpc.append("</filter>"); |
| 477 | rpc.append("</get-config>"); |
| 478 | rpc.append("</rpc>"); |
| 479 | |
| 480 | return rpc.toString(); |
| 481 | } |
| 482 | |
Andreas Papazois | 92e4a04 | 2016-02-24 15:29:30 +0200 | [diff] [blame] | 483 | } |
| 484 | |