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/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
index 9a6680e..e5388d2 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/BgpRouter.java
@@ -20,7 +20,6 @@
 import com.google.common.collect.Maps;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multiset;
-
 import org.apache.felix.scr.annotations.Activate;
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
@@ -29,9 +28,11 @@
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
-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.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.device.DeviceEvent;
 import org.onosproject.net.device.DeviceListener;
@@ -56,15 +57,14 @@
 import org.onosproject.routing.FibListener;
 import org.onosproject.routing.FibUpdate;
 import org.onosproject.routing.RoutingService;
-import org.onosproject.routing.config.BgpSpeaker;
-import org.onosproject.routing.config.Interface;
-import org.onosproject.routing.config.RoutingConfigurationService;
+import org.onosproject.routing.config.BgpConfig;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 
 /* For test only - will be removed before Cardinal release
@@ -95,7 +95,10 @@
     protected RoutingService routingService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected RoutingConfigurationService configService;
+    protected InterfaceService interfaceService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected NetworkConfigService networkConfigService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected PacketService packetService;
@@ -106,14 +109,6 @@
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
     protected DeviceService deviceService;
 
-    //
-    // 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;
-
     private ApplicationId appId;
 
     // Reference count for how many times a next hop is used by a route
@@ -145,14 +140,25 @@
     @Activate
     protected void activate() {
         appId = coreService.registerApplication(BGP_ROUTER_APP);
-        getDeviceConfiguration(configService.getBgpSpeakers());
+
+        ApplicationId routerAppId = coreService.getAppId(RoutingService.ROUTER_APP_ID);
+        BgpConfig bgpConfig =
+                networkConfigService.getConfig(routerAppId, RoutingService.CONFIG_CLASS);
+
+        if (bgpConfig == null) {
+            log.error("No BgpConfig found");
+            return;
+        }
+
+        getDeviceConfiguration(bgpConfig);
 
         connectivityManager = new TunnellingConnectivityManager(appId,
-                                                                configService,
+                                                                bgpConfig,
+                                                                interfaceService,
                                                                 packetService,
                                                                 flowObjectiveService);
 
-        icmpHandler = new IcmpHandler(configService, packetService);
+        icmpHandler = new IcmpHandler(interfaceService, packetService);
         deviceListener = new InnerDeviceListener();
         routingService.addFibListener(new InternalFibListener());
         routingService.start();
@@ -162,7 +168,7 @@
 
         // Initialize devices now if they are already connected
         if (deviceService.isAvailable(deviceId)) {
-            processIntfFilters(true, configService.getInterfaces());
+            processIntfFilters(true, interfaceService.getInterfaces());
         }
 
         if (deviceService.isAvailable(ctrlDeviceId)) {
@@ -182,21 +188,36 @@
         log.info("BgpRouter stopped");
     }
 
-    private void getDeviceConfiguration(Map<String, BgpSpeaker> bgps) {
-        if (bgps == null || bgps.values().isEmpty()) {
-            log.error("BGP speakers configuration is missing");
+    private void getDeviceConfiguration(BgpConfig bgpConfig) {
+        Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
+                bgpConfig.bgpSpeakers().stream().findAny();
+
+        if (!bgpSpeaker.isPresent()) {
+            log.error("BGP speaker configuration not found");
             return;
         }
-        for (BgpSpeaker s : bgps.values()) {
-            ctrlDeviceId = s.connectPoint().deviceId();
-            if (s.interfaceAddresses() == null || s.interfaceAddresses().isEmpty()) {
-                log.error("BGP Router must have interfaces configured");
-                return;
-            }
-            deviceId = s.interfaceAddresses().get(0).connectPoint().deviceId();
-            break;
+
+        ctrlDeviceId = bgpSpeaker.get().connectPoint().deviceId();
+
+        Optional<IpAddress> peerAddress =
+                bgpSpeaker.get().peers().stream().findAny();
+
+        if (!peerAddress.isPresent()) {
+            log.error("BGP speaker must have peers configured");
+            return;
         }
 
+        Interface intf = interfaceService.getMatchingInterface(peerAddress.get());
+
+        if (intf == null) {
+            log.error("No interface found for peer");
+            return;
+        }
+
+        // Assume all peers are configured on the same device - this is required
+        // by the BGP router
+        deviceId = intf.connectPoint().deviceId();
+
         log.info("Router dpid: {}", deviceId);
         log.info("Control Plane OVS dpid: {}", ctrlDeviceId);
     }
@@ -283,7 +304,7 @@
         if (nextHopsCount.count(entry.nextHopIp()) == 0) {
             // There was no next hop in the multiset
 
-            Interface egressIntf = configService.getMatchingInterface(entry.nextHopIp());
+            Interface egressIntf = interfaceService.getMatchingInterface(entry.nextHopIp());
             if (egressIntf == null) {
                 log.warn("no egress interface found for {}", entry);
                 return;
@@ -405,7 +426,7 @@
                     if (deviceService.isAvailable(event.subject().id())) {
                         log.info("Device connected {}", event.subject().id());
                         if (event.subject().id().equals(deviceId)) {
-                            processIntfFilters(true, configService.getInterfaces());
+                            processIntfFilters(true, interfaceService.getInterfaces());
 
                             /* For test only - will be removed before Cardinal release
                             delay(1000);