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