Route Simplification programming

Change-Id: If3d8aaa3fe2dad5538b10ddc54b97f504d0f1a35
diff --git a/app/src/main/java/org/onosproject/segmentrouting/OsgiPropertyConstants.java b/app/src/main/java/org/onosproject/segmentrouting/OsgiPropertyConstants.java
index 7b5a6fc..ac63f73 100644
--- a/app/src/main/java/org/onosproject/segmentrouting/OsgiPropertyConstants.java
+++ b/app/src/main/java/org/onosproject/segmentrouting/OsgiPropertyConstants.java
@@ -44,4 +44,8 @@
     static final String PROP_SYMMETRIC_PROBING = "symmetricProbing";
     static final boolean SYMMETRIC_PROBING_DEFAULT = false;
 
+    public static final String PROP_ROUTE_SIMPLIFICATION = "routeSimplification";
+    public static final boolean ROUTE_SIMPLIFICATION_DEFAULT = false;
+
+
 }
diff --git a/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java b/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java
new file mode 100644
index 0000000..817b32e
--- /dev/null
+++ b/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java
@@ -0,0 +1,68 @@
+/*
+ * Copyright 2018-present Open Networking Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onosproject.segmentrouting;
+
+/**
+ * Utility class for route simplification.
+ */
+import com.google.common.collect.ImmutableList;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.routeservice.ResolvedRoute;
+import org.onosproject.routeservice.Route;
+
+final class RouteSimplifierUtils {
+
+    /**
+     * When route with source type listed in leafExclusionRouteTypes,
+     * it will programme only on the leaf pair the nexthop attaches to. Other leaves will be ignored.
+     */
+    private static final ImmutableList<Route.Source> LEAF_EXCLUSION_ROUTE_TYPES =
+            ImmutableList.of(Route.Source.DHCP, Route.Source.RIP, Route.Source.DHCPLQ);
+
+    private SegmentRoutingManager srManager;
+
+    RouteSimplifierUtils(SegmentRoutingManager srManager) {
+
+        this.srManager = srManager;
+    }
+
+    /**
+     * Checking whether the leafExclusionRouteTypes contains the given source type.
+     *
+     * @return boolean if it containsd the source type.
+     * */
+    private boolean hasLeafExclusionEnabledForType(Route.Source s) {
+        return LEAF_EXCLUSION_ROUTE_TYPES.contains(s);
+    }
+
+    /*
+     * When route with any source of given prefix is  listed in leafExclusionRouteTypes,
+     * it will programme only on the leaf pair the nexthop attaches to. Other leaves will be ignored.
+     *
+     * @param ipPrefix  ip prefix of the route.
+     * @return boolean if contains the prefix of the mentioned source type.
+     * */
+    public boolean hasLeafExclusionEnabledForPrefix(IpPrefix ipPrefix) {
+        for (ResolvedRoute route : srManager.routeService.getAllResolvedRoutes(ipPrefix)) {
+            if (hasLeafExclusionEnabledForType(route.route().source())) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+}
diff --git a/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java b/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java
index 7ace77f..de0eb58 100644
--- a/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java
+++ b/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java
@@ -90,6 +90,7 @@
     private AtomicLong rulePopulationCounter;
     private SegmentRoutingManager srManager;
     private DeviceConfiguration config;
+    private RouteSimplifierUtils routeSimplifierUtils;
 
     /**
      * Creates a RoutingRulePopulator object.
@@ -100,6 +101,7 @@
         this.srManager = srManager;
         this.config = checkNotNull(srManager.deviceConfiguration);
         this.rulePopulationCounter = new AtomicLong(0);
+        this.routeSimplifierUtils = new RouteSimplifierUtils(srManager);
     }
 
     /**
@@ -470,9 +472,35 @@
      */
     boolean populateIpRuleForSubnet(DeviceId targetSw, Set<IpPrefix> subnets,
             DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
-        for (IpPrefix subnet : subnets) {
-            if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
-                return false;
+        // Get pair device of the target switch
+        Optional<DeviceId> pairDev = srManager.getPairDeviceId(targetSw);
+        // Route simplification will be off in case of the nexthop location at target switch is down
+        // (routing through spine case)
+        boolean routeSimplOff = pairDev.isPresent() && pairDev.get().equals(destSw1) && destSw2 == null;
+        // Iterates over the routes
+        // If route simplification is enabled
+        // If the target device is another leaf in the network
+        if (srManager.routeSimplification && !routeSimplOff) {
+            for (IpPrefix subnet : subnets) {
+                // Skip route programming on the target device
+                // If route simplification applies
+                if (routeSimplifierUtils.hasLeafExclusionEnabledForPrefix(subnet)) {
+                    // XXX route simplification assumes that source of the traffic
+                    // towards the nexthops are co-located with the nexthops. In different
+                    // scenarios will not work properly.
+                    continue;
+                }
+                // populate the route in the remaning scenarios
+                if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
+                    return false;
+                }
+            }
+        } else {
+            // Populate IP flow rules for all the subnets.
+            for (IpPrefix subnet : subnets) {
+                if (!populateIpRuleForRouter(targetSw, subnet, destSw1, destSw2, nextHops)) {
+                    return false;
+                }
             }
         }
         return true;
diff --git a/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index a5f8a51..bb7a47a 100644
--- a/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -165,11 +165,13 @@
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_PW_TRANSPORT_VLAN;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_RESPOND_TO_UNKNOWN_HOSTS;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_DOUBLE_TAGGED_HOSTS;
+import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_ROUTE_SIMPLIFICATION;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SINGLE_HOMED_DOWN;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PROP_SYMMETRIC_PROBING;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.PW_TRANSPORT_VLAN_DEFAULT;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.RESPOND_TO_UNKNOWN_HOSTS_DEFAULT;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT;
+import static org.onosproject.segmentrouting.OsgiPropertyConstants.ROUTE_SIMPLIFICATION_DEFAULT;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.SINGLE_HOMED_DOWN_DEFAULT;
 import static org.onosproject.segmentrouting.OsgiPropertyConstants.SYMMETRIC_PROBING_DEFAULT;
 
@@ -186,7 +188,8 @@
         PROP_ROUTE_DOUBLE_TAGGED_HOSTS + ":Boolean=" + ROUTE_DOUBLE_TAGGED_HOSTS_DEFAULT,
         PROP_DEFAULT_INTERNAL_VLAN + ":Integer=" + DEFAULT_INTERNAL_VLAN_DEFAULT,
         PROP_PW_TRANSPORT_VLAN + ":Integer=" + PW_TRANSPORT_VLAN_DEFAULT,
-        PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT
+        PROP_SYMMETRIC_PROBING + ":Boolean=" + SYMMETRIC_PROBING_DEFAULT,
+        PROP_ROUTE_SIMPLIFICATION + ":Boolean=" + ROUTE_SIMPLIFICATION_DEFAULT
     }
 )
 public class SegmentRoutingManager implements SegmentRoutingService {
@@ -281,6 +284,9 @@
     /** vlan used for transport of pseudowires between switches. */
     private int pwTransportVlan = PW_TRANSPORT_VLAN_DEFAULT;
 
+    /** Enabling route simplification. */
+    boolean  routeSimplification = ROUTE_SIMPLIFICATION_DEFAULT;
+
     ArpHandler arpHandler = null;
     IcmpHandler icmpHandler = null;
     IpHandler ipHandler = null;
@@ -751,6 +757,13 @@
             }
         }
 
+        String strRouteSimplification = Tools.get(properties, PROP_ROUTE_SIMPLIFICATION);
+        boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
+        if (expectRouteSimplification != routeSimplification) {
+            routeSimplification = expectRouteSimplification;
+            log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
+        }
+
     }
 
     /**