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