Merge "BUG-2953: do not use a complete InMemoryDataStore"
authorTony Tkacik <ttkacik@cisco.com>
Wed, 15 Apr 2015 14:49:44 +0000 (14:49 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Wed, 15 Apr 2015 14:49:44 +0000 (14:49 +0000)
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/TransactionProxy.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/NormalizedNodeAggregator.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/utils/NormalizedNodeAggregatorTest.java

index a0987cd..71799c9 100644 (file)
@@ -40,6 +40,7 @@ import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransactio
 import org.opendaylight.yangtools.util.concurrent.MappingCheckedFuture;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -219,7 +220,7 @@ public class TransactionProxy extends AbstractDOMStoreTransaction<TransactionIde
                 try {
                     proxyFuture.set(NormalizedNodeAggregator.aggregate(YangInstanceIdentifier.builder().build(),
                             future.get(), actorContext.getSchemaContext()));
-                } catch (InterruptedException | ExecutionException e) {
+                } catch (DataValidationFailedException | InterruptedException | ExecutionException e) {
                     proxyFuture.setException(e);
                 }
             }
index eb13078..a406b9a 100644 (file)
@@ -5,39 +5,30 @@
  * 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 org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
+import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class NormalizedNodeAggregator {
-
-    private static final ExecutorService executorService = MoreExecutors.newDirectExecutorService();
-
     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 DataTree dataTree;
 
+    private NormalizedNodeAggregator(YangInstanceIdentifier rootIdentifier, List<Optional<NormalizedNode<?, ?>>> nodes,
+                             SchemaContext schemaContext) {
         this.rootIdentifier = rootIdentifier;
         this.nodes = nodes;
-        this.dataStore = new InMemoryDOMDataStore("aggregator", executorService);
-        this.dataStore.onGlobalContextUpdated(schemaContext);
+        this.dataTree = InMemoryDataTreeFactory.getInstance().create();
+        this.dataTree.setSchemaContext(schemaContext);
     }
 
     /**
@@ -46,44 +37,35 @@ public class NormalizedNodeAggregator {
      * @param nodes
      * @param schemaContext
      * @return
-     * @throws ExecutionException
-     * @throws InterruptedException
+     * @throws DataValidationFailedException
      */
     public static Optional<NormalizedNode<?,?>> aggregate(YangInstanceIdentifier rootIdentifier,
                                                           List<Optional<NormalizedNode<?, ?>>> nodes,
-                                                          SchemaContext schemaContext)
-            throws ExecutionException, InterruptedException {
+                                                          SchemaContext schemaContext) throws DataValidationFailedException {
         return new NormalizedNodeAggregator(rootIdentifier, nodes, schemaContext).aggregate();
     }
 
-    private Optional<NormalizedNode<?,?>> aggregate() throws ExecutionException, InterruptedException {
+    private Optional<NormalizedNode<?,?>> aggregate() throws DataValidationFailedException {
         return combine().getRootNode();
     }
 
-    private NormalizedNodeAggregator combine() throws InterruptedException, ExecutionException {
-        DOMStoreWriteTransaction domStoreWriteTransaction = dataStore.newWriteOnlyTransaction();
+    private NormalizedNodeAggregator combine() throws DataValidationFailedException {
+        DataTreeModification mod = dataTree.takeSnapshot().newModification();
 
-        for(Optional<NormalizedNode<?,?>> node : nodes) {
-            if(node.isPresent()) {
-                domStoreWriteTransaction.merge(rootIdentifier, node.get());
+        for (Optional<NormalizedNode<?,?>> node : nodes) {
+            if (node.isPresent()) {
+                mod.merge(rootIdentifier, node.get());
             }
         }
-        DOMStoreThreePhaseCommitCohort ready = domStoreWriteTransaction.ready();
-        ready.canCommit().get();
-        ready.preCommit().get();
-        ready.commit().get();
+
+        dataTree.validate(mod);
+        final DataTreeCandidate candidate = dataTree.prepare(mod);
+        dataTree.commit(candidate);
 
         return this;
     }
 
-    private Optional<NormalizedNode<?, ?>> getRootNode() throws InterruptedException, ExecutionException {
-        DOMStoreReadTransaction readTransaction = dataStore.newReadOnlyTransaction();
-
-        CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
-                readTransaction.read(rootIdentifier);
-
-        return read.get();
+    private Optional<NormalizedNode<?, ?>> getRootNode() {
+        return dataTree.takeSnapshot().readNode(rootIdentifier);
     }
-
-
 }
index 40d3704..8c86310 100644 (file)
@@ -29,13 +29,14 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 
 public class NormalizedNodeAggregatorTest {
 
     @Test
-    public void testAggregate() throws InterruptedException, ExecutionException, ReadFailedException {
+    public void testAggregate() throws InterruptedException, ExecutionException, ReadFailedException, DataValidationFailedException {
         SchemaContext schemaContext = SchemaContextHelper.full();
         NormalizedNode<?, ?> expectedNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
         NormalizedNode<?, ?> expectedNode2 = ImmutableNodes.containerNode(CarsModel.CARS_QNAME);

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.