Add BindingNormalizedNodeSerializer.coerceFromYangInstanceIdentifier
[mdsal.git] / binding / mdsal-binding-dom-codec / src / main / java / org / opendaylight / mdsal / binding / dom / codec / api / BindingNormalizedNodeSerializer.java
index eb6e416645e36298048979674a709e0f9fd2f9cb..1f2287b2c5a5c976387d40aacd8d7cf5fcaee012 100644 (file)
@@ -7,7 +7,10 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.api;
 
+import static com.google.common.base.Verify.verifyNotNull;
+
 import com.google.common.annotations.Beta;
+import com.google.common.base.VerifyException;
 import java.util.Map.Entry;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
@@ -41,7 +44,9 @@ public interface BindingNormalizedNodeSerializer {
     YangInstanceIdentifier toYangInstanceIdentifier(@NonNull InstanceIdentifier<?> binding);
 
     /**
-     * Translates supplied YANG Instance Identifier into Binding instance identifier.
+     * Translates supplied YANG Instance Identifier into Binding instance identifier. This method can return null
+     * if the instance identifier is not representable in Binding. If it is statically known this is not the case,
+     * use {@link #coerceFromYangInstanceIdentifier(YangInstanceIdentifier)}.
      *
      * @param dom YANG Instance Identifier
      * @return Binding Instance Identifier, or null if the instance identifier is not representable.
@@ -49,6 +54,19 @@ public interface BindingNormalizedNodeSerializer {
     <T extends DataObject> @Nullable InstanceIdentifier<T> fromYangInstanceIdentifier(
             @NonNull YangInstanceIdentifier dom);
 
+    /**
+     * Translates supplied YANG Instance Identifier into Binding instance identifier.
+     *
+     * @param dom YANG Instance Identifier
+     * @return Binding Instance Identifier
+     * @throws VerifyException if the instance identifier is not representable.
+     */
+    @Beta
+    default <T extends DataObject> @NonNull InstanceIdentifier<T> coerceFromYangInstanceIdentifier(
+            final @NonNull YangInstanceIdentifier dom) {
+        return verifyNotNull(fromYangInstanceIdentifier(dom), "%s is not representable", dom);
+    }
+
     /**
      * Translates supplied Binding Instance Identifier and data into NormalizedNode representation.
      *
@@ -149,8 +167,9 @@ public interface BindingNormalizedNodeSerializer {
      * @return NormalizedNode representation of action input
      * @throws NullPointerException if any of the arguments is null
      */
-    @Beta default @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
-            @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcInput input) {
+    @Beta
+    default @NonNull BindingLazyContainerNode<RpcInput> toLazyNormalizedNodeActionInput(
+            final @NonNull Class<? extends Action<?, ?, ?>> action, final @NonNull RpcInput input) {
         return toLazyNormalizedNodeActionInput(action,
             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input);
     }
@@ -163,8 +182,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) {
+    @Beta
+    default @NonNull ContainerNode toNormalizedNodeActionInput(final @NonNull Class<? extends Action<?, ?, ?>> action,
+            @NonNull final RpcInput input) {
         return toLazyNormalizedNodeActionInput(action,
             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), input)
                 .getDelegate();
@@ -189,8 +209,9 @@ public interface BindingNormalizedNodeSerializer {
      * @param output Binding action output
      * @return NormalizedNode representation of action output
      */
-    @Beta default @NonNull BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
-            @NonNull final Class<? extends Action<?, ?, ?>> action, @NonNull final RpcOutput output) {
+    @Beta
+    default @NonNull BindingLazyContainerNode<RpcOutput> toLazyNormalizedNodeActionOutput(
+            final @NonNull Class<? extends Action<?, ?, ?>> action, final @NonNull RpcOutput output) {
         return toLazyNormalizedNodeActionOutput(action,
             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output);
     }
@@ -201,8 +222,9 @@ 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) {
+    @Beta
+    default @NonNull ContainerNode toNormalizedNodeActionOutput(final @NonNull Class<? extends Action<?, ?, ?>> action,
+            final @NonNull RpcOutput output) {
         return toLazyNormalizedNodeActionOutput(action,
             new NodeIdentifier(YangConstants.operationInputQName(BindingReflections.getQNameModule(action))), output)
                 .getDelegate();