/*
* 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.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.parser.spi.meta.CopyHistory;
import org.opendaylight.yangtools.yang.parser.spi.meta.CopyType;
import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
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.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 {
private static final Logger LOG = LoggerFactory.getLogger(InferredStatementContext.class);
private final @NonNull StatementContextBase prototype;
private final @NonNull StatementContextBase, ?, ?> parent;
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.argument = original.argument;
}
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;
// FIXME: YANGTOOLS-784: instantiate these lazily
addEffectiveSubstatements(createEffective());
}
@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 prototype.getStatementSourceReference();
}
@Override
public String rawStatementArgument() {
return prototype.rawStatementArgument();
}
@Override
public Optional> getOriginalCtx() {
final Optional> orig = prototype.getOriginalCtx();
return orig.isPresent() ? orig : Optional.of(prototype);
}
@Override
public Optional extends StmtContext, ?, ?>> getPreviousCopyCtx() {
return Optional.of(prototype);
}
@Override
D createDeclared() {
return prototype.buildDeclared();
}
@Override
InferredStatementContext reparent(final StatementContextBase, ?, ?> newParent) {
return new InferredStatementContext<>(this, newParent);
}
// 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.
private List> createEffective() {
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);
}
return 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: these maps look very much like those in UsesStatementImpl
private static final ImmutableSet REUSED_DEF_SET = ImmutableSet.of(
YangStmtMapping.TYPE,
YangStmtMapping.TYPEDEF,
YangStmtMapping.USES);
private static final ImmutableSet NOCOPY_FROM_GROUPING_SET = ImmutableSet.of(
YangStmtMapping.DESCRIPTION,
YangStmtMapping.REFERENCE,
YangStmtMapping.STATUS);
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;
}
// FIXME: YANGTOOLS-652: formerly known as "needToCopyByUses" (note inverted check, though)
if (NOCOPY_FROM_GROUPING_SET.contains(def)) {
// This is to say: if parent of source context is a grouping, ignore this statement.
if (YangStmtMapping.GROUPING.equals(substatement.coerceParentContext().getPublicDefinition())) {
LOG.debug("Skipping grouping statement {}", substatement);
return;
}
}
// FIXME: YANGTOOLS-694: we are forcing a copy here, hence even statements not affected by parent, copyType
// or targetModule (and don't forget its substatements!). This really should be a callout
// to StatementSupport. Note if that callout is allowed to return an Optional, it can
// take care at least of the 'grouping from uses' case above.
final Mutable, ?, ?> copy = childCopyOf(substatement, childCopyType, targetModule);
LOG.debug("Copying substatement {} for {} as {}", substatement, this, copy);
buffer.add(copy);
}
// 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
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 NamespaceStorageNode 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();
}
}