ONOS-6903 Move EVPN from incubator to apps

Change-Id: Id84c59e28f2591535b0726afbc1a2fa3caf07db5
diff --git a/apps/evpn-route-service/app/BUCK b/apps/evpn-route-service/app/BUCK
new file mode 100644
index 0000000..1231207
--- /dev/null
+++ b/apps/evpn-route-service/app/BUCK
@@ -0,0 +1,16 @@
+COMPILE_DEPS = [
+    '//lib:CORE_DEPS',
+    '//utils/misc:onlab-misc',
+    '//core/store/serializers:onos-core-serializers',
+    '//apps/evpn-route-service/api:onos-apps-evpn-route-service-api',
+]
+
+TEST_DEPS = [
+    '//lib:TEST',
+    '//core/api:onos-api-tests',
+]
+
+osgi_jar_with_tests (
+    deps = COMPILE_DEPS,
+    test_deps = TEST_DEPS,
+)
diff --git a/apps/evpn-route-service/app/pom.xml b/apps/evpn-route-service/app/pom.xml
new file mode 100644
index 0000000..780a9db
--- /dev/null
+++ b/apps/evpn-route-service/app/pom.xml
@@ -0,0 +1,66 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ Copyright 2017-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.
+  -->
+<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/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <parent>
+        <artifactId>onos-app-evpn-route-service</artifactId>
+        <groupId>org.onosproject</groupId>
+        <version>1.11.0-SNAPSHOT</version>
+    </parent>
+
+    <artifactId>onos-app-evpn-route-service-app</artifactId>
+    <packaging>bundle</packaging>
+
+    <url>http://onosproject.org</url>
+
+    <description>EVPN Routing Application Implementation</description>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onlab-junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-core-serializers</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-incubator-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-api</artifactId>
+            <classifier>tests</classifier>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.onosproject</groupId>
+            <artifactId>onos-app-evpn-route-service-api</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+    </dependencies>
+
+
+</project>
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnListenerQueue.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnListenerQueue.java
new file mode 100644
index 0000000..ce3d012
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnListenerQueue.java
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2017-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.evpnrouteservice.impl;
+
+import org.onosproject.evpnrouteservice.EvpnRouteEvent;
+
+/**
+ * Queues updates for a route listener to ensure they are received in the
+ * correct order.
+ */
+interface EvpnListenerQueue {
+
+    /**
+     * Posts an event to the listener.
+     *
+     * @param event event
+     */
+    void post(EvpnRouteEvent event);
+
+    /**
+     * Initiates event delivery to the listener.
+     */
+    void start();
+
+    /**
+     * Halts event delivery to the listener.
+     */
+    void stop();
+}
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnRouteManager.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnRouteManager.java
new file mode 100644
index 0000000..19e6311
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/EvpnRouteManager.java
@@ -0,0 +1,274 @@
+/*
+ * Copyright 2017-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.evpnrouteservice.impl;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ThreadFactory;
+
+import javax.annotation.concurrent.GuardedBy;
+
+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.apache.felix.scr.annotations.Service;
+
+import org.onosproject.evpnrouteservice.EvpnInternalRouteEvent;
+import org.onosproject.evpnrouteservice.EvpnRoute;
+import org.onosproject.evpnrouteservice.EvpnRouteAdminService;
+import org.onosproject.evpnrouteservice.EvpnRouteEvent;
+import org.onosproject.evpnrouteservice.EvpnRouteListener;
+import org.onosproject.evpnrouteservice.EvpnRouteService;
+import org.onosproject.evpnrouteservice.EvpnRouteSet;
+import org.onosproject.evpnrouteservice.EvpnRouteStore;
+import org.onosproject.evpnrouteservice.EvpnRouteStoreDelegate;
+import org.onosproject.evpnrouteservice.EvpnRouteTableId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static java.util.concurrent.Executors.newSingleThreadExecutor;
+import static org.onlab.util.Tools.groupedThreads;
+
+/**
+ * Implementation of the EVPN route service.
+ */
+@Service
+@Component
+public class EvpnRouteManager implements EvpnRouteService,
+        EvpnRouteAdminService {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    protected EvpnRouteStore evpnRouteStore;
+
+    @GuardedBy(value = "this")
+    private Map<EvpnRouteListener, EvpnListenerQueue> listeners = new
+            HashMap<>();
+
+    private ThreadFactory threadFactory;
+
+    private EvpnRouteStoreDelegate evpnRouteStoreDelegate = new
+            InternalEvpnRouteStoreDelegate();
+
+    @Activate
+    protected void activate() {
+        threadFactory = groupedThreads("onos/route", "listener-%d", log);
+        evpnRouteStore.setDelegate(evpnRouteStoreDelegate);
+
+    }
+
+    @Deactivate
+    protected void deactivate() {
+        evpnRouteStore.unsetDelegate(evpnRouteStoreDelegate);
+        listeners.values().forEach(EvpnListenerQueue::stop);
+    }
+
+    /**
+     * {@inheritDoc}
+     * <p>
+     * In a departure from other services in ONOS, calling addListener will
+     * cause all current routes to be pushed to the listener before any new
+     * events are sent. This allows a listener to easily get the exact set of
+     * routes without worrying about missing any.
+     *
+     * @param listener listener to be added
+     */
+    @Override
+    public void addListener(EvpnRouteListener listener) {
+        synchronized (this) {
+            EvpnListenerQueue l = createListenerQueue(listener);
+
+            evpnRouteStore.getRouteTables().forEach(routeTableId
+                                                            -> {
+                Collection<EvpnRouteSet> routes
+                        = evpnRouteStore.getRoutes(routeTableId);
+                if (routes != null) {
+                    routes.forEach(route -> {
+                        Collection<EvpnRoute> evpnRoutes = route.routes();
+                        for (EvpnRoute evpnRoute : evpnRoutes) {
+                            l.post(new EvpnRouteEvent(
+                                    EvpnRouteEvent.Type.ROUTE_ADDED,
+                                    evpnRoute,
+                                    route.routes()));
+                        }
+                    });
+                }
+            });
+            listeners.put(listener, l);
+
+            l.start();
+            log.debug("Route synchronization complete");
+        }
+    }
+
+    @Override
+    public void removeListener(EvpnRouteListener listener) {
+        synchronized (this) {
+            EvpnListenerQueue l = listeners.remove(listener);
+            if (l != null) {
+                l.stop();
+            }
+        }
+    }
+
+    /**
+     * Posts an event to all listeners.
+     *
+     * @param event event
+     */
+
+    private void post(EvpnRouteEvent event) {
+        if (event != null) {
+            log.debug("Sending event {}", event);
+            synchronized (this) {
+                listeners.values().forEach(l -> l.post(event));
+            }
+        }
+    }
+
+
+    public Collection<EvpnRouteTableId> getRouteTables() {
+        return evpnRouteStore.getRouteTables();
+    }
+
+    @Override
+    public void update(Collection<EvpnRoute> routes) {
+        synchronized (this) {
+            routes.forEach(route -> {
+                log.debug("Received update {}", route);
+                evpnRouteStore.updateRoute(route);
+            });
+        }
+    }
+
+    @Override
+    public void withdraw(Collection<EvpnRoute> routes) {
+        synchronized (this) {
+            routes.forEach(route -> {
+                log.debug("Received withdraw {}", route);
+                evpnRouteStore.removeRoute(route);
+            });
+        }
+    }
+
+    /**
+     * Creates a new listener queue.
+     *
+     * @param listener route listener
+     * @return listener queue
+     */
+    DefaultListenerQueue createListenerQueue(EvpnRouteListener listener) {
+        return new DefaultListenerQueue(listener);
+    }
+
+    /**
+     * Default route listener queue.
+     */
+    private class DefaultListenerQueue implements EvpnListenerQueue {
+
+        private final ExecutorService executorService;
+        private final BlockingQueue<EvpnRouteEvent> queue;
+        private final EvpnRouteListener listener;
+
+        /**
+         * Creates a new listener queue.
+         *
+         * @param listener route listener to queue updates for
+         */
+        public DefaultListenerQueue(EvpnRouteListener listener) {
+            this.listener = listener;
+            queue = new LinkedBlockingQueue<>();
+            executorService = newSingleThreadExecutor(threadFactory);
+        }
+
+        @Override
+        public void post(EvpnRouteEvent event) {
+            queue.add(event);
+        }
+
+        @Override
+        public void start() {
+            executorService.execute(this::poll);
+        }
+
+        @Override
+        public void stop() {
+            executorService.shutdown();
+        }
+
+        private void poll() {
+            while (true) {
+                try {
+                    listener.event(queue.take());
+                } catch (InterruptedException e) {
+                    log.info("Route listener event thread shutting down: {}", e.getMessage());
+                    break;
+                } catch (Exception e) {
+                    log.warn("Exception during route event handler", e);
+                }
+            }
+        }
+    }
+
+    /**
+     * Delegate to receive events from the route store.
+     */
+    private class InternalEvpnRouteStoreDelegate implements
+            EvpnRouteStoreDelegate {
+        EvpnRouteSet routes;
+
+        @Override
+        public void notify(EvpnInternalRouteEvent event) {
+            switch (event.type()) {
+                case ROUTE_ADDED:
+                    routes = event.subject();
+                    if (routes != null) {
+                        Collection<EvpnRoute> evpnRoutes = routes.routes();
+                        for (EvpnRoute evpnRoute : evpnRoutes) {
+                            post(new EvpnRouteEvent(
+                                    EvpnRouteEvent.Type.ROUTE_ADDED,
+                                    evpnRoute,
+                                    routes.routes()));
+                        }
+                    }
+                    break;
+                case ROUTE_REMOVED:
+                    routes = event.subject();
+                    if (routes != null) {
+                        Collection<EvpnRoute> evpnRoutes = routes.routes();
+                        for (EvpnRoute evpnRoute : evpnRoutes) {
+                            post(new EvpnRouteEvent(
+                                    EvpnRouteEvent.Type.ROUTE_REMOVED,
+                                    evpnRoute,
+                                    routes.routes()));
+                        }
+                    }
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+
+}
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/package-info.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/package-info.java
new file mode 100644
index 0000000..80892c9
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/impl/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * Implementation of route service.
+ */
+package org.onosproject.evpnrouteservice.impl;
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/DistributedEvpnRouteStore.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/DistributedEvpnRouteStore.java
new file mode 100644
index 0000000..de1dbd6
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/DistributedEvpnRouteStore.java
@@ -0,0 +1,203 @@
+/*
+ * Copyright 2017-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.evpnrouteservice.store;
+
+import com.google.common.collect.ImmutableSet;
+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.apache.felix.scr.annotations.Service;
+import org.onlab.packet.IpAddress;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.evpnrouteservice.EvpnInternalRouteEvent;
+import org.onosproject.evpnrouteservice.EvpnRoute;
+import org.onosproject.evpnrouteservice.EvpnRouteSet;
+import org.onosproject.evpnrouteservice.EvpnRouteStore;
+import org.onosproject.evpnrouteservice.EvpnRouteStoreDelegate;
+import org.onosproject.evpnrouteservice.EvpnRouteTableId;
+import org.onosproject.evpnrouteservice.EvpnTable;
+import org.onosproject.store.AbstractStore;
+import org.onosproject.store.service.DistributedSet;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.SetEvent;
+import org.onosproject.store.service.SetEventListener;
+import org.onosproject.store.service.StorageService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+
+import static org.onlab.util.Tools.groupedThreads;
+
+/**
+ * Route store based on distributed storage.
+ */
+@Service
+@Component
+public class DistributedEvpnRouteStore extends
+        AbstractStore<EvpnInternalRouteEvent,
+                EvpnRouteStoreDelegate>
+        implements EvpnRouteStore {
+
+    private static final Logger log = LoggerFactory
+            .getLogger(DistributedEvpnRouteStore.class);
+
+    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
+    public StorageService storageService;
+
+    private static final EvpnRouteTableId EVPN_IPV4 = new EvpnRouteTableId("evpn_ipv4");
+    private static final EvpnRouteTableId EVPN_IPV6 = new EvpnRouteTableId("evpn_ipv6");
+
+    private final SetEventListener<EvpnRouteTableId> masterRouteTableListener =
+            new MasterRouteTableListener();
+    private final EvpnRouteStoreDelegate ourDelegate = new
+            InternalEvpnRouteStoreDelegate();
+
+    // Stores the route tables that have been created
+    public DistributedSet<EvpnRouteTableId> masterRouteTable;
+    // Local memory map to store route table object
+    public Map<EvpnRouteTableId, EvpnTable> routeTables;
+
+    private ExecutorService executor;
+
+
+    /**
+     * Sets up distributed route store.
+     */
+    @Activate
+    public void activate() {
+        routeTables = new ConcurrentHashMap<>();
+        executor = Executors.newSingleThreadExecutor(groupedThreads("onos/route", "store", log));
+
+        KryoNamespace masterRouteTableSerializer = KryoNamespace.newBuilder()
+                .register(EvpnRouteTableId.class)
+                .build();
+
+        masterRouteTable = storageService.<EvpnRouteTableId>setBuilder()
+                .withName("onos-master-route-table")
+                .withSerializer(Serializer.using(masterRouteTableSerializer))
+                .build()
+                .asDistributedSet();
+
+        masterRouteTable.forEach(this::createRouteTable);
+
+        masterRouteTable.addListener(masterRouteTableListener);
+
+        // Add default tables (add is idempotent)
+        masterRouteTable.add(EVPN_IPV4);
+        masterRouteTable.add(EVPN_IPV6);
+
+        log.info("Started");
+    }
+
+    /**
+     * Cleans up distributed route store.
+     */
+    @Deactivate
+    public void deactivate() {
+        masterRouteTable.removeListener(masterRouteTableListener);
+
+        routeTables.values().forEach(EvpnTable::shutdown);
+
+        log.info("Stopped");
+    }
+
+    @Override
+    public void updateRoute(EvpnRoute route) {
+        getDefaultRouteTable(route).update(route);
+    }
+
+    @Override
+    public void removeRoute(EvpnRoute route) {
+        getDefaultRouteTable(route).remove(route);
+    }
+
+    @Override
+    public Set<EvpnRouteTableId> getRouteTables() {
+        return ImmutableSet.copyOf(masterRouteTable);
+    }
+
+    @Override
+    public Collection<EvpnRouteSet> getRoutes(EvpnRouteTableId table) {
+        EvpnTable routeTable = routeTables.get(table);
+        if (routeTable == null) {
+            return Collections.emptySet();
+        } else {
+            return ImmutableSet.copyOf(routeTable.getRoutes());
+        }
+    }
+
+    @Override
+    public Collection<EvpnRoute> getRoutesForNextHop(IpAddress ip) {
+        return getDefaultRouteTable(ip).getRoutesForNextHop(ip);
+    }
+
+    private void createRouteTable(EvpnRouteTableId tableId) {
+        routeTables.computeIfAbsent(tableId, id -> new EvpnRouteTable(id,
+                                                                      ourDelegate, storageService, executor));
+    }
+
+    private void destroyRouteTable(EvpnRouteTableId tableId) {
+        EvpnTable table = routeTables.remove(tableId);
+        if (table != null) {
+            table.destroy();
+        }
+    }
+
+    private EvpnTable getDefaultRouteTable(EvpnRoute route) {
+        return getDefaultRouteTable(route.prefixIp().address());
+    }
+
+    private EvpnTable getDefaultRouteTable(IpAddress ip) {
+        EvpnRouteTableId routeTableId = (ip.isIp4()) ? EVPN_IPV4 : EVPN_IPV6;
+        return routeTables.getOrDefault(routeTableId, EmptyEvpnRouteTable
+                .instance());
+    }
+
+    private class InternalEvpnRouteStoreDelegate implements
+            EvpnRouteStoreDelegate {
+        @Override
+        public void notify(EvpnInternalRouteEvent event) {
+            executor.execute(() -> DistributedEvpnRouteStore
+                    .this.notifyDelegate(event));
+        }
+    }
+
+    private class MasterRouteTableListener implements SetEventListener<EvpnRouteTableId> {
+        @Override
+        public void event(SetEvent<EvpnRouteTableId> event) {
+            switch (event.type()) {
+                case ADD:
+                    executor.execute(() -> createRouteTable(event.entry()));
+                    break;
+                case REMOVE:
+                    executor.execute(() -> destroyRouteTable(event.entry()));
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EmptyEvpnRouteTable.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EmptyEvpnRouteTable.java
new file mode 100644
index 0000000..f49a174
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EmptyEvpnRouteTable.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright 2017-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.evpnrouteservice.store;
+
+import org.onlab.packet.IpAddress;
+import org.onosproject.evpnrouteservice.EvpnPrefix;
+import org.onosproject.evpnrouteservice.EvpnRoute;
+import org.onosproject.evpnrouteservice.EvpnRouteSet;
+import org.onosproject.evpnrouteservice.EvpnRouteTableId;
+import org.onosproject.evpnrouteservice.EvpnTable;
+
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * Route table that contains no routes.
+ */
+public final class EmptyEvpnRouteTable implements EvpnTable {
+
+    private final EvpnRouteTableId id = new EvpnRouteTableId("empty");
+
+    private static final EmptyEvpnRouteTable INSTANCE = new EmptyEvpnRouteTable();
+
+    /**
+     * Returns the instance of the empty route table.
+     *
+     * @return empty route table
+     */
+    public static EmptyEvpnRouteTable instance() {
+        return INSTANCE;
+    }
+
+    private EmptyEvpnRouteTable() {
+    }
+
+    @Override
+    public void update(EvpnRoute route) {
+
+    }
+
+    @Override
+    public void remove(EvpnRoute route) {
+
+    }
+
+    @Override
+    public EvpnRouteTableId id() {
+        return id;
+    }
+
+    @Override
+    public Collection<EvpnRouteSet> getRoutes() {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public EvpnRouteSet getRoutes(EvpnPrefix prefix) {
+        return null;
+    }
+
+    @Override
+    public Collection<EvpnRoute> getRoutesForNextHop(IpAddress nextHop) {
+        return Collections.emptyList();
+    }
+
+    @Override
+    public void shutdown() {
+
+    }
+
+    @Override
+    public void destroy() {
+
+    }
+}
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EvpnRouteTable.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EvpnRouteTable.java
new file mode 100644
index 0000000..2c3e07b
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/EvpnRouteTable.java
@@ -0,0 +1,230 @@
+/*
+ * Copyright 2017-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.evpnrouteservice.store;
+
+import org.onlab.packet.IpAddress;
+import org.onlab.packet.IpPrefix;
+import org.onlab.packet.MacAddress;
+import org.onlab.util.KryoNamespace;
+import org.onosproject.evpnrouteservice.EvpnInternalRouteEvent;
+import org.onosproject.evpnrouteservice.EvpnPrefix;
+import org.onosproject.evpnrouteservice.EvpnRoute;
+import org.onosproject.evpnrouteservice.EvpnRouteSet;
+import org.onosproject.evpnrouteservice.EvpnRouteStoreDelegate;
+import org.onosproject.evpnrouteservice.EvpnRouteTableId;
+import org.onosproject.evpnrouteservice.EvpnTable;
+import org.onosproject.evpnrouteservice.Label;
+import org.onosproject.evpnrouteservice.RouteDistinguisher;
+import org.onosproject.evpnrouteservice.VpnRouteTarget;
+import org.onosproject.store.serializers.KryoNamespaces;
+import org.onosproject.store.service.ConsistentMap;
+import org.onosproject.store.service.DistributedPrimitive;
+import org.onosproject.store.service.MapEvent;
+import org.onosproject.store.service.MapEventListener;
+import org.onosproject.store.service.Serializer;
+import org.onosproject.store.service.StorageService;
+import org.onosproject.store.service.Versioned;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+
+import static com.google.common.base.Preconditions.checkNotNull;
+
+/**
+ * Default implementation of a route table based on a consistent map.
+ */
+public class EvpnRouteTable implements EvpnTable {
+
+    private final EvpnRouteTableId id;
+    private final ConsistentMap<EvpnPrefix, Set<EvpnRoute>> routes;
+    private final EvpnRouteStoreDelegate delegate;
+    private final ExecutorService executor;
+    private final RouteTableListener listener = new RouteTableListener();
+
+    private final Consumer<DistributedPrimitive.Status> statusChangeListener;
+
+    /**
+     * Creates a new route table.
+     *
+     * @param id             route table ID
+     * @param delegate       route store delegate to notify of events
+     * @param storageService storage service
+     * @param executor       executor service
+     */
+    public EvpnRouteTable(EvpnRouteTableId id, EvpnRouteStoreDelegate delegate,
+                          StorageService storageService, ExecutorService executor) {
+        this.delegate = checkNotNull(delegate);
+        this.id = checkNotNull(id);
+        this.routes = buildRouteMap(checkNotNull(storageService));
+        this.executor = checkNotNull(executor);
+
+        statusChangeListener = status -> {
+            if (status.equals(DistributedPrimitive.Status.ACTIVE)) {
+                executor.execute(this::notifyExistingRoutes);
+            }
+        };
+        routes.addStatusChangeListener(statusChangeListener);
+
+        notifyExistingRoutes();
+
+        routes.addListener(listener);
+    }
+
+    private void notifyExistingRoutes() {
+        routes.entrySet().stream()
+                .map(e -> new EvpnInternalRouteEvent(
+                        EvpnInternalRouteEvent.Type.ROUTE_ADDED,
+                        new EvpnRouteSet(id, e.getKey(), e.getValue().value())))
+                .forEach(delegate::notify);
+    }
+
+    private ConsistentMap<EvpnPrefix, Set<EvpnRoute>> buildRouteMap(StorageService
+                                                                            storageService) {
+        KryoNamespace routeTableSerializer = KryoNamespace.newBuilder()
+                .register(KryoNamespaces.API)
+                .register(KryoNamespaces.MISC)
+                .register(EvpnRoute.class)
+                .register(EvpnPrefix.class)
+                .register(RouteDistinguisher.class)
+                .register(MacAddress.class)
+                .register(IpPrefix.class)
+                .register(EvpnRoute.Source.class)
+                .register(IpAddress.class)
+                .register(VpnRouteTarget.class)
+                .register(Label.class)
+                .register(EvpnRouteTableId.class)
+                .build();
+        return storageService.<EvpnPrefix, Set<EvpnRoute>>consistentMapBuilder()
+                .withName("onos-evpn-routes-" + id.name())
+                .withRelaxedReadConsistency()
+                .withSerializer(Serializer.using(routeTableSerializer))
+                .build();
+    }
+
+    @Override
+    public EvpnRouteTableId id() {
+        return id;
+    }
+
+    @Override
+    public void shutdown() {
+        routes.removeStatusChangeListener(statusChangeListener);
+        routes.removeListener(listener);
+    }
+
+    @Override
+    public void destroy() {
+        shutdown();
+        routes.destroy();
+    }
+
+    @Override
+    public void update(EvpnRoute route) {
+        routes.compute(route.evpnPrefix(), (prefix, set) -> {
+            if (set == null) {
+                set = new HashSet<>();
+            }
+            set.add(route);
+            return set;
+        });
+    }
+
+    @Override
+    public void remove(EvpnRoute route) {
+        routes.compute(route.evpnPrefix(), (prefix, set) -> {
+            if (set != null) {
+                set.remove(route);
+                if (set.isEmpty()) {
+                    return null;
+                }
+                return set;
+            }
+            return null;
+        });
+    }
+
+    @Override
+    public Collection<EvpnRouteSet> getRoutes() {
+        return routes.entrySet().stream()
+                .map(e -> new EvpnRouteSet(id, e.getKey(), e.getValue().value()))
+                .collect(Collectors.toSet());
+    }
+
+    @Override
+    public EvpnRouteSet getRoutes(EvpnPrefix prefix) {
+        Versioned<Set<EvpnRoute>> routeSet = routes.get(prefix);
+
+        if (routeSet != null) {
+            return new EvpnRouteSet(id, prefix, routeSet.value());
+        }
+        return null;
+    }
+
+    @Override
+    public Collection<EvpnRoute> getRoutesForNextHop(IpAddress nextHop) {
+        // TODO index
+        return routes.values().stream()
+                .flatMap(v -> v.value().stream())
+                .filter(r -> r.nextHop().equals(nextHop))
+                .collect(Collectors.toSet());
+    }
+
+    private class RouteTableListener
+            implements MapEventListener<EvpnPrefix, Set<EvpnRoute>> {
+
+        private EvpnInternalRouteEvent createRouteEvent(
+                EvpnInternalRouteEvent.Type type, MapEvent<EvpnPrefix, Set<EvpnRoute>>
+                event) {
+            Set<EvpnRoute> currentRoutes =
+                    (event.newValue() == null) ? Collections.emptySet() : event.newValue().value();
+            return new EvpnInternalRouteEvent(type, new EvpnRouteSet(id, event
+                    .key(), currentRoutes));
+        }
+
+        @Override
+        public void event(MapEvent<EvpnPrefix, Set<EvpnRoute>> event) {
+            EvpnInternalRouteEvent ire = null;
+            switch (event.type()) {
+                case INSERT:
+                    ire = createRouteEvent(EvpnInternalRouteEvent.Type.ROUTE_ADDED, event);
+                    break;
+                case UPDATE:
+                    if (event.newValue().value().size() > event.oldValue().value().size()) {
+                        ire = createRouteEvent(EvpnInternalRouteEvent.Type.ROUTE_ADDED, event);
+                    } else {
+                        ire = createRouteEvent(EvpnInternalRouteEvent.Type.ROUTE_REMOVED, event);
+                    }
+                    break;
+                case REMOVE:
+                    ire = createRouteEvent(EvpnInternalRouteEvent.Type.ROUTE_REMOVED, event);
+                    break;
+                default:
+                    break;
+            }
+            if (ire != null) {
+                delegate.notify(ire);
+            }
+        }
+    }
+
+}
+
diff --git a/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/package-info.java b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/package-info.java
new file mode 100644
index 0000000..52e9d6c
--- /dev/null
+++ b/apps/evpn-route-service/app/src/main/java/org/onosproject/evpnrouteservice/store/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2016-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.
+ */
+
+/**
+ * Implementation of the unicast routing service.
+ */
+package org.onosproject.evpnrouteservice.store;