Fix enumeration leafref lookup 26/74626/13
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 30 Jul 2018 07:40:05 +0000 (09:40 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 9 Aug 2018 17:17:32 +0000 (17:17 +0000)
Leafref lookup for straight uses of enumeration typedefs seems
to be failing. The code in question seems to rely on weird assumptions,
hence this patch places a FIXME and turns an alternative branch
into a fallback if the weird code fails to find a definition.

This also activates ietf-hardware models.

JIRA: MDSAL-352
Change-Id: I6d342c7418c1538ee928f85b33f8f58da8aca5c6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/mdsal/binding/yang/types/AbstractTypeProvider.java
binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal352Test.java [new file with mode: 0644]
binding/mdsal-binding-generator-impl/src/test/resources/mdsal352.yang [new file with mode: 0644]
model/ietf/rfc8348-ietf-hardware-state/src/main/yang/ietf-hardware-state@2018-03-13.yang [moved from model/ietf/rfc8348-ietf-hardware-state/src/main/yang-disabled/ietf-hardware-state@2018-03-13.yang with 100% similarity]
model/ietf/rfc8348-ietf-hardware/src/main/yang/ietf-hardware@2018-03-13.yang [moved from model/ietf/rfc8348-ietf-hardware/src/main/yang-disabled/ietf-hardware@2018-03-13.yang with 100% similarity]

index 1e990f5206d360383f2ad5f8ca1588a9e2774381..86ef3df0863adcdb41e0355052384119f1070eeb 100644 (file)
@@ -553,11 +553,13 @@ public abstract class AbstractTypeProvider implements TypeProvider {
                 Preconditions.checkArgument(dataNode != null, "Failed to find leafref target: %s in module %s (%s)",
                         strXPath, this.getParentModule(parentNode).getName(), parentNode.getQName().getModule());
 
+                // FIXME: this block seems to be some weird magic hack. Analyze and refactor it.
                 if (leafContainsEnumDefinition(dataNode)) {
                     returnType = referencedTypes.get(dataNode.getPath());
                 } else if (leafListContainsEnumDefinition(dataNode)) {
                     returnType = Types.listTypeFor(referencedTypes.get(dataNode.getPath()));
-                } else {
+                }
+                if (returnType == null) {
                     returnType = resolveTypeFromDataSchemaNode(dataNode);
                 }
             } else {
diff --git a/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal352Test.java b/binding/mdsal-binding-generator-impl/src/test/java/org/opendaylight/mdsal/binding/generator/impl/Mdsal352Test.java
new file mode 100644 (file)
index 0000000..c5c224d
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2018 Pantheon Technologies, 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.generator.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.util.List;
+import org.junit.Test;
+import org.opendaylight.mdsal.binding.model.api.Type;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public class Mdsal352Test {
+
+    @Test
+    public void mdsal352Test() {
+        final SchemaContext context = YangParserTestUtils.parseYangResource("/mdsal352.yang");
+
+        final List<Type> generateTypes = new BindingGeneratorImpl().generateTypes(context);
+        assertNotNull(generateTypes);
+        assertEquals(6, generateTypes.size());
+    }
+}
diff --git a/binding/mdsal-binding-generator-impl/src/test/resources/mdsal352.yang b/binding/mdsal-binding-generator-impl/src/test/resources/mdsal352.yang
new file mode 100644 (file)
index 0000000..32d765f
--- /dev/null
@@ -0,0 +1,32 @@
+module enum-leafref {
+  namespace "urn:donaldh:yang:leafref:bug";
+  prefix l;
+
+  typedef action-effort {
+    type enumeration {
+      enum best-effort {
+      }
+      enum exact-match {
+      }
+    }
+  }
+
+  container operations {
+    list operation {
+      key 'effort';
+      uses operation-g;
+    }
+  }
+
+  grouping operation-g {
+    leaf effort {
+      type action-effort;
+    }
+    leaf-list operation {
+      type leafref {
+        path '/operations/operation/effort';
+      }
+    }
+  }
+}
+