import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.meta.StatementSource;
import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.StatementContextBase;
public interface StmtContext<A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>> {
Collection<? extends StmtContext<?, ?, ?>> getEffectOfStatement();
- /**
- * @return copy of this considering {@link CopyType} (augment, uses)
- *
- * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
- */
- Mutable<A, D, E> createCopy(StatementContextBase<?, ?, ?> newParent, CopyType typeOfCopy);
-
- /**
- * @return copy of this considering {@link CopyType} (augment, uses)
- *
- * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
- */
- Mutable<A, D, E> createCopy(QNameModule newQNameModule, StatementContextBase<?, ?, ?> newParent,
- CopyType typeOfCopy);
-
CopyHistory getCopyHistory();
boolean isSupportedByFeatures();
@Override
Mutable<?, ?, ?> getRoot();
+ /**
+ * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
+ * child and recording copy type. Resulting object may only be added as a child of this statement.
+ *
+ * @param stmt Statement to be used as a template
+ * @param type Type of copy to record in history
+ * @param targetModule Optional new target module
+ * @return copy of statement considering {@link CopyType} (augment, uses)
+ *
+ * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
+ * from an alien implementation.
+ * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+ */
+ <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
+ StmtContext<X, Y, Z> stmt, CopyType type, @Nullable QNameModule targetModule);
+
+ /**
+ * Create a child sub-statement, which is a child of this statement, inheriting all attributes from specified
+ * child and recording copy type. Resulting object may only be added as a child of this statement.
+ *
+ * @param stmt Statement to be used as a template
+ * @param type Type of copy to record in history
+ * @return copy of statement considering {@link CopyType} (augment, uses)
+ *
+ * @throws IllegalArgumentException if stmt cannot be copied into this statement, for example because it comes
+ * from an alien implementation.
+ * @throws org.opendaylight.yangtools.yang.parser.spi.source.SourceException instance of SourceException
+ */
+ default <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
+ final StmtContext<X, Y, Z> stmt, final CopyType type) {
+ return childCopyOf(stmt, type, null);
+ }
+
@Nonnull
Collection<? extends Mutable<?, ?, ?>> mutableDeclaredSubstatements();
import java.util.Optional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.common.YangVersion;
import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
import org.opendaylight.yangtools.yang.parser.spi.meta.MutableStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
return argument;
}
- @Override
- public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
- final CopyType typeOfCopy) {
- throw new UnsupportedOperationException("Root context cannot be copied");
- }
-
- @Override
- public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
- final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
- throw new UnsupportedOperationException("Root context cannot be copied");
- }
-
@Nonnull
@Override
public Optional<SchemaPath> getSchemaPath() {
import javax.annotation.Nonnull;
import org.opendaylight.yangtools.util.OptionalBoolean;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
addContextToNamespace(namespace, key, stmt);
}
+ @Override
+ public <X, Y extends DeclaredStatement<X>, Z extends EffectiveStatement<X, Y>> Mutable<X, Y, Z> childCopyOf(
+ final StmtContext<X, Y, Z> stmt, final CopyType type, final QNameModule targetModule) {
+ Preconditions.checkArgument(stmt instanceof SubstatementContext, "Unsupported statement %s", stmt);
+ final SubstatementContext<X, Y, Z> original = (SubstatementContext<X, Y, Z>)stmt;
+ return original.createCopy(targetModule, this, type);
+ }
+
@Override
public final String toString() {
return addToStringAttributes(MoreObjects.toStringHelper(this).omitNullValues()).toString();
this.argument = def.parseArgumentValue(this, rawStatementArgument());
}
- @SuppressWarnings("unchecked")
- private SubstatementContext(final SubstatementContext<A, D, E> original, final QNameModule newQNameModule,
- final StatementContextBase<?, ?, ?> newParent, final CopyType copyType) {
+ SubstatementContext(final StatementContextBase<A, D, E> original, final StatementContextBase<?, ?, ?> parent,
+ final A argument, final CopyType copyType) {
super(original, copyType);
- this.parent = Preconditions.checkNotNull(newParent);
-
- if (newQNameModule != null) {
- final A originalArg = original.argument;
- if (originalArg instanceof QName) {
- final QName originalQName = (QName) originalArg;
- this.argument = (A) getFromNamespace(QNameCacheNamespace.class,
- QName.create(newQNameModule, originalQName.getLocalName()));
- } else if (StmtContextUtils.producesDeclared(original, KeyStatement.class)) {
- this.argument = (A) StmtContextUtils.replaceModuleQNameForKey(
- (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) original, newQNameModule);
- } else {
- this.argument = original.argument;
- }
- } else {
- this.argument = original.argument;
- }
+ this.parent = Preconditions.checkNotNull(parent);
+ this.argument = argument;
}
@Override
return argument;
}
- @Override
- public StatementContextBase<A, D, E> createCopy(final StatementContextBase<?, ?, ?> newParent,
- final CopyType typeOfCopy) {
- return createCopy(null, newParent, typeOfCopy);
- }
-
- @Override
- public StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
+ @SuppressWarnings("unchecked")
+ StatementContextBase<A, D, E> createCopy(final QNameModule newQNameModule,
final StatementContextBase<?, ?, ?> newParent, final CopyType typeOfCopy) {
Preconditions.checkState(getCompletedPhase() == ModelProcessingPhase.EFFECTIVE_MODEL,
"Attempted to copy statement %s which has completed phase %s", this, getCompletedPhase());
- final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newQNameModule, newParent, typeOfCopy);
+ // FIXME: this should live in StatementSupport or similar
+ final A argumentCopy;
+ if (newQNameModule != null) {
+ if (argument instanceof QName) {
+ argumentCopy = (A) getFromNamespace(QNameCacheNamespace.class,
+ QName.create(newQNameModule, ((QName) argument).getLocalName()));
+ } else if (StmtContextUtils.producesDeclared(this, KeyStatement.class)) {
+ argumentCopy = (A) StmtContextUtils.replaceModuleQNameForKey(
+ (StmtContext<Collection<SchemaNodeIdentifier>, KeyStatement, ?>) this, newQNameModule);
+ } else {
+ argumentCopy = argument;
+ }
+ } else {
+ argumentCopy = argument;
+ }
+
+ final SubstatementContext<A, D, E> copy = new SubstatementContext<>(this, newParent, argumentCopy, typeOfCopy);
definition().onStatementAdded(copy);
private void copySubstatement(final Mutable<?, ?, ?> stmtContext, final QNameModule newQNameModule,
final CopyType typeOfCopy, final Collection<Mutable<?, ?, ?>> buffer) {
if (needToCopyByUses(stmtContext)) {
- final Mutable<?, ?, ?> copy = stmtContext.createCopy(newQNameModule, this, typeOfCopy);
+ final Mutable<?, ?, ?> copy = childCopyOf(stmtContext, typeOfCopy, newQNameModule);
LOG.debug("Copying substatement {} for {} as", stmtContext, this, copy);
buffer.add(copy);
} else if (isReusedByUses(stmtContext)) {
if (needToCopyByAugment(original)) {
validateNodeCanBeCopiedByAugment(original, target, typeOfCopy, skipCheckOfMandatoryNodes);
- final Mutable<?, ?, ?> copy = original.createCopy(target, typeOfCopy);
- buffer.add(copy);
+ buffer.add(target.childCopyOf(original, typeOfCopy));
} else if (isReusedByAugment(original)) {
buffer.add(original);
}
private static void addStatement(final Mutable<?, ?, ?> stmtCtxToBeAdded,
final StatementContextBase<?, ?, ?> targetCtx) {
if (StmtContextUtils.isUnknownStatement(stmtCtxToBeAdded)) {
- targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
+ targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeAdded, CopyType.ORIGINAL));
return;
}
}
}
- targetCtx.addEffectiveSubstatement(stmtCtxToBeAdded.createCopy(targetCtx, CopyType.ORIGINAL));
+ targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeAdded, CopyType.ORIGINAL));
}
private static void performDeviateReplace(final StatementContextBase<?, ?, ?> deviateStmtCtx,
for (final StmtContext<?, ?, ?> targetCtxSubstatement : targetCtx.effectiveSubstatements()) {
if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
targetCtx.removeStatementFromEffectiveSubstatements(stmtToBeReplaced);
- targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+ targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeReplaced, CopyType.ORIGINAL));
return;
}
}
for (final Mutable<?, ?, ?> targetCtxSubstatement : targetCtx.mutableDeclaredSubstatements()) {
if (stmtToBeReplaced.equals(targetCtxSubstatement.getPublicDefinition())) {
targetCtxSubstatement.setIsSupportedToBuildEffective(false);
- targetCtx.addEffectiveSubstatement(stmtCtxToBeReplaced.createCopy(targetCtx, CopyType.ORIGINAL));
+ targetCtx.addEffectiveSubstatement(targetCtx.childCopyOf(stmtCtxToBeReplaced, CopyType.ORIGINAL));
return;
}
}
* @throws SourceException
* instance of SourceException
*/
- private static void copyFromSourceToTarget(final Mutable<?, ?, ?> sourceGrpStmtCtx,
+ static void copyFromSourceToTarget(final Mutable<?, ?, ?> sourceGrpStmtCtx,
final StatementContextBase<?, ?, ?> targetCtx,
final StmtContext.Mutable<QName, UsesStatement, EffectiveStatement<QName, UsesStatement>> usesNode) {
final Collection<? extends Mutable<?, ?, ?>> declared = sourceGrpStmtCtx.mutableDeclaredSubstatements();
final StatementContextBase<?, ?, ?> targetCtx, final QNameModule targetModule,
final Collection<Mutable<?, ?, ?>> buffer) {
if (needToCopyByUses(original)) {
- final Mutable<?, ?, ?> copy = original.createCopy(targetModule, targetCtx, CopyType.ADDED_BY_USES);
+ final Mutable<?, ?, ?> copy = targetCtx.childCopyOf(original, CopyType.ADDED_BY_USES, targetModule);
buffer.add(copy);
} else if (isReusedByUsesOnTop(original)) {
buffer.add(original);