774573a9f76a25b77eff59e971206e3ae415c341
[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 import static com.google.common.base.Verify.verify;
12 import static com.google.common.base.Verify.verifyNotNull;
13
14 import com.google.common.base.Throwables;
15 import com.google.common.collect.ImmutableList;
16 import com.google.common.collect.ImmutableMap;
17 import com.google.common.collect.ImmutableMap.Builder;
18 import com.google.common.collect.ImmutableSortedMap;
19 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
20 import java.lang.invoke.MethodHandle;
21 import java.lang.invoke.MethodHandles;
22 import java.lang.invoke.MethodType;
23 import java.lang.reflect.InvocationHandler;
24 import java.lang.reflect.Method;
25 import java.lang.reflect.Proxy;
26 import java.util.Collection;
27 import java.util.Comparator;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Optional;
33 import java.util.SortedMap;
34 import java.util.TreeMap;
35 import java.util.concurrent.ConcurrentHashMap;
36 import java.util.concurrent.ConcurrentMap;
37 import java.util.function.Function;
38 import org.eclipse.jdt.annotation.NonNull;
39 import org.eclipse.jdt.annotation.Nullable;
40 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
41 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
42 import org.opendaylight.mdsal.binding.model.api.Type;
43 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
44 import org.opendaylight.yangtools.util.ClassLoaderUtils;
45 import org.opendaylight.yangtools.yang.binding.Augmentable;
46 import org.opendaylight.yangtools.yang.binding.Augmentation;
47 import org.opendaylight.yangtools.yang.binding.AugmentationHolder;
48 import org.opendaylight.yangtools.yang.binding.DataObject;
49 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
54 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
55 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
56 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
57 import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode;
58 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
59 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
60 import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
61 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
62 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
63 import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
64 import org.opendaylight.yangtools.yang.model.util.SchemaNodeUtils;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
67
68 abstract class DataObjectCodecContext<D extends DataObject, T extends DataNodeContainer & WithStatus>
69         extends DataContainerCodecContext<D, T> {
70     private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
71     private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class, InvocationHandler.class);
72     private static final MethodType DATAOBJECT_TYPE = MethodType.methodType(DataObject.class, InvocationHandler.class);
73     private static final Comparator<Method> METHOD_BY_ALPHABET = Comparator.comparing(Method::getName);
74
75     private final ImmutableMap<String, LeafNodeCodecContext<?>> leafChild;
76     private final ImmutableMap<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYang;
77     private final ImmutableSortedMap<Method, NodeContextSupplier> byMethod;
78     private final ImmutableMap<Method, NodeContextSupplier> nonnullMethods;
79     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byStreamClass;
80     private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byBindingArgClass;
81     private final ImmutableMap<AugmentationIdentifier, Type> possibleAugmentations;
82     private final MethodHandle proxyConstructor;
83
84     private final ConcurrentMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangAugmented =
85             new ConcurrentHashMap<>();
86     private final ConcurrentMap<Class<?>, DataContainerCodecPrototype<?>> byStreamAugmented = new ConcurrentHashMap<>();
87
88     private volatile ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> mismatchedAugmented = ImmutableMap.of();
89
90     DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype) {
91         super(prototype);
92
93         final Class<D> bindingClass = getBindingClass();
94         this.leafChild = factory().getLeafNodes(bindingClass, getSchema());
95
96         final Map<Class<?>, Method> clsToMethod = BindingReflections.getChildrenClassToMethod(bindingClass);
97
98         final Map<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYangBuilder = new HashMap<>();
99         final SortedMap<Method, NodeContextSupplier> byMethodBuilder = new TreeMap<>(METHOD_BY_ALPHABET);
100         final Map<Class<?>, DataContainerCodecPrototype<?>> byStreamClassBuilder = new HashMap<>();
101         final Map<Class<?>, DataContainerCodecPrototype<?>> byBindingArgClassBuilder = new HashMap<>();
102
103         // Adds leaves to mapping
104         for (final LeafNodeCodecContext<?> leaf : leafChild.values()) {
105             byMethodBuilder.put(leaf.getGetter(), leaf);
106             byYangBuilder.put(leaf.getDomPathArgument(), leaf);
107         }
108
109         for (final Entry<Class<?>, Method> childDataObj : clsToMethod.entrySet()) {
110             final Method method = childDataObj.getValue();
111             verify(!method.isDefault(), "Unexpected default method %s in %s", method, bindingClass);
112             final DataContainerCodecPrototype<?> childProto = loadChildPrototype(childDataObj.getKey());
113             byMethodBuilder.put(method, childProto);
114             byStreamClassBuilder.put(childProto.getBindingClass(), childProto);
115             byYangBuilder.put(childProto.getYangArg(), childProto);
116             if (childProto.isChoice()) {
117                 final ChoiceNodeCodecContext<?> choice = (ChoiceNodeCodecContext<?>) childProto.get();
118                 for (final Class<?> cazeChild : choice.getCaseChildrenClasses()) {
119                     byBindingArgClassBuilder.put(cazeChild, childProto);
120                 }
121             }
122         }
123         this.byMethod = ImmutableSortedMap.copyOfSorted(byMethodBuilder);
124         this.byYang = ImmutableMap.copyOf(byYangBuilder);
125         this.byStreamClass = ImmutableMap.copyOf(byStreamClassBuilder);
126         byBindingArgClassBuilder.putAll(byStreamClass);
127         this.byBindingArgClass = ImmutableMap.copyOf(byBindingArgClassBuilder);
128
129         final Map<Class<?>, Method> clsToNonnull = BindingReflections.getChildrenClassToNonnullMethod(bindingClass);
130         final Map<Method, NodeContextSupplier> nonnullMethodsBuilder = new HashMap<>();
131         for (final Entry<Class<?>, Method> entry : clsToNonnull.entrySet()) {
132             final Method method = entry.getValue();
133             if (!method.isDefault()) {
134                 LOG.warn("Ignoring non-default method {} in {}", method, bindingClass);
135                 continue;
136             }
137             final DataContainerCodecPrototype<?> supplier = byStreamClass.get(entry.getKey());
138             if (supplier != null) {
139                 nonnullMethodsBuilder.put(method, supplier);
140             } else {
141                 LOG.warn("Failed to look up data handler for method {}", method);
142             }
143         }
144
145         nonnullMethods = ImmutableMap.copyOf(nonnullMethodsBuilder);
146
147         if (Augmentable.class.isAssignableFrom(bindingClass)) {
148             this.possibleAugmentations = factory().getRuntimeContext().getAvailableAugmentationTypes(getSchema());
149         } else {
150             this.possibleAugmentations = ImmutableMap.of();
151         }
152         reloadAllAugmentations();
153
154         final Class<?> proxyClass = Proxy.getProxyClass(bindingClass.getClassLoader(), bindingClass,
155             AugmentationHolder.class);
156         try {
157             proxyConstructor = MethodHandles.publicLookup().findConstructor(proxyClass, CONSTRUCTOR_TYPE)
158                     .asType(DATAOBJECT_TYPE);
159         } catch (NoSuchMethodException | IllegalAccessException e) {
160             throw new IllegalStateException("Failed to find contructor for class " + proxyClass, e);
161         }
162     }
163
164     @SuppressFBWarnings("RV_RETURN_VALUE_OF_PUTIFABSENT_IGNORED")
165     private void reloadAllAugmentations() {
166         for (final Type augment : possibleAugmentations.values()) {
167             final DataContainerCodecPrototype<?> augProto = getAugmentationPrototype(augment);
168             if (augProto != null) {
169                 byYangAugmented.putIfAbsent(augProto.getYangArg(), augProto);
170                 byStreamAugmented.putIfAbsent(augProto.getBindingClass(), augProto);
171             }
172         }
173     }
174
175     @SuppressWarnings("unchecked")
176     @Override
177     public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
178         final DataContainerCodecPrototype<?> childProto = streamChildPrototype(childClass);
179         return (DataContainerCodecContext<C, ?>) childNonNull(childProto, childClass, " Child %s is not valid child.",
180                 childClass).get();
181     }
182
183     private DataContainerCodecPrototype<?> streamChildPrototype(final Class<?> childClass) {
184         final DataContainerCodecPrototype<?> childProto = byStreamClass.get(childClass);
185         if (childProto != null) {
186             return childProto;
187         }
188         if (Augmentation.class.isAssignableFrom(childClass)) {
189             return augmentationByClass(childClass);
190         }
191         return null;
192     }
193
194     @SuppressWarnings("unchecked")
195     @Override
196     public <C extends DataObject> Optional<DataContainerCodecContext<C, ?>> possibleStreamChild(
197             final Class<C> childClass) {
198         final DataContainerCodecPrototype<?> childProto = streamChildPrototype(childClass);
199         if (childProto != null) {
200             return Optional.of((DataContainerCodecContext<C, ?>) childProto.get());
201         }
202         return Optional.empty();
203     }
204
205     @Override
206     public DataContainerCodecContext<?,?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
207             final List<YangInstanceIdentifier.PathArgument> builder) {
208
209         final Class<? extends DataObject> argType = arg.getType();
210         DataContainerCodecPrototype<?> ctxProto = byBindingArgClass.get(argType);
211         if (ctxProto == null && Augmentation.class.isAssignableFrom(argType)) {
212             ctxProto = augmentationByClass(argType);
213         }
214         final DataContainerCodecContext<?, ?> context =
215                 childNonNull(ctxProto, argType, "Class %s is not valid child of %s", argType, getBindingClass()).get();
216         if (context instanceof ChoiceNodeCodecContext) {
217             final ChoiceNodeCodecContext<?> choice = (ChoiceNodeCodecContext<?>) context;
218             choice.addYangPathArgument(arg, builder);
219
220             final Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
221             final Class<? extends DataObject> type = arg.getType();
222             final DataContainerCodecContext<?, ?> caze;
223             if (caseType.isPresent()) {
224                 // Non-ambiguous addressing this should not pose any problems
225                 caze = choice.streamChild(caseType.get());
226             } else {
227                 caze = choice.getCaseByChildClass(type);
228             }
229
230             caze.addYangPathArgument(arg, builder);
231             return caze.bindingPathArgumentChild(arg, builder);
232         }
233         context.addYangPathArgument(arg, builder);
234         return context;
235     }
236
237     @SuppressWarnings("unchecked")
238     @Override
239     public NodeCodecContext<D> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
240         final NodeContextSupplier childSupplier;
241         if (arg instanceof NodeIdentifierWithPredicates) {
242             childSupplier = byYang.get(new NodeIdentifier(arg.getNodeType()));
243         } else if (arg instanceof AugmentationIdentifier) {
244             childSupplier = yangAugmentationChild((AugmentationIdentifier) arg);
245         } else {
246             childSupplier = byYang.get(arg);
247         }
248
249         return (NodeCodecContext<D>) childNonNull(childSupplier, arg,
250             "Argument %s is not valid child of %s", arg, getSchema()).get();
251     }
252
253     protected final LeafNodeCodecContext<?> getLeafChild(final String name) {
254         final LeafNodeCodecContext<?> value = leafChild.get(name);
255         return IncorrectNestingException.checkNonNull(value, "Leaf %s is not valid for %s", name, getBindingClass());
256     }
257
258     private DataContainerCodecPrototype<?> loadChildPrototype(final Class<?> childClass) {
259         final DataSchemaNode origDef = factory().getRuntimeContext().getSchemaDefinition(childClass);
260         // Direct instantiation or use in same module in which grouping
261         // was defined.
262         DataSchemaNode sameName;
263         try {
264             sameName = getSchema().getDataChildByName(origDef.getQName());
265         } catch (final IllegalArgumentException e) {
266             sameName = null;
267         }
268         final DataSchemaNode childSchema;
269         if (sameName != null) {
270             // Exactly same schema node
271             if (origDef.equals(sameName)) {
272                 childSchema = sameName;
273                 // We check if instantiated node was added via uses
274                 // statement and is instantiation of same grouping
275             } else if (origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(sameName))) {
276                 childSchema = sameName;
277             } else {
278                 // Node has same name, but clearly is different
279                 childSchema = null;
280             }
281         } else {
282             // We are looking for instantiation via uses in other module
283             final QName instantiedName = origDef.getQName().withModule(namespace());
284             final DataSchemaNode potential = getSchema().getDataChildByName(instantiedName);
285             // We check if it is really instantiated from same
286             // definition as class was derived
287             if (potential != null && origDef.equals(SchemaNodeUtils.getRootOriginalIfPossible(potential))) {
288                 childSchema = potential;
289             } else {
290                 childSchema = null;
291             }
292         }
293         final DataSchemaNode nonNullChild =
294                 childNonNull(childSchema, childClass, "Node %s does not have child named %s", getSchema(), childClass);
295         return DataContainerCodecPrototype.from(createBindingArg(childClass, nonNullChild), nonNullChild, factory());
296     }
297
298     @SuppressWarnings("unchecked")
299     Item<?> createBindingArg(final Class<?> childClass, final DataSchemaNode childSchema) {
300         return Item.of((Class<? extends DataObject>) childClass);
301     }
302
303     private DataContainerCodecPrototype<?> yangAugmentationChild(final AugmentationIdentifier arg) {
304         final DataContainerCodecPrototype<?> firstTry = byYangAugmented.get(arg);
305         if (firstTry != null) {
306             return firstTry;
307         }
308         if (possibleAugmentations.containsKey(arg)) {
309             reloadAllAugmentations();
310             return byYangAugmented.get(arg);
311         }
312         return null;
313     }
314
315     private @Nullable DataContainerCodecPrototype<?> augmentationByClass(final @NonNull Class<?> childClass) {
316         DataContainerCodecPrototype<?> lookup = augmentationByClassOrEquivalentClass(childClass);
317         if (lookup != null || !isPotentialAugmentation(childClass)) {
318             return lookup;
319         }
320
321         // Attempt to reload all augmentations using TCCL and lookup again
322         reloadAllAugmentations();
323         lookup = augmentationByClassOrEquivalentClass(childClass);
324         if (lookup != null) {
325             return lookup;
326         }
327
328         // Still no result, this can be caused by TCCL not being set up properly -- try the class's ClassLoader
329         // if it is present;
330         final ClassLoader loader = childClass.getClassLoader();
331         if (loader == null) {
332             return null;
333         }
334
335         LOG.debug("Class {} not loaded via TCCL, attempting to recover", childClass);
336         ClassLoaderUtils.runWithClassLoader(loader, this::reloadAllAugmentations);
337         return augmentationByClassOrEquivalentClass(childClass);
338     }
339
340     private boolean isPotentialAugmentation(final Class<?> childClass) {
341         final JavaTypeName name = JavaTypeName.create(childClass);
342         for (Type type : possibleAugmentations.values()) {
343             if (name.equals(type.getIdentifier())) {
344                 return true;
345             }
346         }
347         return false;
348     }
349
350     private @Nullable DataContainerCodecPrototype<?> augmentationByClassOrEquivalentClass(
351             final @NonNull Class<?> childClass) {
352         final DataContainerCodecPrototype<?> childProto = byStreamAugmented.get(childClass);
353         if (childProto != null) {
354             return childProto;
355         }
356
357         /*
358          * It is potentially mismatched valid augmentation - we look up equivalent augmentation using reflection
359          * and walk all stream child and compare augmentations classes if they are equivalent. When we find a match
360          * we'll cache it so we do not need to perform reflection operations again.
361          */
362         final DataContainerCodecPrototype<?> mismatched = mismatchedAugmented.get(childClass);
363         if (mismatched != null) {
364             return mismatched;
365         }
366
367         @SuppressWarnings("rawtypes")
368         final Class<?> augTarget = BindingReflections.findAugmentationTarget((Class) childClass);
369         if (getBindingClass().equals(augTarget)) {
370             for (final DataContainerCodecPrototype<?> realChild : byStreamAugmented.values()) {
371                 if (Augmentation.class.isAssignableFrom(realChild.getBindingClass())
372                         && BindingReflections.isSubstitutionFor(childClass, realChild.getBindingClass())) {
373                     return cacheMismatched(childClass, realChild);
374                 }
375             }
376         }
377         return null;
378     }
379
380     private synchronized DataContainerCodecPrototype<?> cacheMismatched(final Class<?> childClass,
381             final DataContainerCodecPrototype<?> prototype) {
382         // Original access was unsynchronized, we need to perform additional checking
383         final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> local = mismatchedAugmented;
384         final DataContainerCodecPrototype<?> existing = local.get(childClass);
385         if (existing != null) {
386             return existing;
387         }
388
389         final Builder<Class<?>, DataContainerCodecPrototype<?>> builder = ImmutableMap.builderWithExpectedSize(
390             local.size() + 1);
391         builder.putAll(local);
392         builder.put(childClass, prototype);
393
394         mismatchedAugmented = builder.build();
395         return prototype;
396     }
397
398     private DataContainerCodecPrototype<?> getAugmentationPrototype(final Type value) {
399         final ClassLoadingStrategy loader = factory().getRuntimeContext().getStrategy();
400         @SuppressWarnings("rawtypes")
401         final Class augClass;
402         try {
403             augClass = loader.loadClass(value);
404         } catch (final ClassNotFoundException e) {
405             LOG.debug("Failed to load augmentation prototype for {}. Will be retried when needed.", value, e);
406             return null;
407         }
408
409         @SuppressWarnings("unchecked")
410         final Entry<AugmentationIdentifier, AugmentationSchemaNode> augSchema = factory().getRuntimeContext()
411                 .getResolvedAugmentationSchema(getSchema(), augClass);
412         return DataContainerCodecPrototype.from(augClass, augSchema.getKey(), augSchema.getValue(), factory());
413     }
414
415     Object getBindingChildValue(final Method method, final NormalizedNodeContainer<?, ?, ?> domData) {
416         return method.isDefault() ? getBindingChildValue(nonnullMethods, method, domData, dummy -> ImmutableList.of())
417                 : getBindingChildValue(byMethod, method, domData, NodeCodecContext::defaultObject);
418     }
419
420     @SuppressWarnings("rawtypes")
421     private static Object getBindingChildValue(final ImmutableMap<Method, NodeContextSupplier> map, final Method method,
422             final NormalizedNodeContainer domData, final Function<NodeCodecContext<?>, Object> getDefaultObject) {
423         final NodeCodecContext<?> childContext = verifyNotNull(map.get(method),
424             "Cannot find data handler for method %s", method).get();
425
426         @SuppressWarnings("unchecked")
427         final Optional<NormalizedNode<?, ?>> domChild = domData.getChild(childContext.getDomPathArgument());
428
429         // We do not want to use Optional.map() here because we do not want to invoke defaultObject() when we have
430         // normal value because defaultObject() may end up throwing an exception intentionally.
431         return domChild.isPresent() ? childContext.deserializeObject(domChild.get())
432                 : getDefaultObject.apply(childContext);
433     }
434
435     @SuppressWarnings("checkstyle:illegalCatch")
436     protected final D createBindingProxy(final NormalizedNodeContainer<?, ?, ?> node) {
437         try {
438             return (D) proxyConstructor.invokeExact((InvocationHandler)new LazyDataObject<>(this, node));
439         } catch (final Throwable e) {
440             Throwables.throwIfUnchecked(e);
441             throw new RuntimeException(e);
442         }
443     }
444
445     @SuppressWarnings("unchecked")
446     Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
447             final NormalizedNodeContainer<?, PathArgument, NormalizedNode<?, ?>> data) {
448
449         @SuppressWarnings("rawtypes")
450         final Map map = new HashMap<>();
451
452         for (final NormalizedNode<?, ?> childValue : data.getValue()) {
453             if (childValue instanceof AugmentationNode) {
454                 final AugmentationNode augDomNode = (AugmentationNode) childValue;
455                 final DataContainerCodecPrototype<?> codecProto = yangAugmentationChild(augDomNode.getIdentifier());
456                 if (codecProto != null) {
457                     final DataContainerCodecContext<?, ?> codec = codecProto.get();
458                     map.put(codec.getBindingClass(), codec.deserializeObject(augDomNode));
459                 }
460             }
461         }
462         for (final DataContainerCodecPrototype<?> value : byStreamAugmented.values()) {
463             final Optional<NormalizedNode<?, ?>> augData = data.getChild(value.getYangArg());
464             if (augData.isPresent()) {
465                 map.put(value.getBindingClass(), value.get().deserializeObject(augData.get()));
466             }
467         }
468         return map;
469     }
470
471     Collection<Method> getHashCodeAndEqualsMethods() {
472         return byMethod.keySet();
473     }
474
475     @Override
476     public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
477         checkArgument(getDomPathArgument().equals(arg));
478         return bindingArg();
479     }
480
481     @Override
482     public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
483         checkArgument(bindingArg().equals(arg));
484         return getDomPathArgument();
485     }
486 }