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%2FAugmentToGenType.java;h=641b71232c5cac92d30cff32516452df569cddd1;hb=b1fa86d7d82b6f20576b8b1f94af90081b44a3e6;hp=91a5fa15cfc775a262627595ad740b8e931842f3;hpb=950d1931ec8830300bf5904dc0af082d9e9dc59e;p=mdsal.git diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AugmentToGenType.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AugmentToGenType.java index 91a5fa15cf..641b71232c 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AugmentToGenType.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/AugmentToGenType.java @@ -17,12 +17,14 @@ import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; +import java.util.stream.Collectors; import org.opendaylight.mdsal.binding.javav2.generator.spi.TypeProvider; import org.opendaylight.mdsal.binding.javav2.generator.util.BindingGeneratorUtil; -import org.opendaylight.mdsal.binding.javav2.generator.util.ReferencedTypeImpl; import org.opendaylight.mdsal.binding.javav2.model.api.Type; 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.common.QName; import org.opendaylight.yangtools.yang.model.api.AugmentationSchema; @@ -64,6 +66,27 @@ final class AugmentToGenType { return otherIt.hasNext() ? -1 : 0; }; + /** + * Comparator based on augment target path. + */ + private static final Comparator>> AUGMENTS_COMP = (o1, o2) -> { + final Iterator thisIt = o1.getKey().getPathFromRoot().iterator(); + final Iterator otherIt = o2.getKey().getPathFromRoot().iterator(); + + while (thisIt.hasNext()) { + if (!otherIt.hasNext()) { + return 1; + } + + final int comp = thisIt.next().compareTo(otherIt.next()); + if (comp != 0) { + return comp; + } + } + + return otherIt.hasNext() ? -1 : 0; + }; + private AugmentToGenType() { throw new UnsupportedOperationException("Utility class"); } @@ -75,9 +98,11 @@ final class AugmentToGenType { * @param module * module from which is obtained list of all augmentation objects * to iterate over them - * @param schemaContext - * @param genCtx - * @param genTypeBuilders + * @param schemaContext actual schema context + * @param typeProvider actual type provider instance + * @param genCtx generated input context + * @param genTypeBuilders auxiliary type builders map + * @param verboseClassComments verbosity switch * * @throws IllegalArgumentException *
    @@ -96,12 +121,28 @@ final class AugmentToGenType { Preconditions.checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL."); final String basePackageName = BindingMapping.getRootPackageName(module); - final List augmentations = resolveAugmentations(module); + final List augmentations = resolveAugmentations(module, schemaContext); Map resultCtx = genCtx; - for (final AugmentationSchema augment : augmentations) { - resultCtx = augmentationToGenTypes(basePackageName, augment, module, schemaContext, verboseClassComments, - resultCtx, genTypeBuilders, typeProvider); + + //let's group augments by target path + Map> augmentationsGrouped = + augmentations.stream().collect(Collectors.groupingBy(AugmentationSchema::getTargetPath)); + + List>> sortedAugmentationsGrouped = + new ArrayList<>(augmentationsGrouped.entrySet()); + Collections.sort(sortedAugmentationsGrouped, AUGMENTS_COMP); + + //process child nodes of grouped augment entries + for (Map.Entry> schemaPathAugmentListEntry : sortedAugmentationsGrouped) { + resultCtx = augmentationToGenTypes(basePackageName, schemaPathAugmentListEntry, module, schemaContext, + verboseClassComments, resultCtx, genTypeBuilders, typeProvider); + + for (AugmentationSchema augSchema : schemaPathAugmentListEntry.getValue()) { + GenHelperUtil.processUsesImplements(augSchema, module, schemaContext, genCtx, BindingNamespaceType.Data); + } + } + return resultCtx; } @@ -119,17 +160,25 @@ final class AugmentToGenType { * @throws IllegalStateException * if set of module augmentations is null */ - private static List resolveAugmentations(final Module module) { + private static List resolveAugmentations(final Module module, final SchemaContext schemaContext) { Preconditions.checkArgument(module != null, "Module reference cannot be NULL."); Preconditions.checkState(module.getAugmentations() != null, "Augmentations Set cannot be NULL."); final Set augmentations = module.getAugmentations(); - final List sortedAugmentations = new ArrayList<>(augmentations); + final List sortedAugmentations = new ArrayList<>(augmentations).stream() + .filter(aug -> !module.equals(findAugmentTargetModule(schemaContext, aug))) + .collect(Collectors.toList()); Collections.sort(sortedAugmentations, AUGMENT_COMP); return sortedAugmentations; } + public static Module findAugmentTargetModule(final SchemaContext schemaContext , final AugmentationSchema aug) { + Preconditions.checkNotNull(aug, "Augmentation schema can not be null."); + final QName first = aug.getTargetPath().getPathFromRoot().iterator().next(); + return schemaContext.findModuleByNamespaceAndRevision(first.getNamespace(), first.getRevision()); + } + /** * Converts augSchema to list of Type which * contains generated type for augmentation. In addition there are also @@ -137,49 +186,46 @@ final class AugmentToGenType { * augSchema node or a generated types for cases are added if * augmented node is choice. * - * @param augmentPackageName + * @param basePackageName * string with the name of the package to which the augmentation * belongs - * @param augSchema - * AugmentationSchema which is contains data about augmentation - * (target path, childs...) - * @param module - * current module - * @param schemaContext - * @param genCtx - * @param genTypeBuilders + * @param schemaPathAugmentListEntry + * list of AugmentationSchema nodes grouped by target path + * @param module current module + * @param schemaContext actual schema context + * @param verboseClassComments verbosity switch + * @param genCtx generated input context + * @param genTypeBuilders auxiliary type builders map + * @param typeProvider actual type provider instance * @throws IllegalArgumentException - *
      - *
    • if augmentPackageName equals null
    • - *
    • if augSchema equals null
    • - *
    + * if augmentPackageName equals null * @throws IllegalStateException * if augment target path is null - * @return + * @return generated context */ - private static Map augmentationToGenTypes(final String augmentPackageName, final AugmentationSchema augSchema, - final Module module, final SchemaContext schemaContext, final boolean verboseClassComments, + private static Map augmentationToGenTypes(final String basePackageName, + final Entry> schemaPathAugmentListEntry, final Module module, + final SchemaContext schemaContext, final boolean verboseClassComments, Map genCtx, Map> genTypeBuilders, final TypeProvider typeProvider) { + Preconditions.checkArgument(basePackageName != null, "Package Name cannot be NULL."); + Preconditions.checkArgument(schemaPathAugmentListEntry != null, "Augmentation List Entry cannot be NULL."); + final SchemaPath targetPath = schemaPathAugmentListEntry.getKey(); + Preconditions.checkState(targetPath != null, + "Augmentation List Entry does not contain Target Path (Target Path is NULL)."); - Map generatedCtx; - Preconditions.checkArgument(augmentPackageName != null, "Package Name cannot be NULL."); - Preconditions.checkArgument(augSchema != null, "Augmentation Schema cannot be NULL."); - Preconditions.checkState(augSchema.getTargetPath() != null, - "Augmentation Schema does not contain Target Path (Target Path is NULL)."); - - generatedCtx = GenHelperUtil.processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders, - verboseClassComments, typeProvider); - final SchemaPath targetPath = augSchema.getTargetPath(); - SchemaNode targetSchemaNode; + final List augmentationSchemaList = schemaPathAugmentListEntry.getValue(); + Preconditions.checkState(augmentationSchemaList.size() > 0, + "Augmentation List cannot be empty."); - targetSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext, targetPath); + SchemaNode targetSchemaNode = SchemaContextUtil.findDataSchemaNode(schemaContext, targetPath); if (targetSchemaNode instanceof DataSchemaNode && ((DataSchemaNode) targetSchemaNode).isAddedByUses()) { if (targetSchemaNode instanceof DerivableSchemaNode) { targetSchemaNode = ((DerivableSchemaNode) targetSchemaNode).getOriginal().orNull(); } if (targetSchemaNode == null) { - throw new IllegalStateException("Failed to find target node from grouping in augmentation " + augSchema + throw new IllegalStateException("Failed to find target node from grouping in augmentation " + + schemaPathAugmentListEntry.getValue().get(0) + " in module " + module.getName()); } } @@ -188,54 +234,57 @@ final class AugmentToGenType { } GeneratedTypeBuilder targetTypeBuilder = GenHelperUtil.findChildNodeByPath(targetSchemaNode.getPath(), - generatedCtx); + genCtx); if (targetTypeBuilder == null) { - targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), generatedCtx); + targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), genCtx); } if (targetTypeBuilder == null) { throw new NullPointerException("Target type not yet generated: " + targetSchemaNode); } - if (!(targetSchemaNode instanceof ChoiceSchemaNode)) { - final String packageName = augmentPackageName; - final Type targetType = new ReferencedTypeImpl(targetTypeBuilder.getPackageName(), - targetTypeBuilder.getName()); - generatedCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, targetType, - augSchema, genTypeBuilders, generatedCtx, schemaContext, verboseClassComments, typeProvider); - return generatedCtx; + final String augmentPackageName = + BindingGeneratorUtil.packageNameWithNamespacePrefix(basePackageName, BindingNamespaceType.Data); + if (!(targetSchemaNode instanceof ChoiceSchemaNode)) { + genCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, augmentPackageName, + targetTypeBuilder.toInstance(), targetSchemaNode, schemaPathAugmentListEntry.getValue(), genTypeBuilders, genCtx, + schemaContext, verboseClassComments, typeProvider, BindingNamespaceType.Data); } else { - generatedCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, augmentPackageName, - targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), - null, generatedCtx, verboseClassComments, genTypeBuilders, typeProvider); - return generatedCtx; + genCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, basePackageName, + targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, + schemaPathAugmentListEntry.getValue(), + null, genCtx, verboseClassComments, genTypeBuilders, typeProvider, + BindingNamespaceType.Data); } + return genCtx; } - public static Map usesAugmentationToGenTypes(final SchemaContext schemaContext, final String - augmentPackageName, final AugmentationSchema augSchema, final Module module, final UsesNode usesNode, final DataNodeContainer - usesNodeParent, Map genCtx, - Map> genTypeBuilders, - final boolean verboseClassComments, final TypeProvider typeProvider) { + @Deprecated + static Map usesAugmentationToGenTypes(final SchemaContext schemaContext, + final String augmentPackageName, final List schemaPathAugmentListEntry, final Module module, + final UsesNode usesNode, final DataNodeContainer usesNodeParent, Map genCtx, + Map> genTypeBuilders, final boolean verboseClassComments, + final TypeProvider typeProvider, final BindingNamespaceType namespaceType) { - Map generatedCtx; Preconditions.checkArgument(augmentPackageName != null, "Package Name cannot be NULL."); - Preconditions.checkArgument(augSchema != null, "Augmentation Schema cannot be NULL."); - Preconditions.checkState(augSchema.getTargetPath() != null, + Preconditions.checkArgument(schemaPathAugmentListEntry != null, + "Augmentation Schema List Entry cannot be NULL."); + Preconditions.checkState(schemaPathAugmentListEntry.size() > 0, + "Augmentation Schema List cannot be empty"); + + final SchemaPath targetPath = schemaPathAugmentListEntry.get(0).getTargetPath(); + Preconditions.checkState(targetPath != null, "Augmentation Schema does not contain Target Path (Target Path is NULL)."); - generatedCtx = GenHelperUtil.processUsesAugments(schemaContext, augSchema, module, genCtx, genTypeBuilders, - verboseClassComments, typeProvider); - final SchemaPath targetPath = augSchema.getTargetPath(); final SchemaNode targetSchemaNode = findOriginalTargetFromGrouping(schemaContext, targetPath, usesNode); if (targetSchemaNode == null) { throw new IllegalArgumentException("augment target not found: " + targetPath); } GeneratedTypeBuilder targetTypeBuilder = GenHelperUtil.findChildNodeByPath(targetSchemaNode.getPath(), - generatedCtx); + genCtx); if (targetTypeBuilder == null) { - targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), generatedCtx); + targetTypeBuilder = GenHelperUtil.findCaseByPath(targetSchemaNode.getPath(), genCtx); } if (targetTypeBuilder == null) { throw new NullPointerException("Target type not yet generated: " + targetSchemaNode); @@ -246,16 +295,22 @@ final class AugmentToGenType { if (usesNodeParent instanceof SchemaNode) { packageName = BindingGeneratorUtil.packageNameForAugmentedGeneratedType(augmentPackageName, ((SchemaNode) usesNodeParent).getPath()); + } else if (usesNodeParent instanceof AugmentationSchema) { + Type parentTypeBuilder = genCtx.get(module).getTargetToAugmentation() + .get(((AugmentationSchema) usesNodeParent).getTargetPath()); + packageName = BindingGeneratorUtil.packageNameForAugmentedGeneratedType(parentTypeBuilder.getPackageName(), + (AugmentationSchema)usesNodeParent); } - generatedCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, packageName, augmentPackageName, - targetTypeBuilder.toInstance(), augSchema, genTypeBuilders, generatedCtx, schemaContext, - verboseClassComments, typeProvider); - return generatedCtx; + genCtx = GenHelperUtil.addRawAugmentGenTypeDefinition(module, packageName, + targetTypeBuilder.toInstance(), targetSchemaNode, schemaPathAugmentListEntry, genTypeBuilders, genCtx, + schemaContext, verboseClassComments, typeProvider, namespaceType); + return genCtx; } else { - generatedCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, augmentPackageName, - targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), - usesNodeParent, generatedCtx, verboseClassComments, genTypeBuilders, typeProvider); - return generatedCtx; + genCtx = generateTypesFromAugmentedChoiceCases(schemaContext, module, augmentPackageName, + targetTypeBuilder.toInstance(), (ChoiceSchemaNode) targetSchemaNode, + schemaPathAugmentListEntry, + usesNodeParent, genCtx, verboseClassComments, genTypeBuilders, typeProvider, namespaceType); + return genCtx; } } @@ -270,15 +325,25 @@ final class AugmentToGenType { */ private static DataSchemaNode findOriginalTargetFromGrouping(final SchemaContext schemaContext, final SchemaPath targetPath, final UsesNode parentUsesNode) { - final SchemaNode targetGrouping = SchemaContextUtil.findNodeInSchemaContext(schemaContext, parentUsesNode - .getGroupingPath() - .getPathFromRoot()); - if (!(targetGrouping instanceof GroupingDefinition)) { + SchemaNode targetGrouping = null; + QName current = parentUsesNode.getGroupingPath().getPathFromRoot().iterator().next(); + Module module = schemaContext.findModuleByNamespaceAndRevision(current.getNamespace(), current.getRevision()); + if (module == null) { + throw new IllegalArgumentException("Fialed to find module for grouping in: " + parentUsesNode); + } else { + for (GroupingDefinition group : module.getGroupings()) { + if (group.getQName().equals(current)) { + targetGrouping = group; + break; + } + } + } + + if (targetGrouping == null) { throw new IllegalArgumentException("Failed to generate code for augment in " + parentUsesNode); } - final GroupingDefinition grouping = (GroupingDefinition) targetGrouping; - SchemaNode result = grouping; + SchemaNode result = targetGrouping; for (final QName node : targetPath.getPathFromRoot()) { if (result instanceof DataNodeContainer) { final QName resultNode = QName.create(result.getQName().getModule(), node.getLocalName()); @@ -335,9 +400,8 @@ final class AugmentToGenType { * Type which represents target choice * @param targetNode * node which represents target choice - * @param augmentedNodes - * set of choice case nodes for which is checked if are/aren't - * added to choice through augmentation + * @param schemaPathAugmentListEntry + * list of AugmentationSchema nodes grouped by target path * @return list of generated types which represents augmented cases of * choice refChoiceType * @throws IllegalArgumentException @@ -347,65 +411,71 @@ final class AugmentToGenType { *
  • if augmentedNodes is null
  • *
*/ - private static Map generateTypesFromAugmentedChoiceCases(final SchemaContext schemaContext, final Module module, - final String basePackageName, final Type targetType, final ChoiceSchemaNode targetNode, - final Iterable augmentedNodes, final DataNodeContainer usesNodeParent, - Map genCtx, final boolean verboseClassComments, - Map> genTypeBuilders, final TypeProvider typeProvider) { + private static Map generateTypesFromAugmentedChoiceCases( + final SchemaContext schemaContext, final Module module, + final String basePackageName, final Type targetType, final ChoiceSchemaNode targetNode, + final List schemaPathAugmentListEntry, + final DataNodeContainer usesNodeParent, + Map genCtx, final boolean verboseClassComments, + Map> genTypeBuilders, final TypeProvider typeProvider, + final BindingNamespaceType namespaceType) { Preconditions.checkArgument(basePackageName != null, "Base Package Name cannot be NULL."); Preconditions.checkArgument(targetType != null, "Referenced Choice Type cannot be NULL."); - Preconditions.checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL."); - - for (final DataSchemaNode caseNode : augmentedNodes) { - if (caseNode != null) { - final String packageName = BindingGeneratorUtil.packageNameForGeneratedType(basePackageName, - caseNode.getPath(), null); - final GeneratedTypeBuilder caseTypeBuilder = GenHelperUtil.addDefaultInterfaceDefinition(packageName, - caseNode, module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider); - caseTypeBuilder.addImplementsType(targetType); - - SchemaNode parent; - final SchemaPath nodeSp = targetNode.getPath(); - parent = SchemaContextUtil.findDataSchemaNode(schemaContext, nodeSp.getParent()); - - GeneratedTypeBuilder childOfType = null; - if (parent instanceof Module) { - childOfType = genCtx.get(parent).getModuleNode(); - } else if (parent instanceof ChoiceCaseNode) { - childOfType = GenHelperUtil.findCaseByPath(parent.getPath(), genCtx); - } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) { - childOfType = GenHelperUtil.findChildNodeByPath(parent.getPath(), genCtx); - } else if (parent instanceof GroupingDefinition) { - childOfType = GenHelperUtil.findGroupingByPath(parent.getPath(), genCtx); - } + Preconditions.checkArgument(schemaPathAugmentListEntry != null, "Set of Choice Case Nodes cannot be NULL."); + + + for (final AugmentationSchema augmentationSchema : schemaPathAugmentListEntry) { + for (final DataSchemaNode caseNode : augmentationSchema.getChildNodes()) { + if (caseNode != null) { + final GeneratedTypeBuilder caseTypeBuilder = GenHelperUtil.addDefaultInterfaceDefinition(basePackageName, + caseNode, module, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider, + namespaceType); + caseTypeBuilder.addImplementsType(targetType); + + SchemaNode parent; + final SchemaPath nodeSp = targetNode.getPath(); + parent = SchemaContextUtil.findDataSchemaNode(schemaContext, nodeSp.getParent()); + + GeneratedTypeBuilder childOfType = null; + if (parent instanceof Module) { + childOfType = genCtx.get(parent).getModuleNode(); + } else if (parent instanceof ChoiceCaseNode) { + childOfType = GenHelperUtil.findCaseByPath(parent.getPath(), genCtx); + } else if (parent instanceof DataSchemaNode || parent instanceof NotificationDefinition) { + childOfType = GenHelperUtil.findChildNodeByPath(parent.getPath(), genCtx); + } else if (parent instanceof GroupingDefinition) { + childOfType = GenHelperUtil.findGroupingByPath(parent.getPath(), genCtx); + } - if (childOfType == null) { - throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode); - } + if (childOfType == null) { + throw new IllegalArgumentException("Failed to find parent type of choice " + targetNode); + } - ChoiceCaseNode node = null; - final String caseLocalName = caseNode.getQName().getLocalName(); - if (caseNode instanceof ChoiceCaseNode) { - node = (ChoiceCaseNode) caseNode; - } else if (targetNode.getCaseNodeByName(caseLocalName) == null) { - final String targetNodeLocalName = targetNode.getQName().getLocalName(); - for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) { - if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName - ().getLocalName())) { - node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName); - break; + ChoiceCaseNode node = null; + final String caseLocalName = caseNode.getQName().getLocalName(); + if (caseNode instanceof ChoiceCaseNode) { + node = (ChoiceCaseNode) caseNode; + } else if (targetNode.getCaseNodeByName(caseLocalName) == null) { + final String targetNodeLocalName = targetNode.getQName().getLocalName(); + for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) { + if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName + ().getLocalName())) { + node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName); + break; + } } + } else { + node = targetNode.getCaseNodeByName(caseLocalName); } - } else { - node = targetNode.getCaseNodeByName(caseLocalName); - } - final Iterable childNodes = node.getChildNodes(); - if (childNodes != null) { - GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, - childNodes, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider); + final Iterable childNodes = node.getChildNodes(); + if (childNodes != null) { + GenHelperUtil.resolveDataSchemaNodes(module, basePackageName, caseTypeBuilder, childOfType, + childNodes, genCtx, schemaContext, verboseClassComments, genTypeBuilders, typeProvider, + namespaceType); + } + genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder); + genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node); } - genCtx.get(module).addCaseType(caseNode.getPath(), caseTypeBuilder); - genCtx.get(module).addChoiceToCaseMapping(targetType, caseTypeBuilder, node); } } return genCtx;