Update BindingRuntime{Context,Generator,Types}
[mdsal.git] / binding / mdsal-binding-generator-impl / 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 static com.google.common.base.Preconditions.checkArgument;
11
12 import com.google.common.annotations.Beta;
13 import com.google.common.annotations.VisibleForTesting;
14 import java.util.Collection;
15 import java.util.IdentityHashMap;
16 import java.util.List;
17 import java.util.Map;
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.model.api.JavaTypeName;
23 import org.opendaylight.mdsal.binding.model.api.Type;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
27
28 /**
29  * Default implementation of {@link BindingGenerator}.
30  */
31 @Beta
32 @MetaInfServices
33 @Singleton
34 // Note: not exposed in OSGi on purpose, as this should only be needed at compile-time
35 public final class DefaultBindingGenerator implements BindingGenerator {
36     @Override
37     public List<Type> generateTypes(final EffectiveModelContext context, final Collection<? extends Module> modules) {
38         return generateFor(context, modules);
39     }
40
41     @VisibleForTesting
42     static @NonNull List<Type> generateFor(final EffectiveModelContext context) {
43         return generateFor(context, context.getModules());
44     }
45
46     /**
47      * Resolves generated types from <code>context</code> schema nodes only for modules specified
48      * in <code>modules</code>. Generated types are created for modules, groupings, types, containers, lists, choices,
49      * augments, rpcs, notification, identities.
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 <code>GeneratedType</code> or
54      *         <code>GeneratedTransferObject</code>) which:
55      *         <ul>
56      *         <li>are generated from <code>context</code> schema nodes and</li>
57      *         <li>are also part of some of the module in <code>modules</code>
58      *         set.</li>
59      *         </ul>
60      * @throws IllegalArgumentException
61      *             <ul>
62      *             <li>if arg <code>context</code> is null or</li>
63      *             <li>if arg <code>modules</code> is null</li>
64      *             </ul>
65      * @throws IllegalStateException
66      *             if <code>context</code> contain no modules
67      */
68     @VisibleForTesting
69     static @NonNull List<Type> generateFor(final EffectiveModelContext context,
70             final Collection<? extends Module> modules) {
71         GeneratorUtils.checkContext(context);
72         checkArgument(modules != null, "Set of Modules cannot be NULL.");
73
74         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
75         for (;;) {
76             try {
77                 return new CodegenTypeGenerator(context, renames).toTypes(modules);
78             } catch (RenameMappingException e) {
79                 GeneratorUtils.rename(renames, e);
80             }
81         }
82     }
83 }