BUG-6522: Improve parser reactor logging
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / stmt / reactor / SourceSpecificContext.java
index 297b99561ab25f212ec0c406640ecdc24218fffc..48447300ef9699139cea6c389d23bf558ef650c1 100644 (file)
@@ -19,6 +19,7 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Objects;
+import java.util.Optional;
 import javax.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Mutable;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -37,7 +38,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.NamespaceStorageNode;
 import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour.StorageNodeType;
-import org.opendaylight.yangtools.yang.parser.spi.meta.QNameCacheNamespace;
+import org.opendaylight.yangtools.yang.parser.spi.meta.StatementDefinitionNamespace;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StatementSupport;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.BelongsToPrefixToModuleIdentifier;
@@ -57,10 +58,13 @@ import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.EnumSpecificationImpl
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.IdentityRefSpecificationImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.InstanceIdentifierSpecificationImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.LeafrefSpecificationImpl;
+import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.ModelDefinedStatementDefinition;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.TypeUtils;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnionSpecificationImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.UnknownStatementImpl;
 import org.opendaylight.yangtools.yang.parser.stmt.rfc6020.Utils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBehaviour.Registry, Mutable {
 
@@ -70,22 +74,24 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         FINISHED
     }
 
+    private static final Logger LOG = LoggerFactory.getLogger(SourceSpecificContext.class);
+
     private final StatementStreamSource source;
     private final BuildGlobalContext currentContext;
     private final Collection<NamespaceStorageNode> importedNamespaces = new ArrayList<>();
     private final Multimap<ModelProcessingPhase, ModifierImpl> modifiers = HashMultimap.create();
 
-    private RootStatementContext<?, ?, ?> root;
 
-    private ModelProcessingPhase inProgressPhase;
-    private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
     private final QNameToStatementDefinitionMap qNameToStmtDefMap = new QNameToStatementDefinitionMap();
     private final PrefixToModuleMap prefixToModuleMap = new PrefixToModuleMap();
 
+    private ModelProcessingPhase finishedPhase = ModelProcessingPhase.INIT;
+    private ModelProcessingPhase inProgressPhase;
+    private RootStatementContext<?, ?, ?> root;
 
     SourceSpecificContext(final BuildGlobalContext currentContext, final StatementStreamSource source) {
-        this.source = source;
         this.currentContext = currentContext;
+        this.source = source;
     }
 
     public boolean isEnabledSemanticVersioning(){
@@ -107,27 +113,22 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         if (def == null) {
             // unknown-stmts (from import, include or local-scope)
             if (qNameToStmtDefMap.get(name) != null) {
-                final StatementContextBase<?, ?, ?> extension =
-                        (StatementContextBase<?, ?, ?>) currentContext.getAllFromNamespace(ExtensionNamespace.class).get(name);
-
+                final StatementDefinition extension = currentContext.getFromNamespace(
+                    StatementDefinitionNamespace.class, name);
                 SourceException.throwIfNull(extension, current.getStatementSourceReference(), "Extension %s not found",
                     name);
 
-                final QName arg = (QName) extension.getStatementArgument();
-                final QName qName = current.getFromNamespace(QNameCacheNamespace.class,
-                    QName.create(arg, extension.getIdentifier().getArgument()));
-
-                def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(
-                    getNewStatementDefinition(qName)));
+                def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(extension));
             } else {
                 // type-body-stmts
                 def = resolveTypeBodyStmts(name.getLocalName());
             }
         } else if (current != null && current.definition().getRepresentingClass().equals(UnknownStatementImpl.class)) {
+            // FIXME: What's going on here?
             final QName qName = Utils.qNameFromArgument(current, name.getLocalName());
 
             def = new StatementDefinitionContext<>(new UnknownStatementImpl.Definition(
-                getNewStatementDefinition(qName)));
+                new ModelDefinedStatementDefinition(qName)));
         }
 
         Preconditions.checkArgument(def != null, "Statement %s does not have type mapping defined.", name);
@@ -137,11 +138,6 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         return current.substatementBuilder(def, ref);
     }
 
-    // FIXME: This should be populated differently
-    StatementDefinition getNewStatementDefinition(final QName qName) {
-        return new ModelDefinedStatementDefinition(qName);
-    }
-
     @SuppressWarnings({"rawtypes", "unchecked"})
     private ContextBuilder<?, ?, ?> createDeclaredRoot(final StatementDefinitionContext<?, ?, ?> def,
                                                        final StatementSourceReference ref) {
@@ -175,11 +171,11 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
     }
 
     void startPhase(final ModelProcessingPhase phase) {
-        @Nullable
-        final ModelProcessingPhase previousPhase = phase.getPreviousPhase();
+        @Nullable final ModelProcessingPhase previousPhase = phase.getPreviousPhase();
         Preconditions.checkState(Objects.equals(previousPhase, finishedPhase));
         Preconditions.checkState(modifiers.get(previousPhase).isEmpty());
         inProgressPhase = phase;
+        LOG.debug("Source {} started phase {}", source, phase);
     }
 
     @Override
@@ -249,17 +245,16 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
         Preconditions.checkNotNull(this.root, "Malformed source. Valid root element is missing.");
         final boolean phaseCompleted = root.tryToCompletePhase(phase);
 
-        hasProgressed = (tryToProgress(currentPhaseModifiers) | hasProgressed);
+        hasProgressed |= tryToProgress(currentPhaseModifiers);
 
-        if (phaseCompleted && (currentPhaseModifiers.isEmpty())) {
+        if (phaseCompleted && currentPhaseModifiers.isEmpty()) {
             finishedPhase = phase;
+            LOG.debug("Source {} finished phase {}", source, phase);
             return PhaseCompletionProgress.FINISHED;
 
         }
-        if (hasProgressed) {
-            return PhaseCompletionProgress.PROGRESS;
-        }
-        return PhaseCompletionProgress.NO_PROGRESS;
+
+        return hasProgressed ? PhaseCompletionProgress.PROGRESS : PhaseCompletionProgress.NO_PROGRESS;
     }
 
 
@@ -290,7 +285,7 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
                 + finishedPhase + "]";
     }
 
-    SourceException failModifiers(final ModelProcessingPhase identifier) {
+    Optional<SourceException> failModifiers(final ModelProcessingPhase identifier) {
         final List<SourceException> exceptions = new ArrayList<>();
         for (final ModifierImpl mod : modifiers.get(identifier)) {
             try {
@@ -300,11 +295,11 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
             }
         }
 
-        final String message = String.format("Yang model processing phase %s failed", identifier);
         if (exceptions.isEmpty()) {
-            return new InferenceException(message, root.getStatementSourceReference());
+            return Optional.empty();
         }
 
+        final String message = String.format("Yang model processing phase %s failed", identifier);
         final InferenceException e = new InferenceException(message, root.getStatementSourceReference(),
             exceptions.get(0));
         final Iterator<SourceException> it = exceptions.listIterator(1);
@@ -312,10 +307,12 @@ public class SourceSpecificContext implements NamespaceStorageNode, NamespaceBeh
             e.addSuppressed(it.next());
         }
 
-        return e;
+        return Optional.of(e);
     }
 
     void loadStatements() throws SourceException {
+        LOG.trace("Source {} loading statements for phase {}", source, inProgressPhase);
+
         switch (inProgressPhase) {
             case SOURCE_PRE_LINKAGE:
                 source.writePreLinkage(new StatementContextWriter(this, inProgressPhase), stmtDef());