5e7b6bf407b927765b5c1c4d6dfd2d0a837181c4
[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.Set;
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;
28
29 /**
30  * Default implementation of {@link BindingGenerator}.
31  */
32 @MetaInfServices
33 // Note: not exposed in DI on purpose, as this should only be needed at compile-time
34 public final class DefaultBindingGenerator implements BindingGenerator {
35     @Override
36     public List<GeneratedType> generateTypes(final EffectiveModelContext context,
37             final Collection<? extends Module> modules) {
38         return generateFor(context, modules);
39     }
40
41     @VisibleForTesting
42     static @NonNull List<GeneratedType> generateFor(final EffectiveModelContext context) {
43         return generateFor(context, context.getModules());
44     }
45
46     /**
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.
50      *
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:
54      *         <ul>
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>
57      *         </ul>
58      * @throws NullPointerException if any argument is {@code null}, or if {@code modules} contains a {@code null}
59      *                              element
60      */
61     @VisibleForTesting
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());
66
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);
71             }
72         }
73
74         return result;
75     }
76
77     private static void addTypes(final List<GeneratedType> result, final Generator gen) {
78         gen.generatedType()
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);
84         }
85     }
86 }