2 * Copyright (c) 2019 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.codec.impl;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import java.util.Optional;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.yangtools.yang.binding.Augmentable;
18 import org.opendaylight.yangtools.yang.binding.Augmentation;
19 import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
25 * A base class for {@link DataObject}s which are also {@link Augmentable}, backed by {@link DataObjectCodecContext}.
26 * While this class is public, it not part of API surface and is an implementation detail. The only reason for it being
27 * public is that it needs to be accessible by code generated at runtime.
29 * @param <T> DataObject type
31 public abstract class AugmentableCodecDataObject<T extends DataObject & Augmentable<T>>
32 extends CodecDataObject<T> implements Augmentable<T>, AugmentationHolder<T> {
33 private static final VarHandle CACHED_AUGMENTATIONS;
37 CACHED_AUGMENTATIONS = MethodHandles.lookup().findVarHandle(AugmentableCodecDataObject.class,
38 "cachedAugmentations", ImmutableMap.class);
39 } catch (ReflectiveOperationException e) {
40 throw new ExceptionInInitializerError(e);
45 @SuppressWarnings("unused")
46 private volatile ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> cachedAugmentations;
48 protected AugmentableCodecDataObject(final DataObjectCodecContext<T, ?> context,
49 final NormalizedNodeContainer<?, ?, ?> data) {
53 @SuppressWarnings("unchecked")
55 public final <A extends Augmentation<T>> @Nullable A augmentation(final Class<A> augmentationType) {
56 requireNonNull(augmentationType, "Supplied augmentation must not be null.");
58 final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> aug = acquireAugmentations();
60 return (A) aug.get(augmentationType);
63 @SuppressWarnings("rawtypes")
64 final Optional<DataContainerCodecContext<?, ?>> optAugCtx = codecContext().possibleStreamChild(
65 (Class) augmentationType);
66 if (optAugCtx.isPresent()) {
67 final DataContainerCodecContext<?, ?> augCtx = optAugCtx.get();
68 // Due to binding specification not representing grouping instantiations we can end up having the same
69 // augmentation applied to a grouping multiple times. While these augmentations have the same shape, they
70 // are still represented by distinct binding classes and therefore we need to make sure the result matches
71 // the augmentation the user is requesting -- otherwise a strict receiver would end up with a cryptic
72 // ClassCastException.
73 if (augmentationType.isAssignableFrom(augCtx.getBindingClass())) {
74 final Optional<NormalizedNode<?, ?>> augData = codecData().getChild(augCtx.getDomPathArgument());
75 if (augData.isPresent()) {
76 return (A) augCtx.deserialize(augData.get());
84 public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
85 final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> local = acquireAugmentations();
86 return local != null ? local : loadAugmentations();
89 private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> acquireAugmentations() {
90 return (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) CACHED_AUGMENTATIONS.getAcquire(this);
93 @SuppressWarnings("unchecked")
94 private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> loadAugmentations() {
95 final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> ret = ImmutableMap.copyOf(
96 codecContext().getAllAugmentationsFrom(codecData()));
97 final Object witness = CACHED_AUGMENTATIONS.compareAndExchangeRelease(this, null, ret);
98 return witness == null ? ret : (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) witness;