Modified BGP and FPM route sources to push to new route service.

Also created an adapter to adapt the new interface to the old one for
backwards compatibilty with existing FIB components.

Change-Id: If8eb2220d9e4e69af135a8f9469ffda567ed4448
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpRouteSelector.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpRouteSelector.java
index 01d0f41..92c4e7d 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpRouteSelector.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpRouteSelector.java
@@ -16,6 +16,7 @@
 package org.onosproject.routing.bgp;
 
 import org.onlab.packet.IpPrefix;
+import org.onosproject.incubator.net.routing.Route;
 import org.onosproject.routing.RouteUpdate;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -45,15 +46,16 @@
      * Processes route entry updates: added/updated and deleted route
      * entries.
      *
-     * @param bgpSession the BGP session the route entry updates were
-     * received on
      * @param addedBgpRouteEntries the added/updated route entries to process
      * @param deletedBgpRouteEntries the deleted route entries to process
      */
-    synchronized void routeUpdates(BgpSession bgpSession,
+    synchronized void routeUpdates(
                         Collection<BgpRouteEntry> addedBgpRouteEntries,
                         Collection<BgpRouteEntry> deletedBgpRouteEntries) {
-        Collection<RouteUpdate> routeUpdates = new LinkedList<>();
+
+        Collection<Route> updates = new LinkedList<>();
+        Collection<Route> withdraws = new LinkedList<>();
+
         RouteUpdate routeUpdate;
 
         if (bgpSessionManager.isShutdown()) {
@@ -61,32 +63,42 @@
         }
         // Process the deleted route entries
         for (BgpRouteEntry bgpRouteEntry : deletedBgpRouteEntries) {
-            routeUpdate = processDeletedRoute(bgpSession, bgpRouteEntry);
-            if (routeUpdate != null) {
-                routeUpdates.add(routeUpdate);
-            }
+            routeUpdate = processDeletedRoute(bgpRouteEntry);
+            convertRouteUpdateToRoute(routeUpdate, updates, withdraws);
         }
 
         // Process the added/updated route entries
         for (BgpRouteEntry bgpRouteEntry : addedBgpRouteEntries) {
-            routeUpdate = processAddedRoute(bgpSession, bgpRouteEntry);
-            if (routeUpdate != null) {
-                routeUpdates.add(routeUpdate);
+            routeUpdate = processAddedRoute(bgpRouteEntry);
+            convertRouteUpdateToRoute(routeUpdate, updates, withdraws);
+        }
+
+        bgpSessionManager.withdraw(withdraws);
+        bgpSessionManager.update(updates);
+    }
+
+    private void convertRouteUpdateToRoute(RouteUpdate routeUpdate,
+                                           Collection<Route> updates,
+                                           Collection<Route> withdraws) {
+        if (routeUpdate != null) {
+            Route route = new Route(Route.Source.BGP, routeUpdate.routeEntry().prefix(),
+                    routeUpdate.routeEntry().nextHop());
+            if (routeUpdate.type().equals(RouteUpdate.Type.UPDATE)) {
+                updates.add(route);
+            } else if (routeUpdate.type().equals(RouteUpdate.Type.DELETE)) {
+                withdraws.add(route);
             }
         }
-        bgpSessionManager.getRouteListener().update(routeUpdates);
     }
 
     /**
      * Processes an added/updated route entry.
      *
-     * @param bgpSession the BGP session the route entry update was received on
      * @param bgpRouteEntry the added/updated route entry
      * @return the result route update that should be forwarded to the
      * Route Listener, or null if no route update should be forwarded
      */
-    private RouteUpdate processAddedRoute(BgpSession bgpSession,
-                                          BgpRouteEntry bgpRouteEntry) {
+    private RouteUpdate processAddedRoute(BgpRouteEntry bgpRouteEntry) {
         RouteUpdate routeUpdate;
         BgpRouteEntry bestBgpRouteEntry =
             bgpSessionManager.findBgpRoute(bgpRouteEntry.prefix());
@@ -136,13 +148,11 @@
     /**
      * Processes a deleted route entry.
      *
-     * @param bgpSession the BGP session the route entry update was received on
      * @param bgpRouteEntry the deleted route entry
      * @return the result route update that should be forwarded to the
      * Route Listener, or null if no route update should be forwarded
      */
-    private RouteUpdate processDeletedRoute(BgpSession bgpSession,
-                                            BgpRouteEntry bgpRouteEntry) {
+    private RouteUpdate processDeletedRoute(BgpRouteEntry bgpRouteEntry) {
         RouteUpdate routeUpdate;
         BgpRouteEntry bestBgpRouteEntry =
             bgpSessionManager.findBgpRoute(bgpRouteEntry.prefix());
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSession.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSession.java
index bc85371a..0352520 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSession.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSession.java
@@ -327,6 +327,7 @@
                   ctx.getChannel().getRemoteAddress(),
                   ctx.getChannel().getLocalAddress(),
                   e);
+        log.debug("Exception:", e.getCause());
         processChannelDisconnected();
     }
 
@@ -350,8 +351,8 @@
         BgpRouteSelector bgpRouteSelector =
             bgpSessionManager.getBgpRouteSelector();
         Collection<BgpRouteEntry> addedRoutes = Collections.emptyList();
-        bgpRouteSelector.routeUpdates(this, addedRoutes, deletedRoutes4);
-        bgpRouteSelector.routeUpdates(this, addedRoutes, deletedRoutes6);
+        bgpRouteSelector.routeUpdates(addedRoutes, deletedRoutes4);
+        bgpRouteSelector.routeUpdates(addedRoutes, deletedRoutes6);
 
         bgpSessionManager.peerDisconnected(this);
     }
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
index 5c3c18e..92448b4 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpSessionManager.java
@@ -19,6 +19,8 @@
 import org.apache.felix.scr.annotations.Component;
 import org.apache.felix.scr.annotations.Deactivate;
 import org.apache.felix.scr.annotations.Modified;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
 import org.apache.felix.scr.annotations.Service;
 import org.jboss.netty.bootstrap.ServerBootstrap;
 import org.jboss.netty.channel.Channel;
@@ -34,8 +36,8 @@
 import org.onlab.packet.Ip4Prefix;
 import org.onlab.packet.Ip6Prefix;
 import org.onlab.packet.IpPrefix;
-import org.onosproject.routing.RouteSourceService;
-import org.onosproject.routing.RouteListener;
+import org.onosproject.incubator.net.routing.Route;
+import org.onosproject.incubator.net.routing.RouteAdminService;
 import org.osgi.service.component.ComponentContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -48,7 +50,6 @@
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
-import static com.google.common.base.Preconditions.checkNotNull;
 import static java.util.concurrent.Executors.newCachedThreadPool;
 import static org.onlab.util.Tools.groupedThreads;
 
@@ -57,10 +58,13 @@
  */
 @Component(immediate = true, enabled = false)
 @Service
-public class BgpSessionManager implements BgpInfoService, RouteSourceService {
+public class BgpSessionManager implements BgpInfoService {
     private static final Logger log =
             LoggerFactory.getLogger(BgpSessionManager.class);
 
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected RouteAdminService routeService;
+
     boolean isShutdown = true;
     private Channel serverChannel;     // Listener for incoming BGP connections
     private ServerBootstrap serverBootstrap;
@@ -75,19 +79,19 @@
     private ConcurrentMap<Ip6Prefix, BgpRouteEntry> bgpRoutes6 =
             new ConcurrentHashMap<>();
 
-    private RouteListener routeListener;
-
     private static final int DEFAULT_BGP_PORT = 2000;
     private int bgpPort;
 
     @Activate
     protected void activate(ComponentContext context) {
         readComponentConfiguration(context);
+        start();
         log.info("BgpSessionManager started");
     }
 
     @Deactivate
     protected void deactivate() {
+        stop();
         log.info("BgpSessionManager stopped");
     }
 
@@ -128,15 +132,6 @@
     }
 
     /**
-     * Gets the route listener.
-     *
-     * @return the route listener to use
-     */
-    RouteListener getRouteListener() {
-        return routeListener;
-    }
-
-    /**
      * Gets the BGP sessions.
      *
      * @return the BGP sessions
@@ -290,13 +285,29 @@
         return bgpRouteSelector;
     }
 
-    @Override
-    public void start(RouteListener routeListener) {
+    /**
+     * Sends updates routes to the route service.
+     *
+     * @param updates routes to update
+     */
+    void update(Collection<Route> updates) {
+        routeService.update(updates);
+    }
+
+    /**
+     * Sends withdrawn routes to the routes service.
+     *
+     * @param withdraws routes to withdraw
+     */
+    void withdraw(Collection<Route> withdraws) {
+        routeService.withdraw(withdraws);
+    }
+
+
+    public void start() {
         log.debug("BGP Session Manager start.");
         isShutdown = false;
 
-        this.routeListener = checkNotNull(routeListener);
-
         ChannelFactory channelFactory = new NioServerSocketChannelFactory(
                 newCachedThreadPool(groupedThreads("onos/bgp", "sm-boss-%d")),
                 newCachedThreadPool(groupedThreads("onos/bgp", "sm-worker-%d")));
@@ -330,7 +341,6 @@
         }
     }
 
-    @Override
     public void stop() {
         isShutdown = true;
         allChannels.close().awaitUninterruptibly();
diff --git a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
index 905f1b1..b30fb8d 100644
--- a/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
+++ b/apps/routing/src/main/java/org/onosproject/routing/bgp/BgpUpdate.java
@@ -154,10 +154,10 @@
         //
         BgpRouteSelector bgpRouteSelector =
             bgpSession.getBgpSessionManager().getBgpRouteSelector();
-        bgpRouteSelector.routeUpdates(bgpSession,
+        bgpRouteSelector.routeUpdates(
                                 decodedBgpRoutes.addedUnicastRoutes4.values(),
                                 decodedBgpRoutes.deletedUnicastRoutes4.values());
-        bgpRouteSelector.routeUpdates(bgpSession,
+        bgpRouteSelector.routeUpdates(
                                 decodedBgpRoutes.addedUnicastRoutes6.values(),
                                 decodedBgpRoutes.deletedUnicastRoutes6.values());