Moved ProxyArp, SDN-IP and BgpRouter to use new config format.

The new config format is based on the new network configuration subsystem.

Includes a few config fixes to NetworkConfigLoader and InterfaceManager.

Change-Id: Id7f766736decb7afb6b63c2731d3baba9fc7c764
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
index cde59d1..e0e9faf 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/IntentSynchronizer.java
@@ -23,6 +23,8 @@
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.Host;
 import org.onosproject.net.flow.DefaultTrafficSelector;
@@ -43,8 +45,6 @@
 import org.onosproject.routing.FibListener;
 import org.onosproject.routing.FibUpdate;
 import org.onosproject.routing.IntentRequestListener;
-import org.onosproject.routing.config.BgpPeer;
-import org.onosproject.routing.config.Interface;
 import org.onosproject.routing.config.RoutingConfigurationService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -81,6 +81,7 @@
     private final ApplicationId appId;
     private final IntentService intentService;
     private final HostService hostService;
+    private final InterfaceService interfaceService;
     private final Map<IntentKey, PointToPointIntent> peerIntents;
     private final Map<IpPrefix, MultiPointToSinglePointIntent> routeIntents;
 
@@ -104,10 +105,12 @@
      */
     IntentSynchronizer(ApplicationId appId, IntentService intentService,
                        HostService hostService,
-                       RoutingConfigurationService configService) {
+                       RoutingConfigurationService configService,
+                       InterfaceService interfaceService) {
         this.appId = appId;
         this.intentService = intentService;
         this.hostService = hostService;
+        this.interfaceService = interfaceService;
         peerIntents = new ConcurrentHashMap<>();
         routeIntents = new ConcurrentHashMap<>();
 
@@ -122,12 +125,7 @@
      * Starts the synchronizer.
      */
     public void start() {
-        bgpIntentsSynchronizerExecutor.execute(new Runnable() {
-            @Override
-            public void run() {
-                doIntentSynchronizationThread();
-            }
-        });
+        bgpIntentsSynchronizerExecutor.execute(this::doIntentSynchronizationThread);
     }
 
     /**
@@ -313,24 +311,11 @@
             MacAddress nextHopMacAddress) {
 
         // Find the attachment point (egress interface) of the next hop
-        Interface egressInterface;
-        if (configService.getBgpPeers().containsKey(nextHopIpAddress)) {
-            // Route to a peer
-            log.debug("Route to peer {}", nextHopIpAddress);
-            BgpPeer peer =
-                    configService.getBgpPeers().get(nextHopIpAddress);
-            egressInterface =
-                    configService.getInterface(peer.connectPoint());
-        } else {
-            // Route to non-peer
-            log.debug("Route to non-peer {}", nextHopIpAddress);
-            egressInterface =
-                    configService.getMatchingInterface(nextHopIpAddress);
-            if (egressInterface == null) {
-                log.warn("No outgoing interface found for {}",
-                         nextHopIpAddress);
-                return null;
-            }
+        Interface egressInterface = interfaceService.getMatchingInterface(nextHopIpAddress);
+        if (egressInterface == null) {
+            log.warn("No outgoing interface found for {}",
+                     nextHopIpAddress);
+            return null;
         }
 
         //
@@ -341,7 +326,8 @@
         log.debug("Generating intent for prefix {}, next hop mac {}",
                   prefix, nextHopMacAddress);
 
-        for (Interface intf : configService.getInterfaces()) {
+        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);
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 ce85945..1e9db5b 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/PeerConnectivityManager.java
@@ -22,6 +22,9 @@
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.TpPort;
 import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
@@ -29,11 +32,8 @@
 import org.onosproject.net.flow.TrafficTreatment;
 import org.onosproject.net.host.InterfaceIpAddress;
 import org.onosproject.net.intent.PointToPointIntent;
-import org.onosproject.routing.config.BgpPeer;
-import org.onosproject.routing.config.BgpSpeaker;
-import org.onosproject.routing.config.Interface;
-import org.onosproject.routing.config.InterfaceAddress;
-import org.onosproject.routing.config.RoutingConfigurationService;
+import org.onosproject.routing.RoutingService;
+import org.onosproject.routing.config.BgpConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,6 +41,8 @@
 import java.util.Collection;
 import java.util.List;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 /**
  * Manages the connectivity requirements between peers.
  */
@@ -53,9 +55,11 @@
     private static final short BGP_PORT = 179;
 
     private final IntentSynchronizer intentSynchronizer;
-    private final RoutingConfigurationService configService;
+    private final NetworkConfigService configService;
+    private final InterfaceService interfaceService;
 
     private final ApplicationId appId;
+    private final ApplicationId routerAppId;
 
     /**
      * Creates a new PeerConnectivityManager.
@@ -66,28 +70,20 @@
      */
     public PeerConnectivityManager(ApplicationId appId,
                                    IntentSynchronizer intentSynchronizer,
-                                   RoutingConfigurationService configService) {
+                                   NetworkConfigService configService,
+                                   ApplicationId routerAppId,
+                                   InterfaceService interfaceService) {
         this.appId = appId;
         this.intentSynchronizer = intentSynchronizer;
         this.configService = configService;
+        this.routerAppId = routerAppId;
+        this.interfaceService = interfaceService;
     }
 
     /**
      * Starts the peer connectivity manager.
      */
     public void start() {
-        if (configService.getInterfaces().isEmpty()) {
-            log.warn("No interfaces found in configuration file");
-        }
-
-        if (configService.getBgpPeers().isEmpty()) {
-            log.warn("No BGP peers found in configuration file");
-        }
-
-        if (configService.getBgpSpeakers().isEmpty()) {
-            log.error("No BGP speakers found in configuration file");
-        }
-
         setUpConnectivity();
     }
 
@@ -99,78 +95,74 @@
 
     /**
      * Sets up paths to establish connectivity between all internal
-     * {@link BgpSpeaker}s and all external {@link BgpPeer}s.
+     * BGP speakers and external BGP peers.
      */
     private void setUpConnectivity() {
         List<PointToPointIntent> intents = new ArrayList<>();
 
-        for (BgpSpeaker bgpSpeaker : configService.getBgpSpeakers()
-                .values()) {
+        BgpConfig config = configService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
+
+        if (config == null) {
+            log.warn("No BgpConfig found");
+            return;
+        }
+
+        for (BgpConfig.BgpSpeakerConfig bgpSpeaker : config.bgpSpeakers()) {
             log.debug("Start to set up BGP paths for BGP speaker: {}",
-                      bgpSpeaker);
+                    bgpSpeaker);
 
-            for (BgpPeer bgpPeer : configService.getBgpPeers().values()) {
-
-                log.debug("Start to set up BGP paths between BGP speaker: {} "
-                                  + "to BGP peer: {}", bgpSpeaker, bgpPeer);
-
-                intents.addAll(buildPeerIntents(bgpSpeaker, bgpPeer));
-            }
+            intents.addAll(buildSpeakerIntents(bgpSpeaker));
         }
 
         // Submit all the intents.
         intentSynchronizer.submitPeerIntents(intents);
     }
 
-    /**
-     * Builds the required intents between a given internal BGP speaker and
-     * external BGP peer.
-     *
-     * @param bgpSpeaker the BGP speaker
-     * @param bgpPeer the BGP peer
-     * @return the intents to install
-     */
-    private Collection<PointToPointIntent> buildPeerIntents(
-            BgpSpeaker bgpSpeaker,
-            BgpPeer bgpPeer) {
+    private Collection<PointToPointIntent> buildSpeakerIntents(BgpConfig.BgpSpeakerConfig speaker) {
         List<PointToPointIntent> intents = new ArrayList<>();
 
-        ConnectPoint bgpdConnectPoint = bgpSpeaker.connectPoint();
+        for (IpAddress peerAddress : speaker.peers()) {
+            Interface peeringInterface = interfaceService.getMatchingInterface(peerAddress);
 
-        List<InterfaceAddress> interfaceAddresses =
-                bgpSpeaker.interfaceAddresses();
-
-        IpAddress bgpdAddress = null;
-        for (InterfaceAddress interfaceAddress : interfaceAddresses) {
-            Interface peerInterface = configService.getInterface(interfaceAddress.ipAddress());
-            if (peerInterface == null) {
+            if (peeringInterface == null) {
+                log.debug("No peering interface found for peer {} on speaker {}",
+                        peerAddress, speaker);
                 continue;
             }
 
-            for (InterfaceIpAddress interfaceIpAddress : peerInterface.ipAddresses()) {
-                // Only add intents where the peer and ONOS's addresses are
-                // in the same subnet
-                if (interfaceIpAddress.subnetAddress().contains(bgpPeer.ipAddress())) {
-                    bgpdAddress = interfaceAddress.ipAddress();
+            IpAddress peeringAddress = null;
+            for (InterfaceIpAddress address : peeringInterface.ipAddresses()) {
+                if (address.subnetAddress().contains(peerAddress)) {
+                    peeringAddress = address.ipAddress();
                     break;
                 }
             }
-            if (bgpdAddress != null) {
-                break;
-            }
-        }
-        if (bgpdAddress == null) {
-            log.debug("No IP address found for peer {} on interface {}",
-                    bgpPeer, bgpPeer.connectPoint());
-            return intents;
+
+            checkNotNull(peeringAddress);
+
+            intents.addAll(buildIntents(speaker.connectPoint(), peeringAddress,
+                    peeringInterface.connectPoint(), peerAddress));
         }
 
-        IpAddress bgpdPeerAddress = bgpPeer.ipAddress();
-        ConnectPoint bgpdPeerConnectPoint = bgpPeer.connectPoint();
+        return intents;
+    }
 
-        if (bgpdAddress.version() != bgpdPeerAddress.version()) {
-            return intents;
-        }
+    /**
+     * Builds the required intents between the two pairs of connect points and
+     * IP addresses.
+     *
+     * @param portOne the first connect point
+     * @param ipOne the first IP address
+     * @param portTwo the second connect point
+     * @param ipTwo the second IP address
+     * @return the intents to install
+     */
+    private Collection<PointToPointIntent> buildIntents(ConnectPoint portOne,
+                                                        IpAddress ipOne,
+                                                        ConnectPoint portTwo,
+                                                        IpAddress ipTwo) {
+
+        List<PointToPointIntent> intents = new ArrayList<>();
 
         TrafficTreatment treatment = DefaultTrafficTreatment.emptyTreatment();
 
@@ -179,7 +171,7 @@
         byte tcpProtocol;
         byte icmpProtocol;
 
-        if (bgpdAddress.isIp4()) {
+        if (ipOne.isIp4()) {
             tcpProtocol = IPv4.PROTOCOL_TCP;
             icmpProtocol = IPv4.PROTOCOL_ICMP;
         } else {
@@ -189,26 +181,24 @@
 
         // Path from BGP speaker to BGP peer matching destination TCP port 179
         selector = buildSelector(tcpProtocol,
-                bgpdAddress,
-                bgpdPeerAddress,
+                ipOne,
+                ipTwo,
                 null,
                 BGP_PORT);
 
-        int priority = PRIORITY_OFFSET;
-
         intents.add(PointToPointIntent.builder()
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdConnectPoint)
-                .egressPoint(bgpdPeerConnectPoint)
-                .priority(priority)
+                .ingressPoint(portOne)
+                .egressPoint(portTwo)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         // Path from BGP speaker to BGP peer matching source TCP port 179
         selector = buildSelector(tcpProtocol,
-                bgpdAddress,
-                bgpdPeerAddress,
+                ipOne,
+                ipTwo,
                 BGP_PORT,
                 null);
 
@@ -216,15 +206,15 @@
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdConnectPoint)
-                .egressPoint(bgpdPeerConnectPoint)
-                .priority(priority)
+                .ingressPoint(portOne)
+                .egressPoint(portTwo)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         // Path from BGP peer to BGP speaker matching destination TCP port 179
         selector = buildSelector(tcpProtocol,
-                bgpdPeerAddress,
-                bgpdAddress,
+                ipTwo,
+                ipOne,
                 null,
                 BGP_PORT);
 
@@ -232,15 +222,15 @@
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdPeerConnectPoint)
-                .egressPoint(bgpdConnectPoint)
-                .priority(priority)
+                .ingressPoint(portTwo)
+                .egressPoint(portOne)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         // Path from BGP peer to BGP speaker matching source TCP port 179
         selector = buildSelector(tcpProtocol,
-                bgpdPeerAddress,
-                bgpdAddress,
+                ipTwo,
+                ipOne,
                 BGP_PORT,
                 null);
 
@@ -248,15 +238,15 @@
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdPeerConnectPoint)
-                .egressPoint(bgpdConnectPoint)
-                .priority(priority)
+                .ingressPoint(portTwo)
+                .egressPoint(portOne)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         // ICMP path from BGP speaker to BGP peer
         selector = buildSelector(icmpProtocol,
-                bgpdAddress,
-                bgpdPeerAddress,
+                ipOne,
+                ipTwo,
                 null,
                 null);
 
@@ -264,15 +254,15 @@
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdConnectPoint)
-                .egressPoint(bgpdPeerConnectPoint)
-                .priority(priority)
+                .ingressPoint(portOne)
+                .egressPoint(portTwo)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         // ICMP path from BGP peer to BGP speaker
         selector = buildSelector(icmpProtocol,
-                bgpdPeerAddress,
-                bgpdAddress,
+                ipTwo,
+                ipOne,
                 null,
                 null);
 
@@ -280,9 +270,9 @@
                 .appId(appId)
                 .selector(selector)
                 .treatment(treatment)
-                .ingressPoint(bgpdPeerConnectPoint)
-                .egressPoint(bgpdConnectPoint)
-                .priority(priority)
+                .ingressPoint(portTwo)
+                .egressPoint(portOne)
+                .priority(PRIORITY_OFFSET)
                 .build());
 
         return intents;
@@ -301,24 +291,16 @@
     private TrafficSelector buildSelector(byte ipProto, IpAddress srcIp,
                                           IpAddress dstIp, Short srcTcpPort,
                                           Short dstTcpPort) {
-        TrafficSelector.Builder builder = null;
+        TrafficSelector.Builder builder = DefaultTrafficSelector.builder()
+                .matchEthType(Ethernet.TYPE_IPV4)
+                .matchIPProtocol(ipProto);
 
         if (dstIp.isIp4()) {
-            builder = DefaultTrafficSelector.builder()
-                    .matchEthType(Ethernet.TYPE_IPV4)
-                    .matchIPProtocol(ipProto)
-                    .matchIPSrc(IpPrefix.valueOf(srcIp,
-                            IpPrefix.MAX_INET_MASK_LENGTH))
-                    .matchIPDst(IpPrefix.valueOf(dstIp,
-                            IpPrefix.MAX_INET_MASK_LENGTH));
+            builder.matchIPSrc(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET_MASK_LENGTH))
+                    .matchIPDst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET_MASK_LENGTH));
         } else {
-            builder = DefaultTrafficSelector.builder()
-                    .matchEthType(Ethernet.TYPE_IPV6)
-                    .matchIPProtocol(ipProto)
-                    .matchIPv6Src(IpPrefix.valueOf(srcIp,
-                            IpPrefix.MAX_INET6_MASK_LENGTH))
-                    .matchIPv6Dst(IpPrefix.valueOf(dstIp,
-                            IpPrefix.MAX_INET6_MASK_LENGTH));
+            builder.matchIPv6Src(IpPrefix.valueOf(srcIp, IpPrefix.MAX_INET6_MASK_LENGTH))
+                    .matchIPv6Dst(IpPrefix.valueOf(dstIp, IpPrefix.MAX_INET6_MASK_LENGTH));
         }
 
         if (srcTcpPort != null) {
diff --git a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
index d97e28a..3d1fe65 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIp.java
@@ -26,9 +26,10 @@
 import org.onosproject.cluster.LeadershipEvent;
 import org.onosproject.cluster.LeadershipEventListener;
 import org.onosproject.cluster.LeadershipService;
-import org.onosproject.config.NetworkConfigService;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.core.CoreService;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.host.HostService;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.routing.RoutingService;
@@ -70,14 +71,12 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected RoutingConfigurationService config;
 
-    //
-    // NOTE: Unused reference - needed to guarantee that the
-    // NetworkConfigReader component is activated and the network configuration
-    // is read.
-    //
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected NetworkConfigService networkConfigService;
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected InterfaceService interfaceService;
+
     private IntentSynchronizer intentSynchronizer;
     private PeerConnectivityManager peerConnectivity;
 
@@ -96,12 +95,15 @@
 
         intentSynchronizer = new IntentSynchronizer(appId, intentService,
                                                     hostService,
-                                                    config);
+                                                    config,
+                                                    interfaceService);
         intentSynchronizer.start();
 
         peerConnectivity = new PeerConnectivityManager(appId,
                                                        intentSynchronizer,
-                                                       config);
+                                                       networkConfigService,
+                coreService.getAppId(RoutingService.ROUTER_APP_ID),
+                                                       interfaceService);
         peerConnectivity.start();
 
         routingService.addFibListener(intentSynchronizer);
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/IntentSyncTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/IntentSyncTest.java
index 2a52539..fc5782e 100644
--- a/apps/sdnip/src/test/java/org/onosproject/sdnip/IntentSyncTest.java
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/IntentSyncTest.java
@@ -28,6 +28,9 @@
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
@@ -45,7 +48,6 @@
 import org.onosproject.routing.FibUpdate;
 import org.onosproject.routing.RouteEntry;
 import org.onosproject.routing.config.BgpPeer;
-import org.onosproject.routing.config.Interface;
 import org.onosproject.routing.config.RoutingConfigurationService;
 import org.onosproject.sdnip.IntentSynchronizer.IntentKey;
 
@@ -56,7 +58,11 @@
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
-import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.createMock;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.reset;
+import static org.easymock.EasyMock.verify;
 import static org.hamcrest.Matchers.is;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
@@ -71,7 +77,9 @@
 public class IntentSyncTest extends AbstractIntentTest {
 
     private RoutingConfigurationService routingConfig;
+    private InterfaceService interfaceService;
     private IntentService intentService;
+    private NetworkConfigService configService;
 
     private static final ConnectPoint SW1_ETH1 = new ConnectPoint(
             DeviceId.deviceId("of:0000000000000001"),
@@ -90,6 +98,7 @@
             PortNumber.portNumber(1));
 
     private IntentSynchronizer intentSynchronizer;
+    private final Set<Interface> interfaces = Sets.newHashSet();
 
     private static final ApplicationId APPID = new ApplicationId() {
         @Override
@@ -108,17 +117,21 @@
         super.setUp();
 
         routingConfig = createMock(RoutingConfigurationService.class);
+        interfaceService = createMock(InterfaceService.class);
+        configService = createMock(NetworkConfigService.class);
 
         // These will set expectations on routingConfig
         setUpInterfaceService();
         setUpBgpPeers();
 
         replay(routingConfig);
+        replay(interfaceService);
 
         intentService = createMock(IntentService.class);
 
         intentSynchronizer = new IntentSynchronizer(APPID, intentService,
-                                                    null, routingConfig);
+                                                    null, routingConfig,
+                                                    interfaceService);
     }
 
     /**
@@ -152,9 +165,6 @@
      * Sets up InterfaceService.
      */
     private void setUpInterfaceService() {
-
-        Set<Interface> interfaces = Sets.newHashSet();
-
         Set<InterfaceIpAddress> interfaceIpAddresses1 = Sets.newHashSet();
         interfaceIpAddresses1.add(new InterfaceIpAddress(
                 IpAddress.valueOf("192.168.10.101"),
@@ -190,16 +200,26 @@
                                           MacAddress.valueOf("00:00:00:00:00:04"),
                                           VlanId.vlanId((short) 1));
 
-        expect(routingConfig.getInterface(SW4_ETH1)).andReturn(
-                sw4Eth1).anyTimes();
+        expect(interfaceService.getInterfacesByPort(SW4_ETH1)).andReturn(
+                Collections.singleton(sw4Eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(Ip4Address.valueOf("192.168.40.1")))
+                .andReturn(sw4Eth1).anyTimes();
+
         interfaces.add(sw4Eth1);
 
-        expect(routingConfig.getInterface(SW1_ETH1)).andReturn(
-                sw1Eth1).anyTimes();
-        expect(routingConfig.getInterface(SW2_ETH1)).andReturn(
-                sw2Eth1).anyTimes();
-        expect(routingConfig.getInterface(SW3_ETH1)).andReturn(sw3Eth1).anyTimes();
-        expect(routingConfig.getInterfaces()).andReturn(interfaces).anyTimes();
+        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();
     }
 
     /**
@@ -599,8 +619,8 @@
         treatmentBuilder.setEthDst(MacAddress.valueOf(nextHopMacAddress));
 
         Set<ConnectPoint> ingressPoints = new HashSet<>();
-        for (Interface intf : routingConfig.getInterfaces()) {
-            if (!intf.equals(routingConfig.getInterface(egressPoint))) {
+        for (Interface intf : interfaces) {
+            if (!intf.connectPoint().equals(egressPoint)) {
                 ConnectPoint srcPort = intf.connectPoint();
                 ingressPoints.add(srcPort);
             }
@@ -621,7 +641,6 @@
      * A static MultiPointToSinglePointIntent builder, the returned intent is
      * equal to the input intent except that the id is different.
      *
-     *
      * @param intent the intent to be used for building a new intent
      * @param routeEntry the relative routeEntry of the intent
      * @return the newly constructed MultiPointToSinglePointIntent
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/PeerConnectivityManagerTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/PeerConnectivityManagerTest.java
index 19c1d50..164b54d 100644
--- a/apps/sdnip/src/test/java/org/onosproject/sdnip/PeerConnectivityManagerTest.java
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/PeerConnectivityManagerTest.java
@@ -29,6 +29,9 @@
 import org.onlab.packet.TpPort;
 import org.onlab.packet.VlanId;
 import org.onosproject.core.ApplicationId;
+import org.onosproject.net.config.NetworkConfigService;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.PortNumber;
@@ -41,9 +44,9 @@
 import org.onosproject.net.intent.Intent;
 import org.onosproject.net.intent.IntentService;
 import org.onosproject.net.intent.PointToPointIntent;
+import org.onosproject.routing.config.BgpConfig;
 import org.onosproject.routing.config.BgpPeer;
 import org.onosproject.routing.config.BgpSpeaker;
-import org.onosproject.routing.config.Interface;
 import org.onosproject.routing.config.InterfaceAddress;
 import org.onosproject.routing.config.RoutingConfigurationService;
 
@@ -53,6 +56,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import static org.easymock.EasyMock.createMock;
 import static org.easymock.EasyMock.expect;
@@ -78,16 +82,21 @@
         }
     };
 
+    private static final ApplicationId CONFIG_APP_ID = APPID;
+
     private PeerConnectivityManager peerConnectivityManager;
     private IntentSynchronizer intentSynchronizer;
     private RoutingConfigurationService routingConfig;
+    private InterfaceService interfaceService;
+    private NetworkConfigService networkConfigService;
     private IntentService intentService;
 
-    private Map<String, BgpSpeaker> bgpSpeakers;
+    private Set<BgpConfig.BgpSpeakerConfig> bgpSpeakers;
     private Map<String, Interface> interfaces;
     private Map<IpAddress, BgpPeer> peers;
 
-    private Map<String, BgpSpeaker> configuredBgpSpeakers;
+    private BgpConfig bgpConfig;
+
     private Map<String, Interface> configuredInterfaces;
     private Map<IpAddress, BgpPeer> configuredPeers;
     private List<PointToPointIntent> intentList;
@@ -119,9 +128,12 @@
     public void setUp() throws Exception {
         super.setUp();
         routingConfig = createMock(RoutingConfigurationService.class);
+        interfaceService = createMock(InterfaceService.class);
+        networkConfigService = createMock(NetworkConfigService.class);
+        bgpConfig = createMock(BgpConfig.class);
 
-        // These will set expectations on routingConfig
-        bgpSpeakers = Collections.unmodifiableMap(setUpBgpSpeakers());
+        // These will set expectations on routingConfig and interfaceService
+        bgpSpeakers = setUpBgpSpeakers();
         interfaces = Collections.unmodifiableMap(setUpInterfaces());
         peers = Collections.unmodifiableMap(setUpPeers());
 
@@ -134,31 +146,20 @@
      *
      * @return configured BGP speakers as a map from speaker name to speaker
      */
-    private Map<String, BgpSpeaker> setUpBgpSpeakers() {
+    private Set<BgpConfig.BgpSpeakerConfig> setUpBgpSpeakers() {
 
-        configuredBgpSpeakers = new HashMap<>();
+        BgpConfig.BgpSpeakerConfig speaker1 = new BgpConfig.BgpSpeakerConfig(
+                s1Eth100, Collections.singleton(IpAddress.valueOf("192.168.10.1")));
 
-        BgpSpeaker bgpSpeaker1 = new BgpSpeaker(
-                "bgpSpeaker1",
-                "00:00:00:00:00:00:00:01", 100,
-                "00:00:00:00:00:01");
-        List<InterfaceAddress> interfaceAddresses1 = new LinkedList<>();
-        interfaceAddresses1.add(new InterfaceAddress(dpid1, 1, "192.168.10.101"));
-        bgpSpeaker1.setInterfaceAddresses(interfaceAddresses1);
-        configuredBgpSpeakers.put(bgpSpeaker1.name(), bgpSpeaker1);
+        BgpConfig.BgpSpeakerConfig speaker2 = new BgpConfig.BgpSpeakerConfig(
+                s1Eth100, Sets.newHashSet(IpAddress.valueOf("192.168.20.1"),
+                IpAddress.valueOf("192.168.30.1")));
 
-        // BGP speaker2 is attached to the same switch port with speaker1
-        BgpSpeaker bgpSpeaker2 = new BgpSpeaker(
-                "bgpSpeaker2",
-                "00:00:00:00:00:00:00:01", 100,
-                "00:00:00:00:00:02");
-        List<InterfaceAddress> interfaceAddresses2 = new LinkedList<>();
-        interfaceAddresses2.add(new InterfaceAddress(dpid2, 1, "192.168.20.101"));
-        interfaceAddresses2.add(new InterfaceAddress(dpid2, 1, "192.168.30.101"));
-        bgpSpeaker2.setInterfaceAddresses(interfaceAddresses2);
-        configuredBgpSpeakers.put(bgpSpeaker2.name(), bgpSpeaker2);
+        Set<BgpConfig.BgpSpeakerConfig> bgpSpeakers = Sets.newHashSet();
+        bgpSpeakers.add(speaker1);
+        bgpSpeakers.add(speaker2);
 
-        return configuredBgpSpeakers;
+        return bgpSpeakers;
     }
 
     /**
@@ -201,26 +202,31 @@
                 VlanId.NONE);
         configuredInterfaces.put(interfaceSw2Eth1intf2, intfsw2eth1intf2);
 
-        expect(routingConfig.getInterface(s1Eth1))
+        expect(interfaceService.getInterfacesByPort(s1Eth1))
+                .andReturn(Collections.singleton(intfsw1eth1)).anyTimes();
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.10.101")))
+                .andReturn(Collections.singleton(intfsw1eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.10.1")))
                 .andReturn(intfsw1eth1).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.10.101")))
-                .andReturn(intfsw1eth1).anyTimes();
-        expect(routingConfig.getInterface(s2Eth1))
+        expect(interfaceService.getInterfacesByPort(s2Eth1))
+                .andReturn(Collections.singleton(intfsw2eth1)).anyTimes();
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.20.101")))
+                .andReturn(Collections.singleton(intfsw2eth1)).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.20.1")))
                 .andReturn(intfsw2eth1).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.20.101")))
-                .andReturn(intfsw2eth1).anyTimes();
-        //expect(routingConfig.getInterface(s2Eth1))
-        //        .andReturn(intfsw2eth1intf2).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.30.101")))
+
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.30.101")))
+                .andReturn(Collections.singleton(intfsw2eth1intf2)).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.30.1")))
                 .andReturn(intfsw2eth1intf2).anyTimes();
 
         // Non-existent interface used during one of the tests
-        expect(routingConfig.getInterface(new ConnectPoint(
-                    DeviceId.deviceId(SdnIp.dpidToUri("00:00:00:00:00:00:01:00")),
-                    PortNumber.portNumber(1))))
+        expect(interfaceService.getInterfacesByPort(new ConnectPoint(
+                DeviceId.deviceId(SdnIp.dpidToUri("00:00:00:00:00:00:01:00")),
+                PortNumber.portNumber(1))))
                     .andReturn(null).anyTimes();
 
-        expect(routingConfig.getInterfaces()).andReturn(
+        expect(interfaceService.getInterfaces()).andReturn(
                 Sets.newHashSet(configuredInterfaces.values())).anyTimes();
 
         return configuredInterfaces;
@@ -398,7 +404,6 @@
      * Sets up intents for ICMP paths.
      */
     private void setUpIcmpIntents() {
-
         // Start to build intents between BGP speaker1 and BGP peer1
         icmpPathintentConstructor(
                 "192.168.10.101/32", "192.168.10.1/32", s1Eth100, s1Eth1);
@@ -415,7 +420,6 @@
                 "192.168.30.101/32", "192.168.30.1/32", s1Eth100, s2Eth1);
         icmpPathintentConstructor(
                 "192.168.30.1/32", "192.168.30.101/32", s2Eth1, s1Eth100);
-
     }
 
     /**
@@ -424,22 +428,28 @@
      * @throws TestUtilsException if exceptions when using TestUtils
      */
     private void initPeerConnectivity() throws TestUtilsException {
-
         expect(routingConfig.getBgpPeers()).andReturn(peers).anyTimes();
-        expect(routingConfig.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
+        expect(bgpConfig.bgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
+        replay(bgpConfig);
+        expect(networkConfigService.getConfig(APPID, BgpConfig.class)).andReturn(bgpConfig).anyTimes();
+        replay(networkConfigService);
         replay(routingConfig);
+        replay(interfaceService);
 
         intentService = createMock(IntentService.class);
         replay(intentService);
 
         intentSynchronizer = new IntentSynchronizer(APPID, intentService,
-                                                    null, routingConfig);
+                                                    null, routingConfig,
+                                                    interfaceService);
         intentSynchronizer.leaderChanged(true);
         TestUtils.setField(intentSynchronizer, "isActivatedLeader", true);
 
         peerConnectivityManager =
             new PeerConnectivityManager(APPID, intentSynchronizer,
-                                        routingConfig);
+                                        networkConfigService,
+                                        CONFIG_APP_ID,
+                                        interfaceService);
     }
 
     /**
@@ -472,42 +482,28 @@
      */
     @Test
     public void testNullInterfaces() {
-        reset(routingConfig);
-        expect(routingConfig.getInterfaces()).andReturn(
+        reset(interfaceService);
+
+        expect(interfaceService.getInterfaces()).andReturn(
                 Sets.<Interface>newHashSet()).anyTimes();
-        expect(routingConfig.getInterface(s2Eth1))
+        expect(interfaceService.getInterfacesByPort(s2Eth1))
+                .andReturn(Collections.emptySet()).anyTimes();
+        expect(interfaceService.getInterfacesByPort(s1Eth1))
+        .andReturn(Collections.emptySet()).anyTimes();
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.10.101")))
+                .andReturn(Collections.emptySet()).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.10.1")))
                 .andReturn(null).anyTimes();
-        expect(routingConfig.getInterface(s1Eth1))
-        .andReturn(null).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.10.101")))
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.20.101")))
+                .andReturn(Collections.emptySet()).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.20.1")))
                 .andReturn(null).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.20.101")))
-                .andReturn(null).anyTimes();
-        expect(routingConfig.getInterface(IpAddress.valueOf("192.168.30.101")))
+        expect(interfaceService.getInterfacesByIp(IpAddress.valueOf("192.168.30.101")))
+                .andReturn(Collections.emptySet()).anyTimes();
+        expect(interfaceService.getMatchingInterface(IpAddress.valueOf("192.168.30.1")))
                 .andReturn(null).anyTimes();
 
-        expect(routingConfig.getBgpPeers()).andReturn(peers).anyTimes();
-        expect(routingConfig.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
-        replay(routingConfig);
-
-        reset(intentService);
-        replay(intentService);
-        peerConnectivityManager.start();
-        verify(intentService);
-    }
-
-    /**
-     *  Tests a corner case, when there are no BGP peers in the configuration.
-     */
-    @Test
-    public void testNullBgpPeers() {
-        reset(routingConfig);
-        expect(routingConfig.getInterfaces()).andReturn(
-                Sets.newHashSet(interfaces.values())).anyTimes();
-
-        expect(routingConfig.getBgpPeers()).andReturn(new HashMap<>()).anyTimes();
-        expect(routingConfig.getBgpSpeakers()).andReturn(bgpSpeakers).anyTimes();
-        replay(routingConfig);
+        replay(interfaceService);
 
         reset(intentService);
         replay(intentService);
@@ -521,12 +517,11 @@
     @Test
     public void testNullBgpSpeakers() {
         reset(routingConfig);
-        expect(routingConfig.getInterfaces()).andReturn(
-                Sets.newHashSet(interfaces.values())).anyTimes();
+        reset(bgpConfig);
 
+        expect(bgpConfig.bgpSpeakers()).andReturn(Collections.emptySet()).anyTimes();
+        replay(bgpConfig);
         expect(routingConfig.getBgpPeers()).andReturn(peers).anyTimes();
-        expect(routingConfig.getBgpSpeakers()).andReturn(
-                Collections.emptyMap()).anyTimes();
         replay(routingConfig);
 
         reset(intentService);
@@ -562,7 +557,6 @@
         interfaceAddresses100.add(new InterfaceAddress(dpid1, 1, "192.168.10.201"));
         interfaceAddresses100.add(new InterfaceAddress(dpid2, 1, "192.168.20.201"));
         bgpSpeaker100.setInterfaceAddresses(interfaceAddresses100);
-        configuredBgpSpeakers.put(bgpSpeaker100.name(), bgpSpeaker100);
         testConnectionSetup();
     }
 }