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 java.util.Collection;
21 import java.util.List;
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;
38 final class ModuleToGenType {
40 private ModuleToGenType() {
41 throw new UnsupportedOperationException("Utility class");
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) {
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);
56 //TODO: call generate for other entities (identities)
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);
70 * Converts all extended type definitions of module to the list of
71 * <code>Type</code> objects.
74 * module from which is obtained set of type definitions
75 * @throws IllegalArgumentException
77 * <li>if module is null</li>
78 * <li>if name of module is null</li>
80 * @throws IllegalStateException
81 * if set of type definitions from module is null
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.");
91 typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> {
92 final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef,
95 final ModuleContext ctx = genCtx.get(module);
96 ctx.addTypedefType(typedef.getPath(), type);
97 ctx.addTypeToSchema(type, typedef);
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) {
107 genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
108 genTypeBuilders, typeProvider);
109 genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments,
110 genTypeBuilders, typeProvider);
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.
121 * module from which is obtained set of all notification objects
122 * to iterate over them
123 * @throws IllegalArgumentException
125 * <li>if the module equals null</li>
126 * <li>if the name of module equals null</li>
128 * @throws IllegalStateException
129 * if set of notifications from module is null
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()) {
141 final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments);
142 listenerInterface.addImplementsType(NOTIFICATION_LISTENER);
143 final String basePackageName = BindingMapping.getRootPackageName(module);
145 for (final NotificationDefinition notification : notifications) {
146 if (notification != null) {
147 resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext,
148 verboseClassComments, genTypeBuilders, typeProvider, genCtx);
152 //YANG 1.1 allows notifications be tied to containers and lists
153 final Collection<DataSchemaNode> potentials = module.getChildNodes();
155 for (final DataSchemaNode potential : potentials) {
156 if (potential instanceof NotificationNodeContainer) {
157 final Set<NotificationDefinition> tiedNotifications = ((NotificationNodeContainer) potential)
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);
170 listenerInterface.setDescription(createDescription(notifications, module, verboseClassComments));
172 genCtx.get(module).addTopLevelNodeType(listenerInterface);