X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=binding2%2Fmdsal-binding2-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fmdsal%2Fbinding%2Fjavav2%2Fgenerator%2Fimpl%2FModuleToGenType.java;h=3515c472de7bc2f1d73f2c0f88ba8dfbf7103e07;hb=b020023b71447f7fd2eee3c1eb4108578346da6d;hp=f98f64a82b6565f5f8b530d4041bd1002fecc18a;hpb=710cfedc1de456b7584b1ec02c45b38f5983c3c2;p=mdsal.git diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/ModuleToGenType.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/ModuleToGenType.java old mode 100644 new mode 100755 index f98f64a82b..3515c472de --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/ModuleToGenType.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/ModuleToGenType.java @@ -8,16 +8,37 @@ package org.opendaylight.mdsal.binding.javav2.generator.impl; +import static com.google.common.base.Preconditions.checkArgument; +import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.groupingsToGenTypes; +import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.moduleTypeBuilder; +import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.processUsesImplements; +import static org.opendaylight.mdsal.binding.javav2.generator.impl.GenHelperUtil.resolveNotification; +import static org.opendaylight.mdsal.binding.javav2.generator.util.BindingTypes.NOTIFICATION_LISTENER; + import com.google.common.annotations.Beta; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; + +import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Set; + +import org.opendaylight.mdsal.binding.javav2.generator.context.ModuleContext; import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider; +import org.opendaylight.mdsal.binding.javav2.generator.util.TypeComments; import org.opendaylight.mdsal.binding.javav2.generator.yang.types.TypeProviderImpl; import org.opendaylight.mdsal.binding.javav2.model.api.Type; +import org.opendaylight.mdsal.binding.javav2.model.api.YangSourceDefinition; import org.opendaylight.mdsal.binding.javav2.model.api.type.builder.GeneratedTypeBuilder; +import org.opendaylight.mdsal.binding.javav2.spec.runtime.BindingNamespaceType; import org.opendaylight.mdsal.binding.javav2.util.BindingMapping; +import org.opendaylight.yangtools.yang.model.api.DataSchemaNode; +import org.opendaylight.yangtools.yang.model.api.IdentitySchemaNode; import org.opendaylight.yangtools.yang.model.api.Module; +import org.opendaylight.yangtools.yang.model.api.NotificationDefinition; +import org.opendaylight.yangtools.yang.model.api.NotificationNodeContainer; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.util.DataNodeIterator; @@ -29,23 +50,36 @@ final class ModuleToGenType { throw new UnsupportedOperationException("Utility class"); } - static Map generate(final Module module, Map> - genTypeBuilders, final SchemaContext schemaContext, TypeProvider typeProvider, Map generate(final Module module, final Map> + genTypeBuilders, final SchemaContext schemaContext, final TypeProvider typeProvider, Map genCtx, final boolean verboseClassComments) { genCtx.put(module, new ModuleContext()); genCtx = allTypeDefinitionsToGenTypes(module, genCtx, typeProvider); - - //TODO: call generate for other entities (groupings, rpcs, identities, notifications) + genCtx = groupingsToGenTypes(module, module.getGroupings(), genCtx, schemaContext, verboseClassComments, + genTypeBuilders, typeProvider); + genCtx = allIdentitiesToGenTypes(module, schemaContext, genCtx, verboseClassComments, genTypeBuilders, + typeProvider); if (!module.getChildNodes().isEmpty()) { - final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, verboseClassComments); + final GeneratedTypeBuilder moduleType = GenHelperUtil.moduleToDataType(module, genCtx, + verboseClassComments); genCtx.get(module).addModuleNode(moduleType); final String basePackageName = BindingMapping.getRootPackageName(module); GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, moduleType, moduleType, module - .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider); + .getChildNodes(), genCtx, schemaContext, verboseClassComments, genTypeBuilders, + typeProvider, + BindingNamespaceType.Data); + processUsesImplements(module, module, schemaContext, genCtx, BindingNamespaceType.Data); } + genCtx = notificationsToGenType(module, genCtx, schemaContext, genTypeBuilders, verboseClassComments, + typeProvider); + + //after potential parent data schema nodes + genCtx = actionsAndRPCMethodsToGenType(module, genCtx, schemaContext, verboseClassComments, + genTypeBuilders, typeProvider); + return genCtx; } @@ -53,25 +87,23 @@ final class ModuleToGenType { * Converts all extended type definitions of module to the list of * Type objects. * - * @param module - * module from which is obtained set of type definitions - * @throws IllegalArgumentException - *
    - *
  • if module is null
  • - *
  • if name of module is null
  • - *
- * @throws IllegalStateException - * if set of type definitions from module is null + * @param module module from which is obtained set of type definitions + * @throws IllegalArgumentException
    + *
  • if module is null
  • + *
  • if name of module is null
  • + *
+ * @throws IllegalStateException if set of type definitions from module is null */ - private static Map allTypeDefinitionsToGenTypes(final Module module, Map genCtx, - TypeProvider typeProvider) { + private static Map allTypeDefinitionsToGenTypes(final Module module, final Map genCtx, + final TypeProvider typeProvider) { Preconditions.checkArgument(module != null, "Module reference cannot be NULL."); Preconditions.checkArgument(module.getName() != null, "Module name cannot be NULL."); final DataNodeIterator it = new DataNodeIterator(module); final List> typeDefinitions = it.allTypedefs(); Preconditions.checkState(typeDefinitions != null, "Type Definitions for module «module.name» cannot be NULL."); - typeDefinitions.stream().filter(typedef -> typedef != null).forEach(typedef -> { + typeDefinitions.stream().filter(Objects::nonNull).forEach(typedef -> { final Type type = ((TypeProviderImpl) typeProvider).generatedTypeForExtendedDefinitionType(typedef, typedef); if (type != null) { @@ -82,4 +114,128 @@ final class ModuleToGenType { }); return genCtx; } + + private static Map actionsAndRPCMethodsToGenType(final Module module, Map genCtx, final SchemaContext schemaContext, final boolean verboseClassComments, + final Map> + genTypeBuilders, final + TypeProvider typeProvider) { + + genCtx = RpcActionGenHelper.rpcMethodsToGenType(module, genCtx, schemaContext, verboseClassComments, + genTypeBuilders, typeProvider); + genCtx = RpcActionGenHelper.actionMethodsToGenType(module, genCtx, schemaContext, verboseClassComments, + genTypeBuilders, typeProvider); + + return genCtx; + } + + /** + * Converts all identities of the module to the list of + * Type objects. + * + * @param module module from which is obtained set of all identity objects to + * iterate over them + * @param schemaContext schema context only used as input parameter for method + * {@link GenHelperUtil#identityToGenType(Module, String, IdentitySchemaNode, SchemaContext,Map, boolean)} + * @param genCtx generated context + * @return returns generated context + */ + private static Map allIdentitiesToGenTypes(final Module module, + final SchemaContext schemaContext, Map genCtx, boolean verboseClassComments, + final Map> genTypeBuilders, + final TypeProvider typeProvider) { + + final Set schemaIdentities = module.getIdentities(); + final String basePackageName = BindingMapping.getRootPackageName(module); + + if (schemaIdentities != null && !schemaIdentities.isEmpty()) { + for (final IdentitySchemaNode identity : schemaIdentities) { + GenHelperUtil.identityToGenType(module, basePackageName, identity, schemaContext, genCtx, + verboseClassComments); + } + } + + return genCtx; + } + + /** + * Converts all notifications of the module to the list of + * Type objects. In addition are to this list added containers + * and lists which are part of this notification. + * + * @param module module from which is obtained set of all notification objects + * to iterate over them + * @throws IllegalArgumentException
    + *
  • if the module equals null
  • + *
  • if the name of module equals null
  • + *
+ * @throws IllegalStateException if set of notifications from module is null + */ + private static Map notificationsToGenType(final Module module, final Map genCtx, + final SchemaContext schemaContext, final + Map> + genTypeBuilders, + final boolean verboseClassComments, final + TypeProvider typeProvider) { + checkArgument(module != null, "Module reference cannot be NULL."); + checkArgument(module.getName() != null, "Module name cannot be NULL."); + final Set notifications = module.getNotifications(); + if (notifications.isEmpty()) { + return genCtx; + } + + final GeneratedTypeBuilder listenerInterface = moduleTypeBuilder(module, "Listener", verboseClassComments, + genCtx.get(module)); + listenerInterface.addImplementsType(NOTIFICATION_LISTENER); + final String basePackageName = BindingMapping.getRootPackageName(module); + + for (final NotificationDefinition notification : notifications) { + if (notification != null) { + resolveNotification(listenerInterface, null, basePackageName, notification, module, schemaContext, + verboseClassComments, genTypeBuilders, typeProvider, genCtx); + processUsesImplements(notification, module, schemaContext, genCtx, BindingNamespaceType.Notification); + } + } + + //YANG 1.1 allows notifications be tied to containers and lists + final Collection potentials = module.getChildNodes(); + Set tiedNotifications = null; + + for (final DataSchemaNode potential : potentials) { + if (potential instanceof NotificationNodeContainer) { + tiedNotifications = ((NotificationNodeContainer) potential) + .getNotifications(); + for (final NotificationDefinition tiedNotification : tiedNotifications) { + if (tiedNotification != null) { + resolveNotification(listenerInterface, potential.getQName().getLocalName(), basePackageName, + tiedNotification, module, schemaContext, verboseClassComments, genTypeBuilders, + typeProvider, genCtx); + processUsesImplements(tiedNotification, module, schemaContext, genCtx, BindingNamespaceType + .Notification); + } + } + } + } + + if (verboseClassComments) { + if (tiedNotifications != null) { + YangSourceDefinition.of(module, + ImmutableSet.builder().addAll(notifications).addAll(tiedNotifications) + .build()).ifPresent(listenerInterface::setYangSourceDefinition); + } else { + YangSourceDefinition.of(module, notifications).ifPresent(listenerInterface::setYangSourceDefinition); + } + listenerInterface.addComment(TypeComments.javadoc( + "Interface for receiving the following YANG notifications defined in module " + module.getName() + + "").get()); + } + + genCtx.get(module).addTopLevelNodeType(listenerInterface); + + return genCtx; + } }