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;
15 import java.util.stream.Collectors;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.kohsuke.MetaInfServices;
18 import org.opendaylight.mdsal.binding.generator.BindingGenerator;
19 import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
20 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
21 import org.opendaylight.mdsal.binding.generator.impl.reactor.ModuleGenerator;
22 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
23 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
24 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26 import org.opendaylight.yangtools.yang.model.api.Module;
27 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
30 * Default implementation of {@link BindingGenerator}.
33 // Note: not exposed in DI on purpose, as this should only be needed at compile-time
34 public final class DefaultBindingGenerator implements BindingGenerator {
36 public List<GeneratedType> generateTypes(final EffectiveModelContext context,
37 final Collection<? extends Module> modules) {
38 return generateFor(context, modules);
42 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context) {
43 return generateFor(context, context.getModules());
47 * Resolves generated types from {@code context} schema nodes only for modules specified in {@code modules}.
48 * Generated types are created for modules, groupings, types, containers, lists, choices, augments, rpcs,
49 * notification, identities and actions.
51 * @param context schema context which contains data about all schema nodes saved in modules
52 * @param modules set of modules for which schema nodes should be generated types
53 * @return list of types (usually a {@link GeneratedType} or an {@link GeneratedTransferObject}), which:
55 * <li>are generated from {@code context} schema nodes and</li>
56 * <li>are also part of some of the module in {@code modules} set.</li>
58 * @throws NullPointerException if any argument is {@code null}, or if {@code modules} contains a {@code null}
62 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context,
63 final Collection<? extends Module> modules) {
64 final Set<ModuleEffectiveStatement> filter = modules.stream().map(Module::asEffectiveStatement)
65 .collect(Collectors.toUnmodifiableSet());
67 final List<GeneratedType> result = new ArrayList<>();
68 for (ModuleGenerator gen : new GeneratorReactor(context).execute(TypeBuilderFactory.codegen()).values()) {
69 if (filter.contains(gen.statement())) {
70 addTypes(result, gen);
77 private static void addTypes(final List<GeneratedType> result, final Generator gen) {
79 .filter(type -> type.getIdentifier().immediatelyEnclosingClass().isEmpty())
80 .ifPresent(result::add);
81 result.addAll(gen.auxiliaryGeneratedTypes());
82 for (Generator child : gen) {
83 addTypes(result, child);