/* * Copyright (c) 2019 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.module; import static com.google.common.base.Verify.verify; import static java.util.Objects.requireNonNull; import com.google.common.collect.ForwardingObject; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import java.util.ArrayList; import java.util.Collection; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; import org.opendaylight.yangtools.yang.common.YangVersion; import org.opendaylight.yangtools.yang.model.api.DataNodeContainer; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaNode; import org.opendaylight.yangtools.yang.model.api.SchemaPath; 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.StatementDefinition; import org.opendaylight.yangtools.yang.model.api.meta.StatementSource; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement; import org.opendaylight.yangtools.yang.model.api.stmt.ModuleStatement; import org.opendaylight.yangtools.yang.parser.spi.meta.CopyHistory; import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase; import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext; import org.opendaylight.yangtools.yang.parser.spi.source.IncludedSubmoduleNameToModuleCtx; import org.opendaylight.yangtools.yang.parser.spi.source.StatementSourceReference; /** * A concentrator {@link StmtContext}, which makes it appear as though as all effective statements in submodules * are included in it. */ final class ModuleStmtContext extends ForwardingObject implements StmtContext { private final @NonNull StmtContext delegate; private final @NonNull ImmutableList> effectiveSubstatements; private final @NonNull ImmutableSet submodules; private ModuleStmtContext(final StmtContext delegate, final Collection> submodules) { this.delegate = requireNonNull(delegate); final List> statements = new ArrayList<>(delegate.effectiveSubstatements()); final Set subs = new LinkedHashSet<>(submodules.size()); for (StmtContext submoduleCtx : submodules) { final EffectiveStatement submodule = submoduleCtx.buildEffective(); verify(submodule instanceof Module, "Submodule statement %s is not a Module", submodule); subs.add((Module) submodule); for (StmtContext stmt : submoduleCtx.allSubstatements()) { if (stmt.isSupportedByFeatures()) { final EffectiveStatement effective = stmt.buildEffective(); if (effective instanceof SchemaNode || effective instanceof DataNodeContainer) { statements.add(stmt); } } } } this.effectiveSubstatements = ImmutableList.copyOf(statements); this.submodules = ImmutableSet.copyOf(subs); } static @NonNull ModuleStmtContext create( final StmtContext delegate) { final Map> includedSubmodules = delegate.getAllFromCurrentStmtCtxNamespace( IncludedSubmoduleNameToModuleCtx.class); return new ModuleStmtContext(delegate, includedSubmodules == null || includedSubmodules.isEmpty() ? ImmutableList.of() : includedSubmodules.values()); } @Override protected @NonNull StmtContext delegate() { return delegate; } ImmutableSet getSubmodules() { return submodules; } @Override public ImmutableList> effectiveSubstatements() { return effectiveSubstatements; } @Override public ModuleEffectiveStatement buildEffective() { throw new UnsupportedOperationException("Attempted to instantiate proxy context " + this); } @Override public ModuleStatement buildDeclared() { return delegate.buildDeclared(); } @Override public StatementSource getStatementSource() { return delegate.getStatementSource(); } @Override public StatementSourceReference getStatementSourceReference() { return delegate.getStatementSourceReference(); } @Override public StatementDefinition getPublicDefinition() { return delegate.getPublicDefinition(); } @Override public StmtContext getParentContext() { return delegate.getParentContext(); } @Override public String rawStatementArgument() { return delegate.rawStatementArgument(); } @Override public @Nullable String getStatementArgument() { return delegate.getStatementArgument(); } @Override public @NonNull Optional getSchemaPath() { return delegate.getSchemaPath(); } @Override public boolean isConfiguration() { return delegate.isConfiguration(); } @Override public boolean isEnabledSemanticVersioning() { return delegate.isEnabledSemanticVersioning(); } @Override public > @NonNull V getFromNamespace(final Class type, final T key) { return delegate.getFromNamespace(type, key); } @Override public > Map getAllFromNamespace(final Class type) { return delegate.getAllFromNamespace(type); } @Override public > Map getAllFromCurrentStmtCtxNamespace( final Class type) { return delegate.getAllFromCurrentStmtCtxNamespace(type); } @Override public StmtContext getRoot() { return delegate.getRoot(); } @Override public Collection> declaredSubstatements() { return delegate.declaredSubstatements(); } @Override public boolean isSupportedToBuildEffective() { return delegate.isSupportedToBuildEffective(); } @Override public boolean isSupportedByFeatures() { return delegate.isSupportedByFeatures(); } @Override public Collection> getEffectOfStatement() { return delegate.getEffectOfStatement(); } @Override public CopyHistory getCopyHistory() { return delegate.getCopyHistory(); } @Override public Optional> getOriginalCtx() { return delegate.getOriginalCtx(); } @Override public Optional> getPreviousCopyCtx() { return delegate.getPreviousCopyCtx(); } @Override public ModelProcessingPhase getCompletedPhase() { return delegate.getCompletedPhase(); } @Override public @NonNull YangVersion getRootVersion() { return delegate.getRootVersion(); } }