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.GenHelperUtil.groupingsToGenTypes;
13 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.moduleTypeBuilder;
14 import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.processUsesImplements;
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.List;
24 import java.util.Objects;
26 import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext;
27 import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider;
28 import org.opendaylight.mdsal.binding.javav2.generator.util.TypeComments;
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.YangSourceDefinition;
32 import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder;
33 import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType;
34 import org.opendaylight.mdsal.binding.javav2.util.BindingMapping;
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;
45 final class ModuleToGenType {
47 private ModuleToGenType() {
48 throw new UnsupportedOperationException("Utility class");
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) {
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);
61 if (!module.getChildNodes().isEmpty()) {
62 final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments);
63 genCtx.get(module).addModuleNode(moduleType);
64 final String basePackageName = BindingMapping.getRootPackageName(module);
65 GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module
66 .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider,
67 BindingNamespaceType.Data);
68 processUsesImplements(module, module, schemaContext, genCtx, BindingNamespaceType.Data);
71 genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
73 //after potential parent data schema nodes
74 genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
75 genTypeBuilders, typeProvider);
81 * Converts all extended type definitions of module to the list of
82 * <code>Type</code> objects.
85 * module from which is obtained set of type definitions
86 * @throws IllegalArgumentException
88 * <li>if module is null</li>
89 * <li>if name of module is null</li>
91 * @throws IllegalStateException
92 * if set of type definitions from module is null
94 private static Map<Module, ModuleContext> allTypeDefinitionsToGenTypes(final Module module, final Map<Module, ModuleContext> genCtx,
95 final TypeProvider typeProvider) {
96 Preconditions.checkArgument(module != null, "Module reference cannot be NULL.");
97 Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL.");
98 final DataNodeIterator it = new DataNodeIterator(module);
99 final List<TypeDefinition<?>> typeDefinitions = it.allTypedefs();
100 Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL.");
102 typeDefinitions.stream().filter(Objects::nonNull).forEach(typedef -> {
103 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
106 final ModuleContext ctx = genCtx.get(module);
107 ctx.addTypedefType(typedef.getPath(), type);
108 ctx.addTypeToSchema(type, typedef);
114 private static Map<Module, ModuleContext> actionsAndRPCMethodsToGenType(final Module module, Map<Module,
115 ModuleContext> genCtx, final SchemaContext schemaContext, final boolean verboseClassComments,
116 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
118 genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
119 genTypeBuilders, typeProvider);
120 genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
121 genTypeBuilders, typeProvider);
127 * Converts all <b>identities</b> of the module to the list of
128 * <code>Type</code> objects.
131 * module from which is obtained set of all identity objects to
133 * @param schemaContext
134 * schema context only used as input parameter for method
135 * {@link GenHelperUtil#identityToGenType(Module, String, IdentitySchemaNode, SchemaContext, Map, boolean)}
136 * @param genCtx generated context
137 * @return returns generated context
140 private static Map<Module, ModuleContext> allIdentitiesToGenTypes(final Module module,
141 final SchemaContext schemaContext, Map<Module, ModuleContext> genCtx, boolean verboseClassComments,
142 final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders, final TypeProvider typeProvider) {
144 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
145 final String basePackageName = BindingMapping.getRootPackageName(module);
147 if (schemaIdentities != null && !schemaIdentities.isEmpty()) {
148 for (final IdentitySchemaNode identity : schemaIdentities) {
149 GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx,
150 verboseClassComments);
158 * Converts all <b>notifications</b> of the module to the list of
159 * <code>Type</code> objects. In addition are to this list added containers
160 * and lists which are part of this notification.
163 * module from which is obtained set of all notification objects
164 * to iterate over them
165 * @throws IllegalArgumentException
167 * <li>if the module equals null</li>
168 * <li>if the name of module equals null</li>
170 * @throws IllegalStateException
171 * if set of notifications from module is null
173 private static Map<Module, ModuleContext> notificationsToGenType(final Module module, final Map<Module, ModuleContext> genCtx,
174 final SchemaContext schemaContext, final Map<String, Map<String, GeneratedTypeBuilder>> genTypeBuilders,
175 final boolean verboseClassComments, final TypeProvider typeProvider) {
176 checkArgument(module != null, "Module reference cannot be NULL.");
177 checkArgument(module.getName() != null, "Module name cannot be NULL.");
178 final Set<NotificationDefinition> notifications = module.getNotifications();
179 if (notifications.isEmpty()) {
183 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments,
185 listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
186 final String basePackageName = BindingMapping.getRootPackageName(module);
188 for (final NotificationDefinition notification : notifications) {
189 if (notification != null) {
190 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
191 verboseClassComments, genTypeBuilders, typeProvider, genCtx);
192 processUsesImplements(notification, module, schemaContext, genCtx, BindingNamespaceType.Data);
196 //YANG 1.1 allows notifications be tied to containers and lists
197 final Collection<DataSchemaNode> potentials = module.getChildNodes();
198 Set<NotificationDefinition> tiedNotifications = null;
200 for (final DataSchemaNode potential : potentials) {
201 if (potential instanceof NotificationNodeContainer) {
202 tiedNotifications = ((NotificationNodeContainer) potential)
204 for (final NotificationDefinition tiedNotification: tiedNotifications) {
205 if (tiedNotification != null) {
206 resolveNotification(listenerInterface, potential.getQName().getLocalName(), basePackageName,
207 tiedNotification, module, schemaContext, verboseClassComments, genTypeBuilders,
208 typeProvider, genCtx);
209 processUsesImplements(tiedNotification, module, schemaContext, genCtx, BindingNamespaceType.Data);
215 if (verboseClassComments) {
216 if (tiedNotifications != null) {
217 YangSourceDefinition.of(module,
218 ImmutableSet.<NotificationDefinition>builder().addAll(notifications).addAll(tiedNotifications)
219 .build()).ifPresent(listenerInterface::setYangSourceDefinition);
221 YangSourceDefinition.of(module, notifications).ifPresent(listenerInterface::setYangSourceDefinition);
223 listenerInterface.addComment(TypeComments.javadoc(
224 "Interface for receiving the following YANG notifications defined in module <b>" + module.getName()
228 genCtx.get(module).addTopLevelNodeType(listenerInterface);