Expose BindingInstanceIdentifierCodec from BindingCodecTree 43/93343/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 29 Feb 2020 14:38:05 +0000 (15:38 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 25 Oct 2020 12:40:50 +0000 (13:40 +0100)
This codec is in similar class as IdentityCodec -- make sure it is
available to users.

JIRA: MDSAL-522
JIRA: MDSAL-525
Change-Id: I08371dfbbb264991d6d5840838c0736b35921b8f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 2f25664c714829303174a18fbd960d07f101507e)

binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/BindingCodecTree.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/BindingInstanceIdentifierCodec.java [new file with mode: 0644]
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingCodecContext.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/BindingNormalizedNodeCodecRegistry.java
binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/impl/InstanceIdentifierCodec.java
trace/mdsal-trace-impl/src/main/resources/org/opendaylight/blueprint/impl-blueprint.xml

index bb450cf6751058e3cf1565d379f63ab782824be3..f20a579c8d7e7e633b7510cfff227f68b2fb3a5a 100644 (file)
@@ -30,10 +30,18 @@ public interface BindingCodecTree {
     @Nullable BindingCodecTreeNode getSubtreeCodec(SchemaPath path);
 
     /**
-     * Get the BindingIdentityCodec associated with this tree.
+     * Get the {@link BindingIdentityCodec} associated with this tree.
      *
      * @return A BindingIdentityCodec instance.
      */
     @Beta
     @NonNull BindingIdentityCodec getIdentityCodec();
+
+    /**
+     * Get the {@link BindingInstanceIdentifierCodec} associated with this tree.
+     *
+     * @return A BindingInstanceIdentifierCodec instance.
+     */
+    @Beta
+    @NonNull BindingInstanceIdentifierCodec getInstanceIdentifierCodec();
 }
diff --git a/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/BindingInstanceIdentifierCodec.java b/binding/mdsal-binding-dom-codec/src/main/java/org/opendaylight/mdsal/binding/dom/codec/api/BindingInstanceIdentifierCodec.java
new file mode 100644 (file)
index 0000000..4337f34
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.dom.codec.api;
+
+import com.google.common.annotations.Beta;
+import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+
+@Beta
+public interface BindingInstanceIdentifierCodec extends Immutable {
+    /**
+     * Translates supplied {@link YangInstanceIdentifier} into an {@link InstanceIdentifier}, if possible.
+     *
+     * @param domPath YANG Instance Identifier
+     * @return Binding Instance Identifier, or null if the instance identifier is not representable.
+     * @throws NullPointerException if domPath is null
+     */
+    <T extends DataObject> @Nullable InstanceIdentifier<T> toBinding(@NonNull YangInstanceIdentifier domPath);
+
+    /**
+     * Translates supplied {@link InstanceIdentifier} into {@link YangInstanceIdentifier}.
+     *
+     * @param bindingPath Binding Instance Identifier
+     * @return DOM Instance Identifier
+     * @throws NullPointerException if bindingPath is null
+     * @throws IllegalArgumentException if bindingPath is not valid.
+     */
+    @NonNull YangInstanceIdentifier fromBinding(@NonNull InstanceIdentifier<?> bindingPath);
+}
index 1db23307a8ea2139dccaddfc350555967afee768..332262e978589f18ee58da8dd39f163c13058198 100644 (file)
@@ -36,6 +36,7 @@ import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTree;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingCodecTreeNode;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingStreamEventWriter;
 import org.opendaylight.mdsal.binding.dom.codec.impl.NodeCodecContext.CodecContextFactory;
 import org.opendaylight.mdsal.binding.dom.codec.loader.CodecClassLoader;
@@ -117,7 +118,7 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         });
 
     private final @NonNull CodecClassLoader loader = CodecClassLoader.create();
-    private final InstanceIdentifierCodec instanceIdentifierCodec;
+    private final @NonNull InstanceIdentifierCodec instanceIdentifierCodec;
     private final @NonNull IdentityCodec identityCodec;
     private final BindingNormalizedNodeCodecRegistry registry;
     private final BindingRuntimeContext context;
@@ -141,16 +142,17 @@ final class BindingCodecContext implements CodecContextFactory, BindingCodecTree
         return loader;
     }
 
-    InstanceIdentifierCodec getInstanceIdentifierCodec() {
-        return instanceIdentifierCodec;
-    }
-
     @Override
     public IdentityCodec getIdentityCodec() {
         return identityCodec;
     }
 
     @SuppressWarnings({"rawtypes", "unchecked"})
+    @Override
+    public BindingInstanceIdentifierCodec getInstanceIdentifierCodec() {
+        return instanceIdentifierCodec;
+    }
+
     @Override
     public DataObjectSerializer getEventStreamSerializer(final Class<?> type) {
         return registry.getSerializer((Class) type);
index 30aea995cba51d00824d18d1f68944f737c04947..0c912fe8f0fc3472fe7e53c9da8bac9b53dde13d 100644 (file)
@@ -109,12 +109,12 @@ public class BindingNormalizedNodeCodecRegistry implements DataObjectSerializerR
 
     @Override
     public YangInstanceIdentifier toYangInstanceIdentifier(final InstanceIdentifier<?> binding) {
-        return codecContext.getInstanceIdentifierCodec().serialize(binding);
+        return codecContext.getInstanceIdentifierCodec().fromBinding(binding);
     }
 
     @Override
-    public InstanceIdentifier<?> fromYangInstanceIdentifier(final YangInstanceIdentifier dom) {
-        return codecContext.getInstanceIdentifierCodec().deserialize(dom);
+    public <T extends DataObject> InstanceIdentifier<T> fromYangInstanceIdentifier(final YangInstanceIdentifier dom) {
+        return codecContext.getInstanceIdentifierCodec().toBinding(dom);
     }
 
     @Override
index e182526e5abed009eae9e6af0f3a7fa0c3d032e8..2138c777bddab94602ac5e9e8a5698d0279f700a 100644 (file)
@@ -12,14 +12,18 @@ import static java.util.Objects.requireNonNull;
 import com.google.common.collect.Iterables;
 import java.util.ArrayList;
 import java.util.List;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.binding.dom.codec.api.BindingDataObjectCodecTreeNode;
+import org.opendaylight.mdsal.binding.dom.codec.api.BindingInstanceIdentifierCodec;
 import org.opendaylight.yangtools.concepts.IllegalArgumentCodec;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 
-// FIXME: this is not really an IllegalArgumentCodec, as it can legally return null from deserialize()
-final class InstanceIdentifierCodec implements IllegalArgumentCodec<YangInstanceIdentifier, InstanceIdentifier<?>> {
+final class InstanceIdentifierCodec implements BindingInstanceIdentifierCodec,
+        //FIXME: this is not really an IllegalArgumentCodec, as it can legally return null from deserialize()
+        IllegalArgumentCodec<YangInstanceIdentifier, InstanceIdentifier<?>> {
     private final BindingCodecContext context;
 
     InstanceIdentifierCodec(final BindingCodecContext context) {
@@ -27,16 +31,9 @@ final class InstanceIdentifierCodec implements IllegalArgumentCodec<YangInstance
     }
 
     @Override
-    public YangInstanceIdentifier serialize(final InstanceIdentifier<?> input) {
-        final List<PathArgument> domArgs = new ArrayList<>();
-        context.getCodecContextNode(input, domArgs);
-        return YangInstanceIdentifier.create(domArgs);
-    }
-
-    @Override
-    public InstanceIdentifier<?> deserialize(final YangInstanceIdentifier input) {
+    public <T extends DataObject> InstanceIdentifier<T> toBinding(final YangInstanceIdentifier domPath) {
         final List<InstanceIdentifier.PathArgument> builder = new ArrayList<>();
-        final BindingDataObjectCodecTreeNode<?> codec = context.getCodecContextNode(input, builder);
+        final BindingDataObjectCodecTreeNode<?> codec = context.getCodecContextNode(domPath, builder);
         if (codec == null) {
             return null;
         }
@@ -45,6 +42,27 @@ final class InstanceIdentifierCodec implements IllegalArgumentCodec<YangInstance
             // which is not binding representable.
             return null;
         }
-        return InstanceIdentifier.create(builder);
+        @SuppressWarnings("unchecked")
+        final InstanceIdentifier<T> ret = (InstanceIdentifier<T>) InstanceIdentifier.create(builder);
+        return ret;
+    }
+
+    @Override
+    public @NonNull YangInstanceIdentifier fromBinding(@NonNull final InstanceIdentifier<?> bindingPath) {
+        final List<PathArgument> domArgs = new ArrayList<>();
+        context.getCodecContextNode(bindingPath, domArgs);
+        return YangInstanceIdentifier.create(domArgs);
+    }
+
+    @Override
+    @Deprecated
+    public YangInstanceIdentifier serialize(final InstanceIdentifier<?> input) {
+        return fromBinding(input);
+    }
+
+    @Override
+    @Deprecated
+    public InstanceIdentifier<?> deserialize(final YangInstanceIdentifier input) {
+        return toBinding(input);
     }
-}
\ No newline at end of file
+}
index 619ee3e3ed54d5813a53aaae19c4d83893a2f79b..000398fc5c669ac8b4bba95bce056f2965ee35db 100644 (file)
@@ -15,6 +15,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html
                             binding-class="org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsaltrace.rev160908.Config">
   </odl:clustered-app-config>
 
+  <!-- FIXME: MDSAL-522: this should be BindingCodecTree, from which we need BindingInstanceIdentifierCodec -->
   <reference id="codec"
         interface="org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer"
         odl:type="default" />