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);
diff --git a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/IcmpHandler.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/IcmpHandler.java
index 5e52b15..8826535 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/IcmpHandler.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/IcmpHandler.java
@@ -19,6 +19,8 @@
 import org.onlab.packet.ICMP;
 import org.onlab.packet.IPv4;
 import org.onlab.packet.IpAddress;
+import org.onosproject.incubator.net.intf.Interface;
+import org.onosproject.incubator.net.intf.InterfaceService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
 import org.onosproject.net.flow.TrafficTreatment;
@@ -29,8 +31,6 @@
 import org.onosproject.net.packet.PacketContext;
 import org.onosproject.net.packet.PacketProcessor;
 import org.onosproject.net.packet.PacketService;
-import org.onosproject.routing.config.Interface;
-import org.onosproject.routing.config.RoutingConfigurationService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,14 +41,14 @@
     private static final Logger log = LoggerFactory.getLogger(IcmpHandler.class);
 
     private final PacketService packetService;
-    private final RoutingConfigurationService configService;
+    private final InterfaceService interfaceService;
 
     private final IcmpProcessor processor = new IcmpProcessor();
 
 
-    public IcmpHandler(RoutingConfigurationService configService,
+    public IcmpHandler(InterfaceService interfaceService,
                        PacketService packetService) {
-        this.configService = configService;
+        this.interfaceService = interfaceService;
         this.packetService = packetService;
     }
 
@@ -67,7 +67,7 @@
         IPv4 ipv4 = (IPv4) ethernet.getPayload();
         ConnectPoint connectPoint = pkt.receivedFrom();
         IpAddress destIpAddress = IpAddress.valueOf(ipv4.getDestinationAddress());
-        Interface targetInterface = configService.getMatchingInterface(destIpAddress);
+        Interface targetInterface = interfaceService.getMatchingInterface(destIpAddress);
 
         if (targetInterface == null) {
             log.trace("No matching interface for {}", destIpAddress);
diff --git a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
index 06420c1..35af05e 100644
--- a/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
+++ b/apps/bgprouter/src/main/java/org/onosproject/bgprouter/TunnellingConnectivityManager.java
@@ -15,14 +15,14 @@
  */
 package org.onosproject.bgprouter;
 
-import static org.slf4j.LoggerFactory.getLogger;
-
 import org.onlab.packet.Ethernet;
 import org.onlab.packet.IPv4;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.TCP;
 import org.onlab.packet.TpPort;
 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.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
@@ -36,12 +36,14 @@
 import org.onosproject.net.packet.PacketContext;
 import org.onosproject.net.packet.PacketProcessor;
 import org.onosproject.net.packet.PacketService;
-import org.onosproject.routing.config.BgpPeer;
-import org.onosproject.routing.config.BgpSpeaker;
-import org.onosproject.routing.config.InterfaceAddress;
-import org.onosproject.routing.config.RoutingConfigurationService;
+import org.onosproject.routing.config.BgpConfig;
 import org.slf4j.Logger;
 
+import java.util.Optional;
+import java.util.Set;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
 
 /**
  * Manages connectivity between peers by tunnelling BGP traffic through
@@ -53,34 +55,32 @@
     private final Logger log = getLogger(getClass());
     private final ApplicationId appId;
 
-    private final BgpSpeaker bgpSpeaker;
+    private final BgpConfig.BgpSpeakerConfig bgpSpeaker;
 
     private final PacketService packetService;
-    private final RoutingConfigurationService configService;
+    private final InterfaceService interfaceService;
     private final FlowObjectiveService flowObjectiveService;
 
     private final BgpProcessor processor = new BgpProcessor();
 
     public TunnellingConnectivityManager(ApplicationId appId,
-                                         RoutingConfigurationService configService,
+                                         BgpConfig bgpConfig,
+                                         InterfaceService interfaceService,
                                          PacketService packetService,
                                          FlowObjectiveService flowObjectiveService) {
         this.appId = appId;
-        this.configService = configService;
+        this.interfaceService = interfaceService;
         this.packetService = packetService;
         this.flowObjectiveService = flowObjectiveService;
 
-        BgpSpeaker bgpSpeaker = null;
-        for (BgpSpeaker speaker : configService.getBgpSpeakers().values()) {
-            bgpSpeaker = speaker;
-            break;
-        }
+        Optional<BgpConfig.BgpSpeakerConfig> bgpSpeaker =
+                bgpConfig.bgpSpeakers().stream().findAny();
 
-        if (bgpSpeaker == null) {
+        if (!bgpSpeaker.isPresent()) {
             throw new IllegalArgumentException("Must have at least one BGP speaker configured");
         }
 
-        this.bgpSpeaker = bgpSpeaker;
+        this.bgpSpeaker = bgpSpeaker.get();
 
     }
 
@@ -149,14 +149,19 @@
         IpAddress dstAddress = IpAddress.valueOf(ipv4.getDestinationAddress());
 
         if (context.inPacket().receivedFrom().equals(bgpSpeaker.connectPoint())) {
-            BgpPeer peer = configService.getBgpPeers().get(dstAddress);
-            if (peer != null) {
-                outputPort = peer.connectPoint();
+            if (bgpSpeaker.peers().contains(dstAddress)) {
+                Interface intf = interfaceService.getMatchingInterface(dstAddress);
+                if (intf != null) {
+                    outputPort = intf.connectPoint();
+                }
             }
-        }
-        for (InterfaceAddress addr : bgpSpeaker.interfaceAddresses()) {
-            if (addr.ipAddress().equals(dstAddress) && !context.inPacket()
-                    .receivedFrom().equals(bgpSpeaker.connectPoint())) {
+        } else {
+            Set<Interface> interfaces =
+                    interfaceService.getInterfacesByPort(context.inPacket().receivedFrom());
+
+            if (interfaces.stream()
+                    .flatMap(intf -> intf.ipAddresses().stream())
+                    .anyMatch(ia -> ia.ipAddress().equals(dstAddress))) {
                 outputPort = bgpSpeaker.connectPoint();
             }
         }