/* * 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.model.spi.meta; 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.Map; import java.util.Optional; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; 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.StatementOrigin; 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; /** * 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 StatementOrigin statementOrigin() { return StatementOrigin.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#dataChildByName(QName)}. */ protected final @Nullable DataSchemaNode dataSchemaNode(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 ? (DataSchemaNode) child : null; } 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); } protected Default(final Default original) { this.declared = original.declared; } @Override public final D getDeclared() { return declared; } } /** * 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); } protected WithSubstatements(final WithSubstatements original) { super(original); this.substatements = original.substatements; } @Override public final ImmutableList> effectiveSubstatements() { return unmaskList(substatements); } } protected DefaultArgument(final D declared) { super(declared); } protected DefaultArgument(final DefaultArgument original) { super(original); } @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 ImmutableList> substatements) { super(declared, substatements); this.substatements = maskList(substatements); } protected WithSubstatements(final WithSubstatements original) { super(original); this.substatements = original.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 ImmutableList> substatements) { this.declared = requireNonNull(declared); this.schemaTree = ImmutableMap.copyOf(createSchemaTreeNamespace(substatements)); } protected DefaultWithSchemaTree(final DefaultWithSchemaTree original) { this.declared = original.declared; this.schemaTree = original.schemaTree; } @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 ImmutableList> substatements) { super(declared, substatements); this.substatements = maskList(substatements); } protected WithSubstatements(final WithSubstatements original) { super(original); this.substatements = original.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 ImmutableList> substatements) { this.declared = requireNonNull(declared); final Map> schema = createSchemaTreeNamespace(substatements); this.schemaTree = ImmutableMap.copyOf(schema); this.dataTree = createDataTreeNamespace(schema.values(), schemaTree); } protected DefaultWithDataTree(final DefaultWithDataTree original) { this.declared = original.declared; this.schemaTree = original.schemaTree; this.dataTree = original.dataTree; } @Override public final D getDeclared() { return declared; } @Override protected final Map> schemaTreeNamespace() { return schemaTree; } @Override protected final Map> dataTreeNamespace() { return dataTree; } } }