9af13aa2932ff9a6a37df4dabddd92d949650ff3
[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 java.util.Collection;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
25 import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl;
26 import org.opendaylight.mdsal.binding.javav2.model.api.Type;
27 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
28 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
29 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
30 import org.opendaylight.yangtools.yang.model.api.Module;
31 import org.opendaylight.yangtools.yang.model.api.NotificationDefinition;
32 import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
35 import org.opendaylight.yangtools.yang.model.util.DataNodeIterator;
36
37 @Beta
38 final class ModuleToGenType {
39
40     private ModuleToGenType() {
41         throw new UnsupportedOperationException("Utility class");
42     }
43
44     static Map<Module, ModuleContext> generate(final Module module, final Map<String, Map<String, GeneratedTypeBuilder>>
45             genTypeBuilders, final SchemaContext schemaContext, final TypeProvider typeProvider, Map<Module,
46             ModuleContext> genCtx, final boolean verboseClassComments) {
47
48         genCtx.put(module, new ModuleContext());
49         genCtx = allTypeDefinitionsToGenTypes(module, genCtx, typeProvider);
50         genCtx = groupingsToGenTypes(module, module.getGroupings(), genCtx, schemaContext, verboseClassComments,
51                 genTypeBuilders, typeProvider);
52         genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
53                 genTypeBuilders, typeProvider);
54         genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
55
56         //TODO: call generate for other entities (identities)
57
58         if (!module.getChildNodes().isEmpty()) {
59             final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
60             genCtx.get(module).addModuleNode(moduleType);
61             final String basePackageName = BindingMapping.getRootPackageName(module);
62             GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
63                     .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider);
64         }
65
66         return genCtx;
67     }
68
69     /**
70      * Converts all extended type definitions of module to the list of
71      * <code>Type</code> objects.
72      *
73      * @param module
74      *            module from which is obtained set of type definitions
75      * @throws IllegalArgumentException
76      *             <ul>
77      *             <li>if module is null</li>
78      *             <li>if name of module is null</li>
79      *             </ul>
80      * @throws IllegalStateException
81      *             if set of type definitions from module is null
82      */
83     private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, final Map<Module, ModuleContext> genCtx,
84             final TypeProvider typeProvider) {
85         Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
86         Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
87         final DataNodeIterator it = new DataNodeIterator(module);
88         final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
89         Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
90
91         typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
92             final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
93                     typedef);
94             if (type != null) {
95                 final ModuleContext ctx = genCtx.get(module);
96                 ctx.addTypedefType(typedef.getPath(), type);
97                 ctx.addTypeToSchema(type, typedef);
98             }
99         });
100         return genCtx;
101     }
102
103     private static Map<Module, ModuleContext> actionsAndRPCMethodsToGenType(final Module module, Map<Module,
104             ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
105             final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
106
107         genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
108                 genTypeBuilders, typeProvider);
109         genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
110                 genTypeBuilders, typeProvider);
111
112         return genCtx;
113     }
114
115     /**
116      * Converts all <b>notifications</b> of the module to the list of
117      * <code>Type</code> objects. In addition are to this list added containers
118      * and lists which are part of this notification.
119      *
120      * @param module
121      *            module from which is obtained set of all notification objects
122      *            to iterate over them
123      * @throws IllegalArgumentException
124      *             <ul>
125      *             <li>if the module equals null</li>
126      *             <li>if the name of module equals null</li>
127      *             </ul>
128      * @throws IllegalStateException
129      *             if set of notifications from module is null
130      */
131     private static Map<Module, ModuleContext> notificationsToGenType(final Module module, final Map<Module, ModuleContext> genCtx,
132             final SchemaContext schemaContext, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
133             final boolean verboseClassComments, final TypeProvider typeProvider) {
134         checkArgument(module != null, "Module reference cannot be NULL.");
135         checkArgument(module.getName() != null, "Module name cannot be NULL.");
136         final Set<NotificationDefinition> notifications = module.getNotifications();
137         if (notifications.isEmpty()) {
138             return genCtx;
139         }
140
141         final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
142         listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
143         final String basePackageName = BindingMapping.getRootPackageName(module);
144
145         for (final NotificationDefinition notification : notifications) {
146             if (notification != null) {
147                 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
148                         verboseClassComments, genTypeBuilders, typeProvider, genCtx);
149             }
150         }
151
152         //YANG 1.1 allows notifications be tied to containers and lists
153         final Collection<DataSchemaNode> potentials = module.getChildNodes();
154
155         for (final DataSchemaNode potential : potentials) {
156             if (potential instanceof NotificationNodeContainer) {
157                 final Set<NotificationDefinition> tiedNotifications = ((NotificationNodeContainer) potential)
158                         .getNotifications();
159                 for (final NotificationDefinition tiedNotification: tiedNotifications) {
160                     if (tiedNotification != null) {
161                         resolveNotification(listenerInterface, potential.getQName().getLocalName(), basePackageName,
162                                 tiedNotification, module, schemaContext, verboseClassComments, genTypeBuilders,
163                                 typeProvider, genCtx);
164                         notifications.add(tiedNotification);
165                     }
166                 }
167             }
168         }
169
170         listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
171
172         genCtx.get(module).addTopLevelNodeType(listenerInterface);
173
174         return genCtx;
175     }
176 }