Merge "Introduce InstanceIdToNodes into yang-data-impl"
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / BuildGlobalContext.java
index b5380224047a81674756715b0d2fed91b35349b8..ad6d70a823c9060d454394b2460d8cc43ce4f2b3 100644 (file)
@@ -7,6 +7,9 @@
  */
 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;
@@ -39,10 +42,10 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.SourceSpecificContext
 class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBehaviour.Registry {
 
     private static final List<ModelProcessingPhase> PHASE_EXECUTION_ORDER = ImmutableList.<ModelProcessingPhase>builder()
-            .add(ModelProcessingPhase.SourceLinkage)
-            .add(ModelProcessingPhase.StatementDefinition)
-            .add(ModelProcessingPhase.FullDeclaration)
-            .add(ModelProcessingPhase.EffectiveModel)
+            .add(ModelProcessingPhase.SOURCE_LINKAGE)
+            .add(ModelProcessingPhase.STATEMENT_DEFINITION)
+            .add(ModelProcessingPhase.FULL_DECLARATION)
+            .add(ModelProcessingPhase.EFFECTIVE_MODEL)
             .build();
 
     private final Map<QName,StatementDefinitionContext<?,?,?>> definitions = new HashMap<>();
@@ -60,13 +63,17 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         this.supports = supports;
     }
 
+    public StatementSupportBundle getSupportsForPhase(ModelProcessingPhase currentPhase) {
+        return supports.get(currentPhase);
+    }
+
     public void addSource(@Nonnull StatementStreamSource source) {
         sources.add(new SourceSpecificContext(this,source));
     }
 
     @Override
     public StorageNodeType getStorageNodeType() {
-        return StorageNodeType.Global;
+        return StorageNodeType.GLOBAL;
     }
 
     @Override
@@ -96,9 +103,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
              * Safe cast, previous checkState checks equivalence of key from
              * which type argument are derived
              */
-            @SuppressWarnings("unchecked")
-            NamespaceBehaviourWithListeners<K, V, N> casted = (NamespaceBehaviourWithListeners<K, V, N>) potential;
-            return casted;
+            return (NamespaceBehaviourWithListeners<K, V, N>) potential;
         }
         throw new NamespaceNotAvailableException("Namespace " + type + "is not available in phase " + currentPhase);
     }
@@ -126,7 +131,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
     }
 
     private EffectiveModelContext transform() {
-        Preconditions.checkState(finishedPhase == ModelProcessingPhase.EffectiveModel);
+        Preconditions.checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
         List<DeclaredStatement<?>> rootStatements = new ArrayList<>();
         for(SourceSpecificContext source : sources) {
             DeclaredStatement<?> root = source.getRoot().buildDeclared();
@@ -135,6 +140,32 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
         return new EffectiveModelContext(rootStatements);
     }
 
+    public EffectiveSchemaContext buildEffective() throws SourceException, ReactorException {
+        for(ModelProcessingPhase phase : PHASE_EXECUTION_ORDER) {
+            startPhase(phase);
+            loadPhaseStatements();
+            completePhaseActions();
+            endPhase(phase);
+        }
+        return transformEffective();
+    }
+
+    private EffectiveSchemaContext transformEffective() {
+        Preconditions.checkState(finishedPhase == ModelProcessingPhase.EFFECTIVE_MODEL);
+        List<DeclaredStatement<?>> rootStatements = new ArrayList<>();
+        List<EffectiveStatement<?,?>> rootEffectiveStatements = new ArrayList<>();
+
+        for(SourceSpecificContext source : sources) {
+            DeclaredStatement<?> root = source.getRoot().buildDeclared();
+            rootStatements.add(root);
+
+            EffectiveStatement<?,?> rootEffective = source.getRoot().buildEffective();
+            rootEffectiveStatements.add(rootEffective);
+        }
+
+        return new EffectiveSchemaContext(rootStatements,rootEffectiveStatements);
+    }
+
     private void startPhase(ModelProcessingPhase phase) {
         Preconditions.checkState(Objects.equals(finishedPhase, phase.getPreviousPhase()));
         for(SourceSpecificContext source : sources) {
@@ -152,7 +183,7 @@ class BuildGlobalContext extends NamespaceStorageSupport implements NamespaceBeh
 
     private  void completePhaseActions() throws ReactorException {
         Preconditions.checkState(currentPhase != null);
-        ArrayList<SourceSpecificContext> sourcesToProgress = Lists.newArrayList(sources);
+        List<SourceSpecificContext> sourcesToProgress = Lists.newArrayList(sources);
         try {
             boolean progressing = true;
             while(progressing) {