Binding generator v2 - Identities support
[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 static com.google.common.base.Preconditions.checkArgument;
12 import static org.opendaylight.mdsal.binding.javav2.generator.impl.AuxiliaryGenUtils.createDescription;
13 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.groupingsToGenTypes;
14 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.moduleTypeBuilder;
15 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.resolveNotification;
16 import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.NOTIFICATION_LISTENER;
17
18 import com.google.common.annotations.Beta;
19 import com.google.common.base.Preconditions;
20 import com.google.common.collect.ImmutableSet;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
27 import org.opendaylight.mdsal.binding.javav2.generator.util.generated.type.builder.GeneratedTOBuilderImpl;
28 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
29 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
30 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
31 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode;
35 import org.opendaylight.yangtools.yang.model.api.Module;
36 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
37 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
38 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
39 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
41
42 @Beta
43 final class ModuleToGenType {
44
45     private ModuleToGenType() {
46         throw new UnsupportedOperationException("Utility class");
47     }
48
49     static Map<Module, ModuleContext> generate(final Module module, final Map<String, Map<String, GeneratedTypeBuilder>>
50             genTypeBuilders, final SchemaContext schemaContext, final TypeProvider typeProvider, Map<Module,
51             ModuleContext> genCtx, final boolean verboseClassComments) {
52
53         genCtx.put(module, new ModuleContext());
54         genCtx = allTypeDefinitionsToGenTypes(module, genCtx, typeProvider);
55         genCtx = groupingsToGenTypes(module, module.getGroupings(), genCtx, schemaContext, verboseClassComments,
56                 genTypeBuilders, typeProvider);
57         genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
58                 genTypeBuilders, typeProvider);
59         genCtx = allIdentitiesToGenTypes(module, schemaContext, genCtx, verboseClassComments,  genTypeBuilders, typeProvider);
60         genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
61
62         if (!module.getChildNodes().isEmpty()) {
63             final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
64             genCtx.get(module).addModuleNode(moduleType);
65             final String basePackageName = BindingMapping.getRootPackageName(module);
66             GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
67                     .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
68         }
69
70         return genCtx;
71     }
72
73     /**
74      * Converts all extended type definitions of module to the list of
75      * <code>Type</code> objects.
76      *
77      * @param module
78      *            module from which is obtained set of type definitions
79      * @throws IllegalArgumentException
80      *             <ul>
81      *             <li>if module is null</li>
82      *             <li>if name of module is null</li>
83      *             </ul>
84      * @throws IllegalStateException
85      *             if set of type definitions from module is null
86      */
87     private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, final Map<Module, ModuleContext> genCtx,
88             final TypeProvider typeProvider) {
89         Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
90         Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
91         final DataNodeIterator it = new DataNodeIterator(module);
92         final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
93         Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
94
95         typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
96             final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
97                     typedef);
98             if (type != null) {
99                 final ModuleContext ctx = genCtx.get(module);
100                 ctx.addTypedefType(typedef.getPath(), type);
101                 ctx.addTypeToSchema(type, typedef);
102             }
103         });
104         return genCtx;
105     }
106
107     private static Map<Module, ModuleContext> actionsAndRPCMethodsToGenType(final Module module, Map<Module,
108             ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
109             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
110
111         genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
112                 genTypeBuilders, typeProvider);
113         genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
114                 genTypeBuilders, typeProvider);
115
116         return genCtx;
117     }
118
119     /**
120      * Converts all <b>identities</b> of the module to the list of
121      * <code>Type</code> objects.
122      *
123      * @param module
124      *            module from which is obtained set of all identity objects to
125      *            iterate over them
126      * @param schemaContext
127      *            schema context only used as input parameter for method
128      *            {@link GenHelperUtil#identityToGenType(Module, String, IdentitySchemaNode, SchemaContext, Map, boolean, Map, TypeProvider, Map)}
129      * @param genCtx generated context
130      * @return returns generated context
131      *
132      */
133     private static Map<Module, ModuleContext> allIdentitiesToGenTypes(final Module module,
134             final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx, boolean verboseClassComments,
135             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
136
137         final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
138         final String basePackageName = BindingMapping.getRootPackageName(module);
139
140         if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
141             Map<QName, GeneratedTOBuilderImpl> generatedIdentities = new HashMap<>();
142             for (final IdentitySchemaNode identity : schemaIdentities) {
143                 GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx,
144                     verboseClassComments, genTypeBuilders, typeProvider, generatedIdentities);
145             }
146         }
147
148         return genCtx;
149     }
150
151     /**
152      * Converts all <b>notifications</b> of the module to the list of
153      * <code>Type</code> objects. In addition are to this list added containers
154      * and lists which are part of this notification.
155      *
156      * @param module
157      *            module from which is obtained set of all notification objects
158      *            to iterate over them
159      * @throws IllegalArgumentException
160      *             <ul>
161      *             <li>if the module equals null</li>
162      *             <li>if the name of module equals null</li>
163      *             </ul>
164      * @throws IllegalStateException
165      *             if set of notifications from module is null
166      */
167     private static Map<Module, ModuleContext> notificationsToGenType(final Module module, final Map<Module, ModuleContext> genCtx,
168             final SchemaContext schemaContext, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
169             final boolean verboseClassComments, final TypeProvider typeProvider) {
170         checkArgument(module != null, "Module reference cannot be NULL.");
171         checkArgument(module.getName() != null, "Module name cannot be NULL.");
172         final Set<NotificationDefinition> notifications = module.getNotifications();
173         if (notifications.isEmpty()) {
174             return genCtx;
175         }
176
177         final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
178         listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
179         final String basePackageName = BindingMapping.getRootPackageName(module);
180
181         for (final NotificationDefinition notification : notifications) {
182             if (notification != null) {
183                 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
184                         verboseClassComments, genTypeBuilders, typeProvider, genCtx);
185             }
186         }
187
188         //YANG 1.1 allows notifications be tied to containers and lists
189         final Collection<DataSchemaNode> potentials = module.getChildNodes();
190         Set<NotificationDefinition> tiedNotifications = null;
191
192         for (final DataSchemaNode potential : potentials) {
193             if (potential instanceof NotificationNodeContainer) {
194                 tiedNotifications = ((NotificationNodeContainer) potential)
195                         .getNotifications();
196                 for (final NotificationDefinition tiedNotification: tiedNotifications) {
197                     if (tiedNotification != null) {
198                         resolveNotification(listenerInterface, potential.getQName().getLocalName(), basePackageName,
199                                 tiedNotification, module, schemaContext, verboseClassComments, genTypeBuilders,
200                                 typeProvider, genCtx);
201                     }
202                 }
203             }
204         }
205
206         if (tiedNotifications != null) {
207             listenerInterface.setDescription(createDescription(ImmutableSet.<NotificationDefinition>builder()
208                 .addAll(notifications).addAll(tiedNotifications).build(), module, verboseClassComments));
209         } else {
210             listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
211         }
212
213         genCtx.get(module).addTopLevelNodeType(listenerInterface);
214
215         return genCtx;
216     }
217 }