Route simplification; cherry pick to onos-1.13

Change-Id: I2966ed42abefc9f916b3af26e43572c0d3ec85fb
diff --git a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java
new file mode 100644
index 0000000..2ad7f65
--- /dev/null
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RouteSimplifierUtils.java
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2019-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;
+
+import com.google.common.collect.ImmutableList;
+import org.onlab.packet.IpPrefix;
+import org.onosproject.routeservice.ResolvedRoute;
+import org.onosproject.routeservice.Route;
+
+/**
+ * Utility class for route simplification.
+ */
+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.
+     *
+     * @param s source type
+     * @return boolean if it containsd the source type.
+     */
+    public 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/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java
index fb72940..768c715 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/RoutingRulePopulator.java
+++ b/apps/segmentrouting/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;
 
     // used for signalling the driver to remove vlan table and tmac entry also
     private static final long CLEANUP_DOUBLE_TAGGED_HOST_ENTRIES = 1;
@@ -104,6 +105,7 @@
         this.srManager = srManager;
         this.config = checkNotNull(srManager.deviceConfiguration);
         this.rulePopulationCounter = new AtomicLong(0);
+        this.routeSimplifierUtils = new RouteSimplifierUtils(srManager);
     }
 
     /**
@@ -473,10 +475,36 @@
      * @return true if all rules are set successfully, false otherwise
      */
     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;
+                                    DeviceId destSw1, DeviceId destSw2, Map<DeviceId, Set<DeviceId>> nextHops) {
+        // 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/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
index 7d3aad7..07f3ecf 100644
--- a/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
+++ b/apps/segmentrouting/app/src/main/java/org/onosproject/segmentrouting/SegmentRoutingManager.java
@@ -270,6 +270,10 @@
             label = "vlan used for transport of pseudowires between switches")
     private int pwTransportVlan = PW_TRANSPORT_VLAN;
 
+    @Property(name = "routeSimplification", boolValue = false,
+            label = "Enable route simplification")
+    boolean routeSimplification = false;
+
     ArpHandler arpHandler = null;
     IcmpHandler icmpHandler = null;
     IpHandler ipHandler = null;
@@ -734,6 +738,13 @@
             }
         }
 
+        String strRouteSimplification = Tools.get(properties, "routeSimplification");
+        boolean expectRouteSimplification = Boolean.parseBoolean(strRouteSimplification);
+        if (expectRouteSimplification != routeSimplification) {
+            routeSimplification = expectRouteSimplification;
+            log.info("{} route simplification", routeSimplification ? "Enabling" : "Disabling");
+        }
+
     }
 
     /**