Hide CodecContext methods
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecContext.java
index 9c941089fd7a434d35072f7005a708d5ff8842fc..d99da25ddc04f21dd914376af14c9089ea26dcc5 100644 (file)
@@ -11,39 +11,40 @@ import static com.google.common.base.Preconditions.checkArgument;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Throwables;
+import com.google.common.base.VerifyException;
+import com.google.common.collect.ImmutableCollection;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
 import java.lang.invoke.MethodHandle;
 import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import java.lang.invoke.VarHandle;
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-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.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeCachingCodec;
 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
 import org.opendaylight.mdsal.binding.model.api.Type;
 import org.opendaylight.mdsal.binding.runtime.api.AugmentRuntimeType;
 import org.opendaylight.mdsal.binding.runtime.api.AugmentableRuntimeType;
-import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
 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;
+import org.opendaylight.yangtools.yang.binding.BindingObject;
 import org.opendaylight.yangtools.yang.binding.DataObject;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.binding.DataObjectStep;
 import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.AugmentationIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
-import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
-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.DataContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
-import org.opendaylight.yangtools.yang.model.api.DocumentedNode.WithStatus;
+import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -52,10 +53,15 @@ 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<D extends DataObject, T extends CompositeRuntimeType>
-        extends DataContainerCodecContext<D, T> {
+public abstract sealed class DataObjectCodecContext<D extends DataObject, T extends CompositeRuntimeType>
+        extends AbstractDataObjectCodecContext<D, T> implements BindingDataObjectCodecTreeNode<D>
+        permits CaseCodecContext, ContainerLikeCodecContext, ListCodecContext, NotificationCodecContext {
     private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
 
+    private static final MethodType CONSTRUCTOR_TYPE = MethodType.methodType(void.class,
+        AbstractDataObjectCodecContext.class, DataContainerNode.class);
+    private static final MethodType DATAOBJECT_TYPE = MethodType.methodType(DataObject.class,
+        DataObjectCodecContext.class, DataContainerNode.class);
     private static final VarHandle MISMATCHED_AUGMENTED;
 
     static {
@@ -67,181 +73,136 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         }
     }
 
-    private final ImmutableMap<String, ValueNodeCodecContext> leafChild;
-    private final ImmutableMap<YangInstanceIdentifier.PathArgument, NodeContextSupplier> byYang;
-    private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byStreamClass;
-    private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> byBindingArgClass;
-    private final ImmutableMap<YangInstanceIdentifier.PathArgument, DataContainerCodecPrototype<?>> augmentationByYang;
-    private final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> augmentationByStream;
+    private final ImmutableMap<Class<?>, AugmentationCodecPrototype> augmentToPrototype;
+    private final ImmutableMap<NodeIdentifier, Class<?>> yangToAugmentClass;
     private final @NonNull Class<? extends CodecDataObject<?>> generatedClass;
     private final MethodHandle proxyConstructor;
 
     // Note this the content of this field depends only of invariants expressed as this class's fields or
     // BindingRuntimeContext. It is only accessed via MISMATCHED_AUGMENTED above.
     @SuppressWarnings("unused")
-    private volatile ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> mismatchedAugmented = ImmutableMap.of();
+    @SuppressFBWarnings(value = "URF_UNREAD_FIELD", justification = "https://github.com/spotbugs/spotbugs/issues/2749")
+    private volatile ImmutableMap<Class<?>, CommonDataObjectCodecPrototype<?>> mismatchedAugmented = ImmutableMap.of();
 
-    DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype) {
+    DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype) {
         this(prototype, CodecItemFactory.of());
     }
 
-    DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype, final CodecItemFactory itemFactory) {
-        this(prototype, new CodecDataObjectAnalysis<>(prototype, itemFactory, null));
+    DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype, final CodecItemFactory itemFactory) {
+        this(prototype, new DataContainerAnalysis<>(prototype, itemFactory), null);
     }
 
-    DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype, final Method keyMethod) {
-        this(prototype, new CodecDataObjectAnalysis<>(prototype, CodecItemFactory.of(), keyMethod));
+    DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype, final Method keyMethod) {
+        this(prototype, new DataContainerAnalysis<>(prototype, CodecItemFactory.of()), keyMethod);
     }
 
-    private DataObjectCodecContext(final DataContainerCodecPrototype<T> prototype,
-            final CodecDataObjectAnalysis<T> analysis) {
-        super(prototype);
+    private DataObjectCodecContext(final CommonDataObjectCodecPrototype<T> prototype,
+            final DataContainerAnalysis<T> analysis, final Method keyMethod) {
+        super(prototype, analysis);
+
+        final var bindingClass = getBindingClass();
+
+        // Final bits: generate the appropriate class, As a side effect we identify what Augmentations are possible
+        final List<AugmentRuntimeType> possibleAugmentations;
+        final var loader = prototype().contextFactory().getLoader();
+        if (Augmentable.class.isAssignableFrom(bindingClass)) {
+            // Verify we have the appropriate backing runtimeType
+            final var runtimeType = prototype.runtimeType();
+            if (!(runtimeType instanceof AugmentableRuntimeType augmentableRuntimeType)) {
+                throw new VerifyException(
+                    "Unexpected type %s backing augmenable %s".formatted(runtimeType, bindingClass));
+            }
+
+            possibleAugmentations = augmentableRuntimeType.augments();
+            generatedClass = CodecDataObjectGenerator.generateAugmentable(loader, bindingClass, analysis.leafContexts,
+                analysis.daoProperties, keyMethod);
+        } else {
+            possibleAugmentations = List.of();
+            generatedClass = CodecDataObjectGenerator.generate(loader, bindingClass, analysis.leafContexts,
+                analysis.daoProperties, keyMethod);
+        }
+
+        // All done: acquire the constructor: it is supposed to be public
+        final MethodHandle ctor;
+        try {
+            ctor = MethodHandles.publicLookup().findConstructor(generatedClass, CONSTRUCTOR_TYPE);
+        } catch (NoSuchMethodException | IllegalAccessException e) {
+            throw new LinkageError("Failed to find contructor for class " + generatedClass, e);
+        }
 
-        // Inherit analysis stuff
-        leafChild = analysis.leafNodes;
-        proxyConstructor = analysis.proxyConstructor;
-        generatedClass = analysis.generatedClass;
-        byBindingArgClass = analysis.byBindingArgClass;
-        byStreamClass = analysis.byStreamClass;
-        byYang = analysis.byYang;
+        proxyConstructor = ctor.asType(DATAOBJECT_TYPE);
 
         // Deal with augmentations, which are not something we analysis provides
-        final var augByYang = new HashMap<PathArgument, DataContainerCodecPrototype<?>>();
-        final var augByStream = new HashMap<Class<?>, DataContainerCodecPrototype<?>>();
-        for (var augment : analysis.possibleAugmentations) {
+        final var augPathToBinding = new HashMap<NodeIdentifier, Class<?>>();
+        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype<?>>();
+        for (var augment : possibleAugmentations) {
             final var augProto = loadAugmentPrototype(augment);
             if (augProto != null) {
-                final var augYangArg = augProto.getYangArg();
-                if (augByYang.putIfAbsent(augYangArg, augProto) == null) {
-                    LOG.trace("Discovered new YANG mapping {} -> {} in {}", augYangArg, augProto, this);
-                }
-                final var augBindingClass = augProto.getBindingClass();
-                if (augByStream.putIfAbsent(augBindingClass, augProto) == null) {
-                    LOG.trace("Discovered new class mapping {} -> {} in {}", augBindingClass, augProto, this);
+                final var augBindingClass = augProto.javaClass();
+                for (var childPath : augProto.getChildArgs()) {
+                    augPathToBinding.putIfAbsent(childPath, augBindingClass);
                 }
+                augClassToProto.putIfAbsent(augBindingClass, augProto);
             }
         }
-        augmentationByYang = ImmutableMap.copyOf(augByYang);
-        augmentationByStream = ImmutableMap.copyOf(augByStream);
+        yangToAugmentClass = ImmutableMap.copyOf(augPathToBinding);
+        augmentToPrototype = ImmutableMap.copyOf(augClassToProto);
     }
 
     @Override
-    public final WithStatus getSchema() {
-        // FIXME: Bad cast, we should be returning an EffectiveStatement perhaps?
-        return (WithStatus) getType().statement();
+    final DataContainerPrototype<?, ?> pathChildPrototype(final Class<? extends DataObject> argType) {
+        final var child = super.pathChildPrototype(argType);
+        return child != null ? child : augmentToPrototype.get(argType);
     }
 
     @Override
-    @SuppressWarnings("unchecked")
-    public <C extends DataObject> DataContainerCodecContext<C, ?> streamChild(final Class<C> childClass) {
-        return (DataContainerCodecContext<C, ?>) childNonNull(streamChildPrototype(childClass), childClass,
-            "Child %s is not valid child of %s", getBindingClass(), childClass).get();
-    }
-
-    private DataContainerCodecPrototype<?> streamChildPrototype(final Class<?> childClass) {
-        final DataContainerCodecPrototype<?> childProto = byStreamClass.get(childClass);
-        if (childProto != null) {
-            return childProto;
-        }
-        if (Augmentation.class.isAssignableFrom(childClass)) {
-            return augmentationByClass(childClass);
+    final DataContainerPrototype<?, ?> streamChildPrototype(final Class<?> childClass) {
+        final var child = super.streamChildPrototype(childClass);
+        if (child == null && Augmentation.class.isAssignableFrom(childClass)) {
+            return getAugmentationProtoByClass(childClass);
         }
-        return null;
+        return child;
     }
 
-    @SuppressWarnings("unchecked")
     @Override
-    public <C extends DataObject> Optional<DataContainerCodecContext<C, ?>> possibleStreamChild(
-            final Class<C> childClass) {
-        final DataContainerCodecPrototype<?> childProto = streamChildPrototype(childClass);
-        if (childProto != null) {
-            return Optional.of((DataContainerCodecContext<C, ?>) childProto.get());
-        }
-        return Optional.empty();
-    }
-
-    @Override
-    public DataContainerCodecContext<?,?> bindingPathArgumentChild(final InstanceIdentifier.PathArgument arg,
-            final List<YangInstanceIdentifier.PathArgument> builder) {
-
-        final Class<? extends DataObject> argType = arg.getType();
-        DataContainerCodecPrototype<?> ctxProto = byBindingArgClass.get(argType);
-        if (ctxProto == null && Augmentation.class.isAssignableFrom(argType)) {
-            ctxProto = augmentationByClass(argType);
-        }
-        final DataContainerCodecContext<?, ?> context = childNonNull(ctxProto, argType,
-            "Class %s is not valid child of %s", argType, getBindingClass()).get();
-        if (context instanceof ChoiceNodeCodecContext<?> choice) {
-            choice.addYangPathArgument(arg, builder);
-
-            final Optional<? extends Class<? extends DataObject>> caseType = arg.getCaseType();
-            final Class<? extends DataObject> type = arg.getType();
-            final DataContainerCodecContext<?, ?> caze;
-            if (caseType.isPresent()) {
-                // Non-ambiguous addressing this should not pose any problems
-                caze = choice.streamChild(caseType.orElseThrow());
-            } else {
-                caze = choice.getCaseByChildClass(type);
+    final CodecContextSupplier yangChildSupplier(final NodeIdentifier arg) {
+        final var child = super.yangChildSupplier(arg);
+        if (child == null) {
+            final var augClass = yangToAugmentClass.get(arg);
+            if (augClass != null) {
+                return augmentToPrototype.get(augClass);
             }
-
-            caze.addYangPathArgument(arg, builder);
-            return caze.bindingPathArgumentChild(arg, builder);
-        }
-        context.addYangPathArgument(arg, builder);
-        return context;
-    }
-
-    @Override
-    public NodeCodecContext yangPathArgumentChild(final YangInstanceIdentifier.PathArgument arg) {
-        final NodeContextSupplier childSupplier;
-        if (arg instanceof NodeIdentifierWithPredicates) {
-            childSupplier = byYang.get(new NodeIdentifier(arg.getNodeType()));
-        } else if (arg instanceof AugmentationIdentifier) {
-            childSupplier = augmentationByYang.get(arg);
-        } else {
-            childSupplier = byYang.get(arg);
-        }
-
-        return childNonNull(childSupplier, arg, "Argument %s is not valid child of %s", arg, getSchema()).get();
-    }
-
-    protected final ValueNodeCodecContext getLeafChild(final String name) {
-        final ValueNodeCodecContext value = leafChild.get(name);
-        if (value == null) {
-            throw new IncorrectNestingException("Leaf %s is not valid for %s", name, getBindingClass());
         }
-        return value;
+        return child;
     }
 
-    private @Nullable DataContainerCodecPrototype<?> augmentationByClass(final @NonNull Class<?> childClass) {
-        final DataContainerCodecPrototype<?> childProto = augmentationByStream.get(childClass);
-        return childProto != null ? childProto : mismatchedAugmentationByClass(childClass);
+    private @Nullable AugmentationCodecPrototype getAugmentationProtoByClass(final @NonNull Class<?> augmClass) {
+        final var childProto = augmentToPrototype.get(augmClass);
+        return childProto != null ? childProto : mismatchedAugmentationByClass(augmClass);
     }
 
-    private @Nullable DataContainerCodecPrototype<?> mismatchedAugmentationByClass(final @NonNull Class<?> childClass) {
+    private @Nullable AugmentationCodecPrototype mismatchedAugmentationByClass(final @NonNull Class<?> childClass) {
         /*
          * It is potentially mismatched valid augmentation - we look up equivalent augmentation using reflection
          * and walk all stream child and compare augmentations classes if they are equivalent. When we find a match
          * we'll cache it so we do not need to perform reflection operations again.
          */
-        final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> local =
-                (ImmutableMap<Class<?>, DataContainerCodecPrototype<?>>) MISMATCHED_AUGMENTED.getAcquire(this);
-        final DataContainerCodecPrototype<?> mismatched = local.get(childClass);
+        final var local = (ImmutableMap<Class<?>, AugmentationCodecPrototype<?>>) MISMATCHED_AUGMENTED.getAcquire(this);
+        final var mismatched = local.get(childClass);
         return mismatched != null ? mismatched : loadMismatchedAugmentation(local, childClass);
-
     }
 
-    private @Nullable DataContainerCodecPrototype<?> loadMismatchedAugmentation(
-            final ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> oldMismatched,
+    private @Nullable AugmentationCodecPrototype<?> loadMismatchedAugmentation(
+            final ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
             final @NonNull Class<?> childClass) {
         @SuppressWarnings("rawtypes")
-        final Class<?> augTarget = BindingReflections.findAugmentationTarget((Class) childClass);
+        final Class<?> augTarget = findAugmentationTarget((Class) childClass);
         // Do not bother with proposals which are not augmentations of our class, or do not match what the runtime
         // context would load.
         if (getBindingClass().equals(augTarget) && belongsToRuntimeContext(childClass)) {
-            for (final DataContainerCodecPrototype<?> realChild : augmentationByStream.values()) {
-                if (Augmentation.class.isAssignableFrom(realChild.getBindingClass())
-                        && isSubstitutionFor(childClass, realChild.getBindingClass())) {
+            for (var realChild : augmentToPrototype.values()) {
+                final var realClass = realChild.javaClass();
+                if (Augmentation.class.isAssignableFrom(realClass) && isSubstitutionFor(childClass, realClass)) {
                     return cacheMismatched(oldMismatched, childClass, realChild);
                 }
             }
@@ -250,19 +211,18 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         return null;
     }
 
-    private @NonNull DataContainerCodecPrototype<?> cacheMismatched(
-            final @NonNull ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> oldMismatched,
-            final @NonNull Class<?> childClass, final @NonNull DataContainerCodecPrototype<?> prototype) {
-
-        ImmutableMap<Class<?>, DataContainerCodecPrototype<?>> expected = oldMismatched;
+    private @NonNull AugmentationCodecPrototype<?> cacheMismatched(
+            final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
+            final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype<?> prototype) {
+        var expected = oldMismatched;
         while (true) {
-            final Map<Class<?>, DataContainerCodecPrototype<?>> newMismatched =
-                    ImmutableMap.<Class<?>, DataContainerCodecPrototype<?>>builderWithExpectedSize(expected.size() + 1)
-                        .putAll(expected)
-                        .put(childClass, prototype)
-                        .build();
+            final var newMismatched =
+                ImmutableMap.<Class<?>, CommonDataObjectCodecPrototype<?>>builderWithExpectedSize(expected.size() + 1)
+                    .putAll(expected)
+                    .put(childClass, prototype)
+                    .build();
 
-            final var witness = (ImmutableMap<Class<?>, DataContainerCodecPrototype<?>>)
+            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype<?>>)
                 MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched);
             if (witness == expected) {
                 LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this);
@@ -270,7 +230,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
             }
 
             expected = witness;
-            final DataContainerCodecPrototype<?> existing = expected.get(childClass);
+            final var existing = expected.get(childClass);
             if (existing != null) {
                 LOG.trace("Using raced mismatched augmentation {} -> {} in {}", childClass, existing, this);
                 return existing;
@@ -279,7 +239,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
     }
 
     private boolean belongsToRuntimeContext(final Class<?> cls) {
-        final BindingRuntimeContext ctx = factory().getRuntimeContext();
+        final var ctx = prototype().contextFactory().getRuntimeContext();
         final Class<?> loaded;
         try {
             loaded = ctx.loadClass(Type.of(cls));
@@ -290,18 +250,19 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         return cls.equals(loaded);
     }
 
-    private @Nullable DataContainerCodecPrototype<?> loadAugmentPrototype(final AugmentRuntimeType augment) {
+    private @Nullable AugmentationCodecPrototype<?> loadAugmentPrototype(final AugmentRuntimeType augment) {
         // FIXME: in face of deviations this code should be looking at declared view, i.e. all possibilities at augment
         //        declaration site
-        final var possibleChildren = augment.statement()
+        final var childPaths = augment.statement()
             .streamEffectiveSubstatements(SchemaTreeEffectiveStatement.class)
-            .map(stmt -> (QName) stmt.argument())
+            .map(stmt -> new NodeIdentifier((QName) stmt.argument()))
             .collect(ImmutableSet.toImmutableSet());
-        if (possibleChildren.isEmpty()) {
+
+        if (childPaths.isEmpty()) {
             return null;
         }
 
-        final var factory = factory();
+        final var factory = prototype().contextFactory();
         final GeneratedType javaType = augment.javaType();
         final Class<? extends Augmentation<?>> augClass;
         try {
@@ -310,67 +271,80 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
             throw new IllegalStateException(
                 "RuntimeContext references type " + javaType + " but failed to load its class", e);
         }
-
-        return DataContainerCodecPrototype.from(augClass, new AugmentationIdentifier(possibleChildren), augment,
-            factory);
-    }
-
-    @SuppressWarnings("checkstyle:illegalCatch")
-    protected final @NonNull D createBindingProxy(final DistinctNodeContainer<?, ?> node) {
-        try {
-            return (D) proxyConstructor.invokeExact(this, node);
-        } catch (final Throwable e) {
-            Throwables.throwIfUnchecked(e);
-            throw new IllegalStateException(e);
-        }
+        return new AugmentationCodecPrototype<>(augClass, augment, factory, childPaths);
     }
 
+    @Override
     @SuppressWarnings("unchecked")
-    Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
-            final DistinctNodeContainer<PathArgument, NormalizedNode> data) {
-
-        @SuppressWarnings("rawtypes")
-        final Map map = new HashMap<>();
-
-        for (final NormalizedNode childValue : data.body()) {
-            if (childValue instanceof AugmentationNode augDomNode) {
-                final DataContainerCodecPrototype<?> codecProto = augmentationByYang.get(augDomNode.getIdentifier());
-                if (codecProto != null) {
-                    final DataContainerCodecContext<?, ?> codec = codecProto.get();
-                    map.put(codec.getBindingClass(), codec.deserializeObject(augDomNode));
-                }
+    Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(final DataContainerNode data) {
+        /**
+         * Due to augmentation fields are at same level as direct children the data of each augmentation needs to be
+         * aggregated into own container node, then only deserialized using associated prototype.
+         */
+        final var builders = new HashMap<Class<?>, DataContainerNodeBuilder>();
+        for (var childValue : data.body()) {
+            final var bindingClass = yangToAugmentClass.get(childValue.name());
+            if (bindingClass != null) {
+                builders.computeIfAbsent(bindingClass,
+                    key -> Builders.containerBuilder()
+                        .withNodeIdentifier(new NodeIdentifier(data.name().getNodeType())))
+                        .addChild(childValue);
             }
         }
-        for (final DataContainerCodecPrototype<?> value : augmentationByStream.values()) {
-            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)
-                && ((AugmentableRuntimeType) 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));
+        @SuppressWarnings("rawtypes")
+        final var map = new HashMap();
+        for (final var entry : builders.entrySet()) {
+            final var bindingClass = entry.getKey();
+            final var codecProto = augmentToPrototype.get(bindingClass);
+            if (codecProto != null) {
+                final var bindingObj = codecProto.getCodecContext().deserializeObject(entry.getValue().build());
+                if (bindingObj != null) {
+                    map.put(bindingClass, bindingObj);
                 }
             }
         }
         return map;
     }
 
-    final @NonNull Class<? extends CodecDataObject<?>> generatedClass() {
-        return generatedClass;
-    }
-
     @Override
-    public InstanceIdentifier.PathArgument deserializePathArgument(final YangInstanceIdentifier.PathArgument arg) {
+    public DataObjectStep<?> deserializePathArgument(final PathArgument arg) {
         checkArgument(getDomPathArgument().equals(arg));
         return bindingArg();
     }
 
     @Override
-    public YangInstanceIdentifier.PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
-        checkArgument(bindingArg().equals(arg));
+    public PathArgument serializePathArgument(final DataObjectStep<?> step) {
+        checkArgument(bindingArg().equals(step));
         return getDomPathArgument();
     }
 
+    @Override
+    public NormalizedNode serialize(final D data) {
+        return serializeImpl(data);
+    }
+
+    @Override
+    public final BindingNormalizedNodeCachingCodec<D> createCachingCodec(
+            final ImmutableCollection<Class<? extends BindingObject>> cacheSpecifier) {
+        return createCachingCodec(this, cacheSpecifier);
+    }
+
+    final @NonNull Class<? extends CodecDataObject<?>> generatedClass() {
+        return generatedClass;
+    }
+
+    @SuppressWarnings("checkstyle:illegalCatch")
+    final @NonNull D createBindingProxy(final DataContainerNode node) {
+        try {
+            return (D) proxyConstructor.invokeExact(this, node);
+        } catch (final Throwable e) {
+            Throwables.throwIfUnchecked(e);
+            throw new IllegalStateException(e);
+        }
+    }
+
+    @Override
+    Object deserializeObject(final NormalizedNode normalizedNode) {
+        return deserialize(normalizedNode);
+    }
 }