*/
package org.opendaylight.mdsal.binding.dom.codec.impl;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.MultimapBuilder.SetMultimapBuilder;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.model.api.JavaTypeName;
+import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
+import org.opendaylight.mdsal.binding.runtime.api.CaseRuntimeType;
+import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType;
import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
-import org.opendaylight.yangtools.yang.data.impl.schema.SchemaUtils;
+import org.opendaylight.yangtools.yang.data.util.DataSchemaContextNode;
+import org.opendaylight.yangtools.yang.data.util.NormalizedNodeSchemaUtils;
import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.CaseSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.ChoiceSchemaNode;
import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-final class ChoiceNodeCodecContext<D extends DataObject> extends DataContainerCodecContext<D, ChoiceSchemaNode> {
+/**
+ * This is a bit tricky. DataObject addressing does not take into account choice/case statements, and hence given:
+ *
+ * <pre>
+ * <code>
+ * container foo {
+ * choice bar {
+ * leaf baz;
+ * }
+ * }
+ * </code>
+ * </pre>
+ * we will see {@code Baz extends ChildOf<Foo>}, which is how the users would address it in InstanceIdentifier terms.
+ * The implicit assumption being made is that {@code Baz} identifies a particular instantiation and hence provides
+ * unambiguous reference to an effective schema statement.
+ *
+ * <p>
+ * Unfortunately this does not quite work with groupings, as their generation has changed: we do not have interfaces
+ * that would capture grouping instantiations, hence we do not have a proper addressing point and users need to specify
+ * the interfaces generated in the grouping's definition. These can be very much ambiguous, as a {@code grouping} can be
+ * used in multiple modules independently within an {@code augment} targeting {@code choice}, as each instantiation is
+ * guaranteed to have a unique namespace -- but we do not have the appropriate instantiations of those nodes.
+ *
+ * <p>
+ * To address this issue we have a two-class lookup mechanism, which relies on the interface generated for the
+ * {@code case} statement to act as the namespace anchor bridging the nodes inside the grouping to the namespace in
+ * which they are instantiated.
+ *
+ * <p>
+ * Furthermore downstream code relies on historical mechanics, which would guess what the instantiation is, silently
+ * assuming the ambiguity is theoretical and does not occur in practice.
+ *
+ * <p>
+ * This leads to three classes of addressing, in order descending performance requirements.
+ * <ul>
+ * <li>Direct DataObject, where we name an exact child</li>
+ * <li>Case DataObject + Grouping DataObject</li>
+ * <li>Grouping DataObject, which is ambiguous</li>
+ * </ul>
+ *
+ * {@link #byCaseChildClass} supports direct DataObject mapping and contains only unambiguous children, while
+ * {@link #byClass} supports indirect mapping and contains {@code case} sub-statements.
+ *
+ * {@link #ambiguousByCaseChildClass} contains ambiguous mappings, for which we end up issuing warnings. We track each
+ * ambiguous reference and issue warn once when they are encountered -- tracking warning information in
+ * {@link #ambiguousByCaseChildWarnings}.
+ */
+final class ChoiceNodeCodecContext<D extends DataObject> extends DataContainerCodecContext<D, ChoiceRuntimeType> {
private static final Logger LOG = LoggerFactory.getLogger(ChoiceNodeCodecContext.class);
- private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
- /*
- * This is a bit tricky. DataObject addressing does not take into account choice/case statements, and hence
- * given:
- *
- * container foo {
- * choice bar {
- * leaf baz;
- * }
- * }
- *
- * we will see {@code Baz extends ChildOf<Foo>}, which is how the users would address it in InstanceIdentifier
- * terms. The implicit assumption being made is that {@code Baz} identifies a particular instantiation and hence
- * provides unambiguous reference to an effective schema statement.
- *
- * <p>
- * Unfortunately this does not quite work with groupings, as their generation has changed: we do not have
- * interfaces that would capture grouping instantiations, hence we do not have a proper addressing point and
- * users need to specify the interfaces generated in the grouping's definition. These can be very much
- * ambiguous, as a {@code grouping} can be used in multiple modules independently within an {@code augment}
- * targeting {@code choice}, as each instantiation is guaranteed to have a unique namespace -- but we do not
- * have the appropriate instantiations of those nodes.
- *
- * <p>
- * To address this issue we have a two-class lookup mechanism, which relies on the interface generated for
- * the {@code case} statement to act as the namespace anchor bridging the nodes inside the grouping to the
- * namespace in which they are instantiated.
- *
- * <p>
- * Furthermore downstream code relies on historical mechanics, which would guess what the instantiation is,
- * silently assuming the ambiguity is theoretical and does not occur in practice.
- *
- * <p>
- * This leads to three classes of addressing, in order descending performance requirements.
- * <ul>
- * <li>Direct DataObject, where we name an exact child</li>
- * <li>Case DataObject + Grouping DataObject</li>
- * <li>Grouping DataObject, which is ambiguous</li>
- * </ul>
- *
- * {@code byCaseChildClass} supports direct DataObject mapping and contains only unambiguous children, while
- * {@code byClass} supports indirect mapping and contains {@code case} sub-statements.
- *
- * ambiguousByCaseChildClass contains ambiguous mappings, for which we end up issuing warnings. We track each
- * ambiguous reference and issue warnings when they are encountered.
- */
- private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
- private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
+ private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChild;
private final ImmutableListMultimap<Class<?>, DataContainerCodecPrototype<?>> ambiguousByCaseChildClass;
+ private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byCaseChildClass;
+ private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byClass;
private final Set<Class<?>> ambiguousByCaseChildWarnings;
- ChoiceNodeCodecContext(final DataContainerCodecPrototype<ChoiceSchemaNode> prototype) {
+ ChoiceNodeCodecContext(final DataContainerCodecPrototype<ChoiceRuntimeType> prototype) {
super(prototype);
final Map<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> byYangCaseChildBuilder =
- new HashMap<>();
+ new HashMap<>();
final Map<Class<?>, DataContainerCodecPrototype<?>> byClassBuilder = new HashMap<>();
- final SetMultimap<Class<?>, DataContainerCodecPrototype<?>> childToCase = SetMultimapBuilder.hashKeys()
- .hashSetValues().build();
- final Set<Class<?>> potentialSubstitutions = new HashSet<>();
- // Walks all cases for supplied choice in current runtime context
- for (final Class<?> caze : factory().getRuntimeContext().getCases(getBindingClass())) {
- // We try to load case using exact match thus name
- // and original schema must equals
- final DataContainerCodecPrototype<CaseSchemaNode> cazeDef = loadCase(caze);
- // If we have case definition, this case is instantiated
- // at current location and thus is valid in context of parent choice
- if (cazeDef != null) {
- byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
- // Updates collection of case children
- @SuppressWarnings("unchecked")
- final Class<? extends DataObject> cazeCls = (Class<? extends DataObject>) caze;
- for (final Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses(cazeCls)) {
- childToCase.put(cazeChild, cazeDef);
- }
- // Updates collection of YANG instance identifier to case
- for (final DataSchemaNode cazeChild : cazeDef.getSchema().getChildNodes()) {
+ final SetMultimap<Class<?>, DataContainerCodecPrototype<?>> childToCase =
+ SetMultimapBuilder.hashKeys().hashSetValues().build();
+
+ // Load case statements valid in this choice and keep track of their names
+ final var choiceType = prototype.getType();
+ final var factory = prototype.getFactory();
+ final var localCases = new HashSet<JavaTypeName>();
+ for (var caseType : choiceType.validCaseChildren()) {
+ final var cazeDef = loadCase(factory, caseType);
+ localCases.add(caseType.getIdentifier());
+ byClassBuilder.put(cazeDef.getBindingClass(), cazeDef);
+
+ // Updates collection of case children
+ @SuppressWarnings("unchecked")
+ final Class<? extends DataObject> cazeCls = (Class<? extends DataObject>) cazeDef.getBindingClass();
+ for (final Class<? extends DataObject> cazeChild : BindingReflections.getChildrenClasses(cazeCls)) {
+ childToCase.put(cazeChild, cazeDef);
+ }
+ // Updates collection of YANG instance identifier to case
+ for (var stmt : cazeDef.getType().statement().effectiveSubstatements()) {
+ if (stmt instanceof DataSchemaNode) {
+ final DataSchemaNode cazeChild = (DataSchemaNode) stmt;
if (cazeChild.isAugmenting()) {
- final AugmentationSchemaNode augment = SchemaUtils.findCorrespondingAugment(cazeDef.getSchema(),
- cazeChild);
+ final AugmentationSchemaNode augment = NormalizedNodeSchemaUtils.findCorrespondingAugment(
+ // FIXME: bad cast
+ (DataSchemaNode) cazeDef.getType().statement(), cazeChild);
if (augment != null) {
- byYangCaseChildBuilder.put(SchemaUtils.getNodeIdentifierForAugmentation(augment), cazeDef);
+ byYangCaseChildBuilder.put(DataSchemaContextNode.augmentationIdentifierFrom(augment),
+ cazeDef);
continue;
}
}
byYangCaseChildBuilder.put(NodeIdentifier.create(cazeChild.getQName()), cazeDef);
}
- } else {
- /*
- * If case definition is not available, we store it for
- * later check if it could be used as substitution of existing one.
- */
- potentialSubstitutions.add(caze);
}
}
byYangCaseChild = ImmutableMap.copyOf(byYangCaseChildBuilder);
ambiguousByCaseChildWarnings = ambiguousByCaseChildClass.isEmpty() ? ImmutableSet.of()
: ConcurrentHashMap.newKeySet();
- final Map<Class<?>, DataContainerCodecPrototype<?>> bySubstitutionBuilder = new HashMap<>();
/*
- * Walks all cases which are not directly instantiated and
- * tries to match them to instantiated cases - represent same data as instantiated case,
- * only case name or schema path is different. This is required due property of
- * binding specification, that if choice is in grouping schema path location is lost,
- * and users may use incorrect case class using copy builders.
+ * Choice/Case mapping across groupings is compile-time unsafe and we therefore need to also track any
+ * CaseRuntimeTypes added to the choice in other contexts. This is necessary to discover when a case represents
+ * equivalent data in a different instantiation context.
+ *
+ * This is required due property of binding specification, that if choice is in grouping schema path location is
+ * lost, and users may use incorrect case class using copy builders.
*/
- for (final Class<?> substitution : potentialSubstitutions) {
- search: for (final Entry<Class<?>, DataContainerCodecPrototype<?>> real : byClassBuilder.entrySet()) {
- if (BindingReflections.isSubstitutionFor(substitution, real.getKey())) {
- bySubstitutionBuilder.put(substitution, real.getValue());
- break search;
+ final Map<Class<?>, DataContainerCodecPrototype<?>> bySubstitutionBuilder = new HashMap<>();
+ final var context = factory.getRuntimeContext();
+ for (var caseType : context.getTypes().allCaseChildren(choiceType)) {
+ final var caseName = caseType.getIdentifier();
+ if (!localCases.contains(caseName)) {
+ // FIXME: do not rely on class loading here, the check we are performing should be possible on
+ // GeneratedType only -- or it can be provided by BindingRuntimeTypes -- i.e. rather than
+ // 'allCaseChildren()' it would calculate additional mappings we can use off-the-bat.
+ final Class<?> substitution = loadCase(context, caseType);
+
+ search: for (final Entry<Class<?>, DataContainerCodecPrototype<?>> real : byClassBuilder.entrySet()) {
+ if (BindingReflections.isSubstitutionFor(substitution, real.getKey())) {
+ bySubstitutionBuilder.put(substitution, real.getValue());
+ break search;
+ }
}
}
}
+
byClassBuilder.putAll(bySubstitutionBuilder);
byClass = ImmutableMap.copyOf(byClassBuilder);
}
+ private static DataContainerCodecPrototype<CaseRuntimeType> loadCase(final CodecContextFactory factory,
+ final CaseRuntimeType caseType) {
+ return DataContainerCodecPrototype.from(loadCase(factory.getRuntimeContext(), caseType), caseType, factory);
+ }
+
+ private static Class<?> loadCase(final BindingRuntimeContext context, final CaseRuntimeType caseType) {
+ final var className = caseType.getIdentifier();
+ try {
+ return context.loadClass(className);
+ } catch (ClassNotFoundException e) {
+ throw new LinkageError("Failed to load class for " + className, e);
+ }
+ }
+
+ @Override
+ public WithStatus getSchema() {
+ // FIXME: Bad cast, we should be returning an EffectiveStatement perhaps?
+ return (WithStatus) getType().statement();
+ }
+
@SuppressWarnings("unchecked")
@Override
public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
return Iterables.concat(byCaseChildClass.keySet(), ambiguousByCaseChildClass.keySet());
}
- protected DataContainerCodecPrototype<CaseSchemaNode> loadCase(final Class<?> childClass) {
- final Optional<CaseSchemaNode> childSchema = factory().getRuntimeContext().getCaseSchemaDefinition(getSchema(),
- childClass);
- if (childSchema.isPresent()) {
- return DataContainerCodecPrototype.from(childClass, childSchema.get(), factory());
- }
-
- LOG.debug("Supplied class %s is not valid case in schema %s", childClass, getSchema());
- return null;
- }
-
@Override
- public NodeCodecContext<?> yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
+ public NodeCodecContext yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
final DataContainerCodecPrototype<?> cazeProto;
if (arg instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
cazeProto = byYangCaseChild.get(new NodeIdentifier(arg.getNodeType()));
cazeProto = byYangCaseChild.get(arg);
}
- return childNonNull(cazeProto, arg,"Argument %s is not valid child of %s", arg, getSchema()).get()
+ return childNonNull(cazeProto, arg, "Argument %s is not valid child of %s", arg, getSchema()).get()
.yangPathArgumentChild(arg);
}
- @SuppressWarnings("unchecked")
@Override
- public D deserialize(final NormalizedNode<?, ?> data) {
- Preconditions.checkArgument(data instanceof ChoiceNode);
- final NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> casted =
- (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) data;
- final NormalizedNode<?, ?> first = Iterables.getFirst(casted.getValue(), null);
+ @SuppressWarnings("unchecked")
+ @SuppressFBWarnings(value = "NP_NONNULL_RETURN_VIOLATION", justification = "See FIXME below")
+ public D deserialize(final NormalizedNode data) {
+ checkArgument(data instanceof ChoiceNode);
+ final ChoiceNode casted = (ChoiceNode) data;
+ final NormalizedNode first = Iterables.getFirst(casted.body(), null);
if (first == null) {
// FIXME: this needs to be sorted out
}
@Override
- protected Object deserializeObject(final NormalizedNode<?, ?> normalizedNode) {
+ protected Object deserializeObject(final NormalizedNode normalizedNode) {
return deserialize(normalizedNode);
}
@Override
public PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
- Preconditions.checkArgument(getDomPathArgument().equals(arg));
+ checkArgument(getDomPathArgument().equals(arg));
return null;
}