Use @Nullable in AbstractExplicitGenerator.runtimeType()
[mdsal.git] / binding / mdsal-binding-generator / src / main / java / org / opendaylight / mdsal / binding / generator / impl / DefaultBindingGenerator.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.binding.generator.impl;
9
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;
25
26 /**
27  * Default implementation of {@link BindingGenerator}.
28  */
29 @MetaInfServices
30 // Note: not exposed in DI on purpose, as this should only be needed at compile-time
31 public final class DefaultBindingGenerator implements BindingGenerator {
32     @Override
33     public List<GeneratedType> generateTypes(final EffectiveModelContext context,
34             final Collection<? extends Module> modules) {
35         return generateFor(context, modules);
36     }
37
38     @VisibleForTesting
39     static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context) {
40         return generateFor(context, context.getModules());
41     }
42
43     /**
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.
47      *
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:
51      *         <ul>
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>
54      *         </ul>
55      * @throws NullPointerException if any argument is {@code null}, or if {@code modules} contains a {@code null}
56      *                              element
57      */
58     @VisibleForTesting
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());
63
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);
68             }
69         }
70
71         return result;
72     }
73
74     private static void addTypes(final List<GeneratedType> result, final Generator gen) {
75         final var type = gen.generatedType();
76         if (type != null) {
77             if (type.getIdentifier().immediatelyEnclosingClass().isEmpty()) {
78                 result.add(type);
79             }
80         }
81
82         result.addAll(gen.auxiliaryGeneratedTypes());
83         for (var child : gen) {
84             addTypes(result, child);
85         }
86     }
87 }