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.Objects;
11 import com.google.common.base.Optional;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.ArrayListMultimap;
14 import com.google.common.collect.ImmutableMultimap;
15 import com.google.common.collect.Maps;
16 import com.google.common.collect.Multimap;
17 import java.util.ArrayList;
18 import java.util.Collection;
19 import java.util.Collections;
20 import java.util.Date;
21 import java.util.Iterator;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
26 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
27 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Inter-module dependency resolved. Given a set of schema source identifiers and their
33 * corresponding dependency information, the {@link #create(Map)} method creates a
34 * a view of how consistent the dependencies are. In particular, this detects whether
35 * any imports are unsatisfied.
37 * FIXME: improve this class to track and expose how wildcard imports were resolved.
38 * That information will allow us to track "damage" to dependency resolution
39 * as new models are added to a schema context.
41 final class DependencyResolver {
42 private static final Logger LOG = LoggerFactory.getLogger(DependencyResolver.class);
43 private final Collection<SourceIdentifier> resolvedSources;
44 private final Collection<SourceIdentifier> unresolvedSources;
45 private final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
47 public DependencyResolver(final Collection<SourceIdentifier> resolvedSources,
48 final Collection<SourceIdentifier> unresolvedSources, final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
49 this.resolvedSources = Preconditions.checkNotNull(resolvedSources);
50 this.unresolvedSources = Preconditions.checkNotNull(unresolvedSources);
51 this.unsatisfiedImports = Preconditions.checkNotNull(unsatisfiedImports);
54 private static SourceIdentifier findWildcard(final Iterable<SourceIdentifier> haystack, final String needle) {
55 for (final SourceIdentifier r : haystack) {
56 if (r.getName().equals(needle)) {
64 private static boolean isKnown(final Collection<SourceIdentifier> haystack, final ModuleImport mi) {
65 final String rev = mi.getRevision() != null ? QName.formattedRevision(mi.getRevision()) : null;
66 final SourceIdentifier msi = SourceIdentifier.create(mi.getModuleName(), Optional.fromNullable(rev));
69 if (haystack.contains(msi)) {
73 // Slow revision-less walk
74 return rev == null && findWildcard(haystack, mi.getModuleName()) != null;
79 public static DependencyResolver create(final Map<SourceIdentifier, YangModelDependencyInfo> depInfo) {
80 final Collection<SourceIdentifier> resolved = new ArrayList<>(depInfo.size());
81 final Collection<SourceIdentifier> pending = new ArrayList<>(depInfo.keySet());
82 final Map<SourceIdentifier, BelongsToDependency> submodules = Maps.newHashMap();
88 final Iterator<SourceIdentifier> it = pending.iterator();
89 while (it.hasNext()) {
90 final SourceIdentifier id = it.next();
91 final YangModelDependencyInfo dep = depInfo.get(id);
95 final Set<ModuleImport> dependencies = dep.getDependencies();
97 // in case of submodule, remember belongs to
98 if(dep instanceof YangModelDependencyInfo.SubmoduleDependencyInfo) {
99 final String parent = ((YangModelDependencyInfo.SubmoduleDependencyInfo) dep).getParentModule();
100 submodules.put(id, new BelongsToDependency(parent));
103 for (final ModuleImport mi : dependencies) {
104 if (!isKnown(resolved, mi)) {
105 LOG.debug("Source {} is missing import {}", id, mi);
112 LOG.debug("Resolved source {}", id);
120 /// Additional check only for belongs-to statement
121 for (final SourceIdentifier sourceIdentifier : submodules.keySet()) {
122 final BelongsToDependency belongs = submodules.get(sourceIdentifier);
123 if (!isKnown(resolved, belongs)) {
124 LOG.debug("Source {} is missing parent {}", sourceIdentifier, belongs);
125 pending.add(sourceIdentifier);
126 resolved.remove(sourceIdentifier);
130 if (!pending.isEmpty()) {
131 final Multimap<SourceIdentifier, ModuleImport> imports = ArrayListMultimap.create();
132 for (final SourceIdentifier id : pending) {
133 final YangModelDependencyInfo dep = depInfo.get(id);
134 for (final ModuleImport mi : dep.getDependencies()) {
135 if (!isKnown(pending, mi) && !isKnown(resolved, mi)) {
141 return new DependencyResolver(resolved, pending, imports);
143 return new DependencyResolver(resolved, Collections.<SourceIdentifier>emptyList(), ImmutableMultimap.<SourceIdentifier, ModuleImport>of());
148 * Collection of sources which have been resolved.
152 Collection<SourceIdentifier> getResolvedSources() {
153 return resolvedSources;
157 * Collection of sources which have not been resolved due to missing dependencies.
161 Collection<SourceIdentifier> getUnresolvedSources() {
162 return unresolvedSources;
166 * Detailed information about which imports were missing. The key in the map
167 * is the source identifier of module which was issuing an import, the values
168 * are imports which were unsatisfied.
170 * Note that this map contains only imports which are missing from the reactor,
171 * not transitive failures.
175 * If A imports B, B imports C, and both A and B are in the reactor, only B->C
178 * If A imports B and C, B imports C, and both A and B are in the reactor,
179 * A->C and B->C will be reported.
183 Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
184 return unsatisfiedImports;
187 private static class BelongsToDependency implements ModuleImport {
188 private final String parent;
190 public BelongsToDependency(final String parent) {
191 this.parent = parent;
195 public String getModuleName() {
200 public Date getRevision() {
205 public String getPrefix() {
210 public String toString() {
211 return Objects.toStringHelper(this)
212 .add("parent", parent)