Hide CodecContext methods
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecContext.java
index f076958cf33c1652e0161c21d59fcd563553ce89..d99da25ddc04f21dd914376af14c9089ea26dcc5 100644 (file)
@@ -32,13 +32,12 @@ 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.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.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
@@ -105,21 +104,22 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
 
         // 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.getType();
+            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(factory().getLoader(), bindingClass,
-                analysis.leafContexts, analysis.daoProperties, keyMethod);
+            generatedClass = CodecDataObjectGenerator.generateAugmentable(loader, bindingClass, analysis.leafContexts,
+                analysis.daoProperties, keyMethod);
         } else {
             possibleAugmentations = List.of();
-            generatedClass = CodecDataObjectGenerator.generate(factory().getLoader(), bindingClass,
-                analysis.leafContexts, analysis.daoProperties, keyMethod);
+            generatedClass = CodecDataObjectGenerator.generate(loader, bindingClass, analysis.leafContexts,
+                analysis.daoProperties, keyMethod);
         }
 
         // All done: acquire the constructor: it is supposed to be public
@@ -134,11 +134,11 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
 
         // Deal with augmentations, which are not something we analysis provides
         final var augPathToBinding = new HashMap<NodeIdentifier, Class<?>>();
-        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype>();
+        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype<?>>();
         for (var augment : possibleAugmentations) {
             final var augProto = loadAugmentPrototype(augment);
             if (augProto != null) {
-                final var augBindingClass = augProto.getBindingClass();
+                final var augBindingClass = augProto.javaClass();
                 for (var childPath : augProto.getChildArgs()) {
                     augPathToBinding.putIfAbsent(childPath, augBindingClass);
                 }
@@ -150,13 +150,13 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
     }
 
     @Override
-    final CommonDataObjectCodecPrototype<?> pathChildPrototype(final Class<? extends DataObject> argType) {
+    final DataContainerPrototype<?, ?> pathChildPrototype(final Class<? extends DataObject> argType) {
         final var child = super.pathChildPrototype(argType);
         return child != null ? child : augmentToPrototype.get(argType);
     }
 
     @Override
-    final CommonDataObjectCodecPrototype<?> streamChildPrototype(final Class<?> childClass) {
+    final DataContainerPrototype<?, ?> streamChildPrototype(final Class<?> childClass) {
         final var child = super.streamChildPrototype(childClass);
         if (child == null && Augmentation.class.isAssignableFrom(childClass)) {
             return getAugmentationProtoByClass(childClass);
@@ -187,13 +187,13 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
          * 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 var local = (ImmutableMap<Class<?>, AugmentationCodecPrototype>) MISMATCHED_AUGMENTED.getAcquire(this);
+        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 AugmentationCodecPrototype loadMismatchedAugmentation(
-            final ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
+    private @Nullable AugmentationCodecPrototype<?> loadMismatchedAugmentation(
+            final ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
             final @NonNull Class<?> childClass) {
         @SuppressWarnings("rawtypes")
         final Class<?> augTarget = findAugmentationTarget((Class) childClass);
@@ -201,8 +201,8 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
         // context would load.
         if (getBindingClass().equals(augTarget) && belongsToRuntimeContext(childClass)) {
             for (var realChild : augmentToPrototype.values()) {
-                if (Augmentation.class.isAssignableFrom(realChild.getBindingClass())
-                        && isSubstitutionFor(childClass, realChild.getBindingClass())) {
+                final var realClass = realChild.javaClass();
+                if (Augmentation.class.isAssignableFrom(realClass) && isSubstitutionFor(childClass, realClass)) {
                     return cacheMismatched(oldMismatched, childClass, realChild);
                 }
             }
@@ -211,9 +211,9 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
         return null;
     }
 
-    private @NonNull AugmentationCodecPrototype cacheMismatched(
-            final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype> oldMismatched,
-            final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype prototype) {
+    private @NonNull AugmentationCodecPrototype<?> cacheMismatched(
+            final @NonNull ImmutableMap<Class<?>, AugmentationCodecPrototype<?>> oldMismatched,
+            final @NonNull Class<?> childClass, final @NonNull AugmentationCodecPrototype<?> prototype) {
         var expected = oldMismatched;
         while (true) {
             final var newMismatched =
@@ -222,7 +222,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
                     .put(childClass, prototype)
                     .build();
 
-            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype>)
+            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype<?>>)
                 MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched);
             if (witness == expected) {
                 LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this);
@@ -239,7 +239,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
     }
 
     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));
@@ -250,7 +250,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
         return cls.equals(loaded);
     }
 
-    private @Nullable AugmentationCodecPrototype 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 childPaths = augment.statement()
@@ -262,7 +262,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
             return null;
         }
 
-        final var factory = factory();
+        final var factory = prototype().contextFactory();
         final GeneratedType javaType = augment.javaType();
         final Class<? extends Augmentation<?>> augClass;
         try {
@@ -271,7 +271,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
             throw new IllegalStateException(
                 "RuntimeContext references type " + javaType + " but failed to load its class", e);
         }
-        return new AugmentationCodecPrototype(augClass, augment, factory, childPaths);
+        return new AugmentationCodecPrototype<>(augClass, augment, factory, childPaths);
     }
 
     @Override
@@ -297,7 +297,7 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
             final var bindingClass = entry.getKey();
             final var codecProto = augmentToPrototype.get(bindingClass);
             if (codecProto != null) {
-                final var bindingObj = codecProto.get().deserializeObject(entry.getValue().build());
+                final var bindingObj = codecProto.getCodecContext().deserializeObject(entry.getValue().build());
                 if (bindingObj != null) {
                     map.put(bindingClass, bindingObj);
                 }
@@ -307,14 +307,14 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
     }
 
     @Override
-    public InstanceIdentifier.PathArgument deserializePathArgument(final PathArgument arg) {
+    public DataObjectStep<?> deserializePathArgument(final PathArgument arg) {
         checkArgument(getDomPathArgument().equals(arg));
         return bindingArg();
     }
 
     @Override
-    public PathArgument serializePathArgument(final InstanceIdentifier.PathArgument arg) {
-        checkArgument(bindingArg().equals(arg));
+    public PathArgument serializePathArgument(final DataObjectStep<?> step) {
+        checkArgument(bindingArg().equals(step));
         return getDomPathArgument();
     }
 
@@ -342,4 +342,9 @@ public abstract sealed class DataObjectCodecContext<D extends DataObject, T exte
             throw new IllegalStateException(e);
         }
     }
+
+    @Override
+    Object deserializeObject(final NormalizedNode normalizedNode) {
+        return deserialize(normalizedNode);
+    }
 }