import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.ParameterizedType;
+import java.util.Arrays;
+import java.util.HashSet;
import java.util.List;
+import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.mdsal.binding.dom.codec.api.MissingClassInLoadingStrategyException;
import org.opendaylight.mdsal.binding.dom.codec.api.MissingSchemaException;
import org.opendaylight.mdsal.binding.dom.codec.api.MissingSchemaForClassException;
-import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
-import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.RuntimeTypeContainer;
+import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
+import org.opendaylight.yangtools.util.ClassLoaderUtils;
import org.opendaylight.yangtools.yang.binding.Augmentation;
import org.opendaylight.yangtools.yang.binding.BindingObject;
+import org.opendaylight.yangtools.yang.binding.DataContainer;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
-import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-abstract class DataContainerCodecContext<D extends DataObject, T extends WithStatus> extends NodeCodecContext
+abstract class DataContainerCodecContext<D extends DataObject, T extends RuntimeTypeContainer> extends NodeCodecContext
implements BindingDataObjectCodecTreeNode<D> {
+ private static final Logger LOG = LoggerFactory.getLogger(DataContainerCodecContext.class);
private static final VarHandle EVENT_STREAM_SERIALIZER;
static {
this.prototype = requireNonNull(prototype);
}
- @Override
- public final T getSchema() {
- return prototype.getSchema();
+ public final @NonNull T getType() {
+ return prototype.getType();
}
@Override
return new CachingNormalizedNodeCodec<>(this, ImmutableSet.copyOf(cacheSpecifier));
}
- BindingStreamEventWriter createWriter(final NormalizedNodeStreamWriter domWriter) {
- return BindingToNormalizedStreamWriter.create(this, domWriter);
- }
-
protected final <V> @NonNull V childNonNull(final @Nullable V nullable,
final YangInstanceIdentifier.PathArgument child, final String message, final Object... args) {
if (nullable == null) {
private IllegalArgumentException childNullException(final QName child, final String message, final Object... args) {
final QNameModule module = child.getModule();
- if (!factory().getRuntimeContext().getSchemaContext().findModule(module).isPresent()) {
+ if (!factory().getRuntimeContext().getEffectiveModelContext().findModule(module).isPresent()) {
throw new MissingSchemaException("Module " + module + " is not present in current schema context.");
}
throw IncorrectNestingException.create(message, args);
private IllegalArgumentException childNullException(final Class<?> childClass, final String message,
final Object... args) {
final BindingRuntimeContext runtimeContext = factory().getRuntimeContext();
- final WithStatus schema;
+ final CompositeRuntimeType schema;
if (Augmentation.class.isAssignableFrom(childClass)) {
- schema = runtimeContext.getAugmentationDefinition(childClass);
+ schema = runtimeContext.getAugmentationDefinition(childClass.asSubclass(Augmentation.class));
} else {
schema = runtimeContext.getSchemaDefinition(childClass);
}
throw new MissingSchemaForClassException(childClass);
}
- final ClassLoadingStrategy strategy = runtimeContext.getStrategy();
try {
- strategy.loadClass(childClass.getName());
+ runtimeContext.loadClass(Type.of(childClass));
} catch (final ClassNotFoundException e) {
throw new MissingClassInLoadingStrategyException(
- "User supplied class " + childClass.getName() + " is not available in " + strategy, e);
+ "User supplied class " + childClass.getName() + " is not available in " + runtimeContext, e);
}
throw IncorrectNestingException.create(message, args);
}
@Override
- public NormalizedNode<?, ?> serialize(final D data) {
+ public NormalizedNode serialize(final D data) {
final NormalizedNodeResult result = new NormalizedNodeResult();
// We create DOM stream writer which produces normalized nodes
final NormalizedNodeStreamWriter domWriter = ImmutableNormalizedNodeStreamWriter.from(result);
@Override
public void writeAsNormalizedNode(final D data, final NormalizedNodeStreamWriter writer) {
try {
- eventStreamSerializer().serialize(data, createWriter(writer));
+ eventStreamSerializer().serialize(data, new BindingToNormalizedStreamWriter(this, writer));
} catch (final IOException e) {
throw new IllegalStateException("Failed to serialize Binding DTO",e);
}
}
+
+ static final <T extends NormalizedNode> @NonNull T checkDataArgument(final @NonNull Class<T> expectedType,
+ final NormalizedNode data) {
+ try {
+ return expectedType.cast(requireNonNull(data));
+ } catch (ClassCastException e) {
+ throw new IllegalArgumentException("Expected " + expectedType.getSimpleName(), e);
+ }
+ }
+
+ // FIXME: MDSAL-780 replace this method with BindingRuntimeTypes-driven logic
+ static final Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
+ final String prefix) {
+ final String methodName = method.getName();
+ if ("getClass".equals(methodName) || !methodName.startsWith(prefix) || method.getParameterCount() > 0) {
+ return Optional.empty();
+ }
+
+ final Class<?> returnType = method.getReturnType();
+ if (DataContainer.class.isAssignableFrom(returnType)) {
+ return optionalDataContainer(returnType);
+ } else if (List.class.isAssignableFrom(returnType)) {
+ return getYangModeledReturnType(method, 0);
+ } else if (Map.class.isAssignableFrom(returnType)) {
+ return getYangModeledReturnType(method, 1);
+ }
+ return Optional.empty();
+ }
+
+ @SuppressWarnings("checkstyle:illegalCatch")
+ private static Optional<Class<? extends DataContainer>> getYangModeledReturnType(final Method method,
+ final int parameterOffset) {
+ try {
+ return ClassLoaderUtils.callWithClassLoader(method.getDeclaringClass().getClassLoader(),
+ () -> genericParameter(method.getGenericReturnType(), parameterOffset)
+ .flatMap(result -> result instanceof Class ? optionalCast((Class<?>) result) : Optional.empty()));
+ } catch (Exception e) {
+ /*
+ * It is safe to log this this exception on debug, since this
+ * method should not fail. Only failures are possible if the
+ * runtime / backing.
+ */
+ LOG.debug("Unable to find YANG modeled return type for {}", method, e);
+ }
+ return Optional.empty();
+ }
+
+ private static Optional<java.lang.reflect.Type> genericParameter(final java.lang.reflect.Type type,
+ final int offset) {
+ if (type instanceof ParameterizedType parameterized) {
+ final var parameters = parameterized.getActualTypeArguments();
+ if (parameters.length > offset) {
+ return Optional.of(parameters[offset]);
+ }
+ }
+ return Optional.empty();
+ }
+
+ private static Optional<Class<? extends DataContainer>> optionalCast(final Class<?> type) {
+ return DataContainer.class.isAssignableFrom(type) ? optionalDataContainer(type) : Optional.empty();
+ }
+
+
+ // FIXME: MDSAL-780: remove this method
+ static final Optional<Class<? extends DataContainer>> optionalDataContainer(final Class<?> type) {
+ return Optional.of(type.asSubclass(DataContainer.class));
+ }
+
+
+ /**
+ * Determines if two augmentation classes or case classes represents same data.
+ *
+ * <p>
+ * Two augmentations or cases could be substituted only if and if:
+ * <ul>
+ * <li>Both implements same interfaces</li>
+ * <li>Both have same children</li>
+ * <li>If augmentations: Both have same augmentation target class. Target class was generated for data node in a
+ * grouping.</li>
+ * <li>If cases: Both are from same choice. Choice class was generated for data node in grouping.</li>
+ * </ul>
+ *
+ * <p>
+ * <b>Explanation:</b>
+ * Binding Specification reuses classes generated for groupings as part of normal data tree, this classes from
+ * grouping could be used at various locations and user may not be aware of it and may use incorrect case or
+ * augmentation in particular subtree (via copy constructors, etc).
+ *
+ * @param potential Class which is potential substitution
+ * @param target Class which should be used at particular subtree
+ * @return true if and only if classes represents same data.
+ * @throws NullPointerException if any argument is {@code null}
+ */
+ // FIXME: MDSAL-785: this really should live in BindingRuntimeTypes and should not be based on reflection. The only
+ // user is binding-dom-codec and the logic could easily be performed on GeneratedType instead. For
+ // a particular world this boils down to a matrix, which can be calculated either on-demand or
+ // when we create BindingRuntimeTypes. Achieving that will bring us one step closer to being able
+ // to have a pre-compiled Binding Runtime.
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ static boolean isSubstitutionFor(final Class potential, final Class target) {
+ Set<Class> subImplemented = new HashSet<>(Arrays.asList(potential.getInterfaces()));
+ Set<Class> targetImplemented = new HashSet<>(Arrays.asList(target.getInterfaces()));
+ if (!subImplemented.equals(targetImplemented)) {
+ return false;
+ }
+ if (Augmentation.class.isAssignableFrom(potential)
+ && !BindingReflections.findAugmentationTarget(potential).equals(
+ BindingReflections.findAugmentationTarget(target))) {
+ return false;
+ }
+ for (Method potentialMethod : potential.getMethods()) {
+ if (Modifier.isStatic(potentialMethod.getModifiers())) {
+ // Skip any static methods, as we are not interested in those
+ continue;
+ }
+
+ try {
+ Method targetMethod = target.getMethod(potentialMethod.getName(), potentialMethod.getParameterTypes());
+ if (!potentialMethod.getReturnType().equals(targetMethod.getReturnType())) {
+ return false;
+ }
+ } catch (NoSuchMethodException e) {
+ // Counterpart method is missing, so classes could not be substituted.
+ return false;
+ } catch (SecurityException e) {
+ throw new IllegalStateException("Could not compare methods", e);
+ }
+ }
+ return true;
+ }
}