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 java.text.SimpleDateFormat;
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.Collections;
14 import java.util.Date;
15 import java.util.List;
19 import org.opendaylight.yangtools.yang.model.api.Module;
20 import org.opendaylight.yangtools.yang.model.api.ModuleImport;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.parser.builder.impl.ModuleBuilder;
23 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.Node;
24 import org.opendaylight.yangtools.yang.parser.util.TopologicalSort.NodeImpl;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.annotations.VisibleForTesting;
29 import com.google.common.base.Function;
30 import com.google.common.collect.Lists;
31 import com.google.common.collect.Maps;
32 import com.google.common.collect.Sets;
35 * Creates a module dependency graph from provided {@link ModuleBuilder}s and
36 * provides a {@link #sort(ModuleBuilder...)} method. It is topological sort and
37 * returns modules in order in which they should be processed (e.g. if A imports
38 * B, sort returns {B, A}).
40 public final class ModuleDependencySort {
42 private static final Date DEFAULT_REVISION = new Date(0);
43 private static final Logger LOGGER = LoggerFactory.getLogger(ModuleDependencySort.class);
46 * It is not desirable to instance this class
48 private ModuleDependencySort() {
52 * Topological sort of module builder dependency graph.
54 * @return Sorted list of Module builders. Modules can be further processed
57 public static List<ModuleBuilder> sort(ModuleBuilder... builders) {
58 List<Node> sorted = sortInternal(Arrays.asList(builders));
59 // Cast to ModuleBuilder from Node and return
60 return Lists.transform(sorted, new Function<Node, ModuleBuilder>() {
63 public ModuleBuilder apply(Node input) {
64 return (ModuleBuilder) ((ModuleNodeImpl) input).getReference();
69 public static List<ModuleBuilder> sortWithContext(SchemaContext context, ModuleBuilder... builders) {
70 List<Object> modules = new ArrayList<Object>();
71 Collections.addAll(modules, builders);
72 modules.addAll(context.getModules());
74 List<Node> sorted = sortInternal(modules);
75 // Cast to ModuleBuilder from Node if possible and return
76 return Lists.transform(sorted, new Function<Node, ModuleBuilder>() {
79 public ModuleBuilder apply(Node input) {
80 if (((ModuleNodeImpl) input).getReference() instanceof ModuleBuilder) {
81 return (ModuleBuilder) ((ModuleNodeImpl) input).getReference();
90 * Topological sort of module dependency graph.
92 * @return Sorted list of Modules. Modules can be further processed in
95 public static List<Module> sort(Module... modules) {
96 List<Node> sorted = sortInternal(Arrays.asList(modules));
97 // Cast to Module from Node and return
98 return Lists.transform(sorted, new Function<Node, Module>() {
101 public Module apply(Node input) {
102 return (Module) ((ModuleNodeImpl) input).getReference();
107 private static List<Node> sortInternal(List<?> modules) {
108 Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = createModuleGraph(modules);
110 Set<Node> nodes = Sets.newHashSet();
111 for (Map<Date, ModuleNodeImpl> map : moduleGraph.values()) {
112 for (ModuleNodeImpl node : map.values()) {
117 return TopologicalSort.sort(nodes);
121 static Map<String, Map<Date, ModuleNodeImpl>> createModuleGraph(List<?> builders) {
122 Map<String, Map<Date, ModuleNodeImpl>> moduleGraph = Maps.newHashMap();
124 processModules(moduleGraph, builders);
125 processDependencies(moduleGraph, builders);
131 * Extract module:revision from module builders
133 private static void processDependencies(Map<String, Map<Date, ModuleNodeImpl>> moduleGraph, List<?> builders) {
136 // Create edges in graph
137 for (Object mb : builders) {
138 Map<String, Date> imported = Maps.newHashMap();
140 String fromName = null;
141 Date fromRevision = null;
142 Set<ModuleImport> imports = null;
144 if (mb instanceof Module) {
145 fromName = ((Module) mb).getName();
146 fromRevision = ((Module) mb).getRevision();
147 imports = ((Module) mb).getImports();
148 } else if (mb instanceof ModuleBuilder) {
149 fromName = ((ModuleBuilder) mb).getName();
150 fromRevision = ((ModuleBuilder) mb).getRevision();
151 imports = ((ModuleBuilder) mb).getModuleImports();
153 // no need to check if other Type of object, check is performed in
156 if (fromRevision == null) {
157 fromRevision = DEFAULT_REVISION;
160 for (ModuleImport imprt : imports) {
161 String toName = imprt.getModuleName();
162 Date toRevision = imprt.getRevision() == null ? DEFAULT_REVISION : imprt.getRevision();
164 ModuleNodeImpl from = moduleGraph.get(fromName).get(fromRevision);
166 ModuleNodeImpl to = getModuleByNameAndRevision(moduleGraph, fromName, fromRevision, toName, toRevision);
169 * Check imports: If module is imported twice with different
170 * revisions then throw exception
172 if (imported.get(toName) != null && !imported.get(toName).equals(toRevision)
173 && !imported.get(toName).equals(DEFAULT_REVISION) && !toRevision.equals(DEFAULT_REVISION)) {
174 ex(String.format("Module:%s imported twice with different revisions:%s, %s", toName,
175 formatRevDate(imported.get(toName)), formatRevDate(toRevision)));
178 imported.put(toName, toRevision);
186 * Get imported module by its name and revision from moduleGraph
188 private static ModuleNodeImpl getModuleByNameAndRevision(Map<String, Map<Date, ModuleNodeImpl>> moduleGraph,
189 String fromName, Date fromRevision, String toName, Date toRevision) {
190 ModuleNodeImpl to = null;
192 if (moduleGraph.get(toName) == null || !moduleGraph.get(toName).containsKey(toRevision)) {
193 // If revision is not specified in import, but module exists
194 // with different revisions, take first
195 if (moduleGraph.get(toName) != null && !moduleGraph.get(toName).isEmpty()
196 && toRevision.equals(DEFAULT_REVISION)) {
197 to = moduleGraph.get(toName).values().iterator().next();
199 .format("Import:%s:%s by module:%s:%s does not specify revision, using:%s:%s for module dependency sort",
200 toName, formatRevDate(toRevision), fromName, formatRevDate(fromRevision), to.getName(),
201 formatRevDate(to.getRevision())));
203 LOGGER.warn(String.format("Not existing module imported:%s:%s by:%s:%s", toName,
204 formatRevDate(toRevision), fromName, formatRevDate(fromRevision)));
205 LOGGER.warn("Available models: {}", moduleGraph);
206 ex(String.format("Not existing module imported:%s:%s by:%s:%s", toName, formatRevDate(toRevision),
207 fromName, formatRevDate(fromRevision)));
210 to = moduleGraph.get(toName).get(toRevision);
215 private static void ex(String message) {
216 throw new YangValidationException(message);
220 * Extract dependencies from module builders or modules to fill dependency
223 private static void processModules(Map<String, Map<Date, ModuleNodeImpl>> moduleGraph, List<?> builders) {
226 for (Object mb : builders) {
231 if (mb instanceof Module) {
232 name = ((Module) mb).getName();
233 rev = ((Module) mb).getRevision();
234 } else if (mb instanceof ModuleBuilder) {
235 name = ((ModuleBuilder) mb).getName();
236 rev = ((ModuleBuilder) mb).getRevision();
238 throw new IllegalStateException(String.format(
239 "Unexpected type of node for sort, expected only:%s, %s, got:%s", Module.class,
240 ModuleBuilder.class, mb.getClass()));
244 rev = DEFAULT_REVISION;
247 if (moduleGraph.get(name) == null) {
248 moduleGraph.put(name, Maps.<Date, ModuleNodeImpl> newHashMap());
251 if (moduleGraph.get(name).get(rev) != null) {
252 ex(String.format("Module:%s with revision:%s declared twice", name, formatRevDate(rev)));
255 moduleGraph.get(name).put(rev, new ModuleNodeImpl(name, rev, mb));
259 private static String formatRevDate(Date rev) {
260 return rev.equals(DEFAULT_REVISION) ? "default" : new SimpleDateFormat("yyyy-MM-dd").format(rev);
264 static class ModuleNodeImpl extends NodeImpl {
265 private final String name;
266 private final Date revision;
267 private final Object originalObject;
269 public ModuleNodeImpl(String name, Date revision, Object builder) {
271 this.revision = revision;
272 this.originalObject = builder;
275 public String getName() {
279 public Date getRevision() {
284 public int hashCode() {
285 final int prime = 31;
287 result = prime * result + ((name == null) ? 0 : name.hashCode());
288 result = prime * result + ((revision == null) ? 0 : revision.hashCode());
293 public boolean equals(Object obj) {
300 if (getClass() != obj.getClass()) {
303 ModuleNodeImpl other = (ModuleNodeImpl) obj;
305 if (other.name != null) {
308 } else if (!name.equals(other.name)) {
311 if (revision == null) {
312 if (other.revision != null) {
315 } else if (!revision.equals(other.revision)) {
322 public String toString() {
323 return "Module [name=" + name + ", revision=" + formatRevDate(revision) + "]";
326 public Object getReference() {
327 return originalObject;