2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.binding.javav2.generator.impl;
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;
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;
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.
32 public class BindingGeneratorImpl implements BindingGenerator {
35 * When set to true, generated classes will include Javadoc comments
36 * which are useful for users.
38 private final boolean verboseClassComments;
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.
46 //TODO: convert it to local variable eventually
47 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
49 private Map<Module, ModuleContext> genCtx = new HashMap<>();
52 * Provide methods for converting YANG types to JAVA types.
54 private TypeProvider typeProvider;
57 * Creates a new binding generator v2.
59 * @param verboseClassComments generate verbose comments
61 public BindingGeneratorImpl(final boolean verboseClassComments) {
62 this.verboseClassComments = verboseClassComments;
66 * Resolves generated types from <code>context</code> schema nodes of all modules.
68 * Generated types are created for modules, groupings, types, containers, lists, choices, augments, rpcs,
69 * notification, identities.
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
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);
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.");
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<>();
98 for (final Module contextModule : contextModules) {
99 genCtx = ModuleToGenType.generate(contextModule, genTypeBuilders, context, typeProvider,
100 verboseClassComments);
102 for (final Module contextModule : contextModules) {
103 genCtx = AugmentToGenType.generate(contextModule, context, genCtx,
104 genTypeBuilders, verboseClassComments);
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);
118 return filteredGenTypes;