import com.google.common.collect.Lists;
import com.google.common.collect.Table;
import java.net.URI;
+import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
public final class ModuleDependencySort {
private static final Logger LOG = LoggerFactory.getLogger(ModuleDependencySort.class);
+ private ModuleDependencySort() {
+ // Hidden on purpose
+ }
+
/**
- * It is not desirable to instance this class.
+ * Topological sort of module dependency graph.
+ *
+ * @param modules YANG modules
+ * @return Sorted list of Modules. Modules can be further processed in returned order.
+ * @throws IllegalArgumentException when provided modules are not consistent.
*/
- private ModuleDependencySort() {
- throw new UnsupportedOperationException();
+ public static List<Module> sort(final Module... modules) {
+ return sort(Arrays.asList(modules));
}
/**
}
// no need to check if other Type of object, check is performed in process modules
- for (final ModuleImport imprt : module.getImports()) {
+ for (final ModuleImport imprt : allImports(module)) {
final String toName = imprt.getModuleName();
final Optional<Revision> toRevision = imprt.getRevision();
}
}
+ private static Collection<ModuleImport> allImports(final Module mod) {
+ if (mod.getSubmodules().isEmpty()) {
+ return mod.getImports();
+ }
+
+ final Collection<ModuleImport> concat = new LinkedHashSet<>();
+ concat.addAll(mod.getImports());
+ for (Module sub : mod.getSubmodules()) {
+ concat.addAll(sub.getImports());
+ }
+ return concat;
+ }
+
/**
* Get imported module by its name and revision from moduleGraph.
*/
}
// If revision is not specified in import, but module exists with different revisions, take first one
- if (toRevision == null) {
+ if (!toRevision.isPresent()) {
final Map<Optional<Revision>, ModuleNodeImpl> modulerevs = moduleGraph.row(toName);
if (!modulerevs.isEmpty()) {