import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
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.model.api.stmt.UsesStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.WhenStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.InferenceException;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
-import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.TypeOfCopy;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
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;
private static void copyDeclaredStmts(final StatementContextBase<?, ?, ?> sourceCtx,
final StatementContextBase<?, ?, ?> targetCtx) {
- final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
- .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
- : TypeOfCopy.ADDED_BY_AUGMENTATION;
+ final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+ .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+ : CopyType.ADDED_BY_AUGMENTATION;
for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.declaredSubstatements()) {
if (!StmtContextUtils.areFeaturesSupported(originalStmtCtx)) {
private static void copyEffectiveStmts(final StatementContextBase<?, ?, ?> sourceCtx,
final StatementContextBase<?, ?, ?> targetCtx) {
- final TypeOfCopy typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
- .getDeclaredRepresentationClass().equals(UsesStatement.class) ? TypeOfCopy.ADDED_BY_USES_AUGMENTATION
- : TypeOfCopy.ADDED_BY_AUGMENTATION;
+ final CopyType typeOfCopy = sourceCtx.getParentContext().getPublicDefinition()
+ .getDeclaredRepresentationClass().equals(UsesStatement.class) ? CopyType.ADDED_BY_USES_AUGMENTATION
+ : CopyType.ADDED_BY_AUGMENTATION;
for (final StatementContextBase<?, ?, ?> originalStmtCtx : sourceCtx.effectiveSubstatements()) {
if (needToCopyByAugment(originalStmtCtx)) {
}
private static void validateNodeCanBeCopiedByAugment(final StatementContextBase<?, ?, ?> sourceCtx,
- final StatementContextBase<?, ?, ?> targetCtx, final TypeOfCopy typeOfCopy) {
+ final StatementContextBase<?, ?, ?> targetCtx, final CopyType typeOfCopy) {
if (sourceCtx.getPublicDefinition().getDeclaredRepresentationClass().equals(WhenStatement.class)) {
return;
}
- if (typeOfCopy == TypeOfCopy.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
- final List<StatementContextBase<?, ?, ?>> sourceSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
- .addAll(sourceCtx.declaredSubstatements()).addAll(sourceCtx.effectiveSubstatements()).build();
-
- for (final StatementContextBase<?, ?, ?> sourceSubStatement : sourceSubStatements) {
- InferenceException.throwIf(MandatoryStatement.class.equals(sourceSubStatement.getPublicDefinition()
- .getDeclaredRepresentationClass()), sourceCtx.getStatementSourceReference(),
- "An augment cannot add node '%s' because it is mandatory and in module different from target",
- sourceCtx.rawStatementArgument());
- }
+ if (typeOfCopy == CopyType.ADDED_BY_AUGMENTATION && reguiredCheckOfMandatoryNodes(sourceCtx, targetCtx)) {
+ checkForMandatoryNodes(sourceCtx);
}
final List<StatementContextBase<?, ?, ?>> targetSubStatements = new Builder<StatementContextBase<?, ?, ?>>()
}
}
+ private static void checkForMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx) {
+ if (StmtContextUtils.isNonPresenceContainer(sourceCtx)) {
+ /*
+ * We need to iterate over both declared and effective sub-statements,
+ * because a mandatory node can be:
+ * a) declared in augment body
+ * b) added to augment body also via uses of a grouping and
+ * such sub-statements are stored in effective sub-statements collection.
+ */
+ for (final StatementContextBase<?, ?, ?> sourceSubStatement : Iterables.concat(
+ sourceCtx.declaredSubstatements(), sourceCtx.declaredSubstatements())) {
+ checkForMandatoryNodes(sourceSubStatement);
+ }
+ }
+
+ InferenceException.throwIf(StmtContextUtils.isMandatoryNode(sourceCtx),
+ sourceCtx.getStatementSourceReference(),
+ "An augment cannot add node '%s' because it is mandatory and in module different than target",
+ sourceCtx.rawStatementArgument());
+ }
+
private static boolean reguiredCheckOfMandatoryNodes(final StatementContextBase<?, ?, ?> sourceCtx,
StatementContextBase<?, ?, ?> targetCtx) {
/*
* the same module, return false and skip mandatory nodes
* validation
*/
- if (Utils.isPresenceContainer(targetCtx)) {
+ if (StmtContextUtils.isPresenceContainer(targetCtx)) {
return false;
}
}