CORD-523 Introduce generic routing service in Segment Routing

Segment Routing now reacts to routes being added/removed through RouteService.

SR will disable SingleSwitchFibInstaller if vRouter is activated

SR will install a routing table entry on
    - The leaf where next hop attaches to
    - The other leaves that points packets to the leaf where next hop attaches to

Host handler no longer add any IP flow for hosts outside the configured subnet
    - We need to explicitly add a per host route via RouteService when needed (vSG)

Change suppressSubnet behavior
    - Before: do not push any flow
    - After: ignore subnet config but still push filtering obj with VLAN 4094
             ARP handler drops all packets from suppressed ports

Additional refactoring
    - Remove vRouterId. Gateway router now needs to be specify through route API
    - Limit the scope of some variables
    - Unify handler.init method name

Change-Id: Idd2fd19fa74e3fa6209eef5cf2ed79957715c5e9
diff --git a/src/main/java/org/onosproject/segmentrouting/HostHandler.java b/src/main/java/org/onosproject/segmentrouting/HostHandler.java
index 0770546..82a2829 100644
--- a/src/main/java/org/onosproject/segmentrouting/HostHandler.java
+++ b/src/main/java/org/onosproject/segmentrouting/HostHandler.java
@@ -16,13 +16,10 @@
 
 package org.onosproject.segmentrouting;
 
-import com.google.common.collect.ImmutableSet;
-import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip4Prefix;
 import org.onlab.packet.IpAddress;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
-import org.onosproject.net.ConnectPoint;
 import org.onosproject.net.DeviceId;
 import org.onosproject.net.Host;
 import org.onosproject.net.HostLocation;
@@ -64,7 +61,7 @@
         flowObjectiveService = srManager.flowObjectiveService;
     }
 
-    protected void readInitialHosts(DeviceId devId) {
+    protected void init(DeviceId devId) {
         hostService.getHosts().forEach(host -> {
             DeviceId deviceId = host.location().deviceId();
             // The host does not attach to this device
@@ -106,8 +103,7 @@
 
             ips.forEach(ip -> {
                 // Populate IP table entry
-                if (ip.isIp4()) {
-                    addPerHostRoute(location, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(location, ip)) {
                     srManager.routingRulePopulator.populateRoute(
                             deviceId, ip.toIpPrefix(), mac, port);
                 }
@@ -144,8 +140,7 @@
 
             // Revoke IP table entry
             ips.forEach(ip -> {
-                if (ip.isIp4()) {
-                    removePerHostRoute(location, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(location, ip)) {
                     srManager.routingRulePopulator.revokeRoute(
                             deviceId, ip.toIpPrefix(), mac, port);
                 }
@@ -183,8 +178,7 @@
 
             // Revoke previous IP table entry
             prevIps.forEach(ip -> {
-                if (ip.isIp4()) {
-                    removePerHostRoute(prevLocation, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(prevLocation, ip)) {
                     srManager.routingRulePopulator.revokeRoute(
                             prevDeviceId, ip.toIpPrefix(), mac, prevPort);
                 }
@@ -207,8 +201,7 @@
 
             // Populate new IP table entry
             newIps.forEach(ip -> {
-                if (ip.isIp4()) {
-                    addPerHostRoute(newLocation, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(newLocation, ip)) {
                     srManager.routingRulePopulator.populateRoute(
                             newDeviceId, ip.toIpPrefix(), mac, newPort);
                 }
@@ -232,8 +225,7 @@
         if (accepted(event.prevSubject())) {
             // Revoke previous IP table entry
             prevIps.forEach(ip -> {
-                if (ip.isIp4()) {
-                    removePerHostRoute(prevLocation, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(prevLocation, ip)) {
                     srManager.routingRulePopulator.revokeRoute(
                             prevDeviceId, ip.toIpPrefix(), mac, prevPort);
                 }
@@ -243,8 +235,7 @@
         if (accepted(event.subject())) {
             // Populate new IP table entry
             newIps.forEach(ip -> {
-                if (ip.isIp4()) {
-                    addPerHostRoute(newLocation, ip.getIp4Address());
+                if (ip.isIp4() && srManager.deviceConfiguration.inSameSubnet(newLocation, ip)) {
                     srManager.routingRulePopulator.populateRoute(
                             newDeviceId, ip.toIpPrefix(), mac, newPort);
                 }
@@ -313,42 +304,7 @@
     }
 
     /**
-     * Add per host route to subnet list and populate the flow rule if the host
-     * does not belong to the configured subnet.
-     *
-     * @param location location of the host being added
-     * @param ip IP address of the host being added
-     */
-    private void addPerHostRoute(ConnectPoint location, Ip4Address ip) {
-        Ip4Prefix portSubnet = srManager.deviceConfiguration.getPortSubnet(
-                location.deviceId(), location.port());
-        if (portSubnet != null && !portSubnet.contains(ip)) {
-            Ip4Prefix ip4Prefix = ip.toIpPrefix().getIp4Prefix();
-            srManager.deviceConfiguration.addSubnet(location, ip4Prefix);
-            srManager.defaultRoutingHandler.populateSubnet(location,
-                    ImmutableSet.of(ip4Prefix));
-        }
-    }
-
-    /**
-     * Remove per host route from subnet list and revoke the flow rule if the
-     * host does not belong to the configured subnet.
-     *
-     * @param location location of the host being removed
-     * @param ip IP address of the host being removed
-     */
-    private void removePerHostRoute(ConnectPoint location, Ip4Address ip) {
-        Ip4Prefix portSubnet = srManager.deviceConfiguration.getPortSubnet(
-                location.deviceId(), location.port());
-        if (portSubnet != null && !portSubnet.contains(ip)) {
-            Ip4Prefix ip4Prefix = ip.toIpPrefix().getIp4Prefix();
-            srManager.deviceConfiguration.removeSubnet(location, ip4Prefix);
-            srManager.defaultRoutingHandler.revokeSubnet(ImmutableSet.of(ip4Prefix));
-        }
-    }
-
-    /**
-     * Check if a host is accepted or not.
+     * Determines whether a host should be accepted by SR or not.
      *
      * @param host host to be checked
      * @return true if segment routing accepts the host