Rename ContainerNodeCodecContext
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / impl / DataObjectCodecContext.java
index 44404ad34649364dd08be9260f6bcb498ab9ed6e..4c9b0c5a8d908ef76b843547fc9df6117d6c6111 100644 (file)
@@ -27,7 +27,6 @@ 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.CompositeRuntimeType;
-import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BindingObject;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -35,7 +34,7 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 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;
-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.data.api.schema.builder.DataContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
@@ -47,8 +46,9 @@ 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 AbstractDataObjectCodecContext<D, T> implements BindingDataObjectCodecTreeNode<D> {
+public abstract sealed class DataObjectCodecContext<D extends DataObject, T extends CompositeRuntimeType>
+        extends AbstractDataObjectCodecContext<D, T> implements BindingDataObjectCodecTreeNode<D>
+        permits CaseNodeCodecContext, ContainerLikeCodecContext, ListNodeCodecContext, NotificationCodecContext {
     private static final Logger LOG = LoggerFactory.getLogger(DataObjectCodecContext.class);
 
     private static final VarHandle MISMATCHED_AUGMENTED;
@@ -62,8 +62,8 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         }
     }
 
-    private final ImmutableMap<Class<?>, DataContainerCodecPrototype.Augmentation> augmentToPrototype;
-    private final ImmutableMap<PathArgument, Class<?>> yangToAugmentClass;
+    private final ImmutableMap<Class<?>, AugmentationCodecPrototype> augmentToPrototype;
+    private final ImmutableMap<NodeIdentifier, Class<?>> yangToAugmentClass;
     private final @NonNull Class<? extends CodecDataObject<?>> generatedClass;
 
     // Note this the content of this field depends only of invariants expressed as this class's fields or
@@ -91,8 +91,8 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         generatedClass = analysis.generatedClass;
 
         // Deal with augmentations, which are not something we analysis provides
-        final var augPathToBinding = new HashMap<PathArgument, Class<?>>();
-        final var augClassToProto = new HashMap<Class<?>, DataContainerCodecPrototype.Augmentation>();
+        final var augPathToBinding = new HashMap<NodeIdentifier, Class<?>>();
+        final var augClassToProto = new HashMap<Class<?>, AugmentationCodecPrototype>();
         for (var augment : analysis.possibleAugmentations) {
             final var augProto = loadAugmentPrototype(augment);
             if (augProto != null) {
@@ -123,7 +123,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
     }
 
     @Override
-    final NodeContextSupplier yangChildSupplier(final PathArgument arg) {
+    final CodecContextSupplier yangChildSupplier(final NodeIdentifier arg) {
         final var child = super.yangChildSupplier(arg);
         if (child == null) {
             final var augClass = yangToAugmentClass.get(arg);
@@ -134,30 +134,27 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         return child;
     }
 
-    private DataContainerCodecPrototype.@Nullable Augmentation getAugmentationProtoByClass(
-            final @NonNull Class<?> augmClass) {
+    private @Nullable AugmentationCodecPrototype getAugmentationProtoByClass(final @NonNull Class<?> augmClass) {
         final var childProto = augmentToPrototype.get(augmClass);
         return childProto != null ? childProto : mismatchedAugmentationByClass(augmClass);
     }
 
-    private DataContainerCodecPrototype.@Nullable Augmentation 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 var local =
-            (ImmutableMap<Class<?>, DataContainerCodecPrototype.Augmentation>) 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 DataContainerCodecPrototype.@Nullable Augmentation loadMismatchedAugmentation(
-            final ImmutableMap<Class<?>, DataContainerCodecPrototype.Augmentation> 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)) {
@@ -172,10 +169,9 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         return null;
     }
 
-    private DataContainerCodecPrototype.@NonNull Augmentation cacheMismatched(
-            final @NonNull ImmutableMap<Class<?>, DataContainerCodecPrototype.Augmentation> oldMismatched,
-            final @NonNull Class<?> childClass, final DataContainerCodecPrototype.@NonNull Augmentation 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 =
@@ -184,7 +180,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
                     .put(childClass, prototype)
                     .build();
 
-            final var witness = (ImmutableMap<Class<?>, DataContainerCodecPrototype.Augmentation>)
+            final var witness = (ImmutableMap<Class<?>, AugmentationCodecPrototype>)
                 MISMATCHED_AUGMENTED.compareAndExchangeRelease(this, expected, newMismatched);
             if (witness == expected) {
                 LOG.trace("Cached mismatched augmentation {} -> {} in {}", childClass, prototype, this);
@@ -212,7 +208,7 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
         return cls.equals(loaded);
     }
 
-    private DataContainerCodecPrototype.@Nullable Augmentation 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()
@@ -235,24 +231,23 @@ 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 new DataContainerCodecPrototype.Augmentation(augClass, namespace, augment, factory, childPaths);
+        return new AugmentationCodecPrototype(augClass, namespace, augment, factory, childPaths);
     }
 
     @Override
     @SuppressWarnings("unchecked")
-    Map<Class<? extends Augmentation<?>>, Augmentation<?>> getAllAugmentationsFrom(
-            final DistinctNodeContainer<PathArgument, NormalizedNode> data) {
+    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.getIdentifier());
+            final var bindingClass = yangToAugmentClass.get(childValue.name());
             if (bindingClass != null) {
                 builders.computeIfAbsent(bindingClass,
                     key -> Builders.containerBuilder()
-                        .withNodeIdentifier(new NodeIdentifier(data.getIdentifier().getNodeType())))
+                        .withNodeIdentifier(new NodeIdentifier(data.name().getNodeType())))
                         .addChild(childValue);
             }
         }
@@ -262,7 +257,10 @@ public abstract class DataObjectCodecContext<D extends DataObject, T extends Com
             final var bindingClass = entry.getKey();
             final var codecProto = augmentToPrototype.get(bindingClass);
             if (codecProto != null) {
-                map.put(bindingClass, codecProto.get().deserializeObject(entry.getValue().build()));
+                final var bindingObj = codecProto.get().deserializeObject(entry.getValue().build());
+                if (bindingObj != null) {
+                    map.put(bindingClass, bindingObj);
+                }
             }
         }
         return map;