53f5bcd16bd628e83e81ca7bdbd5bbfeb67965c0
[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.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
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 public final class DefaultBindingGenerator implements BindingGenerator {
35     @Override
36     public List<Type> generateTypes(final SchemaContext context, final Collection<? extends Module> modules) {
37         return generateFor(context, modules);
38     }
39
40     @VisibleForTesting
41     static @NonNull List<Type> generateFor(final SchemaContext context) {
42         return generateFor(context, context.getModules());
43     }
44
45     /**
46      * Resolves generated types from <code>context</code> schema nodes only for modules specified
47      * in <code>modules</code>. Generated types are created for modules, groupings, types, containers, lists, choices,
48      * augments, rpcs, notification, identities.
49      *
50      * @param context schema context which contains data about all schema nodes saved in modules
51      * @param modules set of modules for which schema nodes should be generated types
52      * @return list of types (usually <code>GeneratedType</code> or
53      *         <code>GeneratedTransferObject</code>) which:
54      *         <ul>
55      *         <li>are generated from <code>context</code> schema nodes and</li>
56      *         <li>are also part of some of the module in <code>modules</code>
57      *         set.</li>
58      *         </ul>
59      * @throws IllegalArgumentException
60      *             <ul>
61      *             <li>if arg <code>context</code> is null or</li>
62      *             <li>if arg <code>modules</code> is null</li>
63      *             </ul>
64      * @throws IllegalStateException
65      *             if <code>context</code> contain no modules
66      */
67     @VisibleForTesting
68     static @NonNull List<Type> generateFor(final SchemaContext context, final Collection<? extends Module> modules) {
69         GeneratorUtils.checkContext(context);
70         checkArgument(modules != null, "Set of Modules cannot be NULL.");
71
72         final Map<SchemaNode, JavaTypeName> renames = new IdentityHashMap<>();
73         for (;;) {
74             try {
75                 return new CodegenTypeGenerator(context, renames).toTypes(modules);
76             } catch (RenameMappingException e) {
77                 GeneratorUtils.rename(renames, e);
78             }
79         }
80     }
81 }