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;
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;
}
}
+ checkAugmentMandatoryNodes(allAugments);
+
for (int i = 0; i < allAugments.size(); i++) {
// pick one augment
final AugmentationSchemaBuilder augment = allAugments.get(i);
}
}
+ /**
+ * 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.
*
}
}
+ @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());
+ }
+ }
+
}
--- /dev/null
+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;
+ }
+ }
+
+}
--- /dev/null
+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;
+ }
+ }
+ }
+
+}