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 = allIdentitiesToGenTypes(module, schemaContext, genCtx, verboseClassComments, genTypeBuilders, typeProvider);
58 genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, typeProvider);
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);
68 //after potential parent data schema nodes
69 genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
70 genTypeBuilders, typeProvider);
76 * Converts all extended type definitions of module to the list of
77 * <code>Type</code> objects.
80 * module from which is obtained set of type definitions
81 * @throws IllegalArgumentException
83 * <li>if module is null</li>
84 * <li>if name of module is null</li>
86 * @throws IllegalStateException
87 * if set of type definitions from module is null
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.");
97 typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
98 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
101 final ModuleContext ctx = genCtx.get(module);
102 ctx.addTypedefType(typedef.getPath(), type);
103 ctx.addTypeToSchema(type, typedef);
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) {
113 genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
114 genTypeBuilders, typeProvider);
115 genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
116 genTypeBuilders, typeProvider);
122 * Converts all <b>identities</b> of the module to the list of
123 * <code>Type</code> objects.
126 * module from which is obtained set of all identity objects to
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
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) {
139 final Set<IdentitySchemaNode> schemaIdentities = module.getIdentities();
140 final String basePackageName = BindingMapping.getRootPackageName(module);
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);
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.
159 * module from which is obtained set of all notification objects
160 * to iterate over them
161 * @throws IllegalArgumentException
163 * <li>if the module equals null</li>
164 * <li>if the name of module equals null</li>
166 * @throws IllegalStateException
167 * if set of notifications from module is null
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()) {
179 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
180 listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
181 final String basePackageName = BindingMapping.getRootPackageName(module);
183 for (final NotificationDefinition notification : notifications) {
184 if (notification != null) {
185 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
186 verboseClassComments, genTypeBuilders, typeProvider, genCtx);
190 //YANG 1.1 allows notifications be tied to containers and lists
191 final Collection<DataSchemaNode> potentials = module.getChildNodes();
192 Set<NotificationDefinition> tiedNotifications = null;
194 for (final DataSchemaNode potential : potentials) {
195 if (potential instanceof NotificationNodeContainer) {
196 tiedNotifications = ((NotificationNodeContainer) potential)
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);
208 if (tiedNotifications != null) {
209 listenerInterface.setDescription(createDescription(ImmutableSet.<NotificationDefinition>builder()
210 .addAll(notifications).addAll(tiedNotifications).build(), module, verboseClassComments));
212 listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
215 genCtx.get(module).addTopLevelNodeType(listenerInterface);