Adapt FIBs to new route interface

Change-Id: I8ac19ad578aac4607fd8319186b6568a21edc1fd
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 a4bd14c..1179c3d 100644
--- a/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java
+++ b/apps/sdnip/src/main/java/org/onosproject/sdnip/SdnIpFib.java
@@ -34,6 +34,10 @@
 import org.onosproject.incubator.net.intf.InterfaceEvent;
 import org.onosproject.incubator.net.intf.InterfaceListener;
 import org.onosproject.incubator.net.intf.InterfaceService;
+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.RouteService;
 import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.flow.DefaultTrafficSelector;
 import org.onosproject.net.flow.DefaultTrafficTreatment;
@@ -43,21 +47,15 @@
 import org.onosproject.net.intent.Key;
 import org.onosproject.net.intent.MultiPointToSinglePointIntent;
 import org.onosproject.net.intent.constraint.PartialFailureConstraint;
-import org.onosproject.routing.FibListener;
-import org.onosproject.routing.FibUpdate;
 import org.onosproject.routing.IntentSynchronizationService;
-import org.onosproject.routing.RoutingService;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Collection;
 import java.util.HashSet;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 /**
  * FIB component of SDN-IP.
  */
@@ -75,9 +73,9 @@
     protected CoreService coreService;
 
     @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
-    protected RoutingService routingService;
+    protected RouteService routeService;
 
-    private final InternalFibListener fibListener = new InternalFibListener();
+    private final InternalRouteListener routeListener = new InternalRouteListener();
     private final InternalInterfaceListener interfaceListener = new InternalInterfaceListener();
 
     private static final int PRIORITY_OFFSET = 100;
@@ -96,69 +94,41 @@
 
         interfaceService.addListener(interfaceListener);
 
-        routingService.addFibListener(fibListener);
-        routingService.start();
+        routeService.addListener(routeListener);
     }
 
     @Deactivate
     public void deactivate() {
         interfaceService.removeListener(interfaceListener);
-        // TODO remove listener
-        routingService.stop();
+        routeService.removeListener(routeListener);
     }
 
-    private void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) {
-        int submitCount = 0, withdrawCount = 0;
-        //
-        // NOTE: Semantically, we MUST withdraw existing intents before
-        // submitting new intents.
-        //
+    private void update(ResolvedRoute route) {
         synchronized (this) {
-            MultiPointToSinglePointIntent intent;
+            IpPrefix prefix = route.prefix();
+            MultiPointToSinglePointIntent intent =
+                    generateRouteIntent(prefix, route.nextHop(), route.nextHopMac());
 
-            //
-            // Prepare the Intent batch operations for the intents to withdraw
-            //
-            for (FibUpdate withdraw : withdraws) {
-                checkArgument(withdraw.type() == FibUpdate.Type.DELETE,
-                        "FibUpdate with wrong type in withdraws list");
-
-                IpPrefix prefix = withdraw.entry().prefix();
-                intent = routeIntents.remove(prefix);
-                if (intent == null) {
-                    log.trace("SDN-IP No intent in routeIntents to delete " +
-                            "for prefix: {}", prefix);
-                    continue;
-                }
-                intentSynchronizer.withdraw(intent);
-                withdrawCount++;
+            if (intent == null) {
+                log.debug("SDN-IP no interface found for route {}", route);
+                return;
             }
 
-            //
-            // Prepare the Intent batch operations for the intents to submit
-            //
-            for (FibUpdate update : updates) {
-                checkArgument(update.type() == FibUpdate.Type.UPDATE,
-                        "FibUpdate with wrong type in updates list");
+            routeIntents.put(prefix, intent);
+            intentSynchronizer.submit(intent);
+        }
+    }
 
-                IpPrefix prefix = update.entry().prefix();
-                intent = generateRouteIntent(prefix, update.entry().nextHopIp(),
-                        update.entry().nextHopMac());
-
-                if (intent == null) {
-                    // This preserves the old semantics - if an intent can't be
-                    // generated, we don't do anything with that prefix. But
-                    // perhaps we should withdraw the old intent anyway?
-                    continue;
-                }
-
-                routeIntents.put(prefix, intent);
-                intentSynchronizer.submit(intent);
-                submitCount++;
+    private void withdraw(ResolvedRoute route) {
+        synchronized (this) {
+            IpPrefix prefix = route.prefix();
+            MultiPointToSinglePointIntent intent = routeIntents.remove(prefix);
+            if (intent == null) {
+                log.trace("SDN-IP no intent in routeIntents to delete " +
+                        "for prefix: {}", prefix);
+                return;
             }
-
-            log.debug("SDN-IP submitted {}/{}, withdrew = {}/{}", submitCount,
-                    updates.size(), withdrawCount, withdraws.size());
+            intentSynchronizer.withdraw(intent);
         }
     }
 
@@ -292,10 +262,20 @@
         }
     }
 
-    private class InternalFibListener implements FibListener {
+    private class InternalRouteListener implements RouteListener {
         @Override
-        public void update(Collection<FibUpdate> updates, Collection<FibUpdate> withdraws) {
-            SdnIpFib.this.update(updates, withdraws);
+        public void event(RouteEvent event) {
+            switch (event.type()) {
+            case ROUTE_ADDED:
+            case ROUTE_UPDATED:
+                update(event.subject());
+                break;
+            case ROUTE_REMOVED:
+                withdraw(event.subject());
+                break;
+            default:
+                break;
+            }
         }
     }