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