Andreas Papazois
Committed by Gerrit Code Review

[GEANT] Rate limit on port via NetConf and refactoring.

Change-Id: Id5b5a196bed3b28159160b94bc5ae838d00cb765
......@@ -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);
List<VlanId> vlanIds = new ArrayList<>();
for (String vlanString : vlanStrings) {
vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
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);
}
}
if (trunkMode) {
// Trunk mode to be enabled for VLAN.
if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanIds)) {
print(CONFIG_TRUNK_SUCCESS, vlanIds, deviceId, portName);
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_TRUNK_FAILURE, vlanIds, deviceId, portName);
print(CONFIG_RATE_FAILURE, rate, uri, portName);
}
return;
}
// Access mode to be enabled for VLAN.
if (vlanIds.size() != 1) {
print(ONE_VLAN_ALLOWED, deviceId, portName);
return;
private void addTrunkModeToIntf(InterfaceConfig config) {
List<VlanId> vlanIds = new ArrayList<>();
for (String vlanString : trunkVlanStrings) {
vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
}
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_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, deviceId, portName);
print(CONFIG_VLAN_FAILURE, accessVlanId, uri, portName);
}
}
......
......@@ -25,21 +25,26 @@ 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)
......@@ -50,11 +55,21 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
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);
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_TRUNK_FAILURE, deviceId, portName);
// Option has not been correctly set.
print(ONE_ACTION_ALLOWED);
}
return;
}
// Access mode for VLAN to be removed.
if (interfaceConfig.removeAccessInterface(deviceId, portName)) {
print(REMOVE_ACCESS_SUCCESS, deviceId, portName);
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_ACCESS_FAILURE, deviceId, portName);
print(REMOVE_RATE_FAILURE, uri, portName);
}
}
......
......@@ -78,7 +78,7 @@ public class DeviceInterfacesListCommand extends DevicesListCommand {
InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
List<DeviceInterfaceDescription> interfaces =
interfaceConfig.getInterfaces(device.id());
interfaceConfig.getInterfaces();
if (interfaces == null) {
print(ERROR_RESULT);
} else if (interfaces.isEmpty()) {
......
......@@ -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,40 +54,99 @@ 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.
*
* @param deviceId the device ID
* @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<VlanId> vlanIds);
/**
* 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<VlanId> 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
*/
@Deprecated
public List<DeviceInterfaceDescription> getInterfaces(DeviceId deviceId);
/**
* Provides the interfaces configured on a device.
*
* @return the list of the configured interfaces
*/
List<DeviceInterfaceDescription> getInterfaces(DeviceId deviceId);
List<DeviceInterfaceDescription> 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.
*/
}
......