2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
10 import com.google.common.base.Preconditions;
11 import com.google.common.base.Throwables;
12 import com.google.common.base.Verify;
13 import com.google.common.collect.ImmutableList;
14 import com.google.common.collect.Lists;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Iterator;
20 import java.util.List;
22 import java.util.Map.Entry;
23 import java.util.Objects;
25 import javax.annotation.Nonnull;
26 import org.opendaylight.yangtools.yang.common.QName;
27 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
28 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
29 import org.opendaylight.yangtools.yang.model.api.meta.IdentifierNamespace;
30 import org.opendaylight.yangtools.yang.parser.spi.meta.DerivedNamespaceBehaviour;
31 import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
33 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
34 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
35 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceNotAvailableException;
36 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
37 import org.opendaylight.yangtools.yang.parser.spi.meta.SomeModifiersUnresolvedException;
38 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
39 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
40 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
41 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
42 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
43 import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
44 import org.opendaylight.yangtools.yang.parser.stmt.reactor.SourceSpecificContext.PhaseCompletionProgress;
45 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBehaviour.Registry {
50 private static final Logger LOG = LoggerFactory.getLogger(BuildGlobalContext.class);
52 private static final List<ModelProcessingPhase> PHASE_EXECUTION_ORDER = ImmutableList.<ModelProcessingPhase>builder()
53 .add(ModelProcessingPhase.SOURCE_LINKAGE)
54 .add(ModelProcessingPhase.STATEMENT_DEFINITION)
55 .add(ModelProcessingPhase.FULL_DECLARATION)
56 .add(ModelProcessingPhase.EFFECTIVE_MODEL)
59 private final Map<QName,StatementDefinitionContext<?,?,?>> definitions = new HashMap<>();
60 private final Map<Class<?>,NamespaceBehaviourWithListeners<?, ?, ?>> supportedNamespaces = new HashMap<>();
62 private final Map<ModelProcessingPhase,StatementSupportBundle> supports;
63 private final Set<SourceSpecificContext> sources = new HashSet<>();
65 private ModelProcessingPhase currentPhase = ModelProcessingPhase.INIT;
66 private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
68 public BuildGlobalContext(final Map<ModelProcessingPhase, StatementSupportBundle> supports) {
70 this.supports = Preconditions.checkNotNull(supports, "BuildGlobalContext#supports cannot be null");
73 public BuildGlobalContext(final Map<ModelProcessingPhase, StatementSupportBundle> supports, final Map<ValidationBundleType,Collection<?>> supportedValidation) {
75 this.supports = Preconditions.checkNotNull(supports, "BuildGlobalContext#supports cannot be null");
77 for (Entry<ValidationBundleType, Collection<?>> validationBundle : supportedValidation.entrySet()) {
78 addToNs(ValidationBundlesNamespace.class, validationBundle.getKey(), validationBundle.getValue());
82 public StatementSupportBundle getSupportsForPhase(final ModelProcessingPhase currentPhase) {
83 return supports.get(currentPhase);
86 public void addSource(@Nonnull final StatementStreamSource source) {
87 sources.add(new SourceSpecificContext(this,source));
91 public StorageNodeType getStorageNodeType() {
92 return StorageNodeType.GLOBAL;
96 public NamespaceStorageNode getParentNamespaceStorage() {
101 public NamespaceBehaviour.Registry getBehaviourRegistry() {
106 public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviourWithListeners<K, V, N> getNamespaceBehaviour(final Class<N> type) {
107 NamespaceBehaviourWithListeners<?, ?, ?> potential = supportedNamespaces.get(type);
108 if (potential == null) {
109 NamespaceBehaviour<K, V, N> potentialRaw = supports.get(currentPhase).getNamespaceBehaviour(type);
110 if (potentialRaw != null) {
111 potential = createNamespaceContext(potentialRaw);
112 supportedNamespaces.put(type, potential);
114 throw new NamespaceNotAvailableException(
115 "Namespace " + type + " is not available in phase " + currentPhase);
119 Verify.verify(type.equals(potential.getIdentifier()));
121 * Safe cast, previous checkState checks equivalence of key from which type argument are
124 return (NamespaceBehaviourWithListeners<K, V, N>) potential;
127 @SuppressWarnings({"unchecked", "rawtypes"})
128 private <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviourWithListeners<K, V, N> createNamespaceContext(
129 final NamespaceBehaviour<K, V, N> potentialRaw) {
130 if (potentialRaw instanceof DerivedNamespaceBehaviour) {
131 VirtualNamespaceContext derivedContext =
132 new VirtualNamespaceContext((DerivedNamespaceBehaviour) potentialRaw);
133 getNamespaceBehaviour(((DerivedNamespaceBehaviour) potentialRaw).getDerivedFrom())
134 .addDerivedNamespace(derivedContext);
135 return derivedContext;
137 return new SimpleNamespaceContext<>(potentialRaw);
140 public StatementDefinitionContext<?, ?, ?> getStatementDefinition(final QName name) {
141 StatementDefinitionContext<?, ?, ?> potential = definitions.get(name);
142 if (potential == null) {
143 StatementSupport<?, ?, ?> potentialRaw = supports.get(currentPhase).getStatementDefinition(name);
144 if (potentialRaw != null) {
145 potential = new StatementDefinitionContext<>(potentialRaw);
146 definitions.put(name, potential);
152 public EffectiveModelContext build() throws SourceException, ReactorException {
153 for (ModelProcessingPhase phase : PHASE_EXECUTION_ORDER) {
155 loadPhaseStatements();
156 completePhaseActions();
162 private EffectiveModelContext transform() {
163 Preconditions.checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
164 List<DeclaredStatement<?>> rootStatements = new ArrayList<>(sources.size());
165 for (SourceSpecificContext source : sources) {
166 rootStatements.add(source.getRoot().buildDeclared());
168 return new EffectiveModelContext(rootStatements);
171 public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
172 for (ModelProcessingPhase phase : PHASE_EXECUTION_ORDER) {
174 loadPhaseStatements();
175 completePhaseActions();
178 return transformEffective();
181 private EffectiveSchemaContext transformEffective() {
182 Preconditions.checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
183 List<DeclaredStatement<?>> rootStatements = new ArrayList<>(sources.size());
184 List<EffectiveStatement<?,?>> rootEffectiveStatements = new ArrayList<>(sources.size());
186 for (SourceSpecificContext source : sources) {
187 final RootStatementContext<?, ?, ?> root = source.getRoot();
188 rootStatements.add(root.buildDeclared());
189 rootEffectiveStatements.add(root.buildEffective());
192 return new EffectiveSchemaContext(rootStatements, rootEffectiveStatements);
195 private void startPhase(final ModelProcessingPhase phase) {
196 Preconditions.checkState(Objects.equals(finishedPhase, phase.getPreviousPhase()));
197 for (SourceSpecificContext source : sources) {
198 source.startPhase(phase);
200 currentPhase = phase;
203 private void loadPhaseStatements() throws SourceException {
204 Preconditions.checkState(currentPhase != null);
205 for (SourceSpecificContext source : sources) {
206 source.loadStatements();
210 private SomeModifiersUnresolvedException addSourceExceptions(final SomeModifiersUnresolvedException buildFailure,
211 final List<SourceSpecificContext> sourcesToProgress) {
212 boolean addedCause = false;
213 for (SourceSpecificContext failedSource : sourcesToProgress) {
214 final SourceException sourceEx = failedSource.failModifiers(currentPhase);
216 // Workaround for broken logging implementations which ignore suppressed exceptions
217 Throwable cause = sourceEx.getCause() != null ? sourceEx.getCause() : sourceEx;
218 if (LOG.isDebugEnabled()) {
219 LOG.error("Failed to parse YANG from source {}", failedSource, sourceEx);
221 LOG.error("Failed to parse YANG from source {}: {}", failedSource, cause.getMessage());
224 final Throwable[] suppressed = sourceEx.getSuppressed();
225 if (suppressed.length > 0) {
226 LOG.error("{} additional errors reported:", suppressed.length);
229 for (Throwable t : suppressed) {
230 // FIXME: this should be configured in the appender, really
231 if (LOG.isDebugEnabled()) {
232 LOG.error("Error {}: {}", i, t.getMessage(), t);
234 LOG.error("Error {}: {}", i, t.getMessage());
243 buildFailure.initCause(sourceEx);
245 buildFailure.addSuppressed(sourceEx);
251 private void completePhaseActions() throws ReactorException {
252 Preconditions.checkState(currentPhase != null);
253 List<SourceSpecificContext> sourcesToProgress = Lists.newArrayList(sources);
255 boolean progressing = true;
256 while (progressing) {
257 // We reset progressing to false.
259 Iterator<SourceSpecificContext> currentSource = sourcesToProgress.iterator();
260 while(currentSource.hasNext()) {
261 SourceSpecificContext nextSourceCtx = currentSource.next();
262 PhaseCompletionProgress sourceProgress = nextSourceCtx.tryToCompletePhase(currentPhase);
263 switch (sourceProgress) {
265 currentSource.remove();
266 // Fallback to progress, since we were able to make progress in computation
274 throw new IllegalStateException("Unsupported phase progress " + sourceProgress);
278 } catch (SourceException e) {
279 throw Throwables.propagate(e);
281 if (!sourcesToProgress.isEmpty()) {
282 SomeModifiersUnresolvedException buildFailure = new SomeModifiersUnresolvedException(currentPhase);
283 buildFailure = addSourceExceptions(buildFailure, sourcesToProgress);
288 private void endPhase(final ModelProcessingPhase phase) {
289 Preconditions.checkState(currentPhase == phase);
290 finishedPhase = currentPhase;
293 public Set<SourceSpecificContext> getSources() {