Add BindingNormalizedNodeSerializer.coerceFromYangInstanceIdentifier 23/79623/1
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 17 Jan 2019 16:18:21 +0000 (17:18 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 17 Jan 2019 16:18:21 +0000 (17:18 +0100)
In application-level adapter, which are bound to particular models,
the nullable annotation gets in the way, as it flags a potential
error, which needs to be expensively masked.

Introduce a coerceFromYangInstanceIdentifier() method, which includes
the masking, leaving application code unburdened with this detail.

Change-Id: I0b6e052b6b9be7c42538ab25924fd47b0421b274
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/BindingNormalizedNodeSerializer.java
entityownership/mdsal-eos-binding-adapter/src/main/java/org/opendaylight/mdsal/eos/binding/dom/adapter/DOMEntityOwnershipListenerAdapter.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();
index f04365ddbfa8d012d756b69edb93eb7552d44a6e..97804490bdfdc50a02050c77dd7755f0384fc736 100644 (file)
@@ -7,7 +7,6 @@
  */
 package org.opendaylight.mdsal.eos.binding.dom.adapter;
 
-import static com.google.common.base.Verify.verifyNotNull;
 import static java.util.Objects.requireNonNull;
 
 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
@@ -48,7 +47,7 @@ class DOMEntityOwnershipListenerAdapter implements DOMEntityOwnershipListener {
         final YangInstanceIdentifier domId = domEntity.getIdentifier();
         final InstanceIdentifier<?> bindingId;
         try {
-            bindingId = verifyNotNull(conversionCodec.fromYangInstanceIdentifier(domId));
+            bindingId = conversionCodec.coerceFromYangInstanceIdentifier(domId);
         } catch (RuntimeException e) {
             LOG.error("Error converting DOM entity ID {} to binding InstanceIdentifier", domId, e);
             return;