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