*/
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
+import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Set;
+import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
import org.opendaylight.yangtools.yang.model.api.stmt.DataDefinitionStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.MandatoryStatement;
import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
+import org.opendaylight.yangtools.yang.parser.stmt.reactor.RootStatementContext;
import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
// FIXME: Move this to the AugmentStatementDefinition#ApplyAction
private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
- final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
- .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
- boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
- Utils.getRootModuleQName(targetCtx));
-
TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
.equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
if (needToCopyByAugment(originalStmtCtx)) {
- validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
+ validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
targetCtx.addEffectiveSubstatement(copy);
private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
final StatementContextBase<?, ?, ?> targetCtx) throws SourceException {
-
- final List<StatementContextBase<?, ?, ?>> subStatements = new Builder<StatementContextBase<?, ?, ?>>()
- .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
- boolean sourceAndTargetInSameModule = Utils.getRootModuleQName(sourceCtx).equals(
- Utils.getRootModuleQName(targetCtx));
-
TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition().getDeclaredRepresentationClass()
.equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION : TypeOfCopy.ADDED_BY_AUGMENTATION;
for (StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
if (needToCopyByAugment(originalStmtCtx)) {
- validateNodeCanBeCopiedByAugment(originalStmtCtx, subStatements, sourceAndTargetInSameModule);
+ validateNodeCanBeCopiedByAugment(originalStmtCtx, targetCtx);
StatementContextBase<?, ?, ?> copy = originalStmtCtx.createCopy(targetCtx, typeOfCopy);
targetCtx.addEffectiveSubstatement(copy);
}
private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
- final List<StatementContextBase<?, ?, ?>> targetSubStatements, final boolean sourceAndTargetInSameModule) {
+ final StatementContextBase<?, ?, ?> targetCtx) {
if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
return;
}
- if (!sourceAndTargetInSameModule) {
+ if (reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
.addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
}
}
+ final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
+ .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
+
for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
final boolean sourceIsDataNode = DataDefinitionStatement.class.isAssignableFrom(sourceCtx
}
}
+ private static boolean reguiredCheckOfMandatoryNodes(StatementContextBase<?, ?, ?> sourceCtx,
+ StatementContextBase<?, ?, ?> targetCtx) {
+ /*
+ * If the statement argument is not QName, it cannot be mandatory statement,
+ * therefore return false and skip mandatory nodes validation
+ */
+ if(!(sourceCtx.getStatementArgument() instanceof QName)) {
+ return false;
+ }
+ QName sourceStmtQName = (QName) sourceCtx.getStatementArgument();
+
+ RootStatementContext<?, ?, ?> root = targetCtx.getRoot();
+ do {
+ Verify.verify(targetCtx.getStatementArgument() instanceof QName,
+ "Argument of augment target statement must be QName.");
+ QName targetStmtQName = (QName) targetCtx.getStatementArgument();
+ /*
+ * If target is from another module, return true and perform
+ * mandatory nodes validation
+ */
+ if (!Utils.belongsToTheSameModule(targetStmtQName, sourceStmtQName)) {
+ return true;
+ } else {
+ /*
+ * If target or one of its parent is a presence container from
+ * the same module, return false and skip mandatory nodes
+ * validation
+ */
+ if (Utils.isPresenceContainer(targetCtx)) {
+ return false;
+ }
+ }
+ } while ((targetCtx = targetCtx.getParentContext()) != root);
+
+ /*
+ * All target node's parents belong to the same module as source node,
+ * therefore return false and skip mandatory nodes validation.
+ */
+ return false;
+ }
+
private static final Set<Rfc6020Mapping> NOCOPY_DEV_SET = ImmutableSet.of(Rfc6020Mapping.USES);
public static boolean needToCopyByAugment(final StmtContext<?, ?, ?> stmtContext) {
static boolean isSupportedAugmentTarget(final StatementContextBase<?, ?, ?> substatementCtx) {
/*
- * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must be not for example
+ * :TODO Substatement must be allowed augment target type e.g. Container, etc... and must not be for example
* grouping, identity etc. It is problem in case when more than one substatements have the same QName, for
* example Grouping and Container are siblings and they have the same QName. We must find the Container and the
* Grouping must be ignored as disallowed augment target.
package org.opendaylight.yangtools.yang.parser.stmt.rfc6020;
import static org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils.firstAttributeOf;
+
import com.google.common.base.CharMatcher;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.collect.ImmutableSet;
import org.opendaylight.yangtools.yang.model.api.Deviation.Deviate;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.RevisionAwareXPath;
+import org.opendaylight.yangtools.yang.model.api.Rfc6020Mapping;
import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.stmt.BelongsToStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement;
}
return result.toString();
}
+
+ public static boolean belongsToTheSameModule(QName targetStmtQName, QName sourceStmtQName) {
+ if (targetStmtQName.getModule().equals(sourceStmtQName.getModule())) {
+ return true;
+ }
+ return false;
+ }
+
+ public static boolean isPresenceContainer(StatementContextBase<?, ?, ?> targetCtx) {
+ if (!targetCtx.getPublicDefinition().equals(Rfc6020Mapping.CONTAINER)) {
+ return false;
+ }
+
+ final List<StatementContextBase<?, ?, ?>> targetSubStatements = new ImmutableList.Builder<StatementContextBase<?, ?, ?>>()
+ .addAll(targetCtx.declaredSubstatements()).addAll(targetCtx.effectiveSubstatements()).build();
+ for (final StatementContextBase<?, ?, ?> subStatement : targetSubStatements) {
+ if (subStatement.getPublicDefinition().equals(Rfc6020Mapping.PRESENCE)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
}
--- /dev/null
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. 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.yangtools.yang.stmt.test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.net.URISyntaxException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
+
+public class Bug5335 {
+ private static final String FOO = "foo";
+ private static final String BAR = "bar";
+ private static final String REV = "2016-03-04";
+
+ private static final QName ROOT = QName.create(FOO, REV, "root");
+ private static final QName PRESENCE_CONTAINER_F = QName.create(FOO, REV, "presence-container");
+ private static final QName NON_PRESENCE_CONTAINER_F = QName.create(FOO, REV, "non-presence-container");
+ private static final QName MANDATORY_LEAF_F = QName.create(FOO, REV, "mandatory-leaf");
+ private static final QName PRESENCE_CONTAINER_B = QName.create(BAR, REV, "presence-container");
+ private static final QName NON_PRESENCE_CONTAINER_B = QName.create(BAR, REV, "non-presence-container");
+ private static final QName MANDATORY_LEAF_B = QName.create(BAR, REV, "mandatory-leaf");
+
+ private final ByteArrayOutputStream output = new ByteArrayOutputStream();
+
+ @Before
+ public void setUp() throws UnsupportedEncodingException {
+ System.setOut(new PrintStream(output, true, "UTF-8"));
+ }
+
+ @After
+ public void cleanUp() {
+ System.setOut(System.out);
+ }
+
+ @Test
+ public void incorrectTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-1");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertNull(mandatoryLeaf);
+
+ String testLog = output.toString();
+ assertTrue(testLog
+ .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
+ }
+
+ @Test
+ public void incorrectTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertNull(mandatoryLeaf);
+
+ String testLog = output.toString();
+ assertTrue(testLog
+ .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
+ }
+
+ @Test
+ public void incorrectTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/incorrect/case-2");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_F, NON_PRESENCE_CONTAINER_B,
+ MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertNull(mandatoryLeaf);
+
+ String testLog = output.toString();
+ assertTrue(testLog
+ .contains("An augment cannot add node 'mandatory-leaf' because it is mandatory and in module different from target"));
+ }
+
+ @Test
+ public void correctTest1() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-1");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
+ }
+
+ @Test
+ public void correctTest2() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-2");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
+ MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
+ }
+
+ @Test
+ public void correctTest3() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-3");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, PRESENCE_CONTAINER_B, NON_PRESENCE_CONTAINER_B,
+ MANDATORY_LEAF_B);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
+ }
+
+ @Test
+ public void correctTest4() throws SourceException, FileNotFoundException, ReactorException, URISyntaxException {
+ SchemaContext context = StmtTestUtils.parseYangSources("/bugs/bug5335/correct/case-4");
+ assertNotNull(context);
+
+ SchemaPath schemaPath = SchemaPath.create(true, ROOT, NON_PRESENCE_CONTAINER_F, MANDATORY_LEAF_F);
+ SchemaNode mandatoryLeaf = SchemaContextUtil.findDataSchemaNode(context, schemaPath);
+ assertTrue(mandatoryLeaf instanceof LeafSchemaNode);
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root" {
+ container presence-container {
+ presence "presence-container";
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+}
--- /dev/null
+submodule sub-bar {
+ belongs-to bar {
+ prefix bar;
+ }
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ augment "/foo:root/bar:presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root" {
+ container presence-container {
+ presence "presence-container";
+ }
+ }
+
+ augment "/foo:root/bar:presence-container" {
+ container non-presence-container {
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+}
--- /dev/null
+submodule sub-bar {
+ belongs-to bar {
+ prefix bar;
+ }
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ augment "/foo:root/bar:presence-container/bar:non-presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root" {
+ container presence-container {
+ presence "presence-container";
+ }
+ }
+
+ augment "/foo:root/bar:presence-container" {
+ container non-presence-container {
+ }
+ }
+
+ augment "/foo:root/bar:presence-container/bar:non-presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+
+ augment "/root" {
+ container non-presence-container {
+ }
+ }
+
+ augment "/root/non-presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root" {
+ container non-presence-container {
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+}
--- /dev/null
+submodule sub-bar {
+ belongs-to bar {
+ prefix bar;
+ }
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ augment "/foo:root/bar:non-presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root/foo:presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+
+ augment "/root" {
+ container presence-container {
+ presence "presence-container";
+ }
+ }
+}
--- /dev/null
+module bar {
+ namespace "bar";
+ prefix bar;
+ yang-version 1;
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ augment "/foo:root/foo:presence-container" {
+ container non-presence-container {
+ }
+ }
+}
--- /dev/null
+module foo {
+ namespace "foo";
+ prefix foo;
+ yang-version 1;
+
+ revision "2016-03-04" {
+ description "test";
+ }
+
+ container root {
+ }
+
+ augment "/root" {
+ container presence-container {
+ presence "presence-container";
+ }
+ }
+}
--- /dev/null
+submodule sub-bar {
+ belongs-to bar {
+ prefix bar;
+ }
+
+ import foo { prefix foo; revision-date 2016-03-04; }
+
+ augment "/foo:root/foo:presence-container/bar:non-presence-container" {
+ leaf mandatory-leaf {
+ type string;
+ mandatory true;
+ }
+ }
+}