X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=code-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fbinding%2Fgenerator%2Fimpl%2FInstanceIdentifierCodecImpl.java;fp=code-generator%2Fbinding-generator-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fsal%2Fbinding%2Fgenerator%2Fimpl%2FInstanceIdentifierCodecImpl.java;h=fda3f068ac8faed5c341ae19083d36ef805f006d;hb=0d81e2938fbdb6c0fabbc6f3a4a1b01de35f12a5;hp=9fac9571c23f2124b36a087da7fd312a7517c075;hpb=c20ed2935f494fbe228ca75262953cb76d8cc801;p=mdsal.git diff --git a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/InstanceIdentifierCodecImpl.java b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/InstanceIdentifierCodecImpl.java index 9fac9571c2..fda3f068ac 100644 --- a/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/InstanceIdentifierCodecImpl.java +++ b/code-generator/binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/InstanceIdentifierCodecImpl.java @@ -11,6 +11,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -46,6 +47,8 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { private final CodecRegistry codecRegistry; + private final Map,Set>> augmentationAdapted = new WeakHashMap<>(); + private final Map, Map, Class>> classToPreviousAugment = Collections .synchronizedMap(new WeakHashMap, Map, Class>>()); @@ -70,6 +73,7 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { } Map, Class> injectAugment = classToPreviousAugment.get(baType); if (injectAugment != null) { + @SuppressWarnings("unchecked") Class augment = (Class) injectAugment.get(scannedPath); if (augment != null) { baArgs.add(new Item(augment)); @@ -77,7 +81,7 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { } baArgs.add(baArg); } - InstanceIdentifier ret = InstanceIdentifier.create(baArgs); + InstanceIdentifier ret = InstanceIdentifier.create(baArgs); LOG.debug("DOM Instance Identifier {} deserialized to {}", input, ret); return ret; } @@ -85,23 +89,28 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { @Override public InstanceIdentifier deserialize( final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier input, - InstanceIdentifier bindingIdentifier) { + final InstanceIdentifier bindingIdentifier) { return deserialize(input); } - private org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument _deserializePathArgument( + private InstanceIdentifier.PathArgument deserializeNodeIdentifier( final NodeIdentifier argument, final List processedPath) { + @SuppressWarnings("rawtypes") final Class cls = codecRegistry.getClassForPath(processedPath); + @SuppressWarnings("unchecked") Item item = new Item<>(cls); return item; } - private org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument _deserializePathArgument( + private InstanceIdentifier.PathArgument deserializeNodeIdentifierWithPrecicates( final NodeIdentifierWithPredicates argument, final List processedPath) { + @SuppressWarnings("rawtypes") final Class type = codecRegistry.getClassForPath(processedPath); + @SuppressWarnings({ "unchecked", "rawtypes" }) final IdentifierCodec codec = codecRegistry .> getIdentifierCodecForIdentifiable(type); CompositeNode _compositeNode = this.toCompositeNode(argument); + @SuppressWarnings("unchecked") ValueWithQName deserialize = codec.deserialize(_compositeNode); Object value = null; if (deserialize != null) { @@ -122,27 +131,21 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { @Override public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier serialize(final InstanceIdentifier input) { Class previousAugmentation = null; - List pathArgs = input.getPath(); + Iterable pathArgs = input.getPathArguments(); QName previousQName = null; - List components = new ArrayList<>(pathArgs.size()); - List qnamePath = new ArrayList<>(pathArgs.size()); + List components = new ArrayList<>(); + List qnamePath = new ArrayList<>(); for (InstanceIdentifier.PathArgument baArg : pathArgs) { - if (!Augmentation.class.isAssignableFrom(baArg.getType())) { - org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument biArg = serializePathArgument( - baArg, previousQName); - previousQName = biArg.getNodeType(); + PathArgument biArg = serializePathArgumentAndUpdateMapping(qnamePath, baArg, previousQName,previousAugmentation); components.add(biArg); qnamePath.add(biArg.getNodeType()); - ImmutableList immutableList = ImmutableList.copyOf(qnamePath); - codecRegistry.putPathToClass(immutableList, baArg.getType()); - if (previousAugmentation != null) { - updateAugmentationInjection(baArg.getType(), immutableList, previousAugmentation); - } + previousQName = biArg.getNodeType(); previousAugmentation = null; } else { previousQName = codecRegistry.getQNameForAugmentation(baArg.getType()); previousAugmentation = baArg.getType(); + ensureAugmentation(qnamePath,previousQName,baArg.getType()); } } org.opendaylight.yangtools.yang.data.api.InstanceIdentifier ret = new org.opendaylight.yangtools.yang.data.api.InstanceIdentifier( @@ -151,15 +154,34 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { return ret; } + private synchronized void ensureAugmentation(final List augPath, final QName augQName, final Class type) { + Set> augPotential = augmentationAdapted.get(type); + if(augPotential == null) { + augPotential = new HashSet<>(); + augmentationAdapted.put(type, augPotential); + } + ImmutableList augTargetPath = ImmutableList.copyOf(augPath); + if(augPotential.contains(augPath)) { + return; + } + + for(Class child : BindingReflections.getChildrenClasses(type)) { + Item baArg = new Item<>(child); + PathArgument biArg = serializePathArgumentAndUpdateMapping(augPath, baArg, augQName,type); + } + augPotential.add(augTargetPath); + } + + public Class updateAugmentationInjection(final Class class1, - final ImmutableList list, final Class augmentation) { + final List list, final Class augmentation) { if (classToPreviousAugment.get(class1) == null) { classToPreviousAugment.put(class1, new ConcurrentHashMap, Class>()); } return classToPreviousAugment.get(class1).put(list, augmentation); } - private PathArgument _serializePathArgument(final Item argument, final QName previousQname) { + private PathArgument serializeItem(final Item argument, final QName previousQname) { Class type = argument.getType(); QName qname = BindingReflections.findQName(type); if (previousQname == null || (BindingReflections.isAugmentationChild(argument.getType()))) { @@ -168,15 +190,19 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { return new NodeIdentifier(QName.create(previousQname, qname.getLocalName())); } - private PathArgument _serializePathArgument(final IdentifiableItem argument, final QName previousQname) { + private PathArgument serializeIdentifiableItem(final IdentifiableItem argument, final QName previousQname) { Map predicates = new HashMap<>(); + @SuppressWarnings("rawtypes") Class type = argument.getType(); + @SuppressWarnings("unchecked") IdentifierCodec keyCodec = codecRegistry.getIdentifierCodecForIdentifiable(type); QName qname = BindingReflections.findQName(type); if (previousQname != null && !(BindingReflections.isAugmentationChild(argument.getType()))) { qname = QName.create(previousQname, qname.getLocalName()); } + @SuppressWarnings({ "rawtypes", "unchecked" }) ValueWithQName combinedInput = new ValueWithQName(previousQname, argument.getKey()); + @SuppressWarnings("unchecked") CompositeNode compositeOutput = keyCodec.serialize(combinedInput); for (Node outputValue : compositeOutput.getValue()) { predicates.put(outputValue.getNodeType(), outputValue.getValue()); @@ -190,26 +216,40 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { private org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument deserializePathArgument( final PathArgument argument, final List processedPath) { if (argument instanceof NodeIdentifier) { - return _deserializePathArgument((NodeIdentifier) argument, processedPath); + return deserializeNodeIdentifier((NodeIdentifier) argument, processedPath); } else if (argument instanceof NodeIdentifierWithPredicates) { - return _deserializePathArgument((NodeIdentifierWithPredicates) argument, processedPath); + return deserializeNodeIdentifierWithPrecicates((NodeIdentifierWithPredicates) argument, processedPath); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays. asList(argument, processedPath).toString()); } } + private PathArgument serializePathArgumentAndUpdateMapping(final List parentPath, final InstanceIdentifier.PathArgument baArg, final QName previousQName, final Class previousAugmentation) { + org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument biArg = serializePathArgument(baArg, previousQName); + List qnamePath = new ArrayList<>(parentPath); + qnamePath.add(biArg.getNodeType()); + ImmutableList currentPath = ImmutableList.copyOf(qnamePath); + codecRegistry.putPathToClass(currentPath, baArg.getType()); + if (previousAugmentation != null) { + updateAugmentationInjection(baArg.getType(), currentPath, previousAugmentation); + } + return biArg; + } + private PathArgument serializePathArgument( - final org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument argument, + final InstanceIdentifier.PathArgument argument, final QName previousQname) { if (argument instanceof IdentifiableItem) { - return _serializePathArgument((IdentifiableItem) argument, previousQname); + return serializeIdentifiableItem((IdentifiableItem) argument, previousQname); } else if (argument instanceof Item) { - return _serializePathArgument((Item) argument, previousQname); + return serializeItem((Item) argument, previousQname); } else { throw new IllegalArgumentException("Unhandled parameter types: " + Arrays. asList(argument, previousQname).toString()); } } + + }