Cleanup yang-parser-impl
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / BuildGlobalContext.java
index 90b41b99120af501050a5b1a8d2ec0b43f46f8d9..a7041f2f14040c117557dc1f7bd75c0e7696e564 100644 (file)
@@ -7,24 +7,24 @@
  */
 package org.opendaylight.yangtools.yang.parser.stmt.reactor;
 
-import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
-
-import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
 import com.google.common.base.Preconditions;
 import com.google.common.base.Throwables;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Set;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.common.QName;
 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.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
@@ -37,7 +37,10 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupportBundle;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
 import org.opendaylight.yangtools.yang.parser.spi.source.StatementStreamSource;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.validation.ValidationBundlesNamespace.ValidationBundleType;
 import org.opendaylight.yangtools.yang.parser.stmt.reactor.SourceSpecificContext.PhaseCompletionProgress;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.effective.EffectiveSchemaContext;
 
 class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBehaviour.Registry {
 
@@ -49,25 +52,35 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
             .build();
 
     private final Map<QName,StatementDefinitionContext<?,?,?>> definitions = new HashMap<>();
-    private final Map<Class<?>,NamespaceBehaviourWithListeners<?, ?, ?>> namespaces = new HashMap<>();
+    private final Map<Class<?>,NamespaceBehaviourWithListeners<?, ?, ?>> supportedNamespaces = new HashMap<>();
 
 
     private final Map<ModelProcessingPhase,StatementSupportBundle> supports;
     private final Set<SourceSpecificContext> sources = new HashSet<>();
 
-    private ModelProcessingPhase currentPhase;
-    private ModelProcessingPhase finishedPhase;
+    private ModelProcessingPhase currentPhase = ModelProcessingPhase.INIT;
+    private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
+
+    public BuildGlobalContext(final Map<ModelProcessingPhase, StatementSupportBundle> supports) {
+        super();
+        this.supports = Preconditions.checkNotNull(supports, "BuildGlobalContext#supports cannot be null");
+    }
 
-    public BuildGlobalContext(Map<ModelProcessingPhase, StatementSupportBundle> supports) {
+    public BuildGlobalContext(final Map<ModelProcessingPhase, StatementSupportBundle> supports,  final Map<ValidationBundleType,Collection<?>> supportedValidation) {
         super();
-        this.supports = supports;
+        this.supports = Preconditions.checkNotNull(supports, "BuildGlobalContext#supports cannot be null");
+
+        Set<Entry<ValidationBundleType, Collection<?>>> validationBundles = supportedValidation.entrySet();
+        for (Entry<ValidationBundleType, Collection<?>> validationBundle : validationBundles) {
+            addToNs(ValidationBundlesNamespace.class, validationBundle.getKey(), validationBundle.getValue());
+        }
     }
 
-    public StatementSupportBundle getSupportsForPhase(ModelProcessingPhase currentPhase) {
+    public StatementSupportBundle getSupportsForPhase(final ModelProcessingPhase currentPhase) {
         return supports.get(currentPhase);
     }
 
-    public void addSource(@Nonnull StatementStreamSource source) {
+    public void addSource(@Nonnull final StatementStreamSource source) {
         sources.add(new SourceSpecificContext(this,source));
     }
 
@@ -87,13 +100,13 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
     }
 
     @Override
-    public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviourWithListeners<K, V, N> getNamespaceBehaviour(Class<N> type) {
-        NamespaceBehaviourWithListeners<?, ?, ?> potential = namespaces.get(type);
+    public <K, V, N extends IdentifierNamespace<K, V>> NamespaceBehaviourWithListeners<K, V, N> getNamespaceBehaviour(final Class<N> type) {
+        NamespaceBehaviourWithListeners<?, ?, ?> potential = supportedNamespaces.get(type);
         if (potential == null) {
             NamespaceBehaviour<K, V, N> potentialRaw = supports.get(currentPhase).getNamespaceBehaviour(type);
             if(potentialRaw != null) {
                 potential = new NamespaceBehaviourWithListeners<>(potentialRaw);
-                namespaces.put(type, potential);
+                supportedNamespaces.put(type, potential);
             }
         }
         if (potential != null) {
@@ -108,7 +121,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         throw new NamespaceNotAvailableException("Namespace " + type + " is not available in phase " + currentPhase);
     }
 
-    public StatementDefinitionContext<?, ?, ?> getStatementDefinition(QName name) {
+    public StatementDefinitionContext<?, ?, ?> getStatementDefinition(final QName name) {
         StatementDefinitionContext<?, ?, ?> potential = definitions.get(name);
         if(potential == null) {
             StatementSupport<?, ?, ?> potentialRaw = supports.get(currentPhase).getStatementDefinition(name);
@@ -166,7 +179,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         return new EffectiveSchemaContext(rootStatements,rootEffectiveStatements);
     }
 
-    private void startPhase(ModelProcessingPhase phase) {
+    private void startPhase(final ModelProcessingPhase phase) {
         Preconditions.checkState(Objects.equals(finishedPhase, phase.getPreviousPhase()));
         for(SourceSpecificContext source : sources) {
             source.startPhase(phase);
@@ -181,6 +194,15 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         }
     }
 
+    private SomeModifiersUnresolvedException addSourceExceptions(final SomeModifiersUnresolvedException buildFailure,
+            final List<SourceSpecificContext> sourcesToProgress) {
+        for(SourceSpecificContext failedSource : sourcesToProgress) {
+            SourceException sourceEx = failedSource.failModifiers(currentPhase);
+            buildFailure.addSuppressed(sourceEx);
+        }
+        return buildFailure;
+    }
+
     private  void completePhaseActions() throws ReactorException {
         Preconditions.checkState(currentPhase != null);
         List<SourceSpecificContext> sourcesToProgress = Lists.newArrayList(sources);
@@ -191,7 +213,8 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
                 progressing = false;
                 Iterator<SourceSpecificContext> currentSource = sourcesToProgress.iterator();
                 while(currentSource.hasNext()) {
-                    PhaseCompletionProgress sourceProgress = currentSource.next().tryToCompletePhase(currentPhase);
+                    SourceSpecificContext nextSourceCtx = currentSource.next();
+                    PhaseCompletionProgress sourceProgress = nextSourceCtx.tryToCompletePhase(currentPhase);
                     switch (sourceProgress) {
                         case FINISHED:
                             currentSource.remove();
@@ -212,15 +235,12 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         }
         if(!sourcesToProgress.isEmpty()) {
             SomeModifiersUnresolvedException buildFailure = new SomeModifiersUnresolvedException(currentPhase);
-                for(SourceSpecificContext failedSource : sourcesToProgress) {
-                    SourceException sourceEx = failedSource.failModifiers(currentPhase);
-                    buildFailure.addSuppressed(sourceEx);
-                }
-                throw buildFailure;
+            buildFailure = addSourceExceptions(buildFailure, sourcesToProgress);
+            throw buildFailure;
         }
     }
 
-    private  void endPhase(ModelProcessingPhase phase) {
+    private  void endPhase(final ModelProcessingPhase phase) {
         Preconditions.checkState(currentPhase == phase);
         finishedPhase = currentPhase;
     }