Fix for deviation list mapping in Restconf 05/82505/2
authorTibor Král <tibor.kral@pantheon.tech>
Tue, 11 Jun 2019 09:02:34 +0000 (11:02 +0200)
committerTibor Král <tibor.kral@pantheon.tech>
Thu, 13 Jun 2019 11:06:12 +0000 (11:06 +0000)
RESTCONF NBP initialization fails with
RestconfDocumentedException when global schema context
contains yang models using deviations.

JIRA: TRNSPRTPCE-126

Change-Id: I480ac6641006e3e82ffd6ac3aa4cf169fe8b665d
Signed-off-by: Tibor Král <tibor.kral@pantheon.tech>
restconf/restconf-nb-rfc8040/src/main/java/org/opendaylight/restconf/nb/rfc8040/utils/mapping/RestconfMappingNodeUtil.java
restconf/restconf-nb-rfc8040/src/test/java/org/opendaylight/restconf/nb/rfc8040/utils/mapping/RestconfMappingNodeUtilTest.java
restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviatee-test@2019-06-11.yang [new file with mode: 0644]
restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviator-test@2019-06-11.yang [new file with mode: 0644]

index 68e03c4121e89c19d55731ac4224991abebead48..8361ace56e2cf819f096af70c9ce5eb5d0e11363 100644 (file)
@@ -197,11 +197,14 @@ public final class RestconfMappingNodeUtil {
             final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> deviationEntryNode =
                     Builders.mapEntryBuilder((ListSchemaNode) deviationsSchema);
             final QName lastComponent = deviation.getTargetPath().getLastComponent();
-            addChildOfModuleBySpecificModule(IetfYangLibrary.SPECIFIC_MODULE_NAME_LEAF_QNAME, deviationEntryNode,
-                    context.findModule(lastComponent.getModule()).get().getName(),
+            addChildOfModuleBySpecificModuleInternal(IetfYangLibrary.SPECIFIC_MODULE_NAME_LEAF_QNAME,
+                    deviationEntryNode, context.findModule(lastComponent.getModule()).get().getName(),
                     ietfYangLibraryModule);
-            addChildOfModuleBySpecificModule(IetfYangLibrary.SPECIFIC_MODULE_REVISION_LEAF_QNAME, deviationEntryNode,
-                    lastComponent.getRevision(), ietfYangLibraryModule);
+            if (lastComponent.getRevision().isPresent()) {
+                addChildOfModuleBySpecificModuleInternal(IetfYangLibrary.SPECIFIC_MODULE_REVISION_LEAF_QNAME,
+                        deviationEntryNode, lastComponent.getRevision(),
+                        ietfYangLibraryModule);
+            }
             deviations.withChild(deviationEntryNode.build());
         }
         mapEntryBuilder.withChild(deviations.build());
@@ -336,25 +339,6 @@ public final class RestconfMappingNodeUtil {
         throw new RestconfDocumentedException(qnameOfSchema.getLocalName() + " doesn't exist.");
     }
 
-    /**
-     * Mapping childrens of list-module.
-     *
-     * @param specifiLeafQName
-     *             qname of leaf
-     * @param mapEntryBuilder
-     *             maptEntryBuilder of parent for mapping children
-     * @param value
-     *             valeu of leaf
-     * @param ietfYangLibraryModule
-     *             ietf-yang-library module
-     */
-    private static void addChildOfModuleBySpecificModule(final QName specifiLeafQName,
-            final DataContainerNodeBuilder<NodeIdentifierWithPredicates, MapEntryNode> mapEntryBuilder,
-            final Object value, final Module ietfYangLibraryModule) {
-        final DataSchemaNode nameLeaf = findNodeInGroupings(specifiLeafQName, ietfYangLibraryModule);
-        mapEntryBuilder.withChild(Builders.leafBuilder((LeafSchemaNode) nameLeaf).withValue(value).build());
-    }
-
     /**
      * Find schema of specific leaf in list-module grouping.
      *
index 116139dc3d1ee694cbe6d460861f6cce28bde681..c08e40814c7a7ed5b49e332f3f5760e08da16fba 100644 (file)
@@ -116,6 +116,8 @@ public class RestconfMappingNodeUtilTest {
 
         // verify loaded modules
         verifyLoadedModules((ContainerNode) mods);
+        // verify deviations
+        verifyDeviations((ContainerNode) mods);
     }
 
     @Test
@@ -204,6 +206,30 @@ public class RestconfMappingNodeUtilTest {
         }
     }
 
+    /**
+     * Verify whether the loaded modules contain any deviations.
+     *
+     * @param containerNode
+     *             modules
+     */
+    private static void verifyDeviations(final ContainerNode containerNode) {
+        int deviationsFound = 0;
+        for (final DataContainerChild child : containerNode.getValue()) {
+            if (child instanceof MapNode) {
+                for (final MapEntryNode mapEntryNode : ((MapNode) child).getValue()) {
+                    for (final DataContainerChild dataContainerChild : mapEntryNode
+                            .getValue()) {
+                        if (dataContainerChild.getNodeType()
+                                .equals(IetfYangLibrary.SPECIFIC_MODULE_DEVIATION_LIST_QNAME)) {
+                            deviationsFound++;
+                        }
+                    }
+                }
+            }
+        }
+        Assert.assertTrue(deviationsFound > 0);
+    }
+
     /**
      * Verify loaded modules.
      *
diff --git a/restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviatee-test@2019-06-11.yang b/restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviatee-test@2019-06-11.yang
new file mode 100644 (file)
index 0000000..78182b4
--- /dev/null
@@ -0,0 +1,26 @@
+module deviatee-test {
+  yang-version 1.1;
+  namespace "http://tech/pantheon/deviatee-test";
+  prefix dev;
+
+  organization
+        "PANTHEON.tech s.r.o.";
+
+  description
+    "Prepared to receive deviations";
+
+  revision 2019-06-11 {
+    description
+      "Deviation test";
+  }
+
+  container test-container {
+    leaf comment {
+      type string {
+        length "0 .. 60";
+    }
+    description
+     "Just for Deviation testing";
+    }
+  }
+}
diff --git a/restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviator-test@2019-06-11.yang b/restconf/restconf-nb-rfc8040/src/test/resources/modules/restconf-module-testing/deviator-test@2019-06-11.yang
new file mode 100644 (file)
index 0000000..8133fd3
--- /dev/null
@@ -0,0 +1,32 @@
+module deviator-test {
+  yang-version 1.1;
+  namespace "http://tech/pantheon/deviator-test";
+  prefix dev;
+
+  organization
+      "PANTHEON.tech s.r.o.";
+
+  import deviatee-test {
+    prefix dv;
+    revision-date 2019-06-11;
+  }
+
+  description
+    "Prepared to set deviations";
+
+  revision 2019-06-11 {
+    description
+      "Deviation test";
+  }
+
+  deviation "/dv:test-container/dv:comment" {
+    deviate add {
+      must
+        "(../dv:comment = 'TEST' or ../dv:comment = 'TEST2')" {
+        error-message "Invalid comment for Deviation test";
+        description
+          "This is just a simple deviation test";
+      }
+    }
+  }
+}