Added check for mandatory nodes if augment target is in another module. 93/1193/1
authorMartin Vitez <mvitez@cisco.com>
Mon, 16 Sep 2013 08:17:23 +0000 (10:17 +0200)
committerMartin Vitez <mvitez@cisco.com>
Mon, 16 Sep 2013 08:17:23 +0000 (10:17 +0200)
Signed-off-by: Martin Vitez <mvitez@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/YangParserNegativeTest.java
yang/yang-parser-impl/src/test/resources/negative-scenario/testfile7.yang [new file with mode: 0644]
yang/yang-parser-impl/src/test/resources/negative-scenario/testfile8.yang [new file with mode: 0644]

index 8f5028432f5268415a49a8b247cbd7f139a58633..0f554c6206b67978ebc1f4e7dc80bf64008e1dfa 100644 (file)
@@ -16,6 +16,7 @@ import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -47,6 +48,7 @@ import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
 import org.opendaylight.yangtools.yang.parser.builder.api.AugmentationSchemaBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.Builder;
 import org.opendaylight.yangtools.yang.parser.builder.api.DataNodeContainerBuilder;
+import org.opendaylight.yangtools.yang.parser.builder.api.DataSchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.GroupingBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.SchemaNodeBuilder;
 import org.opendaylight.yangtools.yang.parser.builder.api.TypeAwareBuilder;
@@ -449,6 +451,8 @@ public final class YangParserImpl implements YangModelParser {
             }
         }
 
+        checkAugmentMandatoryNodes(allAugments);
+
         for (int i = 0; i < allAugments.size(); i++) {
             // pick one augment
             final AugmentationSchemaBuilder augment = allAugments.get(i);
@@ -476,6 +480,33 @@ public final class YangParserImpl implements YangModelParser {
         }
     }
 
+    /**
+     * Check augments for mandatory nodes. If the target node is in another
+     * module, then nodes added by the augmentation MUST NOT be mandatory nodes.
+     * If mandatory node is found, throw an exception.
+     *
+     * @param augments
+     *            augments to check
+     */
+    private void checkAugmentMandatoryNodes(Collection<AugmentationSchemaBuilder> augments) {
+        for (AugmentationSchemaBuilder augment : augments) {
+            String augmentPrefix = augment.getTargetPath().getPath().get(0).getPrefix();
+            ModuleBuilder module = ParserUtils.getParentModule(augment);
+            String modulePrefix = module.getPrefix();
+
+            if (augmentPrefix == null || augmentPrefix.isEmpty() || augmentPrefix.equals(modulePrefix)) {
+                continue;
+            }
+
+            for (DataSchemaNodeBuilder childNode : augment.getChildNodeBuilders()) {
+                if (childNode.getConstraints().isMandatory()) {
+                    throw new YangParseException(augment.getModuleName(), augment.getLine(),
+                            "Error in augment parsing: cannot augment mandatory node");
+                }
+            }
+        }
+    }
+
     /**
      * Search for augment target and perform augmentation.
      *
index b66fb83e92d186742999bb1741faf27f7eed8b84..e05e0224a47ff0a603bcd37e39311f43644fefac 100644 (file)
@@ -196,4 +196,20 @@ public class YangParserNegativeTest {
         }
     }
 
+    @Test
+    public void testMandatoryInAugment() throws IOException {
+        try {
+            try (InputStream stream1 = new FileInputStream(getClass().getResource("/negative-scenario/testfile8.yang")
+                    .getPath());
+                    InputStream stream2 = new FileInputStream(getClass().getResource(
+                            "/negative-scenario/testfile7.yang").getPath())) {
+                TestUtils.loadModules(Arrays.asList(stream1, stream2));
+                fail("YangParseException should by thrown");
+            }
+        } catch (YangParseException e) {
+            String expected = "Error in module 'testfile7' at line 18: Error in augment parsing: cannot augment mandatory node";
+            assertEquals(expected, e.getMessage());
+        }
+    }
+
 }
diff --git a/yang/yang-parser-impl/src/test/resources/negative-scenario/testfile7.yang b/yang/yang-parser-impl/src/test/resources/negative-scenario/testfile7.yang
new file mode 100644 (file)
index 0000000..17dddeb
--- /dev/null
@@ -0,0 +1,26 @@
+module testfile7 {
+    yang-version 1;
+    namespace "urn:simple.test7.demo";
+    prefix "t7";
+
+    import testfile8 {
+        prefix "t8";
+        revision-date 2013-08-03;
+    }
+
+    organization "opendaylight";
+    contact "http://www.opendaylight.org/";
+
+    revision "2013-08-03" {
+        reference " WILL BE DEFINED LATER";
+    }
+
+    augment "/t8:interfaces/t8:ifEntry" {
+        when "if:leafType='ds1'";
+        leaf linkleaf {
+            mandatory true;
+            type binary;
+        }
+    }
+
+}
diff --git a/yang/yang-parser-impl/src/test/resources/negative-scenario/testfile8.yang b/yang/yang-parser-impl/src/test/resources/negative-scenario/testfile8.yang
new file mode 100644 (file)
index 0000000..baf3557
--- /dev/null
@@ -0,0 +1,27 @@
+module testfile8 {
+    yang-version 1;
+    namespace "urn:simple.test8.demo";
+    prefix "t8";
+
+    organization "opendaylight";
+    contact "http://www.opendaylight.org/";
+    description "This is types-data test description";
+
+    revision "2013-08-03" {
+        reference " WILL BE DEFINED LATER";
+    }
+
+    container interfaces {
+        grouping ifEntry {
+            container augment-holder;
+        }
+        list ifEntry {
+            key "ifIndex";
+            leaf ifIndex {
+                type uint32;
+                units minutes;
+            }
+        }
+    }
+
+}