Bug 1411 #3 BindingGeneratorImpl decomposition - Leafs
[mdsal.git] / binding2 / mdsal-binding2-generator-impl / src / main / java / org / opendaylight / mdsal / binding / javav2 / generator / impl / ModuleToGenType.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
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
10
11 import com.google.common.annotations.Beta;
12 import com.google.common.base.Preconditions;
13 import java.util.List;
14 import java.util.Map;
15 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
16 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
17 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
18 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
19 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
23 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
24
25 @Beta
26 final class ModuleToGenType {
27
28     private ModuleToGenType() {
29         throw new UnsupportedOperationException("Utility class");
30     }
31
32     static Map<Module, ModuleContext> generate(final Module module, Map<String, Map<String, GeneratedTypeBuilder>>
33             genTypeBuilders, final SchemaContext schemaContext, TypeProvider typeProvider, Map<Module,
34             ModuleContext> genCtx, final boolean verboseClassComments) {
35
36         genCtx.put(module, new ModuleContext());
37         genCtx = allTypeDefinitionsToGenTypes(module, genCtx, typeProvider);
38
39         //TODO: call generate for other entities (groupings, rpcs, identities, notifications)
40
41         if (!module.getChildNodes().isEmpty()) {
42             final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
43             genCtx.get(module).addModuleNode(moduleType);
44             final String basePackageName = BindingMapping.getRootPackageName(module);
45             GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
46                     .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
47         }
48
49         return genCtx;
50     }
51
52     /**
53      * Converts all extended type definitions of module to the list of
54      * <code>Type</code> objects.
55      *
56      * @param module
57      *            module from which is obtained set of type definitions
58      * @throws IllegalArgumentException
59      *             <ul>
60      *             <li>if module is null</li>
61      *             <li>if name of module is null</li>
62      *             </ul>
63      * @throws IllegalStateException
64      *             if set of type definitions from module is null
65      */
66     private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, Map<Module, ModuleContext> genCtx,
67                                                      TypeProvider typeProvider) {
68         Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
69         Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
70         final DataNodeIterator it = new DataNodeIterator(module);
71         final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
72         Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
73
74         typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
75             final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
76                     typedef);
77             if (type != null) {
78                 final ModuleContext ctx = genCtx.get(module);
79                 ctx.addTypedefType(typedef.getPath(), type);
80                 ctx.addTypeToSchema(type, typedef);
81             }
82         });
83         return genCtx;
84     }
85 }