From 8f8dac3e786741f56ed2cd41622bf0ad05db759f Mon Sep 17 00:00:00 2001 From: Jie Han Date: Wed, 23 Aug 2017 17:19:50 +0800 Subject: [PATCH] Binding codec v2 - fix augmentation #3 - Since multiple augments with same target were grouped as like just one augment, all child nodes of them were resolved as methods to the generated type, so in runtime, it should also find all these child nodes to add to the new EffectiveAugmentSchema object as real childs. Change-Id: I433930be45007e545b2e2fb074af32e118aa7cd4 Signed-off-by: Jie Han --- .../generator/context/ModuleContext.java | 12 +++-- .../javav2/generator/impl/GenHelperUtil.java | 2 + .../context/BindingRuntimeContext.java | 44 +++++++++++-------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/context/ModuleContext.java b/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/context/ModuleContext.java index 34f0114cc8..2f3cef5cc2 100644 --- a/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/context/ModuleContext.java +++ b/binding2/mdsal-binding2-generator-api/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/context/ModuleContext.java @@ -52,7 +52,7 @@ public final class ModuleContext { private final Map identities = new HashMap<>(); private final Set topLevelNodes = new HashSet<>(); private final List augmentations = new ArrayList<>(); - private final BiMap typeToAugmentation = HashBiMap.create(); + private final Multimap typeToAugmentations = HashMultimap.create(); private final BiMap targetToAugmentation = HashBiMap.create(); private final Map typeToSchema = new HashMap<>(); private final Multimap choiceToCases = HashMultimap.create(); @@ -169,8 +169,8 @@ public final class ModuleContext { return Collections.unmodifiableList(this.augmentations); } - public BiMap getTypeToAugmentation() { - return Maps.unmodifiableBiMap(this.typeToAugmentation); + public Multimap getTypeToAugmentations() { + return Multimaps.unmodifiableMultimap(this.typeToAugmentations); } public BiMap getTargetToAugmentation() { @@ -178,10 +178,14 @@ public final class ModuleContext { } public void addTypeToAugmentation(final GeneratedTypeBuilder builder, final AugmentationSchemaNode schema) { - this.typeToAugmentation.put(builder, schema); this.typeToSchema.put(builder, schema); } + public void addTypeToAugmentations(final GeneratedTypeBuilder builder, + final List schemaList) { + schemaList.forEach(augmentNode -> this.typeToAugmentations.put(builder, augmentNode)); + } + public void addTargetToAugmentation(final GeneratedTypeBuilder builder, final SchemaPath augmentTarget) { this.targetToAugmentation.put(augmentTarget, builder); } diff --git a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java index 3b46518e7c..fa4371c29c 100644 --- a/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java +++ b/binding2/mdsal-binding2-generator-impl/src/main/java/org/opendaylight/mdsal/binding/javav2/generator/impl/GenHelperUtil.java @@ -444,11 +444,13 @@ final class GenHelperUtil { augmentBuilders.put(augTypeBuilder.getName(), augTypeBuilder); + if(!augSchema.getChildNodes().isEmpty()) { genCtx.get(module).addTypeToAugmentation(augTypeBuilder, augSchema); genCtx.get(module).addTargetToAugmentation(augTypeBuilder, augSchema.getTargetPath()); } genCtx.get(module).addAugmentType(augTypeBuilder); + genCtx.get(module).addTypeToAugmentations(augTypeBuilder, schemaPathAugmentListEntry); return genCtx; } diff --git a/binding2/mdsal-binding2-runtime/src/main/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContext.java b/binding2/mdsal-binding2-runtime/src/main/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContext.java index caf9fcaa63..f82fc422f2 100755 --- a/binding2/mdsal-binding2-runtime/src/main/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContext.java +++ b/binding2/mdsal-binding2-runtime/src/main/java/org/opendaylight/mdsal/binding/javav2/runtime/context/BindingRuntimeContext.java @@ -24,6 +24,7 @@ import java.util.AbstractMap.SimpleEntry; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; @@ -88,8 +89,7 @@ public class BindingRuntimeContext implements Immutable { private static final char DOT = '.'; private final ClassLoadingStrategy strategy; private final SchemaContext schemaContext; - - private final Map augmentationToSchema = new HashMap<>(); + private final Multimap augmentationToSchema = HashMultimap.create(); private final BiMap typeToDefiningSchema = HashBiMap.create(); private final Multimap choiceToCases = HashMultimap.create(); private final Map identities = new HashMap<>(); @@ -116,7 +116,7 @@ public class BindingRuntimeContext implements Immutable { final Map modules = generator.getModuleContexts(this.schemaContext); for (final ModuleContext ctx : modules.values()) { - this.augmentationToSchema.putAll(ctx.getTypeToAugmentation()); + this.augmentationToSchema.putAll(ctx.getTypeToAugmentations()); this.typeToDefiningSchema.putAll(ctx.getTypeToSchema()); ctx.getTypedefs(); @@ -184,8 +184,11 @@ public class BindingRuntimeContext implements Immutable { * @throws IllegalArgumentException * - if supplied class is not an augmentation */ - public @Nullable AugmentationSchemaNode getAugmentationDefinition(final Class augClass) { - Preconditions.checkArgument(Augmentation.class.isAssignableFrom(augClass), "Class %s does not represent augmentation", augClass); + + public @Nullable Collection getAugmentationDefinition(final Class augClass) + throws IllegalArgumentException { + Preconditions.checkArgument(Augmentation.class.isAssignableFrom(augClass), + "Class %s does not represent augmentation", augClass); return this.augmentationToSchema.get(referencedType(augClass)); } @@ -221,8 +224,8 @@ public class BindingRuntimeContext implements Immutable { */ public Entry getResolvedAugmentationSchema( final DataNodeContainer target, final Class> aug) { - final AugmentationSchemaNode origSchema = getAugmentationDefinition(aug); - Preconditions.checkArgument(origSchema != null, "Augmentation %s is not known in current schema context",aug); + final Collection origSchemas = getAugmentationDefinition(aug); + Preconditions.checkArgument(origSchemas != null, "Augmentation %s is not known in current schema context",aug); /* * FIXME: Validate augmentation schema lookup * @@ -237,24 +240,27 @@ public class BindingRuntimeContext implements Immutable { */ final Set childNames = new HashSet<>(); final Set realChilds = new HashSet<>(); - for (final DataSchemaNode child : origSchema.getChildNodes()) { - final DataSchemaNode dataChildQNname = target.getDataChildByName(child.getQName()); - final String childLocalName = child.getQName().getLocalName(); - if (dataChildQNname == null) { - for (final DataSchemaNode dataSchemaNode : target.getChildNodes()) { - if (childLocalName.equals(dataSchemaNode.getQName().getLocalName())) { - realChilds.add(dataSchemaNode); - childNames.add(dataSchemaNode.getQName()); + for (final AugmentationSchemaNode origSchema : origSchemas) { + for (final DataSchemaNode child : origSchema.getChildNodes()) { + final DataSchemaNode dataChildQNname = target.getDataChildByName(child.getQName()); + final String childLocalName = child.getQName().getLocalName(); + if (dataChildQNname == null) { + for (final DataSchemaNode dataSchemaNode : target.getChildNodes()) { + if (childLocalName.equals(dataSchemaNode.getQName().getLocalName())) { + realChilds.add(dataSchemaNode); + childNames.add(dataSchemaNode.getQName()); + } } + } else { + realChilds.add(dataChildQNname); + childNames.add(child.getQName()); } - } else { - realChilds.add(dataChildQNname); - childNames.add(child.getQName()); } } final AugmentationIdentifier identifier = new AugmentationIdentifier(childNames); - final AugmentationSchemaNode proxy = new EffectiveAugmentationSchema(origSchema, realChilds); + final AugmentationSchemaNode proxy = new EffectiveAugmentationSchema(origSchemas.stream().findFirst().get(), + realChilds); return new SimpleEntry<>(identifier, proxy); } -- 2.36.6