From 34a82cfae20212e10d557a491bca5452274cfd5e Mon Sep 17 00:00:00 2001 From: Andreas Papazois Date: Wed, 27 Apr 2016 09:09:13 +0300 Subject: [PATCH] [GEANT] Rate limit on port via NetConf and refactoring. Change-Id: Id5b5a196bed3b28159160b94bc5ae838d00cb765 --- .../cli/net/DeviceInterfaceAddCommand.java | 88 ++++-- .../cli/net/DeviceInterfaceRemoveCommand.java | 78 +++-- .../cli/net/DeviceInterfacesListCommand.java | 2 +- .../net/behaviour/InterfaceConfig.java | 80 ++++- .../cisco/InterfaceConfigCiscoIosImpl.java | 275 ++++++++++++------ 5 files changed, 385 insertions(+), 138 deletions(-) diff --git a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java index e0901e40a0..36778c0400 100644 --- a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java +++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java @@ -35,17 +35,20 @@ import java.util.List; description = "Configures a device interface") public class DeviceInterfaceAddCommand extends AbstractShellCommand { + private static final String ONE_ACTION_ALLOWED = + "One configuration action allowed at a time"; private static final String CONFIG_VLAN_SUCCESS = "VLAN %s added on device %s interface %s."; private static final String CONFIG_VLAN_FAILURE = "Failed to add VLAN %s on device %s interface %s."; - private static final String ONE_VLAN_ALLOWED = - "Only one VLAN allowed for access mode on device %s interface %s."; - private static final String CONFIG_TRUNK_SUCCESS = "Trunk mode added for VLAN %s on device %s interface %s."; private static final String CONFIG_TRUNK_FAILURE = "Failed to add trunk mode for VLAN %s on device %s interface %s."; + private static final String CONFIG_RATE_SUCCESS = + "Rate limit %d%% added on device %s interface %s."; + private static final String CONFIG_RATE_FAILURE = + "Failed to add rate limit %d%% on device %s interface %s."; @Argument(index = 0, name = "uri", description = "Device ID", required = true, multiValued = false) @@ -56,15 +59,20 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand { required = true, multiValued = false) private String portName = null; - @Argument(index = 2, name = "vlan", - description = "VLAN ID", - required = true, multiValued = true) - private String[] vlanStrings = null; + @Option(name = "-r", aliases = "--rate-limit", + description = "Percentage for egress bandwidth limit", + required = false, multiValued = false) + private String limitString = null; @Option(name = "-t", aliases = "--trunk", - description = "Configure interface as trunk for VLAN(s)", + description = "VLAN(s) for trunk port (multiple values are allowed)", + required = false, multiValued = true) + private String[] trunkVlanStrings = null; + + @Option(name = "-a", aliases = "--access", + description = "VLAN for access port", required = false, multiValued = false) - private boolean trunkMode = false; + private String accessVlanString = null; @Override protected void execute() { @@ -73,31 +81,51 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand { DriverHandler h = service.createHandler(deviceId); InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); + if (accessVlanString != null && trunkVlanStrings == null && + limitString == null) { + // Access mode to be enabled for VLAN. + addAccessModeToIntf(interfaceConfig); + } else if (trunkVlanStrings != null && accessVlanString == null && + limitString == null) { + // Trunk mode to be enabled for VLANs. + addTrunkModeToIntf(interfaceConfig); + } else if (limitString != null && accessVlanString == null && + trunkVlanStrings == null) { + // Rate limit to be set on interface. + addRateLimitToIntf(interfaceConfig); + } else { + // Option has not been correctly set. + print(ONE_ACTION_ALLOWED); + } + } + + private void addRateLimitToIntf(InterfaceConfig config) { + short rate = Short.parseShort(limitString); + if (config.addRateLimit(portName, rate)) { + print(CONFIG_RATE_SUCCESS, rate, uri, portName); + } else { + print(CONFIG_RATE_FAILURE, rate, uri, portName); + } + } + + private void addTrunkModeToIntf(InterfaceConfig config) { List vlanIds = new ArrayList<>(); - for (String vlanString : vlanStrings) { + for (String vlanString : trunkVlanStrings) { vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString))); } - - if (trunkMode) { - // Trunk mode to be enabled for VLAN. - if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanIds)) { - print(CONFIG_TRUNK_SUCCESS, vlanIds, deviceId, portName); - } else { - print(CONFIG_TRUNK_FAILURE, vlanIds, deviceId, portName); - } - return; - } - - // Access mode to be enabled for VLAN. - if (vlanIds.size() != 1) { - print(ONE_VLAN_ALLOWED, deviceId, portName); - return; - } - VlanId accessVlanId = vlanIds.get(0); - if (interfaceConfig.addAccessInterface(deviceId, portName, accessVlanId)) { - print(CONFIG_VLAN_SUCCESS, accessVlanId, deviceId, portName); + if (config.addTrunkMode(portName, vlanIds)) { + print(CONFIG_TRUNK_SUCCESS, vlanIds, uri, portName); } else { - print(CONFIG_VLAN_FAILURE, accessVlanId, deviceId, portName); + print(CONFIG_TRUNK_FAILURE, vlanIds, uri, portName); + } + } + + private void addAccessModeToIntf(InterfaceConfig config) { + VlanId accessVlanId = VlanId.vlanId(Short.parseShort(accessVlanString)); + if (config.addAccessMode(portName, accessVlanId)) { + print(CONFIG_VLAN_SUCCESS, accessVlanId, uri, portName); + } else { + print(CONFIG_VLAN_FAILURE, accessVlanId, uri, portName); } } diff --git a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java index 79bedf3606..3daa77f14a 100644 --- a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java +++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java @@ -25,36 +25,51 @@ import org.onosproject.net.driver.DriverHandler; import org.onosproject.net.driver.DriverService; /** - * Removes configured interface from a device. + * Removes an interface configurion from a device. */ @Command(scope = "onos", name = "device-remove-interface", description = "Removes an interface configuration from a device") public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { + private static final String ONE_ACTION_ALLOWED = + "One configuration removal allowed at a time"; private static final String REMOVE_ACCESS_SUCCESS = - "Access mode deleted from device %s interface %s."; + "Access mode removed from device %s interface %s."; private static final String REMOVE_ACCESS_FAILURE = - "Failed to delete access mode from device %s interface %s."; - + "Failed to remove access mode from device %s interface %s."; private static final String REMOVE_TRUNK_SUCCESS = - "Trunk mode deleted from device %s interface %s."; + "Trunk mode removed from device %s interface %s."; private static final String REMOVE_TRUNK_FAILURE = - "Failed to delete trunk mode from device %s interface %s."; + "Failed to remove trunk mode from device %s interface %s."; + private static final String REMOVE_RATE_SUCCESS = + "Rate limit removed from device %s interface %s."; + private static final String REMOVE_RATE_FAILURE = + "Failed to remove rate limit from device %s interface %s."; @Argument(index = 0, name = "uri", description = "Device ID", required = true, multiValued = false) private String uri = null; @Argument(index = 1, name = "interface", - description = "Interface name", - required = true, multiValued = false) + description = "Interface name", + required = true, multiValued = false) private String portName = null; + @Option(name = "-r", aliases = "--rate-limit", + description = "Percentage for egress bandwidth limit", + required = false, multiValued = false) + private boolean rateLimit = false; + @Option(name = "-t", aliases = "--trunk", description = "Remove trunk mode for VLAN(s)", required = false, multiValued = false) private boolean trunkMode = false; + @Option(name = "-a", aliases = "--access", + description = "Remove access mode for VLAN", + required = false, multiValued = false) + private boolean accessMode = false; + @Override protected void execute() { DriverService service = get(DriverService.class); @@ -62,21 +77,42 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { DriverHandler h = service.createHandler(deviceId); InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); - if (trunkMode) { + if (trunkMode && !accessMode && !rateLimit) { // Trunk mode for VLAN to be removed. - if (interfaceConfig.removeTrunkInterface(deviceId, portName)) { - print(REMOVE_TRUNK_SUCCESS, deviceId, portName); - } else { - print(REMOVE_TRUNK_FAILURE, deviceId, portName); - } - return; - } - - // Access mode for VLAN to be removed. - if (interfaceConfig.removeAccessInterface(deviceId, portName)) { - print(REMOVE_ACCESS_SUCCESS, deviceId, portName); + removeTrunkModeFromIntf(interfaceConfig); + } else if (accessMode && !trunkMode && !rateLimit) { + // Access mode for VLAN to be removed. + removeAccessModeFromIntf(interfaceConfig); + } else if (rateLimit && !trunkMode && !accessMode) { + // Rate limit to be removed. + removeRateLimitFromIntf(interfaceConfig); } else { - print(REMOVE_ACCESS_FAILURE, deviceId, portName); + // Option has not been correctly set. + print(ONE_ACTION_ALLOWED); + } + } + + private void removeAccessModeFromIntf(InterfaceConfig interfaceConfig) { + if (interfaceConfig.removeAccessMode(portName)) { + print(REMOVE_ACCESS_SUCCESS, uri, portName); + } else { + print(REMOVE_ACCESS_FAILURE, uri, portName); + } + } + + private void removeTrunkModeFromIntf(InterfaceConfig interfaceConfig) { + if (interfaceConfig.removeTrunkMode(portName)) { + print(REMOVE_TRUNK_SUCCESS, uri, portName); + } else { + print(REMOVE_TRUNK_FAILURE, uri, portName); + } + } + + private void removeRateLimitFromIntf(InterfaceConfig config) { + if (config.removeRateLimit(portName)) { + print(REMOVE_RATE_SUCCESS, uri, portName); + } else { + print(REMOVE_RATE_FAILURE, uri, portName); } } diff --git a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfacesListCommand.java b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfacesListCommand.java index 5a3c17c943..c2078be76c 100644 --- a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfacesListCommand.java +++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfacesListCommand.java @@ -78,7 +78,7 @@ public class DeviceInterfacesListCommand extends DevicesListCommand { InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); List interfaces = - interfaceConfig.getInterfaces(device.id()); + interfaceConfig.getInterfaces(); if (interfaces == null) { print(ERROR_RESULT); } else if (interfaces.isEmpty()) { diff --git a/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java b/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java index 22c2d8aa90..fecdf7fac3 100644 --- a/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java +++ b/core/api/src/main/java/org/onosproject/net/behaviour/InterfaceConfig.java @@ -34,8 +34,19 @@ public interface InterfaceConfig extends HandlerBehaviour { * @param intf the name of the interface * @param vlanId the VLAN ID * @return the result of operation + * @deprecated in 1.7.0 Hummingbird release - use of addAccessMode() instead */ - boolean addAccessInterface(DeviceId deviceId, String intf, VlanId vlanId); + @Deprecated + public boolean addAccessInterface(DeviceId deviceId, String intf, VlanId vlanId); + + /** + * Adds an access interface to a VLAN. + * + * @param intf the name of the interface + * @param vlanId the VLAN ID + * @return the result of operation + */ + boolean addAccessMode(String intf, VlanId vlanId); /** * Removes an access interface to a VLAN. @@ -43,9 +54,19 @@ public interface InterfaceConfig extends HandlerBehaviour { * @param deviceId the device ID * @param intf the name of the interface * @return the result of operation + * @deprecated in 1.7.0 Hummingbird release - use of removeAccessMode() instead */ + @Deprecated boolean removeAccessInterface(DeviceId deviceId, String intf); + /** + * Removes an access interface to a VLAN. + * + * @param intf the name of the interface + * @return the result of operation + */ + boolean removeAccessMode(String intf); + /** * Adds a trunk interface for VLANs. * @@ -53,30 +74,79 @@ public interface InterfaceConfig extends HandlerBehaviour { * @param intf the name of the interface * @param vlanIds the VLAN IDs * @return the result of operation + * @deprecated in 1.7.0 Hummingbird release - use of addTrunkMode() instead */ + @Deprecated boolean addTrunkInterface(DeviceId deviceId, String intf, List vlanIds); /** - * Removes trunk mode configuration from an interface. + * Adds a trunk interface for VLANs. + * + * @param intf the name of the interface + * @param vlanIds the VLAN IDs + * @return the result of operation + */ + boolean addTrunkMode(String intf, List vlanIds); + + /** + * Removes trunk mode configuration from an interface. * * @param deviceId the device ID * @param intf the name of the interface * @return the result of operation + * @deprecated in 1.7.0 Hummingbird release - use of removeTrunkMode() instead */ + @Deprecated boolean removeTrunkInterface(DeviceId deviceId, String intf); + /** + * Removes trunk mode configuration from an interface. + * + * @param intf the name of the interface + * @return the result of operation + */ + boolean removeTrunkMode(String intf); + + /** + * Adds a rate limit on an interface. + * + * @param intf the name of the interface + * @param limit the limit as a percentage + * @return the result of operation + */ + boolean addRateLimit(String intf, short limit); + + /** + * Removes rate limit from an interface. + * + * @param intf the name of the interface + * @return the result of operation + */ + boolean removeRateLimit(String intf); + /** * Provides the interfaces configured on a device. * * @param deviceId the device ID * @return the list of the configured interfaces + * @deprecated in 1.7.0 Hummingbird release - use of getInterfaces() without + * deviceId as parameter instead */ - List getInterfaces(DeviceId deviceId); + @Deprecated + public List getInterfaces(DeviceId deviceId); + + /** + * Provides the interfaces configured on a device. + * + * @return the list of the configured interfaces + */ + List getInterfaces(); /** * TODO Addition of more methods to make the behavior symmetrical. - * Methods getInterfacesForVlan, getVlansForInterface, getTrunkforInterface, - * getInterfacesForTrunk should be added to complete the behavior. + * Methods getInterfacesForVlan(VlanId), hasAccessMode(), hasTrunkMode(), + * getTrunkVlans(Interface), getAccessVlan(Interface) should be added to + * complete the behavior. */ } diff --git a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java index ae703cfcce..f31c8b0cf1 100644 --- a/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java +++ b/drivers/cisco/src/main/java/org/onosproject/drivers/cisco/InterfaceConfigCiscoIosImpl.java @@ -54,6 +54,18 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour */ @Override public boolean addAccessInterface(DeviceId deviceId, String intf, VlanId vlanId) { + return addAccessMode(intf, vlanId); + } + + /** + * Adds an access interface to a VLAN. + * + * @param intf the name of the interface + * @param vlanId the VLAN ID + * @return the result of operation + */ + @Override + public boolean addAccessMode(String intf, VlanId vlanId) { NetconfController controller = checkNotNull(handler() .get(NetconfController.class)); @@ -61,10 +73,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour .data().deviceId()).getSession(); String reply; try { - reply = session.requestSync(addAccessInterfaceBuilder(intf, vlanId)); + reply = session.requestSync(addAccessModeBuilder(intf, vlanId)); } catch (NetconfException e) { log.error("Failed to configure VLAN ID {} on device {} interface {}.", - vlanId, deviceId, intf, e); + vlanId, handler().data().deviceId(), intf, e); return false; } @@ -79,31 +91,13 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour * @param vlanId the VLAN ID * @return the request string. */ - private String addAccessInterfaceBuilder(String intf, VlanId vlanId) { - StringBuilder rpc = - new StringBuilder(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(intf); - rpc.append(""); - rpc.append(""); + private String addAccessModeBuilder(String intf, VlanId vlanId) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); rpc.append(""); rpc.append(vlanId); rpc.append(""); rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); + rpc.append(getClosingString()); return rpc.toString(); } @@ -117,6 +111,17 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour */ @Override public boolean removeAccessInterface(DeviceId deviceId, String intf) { + return removeAccessMode(intf); + } + + /** + * Removes an access interface to a VLAN. + * + * @param intf the name of the interface + * @return the result of operation + */ + @Override + public boolean removeAccessMode(String intf) { NetconfController controller = checkNotNull(handler() .get(NetconfController.class)); @@ -124,10 +129,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour .data().deviceId()).getSession(); String reply; try { - reply = session.requestSync(removeAccessInterfaceBuilder(intf)); + reply = session.requestSync(removeAccessModeBuilder(intf)); } catch (NetconfException e) { log.error("Failed to remove access mode from device {} interface {}.", - deviceId, intf, e); + handler().data().deviceId(), intf, e); return false; } @@ -141,30 +146,12 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour * @param intf the name of the interface * @return the request string. */ - private String removeAccessInterfaceBuilder(String intf) { - StringBuilder rpc = - new StringBuilder(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(intf); - rpc.append(""); - rpc.append(""); + private String removeAccessModeBuilder(String intf) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); rpc.append(""); rpc.append(""); rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); + rpc.append(getClosingString()); return rpc.toString(); } @@ -179,6 +166,18 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour */ @Override public boolean addTrunkInterface(DeviceId deviceId, String intf, List vlanIds) { + return addTrunkMode(intf, vlanIds); + } + + /** + * Adds a trunk interface for VLANs. + * + * @param intf the name of the interface + * @param vlanIds the VLAN IDs + * @return the result of operation + */ + @Override + public boolean addTrunkMode(String intf, List vlanIds) { NetconfController controller = checkNotNull(handler() .get(NetconfController.class)); @@ -186,10 +185,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour .data().deviceId()).getSession(); String reply; try { - reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanIds)); + reply = session.requestSync(addTrunkModeBuilder(intf, vlanIds)); } catch (NetconfException e) { log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.", - vlanIds, deviceId, intf, e); + vlanIds, handler().data().deviceId(), intf, e); return false; } @@ -204,33 +203,15 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour * @param vlanIds the VLAN IDs * @return the request string. */ - private String addTrunkInterfaceBuilder(String intf, List vlanIds) { - StringBuilder rpc = - new StringBuilder(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(intf); - rpc.append(""); - rpc.append(""); + private String addTrunkModeBuilder(String intf, List vlanIds) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); rpc.append(""); rpc.append(""); rpc.append(""); rpc.append(getVlansString(vlanIds)); rpc.append(""); rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); + rpc.append(getClosingString()); return rpc.toString(); } @@ -244,6 +225,17 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour */ @Override public boolean removeTrunkInterface(DeviceId deviceId, String intf) { + return removeTrunkMode(intf); + } + + /** + * Removes trunk mode configuration from an interface. + * + * @param intf the name of the interface + * @return the result of operation + */ + @Override + public boolean removeTrunkMode(String intf) { NetconfController controller = checkNotNull(handler() .get(NetconfController.class)); @@ -251,10 +243,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour .data().deviceId()).getSession(); String reply; try { - reply = session.requestSync(removeTrunkInterfaceBuilder(intf)); + reply = session.requestSync(removeTrunkModeBuilder(intf)); } catch (NetconfException e) { - log.error("Failed to remove trunk mode on device {} interface {}.", - deviceId, intf, e); + log.error("Failed to remove trunk mode from device {} interface {}.", + handler().data().deviceId(), intf, e); return false; } @@ -268,7 +260,114 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour * @param intf the name of the interface * @return the request string. */ - private String removeTrunkInterfaceBuilder(String intf) { + private String removeTrunkModeBuilder(String intf) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); + rpc.append(""); + rpc.append(""); + rpc.append(""); + rpc.append(""); + rpc.append(""); + rpc.append(""); + rpc.append(""); + rpc.append(getClosingString()); + + return rpc.toString(); + } + + /** + * Adds a rate limit on an interface. + * + * @param intf the name of the interface + * @param limit the limit as a percentage + * @return the result of operation + */ + @Override + public boolean addRateLimit(String intf, short limit) { + NetconfController controller = checkNotNull(handler() + .get(NetconfController.class)); + + NetconfSession session = controller.getDevicesMap().get(handler() + .data().deviceId()).getSession(); + String reply; + try { + reply = session.requestSync(addRateLimitBuilder(intf, limit)); + } catch (NetconfException e) { + log.error("Failed to configure rate limit {}%% on device {} interface {}.", + limit, handler().data().deviceId(), intf, e); + return false; + } + + return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( + new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); + } + + /** + * Builds a request to configure an interface with rate limit. + * + * @param intf the name of the interface + * @param limit the limit as a percentage + * @return the request string. + */ + private String addRateLimitBuilder(String intf, short limit) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); + rpc.append(""); + rpc.append(""); + rpc.append(limit); + rpc.append(""); + rpc.append(""); + rpc.append(getClosingString()); + + return rpc.toString(); + } + + /** + * Removes rate limit from an interface. + * + * @param intf the name of the interface + * @return the result of operation + */ + @Override + public boolean removeRateLimit(String intf) { + NetconfController controller = checkNotNull(handler() + .get(NetconfController.class)); + + NetconfSession session = controller.getDevicesMap().get(handler() + .data().deviceId()).getSession(); + String reply; + try { + reply = session.requestSync(removeRateLimitBuilder(intf)); + } catch (NetconfException e) { + log.error("Failed to remove rate limit from device {} interface {}.", + handler().data().deviceId(), intf, e); + return false; + } + + return XmlConfigParser.configSuccess(XmlConfigParser.loadXml( + new ByteArrayInputStream(reply.getBytes(StandardCharsets.UTF_8)))); + } + + /** + * Builds a request to remove a rate limit from an interface. + * + * @param intf the name of the interface + * @return the request string. + */ + private String removeRateLimitBuilder(String intf) { + StringBuilder rpc = new StringBuilder(getOpeningString(intf)); + rpc.append(""); + rpc.append(""); + rpc.append(getClosingString()); + + return rpc.toString(); + } + + /** + * Builds the opening of a request for the configuration of an interface. + * + * @param intf the interface to be configured + * @return the opening string + */ + private String getOpeningString(String intf) { StringBuilder rpc = new StringBuilder(""); @@ -282,13 +381,17 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour rpc.append(intf); rpc.append(""); rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); - rpc.append(""); + + return rpc.toString(); + } + + /** + * Builds the closing of a request for the configuration of an interface. + * + * @return the closing string + */ + private String getClosingString() { + StringBuilder rpc = new StringBuilder(""); rpc.append(""); rpc.append(""); rpc.append(""); @@ -326,6 +429,16 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour */ @Override public List getInterfaces(DeviceId deviceId) { + return getInterfaces(); + } + + /** + * Provides the interfaces configured on a device. + * + * @return the list of the configured interfaces + */ + @Override + public List getInterfaces() { NetconfController controller = checkNotNull(handler().get(NetconfController.class)); @@ -336,7 +449,7 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour reply = session.requestSync(getConfigBuilder()); } catch (NetconfException e) { log.error("Failed to retrieve configuration from device {}.", - deviceId, e); + handler().data().deviceId(), e); return null; }