2 * Copyright (c) 2013 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.util;
10 import static java.util.Arrays.asList;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Function;
13 import com.google.common.collect.Lists;
14 import com.google.common.collect.Maps;
15 import com.google.common.collect.Sets;
17 import java.util.Collection;
18 import java.util.Collections;
19 import java.util.Date;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.Objects;
25 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
29 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
30 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
31 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.NodeImpl;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Creates a module dependency graph from provided {@link ModuleBuilder}s and
37 * provides a {@link #sort(ModuleBuilder...)} method. It is topological sort and
38 * returns modules in order in which they should be processed (e.g. if A imports
39 * B, sort returns {B, A}).
41 public final class ModuleDependencySort {
43 private static final Date DEFAULT_REVISION = SimpleDateFormatUtil.DEFAULT_DATE_REV;
44 private static final Logger LOGGER = LoggerFactory.getLogger(ModuleDependencySort.class);
45 private static final Function<Node, Module> TOPOLOGY_FUNCTION = new Function<TopologicalSort.Node, Module>() {
47 public Module apply(final TopologicalSort.Node input) {
51 ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
52 return moduleOrModuleBuilder.getModule();
57 * It is not desirable to instance this class
59 private ModuleDependencySort() {
64 * Extracts {@link ModuleBuilder} from a {@link ModuleNodeImpl}.
66 private static final Function<TopologicalSort.Node, ModuleBuilder> NODE_TO_MODULEBUILDER = new Function<TopologicalSort.Node, ModuleBuilder>() {
68 public ModuleBuilder apply(final TopologicalSort.Node input) {
69 // Cast to ModuleBuilder from Node and return
73 ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
74 return moduleOrModuleBuilder.getModuleBuilder();
79 * Topological sort of module builder dependency graph.
81 * @param builders builders of Module object
82 * @return Sorted list of Module builders. Modules can be further processed
84 * @deprecated Pre-Beryllium implementation, scheduled for removal.
87 public static List<ModuleBuilder> sort(final ModuleBuilder... builders) {
88 return sort(asList(builders));
92 * @deprecated Pre-Beryllium implementation, scheduled for removal.
95 public static List<ModuleBuilder> sort(final Collection<ModuleBuilder> builders) {
96 List<TopologicalSort.Node> sorted = sortInternal(ModuleOrModuleBuilder.fromAll(
97 Collections.<Module>emptySet(),builders));
98 return Lists.transform(sorted, NODE_TO_MODULEBUILDER);
101 public static List<ModuleBuilder> sortWithContext(final SchemaContext context, final ModuleBuilder... builders) {
102 List<ModuleOrModuleBuilder> all = ModuleOrModuleBuilder.fromAll(context.getModules(), asList(builders));
104 List<TopologicalSort.Node> sorted = sortInternal(all);
105 // Cast to ModuleBuilder from Node if possible and return
106 return Lists.transform(sorted, new Function<TopologicalSort.Node, ModuleBuilder>() {
109 public ModuleBuilder apply(final TopologicalSort.Node input) {
113 ModuleOrModuleBuilder moduleOrModuleBuilder = ((ModuleNodeImpl) input).getReference();
114 if (moduleOrModuleBuilder.isModuleBuilder()) {
115 return moduleOrModuleBuilder.getModuleBuilder();
124 * Topological sort of module dependency graph.
126 * @param modules YANG modules
127 * @return Sorted list of Modules. Modules can be further processed in
130 public static List<Module> sort(final Module... modules) {
131 List<TopologicalSort.Node> sorted = sortInternal(ModuleOrModuleBuilder.fromAll(asList(modules),
132 Collections.<ModuleBuilder>emptyList()));
133 // Cast to Module from Node and return
134 return Lists.transform(sorted, TOPOLOGY_FUNCTION);
137 private static List<TopologicalSort.Node> sortInternal(final Iterable<ModuleOrModuleBuilder> modules) {
138 Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = createModuleGraph(modules);
140 Set<TopologicalSort.Node> nodes = Sets.newHashSet();
141 for (Map<Date, ModuleNodeImpl> map : moduleGraph.values()) {
142 for (ModuleNodeImpl node : map.values()) {
147 return TopologicalSort.sort(nodes);
151 static Map<String, Map<Date, ModuleNodeImpl>> createModuleGraph(final Iterable<ModuleOrModuleBuilder> builders) {
152 Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = Maps.newHashMap();
154 processModules(moduleGraph, builders);
155 processDependencies(moduleGraph, builders);
161 * Extract module:revision from module builders
163 private static void processDependencies(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
164 final Iterable<ModuleOrModuleBuilder> mmbs) {
165 Map<URI, ModuleOrModuleBuilder> allNS = new HashMap<>();
167 // Create edges in graph
168 for (ModuleOrModuleBuilder mmb : mmbs) {
169 Map<String, Date> imported = Maps.newHashMap();
173 Collection<ModuleImport> imports;
176 if (mmb.isModule()) {
177 Module module = mmb.getModule();
178 fromName = module.getName();
179 fromRevision = module.getRevision();
180 imports = module.getImports();
181 ns = module.getNamespace();
183 ModuleBuilder moduleBuilder = mmb.getModuleBuilder();
184 fromName = moduleBuilder.getName();
185 fromRevision = moduleBuilder.getRevision();
186 imports = moduleBuilder.getImports().values();
187 ns = moduleBuilder.getNamespace();
190 // check for existence of module with same namespace
191 if (allNS.containsKey(ns)) {
192 ModuleOrModuleBuilder mod = allNS.get(ns);
194 Date revision = null;
195 if (mod.isModule()) {
196 name = mod.getModule().getName();
197 revision = mod.getModule().getRevision();
198 } else if (mod.isModuleBuilder()) {
199 name = mod.getModuleBuilder().getName();
200 revision = mod.getModuleBuilder().getRevision();
202 if (!(fromName.equals(name))) {
204 "Error while sorting module [{}, {}]: module with same namespace ({}) already loaded: [{}, {}]",
205 fromName, fromRevision, ns, name, revision);
211 // no need to check if other Type of object, check is performed in
214 if (fromRevision == null) {
215 fromRevision = DEFAULT_REVISION;
218 for (ModuleImport imprt : imports) {
219 String toName = imprt.getModuleName();
220 Date toRevision = imprt.getRevision() == null ? DEFAULT_REVISION : imprt.getRevision();
222 ModuleNodeImpl from = moduleGraph.get(fromName).get(fromRevision);
224 ModuleNodeImpl to = getModuleByNameAndRevision(moduleGraph, fromName, fromRevision, toName, toRevision);
227 * Check imports: If module is imported twice with different
228 * revisions then throw exception
230 if (imported.get(toName) != null && !imported.get(toName).equals(toRevision)
231 && !imported.get(toName).equals(DEFAULT_REVISION) && !toRevision.equals(DEFAULT_REVISION)) {
232 ex(String.format("Module:%s imported twice with different revisions:%s, %s", toName,
233 formatRevDate(imported.get(toName)), formatRevDate(toRevision)));
236 imported.put(toName, toRevision);
244 * Get imported module by its name and revision from moduleGraph
246 private static ModuleNodeImpl getModuleByNameAndRevision(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
247 final String fromName, final Date fromRevision, final String toName, final Date toRevision) {
248 ModuleNodeImpl to = null;
250 if (moduleGraph.get(toName) == null || !moduleGraph.get(toName).containsKey(toRevision)) {
251 // If revision is not specified in import, but module exists
252 // with different revisions, take first
253 if (moduleGraph.get(toName) != null && !moduleGraph.get(toName).isEmpty()
254 && toRevision.equals(DEFAULT_REVISION)) {
255 to = moduleGraph.get(toName).values().iterator().next();
257 .format("Import:%s:%s by module:%s:%s does not specify revision, using:%s:%s for module dependency sort",
258 toName, formatRevDate(toRevision), fromName, formatRevDate(fromRevision), to.getName(),
259 formatRevDate(to.getRevision())));
261 LOGGER.warn(String.format("Not existing module imported:%s:%s by:%s:%s", toName,
262 formatRevDate(toRevision), fromName, formatRevDate(fromRevision)));
263 LOGGER.warn("Available models: {}", moduleGraph);
264 ex(String.format("Not existing module imported:%s:%s by:%s:%s", toName, formatRevDate(toRevision),
265 fromName, formatRevDate(fromRevision)));
268 to = moduleGraph.get(toName).get(toRevision);
273 private static void ex(final String message) {
274 throw new YangValidationException(message);
278 * Extract dependencies from module builders or modules to fill dependency
281 private static void processModules(final Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
282 final Iterable<ModuleOrModuleBuilder> builders) {
285 for (ModuleOrModuleBuilder momb : builders) {
290 if (momb.isModule()) {
291 name = momb.getModule().getName();
292 rev = momb.getModule().getRevision();
294 name = momb.getModuleBuilder().getName();
295 rev = momb.getModuleBuilder().getRevision();
299 rev = DEFAULT_REVISION;
302 if (moduleGraph.get(name) == null) {
303 moduleGraph.put(name, Maps.<Date, ModuleNodeImpl> newHashMap());
306 if (moduleGraph.get(name).get(rev) != null) {
307 ex(String.format("Module:%s with revision:%s declared twice", name, formatRevDate(rev)));
310 moduleGraph.get(name).put(rev, new ModuleNodeImpl(name, rev, momb));
314 private static String formatRevDate(final Date rev) {
315 return rev.equals(DEFAULT_REVISION) ? "default" : SimpleDateFormatUtil.getRevisionFormat().format(rev);
319 static class ModuleNodeImpl extends NodeImpl {
320 private final String name;
321 private final Date revision;
322 private final ModuleOrModuleBuilder originalObject;
324 public ModuleNodeImpl(final String name, final Date revision, final ModuleOrModuleBuilder builder) {
326 this.revision = revision;
327 this.originalObject = builder;
330 public String getName() {
334 public Date getRevision() {
339 public int hashCode() {
340 final int prime = 31;
342 result = prime * result + Objects.hashCode(name);
343 result = prime * result + Objects.hashCode(revision);
348 public boolean equals(final Object obj) {
355 if (getClass() != obj.getClass()) {
358 ModuleNodeImpl other = (ModuleNodeImpl) obj;
360 if (other.name != null) {
363 } else if (!name.equals(other.name)) {
366 if (revision == null) {
367 if (other.revision != null) {
370 } else if (!revision.equals(other.revision)) {
377 public String toString() {
378 return "Module [name=" + name + ", revision=" + formatRevDate(revision) + "]";
381 public ModuleOrModuleBuilder getReference() {
382 return originalObject;