2 * Copyright (c) 2021 PANTHEON.tech, s.r.o. 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.mdsal.binding.generator.impl.reactor;
10 import static com.google.common.base.Verify.verify;
11 import static com.google.common.base.Verify.verifyNotNull;
12 import static java.util.Objects.requireNonNull;
14 import com.google.common.base.Stopwatch;
15 import com.google.common.base.VerifyException;
16 import com.google.common.collect.Maps;
17 import java.util.ArrayDeque;
18 import java.util.ArrayList;
19 import java.util.Deque;
20 import java.util.HashSet;
21 import java.util.List;
23 import java.util.function.Function;
24 import java.util.stream.Collectors;
25 import org.eclipse.jdt.annotation.NonNull;
26 import org.eclipse.jdt.annotation.Nullable;
27 import org.opendaylight.yangtools.binding.ChildOf;
28 import org.opendaylight.yangtools.binding.ChoiceIn;
29 import org.opendaylight.yangtools.binding.model.api.GeneratedType;
30 import org.opendaylight.yangtools.binding.model.api.JavaTypeName;
31 import org.opendaylight.yangtools.binding.model.api.Type;
32 import org.opendaylight.yangtools.concepts.Mutable;
33 import org.opendaylight.yangtools.yang.common.QName;
34 import org.opendaylight.yangtools.yang.common.QNameModule;
35 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
36 import org.opendaylight.yangtools.yang.model.api.Module;
37 import org.opendaylight.yangtools.yang.model.api.PathExpression;
38 import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
39 import org.opendaylight.yangtools.yang.model.ri.type.TypeBuilder;
40 import org.opendaylight.yangtools.yang.model.spi.ModuleDependencySort;
41 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * A multi-stage reactor for generating {@link GeneratedType} instances from an {@link EffectiveModelContext}.
49 * The reason for multi-stage processing is that the problem ahead of us involves:
51 * <li>mapping {@code typedef} and restricted {@code type} statements onto Java classes</li>
52 * <li>mapping a number of schema tree nodes into Java interfaces with properties</li>
53 * <li>taking advantage of Java composition to provide {@code grouping} mobility</li>
56 public final class GeneratorReactor extends GeneratorContext implements Mutable {
63 private static final Logger LOG = LoggerFactory.getLogger(GeneratorReactor.class);
65 private final Deque<Iterable<? extends Generator>> stack = new ArrayDeque<>();
66 private final @NonNull Map<QNameModule, ModuleGenerator> generators;
67 private final @NonNull List<ModuleGenerator> children;
68 private final @NonNull SchemaInferenceStack inferenceStack;
70 private State state = State.INITIALIZED;
72 public GeneratorReactor(final EffectiveModelContext context) {
74 inferenceStack = SchemaInferenceStack.of(context);
76 // Construct modules and their subtrees. Dependency sort is very much needed here, as it establishes order of
77 // module evaluation, and that (along with the sort in AbstractCompositeGenerator) ensures we visit
78 // AugmentGenerators without having forward references.
79 // FIXME: migrate to new ModuleDependencySort when it is available, which streamline things here
80 children = ModuleDependencySort.sort(context.getModules()).stream()
81 .map(Module::asEffectiveStatement)
82 .map(ModuleGenerator::new)
83 .collect(Collectors.toUnmodifiableList());
84 generators = Maps.uniqueIndex(children, gen -> gen.statement().localQNameModule());
88 * Execute the reactor. Execution follows the following steps:
90 * <li>link the statement inheritance graph along {@code uses}/{@code grouping} statements</li>
91 * <li>link the {@code typedef} inheritance hierarchy by visiting all {@link TypedefGenerator}s and memoizing the
92 * {@code type} lookup</li>
93 * <li>link the {@code identity} inheritance hierarchy by visiting all {@link IdentityGenerator}s and memoizing
94 * the {@code base} lookup</li>
95 * <li>link the {@code type} statements and resolve type restriction hierarchy, determining the set of Java
96 classes required for Java equivalent of effective YANG type definitions</li>
97 * <li>bind {@code leafref} and {@code identityref} references to their Java class roots</li>
98 * <li>resolve {@link ChoiceIn}/{@link ChildOf} hierarchy</li>
99 * <li>assign Java package names and {@link JavaTypeName}s to all generated classes</li>
100 * <li>create {@link Type} instances</li>
103 * @param builderFactory factory for creating {@link TypeBuilder}s for resulting types
104 * @return Resolved generators
105 * @throws IllegalStateException if the reactor has failed execution
106 * @throws NullPointerException if {@code builderFactory} is {@code null}
108 public @NonNull Map<QNameModule, ModuleGenerator> execute(final TypeBuilderFactory builderFactory) {
111 state = State.EXECUTING;
116 throw new IllegalStateException("Cannot resume partial execution");
118 throw new IllegalStateException("Unhandled state" + state);
121 // Start measuring time...
122 final var sw = Stopwatch.createStarted();
124 // Step 1a: Walk all composite generators and resolve 'uses' statements to the corresponding grouping generator,
125 // establishing implied inheritance. During this walk we maintain 'stack' to aid this process.
126 // This indirectly triggers resolution of UsesAugmentGenerators' targets by hooking a requirement
127 // on the resolved grouping's child nodes as needed.
128 linkUsesDependencies(children);
130 // Step 1b: Walk all module generators and start ModuleAugmentGenerators' target resolution by linking the first
131 // step of each 'augment' statement to its corresponding instantiated site.
132 // Then start all UsesAugmentGenerators' target resolution.
133 final var augments = new ArrayList<AugmentRequirement>();
134 for (var module : children) {
135 for (var gen : module) {
136 if (gen instanceof ModuleAugmentGenerator moduleGen) {
137 augments.add(moduleGen.startLinkage(this));
141 for (var module : children) {
142 module.startUsesAugmentLinkage(augments);
144 LOG.trace("Processing linkage of {} augment generators", augments.size());
146 // Step 1c: Establish linkage along the reverse uses/augment axis. This is needed to route generated type
147 // manifestations (isAddedByUses/isAugmenting) to their type generation sites. Since generator tree
148 // iteration order does not match dependencies, we may need to perform multiple passes.
149 for (var module : children) {
150 verify(module.linkOriginalGenerator(), "Module %s failed to link", module);
153 final var unlinkedModules = new ArrayList<>(children);
155 final boolean progress =
156 progressAndClean(unlinkedModules, ModuleGenerator::linkOriginalGeneratorRecursive)
157 // not '||' because we need the side-effects, which would get short-circuited
158 | progressAndClean(augments, AugmentRequirement::resolve);
160 if (augments.isEmpty() && unlinkedModules.isEmpty()) {
165 final var ex = new VerifyException("Failed to make progress on linking of original generators");
166 for (var augment : augments) {
167 ex.addSuppressed(new IllegalStateException(augment + " is incomplete"));
169 for (var module : unlinkedModules) {
170 ex.addSuppressed(new IllegalStateException(module + " remains unlinked"));
177 * Step 2: link typedef statements, so that typedef's 'type' axis is fully established
178 * Step 3: link all identity statements, so that identity's 'base' axis is fully established
179 * Step 4: link all type statements, so that leafs and leaf-lists have restrictions established
181 * Since our implementation class hierarchy captures all four statements involved in a common superclass, we can
182 * perform this in a single pass.
184 linkDependencies(children);
186 // Step 5: resolve grouping usage, so that each GroupingGenerator has links to their instantiation sites and
188 resolveGroupingUsers();
189 freezeGroupingUsers(children);
191 // Step 6: resolve all 'type leafref' and 'type identityref' statements, so they point to their corresponding
192 // Java type representation.
193 bindTypeDefinition(children);
195 // Step 7: walk all composite generators and link ChildOf/ChoiceIn relationships with parents. We have taken
196 // care of this step during tree construction, hence this now a no-op.
199 * Step 8: assign java packages and JavaTypeNames
201 * This is a really tricky part, as we have large number of factors to consider:
202 * - we are mapping grouping, typedef, identity and schema tree namespaces into Fully Qualified Class Names,
203 * i.e. four namespaces into one
204 * - our source of class naming are YANG identifiers, which allow characters not allowed by Java
205 * - we generate class names as well as nested package hierarchy
206 * - we want to generate names which look like Java as much as possible
207 * - we need to always have an (arbitrarily-ugly) fail-safe name
209 * To deal with all that, we split this problem into multiple manageable chunks.
211 * The first chunk is here: we walk all generators and ask them to do two things:
212 * - instantiate their CollisionMembers and link them to appropriate CollisionDomains
213 * - return their collision domain
215 * Then we process we ask collision domains until all domains are resolved, driving the second chunk of the
216 * algorithm in CollisionDomain. Note that we may need to walk the domains multiple times, as the process of
217 * solving a domain may cause another domain's solution to be invalidated.
219 final var domains = new ArrayList<CollisionDomain>();
220 collectCollisionDomains(domains, children);
221 boolean haveUnresolved;
223 haveUnresolved = false;
224 for (var domain : domains) {
225 if (domain.findSolution()) {
226 haveUnresolved = true;
229 } while (haveUnresolved);
232 * Step 9: generate actual Types
234 * We have now properly cross-linked all generators and have assigned their naming roots, so from this point
235 * it looks as though we are performing a simple recursive execution. In reality, though, the actual path taken
236 * through generators is dictated by us as well as generator linkage.
238 for (var module : children) {
239 module.ensureType(builderFactory);
242 LOG.debug("Processed {} modules in {}", generators.size(), sw);
243 state = State.FINISHED;
247 private void collectCollisionDomains(final List<CollisionDomain> result,
248 final Iterable<? extends Generator> parent) {
249 for (var gen : parent) {
251 collectCollisionDomains(result, gen);
252 if (gen instanceof AbstractCompositeGenerator<?, ?> compositeGen) {
253 result.add(compositeGen.domain());
259 <E extends EffectiveStatement<QName, ?>, G extends AbstractExplicitGenerator<E, ?>> G resolveTreeScoped(
260 final Class<G> type, final QName argument) {
261 LOG.trace("Searching for tree-scoped argument {} at {}", argument, stack);
263 // Check if the requested QName matches current module, if it does search the stack
264 final var last = stack.getLast();
265 if (!(last instanceof ModuleGenerator lastModule)) {
266 throw new VerifyException("Unexpected last stack item " + last);
269 if (argument.getModule().equals(lastModule.statement().localQNameModule())) {
270 for (var ancestor : stack) {
271 for (var child : ancestor) {
272 if (type.isInstance(child)) {
273 final var cast = type.cast(child);
274 if (argument.equals(cast.statement().argument())) {
275 LOG.trace("Found matching {}", child);
282 final var module = generators.get(argument.getModule());
283 if (module != null) {
284 for (var child : module) {
285 if (type.isInstance(child)) {
286 final var cast = type.cast(child);
287 if (argument.equals(cast.statement().argument())) {
288 LOG.trace("Found matching {}", child);
296 throw new IllegalStateException("Could not find " + type + " argument " + argument + " in " + stack);
300 ModuleGenerator resolveModule(final QNameModule namespace) {
301 final var module = generators.get(requireNonNull(namespace));
302 if (module == null) {
303 throw new IllegalStateException("Failed to find module for " + namespace);
309 AbstractTypeObjectGenerator<?, ?> resolveLeafref(final PathExpression path) {
310 LOG.trace("Resolving path {}", path);
311 verify(inferenceStack.isEmpty(), "Unexpected data tree state %s", inferenceStack);
313 // Populate inferenceStack with a grouping + data tree equivalent of current stack's state.
314 final var it = stack.descendingIterator();
315 // Skip first item, as it points to our children
316 verify(it.hasNext(), "Unexpected empty stack");
319 while (it.hasNext()) {
320 final var item = it.next();
321 if (item instanceof Generator generator) {
322 generator.pushToInference(inferenceStack);
324 throw new VerifyException("Unexpected stack item " + item);
328 return inferenceStack.inGrouping() ? lenientResolveLeafref(path) : strictResolvePath(path);
330 inferenceStack.clear();
334 private @NonNull AbstractTypeAwareGenerator<?, ?, ?> strictResolvePath(final @NonNull PathExpression path) {
336 inferenceStack.resolvePathExpression(path);
337 } catch (IllegalArgumentException e) {
338 throw new IllegalArgumentException("Failed to find leafref target " + path.getOriginalString(), e);
340 return mapToGenerator();
343 private @Nullable AbstractTypeAwareGenerator<?, ?, ?> lenientResolveLeafref(final @NonNull PathExpression path) {
345 inferenceStack.resolvePathExpression(path);
346 } catch (IllegalArgumentException e) {
347 LOG.debug("Ignoring unresolved path {}", path, e);
350 return mapToGenerator();
353 // Map a statement to the corresponding generator
354 private @NonNull AbstractTypeAwareGenerator<?, ?, ?> mapToGenerator() {
355 // Some preliminaries first: we need to be in the correct module to walk the path
356 final var module = inferenceStack.currentModule();
357 final var gen = verifyNotNull(generators.get(module.localQNameModule()),
358 "Cannot find generator for %s", module);
360 // Now kick of the search
361 final var stmtPath = inferenceStack.toInference().statementPath();
362 final var found = gen.findGenerator(stmtPath);
363 if (found instanceof AbstractTypeAwareGenerator<?, ?, ?> typeAware) {
366 throw new VerifyException("Statements " + stmtPath + " resulted in unexpected " + found);
369 // Note: unlike other methods, this method pushes matching child to the stack
370 private void linkUsesDependencies(final Iterable<? extends Generator> parent) {
371 for (var child : parent) {
372 if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
373 LOG.trace("Visiting composite {}", composite);
374 stack.push(composite);
375 composite.linkUsesDependencies(this);
376 linkUsesDependencies(composite);
382 private static <T> boolean progressAndClean(final List<T> items, final Function<T, LinkageProgress> function) {
383 boolean progress = false;
385 final var it = items.iterator();
386 while (it.hasNext()) {
387 final var item = it.next();
388 final var tmp = function.apply(item);
389 if (tmp == LinkageProgress.NONE) {
390 LOG.debug("No progress made linking {}", item);
395 if (tmp == LinkageProgress.DONE) {
396 LOG.debug("Finished linking {}", item);
399 LOG.debug("Progress made linking {}", item);
406 private void linkDependencies(final Iterable<? extends Generator> parent) {
407 for (var child : parent) {
408 if (child instanceof AbstractDependentGenerator<?, ?> dependent) {
409 dependent.linkDependencies(this);
410 } else if (child instanceof AbstractCompositeGenerator) {
412 linkDependencies(child);
418 private void bindTypeDefinition(final Iterable<? extends Generator> parent) {
419 for (var child : parent) {
421 if (child instanceof AbstractTypeObjectGenerator<?, ?> typeObject) {
422 typeObject.bindTypeDefinition(this);
423 } else if (child instanceof AbstractCompositeGenerator) {
424 bindTypeDefinition(child);
430 private void resolveGroupingUsers() {
431 // Primary pass on modules, collecting all groupings which were left unprocessed
432 // TODO: use a plain List
433 final var remaining = new HashSet<GroupingGenerator>();
434 for (var module : children) {
435 module.linkUsedGroupings(remaining);
437 LOG.debug("Grouping pass 1 found {} groupings", remaining.size());
439 // Secondary passes: if any unprocessed groupings have been marked as used, process their children, potentially
441 final var found = new HashSet<GroupingGenerator>();
445 // Do not process groupings again unless we make some progress
448 final var it = remaining.iterator();
449 while (it.hasNext()) {
450 final var next = it.next();
451 if (next.hasUser()) {
452 // Process this grouping and remember we need to iterate again, as groupings we have already visited
453 // may become used as a side-effect.
455 next.linkUsedGroupings(found);
460 final var foundSize = found.size();
461 LOG.debug("Grouping pass {} processed {} and found {} grouping(s)", passes, processed, foundSize);
462 if (foundSize != 0) {
463 // we have some more groupings to process, shove them into the next iteration
464 remaining.addAll(found);
469 } while (processed != 0);
471 LOG.debug("Grouping usage completed after {} pass(es) with unused {} grouping(s)", passes, remaining.size());
474 private static void freezeGroupingUsers(final Iterable<? extends Generator> parent) {
475 for (var child : parent) {
476 if (child instanceof AbstractCompositeGenerator<?, ?> composite) {
477 if (composite instanceof GroupingGenerator grouping) {
478 grouping.freezeUsers();
480 freezeGroupingUsers(composite);