*/
package org.opendaylight.mdsal.binding.dom.adapter;
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
-import java.util.Map.Entry;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.mdsal.binding.api.DataObjectModification;
import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
import org.opendaylight.mdsal.binding.api.DataTreeModification;
import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
import org.opendaylight.mdsal.dom.api.DOMDataTreeCandidate;
-import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
+import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidate;
/**
* Lazily translated {@link DataTreeModification} based on {@link DataTreeCandidate}.
*
- * {@link DataTreeModification} represents Data tree change event,
- * but whole tree is not translated or resolved eagerly, but only child nodes
- * which are directly accessed by user of data object modification.
- *
+ * <p>
+ * {@link DataTreeModification} represents Data tree change event, but whole tree is not translated or resolved eagerly,
+ * but only child nodes which are directly accessed by user of data object modification.
*/
-class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
+final class LazyDataTreeModification<T extends DataObject> implements DataTreeModification<T> {
+ private final @NonNull DataTreeIdentifier<T> path;
+ private final @NonNull DataObjectModification<T> rootNode;
- private final DataTreeIdentifier<T> path;
- private final DataObjectModification<T> rootNode;
+ private LazyDataTreeModification(final DataTreeIdentifier<T> path, final DataObjectModification<T> modification) {
+ this.path = requireNonNull(path);
+ rootNode = requireNonNull(modification);
+ }
- LazyDataTreeModification(DataTreeIdentifier<T> path, final DataObjectModification<T> modification) {
- this.path = Preconditions.checkNotNull(path);
- this.rootNode = Preconditions.checkNotNull(modification);
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ static <T extends DataObject> @Nullable DataTreeModification<T> from(final CurrentAdapterSerializer serializer,
+ final DataTreeCandidate domChange, final LogicalDatastoreType datastoreType,
+ final @Nullable Class<? extends Augmentation<?>> augment) {
+ final var bindingPath = createBindingPath(serializer, domChange.getRootPath(), augment);
+ final var codec = serializer.getSubtreeCodec(bindingPath);
+ final var modification = LazyDataObjectModification.from(codec, domChange.getRootNode());
+ return modification == null ? null
+ : new LazyDataTreeModification(DataTreeIdentifier.of(datastoreType, bindingPath), modification);
}
- @Override
- public DataObjectModification<T> getRootNode() {
- return rootNode;
+ static <T extends DataObject> @Nullable DataTreeModification<T> from(final CurrentAdapterSerializer serializer,
+ final DOMDataTreeCandidate candidate, final @Nullable Class<T> augment) {
+ final var domRootPath = candidate.getRootPath();
+ @SuppressWarnings("unchecked")
+ final var bindingPath = (InstanceIdentifier<T>) createBindingPath(serializer, domRootPath.path(), augment);
+ final var codec = serializer.getSubtreeCodec(bindingPath);
+ @SuppressWarnings("unchecked")
+ final var modification = (DataObjectModification<T>) LazyDataObjectModification.from(codec,
+ candidate.getRootNode());
+ return modification == null ? null
+ : new LazyDataTreeModification<>(DataTreeIdentifier.of(domRootPath.datastore(), bindingPath), modification);
}
- @Override
- public DataTreeIdentifier<T> getRootPath() {
- return path;
+ static <T extends DataObject> @NonNull List<DataTreeModification<T>> from(final CurrentAdapterSerializer codec,
+ final List<DataTreeCandidate> domChanges, final LogicalDatastoreType datastoreType,
+ final @Nullable Class<? extends Augmentation<?>> augment) {
+ final var result = new ArrayList<DataTreeModification<T>>(domChanges.size());
+ for (var domChange : domChanges) {
+ final var bindingChange = LazyDataTreeModification.<T>from(codec, domChange, datastoreType, augment);
+ if (bindingChange != null) {
+ result.add(bindingChange);
+ }
+ }
+ return result;
}
+ // We are given a DOM path, which does not reflect augmentations, as those are not representable in NormalizedNode
+ // world. This method takes care of reconstructing the InstanceIdentifier, appending the missing Augmentation. This
+ // important to get the correct codec into the mix -- otherwise we would be operating on the parent container's
+ // codec and mis-report what is actually going on.
@SuppressWarnings({"unchecked", "rawtypes"})
- static <T extends DataObject> DataTreeModification<T> create(final BindingToNormalizedNodeCodec codec, final DataTreeCandidate domChange,
- final LogicalDatastoreType datastoreType) {
- final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
- codec.getSubtreeCodec(domChange.getRootPath());
- final DataTreeIdentifier<?> path = DataTreeIdentifier.create(datastoreType, codecCtx.getKey());
- final DataObjectModification<?> modification =
- LazyDataObjectModification.create(codecCtx.getValue(), domChange.getRootNode());
- return new LazyDataTreeModification(path, modification);
+ private static @NonNull InstanceIdentifier<?> createBindingPath(final CurrentAdapterSerializer serializer,
+ final YangInstanceIdentifier domPath, final @Nullable Class<?> augment) {
+ final var bindingPath = serializer.coerceInstanceIdentifier(domPath);
+ return augment == null ? bindingPath : bindingPath.augmentation((Class) augment.asSubclass(Augmentation.class));
}
- static <T extends DataObject> Collection<DataTreeModification<T>> from(final BindingToNormalizedNodeCodec codec,
- final Collection<DataTreeCandidate> domChanges, final LogicalDatastoreType datastoreType) {
- final List<DataTreeModification<T>> result = new ArrayList<>(domChanges.size());
- for (final DataTreeCandidate domChange : domChanges) {
- result.add(LazyDataTreeModification.<T>create(codec, domChange, datastoreType));
- }
- return result;
+ @Override
+ public DataObjectModification<T> getRootNode() {
+ return rootNode;
}
- static <T extends DataObject> DataTreeModification<T> create(BindingToNormalizedNodeCodec codec,
- DOMDataTreeCandidate candidate) {
- final Entry<InstanceIdentifier<?>, BindingCodecTreeNode<?>> codecCtx =
- codec.getSubtreeCodec(candidate.getRootPath().getRootIdentifier());
- final DataTreeIdentifier<?> path =
- DataTreeIdentifier.create(candidate.getRootPath().getDatastoreType(), codecCtx.getKey());
- final DataObjectModification<?> modification =
- LazyDataObjectModification.create(codecCtx.getValue(), candidate.getRootNode());
- return new LazyDataTreeModification(path, modification);
+ @Override
+ public DataTreeIdentifier<T> getRootPath() {
+ return path;
}
+ @Override
+ public String toString() {
+ return MoreObjects.toStringHelper(this).add("path", path).add("rootNode", rootNode).toString();
+ }
}