* 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;
+package org.opendaylight.controller.cluster.datastore.util;
+import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
-import java.util.ArrayDeque;
-import java.util.Deque;
+import com.google.common.base.Verify;
import javax.annotation.Nonnull;
+import javax.annotation.concurrent.NotThreadSafe;
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;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModificationCursor;
/**
- * Base class for a DataTreeModificationCursor.
+ * Abstract {@link DataTreeModificationCursor} which tracks the current path. Subclasses can get the current path
+ * via {@link #current()}.
*
* @author Thomas Pantelis
*/
+@Beta
+@NotThreadSafe
public abstract class AbstractDataTreeModificationCursor implements DataTreeModificationCursor {
- private final Deque<YangInstanceIdentifier> stack = new ArrayDeque<>();
+ private YangInstanceIdentifier current = YangInstanceIdentifier.EMPTY;
- protected AbstractDataTreeModificationCursor() {
- stack.push(YangInstanceIdentifier.EMPTY);
- }
-
- protected YangInstanceIdentifier next(@Nonnull final PathArgument child) {
- return stack.peek().node(child);
+ protected final YangInstanceIdentifier current() {
+ return current;
}
@Override
- public void enter(@Nonnull final PathArgument child) {
- stack.push(stack.peek().node(child));
+ public final void enter(@Nonnull final PathArgument child) {
+ current = current.node(child);
}
@Override
- public void enter(@Nonnull final PathArgument... path) {
+ public final void enter(@Nonnull final PathArgument... path) {
for (PathArgument arg : path) {
enter(arg);
}
}
@Override
- public void enter(@Nonnull final Iterable<PathArgument> path) {
+ public final void enter(@Nonnull final Iterable<PathArgument> path) {
for (PathArgument arg : path) {
enter(arg);
}
}
@Override
- public void exit() {
- stack.pop();
+ public final void exit() {
+ Preconditions.checkState(!current.isEmpty());
+ current = Verify.verifyNotNull(current.getParent());
}
@Override
- public void exit(final int depth) {
- Preconditions.checkArgument(depth < stack.size(), "Stack holds only %s elements, cannot exit %s levels", stack.size(), depth);
+ public final void exit(final int depth) {
+ Preconditions.checkArgument(depth >= 0);
+
+ YangInstanceIdentifier next = current;
for (int i = 0; i < depth; ++i) {
- stack.pop();
+ next = next.getParent();
+ Preconditions.checkState(next != null);
}
+
+ current = next;
}
@Override
- public Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
+ public final Optional<NormalizedNode<?, ?>> readNode(@Nonnull final PathArgument child) {
throw new UnsupportedOperationException("Not implemented");
}
import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
+import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
* @author Thomas Pantelis
*/
public abstract class AbstractBatchedModificationsCursor extends AbstractDataTreeModificationCursor {
+
protected abstract BatchedModifications getModifications();
@Override
- public void delete(final PathArgument child) {
- getModifications().addModification(new DeleteModification(next(child)));
+ public final void delete(final PathArgument child) {
+ getModifications().addModification(new DeleteModification(current().node(child)));
}
@Override
- public void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
- getModifications().addModification(new MergeModification(next(child), data));
+ public final void merge(final PathArgument child, final NormalizedNode<?, ?> data) {
+ getModifications().addModification(new MergeModification(current().node(child), data));
}
@Override
- public void write(final PathArgument child, final NormalizedNode<?, ?> data) {
- getModifications().addModification(new WriteModification(next(child), data));
+ public final void write(final PathArgument child, final NormalizedNode<?, ?> data) {
+ getModifications().addModification(new WriteModification(current().node(child), data));
}
}
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.stream.XMLStreamException;
+import org.opendaylight.controller.cluster.datastore.util.AbstractDataTreeModificationCursor;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
public void delete(PathArgument child) {
try {
output.write("\nDELETE -> ".getBytes());
- output.write(next(child).toString().getBytes());
+ output.write(current().node(child).toString().getBytes());
output.writeByte('\n');
} catch(IOException e) {
Throwables.propagate(e);
output.writeByte('\n');
output.write(name.getBytes());
output.write(" -> ".getBytes());
- output.write(next(child).toString().getBytes());
+ output.write(current().node(child).toString().getBytes());
output.write(": \n".getBytes());
NormalizedNodeXMLOutput.toStream(output, data);
output.writeByte('\n');
import com.google.common.base.Optional;
import java.io.IOException;
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;
@Override
public void write(PathArgument child, NormalizedNode<?, ?> data) {
- YangInstanceIdentifier path = next(child);
+ YangInstanceIdentifier path = current().node(child);
NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
if(prunedNode != null) {
toModification.write(path, prunedNode);
@Override
public void merge(PathArgument child, NormalizedNode<?, ?> data) {
- YangInstanceIdentifier path = next(child);
+ YangInstanceIdentifier path = current().node(child);
NormalizedNode<?, ?> prunedNode = pruningModification.pruneNormalizedNode(path, data);
if(prunedNode != null) {
toModification.merge(path, prunedNode);
@Override
public void delete(PathArgument child) {
try {
- toModification.delete(next(child));
+ toModification.delete(current().node(child));
} catch(SchemaValidationFailedException e) {
// Ignoring since we would've already logged this in the call to the original modification.
}