/* * 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.rfc7950.stmt; import static com.google.common.base.Verify.verify; import static java.util.Objects.requireNonNull; import com.google.common.annotations.Beta; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.HashMap; import java.util.Map; import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; 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.model.api.meta.StatementSource; import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeAwareEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement; import org.opendaylight.yangtools.yang.parser.rfc7950.stmt.EffectiveStatementMixins.DataNodeContainerMixin; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.source.SourceException; import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference; /** * Base stateless superclass for statements which (logically) always have an associated {@link DeclaredStatement}. This * is notably not true for all {@code case} statements, some of which may actually be implied. * *

* Note implementations are not strictly required to make the declared statement available, they are free to throw * {@link UnsupportedOperationException} from {@link #getDeclared()}, rendering any services relying on declared * statement to be not available. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. */ @Beta public abstract class AbstractDeclaredEffectiveStatement> extends AbstractEffectiveStatement { @Override public final StatementSource getStatementSource() { return StatementSource.DECLARATION; } @Override public abstract @NonNull D getDeclared(); /** * Base stateless superclass form {@link SchemaTreeAwareEffectiveStatement}s. It maintains the contents of schema * tree namespace based of effective substatements. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. * @param Class representing effective version of this statement. */ public abstract static class WithSchemaTree, E extends SchemaTreeAwareEffectiveStatement> extends AbstractDeclaredEffectiveStatement { @Override @SuppressWarnings("unchecked") protected > Optional> getNamespaceContents( final Class namespace) { if (SchemaTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) { return Optional.of((Map) schemaTreeNamespace()); } return super.getNamespaceContents(namespace); } /** * Indexing support for {@link DataNodeContainer#findDataChildByName(QName)}. */ protected final Optional findDataSchemaNode(final QName name) { // Only DataNodeContainer subclasses should be calling this method verify(this instanceof DataNodeContainer); final SchemaTreeEffectiveStatement child = schemaTreeNamespace().get(requireNonNull(name)); return child instanceof DataSchemaNode ? Optional.of((DataSchemaNode) child) : Optional.empty(); } protected abstract Map> schemaTreeNamespace(); } /** * Base stateless superclass form {@link DataTreeAwareEffectiveStatement}s. It maintains the contents of data tree * namespace based of effective substatements. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. * @param Class representing effective version of this statement. */ public abstract static class WithDataTree, E extends DataTreeAwareEffectiveStatement> extends WithSchemaTree { @Override @SuppressWarnings("unchecked") protected > Optional> getNamespaceContents( final Class namespace) { if (DataTreeAwareEffectiveStatement.Namespace.class.equals(namespace)) { return Optional.of((Map) dataTreeNamespace()); } return super.getNamespaceContents(namespace); } protected abstract Map> dataTreeNamespace(); } /** * A stateful version of {@link AbstractDeclaredEffectiveStatement}, which holds (and requires) a declared * statement. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. */ public abstract static class Default> extends AbstractDeclaredEffectiveStatement { private final @NonNull D declared; protected Default(final D declared) { this.declared = requireNonNull(declared); } @Override public final D getDeclared() { return declared; } } /** * Utility class for implementing DataNodeContainer-type statements. */ public abstract static class DefaultDataNodeContainer> extends Default implements DataNodeContainerMixin { private final @NonNull ImmutableMap dataChildren; private final @NonNull Object substatements; protected DefaultDataNodeContainer(final D declared, final StatementSourceReference ref, final ImmutableList> substatements) { super(declared); this.substatements = maskList(substatements); // Note: we do not leak this map, so iteration order does not matter final Map tmp = new HashMap<>(); for (EffectiveStatement stmt : effectiveSubstatements()) { if (stmt instanceof DataSchemaNode) { final DataSchemaNode node = (DataSchemaNode) stmt; final QName id = node.getQName(); final DataSchemaNode prev = tmp.put(id, node); SourceException.throwIf(prev != null, ref, "Cannot add child with name %s, a conflicting child already exists", id); } } dataChildren = ImmutableMap.copyOf(tmp); } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } @Override public final Optional findDataChildByName(final QName name) { return Optional.ofNullable(dataChildren.get(requireNonNull(name))); } } /** * An extra building block on top of {@link Default}, which is wiring {@link #argument()} to the declared statement. * This is mostly useful for arguments that are not subject to inference transformation -- for example Strings in * {@code description}, etc. This explicitly is not true of statements which underwent namespace binding via * {@code uses} or similar. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. */ public abstract static class DefaultArgument> extends Default { public abstract static class WithSubstatements> extends DefaultArgument { private final @NonNull Object substatements; protected WithSubstatements(final D declared, final ImmutableList> substatements) { super(declared); this.substatements = maskList(substatements); } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } } protected DefaultArgument(final D declared) { super(declared); } @Override public final A argument() { return getDeclared().argument(); } } /** * A building block on top of {@link Default}, which adds an explicit argument value, which is not related to the * context. This is mostly useful when the effective argument value reflects additional statements and similar. * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. */ public abstract static class DefaultWithArgument> extends Default { public abstract static class WithSubstatements> extends DefaultWithArgument { private final @NonNull Object substatements; protected WithSubstatements(final D declared, final A argument, final ImmutableList> substatements) { super(declared, argument); this.substatements = maskList(substatements); } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } } private final A argument; protected DefaultWithArgument(final D declared, final A argument) { super(declared); this.argument = argument; } @Override public final A argument() { return argument; } } /** * Stateful version of {@link WithSchemaTree}. Schema tree namespace is eagerly instantiated (and checked). * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. * @param Class representing effective version of this statement. */ public abstract static class DefaultWithSchemaTree, E extends SchemaTreeAwareEffectiveStatement> extends WithSchemaTree { public abstract static class WithSubstatements, E extends SchemaTreeAwareEffectiveStatement> extends DefaultWithSchemaTree { private final @NonNull Object substatements; protected WithSubstatements(final D declared, final StmtContext ctx, final ImmutableList> substatements) { super(declared, ctx, substatements); this.substatements = maskList(substatements); } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } } private final @NonNull ImmutableMap> schemaTree; private final @NonNull D declared; protected DefaultWithSchemaTree(final D declared, final StmtContext ctx, final ImmutableList> substatements) { this.declared = requireNonNull(declared); this.schemaTree = ImmutableMap.copyOf(createSchemaTreeNamespace( ctx.getStatementSourceReference(), substatements)); } @Override public final D getDeclared() { return declared; } @Override protected final Map> schemaTreeNamespace() { return schemaTree; } } /** * Stateful version of {@link WithDataTree}. Schema tree and data tree namespaces are eagerly instantiated * (and checked). * * @param Argument type ({@link Void} if statement does not have argument.) * @param Class representing declared version of this statement. * @param Class representing effective version of this statement. */ public abstract static class DefaultWithDataTree, E extends DataTreeAwareEffectiveStatement> extends WithDataTree { public abstract static class WithSubstatements, E extends DataTreeAwareEffectiveStatement> extends DefaultWithDataTree { private final @NonNull Object substatements; protected WithSubstatements(final D declared, final StmtContext ctx, final ImmutableList> substatements) { super(declared, ctx, substatements); this.substatements = maskList(substatements); } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } } private final @NonNull ImmutableMap> schemaTree; private final @NonNull ImmutableMap> dataTree; private final @NonNull D declared; protected DefaultWithDataTree(final D declared, final StmtContext ctx, final ImmutableList> substatements) { this.declared = requireNonNull(declared); final StatementSourceReference ref = ctx.getStatementSourceReference(); final Map> schema = createSchemaTreeNamespace(ref, substatements); this.schemaTree = ImmutableMap.copyOf(schema); this.dataTree = createDataTreeNamespace(ref, schema.values(), schemaTree); } @Override public final D getDeclared() { return declared; } @Override protected final Map> schemaTreeNamespace() { return schemaTree; } @Override protected final Map> dataTreeNamespace() { return dataTree; } } }