NormalizedNodeAggregator should also report empty
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / NormalizedNodeAggregator.java
index eb1307897a761c099105ca4465fb5dc6070ff77e..4a17978f1ca6b81ff737eaa838886c82e29caa1e 100644 (file)
@@ -5,85 +5,66 @@
  * 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.controller.cluster.datastore.utils;
 
-import com.google.common.base.Optional;
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.MoreExecutors;
 import java.util.List;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
-import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import java.util.Optional;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-
-public class NormalizedNodeAggregator {
-
-    private static final ExecutorService executorService = MoreExecutors.newDirectExecutorService();
-
+import org.opendaylight.yangtools.yang.data.tree.api.DataTree;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeConfiguration;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.tree.api.DataValidationFailedException;
+import org.opendaylight.yangtools.yang.data.tree.impl.di.InMemoryDataTreeFactory;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+
+public final class NormalizedNodeAggregator {
     private final YangInstanceIdentifier rootIdentifier;
-    private final List<Optional<NormalizedNode<?, ?>>> nodes;
-    private final InMemoryDOMDataStore dataStore;
-
-    NormalizedNodeAggregator(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
-                             SchemaContext schemaContext){
+    private final List<Optional<NormalizedNode>> nodes;
+    private final DataTree dataTree;
 
+    private NormalizedNodeAggregator(final YangInstanceIdentifier rootIdentifier,
+            final List<Optional<NormalizedNode>> nodes, final EffectiveModelContext schemaContext,
+            final LogicalDatastoreType logicalDatastoreType) {
         this.rootIdentifier = rootIdentifier;
         this.nodes = nodes;
-        this.dataStore = new InMemoryDOMDataStore("aggregator", executorService);
-        this.dataStore.onGlobalContextUpdated(schemaContext);
+        dataTree = new InMemoryDataTreeFactory().create(logicalDatastoreType == LogicalDatastoreType.CONFIGURATION
+            ? DataTreeConfiguration.DEFAULT_CONFIGURATION : DataTreeConfiguration.DEFAULT_OPERATIONAL);
+        dataTree.setEffectiveModelContext(schemaContext);
     }
 
     /**
-     * Combine data from all the nodes in the list into a tree with root as rootIdentifier
-     *
-     * @param nodes
-     * @param schemaContext
-     * @return
-     * @throws ExecutionException
-     * @throws InterruptedException
+     * Combine data from all the nodes in the list into a tree with root as rootIdentifier.
      */
-    public static Optional<NormalizedNode<?,?>> aggregate(YangInstanceIdentifier rootIdentifier,
-                                                          List<Optional<NormalizedNode<?, ?>>> nodes,
-                                                          SchemaContext schemaContext)
-            throws ExecutionException, InterruptedException {
-        return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext).aggregate();
+    public static Optional<NormalizedNode> aggregate(final YangInstanceIdentifier rootIdentifier,
+            final List<Optional<NormalizedNode>> nodes, final EffectiveModelContext schemaContext,
+            final LogicalDatastoreType logicalDatastoreType) throws DataValidationFailedException {
+        return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext, logicalDatastoreType).aggregate();
     }
 
-    private Optional<NormalizedNode<?,?>> aggregate() throws ExecutionException, InterruptedException {
-        return combine().getRootNode();
-    }
+    private Optional<NormalizedNode> aggregate() throws DataValidationFailedException {
+        final DataTreeModification mod = dataTree.takeSnapshot().newModification();
+        boolean nodePresent = false;
 
-    private NormalizedNodeAggregator combine() throws InterruptedException, ExecutionException {
-        DOMStoreWriteTransaction domStoreWriteTransaction = dataStore.newWriteOnlyTransaction();
-
-        for(Optional<NormalizedNode<?,?>> node : nodes) {
-            if(node.isPresent()) {
-                domStoreWriteTransaction.merge(rootIdentifier, node.get());
+        for (final Optional<NormalizedNode> node : nodes) {
+            if (node.isPresent()) {
+                mod.merge(rootIdentifier, node.orElseThrow());
+                nodePresent = true;
             }
         }
-        DOMStoreThreePhaseCommitCohort ready = domStoreWriteTransaction.ready();
-        ready.canCommit().get();
-        ready.preCommit().get();
-        ready.commit().get();
 
-        return this;
-    }
+        if (!nodePresent) {
+            return Optional.empty();
+        }
 
-    private Optional<NormalizedNode<?, ?>> getRootNode() throws InterruptedException, ExecutionException {
-        DOMStoreReadTransaction readTransaction = dataStore.newReadOnlyTransaction();
 
-        CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
-                readTransaction.read(rootIdentifier);
+        mod.ready();
+        dataTree.validate(mod);
+        final DataTreeCandidate candidate = dataTree.prepare(mod);
+        dataTree.commit(candidate);
 
-        return read.get();
+        return dataTree.takeSnapshot().readNode(rootIdentifier);
     }
-
-
 }