From 4cfa141fa101f96671caf85c4eee147bd8b66ca8 Mon Sep 17 00:00:00 2001
From: Gregor Maier <gregor.maier@bigswitch.com>
Date: Mon, 30 Jul 2012 14:44:15 -0700
Subject: [PATCH] Adding IDevice.getSwitchPortVlanIds()

---
 .../devicemanager/IDevice.java                |  8 ++++++
 .../devicemanager/internal/Device.java        | 16 ++++++++++++
 .../internal/DeviceManagerImplTest.java       | 26 +++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/src/main/java/net/floodlightcontroller/devicemanager/IDevice.java b/src/main/java/net/floodlightcontroller/devicemanager/IDevice.java
index dd5463821..95969f8c8 100644
--- a/src/main/java/net/floodlightcontroller/devicemanager/IDevice.java
+++ b/src/main/java/net/floodlightcontroller/devicemanager/IDevice.java
@@ -73,6 +73,14 @@ public interface IDevice {
      * @return an array containing all unique attachment points for the device
      */
     public SwitchPort[] getAttachmentPoints(boolean includeError);
+
+    /**
+     * Returns all unique VLAN IDs for the device that were observed on 
+     * the given switch port
+     * @param swp the switch port to query
+     * @return an array containing the unique VLAN IDs
+     */
+    public Short[] getSwitchPortVlanIds(SwitchPort swp);
     
     /**
      * Get the most recent timestamp for this device
diff --git a/src/main/java/net/floodlightcontroller/devicemanager/internal/Device.java b/src/main/java/net/floodlightcontroller/devicemanager/internal/Device.java
index dc7c0da7b..333777e73 100755
--- a/src/main/java/net/floodlightcontroller/devicemanager/internal/Device.java
+++ b/src/main/java/net/floodlightcontroller/devicemanager/internal/Device.java
@@ -33,6 +33,7 @@ import net.floodlightcontroller.devicemanager.IDevice;
 import net.floodlightcontroller.devicemanager.IEntityClass;
 import net.floodlightcontroller.devicemanager.SwitchPort;
 import static net.floodlightcontroller.devicemanager.SwitchPort.ErrorStatus.*;
+import net.floodlightcontroller.packet.Ethernet;
 import net.floodlightcontroller.topology.ITopologyService;
 
 /**
@@ -342,6 +343,21 @@ public class Device implements IDevice {
 
         return vals.toArray(new SwitchPort[vals.size()]);
     }
+    
+    @Override
+    public Short[] getSwitchPortVlanIds(SwitchPort swp) {
+        TreeSet<Short> vals = new TreeSet<Short>();
+        for (Entity e : entities) {
+            if (e.switchDPID == swp.getSwitchDPID() 
+                    && e.switchPort == swp.getPort()) {
+                if (e.getVlan() == null)
+                    vals.add(Ethernet.VLAN_UNTAGGED);
+                else
+                    vals.add(e.getVlan());
+            }
+        }
+        return vals.toArray(new Short[vals.size()]);
+    }
 
     @Override
     public Date getLastSeen() {
diff --git a/src/test/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImplTest.java b/src/test/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImplTest.java
index f0bc0cdbd..2d86734d4 100644
--- a/src/test/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImplTest.java
+++ b/src/test/java/net/floodlightcontroller/devicemanager/internal/DeviceManagerImplTest.java
@@ -1380,4 +1380,30 @@ public class DeviceManagerImplTest extends FloodlightTestCase {
         Arrays.sort(ips);
         assertArrayEquals(new Integer[] { 2, 42, 4242 }, ips);
     }
+    
+    // TODO: this test should really go into a separate class that collects
+    // unit tests for Device
+    @Test
+    public void testGetSwitchPortVlanId() {
+            Entity entity1 = new Entity(1L, (short)1, null, 10L, 1, new Date());
+            Entity entity2 = new Entity(1L, null, null, 10L, 1, new Date());
+            Entity entity3 = new Entity(1L, (short)3, null,  1L, 1, new Date());
+            Entity entity4 = new Entity(1L, (short)42, null,  1L, 1, new Date());
+            Entity[] entities = new Entity[] { entity1, entity2, 
+                                               entity3, entity4
+                                             };
+            Device d = new Device(null,1L, Arrays.asList(entities), null);
+            SwitchPort swp1x1 = new SwitchPort(1L, 1);
+            SwitchPort swp1x2 = new SwitchPort(1L, 2);
+            SwitchPort swp2x1 = new SwitchPort(2L, 1);
+            SwitchPort swp10x1 = new SwitchPort(10L, 1);
+            assertArrayEquals(new Short[] { -1, 1}, 
+                              d.getSwitchPortVlanIds(swp10x1));
+            assertArrayEquals(new Short[] { 3, 42}, 
+                              d.getSwitchPortVlanIds(swp1x1));
+            assertArrayEquals(new Short[0],
+                              d.getSwitchPortVlanIds(swp1x2));
+            assertArrayEquals(new Short[0],
+                              d.getSwitchPortVlanIds(swp2x1));
+    }
 }
-- 
GitLab