/*
* Copyright (c) 2020 PANTHEON.tech, s.r.o. 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.parser.stmt.reactor;
import static java.util.Objects.requireNonNull;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNull;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.common.QNameModule;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
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.StatementDefinition;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory;
import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.OnDemandSchemaTreeStorageNode;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextDefaults;
import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A statement which has been inferred to exist. Functionally it is equivalent to a SubstatementContext, but it is not
* backed by a declaration (and declared statements). It is backed by a prototype StatementContextBase and has only
* effective substatements, which are either transformed from that prototype or added by inference.
*/
final class InferredStatementContext, E extends EffectiveStatement>
extends StatementContextBase implements OnDemandSchemaTreeStorageNode {
private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
private final @NonNull StatementContextBase prototype;
private final @NonNull StatementContextBase, ?, ?> parent;
private final @NonNull StmtContext originalCtx;
private final @NonNull CopyType childCopyType;
private final QNameModule targetModule;
private final A argument;
private InferredStatementContext(final InferredStatementContext original,
final StatementContextBase, ?, ?> parent) {
super(original);
this.parent = requireNonNull(parent);
this.childCopyType = original.childCopyType;
this.targetModule = original.targetModule;
this.prototype = original.prototype;
this.originalCtx = original.originalCtx;
this.argument = original.argument;
setSubstatementsInitialized();
}
InferredStatementContext(final StatementContextBase, ?, ?> parent, final StatementContextBase prototype,
final CopyType myCopyType, final CopyType childCopyType, final QNameModule targetModule) {
super(prototype.definition(), CopyHistory.of(myCopyType, prototype.getCopyHistory()));
this.parent = requireNonNull(parent);
this.prototype = requireNonNull(prototype);
this.argument = targetModule == null ? prototype.getStatementArgument()
: prototype.definition().adaptArgumentValue(prototype, targetModule);
this.childCopyType = requireNonNull(childCopyType);
this.targetModule = targetModule;
this.originalCtx = prototype.getOriginalCtx().orElse(prototype);
// Note: substatements from prototype are initialized lazily through ensureSubstatements()
}
@Override
public Collection extends StatementContextBase, ?, ?>> mutableDeclaredSubstatements() {
return ImmutableList.of();
}
@Override
public Iterable extends StmtContext, ?, ?>> allSubstatements() {
// No need to concat with declared
return effectiveSubstatements();
}
@Override
public Stream extends StmtContext, ?, ?>> allSubstatementsStream() {
// No need to concat with declared
return effectiveSubstatements().stream();
}
@Override
public StatementSourceReference getStatementSourceReference() {
return originalCtx.getStatementSourceReference();
}
@Override
public String rawStatementArgument() {
return originalCtx.rawStatementArgument();
}
@Override
public Optional> getOriginalCtx() {
return Optional.of(originalCtx);
}
@Override
public Optional> getPreviousCopyCtx() {
return Optional.of(prototype);
}
@Override
public D buildDeclared() {
/*
* Share original instance of declared statement between all effective statements which have been copied or
* derived from this original declared statement.
*/
return originalCtx.buildDeclared();
}
@Override
InferredStatementContext reparent(final StatementContextBase, ?, ?> newParent) {
return new InferredStatementContext<>(this, newParent);
}
@Override
boolean hasEmptySubstatements() {
ensureEffectiveSubstatements();
return hasEmptyEffectiveSubstatements();
}
@Override
public > @NonNull Optional findSubstatementArgument(
final @NonNull Class type) {
if (substatementsInitialized()) {
return StmtContextDefaults.findSubstatementArgument(this, type);
}
final Optional templateArg = prototype.findSubstatementArgument(type);
if (templateArg.isEmpty()) {
return templateArg;
}
if (SchemaTreeEffectiveStatement.class.isAssignableFrom(type)) {
// X is known to be QName
return (Optional) templateArg.map(template -> ((QName) template).bindTo(targetModule));
}
return templateArg;
}
@Override
public boolean hasSubstatement(final @NonNull Class extends EffectiveStatement, ?>> type) {
return substatementsInitialized() ? StmtContextDefaults.hasSubstatement(prototype, type)
: prototype.hasSubstatement(type);
}
@Override
public , E extends EffectiveStatement>
StmtContext requestSchemaTreeChild(final QName qname) {
LOG.debug("Materializing on lookup of {}", qname);
// FIXME: YANGTOOLS-1160: we do not want to force full materialization here
ensureEffectiveSubstatements();
// Now we have to do a lookup as we do not have access to the namespace being populated (yet). Here we are
// bypassing additional checks and talk directly to superclass to get the statements.
for (StmtContext, ?, ?> stmt : super.mutableEffectiveSubstatements()) {
if (stmt.producesEffective(SchemaTreeEffectiveStatement.class)
&& qname.equals(stmt.coerceStatementArgument())) {
return (StmtContext) stmt;
}
}
return null;
}
// Instantiate this statement's effective substatements. Note this method has side-effects in namespaces and overall
// BuildGlobalContext, hence it must be called at most once.
@Override
void ensureEffectiveSubstatements() {
if (!substatementsInitialized()) {
initializeSubstatements();
}
}
@Override
Iterable> effectiveChildrenToComplete() {
// When we have not initialized, there are no statements to catch up: we will catch up when we are copying
// from prototype (which is already at ModelProcessingPhase.EFFECTIVE_MODEL)
return substatementsInitialized() ? super.effectiveChildrenToComplete() : List.of();
}
private void initializeSubstatements() {
final Collection extends StatementContextBase, ?, ?>> declared = prototype.mutableDeclaredSubstatements();
final Collection extends Mutable, ?, ?>> effective = prototype.mutableEffectiveSubstatements();
final List> buffer = new ArrayList<>(declared.size() + effective.size());
for (final Mutable, ?, ?> stmtContext : declared) {
if (stmtContext.isSupportedByFeatures()) {
copySubstatement(stmtContext, buffer);
}
}
for (final Mutable, ?, ?> stmtContext : effective) {
copySubstatement(stmtContext, buffer);
}
// We are bypassing usual safeties here, as this is not introducing new statements but rather just materializing
// them when the need has arised.
addInitialEffectiveSubstatements(buffer);
}
// Statement copy mess starts here
//
// FIXME: This is messy and is probably wrong in some corner case. Even if it is correct, the way how it is correct
// relies on hard-coded maps. At the end of the day, the logic needs to be controlled by statement's
// StatementSupport.
// FIXME: YANGTOOLS-652: this map looks very much like UsesStatementSupport.TOP_REUSED_DEF_SET
private static final ImmutableSet REUSED_DEF_SET = ImmutableSet.of(
YangStmtMapping.TYPE,
YangStmtMapping.TYPEDEF,
YangStmtMapping.USES);
private void copySubstatement(final Mutable, ?, ?> substatement, final Collection> buffer) {
final StatementDefinition def = substatement.getPublicDefinition();
// FIXME: YANGTOOLS-652: formerly known as "isReusedByUses"
if (REUSED_DEF_SET.contains(def)) {
LOG.debug("Reusing substatement {} for {}", substatement, this);
buffer.add(substatement);
return;
}
substatement.copyAsChildOf(this, childCopyType, targetModule).ifPresent(buffer::add);
}
// Statement copy mess ends here
/*
* KEEP THINGS ORGANIZED!
*
* below methods exist in the same form in SubstatementContext. If any adjustment is made here, make sure it is
* properly updated there.
*/
@Override
@Deprecated
public Optional getSchemaPath() {
return substatementGetSchemaPath();
}
@Override
public A getStatementArgument() {
return argument;
}
@Override
public StatementContextBase, ?, ?> getParentContext() {
return parent;
}
@Override
public StorageNodeType getStorageNodeType() {
return StorageNodeType.STATEMENT_LOCAL;
}
@Override
public StatementContextBase, ?, ?> getParentNamespaceStorage() {
return parent;
}
@Override
public RootStatementContext, ?, ?> getRoot() {
return parent.getRoot();
}
@Override
public boolean isConfiguration() {
return isConfiguration(parent);
}
@Override
protected boolean isIgnoringIfFeatures() {
return isIgnoringIfFeatures(parent);
}
@Override
protected boolean isIgnoringConfig() {
return isIgnoringConfig(parent);
}
@Override
protected boolean isParentSupportedByFeatures() {
return parent.isSupportedByFeatures();
}
}