Add StatementSourceException
[yangtools.git] / parser / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / DependencyResolver.java
index c7df113fa9c0188f3c5b736e7d5abd73f36602aa..ba06ab4d64646bbeea3950b99c54dbac98185114 100644 (file)
@@ -11,15 +11,11 @@ import com.google.common.base.MoreObjects;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
-import com.google.common.collect.Multimap;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
-import java.util.Map.Entry;
 import java.util.Optional;
-import java.util.Set;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.common.UnresolvedQName.Unqualified;
 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
@@ -27,6 +23,7 @@ import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.api.stmt.ImportEffectiveStatement;
 import org.opendaylight.yangtools.yang.parser.api.YangParserConfiguration;
 import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo;
+import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangModelDependencyInfo.SubmoduleDependencyInfo;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -41,45 +38,43 @@ import org.slf4j.LoggerFactory;
 //        as new models are added to a schema context.
 abstract class DependencyResolver {
     private static final Logger LOG = LoggerFactory.getLogger(DependencyResolver.class);
+
     private final ImmutableList<SourceIdentifier> resolvedSources;
     private final ImmutableList<SourceIdentifier> unresolvedSources;
     private final ImmutableMultimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
 
     protected DependencyResolver(final Map<SourceIdentifier, YangModelDependencyInfo> depInfo) {
-        final Collection<SourceIdentifier> resolved = new ArrayList<>(depInfo.size());
-        final Collection<SourceIdentifier> pending = new ArrayList<>(depInfo.keySet());
-        final Map<SourceIdentifier, BelongsToDependency> submodules = new HashMap<>();
+        final var resolved = new ArrayList<SourceIdentifier>(depInfo.size());
+        final var pending = new ArrayList<>(depInfo.keySet());
+        final var submodules = new HashMap<SourceIdentifier, BelongsToDependency>();
 
         boolean progress;
         do {
             progress = false;
 
-            final Iterator<SourceIdentifier> it = pending.iterator();
+            final var it = pending.iterator();
             while (it.hasNext()) {
-                final SourceIdentifier id = it.next();
-                final YangModelDependencyInfo dep = depInfo.get(id);
-
-                boolean okay = true;
-
-                final Set<ModuleImport> dependencies = dep.getDependencies();
+                final var sourceId = it.next();
+                final var dep = depInfo.get(sourceId);
 
                 // in case of submodule, remember belongs to
-                if (dep instanceof YangModelDependencyInfo.SubmoduleDependencyInfo) {
-                    final var parent = ((YangModelDependencyInfo.SubmoduleDependencyInfo) dep).getParentModule();
-                    submodules.put(id, new BelongsToDependency(parent));
+                if (dep instanceof SubmoduleDependencyInfo submodule) {
+                    final var parent = submodule.getParentModule();
+                    submodules.put(sourceId, new BelongsToDependency(parent));
                 }
 
-                for (final ModuleImport mi : dependencies) {
-                    if (!isKnown(resolved, mi)) {
-                        LOG.debug("Source {} is missing import {}", id, mi);
+                boolean okay = true;
+                for (var dependency : dep.getDependencies()) {
+                    if (!isKnown(resolved, dependency)) {
+                        LOG.debug("Source {} is missing import {}", sourceId, dependency);
                         okay = false;
                         break;
                     }
                 }
 
                 if (okay) {
-                    LOG.debug("Resolved source {}", id);
-                    resolved.add(id);
+                    LOG.debug("Resolved source {}", sourceId);
+                    resolved.add(sourceId);
                     it.remove();
                     progress = true;
                 }
@@ -87,22 +82,21 @@ abstract class DependencyResolver {
         } while (progress);
 
         /// Additional check only for belongs-to statement
-        for (final Entry<SourceIdentifier, BelongsToDependency> submodule : submodules.entrySet()) {
-            final BelongsToDependency belongs = submodule.getValue();
-            final SourceIdentifier sourceIdentifier = submodule.getKey();
+        for (var submodule : submodules.entrySet()) {
+            final var sourceId = submodule.getKey();
+            final var belongs = submodule.getValue();
             if (!isKnown(resolved, belongs)) {
-                LOG.debug("Source {} is missing parent {}", sourceIdentifier, belongs);
-                pending.add(sourceIdentifier);
-                resolved.remove(sourceIdentifier);
+                LOG.debug("Source {} is missing parent {}", sourceId, belongs);
+                pending.add(sourceId);
+                resolved.remove(sourceId);
             }
         }
 
-        final Multimap<SourceIdentifier, ModuleImport> imports = ArrayListMultimap.create();
-        for (final SourceIdentifier id : pending) {
-            final YangModelDependencyInfo dep = depInfo.get(id);
-            for (final ModuleImport mi : dep.getDependencies()) {
-                if (!isKnown(pending, mi) && !isKnown(resolved, mi)) {
-                    imports.put(id, mi);
+        final var imports = ArrayListMultimap.<SourceIdentifier, ModuleImport>create();
+        for (var sourceId : pending) {
+            for (var dependency : depInfo.get(sourceId).getDependencies()) {
+                if (!isKnown(pending, dependency) && !isKnown(resolved, dependency)) {
+                    imports.put(sourceId, dependency);
                 }
             }
         }
@@ -119,14 +113,14 @@ abstract class DependencyResolver {
     /**
      * Collection of sources which have been resolved.
      */
-    Collection<SourceIdentifier> getResolvedSources() {
+    ImmutableList<SourceIdentifier> resolvedSources() {
         return resolvedSources;
     }
 
     /**
      * Collection of sources which have not been resolved due to missing dependencies.
      */
-    Collection<SourceIdentifier> getUnresolvedSources() {
+    ImmutableList<SourceIdentifier> unresolvedSources() {
         return unresolvedSources;
     }
 
@@ -149,7 +143,7 @@ abstract class DependencyResolver {
      * A->C and B->C will be reported.
      * </li></ul>
      */
-    Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
+    ImmutableMultimap<SourceIdentifier, ModuleImport> unsatisfiedImports() {
         return unsatisfiedImports;
     }