fc5a2026889b6b8d5e9f27fffa7980a64e36bea8
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / BindingGeneratorImpl.java
1 /*
2  * Copyright (c) 2017 Cisco Systems, Inc. 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.javav2.generator.impl;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.mdsal.binding.javav2.generator.api.BindingGenerator;
18 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
19 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
20 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
21 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.parser.util.ModuleDependencySort;
25
26 /**
27  * Main class for Binding generator v2. Provides transformation of Schema Context to
28  * generated transfer objects. Process is accompanied with Twirl templates to generate
29  * particular Javadoc for related YANG elements.
30  */
31 @Beta
32 public class BindingGeneratorImpl implements BindingGenerator {
33
34     /**
35      * When set to true, generated classes will include Javadoc comments
36      * which are useful for users.
37      */
38     private final boolean verboseClassComments;
39
40     /**
41      * Outer key represents the package name. Outer value represents map of all
42      * builders in the same package. Inner key represents the schema node name
43      * (in JAVA class/interface name format). Inner value represents instance of
44      * builder for schema node specified in key part.
45      */
46     //TODO: convert it to local variable eventually
47     private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
48
49     private Map<Module, ModuleContext> genCtx = new HashMap<>();
50
51     /**
52      * Provide methods for converting YANG types to JAVA types.
53      */
54     private TypeProvider typeProvider;
55
56     /**
57      * Creates a new binding generator v2.
58      *
59      * @param verboseClassComments generate verbose comments
60      */
61     public BindingGeneratorImpl(final boolean verboseClassComments) {
62         this.verboseClassComments = verboseClassComments;
63     }
64
65     /**
66      * Resolves generated types from <code>context</code> schema nodes of all modules.
67      *
68      * Generated types are created for modules, groupings, types, containers, lists, choices, augments, rpcs,
69      * notification, identities.
70      *
71      * @param context schema context which contains data about all schema nodes saved in modules
72      * @return list of types (usually <code>GeneratedType</code> <code>GeneratedTransferObject</code>which are generated
73      *         from <code>context</code> data.
74      * @throws IllegalArgumentException if arg <code>context</code> is null
75      * @throws IllegalStateException if <code>context</code> contain no modules
76      */
77     @Override
78     public List<Type> generateTypes(SchemaContext context) {
79         Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL.");
80         Preconditions.checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
81         final Set<Module> modules = context.getModules();
82         return generateTypes(context, modules);
83     }
84
85     /**
86      * Resolves generated types from <code>context</code> schema nodes only for
87      * modules specified in <code>modules</code>
88      *
89      * Generated types are created for modules, groupings, types, containers,
90      * lists, choices, augments, rpcs, notification, identities.
91      *
92      * @param context
93      *            schema context which contains data about all schema nodes
94      *            saved in modules
95      * @param modules
96      *            set of modules for which schema nodes should be generated
97      *            types
98      * @return list of types (usually <code>GeneratedType</code> or
99      *         <code>GeneratedTransferObject</code>) which:
100      *         <ul>
101      *         <li>are generated from <code>context</code> schema nodes and</li>
102      *         <li>are also part of some of the module in <code>modules</code>
103      *         set.</li>
104      *         </ul>
105      * @throws IllegalArgumentException
106      *             <ul>
107      *             <li>if arg <code>context</code> is null or</li>
108      *             <li>if arg <code>modules</code> is null</li>
109      *             </ul>
110      * @throws IllegalStateException
111      *             if <code>context</code> contain no modules
112      */
113     @Override
114     public List<Type> generateTypes(SchemaContext context, Set<Module> modules) {
115         Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL.");
116         Preconditions.checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
117         Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL.");
118
119         typeProvider = new TypeProviderImpl(context);
120         final Module[] modulesArray = new Module[context.getModules().size()];
121         context.getModules().toArray(modulesArray);
122         final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
123         genTypeBuilders = new HashMap<>();
124
125         for (final Module contextModule : contextModules) {
126             genCtx = ModuleToGenType.generate(contextModule, genTypeBuilders, context, typeProvider,
127                     genCtx, verboseClassComments);
128         }
129         for (final Module contextModule : contextModules) {
130             genCtx = AugmentToGenType.generate(contextModule, context, typeProvider, genCtx,
131                     genTypeBuilders, verboseClassComments);
132         }
133
134         final List<Type> filteredGenTypes = new ArrayList<>();
135         for (final Module m : modules) {
136             final ModuleContext ctx = Preconditions.checkNotNull(genCtx.get(m),
137                     "Module context not found for module %s", m);
138             filteredGenTypes.addAll(ctx.getGeneratedTypes());
139             final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
140             if (additionalTypes != null) {
141                 filteredGenTypes.addAll(additionalTypes);
142             }
143         }
144
145         return filteredGenTypes;
146     }
147 }