16314d13efb573ba4033a9aa21f31adf51b964ec
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / AugmentableCodecDataObject.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.dom.codec.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.collect.ImmutableMap;
13 import java.lang.invoke.MethodHandles;
14 import java.lang.invoke.VarHandle;
15 import java.util.Map;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.eclipse.jdt.annotation.Nullable;
18 import org.opendaylight.yangtools.yang.binding.Augmentable;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
22
23 /**
24  * A base class for {@link DataObject}s which are also {@link Augmentable}, backed by {@link DataObjectCodecContext}.
25  * While this class is public, it not part of API surface and is an implementation detail. The only reason for it being
26  * public is that it needs to be accessible by code generated at runtime.
27  *
28  * @param <T> DataObject type
29  */
30 public abstract class AugmentableCodecDataObject<T extends DataObject & Augmentable<T>>
31         extends CodecDataObject<T> implements Augmentable<T> {
32     private static final VarHandle CACHED_AUGMENTATIONS;
33
34     static {
35         try {
36             CACHED_AUGMENTATIONS = MethodHandles.lookup().findVarHandle(AugmentableCodecDataObject.class,
37                 "cachedAugmentations", ImmutableMap.class);
38         } catch (ReflectiveOperationException e) {
39             throw new ExceptionInInitializerError(e);
40         }
41     }
42
43     // Used via VarHandle
44     @SuppressWarnings("unused")
45     private volatile ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> cachedAugmentations;
46
47     protected AugmentableCodecDataObject(final AbstractDataObjectCodecContext<T, ?> context,
48             final DataContainerNode data) {
49         super(context, data);
50     }
51
52     @SuppressWarnings("unchecked")
53     @Override
54     public final <A extends Augmentation<T>> @Nullable A augmentation(final Class<A> augmentationType) {
55         requireNonNull(augmentationType, "Supplied augmentation must not be null.");
56
57         final var aug = acquireAugmentations();
58         if (aug != null) {
59             return (A) aug.get(augmentationType);
60         }
61
62         @SuppressWarnings("rawtypes")
63         final var augCtx = (AugmentationCodecContext<A>) codecContext().streamChild((Class) augmentationType);
64         // Due to binding specification not representing grouping instantiations we can end up having the same
65         // augmentation applied to a grouping multiple times. While these augmentations have the same shape, they are
66         // still represented by distinct binding classes and therefore we need to make sure the result matches
67         // the augmentation the user is requesting -- otherwise a strict receiver would end up with a cryptic
68         // ClassCastException.
69         if (augCtx != null && augmentationType.isAssignableFrom(augCtx.getBindingClass())) {
70             final var augObj = augCtx.filterFrom(codecData());
71             if (augObj != null) {
72                 return augObj;
73             }
74         }
75         return null;
76     }
77
78     @Override
79     public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
80         final var local = acquireAugmentations();
81         return local != null ? local : loadAugmentations();
82     }
83
84     private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> acquireAugmentations() {
85         return (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) CACHED_AUGMENTATIONS.getAcquire(this);
86     }
87
88     @SuppressWarnings("unchecked")
89     private @NonNull ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> loadAugmentations() {
90         @SuppressWarnings("rawtypes")
91         final Map extracted = codecContext().getAllAugmentationsFrom(codecData());
92         final var ret = ImmutableMap.<Class<? extends Augmentation<T>>, Augmentation<T>>copyOf(extracted);
93         final Object witness = CACHED_AUGMENTATIONS.compareAndExchangeRelease(this, null, ret);
94         return witness == null ? ret : (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) witness;
95     }
96 }