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