Cleanup warnings
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / main / java / org / opendaylight / controller / cluster / datastore / utils / PruningDataTreeModification.java
index 0e9726373aadcaf71d0cbd2c691f02cccb97d89f..0e97cd958804ce632d1b45af74f7f6b4192f36c7 100644 (file)
@@ -11,11 +11,10 @@ package org.opendaylight.controller.cluster.datastore.utils;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Optional;
 import com.google.common.base.Preconditions;
+import com.google.common.collect.ForwardingObject;
 import java.io.IOException;
-import java.util.ArrayDeque;
-import java.util.Deque;
-import javax.annotation.Nonnull;
 import org.opendaylight.controller.cluster.datastore.node.utils.transformer.NormalizedNodePruner;
+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;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
@@ -30,9 +29,9 @@ import org.slf4j.LoggerFactory;
 
 /**
  * The PruningDataTreeModification first removes all entries from the data which do not belong in the schemaContext
- * before delegating it to the actual DataTreeModification
+ * before delegating it to the actual DataTreeModification.
  */
-public class PruningDataTreeModification implements DataTreeModification {
+public class PruningDataTreeModification extends ForwardingObject implements DataTreeModification {
 
     private static final Logger LOG = LoggerFactory.getLogger(PruningDataTreeModification.class);
     private DataTreeModification delegate;
@@ -40,16 +39,21 @@ public class PruningDataTreeModification implements DataTreeModification {
     private final DataTree dataTree;
 
     public PruningDataTreeModification(DataTreeModification delegate, DataTree dataTree, SchemaContext schemaContext) {
-        this.delegate = delegate;
-        this.dataTree = dataTree;
-        this.schemaContext = schemaContext;
+        this.delegate = Preconditions.checkNotNull(delegate);
+        this.dataTree = Preconditions.checkNotNull(dataTree);
+        this.schemaContext = Preconditions.checkNotNull(schemaContext);
+    }
+
+    @Override
+    public DataTreeModification delegate() {
+        return delegate;
     }
 
     @Override
     public void delete(YangInstanceIdentifier yangInstanceIdentifier) {
         try {
             delegate.delete(yangInstanceIdentifier);
-        } catch(SchemaValidationFailedException e){
+        } catch (SchemaValidationFailedException e) {
             LOG.warn("Node at path : {} does not exist ignoring delete", yangInstanceIdentifier);
         }
     }
@@ -57,12 +61,12 @@ public class PruningDataTreeModification implements DataTreeModification {
     @Override
     public void merge(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
         try {
-            if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
+            if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
                 pruneAndMergeNode(yangInstanceIdentifier, normalizedNode);
             } else {
                 delegate.merge(yangInstanceIdentifier, normalizedNode);
             }
-        } catch (SchemaValidationFailedException e){
+        } catch (SchemaValidationFailedException e) {
             LOG.warn("Node at path {} was pruned during merge due to validation error: {}",
                     yangInstanceIdentifier, e.getMessage());
 
@@ -74,7 +78,7 @@ public class PruningDataTreeModification implements DataTreeModification {
     private void pruneAndMergeNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
 
-        if(pruned != null) {
+        if (pruned != null) {
             delegate.merge(yangInstanceIdentifier, pruned);
         }
     }
@@ -82,12 +86,12 @@ public class PruningDataTreeModification implements DataTreeModification {
     @Override
     public void write(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
         try {
-            if(YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)){
+            if (YangInstanceIdentifier.EMPTY.equals(yangInstanceIdentifier)) {
                 pruneAndWriteNode(yangInstanceIdentifier, normalizedNode);
             } else {
                 delegate.write(yangInstanceIdentifier, normalizedNode);
             }
-        } catch (SchemaValidationFailedException e){
+        } catch (SchemaValidationFailedException e) {
             LOG.warn("Node at path : {} was pruned during write due to validation error: {}",
                     yangInstanceIdentifier, e.getMessage());
 
@@ -98,7 +102,7 @@ public class PruningDataTreeModification implements DataTreeModification {
     private void pruneAndWriteNode(YangInstanceIdentifier yangInstanceIdentifier, NormalizedNode<?, ?> normalizedNode) {
         NormalizedNode<?,?> pruned = pruneNormalizedNode(yangInstanceIdentifier, normalizedNode);
 
-        if(pruned != null) {
+        if (pruned != null) {
             delegate.write(yangInstanceIdentifier, pruned);
         }
     }
@@ -143,12 +147,7 @@ public class PruningDataTreeModification implements DataTreeModification {
         return pruner.normalizedNode();
     }
 
-    public DataTreeModification getResultingModification(){
-        return delegate;
-    }
-
-    private static class PruningDataTreeModificationCursor implements DataTreeModificationCursor {
-        private final Deque<YangInstanceIdentifier> stack = new ArrayDeque<>();
+    private static class PruningDataTreeModificationCursor extends AbstractDataTreeModificationCursor {
         private final DataTreeModification toModification;
         private final PruningDataTreeModification pruningModification;
 
@@ -156,23 +155,22 @@ public class PruningDataTreeModification implements DataTreeModification {
                 PruningDataTreeModification pruningModification) {
             this.toModification = toModification;
             this.pruningModification = pruningModification;
-            stack.push(YangInstanceIdentifier.EMPTY);
         }
 
         @Override
         public void write(PathArgument child, NormalizedNode<?, ?> data) {
-            YangInstanceIdentifier path = stack.peek().node(child);
+            YangInstanceIdentifier path = current().node(child);
             NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
-            if(prunedNode != null) {
+            if (prunedNode != null) {
                 toModification.write(path, prunedNode);
             }
         }
 
         @Override
         public void merge(PathArgument child, NormalizedNode<?, ?> data) {
-            YangInstanceIdentifier path = stack.peek().node(child);
+            YangInstanceIdentifier path = current().node(child);
             NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
-            if(prunedNode != null) {
+            if (prunedNode != null) {
                 toModification.merge(path, prunedNode);
             }
         }
@@ -180,51 +178,10 @@ public class PruningDataTreeModification implements DataTreeModification {
         @Override
         public void delete(PathArgument child) {
             try {
-                toModification.delete(stack.peek().node(child));
-            } catch(SchemaValidationFailedException e) {
+                toModification.delete(current().node(child));
+            } catch (SchemaValidationFailedException e) {
                 // Ignoring since we would've already logged this in the call to the original modification.
             }
         }
-
-        @Override
-        public void enter(@Nonnull final PathArgument child) {
-            stack.push(stack.peek().node(child));
-        }
-
-        @Override
-        public void enter(@Nonnull final PathArgument... path) {
-            for (PathArgument arg : path) {
-                enter(arg);
-            }
-        }
-
-        @Override
-        public void enter(@Nonnull final Iterable<PathArgument> path) {
-            for (PathArgument arg : path) {
-                enter(arg);
-            }
-        }
-
-        @Override
-        public void exit() {
-            stack.pop();
-        }
-
-        @Override
-        public void exit(final int depth) {
-            Preconditions.checkArgument(depth < stack.size(), "Stack holds only %s elements, cannot exit %s levels", stack.size(), depth);
-            for (int i = 0; i < depth; ++i) {
-                stack.pop();
-            }
-        }
-
-        @Override
-        public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
-            throw new UnsupportedOperationException("Not implemented");
-        }
-
-        @Override
-        public void close() {
-        }
     }
 }