Merge "Bug 1442: Fixed SchemaTracker resolving case children."
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / repo / DependencyResolver.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.yang.parser.repo;
9
10 import com.google.common.collect.Sets;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Collections;
14 import java.util.Date;
15 import java.util.Iterator;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.opendaylight.yangtools.yang.common.QName;
20 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
21 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
22 import org.opendaylight.yangtools.yang.parser.impl.util.YangModelDependencyInfo;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import com.google.common.base.Optional;
27 import com.google.common.base.Preconditions;
28 import com.google.common.collect.ArrayListMultimap;
29 import com.google.common.collect.ImmutableMultimap;
30 import com.google.common.collect.Multimap;
31
32 /**
33  * Inter-module dependency resolved. Given a set of schema source identifiers and their
34  * corresponding dependency information, the {@link #create(Map)} method creates a
35  * a view of how consistent the dependencies are. In particular, this detects whether
36  * any imports are unsatisfied.
37  *
38  * FIXME: improve this class to track and expose how wildcard imports were resolved.
39  *        That information will allow us to track "damage" to dependency resolution
40  *        as new models are added to a schema context.
41  */
42 final class DependencyResolver {
43     private static final Logger LOG = LoggerFactory.getLogger(DependencyResolver.class);
44     private final Collection<SourceIdentifier> resolvedSources;
45     private final Collection<SourceIdentifier> unresolvedSources;
46     private final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports;
47
48     public DependencyResolver(final Collection<SourceIdentifier> resolvedSources,
49             final Collection<SourceIdentifier> unresolvedSources, final Multimap<SourceIdentifier, ModuleImport> unsatisfiedImports) {
50         this.resolvedSources = Preconditions.checkNotNull(resolvedSources);
51         this.unresolvedSources = Preconditions.checkNotNull(unresolvedSources);
52         this.unsatisfiedImports = Preconditions.checkNotNull(unsatisfiedImports);
53     }
54
55     private static SourceIdentifier findWildcard(final Iterable<SourceIdentifier> haystack, final String needle) {
56         for (SourceIdentifier r : haystack) {
57             if (r.getName().equals(needle)) {
58                 return r;
59             }
60         }
61
62         return null;
63     }
64
65     private static boolean isKnown(final Collection<SourceIdentifier> haystack, final ModuleImport mi) {
66         final String rev = mi.getRevision() != null ? QName.formattedRevision(mi.getRevision()) : null;
67         final SourceIdentifier msi = SourceIdentifier.create(mi.getModuleName(), Optional.fromNullable(rev));
68
69         // Quick lookup
70         if (haystack.contains(msi)) {
71             return true;
72         }
73
74         // Slow revision-less walk
75         return rev == null && findWildcard(haystack, mi.getModuleName()) != null;
76     }
77
78
79
80     public static final DependencyResolver create(final Map<SourceIdentifier, YangModelDependencyInfo> depInfo) {
81         final Collection<SourceIdentifier> resolved = new ArrayList<>(depInfo.size());
82         final Collection<SourceIdentifier> pending = new ArrayList<>(depInfo.keySet());
83
84         boolean progress;
85         do {
86             progress = false;
87
88             final Iterator<SourceIdentifier> it = pending.iterator();
89             while (it.hasNext()) {
90                 final SourceIdentifier id = it.next();
91                 final YangModelDependencyInfo dep = depInfo.get(id);
92
93                 boolean okay = true;
94
95                 Set<ModuleImport> dependencies = dep.getDependencies();
96
97                 // in case of submodule, make its parent also a dependency
98                 if(dep instanceof YangModelDependencyInfo.SubmoduleDependencyInfo) {
99                     final String parent = ((YangModelDependencyInfo.SubmoduleDependencyInfo) dep).getParentModule();
100                     dependencies = Sets.newHashSet(dependencies);
101                     dependencies.add(new BelongsToDependency(parent));
102                 }
103
104                 for (ModuleImport mi : dependencies) {
105                     if (!isKnown(resolved, mi)) {
106                         LOG.debug("Source {} is missing import {}", id, mi);
107                         okay = false;
108                         break;
109                     }
110                 }
111
112                 if (okay) {
113                     LOG.debug("Resolved source {}", id);
114                     resolved.add(id);
115                     it.remove();
116                     progress = true;
117                 }
118             }
119         } while (progress);
120
121         if (!pending.isEmpty()) {
122             final Multimap<SourceIdentifier, ModuleImport> imports = ArrayListMultimap.create();
123             for (SourceIdentifier id : pending) {
124                 final YangModelDependencyInfo dep = depInfo.get(id);
125                 for (ModuleImport mi : dep.getDependencies()) {
126                     if (!isKnown(pending, mi) && !isKnown(resolved, mi)) {
127                         imports.put(id, mi);
128                     }
129                 }
130             }
131
132             return new DependencyResolver(resolved, pending, imports);
133         } else {
134             return new DependencyResolver(resolved, Collections.<SourceIdentifier>emptyList(), ImmutableMultimap.<SourceIdentifier, ModuleImport>of());
135         }
136     }
137
138     /**
139      * Collection of sources which have been resolved.
140      *
141      * @return
142      */
143     Collection<SourceIdentifier> getResolvedSources() {
144         return resolvedSources;
145     }
146
147     /**
148      * Collection of sources which have not been resolved due to missing dependencies.
149      *
150      * @return
151      */
152     Collection<SourceIdentifier> getUnresolvedSources() {
153         return unresolvedSources;
154     }
155
156     /**
157      * Detailed information about which imports were missing. The key in the map
158      * is the source identifier of module which was issuing an import, the values
159      * are imports which were unsatisfied.
160      *
161      * Note that this map contains only imports which are missing from the reactor,
162      * not transitive failures.
163      *
164      * Examples:
165      *
166      * If A imports B, B imports C, and both A and B are in the reactor, only B->C
167      * will be reported.
168      *
169      * If A imports B and C, B imports C, and both A and B are in the reactor,
170      * A->C and B->C will be reported.
171      *
172      * @return
173      */
174     Multimap<SourceIdentifier, ModuleImport> getUnsatisfiedImports() {
175         return unsatisfiedImports;
176     }
177
178     private static class BelongsToDependency implements ModuleImport {
179         private final String parent;
180
181         public BelongsToDependency(final String parent) {
182             this.parent = parent;
183         }
184
185         @Override
186         public String getModuleName() {
187             return parent;
188         }
189
190         @Override
191         public Date getRevision() {
192             return null;
193         }
194
195         @Override
196         public String getPrefix() {
197             return null;
198         }
199     }
200 }