Ditch blueprint from frm-sync
[openflowplugin.git] / applications / forwardingrules-sync / src / main / java / org / opendaylight / openflowplugin / applications / frsync / impl / ForwardingRulesSyncProvider.java
index aa003835e105e5699f07391c624b689d80d40687..b4db87d40da7d66206c2020ae827de6bcc43416f 100644 (file)
-/**
+/*
  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.applications.frsync.impl;
 
-import com.google.common.base.Preconditions;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
-import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.applications.frsync.NodeListener;
+import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeCachedDao;
+import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeOdlDao;
+import org.opendaylight.openflowplugin.applications.frsync.dao.FlowCapableNodeSnapshotDao;
+import org.opendaylight.openflowplugin.applications.frsync.impl.clustering.DeviceMastershipManager;
+import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SyncPlanPushStrategyFlatBatchImpl;
+import org.opendaylight.openflowplugin.applications.frsync.util.ReconciliationRegistry;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flat.batch.service.rev160321.ProcessFlatBatch;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.FlowCapableTransactionService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTable;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * Top provider of forwarding rules synchronization functionality.
  */
-public class ForwardingRulesSyncProvider implements AutoCloseable, BindingAwareProvider {
-
+@Singleton
+@Component(service = { })
+public class ForwardingRulesSyncProvider implements AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesSyncProvider.class);
+    private static final String FRS_EXECUTOR_PREFIX = "FRS-executor-";
 
     private final DataBroker dataService;
-    private final SalFlowService salFlowService;
-    private final SalGroupService salGroupService;
-    private final SalMeterService salMeterService;
-    private final SalTableService salTableService;
-    private final FlowCapableTransactionService transactionService;
+    private final ClusterSingletonServiceProvider clusterSingletonService;
+    private final UpdateTable updateTable;
+    private final ProcessFlatBatch processFlatBatch;
 
-    /** wildcard path to flow-capable-node augmentation of inventory node */
+    /** Wildcard path to flow-capable-node augmentation of inventory node. */
     private static final InstanceIdentifier<FlowCapableNode> FLOW_CAPABLE_NODE_WC_PATH =
             InstanceIdentifier.create(Nodes.class).child(Node.class).augmentation(FlowCapableNode.class);
-    /** wildcard path to node (not flow-capable-node augmentation) of inventory node */
+    /** Wildcard path to node (not flow-capable-node augmentation) of inventory node. */
     private static final InstanceIdentifier<Node> NODE_WC_PATH =
             InstanceIdentifier.create(Nodes.class).child(Node.class);
 
-
     private final DataTreeIdentifier<FlowCapableNode> nodeConfigDataTreePath;
     private final DataTreeIdentifier<Node> nodeOperationalDataTreePath;
 
-    public ForwardingRulesSyncProvider(final BindingAwareBroker broker,
-            final DataBroker dataBroker,
-            final RpcConsumerRegistry rpcRegistry) {
-        this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
-
-        Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
-
-        this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
-                "RPC SalFlowService not found.");
-        this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
-                "RPC SalGroupService not found.");
-        this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
-                "RPC SalMeterService not found.");
-        this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
-                "RPC SalTableService not found.");
-        this.transactionService =
-                Preconditions.checkNotNull(rpcRegistry.getRpcService(FlowCapableTransactionService.class),
-                        "RPC SalTableService not found.");
-
-        nodeConfigDataTreePath =
-                new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, FLOW_CAPABLE_NODE_WC_PATH);
-        nodeOperationalDataTreePath = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
-
-        broker.registerProvider(this);
+    private ListenerRegistration<?> dataTreeConfigChangeListener;
+    private ListenerRegistration<?> dataTreeOperationalChangeListener;
+
+    private final ExecutorService syncThreadPool;
+
+    @Inject
+    @Activate
+    public ForwardingRulesSyncProvider(@Reference final DataBroker dataBroker,
+            @Reference final RpcConsumerRegistry rpcRegistry,
+            @Reference final ClusterSingletonServiceProvider clusterSingletonService) {
+        requireNonNull(rpcRegistry, "RpcConsumerRegistry can not be null!");
+        dataService = requireNonNull(dataBroker, "DataBroker can not be null!");
+        this.clusterSingletonService = requireNonNull(clusterSingletonService,
+                "ClusterSingletonServiceProvider can not be null!");
+        updateTable = requireNonNull(rpcRegistry.getRpc(UpdateTable.class),
+                "RPC UpdateTable not found.");
+        processFlatBatch = requireNonNull(rpcRegistry.getRpc(ProcessFlatBatch.class),
+                "RPC SalFlatBatchService not found.");
+
+        nodeConfigDataTreePath = DataTreeIdentifier.create(LogicalDatastoreType.CONFIGURATION,
+                FLOW_CAPABLE_NODE_WC_PATH);
+        nodeOperationalDataTreePath = DataTreeIdentifier.create(LogicalDatastoreType.OPERATIONAL, NODE_WC_PATH);
+
+        syncThreadPool = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
+            .setNameFormat(FRS_EXECUTOR_PREFIX + "%d")
+            .setDaemon(false)
+            .setUncaughtExceptionHandler((thread, ex) -> LOG.error("Uncaught exception {}", thread, ex))
+            .build());
+
+        final var syncPlanPushStrategy = new SyncPlanPushStrategyFlatBatchImpl(processFlatBatch);
+
+        final var reconciliationRegistry = new ReconciliationRegistry();
+        final var deviceMastershipManager = new DeviceMastershipManager(clusterSingletonService,
+            reconciliationRegistry);
+
+        final var syncReactorImpl = new SyncReactorImpl(syncPlanPushStrategy);
+        final var syncReactorRetry = new SyncReactorRetryDecorator(syncReactorImpl, reconciliationRegistry);
+        final var syncReactorGuard = new SyncReactorGuardDecorator(syncReactorRetry);
+        final var syncReactorFutureZip = new SyncReactorFutureZipDecorator(syncReactorGuard, syncThreadPool);
+
+        final var reactor = new SyncReactorClusterDecorator(syncReactorFutureZip, deviceMastershipManager);
+
+        final var configSnapshot = new FlowCapableNodeSnapshotDao();
+        final var operationalSnapshot = new FlowCapableNodeSnapshotDao();
+        final var configDao = new FlowCapableNodeCachedDao(configSnapshot,
+                new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.CONFIGURATION));
+        final var operationalDao = new FlowCapableNodeCachedDao(operationalSnapshot,
+                new FlowCapableNodeOdlDao(dataService, LogicalDatastoreType.OPERATIONAL));
+
+        final NodeListener<FlowCapableNode> nodeListenerConfig =
+                new SimplifiedConfigListener(reactor, configSnapshot, operationalDao);
+        final NodeListener<Node> nodeListenerOperational = new SimplifiedOperationalListener(reactor,
+                operationalSnapshot, configDao, reconciliationRegistry, deviceMastershipManager);
+
+        dataTreeConfigChangeListener =
+                dataService.registerDataTreeChangeListener(nodeConfigDataTreePath, nodeListenerConfig);
+        dataTreeOperationalChangeListener =
+                dataService.registerDataTreeChangeListener(nodeOperationalDataTreePath, nodeListenerOperational);
+        LOG.info("ForwardingRulesSync started");
     }
 
+    @PreDestroy
+    @Deactivate
     @Override
-    public void onSessionInitiated(final BindingAwareBroker.ProviderContext providerContext) {
-
-        LOG.info("ForwardingRulesSync has started.");
-    }
-
     public void close() {
-
+        if (dataTreeConfigChangeListener != null) {
+            dataTreeConfigChangeListener.close();
+            dataTreeConfigChangeListener = null;
+        }
+
+        if (dataTreeOperationalChangeListener != null) {
+            dataTreeOperationalChangeListener.close();
+            dataTreeOperationalChangeListener = null;
+        }
+
+        syncThreadPool.shutdown();
+        LOG.info("ForwardingRulesSync stopped");
     }
 }
-