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;h=aeda45c9904f30634b031f8169e76acc46347e53;hb=c2e5e43b8981ab3522e0f4d4f305942d2f15ac88;hp=0d91bd9551c93be8a672c1a223bf68e03983ba9e;hpb=7b07332934883077d2ee17f2a47c38442b00c868;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 0d91bd9551..aeda45c990 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 @@ -7,34 +7,30 @@ */ package org.opendaylight.yangtools.sal.binding.generator.impl; -import com.google.common.base.Objects; import com.google.common.collect.ImmutableList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; -import java.util.HashMap; +import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; -import org.eclipse.xtext.xbase.lib.Functions.Function0; import org.opendaylight.yangtools.concepts.Identifiable; -import org.opendaylight.yangtools.sal.binding.generator.impl.CodecTypeUtils; import org.opendaylight.yangtools.yang.binding.Augmentation; import org.opendaylight.yangtools.yang.binding.DataObject; -import org.opendaylight.yangtools.yang.binding.Identifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.IdentifiableItem; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.Item; import org.opendaylight.yangtools.yang.binding.util.BindingReflections; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.api.Node; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; +import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument; import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl; import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl; import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry; @@ -49,6 +45,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>>()); @@ -58,12 +56,12 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { @Override public InstanceIdentifier deserialize( - org.opendaylight.yangtools.yang.data.api.InstanceIdentifier input) { + final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier input) { Class baType = null; - List biArgs = input.getPath(); - List scannedPath = new ArrayList<>(biArgs.size()); - List baArgs = new ArrayList(biArgs.size()); - for (org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument biArg : biArgs) { + Iterable biArgs = input.getPathArguments(); + List scannedPath = new ArrayList<>(); + List baArgs = new ArrayList(); + for (org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument biArg : biArgs) { scannedPath.add(biArg.getNodeType()); org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument baArg = deserializePathArgument( @@ -73,6 +71,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)); @@ -80,24 +79,36 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { } baArgs.add(baArg); } - InstanceIdentifier ret = new InstanceIdentifier(baArgs, (Class) baType); + InstanceIdentifier ret = InstanceIdentifier.create(baArgs); LOG.debug("DOM Instance Identifier {} deserialized to {}", input, ret); return ret; } - private org.opendaylight.yangtools.yang.binding.InstanceIdentifier.PathArgument _deserializePathArgument( + @Override + public InstanceIdentifier deserialize( + final org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier input, + final InstanceIdentifier bindingIdentifier) { + return deserialize(input); + } + + 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) { @@ -106,7 +117,7 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { return CodecTypeUtils.newIdentifiableItem(type, value); } - public CompositeNode toCompositeNode(NodeIdentifierWithPredicates predicates) { + public CompositeNode toCompositeNode(final NodeIdentifierWithPredicates predicates) { Set> keyValues = predicates.getKeyValues().entrySet(); List> values = new ArrayList<>(keyValues.size()); for (Map.Entry keyValue : keyValues) { @@ -116,46 +127,59 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { } @Override - public org.opendaylight.yangtools.yang.data.api.InstanceIdentifier serialize(InstanceIdentifier input) { + public org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier 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( - components); + org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier ret = + org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.create(components); LOG.debug("Binding Instance Identifier {} serialized to DOM InstanceIdentifier {}", input, ret); return ret; } - public Class updateAugmentationInjection(Class class1, - ImmutableList list, Class augmentation) { + 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 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(Item argument, 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()))) { @@ -164,16 +188,21 @@ public class InstanceIdentifierCodecImpl implements InstanceIdentifierCodec { return new NodeIdentifier(QName.create(previousQname, qname.getLocalName())); } - private PathArgument _serializePathArgument(IdentifiableItem argument, QName previousQname) { - Map predicates = new HashMap<>(); + private PathArgument serializeIdentifiableItem(final IdentifiableItem argument, final QName previousQname) { + @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); + + final Map predicates = new LinkedHashMap<>(); for (Node outputValue : compositeOutput.getValue()) { predicates.put(outputValue.getNodeType(), outputValue.getValue()); } @@ -186,26 +215,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.YangInstanceIdentifier.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()); } } + + }