2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.binding.dom.adapter;
10 import static com.google.common.base.Verify.verifyNotNull;
11 import static java.util.Objects.requireNonNull;
12 import static org.opendaylight.yangtools.yang.data.tree.api.ModificationType.UNMODIFIED;
14 import com.google.common.base.MoreObjects;
15 import com.google.common.base.MoreObjects.ToStringHelper;
16 import com.google.common.base.VerifyException;
17 import com.google.common.collect.ArrayListMultimap;
18 import com.google.common.collect.ImmutableList;
19 import java.lang.invoke.MethodHandles;
20 import java.lang.invoke.VarHandle;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24 import java.util.Optional;
25 import java.util.stream.Collectors;
26 import java.util.stream.Stream;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.mdsal.binding.api.DataObjectModification;
30 import org.opendaylight.mdsal.binding.dom.codec.api.BindingAugmentationCodecTreeNode;
31 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
32 import org.opendaylight.mdsal.binding.dom.codec.api.CommonDataObjectCodecTreeNode;
33 import org.opendaylight.yangtools.yang.binding.Augmentation;
34 import org.opendaylight.yangtools.yang.binding.ChildOf;
35 import org.opendaylight.yangtools.yang.binding.ChoiceIn;
36 import org.opendaylight.yangtools.yang.binding.DataObject;
37 import org.opendaylight.yangtools.yang.binding.Identifiable;
38 import org.opendaylight.yangtools.yang.binding.Identifier;
39 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
44 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeCandidateNode;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Lazily translated {@link DataObjectModification} based on {@link DataTreeCandidateNode}.
50 * {@link AbstractDataObjectModification} represents Data tree change event, but whole tree is not translated or
51 * resolved eagerly, but only child nodes which are directly accessed by user of data object modification.
54 * This class is further specialized as {@link LazyAugmentationModification} and {@link LazyDataObjectModification}, as
55 * both use different serialization methods.
57 * @param <T> Type of Binding {@link DataObject}
58 * @param <N> Type of underlying {@link CommonDataObjectCodecTreeNode}
60 abstract sealed class AbstractDataObjectModification<T extends DataObject, N extends CommonDataObjectCodecTreeNode<T>>
61 implements DataObjectModification<T>
62 permits LazyAugmentationModification, LazyDataObjectModification {
63 private static final Logger LOG = LoggerFactory.getLogger(AbstractDataObjectModification.class);
64 private static final @NonNull Object NULL_DATA_OBJECT = new Object();
65 private static final VarHandle MODIFICATION_TYPE;
66 private static final VarHandle MODIFIED_CHILDREN;
67 private static final VarHandle DATA_BEFORE;
68 private static final VarHandle DATA_AFTER;
71 final var lookup = MethodHandles.lookup();
74 MODIFICATION_TYPE = lookup.findVarHandle(AbstractDataObjectModification.class, "modificationType",
75 ModificationType.class);
76 MODIFIED_CHILDREN = lookup.findVarHandle(AbstractDataObjectModification.class, "modifiedChildren",
78 DATA_BEFORE = lookup.findVarHandle(AbstractDataObjectModification.class, "dataBefore", Object.class);
79 DATA_AFTER = lookup.findVarHandle(AbstractDataObjectModification.class, "dataAfter", Object.class);
80 } catch (NoSuchFieldException | IllegalAccessException e) {
81 throw new ExceptionInInitializerError(e);
85 final @NonNull DataTreeCandidateNode domData;
86 final @NonNull PathArgument identifier;
87 final @NonNull N codec;
89 @SuppressWarnings("unused")
90 private volatile ImmutableList<AbstractDataObjectModification<?, ?>> modifiedChildren;
91 @SuppressWarnings("unused")
92 private volatile ModificationType modificationType;
93 @SuppressWarnings("unused")
94 private volatile Object dataBefore;
95 @SuppressWarnings("unused")
96 private volatile Object dataAfter;
98 AbstractDataObjectModification(final DataTreeCandidateNode domData, final N codec, final PathArgument identifier) {
99 this.domData = requireNonNull(domData);
100 this.identifier = requireNonNull(identifier);
101 this.codec = requireNonNull(codec);
104 static @Nullable AbstractDataObjectModification<?, ?> from(final CommonDataObjectCodecTreeNode<?> codec,
105 final @NonNull DataTreeCandidateNode current) {
106 if (codec instanceof BindingDataObjectCodecTreeNode<?> childDataObjectCodec) {
107 return new LazyDataObjectModification<>(childDataObjectCodec, current);
108 } else if (codec instanceof BindingAugmentationCodecTreeNode<?> childAugmentationCodec) {
109 return LazyAugmentationModification.forParent(childAugmentationCodec, current);
111 throw new VerifyException("Unhandled codec " + codec);
116 public final Class<T> getDataType() {
117 return codec.getBindingClass();
121 public final PathArgument getIdentifier() {
126 public final ModificationType getModificationType() {
127 final var local = (ModificationType) MODIFICATION_TYPE.getAcquire(this);
128 return local != null ? local : loadModificationType();
131 private @NonNull ModificationType loadModificationType() {
132 final var domModificationType = domModificationType();
133 final var computed = switch (domModificationType) {
134 case APPEARED, WRITE -> ModificationType.WRITE;
135 case DISAPPEARED, DELETE -> ModificationType.DELETE;
136 case SUBTREE_MODIFIED -> resolveSubtreeModificationType();
138 // TODO: Should we lie about modification type instead of exception?
139 throw new IllegalStateException("Unsupported DOM Modification type " + domModificationType);
142 MODIFICATION_TYPE.setRelease(this, computed);
147 public final T getDataBefore() {
148 final var local = DATA_BEFORE.getAcquire(this);
149 return local != null ? unmask(local) : loadDataBefore();
152 private @Nullable T loadDataBefore() {
153 final var computed = deserialize(domData.getDataBefore());
154 final var witness = DATA_BEFORE.compareAndExchangeRelease(this, null, mask(computed));
155 return witness == null ? computed : unmask(witness);
159 public final T getDataAfter() {
160 final var local = DATA_AFTER.getAcquire(this);
161 return local != null ? unmask(local) : loadDataAfter();
164 private @Nullable T loadDataAfter() {
165 final var computed = deserialize(domData.getDataAfter());
166 final var witness = DATA_AFTER.compareAndExchangeRelease(this, null, mask(computed));
167 return witness == null ? computed : unmask(witness);
170 private static <T extends DataObject> @NonNull Object mask(final @Nullable T obj) {
171 return obj != null ? obj : NULL_DATA_OBJECT;
174 @SuppressWarnings("unchecked")
175 private @Nullable T unmask(final @NonNull Object obj) {
176 return obj == NULL_DATA_OBJECT ? null : (T) verifyNotNull(obj);
179 private @Nullable T deserialize(final Optional<NormalizedNode> normalized) {
180 return normalized.isEmpty() ? null : deserialize(normalized.orElseThrow());
183 abstract @Nullable T deserialize(@NonNull NormalizedNode normalized);
186 public final DataObjectModification<?> getModifiedChild(final PathArgument arg) {
187 final var domArgumentList = new ArrayList<YangInstanceIdentifier.PathArgument>();
188 final var childCodec = codec.bindingPathArgumentChild(arg, domArgumentList);
189 final var toEnter = domArgumentList.iterator();
191 // Careful now: we need to validated the first item against subclass
192 var current = toEnter.hasNext() ? firstModifiedChild(toEnter.next()) : domData;
193 // ... and for everything else we can just go wild
194 while (toEnter.hasNext() && current != null) {
195 current = current.getModifiedChild(toEnter.next()).orElse(null);
198 if (current == null || current.getModificationType() == UNMODIFIED) {
201 return from(childCodec, current);
204 abstract @Nullable DataTreeCandidateNode firstModifiedChild(YangInstanceIdentifier.PathArgument arg);
207 public final ImmutableList<AbstractDataObjectModification<?, ?>> getModifiedChildren() {
208 final var local = (ImmutableList<AbstractDataObjectModification<?, ?>>) MODIFIED_CHILDREN.getAcquire(this);
209 return local != null ? local : loadModifiedChilden();
213 public final <C extends ChildOf<? super T>> List<DataObjectModification<C>> getModifiedChildren(
214 final Class<C> childType) {
215 return streamModifiedChildren(childType).collect(Collectors.toList());
219 public final <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>>
220 List<DataObjectModification<C>> getModifiedChildren(final Class<H> caseType, final Class<C> childType) {
221 return streamModifiedChildren(childType)
222 .filter(child -> caseType.equals(child.identifier.getCaseType().orElse(null)))
223 .collect(Collectors.toList());
226 @SuppressWarnings("unchecked")
227 private @NonNull ImmutableList<AbstractDataObjectModification<?, ?>> loadModifiedChilden() {
228 final var builder = ImmutableList.<AbstractDataObjectModification<?, ?>>builder();
229 populateList(builder, codec, domData, domChildNodes());
230 final var computed = builder.build();
231 // Non-trivial return: use CAS to ensure we reuse concurrent loads
232 final var witness = MODIFIED_CHILDREN.compareAndExchangeRelease(this, null, computed);
233 return witness == null ? computed : (ImmutableList<AbstractDataObjectModification<?, ?>>) witness;
236 @SuppressWarnings("unchecked")
237 private <C extends DataObject> Stream<LazyDataObjectModification<C>> streamModifiedChildren(
238 final Class<C> childType) {
239 return getModifiedChildren().stream()
240 .filter(child -> childType.isAssignableFrom(child.getDataType()))
241 .map(child -> (LazyDataObjectModification<C>) child);
245 @SuppressWarnings("unchecked")
246 public final <C extends Identifiable<K> & ChildOf<? super T>, K extends Identifier<C>> DataObjectModification<C>
247 getModifiedChildListItem(final Class<C> listItem, final K listKey) {
248 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(listItem, listKey));
252 @SuppressWarnings("unchecked")
253 public final <H extends ChoiceIn<? super T> & DataObject, C extends Identifiable<K> & ChildOf<? super H>,
254 K extends Identifier<C>> DataObjectModification<C> getModifiedChildListItem(final Class<H> caseType,
255 final Class<C> listItem, final K listKey) {
256 return (DataObjectModification<C>) getModifiedChild(IdentifiableItem.of(caseType, listItem, listKey));
260 @SuppressWarnings("unchecked")
261 public final <C extends ChildOf<? super T>> DataObjectModification<C> getModifiedChildContainer(
262 final Class<C> child) {
263 return (DataObjectModification<C>) getModifiedChild(Item.of(child));
267 @SuppressWarnings("unchecked")
268 public final <H extends ChoiceIn<? super T> & DataObject, C extends ChildOf<? super H>> DataObjectModification<C>
269 getModifiedChildContainer(final Class<H> caseType, final Class<C> child) {
270 return (DataObjectModification<C>) getModifiedChild(Item.of(caseType, child));
274 @SuppressWarnings("unchecked")
275 public final <C extends Augmentation<T> & DataObject> DataObjectModification<C> getModifiedAugmentation(
276 final Class<C> augmentation) {
277 return (DataObjectModification<C>) getModifiedChild(Item.of(augmentation));
281 public final String toString() {
282 return addToStringAttributes(MoreObjects.toStringHelper(this)).toString();
285 ToStringHelper addToStringAttributes(final ToStringHelper helper) {
286 return helper.add("identifier", identifier).add("domData", domData);
289 abstract @NonNull Collection<DataTreeCandidateNode> domChildNodes();
291 abstract org.opendaylight.yangtools.yang.data.tree.api.@NonNull ModificationType domModificationType();
293 private @NonNull ModificationType resolveSubtreeModificationType() {
294 return switch (codec.getChildAddressabilitySummary()) {
296 // All children are addressable, it is safe to report SUBTREE_MODIFIED
297 ModificationType.SUBTREE_MODIFIED;
298 case UNADDRESSABLE ->
299 // All children are non-addressable, report WRITE
300 ModificationType.WRITE;
302 // This case is not completely trivial, as we may have NOT_ADDRESSABLE nodes underneath us. If that
303 // is the case, we need to turn this modification into a WRITE operation, so that the user is able
304 // to observe those nodes being introduced. This is not efficient, but unfortunately unavoidable,
305 // as we cannot accurately represent such changes.
306 for (var child : domChildNodes()) {
307 if (BindingStructuralType.recursiveFrom(child) == BindingStructuralType.NOT_ADDRESSABLE) {
308 // We have a non-addressable child, turn this modification into a write
309 yield ModificationType.WRITE;
313 // No unaddressable children found, proceed in addressed mode
314 yield ModificationType.SUBTREE_MODIFIED;
319 private static void populateList(final ImmutableList.Builder<AbstractDataObjectModification<?, ?>> result,
320 final CommonDataObjectCodecTreeNode<?> parentCodec, final DataTreeCandidateNode parent,
321 final Collection<DataTreeCandidateNode> children) {
322 final var augmentChildren =
323 ArrayListMultimap.<BindingAugmentationCodecTreeNode<?>, DataTreeCandidateNode>create();
325 for (var domChildNode : parent.getChildNodes()) {
326 if (domChildNode.getModificationType() != UNMODIFIED) {
327 final var type = BindingStructuralType.from(domChildNode);
328 if (type != BindingStructuralType.NOT_ADDRESSABLE) {
330 * Even if type is UNKNOWN, from perspective of BindingStructuralType we try to load codec for it.
331 * We will use that type to further specify debug log.
334 final var childCodec = parentCodec.yangPathArgumentChild(domChildNode.getIdentifier());
335 if (childCodec instanceof BindingDataObjectCodecTreeNode<?> childDataObjectCodec) {
336 populateList(result, type, childDataObjectCodec, domChildNode);
337 } else if (childCodec instanceof BindingAugmentationCodecTreeNode<?> childAugmentationCodec) {
338 // Defer creation once we have collected all modified children
339 augmentChildren.put(childAugmentationCodec, domChildNode);
341 throw new VerifyException("Unhandled codec %s for type %s".formatted(childCodec, type));
343 } catch (final IllegalArgumentException e) {
344 if (type == BindingStructuralType.UNKNOWN) {
345 LOG.debug("Unable to deserialize unknown DOM node {}", domChildNode, e);
347 LOG.debug("Binding representation for DOM node {} was not found", domChildNode, e);
354 for (var entry : augmentChildren.asMap().entrySet()) {
355 final var modification = LazyAugmentationModification.forModifications(entry.getKey(), parent,
357 if (modification != null) {
358 result.add(modification);
363 private static void populateList(final ImmutableList.Builder<AbstractDataObjectModification<?, ?>> result,
364 final BindingStructuralType type, final BindingDataObjectCodecTreeNode<?> childCodec,
365 final DataTreeCandidateNode domChildNode) {
368 // We use parent codec intentionally.
369 populateListWithSingleCodec(result, childCodec, domChildNode.getChildNodes());
371 case INVISIBLE_CONTAINER:
372 populateList(result, childCodec, domChildNode, domChildNode.getChildNodes());
375 case VISIBLE_CONTAINER:
376 result.add(new LazyDataObjectModification<>(childCodec, domChildNode));
382 private static void populateListWithSingleCodec(
383 final ImmutableList.Builder<AbstractDataObjectModification<?, ?>> result,
384 final BindingDataObjectCodecTreeNode<?> codec, final Collection<DataTreeCandidateNode> childNodes) {
385 for (var child : childNodes) {
386 if (child.getModificationType() != UNMODIFIED) {
387 result.add(new LazyDataObjectModification<>(codec, child));