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

Change-Id: Ib6add6f4bcdcc9ed0fb0448fef91f9f0dbebb57d
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 5940d55..7db23ae 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceAddCommand.java
@@ -25,6 +25,9 @@
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverService;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Configures a device interface.
  */
@@ -36,6 +39,8 @@
             "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.";
@@ -53,11 +58,11 @@
 
     @Argument(index = 2, name = "vlan",
             description = "VLAN ID",
-            required = true, multiValued = false)
-    private String vlanString = null;
+            required = true, multiValued = true)
+    private String[] vlanStrings = null;
 
     @Option(name = "-t", aliases = "--trunk",
-            description = "Configure interface as trunk for VLAN",
+            description = "Configure interface as trunk for VLAN(s)",
             required = false, multiValued = false)
     private boolean trunkMode = false;
 
@@ -68,23 +73,31 @@
         DriverHandler h = service.createHandler(deviceId);
         InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
 
-        VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString));
+        List<VlanId> vlanIds = new ArrayList<>();
+        for (String vlanString : vlanStrings) {
+            vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
+        }
 
         if (trunkMode) {
             // Trunk mode to be enabled for VLAN.
-            if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanId)) {
-                print(CONFIG_TRUNK_SUCCESS, vlanId, deviceId, portName);
+            if (interfaceConfig.addTrunkInterface(deviceId, portName, vlanIds)) {
+                print(CONFIG_TRUNK_SUCCESS, vlanIds, deviceId, portName);
             } else {
-                print(CONFIG_TRUNK_FAILURE, vlanId, deviceId, portName);
+                print(CONFIG_TRUNK_FAILURE, vlanIds, deviceId, portName);
             }
             return;
         }
 
-        // VLAN to be added to interface.
-        if (interfaceConfig.addInterfaceToVlan(deviceId, portName, vlanId)) {
-            print(CONFIG_VLAN_SUCCESS, vlanId, deviceId, portName);
+        // 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.addInterfaceToVlan(deviceId, portName, accessVlanId)) {
+            print(CONFIG_VLAN_SUCCESS, accessVlanId, deviceId, portName);
         } else {
-            print(CONFIG_VLAN_FAILURE, vlanId, deviceId, portName);
+            print(CONFIG_VLAN_FAILURE, accessVlanId, deviceId, 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 22afa65..643fbb4 100644
--- a/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java
+++ b/cli/src/main/java/org/onosproject/cli/net/DeviceInterfaceRemoveCommand.java
@@ -25,6 +25,9 @@
 import org.onosproject.net.driver.DriverHandler;
 import org.onosproject.net.driver.DriverService;
 
+import java.util.ArrayList;
+import java.util.List;
+
 /**
  * Removes configured interface from a device.
  */
@@ -36,6 +39,8 @@
             "VLAN %s removed from device %s interface %s.";
     private static final String REMOVE_VLAN_FAILURE =
             "Failed to remove VLAN %s from 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 REMOVE_TRUNK_SUCCESS =
             "Trunk mode removed for VLAN %s on device %s interface %s.";
@@ -53,11 +58,11 @@
 
     @Argument(index = 2, name = "vlan",
             description = "VLAN ID",
-            required = true, multiValued = false)
-    private String vlanString = null;
+            required = true, multiValued = true)
+    private String[] vlanStrings = null;
 
     @Option(name = "-t", aliases = "--trunk",
-            description = "Remove trunk mode for VLAN",
+            description = "Remove trunk mode for VLAN(s)",
             required = false, multiValued = false)
     private boolean trunkMode = false;
 
@@ -68,23 +73,31 @@
         DriverHandler h = service.createHandler(deviceId);
         InterfaceConfig interfaceConfig = h.behaviour(InterfaceConfig.class);
 
-        VlanId vlanId = VlanId.vlanId(Short.parseShort(vlanString));
+        List<VlanId> vlanIds = new ArrayList<>();
+        for (String vlanString : vlanStrings) {
+            vlanIds.add(VlanId.vlanId(Short.parseShort(vlanString)));
+        }
 
         if (trunkMode) {
             // Trunk mode for VLAN to be removed.
-            if (interfaceConfig.removeTrunkInterface(deviceId, portName, vlanId)) {
-                print(REMOVE_TRUNK_SUCCESS, vlanId, deviceId, portName);
+            if (interfaceConfig.removeTrunkInterface(deviceId, portName, vlanIds)) {
+                print(REMOVE_TRUNK_SUCCESS, vlanIds, deviceId, portName);
             } else {
-                print(REMOVE_TRUNK_FAILURE, vlanId, deviceId, portName);
+                print(REMOVE_TRUNK_FAILURE, vlanIds, deviceId, portName);
             }
             return;
         }
 
-        // Interface to be removed from VLAN.
-        if (interfaceConfig.removeInterfaceFromVlan(deviceId, portName, vlanId)) {
-            print(REMOVE_VLAN_SUCCESS, vlanId, deviceId, portName);
+        // Access mode for VLAN to be removed.
+        if (vlanIds.size() != 1) {
+            print(ONE_VLAN_ALLOWED, deviceId, portName);
+            return;
+        }
+        VlanId accessVlanId = vlanIds.get(0);
+        if (interfaceConfig.removeInterfaceFromVlan(deviceId, portName, accessVlanId)) {
+            print(REMOVE_VLAN_SUCCESS, accessVlanId, deviceId, portName);
         } else {
-            print(REMOVE_VLAN_FAILURE, vlanId, deviceId, portName);
+            print(REMOVE_VLAN_FAILURE, accessVlanId, deviceId, portName);
         }
     }
 
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 db781b8..b847647 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
@@ -19,6 +19,8 @@
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.driver.HandlerBehaviour;
 
+import java.util.List;
+
 /**
  * Means to configure interfaces on devices.
  */
@@ -43,22 +45,22 @@
     boolean removeInterfaceFromVlan(DeviceId deviceId, String intf, VlanId vlanId);
 
     /**
-     *  Configures an interface as trunk for VLAN.
+     *  Configures an interface as trunk for VLANs.
      * @param deviceId the device ID
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the result of operation
      */
-    boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId);
+    boolean addTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds);
 
     /**
-     *  Removes trunk mode configuration for VLAN from an interface.
+     *  Removes trunk mode configuration for VLANs from an interface.
      * @param deviceId the device ID
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the result of operation
      */
-    boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId);
+    boolean removeTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds);
 
     /**
      *  TODO Addition of more methods to make the behavior symmetrical.
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 7bb02d7..1e94c21 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
@@ -30,6 +30,7 @@
 
 import java.io.ByteArrayInputStream;
 import java.nio.charset.StandardCharsets;
+import java.util.List;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -168,14 +169,14 @@
     }
 
     /**
-     * Configures an interface as trunk for VLAN.
+     *  Configures an interface as trunk for VLANs.
      * @param deviceId the device ID
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the result of operation
      */
     @Override
-    public boolean addTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
+    public boolean addTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds) {
         NetconfController controller = checkNotNull(handler()
                                        .get(NetconfController.class));
 
@@ -183,10 +184,10 @@
                                  .data().deviceId()).getSession();
         String reply;
         try {
-            reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanId));
+            reply = session.requestSync(addTrunkInterfaceBuilder(intf, vlanIds));
         } catch (NetconfException e) {
             log.error("Failed to configure trunk mode for VLAN ID {} on device {} interface {}.",
-                      vlanId, deviceId, intf, e);
+                      vlanIds, deviceId, intf, e);
             return false;
         }
 
@@ -195,12 +196,12 @@
     }
 
     /**
-     * Builds a request to configure an interface as trunk for VLAN.
+     * Builds a request to configure an interface as trunk for VLANs.
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the request string.
      */
-    private String addTrunkInterfaceBuilder(String intf, VlanId vlanId) {
+    private String addTrunkInterfaceBuilder(String intf, List<VlanId> vlanIds) {
         StringBuilder rpc =
                 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
         rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
@@ -217,7 +218,7 @@
         rpc.append("<switchport><trunk><encapsulation><dot1q/></encapsulation>");
         rpc.append("</trunk></switchport><switchport><trunk><allowed><vlan>");
         rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
-        rpc.append(vlanId);
+        rpc.append(getVlansString(vlanIds));
         rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed></trunk>");
         rpc.append("</switchport><switchport><mode><trunk/></mode></switchport>");
         rpc.append("</ConfigIf-Configuration>");
@@ -232,14 +233,14 @@
     }
 
     /**
-     *  Removes trunk mode configuration for VLAN from an interface.
+     *  Removes trunk mode configuration for VLANs from an interface.
      * @param deviceId the device ID
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the result of operation
      */
     @Override
-    public boolean removeTrunkInterface(DeviceId deviceId, String intf, VlanId vlanId) {
+    public boolean removeTrunkInterface(DeviceId deviceId, String intf, List<VlanId> vlanIds) {
         NetconfController controller = checkNotNull(handler()
                                        .get(NetconfController.class));
 
@@ -247,10 +248,10 @@
                              .data().deviceId()).getSession();
     String reply;
     try {
-        reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanId));
+        reply = session.requestSync(removeTrunkInterfaceBuilder(intf, vlanIds));
     } catch (NetconfException e) {
         log.error("Failed to remove trunk mode for VLAN ID {} on device {} interface {}.",
-                  vlanId, deviceId, intf, e);
+                  vlanIds, deviceId, intf, e);
         return false;
     }
 
@@ -259,12 +260,12 @@
 }
 
     /**
-     * Builds a request to remove trunk mode configuration for VLAN from an interface.
+     * Builds a request to remove trunk mode configuration for VLANs from an interface.
      * @param intf the name of the interface
-     * @param vlanId the VLAN ID
+     * @param vlanIds the VLAN IDs
      * @return the request string.
      */
-    private String removeTrunkInterfaceBuilder(String intf, VlanId vlanId) {
+    private String removeTrunkInterfaceBuilder(String intf, List<VlanId> vlanIds) {
         StringBuilder rpc =
                 new StringBuilder("<rpc xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\" ");
         rpc.append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
@@ -283,7 +284,7 @@
         rpc.append("<dot1q/></encapsulation></trunk></switchport>");
         rpc.append("<switchport><trunk operation=\"delete\"><allowed><vlan>");
         rpc.append("<VLANIDsAllowedVLANsPortTrunkingMode>");
-        rpc.append(vlanId);
+        rpc.append(getVlansString(vlanIds));
         rpc.append("</VLANIDsAllowedVLANsPortTrunkingMode></vlan></allowed>");
         rpc.append("</trunk></switchport></ConfigIf-Configuration>");
         rpc.append("</interface>");
@@ -296,5 +297,23 @@
         return rpc.toString();
     }
 
+    /**
+     * Builds a string with comma separated VLAN-IDs.
+     * @param vlanIds the VLAN IDs
+     * @return the string including the VLAN-IDs
+     */
+    private String getVlansString(List<VlanId> vlanIds) {
+        StringBuilder vlansStringBuilder = new StringBuilder();
+
+        for (int i = 0; i < vlanIds.size(); i++) {
+            vlansStringBuilder.append(vlanIds.get(i));
+
+            if (i != vlanIds.size() - 1) {
+                vlansStringBuilder.append(",");
+            }
+        }
+        return  vlansStringBuilder.toString();
+    }
+
 }