Adding route subsystem code to support EVPN

Change-Id: I5d9715af8e2a9474892267c97cd4763f12be4816
diff --git a/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedEvpnRouteStore.java b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/DistributedEvpnRouteStore.java
new file mode 100755
index 0000000..49356b6
--- /dev/null
+++ b/incubator/store/src/main/java/org/onosproject/incubator/store/routing/impl/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.incubator.store.routing.impl;
+
+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.incubator.net.routing.EvpnInternalRouteEvent;
+import org.onosproject.incubator.net.routing.EvpnRoute;
+import org.onosproject.incubator.net.routing.EvpnRouteSet;
+import org.onosproject.incubator.net.routing.EvpnRouteStore;
+import org.onosproject.incubator.net.routing.EvpnRouteStoreDelegate;
+import org.onosproject.incubator.net.routing.EvpnTable;
+import org.onosproject.incubator.net.routing.RouteTableId;
+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 RouteTableId EVPN_IPV4 = new RouteTableId("evpn_ipv4");
+    private static final RouteTableId EVPN_IPV6 = new RouteTableId("evpn_ipv6");
+
+    private final SetEventListener<RouteTableId> masterRouteTableListener =
+            new MasterRouteTableListener();
+    private final EvpnRouteStoreDelegate ourDelegate = new
+            InternalEvpnRouteStoreDelegate();
+
+    // Stores the route tables that have been created
+    public DistributedSet<RouteTableId> masterRouteTable;
+    // Local memory map to store route table object
+    public Map<RouteTableId, 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(RouteTableId.class)
+                .build();
+
+        masterRouteTable = storageService.<RouteTableId>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<RouteTableId> getRouteTables() {
+        return ImmutableSet.copyOf(masterRouteTable);
+    }
+
+    @Override
+    public Collection<EvpnRouteSet> getRoutes(RouteTableId 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(RouteTableId tableId) {
+        routeTables.computeIfAbsent(tableId, id -> new EvpnRouteTable(id,
+                                                                      ourDelegate, storageService, executor));
+    }
+
+    private void destroyRouteTable(RouteTableId 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) {
+        RouteTableId 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<RouteTableId> {
+        @Override
+        public void event(SetEvent<RouteTableId> event) {
+            switch (event.type()) {
+                case ADD:
+                    executor.execute(() -> createRouteTable(event.entry()));
+                    break;
+                case REMOVE:
+                    executor.execute(() -> destroyRouteTable(event.entry()));
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}