2 * Copyright (c) 2020 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;
10 import com.google.common.annotations.VisibleForTesting;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.List;
14 import java.util.stream.Collectors;
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.kohsuke.MetaInfServices;
17 import org.opendaylight.mdsal.binding.generator.BindingGenerator;
18 import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
19 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
20 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
21 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
22 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.Module;
27 * Default implementation of {@link BindingGenerator}.
30 // Note: not exposed in DI on purpose, as this should only be needed at compile-time
31 public final class DefaultBindingGenerator implements BindingGenerator {
33 public List<GeneratedType> generateTypes(final EffectiveModelContext context,
34 final Collection<? extends Module> modules) {
35 return generateFor(context, modules);
39 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context) {
40 return generateFor(context, context.getModules());
44 * Resolves generated types from {@code context} schema nodes only for modules specified in {@code modules}.
45 * Generated types are created for modules, groupings, types, containers, lists, choices, augments, rpcs,
46 * notification, identities and actions.
48 * @param context schema context which contains data about all schema nodes saved in modules
49 * @param modules set of modules for which schema nodes should be generated types
50 * @return list of types (usually a {@link GeneratedType} or an {@link GeneratedTransferObject}), which:
52 * <li>are generated from {@code context} schema nodes and</li>
53 * <li>are also part of some of the module in {@code modules} set.</li>
55 * @throws NullPointerException if any argument is {@code null}, or if {@code modules} contains a {@code null}
59 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context,
60 final Collection<? extends Module> modules) {
61 final var filter = modules.stream().map(Module::asEffectiveStatement)
62 .collect(Collectors.toUnmodifiableSet());
64 final var result = new ArrayList<GeneratedType>();
65 for (var gen : new GeneratorReactor(context).execute(TypeBuilderFactory.codegen()).values()) {
66 if (filter.contains(gen.statement())) {
67 addTypes(result, gen);
74 private static void addTypes(final List<GeneratedType> result, final Generator gen) {
75 final var type = gen.generatedType();
77 if (type.getIdentifier().immediatelyEnclosingClass().isEmpty()) {
82 result.addAll(gen.auxiliaryGeneratedTypes());
83 for (var child : gen) {
84 addTypes(result, child);