initial mobility support

Change-Id: Idf42bd2f769b3c687c4acc18241e19970c6cd7e2
diff --git a/apps/mobility/pom.xml b/apps/mobility/pom.xml
new file mode 100644
index 0000000..a919ff2
--- /dev/null
+++ b/apps/mobility/pom.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <groupId>org.onlab.onos</groupId>
+        <artifactId>onos-apps</artifactId>
+        <version>1.0.0-SNAPSHOT</version>
+        <relativePath>../pom.xml</relativePath>
+    </parent>
+
+    <artifactId>onos-app-mobility</artifactId>
+    <packaging>bundle</packaging>
+
+    <description>ONOS simple Mobility app</description>
+
+</project>
diff --git a/apps/mobility/src/main/java/org/onlab/onos/mobility/HostMobility.java b/apps/mobility/src/main/java/org/onlab/onos/mobility/HostMobility.java
new file mode 100644
index 0000000..cb60405
--- /dev/null
+++ b/apps/mobility/src/main/java/org/onlab/onos/mobility/HostMobility.java
@@ -0,0 +1,118 @@
+package org.onlab.onos.mobility;
+import static org.slf4j.LoggerFactory.getLogger;
+
+import java.util.Collection;
+import java.util.List;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.onlab.onos.ApplicationId;
+import org.onlab.onos.net.Device;
+import org.onlab.onos.net.Host;
+import org.onlab.onos.net.device.DeviceService;
+import org.onlab.onos.net.flow.FlowRule;
+import org.onlab.onos.net.flow.FlowRuleService;
+import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
+import org.onlab.onos.net.flow.criteria.Criterion;
+import org.onlab.onos.net.flow.criteria.Criterion.Type;
+import org.onlab.onos.net.host.HostEvent;
+import org.onlab.onos.net.host.HostListener;
+import org.onlab.onos.net.host.HostService;
+import org.onlab.packet.MacAddress;
+import org.slf4j.Logger;
+
+import com.google.common.collect.Lists;
+
+
+/**
+ * Sample reactive forwarding application.
+ */
+@Component(immediate = true)
+public class HostMobility {
+
+    private final Logger log = getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected HostService hostService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected FlowRuleService flowRuleService;
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected DeviceService deviceService;
+
+    private ApplicationId appId;
+
+    @Activate
+    public void activate() {
+        appId = ApplicationId.getAppId();
+        hostService.addListener(new InternalHostListener());
+        log.info("Started with Application ID {}", appId.id());
+    }
+
+    @Deactivate
+    public void deactivate() {
+        flowRuleService.removeFlowRulesById(appId);
+        log.info("Stopped");
+    }
+
+    public class InternalHostListener
+    implements HostListener {
+
+        @Override
+        public void event(HostEvent event) {
+            switch (event.type()) {
+                case HOST_ADDED:
+                case HOST_REMOVED:
+                case HOST_UPDATED:
+                    // don't care if a host has been added, removed.
+                    break;
+                case HOST_MOVED:
+                    log.info("Host {} has moved; cleaning up.", event.subject());
+                    cleanup(event.subject());
+                    break;
+
+                default:
+                    break;
+
+            }
+
+        }
+
+        private void cleanup(Host host) {
+            Iterable<Device> devices = deviceService.getDevices();
+            List<FlowRule> flowRules = Lists.newLinkedList();
+            for (Device device : devices) {
+                   flowRules.addAll(cleanupDevice(device, host));
+            }
+            FlowRule[] flows = new FlowRule[flowRules.size()];
+            flows = flowRules.toArray(flows);
+            flowRuleService.removeFlowRules(flows);
+        }
+
+        private Collection<? extends FlowRule> cleanupDevice(Device device, Host host) {
+            List<FlowRule> flowRules = Lists.newLinkedList();
+            MacAddress mac = host.mac();
+            for (FlowRule rule : flowRuleService.getFlowEntries(device.id())) {
+                for (Criterion c : rule.selector().criteria()) {
+                    if (c.type() == Type.ETH_DST || c.type() == Type.ETH_SRC) {
+                        EthCriterion eth = (EthCriterion) c;
+                        if (eth.mac().equals(mac)) {
+                            flowRules.add(rule);
+                            break;
+                        }
+                    }
+                }
+            }
+            //TODO: handle ip cleanup
+            return flowRules;
+        }
+
+    }
+
+}
+
+
diff --git a/apps/mobility/src/main/java/org/onlab/onos/mobility/package-info.java b/apps/mobility/src/main/java/org/onlab/onos/mobility/package-info.java
new file mode 100644
index 0000000..ea5bdf0
--- /dev/null
+++ b/apps/mobility/src/main/java/org/onlab/onos/mobility/package-info.java
@@ -0,0 +1,4 @@
+/**
+ * Trivial application that provides simple form of reactive forwarding.
+ */
+package org.onlab.onos.mobility;