2 * Copyright (c) 2016 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
9 package org.opendaylight.mdsal.binding2.generator.impl;
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;
15 import com.google.common.annotations.Beta;
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
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;
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.
36 public class BindingGeneratorImpl implements BindingGenerator {
39 * When set to true, generated classes will include Javadoc comments
40 * which are useful for users.
42 private final boolean verboseClassComments;
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.
50 private Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders = new HashMap<>();
52 private Map<Module, ModuleContext> genCtx = new HashMap<>();
55 * Creates a new binding generator v2.
57 * @param verboseClassComments generate verbose comments
59 public BindingGeneratorImpl(final boolean verboseClassComments) {
60 this.verboseClassComments = verboseClassComments;
64 * Provide methods for converting YANG types to JAVA types.
66 private TypeProvider typeProvider;
69 * Holds reference to schema context to resolve data of augmented element
70 * when creating augmentation builder
72 private SchemaContext schemaContext;
75 * Resolves generated types from <code>context</code> schema nodes of all
78 * Generated types are created for modules, groupings, types, containers,
79 * lists, choices, augments, rpcs, notification, identities.
82 * schema context which contains data about all schema nodes
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
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);
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.");
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);
114 for (final Module contextModule : contextModules) {
115 genCtx = ModuleToGenType.generate(contextModule, context, typeProvider, verboseClassComments);
117 for (final Module contextModule : contextModules) {
118 genCtx = AugmentToGenType.generate(contextModule, schemaContext, genCtx,
119 genTypeBuilders, verboseClassComments);
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);
132 return filteredGenTypes;