X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding%2Fmdsal-binding-dom-codec%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fdom%2Fcodec%2Fimpl%2FDataObjectCodecContext.java;h=52a7b06be46e728825533b2e1108251549d81ae8;hb=refs%2Fchanges%2F45%2F98245%2F100;hp=fd1c2eea8bd9ee66a94922fbd777e740d8f4c5d6;hpb=60039d4d027e172f6e1d644fb1c2fa60688c06c0;p=mdsal.git diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java index fd1c2eea8b..52a7b06be4 100644 --- a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java +++ b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/DataObjectCodecContext.java @@ -14,6 +14,8 @@ import com.google.common.annotations.Beta; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap.Builder; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; @@ -27,8 +29,13 @@ import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.mdsal.binding.dom.codec.api.IncorrectNestingException; +import org.opendaylight.mdsal.binding.model.api.GeneratedType; +import org.opendaylight.mdsal.binding.model.api.JavaTypeName; import org.opendaylight.mdsal.binding.model.api.Type; +import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType; import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext; +import org.opendaylight.mdsal.binding.runtime.api.ChoiceRuntimeType; +import org.opendaylight.mdsal.binding.runtime.api.CompositeRuntimeType; import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections; import org.opendaylight.yangtools.yang.binding.Augmentable; import org.opendaylight.yangtools.yang.binding.Augmentation; @@ -45,10 +52,9 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgum import org.opendaylight.yangtools.yang.data.api.schema.AugmentationNode; import org.opendaylight.yangtools.yang.data.api.schema.DistinctNodeContainer; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.model.api.AugmentationSchemaNode; -import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; -import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus; +import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement; +import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -56,7 +62,7 @@ import org.slf4j.LoggerFactory; * This class is an implementation detail. It is public only due to technical reasons and may change at any time. */ @Beta -public abstract class DataObjectCodecContext +public abstract class DataObjectCodecContext extends DataContainerCodecContext { private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class); private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class, @@ -97,7 +103,8 @@ public abstract class DataObjectCodecContext bindingClass = getBindingClass(); - final ImmutableMap tmpLeaves = factory().getLeafNodes(bindingClass, getSchema()); + final ImmutableMap tmpLeaves = factory().getLeafNodes(bindingClass, + getType().statement()); final Map, Method> clsToMethod = BindingReflections.getChildrenClassToMethod(bindingClass); @@ -133,7 +140,7 @@ public abstract class DataObjectCodecContext choice = (ChoiceNodeCodecContext) childProto.get(); for (final Class cazeChild : choice.getCaseChildrenClasses()) { byBindingArgClassBuilder.put(cazeChild, childProto); @@ -151,13 +158,14 @@ public abstract class DataObjectCodecContext possibleAugmentations; + final Iterable possibleAugmentations; if (Augmentable.class.isAssignableFrom(bindingClass)) { - possibleAugmentations = factory().getRuntimeContext().getAvailableAugmentationTypes(getSchema()); + final var type = getType(); + possibleAugmentations = Iterables.concat(type.augments(), type.mismatchedAugments()); generatedClass = CodecDataObjectGenerator.generateAugmentable(prototype.getFactory().getLoader(), bindingClass, tmpLeaves, tmpDataObjects, keyMethod); } else { - possibleAugmentations = ImmutableMap.of(); + possibleAugmentations = List.of(); generatedClass = CodecDataObjectGenerator.generate(prototype.getFactory().getLoader(), bindingClass, tmpLeaves, tmpDataObjects, keyMethod); } @@ -165,7 +173,7 @@ public abstract class DataObjectCodecContext> augByYang = new HashMap<>(); final Map, DataContainerCodecPrototype> augByStream = new HashMap<>(); - for (final Type augment : possibleAugmentations.values()) { + for (final AugmentRuntimeType augment : possibleAugmentations) { final DataContainerCodecPrototype augProto = getAugmentationPrototype(augment); final PathArgument augYangArg = augProto.getYangArg(); if (augByYang.putIfAbsent(augYangArg, augProto) == null) { @@ -189,12 +197,17 @@ public abstract class DataObjectCodecContext DataContainerCodecContext streamChild(final Class childClass) { - final DataContainerCodecPrototype childProto = streamChildPrototype(childClass); - return (DataContainerCodecContext) childNonNull(childProto, childClass, " Child %s is not valid child.", - childClass).get(); + return (DataContainerCodecContext) childNonNull(streamChildPrototype(childClass), childClass, + "Child %s is not valid child of %s", getBindingClass(), childClass).get(); } private DataContainerCodecPrototype streamChildPrototype(final Class childClass) { @@ -274,10 +287,12 @@ public abstract class DataObjectCodecContext loadChildPrototype(final Class childClass) { - final DataSchemaNode childSchema = childNonNull( - factory().getRuntimeContext().findChildSchemaDefinition(getSchema(), namespace(), childClass), childClass, - "Node %s does not have child named %s", getSchema(), childClass); - return DataContainerCodecPrototype.from(createBindingArg(childClass, childSchema), childSchema, factory()); + final var type = getType(); + final var child = childNonNull(type.bindingChild(JavaTypeName.create(childClass)), childClass, + "Node %s does not have child named %s", type, childClass); + + return DataContainerCodecPrototype.from(createBindingArg(childClass, child.statement()), + (CompositeRuntimeType) child, factory()); } // FIXME: MDSAL-697: move this method into BindingRuntimeContext @@ -287,7 +302,7 @@ public abstract class DataObjectCodecContext, along with the override we create here. One // more input we may need to provide is our bindingClass(). @SuppressWarnings("unchecked") - Item createBindingArg(final Class childClass, final DataSchemaNode childSchema) { + Item createBindingArg(final Class childClass, final EffectiveStatement childSchema) { return Item.of((Class) childClass); } @@ -340,13 +355,14 @@ public abstract class DataObjectCodecContext, DataContainerCodecPrototype>) + MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched); if (witness == expected) { LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this); return prototype; } - expected = (ImmutableMap, DataContainerCodecPrototype>) witness; + expected = witness; final DataContainerCodecPrototype existing = expected.get(childClass); if (existing != null) { LOG.trace("Using raced mismatched augmentation {} -> {} in {}", childClass, existing, this); @@ -367,19 +383,23 @@ public abstract class DataObjectCodecContext getAugmentationPrototype(final Type value) { + private @NonNull DataContainerCodecPrototype getAugmentationPrototype(final AugmentRuntimeType augment) { final BindingRuntimeContext ctx = factory().getRuntimeContext(); + final GeneratedType javaType = augment.javaType(); final Class> augClass; try { - augClass = ctx.loadClass(value); + augClass = ctx.loadClass(javaType); } catch (final ClassNotFoundException e) { - throw new IllegalStateException("RuntimeContext references type " + value + " but failed to its class", e); + throw new IllegalStateException( + "RuntimeContext references type " + javaType + " but failed to load its class", e); } - final Entry augSchema = - ctx.getResolvedAugmentationSchema(getSchema(), augClass); - return DataContainerCodecPrototype.from(augClass, augSchema.getKey(), augSchema.getValue(), factory()); + // TODO: at some point we need the effective children + return DataContainerCodecPrototype.from(augClass, new AugmentationIdentifier(augment.statement() + .streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class) + .map(SchemaTreeEffectiveStatement::getIdentifier) + .collect(ImmutableSet.toImmutableSet())), augment, factory()); } @SuppressWarnings("checkstyle:illegalCatch") @@ -410,9 +430,15 @@ public abstract class DataObjectCodecContext value : augmentationByStream.values()) { - final NormalizedNode augData = data.childByArg(value.getYangArg()); - if (augData != null) { - map.put(value.getBindingClass(), value.get().deserializeObject(augData)); + final var augClass = value.getBindingClass(); + // Do not perform duplicate deserialization if we have already created the corresponding augmentation + // and validate whether the proposed augmentation is valid ion this instantiation context. + if (!map.containsKey(augClass) && getType().augments().contains(value.getType())) { + final NormalizedNode augData = data.childByArg(value.getYangArg()); + if (augData != null) { + // ... make sure we do not replace an e + map.putIfAbsent(augClass, value.get().deserializeObject(augData)); + } } } return map;