Reduce use of BindingReflections in BindingNormalizedNodeSerializer
[yangtools.git] / binding / mdsal-binding-dom-codec-api / src / main / java / org / opendaylight / mdsal / binding / dom / codec / api / BindingNormalizedNodeSerializer.java
index 773b1778717cf414193ecfa28b369517fa0fa453..a3ce9deeaae14b7d57ef357d84af3c2c86811eed 100644 (file)
@@ -7,13 +7,18 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.api;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.Beta;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
 import java.time.Instant;
 import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
 import org.opendaylight.yangtools.yang.binding.Action;
+import org.opendaylight.yangtools.yang.binding.Augmentation;
 import org.opendaylight.yangtools.yang.binding.BaseNotification;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -25,14 +30,54 @@ import org.opendaylight.yangtools.yang.common.YangConstants;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
 
 /**
- * Serialization service, which provides two-way serialization between Java
- * Binding Data representation and NormalizedNode representation.
+ * Serialization service, which provides two-way serialization between Java Binding Data representation and
+ * NormalizedNode representation.
  */
 public interface BindingNormalizedNodeSerializer {
+    /**
+     * Result of a {@link BindingNormalizedNodeSerializer#toNormalizedNode(InstanceIdentifier, DataObject)}. Since the
+     * Binding {@link Augmentation} does not have an exact equivalent, there are two specializations of this class:
+     * {@link NodeResult} and {@link AugmentationResult}.
+     */
+    sealed interface NormalizedResult {
+        /**
+         * Return the {@link YangInstanceIdentifier} path of this result.
+         *
+         * @return A {@link YangInstanceIdentifier}
+         */
+        @NonNull YangInstanceIdentifier path();
+    }
+
+    /**
+     * A {@link NormalizedResult} for an {@link Augmentation}.
+     *
+     * @param path A YangInstanceIdentifier identifying the parent of this augmentation
+     * @param possibleChildren {@link NodeIdentifier}s of each possible child
+     * @param children Augmentation children
+     */
+    record AugmentationResult(
+            @NonNull YangInstanceIdentifier path,
+            @NonNull ImmutableSet<NodeIdentifier> possibleChildren,
+            @NonNull ImmutableList<DataContainerChild> children) implements NormalizedResult {
+        public AugmentationResult {
+            requireNonNull(path);
+            requireNonNull(possibleChildren);
+            requireNonNull(children);
+        }
+    }
+
+    record NodeResult(@NonNull YangInstanceIdentifier path, @NonNull NormalizedNode node) implements NormalizedResult {
+        public NodeResult {
+            requireNonNull(path);
+            requireNonNull(node);
+        }
+    }
+
     /**
      * Translates supplied Binding Instance Identifier into NormalizedNode instance identifier.
      *
@@ -58,11 +103,31 @@ public interface BindingNormalizedNodeSerializer {
      *
      * @param path Binding Instance Identifier pointing to data
      * @param data Data object representing data
-     * @return NormalizedNode representation
+     * @return {@link NormalizedResult} representation
      * @throws IllegalArgumentException If supplied Instance Identifier is not valid.
      */
-    <T extends DataObject> @NonNull Entry<YangInstanceIdentifier, NormalizedNode> toNormalizedNode(
-            InstanceIdentifier<T> path, T data);
+    <T extends DataObject> @NonNull NormalizedResult toNormalizedNode(InstanceIdentifier<T> path, T data);
+
+    /**
+     * Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
+     *
+     * @param path Binding Instance Identifier pointing to data
+     * @param data Data object representing data
+     * @return {@link NormalizedResult} representation
+     * @throws IllegalArgumentException If supplied Instance Identifier is not valid.
+     */
+    <A extends Augmentation<?>> @NonNull AugmentationResult toNormalizedAugmentation(InstanceIdentifier<A> path,
+        A data);
+
+    /**
+     * Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
+     *
+     * @param path Binding Instance Identifier pointing to data
+     * @param data Data object representing data
+     * @return {@link NormalizedResult} representation
+     * @throws IllegalArgumentException If supplied Instance Identifier is not valid.
+     */
+    <T extends DataObject> @NonNull NodeResult toNormalizedDataObject(InstanceIdentifier<T> path, T data);
 
     /**
      * Translates supplied YANG Instance Identifier and NormalizedNode into Binding data.
@@ -175,7 +240,8 @@ public interface BindingNormalizedNodeSerializer {
      * @return NormalizedNode representation of action input
      * @throws NullPointerException if any of the arguments is null
      */
-    @Beta default @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
+    @Beta
+    default @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcInput input) {
         return toLazyNormalizedNodeActionInput(action,
             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input);
@@ -189,12 +255,9 @@ public interface BindingNormalizedNodeSerializer {
      * @return NormalizedNode representation of action input
      * @throws NullPointerException if any of the arguments is null
      */
-    @Beta default @NonNull ContainerNode toNormalizedNodeActionInput(
-            @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcInput input) {
-        return toLazyNormalizedNodeActionInput(action,
-            new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input)
-                .getDelegate();
-    }
+    @Beta
+    @NonNull ContainerNode toNormalizedNodeActionInput(@NonNull Class<? extends Action<?, ?, ?>> action,
+        @NonNull RpcInput input);
 
     /**
      * Lazily translates supplied Binding action output into NormalizedNode data.
@@ -218,7 +281,7 @@ public interface BindingNormalizedNodeSerializer {
     @Beta default @NonNull BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
             @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcOutput output) {
         return toLazyNormalizedNodeActionOutput(action,
-            new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output);
+            new NodeIdentifier(YangConstants.operationOutputQName(BindingReflections.getQNameModule(action))), output);
     }
 
     /**
@@ -227,10 +290,7 @@ public interface BindingNormalizedNodeSerializer {
      * @param output Binding action output
      * @return NormalizedNode representation of action output
      */
-    @Beta default @NonNull ContainerNode toNormalizedNodeActionOutput(
-            @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcOutput output) {
-        return toLazyNormalizedNodeActionOutput(action,
-            new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output)
-                .getDelegate();
-    }
+    @Beta
+    @NonNull ContainerNode toNormalizedNodeActionOutput(@NonNull Class<? extends Action<?, ?, ?>> action,
+        @NonNull RpcOutput output);
 }