Fix leafref-to-enum encoding
authorPeter Valka <Peter.Valka@pantheon.tech>
Wed, 6 May 2020 07:54:18 +0000 (09:54 +0200)
committerAnil Belur <abelur@linuxfoundation.org>
Wed, 19 Jun 2024 00:41:32 +0000 (10:41 +1000)
When we have a leafref pointing to an enum via an absolute path,
in and RPC, we end up using a no-op codec, whereas we should be
extracting the name (from enum constant, or SchemaContext).

This ends up being a problem not in codec itself, as it is using
reflection to acquire the return type, defaulting to no-op.

The problem lies with AbstractTypeGenerator, which ends up not
being able to look up the generated type in case of RPC. The problem
here is the order in which types are generated, as the leaf being
referenced is generated after the RPC itself -- hence we cannot
find the definition.

While this is again highlighting the problem of single-pass code
generation, we can side-step the problem by moving RPC/notification
generation after the root data generation.

As a further complication, we need to record the enum/schema mapping,
as the codec needs to be able to find them via leafref types.

JIRA: MDSAL-552
Change-Id: Ifd92807029cdb7ba92dcad5655bb34d3ff4cef9d
Signed-off-by: Peter Valka <Peter.Valka@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/mdsal/binding/dom/codec/impl/Mdsal552Test.java [new file with mode: 0644]
binding/mdsal-binding-test-model/src/main/yang/mdsal552.yang [new file with mode: 0644]

diff --git a/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/mdsal/binding/dom/codec/impl/Mdsal552Test.java b/binding/mdsal-binding-dom-codec/src/test/java/org/opendaylight/mdsal/binding/dom/codec/impl/Mdsal552Test.java
new file mode 100644 (file)
index 0000000..e2a279b
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * 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.impl;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.mdsal552.norev.Mdsal552Data.OutputA;
+import org.opendaylight.yang.gen.v1.mdsal552.norev.RefTestOutput;
+import org.opendaylight.yang.gen.v1.mdsal552.norev.RefTestOutputBuilder;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class Mdsal552Test extends AbstractBindingCodecTest {
+    private static final SchemaPath OUTPUT_PATH = SchemaPath.create(true, QName.create(RefTestOutput.QNAME, "ref_test"),
+        RefTestOutput.QNAME);
+    private static final QName OUTPUTREF = QName.create(RefTestOutput.QNAME, "outputref");
+
+    @Test
+    public void testLeafrefEnumerationToNormalized() throws IOException {
+        assertEquals(Builders.containerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(RefTestOutput.QNAME))
+            .withChild(ImmutableNodes.leafNode(OUTPUTREF, OutputA.DownTest.getName()))
+            .build(),
+            codecContext.toNormalizedNodeRpcData(new RefTestOutputBuilder().setOutputref(OutputA.DownTest).build()));
+    }
+
+    @Test
+    public void testLeafrefEnumerationFromNormalized() throws IOException {
+        assertEquals(new RefTestOutputBuilder().setOutputref(OutputA.DownTest).build(),
+            codecContext.fromNormalizedNodeRpcData(OUTPUT_PATH, Builders.containerBuilder()
+                .withNodeIdentifier(new NodeIdentifier(RefTestOutput.QNAME))
+                .withChild(ImmutableNodes.leafNode(OUTPUTREF, OutputA.DownTest.getName()))
+                .build()));
+    }
+}
diff --git a/binding/mdsal-binding-test-model/src/main/yang/mdsal552.yang b/binding/mdsal-binding-test-model/src/main/yang/mdsal552.yang
new file mode 100644 (file)
index 0000000..69e153f
--- /dev/null
@@ -0,0 +1,32 @@
+module mdsal552 {
+  yang-version 1.1;
+  namespace "mdsal552";
+  prefix mdsal552;
+
+  leaf output_a {
+    type enumeration {
+       enum uptest {
+         value 0;
+       }
+       enum down_test {
+         value 1;
+       }
+    }
+  }
+
+  rpc ref_test {
+    description "RPC enum validation using leafref";
+    input {
+      leaf input_a {
+        type uint32;
+      }
+    }
+    output {
+      leaf outputref {
+        type leafref {
+          path "/output_a";
+        }
+      }
+    }
+  }
+}