ONOS-4534 Improve VLAN support in SDN-IP

Change-Id: Ib9cf64d8f896462ec18260c4371859f447e7c4de
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java
index 6ed06ae..8a68db2 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java
@@ -202,15 +202,14 @@
     }
 
     /**
-     * Builds the required intents between the two pairs of connect points and
-     * IP addresses.
+     * Builds the required intents between a BGP speaker and an external router.
      *
-     * @param portOne the first connect point
-     * @param vlanOne the ingress VLAN
-     * @param ipOne the first IP address
-     * @param portTwo the second connect point
-     * @param vlanTwo the egress VLAN
-     * @param ipTwo the second IP address
+     * @param portOne the BGP speaker connect point
+     * @param vlanOne the BGP speaker VLAN
+     * @param ipOne the BGP speaker IP address
+     * @param portTwo the external BGP peer connect point
+     * @param vlanTwo the external BGP peer VLAN
+     * @param ipTwo the external BGP peer IP address
      * @return the intents to install
      */
     private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne,
@@ -239,9 +238,13 @@
             icmpProtocol = IPv6.PROTOCOL_ICMP6;
         }
 
-        // Add treatment for VLAN for traffic going from BGP speaker to BGP peer
-        if (!vlanTwo.equals(VlanId.NONE)) {
-            treatmentToPeer.setVlanId(vlanTwo);
+        // Add VLAN treatment for traffic going from BGP speaker to BGP peer
+        if (!vlanOne.equals(vlanTwo)) {
+            if (vlanTwo.equals(VlanId.NONE)) {
+                treatmentToPeer.popVlan();
+            } else {
+                treatmentToPeer.setVlanId(vlanTwo);
+            }
         }
 
         // Path from BGP speaker to BGP peer matching destination TCP port 179
@@ -305,9 +308,13 @@
                             .priority(PRIORITY_OFFSET)
                             .build());
 
-        // Add treatment for VLAN for traffic going from BGP peer to BGP speaker
-        if (!vlanOne.equals(VlanId.NONE)) {
-            treatmentToSpeaker.setVlanId(vlanOne);
+        // Add VLAN treatment for traffic going from BGP peer to BGP speaker
+        if (!vlanTwo.equals(vlanOne)) {
+            if (vlanOne.equals(VlanId.NONE)) {
+                treatmentToSpeaker.popVlan();
+            } else {
+                treatmentToSpeaker.setVlanId(vlanOne);
+            }
         }
 
         // Path from BGP peer to BGP speaker matching destination TCP port 179
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java
index 1179c3d..f326d70 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java
@@ -158,23 +158,38 @@
                     nextHopIpAddress);
             return null;
         }
-
-        // Generate the intent itself
-        Set<ConnectPoint> ingressPorts = new HashSet<>();
         ConnectPoint egressPort = egressInterface.connectPoint();
+
+        Set<Interface> ingressInterfaces = new HashSet<>();
+        Set<ConnectPoint> ingressPorts = new HashSet<>();
         log.debug("Generating intent for prefix {}, next hop mac {}",
                 prefix, nextHopMacAddress);
 
-        for (Interface intf : interfaceService.getInterfaces()) {
-            // TODO this should be only peering interfaces
-            if (!intf.connectPoint().equals(egressInterface.connectPoint())) {
-                ConnectPoint srcPort = intf.connectPoint();
-                ingressPorts.add(srcPort);
+        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
+
+        // Get ingress interfaces and ports
+        // TODO this should be only peering interfaces
+        interfaceService.getInterfaces().stream()
+                .filter(intf -> !intf.equals(egressInterface))
+                .forEach(intf -> {
+                    ingressInterfaces.add(intf);
+                    ConnectPoint ingressPort = intf.connectPoint();
+                    ingressPorts.add(ingressPort);
+                });
+
+        // By default the ingress traffic is not tagged
+        VlanId ingressVlanId = VlanId.NONE;
+
+        // Match VLAN Id ANY if the source VLAN Id is not null
+        // TODO need to be able to set a different VLAN Id per ingress interface
+        for (Interface intf : ingressInterfaces) {
+            if (!intf.vlan().equals(VlanId.NONE)) {
+                selector.matchVlanId(VlanId.ANY);
+                ingressVlanId = intf.vlan();
             }
         }
 
         // Match the destination IP prefix at the first hop
-        TrafficSelector.Builder selector = DefaultTrafficSelector.builder();
         if (prefix.isIp4()) {
             selector.matchEthType(Ethernet.TYPE_IPV4);
             // if it is default route, then we do not need match destination
@@ -189,22 +204,29 @@
             if (prefix.prefixLength() != 0) {
                 selector.matchIPv6Dst(prefix);
             }
-
         }
 
         // Rewrite the destination MAC address
         TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder()
                 .setEthDst(nextHopMacAddress);
-        if (!egressInterface.vlan().equals(VlanId.NONE)) {
-            treatment.setVlanId(egressInterface.vlan());
-            // If we set VLAN ID, we have to make sure a VLAN tag exists.
-            // TODO support no VLAN -> VLAN routing
-            selector.matchVlanId(VlanId.ANY);
+
+        // Set egress VLAN Id
+        // TODO need to make the comparison with different ingress VLAN Ids
+        if (!ingressVlanId.equals(egressInterface.vlan())) {
+            if (egressInterface.vlan().equals(VlanId.NONE)) {
+                treatment.popVlan();
+            } else {
+                treatment.setVlanId(egressInterface.vlan());
+            }
         }
 
+        // Set priority
         int priority =
                 prefix.prefixLength() * PRIORITY_MULTIPLIER + PRIORITY_OFFSET;
+
+        // Set key
         Key key = Key.of(prefix.toString(), appId);
+
         return MultiPointToSinglePointIntent.builder()
                 .appId(appId)
                 .key(key)
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVLansTest.java
similarity index 78%
rename from apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibTest.java
rename to apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVLansTest.java
index 7c2be29..29a548b 100644
--- a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibTest.java
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVLansTest.java
@@ -68,7 +68,7 @@
 /**
  * Unit tests for SdnIpFib.
  */
-public class SdnIpFibTest extends AbstractIntentTest {
+public class SdnIpFibNoVLansTest extends AbstractIntentTest {
 
     private InterfaceService interfaceService;
 
@@ -80,12 +80,12 @@
             DeviceId.deviceId("of:0000000000000002"),
             PortNumber.portNumber(1));
 
-    private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
-            DeviceId.deviceId("of:0000000000000004"),
+    private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000003"),
             PortNumber.portNumber(1));
 
-    private static final ConnectPoint SW5_ETH1 = new ConnectPoint(
-            DeviceId.deviceId("of:0000000000000005"),
+    private static final ConnectPoint SW4_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000004"),
             PortNumber.portNumber(1));
 
     private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
@@ -104,6 +104,7 @@
         super.setUp();
 
         interfaceService = createMock(InterfaceService.class);
+
         interfaceService.addListener(anyObject(InterfaceListener.class));
         expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
 
@@ -124,29 +125,29 @@
     }
 
     /**
-     * Sets up InterfaceService.
+     * Sets up the interface service.
      */
     private void setUpInterfaceService() {
         List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList();
         interfaceIpAddresses1.add(InterfaceIpAddress.valueOf("192.168.10.101/24"));
         Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1,
-                interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
-                VlanId.NONE);
+                                          interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
+                                          VlanId.NONE);
         interfaces.add(sw1Eth1);
 
         List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList();
         interfaceIpAddresses2.add(InterfaceIpAddress.valueOf("192.168.20.101/24"));
         Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1,
-                interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
-                VlanId.NONE);
+                                          interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
+                                          VlanId.NONE);
         interfaces.add(sw2Eth1);
 
-        InterfaceIpAddress interfaceIpAddress4 = InterfaceIpAddress.valueOf("192.168.40.101/24");
-        Interface sw4Eth1 = new Interface("sw4-eth1", SW4_ETH1,
-                Lists.newArrayList(interfaceIpAddress4),
-                MacAddress.valueOf("00:00:00:00:00:04"),
-                VlanId.vlanId((short) 1));
-        interfaces.add(sw4Eth1);
+        InterfaceIpAddress interfaceIpAddress3 = InterfaceIpAddress.valueOf("192.168.30.101/24");
+        Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1,
+                                          Lists.newArrayList(interfaceIpAddress3),
+                                          MacAddress.valueOf("00:00:00:00:00:03"),
+                                          VlanId.NONE);
+        interfaces.add(sw3Eth1);
 
         expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
                 Collections.singleton(sw1Eth1)).anyTimes();
@@ -156,24 +157,24 @@
                 Collections.singleton(sw2Eth1)).anyTimes();
         expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1")))
                 .andReturn(sw2Eth1).anyTimes();
-        expect(interfaceService.getInterfacesByPort(SW4_ETH1)).andReturn(
-                Collections.singleton(sw4Eth1)).anyTimes();
-        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.40.1")))
-                .andReturn(sw4Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
+                Collections.singleton(sw3Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1")))
+                .andReturn(sw3Eth1).anyTimes();
         expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
     }
 
     /**
-     * Tests adding a route.
+     * Tests adding a route. All interfaces have no VLAN Ids configured.
      *
      * We verify that the synchronizer records the correct state and that the
      * correct intent is submitted to the IntentService.
      */
     @Test
-    public void testRouteAdd() {
+    public void testRouteAddNoVlans() {
         ResolvedRoute route = new ResolvedRoute(PREFIX1,
-                Ip4Address.valueOf("192.168.10.1"),
-                MacAddress.valueOf("00:00:00:00:00:01"));
+                                                Ip4Address.valueOf("192.168.30.1"),
+                                                MacAddress.valueOf("00:00:00:00:00:03"));
 
         // Construct a MultiPointToSinglePointIntent intent
         TrafficSelector.Builder selectorBuilder =
@@ -182,56 +183,7 @@
 
         TrafficTreatment.Builder treatmentBuilder =
                 DefaultTrafficTreatment.builder();
-        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
-
-        Set<ConnectPoint> ingressPoints = new HashSet<>();
-        ingressPoints.add(SW2_ETH1);
-        ingressPoints.add(SW4_ETH1);
-
-        MultiPointToSinglePointIntent intent =
-                MultiPointToSinglePointIntent.builder()
-                        .appId(APPID)
-                        .key(Key.of(PREFIX1.toString(), APPID))
-                        .selector(selectorBuilder.build())
-                        .treatment(treatmentBuilder.build())
-                        .ingressPoints(ingressPoints)
-                        .egressPoint(SW1_ETH1)
-                        .constraints(SdnIpFib.CONSTRAINTS)
-                        .build();
-
-        // Setup the expected intents
-        intentSynchronizer.submit(eqExceptId(intent));
-        replay(intentSynchronizer);
-
-        // Send in the added event
-        routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
-
-        verify(intentSynchronizer);
-    }
-
-    /**
-     * Tests adding a route with to a next hop in a VLAN.
-     *
-     * We verify that the synchronizer records the correct state and that the
-     * correct intent is submitted to the IntentService.
-     */
-    @Test
-    public void testRouteAddWithVlan() {
-        ResolvedRoute route = new ResolvedRoute(PREFIX1,
-                Ip4Address.valueOf("192.168.40.1"),
-                MacAddress.valueOf("00:00:00:00:00:04"));
-
-        // Construct a MultiPointToSinglePointIntent intent
-        TrafficSelector.Builder selectorBuilder =
-                DefaultTrafficSelector.builder();
-        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
-                .matchIPDst(PREFIX1)
-                .matchVlanId(VlanId.ANY);
-
-        TrafficTreatment.Builder treatmentBuilder =
-                DefaultTrafficTreatment.builder();
-        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:04"))
-                .setVlanId(VlanId.vlanId((short) 1));
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"));
 
         Set<ConnectPoint> ingressPoints = new HashSet<>();
         ingressPoints.add(SW1_ETH1);
@@ -244,13 +196,12 @@
                         .selector(selectorBuilder.build())
                         .treatment(treatmentBuilder.build())
                         .ingressPoints(ingressPoints)
-                        .egressPoint(SW4_ETH1)
+                        .egressPoint(SW3_ETH1)
                         .constraints(SdnIpFib.CONSTRAINTS)
                         .build();
 
         // Setup the expected intents
         intentSynchronizer.submit(eqExceptId(intent));
-
         replay(intentSynchronizer);
 
         // Send in the added event
@@ -267,8 +218,8 @@
      */
     @Test
     public void testRouteUpdate() {
-        // Firstly add a route
-        testRouteAdd();
+        // Add a route first
+        testRouteAddNoVlans();
 
         // Start to construct a new route entry and new intent
         ResolvedRoute route = new ResolvedRoute(PREFIX1,
@@ -286,7 +237,7 @@
 
         Set<ConnectPoint> ingressPointsNew = new HashSet<>();
         ingressPointsNew.add(SW1_ETH1);
-        ingressPointsNew.add(SW4_ETH1);
+        ingressPointsNew.add(SW3_ETH1);
 
         MultiPointToSinglePointIntent intentNew =
                 MultiPointToSinglePointIntent.builder()
@@ -320,8 +271,8 @@
      */
     @Test
     public void testRouteDelete() {
-        // Firstly add a route
-        testRouteAdd();
+        // Add a route first
+        testRouteAddNoVlans();
 
         // Construct the existing route entry
         ResolvedRoute route = new ResolvedRoute(PREFIX1, null, null);
@@ -333,11 +284,11 @@
 
         TrafficTreatment.Builder treatmentBuilder =
                 DefaultTrafficTreatment.builder();
-        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"));
 
         Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
         ingressPoints.add(SW2_ETH1);
-        ingressPoints.add(SW4_ETH1);
 
         MultiPointToSinglePointIntent addedIntent =
                 MultiPointToSinglePointIntent.builder()
@@ -346,7 +297,7 @@
                         .selector(selectorBuilder.build())
                         .treatment(treatmentBuilder.build())
                         .ingressPoints(ingressPoints)
-                        .egressPoint(SW1_ETH1)
+                        .egressPoint(SW3_ETH1)
                         .constraints(SdnIpFib.CONSTRAINTS)
                         .build();
 
@@ -364,7 +315,8 @@
 
     @Test
     public void testAddInterface() {
-        testRouteAdd();
+        // Add a route first
+        testRouteAddNoVlans();
 
         // Construct the existing MultiPointToSinglePoint intent
         TrafficSelector.Builder selectorBuilder =
@@ -373,12 +325,12 @@
 
         TrafficTreatment.Builder treatmentBuilder =
                 DefaultTrafficTreatment.builder();
-        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"));
 
         Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
         ingressPoints.add(SW2_ETH1);
         ingressPoints.add(SW4_ETH1);
-        ingressPoints.add(SW5_ETH1);
 
         MultiPointToSinglePointIntent addedIntent =
                 MultiPointToSinglePointIntent.builder()
@@ -387,7 +339,7 @@
                         .selector(selectorBuilder.build())
                         .treatment(treatmentBuilder.build())
                         .ingressPoints(ingressPoints)
-                        .egressPoint(SW1_ETH1)
+                        .egressPoint(SW3_ETH1)
                         .constraints(SdnIpFib.CONSTRAINTS)
                         .build();
 
@@ -398,9 +350,9 @@
 
         replay(intentSynchronizer);
 
-        Interface intf = new Interface("newintf", SW5_ETH1,
-                Collections.singletonList(InterfaceIpAddress.valueOf("192.168.50.101/24")),
-                MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE);
+        Interface intf = new Interface("sw4-eth1", SW4_ETH1,
+                Collections.singletonList(InterfaceIpAddress.valueOf("192.168.40.101/24")),
+                MacAddress.valueOf("00:00:00:00:00:04"), VlanId.NONE);
         InterfaceEvent intfEvent = new InterfaceEvent(InterfaceEvent.Type.INTERFACE_ADDED, intf);
         interfaceListener.event(intfEvent);
 
@@ -409,7 +361,8 @@
 
     @Test
     public void testRemoveInterface() {
-        testRouteAdd();
+        // Add a route first
+        testRouteAddNoVlans();
 
         // Construct the existing MultiPointToSinglePoint intent
         TrafficSelector.Builder selectorBuilder =
@@ -418,7 +371,7 @@
 
         TrafficTreatment.Builder treatmentBuilder =
                 DefaultTrafficTreatment.builder();
-        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:01"));
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"));
 
         Set<ConnectPoint> ingressPoints = new HashSet<>();
         ingressPoints.add(SW2_ETH1);
@@ -430,7 +383,7 @@
                         .selector(selectorBuilder.build())
                         .treatment(treatmentBuilder.build())
                         .ingressPoints(ingressPoints)
-                        .egressPoint(SW1_ETH1)
+                        .egressPoint(SW3_ETH1)
                         .constraints(SdnIpFib.CONSTRAINTS)
                         .build();
 
@@ -441,9 +394,9 @@
 
         replay(intentSynchronizer);
 
-        Interface intf = new Interface("newintf", SW4_ETH1,
-                Collections.singletonList(InterfaceIpAddress.valueOf("192.168.50.101/24")),
-                MacAddress.valueOf("00:00:00:00:00:02"), VlanId.NONE);
+        Interface intf = new Interface("sw1-eth1", SW1_ETH1,
+                Collections.singletonList(InterfaceIpAddress.valueOf("192.168.10.101/24")),
+                MacAddress.valueOf("00:00:00:00:00:01"), VlanId.NONE);
         InterfaceEvent intfEvent = new InterfaceEvent(InterfaceEvent.Type.INTERFACE_REMOVED, intf);
         interfaceListener.event(intfEvent);
 
@@ -460,14 +413,14 @@
     private class TestRouteService extends RouteServiceAdapter {
         @Override
         public void addListener(RouteListener routeListener) {
-            SdnIpFibTest.this.routeListener = routeListener;
+            SdnIpFibNoVLansTest.this.routeListener = routeListener;
         }
     }
 
     private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
         @Override
         public void addListener(InterfaceListener listener) {
-            SdnIpFibTest.this.interfaceListener = listener;
+            SdnIpFibNoVLansTest.this.interfaceListener = listener;
         }
     }
 }
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVlanstoVlanTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVlanstoVlanTest.java
new file mode 100644
index 0000000..0358881
--- /dev/null
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibNoVlanstoVlanTest.java
@@ -0,0 +1,225 @@
+/*
+ * Copyright 2015-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.sdnip;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceListener;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
+import org.onosproject.incubator.net.routing.ResolvedRoute;
+import org.onosproject.incubator.net.routing.RouteEvent;
+import org.onosproject.incubator.net.routing.RouteListener;
+import org.onosproject.incubator.net.routing.RouteServiceAdapter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.host.InterfaceIpAddress;
+import org.onosproject.net.intent.AbstractIntentTest;
+import org.onosproject.net.intent.Key;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.routing.IntentSynchronizationService;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId;
+
+/**
+ * Unit tests for SdnIpFib.
+ */
+public class SdnIpFibNoVlanstoVlanTest extends AbstractIntentTest {
+
+    private InterfaceService interfaceService;
+
+    private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000001"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000002"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000003"),
+            PortNumber.portNumber(1));
+
+    private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
+
+    private SdnIpFib sdnipFib;
+    private IntentSynchronizationService intentSynchronizer;
+    private final Set<Interface> interfaces = Sets.newHashSet();
+
+    private static final ApplicationId APPID = TestApplicationId.create("SDNIP");
+
+    private RouteListener routeListener;
+    private InterfaceListener interfaceListener;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        interfaceService = createMock(InterfaceService.class);
+
+        interfaceService.addListener(anyObject(InterfaceListener.class));
+        expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
+
+        // These will set expectations on routingConfig and interfaceService
+        setUpInterfaceService();
+
+        replay(interfaceService);
+
+        intentSynchronizer = createMock(IntentSynchronizationService.class);
+
+        sdnipFib = new SdnIpFib();
+        sdnipFib.routeService = new TestRouteService();
+        sdnipFib.coreService = new TestCoreService();
+        sdnipFib.interfaceService = interfaceService;
+        sdnipFib.intentSynchronizer = intentSynchronizer;
+
+        sdnipFib.activate();
+    }
+
+    /**
+     * Sets up the interface service.
+     */
+    private void setUpInterfaceService() {
+        List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList();
+        interfaceIpAddresses1.add(InterfaceIpAddress.valueOf("192.168.10.101/24"));
+        Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1,
+                                          interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
+                                          VlanId.NONE);
+        interfaces.add(sw1Eth1);
+
+        List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList();
+        interfaceIpAddresses2.add(InterfaceIpAddress.valueOf("192.168.20.101/24"));
+        Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1,
+                                          interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
+                                          VlanId.NONE);
+        interfaces.add(sw2Eth1);
+
+        InterfaceIpAddress interfaceIpAddress3 = InterfaceIpAddress.valueOf("192.168.30.101/24");
+        Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1,
+                                          Lists.newArrayList(interfaceIpAddress3),
+                                          MacAddress.valueOf("00:00:00:00:00:03"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw3Eth1);
+
+        expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
+                Collections.singleton(sw1Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1")))
+                .andReturn(sw1Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn(
+                Collections.singleton(sw2Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1")))
+                .andReturn(sw2Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
+                Collections.singleton(sw3Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1")))
+                .andReturn(sw3Eth1).anyTimes();
+        expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
+    }
+
+    /**
+     * Tests adding a route. Ingresses with no VLAN and next hop with VLAN.
+     *
+     * We verify that the synchronizer records the correct state and that the
+     * correct intent is submitted to the IntentService.
+     */
+    @Test
+    public void testRouteAdd() {
+        ResolvedRoute route = new ResolvedRoute(PREFIX1,
+                Ip4Address.valueOf("192.168.30.1"),
+                MacAddress.valueOf("00:00:00:00:00:03"));
+
+        // Construct a MultiPointToSinglePointIntent intent
+        TrafficSelector.Builder selectorBuilder =
+                DefaultTrafficSelector.builder();
+        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4)
+                .matchIPDst(PREFIX1);
+
+        TrafficTreatment.Builder treatmentBuilder =
+                DefaultTrafficTreatment.builder();
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
+                .setVlanId(VlanId.vlanId((short) 1));
+
+        Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
+        ingressPoints.add(SW2_ETH1);
+
+        MultiPointToSinglePointIntent intent =
+                MultiPointToSinglePointIntent.builder()
+                        .appId(APPID)
+                        .key(Key.of(PREFIX1.toString(), APPID))
+                        .selector(selectorBuilder.build())
+                        .treatment(treatmentBuilder.build())
+                        .ingressPoints(ingressPoints)
+                        .egressPoint(SW3_ETH1)
+                        .constraints(SdnIpFib.CONSTRAINTS)
+                        .build();
+
+        // Setup the expected intents
+        intentSynchronizer.submit(eqExceptId(intent));
+
+        replay(intentSynchronizer);
+
+        // Send in the added event
+        routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
+
+        verify(intentSynchronizer);
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+        @Override
+        public ApplicationId getAppId(String name) {
+            return APPID;
+        }
+    }
+
+    private class TestRouteService extends RouteServiceAdapter {
+        @Override
+        public void addListener(RouteListener routeListener) {
+            SdnIpFibNoVlanstoVlanTest.this.routeListener = routeListener;
+        }
+    }
+
+    private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
+        @Override
+        public void addListener(InterfaceListener listener) {
+            SdnIpFibNoVlanstoVlanTest.this.interfaceListener = listener;
+        }
+    }
+}
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanDifferentTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanDifferentTest.java
new file mode 100644
index 0000000..a5beb46
--- /dev/null
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanDifferentTest.java
@@ -0,0 +1,224 @@
+/*
+ * Copyright 2015-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.sdnip;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceListener;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
+import org.onosproject.incubator.net.routing.ResolvedRoute;
+import org.onosproject.incubator.net.routing.RouteEvent;
+import org.onosproject.incubator.net.routing.RouteListener;
+import org.onosproject.incubator.net.routing.RouteServiceAdapter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.host.InterfaceIpAddress;
+import org.onosproject.net.intent.AbstractIntentTest;
+import org.onosproject.net.intent.Key;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.routing.IntentSynchronizationService;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId;
+
+/**
+ * Unit tests for SdnIpFib.
+ */
+public class SdnIpFibVlansToVlanDifferentTest extends AbstractIntentTest {
+
+    private InterfaceService interfaceService;
+
+    private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000001"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000002"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000003"),
+            PortNumber.portNumber(1));
+
+    private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
+
+    private SdnIpFib sdnipFib;
+    private IntentSynchronizationService intentSynchronizer;
+    private final Set<Interface> interfaces = Sets.newHashSet();
+
+    private static final ApplicationId APPID = TestApplicationId.create("SDNIP");
+
+    private RouteListener routeListener;
+    private InterfaceListener interfaceListener;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        interfaceService = createMock(InterfaceService.class);
+
+        interfaceService.addListener(anyObject(InterfaceListener.class));
+        expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
+
+        // These will set expectations on routingConfig and interfaceService
+        setUpInterfaceService();
+
+        replay(interfaceService);
+
+        intentSynchronizer = createMock(IntentSynchronizationService.class);
+
+        sdnipFib = new SdnIpFib();
+        sdnipFib.routeService = new TestRouteService();
+        sdnipFib.coreService = new TestCoreService();
+        sdnipFib.interfaceService = interfaceService;
+        sdnipFib.intentSynchronizer = intentSynchronizer;
+
+        sdnipFib.activate();
+    }
+
+    /**
+     * Sets up the interface service.
+     */
+    private void setUpInterfaceService() {
+        List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList();
+        interfaceIpAddresses1.add(InterfaceIpAddress.valueOf("192.168.10.101/24"));
+        Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1,
+                                          interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw1Eth1);
+
+        List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList();
+        interfaceIpAddresses2.add(InterfaceIpAddress.valueOf("192.168.20.101/24"));
+        Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1,
+                                          interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw2Eth1);
+
+        InterfaceIpAddress interfaceIpAddress3 = InterfaceIpAddress.valueOf("192.168.30.101/24");
+        Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1,
+                                          Lists.newArrayList(interfaceIpAddress3),
+                                          MacAddress.valueOf("00:00:00:00:00:03"),
+                                          VlanId.vlanId((short) 2));
+        interfaces.add(sw3Eth1);
+
+        expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
+                Collections.singleton(sw1Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1")))
+                .andReturn(sw1Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn(
+                Collections.singleton(sw2Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1")))
+                .andReturn(sw2Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
+                Collections.singleton(sw3Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1")))
+                .andReturn(sw3Eth1).anyTimes();
+        expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
+    }
+
+    /**
+     * Tests adding a route. Ingresses with VLAN and next hop with no VLAN.
+     *
+     * We verify that the synchronizer records the correct state and that the
+     * correct intent is submitted to the IntentService.
+     */
+    @Test
+    public void testRouteAdd() {
+        ResolvedRoute route = new ResolvedRoute(PREFIX1,
+                Ip4Address.valueOf("192.168.30.1"),
+                MacAddress.valueOf("00:00:00:00:00:03"));
+
+        // Construct a MultiPointToSinglePointIntent intent
+        TrafficSelector.Builder selectorBuilder =
+                DefaultTrafficSelector.builder();
+        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(PREFIX1)
+                .matchVlanId(VlanId.ANY);
+
+        TrafficTreatment.Builder treatmentBuilder =
+                DefaultTrafficTreatment.builder();
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
+                .setVlanId(VlanId.vlanId((short) 2));
+
+        Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
+        ingressPoints.add(SW2_ETH1);
+
+        MultiPointToSinglePointIntent intent =
+                MultiPointToSinglePointIntent.builder()
+                        .appId(APPID)
+                        .key(Key.of(PREFIX1.toString(), APPID))
+                        .selector(selectorBuilder.build())
+                        .treatment(treatmentBuilder.build())
+                        .ingressPoints(ingressPoints)
+                        .egressPoint(SW3_ETH1)
+                        .constraints(SdnIpFib.CONSTRAINTS)
+                        .build();
+
+        // Setup the expected intents
+        intentSynchronizer.submit(eqExceptId(intent));
+        replay(intentSynchronizer);
+
+        // Send in the added event
+        routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
+
+        verify(intentSynchronizer);
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+        @Override
+        public ApplicationId getAppId(String name) {
+            return APPID;
+        }
+    }
+
+    private class TestRouteService extends RouteServiceAdapter {
+        @Override
+        public void addListener(RouteListener routeListener) {
+            SdnIpFibVlansToVlanDifferentTest.this.routeListener = routeListener;
+        }
+    }
+
+    private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
+        @Override
+        public void addListener(InterfaceListener listener) {
+            SdnIpFibVlansToVlanDifferentTest.this.interfaceListener = listener;
+        }
+    }
+}
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanSameTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanSameTest.java
new file mode 100644
index 0000000..c802c3a
--- /dev/null
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlansToVlanSameTest.java
@@ -0,0 +1,223 @@
+/*
+ * Copyright 2015-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.sdnip;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceListener;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
+import org.onosproject.incubator.net.routing.ResolvedRoute;
+import org.onosproject.incubator.net.routing.RouteEvent;
+import org.onosproject.incubator.net.routing.RouteListener;
+import org.onosproject.incubator.net.routing.RouteServiceAdapter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.host.InterfaceIpAddress;
+import org.onosproject.net.intent.AbstractIntentTest;
+import org.onosproject.net.intent.Key;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.routing.IntentSynchronizationService;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId;
+
+/**
+ * Unit tests for SdnIpFib.
+ */
+public class SdnIpFibVlansToVlanSameTest extends AbstractIntentTest {
+
+    private InterfaceService interfaceService;
+
+    private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000001"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000002"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000003"),
+            PortNumber.portNumber(1));
+
+    private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
+
+    private SdnIpFib sdnipFib;
+    private IntentSynchronizationService intentSynchronizer;
+    private final Set<Interface> interfaces = Sets.newHashSet();
+
+    private static final ApplicationId APPID = TestApplicationId.create("SDNIP");
+
+    private RouteListener routeListener;
+    private InterfaceListener interfaceListener;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        interfaceService = createMock(InterfaceService.class);
+
+        interfaceService.addListener(anyObject(InterfaceListener.class));
+        expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
+
+        // These will set expectations on routingConfig and interfaceService
+        setUpInterfaceService();
+
+        replay(interfaceService);
+
+        intentSynchronizer = createMock(IntentSynchronizationService.class);
+
+        sdnipFib = new SdnIpFib();
+        sdnipFib.routeService = new TestRouteService();
+        sdnipFib.coreService = new TestCoreService();
+        sdnipFib.interfaceService = interfaceService;
+        sdnipFib.intentSynchronizer = intentSynchronizer;
+
+        sdnipFib.activate();
+    }
+
+    /**
+     * Sets up the interface service.
+     */
+    private void setUpInterfaceService() {
+        List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList();
+        interfaceIpAddresses1.add(InterfaceIpAddress.valueOf("192.168.10.101/24"));
+        Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1,
+                                          interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw1Eth1);
+
+        List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList();
+        interfaceIpAddresses2.add(InterfaceIpAddress.valueOf("192.168.20.101/24"));
+        Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1,
+                                          interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw2Eth1);
+
+        InterfaceIpAddress interfaceIpAddress3 = InterfaceIpAddress.valueOf("192.168.30.101/24");
+        Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1,
+                                          Lists.newArrayList(interfaceIpAddress3),
+                                          MacAddress.valueOf("00:00:00:00:00:03"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw3Eth1);
+
+        expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
+                Collections.singleton(sw1Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1")))
+                .andReturn(sw1Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn(
+                Collections.singleton(sw2Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1")))
+                .andReturn(sw2Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
+                Collections.singleton(sw3Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1")))
+                .andReturn(sw3Eth1).anyTimes();
+        expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
+    }
+
+    /**
+     * Tests adding a route. Ingresses with VLAN and next hop with no VLAN.
+     *
+     * We verify that the synchronizer records the correct state and that the
+     * correct intent is submitted to the IntentService.
+     */
+    @Test
+    public void testRouteAdd() {
+        ResolvedRoute route = new ResolvedRoute(PREFIX1,
+                Ip4Address.valueOf("192.168.30.1"),
+                MacAddress.valueOf("00:00:00:00:00:03"));
+
+        // Construct a MultiPointToSinglePointIntent intent
+        TrafficSelector.Builder selectorBuilder =
+                DefaultTrafficSelector.builder();
+        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(PREFIX1)
+                .matchVlanId(VlanId.ANY);
+
+        TrafficTreatment.Builder treatmentBuilder =
+                DefaultTrafficTreatment.builder();
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"));
+
+        Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
+        ingressPoints.add(SW2_ETH1);
+
+        MultiPointToSinglePointIntent intent =
+                MultiPointToSinglePointIntent.builder()
+                        .appId(APPID)
+                        .key(Key.of(PREFIX1.toString(), APPID))
+                        .selector(selectorBuilder.build())
+                        .treatment(treatmentBuilder.build())
+                        .ingressPoints(ingressPoints)
+                        .egressPoint(SW3_ETH1)
+                        .constraints(SdnIpFib.CONSTRAINTS)
+                        .build();
+
+        // Setup the expected intents
+        intentSynchronizer.submit(eqExceptId(intent));
+        replay(intentSynchronizer);
+
+        // Send in the added event
+        routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
+
+        verify(intentSynchronizer);
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+        @Override
+        public ApplicationId getAppId(String name) {
+            return APPID;
+        }
+    }
+
+    private class TestRouteService extends RouteServiceAdapter {
+        @Override
+        public void addListener(RouteListener routeListener) {
+            SdnIpFibVlansToVlanSameTest.this.routeListener = routeListener;
+        }
+    }
+
+    private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
+        @Override
+        public void addListener(InterfaceListener listener) {
+            SdnIpFibVlansToVlanSameTest.this.interfaceListener = listener;
+        }
+    }
+}
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlanstoNoVlanTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlanstoNoVlanTest.java
new file mode 100644
index 0000000..bef2f1c
--- /dev/null
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/SdnIpFibVlanstoNoVlanTest.java
@@ -0,0 +1,224 @@
+/*
+ * Copyright 2015-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.sdnip;
+
+import com.google.common.collect.Lists;
+import com.google.common.collect.Sets;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Ethernet;
+import org.onlab.packet.Ip4Address;
+import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.packet.VlanId;
+import org.onosproject.TestApplicationId;
+import org.onosproject.core.ApplicationId;
+import org.onosproject.core.CoreServiceAdapter;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceListener;
+import org.onosproject.incubator.net.intf.InterfaceService;
+import org.onosproject.incubator.net.intf.InterfaceServiceAdapter;
+import org.onosproject.incubator.net.routing.ResolvedRoute;
+import org.onosproject.incubator.net.routing.RouteEvent;
+import org.onosproject.incubator.net.routing.RouteListener;
+import org.onosproject.incubator.net.routing.RouteServiceAdapter;
+import org.onosproject.net.ConnectPoint;
+import org.onosproject.net.DeviceId;
+import org.onosproject.net.PortNumber;
+import org.onosproject.net.flow.DefaultTrafficSelector;
+import org.onosproject.net.flow.DefaultTrafficTreatment;
+import org.onosproject.net.flow.TrafficSelector;
+import org.onosproject.net.flow.TrafficTreatment;
+import org.onosproject.net.host.InterfaceIpAddress;
+import org.onosproject.net.intent.AbstractIntentTest;
+import org.onosproject.net.intent.Key;
+import org.onosproject.net.intent.MultiPointToSinglePointIntent;
+import org.onosproject.routing.IntentSynchronizationService;
+
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import static org.easymock.EasyMock.*;
+import static org.onosproject.routing.TestIntentServiceHelper.eqExceptId;
+
+/**
+ * Unit tests for SdnIpFib.
+ */
+public class SdnIpFibVlanstoNoVlanTest extends AbstractIntentTest {
+
+    private InterfaceService interfaceService;
+
+    private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000001"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW2_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000002"),
+            PortNumber.portNumber(1));
+
+    private static final ConnectPoint SW3_ETH1 = new ConnectPoint(
+            DeviceId.deviceId("of:0000000000000003"),
+            PortNumber.portNumber(1));
+
+    private static final IpPrefix PREFIX1 = Ip4Prefix.valueOf("1.1.1.0/24");
+
+    private SdnIpFib sdnipFib;
+    private IntentSynchronizationService intentSynchronizer;
+    private final Set<Interface> interfaces = Sets.newHashSet();
+
+    private static final ApplicationId APPID = TestApplicationId.create("SDNIP");
+
+    private RouteListener routeListener;
+    private InterfaceListener interfaceListener;
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+
+        interfaceService = createMock(InterfaceService.class);
+
+        interfaceService.addListener(anyObject(InterfaceListener.class));
+        expectLastCall().andDelegateTo(new InterfaceServiceDelegate());
+
+        // These will set expectations on routingConfig and interfaceService
+        setUpInterfaceService();
+
+        replay(interfaceService);
+
+        intentSynchronizer = createMock(IntentSynchronizationService.class);
+
+        sdnipFib = new SdnIpFib();
+        sdnipFib.routeService = new TestRouteService();
+        sdnipFib.coreService = new TestCoreService();
+        sdnipFib.interfaceService = interfaceService;
+        sdnipFib.intentSynchronizer = intentSynchronizer;
+
+        sdnipFib.activate();
+    }
+
+    /**
+     * Sets up the interface service.
+     */
+    private void setUpInterfaceService() {
+        List<InterfaceIpAddress> interfaceIpAddresses1 = Lists.newArrayList();
+        interfaceIpAddresses1.add(InterfaceIpAddress.valueOf("192.168.10.101/24"));
+        Interface sw1Eth1 = new Interface("sw1-eth1", SW1_ETH1,
+                                          interfaceIpAddresses1, MacAddress.valueOf("00:00:00:00:00:01"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw1Eth1);
+
+        List<InterfaceIpAddress> interfaceIpAddresses2 = Lists.newArrayList();
+        interfaceIpAddresses2.add(InterfaceIpAddress.valueOf("192.168.20.101/24"));
+        Interface sw2Eth1 = new Interface("sw2-eth1", SW2_ETH1,
+                                          interfaceIpAddresses2, MacAddress.valueOf("00:00:00:00:00:02"),
+                                          VlanId.vlanId((short) 1));
+        interfaces.add(sw2Eth1);
+
+        InterfaceIpAddress interfaceIpAddress3 = InterfaceIpAddress.valueOf("192.168.30.101/24");
+        Interface sw3Eth1 = new Interface("sw3-eth1", SW3_ETH1,
+                                          Lists.newArrayList(interfaceIpAddress3),
+                                          MacAddress.valueOf("00:00:00:00:00:03"),
+                                          VlanId.NONE);
+        interfaces.add(sw3Eth1);
+
+        expect(interfaceService.getInterfacesByPort(SW1_ETH1)).andReturn(
+                Collections.singleton(sw1Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.10.1")))
+                .andReturn(sw1Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW2_ETH1)).andReturn(
+                Collections.singleton(sw2Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.20.1")))
+                .andReturn(sw2Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW3_ETH1)).andReturn(
+                Collections.singleton(sw3Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.30.1")))
+                .andReturn(sw3Eth1).anyTimes();
+        expect(interfaceService.getInterfaces()).andReturn(interfaces).anyTimes();
+    }
+
+    /**
+     * Tests adding a route. Ingresses with VLAN and next hop with no VLAN.
+     *
+     * We verify that the synchronizer records the correct state and that the
+     * correct intent is submitted to the IntentService.
+     */
+    @Test
+    public void testRouteAdd() {
+        ResolvedRoute route = new ResolvedRoute(PREFIX1,
+                Ip4Address.valueOf("192.168.30.1"),
+                MacAddress.valueOf("00:00:00:00:00:03"));
+
+        // Construct a MultiPointToSinglePointIntent intent
+        TrafficSelector.Builder selectorBuilder =
+                DefaultTrafficSelector.builder();
+        selectorBuilder.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(PREFIX1)
+                .matchVlanId(VlanId.ANY);
+
+        TrafficTreatment.Builder treatmentBuilder =
+                DefaultTrafficTreatment.builder();
+        treatmentBuilder.setEthDst(MacAddress.valueOf("00:00:00:00:00:03"))
+                .popVlan();
+
+        Set<ConnectPoint> ingressPoints = new HashSet<>();
+        ingressPoints.add(SW1_ETH1);
+        ingressPoints.add(SW2_ETH1);
+
+        MultiPointToSinglePointIntent intent =
+                MultiPointToSinglePointIntent.builder()
+                        .appId(APPID)
+                        .key(Key.of(PREFIX1.toString(), APPID))
+                        .selector(selectorBuilder.build())
+                        .treatment(treatmentBuilder.build())
+                        .ingressPoints(ingressPoints)
+                        .egressPoint(SW3_ETH1)
+                        .constraints(SdnIpFib.CONSTRAINTS)
+                        .build();
+
+        // Setup the expected intents
+        intentSynchronizer.submit(eqExceptId(intent));
+        replay(intentSynchronizer);
+
+        // Send in the added event
+        routeListener.event(new RouteEvent(RouteEvent.Type.ROUTE_ADDED, route));
+
+        verify(intentSynchronizer);
+    }
+
+    private class TestCoreService extends CoreServiceAdapter {
+        @Override
+        public ApplicationId getAppId(String name) {
+            return APPID;
+        }
+    }
+
+    private class TestRouteService extends RouteServiceAdapter {
+        @Override
+        public void addListener(RouteListener routeListener) {
+            SdnIpFibVlanstoNoVlanTest.this.routeListener = routeListener;
+        }
+    }
+
+    private class InterfaceServiceDelegate extends InterfaceServiceAdapter {
+        @Override
+        public void addListener(InterfaceListener listener) {
+            SdnIpFibVlanstoNoVlanTest.this.interfaceListener = listener;
+        }
+    }
+}