From 1489f5570dde3c047e3542e6d70b1ecb4aac5c32 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 28 Jul 2018 10:26:56 +0200 Subject: [PATCH] Update CursorAwareDataTree{Modification,Snapshot} API Create an explicit openCursor() method and return Optional instead of a Nullable. Change-Id: I6a9f11726f85acb86e6aaf30b7ede12b8420da37 Signed-off-by: Robert Varga (cherry picked from commit 7aa7e29cda8726a9f05ee30a38be17a120e77644) --- .../tree/CursorAwareDataTreeModification.java | 29 ++++++++++++++++++- .../tree/CursorAwareDataTreeSnapshot.java | 25 ++++++++++++++++ .../api/schema/tree/DataTreeCandidates.java | 4 +-- .../schema/tree/DataTreeCandidatesTest.java | 4 +-- .../schema/tree/DataTreeCandidatesTest.java | 3 +- 5 files changed, 58 insertions(+), 7 deletions(-) diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeModification.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeModification.java index 9154d0a273..e03056ea98 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeModification.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeModification.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.data.api.schema.tree; import com.google.common.annotations.Beta; +import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -21,12 +22,38 @@ public interface CursorAwareDataTreeModification extends DataTreeModification, C * Create a new {@link DataTreeModificationCursor} at specified path. May fail * if specified path does not exist. It is a programming error to use normal * - * * @param path Path at which the cursor is to be anchored * @return A new cursor, or null if the path does not exist. * @throws IllegalStateException if there is another cursor currently open, * or the modification is already {@link #ready()}. + * @deprecated Use {@link #openCursor(YangInstanceIdentifier)} instead. */ + @Deprecated @Override @Nullable DataTreeModificationCursor createCursor(@Nonnull YangInstanceIdentifier path); + + /** + * Create a new {@link DataTreeModificationCursor} at specified path. May fail + * if specified path does not exist. + * + * @param path Path at which the cursor is to be anchored + * @return A new cursor, or empty if the path does not exist. + * @throws IllegalStateException if there is another cursor currently open, + * or the modification is already {@link #ready()}. + */ + @Override + default Optional openCursor(@Nonnull final YangInstanceIdentifier path) { + return Optional.ofNullable(createCursor(path)); + } + + /** + * Create a new {@link DataTreeModificationCursor} at the root of the modification. + * + * @return A new cursor + * @throws IllegalStateException if there is another cursor currently open. + */ + @Override + default DataTreeModificationCursor openCursor() { + return openCursor(YangInstanceIdentifier.EMPTY).get(); + } } diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeSnapshot.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeSnapshot.java index c5be257be3..de49f2a2b0 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeSnapshot.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/CursorAwareDataTreeSnapshot.java @@ -8,6 +8,7 @@ package org.opendaylight.yangtools.yang.data.api.schema.tree; import com.google.common.annotations.Beta; +import java.util.Optional; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; @@ -24,9 +25,33 @@ public interface CursorAwareDataTreeSnapshot extends DataTreeSnapshot { * @param path Path at which the cursor is to be anchored * @return A new cursor, or null if the path does not exist. * @throws IllegalStateException if there is another cursor currently open. + * @deprecated Use {@link #openCursor(YangInstanceIdentifier)} instead. */ + @Deprecated @Nullable DataTreeSnapshotCursor createCursor(@Nonnull YangInstanceIdentifier path); + /** + * Create a new {@link DataTreeSnapshotCursor} at specified path. May fail + * if specified path does not exist. + * + * @param path Path at which the cursor is to be anchored + * @return A new cursor, or empty if the path does not exist. + * @throws IllegalStateException if there is another cursor currently open. + */ + default Optional openCursor(@Nonnull final YangInstanceIdentifier path) { + return Optional.ofNullable(createCursor(path)); + } + + /** + * Create a new {@link DataTreeSnapshotCursor} at the root of the modification. + * + * @return A new cursor + * @throws IllegalStateException if there is another cursor currently open. + */ + default DataTreeSnapshotCursor openCursor() { + return openCursor(YangInstanceIdentifier.EMPTY).get(); + } + @Override CursorAwareDataTreeModification newModification(); } diff --git a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidates.java b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidates.java index 840ceab47f..b933ce1e3e 100644 --- a/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidates.java +++ b/yang/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidates.java @@ -82,11 +82,11 @@ public final class DataTreeCandidates { final DataTreeCandidate candidate) { final YangInstanceIdentifier candidatePath = candidate.getRootPath(); if (candidatePath.isEmpty()) { - try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath)) { + try (DataTreeModificationCursor cursor = modification.openCursor()) { DataTreeCandidateNodes.applyRootToCursor(cursor, candidate.getRootNode()); } } else { - try (DataTreeModificationCursor cursor = modification.createCursor(candidatePath.getParent())) { + try (DataTreeModificationCursor cursor = modification.openCursor(candidatePath.getParent()).get()) { DataTreeCandidateNodes.applyRootedNodeToCursor(cursor, candidatePath, candidate.getRootNode()); } } diff --git a/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidatesTest.java b/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidatesTest.java index 1062a49784..e1874e390e 100644 --- a/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidatesTest.java +++ b/yang/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/schema/tree/DataTreeCandidatesTest.java @@ -77,14 +77,14 @@ public class DataTreeCandidatesTest { final YangInstanceIdentifier mockedRootPath = mock(YangInstanceIdentifier.class); doReturn(mockedRootPath).when(mockedDataTreeCandidate).getRootPath(); final DataTreeModificationCursor mockedCursor = mock(DataTreeModificationCursor.class); - doReturn(mockedCursor).when(mockedModification).createCursor(any(YangInstanceIdentifier.class)); + doReturn(Optional.of(mockedCursor)).when(mockedModification).openCursor(any(YangInstanceIdentifier.class)); final DataTreeCandidateNode mockedDataTreeCandidateNode = mock(DataTreeCandidateNode.class); doReturn(mockedDataTreeCandidateNode).when(mockedDataTreeCandidate).getRootNode(); doReturn(ModificationType.DELETE).when(mockedDataTreeCandidateNode).getModificationType(); DataTreeCandidates.applyToModification(mockedModification, mockedDataTreeCandidate); - verify(mockedModification, times(1)).createCursor(any(YangInstanceIdentifier.class)); + verify(mockedModification, times(1)).openCursor(any(YangInstanceIdentifier.class)); verify(mockedCursor, times(1)).delete(any(PathArgument.class)); doReturn(Boolean.TRUE).when(mockedRootPath).isEmpty(); diff --git a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataTreeCandidatesTest.java b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataTreeCandidatesTest.java index 3de42a5592..20361ddacf 100644 --- a/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataTreeCandidatesTest.java +++ b/yang/yang-data-impl/src/test/java/org/opendaylight/yangtools/yang/data/impl/schema/tree/DataTreeCandidatesTest.java @@ -13,7 +13,6 @@ import static org.junit.Assert.fail; import org.junit.Before; import org.junit.Test; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.LeafNode; @@ -52,7 +51,7 @@ public class DataTreeCandidatesTest { final InMemoryDataTreeModification modification = (InMemoryDataTreeModification) dataTree.takeSnapshot() .newModification(); - final DataTreeModificationCursor cursor = modification.createCursor(YangInstanceIdentifier.EMPTY); + final DataTreeModificationCursor cursor = modification.openCursor(); cursor.write(TestModel.TEST_PATH.getLastPathArgument(), testContainer); modification.ready(); -- 2.36.6