Hide CodecContextSupplier
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecContext.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. 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 com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.base.Throwables;
14 import com.google.common.base.VerifyException;
15 import com.google.common.collect.ImmutableCollection;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableSet;
18 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.lang.invoke.MethodHandle;
20 import java.lang.invoke.MethodHandles;
21 import java.lang.invoke.MethodType;
22 import java.lang.invoke.VarHandle;
23 import java.lang.reflect.Method;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
30 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
31 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
32 import org.opendaylight.mdsal.binding.model.api.Type;
33 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
34 import org.opendaylight.mdsal.binding.runtime.api.AugmentableRuntimeType;
35 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
36 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
37 import org.opendaylight.yangtools.yang.binding.Augmentable;
38 import org.opendaylight.yangtools.yang.binding.Augmentation;
39 import org.opendaylight.yangtools.yang.binding.BindingObject;
40 import org.opendaylight.yangtools.yang.binding.DataObject;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
45 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
46 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
47 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
48 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
49 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52
53 /**
54  * This class is an implementation detail. It is public only due to technical reasons and may change at any time.
55  */
56 @Beta
57 public abstract sealed class DataObjectCodecContext<D extends DataObject, T extends CompositeRuntimeType>
58         extends AbstractDataObjectCodecContext<D, T> implements BindingDataObjectCodecTreeNode<D>
59         permits CaseCodecContext, ContainerLikeCodecContext, ListCodecContext, NotificationCodecContext {
60     private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
61
62     private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class,
63         AbstractDataObjectCodecContext.class, DataContainerNode.class);
64     private static final MethodType DATAOBJECT_TYPE = MethodType.methodType(DataObject.class,
65         DataObjectCodecContext.class, DataContainerNode.class);
66     private static final VarHandle MISMATCHED_AUGMENTED;
67
68     static {
69         try {
70             MISMATCHED_AUGMENTED = MethodHandles.lookup().findVarHandle(DataObjectCodecContext.class,
71                 "mismatchedAugmented", ImmutableMap.class);
72         } catch (NoSuchFieldException | IllegalAccessException e) {
73             throw new ExceptionInInitializerError(e);
74         }
75     }
76
77     private final ImmutableMap<Class<?>, AugmentationCodecPrototype> augmentToPrototype;
78     private final ImmutableMap<NodeIdentifier, Class<?>> yangToAugmentClass;
79     private final @NonNull Class<? extends CodecDataObject<?>> generatedClass;
80     private final MethodHandle proxyConstructor;
81
82     // Note this the content of this field depends only of invariants expressed as this class's fields or
83     // BindingRuntimeContext. It is only accessed via MISMATCHED_AUGMENTED above.
84     @SuppressWarnings("unused")
85     @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
86     private volatile ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> mismatchedAugmented = ImmutableMap.of();
87
88     DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype) {
89         this(prototype, CodecItemFactory.of());
90     }
91
92     DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype, final CodecItemFactory itemFactory) {
93         this(prototype, new DataContainerAnalysis<>(prototype, itemFactory), null);
94     }
95
96     DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype, final Method keyMethod) {
97         this(prototype, new DataContainerAnalysis<>(prototype, CodecItemFactory.of()), keyMethod);
98     }
99
100     private DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype,
101             final DataContainerAnalysis<T> analysis, final Method keyMethod) {
102         super(prototype, analysis);
103
104         final var bindingClass = getBindingClass();
105
106         // Final bits: generate the appropriate class, As a side effect we identify what Augmentations are possible
107         final List<AugmentRuntimeType> possibleAugmentations;
108         if (Augmentable.class.isAssignableFrom(bindingClass)) {
109             // Verify we have the appropriate backing runtimeType
110             final var runtimeType = prototype.getType();
111             if (!(runtimeType instanceof AugmentableRuntimeType augmentableRuntimeType)) {
112                 throw new VerifyException(
113                     "Unexpected type %s backing augmenable %s".formatted(runtimeType, bindingClass));
114             }
115
116             possibleAugmentations = augmentableRuntimeType.augments();
117             generatedClass = CodecDataObjectGenerator.generateAugmentable(factory().getLoader(), bindingClass,
118                 analysis.leafContexts, analysis.daoProperties, keyMethod);
119         } else {
120             possibleAugmentations = List.of();
121             generatedClass = CodecDataObjectGenerator.generate(factory().getLoader(), bindingClass,
122                 analysis.leafContexts, analysis.daoProperties, keyMethod);
123         }
124
125         // All done: acquire the constructor: it is supposed to be public
126         final MethodHandle ctor;
127         try {
128             ctor = MethodHandles.publicLookup().findConstructor(generatedClass, CONSTRUCTOR_TYPE);
129         } catch (NoSuchMethodException | IllegalAccessException e) {
130             throw new LinkageError("Failed to find contructor for class " + generatedClass, e);
131         }
132
133         proxyConstructor = ctor.asType(DATAOBJECT_TYPE);
134
135         // Deal with augmentations, which are not something we analysis provides
136         final var augPathToBinding = new HashMap<NodeIdentifier, Class<?>>();
137         final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype>();
138         for (var augment : possibleAugmentations) {
139             final var augProto = loadAugmentPrototype(augment);
140             if (augProto != null) {
141                 final var augBindingClass = augProto.getBindingClass();
142                 for (var childPath : augProto.getChildArgs()) {
143                     augPathToBinding.putIfAbsent(childPath, augBindingClass);
144                 }
145                 augClassToProto.putIfAbsent(augBindingClass, augProto);
146             }
147         }
148         yangToAugmentClass = ImmutableMap.copyOf(augPathToBinding);
149         augmentToPrototype = ImmutableMap.copyOf(augClassToProto);
150     }
151
152     @Override
153     final CommonDataObjectCodecPrototype<?> pathChildPrototype(final Class<? extends DataObject> argType) {
154         final var child = super.pathChildPrototype(argType);
155         return child != null ? child : augmentToPrototype.get(argType);
156     }
157
158     @Override
159     final CommonDataObjectCodecPrototype<?> streamChildPrototype(final Class<?> childClass) {
160         final var child = super.streamChildPrototype(childClass);
161         if (child == null && Augmentation.class.isAssignableFrom(childClass)) {
162             return getAugmentationProtoByClass(childClass);
163         }
164         return child;
165     }
166
167     @Override
168     final CodecContextSupplier yangChildSupplier(final NodeIdentifier arg) {
169         final var child = super.yangChildSupplier(arg);
170         if (child == null) {
171             final var augClass = yangToAugmentClass.get(arg);
172             if (augClass != null) {
173                 return augmentToPrototype.get(augClass);
174             }
175         }
176         return child;
177     }
178
179     private @Nullable AugmentationCodecPrototype getAugmentationProtoByClass(final @NonNull Class<?> augmClass) {
180         final var childProto = augmentToPrototype.get(augmClass);
181         return childProto != null ? childProto : mismatchedAugmentationByClass(augmClass);
182     }
183
184     private @Nullable AugmentationCodecPrototype mismatchedAugmentationByClass(final @NonNull Class<?> childClass) {
185         /*
186          * It is potentially mismatched valid augmentation - we look up equivalent augmentation using reflection
187          * and walk all stream child and compare augmentations classes if they are equivalent. When we find a match
188          * we'll cache it so we do not need to perform reflection operations again.
189          */
190         final var local = (ImmutableMap<Class<?>, AugmentationCodecPrototype>) MISMATCHED_AUGMENTED.getAcquire(this);
191         final var mismatched = local.get(childClass);
192         return mismatched != null ? mismatched : loadMismatchedAugmentation(local, childClass);
193     }
194
195     private @Nullable AugmentationCodecPrototype loadMismatchedAugmentation(
196             final ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
197             final @NonNull Class<?> childClass) {
198         @SuppressWarnings("rawtypes")
199         final Class<?> augTarget = findAugmentationTarget((Class) childClass);
200         // Do not bother with proposals which are not augmentations of our class, or do not match what the runtime
201         // context would load.
202         if (getBindingClass().equals(augTarget) && belongsToRuntimeContext(childClass)) {
203             for (var realChild : augmentToPrototype.values()) {
204                 if (Augmentation.class.isAssignableFrom(realChild.getBindingClass())
205                         && isSubstitutionFor(childClass, realChild.getBindingClass())) {
206                     return cacheMismatched(oldMismatched, childClass, realChild);
207                 }
208             }
209         }
210         LOG.trace("Failed to resolve {} as a valid augmentation in {}", childClass, this);
211         return null;
212     }
213
214     private @NonNull AugmentationCodecPrototype cacheMismatched(
215             final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
216             final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype prototype) {
217         var expected = oldMismatched;
218         while (true) {
219             final var newMismatched =
220                 ImmutableMap.<Class<?>, CommonDataObjectCodecPrototype<?>>builderWithExpectedSize(expected.size() + 1)
221                     .putAll(expected)
222                     .put(childClass, prototype)
223                     .build();
224
225             final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype>)
226                 MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched);
227             if (witness == expected) {
228                 LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this);
229                 return prototype;
230             }
231
232             expected = witness;
233             final var existing = expected.get(childClass);
234             if (existing != null) {
235                 LOG.trace("Using raced mismatched augmentation {} -> {} in {}", childClass, existing, this);
236                 return existing;
237             }
238         }
239     }
240
241     private boolean belongsToRuntimeContext(final Class<?> cls) {
242         final BindingRuntimeContext ctx = factory().getRuntimeContext();
243         final Class<?> loaded;
244         try {
245             loaded = ctx.loadClass(Type.of(cls));
246         } catch (ClassNotFoundException e) {
247             LOG.debug("Proposed {} cannot be loaded in {}", cls, ctx, e);
248             return false;
249         }
250         return cls.equals(loaded);
251     }
252
253     private @Nullable AugmentationCodecPrototype loadAugmentPrototype(final AugmentRuntimeType augment) {
254         // FIXME: in face of deviations this code should be looking at declared view, i.e. all possibilities at augment
255         //        declaration site
256         final var childPaths = augment.statement()
257             .streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class)
258             .map(stmt -> new NodeIdentifier((QName) stmt.argument()))
259             .collect(ImmutableSet.toImmutableSet());
260
261         if (childPaths.isEmpty()) {
262             return null;
263         }
264
265         final var factory = factory();
266         final GeneratedType javaType = augment.javaType();
267         final Class<? extends Augmentation<?>> augClass;
268         try {
269             augClass = factory.getRuntimeContext().loadClass(javaType);
270         } catch (final ClassNotFoundException e) {
271             throw new IllegalStateException(
272                 "RuntimeContext references type " + javaType + " but failed to load its class", e);
273         }
274         return new AugmentationCodecPrototype(augClass, augment, factory, childPaths);
275     }
276
277     @Override
278     @SuppressWarnings("unchecked")
279     Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(final DataContainerNode data) {
280         /**
281          * Due to augmentation fields are at same level as direct children the data of each augmentation needs to be
282          * aggregated into own container node, then only deserialized using associated prototype.
283          */
284         final var builders = new HashMap<Class<?>, DataContainerNodeBuilder>();
285         for (var childValue : data.body()) {
286             final var bindingClass = yangToAugmentClass.get(childValue.name());
287             if (bindingClass != null) {
288                 builders.computeIfAbsent(bindingClass,
289                     key -> Builders.containerBuilder()
290                         .withNodeIdentifier(new NodeIdentifier(data.name().getNodeType())))
291                         .addChild(childValue);
292             }
293         }
294         @SuppressWarnings("rawtypes")
295         final var map = new HashMap();
296         for (final var entry : builders.entrySet()) {
297             final var bindingClass = entry.getKey();
298             final var codecProto = augmentToPrototype.get(bindingClass);
299             if (codecProto != null) {
300                 final var bindingObj = codecProto.getCodecContext().deserializeObject(entry.getValue().build());
301                 if (bindingObj != null) {
302                     map.put(bindingClass, bindingObj);
303                 }
304             }
305         }
306         return map;
307     }
308
309     @Override
310     public InstanceIdentifier.PathArgument deserializePathArgument(final PathArgument arg) {
311         checkArgument(getDomPathArgument().equals(arg));
312         return bindingArg();
313     }
314
315     @Override
316     public PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
317         checkArgument(bindingArg().equals(arg));
318         return getDomPathArgument();
319     }
320
321     @Override
322     public NormalizedNode serialize(final D data) {
323         return serializeImpl(data);
324     }
325
326     @Override
327     public final BindingNormalizedNodeCachingCodec<D> createCachingCodec(
328             final ImmutableCollection<Class<? extends BindingObject>> cacheSpecifier) {
329         return createCachingCodec(this, cacheSpecifier);
330     }
331
332     final @NonNull Class<? extends CodecDataObject<?>> generatedClass() {
333         return generatedClass;
334     }
335
336     @SuppressWarnings("checkstyle:illegalCatch")
337     final @NonNull D createBindingProxy(final DataContainerNode node) {
338         try {
339             return (D) proxyConstructor.invokeExact(this, node);
340         } catch (final Throwable e) {
341             Throwables.throwIfUnchecked(e);
342             throw new IllegalStateException(e);
343         }
344     }
345 }