Andreas Papazois
Committed by Gerrit Code Review

[GEANT] Multiple VLAN-IDs allowed for trunk mode of ports.

Change-Id: Ib6add6f4bcdcc9ed0fb0448fef91f9f0dbebb57d
...@@ -25,6 +25,9 @@ import org.onosproject.net.behaviour.InterfaceConfig; ...@@ -25,6 +25,9 @@ import org.onosproject.net.behaviour.InterfaceConfig;
25 import org.onosproject.net.driver.DriverHandler; 25 import org.onosproject.net.driver.DriverHandler;
26 import org.onosproject.net.driver.DriverService; 26 import org.onosproject.net.driver.DriverService;
27 27
28 +import java.util.ArrayList;
29 +import java.util.List;
30 +
28 /** 31 /**
29 * Configures a device interface. 32 * Configures a device interface.
30 */ 33 */
...@@ -36,6 +39,8 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand { ...@@ -36,6 +39,8 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand {
36 "VLAN %s added on device %s interface %s."; 39 "VLAN %s added on device %s interface %s.";
37 private static final String CONFIG_VLAN_FAILURE = 40 private static final String CONFIG_VLAN_FAILURE =
38 "Failed to add VLAN %s on device %s interface %s."; 41 "Failed to add VLAN %s on device %s interface %s.";
42 + private static final String ONE_VLAN_ALLOWED =
43 + "Only one VLAN allowed for access mode on device %s interface %s.";
39 44
40 private static final String CONFIG_TRUNK_SUCCESS = 45 private static final String CONFIG_TRUNK_SUCCESS =
41 "Trunk mode added for VLAN %s on device %s interface %s."; 46 "Trunk mode added for VLAN %s on device %s interface %s.";
...@@ -53,11 +58,11 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand { ...@@ -53,11 +58,11 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand {
53 58
54 @Argument(index = 2, name = "vlan", 59 @Argument(index = 2, name = "vlan",
55 description = "VLAN ID", 60 description = "VLAN ID",
56 - required = true, multiValued = false) 61 + required = true, multiValued = true)
57 - private String vlanString = null; 62 + private String[] vlanStrings = null;
58 63
59 @Option(name = "-t", aliases = "--trunk", 64 @Option(name = "-t", aliases = "--trunk",
60 - description = "Configure interface as trunk for VLAN", 65 + description = "Configure interface as trunk for VLAN(s)",
61 required = false, multiValued = false) 66 required = false, multiValued = false)
62 private boolean trunkMode = false; 67 private boolean trunkMode = false;
63 68
...@@ -68,23 +73,31 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand { ...@@ -68,23 +73,31 @@ public class DeviceInterfaceAddCommand extends AbstractShellCommand {
68 DriverHandler h = service.createHandler(deviceId); 73 DriverHandler h = service.createHandler(deviceId);
69 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); 74 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
70 75
71 - VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString)); 76 + List<VlanId> vlanIds = new ArrayList<>();
77 + for (String vlanString : vlanStrings) {
78 + vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
79 + }
72 80
73 if (trunkMode) { 81 if (trunkMode) {
74 // Trunk mode to be enabled for VLAN. 82 // Trunk mode to be enabled for VLAN.
75 - if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanId)) { 83 + if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanIds)) {
76 - print(CONFIG_TRUNK_SUCCESS, vlanId, deviceId, portName); 84 + print(CONFIG_TRUNK_SUCCESS, vlanIds, deviceId, portName);
77 } else { 85 } else {
78 - print(CONFIG_TRUNK_FAILURE, vlanId, deviceId, portName); 86 + print(CONFIG_TRUNK_FAILURE, vlanIds, deviceId, portName);
79 } 87 }
80 return; 88 return;
81 } 89 }
82 90
83 - // VLAN to be added to interface. 91 + // Access mode to be enabled for VLAN.
84 - if (interfaceConfig.addInterfaceToVlan(deviceId, portName, vlanId)) { 92 + if (vlanIds.size() != 1) {
85 - print(CONFIG_VLAN_SUCCESS, vlanId, deviceId, portName); 93 + print(ONE_VLAN_ALLOWED, deviceId, portName);
94 + return;
95 + }
96 + VlanId accessVlanId = vlanIds.get(0);
97 + if (interfaceConfig.addInterfaceToVlan(deviceId, portName, accessVlanId)) {
98 + print(CONFIG_VLAN_SUCCESS, accessVlanId, deviceId, portName);
86 } else { 99 } else {
87 - print(CONFIG_VLAN_FAILURE, vlanId, deviceId, portName); 100 + print(CONFIG_VLAN_FAILURE, accessVlanId, deviceId, portName);
88 } 101 }
89 } 102 }
90 103
......
...@@ -25,6 +25,9 @@ import org.onosproject.net.behaviour.InterfaceConfig; ...@@ -25,6 +25,9 @@ import org.onosproject.net.behaviour.InterfaceConfig;
25 import org.onosproject.net.driver.DriverHandler; 25 import org.onosproject.net.driver.DriverHandler;
26 import org.onosproject.net.driver.DriverService; 26 import org.onosproject.net.driver.DriverService;
27 27
28 +import java.util.ArrayList;
29 +import java.util.List;
30 +
28 /** 31 /**
29 * Removes configured interface from a device. 32 * Removes configured interface from a device.
30 */ 33 */
...@@ -36,6 +39,8 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { ...@@ -36,6 +39,8 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
36 "VLAN %s removed from device %s interface %s."; 39 "VLAN %s removed from device %s interface %s.";
37 private static final String REMOVE_VLAN_FAILURE = 40 private static final String REMOVE_VLAN_FAILURE =
38 "Failed to remove VLAN %s from device %s interface %s."; 41 "Failed to remove VLAN %s from device %s interface %s.";
42 + private static final String ONE_VLAN_ALLOWED =
43 + "Only one VLAN allowed for access mode on device %s interface %s.";
39 44
40 private static final String REMOVE_TRUNK_SUCCESS = 45 private static final String REMOVE_TRUNK_SUCCESS =
41 "Trunk mode removed for VLAN %s on device %s interface %s."; 46 "Trunk mode removed for VLAN %s on device %s interface %s.";
...@@ -53,11 +58,11 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { ...@@ -53,11 +58,11 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
53 58
54 @Argument(index = 2, name = "vlan", 59 @Argument(index = 2, name = "vlan",
55 description = "VLAN ID", 60 description = "VLAN ID",
56 - required = true, multiValued = false) 61 + required = true, multiValued = true)
57 - private String vlanString = null; 62 + private String[] vlanStrings = null;
58 63
59 @Option(name = "-t", aliases = "--trunk", 64 @Option(name = "-t", aliases = "--trunk",
60 - description = "Remove trunk mode for VLAN", 65 + description = "Remove trunk mode for VLAN(s)",
61 required = false, multiValued = false) 66 required = false, multiValued = false)
62 private boolean trunkMode = false; 67 private boolean trunkMode = false;
63 68
...@@ -68,23 +73,31 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand { ...@@ -68,23 +73,31 @@ public class DeviceInterfaceRemoveCommand extends AbstractShellCommand {
68 DriverHandler h = service.createHandler(deviceId); 73 DriverHandler h = service.createHandler(deviceId);
69 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class); 74 InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
70 75
71 - VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString)); 76 + List<VlanId> vlanIds = new ArrayList<>();
77 + for (String vlanString : vlanStrings) {
78 + vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
79 + }
72 80
73 if (trunkMode) { 81 if (trunkMode) {
74 // Trunk mode for VLAN to be removed. 82 // Trunk mode for VLAN to be removed.
75 - if (interfaceConfig.removeTrunkInterface(deviceId, portName, vlanId)) { 83 + if (interfaceConfig.removeTrunkInterface(deviceId, portName, vlanIds)) {
76 - print(REMOVE_TRUNK_SUCCESS, vlanId, deviceId, portName); 84 + print(REMOVE_TRUNK_SUCCESS, vlanIds, deviceId, portName);
77 } else { 85 } else {
78 - print(REMOVE_TRUNK_FAILURE, vlanId, deviceId, portName); 86 + print(REMOVE_TRUNK_FAILURE, vlanIds, deviceId, portName);
79 } 87 }
80 return; 88 return;
81 } 89 }
82 90
83 - // Interface to be removed from VLAN. 91 + // Access mode for VLAN to be removed.
84 - if (interfaceConfig.removeInterfaceFromVlan(deviceId, portName, vlanId)) { 92 + if (vlanIds.size() != 1) {
85 - print(REMOVE_VLAN_SUCCESS, vlanId, deviceId, portName); 93 + print(ONE_VLAN_ALLOWED, deviceId, portName);
94 + return;
95 + }
96 + VlanId accessVlanId = vlanIds.get(0);
97 + if (interfaceConfig.removeInterfaceFromVlan(deviceId, portName, accessVlanId)) {
98 + print(REMOVE_VLAN_SUCCESS, accessVlanId, deviceId, portName);
86 } else { 99 } else {
87 - print(REMOVE_VLAN_FAILURE, vlanId, deviceId, portName); 100 + print(REMOVE_VLAN_FAILURE, accessVlanId, deviceId, portName);
88 } 101 }
89 } 102 }
90 103
......
...@@ -19,6 +19,8 @@ import org.onlab.packet.VlanId; ...@@ -19,6 +19,8 @@ import org.onlab.packet.VlanId;
19 import org.onosproject.net.DeviceId; 19 import org.onosproject.net.DeviceId;
20 import org.onosproject.net.driver.HandlerBehaviour; 20 import org.onosproject.net.driver.HandlerBehaviour;
21 21
22 +import java.util.List;
23 +
22 /** 24 /**
23 * Means to configure interfaces on devices. 25 * Means to configure interfaces on devices.
24 */ 26 */
...@@ -43,22 +45,22 @@ public interface InterfaceConfig extends HandlerBehaviour { ...@@ -43,22 +45,22 @@ public interface InterfaceConfig extends HandlerBehaviour {
43 boolean removeInterfaceFromVlan(DeviceId deviceId, String intf, VlanId vlanId); 45 boolean removeInterfaceFromVlan(DeviceId deviceId, String intf, VlanId vlanId);
44 46
45 /** 47 /**
46 - * Configures an interface as trunk for VLAN. 48 + * Configures an interface as trunk for VLANs.
47 * @param deviceId the device ID 49 * @param deviceId the device ID
48 * @param intf the name of the interface 50 * @param intf the name of the interface
49 - * @param vlanId the VLAN ID 51 + * @param vlanIds the VLAN IDs
50 * @return the result of operation 52 * @return the result of operation
51 */ 53 */
52 - boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId); 54 + boolean addTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds);
53 55
54 /** 56 /**
55 - * Removes trunk mode configuration for VLAN from an interface. 57 + * Removes trunk mode configuration for VLANs from an interface.
56 * @param deviceId the device ID 58 * @param deviceId the device ID
57 * @param intf the name of the interface 59 * @param intf the name of the interface
58 - * @param vlanId the VLAN ID 60 + * @param vlanIds the VLAN IDs
59 * @return the result of operation 61 * @return the result of operation
60 */ 62 */
61 - boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId); 63 + boolean removeTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds);
62 64
63 /** 65 /**
64 * TODO Addition of more methods to make the behavior symmetrical. 66 * TODO Addition of more methods to make the behavior symmetrical.
......
...@@ -30,6 +30,7 @@ import org.slf4j.Logger; ...@@ -30,6 +30,7 @@ import org.slf4j.Logger;
30 30
31 import java.io.ByteArrayInputStream; 31 import java.io.ByteArrayInputStream;
32 import java.nio.charset.StandardCharsets; 32 import java.nio.charset.StandardCharsets;
33 +import java.util.List;
33 34
34 import static com.google.common.base.Preconditions.checkNotNull; 35 import static com.google.common.base.Preconditions.checkNotNull;
35 import static org.slf4j.LoggerFactory.getLogger; 36 import static org.slf4j.LoggerFactory.getLogger;
...@@ -168,14 +169,14 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -168,14 +169,14 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
168 } 169 }
169 170
170 /** 171 /**
171 - * Configures an interface as trunk for VLAN. 172 + * Configures an interface as trunk for VLANs.
172 * @param deviceId the device ID 173 * @param deviceId the device ID
173 * @param intf the name of the interface 174 * @param intf the name of the interface
174 - * @param vlanId the VLAN ID 175 + * @param vlanIds the VLAN IDs
175 * @return the result of operation 176 * @return the result of operation
176 */ 177 */
177 @Override 178 @Override
178 - public boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) { 179 + public boolean addTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds) {
179 NetconfController controller = checkNotNull(handler() 180 NetconfController controller = checkNotNull(handler()
180 .get(NetconfController.class)); 181 .get(NetconfController.class));
181 182
...@@ -183,10 +184,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -183,10 +184,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
183 .data().deviceId()).getSession(); 184 .data().deviceId()).getSession();
184 String reply; 185 String reply;
185 try { 186 try {
186 - reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanId)); 187 + reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanIds));
187 } catch (NetconfException e) { 188 } catch (NetconfException e) {
188 log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.", 189 log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.",
189 - vlanId, deviceId, intf, e); 190 + vlanIds, deviceId, intf, e);
190 return false; 191 return false;
191 } 192 }
192 193
...@@ -195,12 +196,12 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -195,12 +196,12 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
195 } 196 }
196 197
197 /** 198 /**
198 - * Builds a request to configure an interface as trunk for VLAN. 199 + * Builds a request to configure an interface as trunk for VLANs.
199 * @param intf the name of the interface 200 * @param intf the name of the interface
200 - * @param vlanId the VLAN ID 201 + * @param vlanIds the VLAN IDs
201 * @return the request string. 202 * @return the request string.
202 */ 203 */
203 - private String addTrunkInterfaceBuilder(String intf, VlanId vlanId) { 204 + private String addTrunkInterfaceBuilder(String intf, List<VlanId> vlanIds) {
204 StringBuilder rpc = 205 StringBuilder rpc =
205 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "); 206 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
206 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"); 207 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
...@@ -217,7 +218,7 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -217,7 +218,7 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
217 rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>"); 218 rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>");
218 rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>"); 219 rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>");
219 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>"); 220 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
220 - rpc.append(vlanId); 221 + rpc.append(getVlansString(vlanIds));
221 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>"); 222 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>");
222 rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>"); 223 rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>");
223 rpc.append("</ConfigIf-Configuration>"); 224 rpc.append("</ConfigIf-Configuration>");
...@@ -232,14 +233,14 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -232,14 +233,14 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
232 } 233 }
233 234
234 /** 235 /**
235 - * Removes trunk mode configuration for VLAN from an interface. 236 + * Removes trunk mode configuration for VLANs from an interface.
236 * @param deviceId the device ID 237 * @param deviceId the device ID
237 * @param intf the name of the interface 238 * @param intf the name of the interface
238 - * @param vlanId the VLAN ID 239 + * @param vlanIds the VLAN IDs
239 * @return the result of operation 240 * @return the result of operation
240 */ 241 */
241 @Override 242 @Override
242 - public boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) { 243 + public boolean removeTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds) {
243 NetconfController controller = checkNotNull(handler() 244 NetconfController controller = checkNotNull(handler()
244 .get(NetconfController.class)); 245 .get(NetconfController.class));
245 246
...@@ -247,10 +248,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -247,10 +248,10 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
247 .data().deviceId()).getSession(); 248 .data().deviceId()).getSession();
248 String reply; 249 String reply;
249 try { 250 try {
250 - reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanId)); 251 + reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanIds));
251 } catch (NetconfException e) { 252 } catch (NetconfException e) {
252 log.error("Failed to remove trunk mode for VLAN ID {} on device {} interface {}.", 253 log.error("Failed to remove trunk mode for VLAN ID {} on device {} interface {}.",
253 - vlanId, deviceId, intf, e); 254 + vlanIds, deviceId, intf, e);
254 return false; 255 return false;
255 } 256 }
256 257
...@@ -259,12 +260,12 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -259,12 +260,12 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
259 } 260 }
260 261
261 /** 262 /**
262 - * Builds a request to remove trunk mode configuration for VLAN from an interface. 263 + * Builds a request to remove trunk mode configuration for VLANs from an interface.
263 * @param intf the name of the interface 264 * @param intf the name of the interface
264 - * @param vlanId the VLAN ID 265 + * @param vlanIds the VLAN IDs
265 * @return the request string. 266 * @return the request string.
266 */ 267 */
267 - private String removeTrunkInterfaceBuilder(String intf, VlanId vlanId) { 268 + private String removeTrunkInterfaceBuilder(String intf, List<VlanId> vlanIds) {
268 StringBuilder rpc = 269 StringBuilder rpc =
269 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" "); 270 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
270 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"); 271 rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
...@@ -283,7 +284,7 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -283,7 +284,7 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
283 rpc.append("<dot1q/></encapsulation></trunk></switchport>"); 284 rpc.append("<dot1q/></encapsulation></trunk></switchport>");
284 rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>"); 285 rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>");
285 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>"); 286 rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
286 - rpc.append(vlanId); 287 + rpc.append(getVlansString(vlanIds));
287 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>"); 288 rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>");
288 rpc.append("</trunk></switchport></ConfigIf-Configuration>"); 289 rpc.append("</trunk></switchport></ConfigIf-Configuration>");
289 rpc.append("</interface>"); 290 rpc.append("</interface>");
...@@ -296,5 +297,23 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour ...@@ -296,5 +297,23 @@ public class InterfaceConfigCiscoIosImpl extends AbstractHandlerBehaviour
296 return rpc.toString(); 297 return rpc.toString();
297 } 298 }
298 299
300 + /**
301 + * Builds a string with comma separated VLAN-IDs.
302 + * @param vlanIds the VLAN IDs
303 + * @return the string including the VLAN-IDs
304 + */
305 + private String getVlansString(List<VlanId> vlanIds) {
306 + StringBuilder vlansStringBuilder = new StringBuilder();
307 +
308 + for (int i = 0; i < vlanIds.size(); i++) {
309 + vlansStringBuilder.append(vlanIds.get(i));
310 +
311 + if (i != vlanIds.size() - 1) {
312 + vlansStringBuilder.append(",");
313 + }
314 + }
315 + return vlansStringBuilder.toString();
316 + }
317 +
299 } 318 }
300 319
......