*/
package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.list;
+import com.google.common.collect.ImmutableList;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
+import java.util.Set;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.ElementCountConstraint;
+import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.ListSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
import org.opendaylight.yangtools.yang.model.api.YangStmtMapping;
import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.KeyEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.ListEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.ListStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.OrderedByEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
+import org.opendaylight.yangtools.yang.model.api.stmt.StatusEffectiveStatement;
import org.opendaylight.yangtools.yang.parser.rfc7950.namespace.ChildSchemaNodeNamespace;
-import org.opendaylight.yangtools.yang.parser.spi.meta.AbstractQNameStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.BaseQNameStatementSupport;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.EffectiveStatementWithFlags.FlagsBuilder;
+import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStmtUtils;
+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.Mutable;
import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContextUtils;
+import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
-abstract class AbstractListStatementSupport
- extends AbstractQNameStatementSupport<ListStatement, EffectiveStatement<QName, ListStatement>> {
+abstract class AbstractListStatementSupport extends BaseQNameStatementSupport<ListStatement, ListEffectiveStatement> {
+ private static final Logger LOG = LoggerFactory.getLogger(AbstractListStatementSupport.class);
AbstractListStatementSupport() {
super(YangStmtMapping.LIST);
}
@Override
- public final void onStatementAdded(
- final Mutable<QName, ListStatement, EffectiveStatement<QName, ListStatement>> stmt) {
+ public final void onStatementAdded(final Mutable<QName, ListStatement, ListEffectiveStatement> stmt) {
stmt.coerceParentContext().addToNs(ChildSchemaNodeNamespace.class, stmt.coerceStatementArgument(), stmt);
}
}
@Override
- public final EffectiveStatement<QName, ListStatement> createEffective(
- final StmtContext<QName, ListStatement, EffectiveStatement<QName, ListStatement>> ctx) {
- return new ListEffectiveStatementImpl(ctx);
+ protected final ListEffectiveStatement createEffective(
+ final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx,
+ final ListStatement declared, final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ final StatementSourceReference ref = ctx.getStatementSourceReference();
+ final SchemaPath path = ctx.getSchemaPath().get();
+ final ListSchemaNode original = (ListSchemaNode) ctx.getOriginalCtx().map(StmtContext::buildEffective)
+ .orElse(null);
+
+ final ImmutableList<QName> keyDefinition;
+ final KeyEffectiveStatement keyStmt = findFirstStatement(substatements, KeyEffectiveStatement.class);
+ if (keyStmt != null) {
+ final List<QName> keyDefinitionInit = new ArrayList<>(keyStmt.argument().size());
+ final Set<QName> possibleLeafQNamesForKey = new HashSet<>();
+ for (final EffectiveStatement<?, ?> effectiveStatement : substatements) {
+ if (effectiveStatement instanceof LeafSchemaNode) {
+ possibleLeafQNamesForKey.add(((LeafSchemaNode) effectiveStatement).getQName());
+ }
+ }
+ for (final SchemaNodeIdentifier key : keyStmt.argument()) {
+ final QName keyQName = key.getLastComponent();
+
+ if (!possibleLeafQNamesForKey.contains(keyQName)) {
+ throw new InferenceException(ref, "Key '%s' misses node '%s' in list '%s'",
+ keyStmt.getDeclared().rawArgument(), keyQName.getLocalName(), ctx.getStatementArgument());
+ }
+ keyDefinitionInit.add(keyQName);
+ }
+
+ keyDefinition = ImmutableList.copyOf(keyDefinitionInit);
+ } else {
+ keyDefinition = ImmutableList.of();
+ }
+
+ final boolean configuration = ctx.isConfiguration();
+ final int flags = new FlagsBuilder()
+ .setHistory(ctx.getCopyHistory())
+ .setStatus(findFirstArgument(substatements, StatusEffectiveStatement.class, Status.CURRENT))
+ .setConfiguration(configuration)
+ .setUserOrdered(findFirstArgument(substatements, OrderedByEffectiveStatement.class, "system")
+ .equals("user"))
+ .toFlags();
+ if (configuration && keyDefinition.isEmpty()) {
+ LOG.info("Configuration list {} does not define any keys in violation of RFC7950 section 7.8.2. While "
+ + " this is fine with OpenDaylight, it can cause interoperability issues with other systems "
+ + "[at {}]", ctx.getStatementArgument(), ref);
+ }
+
+ final Optional<ElementCountConstraint> elementCountConstraint =
+ EffectiveStmtUtils.createElementCountConstraint(substatements);
+ return original == null && !elementCountConstraint.isPresent()
+ ? new EmptyListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition)
+ : new RegularListEffectiveStatement(declared, path, flags, ctx, substatements, keyDefinition,
+ elementCountConstraint.orElse(null), original);
+ }
+
+ @Override
+ protected final ListEffectiveStatement createEmptyEffective(
+ final StmtContext<QName, ListStatement, ListEffectiveStatement> ctx, final ListStatement declared) {
+ return createEffective(ctx, declared, ImmutableList.of());
}
-}
\ No newline at end of file
+}