Use bindingToString() generated for interfaces
[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.Optional;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
18 import org.opendaylight.yangtools.yang.binding.Augmentable;
19 import org.opendaylight.yangtools.yang.binding.Augmentation;
20 import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
23 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
24
25 /**
26  * A base class for {@link DataObject}s which are also {@link Augmentable}, backed by {@link DataObjectCodecContext}.
27  * While this class is public, it not part of API surface and is an implementation detail. The only reason for it being
28  * public is that it needs to be accessible by code generated at runtime.
29  *
30  * @param <T> DataObject type
31  */
32 public abstract class AugmentableCodecDataObject<T extends DataObject & Augmentable<T>>
33         extends CodecDataObject<T> implements Augmentable<T>, AugmentationHolder<T> {
34     private static final VarHandle CACHED_AUGMENTATIONS;
35
36     static {
37         try {
38             CACHED_AUGMENTATIONS = MethodHandles.lookup().findVarHandle(AugmentableCodecDataObject.class,
39                 "cachedAugmentations", ImmutableMap.class);
40         } catch (ReflectiveOperationException e) {
41             throw new ExceptionInInitializerError(e);
42         }
43     }
44
45     // Used via VarHandle
46     @SuppressWarnings("unused")
47     private volatile ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> cachedAugmentations;
48
49     protected AugmentableCodecDataObject(final DataObjectCodecContext<T, ?> context,
50             final NormalizedNodeContainer<?, ?, ?> data) {
51         super(context, data);
52     }
53
54     @SuppressWarnings("unchecked")
55     @Override
56     public final <A extends Augmentation<T>> @Nullable A augmentation(final Class<A> augmentationType) {
57         requireNonNull(augmentationType, "Supplied augmentation must not be null.");
58
59         final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> aug = acquireAugmentations();
60         if (aug != null) {
61             return (A) aug.get(augmentationType);
62         }
63
64         @SuppressWarnings("rawtypes")
65         final Optional<DataContainerCodecContext<?, ?>> optAugCtx = codecContext().possibleStreamChild(
66             (Class) augmentationType);
67         if (optAugCtx.isPresent()) {
68             final DataContainerCodecContext<?, ?> augCtx = optAugCtx.get();
69             // Due to binding specification not representing grouping instantiations we can end up having the same
70             // augmentation applied to a grouping multiple times. While these augmentations have the same shape, they
71             // are still represented by distinct binding classes and therefore we need to make sure the result matches
72             // the augmentation the user is requesting -- otherwise a strict receiver would end up with a cryptic
73             // ClassCastException.
74             if (augmentationType.isAssignableFrom(augCtx.getBindingClass())) {
75                 final Optional<NormalizedNode<?, ?>> augData = codecData().getChild(augCtx.getDomPathArgument());
76                 if (augData.isPresent()) {
77                     return (A) augCtx.deserialize(augData.get());
78                 }
79             }
80         }
81         return null;
82     }
83
84     @Override
85     public final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> augmentations() {
86         final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> local = acquireAugmentations();
87         return local != null ? local : loadAugmentations();
88     }
89
90     @Override
91     final int codecAugmentedHashCode() {
92         return 31 * super.codecAugmentedHashCode() + augmentations().hashCode();
93     }
94
95     @Override
96     final boolean codecAugmentedEquals(final T other) {
97         return super.codecAugmentedEquals(other) && augmentations().equals(BindingReflections.getAugmentations(other));
98     }
99
100     private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> acquireAugmentations() {
101         return (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) CACHED_AUGMENTATIONS.getAcquire(this);
102     }
103
104     @SuppressWarnings("unchecked")
105     private ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> loadAugmentations() {
106         final ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>> ret = ImmutableMap.copyOf(
107             codecContext().getAllAugmentationsFrom(codecData()));
108         final Object witness = CACHED_AUGMENTATIONS.compareAndExchangeRelease(this, null, ret);
109         return witness == null ? ret : (ImmutableMap<Class<? extends Augmentation<T>>, Augmentation<T>>) witness;
110     }
111 }