Bug 1411: BindingGeneratorImpl decomposition - Container schema nodes
[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         typeProvider = new TypeProviderImpl(context);
82         final Set<Module> modules = context.getModules();
83         return generateTypes(context, modules);
84     }
85
86     @Override
87     public List<Type> generateTypes(SchemaContext context, Set<Module> modules) {
88         Preconditions.checkArgument(context != null, "Schema Context reference cannot be NULL.");
89         Preconditions.checkState(context.getModules() != null, "Schema Context does not contain defined modules.");
90         Preconditions.checkArgument(modules != null, "Set of Modules cannot be NULL.");
91
92         typeProvider = new TypeProviderImpl(context);
93         final Module[] modulesArray = new Module[context.getModules().size()];
94         context.getModules().toArray(modulesArray);
95         final List<Module> contextModules = ModuleDependencySort.sort(modulesArray);
96         genTypeBuilders = new HashMap<>();
97
98         for (final Module contextModule : contextModules) {
99             genCtx = ModuleToGenType.generate(contextModule, genTypeBuilders, context, typeProvider,
100                     genCtx, verboseClassComments);
101         }
102         for (final Module contextModule : contextModules) {
103             genCtx = AugmentToGenType.generate(contextModule, context, genCtx,
104                     genTypeBuilders, verboseClassComments);
105         }
106
107         final List<Type> filteredGenTypes = new ArrayList<>();
108         for (final Module m : modules) {
109             final ModuleContext ctx = Preconditions.checkNotNull(genCtx.get(m),
110                     "Module context not found for module %s", m);
111             filteredGenTypes.addAll(ctx.getGeneratedTypes());
112             final Set<Type> additionalTypes = ((TypeProviderImpl) typeProvider).getAdditionalTypes().get(m);
113             if (additionalTypes != null) {
114                 filteredGenTypes.addAll(additionalTypes);
115             }
116         }
117
118         return filteredGenTypes;
119     }
120 }