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/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/DeviceConfiguration.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/DeviceConfiguration.java
index cb0a275..c2360cb 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/DeviceConfiguration.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/DeviceConfiguration.java
@@ -20,6 +20,7 @@
 import com.google.common.collect.SetMultimap;
 import org.onlab.packet.Ip4Address;
 import org.onlab.packet.Ip4Prefix;
+import org.onlab.packet.IpAddress;
 import org.onlab.packet.IpPrefix;
 import org.onlab.packet.MacAddress;
 import org.onlab.packet.VlanId;
@@ -331,15 +332,7 @@
                       srinfo.subnets.values());
 
             ImmutableSet.Builder<Ip4Prefix> builder = ImmutableSet.builder();
-            builder.addAll(srinfo.subnets.values());
-            SegmentRoutingAppConfig appConfig =
-                    srManager.cfgService.getConfig(srManager.appId, SegmentRoutingAppConfig.class);
-            if (appConfig != null) {
-                if (deviceId.equals(appConfig.vRouterId().orElse(null))) {
-                    builder.add(Ip4Prefix.valueOf("0.0.0.0/0"));
-                }
-            }
-            return builder.build();
+            return builder.addAll(srinfo.subnets.values()).build();
         }
         return null;
     }
@@ -452,6 +445,20 @@
     }
 
     /**
+     * Checks if the IP is in the subnet defined on given connect point.
+     *
+     * @param connectPoint Connect point
+     * @param ip The IP address to check
+     * @return True if the IP belongs to the subnet.
+     *         False if the IP does not belong to the subnet, or
+     *         there is no subnet configuration on given connect point.
+     */
+    public boolean inSameSubnet(ConnectPoint connectPoint, IpAddress ip) {
+        Ip4Prefix portSubnet = getPortSubnet(connectPoint.deviceId(), connectPoint.port());
+        return portSubnet != null && portSubnet.contains(ip);
+    }
+
+    /**
      * Returns the ports corresponding to the adjacency Sid given.
      *
      * @param deviceId device identification of the router
@@ -516,7 +523,7 @@
         SegmentRoutingAppConfig appConfig = srManager.cfgService
                 .getConfig(srManager.appId, SegmentRoutingAppConfig.class);
         if (appConfig != null && appConfig.suppressSubnet().contains(connectPoint)) {
-            log.info("Ignore suppressed port {}", connectPoint);
+            log.info("Interface configuration on port {} is ignored", connectPoint);
             return true;
         }
         return false;
diff --git a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
index 3fe7e4b..7e85a74 100644
--- a/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
+++ b/apps/segmentrouting/src/main/java/org/onosproject/segmentrouting/config/SegmentRoutingAppConfig.java
@@ -22,10 +22,8 @@
 import org.onlab.packet.MacAddress;
 import org.onosproject.core.ApplicationId;
 import org.onosproject.net.ConnectPoint;
-import org.onosproject.net.DeviceId;
 import org.onosproject.net.config.Config;
 
-import java.util.Optional;
 import java.util.Set;
 
 import static com.google.common.base.MoreObjects.toStringHelper;
@@ -35,7 +33,6 @@
  */
 public class SegmentRoutingAppConfig extends Config<ApplicationId> {
     private static final String VROUTER_MACS = "vRouterMacs";
-    private static final String VROUTER_ID = "vRouterId";
     private static final String SUPPRESS_SUBNET = "suppressSubnet";
     private static final String SUPPRESS_HOST_BY_PORT = "suppressHostByPort";
     // TODO We might want to move SUPPRESS_HOST_BY_PROVIDER to Component Config
@@ -44,9 +41,9 @@
 
     @Override
     public boolean isValid() {
-        return hasOnlyFields(VROUTER_MACS, VROUTER_ID, SUPPRESS_SUBNET,
+        return hasOnlyFields(VROUTER_MACS, SUPPRESS_SUBNET,
                 SUPPRESS_HOST_BY_PORT, SUPPRESS_HOST_BY_PROVIDER, MPLS_ECMP) &&
-                vRouterMacs() != null && vRouterId() != null &&
+                vRouterMacs() != null &&
                 suppressSubnet() != null && suppressHostByPort() != null &&
                 suppressHostByProvider() != null;
     }
@@ -125,39 +122,6 @@
     }
 
     /**
-     * Gets vRouter device ID.
-     *
-     * @return Optional vRouter device ID,
-     *         empty is not specified or null if not valid
-     */
-    public Optional<DeviceId> vRouterId() {
-        if (!object.has(VROUTER_ID)) {
-            return Optional.empty();
-        }
-
-        try {
-            return Optional.of(DeviceId.deviceId(object.path(VROUTER_ID).asText()));
-        } catch (IllegalArgumentException e) {
-            return null;
-        }
-    }
-
-    /**
-     * Sets vRouter device ID.
-     *
-     * @param vRouterId vRouter device ID
-     * @return this {@link SegmentRoutingAppConfig}
-     */
-    public SegmentRoutingAppConfig setVRouterId(DeviceId vRouterId) {
-        if (vRouterId == null) {
-            object.remove(VROUTER_ID);
-        } else {
-            object.put(VROUTER_ID, vRouterId.toString());
-        }
-        return this;
-    }
-
-    /**
      * Gets names of ports to which SegmentRouting does not push subnet rules.
      *
      * @return Set of port names, empty if not specified, or null
@@ -294,7 +258,6 @@
     public String toString() {
         return toStringHelper(this)
                 .add("vRouterMacs", vRouterMacs())
-                .add("vRouterId", vRouterId())
                 .add("suppressSubnet", suppressSubnet())
                 .add("suppressHostByPort", suppressHostByPort())
                 .add("suppressHostByProvider", suppressHostByProvider())