Use ReusableNormalizedNodePruner in PruningDataTreeModification 58/81258/5
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Mar 2019 12:01:48 +0000 (13:01 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 28 Mar 2019 15:02:52 +0000 (16:02 +0100)
Rather than instantiating a pruner for each invocation, keep an
instance around for reuse. In order to facilitate free-standing
modifications, we also keep a reference to the DataSchemaContextTree
bound our current SchemaContext.

JIRA: CONTROLLER-1887
Change-Id: I5d310b12feb1ff46dc2af0e67294a925d48a4957
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/ShardDataTree.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/utils/PruningDataTreeModification.java

index 8b82601..c93cb50 100644 (file)
@@ -80,6 +80,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeTip;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.TreeType;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.InMemoryDataTreeFactory;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -148,6 +149,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     private DataTreeTip tip;
 
     private SchemaContext schemaContext;
+    private DataSchemaContextTree dataSchemaContext;
 
     private int currentTransactionBatch;
 
@@ -207,6 +209,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     void updateSchemaContext(final SchemaContext newSchemaContext) {
         dataTree.setSchemaContext(newSchemaContext);
         this.schemaContext = Preconditions.checkNotNull(newSchemaContext);
+        this.dataSchemaContext = DataSchemaContextTree.from(newSchemaContext);
     }
 
     void resetTransactionBatch() {
@@ -293,7 +296,7 @@ public class ShardDataTree extends ShardDataTreeTransactionParent {
     }
 
     private PruningDataTreeModification wrapWithPruning(final DataTreeModification delegate) {
-        return new PruningDataTreeModification(delegate, dataTree, schemaContext);
+        return new PruningDataTreeModification(delegate, dataTree, dataSchemaContext);
     }
 
     private static DataTreeModification unwrap(final DataTreeModification modification) {
index d0699fb..f56d027 100644 (file)
@@ -5,15 +5,15 @@
  * 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 static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
-import com.google.common.base.Preconditions;
 import com.google.common.collect.ForwardingObject;
 import java.io.IOException;
 import java.util.Optional;
-import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
+import org.opendaylight.controller.cluster.datastore.node.utils.transformer.ReusableNormalizedNodePruner;
 import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -23,6 +23,7 @@ import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
 import org.opendaylight.yangtools.yang.data.impl.schema.tree.SchemaValidationFailedException;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextTree;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -32,17 +33,28 @@ import org.slf4j.LoggerFactory;
  * before delegating it to the actual DataTreeModification.
  */
 public class PruningDataTreeModification extends ForwardingObject implements DataTreeModification {
-
     private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
-    private DataTreeModification delegate;
-    private final SchemaContext schemaContext;
+
+    private final ReusableNormalizedNodePruner pruner;
     private final DataTree dataTree;
 
+    private DataTreeModification delegate;
+
+    private PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
+            final ReusableNormalizedNodePruner pruner) {
+        this.delegate = requireNonNull(delegate);
+        this.dataTree = requireNonNull(dataTree);
+        this.pruner = requireNonNull(pruner);
+    }
+
     public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
             final SchemaContext schemaContext) {
-        this.delegate = Preconditions.checkNotNull(delegate);
-        this.dataTree = Preconditions.checkNotNull(dataTree);
-        this.schemaContext = Preconditions.checkNotNull(schemaContext);
+        this(delegate, dataTree, ReusableNormalizedNodePruner.forSchemaContext(schemaContext));
+    }
+
+    public PruningDataTreeModification(final DataTreeModification delegate, final DataTree dataTree,
+            final DataSchemaContextTree dataSchemaContext) {
+        this(delegate, dataTree, ReusableNormalizedNodePruner.forDataSchemaContext(dataSchemaContext));
     }
 
     @Override
@@ -78,7 +90,7 @@ public class PruningDataTreeModification extends ForwardingObject implements Dat
 
     private void pruneAndMergeNode(final YangInstanceIdentifier yangInstanceIdentifier,
             final NormalizedNode<?, ?> normalizedNode) {
-        NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
+        NormalizedNode<?, ?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
 
         if (pruned != null) {
             delegate.merge(yangInstanceIdentifier, pruned);
@@ -135,19 +147,20 @@ public class PruningDataTreeModification extends ForwardingObject implements Dat
 
     @Override
     public DataTreeModification newModification() {
-        return new PruningDataTreeModification(delegate.newModification(), dataTree, schemaContext);
+        return new PruningDataTreeModification(delegate.newModification(), dataTree, pruner.duplicate());
     }
 
     @VisibleForTesting
-    NormalizedNode<?, ?> pruneNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?,?> input) {
-        NormalizedNodePruner pruner = new NormalizedNodePruner(path, schemaContext);
+    NormalizedNode<?, ?> pruneNormalizedNode(final YangInstanceIdentifier path, final NormalizedNode<?, ?> input) {
+        pruner.initializeForPath(path);
         try {
             NormalizedNodeWriter.forStreamWriter(pruner).write(input);
         } catch (IOException ioe) {
             LOG.error("Unexpected IOException when pruning normalizedNode", ioe);
+            return null;
         }
 
-        return pruner.normalizedNode();
+        return pruner.getResult().orElse(null);
     }
 
     private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {

©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.