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.Beta;
11 import com.google.common.annotations.VisibleForTesting;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
16 import java.util.stream.Collectors;
17 import javax.inject.Inject;
18 import javax.inject.Singleton;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.kohsuke.MetaInfServices;
21 import org.opendaylight.mdsal.binding.generator.api.BindingGenerator;
22 import org.opendaylight.mdsal.binding.generator.impl.reactor.Generator;
23 import org.opendaylight.mdsal.binding.generator.impl.reactor.GeneratorReactor;
24 import org.opendaylight.mdsal.binding.generator.impl.reactor.ModuleGenerator;
25 import org.opendaylight.mdsal.binding.generator.impl.reactor.TypeBuilderFactory;
26 import org.opendaylight.mdsal.binding.model.api.GeneratedTransferObject;
27 import org.opendaylight.mdsal.binding.model.api.GeneratedType;
28 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
29 import org.opendaylight.yangtools.yang.model.api.Module;
30 import org.opendaylight.yangtools.yang.model.api.stmt.ModuleEffectiveStatement;
33 * Default implementation of {@link BindingGenerator}.
38 // Note: not exposed in OSGi on purpose, as this should only be needed at compile-time
39 public final class DefaultBindingGenerator implements BindingGenerator {
41 public DefaultBindingGenerator() {
46 public List<GeneratedType> generateTypes(final EffectiveModelContext context,
47 final Collection<? extends Module> modules) {
48 return generateFor(context, modules);
52 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context) {
53 return generateFor(context, context.getModules());
57 * Resolves generated types from {@code context} schema nodes only for modules specified in {@code modules}.
58 * Generated types are created for modules, groupings, types, containers, lists, choices, augments, rpcs,
59 * notification, identities and actions.
61 * @param context schema context which contains data about all schema nodes saved in modules
62 * @param modules set of modules for which schema nodes should be generated types
63 * @return list of types (usually a {@link GeneratedType} or an {@link GeneratedTransferObject}), which:
65 * <li>are generated from {@code context} schema nodes and</li>
66 * <li>are also part of some of the module in {@code modules} set.</li>
68 * @throws NullPointerException if any argument is {@code null}, or if {@code modules} contains a {@code null}
72 static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context,
73 final Collection<? extends Module> modules) {
74 final Set<ModuleEffectiveStatement> filter = modules.stream().map(Module::asEffectiveStatement)
75 .collect(Collectors.toUnmodifiableSet());
77 final List<GeneratedType> result = new ArrayList<>();
78 for (ModuleGenerator gen : new GeneratorReactor(context).execute(TypeBuilderFactory.codegen()).values()) {
79 if (filter.contains(gen.statement())) {
80 addTypes(result, gen);
87 private static void addTypes(final List<GeneratedType> result, final Generator gen) {
89 .filter(type -> type.getIdentifier().immediatelyEnclosingClass().isEmpty())
90 .ifPresent(result::add);
91 result.addAll(gen.auxiliaryGeneratedTypes());
92 for (Generator child : gen) {
93 addTypes(result, child);