Binding generator v2 - Action, ListAction fix
[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 = allIdentitiesToGenTypes(module, schemaContext, genCtx, verboseClassComments,  genTypeBuilders, typeProvider);
58         genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
59
60         if (!module.getChildNodes().isEmpty()) {
61             final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
62             genCtx.get(module).addModuleNode(moduleType);
63             final String basePackageName = BindingMapping.getRootPackageName(module);
64             GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
65                     .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
66         }
67
68         //after potential parent data schema nodes
69         genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
70                 genTypeBuilders, typeProvider);
71
72         return genCtx;
73     }
74
75     /**
76      * Converts all extended type definitions of module to the list of
77      * <code>Type</code> objects.
78      *
79      * @param module
80      *            module from which is obtained set of type definitions
81      * @throws IllegalArgumentException
82      *             <ul>
83      *             <li>if module is null</li>
84      *             <li>if name of module is null</li>
85      *             </ul>
86      * @throws IllegalStateException
87      *             if set of type definitions from module is null
88      */
89     private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, final Map<Module, ModuleContext> genCtx,
90             final TypeProvider typeProvider) {
91         Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
92         Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
93         final DataNodeIterator it = new DataNodeIterator(module);
94         final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
95         Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
96
97         typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
98             final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
99                     typedef);
100             if (type != null) {
101                 final ModuleContext ctx = genCtx.get(module);
102                 ctx.addTypedefType(typedef.getPath(), type);
103                 ctx.addTypeToSchema(type, typedef);
104             }
105         });
106         return genCtx;
107     }
108
109     private static Map<Module, ModuleContext> actionsAndRPCMethodsToGenType(final Module module, Map<Module,
110             ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
111             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
112
113         genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
114                 genTypeBuilders, typeProvider);
115         genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
116                 genTypeBuilders, typeProvider);
117
118         return genCtx;
119     }
120
121     /**
122      * Converts all <b>identities</b> of the module to the list of
123      * <code>Type</code> objects.
124      *
125      * @param module
126      *            module from which is obtained set of all identity objects to
127      *            iterate over them
128      * @param schemaContext
129      *            schema context only used as input parameter for method
130      *            {@link GenHelperUtil#identityToGenType(Module, String, IdentitySchemaNode, SchemaContext, Map, boolean, Map, TypeProvider, Map)}
131      * @param genCtx generated context
132      * @return returns generated context
133      *
134      */
135     private static Map<Module, ModuleContext> allIdentitiesToGenTypes(final Module module,
136             final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx, boolean verboseClassComments,
137             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
138
139         final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
140         final String basePackageName = BindingMapping.getRootPackageName(module);
141
142         if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
143             Map<QName, GeneratedTOBuilderImpl> generatedIdentities = new HashMap<>();
144             for (final IdentitySchemaNode identity : schemaIdentities) {
145                 GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx,
146                     verboseClassComments, genTypeBuilders, typeProvider, generatedIdentities);
147             }
148         }
149
150         return genCtx;
151     }
152
153     /**
154      * Converts all <b>notifications</b> of the module to the list of
155      * <code>Type</code> objects. In addition are to this list added containers
156      * and lists which are part of this notification.
157      *
158      * @param module
159      *            module from which is obtained set of all notification objects
160      *            to iterate over them
161      * @throws IllegalArgumentException
162      *             <ul>
163      *             <li>if the module equals null</li>
164      *             <li>if the name of module equals null</li>
165      *             </ul>
166      * @throws IllegalStateException
167      *             if set of notifications from module is null
168      */
169     private static Map<Module, ModuleContext> notificationsToGenType(final Module module, final Map<Module, ModuleContext> genCtx,
170             final SchemaContext schemaContext, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
171             final boolean verboseClassComments, final TypeProvider typeProvider) {
172         checkArgument(module != null, "Module reference cannot be NULL.");
173         checkArgument(module.getName() != null, "Module name cannot be NULL.");
174         final Set<NotificationDefinition> notifications = module.getNotifications();
175         if (notifications.isEmpty()) {
176             return genCtx;
177         }
178
179         final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
180         listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
181         final String basePackageName = BindingMapping.getRootPackageName(module);
182
183         for (final NotificationDefinition notification : notifications) {
184             if (notification != null) {
185                 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
186                         verboseClassComments, genTypeBuilders, typeProvider, genCtx);
187             }
188         }
189
190         //YANG 1.1 allows notifications be tied to containers and lists
191         final Collection<DataSchemaNode> potentials = module.getChildNodes();
192         Set<NotificationDefinition> tiedNotifications = null;
193
194         for (final DataSchemaNode potential : potentials) {
195             if (potential instanceof NotificationNodeContainer) {
196                 tiedNotifications = ((NotificationNodeContainer) potential)
197                         .getNotifications();
198                 for (final NotificationDefinition tiedNotification: tiedNotifications) {
199                     if (tiedNotification != null) {
200                         resolveNotification(listenerInterface, potential.getQName().getLocalName(), basePackageName,
201                                 tiedNotification, module, schemaContext, verboseClassComments, genTypeBuilders,
202                                 typeProvider, genCtx);
203                     }
204                 }
205             }
206         }
207
208         if (tiedNotifications != null) {
209             listenerInterface.setDescription(createDescription(ImmutableSet.<NotificationDefinition>builder()
210                 .addAll(notifications).addAll(tiedNotifications).build(), module, verboseClassComments));
211         } else {
212             listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
213         }
214
215         genCtx.get(module).addTopLevelNodeType(listenerInterface);
216
217         return genCtx;
218     }
219 }