Separate out DataContainerPrototype
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataContainerAnalysis.java
1 /*
2  * Copyright (c) 2023 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 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.collect.ImmutableMap;
15 import java.lang.reflect.Method;
16 import java.lang.reflect.ParameterizedType;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
23 import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
24 import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
25 import org.opendaylight.mdsal.binding.runtime.api.ContainerLikeRuntimeType;
26 import org.opendaylight.mdsal.binding.runtime.api.ContainerRuntimeType;
27 import org.opendaylight.mdsal.binding.runtime.api.ListRuntimeType;
28 import org.opendaylight.yangtools.util.ClassLoaderUtils;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.opendaylight.yangtools.yang.binding.OpaqueObject;
31 import org.opendaylight.yangtools.yang.binding.contract.Naming;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
33 import org.opendaylight.yangtools.yang.model.api.stmt.PresenceEffectiveStatement;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * Analysis of a {@link DataContainer} specialization class. This includes things needed for
39  * {@link DataContainerCodecContext}'s methods as well as the appropriate run-time generated class.
40  */
41 final class DataContainerAnalysis<R extends CompositeRuntimeType> {
42     private static final Logger LOG = LoggerFactory.getLogger(DataContainerAnalysis.class);
43
44     // Needed for DataContainerCodecContext
45     final @NonNull ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> byStreamClass;
46     final @NonNull ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> byBindingArgClass;
47     final @NonNull ImmutableMap<NodeIdentifier, CodecContextSupplier> byYang;
48     final @NonNull ImmutableMap<String, ValueNodeCodecContext> leafNodes;
49
50     // Needed for generated classes
51     final @NonNull ImmutableMap<Method, ValueNodeCodecContext> leafContexts;
52     final @NonNull ImmutableMap<Class<?>, PropertyInfo> daoProperties;
53
54     DataContainerAnalysis(final CommonDataObjectCodecPrototype<R> prototype, final CodecItemFactory itemFactory) {
55         this(prototype.javaClass(), prototype.runtimeType(), prototype.contextFactory(), itemFactory);
56     }
57
58     DataContainerAnalysis(final Class<?> bindingClass, final R runtimeType, final CodecContextFactory factory,
59             final CodecItemFactory itemFactory) {
60         leafContexts = factory.getLeafNodes(bindingClass, runtimeType.statement());
61
62         // Reflection-based on the passed class
63         final var clsToMethod = getChildrenClassToMethod(bindingClass);
64
65         // Indexing part: be very careful around what gets done when
66         final var byYangBuilder = new HashMap<NodeIdentifier, CodecContextSupplier>();
67
68         // Step 1: add leaf children
69         var leafBuilder = ImmutableMap.<String, ValueNodeCodecContext>builderWithExpectedSize(leafContexts.size());
70         for (var leaf : leafContexts.values()) {
71             leafBuilder.put(leaf.getSchema().getQName().getLocalName(), leaf);
72             byYangBuilder.put(leaf.getDomPathArgument(), leaf);
73         }
74         leafNodes = leafBuilder.build();
75
76         final var byBindingArgClassBuilder = new HashMap<Class<?>, CommonDataObjectCodecPrototype<?>>();
77         final var byStreamClassBuilder = new HashMap<Class<?>, CommonDataObjectCodecPrototype<?>>();
78         final var daoPropertiesBuilder = new HashMap<Class<?>, PropertyInfo>();
79         for (var childDataObj : clsToMethod.entrySet()) {
80             final var method = childDataObj.getValue();
81             verify(!method.isDefault(), "Unexpected default method %s in %s", method, bindingClass);
82
83             final var retClass = childDataObj.getKey();
84             if (OpaqueObject.class.isAssignableFrom(retClass)) {
85                 // Filter OpaqueObjects, they are not containers
86                 continue;
87             }
88
89             // Record getter method
90             daoPropertiesBuilder.put(retClass, new PropertyInfo.Getter(method));
91
92             final var childProto = getChildPrototype(runtimeType, factory, itemFactory, retClass);
93             byStreamClassBuilder.put(childProto.javaClass(), childProto);
94             byYangBuilder.put(childProto.getYangArg(), childProto);
95
96             // FIXME: It really feels like we should be specializing DataContainerCodecPrototype so as to ditch
97             //        createInstance() and then we could do an instanceof check instead.
98             if (childProto.runtimeType() instanceof ChoiceRuntimeType) {
99                 final var choice = (ChoiceCodecContext<?>) childProto.getCodecContext();
100                 for (var cazeChild : choice.getCaseChildrenClasses()) {
101                     byBindingArgClassBuilder.put(cazeChild, childProto);
102                 }
103             }
104         }
105
106         // Snapshot before below processing
107         byStreamClass = ImmutableMap.copyOf(byStreamClassBuilder);
108
109         // Slight footprint optimization: we do not want to copy byStreamClass, as that would force its entrySet view
110         // to be instantiated. Furthermore the two maps can easily end up being equal -- hence we can reuse
111         // byStreamClass for the purposes of both.
112         byBindingArgClassBuilder.putAll(byStreamClassBuilder);
113         byBindingArgClass = byStreamClassBuilder.equals(byBindingArgClassBuilder) ? byStreamClass
114             : ImmutableMap.copyOf(byBindingArgClassBuilder);
115
116         // Find all non-default nonnullFoo() methods and update the corresponding property info
117         for (var entry : getChildrenClassToNonnullMethod(bindingClass).entrySet()) {
118             final var method = entry.getValue();
119             if (!method.isDefault()) {
120                 daoPropertiesBuilder.compute(entry.getKey(), (key, value) -> new PropertyInfo.GetterAndNonnull(
121                     verifyNotNull(value, "No getter for %s", key).getterMethod(), method));
122             }
123         }
124
125         // At this point all indexing is done: byYangBuilder should not be referenced
126         byYang = ImmutableMap.copyOf(byYangBuilder);
127         daoProperties = ImmutableMap.copyOf(daoPropertiesBuilder);
128     }
129
130     private static @NonNull CommonDataObjectCodecPrototype<?> getChildPrototype(final CompositeRuntimeType type,
131             final CodecContextFactory factory, final CodecItemFactory itemFactory,
132             final Class<? extends DataContainer> childClass) {
133         final var child = type.bindingChild(JavaTypeName.create(childClass));
134         if (child == null) {
135             throw DataContainerCodecContext.childNullException(factory.getRuntimeContext(), childClass,
136                 "Node %s does not have child named %s", type, childClass);
137         }
138         final var item = itemFactory.createItem(childClass, child.statement());
139         if (child instanceof ContainerLikeRuntimeType containerLike) {
140             if (child instanceof ContainerRuntimeType container
141                 && container.statement().findFirstEffectiveSubstatement(PresenceEffectiveStatement.class).isEmpty()) {
142                 return new StructuralContainerCodecPrototype(item, container, factory);
143             }
144             return new ContainerLikeCodecPrototype(item, containerLike, factory);
145         } else if (child instanceof ListRuntimeType list) {
146             return list.keyType() != null ? new MapCodecPrototype(item, list, factory)
147                 : new ListCodecPrototype(item, list, factory);
148         } else if (child instanceof ChoiceRuntimeType choice) {
149             return new ChoiceCodecPrototype(item, choice, factory);
150         } else {
151             throw new UnsupportedOperationException("Unhandled type " + child);
152         }
153     }
154
155
156     // FIXME: MDSAL-780: these methods perform analytics using java.lang.reflect to acquire the basic shape of the
157     //                   class. This is not exactly AOT friendly, as most of the information should be provided by
158     //                   CompositeRuntimeType.
159     //
160     //                   As as first cut, CompositeRuntimeType should provide the mapping between YANG children and the
161     //                   corresponding GETTER_PREFIX/NONNULL_PREFIX method names, If we have that, the use in this
162     //                   class should be fine.
163     //
164     //                   The second cut is binding the actual Method invocations, which is fine here, as this class is
165     //                   all about having a run-time generated class. AOT would be providing an alternative, where the
166     //                   equivalent class would be generated at compile-time and hence would bind to the methods
167     //                   directly -- and AOT equivalent of this class would really be a compile-time generated registry
168     //                   to those classes' entry points.
169
170     /**
171      * Scans supplied class and returns an iterable of all data children classes.
172      *
173      * @param type YANG Modeled Entity derived from DataContainer
174      * @return Iterable of all data children, which have YANG modeled entity
175      */
176     private static Map<Class<? extends DataContainer>, Method> getChildrenClassToMethod(final Class<?> type) {
177         return getChildClassToMethod(type, Naming.GETTER_PREFIX);
178     }
179
180     private static Map<Class<? extends DataContainer>, Method> getChildrenClassToNonnullMethod(final Class<?> type) {
181         return getChildClassToMethod(type, Naming.NONNULL_PREFIX);
182     }
183
184     private static Map<Class<? extends DataContainer>, Method> getChildClassToMethod(final Class<?> type,
185             final String prefix) {
186         checkArgument(type != null, "Target type must not be null");
187         checkArgument(DataContainer.class.isAssignableFrom(type), "Supplied type %s must be derived from DataContainer",
188             type);
189         final var ret = new HashMap<Class<? extends DataContainer>, Method>();
190         for (Method method : type.getMethods()) {
191             getYangModeledReturnType(method, prefix).ifPresent(entity -> ret.put(entity, method));
192         }
193         return ret;
194     }
195
196     static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
197             final String prefix) {
198         final String methodName = method.getName();
199         if ("getClass".equals(methodName) || !methodName.startsWith(prefix) || method.getParameterCount() > 0) {
200             return Optional.empty();
201         }
202
203         final Class<?> returnType = method.getReturnType();
204         if (DataContainer.class.isAssignableFrom(returnType)) {
205             return optionalDataContainer(returnType);
206         } else if (List.class.isAssignableFrom(returnType)) {
207             return getYangModeledReturnType(method, 0);
208         } else if (Map.class.isAssignableFrom(returnType)) {
209             return getYangModeledReturnType(method, 1);
210         }
211         return Optional.empty();
212     }
213
214     @SuppressWarnings("checkstyle:illegalCatch")
215     private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
216             final int parameterOffset) {
217         try {
218             return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(),
219                 () -> genericParameter(method.getGenericReturnType(), parameterOffset)
220                     .flatMap(result -> result instanceof Class ? optionalCast((Class<?>) result) : Optional.empty()));
221         } catch (Exception e) {
222             /*
223              * It is safe to log this this exception on debug, since this
224              * method should not fail. Only failures are possible if the
225              * runtime / backing.
226              */
227             LOG.debug("Unable to find YANG modeled return type for {}", method, e);
228         }
229         return Optional.empty();
230     }
231
232     private static Optional<java.lang.reflect.Type> genericParameter(final java.lang.reflect.Type type,
233             final int offset) {
234         if (type instanceof ParameterizedType parameterized) {
235             final var parameters = parameterized.getActualTypeArguments();
236             if (parameters.length > offset) {
237                 return Optional.of(parameters[offset]);
238             }
239         }
240         return Optional.empty();
241     }
242
243     private static Optional<Class<? extends DataContainer>> optionalCast(final Class<?> type) {
244         return DataContainer.class.isAssignableFrom(type) ? optionalDataContainer(type) : Optional.empty();
245     }
246
247     private static Optional<Class<? extends DataContainer>> optionalDataContainer(final Class<?> type) {
248         return Optional.of(type.asSubclass(DataContainer.class));
249     }
250 }