2 * Copyright (c) 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.mdsal.binding.javav2.generator.impl;
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;
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;
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;
43 final class ModuleToGenType {
45 private ModuleToGenType() {
46 throw new UnsupportedOperationException("Utility class");
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) {
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);
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);
74 * Converts all extended type definitions of module to the list of
75 * <code>Type</code> objects.
78 * module from which is obtained set of type definitions
79 * @throws IllegalArgumentException
81 * <li>if module is null</li>
82 * <li>if name of module is null</li>
84 * @throws IllegalStateException
85 * if set of type definitions from module is null
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.");
95 typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
96 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
99 final ModuleContext ctx = genCtx.get(module);
100 ctx.addTypedefType(typedef.getPath(), type);
101 ctx.addTypeToSchema(type, typedef);
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) {
111 genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
112 genTypeBuilders, typeProvider);
113 genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
114 genTypeBuilders, typeProvider);
120 * Converts all <b>identities</b> of the module to the list of
121 * <code>Type</code> objects.
124 * module from which is obtained set of all identity objects to
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
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) {
137 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
138 final String basePackageName = BindingMapping.getRootPackageName(module);
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);
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.
157 * module from which is obtained set of all notification objects
158 * to iterate over them
159 * @throws IllegalArgumentException
161 * <li>if the module equals null</li>
162 * <li>if the name of module equals null</li>
164 * @throws IllegalStateException
165 * if set of notifications from module is null
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()) {
177 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
178 listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
179 final String basePackageName = BindingMapping.getRootPackageName(module);
181 for (final NotificationDefinition notification : notifications) {
182 if (notification != null) {
183 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
184 verboseClassComments, genTypeBuilders, typeProvider, genCtx);
188 //YANG 1.1 allows notifications be tied to containers and lists
189 final Collection<DataSchemaNode> potentials = module.getChildNodes();
190 Set<NotificationDefinition> tiedNotifications = null;
192 for (final DataSchemaNode potential : potentials) {
193 if (potential instanceof NotificationNodeContainer) {
194 tiedNotifications = ((NotificationNodeContainer) potential)
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);
206 if (tiedNotifications != null) {
207 listenerInterface.setDescription(createDescription(ImmutableSet.<NotificationDefinition>builder()
208 .addAll(notifications).addAll(tiedNotifications).build(), module, verboseClassComments));
210 listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
213 genCtx.get(module).addTopLevelNodeType(listenerInterface);