2 * Copyright (c) 2014 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.repo;
10 import com.google.common.base.MoreObjects;
11 import com.google.common.collect.ArrayListMultimap;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMultimap;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Multimap;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.Date;
19 import java.util.Iterator;
21 import java.util.Map.Entry;
23 import org.opendaylight.yangtools.concepts.SemVer;
24 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Inter-module dependency resolved. Given a set of schema source identifiers and their
32 * corresponding dependency information, the {@link #create(Map)} method creates a
33 * a view of how consistent the dependencies are. In particular, this detects whether
34 * any imports are unsatisfied.
36 * FIXME: improve this class to track and expose how wildcard imports were resolved.
37 * That information will allow us to track "damage" to dependency resolution
38 * as new models are added to a schema context.
40 abstract class DependencyResolver {
41 private static final Logger LOG = LoggerFactory.getLogger(DependencyResolver.class);
42 private final Collection<SourceIdentifier> resolvedSources;
43 private final Collection<SourceIdentifier> unresolvedSources;
44 private final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
46 protected DependencyResolver(final Map<SourceIdentifier, YangModelDependencyInfo> depInfo) {
47 final Collection<SourceIdentifier> resolved = new ArrayList<>(depInfo.size());
48 final Collection<SourceIdentifier> pending = new ArrayList<>(depInfo.keySet());
49 final Map<SourceIdentifier, BelongsToDependency> submodules = Maps.newHashMap();
55 final Iterator<SourceIdentifier> it = pending.iterator();
56 while (it.hasNext()) {
57 final SourceIdentifier id = it.next();
58 final YangModelDependencyInfo dep = depInfo.get(id);
62 final Set<ModuleImport> dependencies = dep.getDependencies();
64 // in case of submodule, remember belongs to
65 if (dep instanceof YangModelDependencyInfo.SubmoduleDependencyInfo) {
66 final String parent = ((YangModelDependencyInfo.SubmoduleDependencyInfo) dep).getParentModule();
67 submodules.put(id, new BelongsToDependency(parent));
70 for (final ModuleImport mi : dependencies) {
71 if (!isKnown(resolved, mi)) {
72 LOG.debug("Source {} is missing import {}", id, mi);
79 LOG.debug("Resolved source {}", id);
87 /// Additional check only for belongs-to statement
88 for (final Entry<SourceIdentifier, BelongsToDependency> submodule : submodules.entrySet()) {
89 final BelongsToDependency belongs = submodule.getValue();
90 final SourceIdentifier sourceIdentifier = submodule.getKey();
91 if (!isKnown(resolved, belongs)) {
92 LOG.debug("Source {} is missing parent {}", sourceIdentifier, belongs);
93 pending.add(sourceIdentifier);
94 resolved.remove(sourceIdentifier);
98 final Multimap<SourceIdentifier, ModuleImport> imports = ArrayListMultimap.create();
99 for (final SourceIdentifier id : pending) {
100 final YangModelDependencyInfo dep = depInfo.get(id);
101 for (final ModuleImport mi : dep.getDependencies()) {
102 if (!isKnown(pending, mi) && !isKnown(resolved, mi)) {
108 this.resolvedSources = ImmutableList.copyOf(resolved);
109 this.unresolvedSources = ImmutableList.copyOf(pending);
110 this.unsatisfiedImports = ImmutableMultimap.copyOf(imports);
113 abstract protected boolean isKnown(final Collection<SourceIdentifier> haystack, final ModuleImport mi);
116 * Collection of sources which have been resolved.
120 Collection<SourceIdentifier> getResolvedSources() {
121 return resolvedSources;
125 * Collection of sources which have not been resolved due to missing dependencies.
129 Collection<SourceIdentifier> getUnresolvedSources() {
130 return unresolvedSources;
134 * Detailed information about which imports were missing. The key in the map
135 * is the source identifier of module which was issuing an import, the values
136 * are imports which were unsatisfied.
138 * Note that this map contains only imports which are missing from the reactor,
139 * not transitive failures.
143 * If A imports B, B imports C, and both A and B are in the reactor, only B->C
146 * If A imports B and C, B imports C, and both A and B are in the reactor,
147 * A->C and B->C will be reported.
151 Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
152 return unsatisfiedImports;
155 private static class BelongsToDependency implements ModuleImport {
156 private final String parent;
158 public BelongsToDependency(final String parent) {
159 this.parent = parent;
163 public String getModuleName() {
168 public Date getRevision() {
173 public String getPrefix() {
178 public SemVer getSemanticVersion() {
183 public String toString() {
184 return MoreObjects.toStringHelper(this)
185 .add("parent", parent)